Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/java2d/xr/XRCompositeManager.java
32288 views
/*1* Copyright (c) 2010, 2013, 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.jules.*;36import sun.java2d.loops.*;3738/**39* Manages per-application resources, e.g. the 1x1 pixmap used for solid color40* fill as well as per-application state e.g. the currently set source picture41* used for composition .42*43* @author Clemens Eisserer44*/4546public class XRCompositeManager {47private static boolean enableGradCache = true;48private static XRCompositeManager instance;4950private final static int SOLID = 0;51private final static int TEXTURE = 1;52private final static int GRADIENT = 2;5354int srcType;55XRSolidSrcPict solidSrc32;56XRSurfaceData texture;57XRSurfaceData gradient;58int alphaMask = XRUtils.None;5960XRColor solidColor = new XRColor();61float extraAlpha = 1.0f;62byte compRule = XRUtils.PictOpOver;63XRColor alphaColor = new XRColor();6465XRSurfaceData solidSrcPict;66int alphaMaskPict;67int gradCachePixmap;68int gradCachePicture;6970boolean xorEnabled = false;71int validatedPixel = 0;72Composite validatedComp;73Paint validatedPaint;74float validatedExtraAlpha = 1.0f;7576XRBackend con;77MaskTileManager maskBuffer;78XRTextRenderer textRenderer;79XRMaskImage maskImage;8081public static synchronized XRCompositeManager getInstance(82XRSurfaceData surface) {83if (instance == null) {84instance = new XRCompositeManager(surface);85}86return instance;87}8889private XRCompositeManager(XRSurfaceData surface) {90con = new XRBackendNative();9192String 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, true);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 XRCompositeTraps(int dst, int srcX, int srcY,256TrapezoidList trapList) {257int renderReferenceX = 0;258int renderReferenceY = 0;259260if (trapList.getP1YLeft(0) < trapList.getP2YLeft(0)) {261renderReferenceX = trapList.getP1XLeft(0);262renderReferenceY = trapList.getP1YLeft(0);263} else {264renderReferenceX = trapList.getP2XLeft(0);265renderReferenceY = trapList.getP2YLeft(0);266}267268renderReferenceX = (int) Math.floor(XRUtils269.XFixedToDouble(renderReferenceX));270renderReferenceY = (int) Math.floor(XRUtils271.XFixedToDouble(renderReferenceY));272273con.renderCompositeTrapezoids(compRule, getCurrentSource().picture,274XRUtils.PictStandardA8, dst, renderReferenceX,275renderReferenceY, trapList);276}277278public void XRRenderRectangles(XRSurfaceData dst, GrowableRectArray rects) {279if (xorEnabled) {280con.GCRectangles(dst.getXid(), dst.getGC(), rects);281} else {282if (rects.getSize() == 1) {283con.renderRectangle(dst.getPicture(), compRule, solidColor,284rects.getX(0), rects.getY(0), rects.getWidth(0), rects.getHeight(0));285} else {286con.renderRectangles(dst.getPicture(), compRule, solidColor, rects);287}288}289}290291public void XRCompositeRectangles(XRSurfaceData dst, GrowableRectArray rects) {292int srcPict = getCurrentSource().picture;293294for(int i=0; i < rects.getSize(); i++) {295int x = rects.getX(i);296int y = rects.getY(i);297int width = rects.getWidth(i);298int height = rects.getHeight(i);299300con.renderComposite(compRule, srcPict, XRUtils.None, dst.picture, x, y, 0, 0, x, y, width, height);301}302}303304protected XRSurfaceData getCurrentSource() {305switch(srcType) {306case SOLID:307return solidSrc32.prepareSrcPict(validatedPixel);308case TEXTURE:309return texture;310case GRADIENT:311return gradient;312}313314return null;315}316317public void compositeBlit(XRSurfaceData src, XRSurfaceData dst, int sx,318int sy, int dx, int dy, int w, int h) {319con.renderComposite(compRule, src.picture, alphaMask, dst.picture, sx,320sy, 0, 0, dx, dy, w, h);321}322323public void compositeText(XRSurfaceData dst, int sx, int sy, int glyphSet,324int maskFormat, GrowableEltArray elts) {325/*326* Try to emulate the SRC blend mode with SRC_OVER.327* We bail out during pipe validation for cases where this is not possible.328*/329byte textCompRule = (compRule != XRUtils.PictOpSrc) ? compRule : XRUtils.PictOpOver;330con.XRenderCompositeText(textCompRule, getCurrentSource().picture, dst.picture,331maskFormat, sx, sy, 0, 0, glyphSet, elts);332}333334public XRColor getMaskColor() {335return !isTexturePaintActive() ? XRColor.FULL_ALPHA : getAlphaColor();336}337338public int getExtraAlphaMask() {339return alphaMask;340}341342public boolean isTexturePaintActive() {343return srcType == TEXTURE;344}345346public boolean isSolidPaintActive() {347return srcType == SOLID;348}349350public XRColor getAlphaColor() {351return alphaColor;352}353354public XRBackend getBackend() {355return con;356}357358public float getExtraAlpha() {359return validatedExtraAlpha;360}361362public byte getCompRule() {363return compRule;364}365366public XRTextRenderer getTextRenderer() {367return textRenderer;368}369370public MaskTileManager getMaskBuffer() {371return maskBuffer;372}373374public XRMaskImage getMaskImage() {375return maskImage;376}377}378379380