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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkColorFilter.h" |
| 10 | #include "include/core/SkColorPriv.h" |
| 11 | #include "include/core/SkImage.h" |
| 12 | #include "include/core/SkTime.h" |
| 13 | #include "include/core/SkTypeface.h" |
| 14 | #include "include/utils/SkRandom.h" |
| 15 | #include "samplecode/Sample.h" |
| 16 | #include "src/utils/SkUTF.h" |
| 17 | #include "tools/timer/Timer.h" |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 18 | |
kkinnunen | 83a5d42 | 2015-11-17 09:38:05 -0800 | [diff] [blame] | 19 | #if SK_SUPPORT_GPU |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/gpu/GrContext.h" |
| 21 | #include "src/gpu/GrContextPriv.h" |
kkinnunen | 83a5d42 | 2015-11-17 09:38:05 -0800 | [diff] [blame] | 22 | #endif |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 23 | |
| 24 | SkRandom gRand; |
| 25 | |
| 26 | static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkScalar x, SkScalar y, |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 27 | const SkFont& font, const SkPaint& paint) { |
| 28 | SkFont f(font); |
| 29 | f.setSubpixel(true); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 30 | canvas->drawSimpleText(text, length, SkTextEncoding::kUTF8, x, y, f, paint); |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | // This sample demonstrates the cache behavior of bitmap vs. distance field text |
| 34 | // It renders variously sized text with an animated scale and rotation. |
| 35 | // Specifically one should: |
| 36 | // use 'D' to toggle between bitmap and distance field fonts |
| 37 | // use '2' to toggle between scaling the image by 2x |
| 38 | // -- this feature boosts the rendering out of the small point-size |
| 39 | // SDF-text special case (which falls back to bitmap fonts for small points) |
| 40 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 41 | class AnimatedTextView : public Sample { |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 42 | public: |
| 43 | AnimatedTextView() : fScale(1.0f), fScaleInc(0.1f), fRotation(0.0f), fSizeScale(1) { |
| 44 | fCurrentTime = 0; |
| 45 | fTimer.start(); |
| 46 | memset(fTimes, 0, sizeof(fTimes)); |
| 47 | } |
| 48 | |
| 49 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 50 | SkString name() override { return SkString("AnimatedText"); } |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 51 | |
Hal Canary | 6cc65e1 | 2019-07-03 15:53:04 -0400 | [diff] [blame] | 52 | bool onChar(SkUnichar uni) override { |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 53 | if ('2' == uni) { |
| 54 | if (fSizeScale == 2) { |
| 55 | fSizeScale = 1; |
| 56 | } else { |
| 57 | fSizeScale = 2; |
| 58 | } |
| 59 | return true; |
| 60 | } |
Hal Canary | 6cc65e1 | 2019-07-03 15:53:04 -0400 | [diff] [blame] | 61 | return false; |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void onDrawContent(SkCanvas* canvas) override { |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 65 | SkFont font(SkTypeface::MakeFromFile("/skimages/samplefont.ttf")); |
| 66 | |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 67 | SkPaint paint; |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 68 | paint.setAntiAlias(true); |
| 69 | paint.setFilterQuality(kMedium_SkFilterQuality); |
| 70 | |
| 71 | SkString outString("fps: "); |
| 72 | fTimer.end(); |
| 73 | |
| 74 | // TODO: generalize this timing code in utils |
| 75 | fTimes[fCurrentTime] = (float)(fTimer.fWall); |
| 76 | fCurrentTime = (fCurrentTime + 1) & 0x1f; |
| 77 | |
| 78 | float meanTime = 0.0f; |
| 79 | for (int i = 0; i < 32; ++i) { |
| 80 | meanTime += fTimes[i]; |
| 81 | } |
| 82 | meanTime /= 32.f; |
| 83 | SkScalar fps = 1000.f / meanTime; |
| 84 | outString.appendScalar(fps); |
| 85 | outString.append(" ms: "); |
| 86 | outString.appendScalar(meanTime); |
| 87 | |
| 88 | SkString modeString("Text scale: "); |
| 89 | modeString.appendU32(fSizeScale); |
| 90 | modeString.append("x"); |
| 91 | |
| 92 | fTimer.start(); |
| 93 | |
| 94 | canvas->save(); |
| 95 | |
| 96 | #if SK_SUPPORT_GPU |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 97 | GrContext* grContext = canvas->getGrContext(); |
| 98 | if (grContext) { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 99 | sk_sp<SkImage> image = grContext->priv().testingOnly_getFontAtlasImage( |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 100 | GrMaskFormat::kA8_GrMaskFormat); |
Robert Phillips | 22f4a1f | 2016-12-20 08:57:26 -0500 | [diff] [blame] | 101 | canvas->drawImageRect(image, |
| 102 | SkRect::MakeXYWH(512.0f, 10.0f, 512.0f, 512.0f), &paint); |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 103 | } |
| 104 | #endif |
| 105 | canvas->translate(180, 180); |
| 106 | canvas->rotate(fRotation); |
| 107 | canvas->scale(fScale, fScale); |
| 108 | canvas->translate(-180, -180); |
| 109 | |
| 110 | const char* text = "Hamburgefons"; |
| 111 | size_t length = strlen(text); |
| 112 | |
| 113 | SkScalar y = SkIntToScalar(0); |
| 114 | for (int i = 12; i <= 26; i++) { |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 115 | font.setSize(SkIntToScalar(i*fSizeScale)); |
| 116 | y += font.getSpacing(); |
| 117 | DrawTheText(canvas, text, length, SkIntToScalar(110), y, font, paint); |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 118 | } |
| 119 | canvas->restore(); |
| 120 | |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 121 | font.setSize(16); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 122 | // canvas->drawString(outString, 512.f, 540.f, paint); |
Hal Canary | 89a644b | 2019-01-07 09:36:09 -0500 | [diff] [blame] | 123 | canvas->drawString(modeString, 768.f, 540.f, font, paint); |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Mike Klein | cd5104e | 2019-03-20 11:55:08 -0500 | [diff] [blame] | 126 | bool onAnimate(const AnimTimer& timer) override { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 127 | // We add noise to the scale and rotation animations to |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 128 | // keep the font atlas from falling into a steady state |
| 129 | fRotation += (1.0f + gRand.nextRangeF(-0.1f, 0.1f)); |
| 130 | fScale += (fScaleInc + gRand.nextRangeF(-0.025f, 0.025f)); |
| 131 | if (fScale >= 2.0f) { |
| 132 | fScaleInc = -0.1f; |
| 133 | } else if (fScale <= 1.0f) { |
| 134 | fScaleInc = 0.1f; |
| 135 | } |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | private: |
| 140 | float fScale; |
| 141 | float fScaleInc; |
| 142 | float fRotation; |
| 143 | int fSizeScale; |
| 144 | |
| 145 | WallTimer fTimer; |
| 146 | float fTimes[32]; |
| 147 | int fCurrentTime; |
| 148 | |
| 149 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 150 | typedef Sample INHERITED; |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | ////////////////////////////////////////////////////////////////////////////// |
| 154 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 155 | DEF_SAMPLE( return new AnimatedTextView(); ) |