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/SkGradientShader.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};
30    constexpr SkPoint kPts[] {{kRect.fLeft, kRect.fTop}, {kRect.fRight, kRect.fBottom}};
31    constexpr SkColor kColors[] {SK_ColorRED, SK_ColorBLUE};
32    SkPaint paint;
33    sk_sp<SkShader> shader = SkGradientShader::MakeLinear(kPts, kColors, nullptr, 2,
34                                                          SkTileMode::kClamp);
35    paint.setShader(std::move(shader));
36    paint.setStyle(SkPaint::kStroke_Style);
37    // Do a large initial translate so that local coords disagree with device coords significantly
38    // for the first rect drawn.
39    canvas->translate(kRect.centerX(), kRect.centerY());
40    constexpr SkScalar kPad = 20;
41    for (auto aa : {false, true}) {
42        paint.setAntiAlias(aa);
43        canvas->save();
44
45        constexpr SkScalar kStrokeWidth = 10;
46        paint.setStrokeWidth(kStrokeWidth);
47
48        paint.setStrokeJoin(SkPaint::kBevel_Join);
49        canvas->drawRect(kRect, paint);
50        canvas->translate(kRect.width() + kPad, 0);
51
52        paint.setStrokeJoin(SkPaint::kMiter_Join);
53        canvas->drawRect(kRect, paint);
54        canvas->translate(kRect.width() + kPad, 0);
55
56        // This miter limit should effectively produce a bevel join.
57        paint.setStrokeMiter(0.01f);
58        canvas->drawRect(kRect, paint);
59        canvas->translate(kRect.width() + kPad, 0);
60
61        paint.setStrokeJoin(SkPaint::kRound_Join);
62        canvas->drawRect(kRect, paint);
63        canvas->translate(kRect.width() + kPad, 0);
64
65        paint.setStrokeWidth(0);
66        canvas->drawRect(kRect, paint);
67
68        canvas->restore();
69        canvas->translate(0, kRect.height() + kPad);
70    }
71}
72
73}  // namespace skiagm
74