BufferedContext.java 17.1 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
24 25 26 27 28
 */

package sun.java2d.pipe;

import java.awt.AlphaComposite;
29
import java.awt.Color;
D
duke 已提交
30 31 32
import java.awt.Composite;
import java.awt.Paint;
import java.awt.geom.AffineTransform;
33
import sun.java2d.pipe.hw.AccelSurface;
D
duke 已提交
34 35 36 37 38 39
import sun.java2d.InvalidPipeException;
import sun.java2d.SunGraphics2D;
import sun.java2d.loops.XORComposite;
import static sun.java2d.pipe.BufferedOpCodes.*;
import static sun.java2d.pipe.BufferedRenderPipe.BYTES_PER_SPAN;

40 41
import javax.tools.annotation.GenerateNativeHeader;

D
duke 已提交
42 43 44 45 46 47 48
/**
 * Base context class for managing state in a single-threaded rendering
 * environment.  Each state-setting operation (e.g. SET_COLOR) is added to
 * the provided RenderQueue, which will be processed at a later time by a
 * single thread.  Note that the RenderQueue lock must be acquired before
 * calling the validate() method (or any other method in this class).  See
 * the RenderQueue class comments for a sample usage scenario.
49 50
 *
 * @see RenderQueue
D
duke 已提交
51
 */
52 53
/* No native methods here, but the constants are needed in the supporting JNI code */
@GenerateNativeHeader
54
public abstract class BufferedContext {
D
duke 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

    /*
     * The following flags help the internals of validate() determine
     * the appropriate (meaning correct, or optimal) code path when
     * setting up the current context.  The flags can be bitwise OR'd
     * together as needed.
     */

    /**
     * Indicates that no flags are needed; take all default code paths.
     */
    public static final int NO_CONTEXT_FLAGS = (0 << 0);
    /**
     * Indicates that the source surface (or color value, if it is a simple
     * rendering operation) is opaque (has an alpha value of 1.0).  If this
     * flag is present, it allows us to disable blending in certain
     * situations in order to improve performance.
     */
    public static final int SRC_IS_OPAQUE    = (1 << 0);
    /**
     * Indicates that the operation uses an alpha mask, which may determine
     * the code path that is used when setting up the current paint state.
     */
    public static final int USE_MASK         = (1 << 1);

    protected RenderQueue rq;
    protected RenderBuffer buf;

    /**
     * This is a reference to the most recently validated BufferedContext.  If
     * this value is null, it means that there is no current context.  It is
     * provided here so that validate() only needs to do a quick reference
     * check to see if the BufferedContext passed to that method is the same
     * as the one we've cached here.
     */
    protected static BufferedContext currentContext;

92 93
    private AccelSurface    validatedSrcData;
    private AccelSurface    validatedDstData;
D
duke 已提交
94 95 96
    private Region          validatedClip;
    private Composite       validatedComp;
    private Paint           validatedPaint;
97 98
    // renamed from isValidatedPaintAColor as part of a work around for 6764257
    private boolean         isValidatedPaintJustAColor;
99
    private int             validatedRGB;
D
duke 已提交
100 101
    private int             validatedFlags;
    private boolean         xformInUse;
102 103
    private int             transX;
    private int             transY;
D
duke 已提交
104 105 106 107 108 109

    protected BufferedContext(RenderQueue rq) {
        this.rq = rq;
        this.buf = rq.getBuffer();
    }

110 111 112 113 114 115 116 117
    /**
     * Fetches the BufferedContextContext associated with the dst. surface
     * and validates the context using the given parameters.  Most rendering
     * operations will call this method first in order to set the necessary
     * state before issuing rendering commands.
     *
     * Note: must be called while the RenderQueue lock is held.
     *
118 119
     * It's assumed that the type of surfaces has been checked by the Renderer
     *
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
     * @throws InvalidPipeException if either src or dest surface is not valid
     * or lost
     * @see RenderQueue#lock
     * @see RenderQueue#unlock
     */
    public static void validateContext(AccelSurface srcData,
                                       AccelSurface dstData,
                                       Region clip, Composite comp,
                                       AffineTransform xform,
                                       Paint paint, SunGraphics2D sg2d,
                                       int flags)
    {
        // assert rq.lock.isHeldByCurrentThread();
        BufferedContext d3dc = dstData.getContext();
        d3dc.validate(srcData, dstData,
                      clip, comp, xform, paint, sg2d, flags);
    }

    /**
     * Fetches the BufferedContextassociated with the surface
     * and disables all context state settings.
     *
     * Note: must be called while the RenderQueue lock is held.
     *
144 145
     * It's assumed that the type of surfaces has been checked by the Renderer
     *
146 147 148 149 150 151 152 153 154 155 156
     * @throws InvalidPipeException if the surface is not valid
     * or lost
     * @see RenderQueue#lock
     * @see RenderQueue#unlock
     */
    public static void validateContext(AccelSurface surface) {
        // assert rt.lock.isHeldByCurrentThread();
        validateContext(surface, surface,
                        null, null, null, null, null, NO_CONTEXT_FLAGS);
    }

D
duke 已提交
157 158 159 160 161 162 163 164 165 166 167
    /**
     * Validates the given parameters against the current state for this
     * context.  If this context is not current, it will be made current
     * for the given source and destination surfaces, and the viewport will
     * be updated.  Then each part of the context state (clip, composite,
     * etc.) is checked against the previous value.  If the value has changed
     * since the last call to validate(), it will be updated accordingly.
     *
     * Note that the SunGraphics2D parameter is only used for the purposes
     * of validating a (non-null) Paint parameter.  In all other cases it
     * is safe to pass a null SunGraphics2D and it will be ignored.
168 169 170
     *
     * Note: must be called while the RenderQueue lock is held.
     *
171 172
     * It's assumed that the type of surfaces has been checked by the Renderer
     *
173 174
     * @throws InvalidPipeException if either src or dest surface is not valid
     * or lost
D
duke 已提交
175
     */
176
    public void validate(AccelSurface srcData, AccelSurface dstData,
D
duke 已提交
177 178 179 180 181 182
                         Region clip, Composite comp,
                         AffineTransform xform,
                         Paint paint, SunGraphics2D sg2d, int flags)
    {
        // assert rq.lock.isHeldByCurrentThread();

183 184
        boolean updateClip = false;
        boolean updatePaint = false;
D
duke 已提交
185

186 187 188 189 190 191 192 193 194 195
        if (!dstData.isValid() ||
            dstData.isSurfaceLost() || srcData.isSurfaceLost())
        {
            invalidateContext();
            throw new InvalidPipeException("bounds changed or surface lost");
        }

        if (paint instanceof Color) {
            // REMIND: not 30-bit friendly
            int newRGB = ((Color)paint).getRGB();
196
            if (isValidatedPaintJustAColor) {
197 198 199 200 201 202 203
                if (newRGB != validatedRGB) {
                    validatedRGB = newRGB;
                    updatePaint = true;
                }
            } else {
                validatedRGB = newRGB;
                updatePaint = true;
204
                isValidatedPaintJustAColor = true;
205 206 207 208 209
            }
        } else if (validatedPaint != paint) {
            updatePaint = true;
            // this should be set when we are switching from paint to color
            // in which case this condition will be true
210
            isValidatedPaintJustAColor = false;
D
duke 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
        }

        if ((currentContext != this) ||
            (srcData != validatedSrcData) ||
            (dstData != validatedDstData))
        {
            if (dstData != validatedDstData) {
                // the clip is dependent on the destination surface, so we
                // need to update it if we have a new destination surface
                updateClip = true;
            }

            if (paint == null) {
                // make sure we update the color state (otherwise, it might
                // not be updated if this is the first time the context
                // is being validated)
                updatePaint = true;
            }

            // update the current source and destination surfaces
            setSurfaces(srcData, dstData);

            currentContext = this;
            validatedSrcData = srcData;
            validatedDstData = dstData;
        }

        // validate clip
239
        if ((clip != validatedClip) || updateClip) {
D
duke 已提交
240
            if (clip != null) {
241 242 243 244 245 246 247 248 249 250
                if (updateClip ||
                    validatedClip == null ||
                    !(validatedClip.isRectangular() && clip.isRectangular()) ||
                    ((clip.getLoX() != validatedClip.getLoX() ||
                      clip.getLoY() != validatedClip.getLoY() ||
                      clip.getHiX() != validatedClip.getHiX() ||
                      clip.getHiY() != validatedClip.getHiY())))
                {
                    setClip(clip);
                }
D
duke 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
            } else {
                resetClip();
            }
            validatedClip = clip;
        }

        // validate composite (note that a change in the context flags
        // may require us to update the composite state, even if the
        // composite has not changed)
        if ((comp != validatedComp) || (flags != validatedFlags)) {
            if (comp != null) {
                setComposite(comp, flags);
            } else {
                resetComposite();
            }
            // the paint state is dependent on the composite state, so make
            // sure we update the color below
            updatePaint = true;
            validatedComp = comp;
            validatedFlags = flags;
        }

        // validate transform
274
        boolean txChanged = false;
D
duke 已提交
275 276 277 278
        if (xform == null) {
            if (xformInUse) {
                resetTransform();
                xformInUse = false;
279 280 281 282 283 284 285 286 287
                txChanged = true;
            } else if (sg2d != null) {
                if (transX != sg2d.transX || transY != sg2d.transY) {
                    txChanged = true;
                }
            }
            if (sg2d != null) {
                transX = sg2d.transX;
                transY = sg2d.transY;
D
duke 已提交
288 289 290 291
            }
        } else {
            setTransform(xform);
            xformInUse = true;
292 293 294
            txChanged = true;
        }
        // non-Color paints may require paint revalidation
295
        if (!isValidatedPaintJustAColor && txChanged) {
296
            updatePaint = true;
D
duke 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309
        }

        // validate paint
        if (updatePaint) {
            if (paint != null) {
                BufferedPaints.setPaint(rq, sg2d, paint, flags);
            } else {
                BufferedPaints.resetPaint(rq);
            }
            validatedPaint = paint;
        }

        // mark dstData dirty
310
        // REMIND: is this really needed now? we do it in SunGraphics2D..
D
duke 已提交
311 312 313 314 315 316 317
        dstData.markDirty();
    }

    /**
     * Invalidates the surfaces associated with this context.  This is
     * useful when the context is no longer needed, and we want to break
     * the chain caused by these surface references.
318 319 320 321 322
     *
     * Note: must be called while the RenderQueue lock is held.
     *
     * @see RenderQueue#lock
     * @see RenderQueue#unlock
D
duke 已提交
323 324 325 326 327 328
     */
    public void invalidateSurfaces() {
        validatedSrcData = null;
        validatedDstData = null;
    }

329 330 331
    private void setSurfaces(AccelSurface srcData,
                             AccelSurface dstData)
    {
D
duke 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
        // assert rq.lock.isHeldByCurrentThread();
        rq.ensureCapacityAndAlignment(20, 4);
        buf.putInt(SET_SURFACES);
        buf.putLong(srcData.getNativeOps());
        buf.putLong(dstData.getNativeOps());
    }

    private void resetClip() {
        // assert rq.lock.isHeldByCurrentThread();
        rq.ensureCapacity(4);
        buf.putInt(RESET_CLIP);
    }

    private void setClip(Region clip) {
        // assert rq.lock.isHeldByCurrentThread();
        if (clip.isRectangular()) {
            rq.ensureCapacity(20);
            buf.putInt(SET_RECT_CLIP);
            buf.putInt(clip.getLoX()).putInt(clip.getLoY());
            buf.putInt(clip.getHiX()).putInt(clip.getHiY());
        } else {
            rq.ensureCapacity(28); // so that we have room for at least a span
            buf.putInt(BEGIN_SHAPE_CLIP);
            buf.putInt(SET_SHAPE_CLIP_SPANS);
            // include a placeholder for the span count
            int countIndex = buf.position();
            buf.putInt(0);
            int spanCount = 0;
            int remainingSpans = buf.remaining() / BYTES_PER_SPAN;
            int span[] = new int[4];
            SpanIterator si = clip.getSpanIterator();
            while (si.nextSpan(span)) {
                if (remainingSpans == 0) {
                    buf.putInt(countIndex, spanCount);
                    rq.flushNow();
                    buf.putInt(SET_SHAPE_CLIP_SPANS);
                    countIndex = buf.position();
                    buf.putInt(0);
                    spanCount = 0;
                    remainingSpans = buf.remaining() / BYTES_PER_SPAN;
                }
                buf.putInt(span[0]); // x1
                buf.putInt(span[1]); // y1
                buf.putInt(span[2]); // x2
                buf.putInt(span[3]); // y2
                spanCount++;
                remainingSpans--;
            }
            buf.putInt(countIndex, spanCount);
            rq.ensureCapacity(4);
            buf.putInt(END_SHAPE_CLIP);
        }
    }

    private void resetComposite() {
        // assert rq.lock.isHeldByCurrentThread();
        rq.ensureCapacity(4);
        buf.putInt(RESET_COMPOSITE);
    }

    private void setComposite(Composite comp, int flags) {
        // assert rq.lock.isHeldByCurrentThread();
        if (comp instanceof AlphaComposite) {
            AlphaComposite ac = (AlphaComposite)comp;
            rq.ensureCapacity(16);
            buf.putInt(SET_ALPHA_COMPOSITE);
            buf.putInt(ac.getRule());
            buf.putFloat(ac.getAlpha());
            buf.putInt(flags);
        } else if (comp instanceof XORComposite) {
            int xorPixel = ((XORComposite)comp).getXorPixel();
            rq.ensureCapacity(8);
            buf.putInt(SET_XOR_COMPOSITE);
            buf.putInt(xorPixel);
        } else {
            throw new InternalError("not yet implemented");
        }
    }

    private void resetTransform() {
        // assert rq.lock.isHeldByCurrentThread();
        rq.ensureCapacity(4);
        buf.putInt(RESET_TRANSFORM);
    }

    private void setTransform(AffineTransform xform) {
        // assert rq.lock.isHeldByCurrentThread();
        rq.ensureCapacityAndAlignment(52, 4);
        buf.putInt(SET_TRANSFORM);
        buf.putDouble(xform.getScaleX());
        buf.putDouble(xform.getShearY());
        buf.putDouble(xform.getShearX());
        buf.putDouble(xform.getScaleY());
        buf.putDouble(xform.getTranslateX());
        buf.putDouble(xform.getTranslateY());
    }
428 429 430 431 432 433 434 435 436 437 438 439 440

    /**
     * Resets this context's surfaces and all attributes.
     *
     * Note: must be called while the RenderQueue lock is held.
     *
     * @see RenderQueue#lock
     * @see RenderQueue#unlock
     */
    public void invalidateContext() {
        resetTransform();
        resetComposite();
        resetClip();
441
        BufferedPaints.resetPaint(rq);
442 443 444 445
        invalidateSurfaces();
        validatedComp = null;
        validatedClip = null;
        validatedPaint = null;
446
        isValidatedPaintJustAColor = false;
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
        xformInUse = false;
    }

    /**
     * Returns a singleton {@code RenderQueue} object used by the rendering
     * pipeline.
     *
     * @return a render queue
     * @see RenderQueue
     */
    public abstract RenderQueue getRenderQueue();

    /**
     * Saves the the state of this context.
     * It may reset the current context.
     *
     * Note: must be called while the RenderQueue lock is held.
     *
     * @see RenderQueue#lock
     * @see RenderQueue#unlock
     */
    public abstract void saveState();

    /**
     * Restores the native state of this context.
     * It may reset the current context.
     *
     * Note: must be called while the RenderQueue lock is held.
     *
     * @see RenderQueue#lock
     * @see RenderQueue#unlock
     */
    public abstract void restoreState();
D
duke 已提交
480
}