1 /*
   2  * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.javafx.webkit.prism;
  27 
  28 import com.sun.glass.ui.Screen;
  29 import com.sun.javafx.geom.transform.BaseTransform;
  30 import com.sun.media.jfxmedia.MediaManager;
  31 import com.sun.prism.Graphics;
  32 import com.sun.webkit.perf.WCFontPerfLogger;
  33 import com.sun.webkit.perf.WCGraphicsPerfLogger;
  34 import com.sun.webkit.graphics.*;
  35 import java.io.IOException;
  36 import java.io.InputStream;
  37 import java.nio.ByteBuffer;
  38 import java.nio.ByteOrder;
  39 
  40 public final class PrismGraphicsManager extends WCGraphicsManager {
  41 
  42     private final static float highestPixelScale;
  43     private final static BaseTransform pixelScaleTransform;
  44 
  45     static {
  46         float ps = 1f;
  47         for (Screen s : Screen.getScreens()) {
  48             ps = Math.max(s.getRecommendedOutputScaleX(), ps);
  49             ps = Math.max(s.getRecommendedOutputScaleY(), ps);
  50         }
  51         highestPixelScale = (float) Math.ceil(ps);
  52         pixelScaleTransform = BaseTransform.getScaleInstance(ps, ps);
  53     }
  54 
  55     static BaseTransform getPixelScaleTransform() {
  56         return pixelScaleTransform;
  57     }
  58 
  59     @Override public float getDevicePixelScale() {
  60         return highestPixelScale;
  61     }
  62 
  63     @Override protected WCImageDecoder getImageDecoder() {
  64         return new WCImageDecoderImpl();
  65     }
  66 
  67     @Override public WCRenderQueue createRenderQueue(WCRectangle clip,
  68                                                      boolean opaque)
  69     {
  70         return new WCRenderQueueImpl(clip, opaque);
  71     }
  72 
  73     @Override protected WCRenderQueue createBufferedContextRQ(WCImage image) {
  74         WCGraphicsContext g = new WCBufferedContext((PrismImage) image);
  75         WCRenderQueue rq = new WCRenderQueueImpl(
  76                 WCGraphicsPerfLogger.isEnabled()
  77                         ? new WCGraphicsPerfLogger(g) : g);
  78         image.setRQ(rq);
  79         return rq;
  80     }
  81 
  82     @Override protected WCFont getWCFont(String name, boolean bold, boolean italic, float size)
  83     {
  84         WCFont f = WCFontImpl.getFont(name, bold, italic, size);
  85         return WCFontPerfLogger.isEnabled() && (f != null) ? new WCFontPerfLogger(f) : f;
  86     }
  87 
  88     @Override
  89     protected WCFontCustomPlatformData createFontCustomPlatformData(
  90             InputStream inputStream) throws IOException
  91     {
  92         return new WCFontCustomPlatformDataImpl(inputStream);
  93     }
  94 
  95     @Override
  96     public WCGraphicsContext createGraphicsContext(Object platG) {
  97         WCGraphicsContext g = new WCGraphicsPrismContext((Graphics)platG);
  98         return WCGraphicsPerfLogger.isEnabled() ? new WCGraphicsPerfLogger(g) : g;
  99     }
 100 
 101     @Override public WCPageBackBuffer createPageBackBuffer() {
 102         return new WCPageBackBufferImpl(highestPixelScale);
 103     }
 104 
 105     @Override
 106     protected WCPath createWCPath() {
 107         return new WCPathImpl();
 108     }
 109 
 110     @Override
 111     protected WCPath createWCPath(WCPath path) {
 112         return new WCPathImpl((WCPathImpl)path);
 113     }
 114 
 115     @Override
 116     protected WCImage createWCImage(int w, int h) {
 117         return new WCImageImpl(w, h);
 118     }
 119 
 120     @Override
 121     protected WCImage createRTImage(int w, int h) {
 122         return new RTImage(w, h, highestPixelScale);
 123     }
 124 
 125     @Override public WCImage getIconImage(String iconURL) {
 126         return null;
 127     }
 128 
 129     @Override public Object toPlatformImage(WCImage image) {
 130         return ((WCImageImpl) image).getImage();
 131     }
 132 
 133     @Override
 134     protected WCImageFrame createFrame(int w, int h, ByteBuffer bytes) {
 135         int[] data = new int[bytes.capacity() / 4];
 136         bytes.order(ByteOrder.nativeOrder());
 137         bytes.asIntBuffer().get(data);
 138         final WCImageImpl wimg = new WCImageImpl(data, w, h);
 139 
 140         return new WCImageFrame() {
 141             public WCImage getFrame() { return wimg; }
 142         };
 143     }
 144 
 145     @Override
 146     protected WCTransform createTransform(double m00, double m10, double m01,
 147             double m11, double m02, double m12)
 148     {
 149         return new WCTransform(m00, m10, m01, m11, m02, m12);
 150     }
 151 
 152     @Override
 153     protected String[] getSupportedMediaTypes() {
 154         String[] types = MediaManager.getSupportedContentTypes();
 155         // RT-19949: disable FLV support (workaround for youtube):
 156         // if browser reports support for video/x-flv, youtube player sets
 157         // media source to FLV (H264+AAC) stream and does not switch to MP4 on error
 158         int len = types.length;
 159         for (int i=0; i<len; i++) {
 160             if ("video/x-flv".compareToIgnoreCase(types[i]) == 0) {
 161                 System.arraycopy(types, i+1, types, i, len-(i+1));
 162                 len--;
 163             }
 164         }
 165         if (len < types.length) {
 166             String[] trimmedArray = new String[len];
 167             System.arraycopy(types, 0, trimmedArray, 0, len);
 168             types = trimmedArray;
 169         }
 170         return types;
 171     }
 172 
 173     @Override
 174     protected WCMediaPlayer createMediaPlayer() {
 175         return new WCMediaPlayerImpl();
 176     }
 177 }