Track vulkan memory allocations in UMA.

We track the total amount of memory allocated and the percentage of the
allocated memory that is used.

Bug: skia:10871
Change-Id: I4aa120a3545d215cf42430aa6a73e924118f1dbc
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/329963
Commit-Queue: Greg Daniel <egdaniel@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
diff --git a/include/config/SkUserConfig.h b/include/config/SkUserConfig.h
index 015473e..313d324 100644
--- a/include/config/SkUserConfig.h
+++ b/include/config/SkUserConfig.h
@@ -84,5 +84,6 @@
 //#define SK_HISTOGRAM_BOOLEAN(name, sample)
 //#define SK_HISTOGRAM_ENUMERATION(name, sample, enum_size)
 //#define SK_HISTOGRAM_EXACT_LINEAR(name, sample, value_max)
+//#define SK_HISTOGRAM_MEMORY_KB(name, sample)
 
 #endif
diff --git a/include/core/SkTypes.h b/include/core/SkTypes.h
index 711dcbd..62ecfd0 100644
--- a/include/core/SkTypes.h
+++ b/include/core/SkTypes.h
@@ -398,7 +398,8 @@
 
 #if defined(SK_HISTOGRAM_ENUMERATION)  || \
     defined(SK_HISTOGRAM_BOOLEAN)      || \
-    defined(SK_HISTOGRAM_EXACT_LINEAR)
+    defined(SK_HISTOGRAM_EXACT_LINEAR) || \
+    defined(SK_HISTOGRAM_MEMORY_KB)
 #  define SK_HISTOGRAMS_ENABLED 1
 #else
 #  define SK_HISTOGRAMS_ENABLED 0
@@ -413,9 +414,16 @@
 #endif
 
 #ifndef SK_HISTOGRAM_EXACT_LINEAR
-#define SK_HISTOGRAM_EXACT_LINEAR(name, sample, value_max)
+#  define SK_HISTOGRAM_EXACT_LINEAR(name, sample, value_max)
 #endif
 
+#ifndef SK_HISTOGRAM_MEMORY_KB
+#  define SK_HISTOGRAM_MEMORY_KB(name, sample)
+#endif
+
+#define SK_HISTOGRAM_PERCENTAGE(name, percent_as_int) \
+    SK_HISTOGRAM_EXACT_LINEAR(name, percent_as_int, 101)
+
 #ifndef SK_DISABLE_LEGACY_SHADERCONTEXT
 #define SK_ENABLE_LEGACY_SHADERCONTEXT
 #endif
diff --git a/src/gpu/vk/GrVkMemory.cpp b/src/gpu/vk/GrVkMemory.cpp
index aa8660a..fe3b23d 100644
--- a/src/gpu/vk/GrVkMemory.cpp
+++ b/src/gpu/vk/GrVkMemory.cpp
@@ -14,6 +14,19 @@
 using AllocationPropertyFlags = GrVkMemoryAllocator::AllocationPropertyFlags;
 using BufferUsage = GrVkMemoryAllocator::BufferUsage;
 
+static void report_memory_usage(GrVkMemoryAllocator* allocator) {
+#if SK_HISTOGRAMS_ENABLED
+    uint64_t allocatedMemory = allocator->totalAllocatedMemory();
+    uint64_t usedMemory = allocator->totalUsedMemory();
+    SkASSERT(usedMemory <= allocatedMemory);
+    SK_HISTOGRAM_PERCENTAGE("VulkanMemoryAllocator.PercentUsed",
+                            (usedMemory * 100) / allocatedMemory);
+    // allocatedMemory is in bytes and need to be reported it in kilobytes. SK_HISTOGRAM_MEMORY_KB
+    // supports samples up to around 500MB which should support the amounts of memory we allocate.
+    SK_HISTOGRAM_MEMORY_KB("VulkanMemoryAllocator.AmountAllocated", allocatedMemory >> 10);
+#endif
+}
+
 static BufferUsage get_buffer_usage(GrVkBuffer::Type type, bool dynamic) {
     switch (type) {
         case GrVkBuffer::kVertex_Type: // fall through
@@ -72,6 +85,8 @@
         return false;
     }
 
+    report_memory_usage(allocator);
+
     return true;
 }
 
@@ -124,6 +139,8 @@
         return false;
     }
 
+    report_memory_usage(allocator);
+
     return true;
 }