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