Backfill unit tests for SkRecord
BUG=skia:2378
R=fmalita@chromium.org, mtklein@google.com
Author: mtklein@chromium.org
Review URL: https://codereview.chromium.org/251133008
git-svn-id: http://skia.googlecode.com/svn/trunk@14455 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gyp/tests.gypi b/gyp/tests.gypi
index 982d58d..afe256c 100644
--- a/gyp/tests.gypi
+++ b/gyp/tests.gypi
@@ -140,8 +140,8 @@
'../tests/ReadPixelsTest.cpp',
'../tests/ReadWriteAlphaTest.cpp',
'../tests/Reader32Test.cpp',
- '../tests/RecordCullingTest.cpp',
'../tests/RecordDrawTest.cpp',
+ '../tests/RecordOptsTest.cpp',
'../tests/RecordTest.cpp',
'../tests/RecorderTest.cpp',
'../tests/RecordingTest.cpp',
diff --git a/src/record/SkRecordOpts.h b/src/record/SkRecordOpts.h
index c9cbccf..6db7abc 100644
--- a/src/record/SkRecordOpts.h
+++ b/src/record/SkRecordOpts.h
@@ -15,15 +15,15 @@
// Turns logical no-op Save-[non-drawing command]*-Restore patterns into actual no-ops.
-void SkRecordNoopSaveRestores(SkRecord*); // TODO(mtklein): add unit tests
+void SkRecordNoopSaveRestores(SkRecord*);
// Annotates PushCull commands with the relative offset of their paired PopCull.
void SkRecordAnnotateCullingPairs(SkRecord*);
// Convert DrawPosText to DrawPosTextH when all the Y coordinates are equal.
-void SkRecordReduceDrawPosTextStrength(SkRecord*); // TODO(mtklein): add unit tests
+void SkRecordReduceDrawPosTextStrength(SkRecord*);
// Calculate min and max Y bounds for DrawPosTextH commands, for use with SkCanvas::quickRejectY.
-void SkRecordBoundDrawPosTextH(SkRecord*); // TODO(mtklein): add unit tests
+void SkRecordBoundDrawPosTextH(SkRecord*);
#endif//SkRecordOpts_DEFINED
diff --git a/tests/RecordCullingTest.cpp b/tests/RecordCullingTest.cpp
deleted file mode 100644
index 7cb6241..0000000
--- a/tests/RecordCullingTest.cpp
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * 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 "Test.h"
-
-#include "SkRecord.h"
-#include "SkRecordOpts.h"
-#include "SkRecorder.h"
-#include "SkRecords.h"
-
-struct SkipScanner {
- template <typename T> void operator()(const T&) {}
-
- void apply(const SkRecord& record) {
- for (unsigned i = 0; i < record.count(); i++) {
- record.visit(i, *this);
- }
- }
-
- SkTDArray<unsigned> fSkips;
-};
-
-template <> void SkipScanner::operator()(const SkRecords::PairedPushCull& r) {
- *fSkips.append() = r.skip;
-}
-
-
-DEF_TEST(RecordCulling, r) {
- SkRecord record;
- SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, 1920, 1080);
-
- recorder.drawRect(SkRect::MakeWH(1000, 10000), SkPaint());
-
- recorder.pushCull(SkRect::MakeWH(100, 100));
- recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint());
- recorder.drawRect(SkRect::MakeWH(30, 30), SkPaint());
- recorder.pushCull(SkRect::MakeWH(5, 5));
- recorder.drawRect(SkRect::MakeWH(1, 1), SkPaint());
- recorder.popCull();
- recorder.popCull();
-
- SkRecordAnnotateCullingPairs(&record);
-
- SkipScanner scan;
- scan.apply(record);
-
- REPORTER_ASSERT(r, 2 == scan.fSkips.count());
- REPORTER_ASSERT(r, 6 == scan.fSkips[0]);
- REPORTER_ASSERT(r, 2 == scan.fSkips[1]);
-}
diff --git a/tests/RecordDrawTest.cpp b/tests/RecordDrawTest.cpp
index c4dab84..d830aea 100644
--- a/tests/RecordDrawTest.cpp
+++ b/tests/RecordDrawTest.cpp
@@ -16,6 +16,39 @@
static const int W = 1920, H = 1080;
+static void draw_pos_text_h(SkCanvas* canvas, const char* text, SkScalar y) {
+ const size_t len = strlen(text);
+ SkAutoTMalloc<SkScalar> xpos(len);
+ for (size_t i = 0; i < len; i++) {
+ xpos[i] = (SkScalar)i;
+ }
+ canvas->drawPosTextH(text, len, xpos, y, SkPaint());
+}
+
+// Rerecord into another SkRecord using full SkCanvas semantics,
+// tracking clips and allowing SkRecordDraw's quickReject() calls to work.
+static void record_clipped(const SkRecord& record, SkRect clip, SkRecord* clipped) {
+ SkRecorder recorder(SkRecorder::kReadWrite_Mode, clipped, W, H);
+ recorder.clipRect(clip);
+ SkRecordDraw(record, &recorder);
+}
+
+DEF_TEST(RecordDraw_PosTextHQuickReject, r) {
+ SkRecord record;
+ SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H);
+
+ draw_pos_text_h(&recorder, "This will draw.", 20);
+ draw_pos_text_h(&recorder, "This won't.", 5000);
+
+ SkRecordBoundDrawPosTextH(&record);
+
+ SkRecord clipped;
+ record_clipped(record, SkRect::MakeLTRB(20, 20, 200, 200), &clipped);
+
+ // clipRect and the first drawPosTextH.
+ REPORTER_ASSERT(r, 2 == clipped.count());
+}
+
DEF_TEST(RecordDraw_Culling, r) {
// Record these 7 drawing commands verbatim.
SkRecord record;
@@ -32,16 +65,11 @@
// Take a pass over to match up pushCulls and popCulls.
SkRecordAnnotateCullingPairs(&record);
- // Rerecord into another SkRecord using full SkCanvas semantics,
- // tracking clips and allowing SkRecordDraw's quickReject() calls to work.
- SkRecord rerecord;
- SkRecorder rerecorder(SkRecorder::kReadWrite_Mode, &rerecord, W, H);
// This clip intersects the outer cull, but allows us to quick reject the inner one.
- rerecorder.clipRect(SkRect::MakeLTRB(20, 20, 200, 200));
-
- SkRecordDraw(record, &rerecorder);
+ SkRecord clipped;
+ record_clipped(record, SkRect::MakeLTRB(20, 20, 200, 200), &clipped);
// We'll keep the clipRect call from above, and the outer two drawRects, and the push/pop pair.
// If culling weren't working, we'd see 8 commands recorded here.
- REPORTER_ASSERT(r, 5 == rerecord.count());
+ REPORTER_ASSERT(r, 5 == clipped.count());
}
diff --git a/tests/RecordOptsTest.cpp b/tests/RecordOptsTest.cpp
new file mode 100644
index 0000000..140ad7e
--- /dev/null
+++ b/tests/RecordOptsTest.cpp
@@ -0,0 +1,136 @@
+/*
+ * 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 "Test.h"
+
+#include "SkRecord.h"
+#include "SkRecordOpts.h"
+#include "SkRecorder.h"
+#include "SkRecords.h"
+
+static const int W = 1920, H = 1080;
+
+// If the command we're reading is a U, set ptr to it, otherwise set it to NULL.
+template <typename U>
+struct ReadAs {
+ explicit ReadAs(const U** ptr) : ptr(ptr) {}
+ const U** ptr;
+
+ void operator()(const U& r) { *ptr = &r; }
+
+ template <typename T>
+ void operator()(const T&) { *ptr = NULL; }
+};
+
+// Assert that the ith command in record is of type T, and return it.
+template <typename T>
+static const T* assert_type(const SkRecord& record, unsigned index) {
+ const T* ptr = NULL;
+ ReadAs<T> reader(&ptr);
+ record.visit(index, reader);
+ SkASSERT(ptr != NULL);
+ return ptr;
+}
+
+DEF_TEST(RecordOpts_Culling, r) {
+ SkRecord record;
+ SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H);
+
+ recorder.drawRect(SkRect::MakeWH(1000, 10000), SkPaint());
+
+ recorder.pushCull(SkRect::MakeWH(100, 100));
+ recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint());
+ recorder.drawRect(SkRect::MakeWH(30, 30), SkPaint());
+ recorder.pushCull(SkRect::MakeWH(5, 5));
+ recorder.drawRect(SkRect::MakeWH(1, 1), SkPaint());
+ recorder.popCull();
+ recorder.popCull();
+
+ SkRecordAnnotateCullingPairs(&record);
+
+ REPORTER_ASSERT(r, 6 == assert_type<SkRecords::PairedPushCull>(record, 1)->skip);
+ REPORTER_ASSERT(r, 2 == assert_type<SkRecords::PairedPushCull>(record, 4)->skip);
+}
+
+static void draw_pos_text(SkCanvas* canvas, const char* text, bool constantY) {
+ const size_t len = strlen(text);
+ SkAutoTMalloc<SkPoint> pos(len);
+ for (size_t i = 0; i < len; i++) {
+ pos[i].fX = (SkScalar)i;
+ pos[i].fY = constantY ? SK_Scalar1 : (SkScalar)i;
+ }
+ canvas->drawPosText(text, len, pos, SkPaint());
+}
+
+DEF_TEST(RecordOpts_StrengthReduction, r) {
+ SkRecord record;
+ SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H);
+
+ // We can convert a drawPosText into a drawPosTextH when all the Ys are the same.
+ draw_pos_text(&recorder, "This will be reduced to drawPosTextH.", true);
+ draw_pos_text(&recorder, "This cannot be reduced to drawPosTextH.", false);
+
+ SkRecordReduceDrawPosTextStrength(&record);
+
+ assert_type<SkRecords::DrawPosTextH>(record, 0);
+ assert_type<SkRecords::DrawPosText>(record, 1);
+}
+
+DEF_TEST(RecordOpts_TextBounding, r) {
+ SkRecord record;
+ SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H);
+
+ // First, get a drawPosTextH. Here's a handy way. Its text size will be the default (12).
+ draw_pos_text(&recorder, "This will be reduced to drawPosTextH.", true);
+ SkRecordReduceDrawPosTextStrength(&record);
+
+ const SkRecords::DrawPosTextH* original =
+ assert_type<SkRecords::DrawPosTextH>(record, 0);
+
+ // This should wrap the original DrawPosTextH with minY and maxY.
+ SkRecordBoundDrawPosTextH(&record);
+
+ const SkRecords::BoundedDrawPosTextH* bounded =
+ assert_type<SkRecords::BoundedDrawPosTextH>(record, 0);
+
+ const SkPaint defaults;
+ REPORTER_ASSERT(r, bounded->base == original);
+ REPORTER_ASSERT(r, bounded->minY <= SK_Scalar1 - defaults.getTextSize());
+ REPORTER_ASSERT(r, bounded->maxY >= SK_Scalar1 + defaults.getTextSize());
+}
+
+DEF_TEST(RecordOpts_NoopSaveRestores, r) {
+ SkRecord record;
+ SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H);
+
+ // The second pass will clean up this pair after the first pass noops all the innards.
+ recorder.save();
+ // A simple pointless pair of save/restore.
+ recorder.save();
+ recorder.restore();
+
+ // As long as we don't draw in there, everything is a noop.
+ recorder.save();
+ recorder.clipRect(SkRect::MakeWH(200, 200));
+ recorder.clipRect(SkRect::MakeWH(100, 100));
+ recorder.restore();
+ recorder.restore();
+
+ // These will be kept (though some future optimization might noop the save and restore).
+ recorder.save();
+ recorder.drawRect(SkRect::MakeWH(200, 200), SkPaint());
+ recorder.restore();
+
+ SkRecordNoopSaveRestores(&record);
+
+ for (unsigned index = 0; index < 8; index++) {
+ assert_type<SkRecords::NoOp>(record, index);
+ }
+ assert_type<SkRecords::Save>(record, 8);
+ assert_type<SkRecords::DrawRect>(record, 9);
+ assert_type<SkRecords::Restore>(record, 10);
+}