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"
25 void CreateBitmap(gfx::Size size
, const char* uri
, SkBitmap
* bitmap
);
27 class TestDiscardableShader
: public SkShader
{
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
,
39 TileMode xy
[2]) const override
{
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
54 static SkFlattenable
* CreateProc(SkReadBuffer
&);
57 Factory
getFactory() const override
{ return CreateProc
; }
63 SkFlattenable
* TestDiscardableShader::CreateProc(SkReadBuffer
&) {
64 return new TestDiscardableShader
;
67 void CreateBitmap(gfx::Size size
, const char* uri
, SkBitmap
* bitmap
) {
68 bitmap
->allocN32Pixels(size
.width(), size
.height());
69 bitmap
->pixelRef()->setImmutable();
70 bitmap
->pixelRef()->setURI(uri
);
73 SkCanvas
* StartRecording(SkPictureRecorder
* recorder
, gfx::Rect layer_rect
) {
75 recorder
->beginRecording(layer_rect
.width(), layer_rect
.height());
78 canvas
->translate(-layer_rect
.x(), -layer_rect
.y());
79 canvas
->clipRect(SkRect::MakeXYWH(
80 layer_rect
.x(), layer_rect
.y(), layer_rect
.width(), layer_rect
.height()));
85 SkPicture
* StopRecording(SkPictureRecorder
* recorder
, SkCanvas
* canvas
) {
87 return recorder
->endRecordingAsPicture();
92 void VerifyScales(SkScalar x_scale
,
94 const SkMatrix
& matrix
,
97 bool success
= matrix
.decomposeScale(&scales
);
98 EXPECT_TRUE(success
) << "line: " << source_line
;
99 EXPECT_FLOAT_EQ(x_scale
, scales
.width()) << "line: " << source_line
;
100 EXPECT_FLOAT_EQ(y_scale
, scales
.height()) << "line: " << source_line
;
103 TEST(PixelRefUtilsTest
, DrawPaint
) {
104 gfx::Rect
layer_rect(0, 0, 256, 256);
106 SkPictureRecorder recorder
;
107 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
109 TestDiscardableShader first_shader
;
111 first_paint
.setShader(&first_shader
);
113 TestDiscardableShader second_shader
;
114 SkPaint second_paint
;
115 second_paint
.setShader(&second_shader
);
117 TestDiscardableShader third_shader
;
119 third_paint
.setShader(&third_shader
);
121 canvas
->drawPaint(first_paint
);
122 canvas
->clipRect(SkRect::MakeXYWH(34, 45, 56, 67));
123 canvas
->drawPaint(second_paint
);
126 canvas
->scale(2.f
, 3.f
);
127 canvas
->drawPaint(second_paint
);
130 // Total clip is now (34, 45, 56, 55)
131 canvas
->clipRect(SkRect::MakeWH(100, 100));
132 canvas
->drawPaint(third_paint
);
134 skia::RefPtr
<SkPicture
> picture
=
135 skia::AdoptRef(StopRecording(&recorder
, canvas
));
137 std::vector
<skia::PixelRefUtils::PositionPixelRef
> pixel_refs
;
138 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
.get(), &pixel_refs
);
140 EXPECT_EQ(4u, pixel_refs
.size());
141 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 256, 256),
142 gfx::SkRectToRectF(pixel_refs
[0].pixel_ref_rect
));
143 VerifyScales(1.f
, 1.f
, pixel_refs
[0].matrix
, __LINE__
);
144 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[0].filter_quality
);
145 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 67),
146 gfx::SkRectToRectF(pixel_refs
[1].pixel_ref_rect
));
147 VerifyScales(1.f
, 1.f
, pixel_refs
[1].matrix
, __LINE__
);
148 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 67),
149 gfx::SkRectToRectF(pixel_refs
[1].pixel_ref_rect
));
150 VerifyScales(2.f
, 3.f
, pixel_refs
[2].matrix
, __LINE__
);
151 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[2].filter_quality
);
152 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 55),
153 gfx::SkRectToRectF(pixel_refs
[3].pixel_ref_rect
));
154 VerifyScales(1.f
, 1.f
, pixel_refs
[3].matrix
, __LINE__
);
155 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[3].filter_quality
);
158 TEST(PixelRefUtilsTest
, DrawPoints
) {
159 gfx::Rect
layer_rect(0, 0, 256, 256);
161 SkPictureRecorder recorder
;
162 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
164 TestDiscardableShader first_shader
;
166 first_paint
.setShader(&first_shader
);
168 TestDiscardableShader second_shader
;
169 SkPaint second_paint
;
170 second_paint
.setShader(&second_shader
);
172 TestDiscardableShader third_shader
;
174 third_paint
.setShader(&third_shader
);
177 points
[0].set(10, 10);
178 points
[1].set(100, 20);
179 points
[2].set(50, 100);
181 canvas
->drawPoints(SkCanvas::kPolygon_PointMode
, 3, points
, first_paint
);
185 canvas
->clipRect(SkRect::MakeWH(50, 50));
187 canvas
->drawPoints(SkCanvas::kPolygon_PointMode
, 3, points
, second_paint
);
191 points
[0].set(50, 55);
192 points
[1].set(50, 55);
193 points
[2].set(200, 200);
194 // (50, 55, 150, 145).
195 canvas
->drawPoints(SkCanvas::kPolygon_PointMode
, 3, points
, third_paint
);
197 skia::RefPtr
<SkPicture
> picture
=
198 skia::AdoptRef(StopRecording(&recorder
, canvas
));
200 std::vector
<skia::PixelRefUtils::PositionPixelRef
> pixel_refs
;
201 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
.get(), &pixel_refs
);
203 EXPECT_EQ(3u, pixel_refs
.size());
204 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
205 gfx::SkRectToRectF(pixel_refs
[0].pixel_ref_rect
));
206 VerifyScales(1.f
, 1.f
, pixel_refs
[0].matrix
, __LINE__
);
207 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[0].filter_quality
);
208 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 40, 40),
209 gfx::SkRectToRectF(pixel_refs
[1].pixel_ref_rect
));
210 VerifyScales(1.f
, 1.f
, pixel_refs
[1].matrix
, __LINE__
);
211 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[1].filter_quality
);
212 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145),
213 gfx::SkRectToRectF(pixel_refs
[2].pixel_ref_rect
));
214 VerifyScales(1.f
, 1.f
, pixel_refs
[2].matrix
, __LINE__
);
215 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[2].filter_quality
);
218 TEST(PixelRefUtilsTest
, DrawRect
) {
219 gfx::Rect
layer_rect(0, 0, 256, 256);
221 SkPictureRecorder recorder
;
222 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
224 TestDiscardableShader first_shader
;
226 first_paint
.setShader(&first_shader
);
228 TestDiscardableShader second_shader
;
229 SkPaint second_paint
;
230 second_paint
.setShader(&second_shader
);
232 TestDiscardableShader third_shader
;
234 third_paint
.setShader(&third_shader
);
237 canvas
->drawRect(SkRect::MakeXYWH(10, 20, 30, 40), first_paint
);
241 canvas
->translate(5, 17);
243 canvas
->drawRect(SkRect::MakeXYWH(0, 33, 25, 35), second_paint
);
247 canvas
->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
248 canvas
->translate(20, 20);
250 canvas
->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), third_paint
);
252 skia::RefPtr
<SkPicture
> picture
=
253 skia::AdoptRef(StopRecording(&recorder
, canvas
));
255 std::vector
<skia::PixelRefUtils::PositionPixelRef
> pixel_refs
;
256 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
.get(), &pixel_refs
);
258 EXPECT_EQ(3u, pixel_refs
.size());
259 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 20, 30, 40),
260 gfx::SkRectToRectF(pixel_refs
[0].pixel_ref_rect
));
261 VerifyScales(1.f
, 1.f
, pixel_refs
[0].matrix
, __LINE__
);
262 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[0].filter_quality
);
263 EXPECT_FLOAT_RECT_EQ(gfx::RectF(5, 50, 25, 35),
264 gfx::SkRectToRectF(pixel_refs
[1].pixel_ref_rect
));
265 VerifyScales(1.f
, 1.f
, pixel_refs
[1].matrix
, __LINE__
);
266 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[1].filter_quality
);
267 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
268 gfx::SkRectToRectF(pixel_refs
[2].pixel_ref_rect
));
269 VerifyScales(1.f
, 1.f
, pixel_refs
[2].matrix
, __LINE__
);
270 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[2].filter_quality
);
273 TEST(PixelRefUtilsTest
, DrawRRect
) {
274 gfx::Rect
layer_rect(0, 0, 256, 256);
276 SkPictureRecorder recorder
;
277 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
279 TestDiscardableShader first_shader
;
281 first_paint
.setShader(&first_shader
);
283 TestDiscardableShader second_shader
;
284 SkPaint second_paint
;
285 second_paint
.setShader(&second_shader
);
287 TestDiscardableShader third_shader
;
289 third_paint
.setShader(&third_shader
);
292 rrect
.setRect(SkRect::MakeXYWH(10, 20, 30, 40));
295 canvas
->drawRRect(rrect
, first_paint
);
299 canvas
->translate(5, 17);
300 rrect
.setRect(SkRect::MakeXYWH(0, 33, 25, 35));
302 canvas
->drawRRect(rrect
, second_paint
);
306 canvas
->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
307 canvas
->translate(20, 20);
308 rrect
.setRect(SkRect::MakeXYWH(0, 0, 100, 100));
310 canvas
->drawRRect(rrect
, third_paint
);
312 skia::RefPtr
<SkPicture
> picture
=
313 skia::AdoptRef(StopRecording(&recorder
, canvas
));
315 std::vector
<skia::PixelRefUtils::PositionPixelRef
> pixel_refs
;
316 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
.get(), &pixel_refs
);
318 EXPECT_EQ(3u, pixel_refs
.size());
319 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 20, 30, 40),
320 gfx::SkRectToRectF(pixel_refs
[0].pixel_ref_rect
));
321 VerifyScales(1.f
, 1.f
, pixel_refs
[0].matrix
, __LINE__
);
322 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[0].filter_quality
);
323 EXPECT_FLOAT_RECT_EQ(gfx::RectF(5, 50, 25, 35),
324 gfx::SkRectToRectF(pixel_refs
[1].pixel_ref_rect
));
325 VerifyScales(1.f
, 1.f
, pixel_refs
[1].matrix
, __LINE__
);
326 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[1].filter_quality
);
327 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
328 gfx::SkRectToRectF(pixel_refs
[2].pixel_ref_rect
));
329 VerifyScales(1.f
, 1.f
, pixel_refs
[2].matrix
, __LINE__
);
330 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[2].filter_quality
);
333 TEST(PixelRefUtilsTest
, DrawOval
) {
334 gfx::Rect
layer_rect(0, 0, 256, 256);
336 SkPictureRecorder recorder
;
337 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
339 TestDiscardableShader first_shader
;
341 first_paint
.setShader(&first_shader
);
343 TestDiscardableShader second_shader
;
344 SkPaint second_paint
;
345 second_paint
.setShader(&second_shader
);
347 TestDiscardableShader third_shader
;
349 third_paint
.setShader(&third_shader
);
353 canvas
->scale(2.f
, 0.5f
);
355 canvas
->drawOval(SkRect::MakeXYWH(10, 20, 30, 40), first_paint
);
360 canvas
->translate(1, 2);
362 canvas
->drawRect(SkRect::MakeXYWH(0, 33, 25, 35), second_paint
);
366 canvas
->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
367 canvas
->translate(20, 20);
369 canvas
->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), third_paint
);
371 skia::RefPtr
<SkPicture
> picture
=
372 skia::AdoptRef(StopRecording(&recorder
, canvas
));
374 std::vector
<skia::PixelRefUtils::PositionPixelRef
> pixel_refs
;
375 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
.get(), &pixel_refs
);
377 EXPECT_EQ(3u, pixel_refs
.size());
378 EXPECT_FLOAT_RECT_EQ(gfx::RectF(20, 10, 60, 20),
379 gfx::SkRectToRectF(pixel_refs
[0].pixel_ref_rect
));
380 VerifyScales(2.f
, 0.5f
, pixel_refs
[0].matrix
, __LINE__
);
381 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[0].filter_quality
);
382 EXPECT_FLOAT_RECT_EQ(gfx::RectF(1, 35, 25, 35),
383 gfx::SkRectToRectF(pixel_refs
[1].pixel_ref_rect
));
384 VerifyScales(1.f
, 1.f
, pixel_refs
[1].matrix
, __LINE__
);
385 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[1].filter_quality
);
386 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
387 gfx::SkRectToRectF(pixel_refs
[2].pixel_ref_rect
));
388 VerifyScales(1.f
, 1.f
, pixel_refs
[2].matrix
, __LINE__
);
389 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[2].filter_quality
);
392 TEST(PixelRefUtilsTest
, DrawPath
) {
393 gfx::Rect
layer_rect(0, 0, 256, 256);
395 SkPictureRecorder recorder
;
396 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
398 TestDiscardableShader first_shader
;
400 first_paint
.setShader(&first_shader
);
402 TestDiscardableShader second_shader
;
403 SkPaint second_paint
;
404 second_paint
.setShader(&second_shader
);
409 path
.lineTo(22, 101);
412 canvas
->drawPath(path
, first_paint
);
415 canvas
->clipRect(SkRect::MakeWH(50, 50));
418 canvas
->drawPath(path
, second_paint
);
422 skia::RefPtr
<SkPicture
> picture
=
423 skia::AdoptRef(StopRecording(&recorder
, canvas
));
425 std::vector
<skia::PixelRefUtils::PositionPixelRef
> pixel_refs
;
426 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
.get(), &pixel_refs
);
428 EXPECT_EQ(2u, pixel_refs
.size());
429 EXPECT_FLOAT_RECT_EQ(gfx::RectF(12, 13, 38, 88),
430 gfx::SkRectToRectF(pixel_refs
[0].pixel_ref_rect
));
431 VerifyScales(1.f
, 1.f
, pixel_refs
[0].matrix
, __LINE__
);
432 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[0].filter_quality
);
433 EXPECT_FLOAT_RECT_EQ(gfx::RectF(12, 13, 38, 37),
434 gfx::SkRectToRectF(pixel_refs
[1].pixel_ref_rect
));
435 VerifyScales(1.f
, 1.f
, pixel_refs
[1].matrix
, __LINE__
);
436 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[1].filter_quality
);
439 TEST(PixelRefUtilsTest
, DrawBitmap
) {
440 gfx::Rect
layer_rect(0, 0, 256, 256);
442 SkPictureRecorder recorder
;
443 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
446 CreateBitmap(gfx::Size(50, 50), "discardable", &first
);
448 CreateBitmap(gfx::Size(50, 50), "discardable", &second
);
450 CreateBitmap(gfx::Size(50, 50), "discardable", &third
);
452 CreateBitmap(gfx::Size(50, 1), "discardable", &fourth
);
454 CreateBitmap(gfx::Size(10, 10), "discardable", &fifth
);
456 CreateBitmap(gfx::Size(10, 10), "discardable", &sixth
);
461 canvas
->drawBitmap(first
, 0, 0);
462 canvas
->translate(25, 0);
464 canvas
->drawBitmap(second
, 0, 0);
465 canvas
->translate(0, 50);
467 canvas
->drawBitmap(third
, 25, 0);
472 canvas
->translate(1, 0);
474 // At (1, 0), rotated 90 degrees
475 canvas
->drawBitmap(fourth
, 0, 0);
480 canvas
->scale(5.f
, 6.f
);
481 // At (0, 0), scaled by 5 and 6
482 canvas
->drawBitmap(fifth
, 0, 0);
487 canvas
->scale(3.3f
, 0.4f
);
489 canvas
->drawBitmap(sixth
, 0, 0);
493 skia::RefPtr
<SkPicture
> picture
=
494 skia::AdoptRef(StopRecording(&recorder
, canvas
));
496 std::vector
<skia::PixelRefUtils::PositionPixelRef
> pixel_refs
;
497 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
.get(), &pixel_refs
);
499 EXPECT_EQ(6u, pixel_refs
.size());
500 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
501 gfx::SkRectToRectF(pixel_refs
[0].pixel_ref_rect
));
502 VerifyScales(1.f
, 1.f
, pixel_refs
[0].matrix
, __LINE__
);
503 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[0].filter_quality
);
504 EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50),
505 gfx::SkRectToRectF(pixel_refs
[1].pixel_ref_rect
));
506 VerifyScales(1.f
, 1.f
, pixel_refs
[1].matrix
, __LINE__
);
507 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[1].filter_quality
);
508 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
509 gfx::SkRectToRectF(pixel_refs
[2].pixel_ref_rect
));
510 VerifyScales(1.f
, 1.f
, pixel_refs
[2].matrix
, __LINE__
);
511 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[2].filter_quality
);
512 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 1, 50),
513 gfx::SkRectToRectF(pixel_refs
[3].pixel_ref_rect
));
514 VerifyScales(1.f
, 1.f
, pixel_refs
[3].matrix
, __LINE__
);
515 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[3].filter_quality
);
516 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 60),
517 gfx::SkRectToRectF(pixel_refs
[4].pixel_ref_rect
));
518 VerifyScales(5.f
, 6.f
, pixel_refs
[4].matrix
, __LINE__
);
519 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[4].filter_quality
);
520 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 29.403214f
, 18.545712f
),
521 gfx::SkRectToRectF(pixel_refs
[5].pixel_ref_rect
));
522 VerifyScales(3.3f
, 0.4f
, pixel_refs
[5].matrix
, __LINE__
);
523 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[5].filter_quality
);
526 TEST(PixelRefUtilsTest
, DrawBitmapRect
) {
527 gfx::Rect
layer_rect(0, 0, 256, 256);
529 SkPictureRecorder recorder
;
530 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
533 CreateBitmap(gfx::Size(50, 50), "discardable", &first
);
535 CreateBitmap(gfx::Size(50, 50), "discardable", &second
);
537 CreateBitmap(gfx::Size(50, 50), "discardable", &third
);
539 TestDiscardableShader first_shader
;
541 first_paint
.setShader(&first_shader
);
543 SkPaint non_discardable_paint
;
548 canvas
->drawBitmapRect(
549 first
, SkRect::MakeWH(100, 100), &non_discardable_paint
);
550 canvas
->translate(25, 0);
552 canvas
->drawBitmapRect(
553 second
, SkRect::MakeXYWH(50, 50, 10, 10), &non_discardable_paint
);
554 canvas
->translate(5, 50);
555 // (0, 30, 100, 100). One from bitmap, one from paint.
556 canvas
->drawBitmapRect(
557 third
, SkRect::MakeXYWH(-30, -20, 100, 100), &first_paint
);
561 skia::RefPtr
<SkPicture
> picture
=
562 skia::AdoptRef(StopRecording(&recorder
, canvas
));
564 std::vector
<skia::PixelRefUtils::PositionPixelRef
> pixel_refs
;
565 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
.get(), &pixel_refs
);
567 EXPECT_EQ(4u, pixel_refs
.size());
568 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 100, 100),
569 gfx::SkRectToRectF(pixel_refs
[0].pixel_ref_rect
));
570 VerifyScales(2.f
, 2.f
, pixel_refs
[0].matrix
, __LINE__
);
571 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[0].filter_quality
);
572 EXPECT_FLOAT_RECT_EQ(gfx::RectF(75, 50, 10, 10),
573 gfx::SkRectToRectF(pixel_refs
[1].pixel_ref_rect
));
574 VerifyScales(0.2f
, 0.2f
, pixel_refs
[1].matrix
, __LINE__
);
575 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[1].filter_quality
);
576 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100),
577 gfx::SkRectToRectF(pixel_refs
[2].pixel_ref_rect
));
578 VerifyScales(2.f
, 2.f
, pixel_refs
[2].matrix
, __LINE__
);
579 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[2].filter_quality
);
580 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100),
581 gfx::SkRectToRectF(pixel_refs
[3].pixel_ref_rect
));
582 VerifyScales(2.f
, 2.f
, pixel_refs
[3].matrix
, __LINE__
);
583 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[3].filter_quality
);
586 TEST(PixelRefUtilsTest
, DrawSprite
) {
587 gfx::Rect
layer_rect(0, 0, 256, 256);
589 SkPictureRecorder recorder
;
590 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
593 CreateBitmap(gfx::Size(50, 50), "discardable", &first
);
595 CreateBitmap(gfx::Size(50, 50), "discardable", &second
);
597 CreateBitmap(gfx::Size(50, 50), "discardable", &third
);
599 CreateBitmap(gfx::Size(50, 50), "discardable", &fourth
);
601 CreateBitmap(gfx::Size(50, 50), "discardable", &fifth
);
605 // Sprites aren't affected by the current matrix.
608 canvas
->drawSprite(first
, 0, 0);
609 canvas
->translate(25, 0);
611 canvas
->drawSprite(second
, 10, 0);
612 canvas
->translate(0, 50);
614 canvas
->drawSprite(third
, 25, 0);
621 canvas
->drawSprite(fourth
, 0, 0);
625 TestDiscardableShader first_shader
;
627 first_paint
.setShader(&first_shader
);
629 canvas
->scale(5.f
, 6.f
);
630 // (100, 100, 50, 50).
631 canvas
->drawSprite(fifth
, 100, 100, &first_paint
);
633 skia::RefPtr
<SkPicture
> picture
=
634 skia::AdoptRef(StopRecording(&recorder
, canvas
));
636 std::vector
<skia::PixelRefUtils::PositionPixelRef
> pixel_refs
;
637 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
.get(), &pixel_refs
);
639 EXPECT_EQ(6u, pixel_refs
.size());
640 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
641 gfx::SkRectToRectF(pixel_refs
[0].pixel_ref_rect
));
642 VerifyScales(1.f
, 1.f
, pixel_refs
[0].matrix
, __LINE__
);
643 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[0].filter_quality
);
644 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 0, 50, 50),
645 gfx::SkRectToRectF(pixel_refs
[1].pixel_ref_rect
));
646 VerifyScales(1.f
, 1.f
, pixel_refs
[1].matrix
, __LINE__
);
647 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[1].filter_quality
);
648 EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50),
649 gfx::SkRectToRectF(pixel_refs
[2].pixel_ref_rect
));
650 VerifyScales(1.f
, 1.f
, pixel_refs
[2].matrix
, __LINE__
);
651 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[2].filter_quality
);
652 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
653 gfx::SkRectToRectF(pixel_refs
[3].pixel_ref_rect
));
654 VerifyScales(1.f
, 1.f
, pixel_refs
[3].matrix
, __LINE__
);
655 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[3].filter_quality
);
656 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 50, 50),
657 gfx::SkRectToRectF(pixel_refs
[4].pixel_ref_rect
));
658 VerifyScales(1.f
, 1.f
, pixel_refs
[4].matrix
, __LINE__
);
659 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[4].filter_quality
);
660 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 50, 50),
661 gfx::SkRectToRectF(pixel_refs
[5].pixel_ref_rect
));
662 VerifyScales(1.f
, 1.f
, pixel_refs
[5].matrix
, __LINE__
);
663 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[5].filter_quality
);
666 TEST(PixelRefUtilsTest
, DrawText
) {
667 gfx::Rect
layer_rect(0, 0, 256, 256);
669 SkPictureRecorder recorder
;
670 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
672 TestDiscardableShader first_shader
;
674 first_paint
.setShader(&first_shader
);
677 points
[0].set(10, 50);
678 points
[1].set(20, 50);
679 points
[2].set(30, 50);
680 points
[3].set(40, 50);
689 canvas
->drawText("text", 4, 50, 50, first_paint
);
690 canvas
->drawPosText("text", 4, points
, first_paint
);
691 canvas
->drawTextOnPath("text", 4, path
, NULL
, first_paint
);
693 skia::RefPtr
<SkPicture
> picture
=
694 skia::AdoptRef(StopRecording(&recorder
, canvas
));
696 std::vector
<skia::PixelRefUtils::PositionPixelRef
> pixel_refs
;
697 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
.get(), &pixel_refs
);
699 EXPECT_EQ(3u, pixel_refs
.size());
702 TEST(PixelRefUtilsTest
, DrawVertices
) {
703 gfx::Rect
layer_rect(0, 0, 256, 256);
705 SkPictureRecorder recorder
;
706 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
708 TestDiscardableShader first_shader
;
710 first_paint
.setShader(&first_shader
);
712 TestDiscardableShader second_shader
;
713 SkPaint second_paint
;
714 second_paint
.setShader(&second_shader
);
716 TestDiscardableShader third_shader
;
718 third_paint
.setShader(&third_shader
);
722 uint16_t indecies
[3] = {0, 1, 2};
723 points
[0].set(10, 10);
724 points
[1].set(100, 20);
725 points
[2].set(50, 100);
727 canvas
->drawVertices(SkCanvas::kTriangles_VertexMode
,
739 canvas
->clipRect(SkRect::MakeWH(50, 50));
741 canvas
->drawVertices(SkCanvas::kTriangles_VertexMode
,
753 points
[0].set(50, 55);
754 points
[1].set(50, 55);
755 points
[2].set(200, 200);
756 // (50, 55, 150, 145).
757 canvas
->drawVertices(SkCanvas::kTriangles_VertexMode
,
767 skia::RefPtr
<SkPicture
> picture
=
768 skia::AdoptRef(StopRecording(&recorder
, canvas
));
770 std::vector
<skia::PixelRefUtils::PositionPixelRef
> pixel_refs
;
771 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
.get(), &pixel_refs
);
773 EXPECT_EQ(3u, pixel_refs
.size());
774 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
775 gfx::SkRectToRectF(pixel_refs
[0].pixel_ref_rect
));
776 VerifyScales(1.f
, 1.f
, pixel_refs
[0].matrix
, __LINE__
);
777 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[0].filter_quality
);
778 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 40, 40),
779 gfx::SkRectToRectF(pixel_refs
[1].pixel_ref_rect
));
780 VerifyScales(1.f
, 1.f
, pixel_refs
[1].matrix
, __LINE__
);
781 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[1].filter_quality
);
782 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145),
783 gfx::SkRectToRectF(pixel_refs
[2].pixel_ref_rect
));
784 VerifyScales(1.f
, 1.f
, pixel_refs
[2].matrix
, __LINE__
);
785 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[2].filter_quality
);