Path: blob/master/src/java.desktop/share/classes/sun/java2d/loops/Blit.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.Composite;28import java.awt.CompositeContext;29import java.awt.RenderingHints;30import java.awt.image.ColorModel;31import java.awt.image.Raster;32import java.awt.image.WritableRaster;33import java.lang.ref.WeakReference;3435import sun.java2d.SurfaceData;36import sun.java2d.pipe.Region;37import sun.java2d.pipe.SpanIterator;3839/**40* Blit41* 1) copies rectangle of pixels from one surface to another42* 2) performs compositing of colors based upon a Composite43* parameter44*45* precise behavior is undefined if the source surface46* and the destination surface are the same surface47* with overlapping regions of pixels48*/4950public class Blit extends GraphicsPrimitive51{52public static final String methodSignature = "Blit(...)".toString();5354public static final int primTypeID = makePrimTypeID();5556private static RenderCache blitcache = new RenderCache(20);5758public static Blit locate(SurfaceType srctype,59CompositeType comptype,60SurfaceType dsttype)61{62return (Blit)63GraphicsPrimitiveMgr.locate(primTypeID,64srctype, comptype, dsttype);65}6667public static Blit getFromCache(SurfaceType src,68CompositeType comp,69SurfaceType dst)70{71Object o = blitcache.get(src, comp, dst);72if (o != null) {73return (Blit) o;74}7576Blit blit = locate(src, comp, dst);77if (blit == null) {78System.out.println("blit loop not found for:");79System.out.println("src: "+src);80System.out.println("comp: "+comp);81System.out.println("dst: "+dst);82} else {83blitcache.put(src, comp, dst, blit);84}85return blit;86}8788protected Blit(SurfaceType srctype,89CompositeType comptype,90SurfaceType dsttype)91{92super(methodSignature, primTypeID, srctype, comptype, dsttype);93}9495public Blit(long pNativePrim,96SurfaceType srctype,97CompositeType comptype,98SurfaceType dsttype)99{100super(pNativePrim, methodSignature, primTypeID, srctype, comptype, dsttype);101}102103/**104* All Blit implementors must have this invoker method105*/106public native void Blit(SurfaceData src, SurfaceData dst,107Composite comp, Region clip,108int srcx, int srcy,109int dstx, int dsty,110int width, int height);111112static {113GraphicsPrimitiveMgr.registerGeneral(new Blit(null, null, null));114}115116protected GraphicsPrimitive makePrimitive(SurfaceType srctype,117CompositeType comptype,118SurfaceType dsttype)119{120/*121System.out.println("Constructing general blit for:");122System.out.println("src: "+srctype);123System.out.println("comp: "+comptype);124System.out.println("dst: "+dsttype);125*/126127if (comptype.isDerivedFrom(CompositeType.Xor)) {128GeneralXorBlit gxb = new GeneralXorBlit(srctype,129comptype,130dsttype);131setupGeneralBinaryOp(gxb);132return gxb;133} else if (comptype.isDerivedFrom(CompositeType.AnyAlpha)) {134return new GeneralMaskBlit(srctype, comptype, dsttype);135} else {136return AnyBlit.instance;137}138}139140private static class AnyBlit extends Blit {141public static AnyBlit instance = new AnyBlit();142143public AnyBlit() {144super(SurfaceType.Any, CompositeType.Any, SurfaceType.Any);145}146147public void Blit(SurfaceData srcData,148SurfaceData dstData,149Composite comp,150Region clip,151int srcx, int srcy,152int dstx, int dsty,153int width, int height)154{155ColorModel srcCM = srcData.getColorModel();156ColorModel dstCM = dstData.getColorModel();157// REMIND: Should get RenderingHints from sg2d158CompositeContext ctx = comp.createContext(srcCM, dstCM,159new RenderingHints(null));160Raster srcRas = srcData.getRaster(srcx, srcy, width, height);161WritableRaster dstRas =162(WritableRaster) dstData.getRaster(dstx, dsty, width, height);163164if (clip == null) {165clip = Region.getInstanceXYWH(dstx, dsty, width, height);166}167int[] span = {dstx, dsty, dstx+width, dsty+height};168SpanIterator si = clip.getSpanIterator(span);169srcx -= dstx;170srcy -= dsty;171while (si.nextSpan(span)) {172int w = span[2] - span[0];173int h = span[3] - span[1];174Raster tmpSrcRas = srcRas.createChild(srcx + span[0], srcy + span[1],175w, h, 0, 0, null);176WritableRaster tmpDstRas = dstRas.createWritableChild(span[0], span[1],177w, h, 0, 0, null);178ctx.compose(tmpSrcRas, tmpDstRas, tmpDstRas);179}180ctx.dispose();181}182}183184private static class GeneralMaskBlit extends Blit {185MaskBlit performop;186187public GeneralMaskBlit(SurfaceType srctype,188CompositeType comptype,189SurfaceType dsttype)190{191super(srctype, comptype, dsttype);192performop = MaskBlit.locate(srctype, comptype, dsttype);193}194195public void Blit(SurfaceData srcData,196SurfaceData dstData,197Composite comp,198Region clip,199int srcx, int srcy,200int dstx, int dsty,201int width, int height)202{203performop.MaskBlit(srcData, dstData, comp, clip,204srcx, srcy, dstx, dsty,205width, height,206null, 0, 0);207}208}209210private static class GeneralXorBlit211extends Blit212implements GeneralBinaryOp213{214Blit convertsrc;215Blit convertdst;216Blit performop;217Blit convertresult;218219WeakReference<SurfaceData> srcTmp;220WeakReference<SurfaceData> dstTmp;221222public GeneralXorBlit(SurfaceType srctype,223CompositeType comptype,224SurfaceType dsttype)225{226super(srctype, comptype, dsttype);227}228229public void setPrimitives(Blit srcconverter,230Blit dstconverter,231GraphicsPrimitive genericop,232Blit resconverter)233{234this.convertsrc = srcconverter;235this.convertdst = dstconverter;236this.performop = (Blit) genericop;237this.convertresult = resconverter;238}239240public synchronized void Blit(SurfaceData srcData,241SurfaceData dstData,242Composite comp,243Region clip,244int srcx, int srcy,245int dstx, int dsty,246int width, int height)247{248SurfaceData src, dst;249Region opclip;250int sx, sy, dx, dy;251252if (convertsrc == null) {253src = srcData;254sx = srcx;255sy = srcy;256} else {257SurfaceData cachedSrc = null;258if (srcTmp != null) {259cachedSrc = srcTmp.get();260}261src = convertFrom(convertsrc, srcData, srcx, srcy,262width, height, cachedSrc);263sx = 0;264sy = 0;265if (src != cachedSrc) {266srcTmp = new WeakReference<>(src);267}268}269270if (convertdst == null) {271dst = dstData;272dx = dstx;273dy = dsty;274opclip = clip;275} else {276// assert: convertresult != null277SurfaceData cachedDst = null;278if (dstTmp != null) {279cachedDst = dstTmp.get();280}281dst = convertFrom(convertdst, dstData, dstx, dsty,282width, height, cachedDst);283dx = 0;284dy = 0;285opclip = null;286if (dst != cachedDst) {287dstTmp = new WeakReference<>(dst);288}289}290291performop.Blit(src, dst, comp, opclip,292sx, sy, dx, dy,293width, height);294295if (convertresult != null) {296// assert: convertdst != null297convertTo(convertresult, dst, dstData, clip,298dstx, dsty, width, height);299}300}301}302303public GraphicsPrimitive traceWrap() {304return new TraceBlit(this);305}306307private static class TraceBlit extends Blit {308Blit target;309310public TraceBlit(Blit target) {311super(target.getSourceType(),312target.getCompositeType(),313target.getDestType());314this.target = target;315}316317public GraphicsPrimitive traceWrap() {318return this;319}320321public void Blit(SurfaceData src, SurfaceData dst,322Composite comp, Region clip,323int srcx, int srcy, int dstx, int dsty,324int width, int height)325{326tracePrimitive(target);327target.Blit(src, dst, comp, clip,328srcx, srcy, dstx, dsty, width, height);329}330}331}332333334