Add text animation sample; tweak DrawShip sample
authorjvanverth <jvanverth@google.com>
Fri, 6 Nov 2015 22:20:03 +0000 (14:20 -0800)
committerCommit bot <commit-bot@chromium.org>
Fri, 6 Nov 2015 22:20:04 +0000 (14:20 -0800)
Review URL: https://codereview.chromium.org/1410663005

gyp/SampleApp.gyp
include/gpu/GrContext.h
samplecode/SampleAnimatedText.cpp [new file with mode: 0755]
samplecode/SampleApp.cpp
samplecode/SampleShip.cpp
src/gpu/GrTest.cpp

index bbbd0717d206322cb97cde81b9898a66c2611dc4..71c511503e9ac371b809921eb0ddf3c37eec44b4 100644 (file)
@@ -37,6 +37,7 @@
         '../samplecode/SampleAARects.cpp',
         '../samplecode/SampleAARectModes.cpp',
         '../samplecode/SampleAll.cpp',
+       '../samplecode/SampleAnimatedText.cpp',
         '../samplecode/SampleAnimator.cpp',
         '../samplecode/SampleAnimBlur.cpp',
         '../samplecode/SampleApp.cpp',
index e051c7c76f830c71401eef91252826c9a1f79a0c..2d77a0f449fb5305592c6b44d1c05aad9b26075f 100644 (file)
@@ -349,6 +349,10 @@ public:
     /** Enumerates all cached GPU resources and dumps their memory to traceMemoryDump. */
     void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
 
+    /** Draw font cache texture to render target */
+    void drawFontCache(const SkRect& rect, GrMaskFormat format, const SkPaint& paint,
+                       GrRenderTarget* target);
+
 private:
     GrGpu*                          fGpu;
     const GrCaps*                   fCaps;
diff --git a/samplecode/SampleAnimatedText.cpp b/samplecode/SampleAnimatedText.cpp
new file mode 100755 (executable)
index 0000000..e05c4f3
--- /dev/null
@@ -0,0 +1,161 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SampleCode.h"
+#include "SkView.h"
+#include "SkCanvas.h"
+#include "SkUtils.h"
+#include "SkColorPriv.h"
+#include "SkColorFilter.h"
+#include "SkGpuDevice.h"
+#include "SkRandom.h"
+#include "SkSystemEventTypes.h"
+#include "SkTime.h"
+#include "SkTypeface.h"
+#include "SkXfermode.h"
+#include "Timer.h"
+
+#include "GrContext.h"
+
+SkRandom gRand;
+
+static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkScalar x, SkScalar y,
+                        const SkPaint& paint) {
+    SkPaint p(paint);
+
+    p.setSubpixelText(true);
+    canvas->drawText(text, length, x, y, p);
+}
+
+// This sample demonstrates the cache behavior of bitmap vs. distance field text
+// It renders variously sized text with an animated scale and rotation.
+// Specifically one should:
+//   use 'D' to toggle between bitmap and distance field fonts
+//   use '2' to toggle between scaling the image by 2x
+//            -- this feature boosts the rendering out of the small point-size
+//               SDF-text special case (which falls back to bitmap fonts for small points)
+
+class AnimatedTextView : public SampleView {
+public:
+    AnimatedTextView() : fScale(1.0f), fScaleInc(0.1f), fRotation(0.0f), fSizeScale(1) {
+        fCurrentTime = 0;
+        fTimer.start();
+        memset(fTimes, 0, sizeof(fTimes));
+    }
+
+protected:
+    // overrides from SkEventSink
+    bool onQuery(SkEvent* evt) override {
+        if (SampleCode::TitleQ(*evt)) {
+            SampleCode::TitleR(evt, "AnimatedText");
+            return true;
+        }
+
+        SkUnichar uni;
+        if (SampleCode::CharQ(*evt, &uni)) {
+            if ('2' == uni) {
+                if (fSizeScale == 2) {
+                    fSizeScale = 1;
+                } else {
+                    fSizeScale = 2;
+                }
+                return true;
+            }
+        }
+        return this->INHERITED::onQuery(evt);
+    }
+
+    void onDrawContent(SkCanvas* canvas) override {
+        SkPaint paint;
+        SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromFile("/skimages/samplefont.ttf")));
+        paint.setAntiAlias(true);
+        paint.setFilterQuality(kMedium_SkFilterQuality);
+
+        SkString outString("fps: ");
+        fTimer.end();
+
+        // TODO: generalize this timing code in utils
+        fTimes[fCurrentTime] = (float)(fTimer.fWall);
+        fCurrentTime = (fCurrentTime + 1) & 0x1f;
+
+        float meanTime = 0.0f;
+        for (int i = 0; i < 32; ++i) {
+            meanTime += fTimes[i];
+        }
+        meanTime /= 32.f;
+        SkScalar fps = 1000.f / meanTime;
+        outString.appendScalar(fps);
+        outString.append(" ms: ");
+        outString.appendScalar(meanTime);
+
+        SkString modeString("Text scale: ");
+        modeString.appendU32(fSizeScale);
+        modeString.append("x");
+
+        fTimer.start();
+
+        canvas->save();
+
+        SkBaseDevice* device = canvas->getDevice_just_for_deprecated_compatibility_testing();
+        GrContext* grContext = canvas->getGrContext();
+        if (grContext) {
+            grContext->drawFontCache(SkRect::MakeXYWH(512, 10, 512, 512), kA8_GrMaskFormat, paint,
+                                     reinterpret_cast<SkGpuDevice*>(device)->accessRenderTarget());
+        }
+
+        canvas->translate(180, 180);
+        canvas->rotate(fRotation);
+        canvas->scale(fScale, fScale);
+        canvas->translate(-180, -180);
+
+        const char* text = "Hamburgefons";
+        size_t length = strlen(text);
+
+        SkScalar y = SkIntToScalar(0);
+        for (int i = 12; i <= 26; i++) {
+            paint.setTextSize(SkIntToScalar(i*fSizeScale));
+            y += paint.getFontSpacing();
+            DrawTheText(canvas, text, length, SkIntToScalar(110), y, paint);
+        }
+        canvas->restore();
+
+        paint.setTextSize(16);
+        canvas->drawText(outString.c_str(), outString.size(), 512.f, 540.f, paint);
+        canvas->drawText(modeString.c_str(), modeString.size(), 768.f, 540.f, paint);
+    }
+
+    bool onAnimate(const SkAnimTimer& timer) override {
+        // We add noise to the scale and rotation animations to 
+        // keep the font atlas from falling into a steady state
+        fRotation += (1.0f + gRand.nextRangeF(-0.1f, 0.1f));
+        fScale += (fScaleInc + gRand.nextRangeF(-0.025f, 0.025f));
+        if (fScale >= 2.0f) {
+            fScaleInc = -0.1f;
+        } else if (fScale <= 1.0f) {
+            fScaleInc = 0.1f;
+        }
+        return true;
+    }
+
+private:
+    float fScale;
+    float fScaleInc;
+    float fRotation;
+    int   fSizeScale;
+
+    WallTimer   fTimer;
+    float       fTimes[32];
+    int         fCurrentTime;
+
+
+    typedef SampleView INHERITED;
+};
+
+//////////////////////////////////////////////////////////////////////////////
+
+static SkView* MyFactory() { return new AnimatedTextView; }
+static SkViewRegister reg(MyFactory);
index fb261b092cd61c64c621b95fa0f17df34f46e5c3..df83d4dea5dcfd443fd22e251ff52586e5143ebc 100644 (file)
@@ -1847,6 +1847,10 @@ void SampleWindow::toggleFPS() {
 }
 
 void SampleWindow::toggleDistanceFieldFonts() {
+    // reset backend
+    fDevManager->tearDownBackend(this);
+    fDevManager->setUpBackend(this, fMSAASampleCount);
+
     SkSurfaceProps props = this->getSurfaceProps();
     uint32_t flags = props.flags() ^ SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
     this->setSurfaceProps(SkSurfaceProps(flags, props.pixelGeometry()));
index caaad046d91824eaf4c129dff115316558fd22c4..fddf0235baf7836de550e0f313b70f155427f100 100644 (file)
@@ -70,7 +70,7 @@ public:
                 fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f,
                                                    SkIntToScalar(fAtlas->width()),
                                                    SkIntToScalar(fAtlas->height()));
-                fXform[currIndex] = SkRSXform::MakeFromRadians(2.0f, SK_ScalarPI*0.5f,
+                fXform[currIndex] = SkRSXform::MakeFromRadians(0.1f, SK_ScalarPI*0.5f,
                                                                xPos, yPos, anchorX, anchorY);
                 currIndex++;
             }
@@ -78,7 +78,7 @@ public:
         fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f,
                                            SkIntToScalar(fAtlas->width()),
                                            SkIntToScalar(fAtlas->height()));
-        fXform[currIndex] = SkRSXform::MakeFromRadians(2.0f, SK_ScalarPI*0.5f,
+        fXform[currIndex] = SkRSXform::MakeFromRadians(0.5f, SK_ScalarPI*0.5f,
                                                        kWidth*0.5f, kHeight*0.5f, anchorX, anchorY);
         
         fCurrentTime = 0;
@@ -147,7 +147,10 @@ protected:
         }
         
         fProc(canvas, fAtlas, fXform, fTex, nullptr, kGrid*kGrid+1, nullptr, &paint);
-        canvas->drawText(outString.c_str(), outString.size(), 100.f, 100.f, paint);
+        paint.setColor(SK_ColorBLACK);
+        canvas->drawRect(SkRect::MakeXYWH(0, 0, 200, 24), paint);
+        paint.setColor(SK_ColorWHITE);
+        canvas->drawText(outString.c_str(), outString.size(), 5, 15, paint);
 
         this->inval(nullptr);
     }
index 4adc3f7b7b770585a34e92729b3efe509c4c60e9..7a44942a16216f253a65154e072c92b1dfbb1037 100644 (file)
 #include "GrBatchAtlas.h"
 #include "GrBatchFontCache.h"
 #include "GrContextOptions.h"
+#include "GrDrawContext.h"
 #include "GrDrawingManager.h"
 #include "GrGpuResourceCacheAccess.h"
 #include "GrResourceCache.h"
 #include "GrTextBlobCache.h"
+#include "SkGrPriv.h"
 #include "SkString.h"
 
 namespace GrTest {
@@ -97,6 +99,35 @@ void GrContext::printGpuStats() const {
     SkDebugf("%s", out.c_str());
 }
 
+void GrContext::drawFontCache(const SkRect& rect, GrMaskFormat format, const SkPaint& paint,
+                              GrRenderTarget* target) {
+    GrBatchFontCache* cache = this->getBatchFontCache();
+
+    GrTexture* atlas = cache->getTexture(format);
+
+    SkAutoTUnref<GrDrawContext> drawContext(this->drawContext(target));
+    // TODO: add drawContext method to encapsulate this.
+
+    GrPaint grPaint;
+    SkMatrix mat;
+    mat.reset();
+    if (!SkPaintToGrPaint(this, paint, mat, &grPaint)) {
+        return;
+    }
+    SkMatrix textureMat;
+    textureMat.reset();
+    // TODO: use setScaleTranslate()
+    textureMat[SkMatrix::kMScaleX] = 1.0f/rect.width();
+    textureMat[SkMatrix::kMScaleY] = 1.0f/rect.height();
+    textureMat[SkMatrix::kMTransX] = -rect.fLeft/rect.width();
+    textureMat[SkMatrix::kMTransY] = -rect.fTop/rect.height();
+
+    grPaint.addColorTextureProcessor(atlas, textureMat);
+
+    GrClip clip;
+    drawContext->drawRect(clip, grPaint, mat, rect);
+}
+
 #if GR_GPU_STATS
 void GrGpu::Stats::dump(SkString* out) {
     out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);