blob: 17fb88e1e420d7a8bcad7931a0d8f1bcf9ab1a5f [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/core/SkTLazy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "tests/Test.h"
24#include "tools/flags/CommandLineFlags.h"
djsollen@google.com5587ac02013-08-29 20:20:40 +000025
Ben Wagnerb607a8f2018-03-12 13:46:21 -040026#include <cstring>
Ben Wagnerb607a8f2018-03-12 13:46:21 -040027
28class SkCanvasState;
29
Michael Ludwig30217952020-12-04 12:58:35 -050030// Uncomment to include tests of CanvasState across a library boundary. This will change how 'dm'
31// is built so that the functions defined in CanvasStateHelpers do not exist inside 'dm', and are
32// instead compiled as part of the 'canvas_state_lib' build target. This produces a shared library
33// that must be passed to 'dm' using the --library flag when running.
34// #define SK_TEST_CANVAS_STATE_CROSS_LIBRARY
35
36// Must be included after SK_TEST_CANVAS_STATE_CROSS_LIBRARY is defined
37#include "tests/CanvasStateHelpers.h"
38
39// dlopen, the library flag and canvas state helpers are only used for tests which require this flag
40#if defined(SK_TEST_CANVAS_STATE_CROSS_LIBRARY)
scroggo24519372014-07-22 12:38:55 -070041
Mike Klein84836b72019-03-21 11:31:36 -050042static DEFINE_string(library, "",
Michael Ludwig30217952020-12-04 12:58:35 -050043 "Support library to use for CanvasState test. Must be provided when"
44 " SK_TEST_CANVAS_STATE_CROSS_LIBRARY to specify the dynamically loaded library"
45 " that receives the captured canvas state. Functions from the library will be"
46 " called to test SkCanvasState. The library is built from the canvas_state_lib"
47 " target");
scroggo24519372014-07-22 12:38:55 -070048
Michael Ludwig30217952020-12-04 12:58:35 -050049#include "src/ports/SkOSLibrary.h"
scroggo24519372014-07-22 12:38:55 -070050
Michael Ludwig30217952020-12-04 12:58:35 -050051// Automatically loads library passed to --library flag and closes it when it goes out of scope.
scroggo24519372014-07-22 12:38:55 -070052class OpenLibResult {
53public:
scroggo24519372014-07-22 12:38:55 -070054 OpenLibResult(skiatest::Reporter* reporter) {
55 if (FLAGS_library.count() == 1) {
Michael Ludwig30217952020-12-04 12:58:35 -050056 fLibrary = SkLoadDynamicLibrary(FLAGS_library[0]);
57 REPORTER_ASSERT(reporter, fLibrary != nullptr, "Failed to open library!");
scroggo24519372014-07-22 12:38:55 -070058 } else {
Michael Ludwig30217952020-12-04 12:58:35 -050059 fLibrary = nullptr;
scroggo24519372014-07-22 12:38:55 -070060 }
61 }
62
scroggo24519372014-07-22 12:38:55 -070063 ~OpenLibResult() {
Michael Ludwig30217952020-12-04 12:58:35 -050064 if (fLibrary) {
65 SkFreeDynamicLibrary(fLibrary);
scroggo24519372014-07-22 12:38:55 -070066 }
67 }
68
Michael Ludwig30217952020-12-04 12:58:35 -050069 // Load a function address from the library object, or null if the library had failed
70 void* procAddress(const char* funcName) {
71 if (fLibrary) {
72 return SkGetProcedureAddress(fLibrary, funcName);
73 }
74 return nullptr;
75 }
scroggo24519372014-07-22 12:38:55 -070076
77private:
Michael Ludwig30217952020-12-04 12:58:35 -050078 void* fLibrary;
scroggo24519372014-07-22 12:38:55 -070079};
80
Michael Ludwig30217952020-12-04 12:58:35 -050081#endif
82
Mike Reeda5b07692019-09-03 13:23:43 -040083static void write_image(const SkImage* img, const char path[]) {
84 auto data = img->encodeToData();
85 SkFILEWStream(path).write(data->data(), data->size());
86}
87
88static void compare(skiatest::Reporter* reporter, SkImage* img0, SkImage* img1) {
89 if (false) {
90 static int counter;
91
92 SkDebugf("---- counter %d\n", counter);
93 SkString name;
94 name.printf("no_capture_%d.png", counter);
95 write_image(img0, name.c_str());
96 name.printf("capture_%d.png", counter);
97 write_image(img1, name.c_str());
98 counter++;
99 }
100
101 SkPixmap pm[2];
102 REPORTER_ASSERT(reporter, img0->peekPixels(&pm[0]));
103 REPORTER_ASSERT(reporter, img1->peekPixels(&pm[1]));
104 // now we memcmp the two bitmaps
105 REPORTER_ASSERT(reporter, pm[0].computeByteSize() == pm[1].computeByteSize());
106 REPORTER_ASSERT(reporter, pm[0].rowBytes() == (size_t)pm[0].width() * pm[0].info().bytesPerPixel());
107 REPORTER_ASSERT(reporter, pm[1].rowBytes() == (size_t)pm[1].width() * pm[1].info().bytesPerPixel());
Michael Ludwig30217952020-12-04 12:58:35 -0500108 if (memcmp(pm[0].addr(0, 0), pm[1].addr(0, 0), pm[0].computeByteSize()) != 0) {
Mike Reeda5b07692019-09-03 13:23:43 -0400109 REPORTER_ASSERT(reporter, false);
110 }
111}
112
scroggo24519372014-07-22 12:38:55 -0700113DEF_TEST(CanvasState_test_complex_layers, reporter) {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000114 const int WIDTH = 400;
115 const int HEIGHT = 400;
116 const int SPACER = 10;
117
djsollen@google.com20146b32013-08-29 20:36:22 +0000118 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(SPACER), SkIntToScalar(SPACER),
119 SkIntToScalar(WIDTH-(2*SPACER)),
120 SkIntToScalar((HEIGHT-(2*SPACER)) / 7));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000121
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000122 const SkColorType colorTypes[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000123 kRGB_565_SkColorType, kN32_SkColorType
djsollen@google.com5587ac02013-08-29 20:20:40 +0000124 };
djsollen@google.com5587ac02013-08-29 20:20:40 +0000125
126 const int layerAlpha[] = { 255, 255, 0 };
scroggo24519372014-07-22 12:38:55 -0700127
128 bool (*drawFn)(SkCanvasState* state, float l, float t,
129 float r, float b, int32_t s);
130
Michael Ludwig30217952020-12-04 12:58:35 -0500131#if defined(SK_TEST_CANVAS_STATE_CROSS_LIBRARY)
scroggo24519372014-07-22 12:38:55 -0700132 OpenLibResult openLibResult(reporter);
Michael Ludwig30217952020-12-04 12:58:35 -0500133 *(void**) (&drawFn) = openLibResult.procAddress("complex_layers_draw_from_canvas_state");
134#else
135 drawFn = complex_layers_draw_from_canvas_state;
136#endif
scroggo24519372014-07-22 12:38:55 -0700137
138 REPORTER_ASSERT(reporter, drawFn);
139 if (!drawFn) {
140 return;
141 }
djsollen@google.com5587ac02013-08-29 20:20:40 +0000142
scroggoecce60b2014-07-09 07:26:40 -0700143 for (size_t i = 0; i < SK_ARRAY_COUNT(colorTypes); ++i) {
Mike Reeda5b07692019-09-03 13:23:43 -0400144 sk_sp<SkImage> images[2];
djsollen@google.com5587ac02013-08-29 20:20:40 +0000145 for (int j = 0; j < 2; ++j) {
Mike Reeda5b07692019-09-03 13:23:43 -0400146 auto surf = SkSurface::MakeRaster(SkImageInfo::Make(WIDTH, HEIGHT,
147 colorTypes[i],
148 kPremul_SkAlphaType));
149 SkCanvas* canvas = surf->getCanvas();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000150
Mike Reeda5b07692019-09-03 13:23:43 -0400151 canvas->drawColor(SK_ColorRED);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000152
scroggo24519372014-07-22 12:38:55 -0700153 for (size_t k = 0; k < SK_ARRAY_COUNT(layerAlpha); ++k) {
fmalita55b29b22016-01-20 11:17:39 -0800154 SkTLazy<SkPaint> paint;
155 if (layerAlpha[k] != 0xFF) {
156 paint.init()->setAlpha(layerAlpha[k]);
157 }
158
djsollen@google.com5587ac02013-08-29 20:20:40 +0000159 // draw a rect within the layer's bounds and again outside the layer's bounds
Michael Ludwige0dee012020-12-04 12:59:39 -0500160 canvas->saveLayer(SkCanvas::SaveLayerRec(&rect, paint.getMaybeNull()));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000161
djsollen@google.com5587ac02013-08-29 20:20:40 +0000162 if (j) {
scroggo24519372014-07-22 12:38:55 -0700163 // Capture from the first Skia.
Mike Reeda5b07692019-09-03 13:23:43 -0400164 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(canvas);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000165 REPORTER_ASSERT(reporter, state);
scroggo24519372014-07-22 12:38:55 -0700166
167 // And draw to it in the second Skia.
168 bool success = complex_layers_draw_from_canvas_state(state,
169 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, SPACER);
170 REPORTER_ASSERT(reporter, success);
171
172 // And release it in the *first* Skia.
173 SkCanvasStateUtils::ReleaseCanvasState(state);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000174 } else {
scroggo24519372014-07-22 12:38:55 -0700175 // Draw in the first Skia.
Mike Reeda5b07692019-09-03 13:23:43 -0400176 complex_layers_draw(canvas, rect.fLeft, rect.fTop,
scroggo24519372014-07-22 12:38:55 -0700177 rect.fRight, rect.fBottom, SPACER);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000178 }
179
Mike Reeda5b07692019-09-03 13:23:43 -0400180 canvas->restore();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000181
182 // translate the canvas for the next iteration
Mike Reeda5b07692019-09-03 13:23:43 -0400183 canvas->translate(0, 2*(rect.height() + SPACER));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000184 }
Mike Reeda5b07692019-09-03 13:23:43 -0400185 images[j] = surf->makeImageSnapshot();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000186 }
187
Mike Reeda5b07692019-09-03 13:23:43 -0400188 compare(reporter, images[0].get(), images[1].get());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000189 }
190}
191
192////////////////////////////////////////////////////////////////////////////////
193
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,
Michael Ludwig30217952020-12-04 12:58:35 -0500219 SkRegion::kDifference_Op,
djsollen@google.com339e79f2013-09-04 17:16:00 +0000220 };
scroggo24519372014-07-22 12:38:55 -0700221
222 bool (*drawFn)(SkCanvasState* state, int32_t l, int32_t t,
223 int32_t r, int32_t b, int32_t clipOp,
224 int32_t regionRects, int32_t* rectCoords);
225
Michael Ludwig30217952020-12-04 12:58:35 -0500226#if defined(SK_TEST_CANVAS_STATE_CROSS_LIBRARY)
scroggo24519372014-07-22 12:38:55 -0700227 OpenLibResult openLibResult(reporter);
Michael Ludwig30217952020-12-04 12:58:35 -0500228 *(void**) (&drawFn) = openLibResult.procAddress("complex_clips_draw_from_canvas_state");
229#else
230 drawFn = complex_clips_draw_from_canvas_state;
231#endif
scroggo24519372014-07-22 12:38:55 -0700232
233 REPORTER_ASSERT(reporter, drawFn);
234 if (!drawFn) {
235 return;
236 }
djsollen@google.com339e79f2013-09-04 17:16:00 +0000237
Mike Reeda5b07692019-09-03 13:23:43 -0400238 sk_sp<SkImage> images[2];
djsollen@google.com339e79f2013-09-04 17:16:00 +0000239 for (int i = 0; i < 2; ++i) {
Mike Reeda5b07692019-09-03 13:23:43 -0400240 auto surf = SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(WIDTH, HEIGHT));
241 SkCanvas* canvas = surf->getCanvas();
djsollen@google.com339e79f2013-09-04 17:16:00 +0000242
Mike Reeda5b07692019-09-03 13:23:43 -0400243 canvas->drawColor(SK_ColorRED);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000244
245 SkRegion localRegion = clipRegion;
246
fmalita55b29b22016-01-20 11:17:39 -0800247 SkPaint paint;
248 paint.setAlpha(128);
Michael Ludwig30217952020-12-04 12:58:35 -0500249 for (size_t j = 0; j < SK_ARRAY_COUNT(clipOps); ++j) {
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000250 SkRect layerBounds = SkRect::Make(layerRect);
Michael Ludwige0dee012020-12-04 12:59:39 -0500251 canvas->saveLayer(SkCanvas::SaveLayerRec(&layerBounds, &paint));
djsollen@google.com339e79f2013-09-04 17:16:00 +0000252
djsollen@google.com339e79f2013-09-04 17:16:00 +0000253 if (i) {
Mike Reeda5b07692019-09-03 13:23:43 -0400254 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(canvas);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000255 REPORTER_ASSERT(reporter, state);
scroggo24519372014-07-22 12:38:55 -0700256
257 SkRegion::Iterator iter(localRegion);
258 SkTDArray<int32_t> rectCoords;
259 for (; !iter.done(); iter.next()) {
260 const SkIRect& rect = iter.rect();
261 *rectCoords.append() = rect.fLeft;
262 *rectCoords.append() = rect.fTop;
263 *rectCoords.append() = rect.fRight;
264 *rectCoords.append() = rect.fBottom;
265 }
266 bool success = drawFn(state, clipRect.fLeft, clipRect.fTop,
267 clipRect.fRight, clipRect.fBottom, clipOps[j],
268 rectCoords.count() / 4, rectCoords.begin());
269 REPORTER_ASSERT(reporter, success);
270
271 SkCanvasStateUtils::ReleaseCanvasState(state);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000272 } else {
Mike Reeda5b07692019-09-03 13:23:43 -0400273 complex_clips_draw(canvas, clipRect.fLeft, clipRect.fTop,
scroggo24519372014-07-22 12:38:55 -0700274 clipRect.fRight, clipRect.fBottom, clipOps[j],
275 localRegion);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000276 }
277
Mike Reeda5b07692019-09-03 13:23:43 -0400278 canvas->restore();
djsollen@google.com339e79f2013-09-04 17:16:00 +0000279
280 // translate the canvas and region for the next iteration
Mike Reeda5b07692019-09-03 13:23:43 -0400281 canvas->translate(0, SkIntToScalar(2*(layerRect.height() + (SPACER))));
djsollen@google.com339e79f2013-09-04 17:16:00 +0000282 localRegion.translate(0, 2*(layerRect.height() + SPACER));
283 }
Mike Reeda5b07692019-09-03 13:23:43 -0400284 images[i] = surf->makeImageSnapshot();
djsollen@google.com339e79f2013-09-04 17:16:00 +0000285 }
286
Mike Reeda5b07692019-09-03 13:23:43 -0400287 compare(reporter, images[0].get(), images[1].get());
djsollen@google.com339e79f2013-09-04 17:16:00 +0000288}
289
290////////////////////////////////////////////////////////////////////////////////
291
scroggo24519372014-07-22 12:38:55 -0700292DEF_TEST(CanvasState_test_soft_clips, reporter) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000293 SkBitmap bitmap;
294 bitmap.allocN32Pixels(10, 10);
295 SkCanvas canvas(bitmap);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000296
297 SkRRect roundRect;
298 roundRect.setOval(SkRect::MakeWH(5, 5));
299
Michael Ludwig2f6e2f82021-08-03 13:08:50 -0400300 canvas.clipRRect(roundRect, SkClipOp::kIntersect, true);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000301
302 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
303 REPORTER_ASSERT(reporter, !state);
304}
305
scroggo24519372014-07-22 12:38:55 -0700306DEF_TEST(CanvasState_test_saveLayer_clip, reporter) {
senorblanco@chromium.org89f077c2014-02-24 15:16:42 +0000307 const int WIDTH = 100;
308 const int HEIGHT = 100;
309 const int LAYER_WIDTH = 50;
310 const int LAYER_HEIGHT = 50;
311
312 SkBitmap bitmap;
313 bitmap.allocN32Pixels(WIDTH, HEIGHT);
314 SkCanvas canvas(bitmap);
315
316 SkRect bounds = SkRect::MakeWH(SkIntToScalar(LAYER_WIDTH), SkIntToScalar(LAYER_HEIGHT));
317 canvas.clipRect(SkRect::MakeWH(SkIntToScalar(WIDTH), SkIntToScalar(HEIGHT)));
318
Michael Ludwige0dee012020-12-04 12:59:39 -0500319 // Check that saveLayer sets the clip stack to the layer bounds.
fmalita55b29b22016-01-20 11:17:39 -0800320 canvas.saveLayer(&bounds, nullptr);
Michael Ludwige0dee012020-12-04 12:59:39 -0500321 SkIRect devClip = canvas.getDeviceClipBounds();
Mike Reed3726a4a2017-01-19 11:36:41 -0500322 REPORTER_ASSERT(reporter, canvas.isClipRect());
323 REPORTER_ASSERT(reporter, devClip.width() == LAYER_WIDTH);
324 REPORTER_ASSERT(reporter, devClip.height() == LAYER_HEIGHT);
senorblanco@chromium.org89f077c2014-02-24 15:16:42 +0000325 canvas.restore();
scroggo24519372014-07-22 12:38:55 -0700326}