1 /*
2  * Copyright (c) 2007, Oracle and/or its affiliates. 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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * 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 {
createProxy(SurfaceData srcData, X11GraphicsConfig dstConfig)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 
X11SurfaceDataProxy(X11GraphicsConfig x11gc)95     public X11SurfaceDataProxy(X11GraphicsConfig x11gc) {
96         this.x11gc = x11gc;
97     }
98 
99     @Override
validateSurfaceData(SurfaceData srcData, SurfaceData cachedData, int w, int h)100     public SurfaceData validateSurfaceData(SurfaceData srcData,
101                                            SurfaceData cachedData,
102                                            int w, int h)
103     {
104         if (cachedData == null) {
105             try {
106                 // Bitmask will be created lazily during the blit phase
107                 cachedData = X11SurfaceData.createData(x11gc, w, h,
108                                                        x11gc.getColorModel(),
109                                                        null, 0, getTransparency());
110            } catch (OutOfMemoryError oome) {
111            }
112         }
113         return cachedData;
114     }
115 
116     /**
117      * Proxy for opaque source images.
118      * This proxy can accelerate unscaled Src copies.
119      */
120     public static class Opaque extends X11SurfaceDataProxy {
Opaque(X11GraphicsConfig x11gc)121         public Opaque(X11GraphicsConfig x11gc) {
122             super(x11gc);
123         }
124 
getTransparency()125         public int getTransparency() {
126             return Transparency.OPAQUE;
127         }
128 
129         @Override
isSupportedOperation(SurfaceData srcData, int txtype, CompositeType comp, Color bgColor)130         public boolean isSupportedOperation(SurfaceData srcData,
131                                             int txtype,
132                                             CompositeType comp,
133                                             Color bgColor)
134         {
135             return (txtype < SunGraphics2D.TRANSFORM_TRANSLATESCALE &&
136                     (CompositeType.SrcOverNoEa.equals(comp) ||
137                      CompositeType.SrcNoEa.equals(comp)));
138         }
139     }
140 
141     /**
142      * Proxy for bitmask transparent source images.
143      * This proxy can accelerate unscaled Src copies or
144      * unscaled SrcOver copies that use an opaque bgColor.
145      */
146     public static class Bitmask extends X11SurfaceDataProxy {
Bitmask(X11GraphicsConfig x11gc)147         public Bitmask(X11GraphicsConfig x11gc) {
148             super(x11gc);
149         }
150 
getTransparency()151         public int getTransparency() {
152             return Transparency.BITMASK;
153         }
154 
155         @Override
isSupportedOperation(SurfaceData srcData, int txtype, CompositeType comp, Color bgColor)156         public boolean isSupportedOperation(SurfaceData srcData,
157                                             int txtype,
158                                             CompositeType comp,
159                                             Color bgColor)
160         {
161             // These could probably be combined into a single
162             // nested if, but the logic is easier to follow this way.
163 
164             // we don't have X11 scale loops, so always use
165             // software surface in case of scaling
166             if (txtype >= SunGraphics2D.TRANSFORM_TRANSLATESCALE) {
167                 return false;
168             }
169 
170             if (bgColor != null &&
171                 bgColor.getTransparency() != Transparency.OPAQUE)
172             {
173                 return false;
174             }
175 
176             // for transparent images SrcNoEa+bgColor has the
177             // same effect as SrcOverNoEa+bgColor, so we allow
178             // copying from pixmap SD using accelerated blitbg loops:
179             // SrcOver will be changed to SrcNoEa in DrawImage.blitSD
180             if (CompositeType.SrcOverNoEa.equals(comp) ||
181                 (CompositeType.SrcNoEa.equals(comp) &&
182                  bgColor != null))
183             {
184                 return true;
185             }
186 
187             return false;
188         }
189     }
190 }
191