author | serb |
Tue, 27 Aug 2019 04:43:01 -0700 | |
changeset 58315 | 07556f8cd819 |
parent 54873 | 442e683e65fa |
permissions | -rw-r--r-- |
12047 | 1 |
/* |
54873
442e683e65fa
7141393: [macosx] CARemoteLayer code refactoring and unit test
serb
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved. |
12047 | 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 |
||
37787 | 44 |
- (id) initWithJavaLayer:(JNFWeakJObjectWrapper *)layer; |
12047 | 45 |
{ |
46 |
AWT_ASSERT_APPKIT_THREAD; |
|
47 |
// Initialize ourselves |
|
48 |
self = [super init]; |
|
49 |
if (self == nil) return self; |
|
50 |
||
51 |
self.javaLayer = layer; |
|
52 |
||
53 |
// NOTE: async=YES means that the layer is re-cached periodically |
|
54 |
self.asynchronous = FALSE; |
|
55 |
self.contentsGravity = kCAGravityTopLeft; |
|
14306
8d823e84507a
7198229: Painting during resizing of the frame should be more smooth
serb
parents:
12047
diff
changeset
|
56 |
//Layer backed view |
8d823e84507a
7198229: Painting during resizing of the frame should be more smooth
serb
parents:
12047
diff
changeset
|
57 |
//self.needsDisplayOnBoundsChange = YES; |
8d823e84507a
7198229: Painting during resizing of the frame should be more smooth
serb
parents:
12047
diff
changeset
|
58 |
//self.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable; |
16840
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
59 |
|
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
60 |
//Disable CALayer's default animation |
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
61 |
NSMutableDictionary * actions = [[NSMutableDictionary alloc] initWithObjectsAndKeys: |
17411
6a6136a94ebb
8013841: [macosx] Animations not disabled for CALayers used via JAWT
serb
parents:
17151
diff
changeset
|
62 |
[NSNull null], @"anchorPoint", |
16840
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
63 |
[NSNull null], @"bounds", |
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
64 |
[NSNull null], @"contents", |
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
65 |
[NSNull null], @"contentsScale", |
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
66 |
[NSNull null], @"onOrderIn", |
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
67 |
[NSNull null], @"onOrderOut", |
17411
6a6136a94ebb
8013841: [macosx] Animations not disabled for CALayers used via JAWT
serb
parents:
17151
diff
changeset
|
68 |
[NSNull null], @"position", |
16840
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
69 |
[NSNull null], @"sublayers", |
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
70 |
nil]; |
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
71 |
self.actions = actions; |
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
72 |
[actions release]; |
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
73 |
|
12047 | 74 |
textureID = 0; // texture will be created by rendering pipe |
75 |
target = 0; |
|
76 |
||
77 |
return self; |
|
78 |
} |
|
79 |
||
80 |
- (void) dealloc { |
|
81 |
self.javaLayer = nil; |
|
82 |
[super dealloc]; |
|
83 |
} |
|
84 |
||
85 |
- (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask { |
|
86 |
return CGLRetainPixelFormat(sharedPixelFormat.CGLPixelFormatObj); |
|
87 |
} |
|
88 |
||
89 |
- (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat { |
|
90 |
CGLContextObj contextObj = NULL; |
|
91 |
CGLCreateContext(pixelFormat, sharedContext.CGLContextObj, &contextObj); |
|
92 |
return contextObj; |
|
93 |
} |
|
94 |
||
95 |
// use texture (intermediate buffer) as src and blit it to the layer |
|
96 |
- (void) blitTexture |
|
97 |
{ |
|
98 |
if (textureID == 0) { |
|
99 |
return; |
|
100 |
} |
|
101 |
||
102 |
glEnable(target); |
|
103 |
glBindTexture(target, textureID); |
|
104 |
||
105 |
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // srccopy |
|
106 |
||
107 |
float swid = 1.0f, shgt = 1.0f; |
|
108 |
if (target == GL_TEXTURE_RECTANGLE_ARB) { |
|
109 |
swid = textureWidth; |
|
110 |
shgt = textureHeight; |
|
111 |
} |
|
112 |
glBegin(GL_QUADS); |
|
113 |
glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f); |
|
114 |
glTexCoord2f(swid, 0.0f); glVertex2f( 1.0f, -1.0f); |
|
115 |
glTexCoord2f(swid, shgt); glVertex2f( 1.0f, 1.0f); |
|
116 |
glTexCoord2f(0.0f, shgt); glVertex2f(-1.0f, 1.0f); |
|
117 |
glEnd(); |
|
118 |
||
119 |
glBindTexture(target, 0); |
|
120 |
glDisable(target); |
|
121 |
} |
|
122 |
||
14306
8d823e84507a
7198229: Painting during resizing of the frame should be more smooth
serb
parents:
12047
diff
changeset
|
123 |
-(BOOL)canDrawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp{ |
8d823e84507a
7198229: Painting during resizing of the frame should be more smooth
serb
parents:
12047
diff
changeset
|
124 |
return textureID == 0 ? NO : YES; |
8d823e84507a
7198229: Painting during resizing of the frame should be more smooth
serb
parents:
12047
diff
changeset
|
125 |
} |
8d823e84507a
7198229: Painting during resizing of the frame should be more smooth
serb
parents:
12047
diff
changeset
|
126 |
|
12047 | 127 |
-(void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp |
128 |
{ |
|
129 |
AWT_ASSERT_APPKIT_THREAD; |
|
130 |
||
37787 | 131 |
JNIEnv *env = [ThreadUtilities getJNIEnv]; |
132 |
static JNF_CLASS_CACHE(jc_JavaLayer, "sun/java2d/opengl/CGLLayer"); |
|
133 |
static JNF_MEMBER_CACHE(jm_drawInCGLContext, jc_JavaLayer, "drawInCGLContext", "()V"); |
|
134 |
||
135 |
jobject javaLayerLocalRef = [self.javaLayer jObjectWithEnv:env]; |
|
136 |
if ((*env)->IsSameObject(env, javaLayerLocalRef, NULL)) { |
|
137 |
return; |
|
138 |
} |
|
139 |
||
12047 | 140 |
// Set the current context to the one given to us. |
141 |
CGLSetCurrentContext(glContext); |
|
142 |
||
16840
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
143 |
// Should clear the whole CALayer, because it can be larger than our texture. |
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
144 |
glClearColor(0.0, 0.0, 0.0, 0.0); |
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
145 |
glClear(GL_COLOR_BUFFER_BIT); |
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
146 |
|
12047 | 147 |
glViewport(0, 0, textureWidth, textureHeight); |
148 |
||
149 |
JNFCallVoidMethod(env, javaLayerLocalRef, jm_drawInCGLContext); |
|
150 |
(*env)->DeleteLocalRef(env, javaLayerLocalRef); |
|
151 |
||
152 |
// Call super to finalize the drawing. By default all it does is call glFlush(). |
|
153 |
[super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:timeInterval displayTime:timeStamp]; |
|
154 |
||
155 |
CGLSetCurrentContext(NULL); |
|
156 |
} |
|
157 |
||
158 |
@end |
|
159 |
||
160 |
/* |
|
161 |
* Class: sun_java2d_opengl_CGLLayer |
|
162 |
* Method: nativeCreateLayer |
|
163 |
* Signature: ()J |
|
164 |
*/ |
|
165 |
JNIEXPORT jlong JNICALL |
|
166 |
Java_sun_java2d_opengl_CGLLayer_nativeCreateLayer |
|
167 |
(JNIEnv *env, jobject obj) |
|
168 |
{ |
|
169 |
__block CGLLayer *layer = nil; |
|
170 |
||
171 |
JNF_COCOA_ENTER(env); |
|
172 |
||
37787 | 173 |
JNFWeakJObjectWrapper *javaLayer = [JNFWeakJObjectWrapper wrapperWithJObject:obj withEnv:env]; |
12047 | 174 |
|
14753
a56a685d137f
7154778: [macosx] NSView-based implementation of sun.awt.EmbeddedFrame
serb
parents:
14306
diff
changeset
|
175 |
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){ |
a56a685d137f
7154778: [macosx] NSView-based implementation of sun.awt.EmbeddedFrame
serb
parents:
14306
diff
changeset
|
176 |
AWT_ASSERT_APPKIT_THREAD; |
54873
442e683e65fa
7141393: [macosx] CARemoteLayer code refactoring and unit test
serb
parents:
47216
diff
changeset
|
177 |
|
14753
a56a685d137f
7154778: [macosx] NSView-based implementation of sun.awt.EmbeddedFrame
serb
parents:
14306
diff
changeset
|
178 |
layer = [[CGLLayer alloc] initWithJavaLayer: javaLayer]; |
12047 | 179 |
}]; |
54873
442e683e65fa
7141393: [macosx] CARemoteLayer code refactoring and unit test
serb
parents:
47216
diff
changeset
|
180 |
|
12047 | 181 |
JNF_COCOA_EXIT(env); |
182 |
||
183 |
return ptr_to_jlong(layer); |
|
184 |
} |
|
185 |
||
186 |
// Must be called under the RQ lock. |
|
187 |
JNIEXPORT void JNICALL |
|
188 |
Java_sun_java2d_opengl_CGLLayer_validate |
|
16840
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
189 |
(JNIEnv *env, jclass cls, jlong layerPtr, jobject surfaceData) |
12047 | 190 |
{ |
191 |
CGLLayer *layer = OBJC(layerPtr); |
|
192 |
||
193 |
if (surfaceData != NULL) { |
|
194 |
OGLSDOps *oglsdo = (OGLSDOps*) SurfaceData_GetOps(env, surfaceData); |
|
195 |
layer.textureID = oglsdo->textureID; |
|
196 |
layer.target = GL_TEXTURE_2D; |
|
197 |
layer.textureWidth = oglsdo->width; |
|
198 |
layer.textureHeight = oglsdo->height; |
|
199 |
} else { |
|
200 |
layer.textureID = 0; |
|
201 |
} |
|
202 |
} |
|
203 |
||
204 |
// Must be called on the AppKit thread and under the RQ lock. |
|
205 |
JNIEXPORT void JNICALL |
|
206 |
Java_sun_java2d_opengl_CGLLayer_blitTexture |
|
16840
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
207 |
(JNIEnv *env, jclass cls, jlong layerPtr) |
12047 | 208 |
{ |
209 |
CGLLayer *layer = jlong_to_ptr(layerPtr); |
|
210 |
||
211 |
[layer blitTexture]; |
|
212 |
} |
|
16840
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
213 |
|
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
214 |
JNIEXPORT void JNICALL |
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
215 |
Java_sun_java2d_opengl_CGLLayer_nativeSetScale |
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
216 |
(JNIEnv *env, jclass cls, jlong layerPtr, jdouble scale) |
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
217 |
{ |
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
218 |
JNF_COCOA_ENTER(env); |
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
219 |
CGLLayer *layer = jlong_to_ptr(layerPtr); |
54873
442e683e65fa
7141393: [macosx] CARemoteLayer code refactoring and unit test
serb
parents:
47216
diff
changeset
|
220 |
// We always call all setXX methods asynchronously, exception is only in |
17151
9bfcf2a592fc
8009012: [macosx] DisplayChangedListener is not implemented in LWWindowPeer/CGraphicsEnvironment
serb
parents:
16840
diff
changeset
|
221 |
// this method where we need to change native texture size and layer's scale |
54873
442e683e65fa
7141393: [macosx] CARemoteLayer code refactoring and unit test
serb
parents:
47216
diff
changeset
|
222 |
// in one call on appkit, otherwise we'll get window's contents blinking, |
17151
9bfcf2a592fc
8009012: [macosx] DisplayChangedListener is not implemented in LWWindowPeer/CGraphicsEnvironment
serb
parents:
16840
diff
changeset
|
223 |
// during screen-2-screen moving. |
9bfcf2a592fc
8009012: [macosx] DisplayChangedListener is not implemented in LWWindowPeer/CGraphicsEnvironment
serb
parents:
16840
diff
changeset
|
224 |
[ThreadUtilities performOnMainThreadWaiting:[NSThread isMainThread] block:^(){ |
16840
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
225 |
layer.contentsScale = scale; |
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
226 |
}]; |
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
227 |
JNF_COCOA_EXIT(env); |
64446d4710c4
8000629: [macosx] Blurry rendering with Java 7 on Retina display
serb
parents:
14753
diff
changeset
|
228 |
} |