Path: blob/master/src/java.desktop/unix/classes/sun/java2d/xr/XRCompositeManager.java
41159 views
/*1* Copyright (c) 2010, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.xr;2627import java.awt.*;28import java.awt.geom.*;2930import java.security.AccessController;31import java.security.PrivilegedAction;3233import sun.font.*;34import sun.java2d.*;35import sun.java2d.loops.*;3637/**38* Manages per-application resources, e.g. the 1x1 pixmap used for solid color39* fill as well as per-application state e.g. the currently set source picture40* used for composition .41*42* @author Clemens Eisserer43*/4445public class XRCompositeManager {46private static boolean enableGradCache = true;47private static XRCompositeManager instance;4849private static final int SOLID = 0;50private static final int TEXTURE = 1;51private static final int GRADIENT = 2;5253int srcType;54XRSolidSrcPict solidSrc32;55XRSurfaceData texture;56XRSurfaceData gradient;57int alphaMask = XRUtils.None;5859XRColor solidColor = new XRColor();60float extraAlpha = 1.0f;61byte compRule = XRUtils.PictOpOver;62XRColor alphaColor = new XRColor();6364XRSurfaceData solidSrcPict;65int alphaMaskPict;66int gradCachePixmap;67int gradCachePicture;6869boolean xorEnabled = false;70int validatedPixel = 0;71Composite validatedComp;72Paint validatedPaint;73float validatedExtraAlpha = 1.0f;7475XRBackend con;76MaskTileManager maskBuffer;77XRTextRenderer textRenderer;78XRMaskImage maskImage;7980public static synchronized XRCompositeManager getInstance(81XRSurfaceData surface) {82if (instance == null) {83instance = new XRCompositeManager(surface);84}85return instance;86}8788private XRCompositeManager(XRSurfaceData surface) {89con = new XRBackendNative();9091@SuppressWarnings("removal")92String gradProp =93AccessController.doPrivileged(new PrivilegedAction<String>() {94public String run() {95return System.getProperty("sun.java2d.xrgradcache");96}97});9899enableGradCache = gradProp == null ||100!(gradProp.equalsIgnoreCase("false") ||101gradProp.equalsIgnoreCase("f"));102103XRPaints.register(this);104105initResources(surface);106107maskBuffer = new MaskTileManager(this, surface.getXid());108textRenderer = new XRTextRenderer(this);109maskImage = new XRMaskImage(this, surface.getXid());110}111112public void initResources(XRSurfaceData surface) {113int parentXid = surface.getXid();114115solidSrc32 = new XRSolidSrcPict(con, parentXid);116setForeground(0);117118int extraAlphaMask = con.createPixmap(parentXid, 8, 1, 1);119alphaMaskPict = con.createPicture(extraAlphaMask,120XRUtils.PictStandardA8);121con.setPictureRepeat(alphaMaskPict, XRUtils.RepeatNormal);122con.renderRectangle(alphaMaskPict, XRUtils.PictOpClear,123XRColor.NO_ALPHA, 0, 0, 1, 1);124125if (enableGradCache) {126gradCachePixmap = con.createPixmap(parentXid, 32,127MaskTileManager.MASK_SIZE, MaskTileManager.MASK_SIZE);128gradCachePicture = con.createPicture(gradCachePixmap,129XRUtils.PictStandardARGB32);130}131}132133public void setForeground(int pixel) {134solidColor.setColorValues(pixel);135}136137public void setGradientPaint(XRSurfaceData gradient) {138if (this.gradient != null) {139con.freePicture(this.gradient.picture);140}141this.gradient = gradient;142srcType = GRADIENT;143}144145public void setTexturePaint(XRSurfaceData texture) {146this.texture = texture;147this.srcType = TEXTURE;148}149150public void XRResetPaint() {151srcType = SOLID;152}153154public void validateCompositeState(Composite comp, AffineTransform xform,155Paint paint, SunGraphics2D sg2d) {156boolean updatePaint = (paint != validatedPaint) || paint == null;157158// validate composite159if ((comp != validatedComp)) {160if (comp != null) {161setComposite(comp);162} else {163comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER);164setComposite(comp);165}166// the paint state is dependent on the composite state, so make167// sure we update the color below168updatePaint = true;169validatedComp = comp;170}171172if (sg2d != null && (validatedPixel != sg2d.pixel || updatePaint)) {173validatedPixel = sg2d.pixel;174setForeground(validatedPixel);175}176177// validate paint178if (updatePaint) {179if (paint != null && sg2d != null180&& sg2d.paintState >= SunGraphics2D.PAINT_GRADIENT) {181XRPaints.setPaint(sg2d, paint);182} else {183XRResetPaint();184}185validatedPaint = paint;186}187188if (srcType != SOLID) {189AffineTransform at = (AffineTransform) xform.clone();190try {191at.invert();192} catch (NoninvertibleTransformException e) {193at.setToIdentity();194}195getCurrentSource().validateAsSource(at, -1, XRUtils.ATransOpToXRQuality(sg2d.interpolationType));196}197}198199private void setComposite(Composite comp) {200if (comp instanceof AlphaComposite) {201AlphaComposite aComp = (AlphaComposite) comp;202validatedExtraAlpha = aComp.getAlpha();203204this.compRule = XRUtils.j2dAlphaCompToXR(aComp.getRule());205this.extraAlpha = validatedExtraAlpha;206207if (extraAlpha == 1.0f) {208alphaMask = XRUtils.None;209alphaColor.alpha = XRColor.FULL_ALPHA.alpha;210} else {211alphaColor.alpha = XRColor212.byteToXRColorValue((int) (extraAlpha * 255));213alphaMask = alphaMaskPict;214con.renderRectangle(alphaMaskPict, XRUtils.PictOpSrc,215alphaColor, 0, 0, 1, 1);216}217218xorEnabled = false;219} else if (comp instanceof XORComposite) {220/* XOR composite validation is handled in XRSurfaceData */221xorEnabled = true;222} else {223throw new InternalError(224"Composite accaleration not implemented for: "225+ comp.getClass().getName());226}227}228229public boolean maskRequired() {230return (!xorEnabled)231&& ((srcType != SOLID)232|| (srcType == SOLID && (solidColor.alpha != 0xffff) || (extraAlpha != 1.0f)));233}234235public void XRComposite(int src, int mask, int dst, int srcX, int srcY,236int maskX, int maskY, int dstX, int dstY, int width, int height) {237int cachedSrc = (src == XRUtils.None) ? getCurrentSource().picture : src;238int cachedX = srcX;239int cachedY = srcY;240241if (enableGradCache && gradient != null242&& cachedSrc == gradient.picture) {243con.renderComposite(XRUtils.PictOpSrc, gradient.picture,244XRUtils.None, gradCachePicture, srcX, srcY, 0, 0, 0, 0,245width, height);246cachedX = 0;247cachedY = 0;248cachedSrc = gradCachePicture;249}250251con.renderComposite(compRule, cachedSrc, mask, dst, cachedX, cachedY,252maskX, maskY, dstX, dstY, width, height);253}254255public void XRRenderRectangles(XRSurfaceData dst, GrowableRectArray rects) {256if (xorEnabled) {257con.GCRectangles(dst.getXid(), dst.getGC(), rects);258} else {259if (rects.getSize() == 1) {260con.renderRectangle(dst.getPicture(), compRule, solidColor,261rects.getX(0), rects.getY(0), rects.getWidth(0), rects.getHeight(0));262} else {263con.renderRectangles(dst.getPicture(), compRule, solidColor, rects);264}265}266}267268public void XRCompositeRectangles(XRSurfaceData dst, GrowableRectArray rects) {269int srcPict = getCurrentSource().picture;270271for(int i=0; i < rects.getSize(); i++) {272int x = rects.getX(i);273int y = rects.getY(i);274int width = rects.getWidth(i);275int height = rects.getHeight(i);276277con.renderComposite(compRule, srcPict, XRUtils.None, dst.picture, x, y, 0, 0, x, y, width, height);278}279}280281protected XRSurfaceData getCurrentSource() {282switch(srcType) {283case SOLID:284return solidSrc32.prepareSrcPict(validatedPixel);285case TEXTURE:286return texture;287case GRADIENT:288return gradient;289}290291return null;292}293294public void compositeBlit(XRSurfaceData src, XRSurfaceData dst, int sx,295int sy, int dx, int dy, int w, int h) {296con.renderComposite(compRule, src.picture, alphaMask, dst.picture, sx,297sy, 0, 0, dx, dy, w, h);298}299300public void compositeText(XRSurfaceData dst, int sx, int sy, int glyphSet,301int maskFormat, GrowableEltArray elts) {302/*303* Try to emulate the SRC blend mode with SRC_OVER.304* We bail out during pipe validation for cases where this is not possible.305*/306byte textCompRule = (compRule != XRUtils.PictOpSrc) ? compRule : XRUtils.PictOpOver;307con.XRenderCompositeText(textCompRule, getCurrentSource().picture, dst.picture,308maskFormat, sx, sy, 0, 0, glyphSet, elts);309}310311public XRColor getMaskColor() {312return !isTexturePaintActive() ? XRColor.FULL_ALPHA : getAlphaColor();313}314315public int getExtraAlphaMask() {316return alphaMask;317}318319public boolean isTexturePaintActive() {320return srcType == TEXTURE;321}322323public boolean isSolidPaintActive() {324return srcType == SOLID;325}326327public XRColor getAlphaColor() {328return alphaColor;329}330331public XRBackend getBackend() {332return con;333}334335public float getExtraAlpha() {336return validatedExtraAlpha;337}338339public byte getCompRule() {340return compRule;341}342343public XRTextRenderer getTextRenderer() {344return textRenderer;345}346347public MaskTileManager getMaskBuffer() {348return maskBuffer;349}350351public XRMaskImage getMaskImage() {352return maskImage;353}354}355356357