Vulkan: Encapsulate RenderTargetVk.

This makes the members private and adds more functionality. This moves
more responsibility out of vk::CommandGraphNode and also makes the
RenderPass init in the CommandGraphNode class better encapsulated.

Bug: angleproject:2539
Change-Id: Ia16f3f39cf011548c6473805b8b28e284808e856
Reviewed-on: https://chromium-review.googlesource.com/1040279
Reviewed-by: Luc Ferron <lucferron@chromium.org>
Reviewed-by: Frank Henigman <fjhenigman@chromium.org>
Commit-Queue: Jamie Madill <jmadill@chromium.org>
diff --git a/src/libANGLE/renderer/vulkan/FramebufferVk.cpp b/src/libANGLE/renderer/vulkan/FramebufferVk.cpp
index 68b2b74..37037d4 100644
--- a/src/libANGLE/renderer/vulkan/FramebufferVk.cpp
+++ b/src/libANGLE/renderer/vulkan/FramebufferVk.cpp
@@ -36,10 +36,10 @@
                                                 RenderTargetVk *renderTarget)
 {
     GLenum implFormat =
-        renderTarget->image->getFormat().textureFormat().fboImplementationInternalFormat;
+        renderTarget->getImageFormat().textureFormat().fboImplementationInternalFormat;
     return gl::GetSizedInternalFormatInfo(implFormat);
 }
-}  // anonymous namespace<
+}  // anonymous namespace
 
 // static
 FramebufferVk *FramebufferVk::CreateUserFBO(const gl::FramebufferState &state)
@@ -186,8 +186,8 @@
             (stencilAttachment ? VK_IMAGE_ASPECT_STENCIL_BIT : 0);
 
         RenderTargetVk *renderTarget = mRenderTargetCache.getDepthStencil();
-        renderTarget->resource->onWriteResource(writingNode, currentSerial);
-        renderTarget->image->clearDepthStencil(aspectFlags, clearDepthStencilValue, commandBuffer);
+        vk::ImageHelper *image       = renderTarget->getImageForWrite(currentSerial, writingNode);
+        image->clearDepthStencil(aspectFlags, clearDepthStencilValue, commandBuffer);
 
         if (!clearColor)
         {
@@ -212,8 +212,8 @@
     {
         RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndex];
         ASSERT(colorRenderTarget);
-        colorRenderTarget->resource->onWriteResource(writingNode, currentSerial);
-        colorRenderTarget->image->clearColor(clearColorValue, commandBuffer);
+        vk::ImageHelper *image = colorRenderTarget->getImageForWrite(currentSerial, writingNode);
+        image->clearColor(clearColorValue, commandBuffer);
     }
 
     return gl::NoError();
@@ -319,7 +319,7 @@
 RenderTargetVk *FramebufferVk::getColorReadRenderTarget() const
 {
     RenderTargetVk *renderTarget = mRenderTargetCache.getColorRead(mState);
-    ASSERT(renderTarget && renderTarget->image->valid());
+    ASSERT(renderTarget && renderTarget->getImage().valid());
     return renderTarget;
 }
 
@@ -372,7 +372,7 @@
                 RenderTargetVk *renderTarget = mRenderTargetCache.getColors()[colorIndex];
                 if (renderTarget)
                 {
-                    const angle::Format &format = renderTarget->image->getFormat().textureFormat();
+                    const angle::Format &format = renderTarget->getImageFormat().textureFormat();
                     updateActiveColorMasks(colorIndex, format.redBits > 0, format.greenBits > 0,
                                            format.blueBits > 0, format.alphaBits > 0);
                 }
@@ -415,13 +415,13 @@
     {
         RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndex];
         ASSERT(colorRenderTarget);
-        desc.packColorAttachment(*colorRenderTarget->image);
+        desc.packColorAttachment(colorRenderTarget->getImage());
     }
 
     RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
     if (depthStencilRenderTarget)
     {
-        desc.packDepthStencilAttachment(*depthStencilRenderTarget->image);
+        desc.packDepthStencilAttachment(depthStencilRenderTarget->getImage());
     }
 
     mRenderPassDesc = desc;
@@ -458,21 +458,20 @@
     {
         RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndex];
         ASSERT(colorRenderTarget);
-        attachments.push_back(colorRenderTarget->imageView->getHandle());
+        attachments.push_back(colorRenderTarget->getImageView()->getHandle());
 
-        ASSERT(attachmentsSize.empty() ||
-               attachmentsSize == colorRenderTarget->image->getExtents());
-        attachmentsSize = colorRenderTarget->image->getExtents();
+        ASSERT(attachmentsSize.empty() || attachmentsSize == colorRenderTarget->getImageExtents());
+        attachmentsSize = colorRenderTarget->getImageExtents();
     }
 
     RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
     if (depthStencilRenderTarget)
     {
-        attachments.push_back(depthStencilRenderTarget->imageView->getHandle());
+        attachments.push_back(depthStencilRenderTarget->getImageView()->getHandle());
 
         ASSERT(attachmentsSize.empty() ||
-               attachmentsSize == depthStencilRenderTarget->image->getExtents());
-        attachmentsSize = depthStencilRenderTarget->image->getExtents();
+               attachmentsSize == depthStencilRenderTarget->getImageExtents());
+        attachmentsSize = depthStencilRenderTarget->getImageExtents();
     }
 
     ASSERT(!attachments.empty());
@@ -699,6 +698,8 @@
             renderer->getDevice(), renderer->getCommandPool(), &commandBuffer));
     }
 
+    vk::RenderPassDesc renderPassDesc;
+
     // Initialize RenderPass info.
     // TODO(jmadill): Support gaps in RenderTargets. http://anglebug.com/2394
     const auto &colorRenderTargets = mRenderTargetCache.getColors();
@@ -707,14 +708,14 @@
         RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndex];
         ASSERT(colorRenderTarget);
 
-        (*nodeOut)->appendColorRenderTarget(currentSerial, colorRenderTarget);
+        colorRenderTarget->onColorDraw(currentSerial, *nodeOut, &renderPassDesc);
         attachmentClearValues.emplace_back(contextVk->getClearColorValue());
     }
 
     RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
     if (depthStencilRenderTarget)
     {
-        (*nodeOut)->appendDepthStencilRenderTarget(currentSerial, depthStencilRenderTarget);
+        depthStencilRenderTarget->onDepthStencilDraw(currentSerial, *nodeOut, &renderPassDesc);
         attachmentClearValues.emplace_back(contextVk->getClearDepthStencilValue());
     }
 
@@ -722,7 +723,8 @@
         gl::Rectangle(0, 0, mState.getDimensions().width, mState.getDimensions().height);
     // Hard-code RenderPass to clear the first render target to the current clear value.
     // TODO(jmadill): Proper clear value implementation. http://anglebug.com/2361
-    (*nodeOut)->storeRenderPassInfo(*framebuffer, renderArea, attachmentClearValues);
+    (*nodeOut)->storeRenderPassInfo(*framebuffer, renderArea, renderPassDesc,
+                                    attachmentClearValues);
 
     return gl::NoError();
 }
@@ -754,13 +756,17 @@
 
     RenderTargetVk *renderTarget = getColorReadRenderTarget();
 
-    vk::ImageHelper *renderTargetImage = renderTarget->image;
-    const angle::Format &angleFormat   = renderTargetImage->getFormat().textureFormat();
-    VkBuffer bufferHandle              = VK_NULL_HANDLE;
-    uint8_t *readPixelBuffer           = nullptr;
-    bool newBufferAllocated            = false;
-    uint32_t stagingOffset             = 0;
-    size_t allocationSize              = area.width * angleFormat.pixelBytes * area.height;
+    // Note that although we're reading from the image, we need to update the layout below.
+    // TODO(jmadill): Clearify read/write semantics. http://anglebug.com/2539
+    vk::ImageHelper *srcImage =
+        renderTarget->getImageForWrite(renderer->getCurrentQueueSerial(), getCurrentWritingNode());
+
+    const angle::Format &angleFormat = srcImage->getFormat().textureFormat();
+    VkBuffer bufferHandle            = VK_NULL_HANDLE;
+    uint8_t *readPixelBuffer         = nullptr;
+    bool newBufferAllocated          = false;
+    uint32_t stagingOffset           = 0;
+    size_t allocationSize            = area.width * angleFormat.pixelBytes * area.height;
 
     mReadPixelsBuffer.allocate(renderer, allocationSize, &readPixelBuffer, &bufferHandle,
                                &stagingOffset, &newBufferAllocated);
@@ -780,13 +786,12 @@
     region.imageSubresource.layerCount     = 1;
     region.imageSubresource.mipLevel       = 0;
 
-    renderTargetImage->changeLayoutWithStages(
+    srcImage->changeLayoutWithStages(
         VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
         VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, commandBuffer);
 
-    commandBuffer->copyImageToBuffer(renderTargetImage->getImage(),
-                                     renderTargetImage->getCurrentLayout(), bufferHandle, 1,
-                                     &region);
+    commandBuffer->copyImageToBuffer(srcImage->getImage(), srcImage->getCurrentLayout(),
+                                     bufferHandle, 1, &region);
 
     // Triggers a full finish.
     // TODO(jmadill): Don't block on asynchronous readback.
@@ -804,6 +809,6 @@
 
 const gl::Extents &FramebufferVk::getReadImageExtents() const
 {
-    return getColorReadRenderTarget()->image->getExtents();
+    return getColorReadRenderTarget()->getImageExtents();
 }
 }  // namespace rx