Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/native_NOTIOS/sun/java2d/opengl/CGLLayer.m
38829 views
/*1* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#import "CGLGraphicsConfig.h"26#import "CGLLayer.h"27#import "ThreadUtilities.h"28#import "LWCToolkit.h"29#import "CGLSurfaceData.h"303132extern NSOpenGLPixelFormat *sharedPixelFormat;33extern NSOpenGLContext *sharedContext;3435@implementation CGLLayer3637@synthesize javaLayer;38@synthesize textureID;39@synthesize target;40@synthesize textureWidth;41@synthesize textureHeight;42#ifdef REMOTELAYER43@synthesize parentLayer;44@synthesize remoteLayer;45@synthesize jrsRemoteLayer;46#endif4748- (id) initWithJavaLayer:(JNFWeakJObjectWrapper *)layer;49{50AWT_ASSERT_APPKIT_THREAD;51// Initialize ourselves52self = [super init];53if (self == nil) return self;5455self.javaLayer = layer;5657// NOTE: async=YES means that the layer is re-cached periodically58self.asynchronous = FALSE;59self.contentsGravity = kCAGravityTopLeft;60//Layer backed view61//self.needsDisplayOnBoundsChange = YES;62//self.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;6364//Disable CALayer's default animation65NSMutableDictionary * actions = [[NSMutableDictionary alloc] initWithObjectsAndKeys:66[NSNull null], @"anchorPoint",67[NSNull null], @"bounds",68[NSNull null], @"contents",69[NSNull null], @"contentsScale",70[NSNull null], @"onOrderIn",71[NSNull null], @"onOrderOut",72[NSNull null], @"position",73[NSNull null], @"sublayers",74nil];75self.actions = actions;76[actions release];7778textureID = 0; // texture will be created by rendering pipe79target = 0;8081return self;82}8384- (void) dealloc {85self.javaLayer = nil;86[super dealloc];87}8889- (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask {90return CGLRetainPixelFormat(sharedPixelFormat.CGLPixelFormatObj);91}9293- (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat {94CGLContextObj contextObj = NULL;95CGLCreateContext(pixelFormat, sharedContext.CGLContextObj, &contextObj);96return contextObj;97}9899// use texture (intermediate buffer) as src and blit it to the layer100- (void) blitTexture101{102if (textureID == 0) {103return;104}105106glEnable(target);107glBindTexture(target, textureID);108109glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // srccopy110111float swid = 1.0f, shgt = 1.0f;112if (target == GL_TEXTURE_RECTANGLE_ARB) {113swid = textureWidth;114shgt = textureHeight;115}116glBegin(GL_QUADS);117glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);118glTexCoord2f(swid, 0.0f); glVertex2f( 1.0f, -1.0f);119glTexCoord2f(swid, shgt); glVertex2f( 1.0f, 1.0f);120glTexCoord2f(0.0f, shgt); glVertex2f(-1.0f, 1.0f);121glEnd();122123glBindTexture(target, 0);124glDisable(target);125}126127-(BOOL)canDrawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp{128return textureID == 0 ? NO : YES;129}130131-(void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp132{133AWT_ASSERT_APPKIT_THREAD;134135JNIEnv *env = [ThreadUtilities getJNIEnv];136static JNF_CLASS_CACHE(jc_JavaLayer, "sun/java2d/opengl/CGLLayer");137static JNF_MEMBER_CACHE(jm_drawInCGLContext, jc_JavaLayer, "drawInCGLContext", "()V");138139jobject javaLayerLocalRef = [self.javaLayer jObjectWithEnv:env];140if ((*env)->IsSameObject(env, javaLayerLocalRef, NULL)) {141return;142}143144// Set the current context to the one given to us.145CGLSetCurrentContext(glContext);146147// Should clear the whole CALayer, because it can be larger than our texture.148glClearColor(0.0, 0.0, 0.0, 0.0);149glClear(GL_COLOR_BUFFER_BIT);150151glViewport(0, 0, textureWidth, textureHeight);152153JNFCallVoidMethod(env, javaLayerLocalRef, jm_drawInCGLContext);154(*env)->DeleteLocalRef(env, javaLayerLocalRef);155156// Call super to finalize the drawing. By default all it does is call glFlush().157[super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:timeInterval displayTime:timeStamp];158159CGLSetCurrentContext(NULL);160}161162@end163164/*165* Class: sun_java2d_opengl_CGLLayer166* Method: nativeCreateLayer167* Signature: ()J168*/169JNIEXPORT jlong JNICALL170Java_sun_java2d_opengl_CGLLayer_nativeCreateLayer171(JNIEnv *env, jobject obj)172{173__block CGLLayer *layer = nil;174175JNF_COCOA_ENTER(env);176177JNFWeakJObjectWrapper *javaLayer = [JNFWeakJObjectWrapper wrapperWithJObject:obj withEnv:env];178179[ThreadUtilities performOnMainThreadWaiting:YES block:^(){180AWT_ASSERT_APPKIT_THREAD;181182layer = [[CGLLayer alloc] initWithJavaLayer: javaLayer];183}];184185JNF_COCOA_EXIT(env);186187return ptr_to_jlong(layer);188}189190// Must be called under the RQ lock.191JNIEXPORT void JNICALL192Java_sun_java2d_opengl_CGLLayer_validate193(JNIEnv *env, jclass cls, jlong layerPtr, jobject surfaceData)194{195CGLLayer *layer = OBJC(layerPtr);196197if (surfaceData != NULL) {198OGLSDOps *oglsdo = (OGLSDOps*) SurfaceData_GetOps(env, surfaceData);199layer.textureID = oglsdo->textureID;200layer.target = GL_TEXTURE_2D;201layer.textureWidth = oglsdo->width;202layer.textureHeight = oglsdo->height;203} else {204layer.textureID = 0;205}206}207208// Must be called on the AppKit thread and under the RQ lock.209JNIEXPORT void JNICALL210Java_sun_java2d_opengl_CGLLayer_blitTexture211(JNIEnv *env, jclass cls, jlong layerPtr)212{213CGLLayer *layer = jlong_to_ptr(layerPtr);214215[layer blitTexture];216}217218JNIEXPORT void JNICALL219Java_sun_java2d_opengl_CGLLayer_nativeSetScale220(JNIEnv *env, jclass cls, jlong layerPtr, jdouble scale)221{222JNF_COCOA_ENTER(env);223CGLLayer *layer = jlong_to_ptr(layerPtr);224// We always call all setXX methods asynchronously, exception is only in225// this method where we need to change native texture size and layer's scale226// 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:^(){229layer.contentsScale = scale;230}];231JNF_COCOA_EXIT(env);232}233234235