blob: b9ab2dce258abb2a7a56f19be33ca938a4f7774e [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"
Mike Kleinc11530e2014-06-24 11:29:06 -040012#include "SkDrawPictureCallback.h"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000013#include "SkRecord.h"
commit-bot@chromium.orgad8ce572014-04-21 15:03:36 +000014#include "SkRecordOpts.h"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000015#include "SkRecordDraw.h"
16#include "SkRecorder.h"
17#include "SkRecords.h"
18
19static const int W = 1920, H = 1080;
20
Mike Kleinc11530e2014-06-24 11:29:06 -040021class JustOneDraw : public SkDrawPictureCallback {
22public:
23 JustOneDraw() : fCalls(0) {}
24
25 virtual bool abortDrawing() SK_OVERRIDE { return fCalls++ > 0; }
26private:
27 int fCalls;
28};
29
30DEF_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;
mtklein5ad6ee12014-08-11 08:08:43 -070041 SkRecordDraw(record, &canvas, NULL/*bbh*/, &callback);
Mike Kleinc11530e2014-06-24 11:29:06 -040042
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
49DEF_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);
mtklein5ad6ee12014-08-11 08:08:43 -070056 SkRecordDraw(record, &canvas, NULL/*bbh*/, NULL/*callback*/);
Mike Kleinc11530e2014-06-24 11:29:06 -040057
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.org0a98d872014-05-19 15:15:24 +000065DEF_TEST(RecordDraw_SetMatrixClobber, r) {
66 // Set up an SkRecord that just scales by 2x,3x.
67 SkRecord scaleRecord;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000068 SkRecorder scaleCanvas(&scaleRecord, W, H);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000069 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.orga0950412014-05-29 16:52:40 +000075 SkRecorder translateCanvas(&translateRecord, W, H);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000076 SkMatrix translate;
77 translate.setTranslate(20, 20);
78 translateCanvas.setMatrix(translate);
79
mtklein5ad6ee12014-08-11 08:08:43 -070080 SkRecordDraw(scaleRecord, &translateCanvas, NULL/*bbh*/, NULL/*callback*/);
Mike Kleinc11530e2014-06-24 11:29:06 -040081 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.org0a98d872014-05-19 15:15:24 +000086
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 Kleinc11530e2014-06-24 11:29:06 -040093 setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 2);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000094 SkMatrix expected = scale;
95 expected.postConcat(translate);
96 REPORTER_ASSERT(r, setMatrix->matrix == expected);
97}
mtkleina723b572014-08-15 11:49:49 -070098
99struct TestBBH : public SkBBoxHierarchy {
mtklein533eb782014-08-27 10:39:42 -0700100 virtual void insert(void* data, const SkRect& bounds, bool defer) SK_OVERRIDE {
mtkleina723b572014-08-15 11:49:49 -0700101 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
mtklein533eb782014-08-27 10:39:42 -0700108 virtual void search(const SkRect& query, SkTDArray<void*>* results) const SK_OVERRIDE {}
mtkleina723b572014-08-15 11:49:49 -0700109 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;
mtklein533eb782014-08-27 10:39:42 -0700115 SkRect bounds;
mtkleina723b572014-08-15 11:49:49 -0700116 };
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.
122DEF_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
mtklein533eb782014-08-27 10:39:42 -0700140 REPORTER_ASSERT(r, bbh.entries[i].bounds == SkRect::MakeWH(400, 480));
mtkleina723b572014-08-15 11:49:49 -0700141 }
142}