1 /*
   2  * Copyright (c) 2011, 2016, 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 #import "JNIUtilities.h"
  32 
  33 
  34 extern NSOpenGLPixelFormat *sharedPixelFormat;
  35 extern NSOpenGLContext *sharedContext;
  36 
  37 @implementation CGLLayer
  38 
  39 @synthesize javaLayer;
  40 @synthesize textureID;
  41 @synthesize target;
  42 @synthesize textureWidth;
  43 @synthesize textureHeight;
  44 #ifdef REMOTELAYER
  45 @synthesize parentLayer;
  46 @synthesize remoteLayer;
  47 @synthesize jrsRemoteLayer;
  48 #endif
  49 
  50 - (id) initWithJavaLayer:(jobject)layer;
  51 {
  52 AWT_ASSERT_APPKIT_THREAD;
  53     // Initialize ourselves
  54     self = [super init];
  55     if (self == nil) return self;
  56 
  57     self.javaLayer = layer;
  58 
  59     // NOTE: async=YES means that the layer is re-cached periodically
  60     self.asynchronous = FALSE;
  61     self.contentsGravity = kCAGravityTopLeft;
  62     //Layer backed view
  63     //self.needsDisplayOnBoundsChange = YES;
  64     //self.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
  65 
  66     //Disable CALayer's default animation
  67     NSMutableDictionary * actions = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
  68                                     [NSNull null], @"anchorPoint",
  69                                     [NSNull null], @"bounds",
  70                                     [NSNull null], @"contents",
  71                                     [NSNull null], @"contentsScale",
  72                                     [NSNull null], @"onOrderIn",
  73                                     [NSNull null], @"onOrderOut",
  74                                     [NSNull null], @"position",
  75                                     [NSNull null], @"sublayers",
  76                                     nil];
  77     self.actions = actions;
  78     [actions release];
  79 
  80     textureID = 0; // texture will be created by rendering pipe
  81     target = 0;
  82 
  83     return self;
  84 }
  85 
  86 - (void) dealloc {
  87     JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
  88     (*env)->DeleteWeakGlobalRef(env, self.javaLayer);
  89     self.javaLayer = nil;
  90     [super dealloc];
  91 }
  92 
  93 - (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask {
  94     return CGLRetainPixelFormat(sharedPixelFormat.CGLPixelFormatObj);
  95 }
  96 
  97 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat {
  98     CGLContextObj contextObj = NULL;
  99     CGLCreateContext(pixelFormat, sharedContext.CGLContextObj, &contextObj);
 100     return contextObj;
 101 }
 102 
 103 // use texture (intermediate buffer) as src and blit it to the layer
 104 - (void) blitTexture
 105 {
 106     if (textureID == 0) {
 107         return;
 108     }
 109 
 110     glEnable(target);
 111     glBindTexture(target, textureID);
 112 
 113     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // srccopy
 114 
 115     float swid = 1.0f, shgt = 1.0f;
 116     if (target == GL_TEXTURE_RECTANGLE_ARB) {
 117         swid = textureWidth;
 118         shgt = textureHeight;
 119     }
 120     glBegin(GL_QUADS);
 121     glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
 122     glTexCoord2f(swid, 0.0f); glVertex2f( 1.0f, -1.0f);
 123     glTexCoord2f(swid, shgt); glVertex2f( 1.0f,  1.0f);
 124     glTexCoord2f(0.0f, shgt); glVertex2f(-1.0f,  1.0f);
 125     glEnd();
 126 
 127     glBindTexture(target, 0);
 128     glDisable(target);
 129 }
 130 
 131 -(BOOL)canDrawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp{
 132     return textureID == 0 ? NO : YES;
 133 }
 134 
 135 -(void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
 136 {
 137     AWT_ASSERT_APPKIT_THREAD;
 138 
 139     JNIEnv *env = [ThreadUtilities getJNIEnv];
 140     DECLARE_CLASS(jc_JavaLayer, "sun/java2d/opengl/CGLLayer");
 141     DECLARE_METHOD(jm_drawInCGLContext, jc_JavaLayer, "drawInCGLContext", "()V");
 142 
 143     jobject javaLayerLocalRef = (*env)->NewLocalRef(env, self.javaLayer);
 144     if ((*env)->IsSameObject(env, javaLayerLocalRef, NULL)) {
 145         return;
 146     }
 147 
 148     // Set the current context to the one given to us.
 149     CGLSetCurrentContext(glContext);
 150 
 151     // Should clear the whole CALayer, because it can be larger than our texture.
 152     glClearColor(0.0, 0.0, 0.0, 0.0);
 153     glClear(GL_COLOR_BUFFER_BIT);
 154 
 155     glViewport(0, 0, textureWidth, textureHeight);
 156 
 157     (*env)->CallVoidMethod(env, javaLayerLocalRef, jm_drawInCGLContext);
 158     CHECK_EXCEPTION();
 159     (*env)->DeleteLocalRef(env, javaLayerLocalRef);
 160 
 161     // Call super to finalize the drawing. By default all it does is call glFlush().
 162     [super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:timeInterval displayTime:timeStamp];
 163 
 164     CGLSetCurrentContext(NULL);
 165 }
 166 
 167 @end
 168 
 169 /*
 170  * Class:     sun_java2d_opengl_CGLLayer
 171  * Method:    nativeCreateLayer
 172  * Signature: ()J
 173  */
 174 JNIEXPORT jlong JNICALL
 175 Java_sun_java2d_opengl_CGLLayer_nativeCreateLayer
 176 (JNIEnv *env, jobject obj)
 177 {
 178     __block CGLLayer *layer = nil;
 179 
 180 JNI_COCOA_ENTER(env);
 181 
 182     jobject javaLayer = (*env)->NewWeakGlobalRef(env, obj);
 183 
 184     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
 185             AWT_ASSERT_APPKIT_THREAD;
 186         
 187             layer = [[CGLLayer alloc] initWithJavaLayer: javaLayer];
 188     }];
 189     
 190 JNI_COCOA_EXIT(env);
 191 
 192     return ptr_to_jlong(layer);
 193 }
 194 
 195 // Must be called under the RQ lock.
 196 JNIEXPORT void JNICALL
 197 Java_sun_java2d_opengl_CGLLayer_validate
 198 (JNIEnv *env, jclass cls, jlong layerPtr, jobject surfaceData)
 199 {
 200     CGLLayer *layer = OBJC(layerPtr);
 201 
 202     if (surfaceData != NULL) {
 203         OGLSDOps *oglsdo = (OGLSDOps*) SurfaceData_GetOps(env, surfaceData);
 204         layer.textureID = oglsdo->textureID;
 205         layer.target = GL_TEXTURE_2D;
 206         layer.textureWidth = oglsdo->width;
 207         layer.textureHeight = oglsdo->height;
 208     } else {
 209         layer.textureID = 0;
 210     }
 211 }
 212 
 213 // Must be called on the AppKit thread and under the RQ lock.
 214 JNIEXPORT void JNICALL
 215 Java_sun_java2d_opengl_CGLLayer_blitTexture
 216 (JNIEnv *env, jclass cls, jlong layerPtr)
 217 {
 218     CGLLayer *layer = jlong_to_ptr(layerPtr);
 219 
 220     [layer blitTexture];
 221 }
 222 
 223 JNIEXPORT void JNICALL
 224 Java_sun_java2d_opengl_CGLLayer_nativeSetScale
 225 (JNIEnv *env, jclass cls, jlong layerPtr, jdouble scale)
 226 {
 227     JNI_COCOA_ENTER(env);
 228     CGLLayer *layer = jlong_to_ptr(layerPtr);
 229     // We always call all setXX methods asynchronously, exception is only in 
 230     // this method where we need to change native texture size and layer's scale
 231     // in one call on appkit, otherwise we'll get window's contents blinking, 
 232     // during screen-2-screen moving.
 233     [ThreadUtilities performOnMainThreadWaiting:[NSThread isMainThread] block:^(){
 234         layer.contentsScale = scale;
 235     }];
 236     JNI_COCOA_EXIT(env);
 237 }