001: /*
002: * Copyright 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.Color;
029: import java.awt.AlphaComposite;
030: import java.awt.GraphicsConfiguration;
031: import java.awt.Transparency;
032: import java.awt.image.ColorModel;
033: import java.awt.image.IndexColorModel;
034: import java.awt.image.DirectColorModel;
035:
036: import sun.awt.X11GraphicsConfig;
037: import sun.java2d.SurfaceData;
038: import sun.java2d.SurfaceDataProxy;
039: import sun.java2d.SunGraphics2D;
040: import sun.java2d.loops.SurfaceType;
041: import sun.java2d.loops.CompositeType;
042:
043: /**
044: * The proxy class contains the logic for when to replace a
045: * SurfaceData with a cached X11 Pixmap and the code to create
046: * the accelerated surfaces.
047: */
048: public abstract class X11SurfaceDataProxy extends SurfaceDataProxy
049: implements Transparency {
050: public static SurfaceDataProxy createProxy(SurfaceData srcData,
051: X11GraphicsConfig dstConfig) {
052: if (srcData instanceof X11SurfaceData) {
053: // srcData must be a VolatileImage which either matches
054: // our visual or not - either way we do not cache it...
055: return UNCACHED;
056: }
057:
058: ColorModel cm = srcData.getColorModel();
059: int transparency = cm.getTransparency();
060:
061: if (transparency == Transparency.OPAQUE) {
062: return new Opaque(dstConfig);
063: } else if (transparency == Transparency.BITMASK) {
064: // 4673490: updateBitmask() only handles ICMs with 8-bit indices
065: if ((cm instanceof IndexColorModel)
066: && cm.getPixelSize() == 8) {
067: return new Bitmask(dstConfig);
068: }
069: // The only other ColorModel handled by updateBitmask() is
070: // a DCM where the alpha bit, and only the alpha bit, is in
071: // the top 8 bits
072: if (cm instanceof DirectColorModel) {
073: DirectColorModel dcm = (DirectColorModel) cm;
074: int colormask = (dcm.getRedMask() | dcm.getGreenMask() | dcm
075: .getBlueMask());
076: int alphamask = dcm.getAlphaMask();
077:
078: if ((colormask & 0xff000000) == 0
079: && (alphamask & 0xff000000) != 0) {
080: return new Bitmask(dstConfig);
081: }
082: }
083: }
084:
085: // For whatever reason, this image is not a good candidate for
086: // caching in a pixmap so we return the non-caching (non-)proxy.
087: return UNCACHED;
088: }
089:
090: X11GraphicsConfig x11gc;
091:
092: public X11SurfaceDataProxy(X11GraphicsConfig x11gc) {
093: this .x11gc = x11gc;
094: }
095:
096: @Override
097: public SurfaceData validateSurfaceData(SurfaceData srcData,
098: SurfaceData cachedData, int w, int h) {
099: if (cachedData == null) {
100: // Bitmask will be created lazily during the blit phase
101: cachedData = X11SurfaceData.createData(x11gc, w, h, x11gc
102: .getColorModel(), null, 0, getTransparency());
103: }
104: return cachedData;
105: }
106:
107: /**
108: * Proxy for opaque source images.
109: * This proxy can accelerate unscaled Src copies.
110: */
111: public static class Opaque extends X11SurfaceDataProxy {
112: public Opaque(X11GraphicsConfig x11gc) {
113: super (x11gc);
114: }
115:
116: public int getTransparency() {
117: return Transparency.OPAQUE;
118: }
119:
120: @Override
121: public boolean isSupportedOperation(SurfaceData srcData,
122: int txtype, CompositeType comp, Color bgColor) {
123: return (txtype < SunGraphics2D.TRANSFORM_TRANSLATESCALE && (CompositeType.SrcOverNoEa
124: .equals(comp) || CompositeType.SrcNoEa.equals(comp)));
125: }
126: }
127:
128: /**
129: * Proxy for bitmask transparent source images.
130: * This proxy can accelerate unscaled Src copies or
131: * unscaled SrcOver copies that use an opaque bgColor.
132: */
133: public static class Bitmask extends X11SurfaceDataProxy {
134: public Bitmask(X11GraphicsConfig x11gc) {
135: super (x11gc);
136: }
137:
138: public int getTransparency() {
139: return Transparency.BITMASK;
140: }
141:
142: @Override
143: public boolean isSupportedOperation(SurfaceData srcData,
144: int txtype, CompositeType comp, Color bgColor) {
145: // These could probably be combined into a single
146: // nested if, but the logic is easier to follow this way.
147:
148: // we don't have X11 scale loops, so always use
149: // software surface in case of scaling
150: if (txtype >= SunGraphics2D.TRANSFORM_TRANSLATESCALE) {
151: return false;
152: }
153:
154: if (bgColor != null
155: && bgColor.getTransparency() != Transparency.OPAQUE) {
156: return false;
157: }
158:
159: // for transparent images SrcNoEa+bgColor has the
160: // same effect as SrcOverNoEa+bgColor, so we allow
161: // copying from pixmap SD using accelerated blitbg loops:
162: // SrcOver will be changed to SrcNoEa in DrawImage.blitSD
163: if (CompositeType.SrcOverNoEa.equals(comp)
164: || (CompositeType.SrcNoEa.equals(comp) && bgColor != null)) {
165: return true;
166: }
167:
168: return false;
169: }
170: }
171: }
|