blob: 596d879559dd741032123a2e963eb7aa846d7b52 [file] [log] [blame]
bsalomona7d85ba2016-07-06 11:54:59 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/effects/SkGradientShader.h"
bsalomona7d85ba2016-07-06 11:54:59 -070019
Ben Wagner7fde8e12019-05-01 17:28:53 -040020#include <initializer_list>
21#include <utility>
22
bsalomona7d85ba2016-07-06 11:54:59 -070023namespace 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) {
mtkleindbfd7ab2016-09-01 11:24:54 -070029 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};
bsalomona7d85ba2016-07-06 11:54:59 -070032 SkPaint paint;
33 sk_sp<SkShader> shader = SkGradientShader::MakeLinear(kPts, kColors, nullptr, 2,
Mike Reedfae8fce2019-04-03 10:27:45 -040034 SkTileMode::kClamp);
bsalomona7d85ba2016-07-06 11:54:59 -070035 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());
mtkleindbfd7ab2016-09-01 11:24:54 -070040 constexpr SkScalar kPad = 20;
bsalomona7d85ba2016-07-06 11:54:59 -070041 for (auto aa : {false, true}) {
42 paint.setAntiAlias(aa);
43 canvas->save();
44
mtkleindbfd7ab2016-09-01 11:24:54 -070045 constexpr SkScalar kStrokeWidth = 10;
bsalomona7d85ba2016-07-06 11:54:59 -070046 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}