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