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 | |
| 120 | // This test is not meant to make total sense yet. It's testing the status quo |
| 121 | // of SkRecordFillBounds(), which itself doesn't make total sense yet. |
| 122 | DEF_TEST(RecordDraw_BBH, r) { |
| 123 | TestBBH bbh; |
| 124 | |
| 125 | SkRecord record; |
| 126 | |
| 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 | |
| 134 | SkRecordFillBounds(record, &bbh); |
| 135 | |
| 136 | REPORTER_ASSERT(r, bbh.entries.count() == 5); |
| 137 | for (int i = 0; i < bbh.entries.count(); i++) { |
| 138 | REPORTER_ASSERT(r, bbh.entries[i].data == (uintptr_t)i); |
| 139 | |
Mike Klein | 56fa442 | 2014-08-27 19:08:52 -0400 | [diff] [blame^] | 140 | if (bbh.entries[i].bounds != SkRect::MakeWH(400, 480)) { |
| 141 | SkRect bounds = bbh.entries[i].bounds; |
| 142 | SkDebugf("Expected 0,0,400,480, got %f %f %f %f\n", |
| 143 | bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom); |
| 144 | } |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 145 | REPORTER_ASSERT(r, bbh.entries[i].bounds == SkRect::MakeWH(400, 480)); |
mtklein | a723b57 | 2014-08-15 11:49:49 -0700 | [diff] [blame] | 146 | } |
| 147 | } |