commit-bot@chromium.org | c4b21e6 | 2014-04-11 18:33:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 8 | #include "Test.h" |
commit-bot@chromium.org | 0a98d87 | 2014-05-19 15:15:24 +0000 | [diff] [blame] | 9 | #include "RecordTestUtils.h" |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 10 | |
| 11 | #include "SkDebugCanvas.h" |
Mike Klein | c11530e | 2014-06-24 11:29:06 -0400 | [diff] [blame] | 12 | #include "SkDrawPictureCallback.h" |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 13 | #include "SkRecord.h" |
commit-bot@chromium.org | ad8ce57 | 2014-04-21 15:03:36 +0000 | [diff] [blame] | 14 | #include "SkRecordOpts.h" |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 15 | #include "SkRecordDraw.h" |
| 16 | #include "SkRecorder.h" |
| 17 | #include "SkRecords.h" |
| 18 | |
| 19 | static const int W = 1920, H = 1080; |
| 20 | |
Mike Klein | c11530e | 2014-06-24 11:29:06 -0400 | [diff] [blame] | 21 | class JustOneDraw : public SkDrawPictureCallback { |
| 22 | public: |
| 23 | JustOneDraw() : fCalls(0) {} |
| 24 | |
| 25 | virtual bool abortDrawing() SK_OVERRIDE { return fCalls++ > 0; } |
| 26 | private: |
| 27 | int fCalls; |
| 28 | }; |
| 29 | |
| 30 | DEF_TEST(RecordDraw_Abort, r) { |
| 31 | // Record two commands. |
| 32 | SkRecord record; |
| 33 | SkRecorder recorder(&record, W, H); |
| 34 | recorder.drawRect(SkRect::MakeWH(200, 300), SkPaint()); |
| 35 | recorder.clipRect(SkRect::MakeWH(100, 200)); |
| 36 | |
| 37 | SkRecord rerecord; |
| 38 | SkRecorder canvas(&rerecord, W, H); |
| 39 | |
| 40 | JustOneDraw callback; |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 41 | SkRecordDraw(record, &canvas, NULL/*bbh*/, &callback); |
Mike Klein | c11530e | 2014-06-24 11:29:06 -0400 | [diff] [blame] | 42 | |
| 43 | REPORTER_ASSERT(r, 3 == rerecord.count()); |
| 44 | assert_type<SkRecords::Save> (r, rerecord, 0); |
| 45 | assert_type<SkRecords::DrawRect>(r, rerecord, 1); |
| 46 | assert_type<SkRecords::Restore> (r, rerecord, 2); |
| 47 | } |
| 48 | |
| 49 | DEF_TEST(RecordDraw_Unbalanced, r) { |
| 50 | SkRecord record; |
| 51 | SkRecorder recorder(&record, W, H); |
| 52 | recorder.save(); // We won't balance this, but SkRecordDraw will for us. |
| 53 | |
| 54 | SkRecord rerecord; |
| 55 | SkRecorder canvas(&rerecord, W, H); |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 56 | SkRecordDraw(record, &canvas, NULL/*bbh*/, NULL/*callback*/); |
Mike Klein | c11530e | 2014-06-24 11:29:06 -0400 | [diff] [blame] | 57 | |
| 58 | REPORTER_ASSERT(r, 4 == rerecord.count()); |
| 59 | assert_type<SkRecords::Save> (r, rerecord, 0); |
| 60 | assert_type<SkRecords::Save> (r, rerecord, 1); |
| 61 | assert_type<SkRecords::Restore> (r, rerecord, 2); |
| 62 | assert_type<SkRecords::Restore> (r, rerecord, 3); |
| 63 | } |
| 64 | |
commit-bot@chromium.org | 0a98d87 | 2014-05-19 15:15:24 +0000 | [diff] [blame] | 65 | DEF_TEST(RecordDraw_SetMatrixClobber, r) { |
| 66 | // Set up an SkRecord that just scales by 2x,3x. |
| 67 | SkRecord scaleRecord; |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 68 | SkRecorder scaleCanvas(&scaleRecord, W, H); |
commit-bot@chromium.org | 0a98d87 | 2014-05-19 15:15:24 +0000 | [diff] [blame] | 69 | SkMatrix scale; |
| 70 | scale.setScale(2, 3); |
| 71 | scaleCanvas.setMatrix(scale); |
| 72 | |
| 73 | // Set up an SkRecord with an initial +20, +20 translate. |
| 74 | SkRecord translateRecord; |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 75 | SkRecorder translateCanvas(&translateRecord, W, H); |
commit-bot@chromium.org | 0a98d87 | 2014-05-19 15:15:24 +0000 | [diff] [blame] | 76 | SkMatrix translate; |
| 77 | translate.setTranslate(20, 20); |
| 78 | translateCanvas.setMatrix(translate); |
| 79 | |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 80 | SkRecordDraw(scaleRecord, &translateCanvas, NULL/*bbh*/, NULL/*callback*/); |
Mike Klein | c11530e | 2014-06-24 11:29:06 -0400 | [diff] [blame] | 81 | REPORTER_ASSERT(r, 4 == translateRecord.count()); |
| 82 | assert_type<SkRecords::SetMatrix>(r, translateRecord, 0); |
| 83 | assert_type<SkRecords::Save> (r, translateRecord, 1); |
| 84 | assert_type<SkRecords::SetMatrix>(r, translateRecord, 2); |
| 85 | assert_type<SkRecords::Restore> (r, translateRecord, 3); |
commit-bot@chromium.org | 0a98d87 | 2014-05-19 15:15:24 +0000 | [diff] [blame] | 86 | |
| 87 | // When we look at translateRecord now, it should have its first +20,+20 translate, |
| 88 | // then a 2x,3x scale that's been concatted with that +20,+20 translate. |
| 89 | const SkRecords::SetMatrix* setMatrix; |
| 90 | setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 0); |
| 91 | REPORTER_ASSERT(r, setMatrix->matrix == translate); |
| 92 | |
Mike Klein | c11530e | 2014-06-24 11:29:06 -0400 | [diff] [blame] | 93 | setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 2); |
commit-bot@chromium.org | 0a98d87 | 2014-05-19 15:15:24 +0000 | [diff] [blame] | 94 | SkMatrix expected = scale; |
| 95 | expected.postConcat(translate); |
| 96 | REPORTER_ASSERT(r, setMatrix->matrix == expected); |
| 97 | } |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 98 | |
| 99 | struct TestBBH : public SkBBoxHierarchy { |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 100 | virtual void insert(void* data, const SkRect& bounds, bool defer) SK_OVERRIDE { |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 101 | Entry e = { (uintptr_t)data, bounds }; |
| 102 | entries.push(e); |
| 103 | } |
| 104 | virtual int getCount() const SK_OVERRIDE { return entries.count(); } |
| 105 | |
| 106 | virtual void flushDeferredInserts() SK_OVERRIDE {} |
| 107 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 108 | virtual void search(const SkRect& query, SkTDArray<void*>* results) const SK_OVERRIDE {} |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 109 | virtual void clear() SK_OVERRIDE {} |
| 110 | virtual void rewindInserts() SK_OVERRIDE {} |
| 111 | virtual int getDepth() const SK_OVERRIDE { return -1; } |
| 112 | |
| 113 | struct Entry { |
| 114 | uintptr_t data; |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 115 | SkRect bounds; |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 116 | }; |
| 117 | SkTDArray<Entry> entries; |
| 118 | }; |
| 119 | |
mtklein | 937c9c7 | 2014-09-02 15:19:48 -0700 | [diff] [blame] | 120 | // Like a==b, with a little slop recognizing that float equality can be weird. |
| 121 | static bool sloppy_rect_eq(SkRect a, SkRect b) { |
| 122 | SkRect inset(a), outset(a); |
| 123 | inset.inset(1, 1); |
| 124 | outset.outset(1, 1); |
| 125 | return outset.contains(b) && !inset.contains(b); |
| 126 | } |
| 127 | |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 128 | // This test is not meant to make total sense yet. It's testing the status quo |
| 129 | // of SkRecordFillBounds(), which itself doesn't make total sense yet. |
| 130 | DEF_TEST(RecordDraw_BBH, r) { |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 131 | SkRecord record; |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 132 | SkRecorder recorder(&record, W, H); |
| 133 | recorder.save(); |
| 134 | recorder.clipRect(SkRect::MakeWH(400, 500)); |
| 135 | recorder.scale(2, 2); |
| 136 | recorder.drawRect(SkRect::MakeWH(320, 240), SkPaint()); |
| 137 | recorder.restore(); |
| 138 | |
mtklein | 937c9c7 | 2014-09-02 15:19:48 -0700 | [diff] [blame] | 139 | TestBBH bbh; |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 140 | SkRecordFillBounds(record, &bbh); |
| 141 | |
| 142 | REPORTER_ASSERT(r, bbh.entries.count() == 5); |
| 143 | for (int i = 0; i < bbh.entries.count(); i++) { |
| 144 | REPORTER_ASSERT(r, bbh.entries[i].data == (uintptr_t)i); |
| 145 | |
mtklein | 937c9c7 | 2014-09-02 15:19:48 -0700 | [diff] [blame] | 146 | REPORTER_ASSERT(r, sloppy_rect_eq(SkRect::MakeWH(400, 480), bbh.entries[i].bounds)); |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
mtklein | 00f30bd | 2014-09-02 12:03:31 -0700 | [diff] [blame] | 149 | |
mtklein | 937c9c7 | 2014-09-02 15:19:48 -0700 | [diff] [blame] | 150 | // A regression test for crbug.com/409110. |
| 151 | DEF_TEST(RecordDraw_TextBounds, r) { |
| 152 | SkRecord record; |
| 153 | SkRecorder recorder(&record, W, H); |
| 154 | |
| 155 | // Two Chinese characters in UTF-8. |
| 156 | const char text[] = { '\xe6', '\xbc', '\xa2', '\xe5', '\xad', '\x97' }; |
| 157 | const size_t bytes = SK_ARRAY_COUNT(text); |
| 158 | |
| 159 | const SkScalar xpos[] = { 10, 20 }; |
| 160 | recorder.drawPosTextH(text, bytes, xpos, 30, SkPaint()); |
| 161 | |
| 162 | const SkPoint pos[] = { {40, 50}, {60, 70} }; |
| 163 | recorder.drawPosText(text, bytes, pos, SkPaint()); |
| 164 | |
| 165 | TestBBH bbh; |
| 166 | SkRecordFillBounds(record, &bbh); |
| 167 | REPORTER_ASSERT(r, bbh.entries.count() == 2); |
| 168 | |
| 169 | // We can make these next assertions confidently because SkRecordFillBounds |
| 170 | // builds its bounds by overestimating font metrics in a platform-independent way. |
| 171 | // If that changes, these tests will need to be more flexible. |
| 172 | REPORTER_ASSERT(r, sloppy_rect_eq(bbh.entries[0].bounds, SkRect::MakeLTRB(-86, 6, 116, 54))); |
| 173 | REPORTER_ASSERT(r, sloppy_rect_eq(bbh.entries[1].bounds, SkRect::MakeLTRB(-56, 26, 156, 94))); |
| 174 | } |
| 175 | |
mtklein | 00f30bd | 2014-09-02 12:03:31 -0700 | [diff] [blame] | 176 | // Base test to ensure start/stop range is respected |
| 177 | DEF_TEST(RecordDraw_PartialStartStop, r) { |
| 178 | static const int kWidth = 10, kHeight = 10; |
| 179 | |
| 180 | SkRect r1 = { 0, 0, kWidth, kHeight }; |
| 181 | SkRect r2 = { 0, 0, kWidth, kHeight/2 }; |
| 182 | SkRect r3 = { 0, 0, kWidth/2, kHeight }; |
| 183 | SkPaint p; |
| 184 | |
| 185 | SkRecord record; |
| 186 | SkRecorder recorder(&record, kWidth, kHeight); |
| 187 | recorder.drawRect(r1, p); |
| 188 | recorder.drawRect(r2, p); |
| 189 | recorder.drawRect(r3, p); |
| 190 | |
| 191 | SkRecord rerecord; |
| 192 | SkRecorder canvas(&rerecord, kWidth, kHeight); |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame^] | 193 | SkRecordPartialDraw(record, &canvas, r1, 1, 2, SkMatrix::I()); // replay just drawRect of r2 |
mtklein | 00f30bd | 2014-09-02 12:03:31 -0700 | [diff] [blame] | 194 | |
| 195 | REPORTER_ASSERT(r, 3 == rerecord.count()); |
| 196 | assert_type<SkRecords::Save> (r, rerecord, 0); |
| 197 | assert_type<SkRecords::DrawRect> (r, rerecord, 1); |
| 198 | assert_type<SkRecords::Restore> (r, rerecord, 2); |
| 199 | |
| 200 | const SkRecords::DrawRect* drawRect = assert_type<SkRecords::DrawRect>(r, rerecord, 1); |
| 201 | REPORTER_ASSERT(r, drawRect->rect == r2); |
| 202 | } |
| 203 | |
| 204 | // Check that clears are converted to drawRects |
| 205 | DEF_TEST(RecordDraw_PartialClear, r) { |
| 206 | static const int kWidth = 10, kHeight = 10; |
| 207 | |
| 208 | SkRect rect = { 0, 0, kWidth, kHeight }; |
| 209 | |
| 210 | SkRecord record; |
| 211 | SkRecorder recorder(&record, kWidth, kHeight); |
| 212 | recorder.clear(SK_ColorRED); |
| 213 | |
| 214 | SkRecord rerecord; |
| 215 | SkRecorder canvas(&rerecord, kWidth, kHeight); |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame^] | 216 | SkRecordPartialDraw(record, &canvas, rect, 0, 1, SkMatrix::I()); // replay just the clear |
mtklein | 00f30bd | 2014-09-02 12:03:31 -0700 | [diff] [blame] | 217 | |
| 218 | REPORTER_ASSERT(r, 3 == rerecord.count()); |
| 219 | assert_type<SkRecords::Save> (r, rerecord, 0); |
| 220 | assert_type<SkRecords::DrawRect>(r, rerecord, 1); |
| 221 | assert_type<SkRecords::Restore> (r, rerecord, 2); |
| 222 | |
| 223 | const SkRecords::DrawRect* drawRect = assert_type<SkRecords::DrawRect>(r, rerecord, 1); |
| 224 | REPORTER_ASSERT(r, drawRect->rect == rect); |
| 225 | REPORTER_ASSERT(r, drawRect->paint.getColor() == SK_ColorRED); |
| 226 | } |