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

113 lines
3.5 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 "SkPoint.h"
#include "SkShader.h"
#include "SkTextBlob.h"
#include "SkTDArray.h"
#include "SkTypeface.h"
// This GM exercises drawTextBlob offset vs. shader space behavior.
class TextBlobShaderGM : public skiagm::GM {
public:
TextBlobShaderGM(const char* txt) {
SkPaint p;
sk_tool_utils::set_portable_typeface(&p);
size_t txtLen = strlen(txt);
fGlyphs.append(p.textToGlyphs(txt, txtLen, nullptr));
p.textToGlyphs(txt, txtLen, fGlyphs.begin());
}
protected:
void onOnceBeforeDraw() override {
SkPaint p;
p.setAntiAlias(true);
p.setSubpixelText(true);
p.setTextSize(30);
p.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
sk_tool_utils::set_portable_typeface(&p);
SkTextBlobBuilder builder;
int glyphCount = fGlyphs.count();
const SkTextBlobBuilder::RunBuffer* run;
run = &builder.allocRun(p, glyphCount, 10, 10, nullptr);
memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
run = &builder.allocRunPosH(p, glyphCount, 80, nullptr);
memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
for (int i = 0; i < glyphCount; ++i) {
run->pos[i] = p.getTextSize() * i * .75f;
}
run = &builder.allocRunPos(p, glyphCount, nullptr);
memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
for (int i = 0; i < glyphCount; ++i) {
run->pos[i * 2] = p.getTextSize() * i * .75f;
run->pos[i * 2 + 1] = 150 + 5 * sinf((float)i * 8 / glyphCount);
}
fBlob.reset(builder.build());
SkColor colors[2];
colors[0] = SK_ColorRED;
colors[1] = SK_ColorGREEN;
SkScalar pos[SK_ARRAY_COUNT(colors)];
for (unsigned i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
pos[i] = (float)i / (SK_ARRAY_COUNT(pos) - 1);
}
SkISize sz = this->onISize();
fShader = SkGradientShader::MakeRadial(SkPoint::Make(SkIntToScalar(sz.width() / 2),
SkIntToScalar(sz.height() / 2)),
sz.width() * .66f, colors, pos,
SK_ARRAY_COUNT(colors),
SkShader::kRepeat_TileMode);
}
SkString onShortName() override {
return SkString("textblobshader");
}
SkISize onISize() override {
return SkISize::Make(640, 480);
}
void onDraw(SkCanvas* canvas) override {
SkPaint p;
p.setStyle(SkPaint::kFill_Style);
p.setShader(fShader);
SkISize sz = this->onISize();
constexpr int kXCount = 4;
constexpr int kYCount = 3;
for (int i = 0; i < kXCount; ++i) {
for (int j = 0; j < kYCount; ++j) {
canvas->drawTextBlob(fBlob,
SkIntToScalar(i * sz.width() / kXCount),
SkIntToScalar(j * sz.height() / kYCount),
p);
}
}
}
private:
SkTDArray<uint16_t> fGlyphs;
SkAutoTUnref<const SkTextBlob> fBlob;
sk_sp<SkShader> fShader;
typedef skiagm::GM INHERITED;
};
DEF_GM(return new TextBlobShaderGM("Blobber");)