1 /*
   2  * Copyright (c) 2011, 2018, 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.webkit.graphics;
  27 
  28 import com.sun.javafx.logging.PlatformLogger;
  29 import com.sun.webkit.SharedBuffer;
  30 import com.sun.webkit.SimpleSharedBufferInputStream;
  31 import java.io.IOException;
  32 import java.io.InputStream;
  33 import java.nio.ByteBuffer;
  34 import java.util.HashMap;
  35 import java.util.MissingResourceException;
  36 import java.util.ResourceBundle;
  37 import java.util.concurrent.atomic.AtomicInteger;
  38 
  39 public abstract class WCGraphicsManager {
  40 
  41     private static final PlatformLogger logger =
  42             PlatformLogger.getLogger(WCGraphicsManager.class.getName());
  43 
  44     private final AtomicInteger idCount = new AtomicInteger(0);
  45 
  46     private final HashMap<Integer,Ref> refMap = new HashMap<Integer,Ref>();
  47 
  48     private static ResourceBundle imageProperties = null;
  49     private static WCGraphicsManager manager = null;
  50 
  51     public static void setGraphicsManager(WCGraphicsManager manager) {
  52         WCGraphicsManager.manager = manager;
  53     }
  54 
  55     public static WCGraphicsManager getGraphicsManager() {
  56         return manager;
  57     }
  58 
  59     public abstract float getDevicePixelScale();
  60 
  61     protected abstract WCImageDecoder getImageDecoder();
  62 
  63     public abstract WCGraphicsContext createGraphicsContext(Object g);
  64 
  65     public abstract WCRenderQueue createRenderQueue(WCRectangle clip,
  66                                                     boolean opaque);
  67 
  68     protected abstract WCRenderQueue createBufferedContextRQ(WCImage image);
  69 
  70     public abstract WCPageBackBuffer createPageBackBuffer();
  71 
  72     protected abstract WCFont getWCFont(String name, boolean bold, boolean italic, float size);
  73 
  74     private WCFontCustomPlatformData fwkCreateFontCustomPlatformData(
  75             SharedBuffer sharedBuffer)
  76     {
  77         try {
  78             return createFontCustomPlatformData(
  79                     new SimpleSharedBufferInputStream(sharedBuffer));
  80         } catch (IOException ex) {
  81             logger.finest("Error creating font custom platform data", ex);
  82             return null;
  83         }
  84     }
  85 
  86     protected abstract WCFontCustomPlatformData createFontCustomPlatformData(
  87             InputStream inputStream) throws IOException;
  88 
  89     protected abstract WCPath createWCPath();
  90 
  91     protected abstract WCPath createWCPath(WCPath path);
  92 
  93     protected abstract WCImage createWCImage(int w, int h);
  94 
  95     protected abstract WCImage createRTImage(int w, int h);
  96 
  97     public abstract WCImage getIconImage(String iconURL);
  98 
  99     public abstract Object toPlatformImage(WCImage image);
 100 
 101     protected abstract WCImageFrame createFrame(int w, int h, ByteBuffer data);
 102 
 103     public static String getResourceName(String key) {
 104         if (imageProperties == null) {
 105             imageProperties = ResourceBundle.getBundle(
 106                     "com.sun.webkit.graphics.Images");
 107         }
 108         try {
 109             return imageProperties.getString(key);
 110         }
 111         catch (MissingResourceException exception) {
 112             return key;
 113         }
 114     }
 115 
 116     private void fwkLoadFromResource(String key, long bufPtr) {
 117         InputStream in = getClass().getResourceAsStream(getResourceName(key));
 118         if (in == null) {
 119             return;
 120         }
 121 
 122         byte[] buf = new byte[1024]; // big enough for most resources
 123         int count;
 124         try {
 125             while ((count = in.read(buf)) > -1) {
 126                 append(bufPtr, buf, count);
 127             }
 128             in.close();
 129         } catch (IOException e) {
 130             // just use what we've read so far
 131         }
 132     }
 133 
 134     protected abstract WCTransform createTransform(double m00, double m10,
 135             double m01, double m11, double m02, double m12);
 136 
 137     protected String[] getSupportedMediaTypes() {
 138         // default implementation: nothing is supported
 139         return new String[0];
 140     }
 141 
 142     private WCMediaPlayer fwkCreateMediaPlayer(long nativePointer) {
 143         WCMediaPlayer mediaPlayer = createMediaPlayer();
 144         mediaPlayer.setNativePointer(nativePointer);
 145         return mediaPlayer;
 146     }
 147 
 148     protected abstract WCMediaPlayer createMediaPlayer();
 149 
 150     int createID() {
 151         return idCount.incrementAndGet();
 152     }
 153 
 154     synchronized void ref(Ref ref) {
 155         refMap.put(ref.getID(), ref);
 156     }
 157 
 158     synchronized Ref deref(Ref ref) {
 159         return refMap.remove(ref.getID());
 160     }
 161 
 162     synchronized Ref getRef(int id) {
 163         return refMap.get(id);
 164     }
 165 
 166     private static native void append(long bufPtr, byte[] data, int count);
 167 }