Update RecordReplaceDrawTest to generate and pass pictures

This CL splits the unit test changes out of (Fix sub-picture layer rendering bugs - https://codereview.chromium.org/597293002/).

For various reasons GrRecordReplaceDraw now needs to take an SkPicture (rather than an SkRecord and a BBH).

R=egdaniel@google.com, bsalomon@google.com

Author: robertphillips@google.com

Review URL: https://codereview.chromium.org/608823002
diff --git a/include/core/SkPicture.h b/include/core/SkPicture.h
index d8aced1..af4f123 100644
--- a/include/core/SkPicture.h
+++ b/include/core/SkPicture.h
@@ -20,6 +20,7 @@
 class GrContext;
 #endif
 
+class GrReplacements;
 class SkBBoxHierarchy;
 class SkCanvas;
 class SkData;
@@ -299,7 +300,11 @@
     friend class GrLayerHoister;               // access to fRecord
     friend class CollectLayers;                // access to fRecord
     friend class SkPicturePlayback;            // to get fData & OperationList
-    friend class SkPictureReplacementPlayback; // to access OperationList
+    friend void GrRecordReplaceDraw(const SkPicture* picture,
+                                    SkCanvas* canvas,
+                                    const GrReplacements* replacements,
+                                    const SkMatrix& initialMatrix,
+                                    SkDrawPictureCallback* callback);
 
     typedef SkRefCnt INHERITED;
 
diff --git a/src/gpu/GrRecordReplaceDraw.cpp b/src/gpu/GrRecordReplaceDraw.cpp
index b0e00b8a..acdc63f 100644
--- a/src/gpu/GrRecordReplaceDraw.cpp
+++ b/src/gpu/GrRecordReplaceDraw.cpp
@@ -66,19 +66,23 @@
     canvas->restore();
 }
 
-void GrRecordReplaceDraw(const SkRecord& record,
+void GrRecordReplaceDraw(const SkPicture* picture,
                          SkCanvas* canvas,
-                         const SkBBoxHierarchy* bbh,
                          const GrReplacements* replacements,
+                         const SkMatrix& initialMatrix,
                          SkDrawPictureCallback* callback) {
     SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/);
 
+    const SkBBoxHierarchy* bbh = picture->fBBH.get();
+    const SkRecord* record = picture->fRecord.get();
+    if (NULL == record) {
+        return;
+    }
+
     SkRecords::Draw draw(canvas);
     const GrReplacements::ReplacementInfo* ri = NULL;
     int searchStart = 0;
 
-    const SkMatrix initialMatrix = canvas->getTotalMatrix();
-
     if (bbh) {
         // Draw only ops that affect pixels in the canvas's current clip.
         // The SkRecord and BBH were recorded in identity space.  This canvas
@@ -106,10 +110,10 @@
                 continue;
             }
 
-            record.visit<void>((uintptr_t)ops[i], draw);
+            record->visit<void>((uintptr_t)ops[i], draw);
         }
     } else {
-        for (unsigned int i = 0; i < record.count(); ++i) {
+        for (unsigned int i = 0; i < record->count(); ++i) {
             if (callback && callback->abortDrawing()) {
                 return;
             }
@@ -120,7 +124,7 @@
                 continue;
             }
 
-            record.visit<void>(i, draw);
+            record->visit<void>(i, draw);
         }
     }
 }
diff --git a/src/gpu/GrRecordReplaceDraw.h b/src/gpu/GrRecordReplaceDraw.h
index 3ba43dc..4900e0d 100644
--- a/src/gpu/GrRecordReplaceDraw.h
+++ b/src/gpu/GrRecordReplaceDraw.h
@@ -16,7 +16,9 @@
 class SkBitmap;
 class SkCanvas;
 class SkImage;
+class SkMatrix;
 class SkPaint;
+class SkPicture;
 class SkRecord;
 
 // GrReplacements collects op ranges that can be replaced with
@@ -58,12 +60,12 @@
 #endif
 };
 
-// Draw an SkRecord into an SkCanvas replacing saveLayer/restore blocks with
+// Draw an SkPicture into an SkCanvas replacing saveLayer/restore blocks with
 // drawBitmap calls.  A convenience wrapper around SkRecords::Draw.
-void GrRecordReplaceDraw(const SkRecord&, 
+void GrRecordReplaceDraw(const SkPicture*,
                          SkCanvas*,
-                         const SkBBoxHierarchy*,
                          const GrReplacements*,
+                         const SkMatrix&,
                          SkDrawPictureCallback*);
 
 #endif // GrRecordReplaceDraw_DEFINED
diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp
index b647613..bd075cf 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -1864,8 +1864,9 @@
     GrLayerHoister::DrawLayers(atlased, nonAtlased, &replacements);
 
     // Render the entire picture using new layers
-    GrRecordReplaceDraw(*mainPicture->fRecord, mainCanvas, mainPicture->fBBH.get(), 
-                        &replacements, NULL);
+    const SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
+
+    GrRecordReplaceDraw(mainPicture, mainCanvas, &replacements, initialMatrix, NULL);
 
     GrLayerHoister::UnlockLayers(fContext->getLayerCache(), atlased, nonAtlased);
 
diff --git a/tests/RecordReplaceDrawTest.cpp b/tests/RecordReplaceDrawTest.cpp
index 618be72..5b6d12c 100644
--- a/tests/RecordReplaceDrawTest.cpp
+++ b/tests/RecordReplaceDrawTest.cpp
@@ -8,13 +8,14 @@
 #if SK_SUPPORT_GPU
 
 #include "Test.h"
-#include "RecordTestUtils.h"
 
+#include "GrRecordReplaceDraw.h"
+#include "RecordTestUtils.h"
 #include "SkBBHFactory.h"
+#include "SkPictureRecorder.h"
 #include "SkRecordDraw.h"
 #include "SkRecorder.h"
 #include "SkUtils.h"
-#include "GrRecordReplaceDraw.h"
 
 static const int kWidth = 100;
 static const int kHeight = 100;
@@ -30,18 +31,25 @@
 
 // Make sure the abort callback works
 DEF_TEST(RecordReplaceDraw_Abort, r) {
-    // Record two commands.
-    SkRecord record;
-    SkRecorder recorder(&record, kWidth, kHeight);
-    recorder.drawRect(SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight)), SkPaint());
-    recorder.clipRect(SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight)));
+    SkAutoTUnref<const SkPicture> pic;
+
+    {
+        // Record two commands.
+        SkPictureRecorder recorder;
+        SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
+
+        canvas->drawRect(SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight)), SkPaint());
+        canvas->clipRect(SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight)));
+
+        pic.reset(recorder.endRecording());
+    }
 
     SkRecord rerecord;
     SkRecorder canvas(&rerecord, kWidth, kHeight);
 
     GrReplacements replacements;
     JustOneDraw callback;
-    GrRecordReplaceDraw(record, &canvas, NULL/*bbh*/, &replacements, &callback);
+    GrRecordReplaceDraw(pic, &canvas, &replacements, SkMatrix::I(), &callback);
 
     REPORTER_ASSERT(r, 3 == rerecord.count());
     assert_type<SkRecords::Save>(r, rerecord, 0);
@@ -51,15 +59,23 @@
 
 // Make sure GrRecordReplaceDraw balances unbalanced saves
 DEF_TEST(RecordReplaceDraw_Unbalanced, r) {
-    SkRecord record;
-    SkRecorder recorder(&record, kWidth, kHeight);
-    recorder.save();  // We won't balance this, but GrRecordReplaceDraw will for us.
+    SkAutoTUnref<const SkPicture> pic;
+
+    {
+        SkPictureRecorder recorder;
+        SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
+
+        // We won't balance this, but GrRecordReplaceDraw will for us.
+        canvas->save();
+
+        pic.reset(recorder.endRecording());
+    }
 
     SkRecord rerecord;
     SkRecorder canvas(&rerecord, kWidth, kHeight);
 
     GrReplacements replacements;
-    GrRecordReplaceDraw(record, &canvas, NULL/*bbh*/, &replacements, NULL/*callback*/);
+    GrRecordReplaceDraw(pic, &canvas, &replacements, SkMatrix::I(), NULL/*callback*/);
 
     REPORTER_ASSERT(r, 4 == rerecord.count());
     assert_type<SkRecords::Save>(r, rerecord, 0);
@@ -82,14 +98,23 @@
 
 // Test out the layer replacement functionality with and w/o a BBH
 void test_replacements(skiatest::Reporter* r, bool useBBH) {
-    SkRecord record;
-    SkRecorder recorder(&record, kWidth, kHeight);
-    SkAutoTDelete<SkPaint> paint(SkNEW(SkPaint));
-    recorder.saveLayer(NULL, paint);
-    recorder.clear(SK_ColorRED);
-    recorder.restore();
-    recorder.drawRect(SkRect::MakeWH(SkIntToScalar(kWidth/2), SkIntToScalar(kHeight/2)), 
-                      SkPaint());
+    SkAutoTUnref<const SkPicture> pic;
+
+    {
+        SkRTreeFactory bbhFactory;
+        SkPictureRecorder recorder;
+        SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight), 
+                                                   useBBH ? &bbhFactory : NULL);
+
+        SkAutoTDelete<SkPaint> paint(SkNEW(SkPaint));
+        canvas->saveLayer(NULL, paint);
+        canvas->clear(SK_ColorRED);
+        canvas->restore();
+        canvas->drawRect(SkRect::MakeWH(SkIntToScalar(kWidth / 2), SkIntToScalar(kHeight / 2)),
+                         SkPaint());
+
+        pic.reset(recorder.endRecording());
+    }
 
     GrReplacements replacements;
     GrReplacements::ReplacementInfo* ri = replacements.push();
@@ -102,15 +127,9 @@
 
     SkAutoTUnref<SkBBoxHierarchy> bbh;
 
-    if (useBBH) {
-        SkRTreeFactory factory;
-        bbh.reset((factory)(kWidth, kHeight));
-        SkRecordFillBounds(record, bbh);
-    }
-
     SkRecord rerecord;
     SkRecorder canvas(&rerecord, kWidth, kHeight);
-    GrRecordReplaceDraw(record, &canvas, bbh, &replacements, NULL/*callback*/);
+    GrRecordReplaceDraw(pic, &canvas, &replacements, SkMatrix::I(), NULL/*callback*/);
 
     REPORTER_ASSERT(r, 7 == rerecord.count());
     assert_type<SkRecords::Save>(r, rerecord, 0);