Update CrOS OOBE throbber to MD throbber; delete old asset
[chromium-blink-merge.git] / skia / ext / pixel_ref_utils_unittest.cc
blob50691f3dd0a4f6735b21c6eaab58d17df91a9003
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, 90, 90).
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, 90, 90),
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 // (20, 20, 100, 100)
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(20, 20, 100, 100),
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 // (20, 20, 100, 100)
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(20, 20, 100, 100),
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 // (20, 20, 100, 100).
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(20, 20, 100, 100),
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, 88), since clips are ignored as long as the shape is in the
418 // clip.
419 canvas->drawPath(path, second_paint);
421 canvas->restore();
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(2u, pixel_refs.size());
430 EXPECT_FLOAT_RECT_EQ(gfx::RectF(12, 13, 38, 88),
431 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
432 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
433 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
434 EXPECT_FLOAT_RECT_EQ(gfx::RectF(12, 13, 38, 88),
435 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
436 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
437 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
440 TEST(PixelRefUtilsTest, DrawBitmap) {
441 gfx::Rect layer_rect(0, 0, 256, 256);
443 SkPictureRecorder recorder;
444 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
446 SkBitmap first;
447 CreateBitmap(gfx::Size(50, 50), "discardable", &first);
448 SkBitmap second;
449 CreateBitmap(gfx::Size(50, 50), "discardable", &second);
450 SkBitmap third;
451 CreateBitmap(gfx::Size(50, 50), "discardable", &third);
452 SkBitmap fourth;
453 CreateBitmap(gfx::Size(50, 1), "discardable", &fourth);
454 SkBitmap fifth;
455 CreateBitmap(gfx::Size(10, 10), "discardable", &fifth);
456 SkBitmap sixth;
457 CreateBitmap(gfx::Size(10, 10), "discardable", &sixth);
459 canvas->save();
461 // At (0, 0).
462 canvas->drawBitmap(first, 0, 0);
463 canvas->translate(25, 0);
464 // At (25, 0).
465 canvas->drawBitmap(second, 0, 0);
466 canvas->translate(0, 50);
467 // At (50, 50).
468 canvas->drawBitmap(third, 25, 0);
470 canvas->restore();
471 canvas->save();
473 canvas->translate(1, 0);
474 canvas->rotate(90);
475 // At (1, 0), rotated 90 degrees
476 canvas->drawBitmap(fourth, 0, 0);
478 canvas->restore();
479 canvas->save();
481 canvas->scale(5.f, 6.f);
482 // At (0, 0), scaled by 5 and 6
483 canvas->drawBitmap(fifth, 0, 0);
485 canvas->restore();
487 canvas->rotate(27);
488 canvas->scale(3.3f, 0.4f);
490 canvas->drawBitmap(sixth, 0, 0);
492 canvas->restore();
494 skia::RefPtr<SkPicture> picture =
495 skia::AdoptRef(StopRecording(&recorder, canvas));
497 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
498 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
500 EXPECT_EQ(6u, pixel_refs.size());
501 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
502 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
503 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
504 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
505 EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50),
506 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
507 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
508 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
509 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
510 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
511 VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__);
512 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
513 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 1, 50),
514 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
515 VerifyScales(1.f, 1.f, pixel_refs[3].matrix, __LINE__);
516 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[3].filter_quality);
517 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 60),
518 gfx::SkRectToRectF(pixel_refs[4].pixel_ref_rect));
519 VerifyScales(5.f, 6.f, pixel_refs[4].matrix, __LINE__);
520 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[4].filter_quality);
521 EXPECT_FLOAT_RECT_EQ(gfx::RectF(-1.8159621f, 0, 31.219175f, 18.545712f),
522 gfx::SkRectToRectF(pixel_refs[5].pixel_ref_rect));
523 VerifyScales(3.3f, 0.4f, pixel_refs[5].matrix, __LINE__);
524 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[5].filter_quality);
527 TEST(PixelRefUtilsTest, DrawBitmapRect) {
528 gfx::Rect layer_rect(0, 0, 256, 256);
530 SkPictureRecorder recorder;
531 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
533 SkBitmap first;
534 CreateBitmap(gfx::Size(50, 50), "discardable", &first);
535 SkBitmap second;
536 CreateBitmap(gfx::Size(50, 50), "discardable", &second);
537 SkBitmap third;
538 CreateBitmap(gfx::Size(50, 50), "discardable", &third);
540 TestDiscardableShader first_shader;
541 SkPaint first_paint;
542 first_paint.setShader(&first_shader);
544 SkPaint non_discardable_paint;
546 canvas->save();
548 // (0, 0, 100, 100).
549 canvas->drawBitmapRect(
550 first, SkRect::MakeWH(100, 100), &non_discardable_paint);
551 canvas->translate(25, 0);
552 // (75, 50, 10, 10).
553 canvas->drawBitmapRect(
554 second, SkRect::MakeXYWH(50, 50, 10, 10), &non_discardable_paint);
555 canvas->translate(5, 50);
556 // (0, 30, 100, 100). One from bitmap, one from paint.
557 canvas->drawBitmapRect(
558 third, SkRect::MakeXYWH(-30, -20, 100, 100), &first_paint);
560 canvas->restore();
562 skia::RefPtr<SkPicture> picture =
563 skia::AdoptRef(StopRecording(&recorder, canvas));
565 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
566 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
568 EXPECT_EQ(4u, pixel_refs.size());
569 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 100, 100),
570 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
571 VerifyScales(2.f, 2.f, pixel_refs[0].matrix, __LINE__);
572 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
573 EXPECT_FLOAT_RECT_EQ(gfx::RectF(75, 50, 10, 10),
574 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
575 VerifyScales(0.2f, 0.2f, pixel_refs[1].matrix, __LINE__);
576 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
577 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100),
578 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
579 VerifyScales(2.f, 2.f, pixel_refs[2].matrix, __LINE__);
580 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
581 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100),
582 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
583 VerifyScales(2.f, 2.f, pixel_refs[3].matrix, __LINE__);
584 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[3].filter_quality);
587 TEST(PixelRefUtilsTest, DrawSprite) {
588 gfx::Rect layer_rect(0, 0, 256, 256);
590 SkPictureRecorder recorder;
591 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
593 SkBitmap first;
594 CreateBitmap(gfx::Size(50, 50), "discardable", &first);
595 SkBitmap second;
596 CreateBitmap(gfx::Size(50, 50), "discardable", &second);
597 SkBitmap third;
598 CreateBitmap(gfx::Size(50, 50), "discardable", &third);
599 SkBitmap fourth;
600 CreateBitmap(gfx::Size(50, 50), "discardable", &fourth);
601 SkBitmap fifth;
602 CreateBitmap(gfx::Size(50, 50), "discardable", &fifth);
604 canvas->save();
606 // Sprites aren't affected by the current matrix.
608 // (0, 0, 50, 50).
609 canvas->drawSprite(first, 0, 0);
610 canvas->translate(25, 0);
611 // (10, 0, 50, 50).
612 canvas->drawSprite(second, 10, 0);
613 canvas->translate(0, 50);
614 // (25, 0, 50, 50).
615 canvas->drawSprite(third, 25, 0);
617 canvas->restore();
618 canvas->save();
620 canvas->rotate(90);
621 // (0, 0, 50, 50).
622 canvas->drawSprite(fourth, 0, 0);
624 canvas->restore();
626 TestDiscardableShader first_shader;
627 SkPaint first_paint;
628 first_paint.setShader(&first_shader);
630 canvas->scale(5.f, 6.f);
631 // (100, 100, 50, 50).
632 canvas->drawSprite(fifth, 100, 100, &first_paint);
634 skia::RefPtr<SkPicture> picture =
635 skia::AdoptRef(StopRecording(&recorder, canvas));
637 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
638 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
640 EXPECT_EQ(6u, pixel_refs.size());
641 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
642 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
643 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
644 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
645 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 0, 50, 50),
646 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
647 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
648 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
649 EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50),
650 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
651 VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__);
652 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
653 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
654 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
655 VerifyScales(1.f, 1.f, pixel_refs[3].matrix, __LINE__);
656 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[3].filter_quality);
657 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 50, 50),
658 gfx::SkRectToRectF(pixel_refs[4].pixel_ref_rect));
659 VerifyScales(1.f, 1.f, pixel_refs[4].matrix, __LINE__);
660 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[4].filter_quality);
661 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 50, 50),
662 gfx::SkRectToRectF(pixel_refs[5].pixel_ref_rect));
663 VerifyScales(1.f, 1.f, pixel_refs[5].matrix, __LINE__);
664 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[5].filter_quality);
667 TEST(PixelRefUtilsTest, DrawText) {
668 gfx::Rect layer_rect(0, 0, 256, 256);
670 SkPictureRecorder recorder;
671 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
673 TestDiscardableShader first_shader;
674 SkPaint first_paint;
675 first_paint.setShader(&first_shader);
677 SkPoint points[4];
678 points[0].set(10, 50);
679 points[1].set(20, 50);
680 points[2].set(30, 50);
681 points[3].set(40, 50);
683 SkPath path;
684 path.moveTo(10, 50);
685 path.lineTo(20, 50);
686 path.lineTo(30, 50);
687 path.lineTo(40, 50);
688 path.lineTo(50, 50);
690 canvas->drawText("text", 4, 50, 50, first_paint);
691 canvas->drawPosText("text", 4, points, first_paint);
692 canvas->drawTextOnPath("text", 4, path, NULL, first_paint);
694 skia::RefPtr<SkPicture> picture =
695 skia::AdoptRef(StopRecording(&recorder, canvas));
697 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
698 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
700 EXPECT_EQ(3u, pixel_refs.size());
703 TEST(PixelRefUtilsTest, DrawVertices) {
704 gfx::Rect layer_rect(0, 0, 256, 256);
706 SkPictureRecorder recorder;
707 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
709 TestDiscardableShader first_shader;
710 SkPaint first_paint;
711 first_paint.setShader(&first_shader);
713 TestDiscardableShader second_shader;
714 SkPaint second_paint;
715 second_paint.setShader(&second_shader);
717 TestDiscardableShader third_shader;
718 SkPaint third_paint;
719 third_paint.setShader(&third_shader);
721 SkPoint points[3];
722 SkColor colors[3];
723 uint16_t indecies[3] = {0, 1, 2};
724 points[0].set(10, 10);
725 points[1].set(100, 20);
726 points[2].set(50, 100);
727 // (10, 10, 90, 90).
728 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
730 points,
731 points,
732 colors,
733 NULL,
734 indecies,
736 first_paint);
738 canvas->save();
740 canvas->clipRect(SkRect::MakeWH(50, 50));
741 // (10, 10, 90, 90), since clips are ignored as long as the draw object is
742 // within clip.
743 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
745 points,
746 points,
747 colors,
748 NULL,
749 indecies,
751 second_paint);
753 canvas->restore();
755 points[0].set(50, 55);
756 points[1].set(50, 55);
757 points[2].set(200, 200);
758 // (50, 55, 150, 145).
759 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
761 points,
762 points,
763 colors,
764 NULL,
765 indecies,
767 third_paint);
769 skia::RefPtr<SkPicture> picture =
770 skia::AdoptRef(StopRecording(&recorder, canvas));
772 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
773 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
775 EXPECT_EQ(3u, pixel_refs.size());
776 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
777 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
778 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
779 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
780 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
781 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
782 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
783 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
784 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145),
785 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
786 VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__);
787 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
790 } // namespace skia