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

107 lines
3.4 KiB
C++

/*
* Copyright 2014 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"
#include "SkPath.h"
#include "SkRandom.h"
int make_bm(SkBitmap* bm, int height) {
constexpr int kRadius = 22;
constexpr int kMargin = 8;
constexpr SkScalar kStartAngle = 0;
constexpr SkScalar kDAngle = 25;
constexpr SkScalar kSweep = 320;
constexpr SkScalar kThickness = 8;
int count = (height / (2 * kRadius + kMargin));
height = count * (2 * kRadius + kMargin);
bm->allocN32Pixels(2 * (kRadius + kMargin), height);
SkRandom random;
SkCanvas wholeCanvas(*bm);
wholeCanvas.clear(0x00000000);
SkScalar angle = kStartAngle;
for (int i = 0; i < count; ++i) {
SkPaint paint;
// The sw rasterizer disables AA for large canvii. So we make a small canvas for each draw.
SkBitmap smallBM;
SkIRect subRect = SkIRect::MakeXYWH(0, i * (kMargin + 2 * kRadius),
2 * kRadius + kMargin, 2 * kRadius + kMargin);
bm->extractSubset(&smallBM, subRect);
SkCanvas canvas(smallBM);
canvas.translate(kMargin + kRadius, kMargin + kRadius);
paint.setAntiAlias(true);
paint.setColor(random.nextU() | 0xFF000000);
paint.setStyle(SkPaint::kStroke_Style);
paint.setStrokeWidth(kThickness);
paint.setStrokeCap(SkPaint::kRound_Cap);
SkScalar radius = kRadius - kThickness / 2;
SkRect bounds = SkRect::MakeLTRB(-radius, -radius, radius, radius);
canvas.drawArc(bounds, angle, kSweep, false, paint);
angle += kDAngle;
}
bm->setImmutable();
return count;
}
class TallStretchedBitmapsGM : public skiagm::GM {
public:
TallStretchedBitmapsGM() {}
protected:
SkString onShortName() override {
return SkString("tall_stretched_bitmaps");
}
SkISize onISize() override {
return SkISize::Make(730, 690);
}
void onOnceBeforeDraw() override {
for (size_t i = 0; i < SK_ARRAY_COUNT(fTallBmps); ++i) {
int h = SkToInt((4 + i) * 1024);
fTallBmps[i].fItemCnt = make_bm(&fTallBmps[i].fBmp, h);
}
}
void onDraw(SkCanvas* canvas) override {
canvas->scale(1.3f, 1.3f);
for (size_t i = 0; i < SK_ARRAY_COUNT(fTallBmps); ++i) {
SkASSERT(fTallBmps[i].fItemCnt > 10);
SkBitmap bmp = fTallBmps[i].fBmp;
// Draw the last 10 elements of the bitmap.
int startItem = fTallBmps[i].fItemCnt - 10;
int itemHeight = bmp.height() / fTallBmps[i].fItemCnt;
SkIRect subRect = SkIRect::MakeLTRB(0, startItem * itemHeight,
bmp.width(), bmp.height());
SkRect dstRect = SkRect::MakeWH(SkIntToScalar(bmp.width()), 10.f * itemHeight);
SkPaint paint;
paint.setFilterQuality(kLow_SkFilterQuality);
canvas->drawBitmapRect(bmp, subRect, dstRect, &paint);
canvas->translate(SkIntToScalar(bmp.width() + 10), 0);
}
}
private:
struct {
SkBitmap fBmp;
int fItemCnt;
} fTallBmps[8];
typedef skiagm::GM INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
DEF_GM(return new TallStretchedBitmapsGM;)