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" |
Florin Malita | ab244f0 | 2017-05-03 19:16:58 +0000 | [diff] [blame] | 9 | #include "SkBitmap.h" |
Cary Clark | 7eddfb8 | 2018-03-13 14:41:10 -0400 | [diff] [blame] | 10 | #include "SkCanvasPriv.h" |
Ben Wagner | 501c17c | 2018-03-12 20:04:31 +0000 | [diff] [blame] | 11 | #include "SkCanvasStateUtils.h" |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 12 | #include "SkClipOpPriv.h" |
| 13 | #include "SkColor.h" |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 14 | #include "SkCommandLineFlags.h" |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 15 | #include "SkDrawFilter.h" |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 16 | #include "SkImageInfo.h" |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 17 | #include "SkPaint.h" |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 18 | #include "SkRRect.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 19 | #include "SkRect.h" |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 20 | #include "SkRegion.h" |
| 21 | #include "SkScalar.h" |
| 22 | #include "SkTDArray.h" |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 23 | #include "SkTLazy.h" |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 24 | #include "SkTypes.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 25 | #include "Test.h" |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 26 | |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 27 | #include <cstring> |
| 28 | #include <memory> |
| 29 | |
| 30 | class SkCanvasState; |
| 31 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 32 | // 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] | 33 | #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 34 | #include <dlfcn.h> |
| 35 | |
| 36 | DEFINE_string(library, "", "Support library to use for CanvasState test. If empty (the default), " |
| 37 | "the test will be run without crossing a library boundary. Otherwise, " |
| 38 | "it is expected to be a full path to a shared library file, which will" |
| 39 | " be dynamically loaded. Functions from the library will be called to " |
| 40 | "test SkCanvasState. Instructions for generating the library are in " |
| 41 | "gyp/canvas_state_lib.gyp"); |
| 42 | |
| 43 | |
| 44 | // This class calls dlopen on the library passed in to the command line flag library, and handles |
| 45 | // calling dlclose when it goes out of scope. |
| 46 | class OpenLibResult { |
| 47 | public: |
| 48 | // If the flag library was passed to this run of the test, attempt to open it using dlopen and |
| 49 | // report whether it succeeded. |
| 50 | OpenLibResult(skiatest::Reporter* reporter) { |
| 51 | if (FLAGS_library.count() == 1) { |
| 52 | fHandle = dlopen(FLAGS_library[0], RTLD_LAZY | RTLD_LOCAL); |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 53 | REPORTER_ASSERT(reporter, fHandle != nullptr, "Failed to open library!"); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 54 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 55 | fHandle = nullptr; |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | // Automatically call dlclose when going out of scope. |
| 60 | ~OpenLibResult() { |
| 61 | if (fHandle) { |
| 62 | dlclose(fHandle); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Pointer to the shared library object. |
| 67 | void* handle() { return fHandle; } |
| 68 | |
| 69 | private: |
| 70 | void* fHandle; |
| 71 | }; |
| 72 | |
| 73 | DEF_TEST(CanvasState_test_complex_layers, reporter) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 74 | const int WIDTH = 400; |
| 75 | const int HEIGHT = 400; |
| 76 | const int SPACER = 10; |
| 77 | |
djsollen@google.com | 20146b3 | 2013-08-29 20:36:22 +0000 | [diff] [blame] | 78 | SkRect rect = SkRect::MakeXYWH(SkIntToScalar(SPACER), SkIntToScalar(SPACER), |
| 79 | SkIntToScalar(WIDTH-(2*SPACER)), |
| 80 | SkIntToScalar((HEIGHT-(2*SPACER)) / 7)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 81 | |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 82 | const SkColorType colorTypes[] = { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 83 | kRGB_565_SkColorType, kN32_SkColorType |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 84 | }; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 85 | |
| 86 | const int layerAlpha[] = { 255, 255, 0 }; |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 87 | const SkCanvas::SaveLayerFlags flags[] = { |
Cary Clark | 7eddfb8 | 2018-03-13 14:41:10 -0400 | [diff] [blame] | 88 | static_cast<SkCanvas::SaveLayerFlags>(SkCanvasPriv::kDontClipToLayer_SaveLayerFlag), |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 89 | 0, |
Cary Clark | 7eddfb8 | 2018-03-13 14:41:10 -0400 | [diff] [blame] | 90 | static_cast<SkCanvas::SaveLayerFlags>(SkCanvasPriv::kDontClipToLayer_SaveLayerFlag), |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 91 | }; |
| 92 | REPORTER_ASSERT(reporter, sizeof(layerAlpha) == sizeof(flags)); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 93 | |
| 94 | bool (*drawFn)(SkCanvasState* state, float l, float t, |
| 95 | float r, float b, int32_t s); |
| 96 | |
| 97 | OpenLibResult openLibResult(reporter); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 98 | if (openLibResult.handle() != nullptr) { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 99 | *(void**) (&drawFn) = dlsym(openLibResult.handle(), |
| 100 | "complex_layers_draw_from_canvas_state"); |
| 101 | } else { |
| 102 | drawFn = complex_layers_draw_from_canvas_state; |
| 103 | } |
| 104 | |
| 105 | REPORTER_ASSERT(reporter, drawFn); |
| 106 | if (!drawFn) { |
| 107 | return; |
| 108 | } |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 109 | |
scroggo | ecce60b | 2014-07-09 07:26:40 -0700 | [diff] [blame] | 110 | for (size_t i = 0; i < SK_ARRAY_COUNT(colorTypes); ++i) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 111 | SkBitmap bitmaps[2]; |
| 112 | for (int j = 0; j < 2; ++j) { |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 113 | bitmaps[j].allocPixels(SkImageInfo::Make(WIDTH, HEIGHT, |
| 114 | colorTypes[i], |
| 115 | kPremul_SkAlphaType)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 116 | |
| 117 | SkCanvas canvas(bitmaps[j]); |
| 118 | |
| 119 | canvas.drawColor(SK_ColorRED); |
| 120 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 121 | for (size_t k = 0; k < SK_ARRAY_COUNT(layerAlpha); ++k) { |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 122 | SkTLazy<SkPaint> paint; |
| 123 | if (layerAlpha[k] != 0xFF) { |
| 124 | paint.init()->setAlpha(layerAlpha[k]); |
| 125 | } |
| 126 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 127 | // 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] | 128 | canvas.saveLayer(SkCanvas::SaveLayerRec(&rect, paint.getMaybeNull(), flags[k])); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 129 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 130 | if (j) { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 131 | // Capture from the first Skia. |
| 132 | SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 133 | REPORTER_ASSERT(reporter, state); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 134 | |
| 135 | // And draw to it in the second Skia. |
| 136 | bool success = complex_layers_draw_from_canvas_state(state, |
| 137 | rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, SPACER); |
| 138 | REPORTER_ASSERT(reporter, success); |
| 139 | |
| 140 | // And release it in the *first* Skia. |
| 141 | SkCanvasStateUtils::ReleaseCanvasState(state); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 142 | } else { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 143 | // Draw in the first Skia. |
| 144 | complex_layers_draw(&canvas, rect.fLeft, rect.fTop, |
| 145 | rect.fRight, rect.fBottom, SPACER); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 146 | } |
| 147 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 148 | canvas.restore(); |
| 149 | |
| 150 | // translate the canvas for the next iteration |
| 151 | canvas.translate(0, 2*(rect.height() + SPACER)); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // now we memcmp the two bitmaps |
Mike Reed | df071d7 | 2017-10-09 13:09:55 -0400 | [diff] [blame] | 156 | REPORTER_ASSERT(reporter, bitmaps[0].computeByteSize() == bitmaps[1].computeByteSize()); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 157 | REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(), |
| 158 | bitmaps[1].getPixels(), |
Mike Reed | df071d7 | 2017-10-09 13:09:55 -0400 | [diff] [blame] | 159 | bitmaps[0].computeByteSize())); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 162 | #endif |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 163 | |
| 164 | //////////////////////////////////////////////////////////////////////////////// |
| 165 | |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 166 | #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 167 | DEF_TEST(CanvasState_test_complex_clips, reporter) { |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 168 | const int WIDTH = 400; |
| 169 | const int HEIGHT = 400; |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 170 | const int SPACER = 10; |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 171 | |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 172 | SkIRect layerRect = SkIRect::MakeWH(WIDTH, HEIGHT / 4); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 173 | layerRect.inset(2*SPACER, 2*SPACER); |
| 174 | |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 175 | SkIRect clipRect = layerRect; |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 176 | clipRect.fRight = clipRect.fLeft + (clipRect.width() / 2) - (2*SPACER); |
| 177 | clipRect.outset(SPACER, SPACER); |
| 178 | |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 179 | SkIRect regionBounds = clipRect; |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 180 | regionBounds.offset(clipRect.width() + (2*SPACER), 0); |
| 181 | |
| 182 | SkIRect regionInterior = regionBounds; |
| 183 | regionInterior.inset(SPACER*3, SPACER*3); |
| 184 | |
| 185 | SkRegion clipRegion; |
| 186 | clipRegion.setRect(regionBounds); |
| 187 | clipRegion.op(regionInterior, SkRegion::kDifference_Op); |
| 188 | |
| 189 | |
| 190 | const SkRegion::Op clipOps[] = { SkRegion::kIntersect_Op, |
| 191 | SkRegion::kIntersect_Op, |
| 192 | SkRegion::kReplace_Op, |
| 193 | }; |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 194 | const SkCanvas::SaveLayerFlags flags[] = { |
Cary Clark | 7eddfb8 | 2018-03-13 14:41:10 -0400 | [diff] [blame] | 195 | static_cast<SkCanvas::SaveLayerFlags>(SkCanvasPriv::kDontClipToLayer_SaveLayerFlag), |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 196 | 0, |
Cary Clark | 7eddfb8 | 2018-03-13 14:41:10 -0400 | [diff] [blame] | 197 | static_cast<SkCanvas::SaveLayerFlags>(SkCanvasPriv::kDontClipToLayer_SaveLayerFlag), |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 198 | }; |
| 199 | REPORTER_ASSERT(reporter, sizeof(clipOps) == sizeof(flags)); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 200 | |
| 201 | bool (*drawFn)(SkCanvasState* state, int32_t l, int32_t t, |
| 202 | int32_t r, int32_t b, int32_t clipOp, |
| 203 | int32_t regionRects, int32_t* rectCoords); |
| 204 | |
| 205 | OpenLibResult openLibResult(reporter); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 206 | if (openLibResult.handle() != nullptr) { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 207 | *(void**) (&drawFn) = dlsym(openLibResult.handle(), |
| 208 | "complex_clips_draw_from_canvas_state"); |
| 209 | } else { |
| 210 | drawFn = complex_clips_draw_from_canvas_state; |
| 211 | } |
| 212 | |
| 213 | REPORTER_ASSERT(reporter, drawFn); |
| 214 | if (!drawFn) { |
| 215 | return; |
| 216 | } |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 217 | |
| 218 | SkBitmap bitmaps[2]; |
| 219 | for (int i = 0; i < 2; ++i) { |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 220 | bitmaps[i].allocN32Pixels(WIDTH, HEIGHT); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 221 | |
| 222 | SkCanvas canvas(bitmaps[i]); |
| 223 | |
| 224 | canvas.drawColor(SK_ColorRED); |
| 225 | |
| 226 | SkRegion localRegion = clipRegion; |
| 227 | |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 228 | SkPaint paint; |
| 229 | paint.setAlpha(128); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 230 | for (size_t j = 0; j < SK_ARRAY_COUNT(flags); ++j) { |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 231 | SkRect layerBounds = SkRect::Make(layerRect); |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 232 | canvas.saveLayer(SkCanvas::SaveLayerRec(&layerBounds, &paint, flags[j])); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 233 | |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 234 | if (i) { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 235 | SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 236 | REPORTER_ASSERT(reporter, state); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 237 | |
| 238 | SkRegion::Iterator iter(localRegion); |
| 239 | SkTDArray<int32_t> rectCoords; |
| 240 | for (; !iter.done(); iter.next()) { |
| 241 | const SkIRect& rect = iter.rect(); |
| 242 | *rectCoords.append() = rect.fLeft; |
| 243 | *rectCoords.append() = rect.fTop; |
| 244 | *rectCoords.append() = rect.fRight; |
| 245 | *rectCoords.append() = rect.fBottom; |
| 246 | } |
| 247 | bool success = drawFn(state, clipRect.fLeft, clipRect.fTop, |
| 248 | clipRect.fRight, clipRect.fBottom, clipOps[j], |
| 249 | rectCoords.count() / 4, rectCoords.begin()); |
| 250 | REPORTER_ASSERT(reporter, success); |
| 251 | |
| 252 | SkCanvasStateUtils::ReleaseCanvasState(state); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 253 | } else { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 254 | complex_clips_draw(&canvas, clipRect.fLeft, clipRect.fTop, |
| 255 | clipRect.fRight, clipRect.fBottom, clipOps[j], |
| 256 | localRegion); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 257 | } |
| 258 | |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 259 | canvas.restore(); |
| 260 | |
| 261 | // translate the canvas and region for the next iteration |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 262 | canvas.translate(0, SkIntToScalar(2*(layerRect.height() + (SPACER)))); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 263 | localRegion.translate(0, 2*(layerRect.height() + SPACER)); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | // now we memcmp the two bitmaps |
Mike Reed | df071d7 | 2017-10-09 13:09:55 -0400 | [diff] [blame] | 268 | REPORTER_ASSERT(reporter, bitmaps[0].computeByteSize() == bitmaps[1].computeByteSize()); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 269 | REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(), |
| 270 | bitmaps[1].getPixels(), |
Mike Reed | df071d7 | 2017-10-09 13:09:55 -0400 | [diff] [blame] | 271 | bitmaps[0].computeByteSize())); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 272 | } |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 273 | #endif |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 274 | |
| 275 | //////////////////////////////////////////////////////////////////////////////// |
| 276 | |
benjaminwagner | 1c2729c | 2016-01-22 09:45:14 -0800 | [diff] [blame] | 277 | #ifdef SK_SUPPORT_LEGACY_DRAWFILTER |
| 278 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 279 | class TestDrawFilter : public SkDrawFilter { |
| 280 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 281 | bool filter(SkPaint*, Type) override { return true; } |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 282 | }; |
| 283 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 284 | DEF_TEST(CanvasState_test_draw_filters, reporter) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 285 | TestDrawFilter drawFilter; |
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 | canvas.setDrawFilter(&drawFilter); |
| 291 | |
| 292 | SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
| 293 | REPORTER_ASSERT(reporter, state); |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 294 | std::unique_ptr<SkCanvas> tmpCanvas = SkCanvasStateUtils::MakeFromCanvasState(state); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 295 | REPORTER_ASSERT(reporter, tmpCanvas); |
| 296 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 297 | REPORTER_ASSERT(reporter, canvas.getDrawFilter()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 298 | REPORTER_ASSERT(reporter, nullptr == tmpCanvas->getDrawFilter()); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 299 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 300 | SkCanvasStateUtils::ReleaseCanvasState(state); |
| 301 | } |
| 302 | |
benjaminwagner | 1c2729c | 2016-01-22 09:45:14 -0800 | [diff] [blame] | 303 | #endif |
| 304 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 305 | //////////////////////////////////////////////////////////////////////////////// |
| 306 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 307 | DEF_TEST(CanvasState_test_soft_clips, reporter) { |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 308 | SkBitmap bitmap; |
| 309 | bitmap.allocN32Pixels(10, 10); |
| 310 | SkCanvas canvas(bitmap); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 311 | |
| 312 | SkRRect roundRect; |
| 313 | roundRect.setOval(SkRect::MakeWH(5, 5)); |
| 314 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 315 | canvas.clipRRect(roundRect, kIntersect_SkClipOp, true); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 316 | |
| 317 | SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
| 318 | REPORTER_ASSERT(reporter, !state); |
| 319 | } |
| 320 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 321 | DEF_TEST(CanvasState_test_saveLayer_clip, reporter) { |
Mike Reed | 3726a4a | 2017-01-19 11:36:41 -0500 | [diff] [blame] | 322 | #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
Cary Clark | 7eddfb8 | 2018-03-13 14:41:10 -0400 | [diff] [blame] | 323 | static_assert(SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag == |
| 324 | SkCanvasPriv::kDontClipToLayer_SaveLayerFlag, ""); |
Mike Reed | 3726a4a | 2017-01-19 11:36:41 -0500 | [diff] [blame] | 325 | #endif |
senorblanco@chromium.org | 89f077c | 2014-02-24 15:16:42 +0000 | [diff] [blame] | 326 | const int WIDTH = 100; |
| 327 | const int HEIGHT = 100; |
| 328 | const int LAYER_WIDTH = 50; |
| 329 | const int LAYER_HEIGHT = 50; |
| 330 | |
| 331 | SkBitmap bitmap; |
| 332 | bitmap.allocN32Pixels(WIDTH, HEIGHT); |
| 333 | SkCanvas canvas(bitmap); |
| 334 | |
| 335 | SkRect bounds = SkRect::MakeWH(SkIntToScalar(LAYER_WIDTH), SkIntToScalar(LAYER_HEIGHT)); |
| 336 | canvas.clipRect(SkRect::MakeWH(SkIntToScalar(WIDTH), SkIntToScalar(HEIGHT))); |
| 337 | |
Mike Reed | 3726a4a | 2017-01-19 11:36:41 -0500 | [diff] [blame] | 338 | SkIRect devClip; |
| 339 | // Check that saveLayer without the kClipToLayer_SaveFlag leaves the clip unchanged. |
Cary Clark | 7eddfb8 | 2018-03-13 14:41:10 -0400 | [diff] [blame] | 340 | canvas.saveLayer(SkCanvas::SaveLayerRec(&bounds, nullptr, |
| 341 | (SkCanvas::SaveLayerFlags) SkCanvasPriv::kDontClipToLayer_SaveLayerFlag)); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 342 | devClip = canvas.getDeviceClipBounds(); |
Mike Reed | 3726a4a | 2017-01-19 11:36:41 -0500 | [diff] [blame] | 343 | REPORTER_ASSERT(reporter, canvas.isClipRect()); |
| 344 | REPORTER_ASSERT(reporter, devClip.width() == WIDTH); |
| 345 | REPORTER_ASSERT(reporter, devClip.height() == HEIGHT); |
senorblanco@chromium.org | 89f077c | 2014-02-24 15:16:42 +0000 | [diff] [blame] | 346 | canvas.restore(); |
| 347 | |
| 348 | // Check that saveLayer with the kClipToLayer_SaveFlag sets the clip |
| 349 | // stack to the layer bounds. |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 350 | canvas.saveLayer(&bounds, nullptr); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 351 | devClip = canvas.getDeviceClipBounds(); |
Mike Reed | 3726a4a | 2017-01-19 11:36:41 -0500 | [diff] [blame] | 352 | REPORTER_ASSERT(reporter, canvas.isClipRect()); |
| 353 | REPORTER_ASSERT(reporter, devClip.width() == LAYER_WIDTH); |
| 354 | REPORTER_ASSERT(reporter, devClip.height() == LAYER_HEIGHT); |
senorblanco@chromium.org | 89f077c | 2014-02-24 15:16:42 +0000 | [diff] [blame] | 355 | canvas.restore(); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 356 | } |