Skip to content
This repository has been archived by the owner on Aug 27, 2022. It is now read-only.

Commit

Permalink
8233314: LCD Text rendering implementation with glyph cache
Browse files Browse the repository at this point in the history
  • Loading branch information
prsadhuk committed Mar 4, 2020
1 parent bc73e00 commit f66a1f1
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 25 deletions.
Expand Up @@ -359,7 +359,6 @@ public final int getType() {
*/ */
public boolean canRenderLCDText(SunGraphics2D sg2d) { public boolean canRenderLCDText(SunGraphics2D sg2d) {
return return
graphicsConfig.isCapPresent(CAPS_EXT_LCD_SHADER) &&
sg2d.surfaceData.getTransparency() == Transparency.OPAQUE && sg2d.surfaceData.getTransparency() == Transparency.OPAQUE &&
sg2d.paintState <= SunGraphics2D.PAINT_OPAQUECOLOR && sg2d.paintState <= SunGraphics2D.PAINT_OPAQUECOLOR &&
(sg2d.compositeState <= SunGraphics2D.COMP_ISCOPY || (sg2d.compositeState <= SunGraphics2D.COMP_ISCOPY ||
Expand Down
Expand Up @@ -165,6 +165,28 @@
*/ */
static SurfaceDataBounds previousGlyphBounds; static SurfaceDataBounds previousGlyphBounds;


static struct TxtVertex txtVertices[6];
static jint vertexCacheIndex = 0;

#define LCD_ADD_VERTEX(TX, TY, DX, DY, DZ) \
do { \
struct TxtVertex *v = &txtVertices[vertexCacheIndex++]; \
v->txtpos[0] = TX; \
v->txtpos[1] = TY; \
v->position[0]= DX; \
v->position[1] = DY; \
} while (0)

#define LCD_ADD_TRIANGLES(TX1, TY1, TX2, TY2, DX1, DY1, DX2, DY2) \
do { \
LCD_ADD_VERTEX(TX1, TY1, DX1, DY1, 0); \
LCD_ADD_VERTEX(TX2, TY1, DX2, DY1, 0); \
LCD_ADD_VERTEX(TX2, TY2, DX2, DY2, 0); \
LCD_ADD_VERTEX(TX2, TY2, DX2, DY2, 0); \
LCD_ADD_VERTEX(TX1, TY2, DX1, DY2, 0); \
LCD_ADD_VERTEX(TX1, TY1, DX1, DY1, 0); \
} while (0)

/** /**
* Initializes the one glyph cache (texture and data structure). * Initializes the one glyph cache (texture and data structure).
* If lcdCache is JNI_TRUE, the texture will contain RGB data, * If lcdCache is JNI_TRUE, the texture will contain RGB data,
Expand Down Expand Up @@ -477,15 +499,32 @@
} }


static jboolean static jboolean
MTLTR_DrawLCDGlyphViaCache(MTLContext *mtlc, MTLSDOps *dstOps, MTLTR_DrawLCDGlyphViaCache(MTLContext *mtlc, BMTLSDOps *dstOps,
GlyphInfo *ginfo, jint x, jint y, GlyphInfo *ginfo, jint x, jint y,
jint glyphIndex, jint totalGlyphs, jint glyphIndex, jint totalGlyphs,
jboolean rgbOrder, jint contrast, jboolean rgbOrder, jint contrast,
id<MTLTexture> dstTexture) id<MTLTexture> dstTexture)
{ {
CacheCellInfo *cell; CacheCellInfo *cell;
jint dx1, dy1, dx2, dy2; jfloat tx1, ty1, tx2, ty2;
jfloat dtx1, dty1, dtx2, dty2; jfloat dtx1=0, dty1=0, dtx2=0, dty2=0;
jint tw, th;
jint sx=0, sy=0, sw=0, sh=0, dxadj=0, dyadj=0;
jint x0;
jint w = ginfo->width;
jint h = ginfo->height;
id<MTLTexture> blitTexture = nil;

id<MTLRenderCommandEncoder> encoder = nil;

MTLTextureDescriptor *textureDescriptor =
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
width:w
height:h
mipmapped:NO];

blitTexture = [mtlc.device newTextureWithDescriptor:textureDescriptor];
[textureDescriptor release];


if (glyphMode != MODE_USE_CACHE_LCD) { if (glyphMode != MODE_USE_CACHE_LCD) {
if (glyphMode == MODE_NO_CACHE_GRAY) { if (glyphMode == MODE_NO_CACHE_GRAY) {
Expand Down Expand Up @@ -520,6 +559,84 @@
return JNI_TRUE; return JNI_TRUE;
} }
} }
encoder = [mtlc.encoderManager getTextureEncoder:dstOps->pTexture isSrcOpaque:YES isDstOpaque:YES];
if (!MTLTR_EnableLCDGlyphModeState(encoder, mtlc, dstOps,contrast))
{
return JNI_FALSE;
}


unsigned int imageBytes = w * h *4;
unsigned char imageData[imageBytes];
memset(&imageData, 0, sizeof(imageData));

for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
imageData[(i * w * 4) + j * 4] = ginfo->image[(i * w * 3) + j * 3];
imageData[(i * w * 4) + j * 4 + 1] = ginfo->image[(i * w * 3) + j * 3 + 1];
imageData[(i * w * 4) + j * 4 + 2] = ginfo->image[(i * w * 3) + j * 3 + 2];
imageData[(i * w * 4) + j * 4 + 3] = 0xFF;
}
}

// copy LCD mask into glyph texture tile
MTLRegion region = MTLRegionMake2D(0, 0, w, h);

NSUInteger bytesPerRow = 4 * ginfo->width;
[blitTexture replaceRegion:region
mipmapLevel:0
withBytes:imageData
bytesPerRow:bytesPerRow];

J2dTraceLn7(J2D_TRACE_INFO, "sx = %d sy = %d x = %d y = %d sw = %d sh = %d w = %d", sx, sy, x, y, sw, sh, w);

x0 = x;
tx1 = 0.0f;
ty1 = 0.0f;
dtx1 = 0.0f;
dty2 = 0.0f;
tw = MTLTR_NOCACHE_TILE_SIZE;
th = MTLTR_NOCACHE_TILE_SIZE;

// update the lower-right glyph texture coordinates
tx2 = 1.0f;
ty2 = 1.0f;

J2dTraceLn5(J2D_TRACE_INFO, "xOffset %d yOffset %d, dxadj %d, dyadj %d dstOps->height %d", dstOps->xOffset, dstOps->yOffset, dxadj, dyadj, dstOps->height);

dtx1 = ((jfloat)dxadj) / dstOps->textureWidth;
dtx2 = ((float)dxadj + sw) / dstOps->textureWidth;

dty1 = ((jfloat)dyadj + sh) / dstOps->textureHeight;
dty2 = ((jfloat)dyadj) / dstOps->textureHeight;

J2dTraceLn4(J2D_TRACE_INFO, "tx1 %f, ty1 %f, tx2 %f, ty2 %f", tx1, ty1, tx2, ty2);
J2dTraceLn2(J2D_TRACE_INFO, "textureWidth %d textureHeight %d", dstOps->textureWidth, dstOps->textureHeight);
J2dTraceLn4(J2D_TRACE_INFO, "dtx1 %f, dty1 %f, dtx2 %f, dty2 %f", dtx1, dty1, dtx2, dty2);

LCD_ADD_TRIANGLES(tx1, ty1, tx2, ty2, x, y, x+w, y+h);

[encoder setVertexBytes:txtVertices length:sizeof(txtVertices) atIndex:MeshVertexBuffer];
[encoder setFragmentTexture:blitTexture atIndex:0];
[encoder setFragmentTexture:dstOps->pTexture atIndex:1];

[encoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:6];

vertexCacheIndex = 0;
[mtlc.encoderManager endEncoder];

[blitTexture release];

MTLCommandBufferWrapper* cbwrapper = [mtlc pullCommandBufferWrapper];

id<MTLCommandBuffer> commandbuf = [cbwrapper getCommandBuffer];
[commandbuf addCompletedHandler:^(id <MTLCommandBuffer> commandbuf) {
[cbwrapper release];
}];

[commandbuf commit];
[commandbuf waitUntilCompleted];

// TODO : Updating cache bounds and texture mapping. // TODO : Updating cache bounds and texture mapping.
return JNI_TRUE; return JNI_TRUE;
} }
Expand Down Expand Up @@ -566,27 +683,6 @@
return JNI_TRUE; return JNI_TRUE;
} }


static struct TxtVertex txtVertices[6];
static jint vertexCacheIndex = 0;

#define LCD_ADD_VERTEX(TX, TY, DX, DY, DZ) \
do { \
struct TxtVertex *v = &txtVertices[vertexCacheIndex++]; \
v->txtpos[0] = TX; \
v->txtpos[1] = TY; \
v->position[0]= DX; \
v->position[1] = DY; \
} while (0)

#define LCD_ADD_TRIANGLES(TX1, TY1, TX2, TY2, DX1, DY1, DX2, DY2) \
do { \
LCD_ADD_VERTEX(TX1, TY1, DX1, DY1, 0); \
LCD_ADD_VERTEX(TX2, TY1, DX2, DY1, 0); \
LCD_ADD_VERTEX(TX2, TY2, DX2, DY2, 0); \
LCD_ADD_VERTEX(TX2, TY2, DX2, DY2, 0); \
LCD_ADD_VERTEX(TX1, TY2, DX1, DY2, 0); \
LCD_ADD_VERTEX(TX1, TY1, DX1, DY1, 0); \
} while (0)


static jboolean static jboolean
MTLTR_DrawLCDGlyphNoCache(MTLContext *mtlc, BMTLSDOps *dstOps, MTLTR_DrawLCDGlyphNoCache(MTLContext *mtlc, BMTLSDOps *dstOps,
Expand Down

0 comments on commit f66a1f1

Please sign in to comment.