Fix vulkan layout barriers when we are doing in line uploads.

Bug: skia:
Change-Id: I0842a4eebba8cfa8310d0680941d59f1e363e574
Reviewed-on: https://skia-review.googlesource.com/114760
Reviewed-by: Jim Van Verth <jvanverth@google.com>
Commit-Queue: Greg Daniel <egdaniel@google.com>
(cherry picked from commit ea022cd714b206551957fafd7f56e489bb12b128)
Reviewed-on: https://skia-review.googlesource.com/114802
Reviewed-by: Hal Canary <halcanary@google.com>
diff --git a/src/gpu/vk/GrVkGpuCommandBuffer.cpp b/src/gpu/vk/GrVkGpuCommandBuffer.cpp
index 34ed8b7..4011a90 100644
--- a/src/gpu/vk/GrVkGpuCommandBuffer.cpp
+++ b/src/gpu/vk/GrVkGpuCommandBuffer.cpp
@@ -192,6 +192,15 @@
                                       false);
         }
 
+        // If we have any sampled images set their layout now.
+        for (int j = 0; j < cbInfo.fSampledImages.count(); ++j) {
+            cbInfo.fSampledImages[j]->setImageLayout(fGpu,
+                                                     VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
+                                                     VK_ACCESS_SHADER_READ_BIT,
+                                                     VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
+                                                     false);
+        }
+
         // TODO: We can't add this optimization yet since many things create a scratch texture which
         // adds the discard immediately, but then don't draw to it right away. This causes the
         // discard to be ignored and we get yelled at for loading uninitialized data. However, once
@@ -545,18 +554,9 @@
     return pipelineState;
 }
 
-static void set_texture_layout(GrVkTexture* vkTexture, GrVkGpu* gpu) {
-    // TODO: If we ever decide to create the secondary command buffers ahead of time before we
-    // are actually going to submit them, we will need to track the sampled images and delay
-    // adding the layout change/barrier until we are ready to submit.
-    vkTexture->setImageLayout(gpu,
-                              VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
-                              VK_ACCESS_SHADER_READ_BIT,
-                              VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
-                              false);
-}
-
-static void prepare_sampled_images(const GrResourceIOProcessor& processor, GrVkGpu* gpu) {
+static void prepare_sampled_images(const GrResourceIOProcessor& processor,
+                                   SkTArray<GrVkImage*>* sampledImages,
+                                   GrVkGpu* gpu) {
     for (int i = 0; i < processor.numTextureSamplers(); ++i) {
         const GrResourceIOProcessor::TextureSampler& sampler = processor.textureSampler(i);
         GrVkTexture* vkTexture = static_cast<GrVkTexture*>(sampler.peekTexture());
@@ -574,7 +574,7 @@
                 vkTexture->texturePriv().markMipMapsClean();
             }
         }
-        set_texture_layout(vkTexture, gpu);
+        sampledImages->push_back(vkTexture);
     }
 }
 
@@ -589,13 +589,16 @@
     if (!meshCount) {
         return;
     }
-    prepare_sampled_images(primProc, fGpu);
+
+    CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
+
+    prepare_sampled_images(primProc, &cbInfo.fSampledImages, fGpu);
     GrFragmentProcessor::Iter iter(pipeline);
     while (const GrFragmentProcessor* fp = iter.next()) {
-        prepare_sampled_images(*fp, fGpu);
+        prepare_sampled_images(*fp, &cbInfo.fSampledImages, fGpu);
     }
     if (GrTexture* dstTexture = pipeline.peekDstTexture()) {
-        set_texture_layout(static_cast<GrVkTexture*>(dstTexture), fGpu);
+        cbInfo.fSampledImages.push_back(static_cast<GrVkTexture*>(dstTexture));
     }
 
     GrPrimitiveType primitiveType = meshes[0].primitiveType();
@@ -607,8 +610,6 @@
         return;
     }
 
-    CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
-
     for (int i = 0; i < meshCount; ++i) {
         const GrMesh& mesh = meshes[i];
         if (mesh.primitiveType() != primitiveType) {
diff --git a/src/gpu/vk/GrVkGpuCommandBuffer.h b/src/gpu/vk/GrVkGpuCommandBuffer.h
index 2aa457b..0254a3f 100644
--- a/src/gpu/vk/GrVkGpuCommandBuffer.h
+++ b/src/gpu/vk/GrVkGpuCommandBuffer.h
@@ -163,6 +163,10 @@
         // command buffer.
         SkTArray<InlineUploadInfo>             fPreDrawUploads;
         SkTArray<CopyInfo>                     fPreCopies;
+        // Array of images that will be sampled and thus need to be transfered to sampled layout
+        // before submitting the secondary command buffers. This must happen after we do any predraw
+        // uploads or copies.
+        SkTArray<GrVkImage*>                   fSampledImages;
 
         GrVkSecondaryCommandBuffer* currentCmdBuf() {
             return fCommandBuffers.back();