Carve some helper functions off of GPUSink (take 2)

I'm adding a new DDL Sink and would like to reuse this functionality.

TBR=egdaniel@google.com
Change-Id: I17ae929557400be4edd8df78ab6580872024bc15
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/270799
Reviewed-by: Robert Phillips <robertphillips@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/dm/DMSrcSink.cpp b/dm/DMSrcSink.cpp
index 947240f..d4f2139 100644
--- a/dm/DMSrcSink.cpp
+++ b/dm/DMSrcSink.cpp
@@ -79,6 +79,7 @@
 DECLARE_int(gpuThreads);
 
 using sk_gpu_test::GrContextFactory;
+using sk_gpu_test::ContextInfo;
 
 namespace DM {
 
@@ -1368,6 +1369,58 @@
     return this->onDraw(src, dst, dstStream, log, fBaseContextOptions);
 }
 
+sk_sp<SkSurface> GPUSink::createDstSurface(GrContext* context, SkISize size,
+                                           GrBackendTexture* backendTexture,
+                                           GrBackendRenderTarget* backendRT) const {
+    sk_sp<SkSurface> surface;
+
+    SkImageInfo info = SkImageInfo::Make(size, fColorType, fAlphaType, fColorSpace);
+    uint32_t flags = fUseDIText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag : 0;
+    SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
+
+    switch (fSurfType) {
+        case SkCommandLineConfigGpu::SurfType::kDefault:
+            surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, fSampleCount,
+                                                  &props);
+            break;
+        case SkCommandLineConfigGpu::SurfType::kBackendTexture:
+            *backendTexture = context->createBackendTexture(
+                info.width(), info.height(), info.colorType(), SkColors::kTransparent,
+                GrMipMapped::kNo, GrRenderable::kYes, GrProtected::kNo);
+            surface = SkSurface::MakeFromBackendTexture(context, *backendTexture,
+                                                        kTopLeft_GrSurfaceOrigin, fSampleCount,
+                                                        fColorType, info.refColorSpace(), &props);
+            break;
+        case SkCommandLineConfigGpu::SurfType::kBackendRenderTarget:
+            if (1 == fSampleCount) {
+                auto colorType = SkColorTypeToGrColorType(info.colorType());
+                *backendRT = context->priv().getGpu()->createTestingOnlyBackendRenderTarget(
+                    info.width(), info.height(), colorType);
+                surface = SkSurface::MakeFromBackendRenderTarget(
+                    context, *backendRT, kBottomLeft_GrSurfaceOrigin, info.colorType(),
+                    info.refColorSpace(), &props);
+            }
+            break;
+    }
+
+    return surface;
+}
+
+bool GPUSink::readBack(SkSurface* surface, SkBitmap* dst) const {
+    SkCanvas* canvas = surface->getCanvas();
+    SkISize size = surface->imageInfo().dimensions();
+
+    SkImageInfo info = SkImageInfo::Make(size, fColorType, fAlphaType, fColorSpace);
+    if (info.colorType() == kRGB_565_SkColorType || info.colorType() == kARGB_4444_SkColorType ||
+        info.colorType() == kRGB_888x_SkColorType) {
+        // We don't currently support readbacks into these formats on the GPU backend. Convert to
+        // 32 bit.
+        info = SkImageInfo::Make(size, kRGBA_8888_SkColorType, kPremul_SkAlphaType, fColorSpace);
+    }
+    dst->allocPixels(info);
+    return canvas->readPixels(*dst, 0, 0);
+}
+
 Result GPUSink::onDraw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log,
                        const GrContextOptions& baseOptions,
                        std::function<void(GrContext*)> initContext) const {
@@ -1381,46 +1434,20 @@
     SkASSERT(exec == grOptions.fExecutor);
 
     GrContextFactory factory(grOptions);
-    const SkISize size = src.size();
-    SkImageInfo info = SkImageInfo::Make(size, fColorType, fAlphaType, fColorSpace);
-    sk_sp<SkSurface> surface;
     GrContext* context = factory.getContextInfo(fContextType, fContextOverrides).grContext();
     if (initContext) {
         initContext(context);
     }
+
     const int maxDimension = context->priv().caps()->maxTextureSize();
-    if (maxDimension < std::max(size.width(), size.height())) {
+    if (maxDimension < std::max(src.size().width(), src.size().height())) {
         return Result::Skip("Src too large to create a texture.\n");
     }
-    uint32_t flags = fUseDIText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag : 0;
-    SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
+
     GrBackendTexture backendTexture;
     GrBackendRenderTarget backendRT;
-    switch (fSurfType) {
-        case SkCommandLineConfigGpu::SurfType::kDefault:
-            surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, fSampleCount,
-                                                  &props);
-            break;
-        case SkCommandLineConfigGpu::SurfType::kBackendTexture:
-            backendTexture = context->createBackendTexture(
-                    info.width(), info.height(), info.colorType(), SkColors::kTransparent,
-                    GrMipMapped::kNo, GrRenderable::kYes, GrProtected::kNo);
-            surface = SkSurface::MakeFromBackendTexture(context, backendTexture,
-                                                        kTopLeft_GrSurfaceOrigin, fSampleCount,
-                                                        fColorType, info.refColorSpace(), &props);
-            break;
-        case SkCommandLineConfigGpu::SurfType::kBackendRenderTarget:
-            if (1 == fSampleCount) {
-                auto colorType = SkColorTypeToGrColorType(info.colorType());
-                backendRT = context->priv().getGpu()->createTestingOnlyBackendRenderTarget(
-                        info.width(), info.height(), colorType);
-                surface = SkSurface::MakeFromBackendRenderTarget(
-                        context, backendRT, kBottomLeft_GrSurfaceOrigin, info.colorType(),
-                        info.refColorSpace(), &props);
-            }
-            break;
-    }
-
+    sk_sp<SkSurface> surface = this->createDstSurface(context, src.size(),
+                                                      &backendTexture, &backendRT);
     if (!surface) {
         return Result::Fatal("Could not create a surface.");
     }
@@ -1437,14 +1464,9 @@
         canvas->getGrContext()->priv().dumpCacheStats(log);
         canvas->getGrContext()->priv().dumpGpuStats(log);
     }
-    if (info.colorType() == kRGB_565_SkColorType || info.colorType() == kARGB_4444_SkColorType ||
-        info.colorType() == kRGB_888x_SkColorType) {
-        // We don't currently support readbacks into these formats on the GPU backend. Convert to
-        // 32 bit.
-        info = SkImageInfo::Make(size, kRGBA_8888_SkColorType, kPremul_SkAlphaType, fColorSpace);
-    }
-    dst->allocPixels(info);
-    canvas->readPixels(*dst, 0, 0);
+
+    this->readBack(surface.get(), dst);
+
     if (FLAGS_abandonGpuContext) {
         factory.abandonContexts();
     } else if (FLAGS_releaseAndAbandonGpuContext) {
diff --git a/dm/DMSrcSink.h b/dm/DMSrcSink.h
index ae05d9f..4c1d66c 100644
--- a/dm/DMSrcSink.h
+++ b/dm/DMSrcSink.h
@@ -366,7 +366,7 @@
                   std::function<void(GrContext*)> initContext = nullptr) const;
 
     sk_gpu_test::GrContextFactory::ContextType contextType() const { return fContextType; }
-    const sk_gpu_test::GrContextFactory::ContextOverrides& contextOverrides() {
+    const sk_gpu_test::GrContextFactory::ContextOverrides& contextOverrides() const {
         return fContextOverrides;
     }
     SkCommandLineConfigGpu::SurfType surfType() const { return fSurfType; }
@@ -383,6 +383,11 @@
         return SkColorInfo(fColorType, fAlphaType, fColorSpace);
     }
 
+protected:
+    sk_sp<SkSurface> createDstSurface(GrContext*, SkISize size, GrBackendTexture*,
+                                      GrBackendRenderTarget*) const;
+    bool readBack(SkSurface*, SkBitmap* dst) const;
+
 private:
     sk_gpu_test::GrContextFactory::ContextType        fContextType;
     sk_gpu_test::GrContextFactory::ContextOverrides   fContextOverrides;