blob: cd7a4184ebdba0b505a190dc36c75552db6b91a5 [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
scroggo24519372014-07-22 12:38:55 -07008#include "CanvasStateHelpers.h"
djsollen@google.com5587ac02013-08-29 20:20:40 +00009#include "SkCanvas.h"
10#include "SkCanvasStateUtils.h"
scroggo24519372014-07-22 12:38:55 -070011#include "SkCommandLineFlags.h"
djsollen@google.com5587ac02013-08-29 20:20:40 +000012#include "SkDrawFilter.h"
commit-bot@chromium.org07f6cf32013-09-18 20:15:12 +000013#include "SkError.h"
djsollen@google.com5587ac02013-08-29 20:20:40 +000014#include "SkPaint.h"
djsollen@google.com5587ac02013-08-29 20:20:40 +000015#include "SkRRect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000016#include "SkRect.h"
fmalita55b29b22016-01-20 11:17:39 -080017#include "SkTLazy.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000018#include "Test.h"
djsollen@google.com5587ac02013-08-29 20:20:40 +000019
scroggo24519372014-07-22 12:38:55 -070020// dlopen and the library flag are only used for tests which require this flag.
reed@google.comb93ba452014-03-10 19:47:58 +000021#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
scroggo24519372014-07-22 12:38:55 -070022#include <dlfcn.h>
23
24DEFINE_string(library, "", "Support library to use for CanvasState test. If empty (the default), "
25 "the test will be run without crossing a library boundary. Otherwise, "
26 "it is expected to be a full path to a shared library file, which will"
27 " be dynamically loaded. Functions from the library will be called to "
28 "test SkCanvasState. Instructions for generating the library are in "
29 "gyp/canvas_state_lib.gyp");
30
31
32// This class calls dlopen on the library passed in to the command line flag library, and handles
33// calling dlclose when it goes out of scope.
34class OpenLibResult {
35public:
36 // If the flag library was passed to this run of the test, attempt to open it using dlopen and
37 // report whether it succeeded.
38 OpenLibResult(skiatest::Reporter* reporter) {
39 if (FLAGS_library.count() == 1) {
40 fHandle = dlopen(FLAGS_library[0], RTLD_LAZY | RTLD_LOCAL);
halcanary96fcdcc2015-08-27 07:41:13 -070041 REPORTER_ASSERT_MESSAGE(reporter, fHandle != nullptr, "Failed to open library!");
scroggo24519372014-07-22 12:38:55 -070042 } else {
halcanary96fcdcc2015-08-27 07:41:13 -070043 fHandle = nullptr;
scroggo24519372014-07-22 12:38:55 -070044 }
45 }
46
47 // Automatically call dlclose when going out of scope.
48 ~OpenLibResult() {
49 if (fHandle) {
50 dlclose(fHandle);
51 }
52 }
53
54 // Pointer to the shared library object.
55 void* handle() { return fHandle; }
56
57private:
58 void* fHandle;
59};
60
61DEF_TEST(CanvasState_test_complex_layers, reporter) {
djsollen@google.com5587ac02013-08-29 20:20:40 +000062 const int WIDTH = 400;
63 const int HEIGHT = 400;
64 const int SPACER = 10;
65
djsollen@google.com20146b32013-08-29 20:36:22 +000066 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(SPACER), SkIntToScalar(SPACER),
67 SkIntToScalar(WIDTH-(2*SPACER)),
68 SkIntToScalar((HEIGHT-(2*SPACER)) / 7));
djsollen@google.com5587ac02013-08-29 20:20:40 +000069
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000070 const SkColorType colorTypes[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000071 kRGB_565_SkColorType, kN32_SkColorType
djsollen@google.com5587ac02013-08-29 20:20:40 +000072 };
djsollen@google.com5587ac02013-08-29 20:20:40 +000073
74 const int layerAlpha[] = { 255, 255, 0 };
fmalita55b29b22016-01-20 11:17:39 -080075 const SkCanvas::SaveLayerFlags flags[] = {
76 static_cast<SkCanvas::SaveLayerFlags>(SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag),
77 0,
78 static_cast<SkCanvas::SaveLayerFlags>(SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag),
djsollen@google.com5587ac02013-08-29 20:20:40 +000079 };
80 REPORTER_ASSERT(reporter, sizeof(layerAlpha) == sizeof(flags));
scroggo24519372014-07-22 12:38:55 -070081
82 bool (*drawFn)(SkCanvasState* state, float l, float t,
83 float r, float b, int32_t s);
84
85 OpenLibResult openLibResult(reporter);
halcanary96fcdcc2015-08-27 07:41:13 -070086 if (openLibResult.handle() != nullptr) {
scroggo24519372014-07-22 12:38:55 -070087 *(void**) (&drawFn) = dlsym(openLibResult.handle(),
88 "complex_layers_draw_from_canvas_state");
89 } else {
90 drawFn = complex_layers_draw_from_canvas_state;
91 }
92
93 REPORTER_ASSERT(reporter, drawFn);
94 if (!drawFn) {
95 return;
96 }
djsollen@google.com5587ac02013-08-29 20:20:40 +000097
scroggoecce60b2014-07-09 07:26:40 -070098 for (size_t i = 0; i < SK_ARRAY_COUNT(colorTypes); ++i) {
djsollen@google.com5587ac02013-08-29 20:20:40 +000099 SkBitmap bitmaps[2];
100 for (int j = 0; j < 2; ++j) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000101 bitmaps[j].allocPixels(SkImageInfo::Make(WIDTH, HEIGHT,
102 colorTypes[i],
103 kPremul_SkAlphaType));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000104
105 SkCanvas canvas(bitmaps[j]);
106
107 canvas.drawColor(SK_ColorRED);
108
scroggo24519372014-07-22 12:38:55 -0700109 for (size_t k = 0; k < SK_ARRAY_COUNT(layerAlpha); ++k) {
fmalita55b29b22016-01-20 11:17:39 -0800110 SkTLazy<SkPaint> paint;
111 if (layerAlpha[k] != 0xFF) {
112 paint.init()->setAlpha(layerAlpha[k]);
113 }
114
djsollen@google.com5587ac02013-08-29 20:20:40 +0000115 // draw a rect within the layer's bounds and again outside the layer's bounds
fmalita55b29b22016-01-20 11:17:39 -0800116 canvas.saveLayer(SkCanvas::SaveLayerRec(&rect, paint.getMaybeNull(), flags[k]));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000117
djsollen@google.com5587ac02013-08-29 20:20:40 +0000118 if (j) {
scroggo24519372014-07-22 12:38:55 -0700119 // Capture from the first Skia.
120 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000121 REPORTER_ASSERT(reporter, state);
scroggo24519372014-07-22 12:38:55 -0700122
123 // And draw to it in the second Skia.
124 bool success = complex_layers_draw_from_canvas_state(state,
125 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, SPACER);
126 REPORTER_ASSERT(reporter, success);
127
128 // And release it in the *first* Skia.
129 SkCanvasStateUtils::ReleaseCanvasState(state);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000130 } else {
scroggo24519372014-07-22 12:38:55 -0700131 // Draw in the first Skia.
132 complex_layers_draw(&canvas, rect.fLeft, rect.fTop,
133 rect.fRight, rect.fBottom, SPACER);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000134 }
135
djsollen@google.com5587ac02013-08-29 20:20:40 +0000136 canvas.restore();
137
138 // translate the canvas for the next iteration
139 canvas.translate(0, 2*(rect.height() + SPACER));
140 }
141 }
142
143 // now we memcmp the two bitmaps
144 REPORTER_ASSERT(reporter, bitmaps[0].getSize() == bitmaps[1].getSize());
145 REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(),
146 bitmaps[1].getPixels(),
147 bitmaps[0].getSize()));
148 }
149}
scroggo24519372014-07-22 12:38:55 -0700150#endif
djsollen@google.com5587ac02013-08-29 20:20:40 +0000151
152////////////////////////////////////////////////////////////////////////////////
153
reed@google.comb93ba452014-03-10 19:47:58 +0000154#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
scroggo24519372014-07-22 12:38:55 -0700155DEF_TEST(CanvasState_test_complex_clips, reporter) {
djsollen@google.com339e79f2013-09-04 17:16:00 +0000156 const int WIDTH = 400;
157 const int HEIGHT = 400;
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000158 const int SPACER = 10;
djsollen@google.com339e79f2013-09-04 17:16:00 +0000159
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000160 SkIRect layerRect = SkIRect::MakeWH(WIDTH, HEIGHT / 4);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000161 layerRect.inset(2*SPACER, 2*SPACER);
162
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000163 SkIRect clipRect = layerRect;
djsollen@google.com339e79f2013-09-04 17:16:00 +0000164 clipRect.fRight = clipRect.fLeft + (clipRect.width() / 2) - (2*SPACER);
165 clipRect.outset(SPACER, SPACER);
166
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000167 SkIRect regionBounds = clipRect;
djsollen@google.com339e79f2013-09-04 17:16:00 +0000168 regionBounds.offset(clipRect.width() + (2*SPACER), 0);
169
170 SkIRect regionInterior = regionBounds;
171 regionInterior.inset(SPACER*3, SPACER*3);
172
173 SkRegion clipRegion;
174 clipRegion.setRect(regionBounds);
175 clipRegion.op(regionInterior, SkRegion::kDifference_Op);
176
177
178 const SkRegion::Op clipOps[] = { SkRegion::kIntersect_Op,
179 SkRegion::kIntersect_Op,
180 SkRegion::kReplace_Op,
181 };
fmalita55b29b22016-01-20 11:17:39 -0800182 const SkCanvas::SaveLayerFlags flags[] = {
183 static_cast<SkCanvas::SaveLayerFlags>(SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag),
184 0,
185 static_cast<SkCanvas::SaveLayerFlags>(SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag),
djsollen@google.com339e79f2013-09-04 17:16:00 +0000186 };
187 REPORTER_ASSERT(reporter, sizeof(clipOps) == sizeof(flags));
scroggo24519372014-07-22 12:38:55 -0700188
189 bool (*drawFn)(SkCanvasState* state, int32_t l, int32_t t,
190 int32_t r, int32_t b, int32_t clipOp,
191 int32_t regionRects, int32_t* rectCoords);
192
193 OpenLibResult openLibResult(reporter);
halcanary96fcdcc2015-08-27 07:41:13 -0700194 if (openLibResult.handle() != nullptr) {
scroggo24519372014-07-22 12:38:55 -0700195 *(void**) (&drawFn) = dlsym(openLibResult.handle(),
196 "complex_clips_draw_from_canvas_state");
197 } else {
198 drawFn = complex_clips_draw_from_canvas_state;
199 }
200
201 REPORTER_ASSERT(reporter, drawFn);
202 if (!drawFn) {
203 return;
204 }
djsollen@google.com339e79f2013-09-04 17:16:00 +0000205
206 SkBitmap bitmaps[2];
207 for (int i = 0; i < 2; ++i) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000208 bitmaps[i].allocN32Pixels(WIDTH, HEIGHT);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000209
210 SkCanvas canvas(bitmaps[i]);
211
212 canvas.drawColor(SK_ColorRED);
213
214 SkRegion localRegion = clipRegion;
215
fmalita55b29b22016-01-20 11:17:39 -0800216 SkPaint paint;
217 paint.setAlpha(128);
scroggo24519372014-07-22 12:38:55 -0700218 for (size_t j = 0; j < SK_ARRAY_COUNT(flags); ++j) {
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000219 SkRect layerBounds = SkRect::Make(layerRect);
fmalita55b29b22016-01-20 11:17:39 -0800220 canvas.saveLayer(SkCanvas::SaveLayerRec(&layerBounds, &paint, flags[j]));
djsollen@google.com339e79f2013-09-04 17:16:00 +0000221
djsollen@google.com339e79f2013-09-04 17:16:00 +0000222 if (i) {
scroggo24519372014-07-22 12:38:55 -0700223 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000224 REPORTER_ASSERT(reporter, state);
scroggo24519372014-07-22 12:38:55 -0700225
226 SkRegion::Iterator iter(localRegion);
227 SkTDArray<int32_t> rectCoords;
228 for (; !iter.done(); iter.next()) {
229 const SkIRect& rect = iter.rect();
230 *rectCoords.append() = rect.fLeft;
231 *rectCoords.append() = rect.fTop;
232 *rectCoords.append() = rect.fRight;
233 *rectCoords.append() = rect.fBottom;
234 }
235 bool success = drawFn(state, clipRect.fLeft, clipRect.fTop,
236 clipRect.fRight, clipRect.fBottom, clipOps[j],
237 rectCoords.count() / 4, rectCoords.begin());
238 REPORTER_ASSERT(reporter, success);
239
240 SkCanvasStateUtils::ReleaseCanvasState(state);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000241 } else {
scroggo24519372014-07-22 12:38:55 -0700242 complex_clips_draw(&canvas, clipRect.fLeft, clipRect.fTop,
243 clipRect.fRight, clipRect.fBottom, clipOps[j],
244 localRegion);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000245 }
246
djsollen@google.com339e79f2013-09-04 17:16:00 +0000247 canvas.restore();
248
249 // translate the canvas and region for the next iteration
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000250 canvas.translate(0, SkIntToScalar(2*(layerRect.height() + (SPACER))));
djsollen@google.com339e79f2013-09-04 17:16:00 +0000251 localRegion.translate(0, 2*(layerRect.height() + SPACER));
252 }
253 }
254
255 // now we memcmp the two bitmaps
256 REPORTER_ASSERT(reporter, bitmaps[0].getSize() == bitmaps[1].getSize());
257 REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(),
258 bitmaps[1].getPixels(),
259 bitmaps[0].getSize()));
260}
scroggo24519372014-07-22 12:38:55 -0700261#endif
djsollen@google.com339e79f2013-09-04 17:16:00 +0000262
263////////////////////////////////////////////////////////////////////////////////
264
benjaminwagner1c2729c2016-01-22 09:45:14 -0800265#ifdef SK_SUPPORT_LEGACY_DRAWFILTER
266
djsollen@google.com5587ac02013-08-29 20:20:40 +0000267class TestDrawFilter : public SkDrawFilter {
268public:
mtklein36352bf2015-03-25 18:17:31 -0700269 bool filter(SkPaint*, Type) override { return true; }
djsollen@google.com5587ac02013-08-29 20:20:40 +0000270};
271
scroggo24519372014-07-22 12:38:55 -0700272DEF_TEST(CanvasState_test_draw_filters, reporter) {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000273 TestDrawFilter drawFilter;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000274 SkBitmap bitmap;
275 bitmap.allocN32Pixels(10, 10);
276 SkCanvas canvas(bitmap);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000277
278 canvas.setDrawFilter(&drawFilter);
279
280 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
281 REPORTER_ASSERT(reporter, state);
282 SkCanvas* tmpCanvas = SkCanvasStateUtils::CreateFromCanvasState(state);
283 REPORTER_ASSERT(reporter, tmpCanvas);
284
bsalomon49f085d2014-09-05 13:34:00 -0700285 REPORTER_ASSERT(reporter, canvas.getDrawFilter());
halcanary96fcdcc2015-08-27 07:41:13 -0700286 REPORTER_ASSERT(reporter, nullptr == tmpCanvas->getDrawFilter());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000287
288 tmpCanvas->unref();
289 SkCanvasStateUtils::ReleaseCanvasState(state);
290}
291
benjaminwagner1c2729c2016-01-22 09:45:14 -0800292#endif
293
djsollen@google.com5587ac02013-08-29 20:20:40 +0000294////////////////////////////////////////////////////////////////////////////////
295
commit-bot@chromium.org07f6cf32013-09-18 20:15:12 +0000296// we need this function to prevent SkError from printing to stdout
297static void error_callback(SkError code, void* ctx) {}
298
scroggo24519372014-07-22 12:38:55 -0700299DEF_TEST(CanvasState_test_soft_clips, reporter) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000300 SkBitmap bitmap;
301 bitmap.allocN32Pixels(10, 10);
302 SkCanvas canvas(bitmap);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000303
304 SkRRect roundRect;
305 roundRect.setOval(SkRect::MakeWH(5, 5));
306
307 canvas.clipRRect(roundRect, SkRegion::kIntersect_Op, true);
308
halcanary96fcdcc2015-08-27 07:41:13 -0700309 SkSetErrorCallback(error_callback, nullptr);
commit-bot@chromium.org07f6cf32013-09-18 20:15:12 +0000310
djsollen@google.com5587ac02013-08-29 20:20:40 +0000311 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
312 REPORTER_ASSERT(reporter, !state);
commit-bot@chromium.org07f6cf32013-09-18 20:15:12 +0000313
314 REPORTER_ASSERT(reporter, kInvalidOperation_SkError == SkGetLastError());
315 SkClearLastError();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000316}
317
reed@google.comb93ba452014-03-10 19:47:58 +0000318#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
bungemand3ebb482015-08-05 13:57:49 -0700319#include "SkClipStack.h"
scroggo24519372014-07-22 12:38:55 -0700320DEF_TEST(CanvasState_test_saveLayer_clip, reporter) {
senorblanco@chromium.org89f077c2014-02-24 15:16:42 +0000321 const int WIDTH = 100;
322 const int HEIGHT = 100;
323 const int LAYER_WIDTH = 50;
324 const int LAYER_HEIGHT = 50;
325
326 SkBitmap bitmap;
327 bitmap.allocN32Pixels(WIDTH, HEIGHT);
328 SkCanvas canvas(bitmap);
329
330 SkRect bounds = SkRect::MakeWH(SkIntToScalar(LAYER_WIDTH), SkIntToScalar(LAYER_HEIGHT));
331 canvas.clipRect(SkRect::MakeWH(SkIntToScalar(WIDTH), SkIntToScalar(HEIGHT)));
332
333 // Check that saveLayer without the kClipToLayer_SaveFlag leaves the
334 // clip stack unchanged.
fmalita55b29b22016-01-20 11:17:39 -0800335 canvas.saveLayer(SkCanvas::SaveLayerRec(&bounds,
336 nullptr,
337 SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag));
senorblanco@chromium.org89f077c2014-02-24 15:16:42 +0000338 SkRect clipStackBounds;
339 SkClipStack::BoundsType boundsType;
340 canvas.getClipStack()->getBounds(&clipStackBounds, &boundsType);
reedc64eff52015-11-21 12:39:45 -0800341 // The clip stack will return its bounds, or it may be "full" : i.e. empty + inside_out.
342 // Either result is consistent with this test, since the canvas' size is WIDTH/HEIGHT
343 if (SkClipStack::kInsideOut_BoundsType == boundsType) {
344 REPORTER_ASSERT(reporter, clipStackBounds.isEmpty());
345 } else {
346 REPORTER_ASSERT(reporter, clipStackBounds.width() == WIDTH);
347 REPORTER_ASSERT(reporter, clipStackBounds.height() == HEIGHT);
348 }
senorblanco@chromium.org89f077c2014-02-24 15:16:42 +0000349 canvas.restore();
350
351 // Check that saveLayer with the kClipToLayer_SaveFlag sets the clip
352 // stack to the layer bounds.
fmalita55b29b22016-01-20 11:17:39 -0800353 canvas.saveLayer(&bounds, nullptr);
senorblanco@chromium.org89f077c2014-02-24 15:16:42 +0000354 canvas.getClipStack()->getBounds(&clipStackBounds, &boundsType);
355 REPORTER_ASSERT(reporter, clipStackBounds.width() == LAYER_WIDTH);
356 REPORTER_ASSERT(reporter, clipStackBounds.height() == LAYER_HEIGHT);
357
358 canvas.restore();
scroggo24519372014-07-22 12:38:55 -0700359}
reed@google.comb93ba452014-03-10 19:47:58 +0000360#endif