blob: d0a19b6ba6518baf0b632213df9633bd8843381a [file] [log] [blame]
djsollen@google.com5587ac02013-08-29 20:20:40 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -04009#include "include/core/SkCanvas.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkColor.h"
11#include "include/core/SkImageInfo.h"
12#include "include/core/SkPaint.h"
13#include "include/core/SkRRect.h"
14#include "include/core/SkRect.h"
15#include "include/core/SkRegion.h"
16#include "include/core/SkScalar.h"
Mike Reeda5b07692019-09-03 13:23:43 -040017#include "include/core/SkSurface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/core/SkTypes.h"
19#include "include/private/SkTDArray.h"
20#include "include/utils/SkCanvasStateUtils.h"
21#include "src/core/SkCanvasPriv.h"
22#include "src/core/SkClipOpPriv.h"
23#include "src/core/SkTLazy.h"
24#include "tests/CanvasStateHelpers.h"
25#include "tests/Test.h"
26#include "tools/flags/CommandLineFlags.h"
djsollen@google.com5587ac02013-08-29 20:20:40 +000027
Ben Wagnerb607a8f2018-03-12 13:46:21 -040028#include <cstring>
Ben Wagnerb607a8f2018-03-12 13:46:21 -040029
30class SkCanvasState;
31
scroggo24519372014-07-22 12:38:55 -070032// dlopen and the library flag are only used for tests which require this flag.
reed@google.comb93ba452014-03-10 19:47:58 +000033#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
scroggo24519372014-07-22 12:38:55 -070034#include <dlfcn.h>
35
Mike Klein84836b72019-03-21 11:31:36 -050036static DEFINE_string(library, "",
37 "Support library to use for CanvasState test. If empty (the default), "
38 "the test will be run without crossing a library boundary. Otherwise, "
39 "it is expected to be a full path to a shared library file, which will"
40 " be dynamically loaded. Functions from the library will be called to "
41 "test SkCanvasState. Instructions for generating the library are in "
42 "gyp/canvas_state_lib.gyp");
scroggo24519372014-07-22 12:38:55 -070043
44
45// This class calls dlopen on the library passed in to the command line flag library, and handles
46// calling dlclose when it goes out of scope.
47class OpenLibResult {
48public:
49 // If the flag library was passed to this run of the test, attempt to open it using dlopen and
50 // report whether it succeeded.
51 OpenLibResult(skiatest::Reporter* reporter) {
52 if (FLAGS_library.count() == 1) {
53 fHandle = dlopen(FLAGS_library[0], RTLD_LAZY | RTLD_LOCAL);
Brian Salomon1c80e992018-01-29 09:50:47 -050054 REPORTER_ASSERT(reporter, fHandle != nullptr, "Failed to open library!");
scroggo24519372014-07-22 12:38:55 -070055 } else {
halcanary96fcdcc2015-08-27 07:41:13 -070056 fHandle = nullptr;
scroggo24519372014-07-22 12:38:55 -070057 }
58 }
59
60 // Automatically call dlclose when going out of scope.
61 ~OpenLibResult() {
62 if (fHandle) {
63 dlclose(fHandle);
64 }
65 }
66
67 // Pointer to the shared library object.
68 void* handle() { return fHandle; }
69
70private:
71 void* fHandle;
72};
73
Mike Reeda5b07692019-09-03 13:23:43 -040074static void write_image(const SkImage* img, const char path[]) {
75 auto data = img->encodeToData();
76 SkFILEWStream(path).write(data->data(), data->size());
77}
78
79static void compare(skiatest::Reporter* reporter, SkImage* img0, SkImage* img1) {
80 if (false) {
81 static int counter;
82
83 SkDebugf("---- counter %d\n", counter);
84 SkString name;
85 name.printf("no_capture_%d.png", counter);
86 write_image(img0, name.c_str());
87 name.printf("capture_%d.png", counter);
88 write_image(img1, name.c_str());
89 counter++;
90 }
91
92 SkPixmap pm[2];
93 REPORTER_ASSERT(reporter, img0->peekPixels(&pm[0]));
94 REPORTER_ASSERT(reporter, img1->peekPixels(&pm[1]));
95 // now we memcmp the two bitmaps
96 REPORTER_ASSERT(reporter, pm[0].computeByteSize() == pm[1].computeByteSize());
97 REPORTER_ASSERT(reporter, pm[0].rowBytes() == (size_t)pm[0].width() * pm[0].info().bytesPerPixel());
98 REPORTER_ASSERT(reporter, pm[1].rowBytes() == (size_t)pm[1].width() * pm[1].info().bytesPerPixel());
99 if (memcmp(pm[0].addr(0, 0), pm[1].addr(0, 0), pm[0].computeByteSize())) {
100 REPORTER_ASSERT(reporter, false);
101 }
102}
103
scroggo24519372014-07-22 12:38:55 -0700104DEF_TEST(CanvasState_test_complex_layers, reporter) {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000105 const int WIDTH = 400;
106 const int HEIGHT = 400;
107 const int SPACER = 10;
108
djsollen@google.com20146b32013-08-29 20:36:22 +0000109 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(SPACER), SkIntToScalar(SPACER),
110 SkIntToScalar(WIDTH-(2*SPACER)),
111 SkIntToScalar((HEIGHT-(2*SPACER)) / 7));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000112
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000113 const SkColorType colorTypes[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000114 kRGB_565_SkColorType, kN32_SkColorType
djsollen@google.com5587ac02013-08-29 20:20:40 +0000115 };
djsollen@google.com5587ac02013-08-29 20:20:40 +0000116
117 const int layerAlpha[] = { 255, 255, 0 };
fmalita55b29b22016-01-20 11:17:39 -0800118 const SkCanvas::SaveLayerFlags flags[] = {
Cary Clark7eddfb82018-03-13 14:41:10 -0400119 static_cast<SkCanvas::SaveLayerFlags>(SkCanvasPriv::kDontClipToLayer_SaveLayerFlag),
fmalita55b29b22016-01-20 11:17:39 -0800120 0,
Cary Clark7eddfb82018-03-13 14:41:10 -0400121 static_cast<SkCanvas::SaveLayerFlags>(SkCanvasPriv::kDontClipToLayer_SaveLayerFlag),
djsollen@google.com5587ac02013-08-29 20:20:40 +0000122 };
123 REPORTER_ASSERT(reporter, sizeof(layerAlpha) == sizeof(flags));
scroggo24519372014-07-22 12:38:55 -0700124
125 bool (*drawFn)(SkCanvasState* state, float l, float t,
126 float r, float b, int32_t s);
127
128 OpenLibResult openLibResult(reporter);
halcanary96fcdcc2015-08-27 07:41:13 -0700129 if (openLibResult.handle() != nullptr) {
scroggo24519372014-07-22 12:38:55 -0700130 *(void**) (&drawFn) = dlsym(openLibResult.handle(),
131 "complex_layers_draw_from_canvas_state");
132 } else {
133 drawFn = complex_layers_draw_from_canvas_state;
134 }
135
136 REPORTER_ASSERT(reporter, drawFn);
137 if (!drawFn) {
138 return;
139 }
djsollen@google.com5587ac02013-08-29 20:20:40 +0000140
scroggoecce60b2014-07-09 07:26:40 -0700141 for (size_t i = 0; i < SK_ARRAY_COUNT(colorTypes); ++i) {
Mike Reeda5b07692019-09-03 13:23:43 -0400142 sk_sp<SkImage> images[2];
djsollen@google.com5587ac02013-08-29 20:20:40 +0000143 for (int j = 0; j < 2; ++j) {
Mike Reeda5b07692019-09-03 13:23:43 -0400144 auto surf = SkSurface::MakeRaster(SkImageInfo::Make(WIDTH, HEIGHT,
145 colorTypes[i],
146 kPremul_SkAlphaType));
147 SkCanvas* canvas = surf->getCanvas();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000148
Mike Reeda5b07692019-09-03 13:23:43 -0400149 canvas->drawColor(SK_ColorRED);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000150
scroggo24519372014-07-22 12:38:55 -0700151 for (size_t k = 0; k < SK_ARRAY_COUNT(layerAlpha); ++k) {
fmalita55b29b22016-01-20 11:17:39 -0800152 SkTLazy<SkPaint> paint;
153 if (layerAlpha[k] != 0xFF) {
154 paint.init()->setAlpha(layerAlpha[k]);
155 }
156
djsollen@google.com5587ac02013-08-29 20:20:40 +0000157 // draw a rect within the layer's bounds and again outside the layer's bounds
Mike Reeda5b07692019-09-03 13:23:43 -0400158 canvas->saveLayer(SkCanvas::SaveLayerRec(&rect, paint.getMaybeNull(), flags[k]));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000159
djsollen@google.com5587ac02013-08-29 20:20:40 +0000160 if (j) {
scroggo24519372014-07-22 12:38:55 -0700161 // Capture from the first Skia.
Mike Reeda5b07692019-09-03 13:23:43 -0400162 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(canvas);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000163 REPORTER_ASSERT(reporter, state);
scroggo24519372014-07-22 12:38:55 -0700164
165 // And draw to it in the second Skia.
166 bool success = complex_layers_draw_from_canvas_state(state,
167 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, SPACER);
168 REPORTER_ASSERT(reporter, success);
169
170 // And release it in the *first* Skia.
171 SkCanvasStateUtils::ReleaseCanvasState(state);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000172 } else {
scroggo24519372014-07-22 12:38:55 -0700173 // Draw in the first Skia.
Mike Reeda5b07692019-09-03 13:23:43 -0400174 complex_layers_draw(canvas, rect.fLeft, rect.fTop,
scroggo24519372014-07-22 12:38:55 -0700175 rect.fRight, rect.fBottom, SPACER);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000176 }
177
Mike Reeda5b07692019-09-03 13:23:43 -0400178 canvas->restore();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000179
180 // translate the canvas for the next iteration
Mike Reeda5b07692019-09-03 13:23:43 -0400181 canvas->translate(0, 2*(rect.height() + SPACER));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000182 }
Mike Reeda5b07692019-09-03 13:23:43 -0400183 images[j] = surf->makeImageSnapshot();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000184 }
185
Mike Reeda5b07692019-09-03 13:23:43 -0400186 compare(reporter, images[0].get(), images[1].get());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000187 }
188}
scroggo24519372014-07-22 12:38:55 -0700189#endif
djsollen@google.com5587ac02013-08-29 20:20:40 +0000190
191////////////////////////////////////////////////////////////////////////////////
192
reed@google.comb93ba452014-03-10 19:47:58 +0000193#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
scroggo24519372014-07-22 12:38:55 -0700194DEF_TEST(CanvasState_test_complex_clips, reporter) {
djsollen@google.com339e79f2013-09-04 17:16:00 +0000195 const int WIDTH = 400;
196 const int HEIGHT = 400;
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000197 const int SPACER = 10;
djsollen@google.com339e79f2013-09-04 17:16:00 +0000198
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000199 SkIRect layerRect = SkIRect::MakeWH(WIDTH, HEIGHT / 4);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000200 layerRect.inset(2*SPACER, 2*SPACER);
201
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000202 SkIRect clipRect = layerRect;
djsollen@google.com339e79f2013-09-04 17:16:00 +0000203 clipRect.fRight = clipRect.fLeft + (clipRect.width() / 2) - (2*SPACER);
204 clipRect.outset(SPACER, SPACER);
205
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000206 SkIRect regionBounds = clipRect;
djsollen@google.com339e79f2013-09-04 17:16:00 +0000207 regionBounds.offset(clipRect.width() + (2*SPACER), 0);
208
209 SkIRect regionInterior = regionBounds;
210 regionInterior.inset(SPACER*3, SPACER*3);
211
212 SkRegion clipRegion;
213 clipRegion.setRect(regionBounds);
214 clipRegion.op(regionInterior, SkRegion::kDifference_Op);
215
216
217 const SkRegion::Op clipOps[] = { SkRegion::kIntersect_Op,
218 SkRegion::kIntersect_Op,
219 SkRegion::kReplace_Op,
220 };
fmalita55b29b22016-01-20 11:17:39 -0800221 const SkCanvas::SaveLayerFlags flags[] = {
Cary Clark7eddfb82018-03-13 14:41:10 -0400222 static_cast<SkCanvas::SaveLayerFlags>(SkCanvasPriv::kDontClipToLayer_SaveLayerFlag),
fmalita55b29b22016-01-20 11:17:39 -0800223 0,
Cary Clark7eddfb82018-03-13 14:41:10 -0400224 static_cast<SkCanvas::SaveLayerFlags>(SkCanvasPriv::kDontClipToLayer_SaveLayerFlag),
djsollen@google.com339e79f2013-09-04 17:16:00 +0000225 };
226 REPORTER_ASSERT(reporter, sizeof(clipOps) == sizeof(flags));
scroggo24519372014-07-22 12:38:55 -0700227
228 bool (*drawFn)(SkCanvasState* state, int32_t l, int32_t t,
229 int32_t r, int32_t b, int32_t clipOp,
230 int32_t regionRects, int32_t* rectCoords);
231
232 OpenLibResult openLibResult(reporter);
halcanary96fcdcc2015-08-27 07:41:13 -0700233 if (openLibResult.handle() != nullptr) {
scroggo24519372014-07-22 12:38:55 -0700234 *(void**) (&drawFn) = dlsym(openLibResult.handle(),
235 "complex_clips_draw_from_canvas_state");
236 } else {
237 drawFn = complex_clips_draw_from_canvas_state;
238 }
239
240 REPORTER_ASSERT(reporter, drawFn);
241 if (!drawFn) {
242 return;
243 }
djsollen@google.com339e79f2013-09-04 17:16:00 +0000244
Mike Reeda5b07692019-09-03 13:23:43 -0400245 sk_sp<SkImage> images[2];
djsollen@google.com339e79f2013-09-04 17:16:00 +0000246 for (int i = 0; i < 2; ++i) {
Mike Reeda5b07692019-09-03 13:23:43 -0400247 auto surf = SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(WIDTH, HEIGHT));
248 SkCanvas* canvas = surf->getCanvas();
djsollen@google.com339e79f2013-09-04 17:16:00 +0000249
Mike Reeda5b07692019-09-03 13:23:43 -0400250 canvas->drawColor(SK_ColorRED);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000251
252 SkRegion localRegion = clipRegion;
253
fmalita55b29b22016-01-20 11:17:39 -0800254 SkPaint paint;
255 paint.setAlpha(128);
scroggo24519372014-07-22 12:38:55 -0700256 for (size_t j = 0; j < SK_ARRAY_COUNT(flags); ++j) {
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000257 SkRect layerBounds = SkRect::Make(layerRect);
Mike Reeda5b07692019-09-03 13:23:43 -0400258 canvas->saveLayer(SkCanvas::SaveLayerRec(&layerBounds, &paint, flags[j]));
djsollen@google.com339e79f2013-09-04 17:16:00 +0000259
djsollen@google.com339e79f2013-09-04 17:16:00 +0000260 if (i) {
Mike Reeda5b07692019-09-03 13:23:43 -0400261 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(canvas);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000262 REPORTER_ASSERT(reporter, state);
scroggo24519372014-07-22 12:38:55 -0700263
264 SkRegion::Iterator iter(localRegion);
265 SkTDArray<int32_t> rectCoords;
266 for (; !iter.done(); iter.next()) {
267 const SkIRect& rect = iter.rect();
268 *rectCoords.append() = rect.fLeft;
269 *rectCoords.append() = rect.fTop;
270 *rectCoords.append() = rect.fRight;
271 *rectCoords.append() = rect.fBottom;
272 }
273 bool success = drawFn(state, clipRect.fLeft, clipRect.fTop,
274 clipRect.fRight, clipRect.fBottom, clipOps[j],
275 rectCoords.count() / 4, rectCoords.begin());
276 REPORTER_ASSERT(reporter, success);
277
278 SkCanvasStateUtils::ReleaseCanvasState(state);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000279 } else {
Mike Reeda5b07692019-09-03 13:23:43 -0400280 complex_clips_draw(canvas, clipRect.fLeft, clipRect.fTop,
scroggo24519372014-07-22 12:38:55 -0700281 clipRect.fRight, clipRect.fBottom, clipOps[j],
282 localRegion);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000283 }
284
Mike Reeda5b07692019-09-03 13:23:43 -0400285 canvas->restore();
djsollen@google.com339e79f2013-09-04 17:16:00 +0000286
287 // translate the canvas and region for the next iteration
Mike Reeda5b07692019-09-03 13:23:43 -0400288 canvas->translate(0, SkIntToScalar(2*(layerRect.height() + (SPACER))));
djsollen@google.com339e79f2013-09-04 17:16:00 +0000289 localRegion.translate(0, 2*(layerRect.height() + SPACER));
290 }
Mike Reeda5b07692019-09-03 13:23:43 -0400291 images[i] = surf->makeImageSnapshot();
djsollen@google.com339e79f2013-09-04 17:16:00 +0000292 }
293
Mike Reeda5b07692019-09-03 13:23:43 -0400294 compare(reporter, images[0].get(), images[1].get());
djsollen@google.com339e79f2013-09-04 17:16:00 +0000295}
scroggo24519372014-07-22 12:38:55 -0700296#endif
djsollen@google.com339e79f2013-09-04 17:16:00 +0000297
298////////////////////////////////////////////////////////////////////////////////
299
scroggo24519372014-07-22 12:38:55 -0700300DEF_TEST(CanvasState_test_soft_clips, reporter) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000301 SkBitmap bitmap;
302 bitmap.allocN32Pixels(10, 10);
303 SkCanvas canvas(bitmap);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000304
305 SkRRect roundRect;
306 roundRect.setOval(SkRect::MakeWH(5, 5));
307
Mike Reedc1f77742016-12-09 09:00:50 -0500308 canvas.clipRRect(roundRect, kIntersect_SkClipOp, true);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000309
310 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
311 REPORTER_ASSERT(reporter, !state);
312}
313
scroggo24519372014-07-22 12:38:55 -0700314DEF_TEST(CanvasState_test_saveLayer_clip, reporter) {
Mike Reed3726a4a2017-01-19 11:36:41 -0500315#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
Cary Clark7eddfb82018-03-13 14:41:10 -0400316 static_assert(SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag ==
317 SkCanvasPriv::kDontClipToLayer_SaveLayerFlag, "");
Mike Reed3726a4a2017-01-19 11:36:41 -0500318#endif
senorblanco@chromium.org89f077c2014-02-24 15:16:42 +0000319 const int WIDTH = 100;
320 const int HEIGHT = 100;
321 const int LAYER_WIDTH = 50;
322 const int LAYER_HEIGHT = 50;
323
324 SkBitmap bitmap;
325 bitmap.allocN32Pixels(WIDTH, HEIGHT);
326 SkCanvas canvas(bitmap);
327
328 SkRect bounds = SkRect::MakeWH(SkIntToScalar(LAYER_WIDTH), SkIntToScalar(LAYER_HEIGHT));
329 canvas.clipRect(SkRect::MakeWH(SkIntToScalar(WIDTH), SkIntToScalar(HEIGHT)));
330
Mike Reed3726a4a2017-01-19 11:36:41 -0500331 SkIRect devClip;
332 // Check that saveLayer without the kClipToLayer_SaveFlag leaves the clip unchanged.
Cary Clark7eddfb82018-03-13 14:41:10 -0400333 canvas.saveLayer(SkCanvas::SaveLayerRec(&bounds, nullptr,
334 (SkCanvas::SaveLayerFlags) SkCanvasPriv::kDontClipToLayer_SaveLayerFlag));
Mike Reed918e1442017-01-23 11:39:45 -0500335 devClip = canvas.getDeviceClipBounds();
Mike Reed3726a4a2017-01-19 11:36:41 -0500336 REPORTER_ASSERT(reporter, canvas.isClipRect());
337 REPORTER_ASSERT(reporter, devClip.width() == WIDTH);
338 REPORTER_ASSERT(reporter, devClip.height() == HEIGHT);
senorblanco@chromium.org89f077c2014-02-24 15:16:42 +0000339 canvas.restore();
340
341 // Check that saveLayer with the kClipToLayer_SaveFlag sets the clip
342 // stack to the layer bounds.
fmalita55b29b22016-01-20 11:17:39 -0800343 canvas.saveLayer(&bounds, nullptr);
Mike Reed918e1442017-01-23 11:39:45 -0500344 devClip = canvas.getDeviceClipBounds();
Mike Reed3726a4a2017-01-19 11:36:41 -0500345 REPORTER_ASSERT(reporter, canvas.isClipRect());
346 REPORTER_ASSERT(reporter, devClip.width() == LAYER_WIDTH);
347 REPORTER_ASSERT(reporter, devClip.height() == LAYER_HEIGHT);
senorblanco@chromium.org89f077c2014-02-24 15:16:42 +0000348 canvas.restore();
scroggo24519372014-07-22 12:38:55 -0700349}