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