2014-06-09 22:11:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkFontStyle.h"
|
|
|
|
#include "include/core/SkFontTypes.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypeface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2014-06-09 22:11:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws text with random parameters. The text draws each get their own clip rect. It is also
|
2016-12-21 16:14:46 +00:00
|
|
|
* used as a bench to measure how well the GPU backend combines draw ops for text draws.
|
2014-06-09 22:11:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
class VariedTextGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
VariedTextGM(bool effectiveClip, bool lcd)
|
|
|
|
: fEffectiveClip(effectiveClip)
|
|
|
|
, fLCD(lcd) {
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2014-06-09 22:11:30 +00:00
|
|
|
SkString name("varied_text");
|
|
|
|
if (fEffectiveClip) {
|
|
|
|
name.append("_clipped");
|
|
|
|
} else {
|
|
|
|
name.append("_ignorable_clip");
|
|
|
|
}
|
|
|
|
if (fLCD) {
|
|
|
|
name.append("_lcd");
|
|
|
|
} else {
|
|
|
|
name.append("_no_lcd");
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-06-09 22:11:30 +00:00
|
|
|
return SkISize::Make(640, 480);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2014-06-09 22:11:30 +00:00
|
|
|
fPaint.setAntiAlias(true);
|
2019-01-02 17:21:01 +00:00
|
|
|
fFont.setEdging(fLCD ? SkFont::Edging::kSubpixelAntiAlias : SkFont::Edging::kAntiAlias);
|
2014-06-09 22:11:30 +00:00
|
|
|
|
|
|
|
SkISize size = this->getISize();
|
|
|
|
SkScalar w = SkIntToScalar(size.fWidth);
|
|
|
|
SkScalar h = SkIntToScalar(size.fHeight);
|
|
|
|
|
2016-05-12 17:09:30 +00:00
|
|
|
static_assert(4 == SK_ARRAY_COUNT(fTypefaces), "typeface_cnt");
|
2019-03-20 16:12:10 +00:00
|
|
|
fTypefaces[0] = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle());
|
|
|
|
fTypefaces[1] = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Bold());
|
|
|
|
fTypefaces[2] = ToolUtils::create_portable_typeface("serif", SkFontStyle());
|
|
|
|
fTypefaces[3] = ToolUtils::create_portable_typeface("serif", SkFontStyle::Bold());
|
2014-06-09 22:11:30 +00:00
|
|
|
|
|
|
|
SkRandom random;
|
|
|
|
for (int i = 0; i < kCnt; ++i) {
|
|
|
|
int length = random.nextRangeU(kMinLength, kMaxLength);
|
|
|
|
char text[kMaxLength];
|
|
|
|
for (int j = 0; j < length; ++j) {
|
|
|
|
text[j] = (char)random.nextRangeU('!', 'z');
|
|
|
|
}
|
|
|
|
fStrings[i].set(text, length);
|
|
|
|
|
|
|
|
fColors[i] = random.nextU();
|
|
|
|
fColors[i] |= 0xFF000000;
|
2019-03-20 16:12:10 +00:00
|
|
|
fColors[i] = ToolUtils::color_to_565(fColors[i]);
|
2014-06-09 22:11:30 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar kMinPtSize = 8.f;
|
|
|
|
constexpr SkScalar kMaxPtSize = 32.f;
|
2014-06-09 22:11:30 +00:00
|
|
|
|
|
|
|
fPtSizes[i] = random.nextRangeScalar(kMinPtSize, kMaxPtSize);
|
|
|
|
|
2016-05-12 17:09:30 +00:00
|
|
|
fTypefaceIndices[i] = random.nextULessThan(SK_ARRAY_COUNT(fTypefaces));
|
2014-06-09 22:11:30 +00:00
|
|
|
|
|
|
|
SkRect r;
|
|
|
|
fPaint.setColor(fColors[i]);
|
2019-01-02 17:21:01 +00:00
|
|
|
fFont.setTypeface(fTypefaces[fTypefaceIndices[i]]);
|
|
|
|
fFont.setSize(fPtSizes[i]);
|
2014-06-09 22:11:30 +00:00
|
|
|
|
2019-05-07 19:38:46 +00:00
|
|
|
fFont.measureText(fStrings[i].c_str(), fStrings[i].size(), SkTextEncoding::kUTF8, &r);
|
2020-09-25 18:17:30 +00:00
|
|
|
// The set of x,y offsets which place the bounding box inside the GM's border.
|
2014-06-09 22:11:30 +00:00
|
|
|
SkRect safeRect = SkRect::MakeLTRB(-r.fLeft, -r.fTop, w - r.fRight, h - r.fBottom);
|
|
|
|
if (safeRect.isEmpty()) {
|
2020-09-25 18:17:30 +00:00
|
|
|
// If the bounds don't fit then allow any offset in the GM's border.
|
2014-06-09 22:11:30 +00:00
|
|
|
safeRect = SkRect::MakeWH(w, h);
|
|
|
|
}
|
2020-09-25 18:17:30 +00:00
|
|
|
fOffsets[i].fX = random.nextRangeScalar(safeRect.fLeft, safeRect.fRight);
|
|
|
|
fOffsets[i].fY = random.nextRangeScalar(safeRect.fTop, safeRect.fBottom);
|
2014-06-09 22:11:30 +00:00
|
|
|
|
|
|
|
fClipRects[i] = r;
|
2020-09-25 18:17:30 +00:00
|
|
|
fClipRects[i].offset(fOffsets[i].fX, fOffsets[i].fY);
|
2014-06-09 22:11:30 +00:00
|
|
|
fClipRects[i].outset(2.f, 2.f);
|
|
|
|
|
|
|
|
if (fEffectiveClip) {
|
|
|
|
fClipRects[i].fRight -= 0.25f * fClipRects[i].width();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-06-09 22:11:30 +00:00
|
|
|
for (int i = 0; i < kCnt; ++i) {
|
|
|
|
fPaint.setColor(fColors[i]);
|
2019-01-02 17:21:01 +00:00
|
|
|
fFont.setSize(fPtSizes[i]);
|
|
|
|
fFont.setTypeface(fTypefaces[fTypefaceIndices[i]]);
|
2014-06-09 22:11:30 +00:00
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(fClipRects[i]);
|
2020-09-25 18:17:30 +00:00
|
|
|
canvas->translate(fOffsets[i].fX, fOffsets[i].fY);
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(fStrings[i].c_str(), fStrings[i].size(), SkTextEncoding::kUTF8,
|
2019-01-02 17:21:01 +00:00
|
|
|
0, 0, fFont, fPaint);
|
2014-06-09 22:11:30 +00:00
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Visualize the clips, but not in bench mode.
|
|
|
|
if (kBench_Mode != this->getMode()) {
|
|
|
|
SkPaint wirePaint;
|
|
|
|
wirePaint.setAntiAlias(true);
|
|
|
|
wirePaint.setStrokeWidth(0);
|
|
|
|
wirePaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
for (int i = 0; i < kCnt; ++i) {
|
|
|
|
canvas->drawRect(fClipRects[i], wirePaint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool runAsBench() const override { return true; }
|
2014-06-09 22:11:30 +00:00
|
|
|
|
|
|
|
private:
|
2016-09-01 18:24:54 +00:00
|
|
|
static constexpr int kCnt = 30;
|
|
|
|
static constexpr int kMinLength = 15;
|
|
|
|
static constexpr int kMaxLength = 40;
|
2014-06-09 22:11:30 +00:00
|
|
|
|
|
|
|
bool fEffectiveClip;
|
|
|
|
bool fLCD;
|
2016-05-12 17:09:30 +00:00
|
|
|
sk_sp<SkTypeface> fTypefaces[4];
|
2014-06-09 22:11:30 +00:00
|
|
|
SkPaint fPaint;
|
2019-01-02 17:21:01 +00:00
|
|
|
SkFont fFont;
|
2014-06-09 22:11:30 +00:00
|
|
|
|
|
|
|
// precomputed for each text draw
|
|
|
|
SkString fStrings[kCnt];
|
|
|
|
SkColor fColors[kCnt];
|
|
|
|
SkScalar fPtSizes[kCnt];
|
2016-05-12 17:09:30 +00:00
|
|
|
int fTypefaceIndices[kCnt];
|
2020-09-25 18:17:30 +00:00
|
|
|
SkPoint fOffsets[kCnt];
|
2014-06-09 22:11:30 +00:00
|
|
|
SkRect fClipRects[kCnt];
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = skiagm::GM;
|
2014-06-09 22:11:30 +00:00
|
|
|
};
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new VariedTextGM(false, false);)
|
|
|
|
DEF_GM(return new VariedTextGM(true, false);)
|
|
|
|
DEF_GM(return new VariedTextGM(false, true);)
|
|
|
|
DEF_GM(return new VariedTextGM(true, true);)
|