Add option to switch between OGA and NGA SkGpuDevices

Bug: skia:11837
Change-Id: Id52f05a055c97cd2644bc70d600e9313766f6730
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/415796
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/GrDirectContextPriv.cpp b/src/gpu/GrDirectContextPriv.cpp
index 9f0712f..66917a1 100644
--- a/src/gpu/GrDirectContextPriv.cpp
+++ b/src/gpu/GrDirectContextPriv.cpp
@@ -14,6 +14,7 @@
 #include "src/gpu/GrDrawingManager.h"
 #include "src/gpu/GrGpu.h"
 #include "src/gpu/GrMemoryPool.h"
+#include "src/gpu/GrRecordingContextPriv.h"
 #include "src/gpu/GrSurfaceContext.h"
 #include "src/gpu/GrSurfaceDrawContext.h"
 #include "src/gpu/GrTexture.h"
@@ -229,3 +230,28 @@
 
     return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
 }
+
+sk_sp<SkBaseGpuDevice> GrDirectContextPriv::createDevice(GrColorType colorType,
+                                                         sk_sp<GrSurfaceProxy> proxy,
+                                                         sk_sp<SkColorSpace> colorSpace,
+                                                         GrSurfaceOrigin origin,
+                                                         const SkSurfaceProps& props,
+                                                         SkBaseGpuDevice::InitContents init) {
+    return fContext->GrRecordingContext::priv().createDevice(colorType, std::move(proxy),
+                                                             std::move(colorSpace),
+                                                             origin, props, init);
+}
+
+sk_sp<SkBaseGpuDevice> GrDirectContextPriv::createDevice(SkBudgeted budgeted,
+                                                         const SkImageInfo& ii,
+                                                         SkBackingFit fit,
+                                                         int sampleCount,
+                                                         GrMipmapped mipmapped,
+                                                         GrProtected isProtected,
+                                                         GrSurfaceOrigin origin,
+                                                         const SkSurfaceProps& props,
+                                                         SkBaseGpuDevice::InitContents init) {
+    return fContext->GrRecordingContext::priv().createDevice(budgeted, ii, fit, sampleCount,
+                                                             mipmapped, isProtected,
+                                                             origin, props, init);
+}
diff --git a/src/gpu/GrDirectContextPriv.h b/src/gpu/GrDirectContextPriv.h
index caa4d7a..fd205d0 100644
--- a/src/gpu/GrDirectContextPriv.h
+++ b/src/gpu/GrDirectContextPriv.h
@@ -11,6 +11,7 @@
 #include "include/core/SkSpan.h"
 #include "include/core/SkSurface.h"
 #include "include/gpu/GrDirectContext.h"
+#include "src/gpu/SkBaseGpuDevice.h"
 
 class GrAtlasManager;
 class GrBackendFormat;
@@ -145,6 +146,22 @@
         return fContext->fMappedBufferManager.get();
     }
 
+    sk_sp<SkBaseGpuDevice> createDevice(GrColorType,
+                                        sk_sp<GrSurfaceProxy>,
+                                        sk_sp<SkColorSpace>,
+                                        GrSurfaceOrigin,
+                                        const SkSurfaceProps&,
+                                        SkBaseGpuDevice::InitContents);
+    sk_sp<SkBaseGpuDevice> createDevice(SkBudgeted,
+                                        const SkImageInfo&,
+                                        SkBackingFit,
+                                        int sampleCount,
+                                        GrMipmapped,
+                                        GrProtected,
+                                        GrSurfaceOrigin,
+                                        const SkSurfaceProps&,
+                                        SkBaseGpuDevice::InitContents);
+
 #if GR_TEST_UTILS
     /** Reset GPU stats */
     void resetGpuStats() const;
diff --git a/src/gpu/GrRecordingContextPriv.cpp b/src/gpu/GrRecordingContextPriv.cpp
index 4c41abb..8204f45 100644
--- a/src/gpu/GrRecordingContextPriv.cpp
+++ b/src/gpu/GrRecordingContextPriv.cpp
@@ -9,6 +9,57 @@
 
 #include "src/gpu/GrCaps.h"
 #include "src/gpu/GrDrawingManager.h"
+#include "src/gpu/SkGpuDevice.h"
+#ifdef SK_NGA
+#include "src/gpu/SkGpuDevice_nga.h"
+#endif
+
+sk_sp<SkBaseGpuDevice> GrRecordingContextPriv::createDevice(GrColorType colorType,
+                                                            sk_sp<GrSurfaceProxy> proxy,
+                                                            sk_sp<SkColorSpace> colorSpace,
+                                                            GrSurfaceOrigin origin,
+                                                            const SkSurfaceProps& props,
+                                                            SkBaseGpuDevice::InitContents init) {
+#if GR_TEST_UTILS
+    if (this->options().fUseNGA == GrContextOptions::Enable::kYes) {
+#ifdef SK_NGA
+        return SkGpuDevice_nga::Make(fContext, colorType, std::move(proxy), std::move(colorSpace),
+                                     origin, props, init);
+#else
+        return nullptr;
+#endif
+    } else
+#endif
+    {
+        return SkGpuDevice::Make(fContext, colorType, std::move(proxy), std::move(colorSpace),
+                                 origin, props, init);
+    }
+}
+
+sk_sp<SkBaseGpuDevice> GrRecordingContextPriv::createDevice(SkBudgeted budgeted,
+                                                            const SkImageInfo& ii,
+                                                            SkBackingFit fit,
+                                                            int sampleCount,
+                                                            GrMipmapped mipmapped,
+                                                            GrProtected isProtected,
+                                                            GrSurfaceOrigin origin,
+                                                            const SkSurfaceProps& props,
+                                                            SkBaseGpuDevice::InitContents init) {
+#if GR_TEST_UTILS
+    if (this->options().fUseNGA == GrContextOptions::Enable::kYes) {
+#ifdef SK_NGA
+        return SkGpuDevice_nga::Make(fContext, budgeted, ii, fit, sampleCount,
+                                     mipmapped, isProtected, origin, props, init);
+#else
+        return nullptr;
+#endif
+    } else
+#endif
+    {
+        return SkGpuDevice::Make(fContext, budgeted, ii, fit, sampleCount,
+                                 mipmapped, isProtected, origin, props, init);
+    }
+}
 
 void GrRecordingContextPriv::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
     fContext->drawingManager()->moveRenderTasksToDDL(ddl);
diff --git a/src/gpu/GrRecordingContextPriv.h b/src/gpu/GrRecordingContextPriv.h
index 2acab7c..c52413a 100644
--- a/src/gpu/GrRecordingContextPriv.h
+++ b/src/gpu/GrRecordingContextPriv.h
@@ -10,6 +10,7 @@
 
 #include "include/core/SkPaint.h"
 #include "include/gpu/GrRecordingContext.h"
+#include "src/gpu/SkBaseGpuDevice.h"
 #include "src/gpu/text/GrSDFTControl.h"
 
 class SkDeferredDisplayList;
@@ -117,6 +118,22 @@
      */
     static sk_sp<GrRecordingContext> MakeDDL(sk_sp<GrContextThreadSafeProxy>);
 
+    sk_sp<SkBaseGpuDevice> createDevice(GrColorType,
+                                        sk_sp<GrSurfaceProxy>,
+                                        sk_sp<SkColorSpace>,
+                                        GrSurfaceOrigin,
+                                        const SkSurfaceProps&,
+                                        SkBaseGpuDevice::InitContents);
+    sk_sp<SkBaseGpuDevice> createDevice(SkBudgeted,
+                                        const SkImageInfo&,
+                                        SkBackingFit,
+                                        int sampleCount,
+                                        GrMipmapped,
+                                        GrProtected,
+                                        GrSurfaceOrigin,
+                                        const SkSurfaceProps&,
+                                        SkBaseGpuDevice::InitContents);
+
 private:
     explicit GrRecordingContextPriv(GrRecordingContext* context) : fContext(context) {}
     GrRecordingContextPriv(const GrRecordingContextPriv&) = delete;
diff --git a/src/gpu/SkGpuDevice_nga.h b/src/gpu/SkGpuDevice_nga.h
index dc3725c..42670a1 100644
--- a/src/gpu/SkGpuDevice_nga.h
+++ b/src/gpu/SkGpuDevice_nga.h
@@ -19,6 +19,29 @@
  */
 class SkGpuDevice_nga : public SkBaseGpuDevice  {
 public:
+    static sk_sp<SkBaseGpuDevice> Make(GrRecordingContext*,
+                                       GrColorType,
+                                       sk_sp<GrSurfaceProxy>,
+                                       sk_sp<SkColorSpace>,
+                                       GrSurfaceOrigin,
+                                       const SkSurfaceProps&,
+                                       InitContents) {
+        return nullptr;
+    }
+
+    static sk_sp<SkBaseGpuDevice> Make(GrRecordingContext*,
+                                       SkBudgeted,
+                                       const SkImageInfo&,
+                                       SkBackingFit,
+                                       int sampleCount,
+                                       GrMipmapped,
+                                       GrProtected,
+                                       GrSurfaceOrigin,
+                                       const SkSurfaceProps&,
+                                       InitContents) {
+        return nullptr;
+    }
+
     ~SkGpuDevice_nga() override;
 
     GrSurfaceProxyView readSurfaceView() override { return {}; }
diff --git a/src/gpu/vk/GrVkSecondaryCBDrawContext.cpp b/src/gpu/vk/GrVkSecondaryCBDrawContext.cpp
index 7bce083..391b7ed 100644
--- a/src/gpu/vk/GrVkSecondaryCBDrawContext.cpp
+++ b/src/gpu/vk/GrVkSecondaryCBDrawContext.cpp
@@ -19,7 +19,6 @@
 #include "src/gpu/GrProxyProvider.h"
 #include "src/gpu/GrRecordingContextPriv.h"
 #include "src/gpu/GrSurfaceDrawContext.h"
-#include "src/gpu/SkGpuDevice.h"
 
 sk_sp<GrVkSecondaryCBDrawContext> GrVkSecondaryCBDrawContext::Make(GrRecordingContext* rContext,
                                                                    const SkImageInfo& imageInfo,
@@ -42,13 +41,12 @@
 
     SkASSERT(proxy->isInstantiated());
 
-    auto device = SkGpuDevice::Make(rContext,
-                                    SkColorTypeToGrColorType(imageInfo.colorType()),
-                                    std::move(proxy),
-                                    imageInfo.refColorSpace(),
-                                    kTopLeft_GrSurfaceOrigin,
-                                    SkSurfacePropsCopyOrDefault(props),
-                                    SkBaseGpuDevice::kUninit_InitContents);
+    auto device = rContext->priv().createDevice(SkColorTypeToGrColorType(imageInfo.colorType()),
+                                                std::move(proxy),
+                                                imageInfo.refColorSpace(),
+                                                kTopLeft_GrSurfaceOrigin,
+                                                SkSurfacePropsCopyOrDefault(props),
+                                                SkBaseGpuDevice::kUninit_InitContents);
     if (!device) {
         return nullptr;
     }
diff --git a/src/gpu/vk/GrVkSecondaryCBDrawContext.h b/src/gpu/vk/GrVkSecondaryCBDrawContext.h
index dbf4c7a..54fcee0 100644
--- a/src/gpu/vk/GrVkSecondaryCBDrawContext.h
+++ b/src/gpu/vk/GrVkSecondaryCBDrawContext.h
@@ -15,9 +15,9 @@
 class GrBackendSemaphore;
 class GrRecordingContext;
 struct GrVkDrawableInfo;
+class SkBaseGpuDevice;
 class SkCanvas;
 class SkDeferredDisplayList;
-class SkBaseGpuDevice;
 struct SkImageInfo;
 class SkSurfaceCharacterization;
 class SkSurfaceProps;