OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "CanvasStateHelpers.h" | |
8 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
9 #include "SkCanvasStateUtils.h" | 10 #include "SkCanvasStateUtils.h" |
11 #include "SkCommandLineFlags.h" | |
10 #include "SkDrawFilter.h" | 12 #include "SkDrawFilter.h" |
11 #include "SkError.h" | 13 #include "SkError.h" |
12 #include "SkPaint.h" | 14 #include "SkPaint.h" |
13 #include "SkRRect.h" | 15 #include "SkRRect.h" |
14 #include "SkRect.h" | 16 #include "SkRect.h" |
17 #include "SkTemplates.h" | |
15 #include "Test.h" | 18 #include "Test.h" |
16 | 19 |
20 #include <dlfcn.h> | |
21 | |
22 DEFINE_string(library, "", "Support library to use for CanvasState test. If empt y (the default), " | |
23 "the test will be run without crossing a library boun dary. Otherwise, " | |
24 "it is expected to be a full path to a shared library file, which will" | |
25 " be dynamically loaded. Functions from the library w ill be called to " | |
26 "test SkCanvasState."); | |
27 | |
28 | |
17 static void test_complex_layers(skiatest::Reporter* reporter) { | 29 static void test_complex_layers(skiatest::Reporter* reporter) { |
18 #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG | 30 #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
19 const int WIDTH = 400; | 31 const int WIDTH = 400; |
20 const int HEIGHT = 400; | 32 const int HEIGHT = 400; |
21 const int SPACER = 10; | 33 const int SPACER = 10; |
22 | 34 |
23 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(SPACER), SkIntToScalar(SPACER), | 35 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(SPACER), SkIntToScalar(SPACER), |
24 SkIntToScalar(WIDTH-(2*SPACER)), | 36 SkIntToScalar(WIDTH-(2*SPACER)), |
25 SkIntToScalar((HEIGHT-(2*SPACER)) / 7)); | 37 SkIntToScalar((HEIGHT-(2*SPACER)) / 7)); |
26 | 38 |
27 const SkColorType colorTypes[] = { | 39 const SkColorType colorTypes[] = { |
28 kRGB_565_SkColorType, kN32_SkColorType | 40 kRGB_565_SkColorType, kN32_SkColorType |
29 }; | 41 }; |
30 | 42 |
31 const int layerAlpha[] = { 255, 255, 0 }; | 43 const int layerAlpha[] = { 255, 255, 0 }; |
32 const SkCanvas::SaveFlags flags[] = { SkCanvas::kARGB_NoClipLayer_SaveFlag, | 44 const SkCanvas::SaveFlags flags[] = { SkCanvas::kARGB_NoClipLayer_SaveFlag, |
33 SkCanvas::kARGB_ClipLayer_SaveFlag, | 45 SkCanvas::kARGB_ClipLayer_SaveFlag, |
34 SkCanvas::kARGB_NoClipLayer_SaveFlag | 46 SkCanvas::kARGB_NoClipLayer_SaveFlag |
35 }; | 47 }; |
36 REPORTER_ASSERT(reporter, sizeof(layerAlpha) == sizeof(flags)); | 48 REPORTER_ASSERT(reporter, sizeof(layerAlpha) == sizeof(flags)); |
37 | 49 |
50 bool (*drawFn)(SkCanvasState* state, float l, float t, | |
51 float r, float b, int32_t s); | |
52 void* handle; | |
53 if (FLAGS_library.count() == 1) { | |
54 handle = dlopen(FLAGS_library[0], RTLD_LAZY | RTLD_LOCAL); | |
55 REPORTER_ASSERT(reporter, handle); | |
56 if (!handle) { | |
57 return; | |
58 } | |
59 *(void**) (&drawFn) = dlsym(handle, "complex_layers_draw_from_canvas_sta te"); | |
60 } else { | |
61 drawFn = complex_layers_draw_from_canvas_state; | |
62 handle = NULL; | |
63 } | |
64 | |
65 SkAutoTCallIProc<void, dlclose> autoClose(handle); | |
66 REPORTER_ASSERT(reporter, drawFn); | |
67 if (!drawFn) { | |
68 return; | |
69 } | |
70 | |
38 for (size_t i = 0; i < SK_ARRAY_COUNT(colorTypes); ++i) { | 71 for (size_t i = 0; i < SK_ARRAY_COUNT(colorTypes); ++i) { |
39 SkBitmap bitmaps[2]; | 72 SkBitmap bitmaps[2]; |
40 for (int j = 0; j < 2; ++j) { | 73 for (int j = 0; j < 2; ++j) { |
41 bitmaps[j].allocPixels(SkImageInfo::Make(WIDTH, HEIGHT, | 74 bitmaps[j].allocPixels(SkImageInfo::Make(WIDTH, HEIGHT, |
42 colorTypes[i], | 75 colorTypes[i], |
43 kPremul_SkAlphaType)); | 76 kPremul_SkAlphaType)); |
44 | 77 |
45 SkCanvas canvas(bitmaps[j]); | 78 SkCanvas canvas(bitmaps[j]); |
46 | 79 |
47 canvas.drawColor(SK_ColorRED); | 80 canvas.drawColor(SK_ColorRED); |
48 | 81 |
49 for (size_t k = 0; k < SK_ARRAY_COUNT(layerAlpha); ++k) { | 82 for (size_t k = 0; k < SK_ARRAY_COUNT(layerAlpha); ++k) { |
50 // draw a rect within the layer's bounds and again outside the l ayer's bounds | 83 // draw a rect within the layer's bounds and again outside the l ayer's bounds |
51 canvas.saveLayerAlpha(&rect, layerAlpha[k], flags[k]); | 84 canvas.saveLayerAlpha(&rect, layerAlpha[k], flags[k]); |
52 | 85 |
53 SkCanvasState* state = NULL; | |
54 SkCanvas* tmpCanvas = NULL; | |
55 if (j) { | 86 if (j) { |
56 state = SkCanvasStateUtils::CaptureCanvasState(&canvas); | 87 // Capture from the first Skia. |
88 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasStat e(&canvas); | |
57 REPORTER_ASSERT(reporter, state); | 89 REPORTER_ASSERT(reporter, state); |
58 tmpCanvas = SkCanvasStateUtils::CreateFromCanvasState(state) ; | 90 |
59 REPORTER_ASSERT(reporter, tmpCanvas); | 91 // And draw to it in the second Skia. |
92 bool success = complex_layers_draw_from_canvas_state(state, | |
93 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, SP ACER); | |
94 REPORTER_ASSERT(reporter, success); | |
95 | |
96 // And release it in the *first* Skia. | |
97 SkCanvasStateUtils::ReleaseCanvasState(state); | |
60 } else { | 98 } else { |
61 tmpCanvas = SkRef(&canvas); | 99 // Draw in the first Skia. |
100 complex_layers_draw(&canvas, rect.fLeft, rect.fTop, | |
101 rect.fRight, rect.fBottom, SPACER); | |
62 } | 102 } |
63 | 103 |
64 SkPaint bluePaint; | |
65 bluePaint.setColor(SK_ColorBLUE); | |
66 bluePaint.setStyle(SkPaint::kFill_Style); | |
67 | |
68 tmpCanvas->drawRect(rect, bluePaint); | |
69 tmpCanvas->translate(0, rect.height() + SPACER); | |
70 tmpCanvas->drawRect(rect, bluePaint); | |
71 | |
72 tmpCanvas->unref(); | |
73 SkCanvasStateUtils::ReleaseCanvasState(state); | |
74 | |
75 canvas.restore(); | 104 canvas.restore(); |
76 | 105 |
77 // translate the canvas for the next iteration | 106 // translate the canvas for the next iteration |
78 canvas.translate(0, 2*(rect.height() + SPACER)); | 107 canvas.translate(0, 2*(rect.height() + SPACER)); |
79 } | 108 } |
80 } | 109 } |
81 | 110 |
82 // now we memcmp the two bitmaps | 111 // now we memcmp the two bitmaps |
83 REPORTER_ASSERT(reporter, bitmaps[0].getSize() == bitmaps[1].getSize()); | 112 REPORTER_ASSERT(reporter, bitmaps[0].getSize() == bitmaps[1].getSize()); |
84 REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(), | 113 REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(), |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
117 const SkRegion::Op clipOps[] = { SkRegion::kIntersect_Op, | 146 const SkRegion::Op clipOps[] = { SkRegion::kIntersect_Op, |
118 SkRegion::kIntersect_Op, | 147 SkRegion::kIntersect_Op, |
119 SkRegion::kReplace_Op, | 148 SkRegion::kReplace_Op, |
120 }; | 149 }; |
121 const SkCanvas::SaveFlags flags[] = { SkCanvas::kARGB_NoClipLayer_SaveFlag, | 150 const SkCanvas::SaveFlags flags[] = { SkCanvas::kARGB_NoClipLayer_SaveFlag, |
122 SkCanvas::kARGB_ClipLayer_SaveFlag, | 151 SkCanvas::kARGB_ClipLayer_SaveFlag, |
123 SkCanvas::kARGB_NoClipLayer_SaveFlag, | 152 SkCanvas::kARGB_NoClipLayer_SaveFlag, |
124 }; | 153 }; |
125 REPORTER_ASSERT(reporter, sizeof(clipOps) == sizeof(flags)); | 154 REPORTER_ASSERT(reporter, sizeof(clipOps) == sizeof(flags)); |
126 | 155 |
156 // Will using an SkRegion work at all? | |
157 bool (*drawFn)(SkCanvasState* state, int32_t l, int32_t t, | |
158 int32_t r, int32_t b, int32_t clipOp, const SkRegion*); | |
159 void* handle; | |
djsollen
2014/07/18 14:11:54
move this duplicate setup code into a function if
scroggo
2014/07/18 19:31:02
I've moved the call to dlopen into its own functio
| |
160 if (FLAGS_library.count() == 1) { | |
161 handle = dlopen(FLAGS_library[0], RTLD_LAZY | RTLD_LOCAL); | |
162 REPORTER_ASSERT(reporter, handle); | |
163 if (!handle) { | |
164 return; | |
165 } | |
166 *(void**) (&drawFn) = dlsym(handle, "test_complex_clips_draw_from_canvas _state"); | |
167 } else { | |
168 drawFn = test_complex_clips_draw_from_canvas_state; | |
169 handle = NULL; | |
170 } | |
171 | |
172 SkAutoTCallIProc<void, dlclose> autoClose(handle); | |
173 REPORTER_ASSERT(reporter, drawFn); | |
174 if (!drawFn) { | |
175 return; | |
176 } | |
177 | |
127 SkBitmap bitmaps[2]; | 178 SkBitmap bitmaps[2]; |
128 for (int i = 0; i < 2; ++i) { | 179 for (int i = 0; i < 2; ++i) { |
129 bitmaps[i].allocN32Pixels(WIDTH, HEIGHT); | 180 bitmaps[i].allocN32Pixels(WIDTH, HEIGHT); |
130 | 181 |
131 SkCanvas canvas(bitmaps[i]); | 182 SkCanvas canvas(bitmaps[i]); |
132 | 183 |
133 canvas.drawColor(SK_ColorRED); | 184 canvas.drawColor(SK_ColorRED); |
134 | 185 |
135 SkRegion localRegion = clipRegion; | 186 SkRegion localRegion = clipRegion; |
136 | 187 |
137 for (size_t j = 0; j < SK_ARRAY_COUNT(flags); ++j) { | 188 for (size_t j = 0; j < SK_ARRAY_COUNT(flags); ++j) { |
138 SkRect layerBounds = SkRect::Make(layerRect); | 189 SkRect layerBounds = SkRect::Make(layerRect); |
139 canvas.saveLayerAlpha(&layerBounds, 128, flags[j]); | 190 canvas.saveLayerAlpha(&layerBounds, 128, flags[j]); |
140 | 191 |
141 SkCanvasState* state = NULL; | |
142 SkCanvas* tmpCanvas = NULL; | |
143 if (i) { | 192 if (i) { |
144 state = SkCanvasStateUtils::CaptureCanvasState(&canvas); | 193 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&c anvas); |
145 REPORTER_ASSERT(reporter, state); | 194 REPORTER_ASSERT(reporter, state); |
146 tmpCanvas = SkCanvasStateUtils::CreateFromCanvasState(state); | 195 |
147 REPORTER_ASSERT(reporter, tmpCanvas); | 196 bool success = drawFn(state, clipRect.fLeft, clipRect.fTop, |
197 clipRect.fRight, clipRect.fBottom, clipOps [j], | |
198 &localRegion); | |
scroggo
2014/07/17 21:32:37
Note that I'm passing a pointer to an SkRegion acr
scroggo
2014/07/18 19:31:02
Done. Thanks!
| |
199 REPORTER_ASSERT(reporter, success); | |
200 | |
201 SkCanvasStateUtils::ReleaseCanvasState(state); | |
148 } else { | 202 } else { |
149 tmpCanvas = SkRef(&canvas); | 203 test_complex_clips_draw(&canvas, clipRect.fLeft, clipRect.fTop, |
204 clipRect.fRight, clipRect.fBottom, clipO ps[j], | |
205 localRegion); | |
150 } | 206 } |
151 | 207 |
152 tmpCanvas->save(); | |
153 tmpCanvas->clipRect(SkRect::Make(clipRect), clipOps[j]); | |
154 tmpCanvas->drawColor(SK_ColorBLUE); | |
155 tmpCanvas->restore(); | |
156 | |
157 tmpCanvas->clipRegion(localRegion, clipOps[j]); | |
158 tmpCanvas->drawColor(SK_ColorBLUE); | |
159 | |
160 tmpCanvas->unref(); | |
161 SkCanvasStateUtils::ReleaseCanvasState(state); | |
162 | |
163 canvas.restore(); | 208 canvas.restore(); |
164 | 209 |
165 // translate the canvas and region for the next iteration | 210 // translate the canvas and region for the next iteration |
166 canvas.translate(0, SkIntToScalar(2*(layerRect.height() + (SPACER))) ); | 211 canvas.translate(0, SkIntToScalar(2*(layerRect.height() + (SPACER))) ); |
167 localRegion.translate(0, 2*(layerRect.height() + SPACER)); | 212 localRegion.translate(0, 2*(layerRect.height() + SPACER)); |
168 } | 213 } |
169 } | 214 } |
170 | 215 |
171 // now we memcmp the two bitmaps | 216 // now we memcmp the two bitmaps |
172 REPORTER_ASSERT(reporter, bitmaps[0].getSize() == bitmaps[1].getSize()); | 217 REPORTER_ASSERT(reporter, bitmaps[0].getSize() == bitmaps[1].getSize()); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
262 #endif | 307 #endif |
263 } | 308 } |
264 | 309 |
265 DEF_TEST(CanvasState, reporter) { | 310 DEF_TEST(CanvasState, reporter) { |
266 test_complex_layers(reporter); | 311 test_complex_layers(reporter); |
267 test_complex_clips(reporter); | 312 test_complex_clips(reporter); |
268 test_draw_filters(reporter); | 313 test_draw_filters(reporter); |
269 test_soft_clips(reporter); | 314 test_soft_clips(reporter); |
270 test_saveLayer_clip(reporter); | 315 test_saveLayer_clip(reporter); |
271 } | 316 } |
OLD | NEW |