jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 1 | /* |
| 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 Malita | ab244f0 | 2017-05-03 19:16:58 +0000 | [diff] [blame] | 14 | #include "SkImage.h" |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 15 | #include "SkRandom.h" |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 16 | #include "SkTime.h" |
| 17 | #include "SkTypeface.h" |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 18 | #include "Timer.h" |
| 19 | |
kkinnunen | 83a5d42 | 2015-11-17 09:38:05 -0800 | [diff] [blame] | 20 | #if SK_SUPPORT_GPU |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 21 | #include "GrContext.h" |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 22 | #include "GrContextPriv.h" |
kkinnunen | 83a5d42 | 2015-11-17 09:38:05 -0800 | [diff] [blame] | 23 | #endif |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 24 | |
| 25 | SkRandom gRand; |
| 26 | |
| 27 | static 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 | |
| 43 | class AnimatedTextView : public SampleView { |
| 44 | public: |
| 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 | |
| 51 | protected: |
| 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; |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 75 | paint.setTypeface(SkTypeface::MakeFromFile("/skimages/samplefont.ttf")); |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 76 | 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 |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 105 | GrContext* grContext = canvas->getGrContext(); |
| 106 | if (grContext) { |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 107 | sk_sp<SkImage> image = grContext->contextPriv().getFontAtlasImage_ForTesting( |
| 108 | GrMaskFormat::kA8_GrMaskFormat); |
Robert Phillips | 22f4a1f | 2016-12-20 08:57:26 -0500 | [diff] [blame] | 109 | canvas->drawImageRect(image, |
| 110 | SkRect::MakeXYWH(512.0f, 10.0f, 512.0f, 512.0f), &paint); |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 111 | } |
| 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 Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 130 | // canvas->drawString(outString, 512.f, 540.f, paint); |
| 131 | canvas->drawString(modeString, 768.f, 540.f, paint); |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | bool onAnimate(const SkAnimTimer& timer) override { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 135 | // We add noise to the scale and rotation animations to |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 136 | // 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 | |
| 147 | private: |
| 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 | |
| 163 | static SkView* MyFactory() { return new AnimatedTextView; } |
| 164 | static SkViewRegister reg(MyFactory); |