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
&);
55 friend class SkPrivateEffectInitializer
;
58 Factory
getFactory() const override
{ return CreateProc
; }
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
) {
76 recorder
->beginRecording(layer_rect
.width(), layer_rect
.height());
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()));
86 SkPicture
* StopRecording(SkPictureRecorder
* recorder
, SkCanvas
* canvas
) {
88 return recorder
->endRecordingAsPicture();
93 void VerifyScales(SkScalar x_scale
,
95 const SkMatrix
& matrix
,
98 bool success
= matrix
.decomposeScale(&scales
);
99 EXPECT_TRUE(success
) << "line: " << source_line
;
100 EXPECT_FLOAT_EQ(x_scale
, scales
.width()) << "line: " << source_line
;
101 EXPECT_FLOAT_EQ(y_scale
, scales
.height()) << "line: " << source_line
;
104 TEST(PixelRefUtilsTest
, DrawPaint
) {
105 gfx::Rect
layer_rect(0, 0, 256, 256);
107 SkPictureRecorder recorder
;
108 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
110 TestDiscardableShader first_shader
;
112 first_paint
.setShader(&first_shader
);
114 TestDiscardableShader second_shader
;
115 SkPaint second_paint
;
116 second_paint
.setShader(&second_shader
);
118 TestDiscardableShader third_shader
;
120 third_paint
.setShader(&third_shader
);
122 canvas
->drawPaint(first_paint
);
123 canvas
->clipRect(SkRect::MakeXYWH(34, 45, 56, 67));
124 canvas
->drawPaint(second_paint
);
127 canvas
->scale(2.f
, 3.f
);
128 canvas
->drawPaint(second_paint
);
131 // Total clip is now (34, 45, 56, 55)
132 canvas
->clipRect(SkRect::MakeWH(100, 100));
133 canvas
->drawPaint(third_paint
);
135 skia::RefPtr
<SkPicture
> picture
=
136 skia::AdoptRef(StopRecording(&recorder
, canvas
));
138 std::vector
<skia::PixelRefUtils::PositionPixelRef
> pixel_refs
;
139 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
.get(), &pixel_refs
);
141 EXPECT_EQ(4u, pixel_refs
.size());
142 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 256, 256),
143 gfx::SkRectToRectF(pixel_refs
[0].pixel_ref_rect
));
144 VerifyScales(1.f
, 1.f
, pixel_refs
[0].matrix
, __LINE__
);
145 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[0].filter_quality
);
146 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 67),
147 gfx::SkRectToRectF(pixel_refs
[1].pixel_ref_rect
));
148 VerifyScales(1.f
, 1.f
, pixel_refs
[1].matrix
, __LINE__
);
149 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 67),
150 gfx::SkRectToRectF(pixel_refs
[1].pixel_ref_rect
));
151 VerifyScales(2.f
, 3.f
, pixel_refs
[2].matrix
, __LINE__
);
152 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[2].filter_quality
);
153 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 55),
154 gfx::SkRectToRectF(pixel_refs
[3].pixel_ref_rect
));
155 VerifyScales(1.f
, 1.f
, pixel_refs
[3].matrix
, __LINE__
);
156 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[3].filter_quality
);
159 TEST(PixelRefUtilsTest
, DrawPoints
) {
160 gfx::Rect
layer_rect(0, 0, 256, 256);
162 SkPictureRecorder recorder
;
163 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
165 TestDiscardableShader first_shader
;
167 first_paint
.setShader(&first_shader
);
169 TestDiscardableShader second_shader
;
170 SkPaint second_paint
;
171 second_paint
.setShader(&second_shader
);
173 TestDiscardableShader third_shader
;
175 third_paint
.setShader(&third_shader
);
178 points
[0].set(10, 10);
179 points
[1].set(100, 20);
180 points
[2].set(50, 100);
182 canvas
->drawPoints(SkCanvas::kPolygon_PointMode
, 3, points
, first_paint
);
186 canvas
->clipRect(SkRect::MakeWH(50, 50));
188 canvas
->drawPoints(SkCanvas::kPolygon_PointMode
, 3, points
, second_paint
);
192 points
[0].set(50, 55);
193 points
[1].set(50, 55);
194 points
[2].set(200, 200);
195 // (50, 55, 150, 145).
196 canvas
->drawPoints(SkCanvas::kPolygon_PointMode
, 3, points
, third_paint
);
198 skia::RefPtr
<SkPicture
> picture
=
199 skia::AdoptRef(StopRecording(&recorder
, canvas
));
201 std::vector
<skia::PixelRefUtils::PositionPixelRef
> pixel_refs
;
202 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
.get(), &pixel_refs
);
204 EXPECT_EQ(3u, pixel_refs
.size());
205 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
206 gfx::SkRectToRectF(pixel_refs
[0].pixel_ref_rect
));
207 VerifyScales(1.f
, 1.f
, pixel_refs
[0].matrix
, __LINE__
);
208 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[0].filter_quality
);
209 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 40, 40),
210 gfx::SkRectToRectF(pixel_refs
[1].pixel_ref_rect
));
211 VerifyScales(1.f
, 1.f
, pixel_refs
[1].matrix
, __LINE__
);
212 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[1].filter_quality
);
213 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145),
214 gfx::SkRectToRectF(pixel_refs
[2].pixel_ref_rect
));
215 VerifyScales(1.f
, 1.f
, pixel_refs
[2].matrix
, __LINE__
);
216 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[2].filter_quality
);
219 TEST(PixelRefUtilsTest
, DrawRect
) {
220 gfx::Rect
layer_rect(0, 0, 256, 256);
222 SkPictureRecorder recorder
;
223 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
225 TestDiscardableShader first_shader
;
227 first_paint
.setShader(&first_shader
);
229 TestDiscardableShader second_shader
;
230 SkPaint second_paint
;
231 second_paint
.setShader(&second_shader
);
233 TestDiscardableShader third_shader
;
235 third_paint
.setShader(&third_shader
);
238 canvas
->drawRect(SkRect::MakeXYWH(10, 20, 30, 40), first_paint
);
242 canvas
->translate(5, 17);
244 canvas
->drawRect(SkRect::MakeXYWH(0, 33, 25, 35), second_paint
);
248 canvas
->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
249 canvas
->translate(20, 20);
251 canvas
->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), third_paint
);
253 skia::RefPtr
<SkPicture
> picture
=
254 skia::AdoptRef(StopRecording(&recorder
, canvas
));
256 std::vector
<skia::PixelRefUtils::PositionPixelRef
> pixel_refs
;
257 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
.get(), &pixel_refs
);
259 EXPECT_EQ(3u, pixel_refs
.size());
260 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 20, 30, 40),
261 gfx::SkRectToRectF(pixel_refs
[0].pixel_ref_rect
));
262 VerifyScales(1.f
, 1.f
, pixel_refs
[0].matrix
, __LINE__
);
263 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[0].filter_quality
);
264 EXPECT_FLOAT_RECT_EQ(gfx::RectF(5, 50, 25, 35),
265 gfx::SkRectToRectF(pixel_refs
[1].pixel_ref_rect
));
266 VerifyScales(1.f
, 1.f
, pixel_refs
[1].matrix
, __LINE__
);
267 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[1].filter_quality
);
268 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
269 gfx::SkRectToRectF(pixel_refs
[2].pixel_ref_rect
));
270 VerifyScales(1.f
, 1.f
, pixel_refs
[2].matrix
, __LINE__
);
271 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[2].filter_quality
);
274 TEST(PixelRefUtilsTest
, DrawRRect
) {
275 gfx::Rect
layer_rect(0, 0, 256, 256);
277 SkPictureRecorder recorder
;
278 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
280 TestDiscardableShader first_shader
;
282 first_paint
.setShader(&first_shader
);
284 TestDiscardableShader second_shader
;
285 SkPaint second_paint
;
286 second_paint
.setShader(&second_shader
);
288 TestDiscardableShader third_shader
;
290 third_paint
.setShader(&third_shader
);
293 rrect
.setRect(SkRect::MakeXYWH(10, 20, 30, 40));
296 canvas
->drawRRect(rrect
, first_paint
);
300 canvas
->translate(5, 17);
301 rrect
.setRect(SkRect::MakeXYWH(0, 33, 25, 35));
303 canvas
->drawRRect(rrect
, second_paint
);
307 canvas
->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
308 canvas
->translate(20, 20);
309 rrect
.setRect(SkRect::MakeXYWH(0, 0, 100, 100));
311 canvas
->drawRRect(rrect
, third_paint
);
313 skia::RefPtr
<SkPicture
> picture
=
314 skia::AdoptRef(StopRecording(&recorder
, canvas
));
316 std::vector
<skia::PixelRefUtils::PositionPixelRef
> pixel_refs
;
317 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
.get(), &pixel_refs
);
319 EXPECT_EQ(3u, pixel_refs
.size());
320 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 20, 30, 40),
321 gfx::SkRectToRectF(pixel_refs
[0].pixel_ref_rect
));
322 VerifyScales(1.f
, 1.f
, pixel_refs
[0].matrix
, __LINE__
);
323 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[0].filter_quality
);
324 EXPECT_FLOAT_RECT_EQ(gfx::RectF(5, 50, 25, 35),
325 gfx::SkRectToRectF(pixel_refs
[1].pixel_ref_rect
));
326 VerifyScales(1.f
, 1.f
, pixel_refs
[1].matrix
, __LINE__
);
327 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[1].filter_quality
);
328 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
329 gfx::SkRectToRectF(pixel_refs
[2].pixel_ref_rect
));
330 VerifyScales(1.f
, 1.f
, pixel_refs
[2].matrix
, __LINE__
);
331 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[2].filter_quality
);
334 TEST(PixelRefUtilsTest
, DrawOval
) {
335 gfx::Rect
layer_rect(0, 0, 256, 256);
337 SkPictureRecorder recorder
;
338 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
340 TestDiscardableShader first_shader
;
342 first_paint
.setShader(&first_shader
);
344 TestDiscardableShader second_shader
;
345 SkPaint second_paint
;
346 second_paint
.setShader(&second_shader
);
348 TestDiscardableShader third_shader
;
350 third_paint
.setShader(&third_shader
);
354 canvas
->scale(2.f
, 0.5f
);
356 canvas
->drawOval(SkRect::MakeXYWH(10, 20, 30, 40), first_paint
);
361 canvas
->translate(1, 2);
363 canvas
->drawRect(SkRect::MakeXYWH(0, 33, 25, 35), second_paint
);
367 canvas
->clipRect(SkRect::MakeXYWH(50, 50, 50, 50));
368 canvas
->translate(20, 20);
370 canvas
->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), third_paint
);
372 skia::RefPtr
<SkPicture
> picture
=
373 skia::AdoptRef(StopRecording(&recorder
, canvas
));
375 std::vector
<skia::PixelRefUtils::PositionPixelRef
> pixel_refs
;
376 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
.get(), &pixel_refs
);
378 EXPECT_EQ(3u, pixel_refs
.size());
379 EXPECT_FLOAT_RECT_EQ(gfx::RectF(20, 10, 60, 20),
380 gfx::SkRectToRectF(pixel_refs
[0].pixel_ref_rect
));
381 VerifyScales(2.f
, 0.5f
, pixel_refs
[0].matrix
, __LINE__
);
382 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[0].filter_quality
);
383 EXPECT_FLOAT_RECT_EQ(gfx::RectF(1, 35, 25, 35),
384 gfx::SkRectToRectF(pixel_refs
[1].pixel_ref_rect
));
385 VerifyScales(1.f
, 1.f
, pixel_refs
[1].matrix
, __LINE__
);
386 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[1].filter_quality
);
387 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
388 gfx::SkRectToRectF(pixel_refs
[2].pixel_ref_rect
));
389 VerifyScales(1.f
, 1.f
, pixel_refs
[2].matrix
, __LINE__
);
390 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[2].filter_quality
);
393 TEST(PixelRefUtilsTest
, DrawPath
) {
394 gfx::Rect
layer_rect(0, 0, 256, 256);
396 SkPictureRecorder recorder
;
397 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
399 TestDiscardableShader first_shader
;
401 first_paint
.setShader(&first_shader
);
403 TestDiscardableShader second_shader
;
404 SkPaint second_paint
;
405 second_paint
.setShader(&second_shader
);
410 path
.lineTo(22, 101);
413 canvas
->drawPath(path
, first_paint
);
416 canvas
->clipRect(SkRect::MakeWH(50, 50));
419 canvas
->drawPath(path
, second_paint
);
423 skia::RefPtr
<SkPicture
> picture
=
424 skia::AdoptRef(StopRecording(&recorder
, canvas
));
426 std::vector
<skia::PixelRefUtils::PositionPixelRef
> pixel_refs
;
427 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
.get(), &pixel_refs
);
429 EXPECT_EQ(2u, pixel_refs
.size());
430 EXPECT_FLOAT_RECT_EQ(gfx::RectF(12, 13, 38, 88),
431 gfx::SkRectToRectF(pixel_refs
[0].pixel_ref_rect
));
432 VerifyScales(1.f
, 1.f
, pixel_refs
[0].matrix
, __LINE__
);
433 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[0].filter_quality
);
434 EXPECT_FLOAT_RECT_EQ(gfx::RectF(12, 13, 38, 37),
435 gfx::SkRectToRectF(pixel_refs
[1].pixel_ref_rect
));
436 VerifyScales(1.f
, 1.f
, pixel_refs
[1].matrix
, __LINE__
);
437 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[1].filter_quality
);
440 TEST(PixelRefUtilsTest
, DrawBitmap
) {
441 gfx::Rect
layer_rect(0, 0, 256, 256);
443 SkPictureRecorder recorder
;
444 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
447 CreateBitmap(gfx::Size(50, 50), "discardable", &first
);
449 CreateBitmap(gfx::Size(50, 50), "discardable", &second
);
451 CreateBitmap(gfx::Size(50, 50), "discardable", &third
);
453 CreateBitmap(gfx::Size(50, 1), "discardable", &fourth
);
455 CreateBitmap(gfx::Size(10, 10), "discardable", &fifth
);
457 CreateBitmap(gfx::Size(10, 10), "discardable", &sixth
);
462 canvas
->drawBitmap(first
, 0, 0);
463 canvas
->translate(25, 0);
465 canvas
->drawBitmap(second
, 0, 0);
466 canvas
->translate(0, 50);
468 canvas
->drawBitmap(third
, 25, 0);
473 canvas
->translate(1, 0);
475 // At (1, 0), rotated 90 degrees
476 canvas
->drawBitmap(fourth
, 0, 0);
481 canvas
->scale(5.f
, 6.f
);
482 // At (0, 0), scaled by 5 and 6
483 canvas
->drawBitmap(fifth
, 0, 0);
488 canvas
->scale(3.3f
, 0.4f
);
490 canvas
->drawBitmap(sixth
, 0, 0);
494 skia::RefPtr
<SkPicture
> picture
=
495 skia::AdoptRef(StopRecording(&recorder
, canvas
));
497 std::vector
<skia::PixelRefUtils::PositionPixelRef
> pixel_refs
;
498 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
.get(), &pixel_refs
);
500 EXPECT_EQ(6u, pixel_refs
.size());
501 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
502 gfx::SkRectToRectF(pixel_refs
[0].pixel_ref_rect
));
503 VerifyScales(1.f
, 1.f
, pixel_refs
[0].matrix
, __LINE__
);
504 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[0].filter_quality
);
505 EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50),
506 gfx::SkRectToRectF(pixel_refs
[1].pixel_ref_rect
));
507 VerifyScales(1.f
, 1.f
, pixel_refs
[1].matrix
, __LINE__
);
508 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[1].filter_quality
);
509 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
510 gfx::SkRectToRectF(pixel_refs
[2].pixel_ref_rect
));
511 VerifyScales(1.f
, 1.f
, pixel_refs
[2].matrix
, __LINE__
);
512 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[2].filter_quality
);
513 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 1, 50),
514 gfx::SkRectToRectF(pixel_refs
[3].pixel_ref_rect
));
515 VerifyScales(1.f
, 1.f
, pixel_refs
[3].matrix
, __LINE__
);
516 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[3].filter_quality
);
517 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 60),
518 gfx::SkRectToRectF(pixel_refs
[4].pixel_ref_rect
));
519 VerifyScales(5.f
, 6.f
, pixel_refs
[4].matrix
, __LINE__
);
520 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[4].filter_quality
);
521 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 29.403214f
, 18.545712f
),
522 gfx::SkRectToRectF(pixel_refs
[5].pixel_ref_rect
));
523 VerifyScales(3.3f
, 0.4f
, pixel_refs
[5].matrix
, __LINE__
);
524 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[5].filter_quality
);
527 TEST(PixelRefUtilsTest
, DrawBitmapRect
) {
528 gfx::Rect
layer_rect(0, 0, 256, 256);
530 SkPictureRecorder recorder
;
531 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
534 CreateBitmap(gfx::Size(50, 50), "discardable", &first
);
536 CreateBitmap(gfx::Size(50, 50), "discardable", &second
);
538 CreateBitmap(gfx::Size(50, 50), "discardable", &third
);
540 TestDiscardableShader first_shader
;
542 first_paint
.setShader(&first_shader
);
544 SkPaint non_discardable_paint
;
549 canvas
->drawBitmapRect(
550 first
, SkRect::MakeWH(100, 100), &non_discardable_paint
);
551 canvas
->translate(25, 0);
553 canvas
->drawBitmapRect(
554 second
, SkRect::MakeXYWH(50, 50, 10, 10), &non_discardable_paint
);
555 canvas
->translate(5, 50);
556 // (0, 30, 100, 100). One from bitmap, one from paint.
557 canvas
->drawBitmapRect(
558 third
, SkRect::MakeXYWH(-30, -20, 100, 100), &first_paint
);
562 skia::RefPtr
<SkPicture
> picture
=
563 skia::AdoptRef(StopRecording(&recorder
, canvas
));
565 std::vector
<skia::PixelRefUtils::PositionPixelRef
> pixel_refs
;
566 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
.get(), &pixel_refs
);
568 EXPECT_EQ(4u, pixel_refs
.size());
569 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 100, 100),
570 gfx::SkRectToRectF(pixel_refs
[0].pixel_ref_rect
));
571 VerifyScales(2.f
, 2.f
, pixel_refs
[0].matrix
, __LINE__
);
572 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[0].filter_quality
);
573 EXPECT_FLOAT_RECT_EQ(gfx::RectF(75, 50, 10, 10),
574 gfx::SkRectToRectF(pixel_refs
[1].pixel_ref_rect
));
575 VerifyScales(0.2f
, 0.2f
, pixel_refs
[1].matrix
, __LINE__
);
576 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[1].filter_quality
);
577 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100),
578 gfx::SkRectToRectF(pixel_refs
[2].pixel_ref_rect
));
579 VerifyScales(2.f
, 2.f
, pixel_refs
[2].matrix
, __LINE__
);
580 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[2].filter_quality
);
581 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100),
582 gfx::SkRectToRectF(pixel_refs
[3].pixel_ref_rect
));
583 VerifyScales(2.f
, 2.f
, pixel_refs
[3].matrix
, __LINE__
);
584 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[3].filter_quality
);
587 TEST(PixelRefUtilsTest
, DrawSprite
) {
588 gfx::Rect
layer_rect(0, 0, 256, 256);
590 SkPictureRecorder recorder
;
591 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
594 CreateBitmap(gfx::Size(50, 50), "discardable", &first
);
596 CreateBitmap(gfx::Size(50, 50), "discardable", &second
);
598 CreateBitmap(gfx::Size(50, 50), "discardable", &third
);
600 CreateBitmap(gfx::Size(50, 50), "discardable", &fourth
);
602 CreateBitmap(gfx::Size(50, 50), "discardable", &fifth
);
606 // Sprites aren't affected by the current matrix.
609 canvas
->drawSprite(first
, 0, 0);
610 canvas
->translate(25, 0);
612 canvas
->drawSprite(second
, 10, 0);
613 canvas
->translate(0, 50);
615 canvas
->drawSprite(third
, 25, 0);
622 canvas
->drawSprite(fourth
, 0, 0);
626 TestDiscardableShader first_shader
;
628 first_paint
.setShader(&first_shader
);
630 canvas
->scale(5.f
, 6.f
);
631 // (100, 100, 50, 50).
632 canvas
->drawSprite(fifth
, 100, 100, &first_paint
);
634 skia::RefPtr
<SkPicture
> picture
=
635 skia::AdoptRef(StopRecording(&recorder
, canvas
));
637 std::vector
<skia::PixelRefUtils::PositionPixelRef
> pixel_refs
;
638 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
.get(), &pixel_refs
);
640 EXPECT_EQ(6u, pixel_refs
.size());
641 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
642 gfx::SkRectToRectF(pixel_refs
[0].pixel_ref_rect
));
643 VerifyScales(1.f
, 1.f
, pixel_refs
[0].matrix
, __LINE__
);
644 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[0].filter_quality
);
645 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 0, 50, 50),
646 gfx::SkRectToRectF(pixel_refs
[1].pixel_ref_rect
));
647 VerifyScales(1.f
, 1.f
, pixel_refs
[1].matrix
, __LINE__
);
648 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[1].filter_quality
);
649 EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50),
650 gfx::SkRectToRectF(pixel_refs
[2].pixel_ref_rect
));
651 VerifyScales(1.f
, 1.f
, pixel_refs
[2].matrix
, __LINE__
);
652 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[2].filter_quality
);
653 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
654 gfx::SkRectToRectF(pixel_refs
[3].pixel_ref_rect
));
655 VerifyScales(1.f
, 1.f
, pixel_refs
[3].matrix
, __LINE__
);
656 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[3].filter_quality
);
657 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 50, 50),
658 gfx::SkRectToRectF(pixel_refs
[4].pixel_ref_rect
));
659 VerifyScales(1.f
, 1.f
, pixel_refs
[4].matrix
, __LINE__
);
660 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[4].filter_quality
);
661 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 50, 50),
662 gfx::SkRectToRectF(pixel_refs
[5].pixel_ref_rect
));
663 VerifyScales(1.f
, 1.f
, pixel_refs
[5].matrix
, __LINE__
);
664 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[5].filter_quality
);
667 TEST(PixelRefUtilsTest
, DrawText
) {
668 gfx::Rect
layer_rect(0, 0, 256, 256);
670 SkPictureRecorder recorder
;
671 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
673 TestDiscardableShader first_shader
;
675 first_paint
.setShader(&first_shader
);
678 points
[0].set(10, 50);
679 points
[1].set(20, 50);
680 points
[2].set(30, 50);
681 points
[3].set(40, 50);
690 canvas
->drawText("text", 4, 50, 50, first_paint
);
691 canvas
->drawPosText("text", 4, points
, first_paint
);
692 canvas
->drawTextOnPath("text", 4, path
, NULL
, first_paint
);
694 skia::RefPtr
<SkPicture
> picture
=
695 skia::AdoptRef(StopRecording(&recorder
, canvas
));
697 std::vector
<skia::PixelRefUtils::PositionPixelRef
> pixel_refs
;
698 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
.get(), &pixel_refs
);
700 EXPECT_EQ(3u, pixel_refs
.size());
703 TEST(PixelRefUtilsTest
, DrawVertices
) {
704 gfx::Rect
layer_rect(0, 0, 256, 256);
706 SkPictureRecorder recorder
;
707 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
709 TestDiscardableShader first_shader
;
711 first_paint
.setShader(&first_shader
);
713 TestDiscardableShader second_shader
;
714 SkPaint second_paint
;
715 second_paint
.setShader(&second_shader
);
717 TestDiscardableShader third_shader
;
719 third_paint
.setShader(&third_shader
);
723 uint16_t indecies
[3] = {0, 1, 2};
724 points
[0].set(10, 10);
725 points
[1].set(100, 20);
726 points
[2].set(50, 100);
728 canvas
->drawVertices(SkCanvas::kTriangles_VertexMode
,
740 canvas
->clipRect(SkRect::MakeWH(50, 50));
742 canvas
->drawVertices(SkCanvas::kTriangles_VertexMode
,
754 points
[0].set(50, 55);
755 points
[1].set(50, 55);
756 points
[2].set(200, 200);
757 // (50, 55, 150, 145).
758 canvas
->drawVertices(SkCanvas::kTriangles_VertexMode
,
768 skia::RefPtr
<SkPicture
> picture
=
769 skia::AdoptRef(StopRecording(&recorder
, canvas
));
771 std::vector
<skia::PixelRefUtils::PositionPixelRef
> pixel_refs
;
772 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
.get(), &pixel_refs
);
774 EXPECT_EQ(3u, pixel_refs
.size());
775 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
776 gfx::SkRectToRectF(pixel_refs
[0].pixel_ref_rect
));
777 VerifyScales(1.f
, 1.f
, pixel_refs
[0].matrix
, __LINE__
);
778 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[0].filter_quality
);
779 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 40, 40),
780 gfx::SkRectToRectF(pixel_refs
[1].pixel_ref_rect
));
781 VerifyScales(1.f
, 1.f
, pixel_refs
[1].matrix
, __LINE__
);
782 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[1].filter_quality
);
783 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145),
784 gfx::SkRectToRectF(pixel_refs
[2].pixel_ref_rect
));
785 VerifyScales(1.f
, 1.f
, pixel_refs
[2].matrix
, __LINE__
);
786 EXPECT_EQ(kNone_SkFilterQuality
, pixel_refs
[2].filter_quality
);