Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / skia / ext / pixel_ref_utils_unittest.cc
blob2e1fa245050a84b5d3593a5daa39f1b78216c8e6
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/compiler_specific.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "cc/test/geometry_test_utils.h"
8 #include "skia/ext/pixel_ref_utils.h"
9 #include "skia/ext/refptr.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/skia/include/core/SkBitmap.h"
12 #include "third_party/skia/include/core/SkCanvas.h"
13 #include "third_party/skia/include/core/SkPictureRecorder.h"
14 #include "third_party/skia/include/core/SkPixelRef.h"
15 #include "third_party/skia/include/core/SkPoint.h"
16 #include "third_party/skia/include/core/SkShader.h"
17 #include "third_party/skia/src/core/SkOrderedReadBuffer.h"
18 #include "ui/gfx/geometry/rect.h"
19 #include "ui/gfx/skia_util.h"
21 namespace skia {
23 namespace {
25 void CreateBitmap(gfx::Size size, const char* uri, SkBitmap* bitmap);
27 class TestDiscardableShader : public SkShader {
28 public:
29 TestDiscardableShader() {
30 CreateBitmap(gfx::Size(50, 50), "discardable", &bitmap_);
33 TestDiscardableShader(SkReadBuffer& buffer) {
34 CreateBitmap(gfx::Size(50, 50), "discardable", &bitmap_);
37 SkShader::BitmapType asABitmap(SkBitmap* bitmap,
38 SkMatrix* matrix,
39 TileMode xy[2]) const override {
40 if (bitmap)
41 *bitmap = bitmap_;
42 return SkShader::kDefault_BitmapType;
45 // not indended to return an actual context. Just need to supply this.
46 size_t contextSize() const override { return sizeof(SkShader::Context); }
48 void flatten(SkWriteBuffer&) const override {}
50 // Manual expansion of SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS to
51 // satisfy Chrome's style checker, since Skia isn't ready to make the C++11
52 // leap yet.
53 private:
54 static SkFlattenable* CreateProc(SkReadBuffer&);
56 public:
57 Factory getFactory() const override { return CreateProc; }
59 private:
60 SkBitmap bitmap_;
63 SkFlattenable* TestDiscardableShader::CreateProc(SkReadBuffer&) {
64 return new TestDiscardableShader;
67 void CreateBitmap(gfx::Size size, const char* uri, SkBitmap* bitmap) {
68 bitmap->allocN32Pixels(size.width(), size.height());
69 bitmap->pixelRef()->setImmutable();
70 bitmap->pixelRef()->setURI(uri);
73 SkCanvas* StartRecording(SkPictureRecorder* recorder, gfx::Rect layer_rect) {
74 SkCanvas* canvas =
75 recorder->beginRecording(layer_rect.width(), layer_rect.height());
77 canvas->save();
78 canvas->translate(-layer_rect.x(), -layer_rect.y());
79 canvas->clipRect(SkRect::MakeXYWH(
80 layer_rect.x(), layer_rect.y(), layer_rect.width(), layer_rect.height()));
82 return canvas;
85 SkPicture* StopRecording(SkPictureRecorder* recorder, SkCanvas* canvas) {
86 canvas->restore();
87 return recorder->endRecordingAsPicture();
90 } // namespace
92 void VerifyScales(SkScalar x_scale,
93 SkScalar y_scale,
94 const SkMatrix& matrix,
95 int source_line) {
96 SkSize scales;
97 bool success = matrix.decomposeScale(&scales);
98 EXPECT_TRUE(success) << "line: " << source_line;
99 EXPECT_FLOAT_EQ(x_scale, scales.width()) << "line: " << source_line;
100 EXPECT_FLOAT_EQ(y_scale, scales.height()) << "line: " << source_line;
103 TEST(PixelRefUtilsTest, DrawPaint) {
104 gfx::Rect layer_rect(0, 0, 256, 256);
106 SkPictureRecorder recorder;
107 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
109 TestDiscardableShader first_shader;
110 SkPaint first_paint;
111 first_paint.setShader(&first_shader);
113 TestDiscardableShader second_shader;
114 SkPaint second_paint;
115 second_paint.setShader(&second_shader);
117 TestDiscardableShader third_shader;
118 SkPaint third_paint;
119 third_paint.setShader(&third_shader);
121 canvas->drawPaint(first_paint);
122 canvas->clipRect(SkRect::MakeXYWH(34, 45, 56, 67));
123 canvas->drawPaint(second_paint);
125 canvas->save();
126 canvas->scale(2.f, 3.f);
127 canvas->drawPaint(second_paint);
128 canvas->restore();
130 // Total clip is now (34, 45, 56, 55)
131 canvas->clipRect(SkRect::MakeWH(100, 100));
132 canvas->drawPaint(third_paint);
134 skia::RefPtr<SkPicture> picture =
135 skia::AdoptRef(StopRecording(&recorder, canvas));
137 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
138 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
140 EXPECT_EQ(4u, pixel_refs.size());
141 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 256, 256),
142 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
143 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
144 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
145 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 67),
146 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
147 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
148 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 67),
149 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
150 VerifyScales(2.f, 3.f, pixel_refs[2].matrix, __LINE__);
151 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
152 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 55),
153 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
154 VerifyScales(1.f, 1.f, pixel_refs[3].matrix, __LINE__);
155 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[3].filter_quality);
158 TEST(PixelRefUtilsTest, DrawPoints) {
159 gfx::Rect layer_rect(0, 0, 256, 256);
161 SkPictureRecorder recorder;
162 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
164 TestDiscardableShader first_shader;
165 SkPaint first_paint;
166 first_paint.setShader(&first_shader);
168 TestDiscardableShader second_shader;
169 SkPaint second_paint;
170 second_paint.setShader(&second_shader);
172 TestDiscardableShader third_shader;
173 SkPaint third_paint;
174 third_paint.setShader(&third_shader);
176 SkPoint points[3];
177 points[0].set(10, 10);
178 points[1].set(100, 20);
179 points[2].set(50, 100);
180 // (10, 10, 90, 90).
181 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, points, first_paint);
183 canvas->save();
185 canvas->clipRect(SkRect::MakeWH(50, 50));
186 // (10, 10, 40, 40).
187 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, points, second_paint);
189 canvas->restore();
191 points[0].set(50, 55);
192 points[1].set(50, 55);
193 points[2].set(200, 200);
194 // (50, 55, 150, 145).
195 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, points, third_paint);
197 skia::RefPtr<SkPicture> picture =
198 skia::AdoptRef(StopRecording(&recorder, canvas));
200 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
201 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
203 EXPECT_EQ(3u, pixel_refs.size());
204 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
205 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
206 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
207 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
208 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 40, 40),
209 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
210 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
211 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
212 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145),
213 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
214 VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__);
215 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
218 TEST(PixelRefUtilsTest, DrawRect) {
219 gfx::Rect layer_rect(0, 0, 256, 256);
221 SkPictureRecorder recorder;
222 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
224 TestDiscardableShader first_shader;
225 SkPaint first_paint;
226 first_paint.setShader(&first_shader);
228 TestDiscardableShader second_shader;
229 SkPaint second_paint;
230 second_paint.setShader(&second_shader);
232 TestDiscardableShader third_shader;
233 SkPaint third_paint;
234 third_paint.setShader(&third_shader);
236 // (10, 20, 30, 40).
237 canvas->drawRect(SkRect::MakeXYWH(10, 20, 30, 40), first_paint);
239 canvas->save();
241 canvas->translate(5, 17);
242 // (5, 50, 25, 35)
243 canvas->drawRect(SkRect::MakeXYWH(0, 33, 25, 35), second_paint);
245 canvas->restore();
247 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
248 canvas->translate(20, 20);
249 // (50, 50, 50, 50)
250 canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), third_paint);
252 skia::RefPtr<SkPicture> picture =
253 skia::AdoptRef(StopRecording(&recorder, canvas));
255 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
256 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
258 EXPECT_EQ(3u, pixel_refs.size());
259 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 20, 30, 40),
260 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
261 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
262 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
263 EXPECT_FLOAT_RECT_EQ(gfx::RectF(5, 50, 25, 35),
264 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
265 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
266 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
267 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
268 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
269 VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__);
270 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
273 TEST(PixelRefUtilsTest, DrawRRect) {
274 gfx::Rect layer_rect(0, 0, 256, 256);
276 SkPictureRecorder recorder;
277 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
279 TestDiscardableShader first_shader;
280 SkPaint first_paint;
281 first_paint.setShader(&first_shader);
283 TestDiscardableShader second_shader;
284 SkPaint second_paint;
285 second_paint.setShader(&second_shader);
287 TestDiscardableShader third_shader;
288 SkPaint third_paint;
289 third_paint.setShader(&third_shader);
291 SkRRect rrect;
292 rrect.setRect(SkRect::MakeXYWH(10, 20, 30, 40));
294 // (10, 20, 30, 40).
295 canvas->drawRRect(rrect, first_paint);
297 canvas->save();
299 canvas->translate(5, 17);
300 rrect.setRect(SkRect::MakeXYWH(0, 33, 25, 35));
301 // (5, 50, 25, 35)
302 canvas->drawRRect(rrect, second_paint);
304 canvas->restore();
306 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
307 canvas->translate(20, 20);
308 rrect.setRect(SkRect::MakeXYWH(0, 0, 100, 100));
309 // (50, 50, 50, 50)
310 canvas->drawRRect(rrect, third_paint);
312 skia::RefPtr<SkPicture> picture =
313 skia::AdoptRef(StopRecording(&recorder, canvas));
315 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
316 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
318 EXPECT_EQ(3u, pixel_refs.size());
319 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 20, 30, 40),
320 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
321 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
322 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
323 EXPECT_FLOAT_RECT_EQ(gfx::RectF(5, 50, 25, 35),
324 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
325 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
326 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
327 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
328 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
329 VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__);
330 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
333 TEST(PixelRefUtilsTest, DrawOval) {
334 gfx::Rect layer_rect(0, 0, 256, 256);
336 SkPictureRecorder recorder;
337 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
339 TestDiscardableShader first_shader;
340 SkPaint first_paint;
341 first_paint.setShader(&first_shader);
343 TestDiscardableShader second_shader;
344 SkPaint second_paint;
345 second_paint.setShader(&second_shader);
347 TestDiscardableShader third_shader;
348 SkPaint third_paint;
349 third_paint.setShader(&third_shader);
351 canvas->save();
353 canvas->scale(2.f, 0.5f);
354 // (20, 10, 60, 20).
355 canvas->drawOval(SkRect::MakeXYWH(10, 20, 30, 40), first_paint);
357 canvas->restore();
358 canvas->save();
360 canvas->translate(1, 2);
361 // (1, 35, 25, 35)
362 canvas->drawRect(SkRect::MakeXYWH(0, 33, 25, 35), second_paint);
364 canvas->restore();
366 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
367 canvas->translate(20, 20);
368 // (50, 50, 50, 50)
369 canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), third_paint);
371 skia::RefPtr<SkPicture> picture =
372 skia::AdoptRef(StopRecording(&recorder, canvas));
374 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
375 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
377 EXPECT_EQ(3u, pixel_refs.size());
378 EXPECT_FLOAT_RECT_EQ(gfx::RectF(20, 10, 60, 20),
379 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
380 VerifyScales(2.f, 0.5f, pixel_refs[0].matrix, __LINE__);
381 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
382 EXPECT_FLOAT_RECT_EQ(gfx::RectF(1, 35, 25, 35),
383 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
384 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
385 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
386 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
387 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
388 VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__);
389 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
392 TEST(PixelRefUtilsTest, DrawPath) {
393 gfx::Rect layer_rect(0, 0, 256, 256);
395 SkPictureRecorder recorder;
396 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
398 TestDiscardableShader first_shader;
399 SkPaint first_paint;
400 first_paint.setShader(&first_shader);
402 TestDiscardableShader second_shader;
403 SkPaint second_paint;
404 second_paint.setShader(&second_shader);
406 SkPath path;
407 path.moveTo(12, 13);
408 path.lineTo(50, 50);
409 path.lineTo(22, 101);
411 // (12, 13, 38, 88).
412 canvas->drawPath(path, first_paint);
414 canvas->save();
415 canvas->clipRect(SkRect::MakeWH(50, 50));
417 // (12, 13, 38, 37).
418 canvas->drawPath(path, second_paint);
420 canvas->restore();
422 skia::RefPtr<SkPicture> picture =
423 skia::AdoptRef(StopRecording(&recorder, canvas));
425 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
426 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
428 EXPECT_EQ(2u, pixel_refs.size());
429 EXPECT_FLOAT_RECT_EQ(gfx::RectF(12, 13, 38, 88),
430 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
431 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
432 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
433 EXPECT_FLOAT_RECT_EQ(gfx::RectF(12, 13, 38, 37),
434 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
435 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
436 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
439 TEST(PixelRefUtilsTest, DrawBitmap) {
440 gfx::Rect layer_rect(0, 0, 256, 256);
442 SkPictureRecorder recorder;
443 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
445 SkBitmap first;
446 CreateBitmap(gfx::Size(50, 50), "discardable", &first);
447 SkBitmap second;
448 CreateBitmap(gfx::Size(50, 50), "discardable", &second);
449 SkBitmap third;
450 CreateBitmap(gfx::Size(50, 50), "discardable", &third);
451 SkBitmap fourth;
452 CreateBitmap(gfx::Size(50, 1), "discardable", &fourth);
453 SkBitmap fifth;
454 CreateBitmap(gfx::Size(10, 10), "discardable", &fifth);
455 SkBitmap sixth;
456 CreateBitmap(gfx::Size(10, 10), "discardable", &sixth);
458 canvas->save();
460 // At (0, 0).
461 canvas->drawBitmap(first, 0, 0);
462 canvas->translate(25, 0);
463 // At (25, 0).
464 canvas->drawBitmap(second, 0, 0);
465 canvas->translate(0, 50);
466 // At (50, 50).
467 canvas->drawBitmap(third, 25, 0);
469 canvas->restore();
470 canvas->save();
472 canvas->translate(1, 0);
473 canvas->rotate(90);
474 // At (1, 0), rotated 90 degrees
475 canvas->drawBitmap(fourth, 0, 0);
477 canvas->restore();
478 canvas->save();
480 canvas->scale(5.f, 6.f);
481 // At (0, 0), scaled by 5 and 6
482 canvas->drawBitmap(fifth, 0, 0);
484 canvas->restore();
486 canvas->rotate(27);
487 canvas->scale(3.3f, 0.4f);
489 canvas->drawBitmap(sixth, 0, 0);
491 canvas->restore();
493 skia::RefPtr<SkPicture> picture =
494 skia::AdoptRef(StopRecording(&recorder, canvas));
496 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
497 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
499 EXPECT_EQ(6u, pixel_refs.size());
500 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
501 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
502 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
503 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
504 EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50),
505 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
506 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
507 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
508 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
509 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
510 VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__);
511 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
512 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 1, 50),
513 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
514 VerifyScales(1.f, 1.f, pixel_refs[3].matrix, __LINE__);
515 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[3].filter_quality);
516 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 60),
517 gfx::SkRectToRectF(pixel_refs[4].pixel_ref_rect));
518 VerifyScales(5.f, 6.f, pixel_refs[4].matrix, __LINE__);
519 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[4].filter_quality);
520 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 29.403214f, 18.545712f),
521 gfx::SkRectToRectF(pixel_refs[5].pixel_ref_rect));
522 VerifyScales(3.3f, 0.4f, pixel_refs[5].matrix, __LINE__);
523 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[5].filter_quality);
526 TEST(PixelRefUtilsTest, DrawBitmapRect) {
527 gfx::Rect layer_rect(0, 0, 256, 256);
529 SkPictureRecorder recorder;
530 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
532 SkBitmap first;
533 CreateBitmap(gfx::Size(50, 50), "discardable", &first);
534 SkBitmap second;
535 CreateBitmap(gfx::Size(50, 50), "discardable", &second);
536 SkBitmap third;
537 CreateBitmap(gfx::Size(50, 50), "discardable", &third);
539 TestDiscardableShader first_shader;
540 SkPaint first_paint;
541 first_paint.setShader(&first_shader);
543 SkPaint non_discardable_paint;
545 canvas->save();
547 // (0, 0, 100, 100).
548 canvas->drawBitmapRect(
549 first, SkRect::MakeWH(100, 100), &non_discardable_paint);
550 canvas->translate(25, 0);
551 // (75, 50, 10, 10).
552 canvas->drawBitmapRect(
553 second, SkRect::MakeXYWH(50, 50, 10, 10), &non_discardable_paint);
554 canvas->translate(5, 50);
555 // (0, 30, 100, 100). One from bitmap, one from paint.
556 canvas->drawBitmapRect(
557 third, SkRect::MakeXYWH(-30, -20, 100, 100), &first_paint);
559 canvas->restore();
561 skia::RefPtr<SkPicture> picture =
562 skia::AdoptRef(StopRecording(&recorder, canvas));
564 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
565 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
567 EXPECT_EQ(4u, pixel_refs.size());
568 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 100, 100),
569 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
570 VerifyScales(2.f, 2.f, pixel_refs[0].matrix, __LINE__);
571 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
572 EXPECT_FLOAT_RECT_EQ(gfx::RectF(75, 50, 10, 10),
573 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
574 VerifyScales(0.2f, 0.2f, pixel_refs[1].matrix, __LINE__);
575 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
576 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100),
577 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
578 VerifyScales(2.f, 2.f, pixel_refs[2].matrix, __LINE__);
579 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
580 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100),
581 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
582 VerifyScales(2.f, 2.f, pixel_refs[3].matrix, __LINE__);
583 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[3].filter_quality);
586 TEST(PixelRefUtilsTest, DrawSprite) {
587 gfx::Rect layer_rect(0, 0, 256, 256);
589 SkPictureRecorder recorder;
590 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
592 SkBitmap first;
593 CreateBitmap(gfx::Size(50, 50), "discardable", &first);
594 SkBitmap second;
595 CreateBitmap(gfx::Size(50, 50), "discardable", &second);
596 SkBitmap third;
597 CreateBitmap(gfx::Size(50, 50), "discardable", &third);
598 SkBitmap fourth;
599 CreateBitmap(gfx::Size(50, 50), "discardable", &fourth);
600 SkBitmap fifth;
601 CreateBitmap(gfx::Size(50, 50), "discardable", &fifth);
603 canvas->save();
605 // Sprites aren't affected by the current matrix.
607 // (0, 0, 50, 50).
608 canvas->drawSprite(first, 0, 0);
609 canvas->translate(25, 0);
610 // (10, 0, 50, 50).
611 canvas->drawSprite(second, 10, 0);
612 canvas->translate(0, 50);
613 // (25, 0, 50, 50).
614 canvas->drawSprite(third, 25, 0);
616 canvas->restore();
617 canvas->save();
619 canvas->rotate(90);
620 // (0, 0, 50, 50).
621 canvas->drawSprite(fourth, 0, 0);
623 canvas->restore();
625 TestDiscardableShader first_shader;
626 SkPaint first_paint;
627 first_paint.setShader(&first_shader);
629 canvas->scale(5.f, 6.f);
630 // (100, 100, 50, 50).
631 canvas->drawSprite(fifth, 100, 100, &first_paint);
633 skia::RefPtr<SkPicture> picture =
634 skia::AdoptRef(StopRecording(&recorder, canvas));
636 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
637 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
639 EXPECT_EQ(6u, pixel_refs.size());
640 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
641 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
642 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
643 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
644 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 0, 50, 50),
645 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
646 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
647 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
648 EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50),
649 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
650 VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__);
651 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
652 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
653 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
654 VerifyScales(1.f, 1.f, pixel_refs[3].matrix, __LINE__);
655 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[3].filter_quality);
656 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 50, 50),
657 gfx::SkRectToRectF(pixel_refs[4].pixel_ref_rect));
658 VerifyScales(1.f, 1.f, pixel_refs[4].matrix, __LINE__);
659 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[4].filter_quality);
660 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 50, 50),
661 gfx::SkRectToRectF(pixel_refs[5].pixel_ref_rect));
662 VerifyScales(1.f, 1.f, pixel_refs[5].matrix, __LINE__);
663 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[5].filter_quality);
666 TEST(PixelRefUtilsTest, DrawText) {
667 gfx::Rect layer_rect(0, 0, 256, 256);
669 SkPictureRecorder recorder;
670 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
672 TestDiscardableShader first_shader;
673 SkPaint first_paint;
674 first_paint.setShader(&first_shader);
676 SkPoint points[4];
677 points[0].set(10, 50);
678 points[1].set(20, 50);
679 points[2].set(30, 50);
680 points[3].set(40, 50);
682 SkPath path;
683 path.moveTo(10, 50);
684 path.lineTo(20, 50);
685 path.lineTo(30, 50);
686 path.lineTo(40, 50);
687 path.lineTo(50, 50);
689 canvas->drawText("text", 4, 50, 50, first_paint);
690 canvas->drawPosText("text", 4, points, first_paint);
691 canvas->drawTextOnPath("text", 4, path, NULL, first_paint);
693 skia::RefPtr<SkPicture> picture =
694 skia::AdoptRef(StopRecording(&recorder, canvas));
696 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
697 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
699 EXPECT_EQ(3u, pixel_refs.size());
702 TEST(PixelRefUtilsTest, DrawVertices) {
703 gfx::Rect layer_rect(0, 0, 256, 256);
705 SkPictureRecorder recorder;
706 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
708 TestDiscardableShader first_shader;
709 SkPaint first_paint;
710 first_paint.setShader(&first_shader);
712 TestDiscardableShader second_shader;
713 SkPaint second_paint;
714 second_paint.setShader(&second_shader);
716 TestDiscardableShader third_shader;
717 SkPaint third_paint;
718 third_paint.setShader(&third_shader);
720 SkPoint points[3];
721 SkColor colors[3];
722 uint16_t indecies[3] = {0, 1, 2};
723 points[0].set(10, 10);
724 points[1].set(100, 20);
725 points[2].set(50, 100);
726 // (10, 10, 90, 90).
727 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
729 points,
730 points,
731 colors,
732 NULL,
733 indecies,
735 first_paint);
737 canvas->save();
739 canvas->clipRect(SkRect::MakeWH(50, 50));
740 // (10, 10, 40, 40).
741 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
743 points,
744 points,
745 colors,
746 NULL,
747 indecies,
749 second_paint);
751 canvas->restore();
753 points[0].set(50, 55);
754 points[1].set(50, 55);
755 points[2].set(200, 200);
756 // (50, 55, 150, 145).
757 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
759 points,
760 points,
761 colors,
762 NULL,
763 indecies,
765 third_paint);
767 skia::RefPtr<SkPicture> picture =
768 skia::AdoptRef(StopRecording(&recorder, canvas));
770 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
771 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
773 EXPECT_EQ(3u, pixel_refs.size());
774 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
775 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
776 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
777 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
778 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 40, 40),
779 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
780 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
781 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
782 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145),
783 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
784 VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__);
785 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
788 } // namespace skia