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

84 lines
2.0 KiB
C++

/*
* Copyright 2011 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 "SkRandom.h"
namespace skiagm {
#define W 400
#define H 400
#define N 100
constexpr SkScalar SW = SkIntToScalar(W);
constexpr SkScalar SH = SkIntToScalar(H);
class StrokeRectsGM : public GM {
public:
StrokeRectsGM() {}
protected:
SkString onShortName() override {
return SkString("strokerects");
}
SkISize onISize() override {
return SkISize::Make(W*2, H*2);
}
static void rnd_rect(SkRect* r, SkRandom& rand) {
SkScalar x = rand.nextUScalar1() * W;
SkScalar y = rand.nextUScalar1() * H;
SkScalar w = rand.nextUScalar1() * (W >> 2);
SkScalar h = rand.nextUScalar1() * (H >> 2);
SkScalar hoffset = rand.nextSScalar1();
SkScalar woffset = rand.nextSScalar1();
r->set(x, y, x + w, y + h);
r->offset(-w/2 + woffset, -h/2 + hoffset);
}
void onDraw(SkCanvas* canvas) override {
SkPaint paint;
paint.setStyle(SkPaint::kStroke_Style);
for (int y = 0; y < 2; y++) {
paint.setAntiAlias(!!y);
for (int x = 0; x < 2; x++) {
paint.setStrokeWidth(x * SkIntToScalar(3));
SkAutoCanvasRestore acr(canvas, true);
canvas->translate(SW * x, SH * y);
canvas->clipRect(SkRect::MakeLTRB(
SkIntToScalar(2), SkIntToScalar(2)
, SW - SkIntToScalar(2), SH - SkIntToScalar(2)
));
SkRandom rand;
for (int i = 0; i < N; i++) {
SkRect r;
rnd_rect(&r, rand);
canvas->drawRect(r, paint);
}
}
}
}
private:
typedef GM INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
static GM* MyFactory(void*) { return new StrokeRectsGM; }
static GMRegistry reg(MyFactory);
}