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