skia2/gm/alphagradients.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

79 lines
2.3 KiB
C++

/*
* Copyright 2013 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 "SkCanvas.h"
#include "SkGradientShader.h"
class AlphaGradientsGM : public skiagm::GM {
public:
AlphaGradientsGM() {}
protected:
SkString onShortName() override {
return SkString("alphagradients");
}
SkISize onISize() override {
return SkISize::Make(640, 480);
}
static void draw_grad(SkCanvas* canvas, const SkRect& r,
SkColor c0, SkColor c1, bool doPreMul) {
SkColor colors[] = { c0, c1 };
SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fBottom } };
SkPaint paint;
uint32_t flags = doPreMul ? SkGradientShader::kInterpolateColorsInPremul_Flag : 0;
paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
SkShader::kClamp_TileMode, flags, nullptr));
canvas->drawRect(r, paint);
paint.setShader(nullptr);
paint.setStyle(SkPaint::kStroke_Style);
canvas->drawRect(r, paint);
}
void onDraw(SkCanvas* canvas) override {
constexpr struct {
SkColor fColor0;
SkColor fColor1;
} gRec[] = {
{ 0xFFFFFFFF, 0x00000000 },
{ 0xFFFFFFFF, 0x00FF0000 },
{ 0xFFFFFFFF, 0x00FFFF00 },
{ 0xFFFFFFFF, 0x00FFFFFF },
{ 0xFFFF0000, 0x00000000 },
{ 0xFFFF0000, 0x00FF0000 },
{ 0xFFFF0000, 0x00FFFF00 },
{ 0xFFFF0000, 0x00FFFFFF },
{ 0xFF0000FF, 0x00000000 },
{ 0xFF0000FF, 0x00FF0000 },
{ 0xFF0000FF, 0x00FFFF00 },
{ 0xFF0000FF, 0x00FFFFFF },
};
SkRect r = SkRect::MakeWH(300, 30);
canvas->translate(10, 10);
for (int doPreMul = 0; doPreMul <= 1; ++doPreMul) {
canvas->save();
for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
draw_grad(canvas, r, gRec[i].fColor0, gRec[i].fColor1, SkToBool(doPreMul));
canvas->translate(0, r.height() + 8);
}
canvas->restore();
canvas->translate(r.width() + 10, 0);
}
}
private:
typedef skiagm::GM INHERITED;
};
DEF_GM(return new AlphaGradientsGM;)