Reland "Use GrVkMemoryAllocator for vulkan memory allocations in ganesh."

This is a reland of 331c266ed716526478a10885aff66181cec64486

Original change's description:
> Use GrVkMemoryAllocator for vulkan memory allocations in ganesh.
> 
> Besides using the new allocator, the big logical change is that map
> and unmap calls form GrVkMemory are specc'd to map the entire GrVkAlloc
> instead of a specific offset and size as they did before. As a
> consequence of this, we move the handling of non-coherent alignment
> for flush/invalidate calls to GrVkMemory instead of the callers.
> 
> Bug: skia:
> Change-Id: I794d713106602f27aa7e808c306bbb69fd2b67be
> Reviewed-on: https://skia-review.googlesource.com/130021
> Commit-Queue: Greg Daniel <egdaniel@google.com>
> Reviewed-by: Jim Van Verth <jvanverth@google.com>

Bug: skia:
Change-Id: Ia9a4192d344449fb444d2adaa1d62ff1ede4b21d
Reviewed-on: https://skia-review.googlesource.com/131083
Reviewed-by: Jim Van Verth <jvanverth@google.com>
Commit-Queue: Greg Daniel <egdaniel@google.com>
diff --git a/src/gpu/vk/GrVkGpu.cpp b/src/gpu/vk/GrVkGpu.cpp
index 56d0b95..2525c5c 100644
--- a/src/gpu/vk/GrVkGpu.cpp
+++ b/src/gpu/vk/GrVkGpu.cpp
@@ -17,6 +17,7 @@
 #include "GrRenderTargetPriv.h"
 #include "GrTexturePriv.h"
 
+#include "GrVkAMDMemoryAllocator.h"
 #include "GrVkCommandBuffer.h"
 #include "GrVkGpuCommandBuffer.h"
 #include "GrVkImage.h"
@@ -92,6 +93,7 @@
                  sk_sp<const GrVkBackendContext> backendCtx)
         : INHERITED(context)
         , fBackendContext(std::move(backendCtx))
+        , fMemoryAllocator(fBackendContext->fMemoryAllocator)
         , fDevice(fBackendContext->fDevice)
         , fQueue(fBackendContext->fQueue)
         , fResourceProvider(this)
@@ -118,6 +120,12 @@
     }
 #endif
 
+    if (!fMemoryAllocator) {
+        // We were not given a memory allocator at creation
+        fMemoryAllocator.reset(new GrVkAMDMemoryAllocator(fBackendContext->fPhysicalDevice,
+                                                          fDevice, fBackendContext->fInterface));
+    }
+
     fCompiler = new SkSL::Compiler();
 
     fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), fBackendContext->fPhysicalDevice,
@@ -142,17 +150,6 @@
     fCurrentCmdBuffer = fResourceProvider.findOrCreatePrimaryCommandBuffer();
     SkASSERT(fCurrentCmdBuffer);
     fCurrentCmdBuffer->begin(this);
-
-    // set up our heaps
-    fHeaps[kLinearImage_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 16*1024*1024));
-    fHeaps[kOptimalImage_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 64*1024*1024));
-    fHeaps[kSmallOptimalImage_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 2*1024*1024));
-    fHeaps[kVertexBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0));
-    fHeaps[kIndexBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0));
-    fHeaps[kUniformBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 256*1024));
-    fHeaps[kTexelBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0));
-    fHeaps[kCopyReadBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0));
-    fHeaps[kCopyWriteBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 16*1024*1024));
 }
 
 void GrVkGpu::destroyResources() {
@@ -562,7 +559,6 @@
         0,  // arraySlice
     };
     VkSubresourceLayout layout;
-    VkResult err;
 
     const GrVkInterface* interface = this->vkInterface();
 
@@ -573,28 +569,14 @@
 
     int texTop = kBottomLeft_GrSurfaceOrigin == texOrigin ? tex->height() - top - height : top;
     const GrVkAlloc& alloc = tex->alloc();
-    VkDeviceSize offset = alloc.fOffset + texTop*layout.rowPitch + left*bpp;
-    VkDeviceSize offsetDiff = 0;
+    VkDeviceSize offset = texTop*layout.rowPitch + left*bpp;
     VkDeviceSize size = height*layout.rowPitch;
-    // For Noncoherent buffers we want to make sure the range that we map, both offset and size,
-    // are aligned to the nonCoherentAtomSize limit. We may have to move the initial offset back to
-    // meet the alignment requirements. So we track how far we move back and then adjust the mapped
-    // ptr back up so that this is opaque to the caller.
-    if (SkToBool(alloc.fFlags & GrVkAlloc::kNoncoherent_Flag)) {
-        VkDeviceSize alignment = this->physicalDeviceProperties().limits.nonCoherentAtomSize;
-        offsetDiff = offset & (alignment - 1);
-        offset = offset - offsetDiff;
-        // Make size of the map aligned to nonCoherentAtomSize
-        size = (size + alignment - 1) & ~(alignment - 1);
-    }
-    SkASSERT(offset >= alloc.fOffset);
-    SkASSERT(size <= alloc.fOffset + alloc.fSize);
-    void* mapPtr;
-    err = GR_VK_CALL(interface, MapMemory(fDevice, alloc.fMemory, offset, size, 0, &mapPtr));
-    if (err) {
+    SkASSERT(size + offset <= alloc.fSize);
+    void* mapPtr = GrVkMemory::MapAlloc(this, alloc);
+    if (!mapPtr) {
         return false;
     }
-    mapPtr = reinterpret_cast<char*>(mapPtr) + offsetDiff;
+    mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
 
     if (kBottomLeft_GrSurfaceOrigin == texOrigin) {
         // copy into buffer by rows
@@ -611,7 +593,7 @@
     }
 
     GrVkMemory::FlushMappedAlloc(this, alloc, offset, size);
-    GR_VK_CALL(interface, UnmapMemory(fDevice, alloc.fMemory));
+    GrVkMemory::UnmapAlloc(this, alloc);
 
     return true;
 }
@@ -1147,33 +1129,14 @@
 
 bool copy_testing_data(GrVkGpu* gpu, const void* srcData, const GrVkAlloc& alloc,
                        size_t bufferOffset, size_t srcRowBytes, size_t dstRowBytes, int h) {
-    // For Noncoherent buffers we want to make sure the range that we map, both offset and size,
-    // are aligned to the nonCoherentAtomSize limit. We may have to move the initial offset back to
-    // meet the alignment requirements. So we track how far we move back and then adjust the mapped
-    // ptr back up so that this is opaque to the caller.
-    VkDeviceSize mapSize = dstRowBytes * h;
-    VkDeviceSize mapOffset = alloc.fOffset + bufferOffset;
-    VkDeviceSize offsetDiff = 0;
-    if (SkToBool(alloc.fFlags & GrVkAlloc::kNoncoherent_Flag)) {
-        VkDeviceSize alignment = gpu->physicalDeviceProperties().limits.nonCoherentAtomSize;
-        offsetDiff = mapOffset & (alignment - 1);
-        mapOffset = mapOffset - offsetDiff;
-        // Make size of the map aligned to nonCoherentAtomSize
-        mapSize = (mapSize + alignment - 1) & ~(alignment - 1);
-    }
-    SkASSERT(mapOffset >= alloc.fOffset);
-    SkASSERT(mapSize + mapOffset <= alloc.fOffset + alloc.fSize);
-    void* mapPtr;
-    VkResult err = GR_VK_CALL(gpu->vkInterface(), MapMemory(gpu->device(),
-                                                            alloc.fMemory,
-                                                            mapOffset,
-                                                            mapSize,
-                                                            0,
-                                                            &mapPtr));
-    mapPtr = reinterpret_cast<char*>(mapPtr) + offsetDiff;
-    if (err) {
+    VkDeviceSize size = dstRowBytes * h;
+    VkDeviceSize offset = bufferOffset;
+    SkASSERT(size + offset <= alloc.fSize);
+    void* mapPtr = GrVkMemory::MapAlloc(gpu, alloc);
+    if (!mapPtr) {
         return false;
     }
+    mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
 
     if (srcData) {
         // If there is no padding on dst we can do a single memcopy.
@@ -1192,8 +1155,8 @@
             }
         }
     }
-    GrVkMemory::FlushMappedAlloc(gpu, alloc, mapOffset, mapSize);
-    GR_VK_CALL(gpu->vkInterface(), UnmapMemory(gpu->device(), alloc.fMemory));
+    GrVkMemory::FlushMappedAlloc(gpu, alloc, offset, size);
+    GrVkMemory::UnmapAlloc(gpu, alloc);
     return true;
 }
 
@@ -2017,7 +1980,7 @@
     this->submitCommandBuffer(kForce_SyncQueue);
     void* mappedMemory = transferBuffer->map();
     const GrVkAlloc& transAlloc = transferBuffer->alloc();
-    GrVkMemory::InvalidateMappedAlloc(this, transAlloc, transAlloc.fOffset, VK_WHOLE_SIZE);
+    GrVkMemory::InvalidateMappedAlloc(this, transAlloc, 0, transAlloc.fSize);
 
     if (copyFromOrigin) {
         uint32_t skipRows = region.imageExtent.height - height;