1 /*
   2  * Copyright (c) 2019, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.java2d.metal;
  27 
  28 import sun.java2d.pipe.BufferedContext;
  29 import sun.java2d.pipe.RenderBuffer;
  30 import sun.java2d.pipe.RenderQueue;
  31 import sun.java2d.pipe.hw.ContextCapabilities;
  32 import static sun.java2d.pipe.BufferedOpCodes.*;
  33 import static sun.java2d.pipe.hw.ContextCapabilities.*;
  34 
  35 import java.lang.annotation.Native;
  36 
  37 /**
  38  * Note that the RenderQueue lock must be acquired before calling any of
  39  * the methods in this class.
  40  */
  41 public class MetalContext extends BufferedContext {
  42 
  43     private final MetalGraphicsConfig config;
  44 
  45     MetalContext(RenderQueue rq, MetalGraphicsConfig config) {
  46         super(rq);
  47         this.config = config;
  48     }
  49 
  50     static void setScratchSurface(MetalGraphicsConfig gc) {
  51         setScratchSurface(gc.getNativeConfigInfo());
  52     }
  53  
  54     /**
  55      * Makes the given GraphicsConfig's context current to its associated
  56      * "scratch surface".  Each GraphicsConfig maintains a native context
  57      * (GLXContext on Unix, HGLRC on Windows) as well as a native pbuffer
  58      * known as the "scratch surface".  By making the context current to the
  59      * scratch surface, we are assured that we have a current context for
  60      * the relevant GraphicsConfig, and can therefore perform operations
  61      * depending on the capabilities of that GraphicsConfig.  For example,
  62      * if the GraphicsConfig supports the GL_ARB_texture_non_power_of_two
  63      * extension, then we should be able to make a non-pow2 texture for this
  64      * GraphicsConfig once we make the context current to the scratch surface.
  65      *
  66      * This method should be used for operations with an OpenGL texture
  67      * as the destination surface (e.g. a sw->texture blit loop), or in those
  68      * situations where we may not otherwise have a current context (e.g.
  69      * when disposing a texture-based surface).
  70      */
  71     static void setScratchSurface(long pConfigInfo) {
  72         // assert OGLRenderQueue.getInstance().lock.isHeldByCurrentThread();
  73 
  74         // invalidate the current context
  75         currentContext = null;
  76 
  77         // set the scratch context
  78         MetalRenderQueue rq = MetalRenderQueue.getInstance();
  79         RenderBuffer buf = rq.getBuffer();
  80         rq.ensureCapacityAndAlignment(12, 4);
  81         buf.putInt(SET_SCRATCH_SURFACE);
  82         buf.putLong(pConfigInfo);
  83     }
  84 
  85     /**
  86      * Invalidates the currentContext field to ensure that we properly
  87      * revalidate the OGLContext (make it current, etc.) next time through
  88      * the validate() method.  This is typically invoked from methods
  89      * that affect the current context state (e.g. disposing a context or
  90      * surface).
  91      */
  92     static void invalidateCurrentContext() {
  93         // assert OGLRenderQueue.getInstance().lock.isHeldByCurrentThread();
  94 
  95         // invalidate the current Java-level context so that we
  96         // revalidate everything the next time around
  97         if (currentContext != null) {
  98             currentContext.invalidateContext();
  99             currentContext = null;
 100         }
 101 
 102         // invalidate the context reference at the native level, and
 103         // then flush the queue so that we have no pending operations
 104         // dependent on the current context
 105         MetalRenderQueue rq = MetalRenderQueue.getInstance();
 106         rq.ensureCapacity(4);
 107         rq.getBuffer().putInt(INVALIDATE_CONTEXT);
 108         rq.flushNow();
 109     }
 110 
 111     public RenderQueue getRenderQueue() {
 112         return MetalRenderQueue.getInstance();
 113     }
 114 
 115     /**
 116      * Returns a string representing adapter id (vendor, renderer, version).
 117      * Must be called on the rendering thread.
 118      *
 119      * @return an id string for the adapter
 120      */
 121     //static final native String getOGLIdString();
 122 
 123     @Override
 124     public void saveState() {
 125         // assert rq.lock.isHeldByCurrentThread();
 126 
 127         // reset all attributes of this and current contexts
 128         invalidateContext();
 129         invalidateCurrentContext();
 130 
 131         setScratchSurface(config);
 132 
 133         // save the state on the native level
 134         rq.ensureCapacity(4);
 135         buf.putInt(SAVE_STATE);
 136         rq.flushNow();
 137     }
 138 
 139     @Override
 140     public void restoreState() {
 141         // assert rq.lock.isHeldByCurrentThread();
 142 
 143         // reset all attributes of this and current contexts
 144         invalidateContext();
 145         invalidateCurrentContext();
 146 
 147         setScratchSurface(config);
 148 
 149         // restore the state on the native level
 150         rq.ensureCapacity(4);
 151         buf.putInt(RESTORE_STATE);
 152         rq.flushNow();
 153     }
 154 
 155     //static class OGLContextCaps extends ContextCapabilities {
 156         /**
 157          * Indicates the presence of the GL_EXT_framebuffer_object extension.
 158          * This cap will only be set if the fbobject system property has been
 159          * enabled and we are able to create an FBO with depth buffer.
 160          */
 161     //  @Native
 162     // static final int CAPS_EXT_FBOBJECT     =
 163     //            (CAPS_RT_TEXTURE_ALPHA | CAPS_RT_TEXTURE_OPAQUE);
 164         /** Indicates that the context is doublebuffered. */
 165     //    @Native
 166     //    static final int CAPS_DOUBLEBUFFERED   = (FIRST_PRIVATE_CAP << 0);
 167         /**
 168          * Indicates the presence of the GL_ARB_fragment_shader extension.
 169          * This cap will only be set if the lcdshader system property has been
 170          * enabled and the hardware supports the minimum number of texture units
 171          */
 172     //    @Native
 173     //    static final int CAPS_EXT_LCD_SHADER   = (FIRST_PRIVATE_CAP << 1);
 174         /**
 175          * Indicates the presence of the GL_ARB_fragment_shader extension.
 176          * This cap will only be set if the biopshader system property has been
 177          * enabled and the hardware meets our minimum requirements.
 178          */
 179     //    @Native
 180     //    static final int CAPS_EXT_BIOP_SHADER  = (FIRST_PRIVATE_CAP << 2);
 181         /**
 182          * Indicates the presence of the GL_ARB_fragment_shader extension.
 183          * This cap will only be set if the gradshader system property has been
 184          * enabled and the hardware meets our minimum requirements.
 185          */
 186     //    @Native
 187     //    static final int CAPS_EXT_GRAD_SHADER  = (FIRST_PRIVATE_CAP << 3);
 188         /** Indicates the presence of the GL_ARB_texture_rectangle extension. */
 189     //    @Native
 190     //    static final int CAPS_EXT_TEXRECT      = (FIRST_PRIVATE_CAP << 4);
 191         /** Indicates the presence of the GL_NV_texture_barrier extension. */
 192     //    @Native
 193     //    static final int CAPS_EXT_TEXBARRIER = (FIRST_PRIVATE_CAP << 5);
 194 
 195 
 196     /*    OGLContextCaps(int caps, String adapterId) {
 197             super(caps, adapterId);
 198         }
 199 
 200         @Override
 201         public String toString() {
 202             StringBuilder sb = new StringBuilder(super.toString());
 203             if ((caps & CAPS_EXT_FBOBJECT) != 0) {
 204                 sb.append("CAPS_EXT_FBOBJECT|");
 205             }
 206             if ((caps & CAPS_DOUBLEBUFFERED) != 0) {
 207                 sb.append("CAPS_DOUBLEBUFFERED|");
 208             }
 209             if ((caps & CAPS_EXT_LCD_SHADER) != 0) {
 210                 sb.append("CAPS_EXT_LCD_SHADER|");
 211             }
 212             if ((caps & CAPS_EXT_BIOP_SHADER) != 0) {
 213                 sb.append("CAPS_BIOP_SHADER|");
 214             }
 215             if ((caps & CAPS_EXT_GRAD_SHADER) != 0) {
 216                 sb.append("CAPS_EXT_GRAD_SHADER|");
 217             }
 218             if ((caps & CAPS_EXT_TEXRECT) != 0) {
 219                 sb.append("CAPS_EXT_TEXRECT|");
 220             }
 221             if ((caps & CAPS_EXT_TEXBARRIER) != 0) {
 222                 sb.append("CAPS_EXT_TEXBARRIER|");
 223             }
 224             return sb.toString();
 225         }
 226     }*/
 227 }