Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
stroke_rect_shader.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2016 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/SkRect.h"
19
20#include <initializer_list>
21#include <utility>
22
23namespace skiagm {
24
25// Draw stroked rects (both AA and nonAA) with all the types of joins:
26// bevel, miter, miter-limited-to-bevel, round
27// and as a hairline.
28DEF_SIMPLE_GM(stroke_rect_shader, canvas, 690, 300) {
29 constexpr SkRect kRect {0, 0, 100, 100};
31 constexpr SkColor kColors[] {SK_ColorRED, SK_ColorBLUE};
32 sk_sp<SkShader> shader = SkGradientShader::MakeLinear(kPts, kColors, nullptr, 2,
34
35 // Do a large initial translate so that local coords disagree with device coords significantly
36 // for the first rect drawn.
37 canvas->translate(kRect.centerX(), kRect.centerY());
38 constexpr SkScalar kPad = 20;
39 for (auto aa : {false, true}) {
41 paint.setShader(shader);
43 paint.setAntiAlias(aa);
44
45 canvas->save();
46
47 constexpr SkScalar kStrokeWidth = 10;
48 paint.setStrokeWidth(kStrokeWidth);
49
50 paint.setStrokeJoin(SkPaint::kBevel_Join);
51 canvas->drawRect(kRect, paint);
52 canvas->translate(kRect.width() + kPad, 0);
53
54 paint.setStrokeJoin(SkPaint::kMiter_Join);
55 canvas->drawRect(kRect, paint);
56 canvas->translate(kRect.width() + kPad, 0);
57
58 // This miter limit should effectively produce a bevel join.
59 paint.setStrokeMiter(0.01f);
60 canvas->drawRect(kRect, paint);
61 canvas->translate(kRect.width() + kPad, 0);
62
63 paint.setStrokeJoin(SkPaint::kRound_Join);
64 canvas->drawRect(kRect, paint);
65 canvas->translate(kRect.width() + kPad, 0);
66
67 paint.setStrokeWidth(0);
68 canvas->drawRect(kRect, paint);
69
70 canvas->restore();
71 canvas->translate(0, kRect.height() + kPad);
72 }
73}
74
75} // namespace skiagm
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr int kPad
constexpr SkRect kRect
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
@ 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
const Paint & paint
float SkScalar
Definition extension.cpp:12
#define DEF_SIMPLE_GM(NAME, CANVAS, W, H)
Definition gm.h:50
static constexpr SkPoint kPts[kPtsCount]
static constexpr float kStrokeWidth
SkScalar fBottom
larger y-axis bounds
Definition extension.cpp:17
SkScalar fLeft
smaller x-axis bounds
Definition extension.cpp:14
SkScalar fRight
larger x-axis bounds
Definition extension.cpp:16
constexpr float centerX() const
Definition SkRect.h:776
constexpr float height() const
Definition SkRect.h:769
constexpr float centerY() const
Definition SkRect.h:785
constexpr float width() const
Definition SkRect.h:762
SkScalar fTop
smaller y-axis bounds
Definition extension.cpp:15