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