blob: 71f3b87d47760bb10e417f29f52b22fee546b63b [file] [log] [blame]
jvanverth629162d2015-11-08 08:07:24 -08001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkUtils.h"
12#include "SkColorPriv.h"
13#include "SkColorFilter.h"
Florin Malitaab244f02017-05-03 19:16:58 +000014#include "SkImage.h"
jvanverth629162d2015-11-08 08:07:24 -080015#include "SkRandom.h"
jvanverth629162d2015-11-08 08:07:24 -080016#include "SkTime.h"
17#include "SkTypeface.h"
jvanverth629162d2015-11-08 08:07:24 -080018#include "Timer.h"
19
kkinnunen83a5d422015-11-17 09:38:05 -080020#if SK_SUPPORT_GPU
jvanverth629162d2015-11-08 08:07:24 -080021#include "GrContext.h"
Robert Phillips0c4b7b12018-03-06 08:20:37 -050022#include "GrContextPriv.h"
kkinnunen83a5d422015-11-17 09:38:05 -080023#endif
jvanverth629162d2015-11-08 08:07:24 -080024
25SkRandom gRand;
26
27static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkScalar x, SkScalar y,
28 const SkPaint& paint) {
29 SkPaint p(paint);
30
31 p.setSubpixelText(true);
32 canvas->drawText(text, length, x, y, p);
33}
34
35// This sample demonstrates the cache behavior of bitmap vs. distance field text
36// It renders variously sized text with an animated scale and rotation.
37// Specifically one should:
38// use 'D' to toggle between bitmap and distance field fonts
39// use '2' to toggle between scaling the image by 2x
40// -- this feature boosts the rendering out of the small point-size
41// SDF-text special case (which falls back to bitmap fonts for small points)
42
43class AnimatedTextView : public SampleView {
44public:
45 AnimatedTextView() : fScale(1.0f), fScaleInc(0.1f), fRotation(0.0f), fSizeScale(1) {
46 fCurrentTime = 0;
47 fTimer.start();
48 memset(fTimes, 0, sizeof(fTimes));
49 }
50
51protected:
52 // overrides from SkEventSink
53 bool onQuery(SkEvent* evt) override {
54 if (SampleCode::TitleQ(*evt)) {
55 SampleCode::TitleR(evt, "AnimatedText");
56 return true;
57 }
58
59 SkUnichar uni;
60 if (SampleCode::CharQ(*evt, &uni)) {
61 if ('2' == uni) {
62 if (fSizeScale == 2) {
63 fSizeScale = 1;
64 } else {
65 fSizeScale = 2;
66 }
67 return true;
68 }
69 }
70 return this->INHERITED::onQuery(evt);
71 }
72
73 void onDrawContent(SkCanvas* canvas) override {
74 SkPaint paint;
bungeman13b9c952016-05-12 10:09:30 -070075 paint.setTypeface(SkTypeface::MakeFromFile("/skimages/samplefont.ttf"));
jvanverth629162d2015-11-08 08:07:24 -080076 paint.setAntiAlias(true);
77 paint.setFilterQuality(kMedium_SkFilterQuality);
78
79 SkString outString("fps: ");
80 fTimer.end();
81
82 // TODO: generalize this timing code in utils
83 fTimes[fCurrentTime] = (float)(fTimer.fWall);
84 fCurrentTime = (fCurrentTime + 1) & 0x1f;
85
86 float meanTime = 0.0f;
87 for (int i = 0; i < 32; ++i) {
88 meanTime += fTimes[i];
89 }
90 meanTime /= 32.f;
91 SkScalar fps = 1000.f / meanTime;
92 outString.appendScalar(fps);
93 outString.append(" ms: ");
94 outString.appendScalar(meanTime);
95
96 SkString modeString("Text scale: ");
97 modeString.appendU32(fSizeScale);
98 modeString.append("x");
99
100 fTimer.start();
101
102 canvas->save();
103
104#if SK_SUPPORT_GPU
jvanverth629162d2015-11-08 08:07:24 -0800105 GrContext* grContext = canvas->getGrContext();
106 if (grContext) {
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500107 sk_sp<SkImage> image = grContext->contextPriv().getFontAtlasImage_ForTesting(
108 GrMaskFormat::kA8_GrMaskFormat);
Robert Phillips22f4a1f2016-12-20 08:57:26 -0500109 canvas->drawImageRect(image,
110 SkRect::MakeXYWH(512.0f, 10.0f, 512.0f, 512.0f), &paint);
jvanverth629162d2015-11-08 08:07:24 -0800111 }
112#endif
113 canvas->translate(180, 180);
114 canvas->rotate(fRotation);
115 canvas->scale(fScale, fScale);
116 canvas->translate(-180, -180);
117
118 const char* text = "Hamburgefons";
119 size_t length = strlen(text);
120
121 SkScalar y = SkIntToScalar(0);
122 for (int i = 12; i <= 26; i++) {
123 paint.setTextSize(SkIntToScalar(i*fSizeScale));
124 y += paint.getFontSpacing();
125 DrawTheText(canvas, text, length, SkIntToScalar(110), y, paint);
126 }
127 canvas->restore();
128
129 paint.setTextSize(16);
Cary Clark2a475ea2017-04-28 15:35:12 -0400130// canvas->drawString(outString, 512.f, 540.f, paint);
131 canvas->drawString(modeString, 768.f, 540.f, paint);
jvanverth629162d2015-11-08 08:07:24 -0800132 }
133
134 bool onAnimate(const SkAnimTimer& timer) override {
halcanary9d524f22016-03-29 09:03:52 -0700135 // We add noise to the scale and rotation animations to
jvanverth629162d2015-11-08 08:07:24 -0800136 // keep the font atlas from falling into a steady state
137 fRotation += (1.0f + gRand.nextRangeF(-0.1f, 0.1f));
138 fScale += (fScaleInc + gRand.nextRangeF(-0.025f, 0.025f));
139 if (fScale >= 2.0f) {
140 fScaleInc = -0.1f;
141 } else if (fScale <= 1.0f) {
142 fScaleInc = 0.1f;
143 }
144 return true;
145 }
146
147private:
148 float fScale;
149 float fScaleInc;
150 float fRotation;
151 int fSizeScale;
152
153 WallTimer fTimer;
154 float fTimes[32];
155 int fCurrentTime;
156
157
158 typedef SampleView INHERITED;
159};
160
161//////////////////////////////////////////////////////////////////////////////
162
163static SkView* MyFactory() { return new AnimatedTextView; }
164static SkViewRegister reg(MyFactory);