Skip to content
This repository was archived by the owner on Aug 27, 2022. It is now read-only.
/ lanai Public archive

Commit 21d33db

Browse files
committedDec 8, 2020
8256683: Lanai: NetBeans IDE - AA Text rendering appears brighter compared to OpenGL
1 parent cace27d commit 21d33db

File tree

8 files changed

+47
-31
lines changed

8 files changed

+47
-31
lines changed
 

‎src/java.desktop/macosx/native/libawt_lwawt/awt/shaders.metal

+10
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,16 @@ fragment half4 frag_txt(
251251
pixelColor.b, srcA)*uniforms.extraAlpha;
252252
}
253253

254+
fragment half4 frag_text(
255+
TxtShaderInOut vert [[stage_in]],
256+
texture2d<float, access::sample> renderTexture [[texture(0)]],
257+
constant TxtFrameUniforms& uniforms [[buffer(1)]],
258+
sampler textureSampler [[sampler(0)]]
259+
) {
260+
float4 pixelColor = renderTexture.sample(textureSampler, vert.texCoords);
261+
return half4(uniforms.color * pixelColor.a);
262+
}
263+
254264
fragment half4 frag_txt_tp(TxtShaderInOut vert [[stage_in]],
255265
texture2d<float, access::sample> renderTexture [[texture(0)]],
256266
texture2d<float, access::sample> paintTexture [[texture(1)]],

‎src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/EncoderManager.h

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
interpolation:(int)interpolation
5555
isAA:(jboolean)isAA;
5656

57+
- (id<MTLRenderCommandEncoder> _Nonnull)getTextEncoder:(const BMTLSDOps * _Nonnull)dstOps
58+
isSrcOpaque:(bool)isSrcOpaque;
59+
5760
// Base method to obtain any MTLRenderCommandEncoder
5861
- (id<MTLRenderCommandEncoder> _Nonnull) getEncoder:(id<MTLTexture> _Nonnull)dest
5962
isDestOpaque:(jboolean)isDestOpaque

‎src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/EncoderManager.m

+22-6
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ - (void)dealloc;
1818
- (void)reset:(id<MTLTexture>)destination
1919
isDstOpaque:(jboolean)isDstOpaque
2020
isDstPremultiplied:(jboolean)isDstPremultiplied
21-
isAA:(jboolean)isAA;
21+
isAA:(jboolean)isAA
22+
isText:(jboolean)isText;
2223

2324
- (void)updateEncoder:(id<MTLRenderCommandEncoder>)encoder
2425
context:(MTLContext *)mtlc
2526
renderOptions:(const RenderOptions *)renderOptions
2627
forceUpdate:(jboolean)forceUpdate;
2728
@property (assign) jboolean aa;
29+
@property (assign) jboolean text;
2830
@property (retain) MTLPaint* paint;
2931
@end
3032

@@ -37,6 +39,7 @@ @implementation EncoderStates {
3739
SurfaceRasterFlags _dstFlags;
3840

3941
jboolean _isAA;
42+
jboolean _isText;
4043

4144
//
4245
// Cached 'mutable' states of encoder
@@ -60,6 +63,7 @@ @implementation EncoderStates {
6063
MTLTransform * _transform;
6164
}
6265
@synthesize aa = _isAA;
66+
@synthesize text = _isText;
6367
@synthesize paint = _paint;
6468

6569
- (id)init {
@@ -89,11 +93,13 @@ - (void)setContext:(MTLContext * _Nonnull)mtlc {
8993
- (void)reset:(id<MTLTexture>)destination
9094
isDstOpaque:(jboolean)isDstOpaque
9195
isDstPremultiplied:(jboolean)isDstPremultiplied
92-
isAA:(jboolean)isAA {
96+
isAA:(jboolean)isAA
97+
isText:(jboolean)isText {
9398
_destination = destination;
9499
_dstFlags.isOpaque = isDstOpaque;
95100
_dstFlags.isPremultiplied = isDstPremultiplied;
96101
_isAA = isAA;
102+
_isText = isText;
97103
// NOTE: probably it's better to invalidate/reset all cached states now
98104
}
99105

@@ -141,6 +147,7 @@ - (void)updatePipelineState:(id<MTLRenderCommandEncoder>)encoder
141147
&& [_composite isEqual:mtlc.composite]
142148
&& (_isTexture == renderOptions->isTexture && (!renderOptions->isTexture || _interpolationMode == renderOptions->interpolation)) // interpolation is used only in texture mode
143149
&& _isAA == renderOptions->isAA
150+
&& _isText == renderOptions->isText
144151
&& _srcFlags.isOpaque == renderOptions->srcFlags.isOpaque && _srcFlags.isPremultiplied == renderOptions->srcFlags.isPremultiplied)
145152
return;
146153

@@ -149,6 +156,7 @@ - (void)updatePipelineState:(id<MTLRenderCommandEncoder>)encoder
149156
_isTexture = renderOptions->isTexture;
150157
_interpolationMode = renderOptions->interpolation;
151158
_isAA = renderOptions->isAA;
159+
_isText = renderOptions->isText;
152160
_srcFlags = renderOptions->srcFlags;
153161

154162
if ((jint)[mtlc.composite getCompositeState] == sun_java2d_SunGraphics2D_COMP_XOR) {
@@ -242,14 +250,14 @@ - (void)setContext:(MTLContex * _Nonnull)mtlc {
242250

243251
- (id<MTLRenderCommandEncoder> _Nonnull)getAARenderEncoder:(const BMTLSDOps * _Nonnull)dstOps {
244252
id<MTLTexture> dstTxt = dstOps->pTexture;
245-
RenderOptions roptions = {JNI_FALSE, JNI_TRUE, INTERPOLATION_NEAREST_NEIGHBOR, defaultRasterFlags, {dstOps->isOpaque, JNI_TRUE}};
253+
RenderOptions roptions = {JNI_FALSE, JNI_TRUE, INTERPOLATION_NEAREST_NEIGHBOR, defaultRasterFlags, {dstOps->isOpaque, JNI_TRUE}, JNI_FALSE};
246254
return [self getEncoder:dstTxt renderOptions:&roptions];
247255
}
248256

249257
- (id<MTLRenderCommandEncoder> _Nonnull)getRenderEncoder:(id<MTLTexture> _Nonnull)dest
250258
isDstOpaque:(bool)isOpaque
251259
{
252-
RenderOptions roptions = {JNI_FALSE, JNI_FALSE, INTERPOLATION_NEAREST_NEIGHBOR, defaultRasterFlags, {isOpaque, JNI_TRUE}};
260+
RenderOptions roptions = {JNI_FALSE, JNI_FALSE, INTERPOLATION_NEAREST_NEIGHBOR, defaultRasterFlags, {isOpaque, JNI_TRUE}, JNI_FALSE};
253261
return [self getEncoder:dest renderOptions:&roptions];
254262
}
255263

@@ -279,7 +287,7 @@ - (void)setContext:(MTLContex * _Nonnull)mtlc {
279287
interpolation:(int)interpolation
280288
isAA:(jboolean)isAA
281289
{
282-
RenderOptions roptions = {JNI_TRUE, isAA, interpolation, { isSrcOpaque, JNI_TRUE }, {isDstOpaque, JNI_TRUE}};
290+
RenderOptions roptions = {JNI_TRUE, isAA, interpolation, { isSrcOpaque, JNI_TRUE }, {isDstOpaque, JNI_TRUE}, JNI_FALSE};
283291
return [self getEncoder:dest renderOptions:&roptions];
284292
}
285293

@@ -291,6 +299,13 @@ - (void)setContext:(MTLContex * _Nonnull)mtlc {
291299
return [self getTextureEncoder:dest isSrcOpaque:isSrcOpaque isDstOpaque:isDstOpaque interpolation:interpolation isAA:JNI_FALSE];
292300
}
293301

302+
- (id<MTLRenderCommandEncoder> _Nonnull) getTextEncoder:(const BMTLSDOps * _Nonnull)dstOps
303+
isSrcOpaque:(bool)isSrcOpaque
304+
{
305+
RenderOptions roptions = {JNI_TRUE, JNI_FALSE, INTERPOLATION_NEAREST_NEIGHBOR, { isSrcOpaque, JNI_TRUE }, {dstOps->isOpaque, JNI_TRUE}, JNI_TRUE};
306+
return [self getEncoder:dstOps->pTexture renderOptions:&roptions];
307+
}
308+
294309
- (id<MTLRenderCommandEncoder> _Nonnull) getEncoder:(id <MTLTexture> _Nonnull)dest
295310
renderOptions:(const RenderOptions * _Nonnull)renderOptions
296311
{
@@ -358,7 +373,8 @@ - (void)setContext:(MTLContex * _Nonnull)mtlc {
358373
[_encoderStates reset:dest
359374
isDstOpaque:renderOptions->dstFlags.isOpaque
360375
isDstPremultiplied:YES
361-
isAA:renderOptions->isAA];
376+
isAA:renderOptions->isAA
377+
isText:renderOptions->isText];
362378
}
363379

364380
//

‎src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLPaints.m

+3
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ - (void)setPipelineState:(id<MTLRenderCommandEncoder>)encoder
161161
fragShader = @"aa_frag_txt";
162162
rpDesc = [[templateAATexturePipelineDesc copy] autorelease];
163163
}
164+
if (renderOptions->isText) {
165+
fragShader = @"frag_text";
166+
}
164167
setTxtUniforms(encoder, _color, 1,
165168
renderOptions->interpolation, NO, [mtlc.composite getExtraAlpha], &renderOptions->srcFlags,
166169
&renderOptions->dstFlags);

‎src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLPipelineStatesStorage.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ - (NSPointerArray * ) getSubStates:(NSString *)vertexShaderId fragmentShader:(NS
5959
vertexShaderId:(NSString *)vertexShaderId
6060
fragmentShaderId:(NSString *)fragmentShaderId
6161
{
62-
RenderOptions defaultOptions = {JNI_FALSE, JNI_FALSE, 0/*unused*/, {JNI_FALSE, JNI_TRUE}, {JNI_FALSE, JNI_TRUE}};
62+
RenderOptions defaultOptions = {JNI_FALSE, JNI_FALSE, 0/*unused*/, {JNI_FALSE, JNI_TRUE}, {JNI_FALSE, JNI_TRUE}, JNI_FALSE};
6363
return [self getPipelineState:pipelineDescriptor
6464
vertexShaderId:vertexShaderId
6565
fragmentShaderId:fragmentShaderId

‎src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLTextRenderer.m

+4-23
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@
132132
{
133133
J2dTraceLn(J2D_TRACE_INFO, "MTLTR_InitGlyphCache");
134134
// TODO : Need to fix RGB order in case of LCD
135-
MTLPixelFormat pixelFormat = MTLPixelFormatBGRA8Unorm;
135+
MTLPixelFormat pixelFormat =
136+
lcdCache ? MTLPixelFormatBGRA8Unorm : MTLPixelFormatA8Unorm;
136137

137138
MTLGlyphCacheInfo *gcinfo;
138139
// init glyph cache data structure
@@ -220,30 +221,10 @@
220221
{w, h, 1}
221222
};
222223
if (!lcdCache) {
223-
// Opengl uses GL_INTENSITY as internal pixel format to set number of
224-
// color components in the texture for grayscale texture.
225-
// It is mentioned that for GL_INTENSITY format,
226-
// the GL assembles it into an RGBA element by replicating the
227-
// intensity value three times for red, green, blue, and alpha.
228-
// To let metal behave the same for grayscale text,
229-
// we need to make sure we create BGRA component by replicating
230-
// graycale pixel value as in R=G=B=A=grayscale pixel value
231-
232-
unsigned int imageBytes = w * h * 4;
233-
unsigned char imageData[imageBytes];
234-
memset(&imageData, 0, sizeof(imageData));
235-
236-
unsigned int dstindex = 0;
237-
for (int i = 0; i < (w * h); i++) {
238-
imageData[dstindex++] = glyph->image[i];
239-
imageData[dstindex++] = glyph->image[i];
240-
imageData[dstindex++] = glyph->image[i];
241-
imageData[dstindex++] = glyph->image[i];
242-
}
243-
NSUInteger bytesPerRow = 4 * w;
224+
NSUInteger bytesPerRow = 1 * w;
244225
[gcinfo->texture replaceRegion:region
245226
mipmapLevel:0
246-
withBytes:imageData
227+
withBytes:glyph->image
247228
bytesPerRow:bytesPerRow];
248229
} else {
249230
unsigned int imageBytes = w * h * 4;

‎src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLVertexCache.m

+3-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,9 @@ void MTLVertexCache_FreeVertexCache()
247247
void
248248
MTLVertexCache_CreateSamplingEncoder(MTLContext *mtlc, BMTLSDOps *dstOps) {
249249
J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_CreateSamplingEncoder");
250-
encoder = [mtlc.encoderManager getTextureEncoder:dstOps isSrcOpaque:NO];
250+
//encoder = [mtlc.encoderManager getTextureEncoder:dstOps isSrcOpaque:NO];
251+
encoder = [mtlc.encoderManager getTextEncoder:dstOps
252+
isSrcOpaque:NO];
251253
}
252254

253255
void

‎src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/RenderOptions.h

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ typedef struct {
1111
int interpolation;
1212
SurfaceRasterFlags srcFlags;
1313
SurfaceRasterFlags dstFlags;
14+
jboolean isText;
1415
} RenderOptions;
1516

1617

0 commit comments

Comments
 (0)
This repository has been archived.