skia2/gm/stroke_rect_shader.cpp
mtklein dbfd7ab108 Replace a lot of 'static const' with 'constexpr' or 'const'.
'static const' means, there must be at most one of these, and initialize it at
compile time if possible or runtime if necessary.  This leads to unexpected
code execution, and TSAN* will complain about races on the guard variables.

Generally 'constexpr' or 'const' are better choices.  Neither can cause races:
they're either intialized at compile time (constexpr) or intialized each time
independently (const).

This CL prefers constexpr where possible, and uses const where not.  It even
prefers constexpr over const where they don't make a difference... I want to have
lots of examples of constexpr for people to see and mimic.

The scoped-to-class static has nothing to do with any of this, and is not changed.

* Not yet on the bots, which use an older TSAN.

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2300623005

Review-Url: https://codereview.chromium.org/2300623005
2016-09-01 11:24:54 -07:00

62 lines
2.0 KiB
C++

/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm.h"
#include "SkGradientShader.h"
namespace skiagm {
// Draw stroked rects (both AA and nonAA) with all the types of joins:
// bevel, miter, miter-limited-to-bevel, round
// and as a hairline.
DEF_SIMPLE_GM(stroke_rect_shader, canvas, 690, 300) {
constexpr SkRect kRect {0, 0, 100, 100};
constexpr SkPoint kPts[] {{kRect.fLeft, kRect.fTop}, {kRect.fRight, kRect.fBottom}};
constexpr SkColor kColors[] {SK_ColorRED, SK_ColorBLUE};
SkPaint paint;
sk_sp<SkShader> shader = SkGradientShader::MakeLinear(kPts, kColors, nullptr, 2,
SkShader::kClamp_TileMode);
paint.setShader(std::move(shader));
paint.setStyle(SkPaint::kStroke_Style);
// Do a large initial translate so that local coords disagree with device coords significantly
// for the first rect drawn.
canvas->translate(kRect.centerX(), kRect.centerY());
constexpr SkScalar kPad = 20;
for (auto aa : {false, true}) {
paint.setAntiAlias(aa);
canvas->save();
constexpr SkScalar kStrokeWidth = 10;
paint.setStrokeWidth(kStrokeWidth);
paint.setStrokeJoin(SkPaint::kBevel_Join);
canvas->drawRect(kRect, paint);
canvas->translate(kRect.width() + kPad, 0);
paint.setStrokeJoin(SkPaint::kMiter_Join);
canvas->drawRect(kRect, paint);
canvas->translate(kRect.width() + kPad, 0);
// This miter limit should effectively produce a bevel join.
paint.setStrokeMiter(0.01f);
canvas->drawRect(kRect, paint);
canvas->translate(kRect.width() + kPad, 0);
paint.setStrokeJoin(SkPaint::kRound_Join);
canvas->drawRect(kRect, paint);
canvas->translate(kRect.width() + kPad, 0);
paint.setStrokeWidth(0);
canvas->drawRect(kRect, paint);
canvas->restore();
canvas->translate(0, kRect.height() + kPad);
}
}
}