1 /*
   2  * Copyright (c) 2019, 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 sun.java2d.metal;
  27 
  28 import sun.awt.AWTAccessor;
  29 import sun.awt.AWTAccessor.ComponentAccessor;
  30 import sun.awt.image.SunVolatileImage;
  31 import sun.awt.image.VolatileSurfaceManager;
  32 import sun.java2d.BackBufferCapsProvider;
  33 import sun.java2d.SurfaceData;
  34 import sun.java2d.opengl.OGLSurfaceData;
  35 import sun.java2d.pipe.hw.ExtendedBufferCapabilities;
  36 
  37 import java.awt.*;
  38 import java.awt.image.ColorModel;
  39 import java.awt.peer.ComponentPeer;
  40 
  41 import static java.awt.BufferCapabilities.FlipContents.COPIED;
  42 //import static sun.java2d.opengl.OGLContext.OGLContextCaps.CAPS_EXT_FBOBJECT;
  43 import static sun.java2d.pipe.hw.ExtendedBufferCapabilities.VSyncType.VSYNC_ON;
  44 
  45 public class MTLVolatileSurfaceManager extends VolatileSurfaceManager {
  46 
  47     private final boolean accelerationEnabled;
  48 
  49     public MTLVolatileSurfaceManager(SunVolatileImage vImg, Object context) {
  50         super(vImg, context);
  51 
  52         /*
  53          * We will attempt to accelerate this image only under the
  54          * following conditions:
  55          *   - the image is not bitmask AND the GraphicsConfig supports the FBO
  56          *     extension
  57          */
  58         int transparency = vImg.getTransparency();
  59         MTLGraphicsConfig gc = (MTLGraphicsConfig) vImg.getGraphicsConfig();
  60         accelerationEnabled = true;
  61                 //gc.isCapPresent(CAPS_EXT_FBOBJECT)
  62                 //&& transparency != Transparency.BITMASK;
  63     }
  64 
  65     protected boolean isAccelerationEnabled() {
  66         return accelerationEnabled;
  67     }
  68 
  69     /**
  70      * Create a FBO-based SurfaceData object (or init the backbuffer
  71      * of an existing window if this is a double buffered GraphicsConfig)
  72      */
  73     protected SurfaceData initAcceleratedSurface() {
  74         SurfaceData sData = null;
  75         Component comp = vImg.getComponent();
  76         final ComponentAccessor acc = AWTAccessor.getComponentAccessor();
  77         final ComponentPeer peer = (comp != null) ? acc.getPeer(comp) : null;
  78 
  79         try {
  80             boolean createVSynced = false;
  81             boolean forceback = false;
  82             if (context instanceof Boolean) {
  83                 forceback = ((Boolean)context).booleanValue();
  84                 if (forceback && peer instanceof BackBufferCapsProvider) {
  85                     BackBufferCapsProvider provider =
  86                         (BackBufferCapsProvider)peer;
  87                     BufferCapabilities caps = provider.getBackBufferCaps();
  88                     if (caps instanceof ExtendedBufferCapabilities) {
  89                         ExtendedBufferCapabilities ebc =
  90                             (ExtendedBufferCapabilities)caps;
  91                         if (ebc.getVSync() == VSYNC_ON &&
  92                             ebc.getFlipContents() == COPIED)
  93                         {
  94                             createVSynced = true;
  95                             forceback = false;
  96                         }
  97                     }
  98                 }
  99             }
 100 
 101             if (forceback) {
 102                 // peer must be non-null in this case
 103                 // TODO: modify parameter to delegate
 104                 //                sData = MTLSurfaceData.createData(peer, vImg, FLIP_BACKBUFFER);
 105             } else {
 106                 MTLGraphicsConfig gc =
 107                     (MTLGraphicsConfig)vImg.getGraphicsConfig();
 108                 ColorModel cm = gc.getColorModel(vImg.getTransparency());
 109                 int type = vImg.getForcedAccelSurfaceType();
 110                 // if acceleration type is forced (type != UNDEFINED) then
 111                 // use the forced type, otherwise choose RT_TEXTURE
 112                 if (type == OGLSurfaceData.UNDEFINED) {
 113                     type = OGLSurfaceData.FBOBJECT;
 114                 }
 115                 if (createVSynced) {
 116                     // TODO: modify parameter to delegate
 117 //                  sData = MTLSurfaceData.createData(peer, vImg, type);
 118                 } else {
 119                     sData = MTLSurfaceData.createData(gc,
 120                                                       vImg.getWidth(),
 121                                                       vImg.getHeight(),
 122                                                       cm, vImg, type);
 123                 }
 124             }
 125         } catch (NullPointerException ex) {
 126             sData = null;
 127         } catch (OutOfMemoryError er) {
 128             sData = null;
 129         }
 130 
 131         return sData;
 132     }
 133 
 134     @Override
 135     protected boolean isConfigValid(GraphicsConfiguration gc) {
 136         return ((gc == null) || (gc == vImg.getGraphicsConfig()));
 137     }
 138 
 139     @Override
 140     public void initContents() {
 141         if (vImg.getForcedAccelSurfaceType() != OGLSurfaceData.TEXTURE) {
 142             super.initContents();
 143         }
 144     }
 145 }
 146