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