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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/core/SkTLazy.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "tests/Test.h" |
| 24 | #include "tools/flags/CommandLineFlags.h" |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 25 | |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 26 | #include <cstring> |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 27 | |
| 28 | class SkCanvasState; |
| 29 | |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 30 | // Uncomment to include tests of CanvasState across a library boundary. This will change how 'dm' |
| 31 | // is built so that the functions defined in CanvasStateHelpers do not exist inside 'dm', and are |
| 32 | // instead compiled as part of the 'canvas_state_lib' build target. This produces a shared library |
| 33 | // that must be passed to 'dm' using the --library flag when running. |
| 34 | // #define SK_TEST_CANVAS_STATE_CROSS_LIBRARY |
| 35 | |
| 36 | // Must be included after SK_TEST_CANVAS_STATE_CROSS_LIBRARY is defined |
| 37 | #include "tests/CanvasStateHelpers.h" |
| 38 | |
| 39 | // dlopen, the library flag and canvas state helpers are only used for tests which require this flag |
| 40 | #if defined(SK_TEST_CANVAS_STATE_CROSS_LIBRARY) |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 41 | |
Mike Klein | 84836b7 | 2019-03-21 11:31:36 -0500 | [diff] [blame] | 42 | static DEFINE_string(library, "", |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 43 | "Support library to use for CanvasState test. Must be provided when" |
| 44 | " SK_TEST_CANVAS_STATE_CROSS_LIBRARY to specify the dynamically loaded library" |
| 45 | " that receives the captured canvas state. Functions from the library will be" |
| 46 | " called to test SkCanvasState. The library is built from the canvas_state_lib" |
| 47 | " target"); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 48 | |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 49 | #include "src/ports/SkOSLibrary.h" |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 50 | |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 51 | // Automatically loads library passed to --library flag and closes it when it goes out of scope. |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 52 | class OpenLibResult { |
| 53 | public: |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 54 | OpenLibResult(skiatest::Reporter* reporter) { |
| 55 | if (FLAGS_library.count() == 1) { |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 56 | fLibrary = SkLoadDynamicLibrary(FLAGS_library[0]); |
| 57 | REPORTER_ASSERT(reporter, fLibrary != nullptr, "Failed to open library!"); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 58 | } else { |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 59 | fLibrary = nullptr; |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 63 | ~OpenLibResult() { |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 64 | if (fLibrary) { |
| 65 | SkFreeDynamicLibrary(fLibrary); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 69 | // Load a function address from the library object, or null if the library had failed |
| 70 | void* procAddress(const char* funcName) { |
| 71 | if (fLibrary) { |
| 72 | return SkGetProcedureAddress(fLibrary, funcName); |
| 73 | } |
| 74 | return nullptr; |
| 75 | } |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 76 | |
| 77 | private: |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 78 | void* fLibrary; |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 81 | #endif |
| 82 | |
Mike Reed | a5b0769 | 2019-09-03 13:23:43 -0400 | [diff] [blame] | 83 | static void write_image(const SkImage* img, const char path[]) { |
| 84 | auto data = img->encodeToData(); |
| 85 | SkFILEWStream(path).write(data->data(), data->size()); |
| 86 | } |
| 87 | |
| 88 | static void compare(skiatest::Reporter* reporter, SkImage* img0, SkImage* img1) { |
| 89 | if (false) { |
| 90 | static int counter; |
| 91 | |
| 92 | SkDebugf("---- counter %d\n", counter); |
| 93 | SkString name; |
| 94 | name.printf("no_capture_%d.png", counter); |
| 95 | write_image(img0, name.c_str()); |
| 96 | name.printf("capture_%d.png", counter); |
| 97 | write_image(img1, name.c_str()); |
| 98 | counter++; |
| 99 | } |
| 100 | |
| 101 | SkPixmap pm[2]; |
| 102 | REPORTER_ASSERT(reporter, img0->peekPixels(&pm[0])); |
| 103 | REPORTER_ASSERT(reporter, img1->peekPixels(&pm[1])); |
| 104 | // now we memcmp the two bitmaps |
| 105 | REPORTER_ASSERT(reporter, pm[0].computeByteSize() == pm[1].computeByteSize()); |
| 106 | REPORTER_ASSERT(reporter, pm[0].rowBytes() == (size_t)pm[0].width() * pm[0].info().bytesPerPixel()); |
| 107 | REPORTER_ASSERT(reporter, pm[1].rowBytes() == (size_t)pm[1].width() * pm[1].info().bytesPerPixel()); |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 108 | if (memcmp(pm[0].addr(0, 0), pm[1].addr(0, 0), pm[0].computeByteSize()) != 0) { |
Mike Reed | a5b0769 | 2019-09-03 13:23:43 -0400 | [diff] [blame] | 109 | REPORTER_ASSERT(reporter, false); |
| 110 | } |
| 111 | } |
| 112 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 113 | DEF_TEST(CanvasState_test_complex_layers, reporter) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 114 | const int WIDTH = 400; |
| 115 | const int HEIGHT = 400; |
| 116 | const int SPACER = 10; |
| 117 | |
djsollen@google.com | 20146b3 | 2013-08-29 20:36:22 +0000 | [diff] [blame] | 118 | SkRect rect = SkRect::MakeXYWH(SkIntToScalar(SPACER), SkIntToScalar(SPACER), |
| 119 | SkIntToScalar(WIDTH-(2*SPACER)), |
| 120 | SkIntToScalar((HEIGHT-(2*SPACER)) / 7)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 121 | |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 122 | const SkColorType colorTypes[] = { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 123 | kRGB_565_SkColorType, kN32_SkColorType |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 124 | }; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 125 | |
| 126 | const int layerAlpha[] = { 255, 255, 0 }; |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 127 | |
| 128 | bool (*drawFn)(SkCanvasState* state, float l, float t, |
| 129 | float r, float b, int32_t s); |
| 130 | |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 131 | #if defined(SK_TEST_CANVAS_STATE_CROSS_LIBRARY) |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 132 | OpenLibResult openLibResult(reporter); |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 133 | *(void**) (&drawFn) = openLibResult.procAddress("complex_layers_draw_from_canvas_state"); |
| 134 | #else |
| 135 | drawFn = complex_layers_draw_from_canvas_state; |
| 136 | #endif |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 137 | |
| 138 | REPORTER_ASSERT(reporter, drawFn); |
| 139 | if (!drawFn) { |
| 140 | return; |
| 141 | } |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 142 | |
scroggo | ecce60b | 2014-07-09 07:26:40 -0700 | [diff] [blame] | 143 | for (size_t i = 0; i < SK_ARRAY_COUNT(colorTypes); ++i) { |
Mike Reed | a5b0769 | 2019-09-03 13:23:43 -0400 | [diff] [blame] | 144 | sk_sp<SkImage> images[2]; |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 145 | for (int j = 0; j < 2; ++j) { |
Mike Reed | a5b0769 | 2019-09-03 13:23:43 -0400 | [diff] [blame] | 146 | auto surf = SkSurface::MakeRaster(SkImageInfo::Make(WIDTH, HEIGHT, |
| 147 | colorTypes[i], |
| 148 | kPremul_SkAlphaType)); |
| 149 | SkCanvas* canvas = surf->getCanvas(); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 150 | |
Mike Reed | a5b0769 | 2019-09-03 13:23:43 -0400 | [diff] [blame] | 151 | canvas->drawColor(SK_ColorRED); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 152 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 153 | for (size_t k = 0; k < SK_ARRAY_COUNT(layerAlpha); ++k) { |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 154 | SkTLazy<SkPaint> paint; |
| 155 | if (layerAlpha[k] != 0xFF) { |
| 156 | paint.init()->setAlpha(layerAlpha[k]); |
| 157 | } |
| 158 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 159 | // draw a rect within the layer's bounds and again outside the layer's bounds |
Michael Ludwig | e0dee01 | 2020-12-04 12:59:39 -0500 | [diff] [blame] | 160 | canvas->saveLayer(SkCanvas::SaveLayerRec(&rect, paint.getMaybeNull())); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 161 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 162 | if (j) { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 163 | // Capture from the first Skia. |
Mike Reed | a5b0769 | 2019-09-03 13:23:43 -0400 | [diff] [blame] | 164 | SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(canvas); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 165 | REPORTER_ASSERT(reporter, state); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 166 | |
| 167 | // And draw to it in the second Skia. |
| 168 | bool success = complex_layers_draw_from_canvas_state(state, |
| 169 | rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, SPACER); |
| 170 | REPORTER_ASSERT(reporter, success); |
| 171 | |
| 172 | // And release it in the *first* Skia. |
| 173 | SkCanvasStateUtils::ReleaseCanvasState(state); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 174 | } else { |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 175 | // Draw in the first Skia. |
Mike Reed | a5b0769 | 2019-09-03 13:23:43 -0400 | [diff] [blame] | 176 | complex_layers_draw(canvas, rect.fLeft, rect.fTop, |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 177 | rect.fRight, rect.fBottom, SPACER); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Mike Reed | a5b0769 | 2019-09-03 13:23:43 -0400 | [diff] [blame] | 180 | canvas->restore(); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 181 | |
| 182 | // translate the canvas for the next iteration |
Mike Reed | a5b0769 | 2019-09-03 13:23:43 -0400 | [diff] [blame] | 183 | canvas->translate(0, 2*(rect.height() + SPACER)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 184 | } |
Mike Reed | a5b0769 | 2019-09-03 13:23:43 -0400 | [diff] [blame] | 185 | images[j] = surf->makeImageSnapshot(); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Mike Reed | a5b0769 | 2019-09-03 13:23:43 -0400 | [diff] [blame] | 188 | compare(reporter, images[0].get(), images[1].get()); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | //////////////////////////////////////////////////////////////////////////////// |
| 193 | |
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, |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 219 | SkRegion::kDifference_Op, |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 220 | }; |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 221 | |
| 222 | bool (*drawFn)(SkCanvasState* state, int32_t l, int32_t t, |
| 223 | int32_t r, int32_t b, int32_t clipOp, |
| 224 | int32_t regionRects, int32_t* rectCoords); |
| 225 | |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 226 | #if defined(SK_TEST_CANVAS_STATE_CROSS_LIBRARY) |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 227 | OpenLibResult openLibResult(reporter); |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 228 | *(void**) (&drawFn) = openLibResult.procAddress("complex_clips_draw_from_canvas_state"); |
| 229 | #else |
| 230 | drawFn = complex_clips_draw_from_canvas_state; |
| 231 | #endif |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 232 | |
| 233 | REPORTER_ASSERT(reporter, drawFn); |
| 234 | if (!drawFn) { |
| 235 | return; |
| 236 | } |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 237 | |
Mike Reed | a5b0769 | 2019-09-03 13:23:43 -0400 | [diff] [blame] | 238 | sk_sp<SkImage> images[2]; |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 239 | for (int i = 0; i < 2; ++i) { |
Mike Reed | a5b0769 | 2019-09-03 13:23:43 -0400 | [diff] [blame] | 240 | auto surf = SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(WIDTH, HEIGHT)); |
| 241 | SkCanvas* canvas = surf->getCanvas(); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 242 | |
Mike Reed | a5b0769 | 2019-09-03 13:23:43 -0400 | [diff] [blame] | 243 | canvas->drawColor(SK_ColorRED); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 244 | |
| 245 | SkRegion localRegion = clipRegion; |
| 246 | |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 247 | SkPaint paint; |
| 248 | paint.setAlpha(128); |
Michael Ludwig | 3021795 | 2020-12-04 12:58:35 -0500 | [diff] [blame] | 249 | for (size_t j = 0; j < SK_ARRAY_COUNT(clipOps); ++j) { |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 250 | SkRect layerBounds = SkRect::Make(layerRect); |
Michael Ludwig | e0dee01 | 2020-12-04 12:59:39 -0500 | [diff] [blame] | 251 | canvas->saveLayer(SkCanvas::SaveLayerRec(&layerBounds, &paint)); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 252 | |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 253 | if (i) { |
Mike Reed | a5b0769 | 2019-09-03 13:23:43 -0400 | [diff] [blame] | 254 | SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(canvas); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 255 | REPORTER_ASSERT(reporter, state); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 256 | |
| 257 | SkRegion::Iterator iter(localRegion); |
| 258 | SkTDArray<int32_t> rectCoords; |
| 259 | for (; !iter.done(); iter.next()) { |
| 260 | const SkIRect& rect = iter.rect(); |
| 261 | *rectCoords.append() = rect.fLeft; |
| 262 | *rectCoords.append() = rect.fTop; |
| 263 | *rectCoords.append() = rect.fRight; |
| 264 | *rectCoords.append() = rect.fBottom; |
| 265 | } |
| 266 | bool success = drawFn(state, clipRect.fLeft, clipRect.fTop, |
| 267 | clipRect.fRight, clipRect.fBottom, clipOps[j], |
| 268 | rectCoords.count() / 4, rectCoords.begin()); |
| 269 | REPORTER_ASSERT(reporter, success); |
| 270 | |
| 271 | SkCanvasStateUtils::ReleaseCanvasState(state); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 272 | } else { |
Mike Reed | a5b0769 | 2019-09-03 13:23:43 -0400 | [diff] [blame] | 273 | complex_clips_draw(canvas, clipRect.fLeft, clipRect.fTop, |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 274 | clipRect.fRight, clipRect.fBottom, clipOps[j], |
| 275 | localRegion); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Mike Reed | a5b0769 | 2019-09-03 13:23:43 -0400 | [diff] [blame] | 278 | canvas->restore(); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 279 | |
| 280 | // translate the canvas and region for the next iteration |
Mike Reed | a5b0769 | 2019-09-03 13:23:43 -0400 | [diff] [blame] | 281 | canvas->translate(0, SkIntToScalar(2*(layerRect.height() + (SPACER)))); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 282 | localRegion.translate(0, 2*(layerRect.height() + SPACER)); |
| 283 | } |
Mike Reed | a5b0769 | 2019-09-03 13:23:43 -0400 | [diff] [blame] | 284 | images[i] = surf->makeImageSnapshot(); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Mike Reed | a5b0769 | 2019-09-03 13:23:43 -0400 | [diff] [blame] | 287 | compare(reporter, images[0].get(), images[1].get()); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | //////////////////////////////////////////////////////////////////////////////// |
| 291 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 292 | DEF_TEST(CanvasState_test_soft_clips, reporter) { |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 293 | SkBitmap bitmap; |
| 294 | bitmap.allocN32Pixels(10, 10); |
| 295 | SkCanvas canvas(bitmap); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 296 | |
| 297 | SkRRect roundRect; |
| 298 | roundRect.setOval(SkRect::MakeWH(5, 5)); |
| 299 | |
Michael Ludwig | 2f6e2f8 | 2021-08-03 13:08:50 -0400 | [diff] [blame] | 300 | canvas.clipRRect(roundRect, SkClipOp::kIntersect, true); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 301 | |
| 302 | SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
| 303 | REPORTER_ASSERT(reporter, !state); |
| 304 | } |
| 305 | |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 306 | DEF_TEST(CanvasState_test_saveLayer_clip, reporter) { |
senorblanco@chromium.org | 89f077c | 2014-02-24 15:16:42 +0000 | [diff] [blame] | 307 | const int WIDTH = 100; |
| 308 | const int HEIGHT = 100; |
| 309 | const int LAYER_WIDTH = 50; |
| 310 | const int LAYER_HEIGHT = 50; |
| 311 | |
| 312 | SkBitmap bitmap; |
| 313 | bitmap.allocN32Pixels(WIDTH, HEIGHT); |
| 314 | SkCanvas canvas(bitmap); |
| 315 | |
| 316 | SkRect bounds = SkRect::MakeWH(SkIntToScalar(LAYER_WIDTH), SkIntToScalar(LAYER_HEIGHT)); |
| 317 | canvas.clipRect(SkRect::MakeWH(SkIntToScalar(WIDTH), SkIntToScalar(HEIGHT))); |
| 318 | |
Michael Ludwig | e0dee01 | 2020-12-04 12:59:39 -0500 | [diff] [blame] | 319 | // Check that saveLayer sets the clip stack to the layer bounds. |
fmalita | 55b29b2 | 2016-01-20 11:17:39 -0800 | [diff] [blame] | 320 | canvas.saveLayer(&bounds, nullptr); |
Michael Ludwig | e0dee01 | 2020-12-04 12:59:39 -0500 | [diff] [blame] | 321 | SkIRect devClip = canvas.getDeviceClipBounds(); |
Mike Reed | 3726a4a | 2017-01-19 11:36:41 -0500 | [diff] [blame] | 322 | REPORTER_ASSERT(reporter, canvas.isClipRect()); |
| 323 | REPORTER_ASSERT(reporter, devClip.width() == LAYER_WIDTH); |
| 324 | REPORTER_ASSERT(reporter, devClip.height() == LAYER_HEIGHT); |
senorblanco@chromium.org | 89f077c | 2014-02-24 15:16:42 +0000 | [diff] [blame] | 325 | canvas.restore(); |
scroggo | 2451937 | 2014-07-22 12:38:55 -0700 | [diff] [blame] | 326 | } |