#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;
bool fRotate;
bool fScale;
bool fRequestGrabImage;
+ bool fMeasureFPS;
+ SkMSec fMeasureFPS_Time;
// The following are for the 'fatbits' drawing
// Latest position of the mouse.
fRotate = false;
fScale = false;
fRequestGrabImage = false;
+ fMeasureFPS = false;
fLCDState = kUnknown_SkTriState;
fAAState = kUnknown_SkTriState;
fFlipAxis = 0;
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) {
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[] = {
this->loadView(fSamples[fCurrIndex]());
return true;
}
+ if (isInvalEvent(evt)) {
+ this->inval(NULL);
+ return true;
+ }
return this->INHERITED::onEvent(evt);
}
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);
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();
if (fZoomLevel) {
title.prependf("{%d} ", fZoomLevel);
}
+
+ if (fMeasureFPS) {
+ title.appendf(" %4d ms", fMeasureFPS_Time);
+ }
+
this->setTitle(title.c_str());
}
///////////////////////////////////////////////////////////////////////////////
+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;
#include "SkEvent.h"
#include "SkKey.h"
+#include "SkView.h"
class SampleCode {
public:
//////////////////////////////////////////////////////////////////////////////
-class SkView;
-
typedef SkView* (*SkViewFactory)();
class SkViewRegister : SkNoncopyable {
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