1/*
2 * Copyright (c) 2011, 2016, 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
33extern NSOpenGLPixelFormat *sharedPixelFormat;
34extern 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:(JNFWeakJObjectWrapper *)layer;
50{
51AWT_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], @"anchorPoint",
68                                    [NSNull null], @"bounds",
69                                    [NSNull null], @"contents",
70                                    [NSNull null], @"contentsScale",
71                                    [NSNull null], @"onOrderIn",
72                                    [NSNull null], @"onOrderOut",
73                                    [NSNull null], @"position",
74                                    [NSNull null], @"sublayers",
75                                    nil];
76    self.actions = actions;
77    [actions release];
78
79    textureID = 0; // texture will be created by rendering pipe
80    target = 0;
81
82    return self;
83}
84
85- (void) dealloc {
86    self.javaLayer = nil;
87    [super dealloc];
88}
89
90- (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask {
91    return CGLRetainPixelFormat(sharedPixelFormat.CGLPixelFormatObj);
92}
93
94- (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat {
95    CGLContextObj contextObj = NULL;
96    CGLCreateContext(pixelFormat, sharedContext.CGLContextObj, &contextObj);
97    return contextObj;
98}
99
100// use texture (intermediate buffer) as src and blit it to the layer
101- (void) blitTexture
102{
103    if (textureID == 0) {
104        return;
105    }
106
107    glEnable(target);
108    glBindTexture(target, textureID);
109
110    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // srccopy
111
112    float swid = 1.0f, shgt = 1.0f;
113    if (target == GL_TEXTURE_RECTANGLE_ARB) {
114        swid = textureWidth;
115        shgt = textureHeight;
116    }
117    glBegin(GL_QUADS);
118    glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
119    glTexCoord2f(swid, 0.0f); glVertex2f( 1.0f, -1.0f);
120    glTexCoord2f(swid, shgt); glVertex2f( 1.0f,  1.0f);
121    glTexCoord2f(0.0f, shgt); glVertex2f(-1.0f,  1.0f);
122    glEnd();
123
124    glBindTexture(target, 0);
125    glDisable(target);
126}
127
128-(BOOL)canDrawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp{
129    return textureID == 0 ? NO : YES;
130}
131
132-(void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
133{
134    AWT_ASSERT_APPKIT_THREAD;
135
136    JNIEnv *env = [ThreadUtilities getJNIEnv];
137    static JNF_CLASS_CACHE(jc_JavaLayer, "sun/java2d/opengl/CGLLayer");
138    static JNF_MEMBER_CACHE(jm_drawInCGLContext, jc_JavaLayer, "drawInCGLContext", "()V");
139
140    jobject javaLayerLocalRef = [self.javaLayer jObjectWithEnv:env];
141    if ((*env)->IsSameObject(env, javaLayerLocalRef, NULL)) {
142        return;
143    }
144
145    // Set the current context to the one given to us.
146    CGLSetCurrentContext(glContext);
147
148    // Should clear the whole CALayer, because it can be larger than our texture.
149    glClearColor(0.0, 0.0, 0.0, 0.0);
150    glClear(GL_COLOR_BUFFER_BIT);
151
152    glViewport(0, 0, textureWidth, textureHeight);
153
154    JNFCallVoidMethod(env, javaLayerLocalRef, jm_drawInCGLContext);
155    (*env)->DeleteLocalRef(env, javaLayerLocalRef);
156
157    // Call super to finalize the drawing. By default all it does is call glFlush().
158    [super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:timeInterval displayTime:timeStamp];
159
160    CGLSetCurrentContext(NULL);
161}
162
163@end
164
165/*
166 * Class:     sun_java2d_opengl_CGLLayer
167 * Method:    nativeCreateLayer
168 * Signature: ()J
169 */
170JNIEXPORT jlong JNICALL
171Java_sun_java2d_opengl_CGLLayer_nativeCreateLayer
172(JNIEnv *env, jobject obj)
173{
174    __block CGLLayer *layer = nil;
175
176JNF_COCOA_ENTER(env);
177
178    JNFWeakJObjectWrapper *javaLayer = [JNFWeakJObjectWrapper wrapperWithJObject:obj withEnv:env];
179
180    [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
181            AWT_ASSERT_APPKIT_THREAD;
182        
183            layer = [[CGLLayer alloc] initWithJavaLayer: javaLayer];
184    }];
185    
186JNF_COCOA_EXIT(env);
187
188    return ptr_to_jlong(layer);
189}
190
191// Must be called under the RQ lock.
192JNIEXPORT void JNICALL
193Java_sun_java2d_opengl_CGLLayer_validate
194(JNIEnv *env, jclass cls, jlong layerPtr, jobject surfaceData)
195{
196    CGLLayer *layer = OBJC(layerPtr);
197
198    if (surfaceData != NULL) {
199        OGLSDOps *oglsdo = (OGLSDOps*) SurfaceData_GetOps(env, surfaceData);
200        layer.textureID = oglsdo->textureID;
201        layer.target = GL_TEXTURE_2D;
202        layer.textureWidth = oglsdo->width;
203        layer.textureHeight = oglsdo->height;
204    } else {
205        layer.textureID = 0;
206    }
207}
208
209// Must be called on the AppKit thread and under the RQ lock.
210JNIEXPORT void JNICALL
211Java_sun_java2d_opengl_CGLLayer_blitTexture
212(JNIEnv *env, jclass cls, jlong layerPtr)
213{
214    CGLLayer *layer = jlong_to_ptr(layerPtr);
215
216    [layer blitTexture];
217}
218
219JNIEXPORT void JNICALL
220Java_sun_java2d_opengl_CGLLayer_nativeSetScale
221(JNIEnv *env, jclass cls, jlong layerPtr, jdouble scale)
222{
223    JNF_COCOA_ENTER(env);
224    CGLLayer *layer = jlong_to_ptr(layerPtr);
225    // We always call all setXX methods asynchronously, exception is only in 
226    // this method where we need to change native texture size and layer's scale
227    // in one call on appkit, otherwise we'll get window's contents blinking, 
228    // during screen-2-screen moving.
229    [ThreadUtilities performOnMainThreadWaiting:[NSThread isMainThread] block:^(){
230        layer.contentsScale = scale;
231    }];
232    JNF_COCOA_EXIT(env);
233}
234