Use shared gpu memory size for all GrAttachments.

Bug: skia:10727
Change-Id: I07ad4684c7a09963aa4be60e08877dde1c5ca192
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/325663
Commit-Queue: Greg Daniel <egdaniel@google.com>
Reviewed-by: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/GrAttachment.cpp b/src/gpu/GrAttachment.cpp
index 35fdbf2..562526a 100644
--- a/src/gpu/GrAttachment.cpp
+++ b/src/gpu/GrAttachment.cpp
@@ -8,7 +8,19 @@
 #include "src/gpu/GrAttachment.h"
 
 #include "include/private/GrResourceKey.h"
+#include "src/gpu/GrBackendUtils.h"
 #include "src/gpu/GrCaps.h"
+#include "src/gpu/GrDataUtils.h"
+
+size_t GrAttachment::onGpuMemorySize() const {
+    GrBackendFormat format = this->backendFormat();
+    SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
+
+    uint64_t size = GrNumBlocks(compression, this->dimensions());
+    size *= GrBackendFormatBytesPerBlock(this->backendFormat());
+    size *= this->numSamples();
+    return size;
+}
 
 static void build_key(GrResourceKey::Builder* builder,
                       const GrCaps& caps,
diff --git a/src/gpu/GrAttachment.h b/src/gpu/GrAttachment.h
index 9f04117..a5f0dc1 100644
--- a/src/gpu/GrAttachment.h
+++ b/src/gpu/GrAttachment.h
@@ -70,6 +70,8 @@
             , fSampleCnt(sampleCnt) {}
 
 private:
+    size_t onGpuMemorySize() const final;
+
     const char* getResourceType() const override {
         // TODO: Once attachments can have multiple usages this needs to be updated
         switch (fSupportedUsages) {
diff --git a/src/gpu/GrDataUtils.cpp b/src/gpu/GrDataUtils.cpp
index 23e5d27..cb91de2 100644
--- a/src/gpu/GrDataUtils.cpp
+++ b/src/gpu/GrDataUtils.cpp
@@ -152,6 +152,22 @@
     }
 }
 
+size_t GrNumBlocks(SkImage::CompressionType type, SkISize baseDimensions) {
+    switch (type) {
+        case SkImage::CompressionType::kNone:
+            return baseDimensions.width() * baseDimensions.height();
+        case SkImage::CompressionType::kETC2_RGB8_UNORM:
+        case SkImage::CompressionType::kBC1_RGB8_UNORM:
+        case SkImage::CompressionType::kBC1_RGBA8_UNORM: {
+            int numBlocksWidth = num_4x4_blocks(baseDimensions.width());
+            int numBlocksHeight = num_4x4_blocks(baseDimensions.height());
+
+            return numBlocksWidth * numBlocksHeight;
+        }
+    }
+    SkUNREACHABLE;
+}
+
 size_t GrCompressedRowBytes(SkImage::CompressionType type, int width) {
     switch (type) {
         case SkImage::CompressionType::kNone:
diff --git a/src/gpu/GrDataUtils.h b/src/gpu/GrDataUtils.h
index 9cb062d..6b17b46 100644
--- a/src/gpu/GrDataUtils.h
+++ b/src/gpu/GrDataUtils.h
@@ -15,6 +15,8 @@
 
 class GrImageInfo;
 
+size_t GrNumBlocks(SkImage::CompressionType, SkISize baseDimensions);
+
 // Returns a value that can be used to set rowBytes for a transfer function.
 size_t GrCompressedRowBytes(SkImage::CompressionType, int w);
 
diff --git a/src/gpu/d3d/GrD3DAttachment.cpp b/src/gpu/d3d/GrD3DAttachment.cpp
index ba6b34f..e4992c2 100644
--- a/src/gpu/d3d/GrD3DAttachment.cpp
+++ b/src/gpu/d3d/GrD3DAttachment.cpp
@@ -62,14 +62,6 @@
                                                       std::move(state), view));
 }
 
-size_t GrD3DAttachment::onGpuMemorySize() const {
-    uint64_t size = this->width();
-    size *= this->height();
-    size *= GrD3DCaps::GetStencilFormatTotalBitCount(this->dxgiFormat());
-    size *= this->numSamples();
-    return static_cast<size_t>(size / 8);
-}
-
 void GrD3DAttachment::onRelease() {
     GrD3DGpu* gpu = this->getD3DGpu();
     this->releaseResource(gpu);
diff --git a/src/gpu/d3d/GrD3DAttachment.h b/src/gpu/d3d/GrD3DAttachment.h
index 651a04b..1f4194b 100644
--- a/src/gpu/d3d/GrD3DAttachment.h
+++ b/src/gpu/d3d/GrD3DAttachment.h
@@ -34,8 +34,6 @@
     void onAbandon() override;
 
 private:
-    size_t onGpuMemorySize() const override;
-
     GrD3DAttachment(GrD3DGpu* gpu,
                     SkISize dimensions,
                     UsageFlags supportedUsages,
diff --git a/src/gpu/dawn/GrDawnAttachment.cpp b/src/gpu/dawn/GrDawnAttachment.cpp
index 9cd0a1f..ddea0b4 100644
--- a/src/gpu/dawn/GrDawnAttachment.cpp
+++ b/src/gpu/dawn/GrDawnAttachment.cpp
@@ -47,14 +47,6 @@
 
 GrDawnAttachment::~GrDawnAttachment() {}
 
-size_t GrDawnAttachment::onGpuMemorySize() const {
-    uint64_t size = this->width();
-    size *= this->height();
-    size *= 32;
-    size *= std::max(1, this->numSamples());
-    return static_cast<size_t>(size / 8);
-}
-
 void GrDawnAttachment::onRelease() { GrAttachment::onRelease(); }
 
 void GrDawnAttachment::onAbandon() { GrAttachment::onAbandon(); }
diff --git a/src/gpu/dawn/GrDawnAttachment.h b/src/gpu/dawn/GrDawnAttachment.h
index 8063234..a23a367 100644
--- a/src/gpu/dawn/GrDawnAttachment.h
+++ b/src/gpu/dawn/GrDawnAttachment.h
@@ -29,8 +29,6 @@
     void onAbandon() override;
 
 private:
-    size_t onGpuMemorySize() const override;
-
     GrDawnAttachment(GrDawnGpu* gpu,
                      SkISize dimensions,
                      UsageFlags supportedUsages,
diff --git a/src/gpu/gl/GrGLAttachment.cpp b/src/gpu/gl/GrGLAttachment.cpp
index d6bdaaf..8c032a0 100644
--- a/src/gpu/gl/GrGLAttachment.cpp
+++ b/src/gpu/gl/GrGLAttachment.cpp
@@ -10,14 +10,6 @@
 #include "include/core/SkTraceMemoryDump.h"
 #include "src/gpu/gl/GrGLGpu.h"
 
-size_t GrGLAttachment::onGpuMemorySize() const {
-    uint64_t size = this->width();
-    size *= this->height();
-    size *= GrGLFormatBytesPerBlock(fFormat);
-    size *= this->numSamples();
-    return static_cast<size_t>(size);
-}
-
 void GrGLAttachment::onRelease() {
     if (0 != fRenderbufferID) {
         GrGLGpu* gpuGL = (GrGLGpu*)this->getGpu();
diff --git a/src/gpu/gl/GrGLAttachment.h b/src/gpu/gl/GrGLAttachment.h
index 2ebb405..55b58c0 100644
--- a/src/gpu/gl/GrGLAttachment.h
+++ b/src/gpu/gl/GrGLAttachment.h
@@ -42,8 +42,6 @@
                           const SkString& dumpName) const override;
 
 private:
-    size_t onGpuMemorySize() const override;
-
     GrGLFormat fFormat;
 
     // may be zero for external SBs associated with external RTs
diff --git a/src/gpu/mock/GrMockAttachment.h b/src/gpu/mock/GrMockAttachment.h
index 6878965..fc268e8 100644
--- a/src/gpu/mock/GrMockAttachment.h
+++ b/src/gpu/mock/GrMockAttachment.h
@@ -26,11 +26,6 @@
     }
 
 private:
-    size_t onGpuMemorySize() const override {
-        int bpp = GrBackendFormatBytesPerBlock(this->backendFormat());
-        return std::max(1, (int)(bpp)) * this->width() * this->height();
-    }
-
     using INHERITED = GrAttachment;
 };
 
diff --git a/src/gpu/mtl/GrMtlAttachment.h b/src/gpu/mtl/GrMtlAttachment.h
index f198b83..ae31e1e 100644
--- a/src/gpu/mtl/GrMtlAttachment.h
+++ b/src/gpu/mtl/GrMtlAttachment.h
@@ -37,8 +37,6 @@
     void onAbandon() override;
 
 private:
-    size_t onGpuMemorySize() const override;
-
     GrMtlAttachment(GrMtlGpu* gpu,
                     SkISize dimensions,
                     UsageFlags supportedUsages,
diff --git a/src/gpu/mtl/GrMtlAttachment.mm b/src/gpu/mtl/GrMtlAttachment.mm
index be568d4..d31c27e 100644
--- a/src/gpu/mtl/GrMtlAttachment.mm
+++ b/src/gpu/mtl/GrMtlAttachment.mm
@@ -50,14 +50,6 @@
     SkASSERT(!fView);
 }
 
-size_t GrMtlAttachment::onGpuMemorySize() const {
-    uint64_t size = this->width();
-    size *= this->height();
-    size *= GrMtlFormatBytesPerBlock(this->mtlFormat());
-    size *= this->numSamples();
-    return static_cast<size_t>(size);
-}
-
 void GrMtlAttachment::onRelease() {
     fView = nullptr;
     GrAttachment::onRelease();
diff --git a/src/gpu/vk/GrVkAttachment.cpp b/src/gpu/vk/GrVkAttachment.cpp
index 65f081a..ac5f1e8 100644
--- a/src/gpu/vk/GrVkAttachment.cpp
+++ b/src/gpu/vk/GrVkAttachment.cpp
@@ -100,14 +100,6 @@
     SkASSERT(!fView);
 }
 
-size_t GrVkAttachment::onGpuMemorySize() const {
-    uint64_t size = this->width();
-    size *= this->height();
-    size *= GrVkCaps::GetStencilFormatTotalBitCount(this->imageFormat());
-    size *= this->numSamples();
-    return static_cast<size_t>(size / 8);
-}
-
 void GrVkAttachment::onRelease() {
     this->releaseImage();
     fView.reset();
diff --git a/src/gpu/vk/GrVkAttachment.h b/src/gpu/vk/GrVkAttachment.h
index a931300..1185e43 100644
--- a/src/gpu/vk/GrVkAttachment.h
+++ b/src/gpu/vk/GrVkAttachment.h
@@ -40,8 +40,6 @@
     void onAbandon() override;
 
 private:
-    size_t onGpuMemorySize() const override;
-
     static sk_sp<GrVkAttachment> Make(GrVkGpu* gpu,
                                       SkISize dimensions,
                                       UsageFlags attachmentUsages,