Initial state management for Mask Cache with some cleanup metal-prototype-branch
authorjdv
Fri, 19 Jul 2019 15:11:33 +0530
branchmetal-prototype-branch
changeset 57476 64365a5764cd
parent 57475 544abd4b9fd4
child 57477 05637b1896c6
Initial state management for Mask Cache with some cleanup
src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLRenderQueue.h
src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLRenderQueue.m
src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLTextRenderer.m
src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLVertexCache.h
src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLVertexCache.m
--- a/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLRenderQueue.h	Fri Jul 19 14:55:22 2019 +0530
+++ b/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLRenderQueue.h	Fri Jul 19 15:11:33 2019 +0530
@@ -28,6 +28,7 @@
 
 #include "MTLContext.h"
 #include "MTLSurfaceData.h"
+#include "MTLVertexCache.h"
 
 /*
  * The following macros are used to pick values (of the specified type) off
@@ -129,6 +130,5 @@
 MTLContext *MTLRenderQueue_GetCurrentContext();
 BMTLSDOps *MTLRenderQueue_GetCurrentDestination();
 void MTLRenderQueue_CheckPreviousOp(jint op);
-void MTLTR_DisableGlyphModeState();
 
 #endif /* MTLRenderQueue_h_Included */
--- a/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLRenderQueue.m	Fri Jul 19 14:55:22 2019 +0530
+++ b/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLRenderQueue.m	Fri Jul 19 15:11:33 2019 +0530
@@ -801,7 +801,7 @@
             onSurfaceModified(dstOps);
     }
 
-    MTLTR_DisableGlyphModeState();
+    RESET_PREVIOUS_OP();
     scheduleBlitAllModifiedLayers();
 }
 
@@ -833,7 +833,35 @@
 void
 MTLRenderQueue_CheckPreviousOp(jint op)
 {
-    //TODO
+    // TODO : This state management technique is same as OpenGL
+    // and this is used only for Text Rendering. If needed we can extend
+    // to all types.
+    if (previousOp == op) {
+        // The op is the same as last time, so we can return immediately.
+        return;
+    }
+
+    J2dTraceLn1(J2D_TRACE_VERBOSE,
+                "MTLRenderQueue_CheckPreviousOp: new op=%d", op);
+
+    switch (previousOp) {
+    case MTL_STATE_MASK_OP:
+        MTLVertexCache_DisableMaskCache(mtlc);
+        break;
+    case MTL_STATE_GLYPH_OP:
+        MTLTR_DisableGlyphVertexCache(mtlc);
+        break;
+    }
+
+    switch (op) {
+    case MTL_STATE_MASK_OP:
+        MTLVertexCache_EnableMaskCache(mtlc, dstOps);
+        break;
+    case MTL_STATE_GLYPH_OP:
+        MTLTR_EnableGlyphVertexCache(mtlc);
+        break;
+    }
+
     previousOp = op;
 }
 
--- a/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLTextRenderer.m	Fri Jul 19 14:55:22 2019 +0530
+++ b/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLTextRenderer.m	Fri Jul 19 15:11:33 2019 +0530
@@ -256,9 +256,25 @@
 void
 MTLTR_DisableGlyphModeState()
 {
-    //TODO
+    // TODO : This is similar to OpenGL implementation
+    // When LCD implementation is done weshould make
+    // more changes.
     J2dTraceLn1(J2D_TRACE_VERBOSE,
                 "MTLTR_DisableGlyphModeState: mode=%d", glyphMode);
+    switch (glyphMode) {
+    case MODE_NO_CACHE_LCD:
+        // TODO : Along with LCD implementation
+        // changes needs to be made
+    case MODE_USE_CACHE_LCD:
+        // TODO : Along with LCD implementation
+        // changes needs to be made
+        break;
+    case MODE_NO_CACHE_GRAY:
+    case MODE_USE_CACHE_GRAY:
+    case MODE_NOT_INITED:
+    default:
+        break;
+    }
 }
 
 static jboolean
@@ -323,8 +339,8 @@
 
     J2dTraceLn(J2D_TRACE_INFO, "MTLTR_DrawGrayscaleGlyphNoCache");
     if (glyphMode != MODE_NO_CACHE_GRAY) {
-        //OGLTR_DisableGlyphModeState();
-        //CHECK_PREVIOUS_OP(OGL_STATE_MASK_OP);
+        MTLTR_DisableGlyphModeState();
+        CHECK_PREVIOUS_OP(MTL_STATE_MASK_OP);
         glyphMode = MODE_NO_CACHE_GRAY;
     }
 
@@ -396,9 +412,6 @@
     isCachedDestValid = JNI_FALSE;
     J2dTraceLn1(J2D_TRACE_INFO, "totalGlyphs = %d", totalGlyphs);
 
-    MTLVertexCache_CreateSamplingEncoder(mtlc, dstOps);
-    MTLVertexCache_EnableMaskCache(mtlc);
-
     for (glyphCounter = 0; glyphCounter < totalGlyphs; glyphCounter++) {
         J2dTraceLn(J2D_TRACE_INFO, "Entered for loop for glyph list");
         jint x, y;
@@ -492,9 +505,7 @@
         }
     }
 
-    MTLVertexCache_FlushVertexCache(mtlc);
-
-    // TODO : Disable glyph state.
+    MTLTR_DisableGlyphModeState();
 }
 
 JNIEXPORT void JNICALL
@@ -535,12 +546,13 @@
                                 images, NULL);
         }
 
-        // TODO : We are flushing serially as of now
-        // no need for below logic.
-        //if (mtlc != NULL) {
-            //RESET_PREVIOUS_OP();
-            //j2d_glFlush();
-        //}
+        // TODO : We can't flush draw calls here
+        // as it is done while blitting. It needs to
+        // be updated if start flushing right after
+        // rendering
+        if (mtlc != NULL) {
+            RESET_PREVIOUS_OP();
+        }
 
         (*env)->ReleasePrimitiveArrayCritical(env, imgArray,
                                               images, JNI_ABORT);
--- a/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLVertexCache.h	Fri Jul 19 14:55:22 2019 +0530
+++ b/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLVertexCache.h	Fri Jul 19 15:11:33 2019 +0530
@@ -74,7 +74,7 @@
 void MTLVertexCache_FlushVertexCache(MTLContext *mtlc);
 void MTLVertexCache_RestoreColorState(MTLContext *mtlc);
 
-void MTLVertexCache_EnableMaskCache(MTLContext *mtlc);
+void MTLVertexCache_EnableMaskCache(MTLContext *mtlc, BMTLSDOps *dstOps);
 void MTLVertexCache_DisableMaskCache(MTLContext *mtlc);
 void MTLVertexCache_AddMaskQuad(MTLContext *mtlc,
                                 jint srcx, jint srcy,
@@ -84,5 +84,4 @@
                                 BMTLSDOps *dstOps,
                                 jint fullwidth);
 void MTLVertexCache_CreateSamplingEncoder(MTLContext *mtlc, BMTLSDOps *dstOps);
-
 #endif /* MTLVertexCache_h_Included */
--- a/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLVertexCache.m	Fri Jul 19 14:55:22 2019 +0530
+++ b/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLVertexCache.m	Fri Jul 19 15:11:33 2019 +0530
@@ -42,10 +42,9 @@
 static J2DVertex *vertexCache = NULL;
 static jint vertexCacheIndex = 0;
 
-id<MTLTexture> maskCacheTex = NULL;
+static id<MTLTexture> maskCacheTex = NULL;
 static jint maskCacheIndex = 0;
-
-id<MTLRenderCommandEncoder> encoder = NULL;
+static id<MTLRenderCommandEncoder> encoder = NULL;
 
 #define MTLVC_ADD_VERTEX(TX, TY, DX, DY, DZ) \
     do { \
@@ -73,6 +72,7 @@
     J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_InitVertexCache");
 
     if (vertexCache == NULL) {
+        J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_InitVertexCache : vertexCache == NULL");
         vertexCache = (J2DVertex *)malloc(MTLVC_MAX_INDEX * sizeof(J2DVertex));
         if (vertexCache == NULL) {
             return JNI_FALSE;
@@ -87,8 +87,7 @@
 {
     J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_FlushVertexCache");
 
-    if (vertexCacheIndex > 0 ||
-        maskCacheIndex > 0) {
+    if (vertexCacheIndex > 0) {
         id<MTLBuffer>vertexBuffer = [mtlc.device newBufferWithBytes:vertexCache
                                                  length:vertexCacheIndex * sizeof(J2DVertex)
                                                  options:MTLResourceOptionCPUCacheModeDefault];
@@ -99,12 +98,10 @@
             [encoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:i*6 vertexCount:6];
         }
         [encoder endEncoding];
+        encoder = nil;
         vertexBuffer = nil;
     }
     vertexCacheIndex = 0;
-    maskCacheIndex = 0;
-    maskCacheTex = nil;
-    encoder = nil;
 }
 
 /**
@@ -154,13 +151,17 @@
     // TODO : We are creating mask cache only of type MTLPixelFormatA8Unorm
     // when we need more than 1 byte to store a pixel(LCD) we need to update
     // below code.
-    MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatA8Unorm width:MTLVC_MASK_CACHE_WIDTH_IN_TEXELS height:MTLVC_MASK_CACHE_HEIGHT_IN_TEXELS mipmapped:NO];
+    MTLTextureDescriptor *textureDescriptor =
+        [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatA8Unorm
+                                                           width:MTLVC_MASK_CACHE_WIDTH_IN_TEXELS
+                                                          height:MTLVC_MASK_CACHE_HEIGHT_IN_TEXELS
+                                                       mipmapped:NO];
     maskCacheTex = [mtlc.device newTextureWithDescriptor:textureDescriptor];
     return JNI_TRUE;
 }
 
 void
-MTLVertexCache_EnableMaskCache(MTLContext *mtlc)
+MTLVertexCache_EnableMaskCache(MTLContext *mtlc, BMTLSDOps *dstOps)
 {
     J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_EnableMaskCache");
 
@@ -173,14 +174,21 @@
             return;
         }
     }
+    MTLVertexCache_CreateSamplingEncoder(mtlc, dstOps);
 }
 
 void
 MTLVertexCache_DisableMaskCache(MTLContext *mtlc)
 {
-    // TODO
     J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_DisableMaskCache");
+    MTLVertexCache_FlushVertexCache(mtlc);
+    MTLVertexCache_RestoreColorState(mtlc);
+
+    maskCacheTex = nil;
+    encoder = nil;
     maskCacheIndex = 0;
+    free(vertexCache);
+    vertexCache = NULL;
 }
 
 void
@@ -207,13 +215,15 @@
     if (maskCacheIndex >= MTLVC_MASK_CACHE_MAX_INDEX ||
         vertexCacheIndex >= MTLVC_MAX_INDEX)
     {
+        J2dTraceLn2(J2D_TRACE_INFO, "maskCacheIndex = %d, vertexCacheIndex = %d", maskCacheIndex, vertexCacheIndex);
         MTLVertexCache_FlushVertexCache(mtlc);
-        MTLVertexCache_CreateSamplingEncoder(mtlc, dstOps);
+        maskCacheTex = nil;
         // TODO : Since we are not committing command buffer
         // in FlushVertexCache we need to create new maskcache
         // after present cache is full. Check whether we can
         // avoid multiple cache creation.
-        MTLVertexCache_EnableMaskCache(mtlc);
+        MTLVertexCache_EnableMaskCache(mtlc, dstOps);
+        maskCacheIndex = 0;
     }
 
     // TODO : Implement mask == null use case also
@@ -261,6 +271,7 @@
                       mipmapLevel:0
                       withBytes:tile
                       bytesPerRow:bytesPerRow];
+        free(tile);
     }
 
     tx1 = ((jfloat)texx) / MTLVC_MASK_CACHE_WIDTH_IN_TEXELS;