jdk/src/share/classes/sun/awt/image/SurfaceManager.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.GraphicsEnvironment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.GraphicsConfiguration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Image;
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.util.concurrent.ConcurrentHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.java2d.SurfaceData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.java2d.SurfaceDataProxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.java2d.loops.CompositeType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * The abstract base class that manages the various SurfaceData objects that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * represent an Image's contents.  Subclasses can customize how the surfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * are organized, whether to cache the original contents in an accelerated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * surface, and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * The SurfaceManager also maintains an arbitrary "cache" mechanism which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * allows other agents to store data in it specific to their use of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * image.  The most common use of the caching mechanism is for destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * SurfaceData objects to store cached copies of the source image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public abstract class SurfaceManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static abstract class ImageAccessor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        public abstract SurfaceManager getSurfaceManager(Image img);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        public abstract void setSurfaceManager(Image img, SurfaceManager mgr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static ImageAccessor imgaccessor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static void setImageAccessor(ImageAccessor ia) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (imgaccessor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            throw new InternalError("Attempt to set ImageAccessor twice");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        imgaccessor = ia;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Returns the SurfaceManager object contained within the given Image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static SurfaceManager getManager(Image img) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        SurfaceManager sMgr = imgaccessor.getSurfaceManager(img);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (sMgr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
             * In practice only a BufferedImage will get here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                BufferedImage bi = (BufferedImage) img;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                sMgr = new BufImgSurfaceManager(bi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                setManager(bi, sMgr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            } catch (ClassCastException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                throw new IllegalArgumentException("Invalid Image variant");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return sMgr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public static void setManager(Image img, SurfaceManager mgr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        imgaccessor.setSurfaceManager(img, mgr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private ConcurrentHashMap cacheMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Return an arbitrary cached object for an arbitrary cache key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Other objects can use this mechanism to store cached data about
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * the source image that will let them save time when using or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * manipulating the image in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Note that the cache is maintained as a simple Map with no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * attempts to keep it up to date or invalidate it so any data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * stored here must either not be dependent on the state of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * image or it must be individually tracked to see if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * outdated or obsolete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * The SurfaceData object of the primary (destination) surface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * has a StateTracker mechanism which can help track the validity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * and "currentness" of any data stored here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * For convenience and expediency an object stored as cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * data may implement the FlushableCacheData interface specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * below so that it may be notified immediately if the flush()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * method is ever called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public Object getCacheData(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return (cacheMap == null) ? null : cacheMap.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Store an arbitrary cached object for an arbitrary cache key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * See the getCacheData() method for notes on tracking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * validity of data stored using this mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public void setCacheData(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (cacheMap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                if (cacheMap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    cacheMap = new ConcurrentHashMap(2);
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
        cacheMap.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Returns the main SurfaceData object that "owns" the pixels for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * this SurfaceManager.  This SurfaceData is used as the destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * surface in a rendering operation and is the most authoritative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * storage for the current state of the pixels, though other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * versions might be cached in other locations for efficiency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public abstract SurfaceData getPrimarySurfaceData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Restores the primary surface being managed, and then returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * replacement surface.  This is called when an accelerated surface has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * been "lost", in an attempt to auto-restore its contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public abstract SurfaceData restoreContents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Notification that any accelerated surfaces associated with this manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * have been "lost", which might mean that they need to be manually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * restored or recreated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * The default implementation does nothing, but platform-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * variants which have accelerated surfaces should perform any necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public void acceleratedSurfaceLost() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Returns an ImageCapabilities object which can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * inquired as to the specific capabilities of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Image.  The capabilities object will return true for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * isAccelerated() if the image has a current and valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * SurfaceDataProxy object cached for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * GraphicsConfiguration parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * This class provides a default implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * ImageCapabilities that will try to determine if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * is an associated SurfaceDataProxy object and if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * up to date, but only works for GraphicsConfiguration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * objects which implement the ProxiedGraphicsConfig
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * interface defined below.  In practice, all configs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * which can be accelerated are currently implementing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * that interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * A null GraphicsConfiguration returns a value based on whether the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * image is currently accelerated on its default GraphicsConfiguration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @see java.awt.Image#getCapabilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public ImageCapabilities getCapabilities(GraphicsConfiguration gc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return new ImageCapabilitiesGc(gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    class ImageCapabilitiesGc extends ImageCapabilities {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        GraphicsConfiguration gc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        public ImageCapabilitiesGc(GraphicsConfiguration gc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            super(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            this.gc = gc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        public boolean isAccelerated() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            // Note that when img.getAccelerationPriority() gets set to 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            // we remove SurfaceDataProxy objects from the cache and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            // answer will be false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            GraphicsConfiguration tmpGc = gc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            if (tmpGc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                tmpGc = GraphicsEnvironment.getLocalGraphicsEnvironment().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    getDefaultScreenDevice().getDefaultConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            if (tmpGc instanceof ProxiedGraphicsConfig) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                Object proxyKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    ((ProxiedGraphicsConfig) tmpGc).getProxyKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                if (proxyKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    SurfaceDataProxy sdp =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                        (SurfaceDataProxy) getCacheData(proxyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    return (sdp != null && sdp.isAccelerated());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * An interface for GraphicsConfiguration objects to implement if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * their surfaces accelerate images using SurfaceDataProxy objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Implementing this interface facilitates the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * implementation of getImageCapabilities() above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public static interface ProxiedGraphicsConfig {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
         * Return the key that destination surfaces created on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
         * given GraphicsConfiguration use to store SurfaceDataProxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
         * objects for their cached copies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        public Object getProxyKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Releases system resources in use by ancillary SurfaceData objects,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * such as surfaces cached in accelerated memory.  Subclasses should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * override to release any of their flushable data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * The default implementation will visit all of the value objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * in the cacheMap and flush them if they implement the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * FlushableCacheData interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public synchronized void flush() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        flush(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    synchronized void flush(boolean deaccelerate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (cacheMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            Iterator i = cacheMap.values().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                Object o = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                if (o instanceof FlushableCacheData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    if (((FlushableCacheData) o).flush(deaccelerate)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * An interface for Objects used in the SurfaceManager cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * to implement if they have data that should be flushed when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * the Image is flushed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public static interface FlushableCacheData {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
         * Flush all cached resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
         * The deaccelerated parameter indicates if the flush is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
         * happening because the associated surface is no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
         * being accelerated (for instance the acceleration priority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         * is set below the threshold needed for acceleration).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         * Returns a boolean that indicates if the cached object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
         * no longer needed and should be removed from the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        public boolean flush(boolean deaccelerated);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Called when image's acceleration priority is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * The default implementation will visit all of the value objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * in the cacheMap when the priority gets set to 0.0 and flush them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * if they implement the FlushableCacheData interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public void setAccelerationPriority(float priority) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (priority == 0.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            flush(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
}