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.prism.sw;
  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.ResourceFactory;
  38 import com.sun.prism.RTTexture;
  39 import com.sun.prism.Texture;
  40 import com.sun.prism.Texture.Usage;
  41 import com.sun.prism.Texture.WrapMode;
  42 import com.sun.prism.impl.BaseResourceFactory;
  43 import com.sun.prism.impl.PrismSettings;
  44 import com.sun.prism.impl.TextureResourcePool;
  45 import com.sun.prism.impl.VertexBuffer;
  46 import com.sun.prism.impl.shape.BasicRoundRectRep;
  47 import com.sun.prism.impl.shape.BasicShapeRep;
  48 import com.sun.prism.shape.ShapeRep;
  49 import java.util.Map;
  50 import java.util.WeakHashMap;
  51 
  52 final class SWResourceFactory
  53     extends BaseResourceFactory
  54         implements ResourceFactory {
  55 
  56     private static final Map<Image,Texture> clampTexCache = new WeakHashMap<>();
  57     private static final Map<Image,Texture> repeatTexCache = new WeakHashMap<>();
  58     private static final Map<Image,Texture> mipmapTexCache = new WeakHashMap<>();
  59 
  60     private static final ShapeRep theRep = new BasicShapeRep();
  61     private static final ShapeRep rectRep = new BasicRoundRectRep();
  62 
  63     private Screen screen;
  64     private final SWContext context;
  65 
  66     public SWResourceFactory(Screen screen) {
  67         super(clampTexCache, repeatTexCache, mipmapTexCache);
  68         this.screen = screen;
  69         this.context = new SWContext(this);
  70     }
  71 
  72     public TextureResourcePool getTextureResourcePool() {
  73         return SWTexturePool.instance;
  74     }
  75 
  76     public Screen getScreen() {
  77         return screen;
  78     }
  79 
  80     SWContext getContext() {
  81         return context;
  82     }
  83     
  84     @Override public void dispose() {
  85         context.dispose();
  86     }
  87 
  88     @Override public ShapeRep createArcRep() {
  89         return theRep;
  90     }
  91     
  92     @Override public ShapeRep createEllipseRep() {
  93         return theRep;
  94     }
  95     
  96     @Override public ShapeRep createRoundRectRep() {
  97         return rectRep;
  98     }
  99     
 100     @Override public ShapeRep createPathRep() {
 101         return theRep;
 102     }
 103             
 104     @Override public VertexBuffer createVertexBuffer(int maxQuads) {
 105         throw new UnsupportedOperationException("createVertexBuffer:unimp");
 106     }
 107 
 108     
 109     @Override public Presentable createPresentable(PresentableState pState) {
 110         if (PrismSettings.debug) {
 111             System.out.println("+ SWRF.createPresentable()");
 112         }
 113         return new SWPresentable(pState, this);
 114     }
 115 
 116     public int getRTTWidth(int w, WrapMode wrapMode) {
 117         return w;
 118     }
 119 
 120     public int getRTTHeight(int h, WrapMode wrapMode) {
 121         return h;
 122     }
 123 
 124     @Override
 125     public boolean isCompatibleTexture(Texture tex) {
 126         return tex instanceof SWTexture;
 127     }
 128 
 129     @Override public RTTexture createRTTexture(int width, int height, WrapMode wrapMode, boolean msaa) {
 130         return createRTTexture(width, height, wrapMode);
 131     }
 132 
 133     @Override public RTTexture createRTTexture(int width, int height,
 134                                                WrapMode wrapMode)
 135     {
 136         SWTexturePool pool = SWTexturePool.instance;
 137         long size = pool.estimateRTTextureSize(width, height, false);
 138         if (!pool.prepareForAllocation(size)) {
 139             return null;
 140         }
 141         return new SWRTTexture(this, width, height);
 142     }
 143 
 144     @Override public int getMaximumTextureSize() {
 145         return Integer.MAX_VALUE;
 146     }
 147 
 148     @Override public boolean isFormatSupported(PixelFormat format) {
 149         switch (format) {
 150             case BYTE_RGB:
 151             case BYTE_GRAY:
 152             case INT_ARGB_PRE:
 153             case BYTE_BGRA_PRE:
 154                 return true;
 155             case BYTE_ALPHA:
 156             case BYTE_APPLE_422:
 157             case MULTI_YCbCr_420:
 158             case FLOAT_XYZW:
 159             default:
 160                 return false;
 161         }
 162     }
 163 
 164     @Override
 165     protected boolean canClampToZero() {
 166         return false;
 167     }
 168 
 169     @Override public Texture createTexture(MediaFrame vdb) {
 170         return new SWArgbPreTexture(this, WrapMode.CLAMP_TO_EDGE, vdb.getWidth(), vdb.getHeight());
 171     }
 172             
 173     @Override public Texture createTexture(PixelFormat formatHint,
 174                                            Usage usageHint,
 175                                            WrapMode wrapMode,
 176                                            int w, int h)
 177     {
 178         SWTexturePool pool = SWTexturePool.instance;
 179         long size = pool.estimateTextureSize(w, h, formatHint);
 180         if (!pool.prepareForAllocation(size)) {
 181             return null;
 182         }
 183         return SWTexture.create(this, formatHint, wrapMode, w, h);
 184     }
 185 
 186     @Override public Texture createTexture(PixelFormat formatHint, Usage usageHint,
 187             WrapMode wrapMode, int w, int h, boolean useMipmap) {
 188         return createTexture(formatHint, usageHint, wrapMode, w, h);
 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 }