add 'f' toggle to trigger timer for sample. Must subclass SampleView instead
authormike@reedtribe.org <mike@reedtribe.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 22 Apr 2011 01:59:09 +0000 (01:59 +0000)
committermike@reedtribe.org <mike@reedtribe.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 22 Apr 2011 01:59:09 +0000 (01:59 +0000)
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

samplecode/SampleApp.cpp
samplecode/SampleCode.h
samplecode/SampleXfermodes.cpp

index 8ca0958..78ec56f 100644 (file)
@@ -24,10 +24,29 @@ extern SkView* create_overview(int, const SkViewFactory[]);
 #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 @@ private:
     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 @@ SampleWindow::SampleWindow(void* hwnd) : INHERITED(hwnd) {
     fRotate = false;
     fScale = false;
     fRequestGrabImage = false;
+    fMeasureFPS = false;
     fLCDState = kUnknown_SkTriState;
     fAAState = kUnknown_SkTriState;
     fFlipAxis = 0;
@@ -802,7 +824,7 @@ void SampleWindow::afterChildren(SkCanvas* orig) {
         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 @@ void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
         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 @@ bool SampleWindow::onEvent(const SkEvent& evt) {
         this->loadView(fSamples[fCurrIndex]());
         return true;
     }
+    if (isInvalEvent(evt)) {
+        this->inval(NULL);
+        return true;
+    }
     return this->INHERITED::onEvent(evt);
 }
 
@@ -963,30 +1000,11 @@ bool SampleWindow::onHandleChar(SkUnichar uni) {
             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());
-            }
-            return true;
-        }
-        case 'r':
-            fRotate = !fRotate;
-            this->inval(NULL);
+        case 'b':
+            fAAState = cycle_tristate(fAAState);
             this->updateTitle();
-            return true;
-        case 's':
-            fScale = !fScale;
             this->inval(NULL);
-            this->updateTitle();
-            return true;
+            break;
         case 'c':
             fUseClip = !fUseClip;
             this->inval(NULL);
@@ -995,26 +1013,35 @@ bool SampleWindow::onHandleChar(SkUnichar uni) {
         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 'b':
-            fAAState = cycle_tristate(fAAState);
-            this->updateTitle();
-            this->inval(NULL);
-            break;
-        case 'i':
-            this->zoomIn();
-            break;
         case 'o':
             this->zoomOut();
             break;
+        case 'r':
+            fRotate = !fRotate;
+            this->inval(NULL);
+            this->updateTitle();
+            return true;
+        case 's':
+            fScale = !fScale;
+            this->inval(NULL);
+            this->updateTitle();
+            return true;
         case 'x':
             fFlipAxis ^= kFlipAxis_X;
             this->updateTitle();
@@ -1243,6 +1270,11 @@ void SampleWindow::updateTitle() {
     if (fZoomLevel) {
         title.prependf("{%d} ", fZoomLevel);
     }
+    
+    if (fMeasureFPS) {
+        title.appendf(" %4d ms", fMeasureFPS_Time);
+    }
+
     this->setTitle(title.c_str());
 }
 
@@ -1280,6 +1312,40 @@ void SampleWindow::onSizeChange() {
 
 ///////////////////////////////////////////////////////////////////////////////
 
+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;
index 058985a..2eb8bef 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "SkEvent.h"
 #include "SkKey.h"
+#include "SkView.h"
 
 class SampleCode {
 public:
@@ -25,8 +26,6 @@ public:
 
 //////////////////////////////////////////////////////////////////////////////
 
-class SkView;
-
 typedef SkView* (*SkViewFactory)();
 
 class SkViewRegister : SkNoncopyable {
@@ -45,5 +44,28 @@ private:
     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
 
index b5361e3..0a3c4c7 100644 (file)
@@ -110,7 +110,7 @@ static void make_bitmaps(int w, int h, SkBitmap* src, SkBitmap* dst) {
 
 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 @@ protected:
         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 @@ protected:
     }
 
 private:
-    typedef SkView INHERITED;
+    typedef SampleView INHERITED;
 };
 
 //////////////////////////////////////////////////////////////////////////////