src/java.desktop/macosx/classes/sun/java2d/metal/MetalRenderQueue.java
author aghaisas
Wed, 20 Feb 2019 17:00:40 +0530
branchmetal-prototype-branch
changeset 57196 a95707a39ff5
permissions -rw-r--r--
Description : Metal Rendering Pipeline - initial implementation of line and quad rendering Contributed-by: jdv, aghaisas
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57196
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
     1
/*
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
     2
 * Copyright (c) 2019, 2019, Oracle and/or its affiliates. All rights reserved.
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
     4
 *
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    10
 *
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    15
 * accompanied this code).
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    16
 *
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    20
 *
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    23
 * questions.
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    24
 */
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    25
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    26
package sun.java2d.metal;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    27
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    28
import sun.awt.util.ThreadGroupUtils;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    29
import sun.java2d.pipe.RenderBuffer;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    30
import sun.java2d.pipe.RenderQueue;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    31
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    32
import static sun.java2d.pipe.BufferedOpCodes.*;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    33
import java.security.AccessController;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    34
import java.security.PrivilegedAction;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    35
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    36
/**
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    37
 * OGL-specific implementation of RenderQueue.  This class provides a
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    38
 * single (daemon) thread that is responsible for periodically flushing
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    39
 * the queue, thus ensuring that only one thread communicates with the native
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    40
 * OpenGL libraries for the entire process.
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    41
 */
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    42
public class MetalRenderQueue extends RenderQueue {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    43
    
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    44
    private static MetalRenderQueue theInstance;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    45
    private final QueueFlusher flusher;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    46
    
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    47
    private MetalRenderQueue() {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    48
        /*
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    49
         * The thread must be a member of a thread group
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    50
         * which will not get GCed before VM exit.
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    51
         */
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    52
        flusher = AccessController.doPrivileged((PrivilegedAction<QueueFlusher>) QueueFlusher::new);
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    53
    }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    54
    
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    55
    /**
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    56
     * Returns the single OGLRenderQueue instance.  If it has not yet been
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    57
     * initialized, this method will first construct the single instance
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    58
     * before returning it.
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    59
     */
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    60
    public static synchronized MetalRenderQueue getInstance() {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    61
        if (theInstance == null) {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    62
            theInstance = new MetalRenderQueue();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    63
        }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    64
        return theInstance;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    65
    }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    66
    
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    67
    /**
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    68
     * Flushes the single OGLRenderQueue instance synchronously.  If an
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    69
     * OGLRenderQueue has not yet been instantiated, this method is a no-op.
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    70
     * This method is useful in the case of Toolkit.sync(), in which we want
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    71
     * to flush the OGL pipeline, but only if the OGL pipeline is currently
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    72
     * enabled.  Since this class has few external dependencies, callers need
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    73
     * not be concerned that calling this method will trigger initialization
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    74
     * of the OGL pipeline and related classes.
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    75
     */
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    76
    public static void sync() {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    77
        if (theInstance != null) {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    78
            theInstance.lock();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    79
            try {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    80
                theInstance.ensureCapacity(4);
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    81
                theInstance.getBuffer().putInt(SYNC);
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    82
                theInstance.flushNow();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    83
            } finally {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    84
                theInstance.unlock();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    85
            }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    86
        }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    87
    }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    88
    
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    89
    /**
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    90
     * Disposes the native memory associated with the given native
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    91
     * graphics config info pointer on the single queue flushing thread.
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    92
     */
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    93
    public static void disposeGraphicsConfig(long pConfigInfo) {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    94
        MetalRenderQueue rq = getInstance();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    95
        rq.lock();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    96
        try {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    97
            // make sure we make the context associated with the given
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    98
            // GraphicsConfig current before disposing the native resources
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
    99
            MetalContext.setScratchSurface(pConfigInfo);
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   100
            
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   101
            RenderBuffer buf = rq.getBuffer();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   102
            rq.ensureCapacityAndAlignment(12, 4);
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   103
            buf.putInt(DISPOSE_CONFIG);
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   104
            buf.putLong(pConfigInfo);
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   105
            
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   106
            // this call is expected to complete synchronously, so flush now
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   107
            rq.flushNow();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   108
        } finally {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   109
            rq.unlock();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   110
        }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   111
    }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   112
    
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   113
    /**
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   114
     * Returns true if the current thread is the OGL QueueFlusher thread.
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   115
     */
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   116
    public static boolean isQueueFlusherThread() {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   117
        return (Thread.currentThread() == getInstance().flusher.thread);
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   118
    }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   119
    
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   120
    public void flushNow() {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   121
        // assert lock.isHeldByCurrentThread();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   122
        try {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   123
            flusher.flushNow();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   124
        } catch (Exception e) {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   125
            System.err.println("exception in flushNow:");
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   126
            e.printStackTrace();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   127
        }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   128
    }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   129
    
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   130
    public void flushAndInvokeNow(Runnable r) {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   131
        // assert lock.isHeldByCurrentThread();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   132
        try {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   133
            flusher.flushAndInvokeNow(r);
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   134
        } catch (Exception e) {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   135
            System.err.println("exception in flushAndInvokeNow:");
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   136
            e.printStackTrace();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   137
        }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   138
    }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   139
    
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   140
    private native long flushBuffer(long buf, int limit);
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   141
    
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   142
    private void flushBuffer() {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   143
        
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   144
        // assert lock.isHeldByCurrentThread();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   145
        int limit = buf.position();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   146
        if (limit > 0) {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   147
            System.out.println("FlushBuffer ---------- invoking RQ native flushBuffer");
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   148
            
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   149
            // process the queue
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   150
            flushBuffer(buf.getAddress(), limit);
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   151
        }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   152
        // reset the buffer position
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   153
        buf.clear();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   154
        // clear the set of references, since we no longer need them
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   155
        refSet.clear();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   156
    }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   157
    
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   158
    private class QueueFlusher implements Runnable {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   159
        private boolean needsFlush;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   160
        private Runnable task;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   161
        private Error error;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   162
        private final Thread thread;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   163
        
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   164
        public QueueFlusher() {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   165
            String name = "Java2D Queue Flusher";
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   166
            thread = new Thread(ThreadGroupUtils.getRootThreadGroup(),
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   167
                                this, name, 0, false);
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   168
            thread.setDaemon(true);
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   169
            thread.setPriority(Thread.MAX_PRIORITY);
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   170
            thread.start();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   171
        }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   172
        
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   173
        public synchronized void flushNow() {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   174
            // wake up the flusher
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   175
            needsFlush = true;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   176
            System.out.println("****** QueueFlusher : about to notify flusher thread");
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   177
            notify();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   178
            
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   179
            // wait for flush to complete
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   180
            while (needsFlush) {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   181
                try {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   182
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   183
                    System.out.println("****** QueueFlusher : waiting for flush to complete"); 
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   184
                    wait();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   185
                } catch (InterruptedException e) {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   186
                }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   187
            }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   188
            
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   189
            // re-throw any error that may have occurred during the flush
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   190
            if (error != null) {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   191
                throw error;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   192
            }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   193
        }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   194
        
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   195
        public synchronized void flushAndInvokeNow(Runnable task) {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   196
            this.task = task;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   197
            flushNow();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   198
        }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   199
        
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   200
        public synchronized void run() {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   201
            boolean timedOut = false;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   202
            while (true) {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   203
                while (!needsFlush) {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   204
                    try {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   205
                        timedOut = false;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   206
                        /*
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   207
                         * Wait until we're woken up with a flushNow() call,
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   208
                         * or the timeout period elapses (so that we can
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   209
                         * flush the queue periodically).
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   210
                         */
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   211
                        wait(100);
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   212
                        /*
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   213
                         * We will automatically flush the queue if the
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   214
                         * following conditions apply:
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   215
                         *   - the wait() timed out
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   216
                         *   - we can lock the queue (without blocking)
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   217
                         *   - there is something in the queue to flush
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   218
                         * Otherwise, just continue (we'll flush eventually).
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   219
                         */
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   220
                        if (!needsFlush && (timedOut = tryLock())) {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   221
                            if (buf.position() > 0) {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   222
                                needsFlush = true;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   223
                            } else {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   224
                                unlock();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   225
                            }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   226
                        }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   227
                    } catch (InterruptedException e) {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   228
                    }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   229
                }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   230
                try {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   231
                    // reset the throwable state
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   232
                    error = null;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   233
                    // flush the buffer now
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   234
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   235
                    System.out.println("Thread invoking flushBuffer -------------- ");
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   236
                    flushBuffer();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   237
                    // if there's a task, invoke that now as well
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   238
                    if (task != null) {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   239
                        task.run();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   240
                    }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   241
                } catch (Error e) {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   242
                    error = e;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   243
                } catch (Exception x) {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   244
                    System.err.println("exception in QueueFlusher:");
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   245
                    x.printStackTrace();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   246
                } finally {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   247
                    if (timedOut) {
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   248
                        unlock();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   249
                    }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   250
                    task = null;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   251
                    // allow the waiting thread to continue
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   252
                    needsFlush = false;
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   253
                    notify();
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   254
                }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   255
            }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   256
        }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   257
    }
a95707a39ff5 Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
diff changeset
   258
}