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