djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2013 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include "Test.h" |
| 10 | #include "SkBitmapDevice.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkCanvasStateUtils.h" |
| 13 | #include "SkDrawFilter.h" |
| 14 | #include "SkPaint.h" |
| 15 | #include "SkRect.h" |
| 16 | #include "SkRRect.h" |
| 17 | |
| 18 | static void test_complex_layers(skiatest::Reporter* reporter) { |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 19 | const int WIDTH = 400; |
| 20 | const int HEIGHT = 400; |
| 21 | const int SPACER = 10; |
| 22 | |
djsollen@google.com | 20146b3 | 2013-08-29 20:36:22 +0000 | [diff] [blame] | 23 | SkRect rect = SkRect::MakeXYWH(SkIntToScalar(SPACER), SkIntToScalar(SPACER), |
| 24 | SkIntToScalar(WIDTH-(2*SPACER)), |
| 25 | SkIntToScalar((HEIGHT-(2*SPACER)) / 7)); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 26 | |
| 27 | const SkBitmap::Config configs[] = { SkBitmap::kRGB_565_Config, |
| 28 | SkBitmap::kARGB_8888_Config |
| 29 | }; |
| 30 | const int configCount = sizeof(configs) / sizeof(SkBitmap::Config); |
| 31 | |
| 32 | const int layerAlpha[] = { 255, 255, 0 }; |
| 33 | const SkCanvas::SaveFlags flags[] = { SkCanvas::kARGB_NoClipLayer_SaveFlag, |
| 34 | SkCanvas::kARGB_ClipLayer_SaveFlag, |
| 35 | SkCanvas::kARGB_NoClipLayer_SaveFlag |
| 36 | }; |
| 37 | REPORTER_ASSERT(reporter, sizeof(layerAlpha) == sizeof(flags)); |
| 38 | const int layerCombinations = sizeof(layerAlpha) / sizeof(int); |
| 39 | |
| 40 | for (int i = 0; i < configCount; ++i) { |
| 41 | SkBitmap bitmaps[2]; |
| 42 | for (int j = 0; j < 2; ++j) { |
| 43 | bitmaps[j].setConfig(configs[i], WIDTH, HEIGHT); |
| 44 | bitmaps[j].allocPixels(); |
| 45 | |
| 46 | SkCanvas canvas(bitmaps[j]); |
| 47 | |
| 48 | canvas.drawColor(SK_ColorRED); |
| 49 | |
| 50 | for (int k = 0; k < layerCombinations; ++k) { |
| 51 | // draw a rect within the layer's bounds and again outside the layer's bounds |
| 52 | canvas.saveLayerAlpha(&rect, layerAlpha[k], flags[k]); |
| 53 | |
| 54 | SkCanvasState* state = NULL; |
| 55 | SkCanvas* tmpCanvas = NULL; |
| 56 | if (j) { |
| 57 | state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
| 58 | REPORTER_ASSERT(reporter, state); |
| 59 | tmpCanvas = SkCanvasStateUtils::CreateFromCanvasState(state); |
| 60 | REPORTER_ASSERT(reporter, tmpCanvas); |
| 61 | } else { |
| 62 | tmpCanvas = SkRef(&canvas); |
| 63 | } |
| 64 | |
| 65 | SkPaint bluePaint; |
| 66 | bluePaint.setColor(SK_ColorBLUE); |
| 67 | bluePaint.setStyle(SkPaint::kFill_Style); |
| 68 | |
| 69 | tmpCanvas->drawRect(rect, bluePaint); |
| 70 | tmpCanvas->translate(0, rect.height() + SPACER); |
| 71 | tmpCanvas->drawRect(rect, bluePaint); |
| 72 | |
| 73 | tmpCanvas->unref(); |
| 74 | SkCanvasStateUtils::ReleaseCanvasState(state); |
| 75 | |
| 76 | canvas.restore(); |
| 77 | |
| 78 | // translate the canvas for the next iteration |
| 79 | canvas.translate(0, 2*(rect.height() + SPACER)); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // now we memcmp the two bitmaps |
| 84 | REPORTER_ASSERT(reporter, bitmaps[0].getSize() == bitmaps[1].getSize()); |
| 85 | REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(), |
| 86 | bitmaps[1].getPixels(), |
| 87 | bitmaps[0].getSize())); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | //////////////////////////////////////////////////////////////////////////////// |
| 92 | |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame^] | 93 | static void test_complex_clips(skiatest::Reporter* reporter) { |
| 94 | |
| 95 | const int WIDTH = 400; |
| 96 | const int HEIGHT = 400; |
| 97 | const SkScalar SPACER = SkIntToScalar(10); |
| 98 | |
| 99 | SkRect layerRect = SkRect::MakeWH(SkIntToScalar(WIDTH), SkIntToScalar(HEIGHT / 4)); |
| 100 | layerRect.inset(2*SPACER, 2*SPACER); |
| 101 | |
| 102 | SkRect clipRect = layerRect; |
| 103 | clipRect.fRight = clipRect.fLeft + (clipRect.width() / 2) - (2*SPACER); |
| 104 | clipRect.outset(SPACER, SPACER); |
| 105 | |
| 106 | SkIRect regionBounds; |
| 107 | clipRect.roundIn(®ionBounds); |
| 108 | regionBounds.offset(clipRect.width() + (2*SPACER), 0); |
| 109 | |
| 110 | SkIRect regionInterior = regionBounds; |
| 111 | regionInterior.inset(SPACER*3, SPACER*3); |
| 112 | |
| 113 | SkRegion clipRegion; |
| 114 | clipRegion.setRect(regionBounds); |
| 115 | clipRegion.op(regionInterior, SkRegion::kDifference_Op); |
| 116 | |
| 117 | |
| 118 | const SkRegion::Op clipOps[] = { SkRegion::kIntersect_Op, |
| 119 | SkRegion::kIntersect_Op, |
| 120 | SkRegion::kReplace_Op, |
| 121 | }; |
| 122 | const SkCanvas::SaveFlags flags[] = { SkCanvas::kARGB_NoClipLayer_SaveFlag, |
| 123 | SkCanvas::kARGB_ClipLayer_SaveFlag, |
| 124 | SkCanvas::kARGB_NoClipLayer_SaveFlag, |
| 125 | }; |
| 126 | REPORTER_ASSERT(reporter, sizeof(clipOps) == sizeof(flags)); |
| 127 | const int layerCombinations = sizeof(flags) / sizeof(SkCanvas::SaveFlags); |
| 128 | |
| 129 | SkBitmap bitmaps[2]; |
| 130 | for (int i = 0; i < 2; ++i) { |
| 131 | bitmaps[i].setConfig(SkBitmap::kARGB_8888_Config, WIDTH, HEIGHT); |
| 132 | bitmaps[i].allocPixels(); |
| 133 | |
| 134 | SkCanvas canvas(bitmaps[i]); |
| 135 | |
| 136 | canvas.drawColor(SK_ColorRED); |
| 137 | |
| 138 | SkRegion localRegion = clipRegion; |
| 139 | |
| 140 | for (int j = 0; j < layerCombinations; ++j) { |
| 141 | canvas.saveLayerAlpha(&layerRect, 128, flags[j]); |
| 142 | |
| 143 | SkCanvasState* state = NULL; |
| 144 | SkCanvas* tmpCanvas = NULL; |
| 145 | if (i) { |
| 146 | state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
| 147 | REPORTER_ASSERT(reporter, state); |
| 148 | tmpCanvas = SkCanvasStateUtils::CreateFromCanvasState(state); |
| 149 | REPORTER_ASSERT(reporter, tmpCanvas); |
| 150 | } else { |
| 151 | tmpCanvas = SkRef(&canvas); |
| 152 | } |
| 153 | |
| 154 | tmpCanvas->save(); |
| 155 | tmpCanvas->clipRect(clipRect, clipOps[j]); |
| 156 | tmpCanvas->drawColor(SK_ColorBLUE); |
| 157 | tmpCanvas->restore(); |
| 158 | |
| 159 | tmpCanvas->clipRegion(localRegion, clipOps[j]); |
| 160 | tmpCanvas->drawColor(SK_ColorBLUE); |
| 161 | |
| 162 | tmpCanvas->unref(); |
| 163 | SkCanvasStateUtils::ReleaseCanvasState(state); |
| 164 | |
| 165 | canvas.restore(); |
| 166 | |
| 167 | // translate the canvas and region for the next iteration |
| 168 | canvas.translate(0, 2*(layerRect.height() + SPACER)); |
| 169 | localRegion.translate(0, 2*(layerRect.height() + SPACER)); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // now we memcmp the two bitmaps |
| 174 | REPORTER_ASSERT(reporter, bitmaps[0].getSize() == bitmaps[1].getSize()); |
| 175 | REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(), |
| 176 | bitmaps[1].getPixels(), |
| 177 | bitmaps[0].getSize())); |
| 178 | } |
| 179 | |
| 180 | //////////////////////////////////////////////////////////////////////////////// |
| 181 | |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 182 | class TestDrawFilter : public SkDrawFilter { |
| 183 | public: |
| 184 | virtual bool filter(SkPaint*, Type) SK_OVERRIDE { return true; } |
| 185 | }; |
| 186 | |
| 187 | static void test_draw_filters(skiatest::Reporter* reporter) { |
| 188 | TestDrawFilter drawFilter; |
| 189 | SkBitmapDevice device(SkBitmap::kARGB_8888_Config, 10, 10); |
| 190 | SkCanvas canvas(&device); |
| 191 | |
| 192 | canvas.setDrawFilter(&drawFilter); |
| 193 | |
| 194 | SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
| 195 | REPORTER_ASSERT(reporter, state); |
| 196 | SkCanvas* tmpCanvas = SkCanvasStateUtils::CreateFromCanvasState(state); |
| 197 | REPORTER_ASSERT(reporter, tmpCanvas); |
| 198 | |
| 199 | REPORTER_ASSERT(reporter, NULL != canvas.getDrawFilter()); |
| 200 | REPORTER_ASSERT(reporter, NULL == tmpCanvas->getDrawFilter()); |
| 201 | |
| 202 | tmpCanvas->unref(); |
| 203 | SkCanvasStateUtils::ReleaseCanvasState(state); |
| 204 | } |
| 205 | |
| 206 | //////////////////////////////////////////////////////////////////////////////// |
| 207 | |
| 208 | static void test_soft_clips(skiatest::Reporter* reporter) { |
| 209 | SkBitmapDevice device(SkBitmap::kARGB_8888_Config, 10, 10); |
| 210 | SkCanvas canvas(&device); |
| 211 | |
| 212 | SkRRect roundRect; |
| 213 | roundRect.setOval(SkRect::MakeWH(5, 5)); |
| 214 | |
| 215 | canvas.clipRRect(roundRect, SkRegion::kIntersect_Op, true); |
| 216 | |
| 217 | SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
| 218 | REPORTER_ASSERT(reporter, !state); |
| 219 | } |
| 220 | |
| 221 | //////////////////////////////////////////////////////////////////////////////// |
| 222 | |
| 223 | static void test_canvas_state_utils(skiatest::Reporter* reporter) { |
| 224 | test_complex_layers(reporter); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame^] | 225 | test_complex_clips(reporter); |
djsollen@google.com | 5587ac0 | 2013-08-29 20:20:40 +0000 | [diff] [blame] | 226 | test_draw_filters(reporter); |
| 227 | test_soft_clips(reporter); |
| 228 | } |
| 229 | |
| 230 | #include "TestClassDef.h" |
| 231 | DEFINE_TESTCLASS("CanvasState", TestCanvasStateClass, test_canvas_state_utils) |