1 /*
2  * Copyright (c) 2007, 2020, 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.Transparency;
30 import java.awt.image.ColorModel;
31 import java.awt.image.DirectColorModel;
32 import java.awt.image.IndexColorModel;
33 
34 import sun.awt.X11GraphicsConfig;
35 import sun.java2d.SunGraphics2D;
36 import sun.java2d.SurfaceData;
37 import sun.java2d.SurfaceDataProxy;
38 import sun.java2d.loops.CompositeType;
39 
40 /**
41  * The proxy class contains the logic for when to replace a
42  * SurfaceData with a cached X11 Pixmap and the code to create
43  * the accelerated surfaces.
44  */
45 public abstract class X11SurfaceDataProxy extends SurfaceDataProxy
46     implements Transparency
47 {
createProxy(SurfaceData srcData, X11GraphicsConfig dstConfig)48     public static SurfaceDataProxy createProxy(SurfaceData srcData,
49                                                X11GraphicsConfig dstConfig)
50     {
51         if (srcData instanceof X11SurfaceData) {
52             // srcData must be a VolatileImage which either matches
53             // our visual or not - either way we do not cache it...
54             return UNCACHED;
55         }
56 
57         ColorModel cm = srcData.getColorModel();
58         int transparency = cm.getTransparency();
59 
60         if (transparency == Transparency.OPAQUE) {
61             return new Opaque(dstConfig);
62         } else if (transparency == Transparency.BITMASK) {
63             // 4673490: updateBitmask() only handles ICMs with 8-bit indices
64             if ((cm instanceof IndexColorModel) && cm.getPixelSize() == 8) {
65                 return new Bitmask(dstConfig);
66             }
67             // The only other ColorModel handled by updateBitmask() is
68             // a DCM where the alpha bit, and only the alpha bit, is in
69             // the top 8 bits
70             if (cm instanceof DirectColorModel) {
71                 DirectColorModel dcm = (DirectColorModel) cm;
72                 int colormask = (dcm.getRedMask() |
73                                  dcm.getGreenMask() |
74                                  dcm.getBlueMask());
75                 int alphamask = dcm.getAlphaMask();
76 
77                 if ((colormask & 0xff000000) == 0 &&
78                     (alphamask & 0xff000000) != 0)
79                 {
80                     return new Bitmask(dstConfig);
81                 }
82             }
83         }
84 
85         // For whatever reason, this image is not a good candidate for
86         // caching in a pixmap so we return the non-caching (non-)proxy.
87         return UNCACHED;
88     }
89 
90     X11GraphicsConfig x11gc;
91 
X11SurfaceDataProxy(X11GraphicsConfig x11gc)92     public X11SurfaceDataProxy(X11GraphicsConfig x11gc) {
93         this.x11gc = x11gc;
94     }
95 
96     @Override
validateSurfaceData(SurfaceData srcData, SurfaceData cachedData, int w, int h)97     public SurfaceData validateSurfaceData(SurfaceData srcData,
98                                            SurfaceData cachedData,
99                                            int w, int h)
100     {
101         if (cachedData == null) {
102             try {
103                 // Bitmask will be created lazily during the blit phase
104                 cachedData = X11SurfaceData.createData(x11gc, w, h,
105                                                        x11gc.getColorModel(),
106                                                        null, 0,
107                                                        getTransparency(), true);
108            } catch (OutOfMemoryError oome) {
109            }
110         }
111         return cachedData;
112     }
113 
114     /**
115      * Proxy for opaque source images.
116      * This proxy can accelerate unscaled Src copies.
117      */
118     public static class Opaque extends X11SurfaceDataProxy {
Opaque(X11GraphicsConfig x11gc)119         public Opaque(X11GraphicsConfig x11gc) {
120             super(x11gc);
121         }
122 
getTransparency()123         public int getTransparency() {
124             return Transparency.OPAQUE;
125         }
126 
127         @Override
isSupportedOperation(SurfaceData srcData, int txtype, CompositeType comp, Color bgColor)128         public boolean isSupportedOperation(SurfaceData srcData,
129                                             int txtype,
130                                             CompositeType comp,
131                                             Color bgColor)
132         {
133             return (txtype < SunGraphics2D.TRANSFORM_TRANSLATESCALE &&
134                     (CompositeType.SrcOverNoEa.equals(comp) ||
135                      CompositeType.SrcNoEa.equals(comp)));
136         }
137     }
138 
139     /**
140      * Proxy for bitmask transparent source images.
141      * This proxy can accelerate unscaled Src copies or
142      * unscaled SrcOver copies that use an opaque bgColor.
143      */
144     public static class Bitmask extends X11SurfaceDataProxy {
Bitmask(X11GraphicsConfig x11gc)145         public Bitmask(X11GraphicsConfig x11gc) {
146             super(x11gc);
147         }
148 
getTransparency()149         public int getTransparency() {
150             return Transparency.BITMASK;
151         }
152 
153         @Override
isSupportedOperation(SurfaceData srcData, int txtype, CompositeType comp, Color bgColor)154         public boolean isSupportedOperation(SurfaceData srcData,
155                                             int txtype,
156                                             CompositeType comp,
157                                             Color bgColor)
158         {
159             // These could probably be combined into a single
160             // nested if, but the logic is easier to follow this way.
161 
162             // we don't have X11 scale loops, so always use
163             // software surface in case of scaling
164             if (txtype >= SunGraphics2D.TRANSFORM_TRANSLATESCALE) {
165                 return false;
166             }
167 
168             if (bgColor != null &&
169                 bgColor.getTransparency() != Transparency.OPAQUE)
170             {
171                 return false;
172             }
173 
174             // for transparent images SrcNoEa+bgColor has the
175             // same effect as SrcOverNoEa+bgColor, so we allow
176             // copying from pixmap SD using accelerated blitbg loops:
177             // SrcOver will be changed to SrcNoEa in DrawImage.blitSD
178             if (CompositeType.SrcOverNoEa.equals(comp) ||
179                 (CompositeType.SrcNoEa.equals(comp) &&
180                  bgColor != null))
181             {
182                 return true;
183             }
184 
185             return false;
186         }
187     }
188 }
189