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/MTLContext.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
#include <stdlib.h>
27
28
#include "sun_java2d_SunGraphics2D.h"
29
30
#include "jlong.h"
31
#import "MTLContext.h"
32
#include "MTLRenderQueue.h"
33
#import "MTLSamplerManager.h"
34
#import "MTLStencilManager.h"
35
36
37
extern jboolean MTLSD_InitMTLWindow(JNIEnv *env, MTLSDOps *mtlsdo);
38
39
static struct TxtVertex verts[PGRAM_VERTEX_COUNT] = {
40
{{-1.0, 1.0}, {0.0, 0.0}},
41
{{1.0, 1.0}, {1.0, 0.0}},
42
{{1.0, -1.0}, {1.0, 1.0}},
43
{{1.0, -1.0}, {1.0, 1.0}},
44
{{-1.0, -1.0}, {0.0, 1.0}},
45
{{-1.0, 1.0}, {0.0, 0.0}}
46
};
47
48
MTLTransform* tempTransform = nil;
49
50
@implementation MTLCommandBufferWrapper {
51
id<MTLCommandBuffer> _commandBuffer;
52
NSMutableArray * _pooledTextures;
53
NSLock* _lock;
54
}
55
56
- (id) initWithCommandBuffer:(id<MTLCommandBuffer>)cmdBuf {
57
self = [super init];
58
if (self) {
59
_commandBuffer = [cmdBuf retain];
60
_pooledTextures = [[NSMutableArray alloc] init];
61
_lock = [[NSLock alloc] init];
62
}
63
return self;
64
}
65
66
- (id<MTLCommandBuffer>) getCommandBuffer {
67
return _commandBuffer;
68
}
69
70
- (void) onComplete { // invoked from completion handler in some pooled thread
71
[_lock lock];
72
@try {
73
for (int c = 0; c < [_pooledTextures count]; ++c)
74
[[_pooledTextures objectAtIndex:c] releaseTexture];
75
[_pooledTextures removeAllObjects];
76
} @finally {
77
[_lock unlock];
78
}
79
80
}
81
82
- (void) registerPooledTexture:(MTLPooledTextureHandle *)handle {
83
[_lock lock];
84
@try {
85
[_pooledTextures addObject:handle];
86
} @finally {
87
[_lock unlock];
88
}
89
}
90
91
- (void) dealloc {
92
[self onComplete];
93
94
[_pooledTextures release];
95
_pooledTextures = nil;
96
97
[_commandBuffer release];
98
_commandBuffer = nil;
99
100
[_lock release];
101
_lock = nil;
102
[super dealloc];
103
}
104
105
@end
106
107
@implementation MTLContext {
108
MTLCommandBufferWrapper * _commandBufferWrapper;
109
110
MTLComposite * _composite;
111
MTLPaint * _paint;
112
MTLTransform * _transform;
113
MTLTransform * _tempTransform;
114
MTLClip * _clip;
115
NSObject* _bufImgOp; // TODO: pass as parameter of IsoBlit
116
117
EncoderManager * _encoderManager;
118
MTLSamplerManager * _samplerManager;
119
MTLStencilManager * _stencilManager;
120
}
121
122
@synthesize textureFunction,
123
vertexCacheEnabled, aaEnabled, device, pipelineStateStorage,
124
commandQueue, blitCommandQueue, vertexBuffer,
125
texturePool, paint=_paint, encoderManager=_encoderManager,
126
samplerManager=_samplerManager, stencilManager=_stencilManager;
127
128
extern void initSamplers(id<MTLDevice> device);
129
130
- (id)initWithDevice:(id<MTLDevice>)d shadersLib:(NSString*)shadersLib {
131
self = [super init];
132
if (self) {
133
// Initialization code here.
134
device = d;
135
136
pipelineStateStorage = [[MTLPipelineStatesStorage alloc] initWithDevice:device shaderLibPath:shadersLib];
137
if (pipelineStateStorage == nil) {
138
J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLContext.initWithDevice(): Failed to initialize MTLPipelineStatesStorage.");
139
return nil;
140
}
141
142
texturePool = [[MTLTexturePool alloc] initWithDevice:device];
143
144
vertexBuffer = [device newBufferWithBytes:verts
145
length:sizeof(verts)
146
options:MTLResourceCPUCacheModeDefaultCache];
147
148
_encoderManager = [[EncoderManager alloc] init];
149
[_encoderManager setContext:self];
150
_samplerManager = [[MTLSamplerManager alloc] initWithDevice:device];
151
_stencilManager = [[MTLStencilManager alloc] initWithDevice:device];
152
_composite = [[MTLComposite alloc] init];
153
_paint = [[MTLPaint alloc] init];
154
_transform = [[MTLTransform alloc] init];
155
_clip = [[MTLClip alloc] init];
156
_bufImgOp = nil;
157
158
_commandBufferWrapper = nil;
159
160
// Create command queue
161
commandQueue = [device newCommandQueue];
162
blitCommandQueue = [device newCommandQueue];
163
164
_tempTransform = [[MTLTransform alloc] init];
165
}
166
return self;
167
}
168
169
- (void)dealloc {
170
J2dTraceLn(J2D_TRACE_INFO, "MTLContext.dealloc");
171
172
// TODO : Check that texturePool is completely released.
173
// texturePool content is released in MTLCommandBufferWrapper.onComplete()
174
//self.texturePool = nil;
175
self.vertexBuffer = nil;
176
self.commandQueue = nil;
177
self.blitCommandQueue = nil;
178
self.pipelineStateStorage = nil;
179
180
if (_encoderManager != nil) {
181
[_encoderManager release];
182
_encoderManager = nil;
183
}
184
185
if (_samplerManager != nil) {
186
[_samplerManager release];
187
_samplerManager = nil;
188
}
189
190
if (_stencilManager != nil) {
191
[_stencilManager release];
192
_stencilManager = nil;
193
}
194
195
if (_composite != nil) {
196
[_composite release];
197
_composite = nil;
198
}
199
200
if (_paint != nil) {
201
[_paint release];
202
_paint = nil;
203
}
204
205
if (_transform != nil) {
206
[_transform release];
207
_transform = nil;
208
}
209
210
if (_tempTransform != nil) {
211
[_tempTransform release];
212
_tempTransform = nil;
213
}
214
215
if (_clip != nil) {
216
[_clip release];
217
_clip = nil;
218
}
219
220
[super dealloc];
221
}
222
223
- (void) reset {
224
J2dTraceLn(J2D_TRACE_VERBOSE, "MTLContext : reset");
225
226
// Add code for context state reset here
227
}
228
229
- (MTLCommandBufferWrapper *) getCommandBufferWrapper {
230
if (_commandBufferWrapper == nil) {
231
J2dTraceLn(J2D_TRACE_VERBOSE, "MTLContext : commandBuffer is NULL");
232
// NOTE: Command queues are thread-safe and allow multiple outstanding command buffers to be encoded simultaneously.
233
_commandBufferWrapper = [[MTLCommandBufferWrapper alloc] initWithCommandBuffer:[self.commandQueue commandBuffer]];// released in [layer blitTexture]
234
}
235
return _commandBufferWrapper;
236
}
237
238
- (MTLCommandBufferWrapper *) pullCommandBufferWrapper {
239
MTLCommandBufferWrapper * result = _commandBufferWrapper;
240
_commandBufferWrapper = nil;
241
return result;
242
}
243
244
+ (MTLContext*) setSurfacesEnv:(JNIEnv*)env src:(jlong)pSrc dst:(jlong)pDst {
245
BMTLSDOps *srcOps = (BMTLSDOps *)jlong_to_ptr(pSrc);
246
BMTLSDOps *dstOps = (BMTLSDOps *)jlong_to_ptr(pDst);
247
MTLContext *mtlc = NULL;
248
249
if (srcOps == NULL || dstOps == NULL) {
250
J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLContext_SetSurfaces: ops are null");
251
return NULL;
252
}
253
254
J2dTraceLn6(J2D_TRACE_VERBOSE, "MTLContext_SetSurfaces: bsrc=%p (tex=%p type=%d), bdst=%p (tex=%p type=%d)", srcOps, srcOps->pTexture, srcOps->drawableType, dstOps, dstOps->pTexture, dstOps->drawableType);
255
256
if (dstOps->drawableType == MTLSD_TEXTURE) {
257
J2dRlsTraceLn(J2D_TRACE_ERROR,
258
"MTLContext_SetSurfaces: texture cannot be used as destination");
259
return NULL;
260
}
261
262
if (dstOps->drawableType == MTLSD_UNDEFINED) {
263
// initialize the surface as an MTLSD_WINDOW
264
if (!MTLSD_InitMTLWindow(env, dstOps)) {
265
J2dRlsTraceLn(J2D_TRACE_ERROR,
266
"MTLContext_SetSurfaces: could not init MTL window");
267
return NULL;
268
}
269
}
270
271
// make the context current
272
MTLSDOps *dstMTLOps = (MTLSDOps *)dstOps->privOps;
273
mtlc = dstMTLOps->configInfo->context;
274
275
if (mtlc == NULL) {
276
J2dRlsTraceLn(J2D_TRACE_ERROR,
277
"MTLContext_SetSurfaces: could not make context current");
278
return NULL;
279
}
280
281
return mtlc;
282
}
283
284
- (void)resetClip {
285
J2dTraceLn(J2D_TRACE_INFO, "MTLContext.resetClip");
286
[_clip reset];
287
}
288
289
- (void)setClipRectX1:(jint)x1 Y1:(jint)y1 X2:(jint)x2 Y2:(jint)y2 {
290
J2dTraceLn4(J2D_TRACE_INFO, "MTLContext.setClipRect: %d,%d - %d,%d", x1, y1, x2, y2);
291
[_clip setClipRectX1:x1 Y1:y1 X2:x2 Y2:y2];
292
}
293
294
- (void)beginShapeClip:(BMTLSDOps *)dstOps {
295
J2dTraceLn(J2D_TRACE_INFO, "MTLContext.beginShapeClip");
296
[_clip beginShapeClip:dstOps context:self];
297
298
// Store the current transform as we need to use identity transform
299
// for clip spans rendering
300
[_tempTransform copyFrom:_transform];
301
[self resetTransform];
302
}
303
304
- (void)endShapeClip:(BMTLSDOps *)dstOps {
305
J2dTraceLn(J2D_TRACE_INFO, "MTLContext.endShapeClip");
306
[_clip endShapeClip:dstOps context:self];
307
308
// Reset transform for further rendering
309
[_transform copyFrom:_tempTransform];
310
}
311
312
- (void)resetComposite {
313
J2dTraceLn(J2D_TRACE_VERBOSE, "MTLContext_ResetComposite");
314
[_composite reset];
315
}
316
317
- (void)setAlphaCompositeRule:(jint)rule extraAlpha:(jfloat)extraAlpha
318
flags:(jint)flags {
319
J2dTraceLn3(J2D_TRACE_INFO, "MTLContext_SetAlphaComposite: rule=%d, extraAlpha=%1.2f, flags=%d", rule, extraAlpha, flags);
320
321
[_composite setRule:rule extraAlpha:extraAlpha];
322
}
323
324
- (NSString*)getCompositeDescription {
325
return [_composite getDescription];
326
}
327
328
- (NSString*)getPaintDescription {
329
return [_paint getDescription];
330
}
331
332
- (void)setXorComposite:(jint)xp {
333
J2dTraceLn1(J2D_TRACE_INFO, "MTLContext.setXorComposite: xorPixel=%08x", xp);
334
335
[_composite setXORComposite:xp];
336
}
337
338
- (jboolean) useXORComposite {
339
return ([_composite getCompositeState] == sun_java2d_SunGraphics2D_COMP_XOR);
340
}
341
342
- (void)resetTransform {
343
J2dTraceLn(J2D_TRACE_INFO, "MTLContext_ResetTransform");
344
[_transform resetTransform];
345
}
346
347
- (void)setTransformM00:(jdouble) m00 M10:(jdouble) m10
348
M01:(jdouble) m01 M11:(jdouble) m11
349
M02:(jdouble) m02 M12:(jdouble) m12 {
350
J2dTraceLn(J2D_TRACE_INFO, "MTLContext_SetTransform");
351
[_transform setTransformM00:m00 M10:m10 M01:m01 M11:m11 M02:m02 M12:m12];
352
}
353
354
- (void)resetPaint {
355
J2dTraceLn(J2D_TRACE_INFO, "MTLContext.resetPaint");
356
self.paint = [[[MTLPaint alloc] init] autorelease];
357
}
358
359
- (void)setColorPaint:(int)pixel {
360
J2dTraceLn5(J2D_TRACE_INFO, "MTLContext.setColorPaint: pixel=%08x [r=%d g=%d b=%d a=%d]", pixel, (pixel >> 16) & (0xFF), (pixel >> 8) & 0xFF, (pixel) & 0xFF, (pixel >> 24) & 0xFF);
361
self.paint = [[[MTLColorPaint alloc] initWithColor:pixel] autorelease];
362
}
363
364
- (void)setGradientPaintUseMask:(jboolean)useMask
365
cyclic:(jboolean)cyclic
366
p0:(jdouble)p0
367
p1:(jdouble)p1
368
p3:(jdouble)p3
369
pixel1:(jint)pixel1
370
pixel2:(jint) pixel2
371
{
372
J2dTraceLn(J2D_TRACE_INFO, "MTLContext.setGradientPaintUseMask");
373
self.paint = [[[MTLGradPaint alloc] initWithUseMask:useMask
374
cyclic:cyclic
375
p0:p0
376
p1:p1
377
p3:p3
378
pixel1:pixel1
379
pixel2:pixel2] autorelease];
380
}
381
382
- (void)setLinearGradientPaint:(jboolean)useMask
383
linear:(jboolean)linear
384
cycleMethod:(jint)cycleMethod
385
// 0 - NO_CYCLE
386
// 1 - REFLECT
387
// 2 - REPEAT
388
389
numStops:(jint)numStops
390
p0:(jfloat)p0
391
p1:(jfloat)p1
392
p3:(jfloat)p3
393
fractions:(jfloat*)fractions
394
pixels:(jint*)pixels
395
{
396
J2dTraceLn(J2D_TRACE_INFO, "MTLContext.setLinearGradientPaint");
397
self.paint = [[[MTLLinearGradPaint alloc] initWithUseMask:useMask
398
linear:linear
399
cycleMethod:cycleMethod
400
numStops:numStops
401
p0:p0
402
p1:p1
403
p3:p3
404
fractions:fractions
405
pixels:pixels] autorelease];
406
}
407
408
- (void)setRadialGradientPaint:(jboolean)useMask
409
linear:(jboolean)linear
410
cycleMethod:(jboolean)cycleMethod
411
numStops:(jint)numStops
412
m00:(jfloat)m00
413
m01:(jfloat)m01
414
m02:(jfloat)m02
415
m10:(jfloat)m10
416
m11:(jfloat)m11
417
m12:(jfloat)m12
418
focusX:(jfloat)focusX
419
fractions:(void *)fractions
420
pixels:(void *)pixels
421
{
422
J2dTraceLn(J2D_TRACE_INFO, "MTLContext.setRadialGradientPaint");
423
self.paint = [[[MTLRadialGradPaint alloc] initWithUseMask:useMask
424
linear:linear
425
cycleMethod:cycleMethod
426
numStops:numStops
427
m00:m00
428
m01:m01
429
m02:m02
430
m10:m10
431
m11:m11
432
m12:m12
433
focusX:focusX
434
fractions:fractions
435
pixels:pixels] autorelease];
436
}
437
438
- (void)setTexturePaint:(jboolean)useMask
439
pSrcOps:(jlong)pSrcOps
440
filter:(jboolean)filter
441
xp0:(jdouble)xp0
442
xp1:(jdouble)xp1
443
xp3:(jdouble)xp3
444
yp0:(jdouble)yp0
445
yp1:(jdouble)yp1
446
yp3:(jdouble)yp3
447
{
448
BMTLSDOps *srcOps = (BMTLSDOps *)jlong_to_ptr(pSrcOps);
449
450
if (srcOps == NULL || srcOps->pTexture == NULL) {
451
J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLContext_setTexturePaint: texture paint - texture is null");
452
return;
453
}
454
455
J2dTraceLn1(J2D_TRACE_INFO, "MTLContext.setTexturePaint [tex=%p]", srcOps->pTexture);
456
457
self.paint = [[[MTLTexturePaint alloc] initWithUseMask:useMask
458
textureID:srcOps->pTexture
459
isOpaque:srcOps->isOpaque
460
filter:filter
461
xp0:xp0
462
xp1:xp1
463
xp3:xp3
464
yp0:yp0
465
yp1:yp1
466
yp3:yp3] autorelease];
467
}
468
469
- (id<MTLCommandBuffer>)createCommandBuffer {
470
return [self.commandQueue commandBuffer];
471
}
472
473
/*
474
* This should be exclusively used only for final blit
475
* and present of CAMetalDrawable in MTLLayer
476
*/
477
- (id<MTLCommandBuffer>)createBlitCommandBuffer {
478
return [self.blitCommandQueue commandBuffer];
479
}
480
481
-(void)setBufImgOp:(NSObject*)bufImgOp {
482
if (_bufImgOp != nil) {
483
[_bufImgOp release]; // context owns bufImgOp object
484
}
485
_bufImgOp = bufImgOp;
486
}
487
488
-(NSObject*)getBufImgOp {
489
return _bufImgOp;
490
}
491
492
@end
493
494