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" |
| 13 | #include "SkPaint.h" |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 14 | #include "SkRRect.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 15 | #include "SkRect.h" |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 16 | #include "SkTLazy.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 17 | #include "Test.h" |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 18 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 19 | // 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] | 20 | #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 21 | #include <dlfcn.h> |
| 22 | |
| 23 | DEFINE_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. |
| 33 | class OpenLibResult { |
| 34 | public: |
| 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 40 | REPORTER_ASSERT_MESSAGE(reporter, fHandle != nullptr, "Failed to open library!"); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 41 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 42 | fHandle = nullptr; |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 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 | |
| 56 | private: |
| 57 | void* fHandle; |
| 58 | }; |
| 59 | |
| 60 | DEF_TEST(CanvasState_test_complex_layers, reporter) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 61 | const int WIDTH = 400; |
| 62 | const int HEIGHT = 400; |
| 63 | const int SPACER = 10; |
| 64 | |
djsollen@google.com | 20146b3 | 2013-08-29 20:36:22 +0000 | [diff] [blame] | 65 | SkRect rect = SkRect::MakeXYWH(SkIntToScalar(SPACER), SkIntToScalar(SPACER), |
| 66 | SkIntToScalar(WIDTH-(2*SPACER)), |
| 67 | SkIntToScalar((HEIGHT-(2*SPACER)) / 7)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 68 | |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 69 | const SkColorType colorTypes[] = { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 70 | kRGB_565_SkColorType, kN32_SkColorType |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 71 | }; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 72 | |
| 73 | const int layerAlpha[] = { 255, 255, 0 }; |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 74 | 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.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 78 | }; |
| 79 | REPORTER_ASSERT(reporter, sizeof(layerAlpha) == sizeof(flags)); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 80 | |
| 81 | bool (*drawFn)(SkCanvasState* state, float l, float t, |
| 82 | float r, float b, int32_t s); |
| 83 | |
| 84 | OpenLibResult openLibResult(reporter); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 85 | if (openLibResult.handle() != nullptr) { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 86 | *(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.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 96 | |
scroggo | ecce60b | 2014-07-09 07:26:40 -0700 | [diff] [blame] | 97 | for (size_t i = 0; i < SK_ARRAY_COUNT(colorTypes); ++i) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 98 | SkBitmap bitmaps[2]; |
| 99 | for (int j = 0; j < 2; ++j) { |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 100 | bitmaps[j].allocPixels(SkImageInfo::Make(WIDTH, HEIGHT, |
| 101 | colorTypes[i], |
| 102 | kPremul_SkAlphaType)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 103 | |
| 104 | SkCanvas canvas(bitmaps[j]); |
| 105 | |
| 106 | canvas.drawColor(SK_ColorRED); |
| 107 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 108 | for (size_t k = 0; k < SK_ARRAY_COUNT(layerAlpha); ++k) { |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 109 | SkTLazy<SkPaint> paint; |
| 110 | if (layerAlpha[k] != 0xFF) { |
| 111 | paint.init()->setAlpha(layerAlpha[k]); |
| 112 | } |
| 113 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 114 | // 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] | 115 | canvas.saveLayer(SkCanvas::SaveLayerRec(&rect, paint.getMaybeNull(), flags[k])); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 116 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 117 | if (j) { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 118 | // Capture from the first Skia. |
| 119 | SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 120 | REPORTER_ASSERT(reporter, state); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 121 | |
| 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.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 129 | } else { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 130 | // Draw in the first Skia. |
| 131 | complex_layers_draw(&canvas, rect.fLeft, rect.fTop, |
| 132 | rect.fRight, rect.fBottom, SPACER); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 133 | } |
| 134 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 135 | 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 | } |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 149 | #endif |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 150 | |
| 151 | //////////////////////////////////////////////////////////////////////////////// |
| 152 | |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 153 | #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 154 | DEF_TEST(CanvasState_test_complex_clips, reporter) { |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 155 | const int WIDTH = 400; |
| 156 | const int HEIGHT = 400; |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 157 | const int SPACER = 10; |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 158 | |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 159 | SkIRect layerRect = SkIRect::MakeWH(WIDTH, HEIGHT / 4); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 160 | layerRect.inset(2*SPACER, 2*SPACER); |
| 161 | |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 162 | SkIRect clipRect = layerRect; |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 163 | clipRect.fRight = clipRect.fLeft + (clipRect.width() / 2) - (2*SPACER); |
| 164 | clipRect.outset(SPACER, SPACER); |
| 165 | |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 166 | SkIRect regionBounds = clipRect; |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 167 | 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 | }; |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 181 | 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.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 185 | }; |
| 186 | REPORTER_ASSERT(reporter, sizeof(clipOps) == sizeof(flags)); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 187 | |
| 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 193 | if (openLibResult.handle() != nullptr) { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 194 | *(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.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 204 | |
| 205 | SkBitmap bitmaps[2]; |
| 206 | for (int i = 0; i < 2; ++i) { |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 207 | bitmaps[i].allocN32Pixels(WIDTH, HEIGHT); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 208 | |
| 209 | SkCanvas canvas(bitmaps[i]); |
| 210 | |
| 211 | canvas.drawColor(SK_ColorRED); |
| 212 | |
| 213 | SkRegion localRegion = clipRegion; |
| 214 | |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 215 | SkPaint paint; |
| 216 | paint.setAlpha(128); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 217 | for (size_t j = 0; j < SK_ARRAY_COUNT(flags); ++j) { |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 218 | SkRect layerBounds = SkRect::Make(layerRect); |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 219 | canvas.saveLayer(SkCanvas::SaveLayerRec(&layerBounds, &paint, flags[j])); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 220 | |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 221 | if (i) { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 222 | SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 223 | REPORTER_ASSERT(reporter, state); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 224 | |
| 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.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 240 | } else { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 241 | complex_clips_draw(&canvas, clipRect.fLeft, clipRect.fTop, |
| 242 | clipRect.fRight, clipRect.fBottom, clipOps[j], |
| 243 | localRegion); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 244 | } |
| 245 | |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 246 | canvas.restore(); |
| 247 | |
| 248 | // translate the canvas and region for the next iteration |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 249 | canvas.translate(0, SkIntToScalar(2*(layerRect.height() + (SPACER)))); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 250 | 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 | } |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 260 | #endif |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 261 | |
| 262 | //////////////////////////////////////////////////////////////////////////////// |
| 263 | |
benjaminwagner | 1c2729c | 2016-01-22 09:45:14 -0800 | [diff] [blame] | 264 | #ifdef SK_SUPPORT_LEGACY_DRAWFILTER |
| 265 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 266 | class TestDrawFilter : public SkDrawFilter { |
| 267 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 268 | bool filter(SkPaint*, Type) override { return true; } |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 269 | }; |
| 270 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 271 | DEF_TEST(CanvasState_test_draw_filters, reporter) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 272 | TestDrawFilter drawFilter; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 273 | SkBitmap bitmap; |
| 274 | bitmap.allocN32Pixels(10, 10); |
| 275 | SkCanvas canvas(bitmap); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 276 | |
| 277 | canvas.setDrawFilter(&drawFilter); |
| 278 | |
| 279 | SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
| 280 | REPORTER_ASSERT(reporter, state); |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 281 | std::unique_ptr<SkCanvas> tmpCanvas = SkCanvasStateUtils::MakeFromCanvasState(state); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 282 | REPORTER_ASSERT(reporter, tmpCanvas); |
| 283 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 284 | REPORTER_ASSERT(reporter, canvas.getDrawFilter()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 285 | REPORTER_ASSERT(reporter, nullptr == tmpCanvas->getDrawFilter()); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 286 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 287 | SkCanvasStateUtils::ReleaseCanvasState(state); |
| 288 | } |
| 289 | |
benjaminwagner | 1c2729c | 2016-01-22 09:45:14 -0800 | [diff] [blame] | 290 | #endif |
| 291 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 292 | //////////////////////////////////////////////////////////////////////////////// |
| 293 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 294 | DEF_TEST(CanvasState_test_soft_clips, reporter) { |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 295 | SkBitmap bitmap; |
| 296 | bitmap.allocN32Pixels(10, 10); |
| 297 | SkCanvas canvas(bitmap); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 298 | |
| 299 | SkRRect roundRect; |
| 300 | roundRect.setOval(SkRect::MakeWH(5, 5)); |
| 301 | |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 302 | canvas.clipRRect(roundRect, SkCanvas::kIntersect_Op, true); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 303 | |
| 304 | SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
| 305 | REPORTER_ASSERT(reporter, !state); |
| 306 | } |
| 307 | |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 308 | #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 309 | #include "SkClipStack.h" |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 310 | DEF_TEST(CanvasState_test_saveLayer_clip, reporter) { |
senorblanco@chromium.org | 89f077c | 2014-02-24 15:16:42 +0000 | [diff] [blame] | 311 | const int WIDTH = 100; |
| 312 | const int HEIGHT = 100; |
| 313 | const int LAYER_WIDTH = 50; |
| 314 | const int LAYER_HEIGHT = 50; |
| 315 | |
| 316 | SkBitmap bitmap; |
| 317 | bitmap.allocN32Pixels(WIDTH, HEIGHT); |
| 318 | SkCanvas canvas(bitmap); |
| 319 | |
| 320 | SkRect bounds = SkRect::MakeWH(SkIntToScalar(LAYER_WIDTH), SkIntToScalar(LAYER_HEIGHT)); |
| 321 | canvas.clipRect(SkRect::MakeWH(SkIntToScalar(WIDTH), SkIntToScalar(HEIGHT))); |
| 322 | |
| 323 | // Check that saveLayer without the kClipToLayer_SaveFlag leaves the |
| 324 | // clip stack unchanged. |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 325 | canvas.saveLayer(SkCanvas::SaveLayerRec(&bounds, |
| 326 | nullptr, |
| 327 | SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag)); |
senorblanco@chromium.org | 89f077c | 2014-02-24 15:16:42 +0000 | [diff] [blame] | 328 | SkRect clipStackBounds; |
| 329 | SkClipStack::BoundsType boundsType; |
| 330 | canvas.getClipStack()->getBounds(&clipStackBounds, &boundsType); |
reed | c64eff5 | 2015-11-21 12:39:45 -0800 | [diff] [blame] | 331 | // The clip stack will return its bounds, or it may be "full" : i.e. empty + inside_out. |
| 332 | // Either result is consistent with this test, since the canvas' size is WIDTH/HEIGHT |
| 333 | if (SkClipStack::kInsideOut_BoundsType == boundsType) { |
| 334 | REPORTER_ASSERT(reporter, clipStackBounds.isEmpty()); |
| 335 | } else { |
| 336 | REPORTER_ASSERT(reporter, clipStackBounds.width() == WIDTH); |
| 337 | REPORTER_ASSERT(reporter, clipStackBounds.height() == HEIGHT); |
| 338 | } |
senorblanco@chromium.org | 89f077c | 2014-02-24 15:16:42 +0000 | [diff] [blame] | 339 | canvas.restore(); |
| 340 | |
| 341 | // Check that saveLayer with the kClipToLayer_SaveFlag sets the clip |
| 342 | // stack to the layer bounds. |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 343 | canvas.saveLayer(&bounds, nullptr); |
senorblanco@chromium.org | 89f077c | 2014-02-24 15:16:42 +0000 | [diff] [blame] | 344 | canvas.getClipStack()->getBounds(&clipStackBounds, &boundsType); |
| 345 | REPORTER_ASSERT(reporter, clipStackBounds.width() == LAYER_WIDTH); |
| 346 | REPORTER_ASSERT(reporter, clipStackBounds.height() == LAYER_HEIGHT); |
| 347 | |
| 348 | canvas.restore(); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 349 | } |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 350 | #endif |