Fix for an issue in the encoder when using m_state->usingDirectMapping();

This only shows up when we use MoltenVk with the emulator on Macos.
The encoder is not setting the packetlength properly.

Fixing this in the encoder would mean re-spinning all images
that enable Vulkan.

BUG: 297588622
Test: Running vulkan_test_applications/cube
Change-Id: Ifc8dc0c3ce2a21f57bc7acd69d9d0ff967138279
diff --git a/codegen/vulkan/scripts/cereal/decoder.py b/codegen/vulkan/scripts/cereal/decoder.py
index 455cc46..926ea9d 100644
--- a/codegen/vulkan/scripts/cereal/decoder.py
+++ b/codegen/vulkan/scripts/cereal/decoder.py
@@ -513,6 +513,20 @@
     emit_decode_parameters(typeInfo, api, cgen)
 
     cgen.beginIf("!m_state->usingDirectMapping()")
+    cgen.stmt("// This is to deal with a deficiency in the encoder,");
+    cgen.stmt("// where usingDirectMapping fails to set the proper packet size,");
+    cgen.stmt("// meaning we can read off the end of the packet.");
+    cgen.stmt("VkDeviceSize totalMemorySize = 8 * memoryRangeCount;")
+    cgen.beginFor("uint32_t i = 0", "i < memoryRangeCount", "++i")
+    cgen.stmt("totalMemorySize += pMemoryRanges[i].size;")
+    cgen.endFor()
+    cgen.beginIf("(end - *readStreamPtrPtr) < totalMemorySize")
+    cgen.beginIf("m_logCalls")
+    cgen.stmt("fprintf(stderr, \"stream %p: Retrying vkFlushMappedMemoryRanges\\n\", ioStream);")
+    cgen.endIf()
+    cgen.stmt("return ptr - (unsigned char*)buf;")
+    cgen.endIf()
+
     cgen.beginFor("uint32_t i = 0", "i < memoryRangeCount", "++i")
     cgen.stmt("auto range = pMemoryRanges[i]")
     cgen.stmt("auto memory = pMemoryRanges[i].memory")
diff --git a/host/RenderThread.cpp b/host/RenderThread.cpp
index 195ec6a..ef0f1e0 100644
--- a/host/RenderThread.cpp
+++ b/host/RenderThread.cpp
@@ -349,7 +349,7 @@
     auto& metricsLogger = FrameBuffer::getFB()->getMetricsLogger();
 
     const ProcessResources* processResources = nullptr;
-
+    bool anyProgress = false;
     while (true) {
         // Let's make sure we read enough data for at least some processing.
         uint32_t packetSize;
@@ -367,7 +367,11 @@
             // time.
             packetSize = 8;
         }
-
+        if (!anyProgress) {
+            // If we didn't make any progress last time, then make sure we read at least one
+            // extra byte.
+            packetSize = std::max(packetSize, static_cast<uint32_t>(readBuf.validData() + 1));
+        }
         int stat = 0;
         if (packetSize > readBuf.validData()) {
             stat = readBuf.getData(ioStream, packetSize);
@@ -418,9 +422,10 @@
             fflush(dumpFP);
         }
 
-        bool progress;
-
+        bool progress = false;
+        anyProgress = false;
         do {
+            anyProgress |= progress;
             std::unique_ptr<EventHangMetadata::HangAnnotations> renderThreadData =
                 std::make_unique<EventHangMetadata::HangAnnotations>();
 
diff --git a/host/vulkan/VkDecoder.cpp b/host/vulkan/VkDecoder.cpp
index 6a936bf..19701c2 100644
--- a/host/vulkan/VkDecoder.cpp
+++ b/host/vulkan/VkDecoder.cpp
@@ -1896,6 +1896,20 @@
                         (unsigned long long)pMemoryRanges);
                 }
                 if (!m_state->usingDirectMapping()) {
+                    // This is to deal with a deficiency in the encoder,
+                    // where usingDirectMapping fails to set the proper packet size,
+                    // meaning we can read off the end of the packet.
+                    VkDeviceSize totalMemorySize = 8 * memoryRangeCount;
+                    for (uint32_t i = 0; i < memoryRangeCount; ++i) {
+                        totalMemorySize += pMemoryRanges[i].size;
+                    }
+                    if ((end - *readStreamPtrPtr) < totalMemorySize) {
+                        if (m_logCalls) {
+                            fprintf(stderr, "stream %p: Retrying vkFlushMappedMemoryRanges\n", ioStream);
+                        }
+                        return ptr - (unsigned char*)buf;
+                    }
+
                     for (uint32_t i = 0; i < memoryRangeCount; ++i) {
                         auto range = pMemoryRanges[i];
                         auto memory = pMemoryRanges[i].memory;