blob: 5b3b3f34db93eaff7a1d9aafd573f0badb93d95f [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"
Florin Malitaab244f02017-05-03 19:16:58 +00009#include "SkBitmap.h"
Cary Clark7eddfb82018-03-13 14:41:10 -040010#include "SkCanvasPriv.h"
Ben Wagner501c17c2018-03-12 20:04:31 +000011#include "SkCanvasStateUtils.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040012#include "SkClipOpPriv.h"
13#include "SkColor.h"
scroggo24519372014-07-22 12:38:55 -070014#include "SkCommandLineFlags.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040015#include "SkImageInfo.h"
djsollen@google.com5587ac02013-08-29 20:20:40 +000016#include "SkPaint.h"
djsollen@google.com5587ac02013-08-29 20:20:40 +000017#include "SkRRect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000018#include "SkRect.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040019#include "SkRegion.h"
20#include "SkScalar.h"
21#include "SkTDArray.h"
fmalita55b29b22016-01-20 11:17:39 -080022#include "SkTLazy.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040023#include "SkTypes.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000024#include "Test.h"
djsollen@google.com5587ac02013-08-29 20:20:40 +000025
Ben Wagnerb607a8f2018-03-12 13:46:21 -040026#include <cstring>
27#include <memory>
28
29class SkCanvasState;
30
scroggo24519372014-07-22 12:38:55 -070031// dlopen and the library flag are only used for tests which require this flag.
reed@google.comb93ba452014-03-10 19:47:58 +000032#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
scroggo24519372014-07-22 12:38:55 -070033#include <dlfcn.h>
34
35DEFINE_string(library, "", "Support library to use for CanvasState test. If empty (the default), "
36 "the test will be run without crossing a library boundary. Otherwise, "
37 "it is expected to be a full path to a shared library file, which will"
38 " be dynamically loaded. Functions from the library will be called to "
39 "test SkCanvasState. Instructions for generating the library are in "
40 "gyp/canvas_state_lib.gyp");
41
42
43// This class calls dlopen on the library passed in to the command line flag library, and handles
44// calling dlclose when it goes out of scope.
45class OpenLibResult {
46public:
47 // If the flag library was passed to this run of the test, attempt to open it using dlopen and
48 // report whether it succeeded.
49 OpenLibResult(skiatest::Reporter* reporter) {
50 if (FLAGS_library.count() == 1) {
51 fHandle = dlopen(FLAGS_library[0], RTLD_LAZY | RTLD_LOCAL);
Brian Salomon1c80e992018-01-29 09:50:47 -050052 REPORTER_ASSERT(reporter, fHandle != nullptr, "Failed to open library!");
scroggo24519372014-07-22 12:38:55 -070053 } else {
halcanary96fcdcc2015-08-27 07:41:13 -070054 fHandle = nullptr;
scroggo24519372014-07-22 12:38:55 -070055 }
56 }
57
58 // Automatically call dlclose when going out of scope.
59 ~OpenLibResult() {
60 if (fHandle) {
61 dlclose(fHandle);
62 }
63 }
64
65 // Pointer to the shared library object.
66 void* handle() { return fHandle; }
67
68private:
69 void* fHandle;
70};
71
72DEF_TEST(CanvasState_test_complex_layers, reporter) {
djsollen@google.com5587ac02013-08-29 20:20:40 +000073 const int WIDTH = 400;
74 const int HEIGHT = 400;
75 const int SPACER = 10;
76
djsollen@google.com20146b32013-08-29 20:36:22 +000077 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(SPACER), SkIntToScalar(SPACER),
78 SkIntToScalar(WIDTH-(2*SPACER)),
79 SkIntToScalar((HEIGHT-(2*SPACER)) / 7));
djsollen@google.com5587ac02013-08-29 20:20:40 +000080
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000081 const SkColorType colorTypes[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000082 kRGB_565_SkColorType, kN32_SkColorType
djsollen@google.com5587ac02013-08-29 20:20:40 +000083 };
djsollen@google.com5587ac02013-08-29 20:20:40 +000084
85 const int layerAlpha[] = { 255, 255, 0 };
fmalita55b29b22016-01-20 11:17:39 -080086 const SkCanvas::SaveLayerFlags flags[] = {
Cary Clark7eddfb82018-03-13 14:41:10 -040087 static_cast<SkCanvas::SaveLayerFlags>(SkCanvasPriv::kDontClipToLayer_SaveLayerFlag),
fmalita55b29b22016-01-20 11:17:39 -080088 0,
Cary Clark7eddfb82018-03-13 14:41:10 -040089 static_cast<SkCanvas::SaveLayerFlags>(SkCanvasPriv::kDontClipToLayer_SaveLayerFlag),
djsollen@google.com5587ac02013-08-29 20:20:40 +000090 };
91 REPORTER_ASSERT(reporter, sizeof(layerAlpha) == sizeof(flags));
scroggo24519372014-07-22 12:38:55 -070092
93 bool (*drawFn)(SkCanvasState* state, float l, float t,
94 float r, float b, int32_t s);
95
96 OpenLibResult openLibResult(reporter);
halcanary96fcdcc2015-08-27 07:41:13 -070097 if (openLibResult.handle() != nullptr) {
scroggo24519372014-07-22 12:38:55 -070098 *(void**) (&drawFn) = dlsym(openLibResult.handle(),
99 "complex_layers_draw_from_canvas_state");
100 } else {
101 drawFn = complex_layers_draw_from_canvas_state;
102 }
103
104 REPORTER_ASSERT(reporter, drawFn);
105 if (!drawFn) {
106 return;
107 }
djsollen@google.com5587ac02013-08-29 20:20:40 +0000108
scroggoecce60b2014-07-09 07:26:40 -0700109 for (size_t i = 0; i < SK_ARRAY_COUNT(colorTypes); ++i) {
djsollen@google.com5587ac02013-08-29 20:20:40 +0000110 SkBitmap bitmaps[2];
111 for (int j = 0; j < 2; ++j) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000112 bitmaps[j].allocPixels(SkImageInfo::Make(WIDTH, HEIGHT,
113 colorTypes[i],
114 kPremul_SkAlphaType));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000115
116 SkCanvas canvas(bitmaps[j]);
117
118 canvas.drawColor(SK_ColorRED);
119
scroggo24519372014-07-22 12:38:55 -0700120 for (size_t k = 0; k < SK_ARRAY_COUNT(layerAlpha); ++k) {
fmalita55b29b22016-01-20 11:17:39 -0800121 SkTLazy<SkPaint> paint;
122 if (layerAlpha[k] != 0xFF) {
123 paint.init()->setAlpha(layerAlpha[k]);
124 }
125
djsollen@google.com5587ac02013-08-29 20:20:40 +0000126 // draw a rect within the layer's bounds and again outside the layer's bounds
fmalita55b29b22016-01-20 11:17:39 -0800127 canvas.saveLayer(SkCanvas::SaveLayerRec(&rect, paint.getMaybeNull(), flags[k]));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000128
djsollen@google.com5587ac02013-08-29 20:20:40 +0000129 if (j) {
scroggo24519372014-07-22 12:38:55 -0700130 // Capture from the first Skia.
131 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000132 REPORTER_ASSERT(reporter, state);
scroggo24519372014-07-22 12:38:55 -0700133
134 // And draw to it in the second Skia.
135 bool success = complex_layers_draw_from_canvas_state(state,
136 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, SPACER);
137 REPORTER_ASSERT(reporter, success);
138
139 // And release it in the *first* Skia.
140 SkCanvasStateUtils::ReleaseCanvasState(state);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000141 } else {
scroggo24519372014-07-22 12:38:55 -0700142 // Draw in the first Skia.
143 complex_layers_draw(&canvas, rect.fLeft, rect.fTop,
144 rect.fRight, rect.fBottom, SPACER);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000145 }
146
djsollen@google.com5587ac02013-08-29 20:20:40 +0000147 canvas.restore();
148
149 // translate the canvas for the next iteration
150 canvas.translate(0, 2*(rect.height() + SPACER));
151 }
152 }
153
154 // now we memcmp the two bitmaps
Mike Reeddf071d72017-10-09 13:09:55 -0400155 REPORTER_ASSERT(reporter, bitmaps[0].computeByteSize() == bitmaps[1].computeByteSize());
djsollen@google.com5587ac02013-08-29 20:20:40 +0000156 REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(),
157 bitmaps[1].getPixels(),
Mike Reeddf071d72017-10-09 13:09:55 -0400158 bitmaps[0].computeByteSize()));
djsollen@google.com5587ac02013-08-29 20:20:40 +0000159 }
160}
scroggo24519372014-07-22 12:38:55 -0700161#endif
djsollen@google.com5587ac02013-08-29 20:20:40 +0000162
163////////////////////////////////////////////////////////////////////////////////
164
reed@google.comb93ba452014-03-10 19:47:58 +0000165#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
scroggo24519372014-07-22 12:38:55 -0700166DEF_TEST(CanvasState_test_complex_clips, reporter) {
djsollen@google.com339e79f2013-09-04 17:16:00 +0000167 const int WIDTH = 400;
168 const int HEIGHT = 400;
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000169 const int SPACER = 10;
djsollen@google.com339e79f2013-09-04 17:16:00 +0000170
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000171 SkIRect layerRect = SkIRect::MakeWH(WIDTH, HEIGHT / 4);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000172 layerRect.inset(2*SPACER, 2*SPACER);
173
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000174 SkIRect clipRect = layerRect;
djsollen@google.com339e79f2013-09-04 17:16:00 +0000175 clipRect.fRight = clipRect.fLeft + (clipRect.width() / 2) - (2*SPACER);
176 clipRect.outset(SPACER, SPACER);
177
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000178 SkIRect regionBounds = clipRect;
djsollen@google.com339e79f2013-09-04 17:16:00 +0000179 regionBounds.offset(clipRect.width() + (2*SPACER), 0);
180
181 SkIRect regionInterior = regionBounds;
182 regionInterior.inset(SPACER*3, SPACER*3);
183
184 SkRegion clipRegion;
185 clipRegion.setRect(regionBounds);
186 clipRegion.op(regionInterior, SkRegion::kDifference_Op);
187
188
189 const SkRegion::Op clipOps[] = { SkRegion::kIntersect_Op,
190 SkRegion::kIntersect_Op,
191 SkRegion::kReplace_Op,
192 };
fmalita55b29b22016-01-20 11:17:39 -0800193 const SkCanvas::SaveLayerFlags flags[] = {
Cary Clark7eddfb82018-03-13 14:41:10 -0400194 static_cast<SkCanvas::SaveLayerFlags>(SkCanvasPriv::kDontClipToLayer_SaveLayerFlag),
fmalita55b29b22016-01-20 11:17:39 -0800195 0,
Cary Clark7eddfb82018-03-13 14:41:10 -0400196 static_cast<SkCanvas::SaveLayerFlags>(SkCanvasPriv::kDontClipToLayer_SaveLayerFlag),
djsollen@google.com339e79f2013-09-04 17:16:00 +0000197 };
198 REPORTER_ASSERT(reporter, sizeof(clipOps) == sizeof(flags));
scroggo24519372014-07-22 12:38:55 -0700199
200 bool (*drawFn)(SkCanvasState* state, int32_t l, int32_t t,
201 int32_t r, int32_t b, int32_t clipOp,
202 int32_t regionRects, int32_t* rectCoords);
203
204 OpenLibResult openLibResult(reporter);
halcanary96fcdcc2015-08-27 07:41:13 -0700205 if (openLibResult.handle() != nullptr) {
scroggo24519372014-07-22 12:38:55 -0700206 *(void**) (&drawFn) = dlsym(openLibResult.handle(),
207 "complex_clips_draw_from_canvas_state");
208 } else {
209 drawFn = complex_clips_draw_from_canvas_state;
210 }
211
212 REPORTER_ASSERT(reporter, drawFn);
213 if (!drawFn) {
214 return;
215 }
djsollen@google.com339e79f2013-09-04 17:16:00 +0000216
217 SkBitmap bitmaps[2];
218 for (int i = 0; i < 2; ++i) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000219 bitmaps[i].allocN32Pixels(WIDTH, HEIGHT);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000220
221 SkCanvas canvas(bitmaps[i]);
222
223 canvas.drawColor(SK_ColorRED);
224
225 SkRegion localRegion = clipRegion;
226
fmalita55b29b22016-01-20 11:17:39 -0800227 SkPaint paint;
228 paint.setAlpha(128);
scroggo24519372014-07-22 12:38:55 -0700229 for (size_t j = 0; j < SK_ARRAY_COUNT(flags); ++j) {
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000230 SkRect layerBounds = SkRect::Make(layerRect);
fmalita55b29b22016-01-20 11:17:39 -0800231 canvas.saveLayer(SkCanvas::SaveLayerRec(&layerBounds, &paint, flags[j]));
djsollen@google.com339e79f2013-09-04 17:16:00 +0000232
djsollen@google.com339e79f2013-09-04 17:16:00 +0000233 if (i) {
scroggo24519372014-07-22 12:38:55 -0700234 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000235 REPORTER_ASSERT(reporter, state);
scroggo24519372014-07-22 12:38:55 -0700236
237 SkRegion::Iterator iter(localRegion);
238 SkTDArray<int32_t> rectCoords;
239 for (; !iter.done(); iter.next()) {
240 const SkIRect& rect = iter.rect();
241 *rectCoords.append() = rect.fLeft;
242 *rectCoords.append() = rect.fTop;
243 *rectCoords.append() = rect.fRight;
244 *rectCoords.append() = rect.fBottom;
245 }
246 bool success = drawFn(state, clipRect.fLeft, clipRect.fTop,
247 clipRect.fRight, clipRect.fBottom, clipOps[j],
248 rectCoords.count() / 4, rectCoords.begin());
249 REPORTER_ASSERT(reporter, success);
250
251 SkCanvasStateUtils::ReleaseCanvasState(state);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000252 } else {
scroggo24519372014-07-22 12:38:55 -0700253 complex_clips_draw(&canvas, clipRect.fLeft, clipRect.fTop,
254 clipRect.fRight, clipRect.fBottom, clipOps[j],
255 localRegion);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000256 }
257
djsollen@google.com339e79f2013-09-04 17:16:00 +0000258 canvas.restore();
259
260 // translate the canvas and region for the next iteration
djsollen@google.com1037c7b2013-09-04 18:20:30 +0000261 canvas.translate(0, SkIntToScalar(2*(layerRect.height() + (SPACER))));
djsollen@google.com339e79f2013-09-04 17:16:00 +0000262 localRegion.translate(0, 2*(layerRect.height() + SPACER));
263 }
264 }
265
266 // now we memcmp the two bitmaps
Mike Reeddf071d72017-10-09 13:09:55 -0400267 REPORTER_ASSERT(reporter, bitmaps[0].computeByteSize() == bitmaps[1].computeByteSize());
djsollen@google.com339e79f2013-09-04 17:16:00 +0000268 REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(),
269 bitmaps[1].getPixels(),
Mike Reeddf071d72017-10-09 13:09:55 -0400270 bitmaps[0].computeByteSize()));
djsollen@google.com339e79f2013-09-04 17:16:00 +0000271}
scroggo24519372014-07-22 12:38:55 -0700272#endif
djsollen@google.com339e79f2013-09-04 17:16:00 +0000273
274////////////////////////////////////////////////////////////////////////////////
275
scroggo24519372014-07-22 12:38:55 -0700276DEF_TEST(CanvasState_test_soft_clips, reporter) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000277 SkBitmap bitmap;
278 bitmap.allocN32Pixels(10, 10);
279 SkCanvas canvas(bitmap);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000280
281 SkRRect roundRect;
282 roundRect.setOval(SkRect::MakeWH(5, 5));
283
Mike Reedc1f77742016-12-09 09:00:50 -0500284 canvas.clipRRect(roundRect, kIntersect_SkClipOp, true);
djsollen@google.com5587ac02013-08-29 20:20:40 +0000285
286 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
287 REPORTER_ASSERT(reporter, !state);
288}
289
scroggo24519372014-07-22 12:38:55 -0700290DEF_TEST(CanvasState_test_saveLayer_clip, reporter) {
Mike Reed3726a4a2017-01-19 11:36:41 -0500291#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
Cary Clark7eddfb82018-03-13 14:41:10 -0400292 static_assert(SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag ==
293 SkCanvasPriv::kDontClipToLayer_SaveLayerFlag, "");
Mike Reed3726a4a2017-01-19 11:36:41 -0500294#endif
senorblanco@chromium.org89f077c2014-02-24 15:16:42 +0000295 const int WIDTH = 100;
296 const int HEIGHT = 100;
297 const int LAYER_WIDTH = 50;
298 const int LAYER_HEIGHT = 50;
299
300 SkBitmap bitmap;
301 bitmap.allocN32Pixels(WIDTH, HEIGHT);
302 SkCanvas canvas(bitmap);
303
304 SkRect bounds = SkRect::MakeWH(SkIntToScalar(LAYER_WIDTH), SkIntToScalar(LAYER_HEIGHT));
305 canvas.clipRect(SkRect::MakeWH(SkIntToScalar(WIDTH), SkIntToScalar(HEIGHT)));
306
Mike Reed3726a4a2017-01-19 11:36:41 -0500307 SkIRect devClip;
308 // Check that saveLayer without the kClipToLayer_SaveFlag leaves the clip unchanged.
Cary Clark7eddfb82018-03-13 14:41:10 -0400309 canvas.saveLayer(SkCanvas::SaveLayerRec(&bounds, nullptr,
310 (SkCanvas::SaveLayerFlags) SkCanvasPriv::kDontClipToLayer_SaveLayerFlag));
Mike Reed918e1442017-01-23 11:39:45 -0500311 devClip = canvas.getDeviceClipBounds();
Mike Reed3726a4a2017-01-19 11:36:41 -0500312 REPORTER_ASSERT(reporter, canvas.isClipRect());
313 REPORTER_ASSERT(reporter, devClip.width() == WIDTH);
314 REPORTER_ASSERT(reporter, devClip.height() == HEIGHT);
senorblanco@chromium.org89f077c2014-02-24 15:16:42 +0000315 canvas.restore();
316
317 // Check that saveLayer with the kClipToLayer_SaveFlag sets the clip
318 // stack to the layer bounds.
fmalita55b29b22016-01-20 11:17:39 -0800319 canvas.saveLayer(&bounds, nullptr);
Mike Reed918e1442017-01-23 11:39:45 -0500320 devClip = canvas.getDeviceClipBounds();
Mike Reed3726a4a2017-01-19 11:36:41 -0500321 REPORTER_ASSERT(reporter, canvas.isClipRect());
322 REPORTER_ASSERT(reporter, devClip.width() == LAYER_WIDTH);
323 REPORTER_ASSERT(reporter, devClip.height() == LAYER_HEIGHT);
senorblanco@chromium.org89f077c2014-02-24 15:16:42 +0000324 canvas.restore();
scroggo24519372014-07-22 12:38:55 -0700325}