blob: f4db1a53d796b3be949b4c91c3cc0120cec1cacf [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"
17#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);
40 REPORTER_ASSERT_MESSAGE(reporter, fHandle != NULL, "Failed to open library!");
41 } else {
42 fHandle = NULL;
43 }
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 };
74 const SkCanvas::SaveFlags flags[] = { SkCanvas::kARGB_NoClipLayer_SaveFlag,
75 SkCanvas::kARGB_ClipLayer_SaveFlag,
76 SkCanvas::kARGB_NoClipLayer_SaveFlag
77 };
78 REPORTER_ASSERT(reporter, sizeof(layerAlpha) == sizeof(flags));
scroggo24519372014-07-22 12:38:55 -070079
80 bool (*drawFn)(SkCanvasState* state, float l, float t,
81 float r, float b, int32_t s);
82
83 OpenLibResult openLibResult(reporter);
84 if (openLibResult.handle() != NULL) {
85 *(void**) (&drawFn) = dlsym(openLibResult.handle(),
86 "complex_layers_draw_from_canvas_state");
87 } else {
88 drawFn = complex_layers_draw_from_canvas_state;
89 }
90
91 REPORTER_ASSERT(reporter, drawFn);
92 if (!drawFn) {
93 return;
94 }
djsollen@google.com5587ac02013-08-29 20:20:40 +000095
scroggoecce60b2014-07-09 07:26:40 -070096 for (size_t i = 0; i < SK_ARRAY_COUNT(colorTypes); ++i) {
djsollen@google.com5587ac02013-08-29 20:20:40 +000097 SkBitmap bitmaps[2];
98 for (int j = 0; j < 2; ++j) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000099 bitmaps[j].allocPixels(SkImageInfo::Make(WIDTH, HEIGHT,
100 colorTypes[i],
101 kPremul_SkAlphaType));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000102
103 SkCanvas canvas(bitmaps[j]);
104
105 canvas.drawColor(SK_ColorRED);
106
scroggo24519372014-07-22 12:38:55 -0700107 for (size_t k = 0; k < SK_ARRAY_COUNT(layerAlpha); ++k) {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000108 // draw a rect within the layer's bounds and again outside the layer's bounds
109 canvas.saveLayerAlpha(&rect, layerAlpha[k], flags[k]);
110
djsollen@google.com5587ac02013-08-29 20:20:40 +0000111 if (j) {
scroggo24519372014-07-22 12:38:55 -0700112 // Capture from the first Skia.
113 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000114 REPORTER_ASSERT(reporter, state);
scroggo24519372014-07-22 12:38:55 -0700115
116 // And draw to it in the second Skia.
117 bool success = complex_layers_draw_from_canvas_state(state,
118 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, SPACER);
119 REPORTER_ASSERT(reporter, success);
120
121 // And release it in the *first* Skia.
122 SkCanvasStateUtils::ReleaseCanvasState(state);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000123 } else {
scroggo24519372014-07-22 12:38:55 -0700124 // Draw in the first Skia.
125 complex_layers_draw(&canvas, rect.fLeft, rect.fTop,
126 rect.fRight, rect.fBottom, SPACER);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000127 }
128
djsollen@google.com5587ac02013-08-29 20:20:40 +0000129 canvas.restore();
130
131 // translate the canvas for the next iteration
132 canvas.translate(0, 2*(rect.height() + SPACER));
133 }
134 }
135
136 // now we memcmp the two bitmaps
137 REPORTER_ASSERT(reporter, bitmaps[0].getSize() == bitmaps[1].getSize());
138 REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(),
139 bitmaps[1].getPixels(),
140 bitmaps[0].getSize()));
141 }
142}
scroggo24519372014-07-22 12:38:55 -0700143#endif
djsollen@google.com5587ac02013-08-29 20:20:40 +0000144
145////////////////////////////////////////////////////////////////////////////////
146
reed@google.comb93ba452014-03-10 19:47:58 +0000147#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
scroggo24519372014-07-22 12:38:55 -0700148DEF_TEST(CanvasState_test_complex_clips, reporter) {
djsollen@google.com339e79f2013-09-04 17:16:00 +0000149 const int WIDTH = 400;
150 const int HEIGHT = 400;
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000151 const int SPACER = 10;
djsollen@google.com339e79f2013-09-04 17:16:00 +0000152
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000153 SkIRect layerRect = SkIRect::MakeWH(WIDTH, HEIGHT / 4);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000154 layerRect.inset(2*SPACER, 2*SPACER);
155
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000156 SkIRect clipRect = layerRect;
djsollen@google.com339e79f2013-09-04 17:16:00 +0000157 clipRect.fRight = clipRect.fLeft + (clipRect.width() / 2) - (2*SPACER);
158 clipRect.outset(SPACER, SPACER);
159
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000160 SkIRect regionBounds = clipRect;
djsollen@google.com339e79f2013-09-04 17:16:00 +0000161 regionBounds.offset(clipRect.width() + (2*SPACER), 0);
162
163 SkIRect regionInterior = regionBounds;
164 regionInterior.inset(SPACER*3, SPACER*3);
165
166 SkRegion clipRegion;
167 clipRegion.setRect(regionBounds);
168 clipRegion.op(regionInterior, SkRegion::kDifference_Op);
169
170
171 const SkRegion::Op clipOps[] = { SkRegion::kIntersect_Op,
172 SkRegion::kIntersect_Op,
173 SkRegion::kReplace_Op,
174 };
175 const SkCanvas::SaveFlags flags[] = { SkCanvas::kARGB_NoClipLayer_SaveFlag,
176 SkCanvas::kARGB_ClipLayer_SaveFlag,
177 SkCanvas::kARGB_NoClipLayer_SaveFlag,
178 };
179 REPORTER_ASSERT(reporter, sizeof(clipOps) == sizeof(flags));
scroggo24519372014-07-22 12:38:55 -0700180
181 bool (*drawFn)(SkCanvasState* state, int32_t l, int32_t t,
182 int32_t r, int32_t b, int32_t clipOp,
183 int32_t regionRects, int32_t* rectCoords);
184
185 OpenLibResult openLibResult(reporter);
186 if (openLibResult.handle() != NULL) {
187 *(void**) (&drawFn) = dlsym(openLibResult.handle(),
188 "complex_clips_draw_from_canvas_state");
189 } else {
190 drawFn = complex_clips_draw_from_canvas_state;
191 }
192
193 REPORTER_ASSERT(reporter, drawFn);
194 if (!drawFn) {
195 return;
196 }
djsollen@google.com339e79f2013-09-04 17:16:00 +0000197
198 SkBitmap bitmaps[2];
199 for (int i = 0; i < 2; ++i) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000200 bitmaps[i].allocN32Pixels(WIDTH, HEIGHT);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000201
202 SkCanvas canvas(bitmaps[i]);
203
204 canvas.drawColor(SK_ColorRED);
205
206 SkRegion localRegion = clipRegion;
207
scroggo24519372014-07-22 12:38:55 -0700208 for (size_t j = 0; j < SK_ARRAY_COUNT(flags); ++j) {
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000209 SkRect layerBounds = SkRect::Make(layerRect);
210 canvas.saveLayerAlpha(&layerBounds, 128, flags[j]);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000211
djsollen@google.com339e79f2013-09-04 17:16:00 +0000212 if (i) {
scroggo24519372014-07-22 12:38:55 -0700213 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000214 REPORTER_ASSERT(reporter, state);
scroggo24519372014-07-22 12:38:55 -0700215
216 SkRegion::Iterator iter(localRegion);
217 SkTDArray<int32_t> rectCoords;
218 for (; !iter.done(); iter.next()) {
219 const SkIRect& rect = iter.rect();
220 *rectCoords.append() = rect.fLeft;
221 *rectCoords.append() = rect.fTop;
222 *rectCoords.append() = rect.fRight;
223 *rectCoords.append() = rect.fBottom;
224 }
225 bool success = drawFn(state, clipRect.fLeft, clipRect.fTop,
226 clipRect.fRight, clipRect.fBottom, clipOps[j],
227 rectCoords.count() / 4, rectCoords.begin());
228 REPORTER_ASSERT(reporter, success);
229
230 SkCanvasStateUtils::ReleaseCanvasState(state);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000231 } else {
scroggo24519372014-07-22 12:38:55 -0700232 complex_clips_draw(&canvas, clipRect.fLeft, clipRect.fTop,
233 clipRect.fRight, clipRect.fBottom, clipOps[j],
234 localRegion);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000235 }
236
djsollen@google.com339e79f2013-09-04 17:16:00 +0000237 canvas.restore();
238
239 // translate the canvas and region for the next iteration
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000240 canvas.translate(0, SkIntToScalar(2*(layerRect.height() + (SPACER))));
djsollen@google.com339e79f2013-09-04 17:16:00 +0000241 localRegion.translate(0, 2*(layerRect.height() + SPACER));
242 }
243 }
244
245 // now we memcmp the two bitmaps
246 REPORTER_ASSERT(reporter, bitmaps[0].getSize() == bitmaps[1].getSize());
247 REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(),
248 bitmaps[1].getPixels(),
249 bitmaps[0].getSize()));
250}
scroggo24519372014-07-22 12:38:55 -0700251#endif
djsollen@google.com339e79f2013-09-04 17:16:00 +0000252
253////////////////////////////////////////////////////////////////////////////////
254
djsollen@google.com5587ac02013-08-29 20:20:40 +0000255class TestDrawFilter : public SkDrawFilter {
256public:
mtklein36352bf2015-03-25 18:17:31 -0700257 bool filter(SkPaint*, Type) override { return true; }
djsollen@google.com5587ac02013-08-29 20:20:40 +0000258};
259
scroggo24519372014-07-22 12:38:55 -0700260DEF_TEST(CanvasState_test_draw_filters, reporter) {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000261 TestDrawFilter drawFilter;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000262 SkBitmap bitmap;
263 bitmap.allocN32Pixels(10, 10);
264 SkCanvas canvas(bitmap);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000265
266 canvas.setDrawFilter(&drawFilter);
267
268 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
269 REPORTER_ASSERT(reporter, state);
270 SkCanvas* tmpCanvas = SkCanvasStateUtils::CreateFromCanvasState(state);
271 REPORTER_ASSERT(reporter, tmpCanvas);
272
bsalomon49f085d2014-09-05 13:34:00 -0700273 REPORTER_ASSERT(reporter, canvas.getDrawFilter());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000274 REPORTER_ASSERT(reporter, NULL == tmpCanvas->getDrawFilter());
275
276 tmpCanvas->unref();
277 SkCanvasStateUtils::ReleaseCanvasState(state);
278}
279
280////////////////////////////////////////////////////////////////////////////////
281
commit-bot@chromium.org07f6cf32013-09-18 20:15:12 +0000282// we need this function to prevent SkError from printing to stdout
283static void error_callback(SkError code, void* ctx) {}
284
scroggo24519372014-07-22 12:38:55 -0700285DEF_TEST(CanvasState_test_soft_clips, reporter) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000286 SkBitmap bitmap;
287 bitmap.allocN32Pixels(10, 10);
288 SkCanvas canvas(bitmap);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000289
290 SkRRect roundRect;
291 roundRect.setOval(SkRect::MakeWH(5, 5));
292
293 canvas.clipRRect(roundRect, SkRegion::kIntersect_Op, true);
294
commit-bot@chromium.org07f6cf32013-09-18 20:15:12 +0000295 SkSetErrorCallback(error_callback, NULL);
296
djsollen@google.com5587ac02013-08-29 20:20:40 +0000297 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
298 REPORTER_ASSERT(reporter, !state);
commit-bot@chromium.org07f6cf32013-09-18 20:15:12 +0000299
300 REPORTER_ASSERT(reporter, kInvalidOperation_SkError == SkGetLastError());
301 SkClearLastError();
djsollen@google.com5587ac02013-08-29 20:20:40 +0000302}
303
reed@google.comb93ba452014-03-10 19:47:58 +0000304#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
scroggo24519372014-07-22 12:38:55 -0700305DEF_TEST(CanvasState_test_saveLayer_clip, reporter) {
senorblanco@chromium.org89f077c2014-02-24 15:16:42 +0000306 const int WIDTH = 100;
307 const int HEIGHT = 100;
308 const int LAYER_WIDTH = 50;
309 const int LAYER_HEIGHT = 50;
310
311 SkBitmap bitmap;
312 bitmap.allocN32Pixels(WIDTH, HEIGHT);
313 SkCanvas canvas(bitmap);
314
315 SkRect bounds = SkRect::MakeWH(SkIntToScalar(LAYER_WIDTH), SkIntToScalar(LAYER_HEIGHT));
316 canvas.clipRect(SkRect::MakeWH(SkIntToScalar(WIDTH), SkIntToScalar(HEIGHT)));
317
318 // Check that saveLayer without the kClipToLayer_SaveFlag leaves the
319 // clip stack unchanged.
320 canvas.saveLayer(&bounds, NULL, SkCanvas::kARGB_NoClipLayer_SaveFlag);
321 SkRect clipStackBounds;
322 SkClipStack::BoundsType boundsType;
323 canvas.getClipStack()->getBounds(&clipStackBounds, &boundsType);
324 REPORTER_ASSERT(reporter, clipStackBounds.width() == WIDTH);
325 REPORTER_ASSERT(reporter, clipStackBounds.height() == HEIGHT);
326 canvas.restore();
327
328 // Check that saveLayer with the kClipToLayer_SaveFlag sets the clip
329 // stack to the layer bounds.
330 canvas.saveLayer(&bounds, NULL, SkCanvas::kARGB_ClipLayer_SaveFlag);
331 canvas.getClipStack()->getBounds(&clipStackBounds, &boundsType);
332 REPORTER_ASSERT(reporter, clipStackBounds.width() == LAYER_WIDTH);
333 REPORTER_ASSERT(reporter, clipStackBounds.height() == LAYER_HEIGHT);
334
335 canvas.restore();
scroggo24519372014-07-22 12:38:55 -0700336}
reed@google.comb93ba452014-03-10 19:47:58 +0000337#endif