djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 8 | #include "CanvasStateHelpers.h" |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
| 10 | #include "SkCanvasStateUtils.h" |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 11 | #include "SkCommandLineFlags.h" |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 12 | #include "SkDrawFilter.h" |
commit-bot@chromium.org | 07f6cf3 | 2013-09-18 20:15:12 +0000 | [diff] [blame] | 13 | #include "SkError.h" |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 14 | #include "SkPaint.h" |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 15 | #include "SkRRect.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 16 | #include "SkRect.h" |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 17 | #include "SkTLazy.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 18 | #include "Test.h" |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 19 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 20 | // dlopen and the library flag are only used for tests which require this flag. |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 21 | #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 22 | #include <dlfcn.h> |
| 23 | |
| 24 | DEFINE_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. |
| 34 | class OpenLibResult { |
| 35 | public: |
| 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 41 | REPORTER_ASSERT_MESSAGE(reporter, fHandle != nullptr, "Failed to open library!"); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 42 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 43 | fHandle = nullptr; |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 44 | } |
| 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 | |
| 57 | private: |
| 58 | void* fHandle; |
| 59 | }; |
| 60 | |
| 61 | DEF_TEST(CanvasState_test_complex_layers, reporter) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 62 | const int WIDTH = 400; |
| 63 | const int HEIGHT = 400; |
| 64 | const int SPACER = 10; |
| 65 | |
djsollen@google.com | 20146b3 | 2013-08-29 20:36:22 +0000 | [diff] [blame] | 66 | SkRect rect = SkRect::MakeXYWH(SkIntToScalar(SPACER), SkIntToScalar(SPACER), |
| 67 | SkIntToScalar(WIDTH-(2*SPACER)), |
| 68 | SkIntToScalar((HEIGHT-(2*SPACER)) / 7)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 69 | |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 70 | const SkColorType colorTypes[] = { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 71 | kRGB_565_SkColorType, kN32_SkColorType |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 72 | }; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 73 | |
| 74 | const int layerAlpha[] = { 255, 255, 0 }; |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 75 | 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.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 79 | }; |
| 80 | REPORTER_ASSERT(reporter, sizeof(layerAlpha) == sizeof(flags)); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 81 | |
| 82 | bool (*drawFn)(SkCanvasState* state, float l, float t, |
| 83 | float r, float b, int32_t s); |
| 84 | |
| 85 | OpenLibResult openLibResult(reporter); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 86 | if (openLibResult.handle() != nullptr) { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 87 | *(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.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 97 | |
scroggo | ecce60b | 2014-07-09 07:26:40 -0700 | [diff] [blame] | 98 | for (size_t i = 0; i < SK_ARRAY_COUNT(colorTypes); ++i) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 99 | SkBitmap bitmaps[2]; |
| 100 | for (int j = 0; j < 2; ++j) { |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 101 | bitmaps[j].allocPixels(SkImageInfo::Make(WIDTH, HEIGHT, |
| 102 | colorTypes[i], |
| 103 | kPremul_SkAlphaType)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 104 | |
| 105 | SkCanvas canvas(bitmaps[j]); |
| 106 | |
| 107 | canvas.drawColor(SK_ColorRED); |
| 108 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 109 | for (size_t k = 0; k < SK_ARRAY_COUNT(layerAlpha); ++k) { |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 110 | SkTLazy<SkPaint> paint; |
| 111 | if (layerAlpha[k] != 0xFF) { |
| 112 | paint.init()->setAlpha(layerAlpha[k]); |
| 113 | } |
| 114 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 115 | // draw a rect within the layer's bounds and again outside the layer's bounds |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 116 | canvas.saveLayer(SkCanvas::SaveLayerRec(&rect, paint.getMaybeNull(), flags[k])); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 117 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 118 | if (j) { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 119 | // Capture from the first Skia. |
| 120 | SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 121 | REPORTER_ASSERT(reporter, state); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 122 | |
| 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.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 130 | } else { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 131 | // Draw in the first Skia. |
| 132 | complex_layers_draw(&canvas, rect.fLeft, rect.fTop, |
| 133 | rect.fRight, rect.fBottom, SPACER); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 134 | } |
| 135 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 136 | 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 | } |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 150 | #endif |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 151 | |
| 152 | //////////////////////////////////////////////////////////////////////////////// |
| 153 | |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 154 | #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 155 | DEF_TEST(CanvasState_test_complex_clips, reporter) { |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 156 | const int WIDTH = 400; |
| 157 | const int HEIGHT = 400; |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 158 | const int SPACER = 10; |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 159 | |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 160 | SkIRect layerRect = SkIRect::MakeWH(WIDTH, HEIGHT / 4); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 161 | layerRect.inset(2*SPACER, 2*SPACER); |
| 162 | |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 163 | SkIRect clipRect = layerRect; |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 164 | clipRect.fRight = clipRect.fLeft + (clipRect.width() / 2) - (2*SPACER); |
| 165 | clipRect.outset(SPACER, SPACER); |
| 166 | |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 167 | SkIRect regionBounds = clipRect; |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 168 | 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 | }; |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 182 | 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.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 186 | }; |
| 187 | REPORTER_ASSERT(reporter, sizeof(clipOps) == sizeof(flags)); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 188 | |
| 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 194 | if (openLibResult.handle() != nullptr) { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 195 | *(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.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 205 | |
| 206 | SkBitmap bitmaps[2]; |
| 207 | for (int i = 0; i < 2; ++i) { |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 208 | bitmaps[i].allocN32Pixels(WIDTH, HEIGHT); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 209 | |
| 210 | SkCanvas canvas(bitmaps[i]); |
| 211 | |
| 212 | canvas.drawColor(SK_ColorRED); |
| 213 | |
| 214 | SkRegion localRegion = clipRegion; |
| 215 | |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 216 | SkPaint paint; |
| 217 | paint.setAlpha(128); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 218 | for (size_t j = 0; j < SK_ARRAY_COUNT(flags); ++j) { |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 219 | SkRect layerBounds = SkRect::Make(layerRect); |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 220 | canvas.saveLayer(SkCanvas::SaveLayerRec(&layerBounds, &paint, flags[j])); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 221 | |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 222 | if (i) { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 223 | SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 224 | REPORTER_ASSERT(reporter, state); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 225 | |
| 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.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 241 | } else { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 242 | complex_clips_draw(&canvas, clipRect.fLeft, clipRect.fTop, |
| 243 | clipRect.fRight, clipRect.fBottom, clipOps[j], |
| 244 | localRegion); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 245 | } |
| 246 | |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 247 | canvas.restore(); |
| 248 | |
| 249 | // translate the canvas and region for the next iteration |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 250 | canvas.translate(0, SkIntToScalar(2*(layerRect.height() + (SPACER)))); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 251 | 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 | } |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 261 | #endif |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 262 | |
| 263 | //////////////////////////////////////////////////////////////////////////////// |
| 264 | |
benjaminwagner | 1c2729c | 2016-01-22 09:45:14 -0800 | [diff] [blame] | 265 | #ifdef SK_SUPPORT_LEGACY_DRAWFILTER |
| 266 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 267 | class TestDrawFilter : public SkDrawFilter { |
| 268 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 269 | bool filter(SkPaint*, Type) override { return true; } |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 270 | }; |
| 271 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 272 | DEF_TEST(CanvasState_test_draw_filters, reporter) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 273 | TestDrawFilter drawFilter; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 274 | SkBitmap bitmap; |
| 275 | bitmap.allocN32Pixels(10, 10); |
| 276 | SkCanvas canvas(bitmap); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 277 | |
| 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 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 285 | REPORTER_ASSERT(reporter, canvas.getDrawFilter()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 286 | REPORTER_ASSERT(reporter, nullptr == tmpCanvas->getDrawFilter()); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 287 | |
| 288 | tmpCanvas->unref(); |
| 289 | SkCanvasStateUtils::ReleaseCanvasState(state); |
| 290 | } |
| 291 | |
benjaminwagner | 1c2729c | 2016-01-22 09:45:14 -0800 | [diff] [blame] | 292 | #endif |
| 293 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 294 | //////////////////////////////////////////////////////////////////////////////// |
| 295 | |
commit-bot@chromium.org | 07f6cf3 | 2013-09-18 20:15:12 +0000 | [diff] [blame] | 296 | // we need this function to prevent SkError from printing to stdout |
| 297 | static void error_callback(SkError code, void* ctx) {} |
| 298 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 299 | DEF_TEST(CanvasState_test_soft_clips, reporter) { |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 300 | SkBitmap bitmap; |
| 301 | bitmap.allocN32Pixels(10, 10); |
| 302 | SkCanvas canvas(bitmap); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 303 | |
| 304 | SkRRect roundRect; |
| 305 | roundRect.setOval(SkRect::MakeWH(5, 5)); |
| 306 | |
| 307 | canvas.clipRRect(roundRect, SkRegion::kIntersect_Op, true); |
| 308 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 309 | SkSetErrorCallback(error_callback, nullptr); |
commit-bot@chromium.org | 07f6cf3 | 2013-09-18 20:15:12 +0000 | [diff] [blame] | 310 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 311 | SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
| 312 | REPORTER_ASSERT(reporter, !state); |
commit-bot@chromium.org | 07f6cf3 | 2013-09-18 20:15:12 +0000 | [diff] [blame] | 313 | |
| 314 | REPORTER_ASSERT(reporter, kInvalidOperation_SkError == SkGetLastError()); |
| 315 | SkClearLastError(); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 316 | } |
| 317 | |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 318 | #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 319 | #include "SkClipStack.h" |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 320 | DEF_TEST(CanvasState_test_saveLayer_clip, reporter) { |
senorblanco@chromium.org | 89f077c | 2014-02-24 15:16:42 +0000 | [diff] [blame] | 321 | 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. |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 335 | canvas.saveLayer(SkCanvas::SaveLayerRec(&bounds, |
| 336 | nullptr, |
| 337 | SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag)); |
senorblanco@chromium.org | 89f077c | 2014-02-24 15:16:42 +0000 | [diff] [blame] | 338 | SkRect clipStackBounds; |
| 339 | SkClipStack::BoundsType boundsType; |
| 340 | canvas.getClipStack()->getBounds(&clipStackBounds, &boundsType); |
reed | c64eff5 | 2015-11-21 12:39:45 -0800 | [diff] [blame] | 341 | // 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.org | 89f077c | 2014-02-24 15:16:42 +0000 | [diff] [blame] | 349 | canvas.restore(); |
| 350 | |
| 351 | // Check that saveLayer with the kClipToLayer_SaveFlag sets the clip |
| 352 | // stack to the layer bounds. |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 353 | canvas.saveLayer(&bounds, nullptr); |
senorblanco@chromium.org | 89f077c | 2014-02-24 15:16:42 +0000 | [diff] [blame] | 354 | 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(); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 359 | } |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 360 | #endif |