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 8 #include "Test.h" 9 #include "RecordTestUtils.h" 10 11 #include "SkDebugCanvas.h" 12 #include "SkRecord.h" 13 #include "SkRecordOpts.h" 14 #include "SkRecordDraw.h" 15 #include "SkRecorder.h" 16 #include "SkRecords.h" 17 18 static const int W = 1920, H = 1080; 19 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(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(&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 53 DEF_TEST(RecordDraw_Culling, r) { 54 // Record these 7 drawing commands verbatim. 55 SkRecord record; 56 SkRecorder recorder(&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 69 // This clip intersects the outer cull, but allows us to quick reject the inner one. 70 SkRecord clipped; 71 record_clipped(record, SkRect::MakeLTRB(20, 20, 200, 200), &clipped); 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. 75 REPORTER_ASSERT(r, 5 == clipped.count()); 76 } 77 78 DEF_TEST(RecordDraw_SetMatrixClobber, r) { 79 // Set up an SkRecord that just scales by 2x,3x. 80 SkRecord scaleRecord; 81 SkRecorder scaleCanvas(&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(&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 } 106