Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(216)

Side by Side Diff: tests/RecordOptsTest.cpp

Issue 835973005: Fold alpha to the inner savelayer in savelayer-savelayer-restore patterns (Closed) Base URL: https://skia.googlesource.com/skia.git@record-opt-savelayer-draw-non-opaque
Patch Set: remove one too many inttoscalar Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkRecordPattern.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2014 Google Inc. 2 * Copyright 2014 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "Test.h" 8 #include "Test.h"
9 #include "RecordTestUtils.h" 9 #include "RecordTestUtils.h"
10 10
11 #include "SkColorFilter.h"
11 #include "SkRecord.h" 12 #include "SkRecord.h"
12 #include "SkRecordOpts.h" 13 #include "SkRecordOpts.h"
13 #include "SkRecorder.h" 14 #include "SkRecorder.h"
14 #include "SkRecords.h" 15 #include "SkRecords.h"
15 #include "SkXfermode.h" 16 #include "SkXfermode.h"
17 #include "SkPictureRecorder.h"
18 #include "SkPictureImageFilter.h"
16 19
17 static const int W = 1920, H = 1080; 20 static const int W = 1920, H = 1080;
18 21
19 DEF_TEST(RecordOpts_NoopDraw, r) { 22 DEF_TEST(RecordOpts_NoopDraw, r) {
20 SkRecord record; 23 SkRecord record;
21 SkRecorder recorder(&record, W, H); 24 SkRecorder recorder(&record, W, H);
22 25
23 recorder.drawRect(SkRect::MakeWH(200, 200), SkPaint()); 26 recorder.drawRect(SkRect::MakeWH(200, 200), SkPaint());
24 recorder.drawRect(SkRect::MakeWH(300, 300), SkPaint()); 27 recorder.drawRect(SkRect::MakeWH(300, 300), SkPaint());
25 recorder.drawRect(SkRect::MakeWH(100, 100), SkPaint()); 28 recorder.drawRect(SkRect::MakeWH(100, 100), SkPaint());
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 // SaveLayer/Restore removed: we can fold in the alpha! 165 // SaveLayer/Restore removed: we can fold in the alpha!
163 recorder.saveLayer(NULL, &alphaOnlyLayerPaint); 166 recorder.saveLayer(NULL, &alphaOnlyLayerPaint);
164 recorder.drawRect(draw, opaqueDrawPaint); 167 recorder.drawRect(draw, opaqueDrawPaint);
165 recorder.restore(); 168 recorder.restore();
166 assert_savelayer_restore(r, &record, 15, true); 169 assert_savelayer_restore(r, &record, 15, true);
167 170
168 const SkRecords::DrawRect* drawRect = assert_type<SkRecords::DrawRect>(r, re cord, 16); 171 const SkRecords::DrawRect* drawRect = assert_type<SkRecords::DrawRect>(r, re cord, 16);
169 REPORTER_ASSERT(r, drawRect != NULL); 172 REPORTER_ASSERT(r, drawRect != NULL);
170 REPORTER_ASSERT(r, drawRect->paint.getColor() == 0x03020202); 173 REPORTER_ASSERT(r, drawRect->paint.getColor() == 0x03020202);
171 } 174 }
175
176 static void assert_merge_svg_opacity_and_filter_layers(skiatest::Reporter* r,
177 SkRecord* record,
178 unsigned i,
179 bool shouldBeNoOped) {
180 SkRecordMergeSvgOpacityAndFilterLayers(record);
181 if (shouldBeNoOped) {
182 assert_type<SkRecords::NoOp>(r, *record, i);
183 assert_type<SkRecords::NoOp>(r, *record, i + 6);
184 } else {
185 assert_type<SkRecords::SaveLayer>(r, *record, i);
186 assert_type<SkRecords::Restore>(r, *record, i + 6);
187 }
188 }
189
190 DEF_TEST(RecordOpts_MergeSvgOpacityAndFilterLayers, r) {
191 SkRecord record;
192 SkRecorder recorder(&record, W, H);
193
194 SkRect bounds = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(200));
195 SkRect clip = SkRect::MakeWH(SkIntToScalar(50), SkIntToScalar(60));
196
197 SkPaint alphaOnlyLayerPaint;
198 alphaOnlyLayerPaint.setColor(0x03000000); // Only alpha.
199 SkPaint translucentLayerPaint;
200 translucentLayerPaint.setColor(0x03040506); // Not only alpha.
201 SkPaint xfermodePaint;
202 xfermodePaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
203 SkPaint colorFilterPaint;
204 colorFilterPaint.setColorFilter(
205 SkColorFilter::CreateModeFilter(SK_ColorLTGRAY, SkXfermode::kSrcIn_Mode) )->unref();
206
207 SkPaint opaqueFilterLayerPaint;
208 opaqueFilterLayerPaint.setColor(0xFF020202); // Opaque.
209 SkPaint translucentFilterLayerPaint;
210 translucentFilterLayerPaint.setColor(0x0F020202); // Not opaque.
211 SkAutoTUnref<SkPicture> shape;
212 {
213 SkPictureRecorder recorder;
214 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(100), SkIntToSc alar(100));
215 SkPaint shapePaint;
216 shapePaint.setColor(SK_ColorWHITE);
217 canvas->drawRect(SkRect::MakeWH(SkIntToScalar(50), SkIntToScalar(50)), s hapePaint);
218 shape.reset(recorder.endRecordingAsPicture());
219 }
220 translucentFilterLayerPaint.setImageFilter(SkPictureImageFilter::Create(shap e))->unref();
221
222 int index = 0;
223
224 {
225 // Any combination of these should cause the pattern to be optimized.
226 SkRect* firstBounds[] = { NULL, &bounds };
227 SkPaint* firstPaints[] = { NULL, &alphaOnlyLayerPaint };
228 SkRect* secondBounds[] = { NULL, &bounds };
229 SkPaint* secondPaints[] = { &opaqueFilterLayerPaint, &translucentFilterL ayerPaint };
230
231 for (size_t i = 0; i < SK_ARRAY_COUNT(firstBounds); ++ i) {
232 for (size_t j = 0; j < SK_ARRAY_COUNT(firstPaints); ++j) {
233 for (size_t k = 0; k < SK_ARRAY_COUNT(secondBounds); ++k) {
234 for (size_t m = 0; m < SK_ARRAY_COUNT(secondPaints); ++m) {
235 recorder.saveLayer(firstBounds[i], firstPaints[j]);
236 recorder.save();
237 recorder.clipRect(clip);
238 recorder.saveLayer(secondBounds[k], secondPaints[m]);
239 recorder.restore();
240 recorder.restore();
241 recorder.restore();
242 assert_merge_svg_opacity_and_filter_layers(r, &record, i ndex, true);
243 index += 7;
244 }
245 }
246 }
247 }
248 }
249
250 // These should cause the pattern to stay unoptimized:
251 struct {
252 SkPaint* firstPaint;
253 SkPaint* secondPaint;
254 } noChangeTests[] = {
255 // No change: NULL filter layer paint not implemented.
256 { &alphaOnlyLayerPaint, NULL },
257 // No change: layer paint is not alpha-only.
258 { &translucentLayerPaint, &opaqueFilterLayerPaint },
259 // No change: layer paint has an xfereffect.
260 { &xfermodePaint, &opaqueFilterLayerPaint },
261 // No change: filter layer paint has an xfereffect.
262 { &alphaOnlyLayerPaint, &xfermodePaint },
263 // No change: layer paint has a color filter.
264 { &colorFilterPaint, &opaqueFilterLayerPaint },
265 // No change: filter layer paint has a color filter (until the optimizat ion accounts for
266 // constant color draws that can filter the color).
267 { &alphaOnlyLayerPaint, &colorFilterPaint }
268 };
269
270 for (size_t i = 0; i < SK_ARRAY_COUNT(noChangeTests); ++i) {
271 recorder.saveLayer(NULL, noChangeTests[i].firstPaint);
272 recorder.save();
273 recorder.clipRect(clip);
274 recorder.saveLayer(NULL, noChangeTests[i].secondPaint);
275 recorder.restore();
276 recorder.restore();
277 recorder.restore();
278 assert_merge_svg_opacity_and_filter_layers(r, &record, index, false);
279 index += 7;
280 }
281
282 // Test the folded alpha value.
283 recorder.saveLayer(NULL, &alphaOnlyLayerPaint);
284 recorder.save();
285 recorder.clipRect(clip);
286 recorder.saveLayer(NULL, &opaqueFilterLayerPaint);
287 recorder.restore();
288 recorder.restore();
289 recorder.restore();
290 assert_merge_svg_opacity_and_filter_layers(r, &record, index, true);
291
292 const SkRecords::SaveLayer* saveLayer = assert_type<SkRecords::SaveLayer>(r, record, index + 3);
293 REPORTER_ASSERT(r, saveLayer != NULL);
294 REPORTER_ASSERT(r, saveLayer->paint->getColor() == 0x03020202);
295
296 index += 7;
297
298 // Test that currently we do not fold alphas for patterns without the clip. This is just not
299 // implemented.
300 recorder.saveLayer(NULL, &alphaOnlyLayerPaint);
301 recorder.saveLayer(NULL, &opaqueFilterLayerPaint);
302 recorder.restore();
303 recorder.restore();
304 SkRecordMergeSvgOpacityAndFilterLayers(&record);
305 assert_type<SkRecords::SaveLayer>(r, record, index);
306 assert_type<SkRecords::SaveLayer>(r, record, index + 1);
307 assert_type<SkRecords::Restore>(r, record, index + 2);
308 assert_type<SkRecords::Restore>(r, record, index + 3);
309 index += 4;
310 }
OLDNEW
« no previous file with comments | « src/core/SkRecordPattern.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698