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