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

111 lines
3.7 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 "SkAnimTimer.h"
#include "SkPath.h"
#include "SkDashPathEffect.h"
int dash1[] = { 1, 1 };
int dash2[] = { 1, 3 };
int dash3[] = { 1, 1, 3, 3 };
int dash4[] = { 1, 3, 2, 4 };
struct DashExample {
int* pattern;
int length;
} dashExamples[] = {
{ dash1, SK_ARRAY_COUNT(dash1) },
{ dash2, SK_ARRAY_COUNT(dash2) },
{ dash3, SK_ARRAY_COUNT(dash3) },
{ dash4, SK_ARRAY_COUNT(dash4) }
};
class DashCircleGM : public skiagm::GM {
public:
DashCircleGM() : fRotation(0) { }
protected:
SkString onShortName() override { return SkString("dashcircle"); }
SkISize onISize() override { return SkISize::Make(900, 1200); }
void onDraw(SkCanvas* canvas) override {
SkPaint refPaint;
refPaint.setAntiAlias(true);
refPaint.setColor(0xFFbf3f7f);
refPaint.setStyle(SkPaint::kStroke_Style);
refPaint.setStrokeWidth(1);
const SkScalar radius = 125;
SkRect oval = SkRect::MakeLTRB(-radius - 20, -radius - 20, radius + 20, radius + 20);
SkPath circle;
circle.addCircle(0, 0, radius);
SkScalar circumference = radius * SK_ScalarPI * 2;
int wedges[] = { 6, 12, 36 };
canvas->translate(radius+20, radius+20);
for (int wedge : wedges) {
SkScalar arcLength = 360.f / wedge;
canvas->save();
for (const DashExample& dashExample : dashExamples) {
SkPath refPath;
int dashUnits = 0;
for (int index = 0; index < dashExample.length; ++index) {
dashUnits += dashExample.pattern[index];
}
SkScalar unitLength = arcLength / dashUnits;
SkScalar angle = 0;
for (int index = 0; index < wedge; ++index) {
for (int i2 = 0; i2 < dashExample.length; i2 += 2) {
SkScalar span = dashExample.pattern[i2] * unitLength;
refPath.moveTo(0, 0);
refPath.arcTo(oval, angle, span, false);
refPath.close();
angle += span + (dashExample.pattern[i2 + 1]) * unitLength;
}
}
canvas->save();
canvas->rotate(fRotation);
canvas->drawPath(refPath, refPaint);
canvas->restore();
SkPaint p;
p.setAntiAlias(true);
p.setStyle(SkPaint::kStroke_Style);
p.setStrokeWidth(10);
SkScalar intervals[4];
int intervalCount = dashExample.length;
SkScalar dashLength = circumference / wedge / dashUnits;
for (int index = 0; index < dashExample.length; ++index) {
intervals[index] = dashExample.pattern[index] * dashLength;
}
p.setPathEffect(SkDashPathEffect::Make(intervals, intervalCount, 0));
canvas->save();
canvas->rotate(fRotation);
canvas->drawPath(circle, p);
canvas->restore();
canvas->translate(0, radius * 2 + 50);
}
canvas->restore();
canvas->translate(radius * 2 + 50, 0);
}
}
bool onAnimate(const SkAnimTimer& timer) override {
constexpr SkScalar kDesiredDurationSecs = 100.0f;
fRotation = timer.scaled(360.0f/kDesiredDurationSecs, 360.0f);
return true;
}
private:
SkScalar fRotation;
typedef GM INHERITED;
};
DEF_GM(return new DashCircleGM; )