OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2013 Google Inc. | 3 * Copyright 2013 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #include "Test.h" | 9 #include "Test.h" |
10 #include "SkBitmapDevice.h" | 10 #include "SkBitmapDevice.h" |
11 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
12 #include "SkCanvasStateUtils.h" | 12 #include "SkCanvasStateUtils.h" |
13 #include "SkDrawFilter.h" | 13 #include "SkDrawFilter.h" |
14 #include "SkPaint.h" | 14 #include "SkPaint.h" |
15 #include "SkRect.h" | 15 #include "SkRect.h" |
16 #include "SkRRect.h" | 16 #include "SkRRect.h" |
17 | 17 |
18 static void test_complex_layers(skiatest::Reporter* reporter) { | 18 static void test_complex_layers(skiatest::Reporter* reporter) { |
19 | |
20 const int WIDTH = 400; | 19 const int WIDTH = 400; |
21 const int HEIGHT = 400; | 20 const int HEIGHT = 400; |
22 const int SPACER = 10; | 21 const int SPACER = 10; |
23 | 22 |
24 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(SPACER), SkIntToScalar(SPACER), | 23 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(SPACER), SkIntToScalar(SPACER), |
25 SkIntToScalar(WIDTH-(2*SPACER)), | 24 SkIntToScalar(WIDTH-(2*SPACER)), |
26 SkIntToScalar((HEIGHT-(2*SPACER)) / 7)); | 25 SkIntToScalar((HEIGHT-(2*SPACER)) / 7)); |
27 | 26 |
28 const SkBitmap::Config configs[] = { SkBitmap::kRGB_565_Config, | 27 const SkBitmap::Config configs[] = { SkBitmap::kRGB_565_Config, |
29 SkBitmap::kARGB_8888_Config | 28 SkBitmap::kARGB_8888_Config |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 // now we memcmp the two bitmaps | 83 // now we memcmp the two bitmaps |
85 REPORTER_ASSERT(reporter, bitmaps[0].getSize() == bitmaps[1].getSize()); | 84 REPORTER_ASSERT(reporter, bitmaps[0].getSize() == bitmaps[1].getSize()); |
86 REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(), | 85 REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(), |
87 bitmaps[1].getPixels(), | 86 bitmaps[1].getPixels(), |
88 bitmaps[0].getSize())); | 87 bitmaps[0].getSize())); |
89 } | 88 } |
90 } | 89 } |
91 | 90 |
92 //////////////////////////////////////////////////////////////////////////////// | 91 //////////////////////////////////////////////////////////////////////////////// |
93 | 92 |
| 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 |
94 class TestDrawFilter : public SkDrawFilter { | 182 class TestDrawFilter : public SkDrawFilter { |
95 public: | 183 public: |
96 virtual bool filter(SkPaint*, Type) SK_OVERRIDE { return true; } | 184 virtual bool filter(SkPaint*, Type) SK_OVERRIDE { return true; } |
97 }; | 185 }; |
98 | 186 |
99 static void test_draw_filters(skiatest::Reporter* reporter) { | 187 static void test_draw_filters(skiatest::Reporter* reporter) { |
100 TestDrawFilter drawFilter; | 188 TestDrawFilter drawFilter; |
101 SkBitmapDevice device(SkBitmap::kARGB_8888_Config, 10, 10); | 189 SkBitmapDevice device(SkBitmap::kARGB_8888_Config, 10, 10); |
102 SkCanvas canvas(&device); | 190 SkCanvas canvas(&device); |
103 | 191 |
(...skipping 23 matching lines...) Expand all Loading... |
127 canvas.clipRRect(roundRect, SkRegion::kIntersect_Op, true); | 215 canvas.clipRRect(roundRect, SkRegion::kIntersect_Op, true); |
128 | 216 |
129 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); | 217 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
130 REPORTER_ASSERT(reporter, !state); | 218 REPORTER_ASSERT(reporter, !state); |
131 } | 219 } |
132 | 220 |
133 //////////////////////////////////////////////////////////////////////////////// | 221 //////////////////////////////////////////////////////////////////////////////// |
134 | 222 |
135 static void test_canvas_state_utils(skiatest::Reporter* reporter) { | 223 static void test_canvas_state_utils(skiatest::Reporter* reporter) { |
136 test_complex_layers(reporter); | 224 test_complex_layers(reporter); |
| 225 test_complex_clips(reporter); |
137 test_draw_filters(reporter); | 226 test_draw_filters(reporter); |
138 test_soft_clips(reporter); | 227 test_soft_clips(reporter); |
139 } | 228 } |
140 | 229 |
141 #include "TestClassDef.h" | 230 #include "TestClassDef.h" |
142 DEFINE_TESTCLASS("CanvasState", TestCanvasStateClass, test_canvas_state_utils) | 231 DEFINE_TESTCLASS("CanvasState", TestCanvasStateClass, test_canvas_state_utils) |
OLD | NEW |