gn vim: apply to *.gni also
[chromium-blink-merge.git] / skia / ext / pixel_ref_utils_unittest.cc
blob656ef5ce89248a593ccbf0acbcd08c33823f44bd
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/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 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(TestDiscardableShader);
52 private:
53 SkBitmap bitmap_;
56 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
57 SkFlattenable* TestDiscardableShader::CreateProc(SkReadBuffer&) {
58 return new TestDiscardableShader;
60 #endif
62 void CreateBitmap(gfx::Size size, const char* uri, SkBitmap* bitmap) {
63 bitmap->allocN32Pixels(size.width(), size.height());
64 bitmap->pixelRef()->setImmutable();
65 bitmap->pixelRef()->setURI(uri);
68 SkCanvas* StartRecording(SkPictureRecorder* recorder, gfx::Rect layer_rect) {
69 SkCanvas* canvas =
70 recorder->beginRecording(layer_rect.width(), layer_rect.height());
72 canvas->save();
73 canvas->translate(-layer_rect.x(), -layer_rect.y());
74 canvas->clipRect(SkRect::MakeXYWH(
75 layer_rect.x(), layer_rect.y(), layer_rect.width(), layer_rect.height()));
77 return canvas;
80 SkPicture* StopRecording(SkPictureRecorder* recorder, SkCanvas* canvas) {
81 canvas->restore();
82 return recorder->endRecording();
85 } // namespace
87 TEST(PixelRefUtilsTest, DrawPaint) {
88 gfx::Rect layer_rect(0, 0, 256, 256);
90 SkPictureRecorder recorder;
91 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
93 TestDiscardableShader first_shader;
94 SkPaint first_paint;
95 first_paint.setShader(&first_shader);
97 TestDiscardableShader second_shader;
98 SkPaint second_paint;
99 second_paint.setShader(&second_shader);
101 TestDiscardableShader third_shader;
102 SkPaint third_paint;
103 third_paint.setShader(&third_shader);
105 canvas->drawPaint(first_paint);
106 canvas->clipRect(SkRect::MakeXYWH(34, 45, 56, 67));
107 canvas->drawPaint(second_paint);
108 // Total clip is now (34, 45, 56, 55)
109 canvas->clipRect(SkRect::MakeWH(100, 100));
110 canvas->drawPaint(third_paint);
112 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
114 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
115 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
117 EXPECT_EQ(3u, pixel_refs.size());
118 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 256, 256),
119 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
120 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 67),
121 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
122 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 55),
123 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
126 TEST(PixelRefUtilsTest, DrawPoints) {
127 gfx::Rect layer_rect(0, 0, 256, 256);
129 SkPictureRecorder recorder;
130 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
132 TestDiscardableShader first_shader;
133 SkPaint first_paint;
134 first_paint.setShader(&first_shader);
136 TestDiscardableShader second_shader;
137 SkPaint second_paint;
138 second_paint.setShader(&second_shader);
140 TestDiscardableShader third_shader;
141 SkPaint third_paint;
142 third_paint.setShader(&third_shader);
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, 40, 40).
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 = skia::AdoptRef(StopRecording(&recorder, canvas));
167 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
168 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
170 EXPECT_EQ(3u, pixel_refs.size());
171 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
172 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
173 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 40, 40),
174 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
175 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145),
176 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
179 TEST(PixelRefUtilsTest, DrawRect) {
180 gfx::Rect layer_rect(0, 0, 256, 256);
182 SkPictureRecorder recorder;
183 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
185 TestDiscardableShader first_shader;
186 SkPaint first_paint;
187 first_paint.setShader(&first_shader);
189 TestDiscardableShader second_shader;
190 SkPaint second_paint;
191 second_paint.setShader(&second_shader);
193 TestDiscardableShader third_shader;
194 SkPaint third_paint;
195 third_paint.setShader(&third_shader);
197 // (10, 20, 30, 40).
198 canvas->drawRect(SkRect::MakeXYWH(10, 20, 30, 40), first_paint);
200 canvas->save();
202 canvas->translate(5, 17);
203 // (5, 50, 25, 35)
204 canvas->drawRect(SkRect::MakeXYWH(0, 33, 25, 35), second_paint);
206 canvas->restore();
208 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
209 canvas->translate(20, 20);
210 // (50, 50, 50, 50)
211 canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), third_paint);
213 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
215 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
216 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
218 EXPECT_EQ(3u, pixel_refs.size());
219 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 20, 30, 40),
220 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
221 EXPECT_FLOAT_RECT_EQ(gfx::RectF(5, 50, 25, 35),
222 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
223 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
224 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
227 TEST(PixelRefUtilsTest, DrawRRect) {
228 gfx::Rect layer_rect(0, 0, 256, 256);
230 SkPictureRecorder recorder;
231 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
233 TestDiscardableShader first_shader;
234 SkPaint first_paint;
235 first_paint.setShader(&first_shader);
237 TestDiscardableShader second_shader;
238 SkPaint second_paint;
239 second_paint.setShader(&second_shader);
241 TestDiscardableShader third_shader;
242 SkPaint third_paint;
243 third_paint.setShader(&third_shader);
245 SkRRect rrect;
246 rrect.setRect(SkRect::MakeXYWH(10, 20, 30, 40));
248 // (10, 20, 30, 40).
249 canvas->drawRRect(rrect, first_paint);
251 canvas->save();
253 canvas->translate(5, 17);
254 rrect.setRect(SkRect::MakeXYWH(0, 33, 25, 35));
255 // (5, 50, 25, 35)
256 canvas->drawRRect(rrect, second_paint);
258 canvas->restore();
260 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
261 canvas->translate(20, 20);
262 rrect.setRect(SkRect::MakeXYWH(0, 0, 100, 100));
263 // (50, 50, 50, 50)
264 canvas->drawRRect(rrect, third_paint);
266 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
268 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
269 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
271 EXPECT_EQ(3u, pixel_refs.size());
272 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 20, 30, 40),
273 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
274 EXPECT_FLOAT_RECT_EQ(gfx::RectF(5, 50, 25, 35),
275 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
276 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
277 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
280 TEST(PixelRefUtilsTest, DrawOval) {
281 gfx::Rect layer_rect(0, 0, 256, 256);
283 SkPictureRecorder recorder;
284 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
286 TestDiscardableShader first_shader;
287 SkPaint first_paint;
288 first_paint.setShader(&first_shader);
290 TestDiscardableShader second_shader;
291 SkPaint second_paint;
292 second_paint.setShader(&second_shader);
294 TestDiscardableShader third_shader;
295 SkPaint third_paint;
296 third_paint.setShader(&third_shader);
298 canvas->save();
300 canvas->scale(2, 0.5);
301 // (20, 10, 60, 20).
302 canvas->drawOval(SkRect::MakeXYWH(10, 20, 30, 40), first_paint);
304 canvas->restore();
305 canvas->save();
307 canvas->translate(1, 2);
308 // (1, 35, 25, 35)
309 canvas->drawRect(SkRect::MakeXYWH(0, 33, 25, 35), second_paint);
311 canvas->restore();
313 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
314 canvas->translate(20, 20);
315 // (50, 50, 50, 50)
316 canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), third_paint);
318 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
320 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
321 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
323 EXPECT_EQ(3u, pixel_refs.size());
324 EXPECT_FLOAT_RECT_EQ(gfx::RectF(20, 10, 60, 20),
325 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
326 EXPECT_FLOAT_RECT_EQ(gfx::RectF(1, 35, 25, 35),
327 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
328 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
329 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
332 TEST(PixelRefUtilsTest, DrawPath) {
333 gfx::Rect layer_rect(0, 0, 256, 256);
335 SkPictureRecorder recorder;
336 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
338 TestDiscardableShader first_shader;
339 SkPaint first_paint;
340 first_paint.setShader(&first_shader);
342 TestDiscardableShader second_shader;
343 SkPaint second_paint;
344 second_paint.setShader(&second_shader);
346 SkPath path;
347 path.moveTo(12, 13);
348 path.lineTo(50, 50);
349 path.lineTo(22, 101);
351 // (12, 13, 38, 88).
352 canvas->drawPath(path, first_paint);
354 canvas->save();
355 canvas->clipRect(SkRect::MakeWH(50, 50));
357 // (12, 13, 38, 37).
358 canvas->drawPath(path, second_paint);
360 canvas->restore();
362 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
364 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
365 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
367 EXPECT_EQ(2u, pixel_refs.size());
368 EXPECT_FLOAT_RECT_EQ(gfx::RectF(12, 13, 38, 88),
369 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
370 EXPECT_FLOAT_RECT_EQ(gfx::RectF(12, 13, 38, 37),
371 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
374 TEST(PixelRefUtilsTest, DrawBitmap) {
375 gfx::Rect layer_rect(0, 0, 256, 256);
377 SkPictureRecorder recorder;
378 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
380 SkBitmap first;
381 CreateBitmap(gfx::Size(50, 50), "discardable", &first);
382 SkBitmap second;
383 CreateBitmap(gfx::Size(50, 50), "discardable", &second);
384 SkBitmap third;
385 CreateBitmap(gfx::Size(50, 50), "discardable", &third);
386 SkBitmap fourth;
387 CreateBitmap(gfx::Size(50, 1), "discardable", &fourth);
388 SkBitmap fifth;
389 CreateBitmap(gfx::Size(10, 10), "discardable", &fifth);
391 canvas->save();
393 // At (0, 0).
394 canvas->drawBitmap(first, 0, 0);
395 canvas->translate(25, 0);
396 // At (25, 0).
397 canvas->drawBitmap(second, 0, 0);
398 canvas->translate(0, 50);
399 // At (50, 50).
400 canvas->drawBitmap(third, 25, 0);
402 canvas->restore();
403 canvas->save();
405 canvas->translate(1, 0);
406 canvas->rotate(90);
407 // At (1, 0), rotated 90 degrees
408 canvas->drawBitmap(fourth, 0, 0);
410 canvas->restore();
412 canvas->scale(5, 6);
413 // At (0, 0), scaled by 5 and 6
414 canvas->drawBitmap(fifth, 0, 0);
416 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
418 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
419 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
421 EXPECT_EQ(5u, pixel_refs.size());
422 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
423 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
424 EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50),
425 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
426 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
427 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
428 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 1, 50),
429 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
430 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 60),
431 gfx::SkRectToRectF(pixel_refs[4].pixel_ref_rect));
435 TEST(PixelRefUtilsTest, DrawBitmapRect) {
436 gfx::Rect layer_rect(0, 0, 256, 256);
438 SkPictureRecorder recorder;
439 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
441 SkBitmap first;
442 CreateBitmap(gfx::Size(50, 50), "discardable", &first);
443 SkBitmap second;
444 CreateBitmap(gfx::Size(50, 50), "discardable", &second);
445 SkBitmap third;
446 CreateBitmap(gfx::Size(50, 50), "discardable", &third);
448 TestDiscardableShader first_shader;
449 SkPaint first_paint;
450 first_paint.setShader(&first_shader);
452 SkPaint non_discardable_paint;
454 canvas->save();
456 // (0, 0, 100, 100).
457 canvas->drawBitmapRect(
458 first, SkRect::MakeWH(100, 100), &non_discardable_paint);
459 canvas->translate(25, 0);
460 // (75, 50, 10, 10).
461 canvas->drawBitmapRect(
462 second, SkRect::MakeXYWH(50, 50, 10, 10), &non_discardable_paint);
463 canvas->translate(5, 50);
464 // (0, 30, 100, 100). One from bitmap, one from paint.
465 canvas->drawBitmapRect(
466 third, SkRect::MakeXYWH(-30, -20, 100, 100), &first_paint);
468 canvas->restore();
470 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
472 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
473 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
475 EXPECT_EQ(4u, pixel_refs.size());
476 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 100, 100),
477 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
478 EXPECT_FLOAT_RECT_EQ(gfx::RectF(75, 50, 10, 10),
479 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
480 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100),
481 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
482 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100),
483 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
486 TEST(PixelRefUtilsTest, DrawSprite) {
487 gfx::Rect layer_rect(0, 0, 256, 256);
489 SkPictureRecorder recorder;
490 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
492 SkBitmap first;
493 CreateBitmap(gfx::Size(50, 50), "discardable", &first);
494 SkBitmap second;
495 CreateBitmap(gfx::Size(50, 50), "discardable", &second);
496 SkBitmap third;
497 CreateBitmap(gfx::Size(50, 50), "discardable", &third);
498 SkBitmap fourth;
499 CreateBitmap(gfx::Size(50, 50), "discardable", &fourth);
500 SkBitmap fifth;
501 CreateBitmap(gfx::Size(50, 50), "discardable", &fifth);
503 canvas->save();
505 // Sprites aren't affected by the current matrix.
507 // (0, 0, 50, 50).
508 canvas->drawSprite(first, 0, 0);
509 canvas->translate(25, 0);
510 // (10, 0, 50, 50).
511 canvas->drawSprite(second, 10, 0);
512 canvas->translate(0, 50);
513 // (25, 0, 50, 50).
514 canvas->drawSprite(third, 25, 0);
516 canvas->restore();
517 canvas->save();
519 canvas->rotate(90);
520 // (0, 0, 50, 50).
521 canvas->drawSprite(fourth, 0, 0);
523 canvas->restore();
525 TestDiscardableShader first_shader;
526 SkPaint first_paint;
527 first_paint.setShader(&first_shader);
529 canvas->scale(5, 6);
530 // (100, 100, 50, 50).
531 canvas->drawSprite(fifth, 100, 100, &first_paint);
533 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
535 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
536 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
538 EXPECT_EQ(6u, pixel_refs.size());
539 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
540 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
541 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 0, 50, 50),
542 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
543 EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50),
544 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
545 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
546 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
547 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 50, 50),
548 gfx::SkRectToRectF(pixel_refs[4].pixel_ref_rect));
549 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 50, 50),
550 gfx::SkRectToRectF(pixel_refs[5].pixel_ref_rect));
553 TEST(PixelRefUtilsTest, DrawText) {
554 gfx::Rect layer_rect(0, 0, 256, 256);
556 SkPictureRecorder recorder;
557 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
559 TestDiscardableShader first_shader;
560 SkPaint first_paint;
561 first_paint.setShader(&first_shader);
563 SkPoint points[4];
564 points[0].set(10, 50);
565 points[1].set(20, 50);
566 points[2].set(30, 50);
567 points[3].set(40, 50);
569 SkPath path;
570 path.moveTo(10, 50);
571 path.lineTo(20, 50);
572 path.lineTo(30, 50);
573 path.lineTo(40, 50);
574 path.lineTo(50, 50);
576 canvas->drawText("text", 4, 50, 50, first_paint);
577 canvas->drawPosText("text", 4, points, first_paint);
578 canvas->drawTextOnPath("text", 4, path, NULL, first_paint);
580 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
582 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
583 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
585 EXPECT_EQ(3u, pixel_refs.size());
588 TEST(PixelRefUtilsTest, DrawVertices) {
589 gfx::Rect layer_rect(0, 0, 256, 256);
591 SkPictureRecorder recorder;
592 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
594 TestDiscardableShader first_shader;
595 SkPaint first_paint;
596 first_paint.setShader(&first_shader);
598 TestDiscardableShader second_shader;
599 SkPaint second_paint;
600 second_paint.setShader(&second_shader);
602 TestDiscardableShader third_shader;
603 SkPaint third_paint;
604 third_paint.setShader(&third_shader);
606 SkPoint points[3];
607 SkColor colors[3];
608 uint16_t indecies[3] = {0, 1, 2};
609 points[0].set(10, 10);
610 points[1].set(100, 20);
611 points[2].set(50, 100);
612 // (10, 10, 90, 90).
613 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
615 points,
616 points,
617 colors,
618 NULL,
619 indecies,
621 first_paint);
623 canvas->save();
625 canvas->clipRect(SkRect::MakeWH(50, 50));
626 // (10, 10, 40, 40).
627 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
629 points,
630 points,
631 colors,
632 NULL,
633 indecies,
635 second_paint);
637 canvas->restore();
639 points[0].set(50, 55);
640 points[1].set(50, 55);
641 points[2].set(200, 200);
642 // (50, 55, 150, 145).
643 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
645 points,
646 points,
647 colors,
648 NULL,
649 indecies,
651 third_paint);
653 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
655 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
656 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
658 EXPECT_EQ(3u, pixel_refs.size());
659 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
660 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
661 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 40, 40),
662 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
663 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145),
664 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
667 } // namespace skia