1 /*
   2  * Copyright (c) 2010, 2014, 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.prism.j2d;
  27 
  28 import com.sun.glass.ui.Screen;
  29 import com.sun.prism.Image;
  30 import com.sun.prism.MediaFrame;
  31 import com.sun.prism.Mesh;
  32 import com.sun.prism.MeshView;
  33 import com.sun.prism.PhongMaterial;
  34 import com.sun.prism.PixelFormat;
  35 import com.sun.prism.Presentable;
  36 import com.sun.prism.PresentableState;
  37 import com.sun.prism.RTTexture;
  38 import com.sun.prism.Texture;
  39 import com.sun.prism.Texture.Usage;
  40 import com.sun.prism.Texture.WrapMode;
  41 import com.sun.prism.impl.BaseResourceFactory;
  42 import com.sun.prism.impl.TextureResourcePool;
  43 import com.sun.prism.impl.VertexBuffer;
  44 import com.sun.prism.impl.shape.BasicShapeRep;
  45 import com.sun.prism.shape.ShapeRep;
  46 import java.util.Map;
  47 import java.util.WeakHashMap;
  48 
  49 class J2DResourceFactory extends BaseResourceFactory
  50 {
  51     private static final Map<Image,Texture> clampTexCache = new WeakHashMap<>();
  52     private static final Map<Image,Texture> repeatTexCache = new WeakHashMap<>();
  53     private static final Map<Image,Texture> mipmapTexCache = new WeakHashMap<>();
  54 
  55     private Screen screen;
  56 
  57     J2DResourceFactory(Screen screen) {
  58         super(clampTexCache, repeatTexCache, mipmapTexCache);
  59         this.screen = screen;
  60     }
  61 
  62     J2DPrismGraphics createJ2DPrismGraphics(J2DPresentable target,
  63                                             java.awt.Graphics2D g2d) {
  64         return new J2DPrismGraphics(target, g2d);
  65     }
  66 
  67     public TextureResourcePool getTextureResourcePool() {
  68         return J2DTexturePool.instance;
  69     }
  70 
  71     Screen getScreen() {
  72         return screen;
  73     }
  74 
  75     private static ShapeRep theRep = new BasicShapeRep();
  76 
  77     public ShapeRep createArcRep() {
  78         return theRep;
  79     }
  80 
  81     public ShapeRep createEllipseRep() {
  82         return theRep;
  83     }
  84 
  85     public ShapeRep createRoundRectRep() {
  86         return theRep;
  87     }
  88 
  89     public ShapeRep createPathRep() {
  90         return theRep;
  91     }
  92 
  93     public Presentable createPresentable(PresentableState pState) {
  94         return J2DPresentable.create(pState, this);
  95     }
  96 
  97     public int getRTTWidth(int w, WrapMode wrapMode) {
  98         return w;
  99     }
 100 
 101     public int getRTTHeight(int h, WrapMode wrapMode) {
 102         return h;
 103     }
 104 
 105     @Override
 106     public RTTexture createRTTexture(int width, int height, Texture.WrapMode wrapMode, boolean msaa) {
 107         return createRTTexture(width, height, wrapMode);
 108     }
 109 
 110     public RTTexture createRTTexture(int width, int height, WrapMode wrapMode) {
 111         J2DTexturePool pool = J2DTexturePool.instance;
 112         long size = pool.estimateRTTextureSize(width, height, false);
 113         if (!pool.prepareForAllocation(size)) {
 114             return null;
 115         }
 116         return new J2DRTTexture(width, height, this);
 117     }
 118 
 119     public Texture createTexture(PixelFormat formatHint,
 120                                  Usage usageHint, WrapMode wrapMode,
 121                                  int w, int h)
 122     {
 123         return J2DTexture.create(formatHint, wrapMode, w, h);
 124     }
 125 
 126     public Texture createTexture(PixelFormat formatHint,
 127             Usage usageHint, WrapMode wrapMode, int w, int h, boolean useMipmap) {
 128         return createTexture(formatHint, usageHint, wrapMode, w, h);
 129     }
 130 
 131     public Texture createTexture(MediaFrame vdb) {
 132         Texture tex;
 133 
 134         vdb.holdFrame();
 135 
 136         if (vdb.getPixelFormat() != PixelFormat.INT_ARGB_PRE) {
 137             MediaFrame newFrame = vdb.convertToFormat(PixelFormat.INT_ARGB_PRE);
 138             vdb.releaseFrame();
 139             vdb = newFrame;
 140             if (null == vdb) {
 141                 // FIXME: error condition?
 142                 return null;
 143             }
 144         }
 145 
 146         tex = J2DTexture.create(vdb.getPixelFormat(), WrapMode.CLAMP_TO_EDGE,
 147                                 vdb.getWidth(), vdb.getHeight());
 148         vdb.releaseFrame();
 149         return tex;
 150     }
 151 
 152     @Override
 153     public boolean isCompatibleTexture(Texture tex) {
 154         return tex instanceof J2DTexture;
 155     }
 156 
 157     @Override
 158     protected boolean canClampToZero() {
 159         return false;
 160     }
 161 
 162     public int getMaximumTextureSize() {
 163         return Integer.MAX_VALUE;
 164     }
 165 
 166     public boolean isFormatSupported(PixelFormat format) {
 167         switch (format) {
 168             case BYTE_RGB:
 169             case BYTE_GRAY:
 170             case INT_ARGB_PRE:
 171             case BYTE_BGRA_PRE:
 172                 return true;
 173             case BYTE_ALPHA:
 174             case BYTE_APPLE_422:
 175             case MULTI_YCbCr_420:
 176             case FLOAT_XYZW:
 177             default:
 178                 return false;
 179         }
 180     }
 181 
 182     public VertexBuffer createVertexBuffer(int maxQuads) {
 183         // This is only used by ES1 and ES2 - it should perhaps be
 184         // moved to an ES-specific subclass of ResourceFactory?
 185         throw new UnsupportedOperationException("Not supported yet.");
 186     }
 187 
 188     public void dispose() {
 189     }
 190 
 191     public PhongMaterial createPhongMaterial() {
 192         throw new UnsupportedOperationException("Not supported yet.");
 193 }
 194 
 195     public MeshView createMeshView(Mesh mesh) {
 196         throw new UnsupportedOperationException("Not supported yet.");
 197     }
 198 
 199     public Mesh createMesh() {
 200         throw new UnsupportedOperationException("Not supported yet.");
 201     }
 202 }