1 /*
2 * Copyright (c) 2019, 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 "MTLGraphicsConfig.h"
27 #import "MTLLayer.h"
28 #import "ThreadUtilities.h"
29 #import "LWCToolkit.h"
30 #import "MTLSurfaceData.h"
31
32 #import "MTLBlitLoops.h"
33
34 @implementation MTLLayer
35
36
37 @synthesize javaLayer;
38 @synthesize ctx;
39 @synthesize bufferWidth;
40 @synthesize bufferHeight;
41 @synthesize buffer;
42 @synthesize nextDrawableCount;
43 @synthesize topInset;
44 @synthesize leftInset;
45
46 - (id) initWithJavaLayer:(JNFWeakJObjectWrapper *)layer
47 {
48 AWT_ASSERT_APPKIT_THREAD;
49 // Initialize ourselves
50 self = [super init];
51 if (self == nil) return self;
52
53 self.javaLayer = layer;
54
55 self.contentsGravity = kCAGravityTopLeft;
56
57 //Disable CALayer's default animation
58 NSMutableDictionary * actions = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
59 [NSNull null], @"anchorPoint",
60 [NSNull null], @"bounds",
61 [NSNull null], @"contents",
62 [NSNull null], @"contentsScale",
63 [NSNull null], @"onOrderIn",
64 [NSNull null], @"onOrderOut",
65 [NSNull null], @"position",
66 [NSNull null], @"sublayers",
67 nil];
68 self.actions = actions;
69 [actions release];
70 self.topInset = 0;
71 self.leftInset = 0;
72 self.framebufferOnly = NO;
73 self.nextDrawableCount = 0;
74 return self;
75 }
76
77 - (void) blitTexture {
78 if (self.ctx == NULL || self.javaLayer == NULL || self.buffer == nil || self.ctx.device == nil) {
79 J2dTraceLn4(J2D_TRACE_VERBOSE, "MTLLayer.blitTexture: uninitialized (mtlc=%p, javaLayer=%p, buffer=%p, devide=%p)", self.ctx, self.javaLayer, self.buffer, ctx.device);
80 return;
81 }
82
83 if (self.nextDrawableCount != 0)
84 return;
85
86 @autoreleasepool {
87 if ((self.buffer.width == 0) || (self.buffer.height == 0)) {
88 J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer.blitTexture: cannot create drawable of size 0");
89 return;
90 }
91
92 id<MTLCommandBuffer> commandBuf = [self.ctx createBlitCommandBuffer];
93 if (commandBuf == nil) {
94 J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer.blitTexture: nothing to do (commandBuf is null)");
95 return;
96 }
97
98 id<CAMetalDrawable> mtlDrawable = [self nextDrawable];
99 if (mtlDrawable == nil) {
100 J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer.blitTexture: nextDrawable is null)");
101 return;
102 }
103 self.nextDrawableCount++;
104 J2dTraceLn6(J2D_TRACE_VERBOSE, "MTLLayer.blitTexture: src tex=%p (w=%d, h=%d), dst tex=%p (w=%d, h=%d)", self.buffer, self.buffer.width, self.buffer.height, mtlDrawable.texture, mtlDrawable.texture.width, mtlDrawable.texture.height);
105 id <MTLBlitCommandEncoder> blitEncoder = [commandBuf blitCommandEncoder];
106 [blitEncoder
107 copyFromTexture:self.buffer sourceSlice:0 sourceLevel:0
108 sourceOrigin:MTLOriginMake((jint)(self.leftInset*self.contentsScale), (jint)(self.topInset*self.contentsScale), 0)
109 sourceSize:MTLSizeMake(self.buffer.width, self.buffer.height, 1)
110 toTexture:mtlDrawable.texture destinationSlice:0 destinationLevel:0 destinationOrigin:MTLOriginMake(0, 0, 0)];
111 [blitEncoder endEncoding];
112
113 [commandBuf presentDrawable:mtlDrawable];
114
115 [commandBuf addCompletedHandler:^(id <MTLCommandBuffer> commandBuf) {
116 self.nextDrawableCount--;
117 }];
118
119 [commandBuf commit];
120 }
121 }
122
123 - (void) dealloc {
124 self.javaLayer = nil;
125 [super dealloc];
126 }
127
128 - (void) blitCallback {
129
130 JNIEnv *env = [ThreadUtilities getJNIEnv];
131 static JNF_CLASS_CACHE(jc_JavaLayer, "sun/java2d/metal/MTLLayer");
132 static JNF_MEMBER_CACHE(jm_drawInMTLContext, jc_JavaLayer, "drawInMTLContext", "()V");
133
134 jobject javaLayerLocalRef = [self.javaLayer jObjectWithEnv:env];
135 if ((*env)->IsSameObject(env, javaLayerLocalRef, NULL)) {
136 return;
137 }
138
139 JNFCallVoidMethod(env, javaLayerLocalRef, jm_drawInMTLContext);
140 (*env)->DeleteLocalRef(env, javaLayerLocalRef);
141 }
142
143 - (void) display {
144 AWT_ASSERT_APPKIT_THREAD;
145 J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer_display() called");
146 [self blitCallback];
147 [super display];
148 }
149 @end
150
151 /*
152 * Class: sun_java2d_metal_MTLLayer
153 * Method: nativeCreateLayer
154 * Signature: ()J
155 */
156 JNIEXPORT jlong JNICALL
157 Java_sun_java2d_metal_MTLLayer_nativeCreateLayer
158 (JNIEnv *env, jobject obj)
159 {
160 __block MTLLayer *layer = nil;
161
162 JNF_COCOA_ENTER(env);
163
164 JNFWeakJObjectWrapper *javaLayer = [JNFWeakJObjectWrapper wrapperWithJObject:obj withEnv:env];
165
166 [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
167 AWT_ASSERT_APPKIT_THREAD;
168
169 layer = [[MTLLayer alloc] initWithJavaLayer: javaLayer];
170 }];
171
172 JNF_COCOA_EXIT(env);
173
174 return ptr_to_jlong(layer);
175 }
176
177 // Must be called under the RQ lock.
178 JNIEXPORT void JNICALL
179 Java_sun_java2d_metal_MTLLayer_validate
180 (JNIEnv *env, jclass cls, jlong layerPtr, jobject surfaceData)
181 {
182 MTLLayer *layer = OBJC(layerPtr);
183
184 if (surfaceData != NULL) {
185 BMTLSDOps *bmtlsdo = (BMTLSDOps*) SurfaceData_GetOps(env, surfaceData);
186 layer.bufferWidth = bmtlsdo->width;
187 layer.bufferHeight = bmtlsdo->width;
188 layer.buffer = bmtlsdo->pTexture;
189 layer.ctx = ((MTLSDOps *)bmtlsdo->privOps)->configInfo->context;
190 layer.device = layer.ctx.device;
191 layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
192 layer.drawableSize =
193 CGSizeMake(layer.buffer.width,
194 layer.buffer.height);
195 } else {
196 layer.ctx = NULL;
197 }
198 }
199
200 JNIEXPORT void JNICALL
201 Java_sun_java2d_metal_MTLLayer_nativeSetScale
202 (JNIEnv *env, jclass cls, jlong layerPtr, jdouble scale)
203 {
204 JNF_COCOA_ENTER(env);
205 MTLLayer *layer = jlong_to_ptr(layerPtr);
206 // We always call all setXX methods asynchronously, exception is only in
207 // this method where we need to change native texture size and layer's scale
208 // in one call on appkit, otherwise we'll get window's contents blinking,
209 // during screen-2-screen moving.
210 [ThreadUtilities performOnMainThreadWaiting:[NSThread isMainThread] block:^(){
211 layer.contentsScale = scale;
212 }];
213 JNF_COCOA_EXIT(env);
214 }
215
216 JNIEXPORT void JNICALL
217 Java_sun_java2d_metal_MTLLayer_nativeSetInsets
218 (JNIEnv *env, jclass cls, jlong layerPtr, jint top, jint left)
219 {
220 MTLLayer *layer = jlong_to_ptr(layerPtr);
221 layer.topInset = top;
222 layer.leftInset = left;
223 }
224
225 JNIEXPORT void JNICALL
226 Java_sun_java2d_metal_MTLLayer_blitTexture
227 (JNIEnv *env, jclass cls, jlong layerPtr)
228 {
229 J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer_blitTexture");
230 MTLLayer *layer = jlong_to_ptr(layerPtr);
231 MTLContext * ctx = layer.ctx;
232 if (layer == NULL || ctx == NULL) {
233 J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer_blit : Layer or Context is null");
234 return;
235 }
236
237 [layer blitTexture];
238 }