Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(661)

Unified Diff: tests/LayerRasterizerTest.cpp

Issue 233673002: Create SkLayerRasterizer w/o destroying Builder. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Better comments. New function is now const, and has a better name. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« include/effects/SkLayerRasterizer.h ('K') | « src/effects/SkLayerRasterizer.cpp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/LayerRasterizerTest.cpp
diff --git a/tests/LayerRasterizerTest.cpp b/tests/LayerRasterizerTest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..b393525fe124fba5528e91f17845d58dc702b2f4
--- /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));
+ }
+ }
+}
« include/effects/SkLayerRasterizer.h ('K') | « src/effects/SkLayerRasterizer.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698