add 'f' toggle to trigger timer for sample. Must subclass SampleView instead
of SkView to opt-in. Then override onDrawContent() and optionally onDrawBackground.



git-svn-id: http://skia.googlecode.com/svn/trunk@1165 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/samplecode/SampleApp.cpp b/samplecode/SampleApp.cpp
index 8ca0958..78ec56f 100644
--- a/samplecode/SampleApp.cpp
+++ b/samplecode/SampleApp.cpp
@@ -24,10 +24,29 @@
 #define ANIMATING_EVENTTYPE "nextSample"
 #define ANIMATING_DELAY     750
 
+#ifdef SK_DEBUG
+    #define FPS_REPEAT_COUNT    10
+#else
+    #define FPS_REPEAT_COUNT    100
+#endif
+
 #ifdef SK_SUPPORT_GL
     #include "GrGLConfig.h"
 #endif
 
+///////////////
+static const char view_inval_msg[] = "view-inval-msg";
+
+static void postInvalDelay(SkEventSinkID sinkID) {
+    SkEvent* evt = new SkEvent(view_inval_msg);
+    evt->post(sinkID, 10);
+}
+
+static bool isInvalEvent(const SkEvent& evt) {
+    return evt.isType(view_inval_msg);
+}
+//////////////////
+
 SkViewRegister* SkViewRegister::gHead;
 SkViewRegister::SkViewRegister(SkViewFactory fact) : fFact(fact) {
     static bool gOnce;
@@ -265,6 +284,8 @@
     bool fRotate;
     bool fScale;
     bool fRequestGrabImage;
+    bool fMeasureFPS;
+    SkMSec fMeasureFPS_Time;
 
     // The following are for the 'fatbits' drawing
     // Latest position of the mouse.
@@ -402,6 +423,7 @@
     fRotate = false;
     fScale = false;
     fRequestGrabImage = false;
+    fMeasureFPS = false;
     fLCDState = kUnknown_SkTriState;
     fAAState = kUnknown_SkTriState;
     fFlipAxis = 0;
@@ -802,7 +824,7 @@
         r.set(50, 50, 50+100, 50+100);
         bm.scrollRect(&r, dx, dy, &inval);
         paint_rgn(bm, r, inval);
-    }
+    }        
 }
 
 void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
@@ -826,10 +848,21 @@
         kUnknown_SkTriState != fAAState) {
         canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState))->unref();
     }
+
+    SampleView::SetRepeatDraw(child, fMeasureFPS ? FPS_REPEAT_COUNT : 1);
+    if (fMeasureFPS) {
+        fMeasureFPS_Time = SkTime::GetMSecs();
+    }
 }
 
 void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
     canvas->setDrawFilter(NULL);
+
+    if (fMeasureFPS) {
+        fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time;
+        this->updateTitle();
+        postInvalDelay(this->getSinkID());
+    }
 }
 
 static SkBitmap::Config gConfigCycle[] = {
@@ -880,6 +913,10 @@
         this->loadView(fSamples[fCurrIndex]());
         return true;
     }
+    if (isInvalEvent(evt)) {
+        this->inval(NULL);
+        return true;
+    }
     return this->INHERITED::onEvent(evt);
 }
 
@@ -963,20 +1000,38 @@
             this->postAnimatingEvent();
             this->updateTitle();
             return true;
-        case 'f': {
-            const char* title = this->getTitle();
-            if (title[0] == 0) {
-                title = "sampleapp";
-            }
-            SkString name(title);
-            cleanup_for_filename(&name);
-            name.append(".png");
-            if (SkImageEncoder::EncodeFile(name.c_str(), this->getBitmap(),
-                                           SkImageEncoder::kPNG_Type, 100)) {
-                SkDebugf("Created %s\n", name.c_str());
-            }
+        case 'b':
+            fAAState = cycle_tristate(fAAState);
+            this->updateTitle();
+            this->inval(NULL);
+            break;
+        case 'c':
+            fUseClip = !fUseClip;
+            this->inval(NULL);
+            this->updateTitle();
             return true;
-        }
+        case 'd':
+            SkGraphics::SetFontCacheUsed(0);
+            return true;
+        case 'f':
+            fMeasureFPS = !fMeasureFPS;
+            this->inval(NULL);
+            break;
+        case 'g':
+            fRequestGrabImage = true;
+            this->inval(NULL);
+            break;
+        case 'i':
+            this->zoomIn();
+            break;
+        case 'l':
+            fLCDState = cycle_tristate(fLCDState);
+            this->updateTitle();
+            this->inval(NULL);
+            break;
+        case 'o':
+            this->zoomOut();
+            break;
         case 'r':
             fRotate = !fRotate;
             this->inval(NULL);
@@ -987,34 +1042,6 @@
             this->inval(NULL);
             this->updateTitle();
             return true;
-        case 'c':
-            fUseClip = !fUseClip;
-            this->inval(NULL);
-            this->updateTitle();
-            return true;
-        case 'd':
-            SkGraphics::SetFontCacheUsed(0);
-            return true;
-        case 'g':
-            fRequestGrabImage = true;
-            this->inval(NULL);
-            break;
-        case 'l':
-            fLCDState = cycle_tristate(fLCDState);
-            this->updateTitle();
-            this->inval(NULL);
-            break;
-        case 'b':
-            fAAState = cycle_tristate(fAAState);
-            this->updateTitle();
-            this->inval(NULL);
-            break;
-        case 'i':
-            this->zoomIn();
-            break;
-        case 'o':
-            this->zoomOut();
-            break;
         case 'x':
             fFlipAxis ^= kFlipAxis_X;
             this->updateTitle();
@@ -1243,6 +1270,11 @@
     if (fZoomLevel) {
         title.prependf("{%d} ", fZoomLevel);
     }
+    
+    if (fMeasureFPS) {
+        title.appendf(" %4d ms", fMeasureFPS_Time);
+    }
+
     this->setTitle(title.c_str());
 }
 
@@ -1280,6 +1312,40 @@
 
 ///////////////////////////////////////////////////////////////////////////////
 
+static const char repeat_count_tag[] = "sample-set-repeat-count";
+
+void SampleView::SetRepeatDraw(SkView* view, int count) {
+    SkEvent evt(repeat_count_tag);
+    evt.setFast32(count);
+    (void)view->doEvent(evt);
+}
+
+bool SampleView::onEvent(const SkEvent& evt) {
+    if (evt.isType(repeat_count_tag)) {
+        fRepeatCount = evt.getFast32();
+        return true;
+    }
+    return this->INHERITED::onEvent(evt);
+}
+
+bool SampleView::onQuery(SkEvent* evt) {
+    return this->INHERITED::onQuery(evt);
+}
+
+void SampleView::onDraw(SkCanvas* canvas) {
+    this->onDrawBackground(canvas);
+    for (int i = 0; i < fRepeatCount; i++) {
+        SkAutoCanvasRestore acr(canvas, true);
+        this->onDrawContent(canvas);
+    }
+}
+
+void SampleView::onDrawBackground(SkCanvas* canvas) {
+    canvas->drawColor(SK_ColorWHITE);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
 template <typename T> void SkTBSort(T array[], int count) {
     for (int i = 1; i < count - 1; i++) {
         bool didSwap = false;
diff --git a/samplecode/SampleCode.h b/samplecode/SampleCode.h
index 058985a..2eb8bef 100644
--- a/samplecode/SampleCode.h
+++ b/samplecode/SampleCode.h
@@ -3,6 +3,7 @@
 
 #include "SkEvent.h"
 #include "SkKey.h"
+#include "SkView.h"
 
 class SampleCode {
 public:
@@ -25,8 +26,6 @@
 
 //////////////////////////////////////////////////////////////////////////////
 
-class SkView;
-
 typedef SkView* (*SkViewFactory)();
 
 class SkViewRegister : SkNoncopyable {
@@ -45,5 +44,28 @@
     static SkViewRegister* gHead;
 };
 
+///////////////////////////////////////////////////////////////////////////////
+
+class SampleView : public SkView {
+public:
+    SampleView() : fRepeatCount(1) {}
+
+    static void SetRepeatDraw(SkView*, int count);
+
+protected:
+    virtual void onDrawBackground(SkCanvas*);
+    virtual void onDrawContent(SkCanvas*) = 0;
+
+    // overrides
+    virtual bool onEvent(const SkEvent& evt);
+    virtual bool onQuery(SkEvent* evt);
+    virtual void onDraw(SkCanvas*);
+
+private:
+    int fRepeatCount;
+
+    typedef SkView INHERITED;
+};
+
 #endif
 
diff --git a/samplecode/SampleXfermodes.cpp b/samplecode/SampleXfermodes.cpp
index b5361e3..0a3c4c7 100644
--- a/samplecode/SampleXfermodes.cpp
+++ b/samplecode/SampleXfermodes.cpp
@@ -110,7 +110,7 @@
 
 static uint16_t gBG[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
 
-class XfermodesView : public SkView {
+class XfermodesView : public SampleView {
     SkBitmap    fBG;
     SkBitmap    fSrcB, fDstB;
 
@@ -148,15 +148,9 @@
         return this->INHERITED::onQuery(evt);
     }
 
-    void drawBG(SkCanvas* canvas) {
-        canvas->drawColor(SK_ColorWHITE);
-    }
-
-    virtual void onDraw(SkCanvas* canvas) {
+    virtual void onDrawContent(SkCanvas* canvas) {
         canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
 
-        this->drawBG(canvas);
-
         const struct {
             SkXfermode::Mode  fMode;
             const char*         fLabel;
@@ -246,7 +240,7 @@
     }
 
 private:
-    typedef SkView INHERITED;
+    typedef SampleView INHERITED;
 };
 
 //////////////////////////////////////////////////////////////////////////////