Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / skia / ext / pixel_ref_utils_unittest.cc
blobffe1dc068ea1cdb511445c9a2162e32672e8b177
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/SkImage.h"
14 #include "third_party/skia/include/core/SkImageGenerator.h"
15 #include "third_party/skia/include/core/SkPictureRecorder.h"
16 #include "third_party/skia/include/core/SkPixelRef.h"
17 #include "third_party/skia/include/core/SkPoint.h"
18 #include "third_party/skia/include/core/SkShader.h"
19 #include "third_party/skia/src/core/SkOrderedReadBuffer.h"
20 #include "ui/gfx/geometry/rect.h"
21 #include "ui/gfx/skia_util.h"
23 namespace skia {
25 namespace {
27 class TestImageGenerator : public SkImageGenerator {
28 public:
29 TestImageGenerator(const SkImageInfo& info)
30 : SkImageGenerator(info) { }
33 skia::RefPtr<SkImage> CreateDiscardableImage(const gfx::Size& size) {
34 const SkImageInfo info =
35 SkImageInfo::MakeN32Premul(size.width(), size.height());
36 return skia::AdoptRef(
37 SkImage::NewFromGenerator(new TestImageGenerator(info)));
40 void SetDiscardableShader(SkPaint* paint) {
41 skia::RefPtr<SkImage> image = CreateDiscardableImage(gfx::Size(50, 50));
42 skia::RefPtr<SkShader> shader = skia::AdoptRef(
43 image->newShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
44 paint->setShader(shader.get());
47 SkCanvas* StartRecording(SkPictureRecorder* recorder, gfx::Rect layer_rect) {
48 SkCanvas* canvas =
49 recorder->beginRecording(layer_rect.width(), layer_rect.height());
51 canvas->save();
52 canvas->translate(-layer_rect.x(), -layer_rect.y());
53 canvas->clipRect(SkRect::MakeXYWH(
54 layer_rect.x(), layer_rect.y(), layer_rect.width(), layer_rect.height()));
56 return canvas;
59 SkPicture* StopRecording(SkPictureRecorder* recorder, SkCanvas* canvas) {
60 canvas->restore();
61 return recorder->endRecordingAsPicture();
64 } // namespace
66 void VerifyScales(SkScalar x_scale,
67 SkScalar y_scale,
68 const SkMatrix& matrix,
69 int source_line) {
70 SkSize scales;
71 bool success = matrix.decomposeScale(&scales);
72 EXPECT_TRUE(success) << "line: " << source_line;
73 EXPECT_FLOAT_EQ(x_scale, scales.width()) << "line: " << source_line;
74 EXPECT_FLOAT_EQ(y_scale, scales.height()) << "line: " << source_line;
77 TEST(PixelRefUtilsTest, DrawPaint) {
78 gfx::Rect layer_rect(0, 0, 256, 256);
80 SkPictureRecorder recorder;
81 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
83 SkPaint first_paint;
84 SetDiscardableShader(&first_paint);
86 SkPaint second_paint;
87 SetDiscardableShader(&second_paint);
89 SkPaint third_paint;
90 SetDiscardableShader(&third_paint);
92 canvas->drawPaint(first_paint);
93 canvas->clipRect(SkRect::MakeXYWH(34, 45, 56, 67));
94 canvas->drawPaint(second_paint);
96 canvas->save();
97 canvas->scale(2.f, 3.f);
98 canvas->drawPaint(second_paint);
99 canvas->restore();
101 // Total clip is now (34, 45, 56, 55)
102 canvas->clipRect(SkRect::MakeWH(100, 100));
103 canvas->drawPaint(third_paint);
105 skia::RefPtr<SkPicture> picture =
106 skia::AdoptRef(StopRecording(&recorder, canvas));
108 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
109 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
111 EXPECT_EQ(4u, pixel_refs.size());
112 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 256, 256),
113 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
114 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
115 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
116 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 67),
117 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
118 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
119 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 67),
120 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
121 VerifyScales(2.f, 3.f, pixel_refs[2].matrix, __LINE__);
122 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
123 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 55),
124 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
125 VerifyScales(1.f, 1.f, pixel_refs[3].matrix, __LINE__);
126 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[3].filter_quality);
129 TEST(PixelRefUtilsTest, DrawPoints) {
130 gfx::Rect layer_rect(0, 0, 256, 256);
132 SkPictureRecorder recorder;
133 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
135 SkPaint first_paint;
136 SetDiscardableShader(&first_paint);
138 SkPaint second_paint;
139 SetDiscardableShader(&second_paint);
141 SkPaint third_paint;
142 SetDiscardableShader(&third_paint);
144 SkPoint points[3];
145 points[0].set(10, 10);
146 points[1].set(100, 20);
147 points[2].set(50, 100);
148 // (10, 10, 90, 90).
149 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, points, first_paint);
151 canvas->save();
153 canvas->clipRect(SkRect::MakeWH(50, 50));
154 // (10, 10, 90, 90).
155 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, points, second_paint);
157 canvas->restore();
159 points[0].set(50, 55);
160 points[1].set(50, 55);
161 points[2].set(200, 200);
162 // (50, 55, 150, 145).
163 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, points, third_paint);
165 skia::RefPtr<SkPicture> picture =
166 skia::AdoptRef(StopRecording(&recorder, canvas));
168 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
169 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
171 EXPECT_EQ(3u, pixel_refs.size());
172 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
173 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
174 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
175 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
176 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
177 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
178 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
179 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
180 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145),
181 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
182 VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__);
183 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
186 TEST(PixelRefUtilsTest, DrawRect) {
187 gfx::Rect layer_rect(0, 0, 256, 256);
189 SkPictureRecorder recorder;
190 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
192 SkPaint first_paint;
193 SetDiscardableShader(&first_paint);
195 SkPaint second_paint;
196 SetDiscardableShader(&second_paint);
198 SkPaint third_paint;
199 SetDiscardableShader(&third_paint);
201 // (10, 20, 30, 40).
202 canvas->drawRect(SkRect::MakeXYWH(10, 20, 30, 40), first_paint);
204 canvas->save();
206 canvas->translate(5, 17);
207 // (5, 50, 25, 35)
208 canvas->drawRect(SkRect::MakeXYWH(0, 33, 25, 35), second_paint);
210 canvas->restore();
212 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
213 canvas->translate(20, 20);
214 // (20, 20, 100, 100)
215 canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), third_paint);
217 skia::RefPtr<SkPicture> picture =
218 skia::AdoptRef(StopRecording(&recorder, canvas));
220 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
221 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
223 EXPECT_EQ(3u, pixel_refs.size());
224 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 20, 30, 40),
225 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
226 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
227 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
228 EXPECT_FLOAT_RECT_EQ(gfx::RectF(5, 50, 25, 35),
229 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
230 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
231 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
232 EXPECT_FLOAT_RECT_EQ(gfx::RectF(20, 20, 100, 100),
233 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
234 VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__);
235 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
238 TEST(PixelRefUtilsTest, DrawRRect) {
239 gfx::Rect layer_rect(0, 0, 256, 256);
241 SkPictureRecorder recorder;
242 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
244 SkPaint first_paint;
245 SetDiscardableShader(&first_paint);
247 SkPaint second_paint;
248 SetDiscardableShader(&second_paint);
250 SkPaint third_paint;
251 SetDiscardableShader(&third_paint);
253 SkRRect rrect;
254 rrect.setRect(SkRect::MakeXYWH(10, 20, 30, 40));
256 // (10, 20, 30, 40).
257 canvas->drawRRect(rrect, first_paint);
259 canvas->save();
261 canvas->translate(5, 17);
262 rrect.setRect(SkRect::MakeXYWH(0, 33, 25, 35));
263 // (5, 50, 25, 35)
264 canvas->drawRRect(rrect, second_paint);
266 canvas->restore();
268 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
269 canvas->translate(20, 20);
270 rrect.setRect(SkRect::MakeXYWH(0, 0, 100, 100));
271 // (20, 20, 100, 100)
272 canvas->drawRRect(rrect, third_paint);
274 skia::RefPtr<SkPicture> picture =
275 skia::AdoptRef(StopRecording(&recorder, canvas));
277 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
278 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
280 EXPECT_EQ(3u, pixel_refs.size());
281 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 20, 30, 40),
282 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
283 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
284 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
285 EXPECT_FLOAT_RECT_EQ(gfx::RectF(5, 50, 25, 35),
286 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
287 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
288 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
289 EXPECT_FLOAT_RECT_EQ(gfx::RectF(20, 20, 100, 100),
290 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
291 VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__);
292 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
295 TEST(PixelRefUtilsTest, DrawOval) {
296 gfx::Rect layer_rect(0, 0, 256, 256);
298 SkPictureRecorder recorder;
299 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
301 SkPaint first_paint;
302 SetDiscardableShader(&first_paint);
304 SkPaint second_paint;
305 SetDiscardableShader(&second_paint);
307 SkPaint third_paint;
308 SetDiscardableShader(&third_paint);
310 canvas->save();
312 canvas->scale(2.f, 0.5f);
313 // (20, 10, 60, 20).
314 canvas->drawOval(SkRect::MakeXYWH(10, 20, 30, 40), first_paint);
316 canvas->restore();
317 canvas->save();
319 canvas->translate(1, 2);
320 // (1, 35, 25, 35)
321 canvas->drawRect(SkRect::MakeXYWH(0, 33, 25, 35), second_paint);
323 canvas->restore();
325 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
326 canvas->translate(20, 20);
327 // (20, 20, 100, 100).
328 canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), third_paint);
330 skia::RefPtr<SkPicture> picture =
331 skia::AdoptRef(StopRecording(&recorder, canvas));
333 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
334 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
336 EXPECT_EQ(3u, pixel_refs.size());
337 EXPECT_FLOAT_RECT_EQ(gfx::RectF(20, 10, 60, 20),
338 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
339 VerifyScales(2.f, 0.5f, pixel_refs[0].matrix, __LINE__);
340 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
341 EXPECT_FLOAT_RECT_EQ(gfx::RectF(1, 35, 25, 35),
342 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
343 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
344 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
345 EXPECT_FLOAT_RECT_EQ(gfx::RectF(20, 20, 100, 100),
346 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
347 VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__);
348 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
351 TEST(PixelRefUtilsTest, DrawPath) {
352 gfx::Rect layer_rect(0, 0, 256, 256);
354 SkPictureRecorder recorder;
355 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
357 SkPaint first_paint;
358 SetDiscardableShader(&first_paint);
360 SkPaint second_paint;
361 SetDiscardableShader(&second_paint);
363 SkPath path;
364 path.moveTo(12, 13);
365 path.lineTo(50, 50);
366 path.lineTo(22, 101);
368 // (12, 13, 38, 88).
369 canvas->drawPath(path, first_paint);
371 canvas->save();
372 canvas->clipRect(SkRect::MakeWH(50, 50));
374 // (12, 13, 38, 88), since clips are ignored as long as the shape is in the
375 // clip.
376 canvas->drawPath(path, second_paint);
378 canvas->restore();
380 skia::RefPtr<SkPicture> picture =
381 skia::AdoptRef(StopRecording(&recorder, canvas));
383 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
384 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
386 EXPECT_EQ(2u, pixel_refs.size());
387 EXPECT_FLOAT_RECT_EQ(gfx::RectF(12, 13, 38, 88),
388 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
389 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
390 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
391 EXPECT_FLOAT_RECT_EQ(gfx::RectF(12, 13, 38, 88),
392 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
393 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
394 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
397 TEST(PixelRefUtilsTest, DrawText) {
398 gfx::Rect layer_rect(0, 0, 256, 256);
400 SkPictureRecorder recorder;
401 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
403 SkPaint first_paint;
404 SetDiscardableShader(&first_paint);
406 SkPoint points[4];
407 points[0].set(10, 50);
408 points[1].set(20, 50);
409 points[2].set(30, 50);
410 points[3].set(40, 50);
412 SkPath path;
413 path.moveTo(10, 50);
414 path.lineTo(20, 50);
415 path.lineTo(30, 50);
416 path.lineTo(40, 50);
417 path.lineTo(50, 50);
419 canvas->drawText("text", 4, 50, 50, first_paint);
420 canvas->drawPosText("text", 4, points, first_paint);
421 canvas->drawTextOnPath("text", 4, path, NULL, first_paint);
423 skia::RefPtr<SkPicture> picture =
424 skia::AdoptRef(StopRecording(&recorder, canvas));
426 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
427 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
429 EXPECT_EQ(3u, pixel_refs.size());
432 TEST(PixelRefUtilsTest, DrawVertices) {
433 gfx::Rect layer_rect(0, 0, 256, 256);
435 SkPictureRecorder recorder;
436 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
438 SkPaint first_paint;
439 SetDiscardableShader(&first_paint);
441 SkPaint second_paint;
442 SetDiscardableShader(&second_paint);
444 SkPaint third_paint;
445 SetDiscardableShader(&third_paint);
447 SkPoint points[3];
448 SkColor colors[3];
449 uint16_t indecies[3] = {0, 1, 2};
450 points[0].set(10, 10);
451 points[1].set(100, 20);
452 points[2].set(50, 100);
453 // (10, 10, 90, 90).
454 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
456 points,
457 points,
458 colors,
459 NULL,
460 indecies,
462 first_paint);
464 canvas->save();
466 canvas->clipRect(SkRect::MakeWH(50, 50));
467 // (10, 10, 90, 90), since clips are ignored as long as the draw object is
468 // within clip.
469 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
471 points,
472 points,
473 colors,
474 NULL,
475 indecies,
477 second_paint);
479 canvas->restore();
481 points[0].set(50, 55);
482 points[1].set(50, 55);
483 points[2].set(200, 200);
484 // (50, 55, 150, 145).
485 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
487 points,
488 points,
489 colors,
490 NULL,
491 indecies,
493 third_paint);
495 skia::RefPtr<SkPicture> picture =
496 skia::AdoptRef(StopRecording(&recorder, canvas));
498 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
499 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
501 EXPECT_EQ(3u, pixel_refs.size());
502 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
503 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
504 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
505 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
506 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
507 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
508 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
509 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
510 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145),
511 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
512 VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__);
513 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
516 TEST(PixelRefUtilsTest, DrawImage) {
517 gfx::Rect layer_rect(0, 0, 256, 256);
519 SkPictureRecorder recorder;
520 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
522 skia::RefPtr<SkImage> first = CreateDiscardableImage(gfx::Size(50, 50));
523 skia::RefPtr<SkImage> second = CreateDiscardableImage(gfx::Size(50, 50));
524 skia::RefPtr<SkImage> third = CreateDiscardableImage(gfx::Size(50, 50));
525 skia::RefPtr<SkImage> fourth = CreateDiscardableImage(gfx::Size(50, 1));
526 skia::RefPtr<SkImage> fifth = CreateDiscardableImage(gfx::Size(10, 10));
527 skia::RefPtr<SkImage> sixth = CreateDiscardableImage(gfx::Size(10, 10));
529 canvas->save();
531 // At (0, 0).
532 canvas->drawImage(first.get(), 0, 0);
533 canvas->translate(25, 0);
534 // At (25, 0).
535 canvas->drawImage(second.get(), 0, 0);
536 canvas->translate(0, 50);
537 // At (50, 50).
538 canvas->drawImage(third.get(), 25, 0);
540 canvas->restore();
541 canvas->save();
543 canvas->translate(1, 0);
544 canvas->rotate(90);
545 // At (1, 0), rotated 90 degrees
546 canvas->drawImage(fourth.get(), 0, 0);
548 canvas->restore();
549 canvas->save();
551 canvas->scale(5.f, 6.f);
552 // At (0, 0), scaled by 5 and 6
553 canvas->drawImage(fifth.get(), 0, 0);
555 canvas->restore();
557 canvas->rotate(27);
558 canvas->scale(3.3f, 0.4f);
560 canvas->drawImage(sixth.get(), 0, 0);
562 canvas->restore();
564 skia::RefPtr<SkPicture> picture =
565 skia::AdoptRef(StopRecording(&recorder, canvas));
567 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
568 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
570 EXPECT_EQ(6u, pixel_refs.size());
571 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
572 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
573 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
574 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
575 EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50),
576 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
577 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
578 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
579 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
580 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
581 VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__);
582 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
583 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 1, 50),
584 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
585 VerifyScales(1.f, 1.f, pixel_refs[3].matrix, __LINE__);
586 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[3].filter_quality);
587 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 60),
588 gfx::SkRectToRectF(pixel_refs[4].pixel_ref_rect));
589 VerifyScales(5.f, 6.f, pixel_refs[4].matrix, __LINE__);
590 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[4].filter_quality);
591 EXPECT_FLOAT_RECT_EQ(gfx::RectF(-1.8159621f, 0, 31.219175f, 18.545712f),
592 gfx::SkRectToRectF(pixel_refs[5].pixel_ref_rect));
593 VerifyScales(3.3f, 0.4f, pixel_refs[5].matrix, __LINE__);
594 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[5].filter_quality);
597 TEST(PixelRefUtilsTest, DrawImageRect) {
598 gfx::Rect layer_rect(0, 0, 256, 256);
600 SkPictureRecorder recorder;
601 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
603 skia::RefPtr<SkImage> first = CreateDiscardableImage(gfx::Size(50, 50));
604 skia::RefPtr<SkImage> second = CreateDiscardableImage(gfx::Size(50, 50));
605 skia::RefPtr<SkImage> third = CreateDiscardableImage(gfx::Size(50, 50));
607 SkPaint first_paint;
608 SetDiscardableShader(&first_paint);
610 SkPaint non_discardable_paint;
612 canvas->save();
614 // (0, 0, 100, 100).
615 canvas->drawImageRect(
616 first.get(), SkRect::MakeWH(100, 100), &non_discardable_paint);
617 canvas->translate(25, 0);
618 // (75, 50, 10, 10).
619 canvas->drawImageRect(
620 second.get(), SkRect::MakeXYWH(50, 50, 10, 10), &non_discardable_paint);
621 canvas->translate(5, 50);
622 // (0, 30, 100, 100). One from bitmap, one from paint.
623 canvas->drawImageRect(
624 third.get(), SkRect::MakeXYWH(-30, -20, 100, 100), &first_paint);
626 canvas->restore();
628 skia::RefPtr<SkPicture> picture =
629 skia::AdoptRef(StopRecording(&recorder, canvas));
631 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
632 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
634 EXPECT_EQ(4u, pixel_refs.size());
635 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 100, 100),
636 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
637 VerifyScales(2.f, 2.f, pixel_refs[0].matrix, __LINE__);
638 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
639 EXPECT_FLOAT_RECT_EQ(gfx::RectF(75, 50, 10, 10),
640 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
641 VerifyScales(0.2f, 0.2f, pixel_refs[1].matrix, __LINE__);
642 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
643 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100),
644 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
645 VerifyScales(2.f, 2.f, pixel_refs[2].matrix, __LINE__);
646 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
647 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100),
648 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
649 VerifyScales(2.f, 2.f, pixel_refs[3].matrix, __LINE__);
650 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[3].filter_quality);
653 } // namespace skia