dedup images/blobs/pictures by ID in old serialization format
authorreed <reed@google.com>
Mon, 29 Aug 2016 14:52:13 +0000 (07:52 -0700)
committerCommit bot <commit-bot@chromium.org>
Mon, 29 Aug 2016 14:52:13 +0000 (07:52 -0700)
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2289783002

Review-Url: https://codereview.chromium.org/2289783002

include/private/SkTDArray.h
src/core/SkPictureRecord.cpp

index d6ef3a3834141d4d1ee14021617926169fdfbe4b..f71d3570056bc33a414ab8171a8ab788dbb37297 100644 (file)
@@ -217,6 +217,18 @@ public:
         }
     }
 
+    template <typename S> int select(S&& selector) const {
+        const T* iter = fArray;
+        const T* stop = fArray + fCount;
+
+        for (; iter < stop; iter++) {
+            if (selector(*iter)) {
+                return SkToInt(iter - fArray);
+            }
+        }
+        return -1;
+    }
+    
     int find(const T& elem) const {
         const T* iter = fArray;
         const T* stop = fArray + fCount;
index d7716998802a914f00a8c89f6dfed562018b9af9..8eab63016b82998365c8a12ea3ef376046bad1b4 100644 (file)
@@ -867,18 +867,24 @@ void SkPictureRecord::onDrawAnnotation(const SkRect& rect, const char key[], SkD
 
 ///////////////////////////////////////////////////////////////////////////////
 
+template <typename T> int find_or_append_uniqueID(SkTDArray<const T*>& array, const T* obj) {
+    int index = array.select([&](const T* elem) {
+        return elem->uniqueID() == obj->uniqueID();
+    });
+    if (index < 0) {
+        index = array.count();
+        *array.append() = SkRef(obj);
+    }
+    return index;
+}
+
 sk_sp<SkSurface> SkPictureRecord::onNewSurface(const SkImageInfo& info, const SkSurfaceProps&) {
     return nullptr;
 }
 
 void SkPictureRecord::addImage(const SkImage* image) {
-    int index = fImageRefs.find(image);
-    if (index >= 0) {
-        this->addInt(index);
-    } else {
-        *fImageRefs.append() = SkRef(image);
-        this->addInt(fImageRefs.count()-1);
-    }
+    // convention for images is 0-based index
+    this->addInt(find_or_append_uniqueID(fImageRefs, image));
 }
 
 void SkPictureRecord::addMatrix(const SkMatrix& matrix) {
@@ -914,14 +920,8 @@ void SkPictureRecord::addPatch(const SkPoint cubics[12]) {
 }
 
 void SkPictureRecord::addPicture(const SkPicture* picture) {
-    int index = fPictureRefs.find(picture);
-    if (index < 0) {    // not found
-        index = fPictureRefs.count();
-        *fPictureRefs.append() = picture;
-        picture->ref();
-    }
     // follow the convention of recording a 1-based index
-    this->addInt(index + 1);
+    this->addInt(find_or_append_uniqueID(fPictureRefs, picture) + 1);
 }
 
 void SkPictureRecord::addDrawable(SkDrawable* drawable) {
@@ -982,12 +982,9 @@ void SkPictureRecord::addText(const void* text, size_t byteLength) {
     fWriter.writePad(text, byteLength);
 }
 
-void SkPictureRecord::addTextBlob(const SkTextBlob *blob) {
-    int index = fTextBlobRefs.count();
-    *fTextBlobRefs.append() = blob;
-    blob->ref();
+void SkPictureRecord::addTextBlob(const SkTextBlob* blob) {
     // follow the convention of recording a 1-based index
-    this->addInt(index + 1);
+    this->addInt(find_or_append_uniqueID(fTextBlobRefs, blob) + 1);
 }
 
 ///////////////////////////////////////////////////////////////////////////////