1/*
2 * Copyright (c) 2004, 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
26package sun.java2d.opengl;
27
28import java.awt.BufferCapabilities;
29import static java.awt.BufferCapabilities.FlipContents.*;
30import java.awt.Component;
31import java.awt.GraphicsConfiguration;
32import java.awt.Transparency;
33import java.awt.image.ColorModel;
34
35import sun.awt.AWTAccessor;
36import sun.awt.AWTAccessor.ComponentAccessor;
37import sun.awt.image.SunVolatileImage;
38import sun.awt.image.VolatileSurfaceManager;
39import sun.awt.windows.WComponentPeer;
40import sun.java2d.SurfaceData;
41import static sun.java2d.opengl.OGLContext.OGLContextCaps.*;
42import static sun.java2d.pipe.hw.AccelSurface.*;
43import sun.java2d.pipe.hw.ExtendedBufferCapabilities;
44import static sun.java2d.pipe.hw.ExtendedBufferCapabilities.VSyncType.*;
45
46public class WGLVolatileSurfaceManager extends VolatileSurfaceManager {
47
48    private final boolean accelerationEnabled;
49
50    public WGLVolatileSurfaceManager(SunVolatileImage vImg, Object context) {
51        super(vImg, context);
52
53        /*
54         * We will attempt to accelerate this image only under the
55         * following conditions:
56         *   - the image is not bitmask AND the GraphicsConfig supports the FBO
57         *     extension
58         */
59        int transparency = vImg.getTransparency();
60        WGLGraphicsConfig gc = (WGLGraphicsConfig) vImg.getGraphicsConfig();
61        accelerationEnabled = 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;
75        Component comp = vImg.getComponent();
76        final ComponentAccessor acc = AWTAccessor.getComponentAccessor();
77        WComponentPeer 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) {
85                    BufferCapabilities caps = peer.getBackBufferCaps();
86                    if (caps instanceof ExtendedBufferCapabilities) {
87                        ExtendedBufferCapabilities ebc =
88                            (ExtendedBufferCapabilities)caps;
89                        if (ebc.getVSync() == VSYNC_ON &&
90                            ebc.getFlipContents() == COPIED)
91                        {
92                            createVSynced = true;
93                            forceback = false;
94                        }
95                    }
96                }
97            }
98
99            if (forceback) {
100                // peer must be non-null in this case
101                sData = WGLSurfaceData.createData(peer, vImg, FLIP_BACKBUFFER);
102            } else {
103                WGLGraphicsConfig gc =
104                    (WGLGraphicsConfig)vImg.getGraphicsConfig();
105                ColorModel cm = gc.getColorModel(vImg.getTransparency());
106                int type = vImg.getForcedAccelSurfaceType();
107                // if acceleration type is forced (type != UNDEFINED) then
108                // use the forced type, otherwise choose FBOBJECT
109                if (type == OGLSurfaceData.UNDEFINED) {
110                    type = OGLSurfaceData.FBOBJECT;
111                }
112                if (createVSynced) {
113                    sData = WGLSurfaceData.createData(peer, vImg, type);
114                } else {
115                    sData = WGLSurfaceData.createData(gc,
116                                                      vImg.getWidth(),
117                                                      vImg.getHeight(),
118                                                      cm, vImg, type);
119                }
120            }
121        } catch (NullPointerException ex) {
122            sData = null;
123        } catch (OutOfMemoryError er) {
124            sData = null;
125        }
126
127        return sData;
128    }
129
130    @Override
131    protected boolean isConfigValid(GraphicsConfiguration gc) {
132        return ((gc == null) ||
133                ((gc instanceof WGLGraphicsConfig) &&
134                 (gc == vImg.getGraphicsConfig())));
135    }
136
137    @Override
138    public void initContents() {
139        if (vImg.getForcedAccelSurfaceType() != OGLSurfaceData.TEXTURE) {
140            super.initContents();
141        }
142    }
143}
144