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

Unified Diff: tests/DrawLooperTest.cpp

Issue 993863002: Make the canvas draw looper internal layer not part of SkCanvas save count Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: another approach Created 5 years, 9 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
« no previous file with comments | « src/core/SkCanvas.cpp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/DrawLooperTest.cpp
diff --git a/tests/DrawLooperTest.cpp b/tests/DrawLooperTest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..2206f575943d588aa0a29612881ec3ea10d6d5bc
--- /dev/null
+++ b/tests/DrawLooperTest.cpp
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkCanvas.h"
+#include "SkColorFilter.h"
+#include "SkColorFilterImageFilter.h"
+#include "SkDrawLooper.h"
+#include "Test.h"
+
+/* Tests for SkDrawLooper -related APIs and implementations. */
+
+namespace {
+/*
+ * Subclass that caused an assert at the time of writing.
+ */
+class GetSaveCountAssertLooper : public SkDrawLooper {
+public:
+ GetSaveCountAssertLooper(skiatest::Reporter* reporter)
+ : fReporter(reporter) {
+ }
+
+ SkDrawLooper::Context* createContext(SkCanvas*, void* storage) const override {
+ return SkNEW_PLACEMENT_ARGS(storage, GetSaveCountAssertLooperContext, (fReporter));
+ }
+
+ size_t contextSize() const override { return sizeof(GetSaveCountAssertLooperContext); }
+
+#ifndef SK_IGNORE_TO_STRING
+ void toString(SkString* str) const override {
+ str->append("GetSaveCountAssertLooper:");
+ }
+#endif
+
+ SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(GetSaveCountAssertLooper);
+
+private:
+ class GetSaveCountAssertLooperContext : public SkDrawLooper::Context {
+ public:
+ GetSaveCountAssertLooperContext(skiatest::Reporter* reporter)
+ : fReporter(reporter)
+ , fOnce(0) {
+ }
+
+ bool next(SkCanvas* canvas, SkPaint* p) override {
+ // 1st test: Getting the save count would assert in SkCanvas at the time of writing.
+ // 2nd test: test the sentence from SkCanvas.h:
+ // This will equal # save() calls - # restore() calls + 1.
+ REPORTER_ASSERT(fReporter, 1 == canvas->getSaveCount());
+
+ SkASSERT(p->getColor() == SK_ColorRED);
+ // Set the color green so the test knows payload was run. We use this color in order to
+ // try to express the expectation that Skia can not away the color filter. Due to
+ // non-pm-to-pm-to-non-pm conversions, this probably is not exactly correct.
+ p->setColor(SkColorSetARGB(255, 0, 254, 0));
+ return fOnce++ < 1;
+ }
+ private:
+ skiatest::Reporter* fReporter;
+ int fOnce;
+ };
+ mutable skiatest::Reporter* fReporter;
+};
+
+SkFlattenable* GetSaveCountAssertLooper::CreateProc(SkReadBuffer&) {
+ SkASSERT(false);
+ return SkNEW_ARGS(GetSaveCountAssertLooper, (NULL));
+}
+
+}
+
+DEF_TEST(SkCanvas_GetSaveCountInDrawLooperAssert, reporter) {
+ SkBitmap dst;
+ dst.allocN32Pixels(10, 10);
+ dst.eraseColor(SK_ColorTRANSPARENT);
+
+ SkCanvas canvas(dst);
+ SkPaint paint;
+ {
+ SkAutoTUnref<SkColorFilter> addGreenCF(
+ SkColorFilter::CreateModeFilter(SkColorSetARGB(64, 0, 4, 0), SkXfermode::kPlus_Mode));
+ SkAutoTUnref<SkImageFilter> addGreenIF(
+ SkColorFilterImageFilter::Create(addGreenCF));
+
+ // This would trigger the assert upon a draw. It is a color filter that adds (roughly) 1 to
+ // the green component.
+ paint.setImageFilter(addGreenIF);
+
+ SkAutoTUnref<SkDrawLooper> looper(SkNEW_ARGS(GetSaveCountAssertLooper, (reporter)));
+ paint.setLooper(looper);
+ }
+ paint.setColor(SK_ColorRED);
+ paint.setStrokeWidth(1);
+ paint.setStyle(SkPaint::kStroke_Style);
+ canvas.drawPoint(0, 0, paint);
+
+ uint32_t pixel = 0;
+ SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType);
+ canvas.readPixels(info, &pixel, 4, 0, 0);
+ REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN);
+}
« no previous file with comments | « src/core/SkCanvas.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698