blob: edfd88d77fe1b11942d517131354582c97d6f7d5 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "tests/Test.h"
25#include "tools/flags/CommandLineFlags.h"
djsollen@google.com5587ac02013-08-29 20:20:40 +000026
Ben Wagnerb607a8f2018-03-12 13:46:21 -040027#include <cstring>
Ben Wagnerb607a8f2018-03-12 13:46:21 -040028
29class SkCanvasState;
30
Michael Ludwig30217952020-12-04 12:58:35 -050031// Uncomment to include tests of CanvasState across a library boundary. This will change how 'dm'
32// is built so that the functions defined in CanvasStateHelpers do not exist inside 'dm', and are
33// instead compiled as part of the 'canvas_state_lib' build target. This produces a shared library
34// that must be passed to 'dm' using the --library flag when running.
35// #define SK_TEST_CANVAS_STATE_CROSS_LIBRARY
36
37// Must be included after SK_TEST_CANVAS_STATE_CROSS_LIBRARY is defined
38#include "tests/CanvasStateHelpers.h"
39
40// dlopen, the library flag and canvas state helpers are only used for tests which require this flag
41#if defined(SK_TEST_CANVAS_STATE_CROSS_LIBRARY)
scroggo24519372014-07-22 12:38:55 -070042
Mike Klein84836b72019-03-21 11:31:36 -050043static DEFINE_string(library, "",
Michael Ludwig30217952020-12-04 12:58:35 -050044 "Support library to use for CanvasState test. Must be provided when"
45 " SK_TEST_CANVAS_STATE_CROSS_LIBRARY to specify the dynamically loaded library"
46 " that receives the captured canvas state. Functions from the library will be"
47 " called to test SkCanvasState. The library is built from the canvas_state_lib"
48 " target");
scroggo24519372014-07-22 12:38:55 -070049
Michael Ludwig30217952020-12-04 12:58:35 -050050#include "src/ports/SkOSLibrary.h"
scroggo24519372014-07-22 12:38:55 -070051
Michael Ludwig30217952020-12-04 12:58:35 -050052// Automatically loads library passed to --library flag and closes it when it goes out of scope.
scroggo24519372014-07-22 12:38:55 -070053class OpenLibResult {
54public:
scroggo24519372014-07-22 12:38:55 -070055 OpenLibResult(skiatest::Reporter* reporter) {
56 if (FLAGS_library.count() == 1) {
Michael Ludwig30217952020-12-04 12:58:35 -050057 fLibrary = SkLoadDynamicLibrary(FLAGS_library[0]);
58 REPORTER_ASSERT(reporter, fLibrary != nullptr, "Failed to open library!");
scroggo24519372014-07-22 12:38:55 -070059 } else {
Michael Ludwig30217952020-12-04 12:58:35 -050060 fLibrary = nullptr;
scroggo24519372014-07-22 12:38:55 -070061 }
62 }
63
scroggo24519372014-07-22 12:38:55 -070064 ~OpenLibResult() {
Michael Ludwig30217952020-12-04 12:58:35 -050065 if (fLibrary) {
66 SkFreeDynamicLibrary(fLibrary);
scroggo24519372014-07-22 12:38:55 -070067 }
68 }
69
Michael Ludwig30217952020-12-04 12:58:35 -050070 // Load a function address from the library object, or null if the library had failed
71 void* procAddress(const char* funcName) {
72 if (fLibrary) {
73 return SkGetProcedureAddress(fLibrary, funcName);
74 }
75 return nullptr;
76 }
scroggo24519372014-07-22 12:38:55 -070077
78private:
Michael Ludwig30217952020-12-04 12:58:35 -050079 void* fLibrary;
scroggo24519372014-07-22 12:38:55 -070080};
81
Michael Ludwig30217952020-12-04 12:58:35 -050082#endif
83
Mike Reeda5b07692019-09-03 13:23:43 -040084static void write_image(const SkImage* img, const char path[]) {
85 auto data = img->encodeToData();
86 SkFILEWStream(path).write(data->data(), data->size());
87}
88
89static void compare(skiatest::Reporter* reporter, SkImage* img0, SkImage* img1) {
90 if (false) {
91 static int counter;
92
93 SkDebugf("---- counter %d\n", counter);
94 SkString name;
95 name.printf("no_capture_%d.png", counter);
96 write_image(img0, name.c_str());
97 name.printf("capture_%d.png", counter);
98 write_image(img1, name.c_str());
99 counter++;
100 }
101
102 SkPixmap pm[2];
103 REPORTER_ASSERT(reporter, img0->peekPixels(&pm[0]));
104 REPORTER_ASSERT(reporter, img1->peekPixels(&pm[1]));
105 // now we memcmp the two bitmaps
106 REPORTER_ASSERT(reporter, pm[0].computeByteSize() == pm[1].computeByteSize());
107 REPORTER_ASSERT(reporter, pm[0].rowBytes() == (size_t)pm[0].width() * pm[0].info().bytesPerPixel());
108 REPORTER_ASSERT(reporter, pm[1].rowBytes() == (size_t)pm[1].width() * pm[1].info().bytesPerPixel());
Michael Ludwig30217952020-12-04 12:58:35 -0500109 if (memcmp(pm[0].addr(0, 0), pm[1].addr(0, 0), pm[0].computeByteSize()) != 0) {
Mike Reeda5b07692019-09-03 13:23:43 -0400110 REPORTER_ASSERT(reporter, false);
111 }
112}
113
scroggo24519372014-07-22 12:38:55 -0700114DEF_TEST(CanvasState_test_complex_layers, reporter) {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000115 const int WIDTH = 400;
116 const int HEIGHT = 400;
117 const int SPACER = 10;
118
djsollen@google.com20146b32013-08-29 20:36:22 +0000119 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(SPACER), SkIntToScalar(SPACER),
120 SkIntToScalar(WIDTH-(2*SPACER)),
121 SkIntToScalar((HEIGHT-(2*SPACER)) / 7));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000122
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000123 const SkColorType colorTypes[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000124 kRGB_565_SkColorType, kN32_SkColorType
djsollen@google.com5587ac02013-08-29 20:20:40 +0000125 };
djsollen@google.com5587ac02013-08-29 20:20:40 +0000126
127 const int layerAlpha[] = { 255, 255, 0 };
scroggo24519372014-07-22 12:38:55 -0700128
129 bool (*drawFn)(SkCanvasState* state, float l, float t,
130 float r, float b, int32_t s);
131
Michael Ludwig30217952020-12-04 12:58:35 -0500132#if defined(SK_TEST_CANVAS_STATE_CROSS_LIBRARY)
scroggo24519372014-07-22 12:38:55 -0700133 OpenLibResult openLibResult(reporter);
Michael Ludwig30217952020-12-04 12:58:35 -0500134 *(void**) (&drawFn) = openLibResult.procAddress("complex_layers_draw_from_canvas_state");
135#else
136 drawFn = complex_layers_draw_from_canvas_state;
137#endif
scroggo24519372014-07-22 12:38:55 -0700138
139 REPORTER_ASSERT(reporter, drawFn);
140 if (!drawFn) {
141 return;
142 }
djsollen@google.com5587ac02013-08-29 20:20:40 +0000143
scroggoecce60b2014-07-09 07:26:40 -0700144 for (size_t i = 0; i < SK_ARRAY_COUNT(colorTypes); ++i) {
Mike Reeda5b07692019-09-03 13:23:43 -0400145 sk_sp<SkImage> images[2];
djsollen@google.com5587ac02013-08-29 20:20:40 +0000146 for (int j = 0; j < 2; ++j) {
Mike Reeda5b07692019-09-03 13:23:43 -0400147 auto surf = SkSurface::MakeRaster(SkImageInfo::Make(WIDTH, HEIGHT,
148 colorTypes[i],
149 kPremul_SkAlphaType));
150 SkCanvas* canvas = surf->getCanvas();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000151
Mike Reeda5b07692019-09-03 13:23:43 -0400152 canvas->drawColor(SK_ColorRED);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000153
scroggo24519372014-07-22 12:38:55 -0700154 for (size_t k = 0; k < SK_ARRAY_COUNT(layerAlpha); ++k) {
fmalita55b29b22016-01-20 11:17:39 -0800155 SkTLazy<SkPaint> paint;
156 if (layerAlpha[k] != 0xFF) {
157 paint.init()->setAlpha(layerAlpha[k]);
158 }
159
djsollen@google.com5587ac02013-08-29 20:20:40 +0000160 // draw a rect within the layer's bounds and again outside the layer's bounds
Michael Ludwige0dee012020-12-04 12:59:39 -0500161 canvas->saveLayer(SkCanvas::SaveLayerRec(&rect, paint.getMaybeNull()));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000162
djsollen@google.com5587ac02013-08-29 20:20:40 +0000163 if (j) {
scroggo24519372014-07-22 12:38:55 -0700164 // Capture from the first Skia.
Mike Reeda5b07692019-09-03 13:23:43 -0400165 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(canvas);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000166 REPORTER_ASSERT(reporter, state);
scroggo24519372014-07-22 12:38:55 -0700167
168 // And draw to it in the second Skia.
169 bool success = complex_layers_draw_from_canvas_state(state,
170 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, SPACER);
171 REPORTER_ASSERT(reporter, success);
172
173 // And release it in the *first* Skia.
174 SkCanvasStateUtils::ReleaseCanvasState(state);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000175 } else {
scroggo24519372014-07-22 12:38:55 -0700176 // Draw in the first Skia.
Mike Reeda5b07692019-09-03 13:23:43 -0400177 complex_layers_draw(canvas, rect.fLeft, rect.fTop,
scroggo24519372014-07-22 12:38:55 -0700178 rect.fRight, rect.fBottom, SPACER);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000179 }
180
Mike Reeda5b07692019-09-03 13:23:43 -0400181 canvas->restore();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000182
183 // translate the canvas for the next iteration
Mike Reeda5b07692019-09-03 13:23:43 -0400184 canvas->translate(0, 2*(rect.height() + SPACER));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000185 }
Mike Reeda5b07692019-09-03 13:23:43 -0400186 images[j] = surf->makeImageSnapshot();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000187 }
188
Mike Reeda5b07692019-09-03 13:23:43 -0400189 compare(reporter, images[0].get(), images[1].get());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000190 }
191}
192
193////////////////////////////////////////////////////////////////////////////////
194
scroggo24519372014-07-22 12:38:55 -0700195DEF_TEST(CanvasState_test_complex_clips, reporter) {
djsollen@google.com339e79f2013-09-04 17:16:00 +0000196 const int WIDTH = 400;
197 const int HEIGHT = 400;
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000198 const int SPACER = 10;
djsollen@google.com339e79f2013-09-04 17:16:00 +0000199
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000200 SkIRect layerRect = SkIRect::MakeWH(WIDTH, HEIGHT / 4);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000201 layerRect.inset(2*SPACER, 2*SPACER);
202
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000203 SkIRect clipRect = layerRect;
djsollen@google.com339e79f2013-09-04 17:16:00 +0000204 clipRect.fRight = clipRect.fLeft + (clipRect.width() / 2) - (2*SPACER);
205 clipRect.outset(SPACER, SPACER);
206
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000207 SkIRect regionBounds = clipRect;
djsollen@google.com339e79f2013-09-04 17:16:00 +0000208 regionBounds.offset(clipRect.width() + (2*SPACER), 0);
209
210 SkIRect regionInterior = regionBounds;
211 regionInterior.inset(SPACER*3, SPACER*3);
212
213 SkRegion clipRegion;
214 clipRegion.setRect(regionBounds);
215 clipRegion.op(regionInterior, SkRegion::kDifference_Op);
216
217
218 const SkRegion::Op clipOps[] = { SkRegion::kIntersect_Op,
219 SkRegion::kIntersect_Op,
Michael Ludwig30217952020-12-04 12:58:35 -0500220 SkRegion::kDifference_Op,
djsollen@google.com339e79f2013-09-04 17:16:00 +0000221 };
scroggo24519372014-07-22 12:38:55 -0700222
223 bool (*drawFn)(SkCanvasState* state, int32_t l, int32_t t,
224 int32_t r, int32_t b, int32_t clipOp,
225 int32_t regionRects, int32_t* rectCoords);
226
Michael Ludwig30217952020-12-04 12:58:35 -0500227#if defined(SK_TEST_CANVAS_STATE_CROSS_LIBRARY)
scroggo24519372014-07-22 12:38:55 -0700228 OpenLibResult openLibResult(reporter);
Michael Ludwig30217952020-12-04 12:58:35 -0500229 *(void**) (&drawFn) = openLibResult.procAddress("complex_clips_draw_from_canvas_state");
230#else
231 drawFn = complex_clips_draw_from_canvas_state;
232#endif
scroggo24519372014-07-22 12:38:55 -0700233
234 REPORTER_ASSERT(reporter, drawFn);
235 if (!drawFn) {
236 return;
237 }
djsollen@google.com339e79f2013-09-04 17:16:00 +0000238
Mike Reeda5b07692019-09-03 13:23:43 -0400239 sk_sp<SkImage> images[2];
djsollen@google.com339e79f2013-09-04 17:16:00 +0000240 for (int i = 0; i < 2; ++i) {
Mike Reeda5b07692019-09-03 13:23:43 -0400241 auto surf = SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(WIDTH, HEIGHT));
242 SkCanvas* canvas = surf->getCanvas();
djsollen@google.com339e79f2013-09-04 17:16:00 +0000243
Mike Reeda5b07692019-09-03 13:23:43 -0400244 canvas->drawColor(SK_ColorRED);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000245
246 SkRegion localRegion = clipRegion;
247
fmalita55b29b22016-01-20 11:17:39 -0800248 SkPaint paint;
249 paint.setAlpha(128);
Michael Ludwig30217952020-12-04 12:58:35 -0500250 for (size_t j = 0; j < SK_ARRAY_COUNT(clipOps); ++j) {
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000251 SkRect layerBounds = SkRect::Make(layerRect);
Michael Ludwige0dee012020-12-04 12:59:39 -0500252 canvas->saveLayer(SkCanvas::SaveLayerRec(&layerBounds, &paint));
djsollen@google.com339e79f2013-09-04 17:16:00 +0000253
djsollen@google.com339e79f2013-09-04 17:16:00 +0000254 if (i) {
Mike Reeda5b07692019-09-03 13:23:43 -0400255 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(canvas);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000256 REPORTER_ASSERT(reporter, state);
scroggo24519372014-07-22 12:38:55 -0700257
258 SkRegion::Iterator iter(localRegion);
259 SkTDArray<int32_t> rectCoords;
260 for (; !iter.done(); iter.next()) {
261 const SkIRect& rect = iter.rect();
262 *rectCoords.append() = rect.fLeft;
263 *rectCoords.append() = rect.fTop;
264 *rectCoords.append() = rect.fRight;
265 *rectCoords.append() = rect.fBottom;
266 }
267 bool success = drawFn(state, clipRect.fLeft, clipRect.fTop,
268 clipRect.fRight, clipRect.fBottom, clipOps[j],
269 rectCoords.count() / 4, rectCoords.begin());
270 REPORTER_ASSERT(reporter, success);
271
272 SkCanvasStateUtils::ReleaseCanvasState(state);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000273 } else {
Mike Reeda5b07692019-09-03 13:23:43 -0400274 complex_clips_draw(canvas, clipRect.fLeft, clipRect.fTop,
scroggo24519372014-07-22 12:38:55 -0700275 clipRect.fRight, clipRect.fBottom, clipOps[j],
276 localRegion);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000277 }
278
Mike Reeda5b07692019-09-03 13:23:43 -0400279 canvas->restore();
djsollen@google.com339e79f2013-09-04 17:16:00 +0000280
281 // translate the canvas and region for the next iteration
Mike Reeda5b07692019-09-03 13:23:43 -0400282 canvas->translate(0, SkIntToScalar(2*(layerRect.height() + (SPACER))));
djsollen@google.com339e79f2013-09-04 17:16:00 +0000283 localRegion.translate(0, 2*(layerRect.height() + SPACER));
284 }
Mike Reeda5b07692019-09-03 13:23:43 -0400285 images[i] = surf->makeImageSnapshot();
djsollen@google.com339e79f2013-09-04 17:16:00 +0000286 }
287
Mike Reeda5b07692019-09-03 13:23:43 -0400288 compare(reporter, images[0].get(), images[1].get());
djsollen@google.com339e79f2013-09-04 17:16:00 +0000289}
290
291////////////////////////////////////////////////////////////////////////////////
292
scroggo24519372014-07-22 12:38:55 -0700293DEF_TEST(CanvasState_test_soft_clips, reporter) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000294 SkBitmap bitmap;
295 bitmap.allocN32Pixels(10, 10);
296 SkCanvas canvas(bitmap);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000297
298 SkRRect roundRect;
299 roundRect.setOval(SkRect::MakeWH(5, 5));
300
Mike Reedc1f77742016-12-09 09:00:50 -0500301 canvas.clipRRect(roundRect, kIntersect_SkClipOp, true);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000302
303 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
304 REPORTER_ASSERT(reporter, !state);
305}
306
scroggo24519372014-07-22 12:38:55 -0700307DEF_TEST(CanvasState_test_saveLayer_clip, reporter) {
senorblanco@chromium.org89f077c2014-02-24 15:16:42 +0000308 const int WIDTH = 100;
309 const int HEIGHT = 100;
310 const int LAYER_WIDTH = 50;
311 const int LAYER_HEIGHT = 50;
312
313 SkBitmap bitmap;
314 bitmap.allocN32Pixels(WIDTH, HEIGHT);
315 SkCanvas canvas(bitmap);
316
317 SkRect bounds = SkRect::MakeWH(SkIntToScalar(LAYER_WIDTH), SkIntToScalar(LAYER_HEIGHT));
318 canvas.clipRect(SkRect::MakeWH(SkIntToScalar(WIDTH), SkIntToScalar(HEIGHT)));
319
Michael Ludwige0dee012020-12-04 12:59:39 -0500320 // Check that saveLayer sets the clip stack to the layer bounds.
fmalita55b29b22016-01-20 11:17:39 -0800321 canvas.saveLayer(&bounds, nullptr);
Michael Ludwige0dee012020-12-04 12:59:39 -0500322 SkIRect devClip = canvas.getDeviceClipBounds();
Mike Reed3726a4a2017-01-19 11:36:41 -0500323 REPORTER_ASSERT(reporter, canvas.isClipRect());
324 REPORTER_ASSERT(reporter, devClip.width() == LAYER_WIDTH);
325 REPORTER_ASSERT(reporter, devClip.height() == LAYER_HEIGHT);
senorblanco@chromium.org89f077c2014-02-24 15:16:42 +0000326 canvas.restore();
scroggo24519372014-07-22 12:38:55 -0700327}