Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / skia / ext / pixel_ref_utils_unittest.cc
blob1e8f6ee2a0289b683cee16afcf1a12ae5bbfebb6
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 TEST(PixelRefUtilsTest, DrawPaint) {
94 gfx::Rect layer_rect(0, 0, 256, 256);
96 SkPictureRecorder recorder;
97 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
99 TestDiscardableShader first_shader;
100 SkPaint first_paint;
101 first_paint.setShader(&first_shader);
103 TestDiscardableShader second_shader;
104 SkPaint second_paint;
105 second_paint.setShader(&second_shader);
107 TestDiscardableShader third_shader;
108 SkPaint third_paint;
109 third_paint.setShader(&third_shader);
111 canvas->drawPaint(first_paint);
112 canvas->clipRect(SkRect::MakeXYWH(34, 45, 56, 67));
113 canvas->drawPaint(second_paint);
114 // Total clip is now (34, 45, 56, 55)
115 canvas->clipRect(SkRect::MakeWH(100, 100));
116 canvas->drawPaint(third_paint);
118 skia::RefPtr<SkPicture> picture =
119 skia::AdoptRef(StopRecording(&recorder, canvas));
121 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
122 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
124 EXPECT_EQ(3u, pixel_refs.size());
125 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 256, 256),
126 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
127 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 67),
128 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
129 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 55),
130 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
133 TEST(PixelRefUtilsTest, DrawPoints) {
134 gfx::Rect layer_rect(0, 0, 256, 256);
136 SkPictureRecorder recorder;
137 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
139 TestDiscardableShader first_shader;
140 SkPaint first_paint;
141 first_paint.setShader(&first_shader);
143 TestDiscardableShader second_shader;
144 SkPaint second_paint;
145 second_paint.setShader(&second_shader);
147 TestDiscardableShader third_shader;
148 SkPaint third_paint;
149 third_paint.setShader(&third_shader);
151 SkPoint points[3];
152 points[0].set(10, 10);
153 points[1].set(100, 20);
154 points[2].set(50, 100);
155 // (10, 10, 90, 90).
156 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, points, first_paint);
158 canvas->save();
160 canvas->clipRect(SkRect::MakeWH(50, 50));
161 // (10, 10, 40, 40).
162 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, points, second_paint);
164 canvas->restore();
166 points[0].set(50, 55);
167 points[1].set(50, 55);
168 points[2].set(200, 200);
169 // (50, 55, 150, 145).
170 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, points, third_paint);
172 skia::RefPtr<SkPicture> picture =
173 skia::AdoptRef(StopRecording(&recorder, canvas));
175 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
176 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
178 EXPECT_EQ(3u, pixel_refs.size());
179 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
180 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
181 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 40, 40),
182 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
183 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145),
184 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
187 TEST(PixelRefUtilsTest, DrawRect) {
188 gfx::Rect layer_rect(0, 0, 256, 256);
190 SkPictureRecorder recorder;
191 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
193 TestDiscardableShader first_shader;
194 SkPaint first_paint;
195 first_paint.setShader(&first_shader);
197 TestDiscardableShader second_shader;
198 SkPaint second_paint;
199 second_paint.setShader(&second_shader);
201 TestDiscardableShader third_shader;
202 SkPaint third_paint;
203 third_paint.setShader(&third_shader);
205 // (10, 20, 30, 40).
206 canvas->drawRect(SkRect::MakeXYWH(10, 20, 30, 40), first_paint);
208 canvas->save();
210 canvas->translate(5, 17);
211 // (5, 50, 25, 35)
212 canvas->drawRect(SkRect::MakeXYWH(0, 33, 25, 35), second_paint);
214 canvas->restore();
216 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
217 canvas->translate(20, 20);
218 // (50, 50, 50, 50)
219 canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), third_paint);
221 skia::RefPtr<SkPicture> picture =
222 skia::AdoptRef(StopRecording(&recorder, canvas));
224 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
225 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
227 EXPECT_EQ(3u, pixel_refs.size());
228 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 20, 30, 40),
229 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
230 EXPECT_FLOAT_RECT_EQ(gfx::RectF(5, 50, 25, 35),
231 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
232 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
233 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
236 TEST(PixelRefUtilsTest, DrawRRect) {
237 gfx::Rect layer_rect(0, 0, 256, 256);
239 SkPictureRecorder recorder;
240 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
242 TestDiscardableShader first_shader;
243 SkPaint first_paint;
244 first_paint.setShader(&first_shader);
246 TestDiscardableShader second_shader;
247 SkPaint second_paint;
248 second_paint.setShader(&second_shader);
250 TestDiscardableShader third_shader;
251 SkPaint third_paint;
252 third_paint.setShader(&third_shader);
254 SkRRect rrect;
255 rrect.setRect(SkRect::MakeXYWH(10, 20, 30, 40));
257 // (10, 20, 30, 40).
258 canvas->drawRRect(rrect, first_paint);
260 canvas->save();
262 canvas->translate(5, 17);
263 rrect.setRect(SkRect::MakeXYWH(0, 33, 25, 35));
264 // (5, 50, 25, 35)
265 canvas->drawRRect(rrect, second_paint);
267 canvas->restore();
269 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
270 canvas->translate(20, 20);
271 rrect.setRect(SkRect::MakeXYWH(0, 0, 100, 100));
272 // (50, 50, 50, 50)
273 canvas->drawRRect(rrect, third_paint);
275 skia::RefPtr<SkPicture> picture =
276 skia::AdoptRef(StopRecording(&recorder, canvas));
278 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
279 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
281 EXPECT_EQ(3u, pixel_refs.size());
282 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 20, 30, 40),
283 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
284 EXPECT_FLOAT_RECT_EQ(gfx::RectF(5, 50, 25, 35),
285 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
286 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
287 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
290 TEST(PixelRefUtilsTest, DrawOval) {
291 gfx::Rect layer_rect(0, 0, 256, 256);
293 SkPictureRecorder recorder;
294 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
296 TestDiscardableShader first_shader;
297 SkPaint first_paint;
298 first_paint.setShader(&first_shader);
300 TestDiscardableShader second_shader;
301 SkPaint second_paint;
302 second_paint.setShader(&second_shader);
304 TestDiscardableShader third_shader;
305 SkPaint third_paint;
306 third_paint.setShader(&third_shader);
308 canvas->save();
310 canvas->scale(2, 0.5);
311 // (20, 10, 60, 20).
312 canvas->drawOval(SkRect::MakeXYWH(10, 20, 30, 40), first_paint);
314 canvas->restore();
315 canvas->save();
317 canvas->translate(1, 2);
318 // (1, 35, 25, 35)
319 canvas->drawRect(SkRect::MakeXYWH(0, 33, 25, 35), second_paint);
321 canvas->restore();
323 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
324 canvas->translate(20, 20);
325 // (50, 50, 50, 50)
326 canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), third_paint);
328 skia::RefPtr<SkPicture> picture =
329 skia::AdoptRef(StopRecording(&recorder, canvas));
331 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
332 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
334 EXPECT_EQ(3u, pixel_refs.size());
335 EXPECT_FLOAT_RECT_EQ(gfx::RectF(20, 10, 60, 20),
336 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
337 EXPECT_FLOAT_RECT_EQ(gfx::RectF(1, 35, 25, 35),
338 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
339 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
340 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
343 TEST(PixelRefUtilsTest, DrawPath) {
344 gfx::Rect layer_rect(0, 0, 256, 256);
346 SkPictureRecorder recorder;
347 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
349 TestDiscardableShader first_shader;
350 SkPaint first_paint;
351 first_paint.setShader(&first_shader);
353 TestDiscardableShader second_shader;
354 SkPaint second_paint;
355 second_paint.setShader(&second_shader);
357 SkPath path;
358 path.moveTo(12, 13);
359 path.lineTo(50, 50);
360 path.lineTo(22, 101);
362 // (12, 13, 38, 88).
363 canvas->drawPath(path, first_paint);
365 canvas->save();
366 canvas->clipRect(SkRect::MakeWH(50, 50));
368 // (12, 13, 38, 37).
369 canvas->drawPath(path, second_paint);
371 canvas->restore();
373 skia::RefPtr<SkPicture> picture =
374 skia::AdoptRef(StopRecording(&recorder, canvas));
376 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
377 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
379 EXPECT_EQ(2u, pixel_refs.size());
380 EXPECT_FLOAT_RECT_EQ(gfx::RectF(12, 13, 38, 88),
381 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
382 EXPECT_FLOAT_RECT_EQ(gfx::RectF(12, 13, 38, 37),
383 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
386 TEST(PixelRefUtilsTest, DrawBitmap) {
387 gfx::Rect layer_rect(0, 0, 256, 256);
389 SkPictureRecorder recorder;
390 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
392 SkBitmap first;
393 CreateBitmap(gfx::Size(50, 50), "discardable", &first);
394 SkBitmap second;
395 CreateBitmap(gfx::Size(50, 50), "discardable", &second);
396 SkBitmap third;
397 CreateBitmap(gfx::Size(50, 50), "discardable", &third);
398 SkBitmap fourth;
399 CreateBitmap(gfx::Size(50, 1), "discardable", &fourth);
400 SkBitmap fifth;
401 CreateBitmap(gfx::Size(10, 10), "discardable", &fifth);
403 canvas->save();
405 // At (0, 0).
406 canvas->drawBitmap(first, 0, 0);
407 canvas->translate(25, 0);
408 // At (25, 0).
409 canvas->drawBitmap(second, 0, 0);
410 canvas->translate(0, 50);
411 // At (50, 50).
412 canvas->drawBitmap(third, 25, 0);
414 canvas->restore();
415 canvas->save();
417 canvas->translate(1, 0);
418 canvas->rotate(90);
419 // At (1, 0), rotated 90 degrees
420 canvas->drawBitmap(fourth, 0, 0);
422 canvas->restore();
424 canvas->scale(5, 6);
425 // At (0, 0), scaled by 5 and 6
426 canvas->drawBitmap(fifth, 0, 0);
428 skia::RefPtr<SkPicture> picture =
429 skia::AdoptRef(StopRecording(&recorder, canvas));
431 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
432 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
434 EXPECT_EQ(5u, pixel_refs.size());
435 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
436 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
437 EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50),
438 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
439 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
440 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
441 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 1, 50),
442 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
443 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 60),
444 gfx::SkRectToRectF(pixel_refs[4].pixel_ref_rect));
447 TEST(PixelRefUtilsTest, DrawBitmapRect) {
448 gfx::Rect layer_rect(0, 0, 256, 256);
450 SkPictureRecorder recorder;
451 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
453 SkBitmap first;
454 CreateBitmap(gfx::Size(50, 50), "discardable", &first);
455 SkBitmap second;
456 CreateBitmap(gfx::Size(50, 50), "discardable", &second);
457 SkBitmap third;
458 CreateBitmap(gfx::Size(50, 50), "discardable", &third);
460 TestDiscardableShader first_shader;
461 SkPaint first_paint;
462 first_paint.setShader(&first_shader);
464 SkPaint non_discardable_paint;
466 canvas->save();
468 // (0, 0, 100, 100).
469 canvas->drawBitmapRect(
470 first, SkRect::MakeWH(100, 100), &non_discardable_paint);
471 canvas->translate(25, 0);
472 // (75, 50, 10, 10).
473 canvas->drawBitmapRect(
474 second, SkRect::MakeXYWH(50, 50, 10, 10), &non_discardable_paint);
475 canvas->translate(5, 50);
476 // (0, 30, 100, 100). One from bitmap, one from paint.
477 canvas->drawBitmapRect(
478 third, SkRect::MakeXYWH(-30, -20, 100, 100), &first_paint);
480 canvas->restore();
482 skia::RefPtr<SkPicture> picture =
483 skia::AdoptRef(StopRecording(&recorder, canvas));
485 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
486 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
488 EXPECT_EQ(4u, pixel_refs.size());
489 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 100, 100),
490 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
491 EXPECT_FLOAT_RECT_EQ(gfx::RectF(75, 50, 10, 10),
492 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
493 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100),
494 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
495 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100),
496 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
499 TEST(PixelRefUtilsTest, DrawSprite) {
500 gfx::Rect layer_rect(0, 0, 256, 256);
502 SkPictureRecorder recorder;
503 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
505 SkBitmap first;
506 CreateBitmap(gfx::Size(50, 50), "discardable", &first);
507 SkBitmap second;
508 CreateBitmap(gfx::Size(50, 50), "discardable", &second);
509 SkBitmap third;
510 CreateBitmap(gfx::Size(50, 50), "discardable", &third);
511 SkBitmap fourth;
512 CreateBitmap(gfx::Size(50, 50), "discardable", &fourth);
513 SkBitmap fifth;
514 CreateBitmap(gfx::Size(50, 50), "discardable", &fifth);
516 canvas->save();
518 // Sprites aren't affected by the current matrix.
520 // (0, 0, 50, 50).
521 canvas->drawSprite(first, 0, 0);
522 canvas->translate(25, 0);
523 // (10, 0, 50, 50).
524 canvas->drawSprite(second, 10, 0);
525 canvas->translate(0, 50);
526 // (25, 0, 50, 50).
527 canvas->drawSprite(third, 25, 0);
529 canvas->restore();
530 canvas->save();
532 canvas->rotate(90);
533 // (0, 0, 50, 50).
534 canvas->drawSprite(fourth, 0, 0);
536 canvas->restore();
538 TestDiscardableShader first_shader;
539 SkPaint first_paint;
540 first_paint.setShader(&first_shader);
542 canvas->scale(5, 6);
543 // (100, 100, 50, 50).
544 canvas->drawSprite(fifth, 100, 100, &first_paint);
546 skia::RefPtr<SkPicture> picture =
547 skia::AdoptRef(StopRecording(&recorder, canvas));
549 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
550 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
552 EXPECT_EQ(6u, pixel_refs.size());
553 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
554 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
555 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 0, 50, 50),
556 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
557 EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50),
558 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
559 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
560 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
561 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 50, 50),
562 gfx::SkRectToRectF(pixel_refs[4].pixel_ref_rect));
563 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 50, 50),
564 gfx::SkRectToRectF(pixel_refs[5].pixel_ref_rect));
567 TEST(PixelRefUtilsTest, DrawText) {
568 gfx::Rect layer_rect(0, 0, 256, 256);
570 SkPictureRecorder recorder;
571 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
573 TestDiscardableShader first_shader;
574 SkPaint first_paint;
575 first_paint.setShader(&first_shader);
577 SkPoint points[4];
578 points[0].set(10, 50);
579 points[1].set(20, 50);
580 points[2].set(30, 50);
581 points[3].set(40, 50);
583 SkPath path;
584 path.moveTo(10, 50);
585 path.lineTo(20, 50);
586 path.lineTo(30, 50);
587 path.lineTo(40, 50);
588 path.lineTo(50, 50);
590 canvas->drawText("text", 4, 50, 50, first_paint);
591 canvas->drawPosText("text", 4, points, first_paint);
592 canvas->drawTextOnPath("text", 4, path, NULL, first_paint);
594 skia::RefPtr<SkPicture> picture =
595 skia::AdoptRef(StopRecording(&recorder, canvas));
597 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
598 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
600 EXPECT_EQ(3u, pixel_refs.size());
603 TEST(PixelRefUtilsTest, DrawVertices) {
604 gfx::Rect layer_rect(0, 0, 256, 256);
606 SkPictureRecorder recorder;
607 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
609 TestDiscardableShader first_shader;
610 SkPaint first_paint;
611 first_paint.setShader(&first_shader);
613 TestDiscardableShader second_shader;
614 SkPaint second_paint;
615 second_paint.setShader(&second_shader);
617 TestDiscardableShader third_shader;
618 SkPaint third_paint;
619 third_paint.setShader(&third_shader);
621 SkPoint points[3];
622 SkColor colors[3];
623 uint16_t indecies[3] = {0, 1, 2};
624 points[0].set(10, 10);
625 points[1].set(100, 20);
626 points[2].set(50, 100);
627 // (10, 10, 90, 90).
628 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
630 points,
631 points,
632 colors,
633 NULL,
634 indecies,
636 first_paint);
638 canvas->save();
640 canvas->clipRect(SkRect::MakeWH(50, 50));
641 // (10, 10, 40, 40).
642 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
644 points,
645 points,
646 colors,
647 NULL,
648 indecies,
650 second_paint);
652 canvas->restore();
654 points[0].set(50, 55);
655 points[1].set(50, 55);
656 points[2].set(200, 200);
657 // (50, 55, 150, 145).
658 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
660 points,
661 points,
662 colors,
663 NULL,
664 indecies,
666 third_paint);
668 skia::RefPtr<SkPicture> picture =
669 skia::AdoptRef(StopRecording(&recorder, canvas));
671 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
672 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
674 EXPECT_EQ(3u, pixel_refs.size());
675 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
676 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
677 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 40, 40),
678 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
679 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145),
680 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
683 } // namespace skia