blob: 1826f629f6b5c0b9a719378c6bc3000640d9283c [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
8#include "gm.h"
9#include "SkGradientShader.h"
10
11namespace skiagm {
12
13// Draw stroked rects (both AA and nonAA) with all the types of joins:
14// bevel, miter, miter-limited-to-bevel, round
15// and as a hairline.
16DEF_SIMPLE_GM(stroke_rect_shader, canvas, 690, 300) {
mtkleindbfd7ab2016-09-01 11:24:54 -070017 constexpr SkRect kRect {0, 0, 100, 100};
18 constexpr SkPoint kPts[] {{kRect.fLeft, kRect.fTop}, {kRect.fRight, kRect.fBottom}};
19 constexpr SkColor kColors[] {SK_ColorRED, SK_ColorBLUE};
bsalomona7d85ba2016-07-06 11:54:59 -070020 SkPaint paint;
21 sk_sp<SkShader> shader = SkGradientShader::MakeLinear(kPts, kColors, nullptr, 2,
22 SkShader::kClamp_TileMode);
23 paint.setShader(std::move(shader));
24 paint.setStyle(SkPaint::kStroke_Style);
25 // Do a large initial translate so that local coords disagree with device coords significantly
26 // for the first rect drawn.
27 canvas->translate(kRect.centerX(), kRect.centerY());
mtkleindbfd7ab2016-09-01 11:24:54 -070028 constexpr SkScalar kPad = 20;
bsalomona7d85ba2016-07-06 11:54:59 -070029 for (auto aa : {false, true}) {
30 paint.setAntiAlias(aa);
31 canvas->save();
32
mtkleindbfd7ab2016-09-01 11:24:54 -070033 constexpr SkScalar kStrokeWidth = 10;
bsalomona7d85ba2016-07-06 11:54:59 -070034 paint.setStrokeWidth(kStrokeWidth);
35
36 paint.setStrokeJoin(SkPaint::kBevel_Join);
37 canvas->drawRect(kRect, paint);
38 canvas->translate(kRect.width() + kPad, 0);
39
40 paint.setStrokeJoin(SkPaint::kMiter_Join);
41 canvas->drawRect(kRect, paint);
42 canvas->translate(kRect.width() + kPad, 0);
43
44 // This miter limit should effectively produce a bevel join.
45 paint.setStrokeMiter(0.01f);
46 canvas->drawRect(kRect, paint);
47 canvas->translate(kRect.width() + kPad, 0);
48
49 paint.setStrokeJoin(SkPaint::kRound_Join);
50 canvas->drawRect(kRect, paint);
51 canvas->translate(kRect.width() + kPad, 0);
52
53 paint.setStrokeWidth(0);
54 canvas->drawRect(kRect, paint);
55
56 canvas->restore();
57 canvas->translate(0, kRect.height() + kPad);
58 }
59}
60
61}