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