Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
strokerect.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2012 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"
12#include "include/core/SkPath.h"
15#include "include/core/SkRect.h"
17#include "include/core/SkSize.h"
21
22#include <float.h>
23
24using namespace skia_private;
25
26#define STROKE_WIDTH SkIntToScalar(20)
27
28static void draw_path(SkCanvas* canvas, const SkPath& path, const SkRect& rect,
29 SkPaint::Join join, int doFill) {
31 paint.setAntiAlias(true);
33
34 paint.setColor(SK_ColorGRAY);
35 paint.setStrokeWidth(STROKE_WIDTH);
36 paint.setStrokeJoin(join);
37 canvas->drawRect(rect, paint);
38
40 paint.setStrokeWidth(0);
41 paint.setColor(SK_ColorRED);
42 canvas->drawPath(path, paint);
43
44 paint.setStrokeWidth(3);
45 paint.setStrokeJoin(SkPaint::kMiter_Join);
46 int n = path.countPoints();
48 path.getPoints(points.get(), n);
50}
51
52/*
53 * Test calling SkStroker for rectangles. Cases to cover:
54 *
55 * geometry: normal, small (smaller than stroke-width), empty, inverted
56 * joint-type for the corners
57 */
58class StrokeRectGM : public skiagm::GM {
59public:
61
62protected:
63 SkString getName() const override { return SkString("strokerect"); }
64
65 SkISize getISize() override { return SkISize::Make(1400, 740); }
66
67 void onDraw(SkCanvas* canvas) override {
68 canvas->drawColor(SK_ColorWHITE);
69 canvas->translate(STROKE_WIDTH*3/2, STROKE_WIDTH*3/2);
70
73 paint.setStrokeWidth(STROKE_WIDTH);
74
75 constexpr SkPaint::Join gJoins[] = {
77 };
78
79 constexpr SkScalar W = 80;
80 constexpr SkScalar H = 80;
81 constexpr SkRect gRects[] = {
82 { 0, 0, W, H },
83 { W, 0, 0, H },
84 { 0, H, W, 0 },
85 { 0, 0, STROKE_WIDTH, H },
86 { 0, 0, W, STROKE_WIDTH },
87 { 0, 0, STROKE_WIDTH/2, STROKE_WIDTH/2 },
88 { 0, 0, W, 0 },
89 { 0, 0, 0, H },
90 { 0, 0, 0, 0 },
91 { 0, 0, W, FLT_EPSILON },
92 { 0, 0, FLT_EPSILON, H },
93 { 0, 0, FLT_EPSILON, FLT_EPSILON },
94 };
95
96 for (int doFill = 0; doFill <= 1; ++doFill) {
97 for (size_t i = 0; i < std::size(gJoins); ++i) {
98 SkPaint::Join join = gJoins[i];
99 paint.setStrokeJoin(join);
100
101 SkAutoCanvasRestore acr(canvas, true);
102 for (size_t j = 0; j < std::size(gRects); ++j) {
103 const SkRect& r = gRects[j];
104
105 SkPath path, fillPath;
106 path.addRect(r);
107 skpathutils::FillPathWithPaint(path, paint, &fillPath);
108 draw_path(canvas, fillPath, r, join, doFill);
109
110 canvas->translate(W + 2 * STROKE_WIDTH, 0);
111 }
112 acr.restore();
113 canvas->translate(0, H + 2 * STROKE_WIDTH);
114 }
116 }
117 }
118
119private:
120 using INHERITED = GM;
121};
122DEF_GM(return new StrokeRectGM;)
123
124///////////////////////////////////////////////////////////////////////////////////////////////////
125
126/*
127 * Exercise rect-stroking (which is specialized from paths) when the resulting stroke-width is
128 * non-square. See https://bugs.chromium.org/p/skia/issues/detail?id=5408
129 */
130DEF_SIMPLE_GM(strokerect_anisotropic_5408, canvas, 200, 50) {
131 SkPaint p;
132 p.setStyle(SkPaint::kStroke_Style);
133 p.setStrokeWidth(6);
134
135 canvas->scale(10, 1);
136 SkRect r = SkRect::MakeXYWH(5, 20, 10, 10);
137 canvas->drawRect(r, p);
138}
static const int points[]
constexpr SkColor SK_ColorGRAY
Definition SkColor.h:113
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
#define W
Definition aaa.cpp:17
void drawRect(const SkRect &rect, const SkPaint &paint)
void drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint &paint)
void translate(SkScalar dx, SkScalar dy)
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition SkCanvas.h:1182
void drawPath(const SkPath &path, const SkPaint &paint)
@ kPoints_PointMode
draw each point separately
Definition SkCanvas.h:1241
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
@ kStrokeAndFill_Style
sets to stroke and fill geometry
Definition SkPaint.h:195
@ kRound_Join
adds circle
Definition SkPaint.h:360
@ kMiter_Join
extends to miter limit
Definition SkPaint.h:359
@ kBevel_Join
connects outside edges
Definition SkPaint.h:361
SkString getName() const override
void onDraw(SkCanvas *canvas) override
SkISize getISize() override
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition gm.cpp:81
const Paint & paint
float SkScalar
Definition extension.cpp:12
#define DEF_GM(CODE)
Definition gm.h:40
#define DEF_SIMPLE_GM(NAME, CANVAS, W, H)
Definition gm.h:50
SK_API bool FillPathWithPaint(const SkPath &src, const SkPaint &paint, SkPath *dst, const SkRect *cullRect, SkScalar resScale=1)
#define STROKE_WIDTH
static void draw_path(SkCanvas *canvas, const SkPath &path, const SkRect &rect, SkPaint::Join join, int doFill)
Definition SkMD5.cpp:130
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659