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" |
| 12 | #include "SkRecord.h" |
commit-bot@chromium.org | ad8ce57 | 2014-04-21 15:03:36 +0000 | [diff] [blame] | 13 | #include "SkRecordOpts.h" |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 14 | #include "SkRecordDraw.h" |
| 15 | #include "SkRecorder.h" |
| 16 | #include "SkRecords.h" |
| 17 | |
| 18 | static const int W = 1920, H = 1080; |
| 19 | |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 20 | static void draw_pos_text_h(SkCanvas* canvas, const char* text, SkScalar y) { |
| 21 | const size_t len = strlen(text); |
| 22 | SkAutoTMalloc<SkScalar> xpos(len); |
| 23 | for (size_t i = 0; i < len; i++) { |
| 24 | xpos[i] = (SkScalar)i; |
| 25 | } |
| 26 | canvas->drawPosTextH(text, len, xpos, y, SkPaint()); |
| 27 | } |
| 28 | |
| 29 | // Rerecord into another SkRecord using full SkCanvas semantics, |
| 30 | // tracking clips and allowing SkRecordDraw's quickReject() calls to work. |
| 31 | static void record_clipped(const SkRecord& record, SkRect clip, SkRecord* clipped) { |
| 32 | SkRecorder recorder(SkRecorder::kReadWrite_Mode, clipped, W, H); |
| 33 | recorder.clipRect(clip); |
| 34 | SkRecordDraw(record, &recorder); |
| 35 | } |
| 36 | |
| 37 | DEF_TEST(RecordDraw_PosTextHQuickReject, r) { |
| 38 | SkRecord record; |
| 39 | SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H); |
| 40 | |
| 41 | draw_pos_text_h(&recorder, "This will draw.", 20); |
| 42 | draw_pos_text_h(&recorder, "This won't.", 5000); |
| 43 | |
| 44 | SkRecordBoundDrawPosTextH(&record); |
| 45 | |
| 46 | SkRecord clipped; |
| 47 | record_clipped(record, SkRect::MakeLTRB(20, 20, 200, 200), &clipped); |
| 48 | |
| 49 | // clipRect and the first drawPosTextH. |
| 50 | REPORTER_ASSERT(r, 2 == clipped.count()); |
| 51 | } |
| 52 | |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 53 | DEF_TEST(RecordDraw_Culling, r) { |
| 54 | // Record these 7 drawing commands verbatim. |
| 55 | SkRecord record; |
| 56 | SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H); |
| 57 | |
| 58 | recorder.pushCull(SkRect::MakeWH(100, 100)); |
| 59 | recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint()); |
| 60 | recorder.drawRect(SkRect::MakeWH(30, 30), SkPaint()); |
| 61 | recorder.pushCull(SkRect::MakeWH(5, 5)); |
| 62 | recorder.drawRect(SkRect::MakeWH(1, 1), SkPaint()); |
| 63 | recorder.popCull(); |
| 64 | recorder.popCull(); |
| 65 | |
| 66 | // Take a pass over to match up pushCulls and popCulls. |
| 67 | SkRecordAnnotateCullingPairs(&record); |
| 68 | |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 69 | // This clip intersects the outer cull, but allows us to quick reject the inner one. |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 70 | SkRecord clipped; |
| 71 | record_clipped(record, SkRect::MakeLTRB(20, 20, 200, 200), &clipped); |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 72 | |
| 73 | // We'll keep the clipRect call from above, and the outer two drawRects, and the push/pop pair. |
| 74 | // If culling weren't working, we'd see 8 commands recorded here. |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 75 | REPORTER_ASSERT(r, 5 == clipped.count()); |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 76 | } |
commit-bot@chromium.org | 0a98d87 | 2014-05-19 15:15:24 +0000 | [diff] [blame] | 77 | |
| 78 | DEF_TEST(RecordDraw_SetMatrixClobber, r) { |
| 79 | // Set up an SkRecord that just scales by 2x,3x. |
| 80 | SkRecord scaleRecord; |
| 81 | SkRecorder scaleCanvas(SkRecorder::kReadWrite_Mode, &scaleRecord, W, H); |
| 82 | SkMatrix scale; |
| 83 | scale.setScale(2, 3); |
| 84 | scaleCanvas.setMatrix(scale); |
| 85 | |
| 86 | // Set up an SkRecord with an initial +20, +20 translate. |
| 87 | SkRecord translateRecord; |
| 88 | SkRecorder translateCanvas(SkRecorder::kReadWrite_Mode, &translateRecord, W, H); |
| 89 | SkMatrix translate; |
| 90 | translate.setTranslate(20, 20); |
| 91 | translateCanvas.setMatrix(translate); |
| 92 | |
| 93 | SkRecordDraw(scaleRecord, &translateCanvas); |
| 94 | |
| 95 | // When we look at translateRecord now, it should have its first +20,+20 translate, |
| 96 | // then a 2x,3x scale that's been concatted with that +20,+20 translate. |
| 97 | const SkRecords::SetMatrix* setMatrix; |
| 98 | setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 0); |
| 99 | REPORTER_ASSERT(r, setMatrix->matrix == translate); |
| 100 | |
| 101 | setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 1); |
| 102 | SkMatrix expected = scale; |
| 103 | expected.postConcat(translate); |
| 104 | REPORTER_ASSERT(r, setMatrix->matrix == expected); |
| 105 | } |