2015-11-08 16:07:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
#include "Sample.h"
|
2015-11-08 16:07:24 +00:00
|
|
|
#include "SkCanvas.h"
|
2018-08-21 15:45:46 +00:00
|
|
|
#include "SkUTF.h"
|
2015-11-08 16:07:24 +00:00
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkColorFilter.h"
|
2017-05-03 19:16:58 +00:00
|
|
|
#include "SkImage.h"
|
2015-11-08 16:07:24 +00:00
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "SkTime.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
#include "Timer.h"
|
|
|
|
|
2015-11-17 17:38:05 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2015-11-08 16:07:24 +00:00
|
|
|
#include "GrContext.h"
|
2018-03-06 13:20:37 +00:00
|
|
|
#include "GrContextPriv.h"
|
2015-11-17 17:38:05 +00:00
|
|
|
#endif
|
2015-11-08 16:07:24 +00:00
|
|
|
|
|
|
|
SkRandom gRand;
|
|
|
|
|
|
|
|
static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkScalar x, SkScalar y,
|
|
|
|
const SkPaint& paint) {
|
|
|
|
SkPaint p(paint);
|
|
|
|
|
|
|
|
p.setSubpixelText(true);
|
|
|
|
canvas->drawText(text, length, x, y, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This sample demonstrates the cache behavior of bitmap vs. distance field text
|
|
|
|
// It renders variously sized text with an animated scale and rotation.
|
|
|
|
// Specifically one should:
|
|
|
|
// use 'D' to toggle between bitmap and distance field fonts
|
|
|
|
// use '2' to toggle between scaling the image by 2x
|
|
|
|
// -- this feature boosts the rendering out of the small point-size
|
|
|
|
// SDF-text special case (which falls back to bitmap fonts for small points)
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class AnimatedTextView : public Sample {
|
2015-11-08 16:07:24 +00:00
|
|
|
public:
|
|
|
|
AnimatedTextView() : fScale(1.0f), fScaleInc(0.1f), fRotation(0.0f), fSizeScale(1) {
|
|
|
|
fCurrentTime = 0;
|
|
|
|
fTimer.start();
|
|
|
|
memset(fTimes, 0, sizeof(fTimes));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-08-08 15:36:17 +00:00
|
|
|
bool onQuery(Sample::Event* evt) override {
|
|
|
|
if (Sample::TitleQ(*evt)) {
|
|
|
|
Sample::TitleR(evt, "AnimatedText");
|
2015-11-08 16:07:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkUnichar uni;
|
2018-08-08 15:36:17 +00:00
|
|
|
if (Sample::CharQ(*evt, &uni)) {
|
2015-11-08 16:07:24 +00:00
|
|
|
if ('2' == uni) {
|
|
|
|
if (fSizeScale == 2) {
|
|
|
|
fSizeScale = 1;
|
|
|
|
} else {
|
|
|
|
fSizeScale = 2;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
|
|
|
SkPaint paint;
|
2016-05-12 17:09:30 +00:00
|
|
|
paint.setTypeface(SkTypeface::MakeFromFile("/skimages/samplefont.ttf"));
|
2015-11-08 16:07:24 +00:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setFilterQuality(kMedium_SkFilterQuality);
|
|
|
|
|
|
|
|
SkString outString("fps: ");
|
|
|
|
fTimer.end();
|
|
|
|
|
|
|
|
// TODO: generalize this timing code in utils
|
|
|
|
fTimes[fCurrentTime] = (float)(fTimer.fWall);
|
|
|
|
fCurrentTime = (fCurrentTime + 1) & 0x1f;
|
|
|
|
|
|
|
|
float meanTime = 0.0f;
|
|
|
|
for (int i = 0; i < 32; ++i) {
|
|
|
|
meanTime += fTimes[i];
|
|
|
|
}
|
|
|
|
meanTime /= 32.f;
|
|
|
|
SkScalar fps = 1000.f / meanTime;
|
|
|
|
outString.appendScalar(fps);
|
|
|
|
outString.append(" ms: ");
|
|
|
|
outString.appendScalar(meanTime);
|
|
|
|
|
|
|
|
SkString modeString("Text scale: ");
|
|
|
|
modeString.appendU32(fSizeScale);
|
|
|
|
modeString.append("x");
|
|
|
|
|
|
|
|
fTimer.start();
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
GrContext* grContext = canvas->getGrContext();
|
|
|
|
if (grContext) {
|
2018-03-06 13:20:37 +00:00
|
|
|
sk_sp<SkImage> image = grContext->contextPriv().getFontAtlasImage_ForTesting(
|
|
|
|
GrMaskFormat::kA8_GrMaskFormat);
|
2016-12-20 13:57:26 +00:00
|
|
|
canvas->drawImageRect(image,
|
|
|
|
SkRect::MakeXYWH(512.0f, 10.0f, 512.0f, 512.0f), &paint);
|
2015-11-08 16:07:24 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
canvas->translate(180, 180);
|
|
|
|
canvas->rotate(fRotation);
|
|
|
|
canvas->scale(fScale, fScale);
|
|
|
|
canvas->translate(-180, -180);
|
|
|
|
|
|
|
|
const char* text = "Hamburgefons";
|
|
|
|
size_t length = strlen(text);
|
|
|
|
|
|
|
|
SkScalar y = SkIntToScalar(0);
|
|
|
|
for (int i = 12; i <= 26; i++) {
|
|
|
|
paint.setTextSize(SkIntToScalar(i*fSizeScale));
|
|
|
|
y += paint.getFontSpacing();
|
|
|
|
DrawTheText(canvas, text, length, SkIntToScalar(110), y, paint);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
paint.setTextSize(16);
|
2017-04-28 19:35:12 +00:00
|
|
|
// canvas->drawString(outString, 512.f, 540.f, paint);
|
|
|
|
canvas->drawString(modeString, 768.f, 540.f, paint);
|
2015-11-08 16:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool onAnimate(const SkAnimTimer& timer) override {
|
2016-03-29 16:03:52 +00:00
|
|
|
// We add noise to the scale and rotation animations to
|
2015-11-08 16:07:24 +00:00
|
|
|
// keep the font atlas from falling into a steady state
|
|
|
|
fRotation += (1.0f + gRand.nextRangeF(-0.1f, 0.1f));
|
|
|
|
fScale += (fScaleInc + gRand.nextRangeF(-0.025f, 0.025f));
|
|
|
|
if (fScale >= 2.0f) {
|
|
|
|
fScaleInc = -0.1f;
|
|
|
|
} else if (fScale <= 1.0f) {
|
|
|
|
fScaleInc = 0.1f;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
float fScale;
|
|
|
|
float fScaleInc;
|
|
|
|
float fRotation;
|
|
|
|
int fSizeScale;
|
|
|
|
|
|
|
|
WallTimer fTimer;
|
|
|
|
float fTimes[32];
|
|
|
|
int fCurrentTime;
|
|
|
|
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2015-11-08 16:07:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new AnimatedTextView(); )
|