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" |
| 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); |
| 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 | |
| 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 }; |
| 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)); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 79 | |
| 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.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 95 | |
scroggo | ecce60b | 2014-07-09 07:26:40 -0700 | [diff] [blame] | 96 | for (size_t i = 0; i < SK_ARRAY_COUNT(colorTypes); ++i) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 97 | SkBitmap bitmaps[2]; |
| 98 | for (int j = 0; j < 2; ++j) { |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 99 | bitmaps[j].allocPixels(SkImageInfo::Make(WIDTH, HEIGHT, |
| 100 | colorTypes[i], |
| 101 | kPremul_SkAlphaType)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 102 | |
| 103 | SkCanvas canvas(bitmaps[j]); |
| 104 | |
| 105 | canvas.drawColor(SK_ColorRED); |
| 106 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 107 | for (size_t k = 0; k < SK_ARRAY_COUNT(layerAlpha); ++k) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 108 | // 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.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 111 | if (j) { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 112 | // Capture from the first Skia. |
| 113 | SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 114 | REPORTER_ASSERT(reporter, state); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 115 | |
| 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.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 123 | } else { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 124 | // Draw in the first Skia. |
| 125 | complex_layers_draw(&canvas, rect.fLeft, rect.fTop, |
| 126 | rect.fRight, rect.fBottom, SPACER); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 127 | } |
| 128 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 129 | 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 | } |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 143 | #endif |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 144 | |
| 145 | //////////////////////////////////////////////////////////////////////////////// |
| 146 | |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 147 | #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 148 | DEF_TEST(CanvasState_test_complex_clips, reporter) { |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 149 | const int WIDTH = 400; |
| 150 | const int HEIGHT = 400; |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 151 | const int SPACER = 10; |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 152 | |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 153 | SkIRect layerRect = SkIRect::MakeWH(WIDTH, HEIGHT / 4); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 154 | layerRect.inset(2*SPACER, 2*SPACER); |
| 155 | |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 156 | SkIRect clipRect = layerRect; |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 157 | clipRect.fRight = clipRect.fLeft + (clipRect.width() / 2) - (2*SPACER); |
| 158 | clipRect.outset(SPACER, SPACER); |
| 159 | |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 160 | SkIRect regionBounds = clipRect; |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 161 | 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)); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 180 | |
| 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.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 197 | |
| 198 | SkBitmap bitmaps[2]; |
| 199 | for (int i = 0; i < 2; ++i) { |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 200 | bitmaps[i].allocN32Pixels(WIDTH, HEIGHT); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 201 | |
| 202 | SkCanvas canvas(bitmaps[i]); |
| 203 | |
| 204 | canvas.drawColor(SK_ColorRED); |
| 205 | |
| 206 | SkRegion localRegion = clipRegion; |
| 207 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 208 | for (size_t j = 0; j < SK_ARRAY_COUNT(flags); ++j) { |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 209 | SkRect layerBounds = SkRect::Make(layerRect); |
| 210 | canvas.saveLayerAlpha(&layerBounds, 128, flags[j]); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 211 | |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 212 | if (i) { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 213 | SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 214 | REPORTER_ASSERT(reporter, state); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 215 | |
| 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.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 231 | } else { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 232 | complex_clips_draw(&canvas, clipRect.fLeft, clipRect.fTop, |
| 233 | clipRect.fRight, clipRect.fBottom, clipOps[j], |
| 234 | localRegion); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 235 | } |
| 236 | |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 237 | canvas.restore(); |
| 238 | |
| 239 | // translate the canvas and region for the next iteration |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 240 | canvas.translate(0, SkIntToScalar(2*(layerRect.height() + (SPACER)))); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 241 | 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 | } |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 251 | #endif |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 252 | |
| 253 | //////////////////////////////////////////////////////////////////////////////// |
| 254 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 255 | class TestDrawFilter : public SkDrawFilter { |
| 256 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 257 | bool filter(SkPaint*, Type) override { return true; } |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 258 | }; |
| 259 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 260 | DEF_TEST(CanvasState_test_draw_filters, reporter) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 261 | TestDrawFilter drawFilter; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 262 | SkBitmap bitmap; |
| 263 | bitmap.allocN32Pixels(10, 10); |
| 264 | SkCanvas canvas(bitmap); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 265 | |
| 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 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 273 | REPORTER_ASSERT(reporter, canvas.getDrawFilter()); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 274 | REPORTER_ASSERT(reporter, NULL == tmpCanvas->getDrawFilter()); |
| 275 | |
| 276 | tmpCanvas->unref(); |
| 277 | SkCanvasStateUtils::ReleaseCanvasState(state); |
| 278 | } |
| 279 | |
| 280 | //////////////////////////////////////////////////////////////////////////////// |
| 281 | |
commit-bot@chromium.org | 07f6cf3 | 2013-09-18 20:15:12 +0000 | [diff] [blame] | 282 | // we need this function to prevent SkError from printing to stdout |
| 283 | static void error_callback(SkError code, void* ctx) {} |
| 284 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 285 | DEF_TEST(CanvasState_test_soft_clips, reporter) { |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 286 | SkBitmap bitmap; |
| 287 | bitmap.allocN32Pixels(10, 10); |
| 288 | SkCanvas canvas(bitmap); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 289 | |
| 290 | SkRRect roundRect; |
| 291 | roundRect.setOval(SkRect::MakeWH(5, 5)); |
| 292 | |
| 293 | canvas.clipRRect(roundRect, SkRegion::kIntersect_Op, true); |
| 294 | |
commit-bot@chromium.org | 07f6cf3 | 2013-09-18 20:15:12 +0000 | [diff] [blame] | 295 | SkSetErrorCallback(error_callback, NULL); |
| 296 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 297 | SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
| 298 | REPORTER_ASSERT(reporter, !state); |
commit-bot@chromium.org | 07f6cf3 | 2013-09-18 20:15:12 +0000 | [diff] [blame] | 299 | |
| 300 | REPORTER_ASSERT(reporter, kInvalidOperation_SkError == SkGetLastError()); |
| 301 | SkClearLastError(); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 302 | } |
| 303 | |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 304 | #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 305 | DEF_TEST(CanvasState_test_saveLayer_clip, reporter) { |
senorblanco@chromium.org | 89f077c | 2014-02-24 15:16:42 +0000 | [diff] [blame] | 306 | 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(); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 336 | } |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 337 | #endif |