change drawPicture in SkRecord to just ref the picture
authorreed <reed@google.com>
Thu, 7 Aug 2014 19:19:50 +0000 (12:19 -0700)
committerCommit bot <commit-bot@chromium.org>
Thu, 7 Aug 2014 19:19:50 +0000 (12:19 -0700)
also fix some int/unsigned/size_t warnings

BUG=skia:
R=mtklein@google.com

Author: reed@google.com

Review URL: https://codereview.chromium.org/449933002

src/core/SkRecord.h
src/core/SkRecordDraw.cpp
src/core/SkRecorder.cpp
src/core/SkRecorder.h
src/core/SkRecords.h
tests/RecorderTest.cpp

index 6c5177e..96da69b 100644 (file)
@@ -64,7 +64,7 @@ public:
     // Allocate contiguous space for count Ts, to be freed when the SkRecord is destroyed.
     // Here T can be any class, not just those from SkRecords.  Throws on failure.
     template <typename T>
-    T* alloc(unsigned count = 1) {
+    T* alloc(size_t count = 1) {
         return (T*)fAlloc.allocThrow(sizeof(T) * count);
     }
 
index ac46319..c3c767c 100644 (file)
@@ -64,6 +64,7 @@ DRAW(DrawOval, drawOval(r.oval, r.paint));
 DRAW(DrawPaint, drawPaint(r.paint));
 DRAW(DrawPath, drawPath(r.path, r.paint));
 DRAW(DrawPatch, drawPatch(r.patch, r.paint));
+DRAW(DrawPicture, drawPicture(r.picture));
 DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint));
 DRAW(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint));
 DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint));
index 255eb39..19d60d5 100644 (file)
@@ -57,12 +57,12 @@ T* SkRecorder::copy(const T* src) {
 // This copy() is for arrays.
 // It will work with POD or non-POD, though currently we only use it for POD.
 template <typename T>
-T* SkRecorder::copy(const T src[], unsigned count) {
+T* SkRecorder::copy(const T src[], size_t count) {
     if (NULL == src) {
         return NULL;
     }
     T* dst = fRecord->alloc<T>(count);
-    for (unsigned i = 0; i < count; i++) {
+    for (size_t i = 0; i < count; i++) {
         SkNEW_PLACEMENT_ARGS(dst + i, T, (src[i]));
     }
     return dst;
@@ -72,7 +72,7 @@ T* SkRecorder::copy(const T src[], unsigned count) {
 // This measured around 2x faster for copying code points,
 // but I found no corresponding speedup for other arrays.
 template <>
-char* SkRecorder::copy(const char src[], unsigned count) {
+char* SkRecorder::copy(const char src[], size_t count) {
     if (NULL == src) {
         return NULL;
     }
@@ -187,7 +187,7 @@ void SkRecorder::onDrawTextOnPath(const void* text, size_t byteLength, const SkP
 }
 
 void SkRecorder::onDrawPicture(const SkPicture* picture) {
-    picture->draw(this);
+    APPEND(DrawPicture, picture);
 }
 
 void SkRecorder::drawVertices(VertexMode vmode,
index 6eba19e..4371138 100644 (file)
@@ -105,7 +105,7 @@ private:
     T* copy(const T*);
 
     template <typename T>
-    T* copy(const T[], unsigned count);
+    T* copy(const T[], size_t count);
 
     SkRecord* fRecord;
 };
index e6d98f7..1de1675 100644 (file)
@@ -9,6 +9,24 @@
 #define SkRecords_DEFINED
 
 #include "SkCanvas.h"
+#include "SkPicture.h"
+
+class SkPictureBox {
+public:
+    SkPictureBox(const SkPicture* obj) : fObj(SkRef(obj)) {}
+    SkPictureBox(const SkPictureBox& src) : fObj(SkRef(src.fObj)) {}
+    ~SkPictureBox() { fObj->unref(); }
+
+    SkPictureBox& operator=(const SkPictureBox& src) {
+        SkRefCnt_SafeAssign(fObj, src.fObj);
+        return *this;
+    }
+
+    operator const SkPicture*() const { return fObj; }
+
+private:
+    const SkPicture* fObj;
+};
 
 namespace SkRecords {
 
@@ -46,6 +64,7 @@ namespace SkRecords {
     M(DrawPaint)                                                    \
     M(DrawPath)                                                     \
     M(DrawPatch)                                                    \
+    M(DrawPicture)                                                  \
     M(DrawPoints)                                                   \
     M(DrawPosText)                                                  \
     M(DrawPosTextH)                                                 \
@@ -219,6 +238,7 @@ RECORD2(DrawOval, SkPaint, paint, SkRect, oval);
 RECORD1(DrawPaint, SkPaint, paint);
 RECORD2(DrawPath, SkPaint, paint, SkPath, path);
 RECORD2(DrawPatch, SkPaint, paint, SkPatch, patch);
+RECORD1(DrawPicture, SkPictureBox, picture);
 RECORD4(DrawPoints, SkPaint, paint, SkCanvas::PointMode, mode, size_t, count, SkPoint*, pts);
 RECORD4(DrawPosText, SkPaint, paint,
                      PODArray<char>, text,
index 5fcac4d..1ca9206 100644 (file)
@@ -7,6 +7,7 @@
 
 #include "Test.h"
 
+#include "SkPictureRecorder.h"
 #include "SkRecord.h"
 #include "SkRecorder.h"
 #include "SkRecords.h"
@@ -67,3 +68,25 @@ DEF_TEST(Recorder_RefLeaking, r) {
     }
     REPORTER_ASSERT(r, paint.getShader()->unique());
 }
+
+DEF_TEST(Recorder_RefPictures, r) {
+    SkAutoTUnref<SkPicture> pic;
+
+    {
+        SkPictureRecorder pr;
+        SkCanvas* canvas = pr.beginRecording(100, 100);
+        canvas->drawColor(SK_ColorRED);
+        pic.reset(pr.endRecording());
+    }
+    REPORTER_ASSERT(r, pic->unique());
+
+    {
+        SkRecord record;
+        SkRecorder recorder(&record, 100, 100);
+        recorder.drawPicture(pic);
+        // the recorder should now also be an owner
+        REPORTER_ASSERT(r, !pic->unique());
+    }
+    // the recorder destructor should have released us (back to unique)
+    REPORTER_ASSERT(r, pic->unique());
+}