1 /*
2  * Copyright 2014 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/SkTypes.h"
9 
10 #if !defined(SK_TEST_CANVAS_STATE_CROSS_LIBRARY)
11 
12 #include "tests/CanvasStateHelpers.h"
13 
14 #include "include/core/SkCanvas.h"
15 #include "include/core/SkClipOp.h"
16 #include "include/core/SkColor.h"
17 #include "include/core/SkPaint.h"
18 #include "include/core/SkRect.h"
19 #include "include/core/SkRegion.h"
20 #include "include/core/SkScalar.h"
21 #include "include/utils/SkCanvasStateUtils.h"
22 
23 #include <memory>
24 
complex_layers_draw(SkCanvas * canvas,float left,float top,float right,float bottom,int32_t spacer)25 void complex_layers_draw(SkCanvas* canvas, float left, float top,
26                          float right, float bottom, int32_t spacer) {
27     SkPaint bluePaint;
28     bluePaint.setColor(SK_ColorBLUE);
29     bluePaint.setStyle(SkPaint::kFill_Style);
30 
31     SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
32     canvas->drawRect(rect, bluePaint);
33     canvas->translate(0, rect.height() + spacer);
34     canvas->drawRect(rect, bluePaint);
35 }
36 
complex_layers_draw_from_canvas_state(SkCanvasState * state,float left,float top,float right,float bottom,int32_t spacer)37 extern "C" bool complex_layers_draw_from_canvas_state(SkCanvasState* state,
38         float left, float top, float right, float bottom, int32_t spacer) {
39     std::unique_ptr<SkCanvas> canvas = SkCanvasStateUtils::MakeFromCanvasState(state);
40     if (!canvas) {
41         return false;
42     }
43     complex_layers_draw(canvas.get(), left, top, right, bottom, spacer);
44     return true;
45 }
46 
complex_clips_draw(SkCanvas * canvas,int32_t left,int32_t top,int32_t right,int32_t bottom,int32_t clipOp,const SkRegion & localRegion)47 void complex_clips_draw(SkCanvas* canvas, int32_t left, int32_t top,
48         int32_t right, int32_t bottom, int32_t clipOp, const SkRegion& localRegion) {
49     canvas->save();
50     SkRect clipRect = SkRect::MakeLTRB(SkIntToScalar(left), SkIntToScalar(top),
51                                        SkIntToScalar(right), SkIntToScalar(bottom));
52     canvas->clipRect(clipRect, (SkRegion::Op) clipOp);
53     canvas->drawColor(SK_ColorBLUE);
54     canvas->restore();
55 
56     canvas->clipRegion(localRegion, (SkClipOp) clipOp);
57     canvas->drawColor(SK_ColorBLUE);
58 }
59 
complex_clips_draw_from_canvas_state(SkCanvasState * state,int32_t left,int32_t top,int32_t right,int32_t bottom,int32_t clipOp,int32_t regionRects,int32_t * rectCoords)60 extern "C" bool complex_clips_draw_from_canvas_state(SkCanvasState* state,
61         int32_t left, int32_t top, int32_t right, int32_t bottom, int32_t clipOp,
62         int32_t regionRects, int32_t* rectCoords) {
63     std::unique_ptr<SkCanvas> canvas = SkCanvasStateUtils::MakeFromCanvasState(state);
64     if (!canvas) {
65         return false;
66     }
67 
68     SkRegion localRegion;
69     for (int32_t i = 0; i < regionRects; ++i) {
70         localRegion.op({rectCoords[0], rectCoords[1], rectCoords[2], rectCoords[3]},
71                        SkRegion::kUnion_Op);
72         rectCoords += 4;
73     }
74 
75     complex_clips_draw(canvas.get(), left, top, right, bottom, clipOp, localRegion);
76     return true;
77 }
78 
79 #endif // SK_TEST_CANVAS_STATE_CROSS_LIBRARY
80