430N/A/*
2362N/A * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
430N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
430N/A *
430N/A * This code is free software; you can redistribute it and/or modify it
430N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
430N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
430N/A *
430N/A * This code is distributed in the hope that it will be useful, but WITHOUT
430N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
430N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
430N/A * version 2 for more details (a copy is included in the LICENSE file that
430N/A * accompanied this code).
430N/A *
430N/A * You should have received a copy of the GNU General Public License version
430N/A * 2 along with this work; if not, write to the Free Software Foundation,
430N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
430N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
430N/A */
430N/A
430N/Apackage sun.java2d.d3d;
430N/A
430N/Aimport sun.java2d.ScreenUpdateManager;
430N/Aimport sun.java2d.pipe.RenderBuffer;
430N/Aimport sun.java2d.pipe.RenderQueue;
430N/Aimport static sun.java2d.pipe.BufferedOpCodes.*;
430N/A
430N/A/**
430N/A * D3D-specific implementation of RenderQueue.
430N/A */
430N/Apublic class D3DRenderQueue extends RenderQueue {
430N/A
430N/A private static D3DRenderQueue theInstance;
430N/A private static Thread rqThread;
430N/A
430N/A private D3DRenderQueue() {
430N/A }
430N/A
430N/A /**
430N/A * Returns the single D3DRenderQueue instance. If it has not yet been
430N/A * initialized, this method will first construct the single instance
430N/A * before returning it.
430N/A */
430N/A public static synchronized D3DRenderQueue getInstance() {
430N/A if (theInstance == null) {
430N/A theInstance = new D3DRenderQueue();
430N/A // no need to lock, noone has reference to this instance yet
430N/A theInstance.flushAndInvokeNow(new Runnable() {
430N/A public void run() {
430N/A rqThread = Thread.currentThread();
430N/A }
430N/A });
430N/A }
430N/A return theInstance;
430N/A }
430N/A
430N/A /**
430N/A * Flushes the single D3DRenderQueue instance synchronously. If an
430N/A * D3DRenderQueue has not yet been instantiated, this method is a no-op.
430N/A * This method is useful in the case of Toolkit.sync(), in which we want
430N/A * to flush the D3D pipeline, but only if the D3D pipeline is currently
430N/A * enabled. Since this class has few external dependencies, callers need
430N/A * not be concerned that calling this method will trigger initialization
430N/A * of the D3D pipeline and related classes.
430N/A */
430N/A public static void sync() {
430N/A if (theInstance != null) {
430N/A // need to make sure any/all screen surfaces are presented prior
430N/A // to completing the sync operation
430N/A D3DScreenUpdateManager mgr =
430N/A (D3DScreenUpdateManager)ScreenUpdateManager.getInstance();
430N/A mgr.runUpdateNow();
430N/A
430N/A theInstance.lock();
430N/A try {
430N/A theInstance.ensureCapacity(4);
430N/A theInstance.getBuffer().putInt(SYNC);
430N/A theInstance.flushNow();
430N/A } finally {
430N/A theInstance.unlock();
430N/A }
430N/A }
430N/A }
430N/A
430N/A /**
430N/A * Attempt to restore the devices if they're in the lost state.
430N/A * (used when a full-screen window is activated/deactivated)
430N/A */
430N/A public static void restoreDevices() {
430N/A D3DRenderQueue rq = getInstance();
430N/A rq.lock();
430N/A try {
430N/A rq.ensureCapacity(4);
430N/A rq.getBuffer().putInt(RESTORE_DEVICES);
430N/A rq.flushNow();
430N/A } finally {
430N/A rq.unlock();
430N/A }
430N/A }
430N/A
430N/A /**
430N/A * @return true if current thread is the render queue thread,
430N/A * false otherwise
430N/A */
430N/A public static boolean isRenderQueueThread() {
430N/A return (Thread.currentThread() == rqThread);
430N/A }
430N/A
430N/A /**
430N/A * Disposes the native memory associated with the given native
430N/A * graphics config info pointer on the single queue flushing thread.
430N/A */
430N/A public static void disposeGraphicsConfig(long pConfigInfo) {
430N/A D3DRenderQueue rq = getInstance();
430N/A rq.lock();
430N/A try {
430N/A
430N/A RenderBuffer buf = rq.getBuffer();
430N/A rq.ensureCapacityAndAlignment(12, 4);
430N/A buf.putInt(DISPOSE_CONFIG);
430N/A buf.putLong(pConfigInfo);
430N/A
430N/A // this call is expected to complete synchronously, so flush now
430N/A rq.flushNow();
430N/A } finally {
430N/A rq.unlock();
430N/A }
430N/A }
430N/A
430N/A public void flushNow() {
430N/A // assert lock.isHeldByCurrentThread();
430N/A flushBuffer(null);
430N/A }
430N/A
430N/A public void flushAndInvokeNow(Runnable r) {
430N/A // assert lock.isHeldByCurrentThread();
430N/A flushBuffer(r);
430N/A }
430N/A
430N/A private native void flushBuffer(long buf, int limit, Runnable task);
430N/A
430N/A private void flushBuffer(Runnable task) {
430N/A // assert lock.isHeldByCurrentThread();
430N/A int limit = buf.position();
430N/A if (limit > 0 || task != null) {
430N/A // process the queue
430N/A flushBuffer(buf.getAddress(), limit, task);
430N/A }
430N/A // reset the buffer position
430N/A buf.clear();
430N/A // clear the set of references, since we no longer need them
430N/A refSet.clear();
430N/A }
430N/A}