Path: blob/master/src/java.desktop/share/classes/sun/java2d/pipe/RenderQueue.java
41159 views
/*1* Copyright (c) 2005, 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.util.HashSet;28import java.util.Set;29import sun.awt.SunToolkit;3031/**32* The RenderQueue class encapsulates a RenderBuffer on which rendering33* operations are enqueued. Note that the RenderQueue lock must be acquired34* before performing any operations on the queue (e.g. enqueuing an operation35* or flushing the queue). A sample usage scenario follows:36*37* public void drawSomething(...) {38* rq.lock();39* try {40* ctx.validate(...);41* rq.ensureCapacity(4);42* rq.getBuffer().putInt(DRAW_SOMETHING);43* ...44* } finally {45* rq.unlock();46* }47* }48*49* If you are enqueuing an operation that involves 8-byte parameters (i.e.50* long or double values), it is imperative that you ensure proper51* alignment of the underlying RenderBuffer. This can be accomplished52* simply by providing an offset to the first 8-byte parameter in your53* operation to the ensureCapacityAndAlignment() method. For example:54*55* public void drawStuff(...) {56* rq.lock();57* try {58* RenderBuffer buf = rq.getBuffer();59* ctx.validate(...);60* // 28 total bytes in the operation, 12 bytes to the first long61* rq.ensureCapacityAndAlignment(28, 12);62* buf.putInt(DRAW_STUFF);63* buf.putInt(x).putInt(y);64* buf.putLong(addr1);65* buf.putLong(addr2);66* } finally {67* rq.unlock();68* }69* }70*/71public abstract class RenderQueue {7273/** The size of the underlying buffer, in bytes. */74private static final int BUFFER_SIZE = 32000;7576/** The underlying buffer for this queue. */77protected RenderBuffer buf;7879/**80* A Set containing hard references to Objects that must stay alive until81* the queue has been completely flushed.82*/83protected Set<Object> refSet;8485protected RenderQueue() {86refSet = new HashSet<>();87buf = RenderBuffer.allocate(BUFFER_SIZE);88}8990/**91* Locks the queue for read/write access.92*/93public final void lock() {94/*95* Implementation note: In theory we should have two separate locks:96* one lock to synchronize access to the RenderQueue, and then a97* separate lock (the AWT lock) that only needs to be acquired when98* we are about to flush the queue (using native windowing system99* operations). In practice it has been difficult to enforce the100* correct lock ordering; sometimes AWT will have already acquired101* the AWT lock before grabbing the RQ lock (see 6253009), while the102* expected order should be RQ lock and then AWT lock. Due to this103* issue, using two separate locks is prone to deadlocks. Therefore,104* to solve this issue we have decided to eliminate the separate RQ105* lock and instead just acquire the AWT lock here. (Someday it might106* be nice to go back to the old two-lock system, but that would107* require potentially risky changes to AWT to ensure that it never108* acquires the AWT lock before calling into 2D code that wants to109* acquire the RQ lock.)110*/111SunToolkit.awtLock();112}113114/**115* Attempts to lock the queue. If successful, this method returns true,116* indicating that the caller is responsible for calling117* {@code unlock}; otherwise this method returns false.118*/119public final boolean tryLock() {120return SunToolkit.awtTryLock();121}122123/**124* Unlocks the queue.125*/126public final void unlock() {127SunToolkit.awtUnlock();128}129130/**131* Adds the given Object to the set of hard references, which will132* prevent that Object from being disposed until the queue has been133* flushed completely. This is useful in cases where some enqueued134* data could become invalid if the reference Object were garbage135* collected before the queue could be processed. (For example, keeping136* a hard reference to a FontStrike will prevent any enqueued glyph137* images associated with that strike from becoming invalid before the138* queue is flushed.) The reference set will be cleared immediately139* after the queue is flushed each time.140*/141public final void addReference(Object ref) {142refSet.add(ref);143}144145/**146* Returns the encapsulated RenderBuffer object.147*/148public final RenderBuffer getBuffer() {149return buf;150}151152/**153* Ensures that there will be enough room on the underlying buffer154* for the following operation. If the operation will not fit given155* the remaining space, the buffer will be flushed immediately, leaving156* an empty buffer for the impending operation.157*158* @param opsize size (in bytes) of the following operation159*/160public final void ensureCapacity(int opsize) {161if (buf.remaining() < opsize) {162flushNow();163}164}165166/**167* Convenience method that is equivalent to calling ensureCapacity()168* followed by ensureAlignment(). The ensureCapacity() call allows for an169* extra 4 bytes of space in case the ensureAlignment() method needs to170* insert a NOOP token on the buffer.171*172* @param opsize size (in bytes) of the following operation173* @param first8ByteValueOffset offset (in bytes) from the current174* position to the first 8-byte value used in the following operation175*/176public final void ensureCapacityAndAlignment(int opsize,177int first8ByteValueOffset)178{179ensureCapacity(opsize + 4);180ensureAlignment(first8ByteValueOffset);181}182183/**184* Inserts a 4-byte NOOP token when necessary to ensure that all 8-byte185* parameters for the following operation are added to the underlying186* buffer with an 8-byte memory alignment.187*188* @param first8ByteValueOffset offset (in bytes) from the current189* position to the first 8-byte value used in the following operation190*/191public final void ensureAlignment(int first8ByteValueOffset) {192int first8ByteValuePosition = buf.position() + first8ByteValueOffset;193if ((first8ByteValuePosition & 7) != 0) {194buf.putInt(BufferedOpCodes.NOOP);195}196}197198/**199* Immediately processes each operation currently pending on the buffer.200* This method will block until the entire buffer has been flushed. The201* queue lock must be acquired before calling this method.202*/203public abstract void flushNow();204205/**206* Immediately processes each operation currently pending on the buffer,207* and then invokes the provided task. This method will block until the208* entire buffer has been flushed and the provided task has been executed.209* The queue lock must be acquired before calling this method.210*/211public abstract void flushAndInvokeNow(Runnable task);212213/**214* Updates the current position of the underlying buffer, and then215* flushes the queue immediately. This method is useful when native code216* has added data to the queue and needs to flush immediately.217*/218public void flushNow(int position) {219buf.position(position);220flushNow();221}222}223224225