Path: blob/master/src/java.desktop/share/classes/sun/java2d/pipe/BufferedContext.java
41159 views
/*1* Copyright (c) 2005, 2019, 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.pipe;2627import java.awt.AlphaComposite;28import java.awt.Color;29import java.awt.Composite;30import java.awt.Paint;31import java.awt.geom.AffineTransform;32import java.lang.annotation.Native;33import java.lang.ref.Reference;34import java.lang.ref.WeakReference;3536import sun.java2d.InvalidPipeException;37import sun.java2d.SunGraphics2D;38import sun.java2d.loops.XORComposite;39import sun.java2d.pipe.hw.AccelSurface;4041import static sun.java2d.pipe.BufferedOpCodes.BEGIN_SHAPE_CLIP;42import static sun.java2d.pipe.BufferedOpCodes.END_SHAPE_CLIP;43import static sun.java2d.pipe.BufferedOpCodes.RESET_CLIP;44import static sun.java2d.pipe.BufferedOpCodes.RESET_COMPOSITE;45import static sun.java2d.pipe.BufferedOpCodes.RESET_TRANSFORM;46import static sun.java2d.pipe.BufferedOpCodes.SET_ALPHA_COMPOSITE;47import static sun.java2d.pipe.BufferedOpCodes.SET_RECT_CLIP;48import static sun.java2d.pipe.BufferedOpCodes.SET_SHAPE_CLIP_SPANS;49import static sun.java2d.pipe.BufferedOpCodes.SET_SURFACES;50import static sun.java2d.pipe.BufferedOpCodes.SET_TRANSFORM;51import static sun.java2d.pipe.BufferedOpCodes.SET_XOR_COMPOSITE;52import static sun.java2d.pipe.BufferedRenderPipe.BYTES_PER_SPAN;5354/**55* Base context class for managing state in a single-threaded rendering56* environment. Each state-setting operation (e.g. SET_COLOR) is added to57* the provided RenderQueue, which will be processed at a later time by a58* single thread. Note that the RenderQueue lock must be acquired before59* calling the validate() method (or any other method in this class). See60* the RenderQueue class comments for a sample usage scenario.61*62* @see RenderQueue63*/64public abstract class BufferedContext {6566/*67* The following flags help the internals of validate() determine68* the appropriate (meaning correct, or optimal) code path when69* setting up the current context. The flags can be bitwise OR'd70* together as needed.71*/7273/**74* Indicates that no flags are needed; take all default code paths.75*/76@Native public static final int NO_CONTEXT_FLAGS = (0 << 0);77/**78* Indicates that the source surface (or color value, if it is a simple79* rendering operation) is opaque (has an alpha value of 1.0). If this80* flag is present, it allows us to disable blending in certain81* situations in order to improve performance.82*/83@Native public static final int SRC_IS_OPAQUE = (1 << 0);84/**85* Indicates that the operation uses an alpha mask, which may determine86* the code path that is used when setting up the current paint state.87*/88@Native public static final int USE_MASK = (1 << 1);8990private final RenderQueue rq;91private final RenderBuffer buf;9293/**94* This is a reference to the most recently validated BufferedContext. If95* this value is null, it means that there is no current context. It is96* provided here so that validate() only needs to do a quick reference97* check to see if the BufferedContext passed to that method is the same98* as the one we've cached here.99*/100protected static BufferedContext currentContext;101102private Reference<AccelSurface> validSrcDataRef = new WeakReference<>(null);103private Reference<AccelSurface> validDstDataRef = new WeakReference<>(null);104private Reference<Region> validClipRef = new WeakReference<>(null);105private Reference<Composite> validCompRef = new WeakReference<>(null);106private Reference<Paint> validPaintRef = new WeakReference<>(null);107// renamed from isValidatedPaintAColor as part of a work around for 6764257108private boolean isValidatedPaintJustAColor;109private int validatedRGB;110private int validatedFlags;111private boolean xformInUse;112private AffineTransform transform;113114protected BufferedContext(RenderQueue rq) {115this.rq = rq;116this.buf = rq.getBuffer();117}118119/**120* Fetches the BufferedContextContext associated with the dst. surface121* and validates the context using the given parameters. Most rendering122* operations will call this method first in order to set the necessary123* state before issuing rendering commands.124*125* Note: must be called while the RenderQueue lock is held.126*127* It's assumed that the type of surfaces has been checked by the Renderer128*129* @throws InvalidPipeException if either src or dest surface is not valid130* or lost131* @see RenderQueue#lock132* @see RenderQueue#unlock133*/134public static void validateContext(AccelSurface srcData,135AccelSurface dstData,136Region clip, Composite comp,137AffineTransform xform,138Paint paint, SunGraphics2D sg2d,139int flags)140{141// assert rq.lock.isHeldByCurrentThread();142BufferedContext context = dstData.getContext();143context.validate(srcData, dstData,144clip, comp, xform, paint, sg2d, flags);145}146147/**148* Fetches the BufferedContextassociated with the surface149* and disables all context state settings.150*151* Note: must be called while the RenderQueue lock is held.152*153* It's assumed that the type of surfaces has been checked by the Renderer154*155* @throws InvalidPipeException if the surface is not valid156* or lost157* @see RenderQueue#lock158* @see RenderQueue#unlock159*/160public static void validateContext(AccelSurface surface) {161// assert rt.lock.isHeldByCurrentThread();162validateContext(surface, surface,163null, null, null, null, null, NO_CONTEXT_FLAGS);164}165166/**167* Validates the given parameters against the current state for this168* context. If this context is not current, it will be made current169* for the given source and destination surfaces, and the viewport will170* be updated. Then each part of the context state (clip, composite,171* etc.) is checked against the previous value. If the value has changed172* since the last call to validate(), it will be updated accordingly.173*174* Note that the SunGraphics2D parameter is only used for the purposes175* of validating a (non-null) Paint parameter. In all other cases it176* is safe to pass a null SunGraphics2D and it will be ignored.177*178* Note: must be called while the RenderQueue lock is held.179*180* It's assumed that the type of surfaces has been checked by the Renderer181*182* @throws InvalidPipeException if either src or dest surface is not valid183* or lost184*/185private void validate(AccelSurface srcData, AccelSurface dstData,186Region clip, Composite comp,187AffineTransform xform,188Paint paint, SunGraphics2D sg2d, int flags)189{190// assert rq.lock.isHeldByCurrentThread();191192boolean updateClip = false;193boolean updatePaint = false;194195if (!dstData.isValid() ||196dstData.isSurfaceLost() || srcData.isSurfaceLost())197{198invalidateContext();199throw new InvalidPipeException("bounds changed or surface lost");200}201202if (paint instanceof Color) {203// REMIND: not 30-bit friendly204int newRGB = ((Color)paint).getRGB();205if (isValidatedPaintJustAColor) {206if (newRGB != validatedRGB) {207validatedRGB = newRGB;208updatePaint = true;209}210} else {211validatedRGB = newRGB;212updatePaint = true;213isValidatedPaintJustAColor = true;214}215} else if (validPaintRef.get() != paint) {216updatePaint = true;217// this should be set when we are switching from paint to color218// in which case this condition will be true219isValidatedPaintJustAColor = false;220}221222final AccelSurface validatedSrcData = validSrcDataRef.get();223final AccelSurface validatedDstData = validDstDataRef.get();224if ((currentContext != this) ||225(srcData != validatedSrcData) ||226(dstData != validatedDstData))227{228if (dstData != validatedDstData) {229// the clip is dependent on the destination surface, so we230// need to update it if we have a new destination surface231updateClip = true;232}233234if (paint == null) {235// make sure we update the color state (otherwise, it might236// not be updated if this is the first time the context237// is being validated)238updatePaint = true;239}240241// update the current source and destination surfaces242setSurfaces(srcData, dstData);243244currentContext = this;245validSrcDataRef = new WeakReference<>(srcData);246validDstDataRef = new WeakReference<>(dstData);247}248249// validate clip250final Region validatedClip = validClipRef.get();251if ((clip != validatedClip) || updateClip) {252if (clip != null) {253if (updateClip ||254validatedClip == null ||255!(validatedClip.isRectangular() && clip.isRectangular()) ||256((clip.getLoX() != validatedClip.getLoX() ||257clip.getLoY() != validatedClip.getLoY() ||258clip.getHiX() != validatedClip.getHiX() ||259clip.getHiY() != validatedClip.getHiY())))260{261setClip(clip);262}263} else {264resetClip();265}266validClipRef = new WeakReference<>(clip);267}268269// validate composite (note that a change in the context flags270// may require us to update the composite state, even if the271// composite has not changed)272if ((comp != validCompRef.get()) || (flags != validatedFlags)) {273if (comp != null) {274setComposite(comp, flags);275} else {276resetComposite();277}278// the paint state is dependent on the composite state, so make279// sure we update the color below280updatePaint = true;281validCompRef = new WeakReference<>(comp);282validatedFlags = flags;283}284285// validate transform286boolean txChanged = false;287if (xform == null) {288if (xformInUse) {289resetTransform();290xformInUse = false;291txChanged = true;292} else if (sg2d != null && !sg2d.transform.equals(transform)) {293txChanged = true;294}295if (sg2d != null && txChanged) {296transform = new AffineTransform(sg2d.transform);297}298} else {299setTransform(xform);300xformInUse = true;301txChanged = true;302}303// non-Color paints may require paint revalidation304if (!isValidatedPaintJustAColor && txChanged) {305updatePaint = true;306}307308// validate paint309if (updatePaint) {310if (paint != null) {311BufferedPaints.setPaint(rq, sg2d, paint, flags);312} else {313BufferedPaints.resetPaint(rq);314}315validPaintRef = new WeakReference<>(paint);316}317318// mark dstData dirty319// REMIND: is this really needed now? we do it in SunGraphics2D..320dstData.markDirty();321}322323private void setSurfaces(AccelSurface srcData,324AccelSurface dstData)325{326// assert rq.lock.isHeldByCurrentThread();327rq.ensureCapacityAndAlignment(20, 4);328buf.putInt(SET_SURFACES);329buf.putLong(srcData.getNativeOps());330buf.putLong(dstData.getNativeOps());331}332333private void resetClip() {334// assert rq.lock.isHeldByCurrentThread();335rq.ensureCapacity(4);336buf.putInt(RESET_CLIP);337}338339private void setClip(Region clip) {340// assert rq.lock.isHeldByCurrentThread();341if (clip.isRectangular()) {342rq.ensureCapacity(20);343buf.putInt(SET_RECT_CLIP);344buf.putInt(clip.getLoX()).putInt(clip.getLoY());345buf.putInt(clip.getHiX()).putInt(clip.getHiY());346} else {347rq.ensureCapacity(28); // so that we have room for at least a span348buf.putInt(BEGIN_SHAPE_CLIP);349buf.putInt(SET_SHAPE_CLIP_SPANS);350// include a placeholder for the span count351int countIndex = buf.position();352buf.putInt(0);353int spanCount = 0;354int remainingSpans = buf.remaining() / BYTES_PER_SPAN;355int[] span = new int[4];356SpanIterator si = clip.getSpanIterator();357while (si.nextSpan(span)) {358if (remainingSpans == 0) {359buf.putInt(countIndex, spanCount);360rq.flushNow();361buf.putInt(SET_SHAPE_CLIP_SPANS);362countIndex = buf.position();363buf.putInt(0);364spanCount = 0;365remainingSpans = buf.remaining() / BYTES_PER_SPAN;366}367buf.putInt(span[0]); // x1368buf.putInt(span[1]); // y1369buf.putInt(span[2]); // x2370buf.putInt(span[3]); // y2371spanCount++;372remainingSpans--;373}374buf.putInt(countIndex, spanCount);375rq.ensureCapacity(4);376buf.putInt(END_SHAPE_CLIP);377}378}379380private void resetComposite() {381// assert rq.lock.isHeldByCurrentThread();382rq.ensureCapacity(4);383buf.putInt(RESET_COMPOSITE);384}385386private void setComposite(Composite comp, int flags) {387// assert rq.lock.isHeldByCurrentThread();388if (comp instanceof AlphaComposite) {389AlphaComposite ac = (AlphaComposite)comp;390rq.ensureCapacity(16);391buf.putInt(SET_ALPHA_COMPOSITE);392buf.putInt(ac.getRule());393buf.putFloat(ac.getAlpha());394buf.putInt(flags);395} else if (comp instanceof XORComposite) {396int xorPixel = ((XORComposite)comp).getXorPixel();397rq.ensureCapacity(8);398buf.putInt(SET_XOR_COMPOSITE);399buf.putInt(xorPixel);400} else {401throw new InternalError("not yet implemented");402}403}404405private void resetTransform() {406// assert rq.lock.isHeldByCurrentThread();407rq.ensureCapacity(4);408buf.putInt(RESET_TRANSFORM);409}410411private void setTransform(AffineTransform xform) {412// assert rq.lock.isHeldByCurrentThread();413rq.ensureCapacityAndAlignment(52, 4);414buf.putInt(SET_TRANSFORM);415buf.putDouble(xform.getScaleX());416buf.putDouble(xform.getShearY());417buf.putDouble(xform.getShearX());418buf.putDouble(xform.getScaleY());419buf.putDouble(xform.getTranslateX());420buf.putDouble(xform.getTranslateY());421}422423/**424* Resets this context's surfaces and all attributes.425*426* Note: must be called while the RenderQueue lock is held.427*428* @see RenderQueue#lock429* @see RenderQueue#unlock430*/431public final void invalidateContext() {432resetTransform();433resetComposite();434resetClip();435BufferedPaints.resetPaint(rq);436validSrcDataRef.clear();437validDstDataRef.clear();438validCompRef.clear();439validClipRef.clear();440validPaintRef.clear();441isValidatedPaintJustAColor = false;442xformInUse = false;443}444445/**446* Returns a singleton {@code RenderQueue} object used by the rendering447* pipeline.448*449* @return a render queue450* @see RenderQueue451*/452public final RenderQueue getRenderQueue() {453return rq;454}455}456457458