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