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 textureID;
  39 @synthesize target;
  40 @synthesize textureWidth;
  41 @synthesize textureHeight;
  42 #ifdef REMOTELAYER
  43 @synthesize parentLayer;
  44 @synthesize remoteLayer;
  45 @synthesize jrsRemoteLayer;
  46 #endif
  47 
  48 - (id) initWithJavaLayer:(jobject)layer;
  49 {
  50 AWT_ASSERT_APPKIT_THREAD;
  51     // Initialize ourselves
  52     self = [super init];
  53     if (self == nil) return self;
  54 
  55     javaLayer = layer;
  56     
  57     // NOTE: async=YES means that the layer is re-cached periodically
  58     self.asynchronous = FALSE;
  59     self.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
  60     self.contentsGravity = kCAGravityTopLeft;
  61     self.needsDisplayOnBoundsChange = YES;
  62     textureID = 0; // texture will be created by rendering pipe
  63     target = 0;
  64 
  65     return self;
  66 }
  67 
  68 - (void) dealloc {
  69     JNIEnv *env = [ThreadUtilities getJNIEnv];
  70     (*env)->DeleteGlobalRef(env, javaLayer);
  71     javaLayer = NULL;
  72     
  73     [super dealloc];
  74 }
  75 
  76 - (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask {
  77     return CGLRetainPixelFormat(sharedPixelFormat.CGLPixelFormatObj);
  78 }
  79 
  80 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat {
  81     CGLContextObj contextObj = NULL;
  82     CGLCreateContext(pixelFormat, sharedContext.CGLContextObj, &contextObj);
  83     return contextObj;
  84 }
  85 
  86 // use texture (intermediate buffer) as src and blit it to the layer
  87 - (void) blitTexture
  88 {
  89     if (textureID == 0) {
  90         return;
  91     }
  92 
  93     glEnable(target);
  94     glBindTexture(target, textureID);
  95 
  96     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // srccopy
  97 
  98     float swid = 1.0f, shgt = 1.0f;
  99     if (target == GL_TEXTURE_RECTANGLE_ARB) {
 100         swid = textureWidth;
 101         shgt = textureHeight;
 102     }
 103     glBegin(GL_QUADS);
 104     glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
 105     glTexCoord2f(swid, 0.0f); glVertex2f( 1.0f, -1.0f);
 106     glTexCoord2f(swid, shgt); glVertex2f( 1.0f,  1.0f);
 107     glTexCoord2f(0.0f, shgt); glVertex2f(-1.0f,  1.0f);
 108     glEnd();
 109 
 110     glBindTexture(target, 0);
 111     glDisable(target);
 112 }
 113 
 114 -(void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
 115 {
 116     AWT_ASSERT_APPKIT_THREAD;
 117 
 118     // Set the current context to the one given to us.
 119     CGLSetCurrentContext(glContext);
 120 
 121     glViewport(0, 0, textureWidth, textureHeight);
 122 
 123     JNIEnv *env = [ThreadUtilities getJNIEnv];
 124     static JNF_CLASS_CACHE(jc_JavaLayer, "sun/java2d/opengl/CGLLayer");
 125     static JNF_MEMBER_CACHE(jm_drawInCGLContext, jc_JavaLayer, "drawInCGLContext", "()V");
 126     JNFCallVoidMethod(env, javaLayer, jm_drawInCGLContext);
 127 
 128     // Call super to finalize the drawing. By default all it does is call glFlush().
 129     [super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:timeInterval displayTime:timeStamp];
 130 
 131     CGLSetCurrentContext(NULL);
 132 }
 133 
 134 @end
 135 
 136 /*
 137  * Class:     sun_java2d_opengl_CGLLayer
 138  * Method:    nativeCreateLayer
 139  * Signature: ()J
 140  */
 141 JNIEXPORT jlong JNICALL
 142 Java_sun_java2d_opengl_CGLLayer_nativeCreateLayer
 143 (JNIEnv *env, jobject obj)
 144 {
 145     __block CGLLayer *layer = nil;
 146 
 147 JNF_COCOA_ENTER(env);
 148 AWT_ASSERT_NOT_APPKIT_THREAD;
 149 
 150     jobject javaLayer = (*env)->NewGlobalRef(env, obj);
 151     
 152     [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){
 153         AWT_ASSERT_APPKIT_THREAD;
 154 
 155         layer = [[CGLLayer alloc] initWithJavaLayer: javaLayer];
 156     }];
 157 
 158 JNF_COCOA_EXIT(env);
 159 
 160     return ptr_to_jlong(layer);
 161 }
 162 
 163 // Must be called under the RQ lock.
 164 JNIEXPORT void JNICALL
 165 Java_sun_java2d_opengl_CGLLayer_validate
 166 (JNIEnv *env, jobject obj, jlong layerPtr, jobject surfaceData)
 167 {
 168     CGLLayer *layer = OBJC(layerPtr);
 169 
 170     if (surfaceData != NULL) {
 171         OGLSDOps *oglsdo = (OGLSDOps*) SurfaceData_GetOps(env, surfaceData);
 172         layer.textureID = oglsdo->textureID;
 173         layer.target = GL_TEXTURE_2D;
 174         layer.textureWidth = oglsdo->width;
 175         layer.textureHeight = oglsdo->height;
 176     } else {
 177         layer.textureID = 0;            
 178     }
 179 }    
 180 
 181 // Must be called on the AppKit thread and under the RQ lock.
 182 JNIEXPORT void JNICALL
 183 Java_sun_java2d_opengl_CGLLayer_blitTexture
 184 (JNIEnv *env, jobject obj, jlong layerPtr)
 185 {
 186     CGLLayer *layer = jlong_to_ptr(layerPtr);
 187 
 188     [layer blitTexture];
 189 }