blob: 890e85d3b898076d044b3377d17eb0b4edae7fcb [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"
13#include "SkPaint.h"
djsollen@google.com5587ac02013-08-29 20:20:40 +000014#include "SkRRect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000015#include "SkRect.h"
fmalita55b29b22016-01-20 11:17:39 -080016#include "SkTLazy.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000017#include "Test.h"
djsollen@google.com5587ac02013-08-29 20:20:40 +000018
scroggo24519372014-07-22 12:38:55 -070019// dlopen and the library flag are only used for tests which require this flag.
reed@google.comb93ba452014-03-10 19:47:58 +000020#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
scroggo24519372014-07-22 12:38:55 -070021#include <dlfcn.h>
22
23DEFINE_string(library, "", "Support library to use for CanvasState test. If empty (the default), "
24 "the test will be run without crossing a library boundary. Otherwise, "
25 "it is expected to be a full path to a shared library file, which will"
26 " be dynamically loaded. Functions from the library will be called to "
27 "test SkCanvasState. Instructions for generating the library are in "
28 "gyp/canvas_state_lib.gyp");
29
30
31// This class calls dlopen on the library passed in to the command line flag library, and handles
32// calling dlclose when it goes out of scope.
33class OpenLibResult {
34public:
35 // If the flag library was passed to this run of the test, attempt to open it using dlopen and
36 // report whether it succeeded.
37 OpenLibResult(skiatest::Reporter* reporter) {
38 if (FLAGS_library.count() == 1) {
39 fHandle = dlopen(FLAGS_library[0], RTLD_LAZY | RTLD_LOCAL);
halcanary96fcdcc2015-08-27 07:41:13 -070040 REPORTER_ASSERT_MESSAGE(reporter, fHandle != nullptr, "Failed to open library!");
scroggo24519372014-07-22 12:38:55 -070041 } else {
halcanary96fcdcc2015-08-27 07:41:13 -070042 fHandle = nullptr;
scroggo24519372014-07-22 12:38:55 -070043 }
44 }
45
46 // Automatically call dlclose when going out of scope.
47 ~OpenLibResult() {
48 if (fHandle) {
49 dlclose(fHandle);
50 }
51 }
52
53 // Pointer to the shared library object.
54 void* handle() { return fHandle; }
55
56private:
57 void* fHandle;
58};
59
60DEF_TEST(CanvasState_test_complex_layers, reporter) {
djsollen@google.com5587ac02013-08-29 20:20:40 +000061 const int WIDTH = 400;
62 const int HEIGHT = 400;
63 const int SPACER = 10;
64
djsollen@google.com20146b32013-08-29 20:36:22 +000065 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(SPACER), SkIntToScalar(SPACER),
66 SkIntToScalar(WIDTH-(2*SPACER)),
67 SkIntToScalar((HEIGHT-(2*SPACER)) / 7));
djsollen@google.com5587ac02013-08-29 20:20:40 +000068
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000069 const SkColorType colorTypes[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000070 kRGB_565_SkColorType, kN32_SkColorType
djsollen@google.com5587ac02013-08-29 20:20:40 +000071 };
djsollen@google.com5587ac02013-08-29 20:20:40 +000072
73 const int layerAlpha[] = { 255, 255, 0 };
fmalita55b29b22016-01-20 11:17:39 -080074 const SkCanvas::SaveLayerFlags flags[] = {
75 static_cast<SkCanvas::SaveLayerFlags>(SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag),
76 0,
77 static_cast<SkCanvas::SaveLayerFlags>(SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag),
djsollen@google.com5587ac02013-08-29 20:20:40 +000078 };
79 REPORTER_ASSERT(reporter, sizeof(layerAlpha) == sizeof(flags));
scroggo24519372014-07-22 12:38:55 -070080
81 bool (*drawFn)(SkCanvasState* state, float l, float t,
82 float r, float b, int32_t s);
83
84 OpenLibResult openLibResult(reporter);
halcanary96fcdcc2015-08-27 07:41:13 -070085 if (openLibResult.handle() != nullptr) {
scroggo24519372014-07-22 12:38:55 -070086 *(void**) (&drawFn) = dlsym(openLibResult.handle(),
87 "complex_layers_draw_from_canvas_state");
88 } else {
89 drawFn = complex_layers_draw_from_canvas_state;
90 }
91
92 REPORTER_ASSERT(reporter, drawFn);
93 if (!drawFn) {
94 return;
95 }
djsollen@google.com5587ac02013-08-29 20:20:40 +000096
scroggoecce60b2014-07-09 07:26:40 -070097 for (size_t i = 0; i < SK_ARRAY_COUNT(colorTypes); ++i) {
djsollen@google.com5587ac02013-08-29 20:20:40 +000098 SkBitmap bitmaps[2];
99 for (int j = 0; j < 2; ++j) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000100 bitmaps[j].allocPixels(SkImageInfo::Make(WIDTH, HEIGHT,
101 colorTypes[i],
102 kPremul_SkAlphaType));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000103
104 SkCanvas canvas(bitmaps[j]);
105
106 canvas.drawColor(SK_ColorRED);
107
scroggo24519372014-07-22 12:38:55 -0700108 for (size_t k = 0; k < SK_ARRAY_COUNT(layerAlpha); ++k) {
fmalita55b29b22016-01-20 11:17:39 -0800109 SkTLazy<SkPaint> paint;
110 if (layerAlpha[k] != 0xFF) {
111 paint.init()->setAlpha(layerAlpha[k]);
112 }
113
djsollen@google.com5587ac02013-08-29 20:20:40 +0000114 // draw a rect within the layer's bounds and again outside the layer's bounds
fmalita55b29b22016-01-20 11:17:39 -0800115 canvas.saveLayer(SkCanvas::SaveLayerRec(&rect, paint.getMaybeNull(), flags[k]));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000116
djsollen@google.com5587ac02013-08-29 20:20:40 +0000117 if (j) {
scroggo24519372014-07-22 12:38:55 -0700118 // Capture from the first Skia.
119 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000120 REPORTER_ASSERT(reporter, state);
scroggo24519372014-07-22 12:38:55 -0700121
122 // And draw to it in the second Skia.
123 bool success = complex_layers_draw_from_canvas_state(state,
124 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, SPACER);
125 REPORTER_ASSERT(reporter, success);
126
127 // And release it in the *first* Skia.
128 SkCanvasStateUtils::ReleaseCanvasState(state);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000129 } else {
scroggo24519372014-07-22 12:38:55 -0700130 // Draw in the first Skia.
131 complex_layers_draw(&canvas, rect.fLeft, rect.fTop,
132 rect.fRight, rect.fBottom, SPACER);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000133 }
134
djsollen@google.com5587ac02013-08-29 20:20:40 +0000135 canvas.restore();
136
137 // translate the canvas for the next iteration
138 canvas.translate(0, 2*(rect.height() + SPACER));
139 }
140 }
141
142 // now we memcmp the two bitmaps
143 REPORTER_ASSERT(reporter, bitmaps[0].getSize() == bitmaps[1].getSize());
144 REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(),
145 bitmaps[1].getPixels(),
146 bitmaps[0].getSize()));
147 }
148}
scroggo24519372014-07-22 12:38:55 -0700149#endif
djsollen@google.com5587ac02013-08-29 20:20:40 +0000150
151////////////////////////////////////////////////////////////////////////////////
152
reed@google.comb93ba452014-03-10 19:47:58 +0000153#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
scroggo24519372014-07-22 12:38:55 -0700154DEF_TEST(CanvasState_test_complex_clips, reporter) {
djsollen@google.com339e79f2013-09-04 17:16:00 +0000155 const int WIDTH = 400;
156 const int HEIGHT = 400;
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000157 const int SPACER = 10;
djsollen@google.com339e79f2013-09-04 17:16:00 +0000158
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000159 SkIRect layerRect = SkIRect::MakeWH(WIDTH, HEIGHT / 4);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000160 layerRect.inset(2*SPACER, 2*SPACER);
161
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000162 SkIRect clipRect = layerRect;
djsollen@google.com339e79f2013-09-04 17:16:00 +0000163 clipRect.fRight = clipRect.fLeft + (clipRect.width() / 2) - (2*SPACER);
164 clipRect.outset(SPACER, SPACER);
165
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000166 SkIRect regionBounds = clipRect;
djsollen@google.com339e79f2013-09-04 17:16:00 +0000167 regionBounds.offset(clipRect.width() + (2*SPACER), 0);
168
169 SkIRect regionInterior = regionBounds;
170 regionInterior.inset(SPACER*3, SPACER*3);
171
172 SkRegion clipRegion;
173 clipRegion.setRect(regionBounds);
174 clipRegion.op(regionInterior, SkRegion::kDifference_Op);
175
176
177 const SkRegion::Op clipOps[] = { SkRegion::kIntersect_Op,
178 SkRegion::kIntersect_Op,
179 SkRegion::kReplace_Op,
180 };
fmalita55b29b22016-01-20 11:17:39 -0800181 const SkCanvas::SaveLayerFlags flags[] = {
182 static_cast<SkCanvas::SaveLayerFlags>(SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag),
183 0,
184 static_cast<SkCanvas::SaveLayerFlags>(SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag),
djsollen@google.com339e79f2013-09-04 17:16:00 +0000185 };
186 REPORTER_ASSERT(reporter, sizeof(clipOps) == sizeof(flags));
scroggo24519372014-07-22 12:38:55 -0700187
188 bool (*drawFn)(SkCanvasState* state, int32_t l, int32_t t,
189 int32_t r, int32_t b, int32_t clipOp,
190 int32_t regionRects, int32_t* rectCoords);
191
192 OpenLibResult openLibResult(reporter);
halcanary96fcdcc2015-08-27 07:41:13 -0700193 if (openLibResult.handle() != nullptr) {
scroggo24519372014-07-22 12:38:55 -0700194 *(void**) (&drawFn) = dlsym(openLibResult.handle(),
195 "complex_clips_draw_from_canvas_state");
196 } else {
197 drawFn = complex_clips_draw_from_canvas_state;
198 }
199
200 REPORTER_ASSERT(reporter, drawFn);
201 if (!drawFn) {
202 return;
203 }
djsollen@google.com339e79f2013-09-04 17:16:00 +0000204
205 SkBitmap bitmaps[2];
206 for (int i = 0; i < 2; ++i) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000207 bitmaps[i].allocN32Pixels(WIDTH, HEIGHT);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000208
209 SkCanvas canvas(bitmaps[i]);
210
211 canvas.drawColor(SK_ColorRED);
212
213 SkRegion localRegion = clipRegion;
214
fmalita55b29b22016-01-20 11:17:39 -0800215 SkPaint paint;
216 paint.setAlpha(128);
scroggo24519372014-07-22 12:38:55 -0700217 for (size_t j = 0; j < SK_ARRAY_COUNT(flags); ++j) {
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000218 SkRect layerBounds = SkRect::Make(layerRect);
fmalita55b29b22016-01-20 11:17:39 -0800219 canvas.saveLayer(SkCanvas::SaveLayerRec(&layerBounds, &paint, flags[j]));
djsollen@google.com339e79f2013-09-04 17:16:00 +0000220
djsollen@google.com339e79f2013-09-04 17:16:00 +0000221 if (i) {
scroggo24519372014-07-22 12:38:55 -0700222 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000223 REPORTER_ASSERT(reporter, state);
scroggo24519372014-07-22 12:38:55 -0700224
225 SkRegion::Iterator iter(localRegion);
226 SkTDArray<int32_t> rectCoords;
227 for (; !iter.done(); iter.next()) {
228 const SkIRect& rect = iter.rect();
229 *rectCoords.append() = rect.fLeft;
230 *rectCoords.append() = rect.fTop;
231 *rectCoords.append() = rect.fRight;
232 *rectCoords.append() = rect.fBottom;
233 }
234 bool success = drawFn(state, clipRect.fLeft, clipRect.fTop,
235 clipRect.fRight, clipRect.fBottom, clipOps[j],
236 rectCoords.count() / 4, rectCoords.begin());
237 REPORTER_ASSERT(reporter, success);
238
239 SkCanvasStateUtils::ReleaseCanvasState(state);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000240 } else {
scroggo24519372014-07-22 12:38:55 -0700241 complex_clips_draw(&canvas, clipRect.fLeft, clipRect.fTop,
242 clipRect.fRight, clipRect.fBottom, clipOps[j],
243 localRegion);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000244 }
245
djsollen@google.com339e79f2013-09-04 17:16:00 +0000246 canvas.restore();
247
248 // translate the canvas and region for the next iteration
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000249 canvas.translate(0, SkIntToScalar(2*(layerRect.height() + (SPACER))));
djsollen@google.com339e79f2013-09-04 17:16:00 +0000250 localRegion.translate(0, 2*(layerRect.height() + SPACER));
251 }
252 }
253
254 // now we memcmp the two bitmaps
255 REPORTER_ASSERT(reporter, bitmaps[0].getSize() == bitmaps[1].getSize());
256 REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(),
257 bitmaps[1].getPixels(),
258 bitmaps[0].getSize()));
259}
scroggo24519372014-07-22 12:38:55 -0700260#endif
djsollen@google.com339e79f2013-09-04 17:16:00 +0000261
262////////////////////////////////////////////////////////////////////////////////
263
benjaminwagner1c2729c2016-01-22 09:45:14 -0800264#ifdef SK_SUPPORT_LEGACY_DRAWFILTER
265
djsollen@google.com5587ac02013-08-29 20:20:40 +0000266class TestDrawFilter : public SkDrawFilter {
267public:
mtklein36352bf2015-03-25 18:17:31 -0700268 bool filter(SkPaint*, Type) override { return true; }
djsollen@google.com5587ac02013-08-29 20:20:40 +0000269};
270
scroggo24519372014-07-22 12:38:55 -0700271DEF_TEST(CanvasState_test_draw_filters, reporter) {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000272 TestDrawFilter drawFilter;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000273 SkBitmap bitmap;
274 bitmap.allocN32Pixels(10, 10);
275 SkCanvas canvas(bitmap);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000276
277 canvas.setDrawFilter(&drawFilter);
278
279 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
280 REPORTER_ASSERT(reporter, state);
281 SkCanvas* tmpCanvas = SkCanvasStateUtils::CreateFromCanvasState(state);
282 REPORTER_ASSERT(reporter, tmpCanvas);
283
bsalomon49f085d2014-09-05 13:34:00 -0700284 REPORTER_ASSERT(reporter, canvas.getDrawFilter());
halcanary96fcdcc2015-08-27 07:41:13 -0700285 REPORTER_ASSERT(reporter, nullptr == tmpCanvas->getDrawFilter());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000286
287 tmpCanvas->unref();
288 SkCanvasStateUtils::ReleaseCanvasState(state);
289}
290
benjaminwagner1c2729c2016-01-22 09:45:14 -0800291#endif
292
djsollen@google.com5587ac02013-08-29 20:20:40 +0000293////////////////////////////////////////////////////////////////////////////////
294
scroggo24519372014-07-22 12:38:55 -0700295DEF_TEST(CanvasState_test_soft_clips, reporter) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000296 SkBitmap bitmap;
297 bitmap.allocN32Pixels(10, 10);
298 SkCanvas canvas(bitmap);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000299
300 SkRRect roundRect;
301 roundRect.setOval(SkRect::MakeWH(5, 5));
302
reed73603f32016-09-20 08:42:38 -0700303 canvas.clipRRect(roundRect, SkCanvas::kIntersect_Op, true);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000304
305 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
306 REPORTER_ASSERT(reporter, !state);
307}
308
reed@google.comb93ba452014-03-10 19:47:58 +0000309#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
bungemand3ebb482015-08-05 13:57:49 -0700310#include "SkClipStack.h"
scroggo24519372014-07-22 12:38:55 -0700311DEF_TEST(CanvasState_test_saveLayer_clip, reporter) {
senorblanco@chromium.org89f077c2014-02-24 15:16:42 +0000312 const int WIDTH = 100;
313 const int HEIGHT = 100;
314 const int LAYER_WIDTH = 50;
315 const int LAYER_HEIGHT = 50;
316
317 SkBitmap bitmap;
318 bitmap.allocN32Pixels(WIDTH, HEIGHT);
319 SkCanvas canvas(bitmap);
320
321 SkRect bounds = SkRect::MakeWH(SkIntToScalar(LAYER_WIDTH), SkIntToScalar(LAYER_HEIGHT));
322 canvas.clipRect(SkRect::MakeWH(SkIntToScalar(WIDTH), SkIntToScalar(HEIGHT)));
323
324 // Check that saveLayer without the kClipToLayer_SaveFlag leaves the
325 // clip stack unchanged.
fmalita55b29b22016-01-20 11:17:39 -0800326 canvas.saveLayer(SkCanvas::SaveLayerRec(&bounds,
327 nullptr,
328 SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag));
senorblanco@chromium.org89f077c2014-02-24 15:16:42 +0000329 SkRect clipStackBounds;
330 SkClipStack::BoundsType boundsType;
331 canvas.getClipStack()->getBounds(&clipStackBounds, &boundsType);
reedc64eff52015-11-21 12:39:45 -0800332 // The clip stack will return its bounds, or it may be "full" : i.e. empty + inside_out.
333 // Either result is consistent with this test, since the canvas' size is WIDTH/HEIGHT
334 if (SkClipStack::kInsideOut_BoundsType == boundsType) {
335 REPORTER_ASSERT(reporter, clipStackBounds.isEmpty());
336 } else {
337 REPORTER_ASSERT(reporter, clipStackBounds.width() == WIDTH);
338 REPORTER_ASSERT(reporter, clipStackBounds.height() == HEIGHT);
339 }
senorblanco@chromium.org89f077c2014-02-24 15:16:42 +0000340 canvas.restore();
341
342 // Check that saveLayer with the kClipToLayer_SaveFlag sets the clip
343 // stack to the layer bounds.
fmalita55b29b22016-01-20 11:17:39 -0800344 canvas.saveLayer(&bounds, nullptr);
senorblanco@chromium.org89f077c2014-02-24 15:16:42 +0000345 canvas.getClipStack()->getBounds(&clipStackBounds, &boundsType);
346 REPORTER_ASSERT(reporter, clipStackBounds.width() == LAYER_WIDTH);
347 REPORTER_ASSERT(reporter, clipStackBounds.height() == LAYER_HEIGHT);
348
349 canvas.restore();
scroggo24519372014-07-22 12:38:55 -0700350}
reed@google.comb93ba452014-03-10 19:47:58 +0000351#endif