blob: e17b5e6bfa2a3fd4597b59fff19382912025f902 [file] [log] [blame]
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +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
8#include "Test.h"
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +00009#include "RecordTestUtils.h"
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000010
11#include "SkRecord.h"
12#include "SkRecordOpts.h"
13#include "SkRecorder.h"
14#include "SkRecords.h"
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +000015#include "SkXfermode.h"
16
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000017static const int W = 1920, H = 1080;
18
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000019static void draw_pos_text(SkCanvas* canvas, const char* text, bool constantY) {
20 const size_t len = strlen(text);
21 SkAutoTMalloc<SkPoint> pos(len);
22 for (size_t i = 0; i < len; i++) {
23 pos[i].fX = (SkScalar)i;
24 pos[i].fY = constantY ? SK_Scalar1 : (SkScalar)i;
25 }
26 canvas->drawPosText(text, len, pos, SkPaint());
27}
28
29DEF_TEST(RecordOpts_StrengthReduction, r) {
30 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000031 SkRecorder recorder(&record, W, H);
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000032
33 // We can convert a drawPosText into a drawPosTextH when all the Ys are the same.
34 draw_pos_text(&recorder, "This will be reduced to drawPosTextH.", true);
35 draw_pos_text(&recorder, "This cannot be reduced to drawPosTextH.", false);
36
37 SkRecordReduceDrawPosTextStrength(&record);
38
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +000039 assert_type<SkRecords::DrawPosTextH>(r, record, 0);
40 assert_type<SkRecords::DrawPosText>(r, record, 1);
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000041}
42
commit-bot@chromium.org467705a2014-05-07 17:17:48 +000043DEF_TEST(RecordOpts_NoopDrawSaveRestore, r) {
44 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000045 SkRecorder recorder(&record, W, H);
commit-bot@chromium.org467705a2014-05-07 17:17:48 +000046
47 // The save and restore are pointless if there's only draw commands in the middle.
48 recorder.save();
49 recorder.drawRect(SkRect::MakeWH(200, 200), SkPaint());
50 recorder.drawRect(SkRect::MakeWH(300, 300), SkPaint());
51 recorder.drawRect(SkRect::MakeWH(100, 100), SkPaint());
52 recorder.restore();
53
54 record.replace<SkRecords::NoOp>(2); // NoOps should be allowed.
55
56 SkRecordNoopSaveRestores(&record);
57
58 assert_type<SkRecords::NoOp>(r, record, 0);
59 assert_type<SkRecords::DrawRect>(r, record, 1);
60 assert_type<SkRecords::NoOp>(r, record, 2);
61 assert_type<SkRecords::DrawRect>(r, record, 3);
62 assert_type<SkRecords::NoOp>(r, record, 4);
63}
64
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +000065DEF_TEST(RecordOpts_SingleNoopSaveRestore, r) {
66 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000067 SkRecorder recorder(&record, W, H);
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +000068
69 recorder.save();
70 recorder.clipRect(SkRect::MakeWH(200, 200));
71 recorder.restore();
72
73 SkRecordNoopSaveRestores(&record);
74 for (unsigned i = 0; i < 3; i++) {
75 assert_type<SkRecords::NoOp>(r, record, i);
76 }
77}
78
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000079DEF_TEST(RecordOpts_NoopSaveRestores, r) {
80 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000081 SkRecorder recorder(&record, W, H);
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000082
83 // The second pass will clean up this pair after the first pass noops all the innards.
84 recorder.save();
85 // A simple pointless pair of save/restore.
86 recorder.save();
87 recorder.restore();
88
89 // As long as we don't draw in there, everything is a noop.
90 recorder.save();
91 recorder.clipRect(SkRect::MakeWH(200, 200));
92 recorder.clipRect(SkRect::MakeWH(100, 100));
93 recorder.restore();
94 recorder.restore();
95
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000096 SkRecordNoopSaveRestores(&record);
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000097 for (unsigned index = 0; index < 8; index++) {
commit-bot@chromium.org7066bf32014-05-05 17:09:05 +000098 assert_type<SkRecords::NoOp>(r, record, index);
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000099 }
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +0000100}
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000101
mtklein99d6a9e2014-09-10 16:08:27 -0700102DEF_TEST(RecordOpts_SaveSaveLayerRestoreRestore, r) {
103 SkRecord record;
104 SkRecorder recorder(&record, W, H);
105
106 // A previous bug NoOp'd away the first 3 commands.
107 recorder.save();
108 recorder.saveLayer(NULL, NULL);
109 recorder.restore();
110 recorder.restore();
111
112 SkRecordNoopSaveRestores(&record);
113 assert_type<SkRecords::Save> (r, record, 0);
114 assert_type<SkRecords::SaveLayer>(r, record, 1);
115 assert_type<SkRecords::Restore> (r, record, 2);
116 assert_type<SkRecords::Restore> (r, record, 3);
117}
118
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000119static void assert_savelayer_restore(skiatest::Reporter* r,
120 SkRecord* record,
121 unsigned i,
122 bool shouldBeNoOped) {
123 SkRecordNoopSaveLayerDrawRestores(record);
124 if (shouldBeNoOped) {
125 assert_type<SkRecords::NoOp>(r, *record, i);
126 assert_type<SkRecords::NoOp>(r, *record, i+2);
127 } else {
128 assert_type<SkRecords::SaveLayer>(r, *record, i);
129 assert_type<SkRecords::Restore>(r, *record, i+2);
130 }
131}
132
133DEF_TEST(RecordOpts_NoopSaveLayerDrawRestore, r) {
134 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +0000135 SkRecorder recorder(&record, W, H);
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000136
137 SkRect bounds = SkRect::MakeWH(100, 200);
138 SkRect draw = SkRect::MakeWH(50, 60);
139
140 SkPaint goodLayerPaint, badLayerPaint, worseLayerPaint;
141 goodLayerPaint.setColor(0x03000000); // Only alpha.
142 badLayerPaint.setColor( 0x03040506); // Not only alpha.
143 worseLayerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode); // Any effect will do.
144
145 SkPaint goodDrawPaint, badDrawPaint;
146 goodDrawPaint.setColor(0xFF020202); // Opaque.
147 badDrawPaint.setColor( 0x0F020202); // Not opaque.
148
149 // No change: optimization can't handle bounds.
150 recorder.saveLayer(&bounds, NULL);
151 recorder.drawRect(draw, goodDrawPaint);
152 recorder.restore();
153 assert_savelayer_restore(r, &record, 0, false);
154
155 // SaveLayer/Restore removed: no bounds + no paint = no point.
156 recorder.saveLayer(NULL, NULL);
157 recorder.drawRect(draw, goodDrawPaint);
158 recorder.restore();
159 assert_savelayer_restore(r, &record, 3, true);
160
161 // TODO(mtklein): test case with null draw paint
162
163 // No change: layer paint isn't alpha-only.
164 recorder.saveLayer(NULL, &badLayerPaint);
165 recorder.drawRect(draw, goodDrawPaint);
166 recorder.restore();
167 assert_savelayer_restore(r, &record, 6, false);
168
169 // No change: layer paint has an effect.
170 recorder.saveLayer(NULL, &worseLayerPaint);
171 recorder.drawRect(draw, goodDrawPaint);
172 recorder.restore();
173 assert_savelayer_restore(r, &record, 9, false);
174
175 // No change: draw paint isn't opaque.
176 recorder.saveLayer(NULL, &goodLayerPaint);
177 recorder.drawRect(draw, badDrawPaint);
178 recorder.restore();
179 assert_savelayer_restore(r, &record, 12, false);
180
181 // SaveLayer/Restore removed: we can fold in the alpha!
182 recorder.saveLayer(NULL, &goodLayerPaint);
183 recorder.drawRect(draw, goodDrawPaint);
184 recorder.restore();
185 assert_savelayer_restore(r, &record, 15, true);
186
187 const SkRecords::DrawRect* drawRect = assert_type<SkRecords::DrawRect>(r, record, 16);
188 REPORTER_ASSERT(r, drawRect != NULL);
189 REPORTER_ASSERT(r, drawRect->paint.getColor() == 0x03020202);
190}