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