1 // Copyright 2013 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 "skia/ext/analysis_canvas.h"
7 #include "skia/ext/refptr.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "third_party/skia/include/core/SkPicture.h"
10 #include "third_party/skia/include/core/SkPictureRecorder.h"
11 #include "third_party/skia/include/core/SkShader.h"
12 #include "third_party/skia/include/effects/SkOffsetImageFilter.h"
16 void SolidColorFill(skia::AnalysisCanvas
& canvas
) {
17 canvas
.clear(SkColorSetARGB(255, 255, 255, 255));
20 void TransparentFill(skia::AnalysisCanvas
& canvas
) {
21 canvas
.clear(SkColorSetARGB(0, 0, 0, 0));
27 TEST(AnalysisCanvasTest
, EmptyCanvas
) {
28 skia::AnalysisCanvas
canvas(255, 255);
31 EXPECT_TRUE(canvas
.GetColorIfSolid(&color
));
32 EXPECT_EQ(color
, SkColorSetARGB(0, 0, 0, 0));
35 TEST(AnalysisCanvasTest
, ClearCanvas
) {
36 skia::AnalysisCanvas
canvas(255, 255);
39 SkColor color
= SkColorSetARGB(0, 12, 34, 56);
43 EXPECT_TRUE(canvas
.GetColorIfSolid(&outputColor
));
44 EXPECT_EQ(static_cast<SkColor
>(SK_ColorTRANSPARENT
), outputColor
);
47 color
= SkColorSetARGB(255, 65, 43, 21);
50 EXPECT_TRUE(canvas
.GetColorIfSolid(&outputColor
));
51 EXPECT_NE(static_cast<SkColor
>(SK_ColorTRANSPARENT
), outputColor
);
52 EXPECT_EQ(outputColor
, color
);
55 color
= SkColorSetARGB(128, 11, 22, 33);
58 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
60 // Test helper methods
61 SolidColorFill(canvas
);
62 EXPECT_TRUE(canvas
.GetColorIfSolid(&outputColor
));
63 EXPECT_NE(static_cast<SkColor
>(SK_ColorTRANSPARENT
), outputColor
);
65 TransparentFill(canvas
);
66 EXPECT_TRUE(canvas
.GetColorIfSolid(&outputColor
));
67 EXPECT_EQ(static_cast<SkColor
>(SK_ColorTRANSPARENT
), outputColor
);
70 TEST(AnalysisCanvasTest
, ComplexActions
) {
71 skia::AnalysisCanvas
canvas(255, 255);
74 SkColor color
= SkColorSetARGB(255, 11, 22, 33);
76 paint
.setColor(color
);
78 canvas
.drawPaint(paint
);
81 EXPECT_TRUE(canvas
.GetColorIfSolid(&outputColor
));
86 SkPoint::Make(255, 0),
87 SkPoint::Make(255, 255),
91 SolidColorFill(canvas
);
92 canvas
.drawPoints(SkCanvas::kLines_PointMode
, 4, points
, paint
);
94 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
97 SolidColorFill(canvas
);
98 canvas
.drawOval(SkRect::MakeWH(255, 255), paint
);
100 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
103 SolidColorFill(canvas
);
104 SkBitmap secondBitmap
;
105 secondBitmap
.allocN32Pixels(255, 255);
106 canvas
.drawBitmap(secondBitmap
, 0, 0);
108 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
111 TEST(AnalysisCanvasTest
, SimpleDrawRect
) {
112 skia::AnalysisCanvas
canvas(255, 255);
114 SkColor color
= SkColorSetARGB(255, 11, 22, 33);
116 paint
.setColor(color
);
117 canvas
.clipRect(SkRect::MakeWH(255, 255));
118 canvas
.drawRect(SkRect::MakeWH(255, 255), paint
);
121 EXPECT_TRUE(canvas
.GetColorIfSolid(&outputColor
));
122 EXPECT_NE(static_cast<SkColor
>(SK_ColorTRANSPARENT
), outputColor
);
123 EXPECT_EQ(color
, outputColor
);
125 color
= SkColorSetARGB(255, 22, 33, 44);
126 paint
.setColor(color
);
127 canvas
.translate(-128, -128);
128 canvas
.drawRect(SkRect::MakeWH(382, 382), paint
);
130 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
132 color
= SkColorSetARGB(255, 33, 44, 55);
133 paint
.setColor(color
);
134 canvas
.drawRect(SkRect::MakeWH(383, 383), paint
);
136 EXPECT_TRUE(canvas
.GetColorIfSolid(&outputColor
));
137 EXPECT_NE(static_cast<SkColor
>(SK_ColorTRANSPARENT
), outputColor
);
138 EXPECT_EQ(color
, outputColor
);
140 color
= SkColorSetARGB(0, 0, 0, 0);
141 paint
.setColor(color
);
142 canvas
.drawRect(SkRect::MakeWH(383, 383), paint
);
144 // This test relies on canvas treating a paint with 0-color as a no-op
145 // thus not changing its "is_solid" status.
146 EXPECT_TRUE(canvas
.GetColorIfSolid(&outputColor
));
147 EXPECT_NE(static_cast<SkColor
>(SK_ColorTRANSPARENT
), outputColor
);
148 EXPECT_EQ(outputColor
, SkColorSetARGB(255, 33, 44, 55));
150 color
= SkColorSetARGB(128, 128, 128, 128);
151 paint
.setColor(color
);
152 canvas
.drawRect(SkRect::MakeWH(383, 383), paint
);
154 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
156 paint
.setXfermodeMode(SkXfermode::kClear_Mode
);
157 canvas
.drawRect(SkRect::MakeWH(382, 382), paint
);
159 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
161 canvas
.drawRect(SkRect::MakeWH(383, 383), paint
);
163 EXPECT_TRUE(canvas
.GetColorIfSolid(&outputColor
));
164 EXPECT_EQ(static_cast<SkColor
>(SK_ColorTRANSPARENT
), outputColor
);
166 canvas
.translate(128, 128);
167 color
= SkColorSetARGB(255, 11, 22, 33);
168 paint
.setColor(color
);
169 paint
.setXfermodeMode(SkXfermode::kSrcOver_Mode
);
170 canvas
.drawRect(SkRect::MakeWH(255, 255), paint
);
172 EXPECT_TRUE(canvas
.GetColorIfSolid(&outputColor
));
173 EXPECT_NE(static_cast<SkColor
>(SK_ColorTRANSPARENT
), outputColor
);
174 EXPECT_EQ(color
, outputColor
);
177 canvas
.drawRect(SkRect::MakeWH(255, 255), paint
);
179 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
182 TEST(AnalysisCanvasTest
, FilterPaint
) {
183 skia::AnalysisCanvas
canvas(255, 255);
186 skia::RefPtr
<SkImageFilter
> filter
= skia::AdoptRef(SkOffsetImageFilter::Create(10, 10));
187 paint
.setImageFilter(filter
.get());
188 canvas
.drawRect(SkRect::MakeWH(255, 255), paint
);
191 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
194 TEST(AnalysisCanvasTest
, ClipPath
) {
195 skia::AnalysisCanvas
canvas(255, 255);
197 // Skia will look for paths that are actually rects and treat
198 // them as such. We add a divot to the following path to prevent
199 // this optimization and truly test clipPath's behavior.
202 path
.lineTo(128, 50);
204 path
.lineTo(255, 255);
208 SolidColorFill(canvas
);
209 canvas
.clipPath(path
);
210 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
213 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
215 canvas
.clipPath(path
);
216 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
219 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
221 SolidColorFill(canvas
);
222 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
225 TEST(AnalysisCanvasTest
, SaveLayerRestore
) {
226 skia::AnalysisCanvas
canvas(255, 255);
229 SolidColorFill(canvas
);
230 EXPECT_TRUE(canvas
.GetColorIfSolid(&outputColor
));
232 SkRect bounds
= SkRect::MakeWH(255, 255);
234 paint
.setColor(SkColorSetARGB(255, 255, 255, 255));
235 paint
.setXfermodeMode(SkXfermode::kSrcOver_Mode
);
237 // This should force non-transparency
238 canvas
.saveLayer(&bounds
, &paint
);
239 EXPECT_TRUE(canvas
.GetColorIfSolid(&outputColor
));
240 EXPECT_NE(static_cast<SkColor
>(SK_ColorTRANSPARENT
), outputColor
);
242 TransparentFill(canvas
);
243 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
245 SolidColorFill(canvas
);
246 EXPECT_TRUE(canvas
.GetColorIfSolid(&outputColor
));
247 EXPECT_NE(static_cast<SkColor
>(SK_ColorTRANSPARENT
), outputColor
);
249 paint
.setXfermodeMode(SkXfermode::kDst_Mode
);
251 // This should force non-solid color
252 canvas
.saveLayer(&bounds
, &paint
);
253 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
255 TransparentFill(canvas
);
256 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
258 SolidColorFill(canvas
);
259 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
262 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
264 TransparentFill(canvas
);
265 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
267 SolidColorFill(canvas
);
268 EXPECT_TRUE(canvas
.GetColorIfSolid(&outputColor
));
269 EXPECT_NE(static_cast<SkColor
>(SK_ColorTRANSPARENT
), outputColor
);
272 EXPECT_TRUE(canvas
.GetColorIfSolid(&outputColor
));
273 EXPECT_NE(static_cast<SkColor
>(SK_ColorTRANSPARENT
), outputColor
);
275 TransparentFill(canvas
);
276 EXPECT_TRUE(canvas
.GetColorIfSolid(&outputColor
));
277 EXPECT_EQ(static_cast<SkColor
>(SK_ColorTRANSPARENT
), outputColor
);
279 SolidColorFill(canvas
);
280 EXPECT_TRUE(canvas
.GetColorIfSolid(&outputColor
));
281 EXPECT_NE(static_cast<SkColor
>(SK_ColorTRANSPARENT
), outputColor
);
284 TEST(AnalysisCanvasTest
, EarlyOutNotSolid
) {
285 SkRTreeFactory factory
;
286 SkPictureRecorder recorder
;
288 // Create a picture with 3 commands, last of which is non-solid.
289 skia::RefPtr
<SkCanvas
> record_canvas
=
290 skia::SharePtr(recorder
.beginRecording(256, 256, &factory
));
292 std::string text
= "text";
293 SkPoint point
= SkPoint::Make(SkIntToScalar(25), SkIntToScalar(25));
296 paint
.setColor(SkColorSetARGB(255, 255, 255, 255));
297 paint
.setXfermodeMode(SkXfermode::kSrcOver_Mode
);
299 record_canvas
->drawRect(SkRect::MakeWH(256, 256), paint
);
300 record_canvas
->drawRect(SkRect::MakeWH(256, 256), paint
);
301 record_canvas
->drawText(
302 text
.c_str(), text
.length(), point
.fX
, point
.fY
, paint
);
304 skia::RefPtr
<SkPicture
> picture
= skia::AdoptRef(recorder
.endRecording());
306 // Draw the picture into the analysis canvas, using the canvas as a callback
308 skia::AnalysisCanvas
canvas(256, 256);
309 picture
->playback(&canvas
, &canvas
);
311 // Ensure that canvas is not solid.
312 SkColor output_color
;
313 EXPECT_FALSE(canvas
.GetColorIfSolid(&output_color
));
315 // Verify that we aborted drawing.
316 EXPECT_TRUE(canvas
.abortDrawing());
320 TEST(AnalysisCanvasTest
, ClipComplexRegion
) {
321 skia::AnalysisCanvas
canvas(255, 255);
325 path
.lineTo(128, 50);
327 path
.lineTo(255, 255);
329 SkIRect pathBounds
= path
.getBounds().round();
331 region
.setPath(path
, SkRegion(pathBounds
));
334 SolidColorFill(canvas
);
335 canvas
.clipRegion(region
);
336 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
339 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
341 canvas
.clipRegion(region
);
342 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
345 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));
347 SolidColorFill(canvas
);
348 EXPECT_FALSE(canvas
.GetColorIfSolid(&outputColor
));