Install a hook to swap between SkPicture backends with a single define.

BUG=skia:
R=robertphillips@google.com, reed@google.com, mtklein@google.com

Author: mtklein@chromium.org

Review URL: https://codereview.chromium.org/492023002
diff --git a/dm/DMSKPTask.cpp b/dm/DMSKPTask.cpp
index 3d02915..765fe10 100644
--- a/dm/DMSKPTask.cpp
+++ b/dm/DMSKPTask.cpp
@@ -5,47 +5,11 @@
 #include "SkCommandLineFlags.h"
 #include "SkPictureRecorder.h"
 
-DEFINE_bool(skr, true, "Test that SKPs draw the same when re-recorded with SkRecord backend.");
 DEFINE_int32(skpMaxWidth,  1000, "Max SKPTask viewport width.");
 DEFINE_int32(skpMaxHeight, 1000, "Max SKPTask viewport height.");
 
 namespace DM {
 
-// Test that an SkPicture plays back the same when re-recorded into an
-// SkPicture backed by SkRecord.
-class SkrComparisonTask : public CpuTask {
-public:
-    SkrComparisonTask(const Task& parent, const SkPicture* picture, SkBitmap reference)
-        : CpuTask(parent)
-        , fPicture(SkRef(picture))
-        , fReference(reference)
-        , fName(UnderJoin(parent.name().c_str(), "skr")) {}
-
-    virtual bool shouldSkip() const SK_OVERRIDE { return !FLAGS_skr; }
-    virtual SkString name() const SK_OVERRIDE { return fName; }
-
-    virtual void draw() SK_OVERRIDE {
-        SkPictureRecorder recorder;
-        fPicture->draw(recorder.EXPERIMENTAL_beginRecording(fPicture->width(), fPicture->height()));
-        SkAutoTDelete<const SkPicture> skrPicture(recorder.endRecording());
-
-        SkBitmap bitmap;
-        AllocatePixels(kN32_SkColorType, fReference.width(), fReference.height(), &bitmap);
-        DrawPicture(*skrPicture, &bitmap);
-
-        if (!BitmapsEqual(fReference, bitmap)) {
-            this->fail();
-            this->spawnChild(SkNEW_ARGS(WriteTask, (*this, bitmap)));
-        }
-    }
-
-private:
-    SkAutoTUnref<const SkPicture> fPicture;
-    const SkBitmap fReference;
-    const SkString fName;
-};
-
-
 SKPTask::SKPTask(Reporter* r, TaskRunner* tr, const SkPicture* pic, SkString filename)
     : CpuTask(r, tr), fPicture(SkRef(pic)), fName(FileToTaskName(filename)) {}
 
@@ -57,7 +21,6 @@
     DrawPicture(*fPicture, &bitmap);
 
     this->spawnChild(SkNEW_ARGS(WriteTask, (*this, bitmap)));
-    this->spawnChild(SkNEW_ARGS(SkrComparisonTask, (*this, fPicture.get(), bitmap)));
 }
 
 }  // namespace DM
diff --git a/dm/DMSerializeTask.cpp b/dm/DMSerializeTask.cpp
index 86fbff3..1ffe3ca 100644
--- a/dm/DMSerializeTask.cpp
+++ b/dm/DMSerializeTask.cpp
@@ -6,11 +6,11 @@
 #include "SkPicture.h"
 #include "SkPixelRef.h"
 
-DEFINE_bool(serialize, true, "If true, run picture serialization tests.");
-DECLARE_bool(skr);  // in DMReplayTask.cpp
+DEFINE_bool(serialize,     true, "If true, run picture serialization tests via SkPictureData.");
+DEFINE_bool(serialize_skr, true, "If true, run picture serialization tests via SkRecord.");
 
 static const char* kSuffixes[] = { "serialize", "serialize_skr" };
-static const bool* kEnabled[]  = { &FLAGS_serialize, &FLAGS_skr };
+static const bool* kEnabled[]  = { &FLAGS_serialize, &FLAGS_serialize_skr };
 
 namespace DM {
 
diff --git a/dm/DMUtil.cpp b/dm/DMUtil.cpp
index 92ce381..33af18e 100644
--- a/dm/DMUtil.cpp
+++ b/dm/DMUtil.cpp
@@ -26,7 +26,7 @@
     SkPictureRecorder recorder;
 
     SkCanvas* canvas = skr ? recorder.EXPERIMENTAL_beginRecording(w, h, factory)
-                           : recorder.             beginRecording(w, h, factory);
+                           : recorder.  DEPRECATED_beginRecording(w, h, factory);
     canvas->concat(gm->getInitialTransform());
     gm->draw(canvas);
     canvas->flush();
diff --git a/gm/optimizations.cpp b/gm/optimizations.cpp
index d810420..a33f457 100644
--- a/gm/optimizations.cpp
+++ b/gm/optimizations.cpp
@@ -82,7 +82,7 @@
 
     SkPictureRecorder recorder;
 
-    SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
+    SkCanvas* canvas = recorder.DEPRECATED_beginRecording(100, 100, NULL, 0);
     // have to disable the optimizations while generating the picture
     recorder.internalOnly_EnableOpts(false);
 
@@ -216,7 +216,7 @@
 
     SkPictureRecorder recorder;
 
-    SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
+    SkCanvas* canvas = recorder.DEPRECATED_beginRecording(100, 100, NULL, 0);
     // have to disable the optimizations while generating the picture
     recorder.internalOnly_EnableOpts(false);
 
@@ -359,7 +359,8 @@
             // re-render the 'pre' picture and thus 'apply' the optimization
             SkPictureRecorder recorder;
 
-            SkCanvas* recordCanvas = recorder.beginRecording(pre->width(), pre->height(), NULL, 0);
+            SkCanvas* recordCanvas =
+                recorder.DEPRECATED_beginRecording(pre->width(), pre->height(), NULL, 0);
 
             pre->draw(recordCanvas);
 
diff --git a/gyp/common_conditions.gypi b/gyp/common_conditions.gypi
index 4fd9bf8..9a436f3 100644
--- a/gyp/common_conditions.gypi
+++ b/gyp/common_conditions.gypi
@@ -6,6 +6,7 @@
     'SK_SUPPORT_GPU=<(skia_gpu)',
     'SK_SUPPORT_OPENCL=<(skia_opencl)',
     'SK_FORCE_DISTANCEFIELD_FONTS=<(skia_force_distancefield_fonts)',
+    'SK_PICTURE_USE_SK_RECORD',
   ],
   'conditions' : [
     ['skia_pic', {
diff --git a/include/core/SkPictureRecorder.h b/include/core/SkPictureRecorder.h
index c00d1b3..bd86148 100644
--- a/include/core/SkPictureRecorder.h
+++ b/include/core/SkPictureRecorder.h
@@ -41,7 +41,17 @@
                              SkBBHFactory* bbhFactory = NULL,
                              uint32_t recordFlags = 0);
 
-    /** Same as beginRecording(), using a new faster backend. */
+    // As usual, we have a deprecated old version and a maybe almost working
+    // new version.  We currently point beginRecording() to
+    // DEPRECATED_beginRecording() unless SK_PICTURE_USE_SK_RECORD is defined,
+    // then we use EXPERIMENTAL_beginRecording().
+
+    // Old slower backend.
+    SkCanvas* DEPRECATED_beginRecording(int width, int height,
+                                        SkBBHFactory* bbhFactory = NULL,
+                                        uint32_t recordFlags = 0);
+
+    // New faster backend.
     SkCanvas* EXPERIMENTAL_beginRecording(int width, int height,
                                           SkBBHFactory* bbhFactory = NULL);
 
diff --git a/src/core/SkPicture.cpp b/src/core/SkPicture.cpp
index 515f28d..d1a81a5 100644
--- a/src/core/SkPicture.cpp
+++ b/src/core/SkPicture.cpp
@@ -279,7 +279,8 @@
 // This for compatibility with serialization code only.  This is not cheap.
 static SkPicture* backport(const SkRecord& src, int width, int height) {
     SkPictureRecorder recorder;
-    SkRecordDraw(src, recorder.beginRecording(width, height), NULL/*bbh*/, NULL/*callback*/);
+    SkRecordDraw(src,
+                 recorder.DEPRECATED_beginRecording(width, height), NULL/*bbh*/, NULL/*callback*/);
     return recorder.endRecording();
 }
 
diff --git a/src/core/SkPictureRecorder.cpp b/src/core/SkPictureRecorder.cpp
index 7d24caf..d90f885 100644
--- a/src/core/SkPictureRecorder.cpp
+++ b/src/core/SkPictureRecorder.cpp
@@ -20,6 +20,16 @@
 SkCanvas* SkPictureRecorder::beginRecording(int width, int height,
                                             SkBBHFactory* bbhFactory /* = NULL */,
                                             uint32_t recordFlags /* = 0 */) {
+#ifdef SK_PICTURE_USE_SK_RECORD
+    return EXPERIMENTAL_beginRecording(width, height, bbhFactory);
+#else
+    return DEPRECATED_beginRecording(width, height, bbhFactory, recordFlags);
+#endif
+}
+
+SkCanvas* SkPictureRecorder::DEPRECATED_beginRecording(int width, int height,
+                                                       SkBBHFactory* bbhFactory /* = NULL */,
+                                                       uint32_t recordFlags /* = 0 */) {
     fWidth = width;
     fHeight = height;
 
diff --git a/tests/CanvasTest.cpp b/tests/CanvasTest.cpp
index 217cf03..5d6a8d8 100644
--- a/tests/CanvasTest.cpp
+++ b/tests/CanvasTest.cpp
@@ -687,13 +687,13 @@
         // are flattened during the second execution
         testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
         SkPictureRecorder referenceRecorder;
-        SkCanvas* referenceCanvas = referenceRecorder.beginRecording(kWidth, kHeight,
-                                                                     NULL, recordFlags);
+        SkCanvas* referenceCanvas =
+            referenceRecorder.DEPRECATED_beginRecording(kWidth, kHeight, NULL, recordFlags);
         testStep->draw(referenceCanvas, reporter);
 
         SkPictureRecorder testRecorder;
-        SkCanvas* testCanvas = testRecorder.beginRecording(kWidth, kHeight,
-                                                           NULL, recordFlags);
+        SkCanvas* testCanvas =
+            testRecorder.DEPRECATED_beginRecording(kWidth, kHeight, NULL, recordFlags);
         testStep->draw(testCanvas, reporter);
         testStep->setAssertMessageFormat(kPictureSecondDrawAssertMessageFormat);
         testStep->draw(testCanvas, reporter);
diff --git a/tests/PictureTest.cpp b/tests/PictureTest.cpp
index cb29b37..c87d9c7 100644
--- a/tests/PictureTest.cpp
+++ b/tests/PictureTest.cpp
@@ -580,7 +580,7 @@
 
 #define GENERATE_CANVAS(recorder, x) \
     (x) ? recorder.EXPERIMENTAL_beginRecording(100, 100) \
-        : recorder.beginRecording(100,100);
+        : recorder.  DEPRECATED_beginRecording(100,100);
 
 /* Hit a few SkPicture::Analysis cases not handled elsewhere. */
 static void test_analysis(skiatest::Reporter* reporter, bool useNewPath) {
@@ -897,7 +897,7 @@
         {
             SkPictureRecorder recorder;
 
-            SkCanvas* c = recorder.beginRecording(kWidth, kHeight);
+            SkCanvas* c = recorder.DEPRECATED_beginRecording(kWidth, kHeight);
             // 1)
             c->saveLayer(NULL, NULL);
             c->restore();
@@ -1002,7 +1002,7 @@
 static void test_has_text(skiatest::Reporter* reporter, bool useNewPath) {
     SkPictureRecorder recorder;
 #define BEGIN_RECORDING useNewPath ? recorder.EXPERIMENTAL_beginRecording(100, 100) \
-                                   : recorder.             beginRecording(100, 100)
+                                   : recorder.  DEPRECATED_beginRecording(100, 100)
 
     SkCanvas* canvas = BEGIN_RECORDING;
     {
diff --git a/tools/bench_playback.cpp b/tools/bench_playback.cpp
index a4f3a6a..f6c3ca4 100644
--- a/tools/bench_playback.cpp
+++ b/tools/bench_playback.cpp
@@ -45,7 +45,7 @@
 
     SkPictureRecorder recorder;
     src.draw(skr ? recorder.EXPERIMENTAL_beginRecording(src.width(), src.height(), &factory)
-                 : recorder.             beginRecording(src.width(), src.height(), &factory));
+                 : recorder.  DEPRECATED_beginRecording(src.width(), src.height(), &factory));
     return recorder.endRecording();
 }
 
diff --git a/tools/bench_record.cpp b/tools/bench_record.cpp
index 4f928ee..d102250 100644
--- a/tools/bench_record.cpp
+++ b/tools/bench_record.cpp
@@ -66,7 +66,7 @@
     if (FLAGS_skr) {
         src.draw(recorder.EXPERIMENTAL_beginRecording(src.width(), src.height(), bbhFactory));
     } else {
-        src.draw(recorder.beginRecording(src.width(), src.height(), bbhFactory));
+        src.draw(recorder.  DEPRECATED_beginRecording(src.width(), src.height(), bbhFactory));
     }
     SkAutoTUnref<SkPicture> pic(recorder.endRecording());
 }