001: /*
002: * Copyright 2000-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.java2d.x11;
027:
028: import java.awt.GraphicsConfiguration;
029: import java.awt.ImageCapabilities;
030: import java.awt.Transparency;
031: import java.awt.image.ColorModel;
032: import sun.awt.X11GraphicsConfig;
033: import sun.awt.image.SunVolatileImage;
034: import sun.awt.image.VolatileSurfaceManager;
035: import sun.java2d.SurfaceData;
036:
037: /**
038: * X11 platform implementation of the VolatileSurfaceManager class.
039: * The class attempts to create and use a pixmap-based SurfaceData
040: * object (X11PixmapSurfaceData).
041: * If this object cannot be created or re-created as necessary, the
042: * class falls back to a system memory based SurfaceData object
043: * (BufImgSurfaceData) that will be used until the accelerated
044: * SurfaceData can be restored.
045: */
046: public class X11VolatileSurfaceManager extends VolatileSurfaceManager {
047:
048: private boolean accelerationEnabled;
049:
050: public X11VolatileSurfaceManager(SunVolatileImage vImg,
051: Object context) {
052: super (vImg, context);
053:
054: // We only accelerated opaque vImages currently
055: accelerationEnabled = X11SurfaceData.isAccelerationEnabled()
056: && (vImg.getTransparency() == Transparency.OPAQUE);
057:
058: if ((context != null) && !accelerationEnabled) {
059: // if we're wrapping a backbuffer drawable, we must ensure that
060: // the accelerated surface is initialized up front, regardless
061: // of whether acceleration is enabled. But we need to set
062: // the accelerationEnabled field to true to reflect that this
063: // SM is actually accelerated.
064: accelerationEnabled = true;
065: sdAccel = initAcceleratedSurface();
066: sdCurrent = sdAccel;
067:
068: if (sdBackup != null) {
069: // release the system memory backup surface, as we won't be
070: // needing it in this case
071: sdBackup = null;
072: }
073: }
074: }
075:
076: protected boolean isAccelerationEnabled() {
077: return accelerationEnabled;
078: }
079:
080: /**
081: * Create a pixmap-based SurfaceData object
082: */
083: protected SurfaceData initAcceleratedSurface() {
084: SurfaceData sData;
085:
086: try {
087: X11GraphicsConfig gc = (X11GraphicsConfig) vImg
088: .getGraphicsConfig();
089: ColorModel cm = gc.getColorModel();
090: long drawable = 0;
091: if (context instanceof Long) {
092: drawable = ((Long) context).longValue();
093: }
094: sData = X11SurfaceData.createData(gc, vImg.getWidth(), vImg
095: .getHeight(), cm, vImg, drawable,
096: Transparency.OPAQUE);
097: } catch (NullPointerException ex) {
098: sData = null;
099: } catch (OutOfMemoryError er) {
100: sData = null;
101: }
102:
103: return sData;
104: }
105:
106: protected boolean isConfigValid(GraphicsConfiguration gc) {
107: // REMIND: we might be too paranoid here, requiring that
108: // the GC be exactly the same as the original one. The
109: // real answer is one that guarantees that pixmap copies
110: // will be correct (which requires like bit depths and
111: // formats).
112: return ((gc == null) || (gc == vImg.getGraphicsConfig()));
113: }
114:
115: /**
116: * Need to override the default behavior because Pixmaps-based
117: * images are accelerated but not volatile.
118: */
119: @Override
120: public ImageCapabilities getCapabilities(GraphicsConfiguration gc) {
121: if (isConfigValid(gc) && isAccelerationEnabled()) {
122: // accelerated but not volatile
123: return new ImageCapabilities(true);
124: }
125: // neither accelerated nor volatile
126: return new ImageCapabilities(false);
127: }
128: }
|