Path: blob/master/src/java.desktop/share/classes/sun/java2d/loops/BlitBg.java
41159 views
/*1* Copyright (c) 1999, 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.loops;2627import java.awt.AlphaComposite;28import java.awt.Color;29import java.awt.Composite;30import java.awt.Font;31import java.awt.image.BufferedImage;32import java.awt.image.ColorModel;33import java.awt.image.WritableRaster;3435import sun.awt.image.BufImgSurfaceData;36import sun.java2d.SunGraphics2D;37import sun.java2d.SurfaceData;38import sun.java2d.pipe.Region;3940/**41* BlitBg42* 1) copies rectangle of pixels from one surface to another43* 2) performs compositing of colors based upon a Composite44* parameter45* 3) assumes that non-opaque pixels are to be blended with46* the indicated Bg color before compositing with the47* destination48*49* precise behavior is undefined if the source surface50* and the destination surface are the same surface51* with overlapping regions of pixels52*/53public class BlitBg extends GraphicsPrimitive54{55public static final String methodSignature = "BlitBg(...)".toString();5657public static final int primTypeID = makePrimTypeID();5859private static RenderCache blitcache = new RenderCache(20);6061public static BlitBg locate(SurfaceType srctype,62CompositeType comptype,63SurfaceType dsttype)64{65return (BlitBg)66GraphicsPrimitiveMgr.locate(primTypeID,67srctype, comptype, dsttype);68}6970public static BlitBg getFromCache(SurfaceType src,71CompositeType comp,72SurfaceType dst)73{74Object o = blitcache.get(src, comp, dst);75if (o != null) {76return (BlitBg) o;77}78BlitBg blit = locate(src, comp, dst);79if (blit == null) {80System.out.println("blitbg loop not found for:");81System.out.println("src: "+src);82System.out.println("comp: "+comp);83System.out.println("dst: "+dst);84} else {85blitcache.put(src, comp, dst, blit);86}87return blit;88}8990protected BlitBg(SurfaceType srctype,91CompositeType comptype,92SurfaceType dsttype)93{94super(methodSignature, primTypeID, srctype, comptype, dsttype);95}9697public BlitBg(long pNativePrim,98SurfaceType srctype,99CompositeType comptype,100SurfaceType dsttype)101{102super(pNativePrim, methodSignature, primTypeID, srctype, comptype, dsttype);103}104105/**106* All BlitBg implementors must have this invoker method107*/108public native void BlitBg(SurfaceData src, SurfaceData dst,109Composite comp, Region clip,110int bgColor,111int srcx, int srcy,112int dstx, int dsty,113int width, int height);114115static {116GraphicsPrimitiveMgr.registerGeneral(new BlitBg(null, null, null));117}118119protected GraphicsPrimitive makePrimitive(SurfaceType srctype,120CompositeType comptype,121SurfaceType dsttype)122{123/*124System.out.println("Constructing general blitbg for:");125System.out.println("src: "+srctype);126System.out.println("comp: "+comptype);127System.out.println("dst: "+dsttype);128*/129return new General(srctype, comptype, dsttype);130}131132private static class General extends BlitBg {133CompositeType compositeType;134135public General(SurfaceType srctype,136CompositeType comptype,137SurfaceType dsttype)138{139super(srctype, comptype, dsttype);140compositeType = comptype;141}142143@Override144public void BlitBg(SurfaceData srcData,145SurfaceData dstData,146Composite comp,147Region clip,148int bgArgb,149int srcx, int srcy,150int dstx, int dsty,151int width, int height)152{153ColorModel dstModel = dstData.getColorModel();154boolean bgHasAlpha = (bgArgb >>> 24) != 0xff;155if (!dstModel.hasAlpha() && bgHasAlpha) {156dstModel = ColorModel.getRGBdefault();157}158WritableRaster wr =159dstModel.createCompatibleWritableRaster(width, height);160boolean isPremult = dstModel.isAlphaPremultiplied();161BufferedImage bimg =162new BufferedImage(dstModel, wr, isPremult, null);163SurfaceData tmpData = BufImgSurfaceData.createData(bimg);164Color bgColor = new Color(bgArgb, bgHasAlpha);165SunGraphics2D sg2d = new SunGraphics2D(tmpData, bgColor, bgColor,166defaultFont);167FillRect fillop = FillRect.locate(SurfaceType.AnyColor,168CompositeType.SrcNoEa,169tmpData.getSurfaceType());170Blit combineop = Blit.getFromCache(srcData.getSurfaceType(),171CompositeType.SrcOverNoEa,172tmpData.getSurfaceType());173Blit blitop = Blit.getFromCache(tmpData.getSurfaceType(), compositeType,174dstData.getSurfaceType());175fillop.FillRect(sg2d, tmpData, 0, 0, width, height);176combineop.Blit(srcData, tmpData, AlphaComposite.SrcOver, null,177srcx, srcy, 0, 0, width, height);178blitop.Blit(tmpData, dstData, comp, clip,1790, 0, dstx, dsty, width, height);180}181182private static Font defaultFont = new Font("Dialog", Font.PLAIN, 12);183}184185public GraphicsPrimitive traceWrap() {186return new TraceBlitBg(this);187}188189private static class TraceBlitBg extends BlitBg {190BlitBg target;191192public TraceBlitBg(BlitBg target) {193super(target.getSourceType(),194target.getCompositeType(),195target.getDestType());196this.target = target;197}198199public GraphicsPrimitive traceWrap() {200return this;201}202203@Override204public void BlitBg(SurfaceData src, SurfaceData dst,205Composite comp, Region clip,206int bgColor,207int srcx, int srcy, int dstx, int dsty,208int width, int height)209{210tracePrimitive(target);211target.BlitBg(src, dst, comp, clip, bgColor,212srcx, srcy, dstx, dsty, width, height);213}214}215}216217218