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/MTLBufImgOps.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 <jlong.h>
27
28
#include "MTLBufImgOps.h"
29
#include "MTLContext.h"
30
#include "MTLRenderQueue.h"
31
#include "MTLSurfaceDataBase.h"
32
#include "GraphicsPrimitiveMgr.h"
33
34
@implementation MTLRescaleOp {
35
jboolean _isNonPremult;
36
jfloat _normScaleFactors[4];
37
jfloat _normOffsets[4];
38
}
39
40
-(jfloat *) getScaleFactors {
41
return _normScaleFactors;
42
}
43
-(jfloat *) getOffsets {
44
return _normOffsets;
45
}
46
47
- (id)init:(jboolean)isNonPremult factors:(unsigned char *)factors offsets:(unsigned char *)offsets {
48
self = [super init];
49
if (self) {
50
J2dTraceLn1(J2D_TRACE_INFO,"Created MTLRescaleOp: isNonPremult=%d", isNonPremult);
51
52
_isNonPremult = isNonPremult;
53
_normScaleFactors[0] = NEXT_FLOAT(factors);
54
_normScaleFactors[1] = NEXT_FLOAT(factors);
55
_normScaleFactors[2] = NEXT_FLOAT(factors);
56
_normScaleFactors[3] = NEXT_FLOAT(factors);
57
_normOffsets[0] = NEXT_FLOAT(offsets);
58
_normOffsets[1] = NEXT_FLOAT(offsets);
59
_normOffsets[2] = NEXT_FLOAT(offsets);
60
_normOffsets[3] = NEXT_FLOAT(offsets);
61
}
62
return self;
63
}
64
65
- (NSString *)getDescription {
66
return [NSString stringWithFormat:@"rescale: nonPremult=%d", _isNonPremult];
67
}
68
@end
69
70
@implementation MTLConvolveOp {
71
id<MTLBuffer> _buffer;
72
float _imgEdge[4];
73
int _kernelSize;
74
jboolean _isEdgeZeroFill;
75
}
76
77
- (id)init:(jboolean)edgeZeroFill kernelWidth:(jint)kernelWidth
78
kernelHeight:(jint)kernelHeight
79
srcWidth:(jint)srcWidth
80
srcHeight:(jint)srcHeight
81
kernel:(unsigned char *)kernel
82
device:(id<MTLDevice>)device {
83
self = [super init];
84
if (self) {
85
J2dTraceLn2(J2D_TRACE_INFO,"Created MTLConvolveOp: kernelW=%d kernelH=%d", kernelWidth, kernelHeight);
86
_isEdgeZeroFill = edgeZeroFill;
87
88
_kernelSize = kernelWidth * kernelHeight;
89
_buffer = [device newBufferWithLength:_kernelSize*sizeof(vector_float3) options:MTLResourceStorageModeShared];
90
91
float * kernelVals = [_buffer contents];
92
int kIndex = 0;
93
for (int i = -kernelHeight/2; i < kernelHeight/2+1; i++) {
94
for (int j = -kernelWidth/2; j < kernelWidth/2+1; j++) {
95
kernelVals[kIndex+0] = j/(float)srcWidth;
96
kernelVals[kIndex+1] = i/(float)srcHeight;
97
kernelVals[kIndex+2] = NEXT_FLOAT(kernel);
98
kIndex += 3;
99
}
100
}
101
102
_imgEdge[0] = (kernelWidth/2)/(float)srcWidth;
103
_imgEdge[1] = (kernelHeight/2)/(float)srcHeight;
104
_imgEdge[2] = 1 - _imgEdge[0];
105
_imgEdge[3] = 1 - _imgEdge[1];
106
}
107
return self;
108
}
109
110
- (void) dealloc {
111
[_buffer release];
112
[super dealloc];
113
}
114
115
- (id<MTLBuffer>) getBuffer {
116
return _buffer;
117
}
118
119
- (const float *) getImgEdge {
120
return _imgEdge;
121
}
122
123
- (NSString *)getDescription {
124
return [NSString stringWithFormat:@"convolve: isEdgeZeroFill=%d", _isEdgeZeroFill];
125
}
126
@end
127
128
129
@implementation MTLLookupOp {
130
float _offset[4];
131
jboolean _isUseSrcAlpha;
132
jboolean _isNonPremult;
133
134
id<MTLTexture> _lookupTex;
135
}
136
137
- (id)init:(jboolean)nonPremult shortData:(jboolean)shortData
138
numBands:(jint)numBands
139
bandLength:(jint)bandLength
140
offset:(jint)offset
141
tableValues:(void *)tableValues
142
device:(id<MTLDevice>)device {
143
self = [super init];
144
if (self) {
145
J2dTraceLn4(J2D_TRACE_INFO,"Created MTLLookupOp: short=%d num=%d len=%d off=%d",
146
shortData, numBands, bandLength, offset);
147
148
_isUseSrcAlpha = numBands != 4;
149
_isNonPremult = nonPremult;
150
151
_offset[0] = offset / 255.0f;
152
_offset[1] = _offset[0];
153
_offset[2] = _offset[0];
154
_offset[3] = _offset[0];
155
156
MTLTextureDescriptor *textureDescriptor =
157
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatA8Unorm
158
width:(NSUInteger)256
159
height:(NSUInteger)4
160
mipmapped:NO];
161
162
_lookupTex = [device newTextureWithDescriptor:textureDescriptor];
163
164
void *bands[4];
165
for (int i = 0; i < 4; i++) {
166
bands[i] = NULL;
167
}
168
int bytesPerElem = (shortData ? 2 : 1);
169
if (numBands == 1) {
170
// replicate the single band for R/G/B; alpha band is unused
171
for (int i = 0; i < 3; i++) {
172
bands[i] = tableValues;
173
}
174
bands[3] = NULL;
175
} else if (numBands == 3) {
176
// user supplied band for each of R/G/B; alpha band is unused
177
for (int i = 0; i < 3; i++) {
178
bands[i] = PtrPixelsBand(tableValues, i, bandLength, bytesPerElem);
179
}
180
bands[3] = NULL;
181
} else if (numBands == 4) {
182
// user supplied band for each of R/G/B/A
183
for (int i = 0; i < 4; i++) {
184
bands[i] = PtrPixelsBand(tableValues, i, bandLength, bytesPerElem);
185
}
186
}
187
188
for (int i = 0; i < 4; i++) {
189
if (bands[i] == NULL)
190
continue;
191
192
MTLRegion region = {
193
{0, i, 0},
194
{bandLength, 1,1}
195
};
196
197
[_lookupTex replaceRegion:region
198
mipmapLevel:0
199
withBytes:bands[i]
200
bytesPerRow:bandLength*bytesPerElem];
201
}
202
}
203
return self;
204
}
205
206
- (void) dealloc {
207
[_lookupTex release];
208
[super dealloc];
209
}
210
211
- (jfloat *) getOffset {
212
return _offset;
213
}
214
215
- (id<MTLTexture>) getLookupTexture {
216
return _lookupTex;
217
}
218
219
- (NSString *)getDescription {
220
return [NSString stringWithFormat:@"lookup: offset=%f", _offset[0]];
221
}
222
223
@end
224
225