src/solaris/classes/sun/java2d/xr/XRCompositeManager.java

Print this page




  31 import java.security.AccessController;
  32 import java.security.PrivilegedAction;
  33 
  34 import sun.font.*;
  35 import sun.java2d.*;
  36 import sun.java2d.jules.*;
  37 import sun.java2d.loops.*;
  38 
  39 /**
  40  * Manages per-application resources, e.g. the 1x1 pixmap used for solid color
  41  * fill as well as per-application state e.g. the currently set source picture
  42  * used for composition .
  43  *
  44  * @author Clemens Eisserer
  45  */
  46 
  47 public class XRCompositeManager {
  48     private static boolean enableGradCache = true;
  49     private static XRCompositeManager instance;
  50 
  51     XRSurfaceData src;





  52     XRSurfaceData texture;
  53     XRSurfaceData gradient;
  54     int alphaMask = XRUtils.None;
  55 
  56     XRColor solidColor = new XRColor();
  57     float extraAlpha = 1.0f;
  58     byte compRule = XRUtils.PictOpOver;
  59     XRColor alphaColor = new XRColor();
  60 
  61     XRSurfaceData solidSrcPict;
  62     int alphaMaskPict;
  63     int gradCachePixmap;
  64     int gradCachePicture;
  65 
  66     boolean xorEnabled = false;
  67     int validatedPixel = 0;
  68     Composite validatedComp;
  69     Paint validatedPaint;
  70     float validatedExtraAlpha = 1.0f;
  71 
  72     XRBackend con;
  73     MaskTileManager maskBuffer;
  74     XRTextRenderer textRenderer;
  75     XRMaskImage maskImage;
  76 
  77     public static synchronized XRCompositeManager getInstance(
  78             XRSurfaceData surface) {
  79         if (instance == null) {
  80             instance = new XRCompositeManager(surface);
  81         }
  82         return instance;
  83     }
  84 
  85     private XRCompositeManager(XRSurfaceData surface) {
  86         con = new XRBackendNative();
  87         // con = XRBackendJava.getInstance();
  88 
  89         String gradProp =
  90             AccessController.doPrivileged(new PrivilegedAction<String>() {
  91                 public String run() {
  92                     return System.getProperty("sun.java2d.xrgradcache");
  93                 }
  94             });
  95 
  96         enableGradCache = gradProp == null ||
  97                           !(gradProp.equalsIgnoreCase("false") ||
  98                           gradProp.equalsIgnoreCase("f"));
  99 
 100         XRPaints.register(this);
 101 
 102         initResources(surface);
 103 
 104         maskBuffer = new MaskTileManager(this, surface.getXid());
 105         textRenderer = new XRTextRenderer(this);
 106         maskImage = new XRMaskImage(this, surface.getXid());
 107     }
 108 
 109     public void initResources(XRSurfaceData surface) {
 110         int parentXid = surface.getXid();
 111 
 112         int solidPixmap = con.createPixmap(parentXid, 32, 1, 1);
 113         int solidSrcPictXID = con.createPicture(solidPixmap,
 114                 XRUtils.PictStandardARGB32);
 115         con.setPictureRepeat(solidSrcPictXID, XRUtils.RepeatNormal);
 116         con.renderRectangle(solidSrcPictXID, XRUtils.PictOpSrc,
 117                 XRColor.FULL_ALPHA, 0, 0, 1, 1);
 118         solidSrcPict = new XRSurfaceData.XRInternalSurfaceData(con,
 119                 solidSrcPictXID, null);
 120         setForeground(0);
 121 
 122         int extraAlphaMask = con.createPixmap(parentXid, 8, 1, 1);
 123         alphaMaskPict = con.createPicture(extraAlphaMask,
 124                 XRUtils.PictStandardA8);
 125         con.setPictureRepeat(alphaMaskPict, XRUtils.RepeatNormal);
 126         con.renderRectangle(alphaMaskPict, XRUtils.PictOpClear,
 127                 XRColor.NO_ALPHA, 0, 0, 1, 1);
 128 
 129         if (enableGradCache) {
 130             gradCachePixmap = con.createPixmap(parentXid, 32,
 131                     MaskTileManager.MASK_SIZE, MaskTileManager.MASK_SIZE);
 132             gradCachePicture = con.createPicture(gradCachePixmap,
 133                     XRUtils.PictStandardARGB32);
 134         }
 135     }
 136 
 137     public void setForeground(int pixel) {
 138         solidColor.setColorValues(pixel, false);
 139         con.renderRectangle(solidSrcPict.picture, XRUtils.PictOpSrc,
 140                 solidColor, 0, 0, 1, 1);
 141     }
 142 
 143     public void setGradientPaint(XRSurfaceData gradient) {
 144         if (this.gradient != null) {
 145             con.freePicture(this.gradient.picture);
 146         }
 147         this.gradient = gradient;
 148         src = gradient;
 149     }
 150 
 151     public void setTexturePaint(XRSurfaceData texture) {
 152         this.texture = texture;
 153         src = texture;
 154     }
 155 
 156     public void XRResetPaint() {
 157         src = solidSrcPict;
 158     }
 159 
 160     public void validateCompositeState(Composite comp, AffineTransform xform,
 161             Paint paint, SunGraphics2D sg2d) {
 162         boolean updatePaint = (paint != validatedPaint) || paint == null;
 163 
 164         // validate composite
 165         if ((comp != validatedComp)) {
 166             if (comp != null) {
 167                 setComposite(comp);
 168             } else {
 169                 comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER);
 170                 setComposite(comp);
 171             }
 172             // the paint state is dependent on the composite state, so make
 173             // sure we update the color below
 174             updatePaint = true;
 175             validatedComp = comp;
 176         }
 177 
 178         if (sg2d != null && validatedPixel != sg2d.pixel) {
 179             validatedPixel = sg2d.pixel;
 180             setForeground(validatedPixel);
 181         }
 182 
 183         // validate paint
 184         if (updatePaint) {
 185             if (paint != null && sg2d != null
 186                     && sg2d.paintState >= SunGraphics2D.PAINT_GRADIENT) {
 187                 XRPaints.setPaint(sg2d, paint);
 188             } else {
 189                 XRResetPaint();
 190             }
 191             validatedPaint = paint;
 192         }
 193 
 194         if (src != solidSrcPict) {
 195             AffineTransform at = (AffineTransform) xform.clone();
 196             try {
 197                 at.invert();
 198             } catch (NoninvertibleTransformException e) {
 199                 at.setToIdentity();
 200             }
 201             src.validateAsSource(at, -1, -1);
 202         }
 203     }
 204 
 205     private void setComposite(Composite comp) {
 206         if (comp instanceof AlphaComposite) {
 207             AlphaComposite aComp = (AlphaComposite) comp;
 208             validatedExtraAlpha = aComp.getAlpha();
 209 
 210             this.compRule = XRUtils.j2dAlphaCompToXR(aComp.getRule());
 211             this.extraAlpha = validatedExtraAlpha;
 212 
 213             if (extraAlpha == 1.0f) {
 214                 alphaMask = XRUtils.None;
 215                 alphaColor.alpha = XRColor.FULL_ALPHA.alpha;
 216             } else {
 217                 alphaColor.alpha = XRColor
 218                         .byteToXRColorValue((int) (extraAlpha * 255));
 219                 alphaMask = alphaMaskPict;
 220                 con.renderRectangle(alphaMaskPict, XRUtils.PictOpSrc,
 221                         alphaColor, 0, 0, 1, 1);
 222             }
 223 
 224             xorEnabled = false;
 225         } else if (comp instanceof XORComposite) {
 226             /* XOR composite validation is handled in XRSurfaceData */
 227             xorEnabled = true;
 228         } else {
 229             throw new InternalError(
 230                     "Composite accaleration not implemented for: "
 231                             + comp.getClass().getName());
 232         }
 233     }
 234 
 235     public boolean maskRequired() {
 236         return (!xorEnabled)
 237                 && ((src != solidSrcPict)
 238                         || (src == solidSrcPict && solidColor.alpha != 0xffff) || (extraAlpha != 1.0f));
 239     }
 240 
 241     public void XRComposite(int src, int mask, int dst, int srcX, int srcY,
 242             int maskX, int maskY, int dstX, int dstY, int width, int height) {
 243         int cachedSrc = (src == XRUtils.None) ? this.src.picture : src;
 244         int cachedX = srcX;
 245         int cachedY = srcY;
 246 
 247         if (enableGradCache && gradient != null
 248                 && cachedSrc == gradient.picture) {
 249             con.renderComposite(XRUtils.PictOpSrc, gradient.picture,
 250                     XRUtils.None, gradCachePicture, srcX, srcY, 0, 0, 0, 0,
 251                     width, height);
 252             cachedX = 0;
 253             cachedY = 0;
 254             cachedSrc = gradCachePicture;
 255         }
 256 
 257         con.renderComposite(compRule, cachedSrc, mask, dst, cachedX, cachedY,
 258                 maskX, maskY, dstX, dstY, width, height);
 259     }
 260 
 261     public void XRCompositeTraps(int dst, int srcX, int srcY,
 262             TrapezoidList trapList) {
 263         int renderReferenceX = 0;
 264         int renderReferenceY = 0;
 265 
 266         if (trapList.getP1YLeft(0) < trapList.getP2YLeft(0)) {
 267             renderReferenceX = trapList.getP1XLeft(0);
 268             renderReferenceY = trapList.getP1YLeft(0);
 269         } else {
 270             renderReferenceX = trapList.getP2XLeft(0);
 271             renderReferenceY = trapList.getP2YLeft(0);
 272         }
 273 
 274         renderReferenceX = (int) Math.floor(XRUtils
 275                 .XFixedToDouble(renderReferenceX));
 276         renderReferenceY = (int) Math.floor(XRUtils
 277                 .XFixedToDouble(renderReferenceY));
 278 
 279         con.renderCompositeTrapezoids(compRule, src.picture,
 280                 XRUtils.PictStandardA8, dst, renderReferenceX,
 281                 renderReferenceY, trapList);
 282     }
 283 
 284     public void XRRenderRectangles(XRSurfaceData dst, GrowableRectArray rects) {
 285         if (xorEnabled) {
 286             con.GCRectangles(dst.getXid(), dst.getGC(), rects);
 287         } else {
 288             if (rects.getSize() == 1) {
 289                 con.renderRectangle(dst.getPicture(), compRule, solidColor,
 290                         rects.getX(0), rects.getY(0), rects.getWidth(0), rects.getHeight(0));
 291             } else {
 292                 con.renderRectangles(dst.getPicture(), compRule, solidColor, rects);
 293             }
 294         }
 295     }
 296 


























 297     public void compositeBlit(XRSurfaceData src, XRSurfaceData dst, int sx,
 298             int sy, int dx, int dy, int w, int h) {
 299         con.renderComposite(compRule, src.picture, alphaMask, dst.picture, sx,
 300                 sy, 0, 0, dx, dy, w, h);
 301     }
 302 
 303     public void compositeText(XRSurfaceData dst, int sx, int sy,
 304             int glyphSet, int maskFormat, GrowableEltArray elts) {
 305         con.XRenderCompositeText(compRule, src.picture, dst.picture,





 306                 maskFormat, sx, sy, 0, 0, glyphSet, elts);
 307     }
 308 
 309     public XRColor getMaskColor() {
 310         return !isTexturePaintActive() ? XRColor.FULL_ALPHA : getAlphaColor();
 311     }
 312 
 313     public int getExtraAlphaMask() {
 314         return alphaMask;
 315     }
 316 
 317     public boolean isTexturePaintActive() {
 318         return src == texture;




 319     }
 320 
 321     public XRColor getAlphaColor() {
 322         return alphaColor;
 323     }
 324 
 325     public XRBackend getBackend() {
 326         return con;
 327     }
 328 
 329     public float getExtraAlpha() {
 330         return validatedExtraAlpha;
 331     }
 332 
 333     public byte getCompRule() {
 334         return compRule;
 335     }
 336 
 337     public XRTextRenderer getTextRenderer() {
 338         return textRenderer;


  31 import java.security.AccessController;
  32 import java.security.PrivilegedAction;
  33 
  34 import sun.font.*;
  35 import sun.java2d.*;
  36 import sun.java2d.jules.*;
  37 import sun.java2d.loops.*;
  38 
  39 /**
  40  * Manages per-application resources, e.g. the 1x1 pixmap used for solid color
  41  * fill as well as per-application state e.g. the currently set source picture
  42  * used for composition .
  43  *
  44  * @author Clemens Eisserer
  45  */
  46 
  47 public class XRCompositeManager {
  48     private static boolean enableGradCache = true;
  49     private static XRCompositeManager instance;
  50     
  51     private final static int SOLID = 0;
  52     private final static int TEXTURE = 1;
  53     private final static int GRADIENT = 2;
  54 
  55     int srcType;
  56     XRSolidSrcPict solidSrc32;
  57     XRSurfaceData texture;
  58     XRSurfaceData gradient;
  59     int alphaMask = XRUtils.None;
  60 
  61     XRColor solidColor = new XRColor();
  62     float extraAlpha = 1.0f;
  63     byte compRule = XRUtils.PictOpOver;
  64     XRColor alphaColor = new XRColor();
  65 
  66     XRSurfaceData solidSrcPict;
  67     int alphaMaskPict;
  68     int gradCachePixmap;
  69     int gradCachePicture;
  70 
  71     boolean xorEnabled = false;
  72     int validatedPixel = 0;
  73     Composite validatedComp;
  74     Paint validatedPaint;
  75     float validatedExtraAlpha = 1.0f;
  76 
  77     XRBackend con;
  78     MaskTileManager maskBuffer;
  79     XRTextRenderer textRenderer;
  80     XRMaskImage maskImage;
  81 
  82     public static synchronized XRCompositeManager getInstance(
  83             XRSurfaceData surface) {
  84         if (instance == null) {
  85             instance = new XRCompositeManager(surface);
  86         }
  87         return instance;
  88     }
  89 
  90     private XRCompositeManager(XRSurfaceData surface) {
  91         con = new XRBackendNative();

  92 
  93         String gradProp =
  94             AccessController.doPrivileged(new PrivilegedAction<String>() {
  95                 public String run() {
  96                     return System.getProperty("sun.java2d.xrgradcache");
  97                 }
  98             });
  99 
 100         enableGradCache = gradProp == null ||
 101                           !(gradProp.equalsIgnoreCase("false") ||
 102                           gradProp.equalsIgnoreCase("f"));
 103 
 104         XRPaints.register(this);
 105 
 106         initResources(surface);
 107 
 108         maskBuffer = new MaskTileManager(this, surface.getXid());
 109         textRenderer = new XRTextRenderer(this);
 110         maskImage = new XRMaskImage(this, surface.getXid());
 111     }
 112 
 113     public void initResources(XRSurfaceData surface) {
 114         int parentXid = surface.getXid();
 115 
 116         solidSrc32 = new XRSolidSrcPict(con, parentXid);







 117         setForeground(0);
 118 
 119         int extraAlphaMask = con.createPixmap(parentXid, 8, 1, 1);
 120         alphaMaskPict = con.createPicture(extraAlphaMask,
 121                 XRUtils.PictStandardA8);
 122         con.setPictureRepeat(alphaMaskPict, XRUtils.RepeatNormal);
 123         con.renderRectangle(alphaMaskPict, XRUtils.PictOpClear,
 124                 XRColor.NO_ALPHA, 0, 0, 1, 1);
 125 
 126         if (enableGradCache) {
 127             gradCachePixmap = con.createPixmap(parentXid, 32,
 128                     MaskTileManager.MASK_SIZE, MaskTileManager.MASK_SIZE);
 129             gradCachePicture = con.createPicture(gradCachePixmap,
 130                     XRUtils.PictStandardARGB32);
 131         }
 132     }
 133 
 134     public void setForeground(int pixel) {
 135         solidColor.setColorValues(pixel, true);


 136     }
 137 
 138     public void setGradientPaint(XRSurfaceData gradient) {
 139         if (this.gradient != null) {
 140             con.freePicture(this.gradient.picture);
 141         }
 142         this.gradient = gradient;
 143         srcType = GRADIENT;
 144     }
 145 
 146     public void setTexturePaint(XRSurfaceData texture) {
 147         this.texture = texture;
 148         this.srcType = TEXTURE;
 149     }
 150 
 151     public void XRResetPaint() {
 152         srcType = SOLID;
 153     }
 154 
 155     public void validateCompositeState(Composite comp, AffineTransform xform,
 156             Paint paint, SunGraphics2D sg2d) {
 157         boolean updatePaint = (paint != validatedPaint) || paint == null;
 158 
 159         // validate composite
 160         if ((comp != validatedComp)) {
 161             if (comp != null) {
 162                 setComposite(comp);
 163             } else {
 164                 comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER);
 165                 setComposite(comp);
 166             }
 167             // the paint state is dependent on the composite state, so make
 168             // sure we update the color below
 169             updatePaint = true;
 170             validatedComp = comp;
 171         }
 172 
 173         if (sg2d != null && (validatedPixel != sg2d.pixel  || updatePaint)) {
 174             validatedPixel = sg2d.pixel;
 175             setForeground(validatedPixel);
 176         }
 177 
 178         // validate paint
 179         if (updatePaint) {
 180             if (paint != null && sg2d != null
 181                     && sg2d.paintState >= SunGraphics2D.PAINT_GRADIENT) {
 182                 XRPaints.setPaint(sg2d, paint);
 183             } else {
 184                 XRResetPaint();
 185             }
 186             validatedPaint = paint;
 187         }
 188 
 189         if (srcType != SOLID) {
 190             AffineTransform at = (AffineTransform) xform.clone();
 191             try {
 192                 at.invert();
 193             } catch (NoninvertibleTransformException e) {
 194                 at.setToIdentity();
 195             }
 196             getCurrentSource().validateAsSource(at, -1, -1);
 197         }
 198     }
 199 
 200     private void setComposite(Composite comp) {
 201         if (comp instanceof AlphaComposite) {
 202             AlphaComposite aComp = (AlphaComposite) comp;
 203             validatedExtraAlpha = aComp.getAlpha();
 204 
 205             this.compRule = XRUtils.j2dAlphaCompToXR(aComp.getRule());
 206             this.extraAlpha = validatedExtraAlpha;
 207 
 208             if (extraAlpha == 1.0f) {
 209                 alphaMask = XRUtils.None;
 210                 alphaColor.alpha = XRColor.FULL_ALPHA.alpha;
 211             } else {
 212                 alphaColor.alpha = XRColor
 213                         .byteToXRColorValue((int) (extraAlpha * 255));
 214                 alphaMask = alphaMaskPict;
 215                 con.renderRectangle(alphaMaskPict, XRUtils.PictOpSrc,
 216                         alphaColor, 0, 0, 1, 1);
 217             }
 218 
 219             xorEnabled = false;
 220         } else if (comp instanceof XORComposite) {
 221             /* XOR composite validation is handled in XRSurfaceData */
 222             xorEnabled = true;
 223         } else {
 224             throw new InternalError(
 225                     "Composite accaleration not implemented for: "
 226                             + comp.getClass().getName());
 227         }
 228     }
 229 
 230     public boolean maskRequired() {
 231         return (!xorEnabled)
 232                 && ((srcType != SOLID)
 233                         || (srcType == SOLID && (solidColor.alpha != 0xffff) || (extraAlpha != 1.0f)));
 234     }
 235 
 236     public void XRComposite(int src, int mask, int dst, int srcX, int srcY,
 237             int maskX, int maskY, int dstX, int dstY, int width, int height) {
 238         int cachedSrc = (src == XRUtils.None) ? getCurrentSource().picture : src;
 239         int cachedX = srcX;
 240         int cachedY = srcY;
 241 
 242         if (enableGradCache && gradient != null
 243                 && cachedSrc == gradient.picture) {
 244             con.renderComposite(XRUtils.PictOpSrc, gradient.picture,
 245                     XRUtils.None, gradCachePicture, srcX, srcY, 0, 0, 0, 0,
 246                     width, height);
 247             cachedX = 0;
 248             cachedY = 0;
 249             cachedSrc = gradCachePicture;
 250         }
 251 
 252         con.renderComposite(compRule, cachedSrc, mask, dst, cachedX, cachedY,
 253                 maskX, maskY, dstX, dstY, width, height);
 254     }
 255 
 256     public void XRCompositeTraps(int dst, int srcX, int srcY,
 257             TrapezoidList trapList) {
 258         int renderReferenceX = 0;
 259         int renderReferenceY = 0;
 260 
 261         if (trapList.getP1YLeft(0) < trapList.getP2YLeft(0)) {
 262             renderReferenceX = trapList.getP1XLeft(0);
 263             renderReferenceY = trapList.getP1YLeft(0);
 264         } else {
 265             renderReferenceX = trapList.getP2XLeft(0);
 266             renderReferenceY = trapList.getP2YLeft(0);
 267         }
 268 
 269         renderReferenceX = (int) Math.floor(XRUtils
 270                 .XFixedToDouble(renderReferenceX));
 271         renderReferenceY = (int) Math.floor(XRUtils
 272                 .XFixedToDouble(renderReferenceY));
 273 
 274         con.renderCompositeTrapezoids(compRule, getCurrentSource().picture,
 275                 XRUtils.PictStandardA8, dst, renderReferenceX,
 276                 renderReferenceY, trapList);
 277     }
 278 
 279     public void XRRenderRectangles(XRSurfaceData dst, GrowableRectArray rects) {        
 280         if (xorEnabled) {
 281             con.GCRectangles(dst.getXid(), dst.getGC(), rects);
 282         } else {
 283             if (rects.getSize() == 1) {
 284                 con.renderRectangle(dst.getPicture(), compRule, solidColor,
 285                         rects.getX(0), rects.getY(0), rects.getWidth(0), rects.getHeight(0));
 286             } else {
 287                 con.renderRectangles(dst.getPicture(), compRule, solidColor, rects);
 288             }
 289         }
 290     }
 291     
 292     public void XRCompositeRectangles(XRSurfaceData dst, GrowableRectArray rects) {
 293         int srcPict = getCurrentSource().picture;
 294         
 295         for(int i=0; i < rects.getSize(); i++) {
 296             int x = rects.getX(i);
 297             int y = rects.getY(i);
 298             int width = rects.getWidth(i);
 299             int height = rects.getHeight(i);
 300             
 301             con.renderComposite(compRule, srcPict, XRUtils.None, dst.picture, x, y, 0, 0, x, y, width, height);
 302         }
 303     }
 304     
 305     protected XRSurfaceData getCurrentSource() {
 306         switch(srcType) {
 307         case SOLID:
 308             return solidSrc32.prepareSrcPict(validatedPixel);
 309         case TEXTURE:
 310             return texture;
 311         case GRADIENT:
 312             return gradient;
 313         }
 314         
 315         return null;
 316     }
 317 
 318     public void compositeBlit(XRSurfaceData src, XRSurfaceData dst, int sx,
 319             int sy, int dx, int dy, int w, int h) {
 320         con.renderComposite(compRule, src.picture, alphaMask, dst.picture, sx,
 321                 sy, 0, 0, dx, dy, w, h);
 322     }
 323 
 324     public void compositeText(XRSurfaceData dst, int sx, int sy, int glyphSet,
 325             int maskFormat, GrowableEltArray elts) {
 326         /*
 327          * Try to emulate the SRC blend mode with SRC_OVER.
 328          * We bail out during pipe validation for cases where this is not possible.
 329          */
 330         byte textCompRule = (compRule != XRUtils.PictOpSrc) ? compRule : XRUtils.PictOpOver;
 331         con.XRenderCompositeText(textCompRule, getCurrentSource().picture, dst.picture,
 332                 maskFormat, sx, sy, 0, 0, glyphSet, elts);
 333     }
 334 
 335     public XRColor getMaskColor() {
 336         return !isTexturePaintActive() ? XRColor.FULL_ALPHA : getAlphaColor();
 337     }
 338 
 339     public int getExtraAlphaMask() {
 340         return alphaMask;
 341     }
 342 
 343     public boolean isTexturePaintActive() {
 344         return srcType == TEXTURE;
 345     }
 346     
 347     public boolean isSolidPaintActive() {
 348         return srcType == SOLID;
 349     }
 350 
 351     public XRColor getAlphaColor() {
 352         return alphaColor;
 353     }
 354 
 355     public XRBackend getBackend() {
 356         return con;
 357     }
 358 
 359     public float getExtraAlpha() {
 360         return validatedExtraAlpha;
 361     }
 362 
 363     public byte getCompRule() {
 364         return compRule;
 365     }
 366 
 367     public XRTextRenderer getTextRenderer() {
 368         return textRenderer;