• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "include/core/SkBitmap.h"
9 #include "include/core/SkCanvas.h"
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"
17 #include "include/core/SkSurface.h"
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"
24 #include "tests/Test.h"
25 #include "tools/flags/CommandLineFlags.h"
26 
27 #include <cstring>
28 
29 class SkCanvasState;
30 
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)
42 
43 static DEFINE_string(library, "",
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");
49 
50 #include "src/ports/SkOSLibrary.h"
51 
52 // Automatically loads library passed to --library flag and closes it when it goes out of scope.
53 class OpenLibResult {
54 public:
OpenLibResult(skiatest::Reporter * reporter)55     OpenLibResult(skiatest::Reporter* reporter) {
56         if (FLAGS_library.count() == 1) {
57             fLibrary = SkLoadDynamicLibrary(FLAGS_library[0]);
58             REPORTER_ASSERT(reporter, fLibrary != nullptr, "Failed to open library!");
59         } else {
60             fLibrary = nullptr;
61         }
62     }
63 
~OpenLibResult()64     ~OpenLibResult() {
65         if (fLibrary) {
66             SkFreeDynamicLibrary(fLibrary);
67         }
68     }
69 
70     // Load a function address from the library object, or null if the library had failed
procAddress(const char * funcName)71     void* procAddress(const char* funcName) {
72         if (fLibrary) {
73             return SkGetProcedureAddress(fLibrary, funcName);
74         }
75         return nullptr;
76     }
77 
78 private:
79     void* fLibrary;
80 };
81 
82 #endif
83 
write_image(const SkImage * img,const char path[])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 
compare(skiatest::Reporter * reporter,SkImage * img0,SkImage * img1)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());
109     if (memcmp(pm[0].addr(0, 0), pm[1].addr(0, 0), pm[0].computeByteSize()) != 0) {
110         REPORTER_ASSERT(reporter, false);
111     }
112 }
113 
DEF_TEST(CanvasState_test_complex_layers,reporter)114 DEF_TEST(CanvasState_test_complex_layers, reporter) {
115     const int WIDTH = 400;
116     const int HEIGHT = 400;
117     const int SPACER = 10;
118 
119     SkRect rect = SkRect::MakeXYWH(SkIntToScalar(SPACER), SkIntToScalar(SPACER),
120                                    SkIntToScalar(WIDTH-(2*SPACER)),
121                                    SkIntToScalar((HEIGHT-(2*SPACER)) / 7));
122 
123     const SkColorType colorTypes[] = {
124         kRGB_565_SkColorType, kN32_SkColorType
125     };
126 
127     const int layerAlpha[] = { 255, 255, 0 };
128 
129     bool (*drawFn)(SkCanvasState* state, float l, float t,
130                    float r, float b, int32_t s);
131 
132 #if defined(SK_TEST_CANVAS_STATE_CROSS_LIBRARY)
133     OpenLibResult openLibResult(reporter);
134     *(void**) (&drawFn) = openLibResult.procAddress("complex_layers_draw_from_canvas_state");
135 #else
136     drawFn = complex_layers_draw_from_canvas_state;
137 #endif
138 
139     REPORTER_ASSERT(reporter, drawFn);
140     if (!drawFn) {
141         return;
142     }
143 
144     for (size_t i = 0; i < SK_ARRAY_COUNT(colorTypes); ++i) {
145         sk_sp<SkImage> images[2];
146         for (int j = 0; j < 2; ++j) {
147             auto surf = SkSurface::MakeRaster(SkImageInfo::Make(WIDTH, HEIGHT,
148                                                                 colorTypes[i],
149                                                                 kPremul_SkAlphaType));
150             SkCanvas* canvas = surf->getCanvas();
151 
152             canvas->drawColor(SK_ColorRED);
153 
154             for (size_t k = 0; k < SK_ARRAY_COUNT(layerAlpha); ++k) {
155                 SkTLazy<SkPaint> paint;
156                 if (layerAlpha[k] != 0xFF) {
157                     paint.init()->setAlpha(layerAlpha[k]);
158                 }
159 
160                 // draw a rect within the layer's bounds and again outside the layer's bounds
161                 canvas->saveLayer(SkCanvas::SaveLayerRec(&rect, paint.getMaybeNull()));
162 
163                 if (j) {
164                     // Capture from the first Skia.
165                     SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(canvas);
166                     REPORTER_ASSERT(reporter, state);
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);
175                 } else {
176                     // Draw in the first Skia.
177                     complex_layers_draw(canvas, rect.fLeft, rect.fTop,
178                                         rect.fRight, rect.fBottom, SPACER);
179                 }
180 
181                 canvas->restore();
182 
183                 // translate the canvas for the next iteration
184                 canvas->translate(0, 2*(rect.height() + SPACER));
185             }
186             images[j] = surf->makeImageSnapshot();
187         }
188 
189         compare(reporter, images[0].get(), images[1].get());
190     }
191 }
192 
193 ////////////////////////////////////////////////////////////////////////////////
194 
DEF_TEST(CanvasState_test_complex_clips,reporter)195 DEF_TEST(CanvasState_test_complex_clips, reporter) {
196     const int WIDTH = 400;
197     const int HEIGHT = 400;
198     const int SPACER = 10;
199 
200     SkIRect layerRect = SkIRect::MakeWH(WIDTH, HEIGHT / 4);
201     layerRect.inset(2*SPACER, 2*SPACER);
202 
203     SkIRect clipRect = layerRect;
204     clipRect.fRight = clipRect.fLeft + (clipRect.width() / 2) - (2*SPACER);
205     clipRect.outset(SPACER, SPACER);
206 
207     SkIRect regionBounds = clipRect;
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,
220                                      SkRegion::kDifference_Op,
221     };
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 
227 #if defined(SK_TEST_CANVAS_STATE_CROSS_LIBRARY)
228     OpenLibResult openLibResult(reporter);
229     *(void**) (&drawFn) = openLibResult.procAddress("complex_clips_draw_from_canvas_state");
230 #else
231     drawFn = complex_clips_draw_from_canvas_state;
232 #endif
233 
234     REPORTER_ASSERT(reporter, drawFn);
235     if (!drawFn) {
236         return;
237     }
238 
239     sk_sp<SkImage> images[2];
240     for (int i = 0; i < 2; ++i) {
241         auto surf = SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(WIDTH, HEIGHT));
242         SkCanvas* canvas = surf->getCanvas();
243 
244         canvas->drawColor(SK_ColorRED);
245 
246         SkRegion localRegion = clipRegion;
247 
248         SkPaint paint;
249         paint.setAlpha(128);
250         for (size_t j = 0; j < SK_ARRAY_COUNT(clipOps); ++j) {
251             SkRect layerBounds = SkRect::Make(layerRect);
252             canvas->saveLayer(SkCanvas::SaveLayerRec(&layerBounds, &paint));
253 
254             if (i) {
255                 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(canvas);
256                 REPORTER_ASSERT(reporter, state);
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);
273             } else {
274                 complex_clips_draw(canvas, clipRect.fLeft, clipRect.fTop,
275                                    clipRect.fRight, clipRect.fBottom, clipOps[j],
276                                    localRegion);
277             }
278 
279             canvas->restore();
280 
281             // translate the canvas and region for the next iteration
282             canvas->translate(0, SkIntToScalar(2*(layerRect.height() + (SPACER))));
283             localRegion.translate(0, 2*(layerRect.height() + SPACER));
284         }
285         images[i] = surf->makeImageSnapshot();
286     }
287 
288     compare(reporter, images[0].get(), images[1].get());
289 }
290 
291 ////////////////////////////////////////////////////////////////////////////////
292 
DEF_TEST(CanvasState_test_soft_clips,reporter)293 DEF_TEST(CanvasState_test_soft_clips, reporter) {
294     SkBitmap bitmap;
295     bitmap.allocN32Pixels(10, 10);
296     SkCanvas canvas(bitmap);
297 
298     SkRRect roundRect;
299     roundRect.setOval(SkRect::MakeWH(5, 5));
300 
301     canvas.clipRRect(roundRect, kIntersect_SkClipOp, true);
302 
303     SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
304     REPORTER_ASSERT(reporter, !state);
305 }
306 
DEF_TEST(CanvasState_test_saveLayer_clip,reporter)307 DEF_TEST(CanvasState_test_saveLayer_clip, reporter) {
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 
320     // Check that saveLayer sets the clip stack to the layer bounds.
321     canvas.saveLayer(&bounds, nullptr);
322     SkIRect devClip = canvas.getDeviceClipBounds();
323     REPORTER_ASSERT(reporter, canvas.isClipRect());
324     REPORTER_ASSERT(reporter, devClip.width() == LAYER_WIDTH);
325     REPORTER_ASSERT(reporter, devClip.height() == LAYER_HEIGHT);
326     canvas.restore();
327 }
328