1 /*
   2  * Copyright (c) 2011, 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 #import "CGLGraphicsConfig.h"
  27 #import "CGLLayer.h"
  28 #import "ThreadUtilities.h"
  29 #import "LWCToolkit.h"
  30 #import "CGLSurfaceData.h"
  31 
  32 
  33 extern NSOpenGLPixelFormat *sharedPixelFormat;
  34 extern NSOpenGLContext *sharedContext;
  35 
  36 @implementation CGLLayer
  37 
  38 @synthesize javaLayer;
  39 @synthesize textureID;
  40 @synthesize target;
  41 @synthesize textureWidth;
  42 @synthesize textureHeight;
  43 #ifdef REMOTELAYER
  44 @synthesize parentLayer;
  45 @synthesize remoteLayer;
  46 @synthesize jrsRemoteLayer;
  47 #endif
  48 
  49 - (id) initWithJavaLayer:(JNFJObjectWrapper *)layer;
  50 {
  51 AWT_ASSERT_APPKIT_THREAD;
  52     // Initialize ourselves
  53     self = [super init];
  54     if (self == nil) return self;
  55 
  56     self.javaLayer = layer;
  57 
  58     // NOTE: async=YES means that the layer is re-cached periodically
  59     self.asynchronous = FALSE;
  60     self.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
  61     self.contentsGravity = kCAGravityTopLeft;
  62     self.needsDisplayOnBoundsChange = YES;
  63     textureID = 0; // texture will be created by rendering pipe
  64     target = 0;
  65 
  66     return self;
  67 }
  68 
  69 - (void) dealloc {
  70     self.javaLayer = nil;
  71     [super dealloc];
  72 }
  73 
  74 - (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask {
  75     return CGLRetainPixelFormat(sharedPixelFormat.CGLPixelFormatObj);
  76 }
  77 
  78 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat {
  79     CGLContextObj contextObj = NULL;
  80     CGLCreateContext(pixelFormat, sharedContext.CGLContextObj, &contextObj);
  81     return contextObj;
  82 }
  83 
  84 // use texture (intermediate buffer) as src and blit it to the layer
  85 - (void) blitTexture
  86 {
  87     if (textureID == 0) {
  88         return;
  89     }
  90 
  91     glEnable(target);
  92     glBindTexture(target, textureID);
  93 
  94     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // srccopy
  95 
  96     float swid = 1.0f, shgt = 1.0f;
  97     if (target == GL_TEXTURE_RECTANGLE_ARB) {
  98         swid = textureWidth;
  99         shgt = textureHeight;
 100     }
 101     glBegin(GL_QUADS);
 102     glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
 103     glTexCoord2f(swid, 0.0f); glVertex2f( 1.0f, -1.0f);
 104     glTexCoord2f(swid, shgt); glVertex2f( 1.0f,  1.0f);
 105     glTexCoord2f(0.0f, shgt); glVertex2f(-1.0f,  1.0f);
 106     glEnd();
 107 
 108     glBindTexture(target, 0);
 109     glDisable(target);
 110 }
 111 
 112 -(void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
 113 {
 114     AWT_ASSERT_APPKIT_THREAD;
 115 
 116     // Set the current context to the one given to us.
 117     CGLSetCurrentContext(glContext);
 118 
 119     glViewport(0, 0, textureWidth, textureHeight);
 120 
 121     JNIEnv *env = [ThreadUtilities getJNIEnv];
 122     static JNF_CLASS_CACHE(jc_JavaLayer, "sun/java2d/opengl/CGLLayer");
 123     static JNF_MEMBER_CACHE(jm_drawInCGLContext, jc_JavaLayer, "drawInCGLContext", "()V");
 124 
 125     jobject javaLayerLocalRef = [self.javaLayer jObjectWithEnv:env];
 126     JNFCallVoidMethod(env, javaLayerLocalRef, jm_drawInCGLContext);
 127     (*env)->DeleteLocalRef(env, javaLayerLocalRef);
 128 
 129     // Call super to finalize the drawing. By default all it does is call glFlush().
 130     [super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:timeInterval displayTime:timeStamp];
 131 
 132     CGLSetCurrentContext(NULL);
 133 }
 134 
 135 @end
 136 
 137 /*
 138  * Class:     sun_java2d_opengl_CGLLayer
 139  * Method:    nativeCreateLayer
 140  * Signature: ()J
 141  */
 142 JNIEXPORT jlong JNICALL
 143 Java_sun_java2d_opengl_CGLLayer_nativeCreateLayer
 144 (JNIEnv *env, jobject obj)
 145 {
 146     __block CGLLayer *layer = nil;
 147 
 148 JNF_COCOA_ENTER(env);
 149 AWT_ASSERT_NOT_APPKIT_THREAD;
 150 
 151     JNFJObjectWrapper *javaLayer = [JNFJObjectWrapper wrapperWithJObject:obj withEnv:env];
 152 
 153     [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){
 154         AWT_ASSERT_APPKIT_THREAD;
 155 
 156         layer = [[CGLLayer alloc] initWithJavaLayer: javaLayer];
 157     }];
 158 
 159 JNF_COCOA_EXIT(env);
 160 
 161     return ptr_to_jlong(layer);
 162 }
 163 
 164 // Must be called under the RQ lock.
 165 JNIEXPORT void JNICALL
 166 Java_sun_java2d_opengl_CGLLayer_validate
 167 (JNIEnv *env, jobject obj, jlong layerPtr, jobject surfaceData)
 168 {
 169     CGLLayer *layer = OBJC(layerPtr);
 170 
 171     if (surfaceData != NULL) {
 172         OGLSDOps *oglsdo = (OGLSDOps*) SurfaceData_GetOps(env, surfaceData);
 173         layer.textureID = oglsdo->textureID;
 174         layer.target = GL_TEXTURE_2D;
 175         layer.textureWidth = oglsdo->width;
 176         layer.textureHeight = oglsdo->height;
 177     } else {
 178         layer.textureID = 0;
 179     }
 180 }
 181 
 182 // Must be called on the AppKit thread and under the RQ lock.
 183 JNIEXPORT void JNICALL
 184 Java_sun_java2d_opengl_CGLLayer_blitTexture
 185 (JNIEnv *env, jobject obj, jlong layerPtr)
 186 {
 187     CGLLayer *layer = jlong_to_ptr(layerPtr);
 188 
 189     [layer blitTexture];
 190 }