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" |
| 9 | |
| 10 | #include "SkDebugCanvas.h" |
| 11 | #include "SkRecord.h" |
commit-bot@chromium.org | ad8ce57 | 2014-04-21 15:03:36 +0000 | [diff] [blame] | 12 | #include "SkRecordOpts.h" |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 13 | #include "SkRecordDraw.h" |
| 14 | #include "SkRecorder.h" |
| 15 | #include "SkRecords.h" |
| 16 | |
| 17 | static const int W = 1920, H = 1080; |
| 18 | |
commit-bot@chromium.org | 8dac8b1 | 2014-04-30 13:18:12 +0000 | [diff] [blame] | 19 | static void draw_pos_text_h(SkCanvas* canvas, const char* text, SkScalar y) { |
| 20 | const size_t len = strlen(text); |
| 21 | SkAutoTMalloc<SkScalar> xpos(len); |
| 22 | for (size_t i = 0; i < len; i++) { |
| 23 | xpos[i] = (SkScalar)i; |
| 24 | } |
| 25 | canvas->drawPosTextH(text, len, xpos, y, SkPaint()); |
| 26 | } |
| 27 | |
| 28 | // Rerecord into another SkRecord using full SkCanvas semantics, |
| 29 | // tracking clips and allowing SkRecordDraw's quickReject() calls to work. |
| 30 | static void record_clipped(const SkRecord& record, SkRect clip, SkRecord* clipped) { |
| 31 | SkRecorder recorder(SkRecorder::kReadWrite_Mode, clipped, W, H); |
| 32 | recorder.clipRect(clip); |
| 33 | SkRecordDraw(record, &recorder); |
| 34 | } |
| 35 | |
| 36 | DEF_TEST(RecordDraw_PosTextHQuickReject, r) { |
| 37 | SkRecord record; |
| 38 | SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H); |
| 39 | |
| 40 | draw_pos_text_h(&recorder, "This will draw.", 20); |
| 41 | draw_pos_text_h(&recorder, "This won't.", 5000); |
| 42 | |
| 43 | SkRecordBoundDrawPosTextH(&record); |
| 44 | |
| 45 | SkRecord clipped; |
| 46 | record_clipped(record, SkRect::MakeLTRB(20, 20, 200, 200), &clipped); |
| 47 | |
| 48 | // clipRect and the first drawPosTextH. |
| 49 | REPORTER_ASSERT(r, 2 == clipped.count()); |
| 50 | } |
| 51 | |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 52 | DEF_TEST(RecordDraw_Culling, r) { |
| 53 | // Record these 7 drawing commands verbatim. |
| 54 | SkRecord record; |
| 55 | SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H); |
| 56 | |
| 57 | recorder.pushCull(SkRect::MakeWH(100, 100)); |
| 58 | recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint()); |
| 59 | recorder.drawRect(SkRect::MakeWH(30, 30), SkPaint()); |
| 60 | recorder.pushCull(SkRect::MakeWH(5, 5)); |
| 61 | recorder.drawRect(SkRect::MakeWH(1, 1), SkPaint()); |
| 62 | recorder.popCull(); |
| 63 | recorder.popCull(); |
| 64 | |
| 65 | // Take a pass over to match up pushCulls and popCulls. |
| 66 | SkRecordAnnotateCullingPairs(&record); |
| 67 | |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 68 | // 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] | 69 | SkRecord clipped; |
| 70 | record_clipped(record, SkRect::MakeLTRB(20, 20, 200, 200), &clipped); |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 71 | |
| 72 | // We'll keep the clipRect call from above, and the outer two drawRects, and the push/pop pair. |
| 73 | // 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] | 74 | REPORTER_ASSERT(r, 5 == clipped.count()); |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 75 | } |