Debian Codegraph @

gm/stroke_rect_shader.cpp

raw
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"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPoint.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkShader.h"
17 #include "include/core/SkTileMode.h"
18 #include "include/effects/SkGradient.h"
19
20 #include <initializer_list>
21 #include <utility>
22
23 namespace 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.
28 DEF_SIMPLE_GM(stroke_rect_shader, canvas, 690, 300) {
29 constexpr SkRect kRect {0, 0, 100, 100};
30 constexpr SkPoint kPts[] {{kRect.fLeft, kRect.fTop}, {kRect.fRight, kRect.fBottom}};
31 constexpr SkColor4f kColors[] {SkColors::kRed, SkColors::kBlue};
32 sk_sp<SkShader> shader = SkShaders::LinearGradient(kPts,
33 {{kColors, {}, SkTileMode::kClamp}, {}});
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}) {
40 SkPaint paint;
41 paint.setShader(shader);
42 paint.setStyle(SkPaint::kStroke_Style);
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