blob: b0e55a6fdbe7160b2c50c5d2c1aebb96a855a687 [file] [log] [blame]
commit-bot@chromium.orgc4b21e62014-04-11 18:33:31 +00001/*
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.orgd9ce2be2014-04-09 23:30:28 +00008#include "Test.h"
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +00009#include "RecordTestUtils.h"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000010
11#include "SkDebugCanvas.h"
12#include "SkRecord.h"
commit-bot@chromium.orgad8ce572014-04-21 15:03:36 +000013#include "SkRecordOpts.h"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000014#include "SkRecordDraw.h"
15#include "SkRecorder.h"
16#include "SkRecords.h"
17
18static const int W = 1920, H = 1080;
19
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000020static 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.
31static 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
37DEF_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.orgd9ce2be2014-04-09 23:30:28 +000053DEF_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.orgd9ce2be2014-04-09 23:30:28 +000069 // This clip intersects the outer cull, but allows us to quick reject the inner one.
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000070 SkRecord clipped;
71 record_clipped(record, SkRect::MakeLTRB(20, 20, 200, 200), &clipped);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000072
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.org8dac8b12014-04-30 13:18:12 +000075 REPORTER_ASSERT(r, 5 == clipped.count());
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000076}
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000077
78DEF_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}