[sql] Prevent nChildren overrun decoding interior pages in recover.c.
[chromium-blink-merge.git] / skia / ext / pixel_ref_utils_unittest.cc
blob1a0ff96ee0644e88b1a91f1da48b67538d7e7496
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/SkFlattenableBuffers.h"
14 #include "third_party/skia/include/core/SkPictureRecorder.h"
15 #include "third_party/skia/include/core/SkPixelRef.h"
16 #include "third_party/skia/include/core/SkPoint.h"
17 #include "third_party/skia/include/core/SkShader.h"
18 #include "third_party/skia/src/core/SkOrderedReadBuffer.h"
19 #include "ui/gfx/rect.h"
20 #include "ui/gfx/skia_util.h"
22 namespace skia {
24 namespace {
26 void CreateBitmap(gfx::Size size, const char* uri, SkBitmap* bitmap);
28 class TestDiscardableShader : public SkShader {
29 public:
30 TestDiscardableShader() {
31 CreateBitmap(gfx::Size(50, 50), "discardable", &bitmap_);
34 TestDiscardableShader(SkFlattenableReadBuffer& flattenable_buffer) {
35 SkOrderedReadBuffer& buffer =
36 static_cast<SkOrderedReadBuffer&>(flattenable_buffer);
37 SkReader32* reader = buffer.getReader32();
39 reader->skip(-4);
40 uint32_t toSkip = reader->readU32();
41 reader->skip(toSkip);
43 CreateBitmap(gfx::Size(50, 50), "discardable", &bitmap_);
46 virtual SkShader::BitmapType asABitmap(SkBitmap* bitmap,
47 SkMatrix* matrix,
48 TileMode xy[2]) const OVERRIDE {
49 if (bitmap)
50 *bitmap = bitmap_;
51 return SkShader::kDefault_BitmapType;
54 // not indended to return an actual context. Just need to supply this.
55 virtual size_t contextSize() const OVERRIDE {
56 return sizeof(SkShader::Context);
59 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(TestDiscardableShader);
61 private:
62 SkBitmap bitmap_;
65 void CreateBitmap(gfx::Size size, const char* uri, SkBitmap* bitmap) {
66 const SkImageInfo info = {
67 size.width(), size.height(), kPMColor_SkColorType, kPremul_SkAlphaType
70 bitmap->allocPixels(info);
71 bitmap->pixelRef()->setImmutable();
72 bitmap->pixelRef()->setURI(uri);
75 SkCanvas* StartRecording(SkPictureRecorder* recorder, gfx::Rect layer_rect) {
76 SkCanvas* canvas =
77 recorder->beginRecording(layer_rect.width(), layer_rect.height());
79 canvas->save();
80 canvas->translate(-layer_rect.x(), -layer_rect.y());
81 canvas->clipRect(SkRect::MakeXYWH(
82 layer_rect.x(), layer_rect.y(), layer_rect.width(), layer_rect.height()));
84 return canvas;
87 SkPicture* StopRecording(SkPictureRecorder* recorder, SkCanvas* canvas) {
88 canvas->restore();
89 return recorder->endRecording();
92 } // namespace
94 TEST(PixelRefUtilsTest, DrawPaint) {
95 gfx::Rect layer_rect(0, 0, 256, 256);
97 SkPictureRecorder recorder;
98 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
100 TestDiscardableShader first_shader;
101 SkPaint first_paint;
102 first_paint.setShader(&first_shader);
104 TestDiscardableShader second_shader;
105 SkPaint second_paint;
106 second_paint.setShader(&second_shader);
108 TestDiscardableShader third_shader;
109 SkPaint third_paint;
110 third_paint.setShader(&third_shader);
112 canvas->drawPaint(first_paint);
113 canvas->clipRect(SkRect::MakeXYWH(34, 45, 56, 67));
114 canvas->drawPaint(second_paint);
115 // Total clip is now (34, 45, 56, 55)
116 canvas->clipRect(SkRect::MakeWH(100, 100));
117 canvas->drawPaint(third_paint);
119 skia::RefPtr<SkPicture> picture = 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 = skia::AdoptRef(StopRecording(&recorder, canvas));
174 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
175 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
177 EXPECT_EQ(3u, pixel_refs.size());
178 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
179 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
180 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 40, 40),
181 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
182 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145),
183 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
186 TEST(PixelRefUtilsTest, DrawRect) {
187 gfx::Rect layer_rect(0, 0, 256, 256);
189 SkPictureRecorder recorder;
190 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
192 TestDiscardableShader first_shader;
193 SkPaint first_paint;
194 first_paint.setShader(&first_shader);
196 TestDiscardableShader second_shader;
197 SkPaint second_paint;
198 second_paint.setShader(&second_shader);
200 TestDiscardableShader third_shader;
201 SkPaint third_paint;
202 third_paint.setShader(&third_shader);
204 // (10, 20, 30, 40).
205 canvas->drawRect(SkRect::MakeXYWH(10, 20, 30, 40), first_paint);
207 canvas->save();
209 canvas->translate(5, 17);
210 // (5, 50, 25, 35)
211 canvas->drawRect(SkRect::MakeXYWH(0, 33, 25, 35), second_paint);
213 canvas->restore();
215 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
216 canvas->translate(20, 20);
217 // (50, 50, 50, 50)
218 canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), third_paint);
220 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
222 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
223 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
225 EXPECT_EQ(3u, pixel_refs.size());
226 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 20, 30, 40),
227 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
228 EXPECT_FLOAT_RECT_EQ(gfx::RectF(5, 50, 25, 35),
229 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
230 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
231 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
234 TEST(PixelRefUtilsTest, DrawRRect) {
235 gfx::Rect layer_rect(0, 0, 256, 256);
237 SkPictureRecorder recorder;
238 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
240 TestDiscardableShader first_shader;
241 SkPaint first_paint;
242 first_paint.setShader(&first_shader);
244 TestDiscardableShader second_shader;
245 SkPaint second_paint;
246 second_paint.setShader(&second_shader);
248 TestDiscardableShader third_shader;
249 SkPaint third_paint;
250 third_paint.setShader(&third_shader);
252 SkRRect rrect;
253 rrect.setRect(SkRect::MakeXYWH(10, 20, 30, 40));
255 // (10, 20, 30, 40).
256 canvas->drawRRect(rrect, first_paint);
258 canvas->save();
260 canvas->translate(5, 17);
261 rrect.setRect(SkRect::MakeXYWH(0, 33, 25, 35));
262 // (5, 50, 25, 35)
263 canvas->drawRRect(rrect, second_paint);
265 canvas->restore();
267 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
268 canvas->translate(20, 20);
269 rrect.setRect(SkRect::MakeXYWH(0, 0, 100, 100));
270 // (50, 50, 50, 50)
271 canvas->drawRRect(rrect, third_paint);
273 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
275 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
276 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
278 EXPECT_EQ(3u, pixel_refs.size());
279 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 20, 30, 40),
280 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
281 EXPECT_FLOAT_RECT_EQ(gfx::RectF(5, 50, 25, 35),
282 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
283 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
284 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
287 TEST(PixelRefUtilsTest, DrawOval) {
288 gfx::Rect layer_rect(0, 0, 256, 256);
290 SkPictureRecorder recorder;
291 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
293 TestDiscardableShader first_shader;
294 SkPaint first_paint;
295 first_paint.setShader(&first_shader);
297 TestDiscardableShader second_shader;
298 SkPaint second_paint;
299 second_paint.setShader(&second_shader);
301 TestDiscardableShader third_shader;
302 SkPaint third_paint;
303 third_paint.setShader(&third_shader);
305 canvas->save();
307 canvas->scale(2, 0.5);
308 // (20, 10, 60, 20).
309 canvas->drawOval(SkRect::MakeXYWH(10, 20, 30, 40), first_paint);
311 canvas->restore();
312 canvas->save();
314 canvas->translate(1, 2);
315 // (1, 35, 25, 35)
316 canvas->drawRect(SkRect::MakeXYWH(0, 33, 25, 35), second_paint);
318 canvas->restore();
320 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
321 canvas->translate(20, 20);
322 // (50, 50, 50, 50)
323 canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), third_paint);
325 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
327 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
328 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
330 EXPECT_EQ(3u, pixel_refs.size());
331 EXPECT_FLOAT_RECT_EQ(gfx::RectF(20, 10, 60, 20),
332 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
333 EXPECT_FLOAT_RECT_EQ(gfx::RectF(1, 35, 25, 35),
334 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
335 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
336 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
339 TEST(PixelRefUtilsTest, DrawPath) {
340 gfx::Rect layer_rect(0, 0, 256, 256);
342 SkPictureRecorder recorder;
343 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
345 TestDiscardableShader first_shader;
346 SkPaint first_paint;
347 first_paint.setShader(&first_shader);
349 TestDiscardableShader second_shader;
350 SkPaint second_paint;
351 second_paint.setShader(&second_shader);
353 SkPath path;
354 path.moveTo(12, 13);
355 path.lineTo(50, 50);
356 path.lineTo(22, 101);
358 // (12, 13, 38, 88).
359 canvas->drawPath(path, first_paint);
361 canvas->save();
362 canvas->clipRect(SkRect::MakeWH(50, 50));
364 // (12, 13, 38, 37).
365 canvas->drawPath(path, second_paint);
367 canvas->restore();
369 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
371 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
372 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
374 EXPECT_EQ(2u, pixel_refs.size());
375 EXPECT_FLOAT_RECT_EQ(gfx::RectF(12, 13, 38, 88),
376 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
377 EXPECT_FLOAT_RECT_EQ(gfx::RectF(12, 13, 38, 37),
378 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
381 TEST(PixelRefUtilsTest, DrawBitmap) {
382 gfx::Rect layer_rect(0, 0, 256, 256);
384 SkPictureRecorder recorder;
385 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
387 SkBitmap first;
388 CreateBitmap(gfx::Size(50, 50), "discardable", &first);
389 SkBitmap second;
390 CreateBitmap(gfx::Size(50, 50), "discardable", &second);
391 SkBitmap third;
392 CreateBitmap(gfx::Size(50, 50), "discardable", &third);
393 SkBitmap fourth;
394 CreateBitmap(gfx::Size(50, 1), "discardable", &fourth);
395 SkBitmap fifth;
396 CreateBitmap(gfx::Size(10, 10), "discardable", &fifth);
398 canvas->save();
400 // At (0, 0).
401 canvas->drawBitmap(first, 0, 0);
402 canvas->translate(25, 0);
403 // At (25, 0).
404 canvas->drawBitmap(second, 0, 0);
405 canvas->translate(0, 50);
406 // At (50, 50).
407 canvas->drawBitmap(third, 25, 0);
409 canvas->restore();
410 canvas->save();
412 canvas->translate(1, 0);
413 canvas->rotate(90);
414 // At (1, 0), rotated 90 degrees
415 canvas->drawBitmap(fourth, 0, 0);
417 canvas->restore();
419 canvas->scale(5, 6);
420 // At (0, 0), scaled by 5 and 6
421 canvas->drawBitmap(fifth, 0, 0);
423 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
425 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
426 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
428 EXPECT_EQ(5u, pixel_refs.size());
429 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
430 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
431 EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50),
432 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
433 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
434 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
435 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 1, 50),
436 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
437 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 60),
438 gfx::SkRectToRectF(pixel_refs[4].pixel_ref_rect));
442 TEST(PixelRefUtilsTest, DrawBitmapRect) {
443 gfx::Rect layer_rect(0, 0, 256, 256);
445 SkPictureRecorder recorder;
446 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
448 SkBitmap first;
449 CreateBitmap(gfx::Size(50, 50), "discardable", &first);
450 SkBitmap second;
451 CreateBitmap(gfx::Size(50, 50), "discardable", &second);
452 SkBitmap third;
453 CreateBitmap(gfx::Size(50, 50), "discardable", &third);
455 TestDiscardableShader first_shader;
456 SkPaint first_paint;
457 first_paint.setShader(&first_shader);
459 SkPaint non_discardable_paint;
461 canvas->save();
463 // (0, 0, 100, 100).
464 canvas->drawBitmapRect(
465 first, SkRect::MakeWH(100, 100), &non_discardable_paint);
466 canvas->translate(25, 0);
467 // (75, 50, 10, 10).
468 canvas->drawBitmapRect(
469 second, SkRect::MakeXYWH(50, 50, 10, 10), &non_discardable_paint);
470 canvas->translate(5, 50);
471 // (0, 30, 100, 100). One from bitmap, one from paint.
472 canvas->drawBitmapRect(
473 third, SkRect::MakeXYWH(-30, -20, 100, 100), &first_paint);
475 canvas->restore();
477 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
479 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
480 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
482 EXPECT_EQ(4u, pixel_refs.size());
483 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 100, 100),
484 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
485 EXPECT_FLOAT_RECT_EQ(gfx::RectF(75, 50, 10, 10),
486 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
487 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100),
488 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
489 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100),
490 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
493 TEST(PixelRefUtilsTest, DrawSprite) {
494 gfx::Rect layer_rect(0, 0, 256, 256);
496 SkPictureRecorder recorder;
497 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
499 SkBitmap first;
500 CreateBitmap(gfx::Size(50, 50), "discardable", &first);
501 SkBitmap second;
502 CreateBitmap(gfx::Size(50, 50), "discardable", &second);
503 SkBitmap third;
504 CreateBitmap(gfx::Size(50, 50), "discardable", &third);
505 SkBitmap fourth;
506 CreateBitmap(gfx::Size(50, 50), "discardable", &fourth);
507 SkBitmap fifth;
508 CreateBitmap(gfx::Size(50, 50), "discardable", &fifth);
510 canvas->save();
512 // Sprites aren't affected by the current matrix.
514 // (0, 0, 50, 50).
515 canvas->drawSprite(first, 0, 0);
516 canvas->translate(25, 0);
517 // (10, 0, 50, 50).
518 canvas->drawSprite(second, 10, 0);
519 canvas->translate(0, 50);
520 // (25, 0, 50, 50).
521 canvas->drawSprite(third, 25, 0);
523 canvas->restore();
524 canvas->save();
526 canvas->rotate(90);
527 // (0, 0, 50, 50).
528 canvas->drawSprite(fourth, 0, 0);
530 canvas->restore();
532 TestDiscardableShader first_shader;
533 SkPaint first_paint;
534 first_paint.setShader(&first_shader);
536 canvas->scale(5, 6);
537 // (100, 100, 50, 50).
538 canvas->drawSprite(fifth, 100, 100, &first_paint);
540 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
542 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
543 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
545 EXPECT_EQ(6u, pixel_refs.size());
546 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
547 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
548 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 0, 50, 50),
549 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
550 EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50),
551 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
552 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
553 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
554 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 50, 50),
555 gfx::SkRectToRectF(pixel_refs[4].pixel_ref_rect));
556 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 50, 50),
557 gfx::SkRectToRectF(pixel_refs[5].pixel_ref_rect));
560 TEST(PixelRefUtilsTest, DrawText) {
561 gfx::Rect layer_rect(0, 0, 256, 256);
563 SkPictureRecorder recorder;
564 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
566 TestDiscardableShader first_shader;
567 SkPaint first_paint;
568 first_paint.setShader(&first_shader);
570 SkPoint points[4];
571 points[0].set(10, 50);
572 points[1].set(20, 50);
573 points[2].set(30, 50);
574 points[3].set(40, 50);
576 SkPath path;
577 path.moveTo(10, 50);
578 path.lineTo(20, 50);
579 path.lineTo(30, 50);
580 path.lineTo(40, 50);
581 path.lineTo(50, 50);
583 canvas->drawText("text", 4, 50, 50, first_paint);
584 canvas->drawPosText("text", 4, points, first_paint);
585 canvas->drawTextOnPath("text", 4, path, NULL, first_paint);
587 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
589 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
590 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
592 EXPECT_EQ(3u, pixel_refs.size());
595 TEST(PixelRefUtilsTest, DrawVertices) {
596 gfx::Rect layer_rect(0, 0, 256, 256);
598 SkPictureRecorder recorder;
599 SkCanvas* canvas = StartRecording(&recorder, layer_rect);
601 TestDiscardableShader first_shader;
602 SkPaint first_paint;
603 first_paint.setShader(&first_shader);
605 TestDiscardableShader second_shader;
606 SkPaint second_paint;
607 second_paint.setShader(&second_shader);
609 TestDiscardableShader third_shader;
610 SkPaint third_paint;
611 third_paint.setShader(&third_shader);
613 SkPoint points[3];
614 SkColor colors[3];
615 uint16_t indecies[3] = {0, 1, 2};
616 points[0].set(10, 10);
617 points[1].set(100, 20);
618 points[2].set(50, 100);
619 // (10, 10, 90, 90).
620 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
622 points,
623 points,
624 colors,
625 NULL,
626 indecies,
628 first_paint);
630 canvas->save();
632 canvas->clipRect(SkRect::MakeWH(50, 50));
633 // (10, 10, 40, 40).
634 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
636 points,
637 points,
638 colors,
639 NULL,
640 indecies,
642 second_paint);
644 canvas->restore();
646 points[0].set(50, 55);
647 points[1].set(50, 55);
648 points[2].set(200, 200);
649 // (50, 55, 150, 145).
650 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
652 points,
653 points,
654 colors,
655 NULL,
656 indecies,
658 third_paint);
660 skia::RefPtr<SkPicture> picture = skia::AdoptRef(StopRecording(&recorder, canvas));
662 std::vector<skia::PixelRefUtils::PositionPixelRef> pixel_refs;
663 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture.get(), &pixel_refs);
665 EXPECT_EQ(3u, pixel_refs.size());
666 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
667 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
668 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 40, 40),
669 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
670 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145),
671 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
674 } // namespace skia