aboutsummaryrefslogtreecommitdiff
path: root/src/windows/classes/sun/java2d/d3d/D3DVolatileSurfaceManager.java
blob: 3dcf99b4cc0b0c63a87718307bcc3526fe8ec2c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

package sun.java2d.d3d;

import java.awt.Component;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.ColorModel;
import sun.awt.Win32GraphicsConfig;
import sun.awt.image.SunVolatileImage;
import sun.awt.image.SurfaceManager;
import sun.awt.image.VolatileSurfaceManager;
import sun.awt.windows.WComponentPeer;
import sun.java2d.InvalidPipeException;
import sun.java2d.SurfaceData;
import static sun.java2d.pipe.hw.AccelSurface.*;
import static sun.java2d.d3d.D3DContext.D3DContextCaps.*;
import sun.java2d.windows.GDIWindowSurfaceData;

public class D3DVolatileSurfaceManager
    extends VolatileSurfaceManager
{
    private boolean accelerationEnabled;
    private int restoreCountdown;

    public D3DVolatileSurfaceManager(SunVolatileImage vImg, Object context) {
        super(vImg, context);

        /*
         * We will attempt to accelerate this image only under the
         * following conditions:
         *   - the image is opaque OR
         *   - the image is translucent AND
         *       - the GraphicsConfig supports the FBO extension OR
         *       - the GraphicsConfig has a stored alpha channel
         */
        int transparency = vImg.getTransparency();
        D3DGraphicsDevice gd = (D3DGraphicsDevice)
            vImg.getGraphicsConfig().getDevice();
        accelerationEnabled =
            (transparency == Transparency.OPAQUE) ||
            (transparency == Transparency.TRANSLUCENT &&
             (gd.isCapPresent(CAPS_RT_PLAIN_ALPHA) ||
              gd.isCapPresent(CAPS_RT_TEXTURE_ALPHA)));
    }

    protected boolean isAccelerationEnabled() {
        return accelerationEnabled;
    }
    public void setAccelerationEnabled(boolean accelerationEnabled) {
        this.accelerationEnabled = accelerationEnabled;
    }

    /**
     * Create a pbuffer-based SurfaceData object (or init the backbuffer
     * of an existing window if this is a double buffered GraphicsConfig).
     */
    protected SurfaceData initAcceleratedSurface() {
        SurfaceData sData;
        Component comp = vImg.getComponent();
        WComponentPeer peer =
            (comp != null) ? (WComponentPeer)comp.getPeer() : null;

        try {
            boolean forceback = false;
            if (context instanceof Boolean) {
                forceback = ((Boolean)context).booleanValue();
            }

            if (forceback) {
                // peer must be non-null in this case
                sData = D3DSurfaceData.createData(peer, vImg);
            } else {
                D3DGraphicsConfig gc =
                    (D3DGraphicsConfig)vImg.getGraphicsConfig();
                ColorModel cm = gc.getColorModel(vImg.getTransparency());
                int type = vImg.getForcedAccelSurfaceType();
                // if acceleration type is forced (type != UNDEFINED) then
                // use the forced type, otherwise use RT_TEXTURE
                if (type == UNDEFINED) {
                    type = RT_TEXTURE;
                }
                sData = D3DSurfaceData.createData(gc,
                                                  vImg.getWidth(),
                                                  vImg.getHeight(),
                                                  cm, vImg,
                                                  type);
            }
        } catch (NullPointerException ex) {
            sData = null;
        } catch (OutOfMemoryError er) {
            sData = null;
        } catch (InvalidPipeException ipe) {
            sData = null;
        }

        return sData;
    }

    protected boolean isConfigValid(GraphicsConfiguration gc) {
        return ((gc == null) || (gc == vImg.getGraphicsConfig()));
    }

    /**
     * Set the number of iterations for restoreAcceleratedSurface to fail
     * before attempting to restore the accelerated surface.
     *
     * @see #restoreAcceleratedSurface
     * @see #handleVItoScreenOp
     */
    private synchronized void setRestoreCountdown(int count) {
        restoreCountdown = count;
    }

    /**
     * Note that we create a new surface instead of restoring
     * an old one. This will help with D3DContext revalidation.
     */
    @Override
    protected void restoreAcceleratedSurface() {
        synchronized (this) {
            if (restoreCountdown > 0) {
                restoreCountdown--;
                throw new
                    InvalidPipeException("Will attempt to restore surface " +
                                          " in " + restoreCountdown);
            }
        }

        SurfaceData sData = initAcceleratedSurface();
        if (sData != null) {
            sdAccel = sData;
        } else {
            throw new InvalidPipeException("could not restore surface");
            // REMIND: alternatively, we could try this:
//            ((D3DSurfaceData)sdAccel).restoreSurface();
        }
    }

    /**
     * We're asked to restore contents by the accelerated surface, which means
     * that it had been lost.
     */
    @Override
    public SurfaceData restoreContents() {
        acceleratedSurfaceLost();
        return super.restoreContents();
    }

    /**
     * If the destination surface's peer can potentially handle accelerated
     * on-screen rendering then it is likely that the condition which resulted
     * in VI to Screen operation is temporary, so this method sets the
     * restore countdown in hope that the on-screen accelerated rendering will
     * resume. In the meantime the backup surface of the VISM will be used.
     *
     * The countdown is needed because otherwise we may never break out
     * of "do { vi.validate()..} while(vi.lost)" loop since validate() could
     * restore the source surface every time and it will get lost again on the
     * next copy attempt, and we would never get a chance to use the backup
     * surface. By using the countdown we allow the backup surface to be used
     * while the screen surface gets sorted out, or if it for some reason can
     * never be restored.
     *
     * If the destination surface's peer could never do accelerated onscreen
     * rendering then the acceleration for the SurfaceManager associated with
     * the source surface is disabled forever.
     */
    static void handleVItoScreenOp(SurfaceData src, SurfaceData dst) {
        if (src instanceof D3DSurfaceData &&
            dst instanceof GDIWindowSurfaceData)
        {
            D3DSurfaceData d3dsd = (D3DSurfaceData)src;
            SurfaceManager mgr =
                SurfaceManager.getManager((Image)d3dsd.getDestination());
            if (mgr instanceof D3DVolatileSurfaceManager) {
                D3DVolatileSurfaceManager vsm = (D3DVolatileSurfaceManager)mgr;
                if (vsm != null) {
                    d3dsd.setSurfaceLost(true);

                    GDIWindowSurfaceData wsd = (GDIWindowSurfaceData)dst;
                    WComponentPeer p = wsd.getPeer();
                    if (D3DScreenUpdateManager.canUseD3DOnScreen(p,
                            (Win32GraphicsConfig)p.getGraphicsConfiguration(),
                            p.getBackBuffersNum()))
                    {
                        // 10 is only chosen to be greater than the number of
                        // times a sane person would call validate() inside
                        // a validation loop, and to reduce thrashing between
                        // accelerated and backup surfaces
                        vsm.setRestoreCountdown(10);
                    } else {
                        vsm.setAccelerationEnabled(false);
                    }
                }
            }
        }
    }

    @Override
    public void initContents() {
        if (vImg.getForcedAccelSurfaceType() != TEXTURE) {
            super.initContents();
        }
    }
}