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