4632N/A/*
4632N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/A#import "CGLGraphicsConfig.h"
4632N/A#import "CGLLayer.h"
4632N/A#import "ThreadUtilities.h"
4639N/A#import "LWCToolkit.h"
4639N/A#import "CGLSurfaceData.h"
4639N/A
4632N/A
4632N/Aextern NSOpenGLPixelFormat *sharedPixelFormat;
4632N/Aextern NSOpenGLContext *sharedContext;
4632N/A
4632N/A@implementation CGLLayer
4632N/A
4721N/A@synthesize javaLayer;
4632N/A@synthesize textureID;
4632N/A@synthesize target;
4632N/A@synthesize textureWidth;
4632N/A@synthesize textureHeight;
4632N/A#ifdef REMOTELAYER
4632N/A@synthesize parentLayer;
4632N/A@synthesize remoteLayer;
4632N/A@synthesize jrsRemoteLayer;
4632N/A#endif
4632N/A
4721N/A- (id) initWithJavaLayer:(JNFJObjectWrapper *)layer;
4632N/A{
4632N/AAWT_ASSERT_APPKIT_THREAD;
4632N/A // Initialize ourselves
4632N/A self = [super init];
4632N/A if (self == nil) return self;
4632N/A
4721N/A self.javaLayer = layer;
4730N/A
4632N/A // NOTE: async=YES means that the layer is re-cached periodically
4632N/A self.asynchronous = FALSE;
4632N/A self.contentsGravity = kCAGravityTopLeft;
6056N/A //Layer backed view
6056N/A //self.needsDisplayOnBoundsChange = YES;
6056N/A //self.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
6084N/A
6084N/A //Disable CALayer's default animation
6084N/A NSMutableDictionary * actions = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
6084N/A [NSNull null], @"bounds",
6084N/A [NSNull null], @"contents",
6084N/A [NSNull null], @"contentsScale",
6084N/A [NSNull null], @"onOrderIn",
6084N/A [NSNull null], @"onOrderOut",
6084N/A [NSNull null], @"sublayers",
6084N/A nil];
6084N/A self.actions = actions;
6084N/A [actions release];
6084N/A
4632N/A textureID = 0; // texture will be created by rendering pipe
4632N/A target = 0;
4632N/A
4632N/A return self;
4632N/A}
4632N/A
4730N/A- (void) dealloc {
4721N/A self.javaLayer = nil;
4639N/A [super dealloc];
4639N/A}
4639N/A
4632N/A- (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask {
4632N/A return CGLRetainPixelFormat(sharedPixelFormat.CGLPixelFormatObj);
4632N/A}
4632N/A
4632N/A- (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat {
4632N/A CGLContextObj contextObj = NULL;
4632N/A CGLCreateContext(pixelFormat, sharedContext.CGLContextObj, &contextObj);
4632N/A return contextObj;
4632N/A}
4632N/A
4632N/A// use texture (intermediate buffer) as src and blit it to the layer
4639N/A- (void) blitTexture
4632N/A{
4632N/A if (textureID == 0) {
4632N/A return;
4632N/A }
4632N/A
4632N/A glEnable(target);
4632N/A glBindTexture(target, textureID);
4632N/A
4632N/A glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // srccopy
4632N/A
4632N/A float swid = 1.0f, shgt = 1.0f;
4632N/A if (target == GL_TEXTURE_RECTANGLE_ARB) {
4632N/A swid = textureWidth;
4632N/A shgt = textureHeight;
4632N/A }
4632N/A glBegin(GL_QUADS);
4632N/A glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
4632N/A glTexCoord2f(swid, 0.0f); glVertex2f( 1.0f, -1.0f);
4632N/A glTexCoord2f(swid, shgt); glVertex2f( 1.0f, 1.0f);
4632N/A glTexCoord2f(0.0f, shgt); glVertex2f(-1.0f, 1.0f);
4632N/A glEnd();
4632N/A
4632N/A glBindTexture(target, 0);
4632N/A glDisable(target);
4632N/A}
4632N/A
6056N/A-(BOOL)canDrawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp{
6056N/A return textureID == 0 ? NO : YES;
6056N/A}
6056N/A
4632N/A-(void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
4632N/A{
4632N/A AWT_ASSERT_APPKIT_THREAD;
4730N/A
4632N/A // Set the current context to the one given to us.
4632N/A CGLSetCurrentContext(glContext);
4632N/A
6084N/A // Should clear the whole CALayer, because it can be larger than our texture.
6084N/A glClearColor(0.0, 0.0, 0.0, 0.0);
6084N/A glClear(GL_COLOR_BUFFER_BIT);
6084N/A
4632N/A glViewport(0, 0, textureWidth, textureHeight);
6084N/A
4639N/A JNIEnv *env = [ThreadUtilities getJNIEnv];
4639N/A static JNF_CLASS_CACHE(jc_JavaLayer, "sun/java2d/opengl/CGLLayer");
4639N/A static JNF_MEMBER_CACHE(jm_drawInCGLContext, jc_JavaLayer, "drawInCGLContext", "()V");
4721N/A
4721N/A jobject javaLayerLocalRef = [self.javaLayer jObjectWithEnv:env];
4721N/A JNFCallVoidMethod(env, javaLayerLocalRef, jm_drawInCGLContext);
4721N/A (*env)->DeleteLocalRef(env, javaLayerLocalRef);
4632N/A
4632N/A // Call super to finalize the drawing. By default all it does is call glFlush().
4632N/A [super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:timeInterval displayTime:timeStamp];
4632N/A
4632N/A CGLSetCurrentContext(NULL);
4632N/A}
4632N/A
4632N/A@end
4632N/A
4632N/A/*
4632N/A * Class: sun_java2d_opengl_CGLLayer
4632N/A * Method: nativeCreateLayer
4632N/A * Signature: ()J
4632N/A */
4632N/AJNIEXPORT jlong JNICALL
4632N/AJava_sun_java2d_opengl_CGLLayer_nativeCreateLayer
4632N/A(JNIEnv *env, jobject obj)
4632N/A{
4632N/A __block CGLLayer *layer = nil;
4632N/A
4632N/AJNF_COCOA_ENTER(env);
4632N/A
4721N/A JNFJObjectWrapper *javaLayer = [JNFJObjectWrapper wrapperWithJObject:obj withEnv:env];
4730N/A
5555N/A [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
5555N/A AWT_ASSERT_APPKIT_THREAD;
5555N/A
5555N/A layer = [[CGLLayer alloc] initWithJavaLayer: javaLayer];
4632N/A }];
5555N/A
4632N/AJNF_COCOA_EXIT(env);
4632N/A
4632N/A return ptr_to_jlong(layer);
4632N/A}
4639N/A
4639N/A// Must be called under the RQ lock.
4639N/AJNIEXPORT void JNICALL
4639N/AJava_sun_java2d_opengl_CGLLayer_validate
6084N/A(JNIEnv *env, jclass cls, jlong layerPtr, jobject surfaceData)
4639N/A{
4639N/A CGLLayer *layer = OBJC(layerPtr);
4639N/A
4639N/A if (surfaceData != NULL) {
4639N/A OGLSDOps *oglsdo = (OGLSDOps*) SurfaceData_GetOps(env, surfaceData);
4639N/A layer.textureID = oglsdo->textureID;
4639N/A layer.target = GL_TEXTURE_2D;
4639N/A layer.textureWidth = oglsdo->width;
4639N/A layer.textureHeight = oglsdo->height;
4639N/A } else {
4730N/A layer.textureID = 0;
4639N/A }
4730N/A}
4639N/A
4639N/A// Must be called on the AppKit thread and under the RQ lock.
4639N/AJNIEXPORT void JNICALL
4639N/AJava_sun_java2d_opengl_CGLLayer_blitTexture
6084N/A(JNIEnv *env, jclass cls, jlong layerPtr)
4639N/A{
4639N/A CGLLayer *layer = jlong_to_ptr(layerPtr);
4639N/A
4639N/A [layer blitTexture];
4639N/A}
6084N/A
6084N/AJNIEXPORT void JNICALL
6084N/AJava_sun_java2d_opengl_CGLLayer_nativeSetScale
6084N/A(JNIEnv *env, jclass cls, jlong layerPtr, jdouble scale)
6084N/A{
6084N/A JNF_COCOA_ENTER(env);
6084N/A CGLLayer *layer = jlong_to_ptr(layerPtr);
6093N/A // We always call all setXX methods asynchronously, exception is only in
6093N/A // this method where we need to change native texture size and layer's scale
6093N/A // in one call on appkit, otherwise we'll get window's contents blinking,
6093N/A // during screen-2-screen moving.
6093N/A [ThreadUtilities performOnMainThreadWaiting:[NSThread isMainThread] block:^(){
6084N/A layer.contentsScale = scale;
6084N/A }];
6084N/A JNF_COCOA_EXIT(env);
6084N/A}