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/discardable_image_utils.h"
9 #include "skia/ext/refptr.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/skia/include/core/SkCanvas.h"
12 #include "third_party/skia/include/core/SkImage.h"
13 #include "third_party/skia/include/core/SkImageGenerator.h"
14 #include "third_party/skia/include/core/SkPictureRecorder.h"
15 #include "third_party/skia/src/core/SkOrderedReadBuffer.h"
16 #include "ui/gfx/geometry/rect.h"
17 #include "ui/gfx/skia_util.h"
23 class TestImageGenerator
: public SkImageGenerator
{
25 TestImageGenerator(const SkImageInfo
& info
) : SkImageGenerator(info
) {}
28 skia::RefPtr
<SkImage
> CreateDiscardableImage(const gfx::Size
& size
) {
29 const SkImageInfo info
=
30 SkImageInfo::MakeN32Premul(size
.width(), size
.height());
31 return skia::AdoptRef(
32 SkImage::NewFromGenerator(new TestImageGenerator(info
)));
35 SkCanvas
* StartRecording(SkPictureRecorder
* recorder
, gfx::Rect layer_rect
) {
37 recorder
->beginRecording(layer_rect
.width(), layer_rect
.height());
40 canvas
->translate(-layer_rect
.x(), -layer_rect
.y());
41 canvas
->clipRect(SkRect::MakeXYWH(layer_rect
.x(), layer_rect
.y(),
42 layer_rect
.width(), layer_rect
.height()));
47 SkPicture
* StopRecording(SkPictureRecorder
* recorder
, SkCanvas
* canvas
) {
49 return recorder
->endRecordingAsPicture();
52 void VerifyScales(SkScalar x_scale
,
54 const SkMatrix
& matrix
,
57 bool success
= matrix
.decomposeScale(&scales
);
58 EXPECT_TRUE(success
) << "line: " << source_line
;
59 EXPECT_FLOAT_EQ(x_scale
, scales
.width()) << "line: " << source_line
;
60 EXPECT_FLOAT_EQ(y_scale
, scales
.height()) << "line: " << source_line
;
65 TEST(DiscardableImageUtilsTest
, DrawImage
) {
66 gfx::Rect
layer_rect(0, 0, 256, 256);
68 SkPictureRecorder recorder
;
69 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
71 skia::RefPtr
<SkImage
> first
= CreateDiscardableImage(gfx::Size(50, 50));
72 skia::RefPtr
<SkImage
> second
= CreateDiscardableImage(gfx::Size(50, 50));
73 skia::RefPtr
<SkImage
> third
= CreateDiscardableImage(gfx::Size(50, 50));
74 skia::RefPtr
<SkImage
> fourth
= CreateDiscardableImage(gfx::Size(50, 1));
75 skia::RefPtr
<SkImage
> fifth
= CreateDiscardableImage(gfx::Size(10, 10));
76 skia::RefPtr
<SkImage
> sixth
= CreateDiscardableImage(gfx::Size(10, 10));
81 canvas
->drawImage(first
.get(), 0, 0);
82 canvas
->translate(25, 0);
84 canvas
->drawImage(second
.get(), 0, 0);
85 canvas
->translate(0, 50);
87 canvas
->drawImage(third
.get(), 25, 0);
92 canvas
->translate(1, 0);
94 // At (1, 0), rotated 90 degrees
95 canvas
->drawImage(fourth
.get(), 0, 0);
100 canvas
->scale(5.f
, 6.f
);
101 // At (0, 0), scaled by 5 and 6
102 canvas
->drawImage(fifth
.get(), 0, 0);
107 canvas
->scale(3.3f
, 0.4f
);
109 canvas
->drawImage(sixth
.get(), 0, 0);
113 skia::RefPtr
<SkPicture
> picture
=
114 skia::AdoptRef(StopRecording(&recorder
, canvas
));
116 std::vector
<skia::DiscardableImageUtils::PositionImage
> images
;
117 skia::DiscardableImageUtils::GatherDiscardableImages(picture
.get(), &images
);
119 EXPECT_EQ(6u, images
.size());
120 EXPECT_FLOAT_RECT_EQ(SkRect::MakeXYWH(0, 0, 50, 50), images
[0].image_rect
);
121 VerifyScales(1.f
, 1.f
, images
[0].matrix
, __LINE__
);
122 EXPECT_EQ(kNone_SkFilterQuality
, images
[0].filter_quality
);
123 EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50), images
[1].image_rect
);
124 VerifyScales(1.f
, 1.f
, images
[1].matrix
, __LINE__
);
125 EXPECT_EQ(kNone_SkFilterQuality
, images
[1].filter_quality
);
126 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50), images
[2].image_rect
);
127 VerifyScales(1.f
, 1.f
, images
[2].matrix
, __LINE__
);
128 EXPECT_EQ(kNone_SkFilterQuality
, images
[2].filter_quality
);
129 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 1, 50), images
[3].image_rect
);
130 VerifyScales(1.f
, 1.f
, images
[3].matrix
, __LINE__
);
131 EXPECT_EQ(kNone_SkFilterQuality
, images
[3].filter_quality
);
132 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 60), images
[4].image_rect
);
133 VerifyScales(5.f
, 6.f
, images
[4].matrix
, __LINE__
);
134 EXPECT_EQ(kNone_SkFilterQuality
, images
[4].filter_quality
);
135 EXPECT_FLOAT_RECT_EQ(gfx::RectF(-1.8159621f
, 0, 31.219175f
, 18.545712f
),
136 images
[5].image_rect
);
137 VerifyScales(3.3f
, 0.4f
, images
[5].matrix
, __LINE__
);
138 EXPECT_EQ(kNone_SkFilterQuality
, images
[5].filter_quality
);
141 TEST(DiscardableImageUtilsTest
, DrawImageRect
) {
142 gfx::Rect
layer_rect(0, 0, 256, 256);
144 SkPictureRecorder recorder
;
145 SkCanvas
* canvas
= StartRecording(&recorder
, layer_rect
);
147 skia::RefPtr
<SkImage
> first
= CreateDiscardableImage(gfx::Size(50, 50));
148 skia::RefPtr
<SkImage
> second
= CreateDiscardableImage(gfx::Size(50, 50));
149 skia::RefPtr
<SkImage
> third
= CreateDiscardableImage(gfx::Size(50, 50));
156 canvas
->drawImageRect(first
.get(), SkRect::MakeWH(100, 100), &paint
);
157 canvas
->translate(25, 0);
159 canvas
->drawImageRect(second
.get(), SkRect::MakeXYWH(50, 50, 10, 10), &paint
);
160 canvas
->translate(5, 50);
161 // (0, 30, 100, 100).
162 canvas
->drawImageRect(third
.get(), SkRect::MakeXYWH(-30, -20, 100, 100),
167 skia::RefPtr
<SkPicture
> picture
=
168 skia::AdoptRef(StopRecording(&recorder
, canvas
));
170 std::vector
<skia::DiscardableImageUtils::PositionImage
> images
;
171 skia::DiscardableImageUtils::GatherDiscardableImages(picture
.get(), &images
);
173 EXPECT_EQ(3u, images
.size());
174 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 100, 100), images
[0].image_rect
);
175 VerifyScales(2.f
, 2.f
, images
[0].matrix
, __LINE__
);
176 EXPECT_EQ(kNone_SkFilterQuality
, images
[0].filter_quality
);
177 EXPECT_FLOAT_RECT_EQ(gfx::RectF(75, 50, 10, 10), images
[1].image_rect
);
178 VerifyScales(0.2f
, 0.2f
, images
[1].matrix
, __LINE__
);
179 EXPECT_EQ(kNone_SkFilterQuality
, images
[1].filter_quality
);
180 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100), images
[2].image_rect
);
181 VerifyScales(2.f
, 2.f
, images
[2].matrix
, __LINE__
);
182 EXPECT_EQ(kNone_SkFilterQuality
, images
[2].filter_quality
);