Move remaining resources from java_staging/res to java/res.
[chromium-blink-merge.git] / skia / ext / pixel_ref_utils_unittest.cc
blob6ab2f62902e688d16ab65017112df9f1eb017dad
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&);
55 friend class SkPrivateEffectInitializer;
57 public:
58 Factory getFactory() const override { return CreateProc; }
60 private:
61 SkBitmap bitmap_;
64 SkFlattenable* TestDiscardableShader::CreateProc(SkReadBuffer&) {
65 return new TestDiscardableShader;
68 void CreateBitmap(gfx::Size size, const char* uri, SkBitmap* bitmap) {
69 bitmap->allocN32Pixels(size.width(), size.height());
70 bitmap->pixelRef()->setImmutable();
71 bitmap->pixelRef()->setURI(uri);
74 SkCanvas* StartRecording(SkPictureRecorder* recorder, gfx::Rect layer_rect) {
75 SkCanvas* canvas =
76 recorder->beginRecording(layer_rect.width(), layer_rect.height());
78 canvas->save();
79 canvas->translate(-layer_rect.x(), -layer_rect.y());
80 canvas->clipRect(SkRect::MakeXYWH(
81 layer_rect.x(), layer_rect.y(), layer_rect.width(), layer_rect.height()));
83 return canvas;
86 SkPicture* StopRecording(SkPictureRecorder* recorder, SkCanvas* canvas) {
87 canvas->restore();
88 return recorder->endRecordingAsPicture();
91 } // namespace
93 void VerifyScales(SkScalar x_scale,
94 SkScalar y_scale,
95 const SkMatrix& matrix,
96 int source_line) {
97 SkSize scales;
98 bool success = matrix.decomposeScale(&scales);
99 EXPECT_TRUE(success) << "line: " << source_line;
100 EXPECT_FLOAT_EQ(x_scale, scales.width()) << "line: " << source_line;
101 EXPECT_FLOAT_EQ(y_scale, scales.height()) << "line: " << source_line;
104 TEST(PixelRefUtilsTest, DrawPaint) {
105 gfx::Rect layer_rect(0, 0, 256, 256);
107 SkPictureRecorder recorder;
108 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
110 TestDiscardableShader first_shader;
111 SkPaint first_paint;
112 first_paint.setShader(&first_shader);
114 TestDiscardableShader second_shader;
115 SkPaint second_paint;
116 second_paint.setShader(&second_shader);
118 TestDiscardableShader third_shader;
119 SkPaint third_paint;
120 third_paint.setShader(&third_shader);
122 canvas->drawPaint(first_paint);
123 canvas->clipRect(SkRect::MakeXYWH(34, 45, 56, 67));
124 canvas->drawPaint(second_paint);
126 canvas->save();
127 canvas->scale(2.f, 3.f);
128 canvas->drawPaint(second_paint);
129 canvas->restore();
131 // Total clip is now (34, 45, 56, 55)
132 canvas->clipRect(SkRect::MakeWH(100, 100));
133 canvas->drawPaint(third_paint);
135 skia::RefPtr<SkPicture> picture =
136 skia::AdoptRef(StopRecording(&recorder, canvas));
138 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
139 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
141 EXPECT_EQ(4u, pixel_refs.size());
142 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 256, 256),
143 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
144 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
145 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
146 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 67),
147 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
148 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
149 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 67),
150 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
151 VerifyScales(2.f, 3.f, pixel_refs[2].matrix, __LINE__);
152 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
153 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 55),
154 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
155 VerifyScales(1.f, 1.f, pixel_refs[3].matrix, __LINE__);
156 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[3].filter_quality);
159 TEST(PixelRefUtilsTest, DrawPoints) {
160 gfx::Rect layer_rect(0, 0, 256, 256);
162 SkPictureRecorder recorder;
163 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
165 TestDiscardableShader first_shader;
166 SkPaint first_paint;
167 first_paint.setShader(&first_shader);
169 TestDiscardableShader second_shader;
170 SkPaint second_paint;
171 second_paint.setShader(&second_shader);
173 TestDiscardableShader third_shader;
174 SkPaint third_paint;
175 third_paint.setShader(&third_shader);
177 SkPoint points[3];
178 points[0].set(10, 10);
179 points[1].set(100, 20);
180 points[2].set(50, 100);
181 // (10, 10, 90, 90).
182 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, points, first_paint);
184 canvas->save();
186 canvas->clipRect(SkRect::MakeWH(50, 50));
187 // (10, 10, 40, 40).
188 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, points, second_paint);
190 canvas->restore();
192 points[0].set(50, 55);
193 points[1].set(50, 55);
194 points[2].set(200, 200);
195 // (50, 55, 150, 145).
196 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, points, third_paint);
198 skia::RefPtr<SkPicture> picture =
199 skia::AdoptRef(StopRecording(&recorder, canvas));
201 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
202 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
204 EXPECT_EQ(3u, pixel_refs.size());
205 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
206 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
207 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
208 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
209 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 40, 40),
210 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
211 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
212 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
213 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145),
214 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
215 VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__);
216 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
219 TEST(PixelRefUtilsTest, DrawRect) {
220 gfx::Rect layer_rect(0, 0, 256, 256);
222 SkPictureRecorder recorder;
223 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
225 TestDiscardableShader first_shader;
226 SkPaint first_paint;
227 first_paint.setShader(&first_shader);
229 TestDiscardableShader second_shader;
230 SkPaint second_paint;
231 second_paint.setShader(&second_shader);
233 TestDiscardableShader third_shader;
234 SkPaint third_paint;
235 third_paint.setShader(&third_shader);
237 // (10, 20, 30, 40).
238 canvas->drawRect(SkRect::MakeXYWH(10, 20, 30, 40), first_paint);
240 canvas->save();
242 canvas->translate(5, 17);
243 // (5, 50, 25, 35)
244 canvas->drawRect(SkRect::MakeXYWH(0, 33, 25, 35), second_paint);
246 canvas->restore();
248 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
249 canvas->translate(20, 20);
250 // (50, 50, 50, 50)
251 canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), third_paint);
253 skia::RefPtr<SkPicture> picture =
254 skia::AdoptRef(StopRecording(&recorder, canvas));
256 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
257 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
259 EXPECT_EQ(3u, pixel_refs.size());
260 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 20, 30, 40),
261 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
262 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
263 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
264 EXPECT_FLOAT_RECT_EQ(gfx::RectF(5, 50, 25, 35),
265 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
266 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
267 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
268 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
269 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
270 VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__);
271 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
274 TEST(PixelRefUtilsTest, DrawRRect) {
275 gfx::Rect layer_rect(0, 0, 256, 256);
277 SkPictureRecorder recorder;
278 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
280 TestDiscardableShader first_shader;
281 SkPaint first_paint;
282 first_paint.setShader(&first_shader);
284 TestDiscardableShader second_shader;
285 SkPaint second_paint;
286 second_paint.setShader(&second_shader);
288 TestDiscardableShader third_shader;
289 SkPaint third_paint;
290 third_paint.setShader(&third_shader);
292 SkRRect rrect;
293 rrect.setRect(SkRect::MakeXYWH(10, 20, 30, 40));
295 // (10, 20, 30, 40).
296 canvas->drawRRect(rrect, first_paint);
298 canvas->save();
300 canvas->translate(5, 17);
301 rrect.setRect(SkRect::MakeXYWH(0, 33, 25, 35));
302 // (5, 50, 25, 35)
303 canvas->drawRRect(rrect, second_paint);
305 canvas->restore();
307 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
308 canvas->translate(20, 20);
309 rrect.setRect(SkRect::MakeXYWH(0, 0, 100, 100));
310 // (50, 50, 50, 50)
311 canvas->drawRRect(rrect, third_paint);
313 skia::RefPtr<SkPicture> picture =
314 skia::AdoptRef(StopRecording(&recorder, canvas));
316 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
317 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
319 EXPECT_EQ(3u, pixel_refs.size());
320 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 20, 30, 40),
321 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
322 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
323 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
324 EXPECT_FLOAT_RECT_EQ(gfx::RectF(5, 50, 25, 35),
325 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
326 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
327 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
328 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
329 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
330 VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__);
331 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
334 TEST(PixelRefUtilsTest, DrawOval) {
335 gfx::Rect layer_rect(0, 0, 256, 256);
337 SkPictureRecorder recorder;
338 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
340 TestDiscardableShader first_shader;
341 SkPaint first_paint;
342 first_paint.setShader(&first_shader);
344 TestDiscardableShader second_shader;
345 SkPaint second_paint;
346 second_paint.setShader(&second_shader);
348 TestDiscardableShader third_shader;
349 SkPaint third_paint;
350 third_paint.setShader(&third_shader);
352 canvas->save();
354 canvas->scale(2.f, 0.5f);
355 // (20, 10, 60, 20).
356 canvas->drawOval(SkRect::MakeXYWH(10, 20, 30, 40), first_paint);
358 canvas->restore();
359 canvas->save();
361 canvas->translate(1, 2);
362 // (1, 35, 25, 35)
363 canvas->drawRect(SkRect::MakeXYWH(0, 33, 25, 35), second_paint);
365 canvas->restore();
367 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
368 canvas->translate(20, 20);
369 // (50, 50, 50, 50)
370 canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), third_paint);
372 skia::RefPtr<SkPicture> picture =
373 skia::AdoptRef(StopRecording(&recorder, canvas));
375 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
376 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
378 EXPECT_EQ(3u, pixel_refs.size());
379 EXPECT_FLOAT_RECT_EQ(gfx::RectF(20, 10, 60, 20),
380 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
381 VerifyScales(2.f, 0.5f, pixel_refs[0].matrix, __LINE__);
382 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
383 EXPECT_FLOAT_RECT_EQ(gfx::RectF(1, 35, 25, 35),
384 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
385 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
386 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
387 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
388 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
389 VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__);
390 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
393 TEST(PixelRefUtilsTest, DrawPath) {
394 gfx::Rect layer_rect(0, 0, 256, 256);
396 SkPictureRecorder recorder;
397 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
399 TestDiscardableShader first_shader;
400 SkPaint first_paint;
401 first_paint.setShader(&first_shader);
403 TestDiscardableShader second_shader;
404 SkPaint second_paint;
405 second_paint.setShader(&second_shader);
407 SkPath path;
408 path.moveTo(12, 13);
409 path.lineTo(50, 50);
410 path.lineTo(22, 101);
412 // (12, 13, 38, 88).
413 canvas->drawPath(path, first_paint);
415 canvas->save();
416 canvas->clipRect(SkRect::MakeWH(50, 50));
418 // (12, 13, 38, 37).
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, 37),
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(0, 0, 29.403214f, 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, 40, 40).
742 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
744 points,
745 points,
746 colors,
747 NULL,
748 indecies,
750 second_paint);
752 canvas->restore();
754 points[0].set(50, 55);
755 points[1].set(50, 55);
756 points[2].set(200, 200);
757 // (50, 55, 150, 145).
758 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
760 points,
761 points,
762 colors,
763 NULL,
764 indecies,
766 third_paint);
768 skia::RefPtr<SkPicture> picture =
769 skia::AdoptRef(StopRecording(&recorder, canvas));
771 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
772 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
774 EXPECT_EQ(3u, pixel_refs.size());
775 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
776 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
777 VerifyScales(1.f, 1.f, pixel_refs[0].matrix, __LINE__);
778 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[0].filter_quality);
779 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 40, 40),
780 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
781 VerifyScales(1.f, 1.f, pixel_refs[1].matrix, __LINE__);
782 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[1].filter_quality);
783 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145),
784 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
785 VerifyScales(1.f, 1.f, pixel_refs[2].matrix, __LINE__);
786 EXPECT_EQ(kNone_SkFilterQuality, pixel_refs[2].filter_quality);
789 } // namespace skia