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