Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/pipe/BufferedContext.java
38918 views
/*1* Copyright (c) 2005, 2016, 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 sun.java2d.pipe.hw.AccelSurface;33import sun.java2d.InvalidPipeException;34import sun.java2d.SunGraphics2D;35import sun.java2d.loops.XORComposite;36import static sun.java2d.pipe.BufferedOpCodes.*;37import static sun.java2d.pipe.BufferedRenderPipe.BYTES_PER_SPAN;3839import java.lang.annotation.Native;40import java.lang.ref.Reference;41import java.lang.ref.WeakReference;4243/**44* Base context class for managing state in a single-threaded rendering45* environment. Each state-setting operation (e.g. SET_COLOR) is added to46* the provided RenderQueue, which will be processed at a later time by a47* single thread. Note that the RenderQueue lock must be acquired before48* calling the validate() method (or any other method in this class). See49* the RenderQueue class comments for a sample usage scenario.50*51* @see RenderQueue52*/53public abstract class BufferedContext {5455/*56* The following flags help the internals of validate() determine57* the appropriate (meaning correct, or optimal) code path when58* setting up the current context. The flags can be bitwise OR'd59* together as needed.60*/6162/**63* Indicates that no flags are needed; take all default code paths.64*/65@Native public static final int NO_CONTEXT_FLAGS = (0 << 0);66/**67* Indicates that the source surface (or color value, if it is a simple68* rendering operation) is opaque (has an alpha value of 1.0). If this69* flag is present, it allows us to disable blending in certain70* situations in order to improve performance.71*/72@Native public static final int SRC_IS_OPAQUE = (1 << 0);73/**74* Indicates that the operation uses an alpha mask, which may determine75* the code path that is used when setting up the current paint state.76*/77@Native public static final int USE_MASK = (1 << 1);7879protected RenderQueue rq;80protected RenderBuffer buf;8182/**83* This is a reference to the most recently validated BufferedContext. If84* this value is null, it means that there is no current context. It is85* provided here so that validate() only needs to do a quick reference86* check to see if the BufferedContext passed to that method is the same87* as the one we've cached here.88*/89protected static BufferedContext currentContext;9091private Reference<AccelSurface> validSrcDataRef = new WeakReference<>(null);92private Reference<AccelSurface> validDstDataRef = new WeakReference<>(null);93private Reference<Region> validClipRef = new WeakReference<>(null);94private Reference<Composite> validCompRef = new WeakReference<>(null);95private Reference<Paint> validPaintRef = new WeakReference<>(null);96// renamed from isValidatedPaintAColor as part of a work around for 676425797private boolean isValidatedPaintJustAColor;98private int validatedRGB;99private int validatedFlags;100private boolean xformInUse;101private AffineTransform transform;102103protected BufferedContext(RenderQueue rq) {104this.rq = rq;105this.buf = rq.getBuffer();106}107108/**109* Fetches the BufferedContextContext associated with the dst. surface110* and validates the context using the given parameters. Most rendering111* operations will call this method first in order to set the necessary112* state before issuing rendering commands.113*114* Note: must be called while the RenderQueue lock is held.115*116* It's assumed that the type of surfaces has been checked by the Renderer117*118* @throws InvalidPipeException if either src or dest surface is not valid119* or lost120* @see RenderQueue#lock121* @see RenderQueue#unlock122*/123public static void validateContext(AccelSurface srcData,124AccelSurface dstData,125Region clip, Composite comp,126AffineTransform xform,127Paint paint, SunGraphics2D sg2d,128int flags)129{130// assert rq.lock.isHeldByCurrentThread();131BufferedContext context = dstData.getContext();132context.validate(srcData, dstData,133clip, comp, xform, paint, sg2d, flags);134}135136/**137* Fetches the BufferedContextassociated with the surface138* and disables all context state settings.139*140* Note: must be called while the RenderQueue lock is held.141*142* It's assumed that the type of surfaces has been checked by the Renderer143*144* @throws InvalidPipeException if the surface is not valid145* or lost146* @see RenderQueue#lock147* @see RenderQueue#unlock148*/149public static void validateContext(AccelSurface surface) {150// assert rt.lock.isHeldByCurrentThread();151validateContext(surface, surface,152null, null, null, null, null, NO_CONTEXT_FLAGS);153}154155/**156* Validates the given parameters against the current state for this157* context. If this context is not current, it will be made current158* for the given source and destination surfaces, and the viewport will159* be updated. Then each part of the context state (clip, composite,160* etc.) is checked against the previous value. If the value has changed161* since the last call to validate(), it will be updated accordingly.162*163* Note that the SunGraphics2D parameter is only used for the purposes164* of validating a (non-null) Paint parameter. In all other cases it165* is safe to pass a null SunGraphics2D and it will be ignored.166*167* Note: must be called while the RenderQueue lock is held.168*169* It's assumed that the type of surfaces has been checked by the Renderer170*171* @throws InvalidPipeException if either src or dest surface is not valid172* or lost173*/174public void validate(AccelSurface srcData, AccelSurface dstData,175Region clip, Composite comp,176AffineTransform xform,177Paint paint, SunGraphics2D sg2d, int flags)178{179// assert rq.lock.isHeldByCurrentThread();180181boolean updateClip = false;182boolean updatePaint = false;183184if (!dstData.isValid() ||185dstData.isSurfaceLost() || srcData.isSurfaceLost())186{187invalidateContext();188throw new InvalidPipeException("bounds changed or surface lost");189}190191if (paint instanceof Color) {192// REMIND: not 30-bit friendly193int newRGB = ((Color)paint).getRGB();194if (isValidatedPaintJustAColor) {195if (newRGB != validatedRGB) {196validatedRGB = newRGB;197updatePaint = true;198}199} else {200validatedRGB = newRGB;201updatePaint = true;202isValidatedPaintJustAColor = true;203}204} else if (validPaintRef.get() != paint) {205updatePaint = true;206// this should be set when we are switching from paint to color207// in which case this condition will be true208isValidatedPaintJustAColor = false;209}210211final AccelSurface validatedSrcData = validSrcDataRef.get();212final AccelSurface validatedDstData = validDstDataRef.get();213if ((currentContext != this) ||214(srcData != validatedSrcData) ||215(dstData != validatedDstData))216{217if (dstData != validatedDstData) {218// the clip is dependent on the destination surface, so we219// need to update it if we have a new destination surface220updateClip = true;221}222223if (paint == null) {224// make sure we update the color state (otherwise, it might225// not be updated if this is the first time the context226// is being validated)227updatePaint = true;228}229230// update the current source and destination surfaces231setSurfaces(srcData, dstData);232233currentContext = this;234validSrcDataRef = new WeakReference<>(srcData);235validDstDataRef = new WeakReference<>(dstData);236}237238// validate clip239final Region validatedClip = validClipRef.get();240if ((clip != validatedClip) || updateClip) {241if (clip != null) {242if (updateClip ||243validatedClip == null ||244!(validatedClip.isRectangular() && clip.isRectangular()) ||245((clip.getLoX() != validatedClip.getLoX() ||246clip.getLoY() != validatedClip.getLoY() ||247clip.getHiX() != validatedClip.getHiX() ||248clip.getHiY() != validatedClip.getHiY())))249{250setClip(clip);251}252} else {253resetClip();254}255validClipRef = new WeakReference<>(clip);256}257258// validate composite (note that a change in the context flags259// may require us to update the composite state, even if the260// composite has not changed)261if ((comp != validCompRef.get()) || (flags != validatedFlags)) {262if (comp != null) {263setComposite(comp, flags);264} else {265resetComposite();266}267// the paint state is dependent on the composite state, so make268// sure we update the color below269updatePaint = true;270validCompRef = new WeakReference<>(comp);271validatedFlags = flags;272}273274// validate transform275boolean txChanged = false;276if (xform == null) {277if (xformInUse) {278resetTransform();279xformInUse = false;280txChanged = true;281} else if (sg2d != null && !sg2d.transform.equals(transform)) {282txChanged = true;283}284if (sg2d != null && txChanged) {285transform = new AffineTransform(sg2d.transform);286}287} else {288setTransform(xform);289xformInUse = true;290txChanged = true;291}292// non-Color paints may require paint revalidation293if (!isValidatedPaintJustAColor && txChanged) {294updatePaint = true;295}296297// validate paint298if (updatePaint) {299if (paint != null) {300BufferedPaints.setPaint(rq, sg2d, paint, flags);301} else {302BufferedPaints.resetPaint(rq);303}304validPaintRef = new WeakReference<>(paint);305}306307// mark dstData dirty308// REMIND: is this really needed now? we do it in SunGraphics2D..309dstData.markDirty();310}311312/**313* Invalidates the surfaces associated with this context. This is314* useful when the context is no longer needed, and we want to break315* the chain caused by these surface references.316*317* Note: must be called while the RenderQueue lock is held.318*319* @see RenderQueue#lock320* @see RenderQueue#unlock321*/322private void invalidateSurfaces() {323validSrcDataRef.clear();324validDstDataRef.clear();325}326327private void setSurfaces(AccelSurface srcData,328AccelSurface dstData)329{330// assert rq.lock.isHeldByCurrentThread();331rq.ensureCapacityAndAlignment(20, 4);332buf.putInt(SET_SURFACES);333buf.putLong(srcData.getNativeOps());334buf.putLong(dstData.getNativeOps());335}336337private void resetClip() {338// assert rq.lock.isHeldByCurrentThread();339rq.ensureCapacity(4);340buf.putInt(RESET_CLIP);341}342343private void setClip(Region clip) {344// assert rq.lock.isHeldByCurrentThread();345if (clip.isRectangular()) {346rq.ensureCapacity(20);347buf.putInt(SET_RECT_CLIP);348buf.putInt(clip.getLoX()).putInt(clip.getLoY());349buf.putInt(clip.getHiX()).putInt(clip.getHiY());350} else {351rq.ensureCapacity(28); // so that we have room for at least a span352buf.putInt(BEGIN_SHAPE_CLIP);353buf.putInt(SET_SHAPE_CLIP_SPANS);354// include a placeholder for the span count355int countIndex = buf.position();356buf.putInt(0);357int spanCount = 0;358int remainingSpans = buf.remaining() / BYTES_PER_SPAN;359int span[] = new int[4];360SpanIterator si = clip.getSpanIterator();361while (si.nextSpan(span)) {362if (remainingSpans == 0) {363buf.putInt(countIndex, spanCount);364rq.flushNow();365buf.putInt(SET_SHAPE_CLIP_SPANS);366countIndex = buf.position();367buf.putInt(0);368spanCount = 0;369remainingSpans = buf.remaining() / BYTES_PER_SPAN;370}371buf.putInt(span[0]); // x1372buf.putInt(span[1]); // y1373buf.putInt(span[2]); // x2374buf.putInt(span[3]); // y2375spanCount++;376remainingSpans--;377}378buf.putInt(countIndex, spanCount);379rq.ensureCapacity(4);380buf.putInt(END_SHAPE_CLIP);381}382}383384private void resetComposite() {385// assert rq.lock.isHeldByCurrentThread();386rq.ensureCapacity(4);387buf.putInt(RESET_COMPOSITE);388}389390private void setComposite(Composite comp, int flags) {391// assert rq.lock.isHeldByCurrentThread();392if (comp instanceof AlphaComposite) {393AlphaComposite ac = (AlphaComposite)comp;394rq.ensureCapacity(16);395buf.putInt(SET_ALPHA_COMPOSITE);396buf.putInt(ac.getRule());397buf.putFloat(ac.getAlpha());398buf.putInt(flags);399} else if (comp instanceof XORComposite) {400int xorPixel = ((XORComposite)comp).getXorPixel();401rq.ensureCapacity(8);402buf.putInt(SET_XOR_COMPOSITE);403buf.putInt(xorPixel);404} else {405throw new InternalError("not yet implemented");406}407}408409private void resetTransform() {410// assert rq.lock.isHeldByCurrentThread();411rq.ensureCapacity(4);412buf.putInt(RESET_TRANSFORM);413}414415private void setTransform(AffineTransform xform) {416// assert rq.lock.isHeldByCurrentThread();417rq.ensureCapacityAndAlignment(52, 4);418buf.putInt(SET_TRANSFORM);419buf.putDouble(xform.getScaleX());420buf.putDouble(xform.getShearY());421buf.putDouble(xform.getShearX());422buf.putDouble(xform.getScaleY());423buf.putDouble(xform.getTranslateX());424buf.putDouble(xform.getTranslateY());425}426427/**428* Resets this context's surfaces and all attributes.429*430* Note: must be called while the RenderQueue lock is held.431*432* @see RenderQueue#lock433* @see RenderQueue#unlock434*/435public void invalidateContext() {436resetTransform();437resetComposite();438resetClip();439BufferedPaints.resetPaint(rq);440invalidateSurfaces();441validCompRef.clear();442validClipRef.clear();443validPaintRef.clear();444isValidatedPaintJustAColor = false;445xformInUse = false;446}447448/**449* Returns a singleton {@code RenderQueue} object used by the rendering450* pipeline.451*452* @return a render queue453* @see RenderQueue454*/455public abstract RenderQueue getRenderQueue();456457/**458* Saves the the state of this context.459* It may reset the current context.460*461* Note: must be called while the RenderQueue lock is held.462*463* @see RenderQueue#lock464* @see RenderQueue#unlock465*/466public abstract void saveState();467468/**469* Restores the native state of this context.470* It may reset the current context.471*472* Note: must be called while the RenderQueue lock is held.473*474* @see RenderQueue#lock475* @see RenderQueue#unlock476*/477public abstract void restoreState();478}479480481