Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/loops/BlitBg.java
38918 views
/*1* Copyright (c) 1999, 2008, 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.loops;2627import java.awt.Font;28import java.awt.Color;29import java.awt.Composite;30import java.awt.AlphaComposite;31import java.awt.Transparency;32import java.awt.image.ColorModel;33import java.awt.image.WritableRaster;34import java.awt.image.BufferedImage;35import sun.awt.image.BufImgSurfaceData;36import sun.java2d.loops.GraphicsPrimitive;37import sun.java2d.SurfaceData;38import sun.java2d.SunGraphics2D;39import sun.java2d.pipe.Region;4041/**42* BlitBg43* 1) copies rectangle of pixels from one surface to another44* 2) performs compositing of colors based upon a Composite45* parameter46* 3) assumes that non-opaque pixels are to be blended with47* the indicated Bg color before compositing with the48* destination49*50* precise behavior is undefined if the source surface51* and the destination surface are the same surface52* with overlapping regions of pixels53*/54public class BlitBg extends GraphicsPrimitive55{56public static final String methodSignature = "BlitBg(...)".toString();5758public static final int primTypeID = makePrimTypeID();5960private static RenderCache blitcache = new RenderCache(20);6162public static BlitBg locate(SurfaceType srctype,63CompositeType comptype,64SurfaceType dsttype)65{66return (BlitBg)67GraphicsPrimitiveMgr.locate(primTypeID,68srctype, comptype, dsttype);69}7071public static BlitBg getFromCache(SurfaceType src,72CompositeType comp,73SurfaceType dst)74{75Object o = blitcache.get(src, comp, dst);76if (o != null) {77return (BlitBg) o;78}79BlitBg blit = locate(src, comp, dst);80if (blit == null) {81System.out.println("blitbg loop not found for:");82System.out.println("src: "+src);83System.out.println("comp: "+comp);84System.out.println("dst: "+dst);85} else {86blitcache.put(src, comp, dst, blit);87}88return blit;89}9091protected BlitBg(SurfaceType srctype,92CompositeType comptype,93SurfaceType dsttype)94{95super(methodSignature, primTypeID, srctype, comptype, dsttype);96}9798public BlitBg(long pNativePrim,99SurfaceType srctype,100CompositeType comptype,101SurfaceType dsttype)102{103super(pNativePrim, methodSignature, primTypeID, srctype, comptype, dsttype);104}105106/**107* All BlitBg implementors must have this invoker method108*/109public native void BlitBg(SurfaceData src, SurfaceData dst,110Composite comp, Region clip,111int bgColor,112int srcx, int srcy,113int dstx, int dsty,114int width, int height);115116static {117GraphicsPrimitiveMgr.registerGeneral(new BlitBg(null, null, null));118}119120public GraphicsPrimitive makePrimitive(SurfaceType srctype,121CompositeType comptype,122SurfaceType dsttype)123{124/*125System.out.println("Constructing general blitbg for:");126System.out.println("src: "+srctype);127System.out.println("comp: "+comptype);128System.out.println("dst: "+dsttype);129*/130return new General(srctype, comptype, dsttype);131}132133private static class General extends BlitBg {134CompositeType compositeType;135136public General(SurfaceType srctype,137CompositeType comptype,138SurfaceType dsttype)139{140super(srctype, comptype, dsttype);141compositeType = comptype;142}143144@Override145public void BlitBg(SurfaceData srcData,146SurfaceData dstData,147Composite comp,148Region clip,149int bgArgb,150int srcx, int srcy,151int dstx, int dsty,152int width, int height)153{154ColorModel dstModel = dstData.getColorModel();155boolean bgHasAlpha = (bgArgb >>> 24) != 0xff;156if (!dstModel.hasAlpha() && bgHasAlpha) {157dstModel = ColorModel.getRGBdefault();158}159WritableRaster wr =160dstModel.createCompatibleWritableRaster(width, height);161boolean isPremult = dstModel.isAlphaPremultiplied();162BufferedImage bimg =163new BufferedImage(dstModel, wr, isPremult, null);164SurfaceData tmpData = BufImgSurfaceData.createData(bimg);165Color bgColor = new Color(bgArgb, bgHasAlpha);166SunGraphics2D sg2d = new SunGraphics2D(tmpData, bgColor, bgColor,167defaultFont);168FillRect fillop = FillRect.locate(SurfaceType.AnyColor,169CompositeType.SrcNoEa,170tmpData.getSurfaceType());171Blit combineop = Blit.getFromCache(srcData.getSurfaceType(),172CompositeType.SrcOverNoEa,173tmpData.getSurfaceType());174Blit blitop = Blit.getFromCache(tmpData.getSurfaceType(), compositeType,175dstData.getSurfaceType());176fillop.FillRect(sg2d, tmpData, 0, 0, width, height);177combineop.Blit(srcData, tmpData, AlphaComposite.SrcOver, null,178srcx, srcy, 0, 0, width, height);179blitop.Blit(tmpData, dstData, comp, clip,1800, 0, dstx, dsty, width, height);181}182183private static Font defaultFont = new Font("Dialog", Font.PLAIN, 12);184}185186public GraphicsPrimitive traceWrap() {187return new TraceBlitBg(this);188}189190private static class TraceBlitBg extends BlitBg {191BlitBg target;192193public TraceBlitBg(BlitBg target) {194super(target.getSourceType(),195target.getCompositeType(),196target.getDestType());197this.target = target;198}199200public GraphicsPrimitive traceWrap() {201return this;202}203204@Override205public void BlitBg(SurfaceData src, SurfaceData dst,206Composite comp, Region clip,207int bgColor,208int srcx, int srcy, int dstx, int dsty,209int width, int height)210{211tracePrimitive(target);212target.BlitBg(src, dst, comp, clip, bgColor,213srcx, srcy, dstx, dsty, width, height);214}215}216}217218219