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