Flutter Engine
The Flutter Engine
pathopsinverse.cpp
Go to the documentation of this file.
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 "gm/gm.h"
13#include "include/core/SkPath.h"
14#include "include/core/SkRect.h"
16#include "include/core/SkSize.h"
19#include "tools/ToolUtils.h"
20
21namespace skiagm {
22
23class PathOpsInverseGM : public GM {
24public:
26 }
27
28protected:
29 void onOnceBeforeDraw() override {
30 const unsigned oneColor = ToolUtils::color_to_565(0xFF8080FF);
31 const unsigned twoColor = 0x807F1f1f;
32 SkColor blendColor = blend(oneColor, twoColor);
33 makePaint(&fOnePaint, oneColor);
34 makePaint(&fTwoPaint, twoColor);
35 makePaint(&fOpPaint[kDifference_SkPathOp], oneColor);
36 makePaint(&fOpPaint[kIntersect_SkPathOp], blendColor);
37 makePaint(&fOpPaint[kUnion_SkPathOp], ToolUtils::color_to_565(0xFFc0FFc0));
38 makePaint(&fOpPaint[kReverseDifference_SkPathOp], twoColor);
39 makePaint(&fOpPaint[kXOR_SkPathOp], ToolUtils::color_to_565(0xFFa0FFe0));
40 makePaint(&fOutlinePaint, 0xFF000000);
41 fOutlinePaint.setStyle(SkPaint::kStroke_Style);
42 }
43
45 SkBitmap temp;
46 temp.allocN32Pixels(1, 1);
47 SkCanvas canvas(temp);
48 canvas.drawColor(one);
49 canvas.drawColor(two);
50 void* pixels = temp.getPixels();
51 return *(SkColor*) pixels;
52 }
53
55 paint->setAntiAlias(true);
56 paint->setStyle(SkPaint::kFill_Style);
57 paint->setColor(color);
58 }
59
60 SkString getName() const override { return SkString("pathopsinverse"); }
61
62 SkISize getISize() override { return SkISize::Make(1200, 900); }
63
64 void onDraw(SkCanvas* canvas) override {
65 SkPath one, two;
66 int yPos = 0;
67 for (int oneFill = 0; oneFill <= 1; ++oneFill) {
70 for (int twoFill = 0; twoFill <= 1; ++twoFill) {
73 one.reset();
74 one.setFillType(oneF);
75 one.addRect(10, 10, 70, 70);
76 two.reset();
77 two.setFillType(twoF);
78 two.addRect(40, 40, 100, 100);
79 canvas->save();
80 canvas->translate(0, SkIntToScalar(yPos));
81 canvas->clipRect(SkRect::MakeWH(110, 110), true);
82 canvas->drawPath(one, fOnePaint);
83 canvas->drawPath(one, fOutlinePaint);
84 canvas->drawPath(two, fTwoPaint);
85 canvas->drawPath(two, fOutlinePaint);
86 canvas->restore();
87 int xPos = 150;
88 for (int op = kDifference_SkPathOp; op <= kReverseDifference_SkPathOp; ++op) {
90 Op(one, two, (SkPathOp) op, &result);
91 canvas->save();
92 canvas->translate(SkIntToScalar(xPos), SkIntToScalar(yPos));
93 canvas->clipRect(SkRect::MakeWH(110, 110), true);
94 canvas->drawPath(result, fOpPaint[op]);
95 canvas->drawPath(result, fOutlinePaint);
96 canvas->restore();
97 xPos += 150;
98 }
99 yPos += 150;
100 }
101 }
102 }
103
104private:
105 SkPaint fOnePaint;
106 SkPaint fTwoPaint;
107 SkPaint fOutlinePaint;
109 using INHERITED = GM;
110};
111
112//////////////////////////////////////////////////////////////////////////////
113
114DEF_GM( return new PathOpsInverseGM; )
115
116} // namespace skiagm
117
119
120DEF_SIMPLE_GM(pathops_skbug_10155, canvas, 256, 256) {
121 const char* svgStr[] = {
122 "M474.889 27.0952C474.889 27.1002 474.888 27.1018 474.889 27.1004L479.872 27.5019C479.883 27.3656 479.889 27.2299 479.889 27.0952L474.889 27.0952L474.889 27.0952Z",
123 "M474.94 26.9405C474.93 26.9482 474.917 26.9576 474.901 26.9683L477.689 31.1186C477.789 31.0512 477.888 30.9804 477.985 30.9059L474.94 26.9405L474.94 26.9405Z"
124 };
125
126 SkPath path[2], resultPath;
128
129 for (int i = 0; i < 2; i++)
130 {
133 }
134
135 builder.resolve(&resultPath);
136
137 auto r = path[0].getBounds();
138 canvas->translate(30, 30);
139 canvas->scale(200 / r.width(), 200 / r.width());
140 canvas->translate(-r.fLeft, -r.fTop);
141
143 paint.setColor(SK_ColorRED);
144 paint.setAntiAlias(true);
146 paint.setStrokeWidth(0);
147
148 canvas->drawPath(path[0], paint);
149 canvas->drawPath(path[1], paint);
150
151 // The blue draw should (nearly) overdraw all of the red (except where the two paths intersect)
152 paint.setColor(SK_ColorBLUE);
153 canvas->drawPath(resultPath, paint);
154}
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
SkPathOp
Definition: SkPathOps.h:22
@ kReverseDifference_SkPathOp
subtract the first path from the op path
Definition: SkPathOps.h:27
@ kDifference_SkPathOp
subtract the op path from the first path
Definition: SkPathOps.h:23
@ kIntersect_SkPathOp
intersect the two paths
Definition: SkPathOps.h:24
@ kUnion_SkPathOp
union (inclusive-or) the two paths
Definition: SkPathOps.h:25
@ kXOR_SkPathOp
exclusive-or the two paths
Definition: SkPathOps.h:26
SkPathFillType
Definition: SkPathTypes.h:11
#define SkIntToScalar(x)
Definition: SkScalar.h:57
void * getPixels() const
Definition: SkBitmap.h:283
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition: SkBitmap.cpp:232
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition: SkCanvas.h:1182
int save()
Definition: SkCanvas.cpp:447
void drawPath(const SkPath &path, const SkPaint &paint)
Definition: SkCanvas.cpp:1747
void setStyle(Style style)
Definition: SkPaint.cpp:105
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
@ kFill_Style
set to fill geometry
Definition: SkPaint.h:193
static bool FromSVGString(const char str[], SkPath *)
Definition: SkPath.h:59
void setFillType(SkPathFillType ft)
Definition: SkPath.h:235
SkPath & reset()
Definition: SkPath.cpp:370
SkPath & addRect(const SkRect &rect, SkPathDirection dir, unsigned start)
Definition: SkPath.cpp:864
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
SkString getName() const override
void makePaint(SkPaint *paint, SkColor color)
void onOnceBeforeDraw() override
SkColor blend(SkColor one, SkColor two)
SkISize getISize() override
void onDraw(SkCanvas *canvas) override
const Paint & paint
Definition: color_source.cc:38
DlColor color
GAsyncResult * result
SkColor color_to_565(SkColor color)
Definition: ToolUtils.cpp:139
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
DEF_SIMPLE_GM(pathops_skbug_10155, canvas, 256, 256)
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609