Create SkLayerRasterizer w/o destroying Builder.
Add a new method to SkLayerRasterizer::Builder that creates a new
SkLayerRasterizer without destroying the Builder. Necessary to
continue to support Android's API.
Also fix a bug where creating a Builder and never calling
detachRasterizer results in not calling the destructor for any SkPaints
in the Builder.
Add tests.
BUG=b/13729784
R=reed@google.com, dominikg@chromium.org
Author: scroggo@google.com
Review URL: https://codereview.chromium.org/233673002
git-svn-id: http://skia.googlecode.com/svn/trunk@14139 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gyp/tests.gypi b/gyp/tests.gypi
index cf8e8ef..6fbcc03 100644
--- a/gyp/tests.gypi
+++ b/gyp/tests.gypi
@@ -102,6 +102,7 @@
'../tests/JpegTest.cpp',
'../tests/LListTest.cpp',
'../tests/LayerDrawLooperTest.cpp',
+ '../tests/LayerRasterizerTest.cpp',
'../tests/MD5Test.cpp',
'../tests/MallocPixelRefTest.cpp',
'../tests/MathTest.cpp',
diff --git a/include/effects/SkLayerRasterizer.h b/include/effects/SkLayerRasterizer.h
index fdfb179..fc21a7c 100644
--- a/include/effects/SkLayerRasterizer.h
+++ b/include/effects/SkLayerRasterizer.h
@@ -39,10 +39,26 @@
/**
* Pass queue of layers on to newly created layer rasterizer and return it. The builder
- * cannot be used any more after calling this function.
+ * *cannot* be used any more after calling this function.
+ *
+ * The caller is responsible for calling unref() on the returned object.
*/
SkLayerRasterizer* detachRasterizer();
+ /**
+ * Create and return a new immutable SkLayerRasterizer that contains a shapshot of the
+ * layers that were added to the Builder, without modifying the Builder. The Builder
+ * *may* be used after calling this function. It will continue to hold any layers
+ * previously added, so consecutive calls to this function will return identical objects,
+ * and objects returned by future calls to this function contain all the layers in
+ * previously returned objects.
+ *
+ * Future calls to addLayer will not affect rasterizers previously returned by this call.
+ *
+ * The caller is responsible for calling unref() on the returned object.
+ */
+ SkLayerRasterizer* snapshotRasterizer() const;
+
private:
SkDeque* fLayers;
};
@@ -86,6 +102,8 @@
static SkDeque* ReadLayers(SkReadBuffer& buffer);
+ friend class LayerRasterizerTester;
+
typedef SkRasterizer INHERITED;
};
diff --git a/src/effects/SkLayerRasterizer.cpp b/src/effects/SkLayerRasterizer.cpp
index e35c5e8..7143a3b 100644
--- a/src/effects/SkLayerRasterizer.cpp
+++ b/src/effects/SkLayerRasterizer.cpp
@@ -34,15 +34,20 @@
{
}
-SkLayerRasterizer::~SkLayerRasterizer() {
- SkASSERT(fLayers);
- SkDeque::F2BIter iter(*fLayers);
+// Helper function to call destructors on SkPaints held by layers and delete layers.
+static void clean_up_layers(SkDeque* layers) {
+ SkDeque::F2BIter iter(*layers);
SkLayerRasterizer_Rec* rec;
while ((rec = (SkLayerRasterizer_Rec*)iter.next()) != NULL)
rec->fPaint.~SkPaint();
- SkDELETE(fLayers);
+ SkDELETE(layers);
+}
+
+SkLayerRasterizer::~SkLayerRasterizer() {
+ SkASSERT(fLayers);
+ clean_up_layers(const_cast<SkDeque*>(fLayers));
}
#ifdef SK_SUPPORT_LEGACY_LAYERRASTERIZER_API
@@ -194,7 +199,9 @@
SkLayerRasterizer::Builder::~Builder()
{
- SkDELETE(fLayers);
+ if (fLayers != NULL) {
+ clean_up_layers(fLayers);
+ }
}
void SkLayerRasterizer::Builder::addLayer(const SkPaint& paint, SkScalar dx,
@@ -211,3 +218,20 @@
fLayers = NULL;
return rasterizer;
}
+
+SkLayerRasterizer* SkLayerRasterizer::Builder::snapshotRasterizer() const {
+ SkDeque* layers = SkNEW_ARGS(SkDeque, (sizeof(SkLayerRasterizer_Rec), fLayers->count()));
+ SkDeque::F2BIter iter(*fLayers);
+ const SkLayerRasterizer_Rec* recOrig;
+ SkDEBUGCODE(int count = 0;)
+ while ((recOrig = static_cast<SkLayerRasterizer_Rec*>(iter.next())) != NULL) {
+ SkDEBUGCODE(count++);
+ SkLayerRasterizer_Rec* recCopy = static_cast<SkLayerRasterizer_Rec*>(layers->push_back());
+ SkNEW_PLACEMENT_ARGS(&recCopy->fPaint, SkPaint, (recOrig->fPaint));
+ recCopy->fOffset = recOrig->fOffset;
+ }
+ SkASSERT(fLayers->count() == count);
+ SkASSERT(layers->count() == count);
+ SkLayerRasterizer* rasterizer = SkNEW_ARGS(SkLayerRasterizer, (layers));
+ return rasterizer;
+}
diff --git a/tests/LayerRasterizerTest.cpp b/tests/LayerRasterizerTest.cpp
new file mode 100644
index 0000000..b393525
--- /dev/null
+++ b/tests/LayerRasterizerTest.cpp
@@ -0,0 +1,138 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkDeque.h"
+#include "SkLayerRasterizer.h"
+#include "SkPaint.h"
+#include "SkRasterizer.h"
+#include "Test.h"
+
+class SkReadBuffer;
+
+// Dummy class to place on a paint just to ensure the paint's destructor
+// is called.
+// ONLY to be used by LayerRasterizer_destructor, since other tests may
+// be run in a separate thread, and this class is not threadsafe.
+class DummyRasterizer : public SkRasterizer {
+public:
+ DummyRasterizer()
+ : INHERITED()
+ {
+ // Not threadsafe. Only used in one thread.
+ gCount++;
+ }
+
+ ~DummyRasterizer() {
+ // Not threadsafe. Only used in one thread.
+ gCount--;
+ }
+
+ static int GetCount() { return gCount; }
+
+ SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(DummyRasterizer)
+
+private:
+ DummyRasterizer(SkReadBuffer&) {}
+
+ static int gCount;
+
+ typedef SkRasterizer INHERITED;
+};
+
+int DummyRasterizer::gCount;
+
+// Check to make sure that the SkPaint in the layer has its destructor called.
+DEF_TEST(LayerRasterizer_destructor, reporter) {
+ {
+ SkPaint paint;
+ paint.setRasterizer(SkNEW(DummyRasterizer))->unref();
+ REPORTER_ASSERT(reporter, DummyRasterizer::GetCount() == 1);
+
+ SkLayerRasterizer::Builder builder;
+ builder.addLayer(paint);
+ }
+ REPORTER_ASSERT(reporter, DummyRasterizer::GetCount() == 0);
+}
+
+class LayerRasterizerTester {
+public:
+ static int CountLayers(const SkLayerRasterizer& layerRasterizer) {
+ return layerRasterizer.fLayers->count();
+ }
+
+ static const SkDeque& GetLayers(const SkLayerRasterizer& layerRasterizer) {
+ return *layerRasterizer.fLayers;
+ }
+};
+
+// MUST stay in sync with definition of SkLayerRasterizer_Rec in SkLayerRasterizer.cpp.
+struct SkLayerRasterizer_Rec {
+ SkPaint fPaint;
+ SkVector fOffset;
+};
+
+static bool equals(const SkLayerRasterizer_Rec& rec1, const SkLayerRasterizer_Rec& rec2) {
+ return rec1.fPaint == rec2.fPaint && rec1.fOffset == rec2.fOffset;
+}
+
+DEF_TEST(LayerRasterizer_copy, reporter) {
+ SkLayerRasterizer::Builder builder;
+ SkPaint paint;
+ // Create a bunch of paints with different flags.
+ for (uint32_t flags = 0x01; flags < SkPaint::kAllFlags; flags <<= 1) {
+ paint.setFlags(flags);
+ builder.addLayer(paint, flags, flags);
+ }
+
+ // Create a layer rasterizer with all the existing layers.
+ SkAutoTUnref<SkLayerRasterizer> firstCopy(builder.snapshotRasterizer());
+
+ // Add one more layer.
+ paint.setFlags(SkPaint::kAllFlags);
+ builder.addLayer(paint);
+
+ SkAutoTUnref<SkLayerRasterizer> oneLarger(builder.snapshotRasterizer());
+ SkAutoTUnref<SkLayerRasterizer> detached(builder.detachRasterizer());
+
+ // Check the counts for consistency.
+ const int largerCount = LayerRasterizerTester::CountLayers(*oneLarger.get());
+ const int smallerCount = LayerRasterizerTester::CountLayers(*firstCopy.get());
+ REPORTER_ASSERT(reporter, largerCount == LayerRasterizerTester::CountLayers(*detached.get()));
+ REPORTER_ASSERT(reporter, smallerCount == largerCount - 1);
+
+ const SkLayerRasterizer_Rec* recFirstCopy = NULL;
+ const SkLayerRasterizer_Rec* recOneLarger = NULL;
+ const SkLayerRasterizer_Rec* recDetached = NULL;
+
+ const SkDeque& layersFirstCopy = LayerRasterizerTester::GetLayers(*firstCopy.get());
+ const SkDeque& layersOneLarger = LayerRasterizerTester::GetLayers(*oneLarger.get());
+ const SkDeque& layersDetached = LayerRasterizerTester::GetLayers(*detached.get());
+
+ // Ensure that our version of SkLayerRasterizer_Rec is the same as the one in
+ // SkLayerRasterizer.cpp - or at least the same size. If the order were switched, we
+ // would fail the test elsewhere.
+ REPORTER_ASSERT(reporter, layersFirstCopy.elemSize() == sizeof(SkLayerRasterizer_Rec));
+ REPORTER_ASSERT(reporter, layersOneLarger.elemSize() == sizeof(SkLayerRasterizer_Rec));
+ REPORTER_ASSERT(reporter, layersDetached.elemSize() == sizeof(SkLayerRasterizer_Rec));
+
+ SkDeque::F2BIter iterFirstCopy(layersFirstCopy);
+ SkDeque::F2BIter iterOneLarger(layersOneLarger);
+ SkDeque::F2BIter iterDetached(layersDetached);
+
+ for (int i = 0; i < largerCount; ++i) {
+ recFirstCopy = static_cast<const SkLayerRasterizer_Rec*>(iterFirstCopy.next());
+ recOneLarger = static_cast<const SkLayerRasterizer_Rec*>(iterOneLarger.next());
+ recDetached = static_cast<const SkLayerRasterizer_Rec*>(iterDetached.next());
+
+ REPORTER_ASSERT(reporter, equals(*recOneLarger, *recDetached));
+ if (smallerCount == i) {
+ REPORTER_ASSERT(reporter, recFirstCopy == NULL);
+ } else {
+ REPORTER_ASSERT(reporter, equals(*recFirstCopy, *recOneLarger));
+ }
+ }
+}