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 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 8 | #include "SkBitmapDevice.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkCanvasStateUtils.h" |
| 11 | #include "SkDrawFilter.h" |
commit-bot@chromium.org | 07f6cf3 | 2013-09-18 20:15:12 +0000 | [diff] [blame] | 12 | #include "SkError.h" |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 13 | #include "SkPaint.h" |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 14 | #include "SkRRect.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 15 | #include "SkRect.h" |
| 16 | #include "Test.h" |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 17 | |
| 18 | static void test_complex_layers(skiatest::Reporter* reporter) { |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 19 | #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 20 | const int WIDTH = 400; |
| 21 | const int HEIGHT = 400; |
| 22 | const int SPACER = 10; |
| 23 | |
djsollen@google.com | 20146b3 | 2013-08-29 20:36:22 +0000 | [diff] [blame] | 24 | SkRect rect = SkRect::MakeXYWH(SkIntToScalar(SPACER), SkIntToScalar(SPACER), |
| 25 | SkIntToScalar(WIDTH-(2*SPACER)), |
| 26 | SkIntToScalar((HEIGHT-(2*SPACER)) / 7)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 27 | |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 28 | const SkColorType colorTypes[] = { |
commit-bot@chromium.org | 149e9a1 | 2014-04-09 20:45:29 +0000 | [diff] [blame] | 29 | kRGB_565_SkColorType, kN32_SkColorType |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 30 | }; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 31 | const int configCount = sizeof(colorTypes) / sizeof(SkBitmap::Config); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 32 | |
| 33 | const int layerAlpha[] = { 255, 255, 0 }; |
| 34 | const SkCanvas::SaveFlags flags[] = { SkCanvas::kARGB_NoClipLayer_SaveFlag, |
| 35 | SkCanvas::kARGB_ClipLayer_SaveFlag, |
| 36 | SkCanvas::kARGB_NoClipLayer_SaveFlag |
| 37 | }; |
| 38 | REPORTER_ASSERT(reporter, sizeof(layerAlpha) == sizeof(flags)); |
| 39 | const int layerCombinations = sizeof(layerAlpha) / sizeof(int); |
| 40 | |
| 41 | for (int i = 0; i < configCount; ++i) { |
| 42 | SkBitmap bitmaps[2]; |
| 43 | for (int j = 0; j < 2; ++j) { |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 44 | bitmaps[j].allocPixels(SkImageInfo::Make(WIDTH, HEIGHT, |
| 45 | colorTypes[i], |
| 46 | kPremul_SkAlphaType)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 47 | |
| 48 | SkCanvas canvas(bitmaps[j]); |
| 49 | |
| 50 | canvas.drawColor(SK_ColorRED); |
| 51 | |
| 52 | for (int k = 0; k < layerCombinations; ++k) { |
| 53 | // draw a rect within the layer's bounds and again outside the layer's bounds |
| 54 | canvas.saveLayerAlpha(&rect, layerAlpha[k], flags[k]); |
| 55 | |
| 56 | SkCanvasState* state = NULL; |
| 57 | SkCanvas* tmpCanvas = NULL; |
| 58 | if (j) { |
| 59 | state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
| 60 | REPORTER_ASSERT(reporter, state); |
| 61 | tmpCanvas = SkCanvasStateUtils::CreateFromCanvasState(state); |
| 62 | REPORTER_ASSERT(reporter, tmpCanvas); |
| 63 | } else { |
| 64 | tmpCanvas = SkRef(&canvas); |
| 65 | } |
| 66 | |
| 67 | SkPaint bluePaint; |
| 68 | bluePaint.setColor(SK_ColorBLUE); |
| 69 | bluePaint.setStyle(SkPaint::kFill_Style); |
| 70 | |
| 71 | tmpCanvas->drawRect(rect, bluePaint); |
| 72 | tmpCanvas->translate(0, rect.height() + SPACER); |
| 73 | tmpCanvas->drawRect(rect, bluePaint); |
| 74 | |
| 75 | tmpCanvas->unref(); |
| 76 | SkCanvasStateUtils::ReleaseCanvasState(state); |
| 77 | |
| 78 | canvas.restore(); |
| 79 | |
| 80 | // translate the canvas for the next iteration |
| 81 | canvas.translate(0, 2*(rect.height() + SPACER)); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // now we memcmp the two bitmaps |
| 86 | REPORTER_ASSERT(reporter, bitmaps[0].getSize() == bitmaps[1].getSize()); |
| 87 | REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(), |
| 88 | bitmaps[1].getPixels(), |
| 89 | bitmaps[0].getSize())); |
| 90 | } |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 91 | #endif |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | //////////////////////////////////////////////////////////////////////////////// |
| 95 | |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 96 | static void test_complex_clips(skiatest::Reporter* reporter) { |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 97 | #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 98 | const int WIDTH = 400; |
| 99 | const int HEIGHT = 400; |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 100 | const int SPACER = 10; |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 101 | |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 102 | SkIRect layerRect = SkIRect::MakeWH(WIDTH, HEIGHT / 4); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 103 | layerRect.inset(2*SPACER, 2*SPACER); |
| 104 | |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 105 | SkIRect clipRect = layerRect; |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 106 | clipRect.fRight = clipRect.fLeft + (clipRect.width() / 2) - (2*SPACER); |
| 107 | clipRect.outset(SPACER, SPACER); |
| 108 | |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 109 | SkIRect regionBounds = clipRect; |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 110 | regionBounds.offset(clipRect.width() + (2*SPACER), 0); |
| 111 | |
| 112 | SkIRect regionInterior = regionBounds; |
| 113 | regionInterior.inset(SPACER*3, SPACER*3); |
| 114 | |
| 115 | SkRegion clipRegion; |
| 116 | clipRegion.setRect(regionBounds); |
| 117 | clipRegion.op(regionInterior, SkRegion::kDifference_Op); |
| 118 | |
| 119 | |
| 120 | const SkRegion::Op clipOps[] = { SkRegion::kIntersect_Op, |
| 121 | SkRegion::kIntersect_Op, |
| 122 | SkRegion::kReplace_Op, |
| 123 | }; |
| 124 | const SkCanvas::SaveFlags flags[] = { SkCanvas::kARGB_NoClipLayer_SaveFlag, |
| 125 | SkCanvas::kARGB_ClipLayer_SaveFlag, |
| 126 | SkCanvas::kARGB_NoClipLayer_SaveFlag, |
| 127 | }; |
| 128 | REPORTER_ASSERT(reporter, sizeof(clipOps) == sizeof(flags)); |
| 129 | const int layerCombinations = sizeof(flags) / sizeof(SkCanvas::SaveFlags); |
| 130 | |
| 131 | SkBitmap bitmaps[2]; |
| 132 | for (int i = 0; i < 2; ++i) { |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 133 | bitmaps[i].allocN32Pixels(WIDTH, HEIGHT); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 134 | |
| 135 | SkCanvas canvas(bitmaps[i]); |
| 136 | |
| 137 | canvas.drawColor(SK_ColorRED); |
| 138 | |
| 139 | SkRegion localRegion = clipRegion; |
| 140 | |
| 141 | for (int j = 0; j < layerCombinations; ++j) { |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 142 | SkRect layerBounds = SkRect::Make(layerRect); |
| 143 | canvas.saveLayerAlpha(&layerBounds, 128, flags[j]); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 144 | |
| 145 | SkCanvasState* state = NULL; |
| 146 | SkCanvas* tmpCanvas = NULL; |
| 147 | if (i) { |
| 148 | state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
| 149 | REPORTER_ASSERT(reporter, state); |
| 150 | tmpCanvas = SkCanvasStateUtils::CreateFromCanvasState(state); |
| 151 | REPORTER_ASSERT(reporter, tmpCanvas); |
| 152 | } else { |
| 153 | tmpCanvas = SkRef(&canvas); |
| 154 | } |
| 155 | |
| 156 | tmpCanvas->save(); |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 157 | tmpCanvas->clipRect(SkRect::Make(clipRect), clipOps[j]); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 158 | tmpCanvas->drawColor(SK_ColorBLUE); |
| 159 | tmpCanvas->restore(); |
| 160 | |
| 161 | tmpCanvas->clipRegion(localRegion, clipOps[j]); |
| 162 | tmpCanvas->drawColor(SK_ColorBLUE); |
| 163 | |
| 164 | tmpCanvas->unref(); |
| 165 | SkCanvasStateUtils::ReleaseCanvasState(state); |
| 166 | |
| 167 | canvas.restore(); |
| 168 | |
| 169 | // translate the canvas and region for the next iteration |
djsollen@google.com | 1037c7b | 2013-09-04 18:20:30 +0000 | [diff] [blame] | 170 | canvas.translate(0, SkIntToScalar(2*(layerRect.height() + (SPACER)))); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 171 | localRegion.translate(0, 2*(layerRect.height() + SPACER)); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // now we memcmp the two bitmaps |
| 176 | REPORTER_ASSERT(reporter, bitmaps[0].getSize() == bitmaps[1].getSize()); |
| 177 | REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(), |
| 178 | bitmaps[1].getPixels(), |
| 179 | bitmaps[0].getSize())); |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 180 | #endif |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | //////////////////////////////////////////////////////////////////////////////// |
| 184 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 185 | class TestDrawFilter : public SkDrawFilter { |
| 186 | public: |
| 187 | virtual bool filter(SkPaint*, Type) SK_OVERRIDE { return true; } |
| 188 | }; |
| 189 | |
| 190 | static void test_draw_filters(skiatest::Reporter* reporter) { |
| 191 | TestDrawFilter drawFilter; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 192 | SkBitmap bitmap; |
| 193 | bitmap.allocN32Pixels(10, 10); |
| 194 | SkCanvas canvas(bitmap); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 195 | |
| 196 | canvas.setDrawFilter(&drawFilter); |
| 197 | |
| 198 | SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
| 199 | REPORTER_ASSERT(reporter, state); |
| 200 | SkCanvas* tmpCanvas = SkCanvasStateUtils::CreateFromCanvasState(state); |
| 201 | REPORTER_ASSERT(reporter, tmpCanvas); |
| 202 | |
| 203 | REPORTER_ASSERT(reporter, NULL != canvas.getDrawFilter()); |
| 204 | REPORTER_ASSERT(reporter, NULL == tmpCanvas->getDrawFilter()); |
| 205 | |
| 206 | tmpCanvas->unref(); |
| 207 | SkCanvasStateUtils::ReleaseCanvasState(state); |
| 208 | } |
| 209 | |
| 210 | //////////////////////////////////////////////////////////////////////////////// |
| 211 | |
commit-bot@chromium.org | 07f6cf3 | 2013-09-18 20:15:12 +0000 | [diff] [blame] | 212 | // we need this function to prevent SkError from printing to stdout |
| 213 | static void error_callback(SkError code, void* ctx) {} |
| 214 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 215 | static void test_soft_clips(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 216 | SkBitmap bitmap; |
| 217 | bitmap.allocN32Pixels(10, 10); |
| 218 | SkCanvas canvas(bitmap); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 219 | |
| 220 | SkRRect roundRect; |
| 221 | roundRect.setOval(SkRect::MakeWH(5, 5)); |
| 222 | |
| 223 | canvas.clipRRect(roundRect, SkRegion::kIntersect_Op, true); |
| 224 | |
commit-bot@chromium.org | 07f6cf3 | 2013-09-18 20:15:12 +0000 | [diff] [blame] | 225 | SkSetErrorCallback(error_callback, NULL); |
| 226 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 227 | SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
| 228 | REPORTER_ASSERT(reporter, !state); |
commit-bot@chromium.org | 07f6cf3 | 2013-09-18 20:15:12 +0000 | [diff] [blame] | 229 | |
| 230 | REPORTER_ASSERT(reporter, kInvalidOperation_SkError == SkGetLastError()); |
| 231 | SkClearLastError(); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 232 | } |
| 233 | |
senorblanco@chromium.org | 89f077c | 2014-02-24 15:16:42 +0000 | [diff] [blame] | 234 | static void test_saveLayer_clip(skiatest::Reporter* reporter) { |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 235 | #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
senorblanco@chromium.org | 89f077c | 2014-02-24 15:16:42 +0000 | [diff] [blame] | 236 | const int WIDTH = 100; |
| 237 | const int HEIGHT = 100; |
| 238 | const int LAYER_WIDTH = 50; |
| 239 | const int LAYER_HEIGHT = 50; |
| 240 | |
| 241 | SkBitmap bitmap; |
| 242 | bitmap.allocN32Pixels(WIDTH, HEIGHT); |
| 243 | SkCanvas canvas(bitmap); |
| 244 | |
| 245 | SkRect bounds = SkRect::MakeWH(SkIntToScalar(LAYER_WIDTH), SkIntToScalar(LAYER_HEIGHT)); |
| 246 | canvas.clipRect(SkRect::MakeWH(SkIntToScalar(WIDTH), SkIntToScalar(HEIGHT))); |
| 247 | |
| 248 | // Check that saveLayer without the kClipToLayer_SaveFlag leaves the |
| 249 | // clip stack unchanged. |
| 250 | canvas.saveLayer(&bounds, NULL, SkCanvas::kARGB_NoClipLayer_SaveFlag); |
| 251 | SkRect clipStackBounds; |
| 252 | SkClipStack::BoundsType boundsType; |
| 253 | canvas.getClipStack()->getBounds(&clipStackBounds, &boundsType); |
| 254 | REPORTER_ASSERT(reporter, clipStackBounds.width() == WIDTH); |
| 255 | REPORTER_ASSERT(reporter, clipStackBounds.height() == HEIGHT); |
| 256 | canvas.restore(); |
| 257 | |
| 258 | // Check that saveLayer with the kClipToLayer_SaveFlag sets the clip |
| 259 | // stack to the layer bounds. |
| 260 | canvas.saveLayer(&bounds, NULL, SkCanvas::kARGB_ClipLayer_SaveFlag); |
| 261 | canvas.getClipStack()->getBounds(&clipStackBounds, &boundsType); |
| 262 | REPORTER_ASSERT(reporter, clipStackBounds.width() == LAYER_WIDTH); |
| 263 | REPORTER_ASSERT(reporter, clipStackBounds.height() == LAYER_HEIGHT); |
| 264 | |
| 265 | canvas.restore(); |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 266 | #endif |
senorblanco@chromium.org | 89f077c | 2014-02-24 15:16:42 +0000 | [diff] [blame] | 267 | } |
| 268 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 269 | DEF_TEST(CanvasState, reporter) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 270 | test_complex_layers(reporter); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 271 | test_complex_clips(reporter); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 272 | test_draw_filters(reporter); |
| 273 | test_soft_clips(reporter); |
senorblanco@chromium.org | 89f077c | 2014-02-24 15:16:42 +0000 | [diff] [blame] | 274 | test_saveLayer_clip(reporter); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 275 | } |