bsalomon | a7d85ba | 2016-07-06 11:54:59 -0700 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "include/effects/SkGradientShader.h" |
bsalomon | a7d85ba | 2016-07-06 11:54:59 -0700 | [diff] [blame] | 19 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 20 | #include <initializer_list> |
| 21 | #include <utility> |
| 22 | |
bsalomon | a7d85ba | 2016-07-06 11:54:59 -0700 | [diff] [blame] | 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) { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 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}; |
bsalomon | a7d85ba | 2016-07-06 11:54:59 -0700 | [diff] [blame] | 32 | SkPaint paint; |
| 33 | sk_sp<SkShader> shader = SkGradientShader::MakeLinear(kPts, kColors, nullptr, 2, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 34 | SkTileMode::kClamp); |
bsalomon | a7d85ba | 2016-07-06 11:54:59 -0700 | [diff] [blame] | 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()); |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 40 | constexpr SkScalar kPad = 20; |
bsalomon | a7d85ba | 2016-07-06 11:54:59 -0700 | [diff] [blame] | 41 | for (auto aa : {false, true}) { |
| 42 | paint.setAntiAlias(aa); |
| 43 | canvas->save(); |
| 44 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 45 | constexpr SkScalar kStrokeWidth = 10; |
bsalomon | a7d85ba | 2016-07-06 11:54:59 -0700 | [diff] [blame] | 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 | } |