• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.h"
9 #include "SkGradientShader.h"
10 
11 namespace 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.
16 DEF_SIMPLE_GM(stroke_rect_shader, canvas, 690, 300) {
17     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};
20     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());
28     constexpr SkScalar kPad = 20;
29     for (auto aa : {false, true}) {
30         paint.setAntiAlias(aa);
31         canvas->save();
32 
33         constexpr SkScalar kStrokeWidth = 10;
34         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 }
62