jdk/src/share/classes/sun/awt/image/VolatileSurfaceManager.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 888 c7009cf0001f
child 13770 c50a78763e19
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 888
diff changeset
     2
 * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 888
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 888
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 888
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 888
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 888
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt.image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.GraphicsConfiguration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.GraphicsEnvironment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.ImageCapabilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.image.VolatileImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.awt.DisplayChangedListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.awt.image.SunVolatileImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.java2d.SunGraphicsEnvironment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.java2d.SurfaceData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.java2d.loops.CompositeType;
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
    40
import static sun.java2d.pipe.hw.AccelSurface.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * This SurfaceManager variant manages an accelerated volatile surface, if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * is possible to create that surface.  If there is limited accelerated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * memory, or if the volatile surface disappears due to an operating system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * event, the VolatileSurfaceManager will attempt to restore the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * accelerated surface.  If that fails, a system memory surface will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * created in its place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public abstract class VolatileSurfaceManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    extends SurfaceManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    implements DisplayChangedListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * A reference to the VolatileImage whose contents are being managed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected SunVolatileImage vImg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * The accelerated SurfaceData object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    protected SurfaceData sdAccel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * The software-based SurfaceData object.  Only create when first asked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * to (otherwise it is a waste of memory as it will only be used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * situations of surface loss).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    protected SurfaceData sdBackup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * The current SurfaceData object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    protected SurfaceData sdCurrent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * A record-keeping object.  This keeps track of which SurfaceData was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * in use during the last call to validate().  This lets us see whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * the SurfaceData object has changed since then and allows us to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * the correct returnCode to the user in the validate() call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    protected SurfaceData sdPrevious;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Tracks loss of surface contents; queriable by user to see whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * contents need to be restored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    protected boolean lostSurface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Context for extra initialization parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    protected Object context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    protected VolatileSurfaceManager(SunVolatileImage vImg, Object context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        this.vImg = vImg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this.context = context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        GraphicsEnvironment ge =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            GraphicsEnvironment.getLocalGraphicsEnvironment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // We could have a HeadlessGE at this point, so double-check before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        // assuming anything.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (ge instanceof SunGraphicsEnvironment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            ((SunGraphicsEnvironment)ge).addDisplayChangedListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * This init function is separate from the constructor because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * things we are doing here necessitate the object's existence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Otherwise, we end up calling into a subclass' overridden method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * during construction, before that subclass is completely constructed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public void initialize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (isAccelerationEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            sdAccel = initAcceleratedSurface();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (sdAccel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                sdCurrent = sdAccel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   121
        // only initialize the backup surface for images with unforced
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   122
        // acceleration type
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   123
        if (sdCurrent == null &&
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   124
            vImg.getForcedAccelSurfaceType() == UNDEFINED)
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   125
        {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            sdCurrent = getBackupSurface();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public SurfaceData getPrimarySurfaceData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return sdCurrent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Returns true if acceleration is enabled.  If not, we simply use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * backup SurfaceData object and return quickly from most methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * in this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    protected abstract boolean isAccelerationEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Get the image ready for rendering.  This method is called to make
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * sure that the accelerated SurfaceData exists and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * ready to be used.  Users call this method prior to any set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * rendering to or from the image, to make sure the image is ready
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * and compatible with the given GraphicsConfiguration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * The image may not be "ready" if either we had problems creating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * it in the first place (e.g., there was no space in vram) or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * the surface became lost (e.g., some other app or the OS caused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * vram surfaces to be removed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Note that we want to return RESTORED in any situation where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * SurfaceData is different than it was last time.  So whether it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * software or hardware, if we have a different SurfaceData object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * then the contents have been altered and we must reflect that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * change to the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public int validate(GraphicsConfiguration gc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        int returnCode = VolatileImage.IMAGE_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        boolean lostSurfaceTmp = lostSurface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        lostSurface = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (isAccelerationEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if (!isConfigValid(gc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                // If we're asked to render to a different device than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                // one we were created under, return INCOMPATIBLE error code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                // Note that a null gc simply ignores the incompatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                // issue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                returnCode = VolatileImage.IMAGE_INCOMPATIBLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            } else if (sdAccel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                // We either had problems creating the surface or the display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                // mode changed and we nullified the old one.  Try it again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                sdAccel = initAcceleratedSurface();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                if (sdAccel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    // set the current SurfaceData to accelerated version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    sdCurrent = sdAccel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    // we don't need the system memory surface anymore, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    // let's release it now (it can always be restored later)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    sdBackup = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    returnCode = VolatileImage.IMAGE_RESTORED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    sdCurrent = getBackupSurface();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            } else if (sdAccel.isSurfaceLost()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    restoreAcceleratedSurface();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    // set the current SurfaceData to accelerated version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    sdCurrent = sdAccel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    // restoration successful: accel surface no longer lost
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    sdAccel.setSurfaceLost(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    // we don't need the system memory surface anymore, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    // let's release it now (it can always be restored later)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    sdBackup = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    returnCode = VolatileImage.IMAGE_RESTORED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                } catch (sun.java2d.InvalidPipeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    // Set the current SurfaceData to software version so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    // drawing can continue.  Note that we still have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    // the lostAccelSurface flag set so that we will continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    // to attempt to restore the accelerated surface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    sdCurrent = getBackupSurface();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            } else if (lostSurfaceTmp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                // Something else triggered this loss/restoration.  Could
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                // be a palette change that didn't require a SurfaceData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                // recreation but merely a re-rendering of the pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                returnCode = VolatileImage.IMAGE_RESTORED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        } else if (sdAccel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            // if the "acceleration enabled" state changed to disabled,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            // switch to software surface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            sdCurrent = getBackupSurface();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            sdAccel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            returnCode = VolatileImage.IMAGE_RESTORED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if ((returnCode != VolatileImage.IMAGE_INCOMPATIBLE) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            (sdCurrent != sdPrevious))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            // contents have changed - return RESTORED to user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            sdPrevious = sdCurrent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            returnCode = VolatileImage.IMAGE_RESTORED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (returnCode == VolatileImage.IMAGE_RESTORED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            // clear the current surface with the background color,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            // only if the surface has been restored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            initContents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return returnCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Returns true if rendering data was lost since the last validate call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @see java.awt.image.VolatileImage#contentsLost
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public boolean contentsLost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return lostSurface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Creates a new accelerated surface that is compatible with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * current GraphicsConfiguration.  Returns the new accelerated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * SurfaceData object, or null if the surface creation was not successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Platform-specific subclasses should initialize an accelerated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * surface (e.g. a DirectDraw surface on Windows, an OpenGL pbuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * or an X11 pixmap).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    protected abstract SurfaceData initAcceleratedSurface();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Creates a software-based surface (of type BufImgSurfaceData).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * The software representation is only created when needed, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * is only during some situation in which the hardware surface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * cannot be allocated.  This allows apps to at least run,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * albeit more slowly than they would otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    protected SurfaceData getBackupSurface() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (sdBackup == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            BufferedImage bImg = vImg.getBackupImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            // Sabotage the acceleration capabilities of the BufImg surface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            SunWritableRaster.stealTrackable(bImg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                             .getRaster()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                                             .getDataBuffer()).setUntrackable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            sdBackup = BufImgSurfaceData.createData(bImg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        return sdBackup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Set contents of the current SurfaceData to default state (i.e. clear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * the background).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public void initContents() {
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   278
        // images with forced acceleration type may have a null sdCurrent
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   279
        // because we do not create a backup surface for them
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   280
        if (sdCurrent != null) {
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   281
            Graphics g = vImg.createGraphics();
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   282
            g.clearRect(0, 0, vImg.getWidth(), vImg.getHeight());
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   283
            g.dispose();
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   284
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Called from a SurfaceData object, indicating that our
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * accelerated surface has been lost and should be restored (perhaps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * using a backup system memory surface).  Returns the newly restored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * primary SurfaceData object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public SurfaceData restoreContents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        return getBackupSurface();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * If the accelerated surface is the current SurfaceData for this manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * sets the variable lostSurface to true, which indicates that something
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * happened to the image under management.  This variable is used in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * validate method to tell the caller that the surface contents need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * be restored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public void acceleratedSurfaceLost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (isAccelerationEnabled() && (sdCurrent == sdAccel)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            lostSurface = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Restore sdAccel in case it was lost.  Do nothing in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * default case; platform-specific implementations may do more in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * this situation as appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    protected void restoreAcceleratedSurface() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Called from SunGraphicsEnv when there has been a display mode change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * Note that we simply invalidate hardware surfaces here; we do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * attempt to recreate or re-render them.  This is to avoid threading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * conflicts with the native toolkit and associated threads.  Instead,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * we just nullify the old surface data object and wait for a future
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * method in the rendering process to recreate the surface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public void displayChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (!isAccelerationEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        lostSurface = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (sdAccel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            // First, nullify the software surface.  This guards against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            // using a SurfaceData that was created in a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            // display mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            sdBackup = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            sdCurrent = getBackupSurface();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            // Now, invalidate the old hardware-based SurfaceData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            SurfaceData oldData = sdAccel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            sdAccel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            oldData.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        // Update graphicsConfig for the vImg in case it changed due to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        // this display change event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        vImg.updateGraphicsConfig();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * When device palette changes, need to force a new copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * of the image into our hardware cache to update the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * color indices of the pixels (indexed mode only).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public void paletteChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        lostSurface = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Called by validate() to see whether the GC passed in is ok for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * rendering to.  This generic implementation checks to see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * whether the GC is either null or is from the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * device as the one that this image was created on.  Platform-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * specific implementations may perform other checks as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    protected boolean isConfigValid(GraphicsConfiguration gc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return ((gc == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                (gc.getDevice() == vImg.getGraphicsConfig().getDevice()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public ImageCapabilities getCapabilities(GraphicsConfiguration gc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (isConfigValid(gc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            return isAccelerationEnabled() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                new AcceleratedImageCapabilities() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                new ImageCapabilities(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        return super.getCapabilities(gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    private class AcceleratedImageCapabilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        extends ImageCapabilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        AcceleratedImageCapabilities() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            super(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        public boolean isAccelerated() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            return (sdCurrent == sdAccel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        public boolean isTrueVolatile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            return isAccelerated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Releases any associated hardware memory for this image by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * calling flush on sdAccel.  This method forces a lostSurface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * situation so any future operations on the image will need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * revalidate the image first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    public void flush() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        lostSurface = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        SurfaceData oldSD = sdAccel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        sdAccel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if (oldSD != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            oldSD.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
}