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.awt.image;
430N/A
430N/Aimport java.awt.image.BufferStrategy;
430N/Aimport java.lang.ref.WeakReference;
430N/A
430N/A/**
430N/A * Manages v-synced buffer strategies.
430N/A */
430N/Apublic abstract class VSyncedBSManager {
430N/A
430N/A private static VSyncedBSManager theInstance;
430N/A
430N/A private static final boolean vSyncLimit =
430N/A Boolean.valueOf((String)java.security.AccessController.doPrivileged(
430N/A new sun.security.action.GetPropertyAction(
430N/A "sun.java2d.vsynclimit", "true")));
430N/A
430N/A private static VSyncedBSManager getInstance(boolean create) {
430N/A if (theInstance == null && create) {
430N/A theInstance =
430N/A vSyncLimit ? new SingleVSyncedBSMgr() : new NoLimitVSyncBSMgr();
430N/A }
430N/A return theInstance;
430N/A }
430N/A
430N/A abstract boolean checkAllowed(BufferStrategy bs);
430N/A abstract void relinquishVsync(BufferStrategy bs);
430N/A
430N/A /**
430N/A * Returns true if the buffer strategy is allowed to be created
430N/A * v-synced.
430N/A *
430N/A * @return true if the bs is allowed to be v-synced, false otherwise
430N/A */
430N/A public static boolean vsyncAllowed(BufferStrategy bs) {
430N/A VSyncedBSManager bsm = getInstance(true);
430N/A return bsm.checkAllowed(bs);
430N/A }
430N/A
430N/A /**
430N/A * Lets the manager know that this buffer strategy is no longer interested
430N/A * in being v-synced.
430N/A */
430N/A public static synchronized void releaseVsync(BufferStrategy bs) {
430N/A VSyncedBSManager bsm = getInstance(false);
430N/A if (bsm != null) {
430N/A bsm.relinquishVsync(bs);
430N/A }
430N/A }
430N/A
430N/A /**
430N/A * An instance of the manager which allows any buffer strategy to be
430N/A * v-synced.
430N/A */
430N/A private static final class NoLimitVSyncBSMgr extends VSyncedBSManager {
430N/A @Override
430N/A boolean checkAllowed(BufferStrategy bs) {
430N/A return true;
430N/A }
430N/A
430N/A @Override
430N/A void relinquishVsync(BufferStrategy bs) {
430N/A }
430N/A }
430N/A
430N/A /**
430N/A * An instance of the manager which allows only one buffer strategy to
430N/A * be v-synced at any give moment in the vm.
430N/A */
430N/A private static final class SingleVSyncedBSMgr extends VSyncedBSManager {
430N/A private WeakReference<BufferStrategy> strategy;
430N/A
430N/A @Override
430N/A public synchronized boolean checkAllowed(BufferStrategy bs) {
430N/A if (strategy != null) {
430N/A BufferStrategy current = strategy.get();
430N/A if (current != null) {
430N/A return (current == bs);
430N/A }
430N/A }
430N/A strategy = new WeakReference<BufferStrategy>(bs);
430N/A return true;
430N/A }
430N/A
430N/A @Override
430N/A public synchronized void relinquishVsync(BufferStrategy bs) {
430N/A if (strategy != null) {
430N/A BufferStrategy b = strategy.get();
430N/A if (b == bs) {
430N/A strategy.clear();
430N/A strategy = null;
430N/A }
430N/A }
430N/A }
430N/A }
430N/A}