• 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/SkTLazy.h"
23 #include "tests/Test.h"
24 #include "tools/flags/CommandLineFlags.h"
25 
26 #include <cstring>
27 
28 class SkCanvasState;
29 
30 // Uncomment to include tests of CanvasState across a library boundary. This will change how 'dm'
31 // is built so that the functions defined in CanvasStateHelpers do not exist inside 'dm', and are
32 // instead compiled as part of the 'canvas_state_lib' build target. This produces a shared library
33 // that must be passed to 'dm' using the --library flag when running.
34 // #define SK_TEST_CANVAS_STATE_CROSS_LIBRARY
35 
36 // Must be included after SK_TEST_CANVAS_STATE_CROSS_LIBRARY is defined
37 #include "tests/CanvasStateHelpers.h"
38 
39 // dlopen, the library flag and canvas state helpers are only used for tests which require this flag
40 #if defined(SK_TEST_CANVAS_STATE_CROSS_LIBRARY)
41 
42 static DEFINE_string(library, "",
43                      "Support library to use for CanvasState test. Must be provided when"
44                      " SK_TEST_CANVAS_STATE_CROSS_LIBRARY to specify the dynamically loaded library"
45                      " that receives the captured canvas state. Functions from the library will be"
46                      " called to test SkCanvasState. The library is built from the canvas_state_lib"
47                      " target");
48 
49 #include "src/ports/SkOSLibrary.h"
50 
51 // Automatically loads library passed to --library flag and closes it when it goes out of scope.
52 class OpenLibResult {
53 public:
OpenLibResult(skiatest::Reporter * reporter)54     OpenLibResult(skiatest::Reporter* reporter) {
55         if (FLAGS_library.count() == 1) {
56             fLibrary = SkLoadDynamicLibrary(FLAGS_library[0]);
57             REPORTER_ASSERT(reporter, fLibrary != nullptr, "Failed to open library!");
58         } else {
59             fLibrary = nullptr;
60         }
61     }
62 
~OpenLibResult()63     ~OpenLibResult() {
64         if (fLibrary) {
65             SkFreeDynamicLibrary(fLibrary);
66         }
67     }
68 
69     // Load a function address from the library object, or null if the library had failed
procAddress(const char * funcName)70     void* procAddress(const char* funcName) {
71         if (fLibrary) {
72             return SkGetProcedureAddress(fLibrary, funcName);
73         }
74         return nullptr;
75     }
76 
77 private:
78     void* fLibrary;
79 };
80 
81 #endif
82 
write_image(const SkImage * img,const char path[])83 static void write_image(const SkImage* img, const char path[]) {
84     auto data = img->encodeToData();
85     SkFILEWStream(path).write(data->data(), data->size());
86 }
87 
compare(skiatest::Reporter * reporter,SkImage * img0,SkImage * img1)88 static void compare(skiatest::Reporter* reporter, SkImage* img0, SkImage* img1) {
89     if (false) {
90         static int counter;
91 
92         SkDebugf("---- counter %d\n", counter);
93         SkString name;
94         name.printf("no_capture_%d.png", counter);
95         write_image(img0, name.c_str());
96         name.printf("capture_%d.png", counter);
97         write_image(img1, name.c_str());
98         counter++;
99     }
100 
101     SkPixmap pm[2];
102     REPORTER_ASSERT(reporter, img0->peekPixels(&pm[0]));
103     REPORTER_ASSERT(reporter, img1->peekPixels(&pm[1]));
104     // now we memcmp the two bitmaps
105     REPORTER_ASSERT(reporter, pm[0].computeByteSize() == pm[1].computeByteSize());
106     REPORTER_ASSERT(reporter, pm[0].rowBytes() == (size_t)pm[0].width() * pm[0].info().bytesPerPixel());
107     REPORTER_ASSERT(reporter, pm[1].rowBytes() == (size_t)pm[1].width() * pm[1].info().bytesPerPixel());
108     if (memcmp(pm[0].addr(0, 0), pm[1].addr(0, 0), pm[0].computeByteSize()) != 0) {
109         REPORTER_ASSERT(reporter, false);
110     }
111 }
112 
DEF_TEST(CanvasState_test_complex_layers,reporter)113 DEF_TEST(CanvasState_test_complex_layers, reporter) {
114     const int WIDTH = 400;
115     const int HEIGHT = 400;
116     const int SPACER = 10;
117 
118     SkRect rect = SkRect::MakeXYWH(SkIntToScalar(SPACER), SkIntToScalar(SPACER),
119                                    SkIntToScalar(WIDTH-(2*SPACER)),
120                                    SkIntToScalar((HEIGHT-(2*SPACER)) / 7));
121 
122     const SkColorType colorTypes[] = {
123         kRGB_565_SkColorType, kN32_SkColorType
124     };
125 
126     const int layerAlpha[] = { 255, 255, 0 };
127 
128     bool (*drawFn)(SkCanvasState* state, float l, float t,
129                    float r, float b, int32_t s);
130 
131 #if defined(SK_TEST_CANVAS_STATE_CROSS_LIBRARY)
132     OpenLibResult openLibResult(reporter);
133     *(void**) (&drawFn) = openLibResult.procAddress("complex_layers_draw_from_canvas_state");
134 #else
135     drawFn = complex_layers_draw_from_canvas_state;
136 #endif
137 
138     REPORTER_ASSERT(reporter, drawFn);
139     if (!drawFn) {
140         return;
141     }
142 
143     for (size_t i = 0; i < SK_ARRAY_COUNT(colorTypes); ++i) {
144         sk_sp<SkImage> images[2];
145         for (int j = 0; j < 2; ++j) {
146             auto surf = SkSurface::MakeRaster(SkImageInfo::Make(WIDTH, HEIGHT,
147                                                                 colorTypes[i],
148                                                                 kPremul_SkAlphaType));
149             SkCanvas* canvas = surf->getCanvas();
150 
151             canvas->drawColor(SK_ColorRED);
152 
153             for (size_t k = 0; k < SK_ARRAY_COUNT(layerAlpha); ++k) {
154                 SkTLazy<SkPaint> paint;
155                 if (layerAlpha[k] != 0xFF) {
156                     paint.init()->setAlpha(layerAlpha[k]);
157                 }
158 
159                 // draw a rect within the layer's bounds and again outside the layer's bounds
160                 canvas->saveLayer(SkCanvas::SaveLayerRec(&rect, paint.getMaybeNull()));
161 
162                 if (j) {
163                     // Capture from the first Skia.
164                     SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(canvas);
165                     REPORTER_ASSERT(reporter, state);
166 
167                     // And draw to it in the second Skia.
168                     bool success = complex_layers_draw_from_canvas_state(state,
169                             rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, SPACER);
170                     REPORTER_ASSERT(reporter, success);
171 
172                     // And release it in the *first* Skia.
173                     SkCanvasStateUtils::ReleaseCanvasState(state);
174                 } else {
175                     // Draw in the first Skia.
176                     complex_layers_draw(canvas, rect.fLeft, rect.fTop,
177                                         rect.fRight, rect.fBottom, SPACER);
178                 }
179 
180                 canvas->restore();
181 
182                 // translate the canvas for the next iteration
183                 canvas->translate(0, 2*(rect.height() + SPACER));
184             }
185             images[j] = surf->makeImageSnapshot();
186         }
187 
188         compare(reporter, images[0].get(), images[1].get());
189     }
190 }
191 
192 ////////////////////////////////////////////////////////////////////////////////
193 
DEF_TEST(CanvasState_test_complex_clips,reporter)194 DEF_TEST(CanvasState_test_complex_clips, reporter) {
195     const int WIDTH = 400;
196     const int HEIGHT = 400;
197     const int SPACER = 10;
198 
199     SkIRect layerRect = SkIRect::MakeWH(WIDTH, HEIGHT / 4);
200     layerRect.inset(2*SPACER, 2*SPACER);
201 
202     SkIRect clipRect = layerRect;
203     clipRect.fRight = clipRect.fLeft + (clipRect.width() / 2) - (2*SPACER);
204     clipRect.outset(SPACER, SPACER);
205 
206     SkIRect regionBounds = clipRect;
207     regionBounds.offset(clipRect.width() + (2*SPACER), 0);
208 
209     SkIRect regionInterior = regionBounds;
210     regionInterior.inset(SPACER*3, SPACER*3);
211 
212     SkRegion clipRegion;
213     clipRegion.setRect(regionBounds);
214     clipRegion.op(regionInterior, SkRegion::kDifference_Op);
215 
216 
217     const SkRegion::Op clipOps[] = { SkRegion::kIntersect_Op,
218                                      SkRegion::kIntersect_Op,
219                                      SkRegion::kDifference_Op,
220     };
221 
222     bool (*drawFn)(SkCanvasState* state, int32_t l, int32_t t,
223                    int32_t r, int32_t b, int32_t clipOp,
224                    int32_t regionRects, int32_t* rectCoords);
225 
226 #if defined(SK_TEST_CANVAS_STATE_CROSS_LIBRARY)
227     OpenLibResult openLibResult(reporter);
228     *(void**) (&drawFn) = openLibResult.procAddress("complex_clips_draw_from_canvas_state");
229 #else
230     drawFn = complex_clips_draw_from_canvas_state;
231 #endif
232 
233     REPORTER_ASSERT(reporter, drawFn);
234     if (!drawFn) {
235         return;
236     }
237 
238     sk_sp<SkImage> images[2];
239     for (int i = 0; i < 2; ++i) {
240         auto surf = SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(WIDTH, HEIGHT));
241         SkCanvas* canvas = surf->getCanvas();
242 
243         canvas->drawColor(SK_ColorRED);
244 
245         SkRegion localRegion = clipRegion;
246 
247         SkPaint paint;
248         paint.setAlpha(128);
249         for (size_t j = 0; j < SK_ARRAY_COUNT(clipOps); ++j) {
250             SkRect layerBounds = SkRect::Make(layerRect);
251             canvas->saveLayer(SkCanvas::SaveLayerRec(&layerBounds, &paint));
252 
253             if (i) {
254                 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(canvas);
255                 REPORTER_ASSERT(reporter, state);
256 
257                 SkRegion::Iterator iter(localRegion);
258                 SkTDArray<int32_t> rectCoords;
259                 for (; !iter.done(); iter.next()) {
260                     const SkIRect& rect = iter.rect();
261                     *rectCoords.append() = rect.fLeft;
262                     *rectCoords.append() = rect.fTop;
263                     *rectCoords.append() = rect.fRight;
264                     *rectCoords.append() = rect.fBottom;
265                 }
266                 bool success = drawFn(state, clipRect.fLeft, clipRect.fTop,
267                                       clipRect.fRight, clipRect.fBottom, clipOps[j],
268                                       rectCoords.count() / 4, rectCoords.begin());
269                 REPORTER_ASSERT(reporter, success);
270 
271                 SkCanvasStateUtils::ReleaseCanvasState(state);
272             } else {
273                 complex_clips_draw(canvas, clipRect.fLeft, clipRect.fTop,
274                                    clipRect.fRight, clipRect.fBottom, clipOps[j],
275                                    localRegion);
276             }
277 
278             canvas->restore();
279 
280             // translate the canvas and region for the next iteration
281             canvas->translate(0, SkIntToScalar(2*(layerRect.height() + (SPACER))));
282             localRegion.translate(0, 2*(layerRect.height() + SPACER));
283         }
284         images[i] = surf->makeImageSnapshot();
285     }
286 
287     compare(reporter, images[0].get(), images[1].get());
288 }
289 
290 ////////////////////////////////////////////////////////////////////////////////
291 
DEF_TEST(CanvasState_test_soft_clips,reporter)292 DEF_TEST(CanvasState_test_soft_clips, reporter) {
293     SkBitmap bitmap;
294     bitmap.allocN32Pixels(10, 10);
295     SkCanvas canvas(bitmap);
296 
297     SkRRect roundRect;
298     roundRect.setOval(SkRect::MakeWH(5, 5));
299 
300     canvas.clipRRect(roundRect, SkClipOp::kIntersect, true);
301 
302     SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
303     REPORTER_ASSERT(reporter, !state);
304 }
305 
DEF_TEST(CanvasState_test_saveLayer_clip,reporter)306 DEF_TEST(CanvasState_test_saveLayer_clip, reporter) {
307     const int WIDTH = 100;
308     const int HEIGHT = 100;
309     const int LAYER_WIDTH = 50;
310     const int LAYER_HEIGHT = 50;
311 
312     SkBitmap bitmap;
313     bitmap.allocN32Pixels(WIDTH, HEIGHT);
314     SkCanvas canvas(bitmap);
315 
316     SkRect bounds = SkRect::MakeWH(SkIntToScalar(LAYER_WIDTH), SkIntToScalar(LAYER_HEIGHT));
317     canvas.clipRect(SkRect::MakeWH(SkIntToScalar(WIDTH), SkIntToScalar(HEIGHT)));
318 
319     // Check that saveLayer sets the clip stack to the layer bounds.
320     canvas.saveLayer(&bounds, nullptr);
321     SkIRect devClip = canvas.getDeviceClipBounds();
322     REPORTER_ASSERT(reporter, canvas.isClipRect());
323     REPORTER_ASSERT(reporter, devClip.width() == LAYER_WIDTH);
324     REPORTER_ASSERT(reporter, devClip.height() == LAYER_HEIGHT);
325     canvas.restore();
326 }
327