J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.java2d.x11; |
| 27 | |
| 28 | import java.awt.Color; |
| 29 | import java.awt.AlphaComposite; |
| 30 | import java.awt.GraphicsConfiguration; |
| 31 | import java.awt.Transparency; |
| 32 | import java.awt.image.ColorModel; |
| 33 | import java.awt.image.IndexColorModel; |
| 34 | import java.awt.image.DirectColorModel; |
| 35 | |
| 36 | import sun.awt.X11GraphicsConfig; |
| 37 | import sun.java2d.SurfaceData; |
| 38 | import sun.java2d.SurfaceDataProxy; |
| 39 | import sun.java2d.SunGraphics2D; |
| 40 | import sun.java2d.loops.SurfaceType; |
| 41 | import sun.java2d.loops.CompositeType; |
| 42 | |
| 43 | /** |
| 44 | * The proxy class contains the logic for when to replace a |
| 45 | * SurfaceData with a cached X11 Pixmap and the code to create |
| 46 | * the accelerated surfaces. |
| 47 | */ |
| 48 | public abstract class X11SurfaceDataProxy extends SurfaceDataProxy |
| 49 | implements Transparency |
| 50 | { |
| 51 | public static SurfaceDataProxy createProxy(SurfaceData srcData, |
| 52 | X11GraphicsConfig dstConfig) |
| 53 | { |
| 54 | if (srcData instanceof X11SurfaceData) { |
| 55 | // srcData must be a VolatileImage which either matches |
| 56 | // our visual or not - either way we do not cache it... |
| 57 | return UNCACHED; |
| 58 | } |
| 59 | |
| 60 | ColorModel cm = srcData.getColorModel(); |
| 61 | int transparency = cm.getTransparency(); |
| 62 | |
| 63 | if (transparency == Transparency.OPAQUE) { |
| 64 | return new Opaque(dstConfig); |
| 65 | } else if (transparency == Transparency.BITMASK) { |
| 66 | // 4673490: updateBitmask() only handles ICMs with 8-bit indices |
| 67 | if ((cm instanceof IndexColorModel) && cm.getPixelSize() == 8) { |
| 68 | return new Bitmask(dstConfig); |
| 69 | } |
| 70 | // The only other ColorModel handled by updateBitmask() is |
| 71 | // a DCM where the alpha bit, and only the alpha bit, is in |
| 72 | // the top 8 bits |
| 73 | if (cm instanceof DirectColorModel) { |
| 74 | DirectColorModel dcm = (DirectColorModel) cm; |
| 75 | int colormask = (dcm.getRedMask() | |
| 76 | dcm.getGreenMask() | |
| 77 | dcm.getBlueMask()); |
| 78 | int alphamask = dcm.getAlphaMask(); |
| 79 | |
| 80 | if ((colormask & 0xff000000) == 0 && |
| 81 | (alphamask & 0xff000000) != 0) |
| 82 | { |
| 83 | return new Bitmask(dstConfig); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // For whatever reason, this image is not a good candidate for |
| 89 | // caching in a pixmap so we return the non-caching (non-)proxy. |
| 90 | return UNCACHED; |
| 91 | } |
| 92 | |
| 93 | X11GraphicsConfig x11gc; |
| 94 | |
| 95 | public X11SurfaceDataProxy(X11GraphicsConfig x11gc) { |
| 96 | this.x11gc = x11gc; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public SurfaceData validateSurfaceData(SurfaceData srcData, |
| 101 | SurfaceData cachedData, |
| 102 | int w, int h) |
| 103 | { |
| 104 | if (cachedData == null) { |
| 105 | // Bitmask will be created lazily during the blit phase |
| 106 | cachedData = X11SurfaceData.createData(x11gc, w, h, |
| 107 | x11gc.getColorModel(), |
| 108 | null, 0, getTransparency()); |
| 109 | } |
| 110 | return cachedData; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Proxy for opaque source images. |
| 115 | * This proxy can accelerate unscaled Src copies. |
| 116 | */ |
| 117 | public static class Opaque extends X11SurfaceDataProxy { |
| 118 | public Opaque(X11GraphicsConfig x11gc) { |
| 119 | super(x11gc); |
| 120 | } |
| 121 | |
| 122 | public int getTransparency() { |
| 123 | return Transparency.OPAQUE; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public boolean isSupportedOperation(SurfaceData srcData, |
| 128 | int txtype, |
| 129 | CompositeType comp, |
| 130 | Color bgColor) |
| 131 | { |
| 132 | return (txtype < SunGraphics2D.TRANSFORM_TRANSLATESCALE && |
| 133 | (CompositeType.SrcOverNoEa.equals(comp) || |
| 134 | CompositeType.SrcNoEa.equals(comp))); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Proxy for bitmask transparent source images. |
| 140 | * This proxy can accelerate unscaled Src copies or |
| 141 | * unscaled SrcOver copies that use an opaque bgColor. |
| 142 | */ |
| 143 | public static class Bitmask extends X11SurfaceDataProxy { |
| 144 | public Bitmask(X11GraphicsConfig x11gc) { |
| 145 | super(x11gc); |
| 146 | } |
| 147 | |
| 148 | public int getTransparency() { |
| 149 | return Transparency.BITMASK; |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public boolean isSupportedOperation(SurfaceData srcData, |
| 154 | int txtype, |
| 155 | CompositeType comp, |
| 156 | Color bgColor) |
| 157 | { |
| 158 | // These could probably be combined into a single |
| 159 | // nested if, but the logic is easier to follow this way. |
| 160 | |
| 161 | // we don't have X11 scale loops, so always use |
| 162 | // software surface in case of scaling |
| 163 | if (txtype >= SunGraphics2D.TRANSFORM_TRANSLATESCALE) { |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | if (bgColor != null && |
| 168 | bgColor.getTransparency() != Transparency.OPAQUE) |
| 169 | { |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | // for transparent images SrcNoEa+bgColor has the |
| 174 | // same effect as SrcOverNoEa+bgColor, so we allow |
| 175 | // copying from pixmap SD using accelerated blitbg loops: |
| 176 | // SrcOver will be changed to SrcNoEa in DrawImage.blitSD |
| 177 | if (CompositeType.SrcOverNoEa.equals(comp) || |
| 178 | (CompositeType.SrcNoEa.equals(comp) && |
| 179 | bgColor != null)) |
| 180 | { |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | return false; |
| 185 | } |
| 186 | } |
| 187 | } |