Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLSurfaceData.m
41159 views
1
/*
2
* Copyright (c) 2019, 2021, 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 <stdlib.h>
27
28
#import "sun_java2d_metal_MTLSurfaceData.h"
29
30
#import "jni_util.h"
31
#import "MTLGraphicsConfig.h"
32
#import "MTLSurfaceData.h"
33
#include "jlong.h"
34
35
jboolean MTLSD_InitMTLWindow(JNIEnv *env, BMTLSDOps *bmtlsdo);
36
void MTLSD_SetNativeDimensions(JNIEnv *env, BMTLSDOps *bmtlsdo, jint w, jint h);
37
38
static jboolean MTLSurfaceData_initTexture(BMTLSDOps *bmtlsdo, jboolean isOpaque, jboolean rtt, jint width, jint height) {
39
@autoreleasepool {
40
if (bmtlsdo == NULL) {
41
J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLSurfaceData_initTexture: ops are null");
42
return JNI_FALSE;
43
}
44
if (width <= 0 || height <= 0) {
45
J2dRlsTraceLn2(J2D_TRACE_ERROR, "MTLSurfaceData_initTexture: texture dimensions is incorrect, w=%d, h=%d", width, height);
46
return JNI_FALSE;
47
}
48
49
MTLSDOps *mtlsdo = (MTLSDOps *)bmtlsdo->privOps;
50
51
if (mtlsdo == NULL) {
52
J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLSurfaceData_initTexture: MTLSDOps are null");
53
return JNI_FALSE;
54
}
55
if (mtlsdo->configInfo == NULL || mtlsdo->configInfo->context == NULL) {
56
J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLSurfaceData_initTexture: MTLSDOps wasn't initialized (context is null)");
57
return JNI_FALSE;
58
}
59
60
MTLContext* ctx = mtlsdo->configInfo->context;
61
62
width = (width <= MTL_GPU_FAMILY_MAC_TXT_SIZE) ? width : 0;
63
height = (height <= MTL_GPU_FAMILY_MAC_TXT_SIZE) ? height : 0;
64
65
J2dTraceLn3(J2D_TRACE_VERBOSE, " desired texture dimensions: w=%d h=%d max=%d",
66
width, height, MTL_GPU_FAMILY_MAC_TXT_SIZE);
67
68
// if either dimension is 0, we cannot allocate a texture with the
69
// requested dimensions
70
if ((width == 0 || height == 0)) {
71
J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLSurfaceData_initTexture: texture dimensions too large");
72
return JNI_FALSE;
73
}
74
75
MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat: MTLPixelFormatBGRA8Unorm width: width height: height mipmapped: NO];
76
textureDescriptor.usage = MTLTextureUsageUnknown;
77
textureDescriptor.storageMode = MTLStorageModePrivate;
78
bmtlsdo->pTexture = [ctx.device newTextureWithDescriptor: textureDescriptor];
79
80
MTLTextureDescriptor *stencilDataDescriptor =
81
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatR8Uint width:width height:height mipmapped:NO];
82
stencilDataDescriptor.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;
83
stencilDataDescriptor.storageMode = MTLStorageModePrivate;
84
bmtlsdo->pStencilData = [ctx.device newTextureWithDescriptor:stencilDataDescriptor];
85
86
MTLTextureDescriptor *stencilTextureDescriptor =
87
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatStencil8 width:width height:height mipmapped:NO];
88
stencilTextureDescriptor.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite;
89
stencilTextureDescriptor.storageMode = MTLStorageModePrivate;
90
bmtlsdo->pStencilTexture = [ctx.device newTextureWithDescriptor:stencilTextureDescriptor];
91
bmtlsdo->isOpaque = isOpaque;
92
bmtlsdo->width = width;
93
bmtlsdo->height = height;
94
bmtlsdo->drawableType = rtt ? MTLSD_RT_TEXTURE : MTLSD_TEXTURE;
95
96
J2dTraceLn6(J2D_TRACE_VERBOSE, "MTLSurfaceData_initTexture: w=%d h=%d bp=%p [tex=%p] opaque=%d rtt=%d", width, height, bmtlsdo, bmtlsdo->pTexture, isOpaque, rtt);
97
return JNI_TRUE;
98
}
99
}
100
101
/**
102
* Initializes an MTL texture, using the given width and height as
103
* a guide.
104
*/
105
JNIEXPORT jboolean JNICALL
106
Java_sun_java2d_metal_MTLSurfaceData_initTexture(
107
JNIEnv *env, jobject mtlsd,
108
jlong pData, jboolean isOpaque,
109
jint width, jint height
110
) {
111
if (!MTLSurfaceData_initTexture((BMTLSDOps *)pData, isOpaque, JNI_FALSE, width, height))
112
return JNI_FALSE;
113
MTLSD_SetNativeDimensions(env, (BMTLSDOps *)pData, width, height);
114
return JNI_TRUE;
115
}
116
117
/**
118
* Initializes a framebuffer object, using the given width and height as
119
* a guide. See MTLSD_InitTextureObject() and MTLSD_initRTexture()
120
* for more information.
121
*/
122
JNIEXPORT jboolean JNICALL
123
Java_sun_java2d_metal_MTLSurfaceData_initRTexture
124
(JNIEnv *env, jobject mtlsd,
125
jlong pData, jboolean isOpaque,
126
jint width, jint height)
127
{
128
if (!MTLSurfaceData_initTexture((BMTLSDOps *)pData, isOpaque, JNI_TRUE, width, height))
129
return JNI_FALSE;
130
MTLSD_SetNativeDimensions(env, (BMTLSDOps *)pData, width, height);
131
return JNI_TRUE;
132
}
133
134
JNIEXPORT jlong JNICALL
135
Java_sun_java2d_metal_MTLSurfaceData_getMTLTexturePointer(JNIEnv *env, jobject mtlsd, jlong pData) {
136
if (pData == 0)
137
return 0;
138
return ptr_to_jlong(((BMTLSDOps *)pData)->pTexture);
139
}
140
141
/**
142
* Initializes nativeWidth/Height fields of the surfaceData object with
143
* passed arguments.
144
*/
145
void
146
MTLSD_SetNativeDimensions(JNIEnv *env, BMTLSDOps *mtlsdo,
147
jint width, jint height)
148
{
149
jobject sdObject;
150
151
sdObject = (*env)->NewLocalRef(env, mtlsdo->sdOps.sdObject);
152
if (sdObject == NULL) {
153
return;
154
}
155
156
JNU_SetFieldByName(env, NULL, sdObject, "nativeWidth", "I", width);
157
if (!((*env)->ExceptionOccurred(env))) {
158
JNU_SetFieldByName(env, NULL, sdObject, "nativeHeight", "I", height);
159
}
160
161
(*env)->DeleteLocalRef(env, sdObject);
162
}
163
164
/**
165
* Deletes native Metal resources associated with this surface.
166
*/
167
void
168
MTLSD_Delete(JNIEnv *env, BMTLSDOps *bmtlsdo)
169
{
170
J2dTraceLn3(J2D_TRACE_VERBOSE, "MTLSD_Delete: type=%d %p [tex=%p]", bmtlsdo->drawableType, bmtlsdo, bmtlsdo->pTexture);
171
if (bmtlsdo->drawableType == MTLSD_WINDOW) {
172
bmtlsdo->drawableType = MTLSD_UNDEFINED;
173
} else if (
174
bmtlsdo->drawableType == MTLSD_RT_TEXTURE
175
|| bmtlsdo->drawableType == MTLSD_TEXTURE
176
|| bmtlsdo->drawableType == MTLSD_FLIP_BACKBUFFER
177
) {
178
[(NSObject *)bmtlsdo->pTexture release];
179
[(NSObject *)bmtlsdo->pStencilTexture release];
180
[(NSObject *)bmtlsdo->pStencilData release];
181
bmtlsdo->pTexture = NULL;
182
bmtlsdo->drawableType = MTLSD_UNDEFINED;
183
}
184
}
185
186
/**
187
* This is the implementation of the general DisposeFunc defined in
188
* SurfaceData.h and used by the Disposer mechanism. It first flushes all
189
* native Metal resources and then frees any memory allocated within the
190
* native MTLSDOps structure.
191
*/
192
void
193
MTLSD_Dispose(JNIEnv *env, SurfaceDataOps *ops)
194
{
195
BMTLSDOps *bmtlsdo = (BMTLSDOps *)ops;
196
jobject graphicsConfig = bmtlsdo->graphicsConfig;
197
198
JNU_CallStaticMethodByName(env, NULL, "sun/java2d/metal/MTLSurfaceData",
199
"dispose",
200
"(JLsun/java2d/metal/MTLGraphicsConfig;)V",
201
ptr_to_jlong(ops), graphicsConfig);
202
(*env)->DeleteGlobalRef(env, graphicsConfig);
203
bmtlsdo->graphicsConfig = NULL;
204
}
205
206
/**
207
* This is the implementation of the general surface LockFunc defined in
208
* SurfaceData.h.
209
*/
210
jint
211
MTLSD_Lock(JNIEnv *env,
212
SurfaceDataOps *ops,
213
SurfaceDataRasInfo *pRasInfo,
214
jint lockflags)
215
{
216
JNU_ThrowInternalError(env, "MTLSD_Lock not implemented!");
217
return SD_FAILURE;
218
}
219
220
/**
221
* This is the implementation of the general GetRasInfoFunc defined in
222
* SurfaceData.h.
223
*/
224
void
225
MTLSD_GetRasInfo(JNIEnv *env,
226
SurfaceDataOps *ops,
227
SurfaceDataRasInfo *pRasInfo)
228
{
229
JNU_ThrowInternalError(env, "MTLSD_GetRasInfo not implemented!");
230
}
231
232
/**
233
* This is the implementation of the general surface UnlockFunc defined in
234
* SurfaceData.h.
235
*/
236
void
237
MTLSD_Unlock(JNIEnv *env,
238
SurfaceDataOps *ops,
239
SurfaceDataRasInfo *pRasInfo)
240
{
241
JNU_ThrowInternalError(env, "MTLSD_Unlock not implemented!");
242
}
243
244
245
/**
246
* This function initializes a native window surface and caches the window
247
* bounds in the given BMTLSDOps. Returns JNI_TRUE if the operation was
248
* successful; JNI_FALSE otherwise.
249
*/
250
jboolean
251
MTLSD_InitMTLWindow(JNIEnv *env, BMTLSDOps *bmtlsdo)
252
{
253
if (bmtlsdo == NULL) {
254
J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLSD_InitMTLWindow: ops are null");
255
return JNI_FALSE;
256
}
257
258
MTLSDOps *mtlsdo = (MTLSDOps *)bmtlsdo->privOps;
259
if (mtlsdo == NULL) {
260
J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLSD_InitMTLWindow: priv ops are null");
261
return JNI_FALSE;
262
}
263
264
AWTView *v = mtlsdo->peerData;
265
if (v == NULL) {
266
J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLSD_InitMTLWindow: view is invalid");
267
return JNI_FALSE;
268
}
269
270
JNI_COCOA_ENTER(env);
271
NSRect surfaceBounds = [v bounds];
272
bmtlsdo->drawableType = MTLSD_WINDOW;
273
bmtlsdo->isOpaque = JNI_TRUE;
274
bmtlsdo->width = surfaceBounds.size.width;
275
bmtlsdo->height = surfaceBounds.size.height;
276
JNI_COCOA_EXIT(env);
277
278
J2dTraceLn2(J2D_TRACE_VERBOSE, " created window: w=%d h=%d", bmtlsdo->width, bmtlsdo->height);
279
return JNI_TRUE;
280
}
281
282
#pragma mark -
283
#pragma mark "--- MTLSurfaceData methods ---"
284
285
extern LockFunc MTLSD_Lock;
286
extern GetRasInfoFunc MTLSD_GetRasInfo;
287
extern UnlockFunc MTLSD_Unlock;
288
289
290
JNIEXPORT void JNICALL
291
Java_sun_java2d_metal_MTLSurfaceData_initOps
292
(JNIEnv *env, jobject mtlsd, jobject gc,
293
jlong pConfigInfo, jlong pPeerData, jlong layerPtr,
294
jint xoff, jint yoff, jboolean isOpaque)
295
{
296
BMTLSDOps *bmtlsdo = (BMTLSDOps *)SurfaceData_InitOps(env, mtlsd, sizeof(BMTLSDOps));
297
MTLSDOps *mtlsdo = (MTLSDOps *)malloc(sizeof(MTLSDOps));
298
299
J2dTraceLn1(J2D_TRACE_INFO, "MTLSurfaceData_initOps p=%p", bmtlsdo);
300
J2dTraceLn1(J2D_TRACE_INFO, " pPeerData=%p", jlong_to_ptr(pPeerData));
301
J2dTraceLn1(J2D_TRACE_INFO, " layerPtr=%p", jlong_to_ptr(layerPtr));
302
J2dTraceLn2(J2D_TRACE_INFO, " xoff=%d, yoff=%d", (int)xoff, (int)yoff);
303
304
gc = (*env)->NewGlobalRef(env, gc);
305
if (gc == NULL) {
306
JNU_ThrowOutOfMemoryError(env, "Initialization of SurfaceData failed.");
307
return;
308
}
309
310
if (mtlsdo == NULL) {
311
(*env)->DeleteGlobalRef(env, gc);
312
JNU_ThrowOutOfMemoryError(env, "Initialization of SurfaceData failed.");
313
return;
314
}
315
316
// later the graphicsConfig will be used for deallocation of mtlsdo
317
bmtlsdo->privOps = mtlsdo;
318
bmtlsdo->graphicsConfig = gc;
319
320
bmtlsdo->sdOps.Lock = MTLSD_Lock;
321
bmtlsdo->sdOps.GetRasInfo = MTLSD_GetRasInfo;
322
bmtlsdo->sdOps.Unlock = MTLSD_Unlock;
323
bmtlsdo->sdOps.Dispose = MTLSD_Dispose;
324
bmtlsdo->drawableType = MTLSD_UNDEFINED;
325
326
bmtlsdo->isOpaque = isOpaque;
327
328
mtlsdo->peerData = (AWTView *)jlong_to_ptr(pPeerData);
329
mtlsdo->layer = (MTLLayer *)jlong_to_ptr(layerPtr);
330
mtlsdo->configInfo = (MTLGraphicsConfigInfo *)jlong_to_ptr(pConfigInfo);
331
332
if (mtlsdo->configInfo == NULL) {
333
free(mtlsdo);
334
JNU_ThrowNullPointerException(env, "Config info is null in initOps");
335
}
336
}
337
338
JNIEXPORT void JNICALL
339
Java_sun_java2d_metal_MTLSurfaceData_clearWindow
340
(JNIEnv *env, jobject mtlsd)
341
{
342
J2dTraceLn(J2D_TRACE_INFO, "MTLSurfaceData_clearWindow");
343
344
BMTLSDOps *bmtlsdo = (MTLSDOps*) SurfaceData_GetOps(env, mtlsd);
345
MTLSDOps *mtlsdo = (MTLSDOps*) bmtlsdo->privOps;
346
347
mtlsdo->peerData = NULL;
348
mtlsdo->layer = NULL;
349
}
350
351
NSString * getSurfaceDescription(const BMTLSDOps * bmtlsdOps) {
352
if (bmtlsdOps == NULL)
353
return @"NULL";
354
return [NSString stringWithFormat:@"%p [tex=%p, %dx%d, O=%d]", bmtlsdOps, bmtlsdOps->pTexture, bmtlsdOps->width, bmtlsdOps->height, bmtlsdOps->isOpaque];
355
}
356
357