1 /*
   2  * Copyright (c) 2011, 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 
  33 extern NSOpenGLPixelFormat *sharedPixelFormat;
  34 extern 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 {
  51 AWT_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], @"bounds",
  68                                     [NSNull null], @"contents",
  69                                     [NSNull null], @"contentsScale",
  70                                     [NSNull null], @"onOrderIn",
  71                                     [NSNull null], @"onOrderOut",
  72                                     [NSNull null], @"sublayers",
  73                                     nil];
  74     self.actions = actions;
  75     [actions release];
  76 
  77     textureID = 0; // texture will be created by rendering pipe
  78     target = 0;
  79 
  80     return self;
  81 }
  82 
  83 - (void) dealloc {
  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     // Set the current context to the one given to us.
 135     CGLSetCurrentContext(glContext);
 136 
 137     // Should clear the whole CALayer, because it can be larger than our texture.
 138     glClearColor(0.0, 0.0, 0.0, 0.0);
 139     glClear(GL_COLOR_BUFFER_BIT);
 140 
 141     glViewport(0, 0, textureWidth, textureHeight);
 142     
 143     JNIEnv *env = [ThreadUtilities getJNIEnv];
 144     static JNF_CLASS_CACHE(jc_JavaLayer, "sun/java2d/opengl/CGLLayer");
 145     static JNF_MEMBER_CACHE(jm_drawInCGLContext, jc_JavaLayer, "drawInCGLContext", "()V");
 146 
 147     jobject javaLayerLocalRef = [self.javaLayer jObjectWithEnv:env];
 148     JNFCallVoidMethod(env, javaLayerLocalRef, jm_drawInCGLContext);
 149     (*env)->DeleteLocalRef(env, javaLayerLocalRef);
 150 
 151     // Call super to finalize the drawing. By default all it does is call glFlush().
 152     [super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:timeInterval displayTime:timeStamp];
 153 
 154     CGLSetCurrentContext(NULL);
 155 }
 156 
 157 @end
 158 
 159 /*
 160  * Class:     sun_java2d_opengl_CGLLayer
 161  * Method:    nativeCreateLayer
 162  * Signature: ()J
 163  */
 164 JNIEXPORT jlong JNICALL
 165 Java_sun_java2d_opengl_CGLLayer_nativeCreateLayer
 166 (JNIEnv *env, jobject obj)
 167 {
 168     __block CGLLayer *layer = nil;
 169 
 170 JNF_COCOA_ENTER(env);
 171 
 172     JNFJObjectWrapper *javaLayer = [JNFJObjectWrapper wrapperWithJObject:obj withEnv:env];
 173 
 174     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
 175             AWT_ASSERT_APPKIT_THREAD;
 176         
 177             layer = [[CGLLayer alloc] initWithJavaLayer: javaLayer];
 178     }];
 179     
 180 JNF_COCOA_EXIT(env);
 181 
 182     return ptr_to_jlong(layer);
 183 }
 184 
 185 // Must be called under the RQ lock.
 186 JNIEXPORT void JNICALL
 187 Java_sun_java2d_opengl_CGLLayer_validate
 188 (JNIEnv *env, jclass cls, jlong layerPtr, jobject surfaceData)
 189 {
 190     CGLLayer *layer = OBJC(layerPtr);
 191 
 192     if (surfaceData != NULL) {
 193         OGLSDOps *oglsdo = (OGLSDOps*) SurfaceData_GetOps(env, surfaceData);
 194         layer.textureID = oglsdo->textureID;
 195         layer.target = GL_TEXTURE_2D;
 196         layer.textureWidth = oglsdo->width;
 197         layer.textureHeight = oglsdo->height;
 198     } else {
 199         layer.textureID = 0;
 200     }
 201 }
 202 
 203 // Must be called on the AppKit thread and under the RQ lock.
 204 JNIEXPORT void JNICALL
 205 Java_sun_java2d_opengl_CGLLayer_blitTexture
 206 (JNIEnv *env, jclass cls, jlong layerPtr)
 207 {
 208     CGLLayer *layer = jlong_to_ptr(layerPtr);
 209 
 210     [layer blitTexture];
 211 }
 212 
 213 JNIEXPORT void JNICALL
 214 Java_sun_java2d_opengl_CGLLayer_nativeSetScale
 215 (JNIEnv *env, jclass cls, jlong layerPtr, jdouble scale)
 216 {
 217     JNF_COCOA_ENTER(env);
 218     CGLLayer *layer = jlong_to_ptr(layerPtr);
 219     [ThreadUtilities performOnMainThreadWaiting:[NSThread isMainThread] block:^(){
 220         layer.contentsScale = scale;
 221     }];
 222     JNF_COCOA_EXIT(env);
 223 }