[SkContext] Begin adding SkCapture Functionality

This Cl begins to add fEnableCapture to SkContextOptions and fCaptureManager to SkContext

Change-Id: I0041ed2929398b43ef96f1a8728ab129ff593d4d
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1379116
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Alexis Cruz-Ayala <alexisdavidc@google.com>
diff --git a/include/core/SkContext.h b/include/core/SkContext.h
index 23721e3..a5e2078 100644
--- a/include/core/SkContext.h
+++ b/include/core/SkContext.h
@@ -13,6 +13,8 @@
 
 #include <memory>
 
+class SkCapture;
+class SkCaptureManager;
 class SkContextPriv;
 class SkContextPrivConst;
 class SkResourceCache;
@@ -39,6 +41,16 @@
     SkContextPriv priv();
     SkContextPrivConst priv() const;
 
+    /*
+     * Starts the SkCapture. Must have set SkContextOptions::fEnableCapture to start.
+     */
+    void startCapture();
+
+    /*
+     * Ends the SkCapture and returns the collected draws and surface creation.
+     */
+    sk_sp<SkCapture> endCapture();
+
 private:
     SkContext(const SkContextOptions&);
 
@@ -63,6 +75,7 @@
 
     void dumpMemoryStatistics(SkTraceMemoryDump* dump);
 
+    sk_sp<SkCaptureManager> fCaptureManager;
     sk_sp<SkSharedContext> fSharedContext;
 };
 
diff --git a/include/core/SkContextOptions.h b/include/core/SkContextOptions.h
index 8f037f0..9b56ebd 100644
--- a/include/core/SkContextOptions.h
+++ b/include/core/SkContextOptions.h
@@ -50,6 +50,12 @@
      * paths. If the cache needs to allocate more, it will purge previous entries.
      */
     size_t fFontCacheLimit = 2 * 1024 * 1024;
+
+    /**
+     * This enables support for capturing SkCaptures. The client still must call startCapture and
+     * endCapture on the SkContext.
+     */
+    bool fEnableCapture = false;
 };
 
 #endif  // SkContextOptions_DEFINED
diff --git a/src/core/SkContext.cpp b/src/core/SkContext.cpp
index c2c5013..fbfa127 100644
--- a/src/core/SkContext.cpp
+++ b/src/core/SkContext.cpp
@@ -7,10 +7,13 @@
 
 #include "include/core/SkContext.h"
 
+#include "include/core/SkContextOptions.h"
 #include "include/core/SkRefCnt.h"
 #include "include/core/SkString.h"
 #include "include/core/SkTraceMemoryDump.h"
 #include "include/core/SkTypes.h"
+#include "src/capture/SkCapture.h"
+#include "src/capture/SkCaptureManager.h"
 #include "src/core/SkContextPriv.h"
 #include "src/core/SkResourceCache.h"
 #include "src/core/SkSharedContext.h"
@@ -19,6 +22,9 @@
 #include "src/core/SkTypefaceCache.h"
 
 SkContext::SkContext(const SkContextOptions& options) {
+    if (options.fEnableCapture) {
+        fCaptureManager = sk_make_sp<SkCaptureManager>();
+    }
     fSharedContext = sk_make_sp<SkSharedContext>(options);
 }
 
@@ -27,6 +33,23 @@
 SkContextPriv SkContext::priv() { return SkContextPriv(this); }
 SkContextPrivConst SkContext::priv() const { return SkContextPrivConst(this); }
 
+void SkContext::startCapture() {
+    if (!fCaptureManager) {
+        return;
+    }
+
+    fCaptureManager->toggleCapture(true);
+}
+
+sk_sp<SkCapture> SkContext::endCapture() {
+    if (!fCaptureManager) {
+        return nullptr;
+    }
+
+    fCaptureManager->toggleCapture(false);
+    return fCaptureManager->getLastCapture();
+}
+
 SkResourceCache* SkContext::resourceCache() const {
     return fSharedContext->synchronizedResourceCache();
 }