1 /*
   2  * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.java2d.pipe;
  27 
  28 import java.awt.AlphaComposite;
  29 import java.awt.Color;
  30 import java.awt.Composite;
  31 import java.awt.Paint;
  32 import java.awt.geom.AffineTransform;
  33 import sun.java2d.pipe.hw.AccelSurface;
  34 import sun.java2d.InvalidPipeException;
  35 import sun.java2d.SunGraphics2D;
  36 import sun.java2d.loops.XORComposite;
  37 import static sun.java2d.pipe.BufferedOpCodes.*;
  38 import static sun.java2d.pipe.BufferedRenderPipe.BYTES_PER_SPAN;
  39 
  40 import java.lang.annotation.Native;
  41 
  42 /**
  43  * Base context class for managing state in a single-threaded rendering
  44  * environment.  Each state-setting operation (e.g. SET_COLOR) is added to
  45  * the provided RenderQueue, which will be processed at a later time by a
  46  * single thread.  Note that the RenderQueue lock must be acquired before
  47  * calling the validate() method (or any other method in this class).  See
  48  * the RenderQueue class comments for a sample usage scenario.
  49  *
  50  * @see RenderQueue
  51  */
  52 public abstract class BufferedContext {
  53 
  54     /*
  55      * The following flags help the internals of validate() determine
  56      * the appropriate (meaning correct, or optimal) code path when
  57      * setting up the current context.  The flags can be bitwise OR'd
  58      * together as needed.
  59      */
  60 
  61     /**
  62      * Indicates that no flags are needed; take all default code paths.
  63      */
  64     @Native public static final int NO_CONTEXT_FLAGS = (0 << 0);
  65     /**
  66      * Indicates that the source surface (or color value, if it is a simple
  67      * rendering operation) is opaque (has an alpha value of 1.0).  If this
  68      * flag is present, it allows us to disable blending in certain
  69      * situations in order to improve performance.
  70      */
  71     @Native public static final int SRC_IS_OPAQUE    = (1 << 0);
  72     /**
  73      * Indicates that the operation uses an alpha mask, which may determine
  74      * the code path that is used when setting up the current paint state.
  75      */
  76     @Native public static final int USE_MASK         = (1 << 1);
  77 
  78     protected RenderQueue rq;
  79     protected RenderBuffer buf;
  80 
  81     /**
  82      * This is a reference to the most recently validated BufferedContext.  If
  83      * this value is null, it means that there is no current context.  It is
  84      * provided here so that validate() only needs to do a quick reference
  85      * check to see if the BufferedContext passed to that method is the same
  86      * as the one we've cached here.
  87      */
  88     protected static BufferedContext currentContext;
  89 
  90     private AccelSurface    validatedSrcData;
  91     private AccelSurface    validatedDstData;
  92     private Region          validatedClip;
  93     private Composite       validatedComp;
  94     private Paint           validatedPaint;
  95     // renamed from isValidatedPaintAColor as part of a work around for 6764257
  96     private boolean         isValidatedPaintJustAColor;
  97     private int             validatedRGB;
  98     private int             validatedFlags;
  99     private boolean         xformInUse;
 100     private AffineTransform transform;
 101 
 102     protected BufferedContext(RenderQueue rq) {
 103         this.rq = rq;
 104         this.buf = rq.getBuffer();
 105     }
 106 
 107     /**
 108      * Fetches the BufferedContextContext associated with the dst. surface
 109      * and validates the context using the given parameters.  Most rendering
 110      * operations will call this method first in order to set the necessary
 111      * state before issuing rendering commands.
 112      *
 113      * Note: must be called while the RenderQueue lock is held.
 114      *
 115      * It's assumed that the type of surfaces has been checked by the Renderer
 116      *
 117      * @throws InvalidPipeException if either src or dest surface is not valid
 118      * or lost
 119      * @see RenderQueue#lock
 120      * @see RenderQueue#unlock
 121      */
 122     public static void validateContext(AccelSurface srcData,
 123                                        AccelSurface dstData,
 124                                        Region clip, Composite comp,
 125                                        AffineTransform xform,
 126                                        Paint paint, SunGraphics2D sg2d,
 127                                        int flags)
 128     {
 129         // assert rq.lock.isHeldByCurrentThread();
 130         BufferedContext d3dc = dstData.getContext();
 131         d3dc.validate(srcData, dstData,
 132                       clip, comp, xform, paint, sg2d, flags);
 133     }
 134 
 135     /**
 136      * Fetches the BufferedContextassociated with the surface
 137      * and disables all context state settings.
 138      *
 139      * Note: must be called while the RenderQueue lock is held.
 140      *
 141      * It's assumed that the type of surfaces has been checked by the Renderer
 142      *
 143      * @throws InvalidPipeException if the surface is not valid
 144      * or lost
 145      * @see RenderQueue#lock
 146      * @see RenderQueue#unlock
 147      */
 148     public static void validateContext(AccelSurface surface) {
 149         // assert rt.lock.isHeldByCurrentThread();
 150         validateContext(surface, surface,
 151                         null, null, null, null, null, NO_CONTEXT_FLAGS);
 152     }
 153 
 154     /**
 155      * Validates the given parameters against the current state for this
 156      * context.  If this context is not current, it will be made current
 157      * for the given source and destination surfaces, and the viewport will
 158      * be updated.  Then each part of the context state (clip, composite,
 159      * etc.) is checked against the previous value.  If the value has changed
 160      * since the last call to validate(), it will be updated accordingly.
 161      *
 162      * Note that the SunGraphics2D parameter is only used for the purposes
 163      * of validating a (non-null) Paint parameter.  In all other cases it
 164      * is safe to pass a null SunGraphics2D and it will be ignored.
 165      *
 166      * Note: must be called while the RenderQueue lock is held.
 167      *
 168      * It's assumed that the type of surfaces has been checked by the Renderer
 169      *
 170      * @throws InvalidPipeException if either src or dest surface is not valid
 171      * or lost
 172      */
 173     public void validate(AccelSurface srcData, AccelSurface dstData,
 174                          Region clip, Composite comp,
 175                          AffineTransform xform,
 176                          Paint paint, SunGraphics2D sg2d, int flags)
 177     {
 178         // assert rq.lock.isHeldByCurrentThread();
 179 
 180         boolean updateClip = false;
 181         boolean updatePaint = false;
 182 
 183         if (!dstData.isValid() ||
 184             dstData.isSurfaceLost() || srcData.isSurfaceLost())
 185         {
 186             invalidateContext();
 187             throw new InvalidPipeException("bounds changed or surface lost");
 188         }
 189 
 190         if (paint instanceof Color) {
 191             // REMIND: not 30-bit friendly
 192             int newRGB = ((Color)paint).getRGB();
 193             if (isValidatedPaintJustAColor) {
 194                 if (newRGB != validatedRGB) {
 195                     validatedRGB = newRGB;
 196                     updatePaint = true;
 197                 }
 198             } else {
 199                 validatedRGB = newRGB;
 200                 updatePaint = true;
 201                 isValidatedPaintJustAColor = true;
 202             }
 203         } else if (validatedPaint != paint) {
 204             updatePaint = true;
 205             // this should be set when we are switching from paint to color
 206             // in which case this condition will be true
 207             isValidatedPaintJustAColor = false;
 208         }
 209 
 210         if ((currentContext != this) ||
 211             (srcData != validatedSrcData) ||
 212             (dstData != validatedDstData))
 213         {
 214             if (dstData != validatedDstData) {
 215                 // the clip is dependent on the destination surface, so we
 216                 // need to update it if we have a new destination surface
 217                 updateClip = true;
 218             }
 219 
 220             if (paint == null) {
 221                 // make sure we update the color state (otherwise, it might
 222                 // not be updated if this is the first time the context
 223                 // is being validated)
 224                 updatePaint = true;
 225             }
 226 
 227             // update the current source and destination surfaces
 228             setSurfaces(srcData, dstData);
 229 
 230             currentContext = this;
 231             validatedSrcData = srcData;
 232             validatedDstData = dstData;
 233         }
 234 
 235         // validate clip
 236         if ((clip != validatedClip) || updateClip) {
 237             if (clip != null) {
 238                 if (updateClip ||
 239                     validatedClip == null ||
 240                     !(validatedClip.isRectangular() && clip.isRectangular()) ||
 241                     ((clip.getLoX() != validatedClip.getLoX() ||
 242                       clip.getLoY() != validatedClip.getLoY() ||
 243                       clip.getHiX() != validatedClip.getHiX() ||
 244                       clip.getHiY() != validatedClip.getHiY())))
 245                 {
 246                     setClip(clip);
 247                 }
 248             } else {
 249                 resetClip();
 250             }
 251             validatedClip = clip;
 252         }
 253 
 254         // validate composite (note that a change in the context flags
 255         // may require us to update the composite state, even if the
 256         // composite has not changed)
 257         if ((comp != validatedComp) || (flags != validatedFlags)) {
 258             if (comp != null) {
 259                 setComposite(comp, flags);
 260             } else {
 261                 resetComposite();
 262             }
 263             // the paint state is dependent on the composite state, so make
 264             // sure we update the color below
 265             updatePaint = true;
 266             validatedComp = comp;
 267             validatedFlags = flags;
 268         }
 269 
 270         // validate transform
 271         boolean txChanged = false;
 272         if (xform == null) {
 273             if (xformInUse) {
 274                 resetTransform();
 275                 xformInUse = false;
 276                 txChanged = true;
 277             } else if (sg2d != null && !sg2d.transform.equals(transform)) {
 278                 txChanged = true;
 279             }
 280             if (sg2d != null && txChanged) {
 281                 transform = new AffineTransform(sg2d.transform);
 282             }
 283         } else {
 284             setTransform(xform);
 285             xformInUse = true;
 286             txChanged = true;
 287         }
 288         // non-Color paints may require paint revalidation
 289         if (!isValidatedPaintJustAColor && txChanged) {
 290             updatePaint = true;
 291         }
 292 
 293         // validate paint
 294         if (updatePaint) {
 295             if (paint != null) {
 296                 BufferedPaints.setPaint(rq, sg2d, paint, flags);
 297             } else {
 298                 BufferedPaints.resetPaint(rq);
 299             }
 300             validatedPaint = paint;
 301         }
 302 
 303         // mark dstData dirty
 304         // REMIND: is this really needed now? we do it in SunGraphics2D..
 305         dstData.markDirty();
 306     }
 307 
 308     /**
 309      * Invalidates the surfaces associated with this context.  This is
 310      * useful when the context is no longer needed, and we want to break
 311      * the chain caused by these surface references.
 312      *
 313      * Note: must be called while the RenderQueue lock is held.
 314      *
 315      * @see RenderQueue#lock
 316      * @see RenderQueue#unlock
 317      */
 318     public void invalidateSurfaces() {
 319         validatedSrcData = null;
 320         validatedDstData = null;
 321     }
 322 
 323     private void setSurfaces(AccelSurface srcData,
 324                              AccelSurface dstData)
 325     {
 326         // assert rq.lock.isHeldByCurrentThread();
 327         rq.ensureCapacityAndAlignment(20, 4);
 328         buf.putInt(SET_SURFACES);
 329         buf.putLong(srcData.getNativeOps());
 330         buf.putLong(dstData.getNativeOps());
 331     }
 332 
 333     private void resetClip() {
 334         // assert rq.lock.isHeldByCurrentThread();
 335         rq.ensureCapacity(4);
 336         buf.putInt(RESET_CLIP);
 337     }
 338 
 339     private void setClip(Region clip) {
 340         // assert rq.lock.isHeldByCurrentThread();
 341         if (clip.isRectangular()) {
 342             rq.ensureCapacity(20);
 343             buf.putInt(SET_RECT_CLIP);
 344             buf.putInt(clip.getLoX()).putInt(clip.getLoY());
 345             buf.putInt(clip.getHiX()).putInt(clip.getHiY());
 346         } else {
 347             rq.ensureCapacity(28); // so that we have room for at least a span
 348             buf.putInt(BEGIN_SHAPE_CLIP);
 349             buf.putInt(SET_SHAPE_CLIP_SPANS);
 350             // include a placeholder for the span count
 351             int countIndex = buf.position();
 352             buf.putInt(0);
 353             int spanCount = 0;
 354             int remainingSpans = buf.remaining() / BYTES_PER_SPAN;
 355             int span[] = new int[4];
 356             SpanIterator si = clip.getSpanIterator();
 357             while (si.nextSpan(span)) {
 358                 if (remainingSpans == 0) {
 359                     buf.putInt(countIndex, spanCount);
 360                     rq.flushNow();
 361                     buf.putInt(SET_SHAPE_CLIP_SPANS);
 362                     countIndex = buf.position();
 363                     buf.putInt(0);
 364                     spanCount = 0;
 365                     remainingSpans = buf.remaining() / BYTES_PER_SPAN;
 366                 }
 367                 buf.putInt(span[0]); // x1
 368                 buf.putInt(span[1]); // y1
 369                 buf.putInt(span[2]); // x2
 370                 buf.putInt(span[3]); // y2
 371                 spanCount++;
 372                 remainingSpans--;
 373             }
 374             buf.putInt(countIndex, spanCount);
 375             rq.ensureCapacity(4);
 376             buf.putInt(END_SHAPE_CLIP);
 377         }
 378     }
 379 
 380     private void resetComposite() {
 381         // assert rq.lock.isHeldByCurrentThread();
 382         rq.ensureCapacity(4);
 383         buf.putInt(RESET_COMPOSITE);
 384     }
 385 
 386     private void setComposite(Composite comp, int flags) {
 387         // assert rq.lock.isHeldByCurrentThread();
 388         if (comp instanceof AlphaComposite) {
 389             AlphaComposite ac = (AlphaComposite)comp;
 390             rq.ensureCapacity(16);
 391             buf.putInt(SET_ALPHA_COMPOSITE);
 392             buf.putInt(ac.getRule());
 393             buf.putFloat(ac.getAlpha());
 394             buf.putInt(flags);
 395         } else if (comp instanceof XORComposite) {
 396             int xorPixel = ((XORComposite)comp).getXorPixel();
 397             rq.ensureCapacity(8);
 398             buf.putInt(SET_XOR_COMPOSITE);
 399             buf.putInt(xorPixel);
 400         } else {
 401             throw new InternalError("not yet implemented");
 402         }
 403     }
 404 
 405     private void resetTransform() {
 406         // assert rq.lock.isHeldByCurrentThread();
 407         rq.ensureCapacity(4);
 408         buf.putInt(RESET_TRANSFORM);
 409     }
 410 
 411     private void setTransform(AffineTransform xform) {
 412         // assert rq.lock.isHeldByCurrentThread();
 413         rq.ensureCapacityAndAlignment(52, 4);
 414         buf.putInt(SET_TRANSFORM);
 415         buf.putDouble(xform.getScaleX());
 416         buf.putDouble(xform.getShearY());
 417         buf.putDouble(xform.getShearX());
 418         buf.putDouble(xform.getScaleY());
 419         buf.putDouble(xform.getTranslateX());
 420         buf.putDouble(xform.getTranslateY());
 421     }
 422 
 423     /**
 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#lock
 429      * @see RenderQueue#unlock
 430      */
 431     public void invalidateContext() {
 432         resetTransform();
 433         resetComposite();
 434         resetClip();
 435         BufferedPaints.resetPaint(rq);
 436         invalidateSurfaces();
 437         validatedComp = null;
 438         validatedClip = null;
 439         validatedPaint = null;
 440         isValidatedPaintJustAColor = false;
 441         xformInUse = false;
 442     }
 443 
 444     /**
 445      * Returns a singleton {@code RenderQueue} object used by the rendering
 446      * pipeline.
 447      *
 448      * @return a render queue
 449      * @see RenderQueue
 450      */
 451     public abstract RenderQueue getRenderQueue();
 452 
 453     /**
 454      * Saves the state of this context.
 455      * It may reset the current context.
 456      *
 457      * Note: must be called while the RenderQueue lock is held.
 458      *
 459      * @see RenderQueue#lock
 460      * @see RenderQueue#unlock
 461      */
 462     public abstract void saveState();
 463 
 464     /**
 465      * Restores the native state of this context.
 466      * It may reset the current context.
 467      *
 468      * Note: must be called while the RenderQueue lock is held.
 469      *
 470      * @see RenderQueue#lock
 471      * @see RenderQueue#unlock
 472      */
 473     public abstract void restoreState();
 474 }