Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / skia / ext / pixel_ref_utils_unittest.cc
blob92c62d7c2b51e941451ee76319a775a35028d32a
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 virtual 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 virtual size_t contextSize() const OVERRIDE {
47 return sizeof(SkShader::Context);
50 virtual void flatten(SkWriteBuffer&) const OVERRIDE {}
52 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(TestDiscardableShader);
54 private:
55 SkBitmap bitmap_;
58 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
59 SkFlattenable* TestDiscardableShader::CreateProc(SkReadBuffer&) {
60 return new TestDiscardableShader;
62 #endif
64 void CreateBitmap(gfx::Size size, const char* uri, SkBitmap* bitmap) {
65 bitmap->allocN32Pixels(size.width(), size.height());
66 bitmap->pixelRef()->setImmutable();
67 bitmap->pixelRef()->setURI(uri);
70 SkCanvas* StartRecording(SkPictureRecorder* recorder, gfx::Rect layer_rect) {
71 SkCanvas* canvas =
72 recorder->beginRecording(layer_rect.width(), layer_rect.height());
74 canvas->save();
75 canvas->translate(-layer_rect.x(), -layer_rect.y());
76 canvas->clipRect(SkRect::MakeXYWH(
77 layer_rect.x(), layer_rect.y(), layer_rect.width(), layer_rect.height()));
79 return canvas;
82 SkPicture* StopRecording(SkPictureRecorder* recorder, SkCanvas* canvas) {
83 canvas->restore();
84 return recorder->endRecording();
87 } // namespace
89 TEST(PixelRefUtilsTest, DrawPaint) {
90 gfx::Rect layer_rect(0, 0, 256, 256);
92 SkPictureRecorder recorder;
93 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
95 TestDiscardableShader first_shader;
96 SkPaint first_paint;
97 first_paint.setShader(&first_shader);
99 TestDiscardableShader second_shader;
100 SkPaint second_paint;
101 second_paint.setShader(&second_shader);
103 TestDiscardableShader third_shader;
104 SkPaint third_paint;
105 third_paint.setShader(&third_shader);
107 canvas->drawPaint(first_paint);
108 canvas->clipRect(SkRect::MakeXYWH(34, 45, 56, 67));
109 canvas->drawPaint(second_paint);
110 // Total clip is now (34, 45, 56, 55)
111 canvas->clipRect(SkRect::MakeWH(100, 100));
112 canvas->drawPaint(third_paint);
114 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
116 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
117 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
119 EXPECT_EQ(3u, pixel_refs.size());
120 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 256, 256),
121 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
122 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 67),
123 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
124 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 55),
125 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
128 TEST(PixelRefUtilsTest, DrawPoints) {
129 gfx::Rect layer_rect(0, 0, 256, 256);
131 SkPictureRecorder recorder;
132 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
134 TestDiscardableShader first_shader;
135 SkPaint first_paint;
136 first_paint.setShader(&first_shader);
138 TestDiscardableShader second_shader;
139 SkPaint second_paint;
140 second_paint.setShader(&second_shader);
142 TestDiscardableShader third_shader;
143 SkPaint third_paint;
144 third_paint.setShader(&third_shader);
146 SkPoint points[3];
147 points[0].set(10, 10);
148 points[1].set(100, 20);
149 points[2].set(50, 100);
150 // (10, 10, 90, 90).
151 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, points, first_paint);
153 canvas->save();
155 canvas->clipRect(SkRect::MakeWH(50, 50));
156 // (10, 10, 40, 40).
157 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, points, second_paint);
159 canvas->restore();
161 points[0].set(50, 55);
162 points[1].set(50, 55);
163 points[2].set(200, 200);
164 // (50, 55, 150, 145).
165 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, points, third_paint);
167 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
169 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
170 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
172 EXPECT_EQ(3u, pixel_refs.size());
173 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
174 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
175 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 40, 40),
176 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
177 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145),
178 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
181 TEST(PixelRefUtilsTest, DrawRect) {
182 gfx::Rect layer_rect(0, 0, 256, 256);
184 SkPictureRecorder recorder;
185 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
187 TestDiscardableShader first_shader;
188 SkPaint first_paint;
189 first_paint.setShader(&first_shader);
191 TestDiscardableShader second_shader;
192 SkPaint second_paint;
193 second_paint.setShader(&second_shader);
195 TestDiscardableShader third_shader;
196 SkPaint third_paint;
197 third_paint.setShader(&third_shader);
199 // (10, 20, 30, 40).
200 canvas->drawRect(SkRect::MakeXYWH(10, 20, 30, 40), first_paint);
202 canvas->save();
204 canvas->translate(5, 17);
205 // (5, 50, 25, 35)
206 canvas->drawRect(SkRect::MakeXYWH(0, 33, 25, 35), second_paint);
208 canvas->restore();
210 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
211 canvas->translate(20, 20);
212 // (50, 50, 50, 50)
213 canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), third_paint);
215 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
217 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
218 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
220 EXPECT_EQ(3u, pixel_refs.size());
221 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 20, 30, 40),
222 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
223 EXPECT_FLOAT_RECT_EQ(gfx::RectF(5, 50, 25, 35),
224 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
225 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
226 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
229 TEST(PixelRefUtilsTest, DrawRRect) {
230 gfx::Rect layer_rect(0, 0, 256, 256);
232 SkPictureRecorder recorder;
233 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
235 TestDiscardableShader first_shader;
236 SkPaint first_paint;
237 first_paint.setShader(&first_shader);
239 TestDiscardableShader second_shader;
240 SkPaint second_paint;
241 second_paint.setShader(&second_shader);
243 TestDiscardableShader third_shader;
244 SkPaint third_paint;
245 third_paint.setShader(&third_shader);
247 SkRRect rrect;
248 rrect.setRect(SkRect::MakeXYWH(10, 20, 30, 40));
250 // (10, 20, 30, 40).
251 canvas->drawRRect(rrect, first_paint);
253 canvas->save();
255 canvas->translate(5, 17);
256 rrect.setRect(SkRect::MakeXYWH(0, 33, 25, 35));
257 // (5, 50, 25, 35)
258 canvas->drawRRect(rrect, second_paint);
260 canvas->restore();
262 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
263 canvas->translate(20, 20);
264 rrect.setRect(SkRect::MakeXYWH(0, 0, 100, 100));
265 // (50, 50, 50, 50)
266 canvas->drawRRect(rrect, third_paint);
268 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
270 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
271 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
273 EXPECT_EQ(3u, pixel_refs.size());
274 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 20, 30, 40),
275 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
276 EXPECT_FLOAT_RECT_EQ(gfx::RectF(5, 50, 25, 35),
277 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
278 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
279 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
282 TEST(PixelRefUtilsTest, DrawOval) {
283 gfx::Rect layer_rect(0, 0, 256, 256);
285 SkPictureRecorder recorder;
286 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
288 TestDiscardableShader first_shader;
289 SkPaint first_paint;
290 first_paint.setShader(&first_shader);
292 TestDiscardableShader second_shader;
293 SkPaint second_paint;
294 second_paint.setShader(&second_shader);
296 TestDiscardableShader third_shader;
297 SkPaint third_paint;
298 third_paint.setShader(&third_shader);
300 canvas->save();
302 canvas->scale(2, 0.5);
303 // (20, 10, 60, 20).
304 canvas->drawOval(SkRect::MakeXYWH(10, 20, 30, 40), first_paint);
306 canvas->restore();
307 canvas->save();
309 canvas->translate(1, 2);
310 // (1, 35, 25, 35)
311 canvas->drawRect(SkRect::MakeXYWH(0, 33, 25, 35), second_paint);
313 canvas->restore();
315 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
316 canvas->translate(20, 20);
317 // (50, 50, 50, 50)
318 canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), third_paint);
320 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
322 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
323 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
325 EXPECT_EQ(3u, pixel_refs.size());
326 EXPECT_FLOAT_RECT_EQ(gfx::RectF(20, 10, 60, 20),
327 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
328 EXPECT_FLOAT_RECT_EQ(gfx::RectF(1, 35, 25, 35),
329 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
330 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
331 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
334 TEST(PixelRefUtilsTest, DrawPath) {
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 SkPath path;
349 path.moveTo(12, 13);
350 path.lineTo(50, 50);
351 path.lineTo(22, 101);
353 // (12, 13, 38, 88).
354 canvas->drawPath(path, first_paint);
356 canvas->save();
357 canvas->clipRect(SkRect::MakeWH(50, 50));
359 // (12, 13, 38, 37).
360 canvas->drawPath(path, second_paint);
362 canvas->restore();
364 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
366 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
367 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
369 EXPECT_EQ(2u, pixel_refs.size());
370 EXPECT_FLOAT_RECT_EQ(gfx::RectF(12, 13, 38, 88),
371 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
372 EXPECT_FLOAT_RECT_EQ(gfx::RectF(12, 13, 38, 37),
373 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
376 TEST(PixelRefUtilsTest, DrawBitmap) {
377 gfx::Rect layer_rect(0, 0, 256, 256);
379 SkPictureRecorder recorder;
380 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
382 SkBitmap first;
383 CreateBitmap(gfx::Size(50, 50), "discardable", &first);
384 SkBitmap second;
385 CreateBitmap(gfx::Size(50, 50), "discardable", &second);
386 SkBitmap third;
387 CreateBitmap(gfx::Size(50, 50), "discardable", &third);
388 SkBitmap fourth;
389 CreateBitmap(gfx::Size(50, 1), "discardable", &fourth);
390 SkBitmap fifth;
391 CreateBitmap(gfx::Size(10, 10), "discardable", &fifth);
393 canvas->save();
395 // At (0, 0).
396 canvas->drawBitmap(first, 0, 0);
397 canvas->translate(25, 0);
398 // At (25, 0).
399 canvas->drawBitmap(second, 0, 0);
400 canvas->translate(0, 50);
401 // At (50, 50).
402 canvas->drawBitmap(third, 25, 0);
404 canvas->restore();
405 canvas->save();
407 canvas->translate(1, 0);
408 canvas->rotate(90);
409 // At (1, 0), rotated 90 degrees
410 canvas->drawBitmap(fourth, 0, 0);
412 canvas->restore();
414 canvas->scale(5, 6);
415 // At (0, 0), scaled by 5 and 6
416 canvas->drawBitmap(fifth, 0, 0);
418 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
420 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
421 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
423 EXPECT_EQ(5u, pixel_refs.size());
424 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
425 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
426 EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50),
427 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
428 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
429 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
430 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 1, 50),
431 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
432 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 60),
433 gfx::SkRectToRectF(pixel_refs[4].pixel_ref_rect));
437 TEST(PixelRefUtilsTest, DrawBitmapRect) {
438 gfx::Rect layer_rect(0, 0, 256, 256);
440 SkPictureRecorder recorder;
441 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
443 SkBitmap first;
444 CreateBitmap(gfx::Size(50, 50), "discardable", &first);
445 SkBitmap second;
446 CreateBitmap(gfx::Size(50, 50), "discardable", &second);
447 SkBitmap third;
448 CreateBitmap(gfx::Size(50, 50), "discardable", &third);
450 TestDiscardableShader first_shader;
451 SkPaint first_paint;
452 first_paint.setShader(&first_shader);
454 SkPaint non_discardable_paint;
456 canvas->save();
458 // (0, 0, 100, 100).
459 canvas->drawBitmapRect(
460 first, SkRect::MakeWH(100, 100), &non_discardable_paint);
461 canvas->translate(25, 0);
462 // (75, 50, 10, 10).
463 canvas->drawBitmapRect(
464 second, SkRect::MakeXYWH(50, 50, 10, 10), &non_discardable_paint);
465 canvas->translate(5, 50);
466 // (0, 30, 100, 100). One from bitmap, one from paint.
467 canvas->drawBitmapRect(
468 third, SkRect::MakeXYWH(-30, -20, 100, 100), &first_paint);
470 canvas->restore();
472 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
474 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
475 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
477 EXPECT_EQ(4u, pixel_refs.size());
478 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 100, 100),
479 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
480 EXPECT_FLOAT_RECT_EQ(gfx::RectF(75, 50, 10, 10),
481 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
482 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100),
483 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
484 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100),
485 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
488 TEST(PixelRefUtilsTest, DrawSprite) {
489 gfx::Rect layer_rect(0, 0, 256, 256);
491 SkPictureRecorder recorder;
492 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
494 SkBitmap first;
495 CreateBitmap(gfx::Size(50, 50), "discardable", &first);
496 SkBitmap second;
497 CreateBitmap(gfx::Size(50, 50), "discardable", &second);
498 SkBitmap third;
499 CreateBitmap(gfx::Size(50, 50), "discardable", &third);
500 SkBitmap fourth;
501 CreateBitmap(gfx::Size(50, 50), "discardable", &fourth);
502 SkBitmap fifth;
503 CreateBitmap(gfx::Size(50, 50), "discardable", &fifth);
505 canvas->save();
507 // Sprites aren't affected by the current matrix.
509 // (0, 0, 50, 50).
510 canvas->drawSprite(first, 0, 0);
511 canvas->translate(25, 0);
512 // (10, 0, 50, 50).
513 canvas->drawSprite(second, 10, 0);
514 canvas->translate(0, 50);
515 // (25, 0, 50, 50).
516 canvas->drawSprite(third, 25, 0);
518 canvas->restore();
519 canvas->save();
521 canvas->rotate(90);
522 // (0, 0, 50, 50).
523 canvas->drawSprite(fourth, 0, 0);
525 canvas->restore();
527 TestDiscardableShader first_shader;
528 SkPaint first_paint;
529 first_paint.setShader(&first_shader);
531 canvas->scale(5, 6);
532 // (100, 100, 50, 50).
533 canvas->drawSprite(fifth, 100, 100, &first_paint);
535 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
537 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
538 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
540 EXPECT_EQ(6u, pixel_refs.size());
541 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
542 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
543 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 0, 50, 50),
544 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
545 EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50),
546 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
547 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
548 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
549 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 50, 50),
550 gfx::SkRectToRectF(pixel_refs[4].pixel_ref_rect));
551 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 50, 50),
552 gfx::SkRectToRectF(pixel_refs[5].pixel_ref_rect));
555 TEST(PixelRefUtilsTest, DrawText) {
556 gfx::Rect layer_rect(0, 0, 256, 256);
558 SkPictureRecorder recorder;
559 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
561 TestDiscardableShader first_shader;
562 SkPaint first_paint;
563 first_paint.setShader(&first_shader);
565 SkPoint points[4];
566 points[0].set(10, 50);
567 points[1].set(20, 50);
568 points[2].set(30, 50);
569 points[3].set(40, 50);
571 SkPath path;
572 path.moveTo(10, 50);
573 path.lineTo(20, 50);
574 path.lineTo(30, 50);
575 path.lineTo(40, 50);
576 path.lineTo(50, 50);
578 canvas->drawText("text", 4, 50, 50, first_paint);
579 canvas->drawPosText("text", 4, points, first_paint);
580 canvas->drawTextOnPath("text", 4, path, NULL, first_paint);
582 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
584 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
585 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
587 EXPECT_EQ(3u, pixel_refs.size());
590 TEST(PixelRefUtilsTest, DrawVertices) {
591 gfx::Rect layer_rect(0, 0, 256, 256);
593 SkPictureRecorder recorder;
594 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
596 TestDiscardableShader first_shader;
597 SkPaint first_paint;
598 first_paint.setShader(&first_shader);
600 TestDiscardableShader second_shader;
601 SkPaint second_paint;
602 second_paint.setShader(&second_shader);
604 TestDiscardableShader third_shader;
605 SkPaint third_paint;
606 third_paint.setShader(&third_shader);
608 SkPoint points[3];
609 SkColor colors[3];
610 uint16_t indecies[3] = {0, 1, 2};
611 points[0].set(10, 10);
612 points[1].set(100, 20);
613 points[2].set(50, 100);
614 // (10, 10, 90, 90).
615 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
617 points,
618 points,
619 colors,
620 NULL,
621 indecies,
623 first_paint);
625 canvas->save();
627 canvas->clipRect(SkRect::MakeWH(50, 50));
628 // (10, 10, 40, 40).
629 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
631 points,
632 points,
633 colors,
634 NULL,
635 indecies,
637 second_paint);
639 canvas->restore();
641 points[0].set(50, 55);
642 points[1].set(50, 55);
643 points[2].set(200, 200);
644 // (50, 55, 150, 145).
645 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
647 points,
648 points,
649 colors,
650 NULL,
651 indecies,
653 third_paint);
655 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
657 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
658 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
660 EXPECT_EQ(3u, pixel_refs.size());
661 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
662 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
663 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 40, 40),
664 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
665 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145),
666 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
669 } // namespace skia