Flutter Engine
The Flutter Engine
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);
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)
Definition: SkCanvas.cpp:1673
void drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint &paint)
Definition: SkCanvas.cpp:1710
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition: SkCanvas.h:1182
void drawPath(const SkPath &path, const SkPaint &paint)
Definition: SkCanvas.cpp:1747
@ 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
Definition: SkPath.h:59
SkString getName() const override
Definition: strokerect.cpp:63
void onDraw(SkCanvas *canvas) override
Definition: strokerect.cpp:67
SkISize getISize() override
Definition: strokerect.cpp:65
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
const Paint & paint
Definition: color_source.cc:38
#define H
float SkScalar
Definition: extension.cpp:12
#define DEF_GM(CODE)
Definition: gm.h:40
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
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
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
SK_API bool FillPathWithPaint(const SkPath &src, const SkPaint &paint, SkPath *dst, const SkRect *cullRect, SkScalar resScale=1)
Definition: SkPathUtils.cpp:23
static SkString join(const CommandLineFlags::StringArray &)
Definition: skpbench.cpp:741
#define STROKE_WIDTH
Definition: strokerect.cpp:26
DEF_SIMPLE_GM(strokerect_anisotropic_5408, canvas, 200, 50)
Definition: strokerect.cpp:130
static void draw_path(SkCanvas *canvas, const SkPath &path, const SkRect &rect, SkPaint::Join join, int doFill)
Definition: strokerect.cpp:28
Definition: SkMD5.cpp:130
Definition: SkSize.h:16
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