Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLVertexCache.m
66646 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
#include <string.h>
28
29
#include "sun_java2d_SunGraphics2D.h"
30
31
#include "MTLPaints.h"
32
#include "MTLVertexCache.h"
33
#include "MTLTexturePool.h"
34
#include "MTLTextRenderer.h"
35
#include "common.h"
36
37
typedef struct _J2DVertex {
38
float position[2];
39
float txtpos[2];
40
} J2DVertex;
41
42
static J2DVertex *vertexCache = NULL;
43
static jint vertexCacheIndex = 0;
44
45
static MTLPooledTextureHandle * maskCacheTex = NULL;
46
static jint maskCacheIndex = 0;
47
static id<MTLRenderCommandEncoder> encoder = NULL;
48
49
#define MTLVC_ADD_VERTEX(TX, TY, DX, DY, DZ) \
50
do { \
51
J2DVertex *v = &vertexCache[vertexCacheIndex++]; \
52
v->txtpos[0] = TX; \
53
v->txtpos[1] = TY; \
54
v->position[0]= DX; \
55
v->position[1] = DY; \
56
} while (0)
57
58
#define MTLVC_ADD_TRIANGLES(TX1, TY1, TX2, TY2, DX1, DY1, DX2, DY2) \
59
do { \
60
MTLVC_ADD_VERTEX(TX1, TY1, DX1, DY1, 0); \
61
MTLVC_ADD_VERTEX(TX2, TY1, DX2, DY1, 0); \
62
MTLVC_ADD_VERTEX(TX2, TY2, DX2, DY2, 0); \
63
MTLVC_ADD_VERTEX(TX2, TY2, DX2, DY2, 0); \
64
MTLVC_ADD_VERTEX(TX1, TY2, DX1, DY2, 0); \
65
MTLVC_ADD_VERTEX(TX1, TY1, DX1, DY1, 0); \
66
} while (0)
67
68
// Next define should exactly match to the amount
69
// of MTLVC_ADD_VERTEX in MTLVC_ADD_TRIANGLES
70
#define VERTS_FOR_A_QUAD 6
71
72
jboolean
73
MTLVertexCache_InitVertexCache()
74
{
75
J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_InitVertexCache");
76
77
if (vertexCache == NULL) {
78
J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_InitVertexCache : vertexCache == NULL");
79
vertexCache = (J2DVertex *)malloc(MTLVC_MAX_INDEX * sizeof(J2DVertex));
80
if (vertexCache == NULL) {
81
return JNI_FALSE;
82
}
83
}
84
85
return JNI_TRUE;
86
}
87
88
void
89
MTLVertexCache_FlushVertexCache(MTLContext *mtlc)
90
{
91
J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_FlushVertexCache");
92
93
if (vertexCacheIndex > 0) {
94
[encoder setVertexBytes: vertexCache length:vertexCacheIndex * sizeof(J2DVertex)
95
atIndex:MeshVertexBuffer];
96
97
[encoder setFragmentTexture:maskCacheTex.texture atIndex: 0];
98
J2dTraceLn1(J2D_TRACE_INFO,
99
"MTLVertexCache_FlushVertexCache : encode %d characters", (vertexCacheIndex / 6));
100
[encoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:vertexCacheIndex];
101
}
102
vertexCacheIndex = 0;
103
maskCacheIndex = 0;
104
105
if (maskCacheTex != nil) {
106
[[mtlc getCommandBufferWrapper] registerPooledTexture:maskCacheTex];
107
[maskCacheTex release];
108
maskCacheTex = nil;
109
}
110
}
111
112
void
113
MTLVertexCache_FlushGlyphVertexCache()
114
{
115
J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_FlushGlyphVertexCache");
116
117
if (vertexCacheIndex > 0) {
118
[encoder setVertexBytes: vertexCache length:vertexCacheIndex * sizeof(J2DVertex)
119
atIndex:MeshVertexBuffer];
120
id<MTLTexture> glyphCacheTex = MTLTR_GetGlyphCacheTexture();
121
[encoder setFragmentTexture:glyphCacheTex atIndex: 0];
122
J2dTraceLn1(J2D_TRACE_INFO,
123
"MTLVertexCache_FlushGlyphVertexCache : encode %d characters", (vertexCacheIndex / 6));
124
[encoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:vertexCacheIndex];
125
}
126
vertexCacheIndex = 0;
127
}
128
129
void MTLVertexCache_FreeVertexCache()
130
{
131
free(vertexCache);
132
vertexCache = NULL;
133
}
134
135
static jboolean
136
MTLVertexCache_InitMaskCache(MTLContext *mtlc) {
137
J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_InitMaskCache");
138
if (maskCacheTex == NULL) {
139
maskCacheTex = [mtlc.texturePool getTexture:MTLVC_MASK_CACHE_WIDTH_IN_TEXELS
140
height:MTLVC_MASK_CACHE_HEIGHT_IN_TEXELS
141
format:MTLPixelFormatA8Unorm];
142
[maskCacheTex retain];
143
if (maskCacheTex == nil) {
144
J2dTraceLn(J2D_TRACE_ERROR, "MTLVertexCache_InitMaskCache: can't obtain temporary texture object from pool");
145
return JNI_FALSE;
146
}
147
}
148
// init special fully opaque tile in the upper-right corner of
149
// the mask cache texture
150
151
char tile[MTLVC_MASK_CACHE_TILE_SIZE];
152
memset(tile, 0xff, MTLVC_MASK_CACHE_TILE_SIZE);
153
154
jint texx = MTLVC_MASK_CACHE_TILE_WIDTH * (MTLVC_MASK_CACHE_WIDTH_IN_TILES - 1);
155
156
jint texy = MTLVC_MASK_CACHE_TILE_HEIGHT * (MTLVC_MASK_CACHE_HEIGHT_IN_TILES - 1);
157
158
NSUInteger bytesPerRow = 1 * MTLVC_MASK_CACHE_TILE_WIDTH;
159
160
MTLRegion region = {
161
{texx, texy, 0},
162
{MTLVC_MASK_CACHE_TILE_WIDTH, MTLVC_MASK_CACHE_TILE_HEIGHT, 1}
163
};
164
165
166
// do we really need this??
167
[maskCacheTex.texture replaceRegion:region
168
mipmapLevel:0
169
withBytes:tile
170
bytesPerRow:bytesPerRow];
171
172
return JNI_TRUE;
173
}
174
175
void
176
MTLVertexCache_EnableMaskCache(MTLContext *mtlc, BMTLSDOps *dstOps)
177
{
178
J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_EnableMaskCache");
179
180
if (!MTLVertexCache_InitVertexCache()) {
181
return;
182
}
183
184
if (maskCacheTex == NULL) {
185
if (!MTLVertexCache_InitMaskCache(mtlc)) {
186
return;
187
}
188
}
189
MTLVertexCache_CreateSamplingEncoder(mtlc, dstOps);
190
}
191
192
void
193
MTLVertexCache_DisableMaskCache(MTLContext *mtlc)
194
{
195
// TODO : Once we enable check_previous_op
196
// we will start using DisableMaskCache until then
197
// we are force flushing vertexcache.
198
J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_DisableMaskCache");
199
MTLVertexCache_FlushVertexCache(mtlc);
200
maskCacheIndex = 0;
201
free(vertexCache);
202
vertexCache = NULL;
203
}
204
205
void
206
MTLVertexCache_CreateSamplingEncoder(MTLContext *mtlc, BMTLSDOps *dstOps) {
207
J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_CreateSamplingEncoder");
208
encoder = [mtlc.encoderManager getTextEncoder:dstOps
209
isSrcOpaque:NO];
210
}
211
212
void
213
MTLVertexCache_AddMaskQuad(MTLContext *mtlc,
214
jint srcx, jint srcy,
215
jint dstx, jint dsty,
216
jint width, jint height,
217
jint maskscan, void *mask,
218
BMTLSDOps *dstOps)
219
{
220
jfloat tx1, ty1, tx2, ty2;
221
jfloat dx1, dy1, dx2, dy2;
222
223
J2dTraceLn1(J2D_TRACE_INFO, "MTLVertexCache_AddMaskQuad: %d",
224
maskCacheIndex);
225
226
// MTLVC_ADD_TRIANGLES at the end of this function
227
// will place VERTS_FOR_A_QUAD vertexes to the vertex cache
228
// check free space and flush if needed.
229
if ((maskCacheIndex >= MTLVC_MASK_CACHE_MAX_INDEX) ||
230
((vertexCacheIndex + VERTS_FOR_A_QUAD) >= MTLVC_MAX_INDEX))
231
{
232
J2dTraceLn2(J2D_TRACE_INFO, "maskCacheIndex = %d, vertexCacheIndex = %d", maskCacheIndex, vertexCacheIndex);
233
MTLVertexCache_FlushVertexCache(mtlc);
234
MTLVertexCache_EnableMaskCache(mtlc, dstOps);
235
maskCacheIndex = 0;
236
}
237
238
if (mask != NULL) {
239
jint texx = MTLVC_MASK_CACHE_TILE_WIDTH *
240
(maskCacheIndex % MTLVC_MASK_CACHE_WIDTH_IN_TILES);
241
jint texy = MTLVC_MASK_CACHE_TILE_HEIGHT *
242
(maskCacheIndex / MTLVC_MASK_CACHE_WIDTH_IN_TILES);
243
J2dTraceLn5(J2D_TRACE_INFO, "texx = %d texy = %d width = %d height = %d maskscan = %d", texx, texy, width,
244
height, maskscan);
245
NSUInteger bytesPerRow = 1 * width;
246
NSUInteger slice = bytesPerRow * srcy + srcx;
247
MTLRegion region = {
248
{texx, texy, 0},
249
{width, height, 1}
250
};
251
252
// Whenever we have source stride bigger that destination stride
253
// we need to pick appropriate source subtexture. In repalceRegion
254
// we can give destination subtexturing properly but we can't
255
// subtexture from system memory glyph we have. So in such
256
// cases we are creating seperate tile and scan the source
257
// stride into destination using memcpy. In case of OpenGL we
258
// can update source pointers, in case of D3D we ar doing memcpy.
259
// We can use MTLBuffer and then copy source subtexture but that
260
// adds extra blitting logic.
261
// TODO : Research more and try removing memcpy logic.
262
if (maskscan <= width) {
263
int height_offset = bytesPerRow * srcy;
264
[maskCacheTex.texture replaceRegion:region
265
mipmapLevel:0
266
withBytes:mask + height_offset
267
bytesPerRow:bytesPerRow];
268
} else {
269
int dst_offset, src_offset;
270
int size = 1 * width * height;
271
char tile[size];
272
dst_offset = 0;
273
for (int i = srcy; i < srcy + height; i++) {
274
J2dTraceLn2(J2D_TRACE_INFO, "srcx = %d srcy = %d", srcx, srcy);
275
src_offset = maskscan * i + srcx;
276
J2dTraceLn2(J2D_TRACE_INFO, "src_offset = %d dst_offset = %d", src_offset, dst_offset);
277
memcpy(tile + dst_offset, mask + src_offset, width);
278
dst_offset = dst_offset + width;
279
}
280
[maskCacheTex.texture replaceRegion:region
281
mipmapLevel:0
282
withBytes:tile
283
bytesPerRow:bytesPerRow];
284
}
285
286
tx1 = ((jfloat) texx) / MTLVC_MASK_CACHE_WIDTH_IN_TEXELS;
287
ty1 = ((jfloat) texy) / MTLVC_MASK_CACHE_HEIGHT_IN_TEXELS;
288
} else {
289
tx1 = ((jfloat)MTLVC_MASK_CACHE_SPECIAL_TILE_X) /
290
MTLVC_MASK_CACHE_WIDTH_IN_TEXELS;
291
ty1 = ((jfloat)MTLVC_MASK_CACHE_SPECIAL_TILE_Y) /
292
MTLVC_MASK_CACHE_HEIGHT_IN_TEXELS;
293
}
294
maskCacheIndex++;
295
296
tx2 = tx1 + (((jfloat)width) / MTLVC_MASK_CACHE_WIDTH_IN_TEXELS);
297
ty2 = ty1 + (((jfloat)height) / MTLVC_MASK_CACHE_HEIGHT_IN_TEXELS);
298
299
dx1 = (jfloat)dstx;
300
dy1 = (jfloat)dsty;
301
dx2 = dx1 + width;
302
dy2 = dy1 + height;
303
304
J2dTraceLn8(J2D_TRACE_INFO, "tx1 = %f ty1 = %f tx2 = %f ty2 = %f dx1 = %f dy1 = %f dx2 = %f dy2 = %f", tx1, ty1, tx2, ty2, dx1, dy1, dx2, dy2);
305
MTLVC_ADD_TRIANGLES(tx1, ty1, tx2, ty2,
306
dx1, dy1, dx2, dy2);
307
}
308
309
void
310
MTLVertexCache_AddGlyphQuad(MTLContext *mtlc,
311
jfloat tx1, jfloat ty1, jfloat tx2, jfloat ty2,
312
jfloat dx1, jfloat dy1, jfloat dx2, jfloat dy2)
313
{
314
J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_AddGlyphQuad");
315
316
// MTLVC_ADD_TRIANGLES adds VERTS_FOR_A_QUAD vertexes into Cache
317
// so need to check space for VERTS_FOR_A_QUAD elements
318
if ((vertexCacheIndex + VERTS_FOR_A_QUAD) >= MTLVC_MAX_INDEX)
319
{
320
J2dTraceLn2(J2D_TRACE_INFO, "maskCacheIndex = %d, vertexCacheIndex = %d", maskCacheIndex, vertexCacheIndex);
321
MTLVertexCache_FlushGlyphVertexCache();
322
}
323
324
MTLVC_ADD_TRIANGLES(tx1, ty1, tx2, ty2,
325
dx1, dy1, dx2, dy2);
326
}
327
328