1 /*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "CanvasStateHelpers.h"
9 #include "SkBitmap.h"
10 #include "SkCanvas.h"
11 #include "SkClipOpPriv.h"
12 #include "SkCanvasStateUtils.h"
13 #include "SkCommandLineFlags.h"
14 #include "SkDrawFilter.h"
15 #include "SkPaint.h"
16 #include "SkRegion.h"
17 #include "SkRRect.h"
18 #include "SkRect.h"
19 #include "SkTLazy.h"
20 #include "Test.h"
21
22 // dlopen and the library flag are only used for tests which require this flag.
23 #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
24 #include <dlfcn.h>
25
26 DEFINE_string(library, "", "Support library to use for CanvasState test. If empty (the default), "
27 "the test will be run without crossing a library boundary. Otherwise, "
28 "it is expected to be a full path to a shared library file, which will"
29 " be dynamically loaded. Functions from the library will be called to "
30 "test SkCanvasState. Instructions for generating the library are in "
31 "gyp/canvas_state_lib.gyp");
32
33
34 // This class calls dlopen on the library passed in to the command line flag library, and handles
35 // calling dlclose when it goes out of scope.
36 class OpenLibResult {
37 public:
38 // If the flag library was passed to this run of the test, attempt to open it using dlopen and
39 // report whether it succeeded.
OpenLibResult(skiatest::Reporter * reporter)40 OpenLibResult(skiatest::Reporter* reporter) {
41 if (FLAGS_library.count() == 1) {
42 fHandle = dlopen(FLAGS_library[0], RTLD_LAZY | RTLD_LOCAL);
43 REPORTER_ASSERT_MESSAGE(reporter, fHandle != nullptr, "Failed to open library!");
44 } else {
45 fHandle = nullptr;
46 }
47 }
48
49 // Automatically call dlclose when going out of scope.
~OpenLibResult()50 ~OpenLibResult() {
51 if (fHandle) {
52 dlclose(fHandle);
53 }
54 }
55
56 // Pointer to the shared library object.
handle()57 void* handle() { return fHandle; }
58
59 private:
60 void* fHandle;
61 };
62
DEF_TEST(CanvasState_test_complex_layers,reporter)63 DEF_TEST(CanvasState_test_complex_layers, reporter) {
64 const int WIDTH = 400;
65 const int HEIGHT = 400;
66 const int SPACER = 10;
67
68 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(SPACER), SkIntToScalar(SPACER),
69 SkIntToScalar(WIDTH-(2*SPACER)),
70 SkIntToScalar((HEIGHT-(2*SPACER)) / 7));
71
72 const SkColorType colorTypes[] = {
73 kRGB_565_SkColorType, kN32_SkColorType
74 };
75
76 const int layerAlpha[] = { 255, 255, 0 };
77 const SkCanvas::SaveLayerFlags flags[] = {
78 static_cast<SkCanvas::SaveLayerFlags>(SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag),
79 0,
80 static_cast<SkCanvas::SaveLayerFlags>(SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag),
81 };
82 REPORTER_ASSERT(reporter, sizeof(layerAlpha) == sizeof(flags));
83
84 bool (*drawFn)(SkCanvasState* state, float l, float t,
85 float r, float b, int32_t s);
86
87 OpenLibResult openLibResult(reporter);
88 if (openLibResult.handle() != nullptr) {
89 *(void**) (&drawFn) = dlsym(openLibResult.handle(),
90 "complex_layers_draw_from_canvas_state");
91 } else {
92 drawFn = complex_layers_draw_from_canvas_state;
93 }
94
95 REPORTER_ASSERT(reporter, drawFn);
96 if (!drawFn) {
97 return;
98 }
99
100 for (size_t i = 0; i < SK_ARRAY_COUNT(colorTypes); ++i) {
101 SkBitmap bitmaps[2];
102 for (int j = 0; j < 2; ++j) {
103 bitmaps[j].allocPixels(SkImageInfo::Make(WIDTH, HEIGHT,
104 colorTypes[i],
105 kPremul_SkAlphaType));
106
107 SkCanvas canvas(bitmaps[j]);
108
109 canvas.drawColor(SK_ColorRED);
110
111 for (size_t k = 0; k < SK_ARRAY_COUNT(layerAlpha); ++k) {
112 SkTLazy<SkPaint> paint;
113 if (layerAlpha[k] != 0xFF) {
114 paint.init()->setAlpha(layerAlpha[k]);
115 }
116
117 // draw a rect within the layer's bounds and again outside the layer's bounds
118 canvas.saveLayer(SkCanvas::SaveLayerRec(&rect, paint.getMaybeNull(), flags[k]));
119
120 if (j) {
121 // Capture from the first Skia.
122 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
123 REPORTER_ASSERT(reporter, state);
124
125 // And draw to it in the second Skia.
126 bool success = complex_layers_draw_from_canvas_state(state,
127 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, SPACER);
128 REPORTER_ASSERT(reporter, success);
129
130 // And release it in the *first* Skia.
131 SkCanvasStateUtils::ReleaseCanvasState(state);
132 } else {
133 // Draw in the first Skia.
134 complex_layers_draw(&canvas, rect.fLeft, rect.fTop,
135 rect.fRight, rect.fBottom, SPACER);
136 }
137
138 canvas.restore();
139
140 // translate the canvas for the next iteration
141 canvas.translate(0, 2*(rect.height() + SPACER));
142 }
143 }
144
145 // now we memcmp the two bitmaps
146 REPORTER_ASSERT(reporter, bitmaps[0].getSize() == bitmaps[1].getSize());
147 REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(),
148 bitmaps[1].getPixels(),
149 bitmaps[0].getSize()));
150 }
151 }
152 #endif
153
154 ////////////////////////////////////////////////////////////////////////////////
155
156 #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
DEF_TEST(CanvasState_test_complex_clips,reporter)157 DEF_TEST(CanvasState_test_complex_clips, reporter) {
158 const int WIDTH = 400;
159 const int HEIGHT = 400;
160 const int SPACER = 10;
161
162 SkIRect layerRect = SkIRect::MakeWH(WIDTH, HEIGHT / 4);
163 layerRect.inset(2*SPACER, 2*SPACER);
164
165 SkIRect clipRect = layerRect;
166 clipRect.fRight = clipRect.fLeft + (clipRect.width() / 2) - (2*SPACER);
167 clipRect.outset(SPACER, SPACER);
168
169 SkIRect regionBounds = clipRect;
170 regionBounds.offset(clipRect.width() + (2*SPACER), 0);
171
172 SkIRect regionInterior = regionBounds;
173 regionInterior.inset(SPACER*3, SPACER*3);
174
175 SkRegion clipRegion;
176 clipRegion.setRect(regionBounds);
177 clipRegion.op(regionInterior, SkRegion::kDifference_Op);
178
179
180 const SkRegion::Op clipOps[] = { SkRegion::kIntersect_Op,
181 SkRegion::kIntersect_Op,
182 SkRegion::kReplace_Op,
183 };
184 const SkCanvas::SaveLayerFlags flags[] = {
185 static_cast<SkCanvas::SaveLayerFlags>(SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag),
186 0,
187 static_cast<SkCanvas::SaveLayerFlags>(SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag),
188 };
189 REPORTER_ASSERT(reporter, sizeof(clipOps) == sizeof(flags));
190
191 bool (*drawFn)(SkCanvasState* state, int32_t l, int32_t t,
192 int32_t r, int32_t b, int32_t clipOp,
193 int32_t regionRects, int32_t* rectCoords);
194
195 OpenLibResult openLibResult(reporter);
196 if (openLibResult.handle() != nullptr) {
197 *(void**) (&drawFn) = dlsym(openLibResult.handle(),
198 "complex_clips_draw_from_canvas_state");
199 } else {
200 drawFn = complex_clips_draw_from_canvas_state;
201 }
202
203 REPORTER_ASSERT(reporter, drawFn);
204 if (!drawFn) {
205 return;
206 }
207
208 SkBitmap bitmaps[2];
209 for (int i = 0; i < 2; ++i) {
210 bitmaps[i].allocN32Pixels(WIDTH, HEIGHT);
211
212 SkCanvas canvas(bitmaps[i]);
213
214 canvas.drawColor(SK_ColorRED);
215
216 SkRegion localRegion = clipRegion;
217
218 SkPaint paint;
219 paint.setAlpha(128);
220 for (size_t j = 0; j < SK_ARRAY_COUNT(flags); ++j) {
221 SkRect layerBounds = SkRect::Make(layerRect);
222 canvas.saveLayer(SkCanvas::SaveLayerRec(&layerBounds, &paint, flags[j]));
223
224 if (i) {
225 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
226 REPORTER_ASSERT(reporter, state);
227
228 SkRegion::Iterator iter(localRegion);
229 SkTDArray<int32_t> rectCoords;
230 for (; !iter.done(); iter.next()) {
231 const SkIRect& rect = iter.rect();
232 *rectCoords.append() = rect.fLeft;
233 *rectCoords.append() = rect.fTop;
234 *rectCoords.append() = rect.fRight;
235 *rectCoords.append() = rect.fBottom;
236 }
237 bool success = drawFn(state, clipRect.fLeft, clipRect.fTop,
238 clipRect.fRight, clipRect.fBottom, clipOps[j],
239 rectCoords.count() / 4, rectCoords.begin());
240 REPORTER_ASSERT(reporter, success);
241
242 SkCanvasStateUtils::ReleaseCanvasState(state);
243 } else {
244 complex_clips_draw(&canvas, clipRect.fLeft, clipRect.fTop,
245 clipRect.fRight, clipRect.fBottom, clipOps[j],
246 localRegion);
247 }
248
249 canvas.restore();
250
251 // translate the canvas and region for the next iteration
252 canvas.translate(0, SkIntToScalar(2*(layerRect.height() + (SPACER))));
253 localRegion.translate(0, 2*(layerRect.height() + SPACER));
254 }
255 }
256
257 // now we memcmp the two bitmaps
258 REPORTER_ASSERT(reporter, bitmaps[0].getSize() == bitmaps[1].getSize());
259 REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(),
260 bitmaps[1].getPixels(),
261 bitmaps[0].getSize()));
262 }
263 #endif
264
265 ////////////////////////////////////////////////////////////////////////////////
266
267 #ifdef SK_SUPPORT_LEGACY_DRAWFILTER
268
269 class TestDrawFilter : public SkDrawFilter {
270 public:
filter(SkPaint *,Type)271 bool filter(SkPaint*, Type) override { return true; }
272 };
273
DEF_TEST(CanvasState_test_draw_filters,reporter)274 DEF_TEST(CanvasState_test_draw_filters, reporter) {
275 TestDrawFilter drawFilter;
276 SkBitmap bitmap;
277 bitmap.allocN32Pixels(10, 10);
278 SkCanvas canvas(bitmap);
279
280 canvas.setDrawFilter(&drawFilter);
281
282 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
283 REPORTER_ASSERT(reporter, state);
284 std::unique_ptr<SkCanvas> tmpCanvas = SkCanvasStateUtils::MakeFromCanvasState(state);
285 REPORTER_ASSERT(reporter, tmpCanvas);
286
287 REPORTER_ASSERT(reporter, canvas.getDrawFilter());
288 REPORTER_ASSERT(reporter, nullptr == tmpCanvas->getDrawFilter());
289
290 SkCanvasStateUtils::ReleaseCanvasState(state);
291 }
292
293 #endif
294
295 ////////////////////////////////////////////////////////////////////////////////
296
DEF_TEST(CanvasState_test_soft_clips,reporter)297 DEF_TEST(CanvasState_test_soft_clips, reporter) {
298 SkBitmap bitmap;
299 bitmap.allocN32Pixels(10, 10);
300 SkCanvas canvas(bitmap);
301
302 SkRRect roundRect;
303 roundRect.setOval(SkRect::MakeWH(5, 5));
304
305 canvas.clipRRect(roundRect, kIntersect_SkClipOp, true);
306
307 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
308 REPORTER_ASSERT(reporter, !state);
309 }
310
DEF_TEST(CanvasState_test_saveLayer_clip,reporter)311 DEF_TEST(CanvasState_test_saveLayer_clip, reporter) {
312 const uint32_t dontSaveFlag = 1 << 31; // secret flag for don't save
313 #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
314 static_assert(SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag == dontSaveFlag, "");
315 #endif
316 const int WIDTH = 100;
317 const int HEIGHT = 100;
318 const int LAYER_WIDTH = 50;
319 const int LAYER_HEIGHT = 50;
320
321 SkBitmap bitmap;
322 bitmap.allocN32Pixels(WIDTH, HEIGHT);
323 SkCanvas canvas(bitmap);
324
325 SkRect bounds = SkRect::MakeWH(SkIntToScalar(LAYER_WIDTH), SkIntToScalar(LAYER_HEIGHT));
326 canvas.clipRect(SkRect::MakeWH(SkIntToScalar(WIDTH), SkIntToScalar(HEIGHT)));
327
328 SkIRect devClip;
329 // Check that saveLayer without the kClipToLayer_SaveFlag leaves the clip unchanged.
330 canvas.saveLayer(SkCanvas::SaveLayerRec(&bounds, nullptr, dontSaveFlag));
331 devClip = canvas.getDeviceClipBounds();
332 REPORTER_ASSERT(reporter, canvas.isClipRect());
333 REPORTER_ASSERT(reporter, devClip.width() == WIDTH);
334 REPORTER_ASSERT(reporter, devClip.height() == HEIGHT);
335 canvas.restore();
336
337 // Check that saveLayer with the kClipToLayer_SaveFlag sets the clip
338 // stack to the layer bounds.
339 canvas.saveLayer(&bounds, nullptr);
340 devClip = canvas.getDeviceClipBounds();
341 REPORTER_ASSERT(reporter, canvas.isClipRect());
342 REPORTER_ASSERT(reporter, devClip.width() == LAYER_WIDTH);
343 REPORTER_ASSERT(reporter, devClip.height() == LAYER_HEIGHT);
344 canvas.restore();
345 }
346