| OLD | NEW |
| (Empty) |
| 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" | |
| 14 #include "SkGpuDevice.h" | |
| 15 #include "SkRandom.h" | |
| 16 #include "SkSystemEventTypes.h" | |
| 17 #include "SkTime.h" | |
| 18 #include "SkTypeface.h" | |
| 19 #include "SkXfermode.h" | |
| 20 #include "Timer.h" | |
| 21 | |
| 22 #include "GrContext.h" | |
| 23 | |
| 24 SkRandom gRand; | |
| 25 | |
| 26 static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkSc
alar x, SkScalar y, | |
| 27 const SkPaint& paint) { | |
| 28 SkPaint p(paint); | |
| 29 | |
| 30 p.setSubpixelText(true); | |
| 31 canvas->drawText(text, length, x, y, p); | |
| 32 } | |
| 33 | |
| 34 // This sample demonstrates the cache behavior of bitmap vs. distance field text | |
| 35 // It renders variously sized text with an animated scale and rotation. | |
| 36 // Specifically one should: | |
| 37 // use 'D' to toggle between bitmap and distance field fonts | |
| 38 // use '2' to toggle between scaling the image by 2x | |
| 39 // -- this feature boosts the rendering out of the small point-size | |
| 40 // SDF-text special case (which falls back to bitmap fonts for sma
ll points) | |
| 41 | |
| 42 class AnimatedTextView : public SampleView { | |
| 43 public: | |
| 44 AnimatedTextView() : fScale(1.0f), fScaleInc(0.1f), fRotation(0.0f), fSizeSc
ale(1) { | |
| 45 fCurrentTime = 0; | |
| 46 fTimer.start(); | |
| 47 memset(fTimes, 0, sizeof(fTimes)); | |
| 48 } | |
| 49 | |
| 50 protected: | |
| 51 // overrides from SkEventSink | |
| 52 bool onQuery(SkEvent* evt) override { | |
| 53 if (SampleCode::TitleQ(*evt)) { | |
| 54 SampleCode::TitleR(evt, "AnimatedText"); | |
| 55 return true; | |
| 56 } | |
| 57 | |
| 58 SkUnichar uni; | |
| 59 if (SampleCode::CharQ(*evt, &uni)) { | |
| 60 if ('2' == uni) { | |
| 61 if (fSizeScale == 2) { | |
| 62 fSizeScale = 1; | |
| 63 } else { | |
| 64 fSizeScale = 2; | |
| 65 } | |
| 66 return true; | |
| 67 } | |
| 68 } | |
| 69 return this->INHERITED::onQuery(evt); | |
| 70 } | |
| 71 | |
| 72 void onDrawContent(SkCanvas* canvas) override { | |
| 73 SkPaint paint; | |
| 74 SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromFile("/skimages/samp
lefont.ttf"))); | |
| 75 paint.setAntiAlias(true); | |
| 76 paint.setFilterQuality(kMedium_SkFilterQuality); | |
| 77 | |
| 78 SkString outString("fps: "); | |
| 79 fTimer.end(); | |
| 80 | |
| 81 // TODO: generalize this timing code in utils | |
| 82 fTimes[fCurrentTime] = (float)(fTimer.fWall); | |
| 83 fCurrentTime = (fCurrentTime + 1) & 0x1f; | |
| 84 | |
| 85 float meanTime = 0.0f; | |
| 86 for (int i = 0; i < 32; ++i) { | |
| 87 meanTime += fTimes[i]; | |
| 88 } | |
| 89 meanTime /= 32.f; | |
| 90 SkScalar fps = 1000.f / meanTime; | |
| 91 outString.appendScalar(fps); | |
| 92 outString.append(" ms: "); | |
| 93 outString.appendScalar(meanTime); | |
| 94 | |
| 95 SkString modeString("Text scale: "); | |
| 96 modeString.appendU32(fSizeScale); | |
| 97 modeString.append("x"); | |
| 98 | |
| 99 fTimer.start(); | |
| 100 | |
| 101 canvas->save(); | |
| 102 | |
| 103 SkBaseDevice* device = canvas->getDevice_just_for_deprecated_compatibili
ty_testing(); | |
| 104 GrContext* grContext = canvas->getGrContext(); | |
| 105 if (grContext) { | |
| 106 grContext->drawFontCache(SkRect::MakeXYWH(512, 10, 512, 512), kA8_Gr
MaskFormat, paint, | |
| 107 reinterpret_cast<SkGpuDevice*>(device)->acc
essRenderTarget()); | |
| 108 } | |
| 109 | |
| 110 canvas->translate(180, 180); | |
| 111 canvas->rotate(fRotation); | |
| 112 canvas->scale(fScale, fScale); | |
| 113 canvas->translate(-180, -180); | |
| 114 | |
| 115 const char* text = "Hamburgefons"; | |
| 116 size_t length = strlen(text); | |
| 117 | |
| 118 SkScalar y = SkIntToScalar(0); | |
| 119 for (int i = 12; i <= 26; i++) { | |
| 120 paint.setTextSize(SkIntToScalar(i*fSizeScale)); | |
| 121 y += paint.getFontSpacing(); | |
| 122 DrawTheText(canvas, text, length, SkIntToScalar(110), y, paint); | |
| 123 } | |
| 124 canvas->restore(); | |
| 125 | |
| 126 paint.setTextSize(16); | |
| 127 canvas->drawText(outString.c_str(), outString.size(), 512.f, 540.f, pain
t); | |
| 128 canvas->drawText(modeString.c_str(), modeString.size(), 768.f, 540.f, pa
int); | |
| 129 } | |
| 130 | |
| 131 bool onAnimate(const SkAnimTimer& timer) override { | |
| 132 // We add noise to the scale and rotation animations to | |
| 133 // keep the font atlas from falling into a steady state | |
| 134 fRotation += (1.0f + gRand.nextRangeF(-0.1f, 0.1f)); | |
| 135 fScale += (fScaleInc + gRand.nextRangeF(-0.025f, 0.025f)); | |
| 136 if (fScale >= 2.0f) { | |
| 137 fScaleInc = -0.1f; | |
| 138 } else if (fScale <= 1.0f) { | |
| 139 fScaleInc = 0.1f; | |
| 140 } | |
| 141 return true; | |
| 142 } | |
| 143 | |
| 144 private: | |
| 145 float fScale; | |
| 146 float fScaleInc; | |
| 147 float fRotation; | |
| 148 int fSizeScale; | |
| 149 | |
| 150 WallTimer fTimer; | |
| 151 float fTimes[32]; | |
| 152 int fCurrentTime; | |
| 153 | |
| 154 | |
| 155 typedef SampleView INHERITED; | |
| 156 }; | |
| 157 | |
| 158 ////////////////////////////////////////////////////////////////////////////// | |
| 159 | |
| 160 static SkView* MyFactory() { return new AnimatedTextView; } | |
| 161 static SkViewRegister reg(MyFactory); | |
| OLD | NEW |