add SkAnimTimer, SPACE = pause/resume, ESP = stop

BUG=skia:

Review URL: https://codereview.chromium.org/894083003
This commit is contained in:
reed 2015-02-02 12:55:02 -08:00 committed by Commit bot
parent a669bc7a7a
commit 76113a9b77
16 changed files with 241 additions and 147 deletions

139
gm/SkAnimTimer.h Normal file
View File

@ -0,0 +1,139 @@
/*
* 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 "SkTime.h"
#ifndef SkAnimTimer_DEFINED
#define SkAnimTimer_DEFINED
/**
* Class to track a "timer". It supports 3 states: stopped, paused, running.
*
* The caller must call updateTime() to resync with the clock (typically just before
* using the timer). Forcing the caller to do this ensures that the timer's return values
* are consistent if called repeatedly, as they only reflect the time since the last
* calle to updateTimer().
*/
class SkAnimTimer {
public:
enum State {
kStopped_State,
kPaused_State,
kRunning_State
};
/**
* Class begins in the "stopped" state.
*/
SkAnimTimer() : fBaseTime(0), fCurrTime(0), fState(kStopped_State) {}
bool isStopped() const { return kStopped_State == fState; }
bool isRunning() const { return kRunning_State == fState; }
bool isPaused() const { return kPaused_State == fState; }
/**
* Stops the timer, and resets it, such that the next call to run or togglePauseResume
* will begin at time 0.
*/
void stop() {
this->setState(kStopped_State);
}
/**
* If the timer is paused or stopped, it will resume (or start if it was stopped).
*/
void run() {
this->setState(kRunning_State);
}
/**
* If the timer is stopped, this has no effect, else it toggles between paused and running.
*/
void togglePauseResume() {
if (kRunning_State == fState) {
this->setState(kPaused_State);
} else {
this->setState(kRunning_State);
}
}
/**
* Call this each time you want to sample the clock for the timer. This is NOT done
* automatically, so that repeated calls to msec() or secs() will always return the
* same value.
*
* This may safely be called with the timer in any state.
*/
void updateTime() {
if (kRunning_State == fState) {
fCurrTime = SkTime::GetMSecs();
}
}
/**
* Return the time in milliseconds the timer has been in the running state.
* Returns 0 if the timer is stopped.
*/
SkMSec msec() const { return fCurrTime - fBaseTime; }
/**
* Return the time in seconds the timer has been in the running state.
* Returns 0 if the timer is stopped.
*/
double secs() const {
return this->msec() * 0.001;
}
/**
* Return the time in seconds the timer has been in the running state,
* scaled by "speed" and (if not zero) mod by period.
* Returns 0 if the timer is stopped.
*/
SkScalar scaled(SkScalar speed, SkScalar period = 0) const {
double value = this->secs() * speed;
if (period) {
value = ::fmod(value, SkScalarToDouble(period));
}
return SkDoubleToScalar(value);
}
private:
SkMSec fBaseTime;
SkMSec fCurrTime;
State fState;
void setState(State newState) {
switch (newState) {
case kStopped_State:
fBaseTime = fCurrTime = 0;
fState = kStopped_State;
break;
case kPaused_State:
if (kRunning_State == fState) {
fState = kPaused_State;
} // else stay stopped or paused
break;
case kRunning_State:
switch (fState) {
case kStopped_State:
fBaseTime = fCurrTime = SkTime::GetMSecs();
break;
case kPaused_State: {// they want "resume"
SkMSec now = SkTime::GetMSecs();
fBaseTime += now - fCurrTime;
fCurrTime = now;
} break;
case kRunning_State:
break;
}
fState = kRunning_State;
break;
}
}
};
#endif

View File

@ -6,6 +6,7 @@
*/ */
#include "gm.h" #include "gm.h"
#include "SkAnimTimer.h"
#include "SkCanvas.h" #include "SkCanvas.h"
#include "SkRandom.h" #include "SkRandom.h"
@ -49,8 +50,8 @@ protected:
} }
} }
bool onAnimatePulse(SkMSec curr, SkMSec prev) SK_OVERRIDE { bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
fRotate = SkDoubleToScalar(fmod(curr * 0.001, 360)); fRotate = timer.scaled(1, 360);
return true; return true;
} }

View File

@ -51,8 +51,8 @@ void GM::setBGColor(SkColor color) {
fBGColor = color; fBGColor = color;
} }
bool GM::animatePulse(SkMSec curr, SkMSec prev) { bool GM::animate(const SkAnimTimer& timer) {
return this->onAnimatePulse(curr, prev); return this->onAnimate(timer);
} }
///////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -16,6 +16,8 @@
#include "SkTRegistry.h" #include "SkTRegistry.h"
#include "sk_tool_utils.h" #include "sk_tool_utils.h"
class SkAnimTimer;
#if SK_SUPPORT_GPU #if SK_SUPPORT_GPU
#include "GrContext.h" #include "GrContext.h"
#endif #endif
@ -92,7 +94,7 @@ namespace skiagm {
fStarterMatrix = matrix; fStarterMatrix = matrix;
} }
bool animatePulse(SkMSec curr, SkMSec prev); bool animate(const SkAnimTimer&);
protected: protected:
/** draws a standard message that the GM is only intended to be used with the GPU.*/ /** draws a standard message that the GM is only intended to be used with the GPU.*/
@ -103,7 +105,7 @@ namespace skiagm {
virtual SkISize onISize() = 0; virtual SkISize onISize() = 0;
virtual SkString onShortName() = 0; virtual SkString onShortName() = 0;
virtual bool onAnimatePulse(SkMSec curr, SkMSec prev) { return false; } virtual bool onAnimate(const SkAnimTimer&) { return false; }
virtual SkMatrix onGetInitialTransform() const { return SkMatrix::I(); } virtual SkMatrix onGetInitialTransform() const { return SkMatrix::I(); }
private: private:

View File

@ -70,8 +70,8 @@ protected:
fGM->drawBackground(canvas); fGM->drawBackground(canvas);
} }
bool onAnimatePulse(SkMSec curr, SkMSec prev) SK_OVERRIDE { bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
return fGM->animatePulse(curr, prev); return fGM->animate(timer);
} }
private: private:

View File

@ -1,21 +1,22 @@
/* /*
* Copyright 2012 Google Inc. * Copyright 2012 Google Inc.
* *
* Use of this source code is governed by a BSD-style license that can be * Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file. * found in the LICENSE file.
*/ */
#include "SampleCode.h" #include "SampleCode.h"
#include "SkAnimTimer.h"
#include "SkBlurMaskFilter.h" #include "SkBlurMaskFilter.h"
#include "SkColorPriv.h" #include "SkColorPriv.h"
#include "SkCanvas.h" #include "SkCanvas.h"
#include "SkRandom.h" #include "SkRandom.h"
SkScalar get_anim_sin(SkMSec time, SkScalar amplitude, SkScalar periodInSec, SkScalar phaseInSec) { SkScalar get_anim_sin(double secs, SkScalar amplitude, SkScalar periodInSec, SkScalar phaseInSec) {
if (!periodInSec) { if (!periodInSec) {
return 0; return 0;
} }
double t = (double)time / 1000.0 + phaseInSec; double t = secs + phaseInSec;
t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec; t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec;
amplitude = SK_ScalarHalf * amplitude; amplitude = SK_ScalarHalf * amplitude;
return amplitude * SkDoubleToScalar(sin(t)) + amplitude; return amplitude * SkDoubleToScalar(sin(t)) + amplitude;
@ -58,9 +59,9 @@ protected:
} }
} }
bool onAnimatePulse(SkMSec curr, SkMSec prev) SK_OVERRIDE { bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
fBlurSigma = get_anim_sin(curr, 100, 4, 5); fBlurSigma = get_anim_sin(timer.secs(), 100, 4, 5);
fCircleRadius = 3 + get_anim_sin(curr, 150, 25, 3); fCircleRadius = 3 + get_anim_sin(timer.secs(), 150, 25, 3);
return true; return true;
} }

View File

@ -11,6 +11,7 @@
#include "Resources.h" #include "Resources.h"
#include "SampleCode.h" #include "SampleCode.h"
#include "SamplePipeControllers.h" #include "SamplePipeControllers.h"
#include "SkAnimTimer.h"
#include "SkCanvas.h" #include "SkCanvas.h"
#include "SkCommandLineFlags.h" #include "SkCommandLineFlags.h"
#include "SkData.h" #include "SkData.h"
@ -102,6 +103,8 @@ static void post_event_to_sink(SkEvent* evt, SkEventSink* sink) {
evt->setTargetID(sink->getSinkID())->post(); evt->setTargetID(sink->getSinkID())->post();
} }
static SkAnimTimer gAnimTimer;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
static const char* skip_until(const char* str, const char* skip) { static const char* skip_until(const char* str, const char* skip) {
@ -577,39 +580,6 @@ bool SampleCode::FastTextQ(const SkEvent& evt) {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
static SkMSec gAnimTime;
static SkMSec gAnimTimePrev;
SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
SkScalar SampleCode::GetAnimSecondsDelta() {
return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
}
SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
// since gAnimTime can be up to 32 bits, we can't convert it to a float
// or we'll lose the low bits. Hence we use doubles for the intermediate
// calculations
double seconds = (double)gAnimTime / 1000.0;
double value = SkScalarToDouble(speed) * seconds;
if (period) {
value = ::fmod(value, SkScalarToDouble(period));
}
return SkDoubleToScalar(value);
}
SkScalar SampleCode::GetAnimSinScalar(SkScalar amplitude,
SkScalar periodInSec,
SkScalar phaseInSec) {
if (!periodInSec) {
return 0;
}
double t = (double)gAnimTime / 1000.0 + phaseInSec;
t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec;
amplitude = SK_ScalarHalf * amplitude;
return SkScalarMul(amplitude, SkDoubleToScalar(sin(t))) + amplitude;
}
enum TilingMode { enum TilingMode {
kNo_Tiling, kNo_Tiling,
kAbs_128x128_Tiling, kAbs_128x128_Tiling,
@ -665,12 +635,7 @@ static bool curr_title(SkWindow* wind, SkString* title) {
bool SampleWindow::sendAnimatePulse() { bool SampleWindow::sendAnimatePulse() {
SkView* view = curr_view(this); SkView* view = curr_view(this);
if (SampleView::IsSampleView(view)) { if (SampleView::IsSampleView(view)) {
if (fDoAnimate) { return ((SampleView*)view)->animate(gAnimTimer);
return ((SampleView*)view)->animatePulse(gAnimTime, gAnimTimePrev);
} else {
// 0 signals the view that we are no longer animating
((SampleView*)view)->animatePulse(0, 0);
}
} }
return false; return false;
} }
@ -836,14 +801,11 @@ SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* dev
fDeviceType = kANGLE_DeviceType; fDeviceType = kANGLE_DeviceType;
#endif #endif
fDoAnimate = true;
fUseClip = false; fUseClip = false;
fNClip = false; fNClip = false;
fAnimating = false; fAnimating = false;
fRotate = false; fRotate = false;
fRotateAnimTime = 0;
fPerspAnim = false; fPerspAnim = false;
fPerspAnimTime = 0;
fRequestGrabImage = false; fRequestGrabImage = false;
fPipeState = SkOSMenu::kOffState; fPipeState = SkOSMenu::kOffState;
fTilingMode = kNo_Tiling; fTilingMode = kNo_Tiling;
@ -974,6 +936,8 @@ SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* dev
// constructor first. Hence we post an event to ourselves. // constructor first. Hence we post an event to ourselves.
// this->updateTitle(); // this->updateTitle();
post_event_to_sink(new SkEvent(gUpdateWindowTitleEvtName), this); post_event_to_sink(new SkEvent(gUpdateWindowTitleEvtName), this);
gAnimTimer.run();
} }
SampleWindow::~SampleWindow() { SampleWindow::~SampleWindow() {
@ -1050,15 +1014,7 @@ static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar
#define YCLIP_N 8 #define YCLIP_N 8
void SampleWindow::draw(SkCanvas* canvas) { void SampleWindow::draw(SkCanvas* canvas) {
// update the animation time gAnimTimer.updateTime();
if (!gAnimTimePrev && !gAnimTime) {
// first time make delta be 0
gAnimTime = SkTime::GetMSecs();
gAnimTimePrev = gAnimTime;
} else {
gAnimTimePrev = gAnimTime;
gAnimTime = SkTime::GetMSecs();
}
if (fGesture.isActive()) { if (fGesture.isActive()) {
this->updateMatrix(); this->updateMatrix();
@ -1408,22 +1364,20 @@ void SampleWindow::afterChildren(SkCanvas* orig) {
void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) { void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
if (fRotate) { if (fRotate) {
fRotateAnimTime += SampleCode::GetAnimSecondsDelta();
SkScalar cx = this->width() / 2; SkScalar cx = this->width() / 2;
SkScalar cy = this->height() / 2; SkScalar cy = this->height() / 2;
canvas->translate(cx, cy); canvas->translate(cx, cy);
canvas->rotate(fRotateAnimTime * 10); canvas->rotate(gAnimTimer.scaled(10));
canvas->translate(-cx, -cy); canvas->translate(-cx, -cy);
} }
if (fPerspAnim) { if (fPerspAnim) {
fPerspAnimTime += SampleCode::GetAnimSecondsDelta(); SkScalar secs = gAnimTimer.scaled(1);
static const SkScalar gAnimPeriod = 10 * SK_Scalar1; static const SkScalar gAnimPeriod = 10 * SK_Scalar1;
static const SkScalar gAnimMag = SK_Scalar1 / 1000; static const SkScalar gAnimMag = SK_Scalar1 / 1000;
SkScalar t = SkScalarMod(fPerspAnimTime, gAnimPeriod); SkScalar t = SkScalarMod(secs, gAnimPeriod);
if (SkScalarFloorToInt(SkScalarDiv(fPerspAnimTime, gAnimPeriod)) & 0x1) { if (SkScalarFloorToInt(SkScalarDiv(secs, gAnimPeriod)) & 0x1) {
t = gAnimPeriod - t; t = gAnimPeriod - t;
} }
t = 2 * t - gAnimPeriod; t = 2 * t - gAnimPeriod;
@ -1669,20 +1623,6 @@ bool SampleWindow::onQuery(SkEvent* query) {
return this->INHERITED::onQuery(query); return this->INHERITED::onQuery(query);
} }
#if 0 // UNUSED
static void cleanup_for_filename(SkString* name) {
char* str = name->writable_str();
for (size_t i = 0; i < name->size(); i++) {
switch (str[i]) {
case ':': str[i] = '-'; break;
case '/': str[i] = '-'; break;
case ' ': str[i] = '_'; break;
default: break;
}
}
}
#endif
DECLARE_bool(portableFonts); DECLARE_bool(portableFonts);
bool SampleWindow::onHandleChar(SkUnichar uni) { bool SampleWindow::onHandleChar(SkUnichar uni) {
@ -1727,8 +1667,14 @@ bool SampleWindow::onHandleChar(SkUnichar uni) {
} }
switch (uni) { switch (uni) {
case 27: // ESC
gAnimTimer.stop();
if (this->sendAnimatePulse()) {
this->inval(NULL);
}
break;
case ' ': case ' ':
fDoAnimate = !fDoAnimate; gAnimTimer.togglePauseResume();
if (this->sendAnimatePulse()) { if (this->sendAnimatePulse()) {
this->inval(NULL); this->inval(NULL);
} }
@ -1769,7 +1715,6 @@ bool SampleWindow::onHandleChar(SkUnichar uni) {
break; break;
case 'r': case 'r':
fRotate = !fRotate; fRotate = !fRotate;
fRotateAnimTime = 0;
this->inval(NULL); this->inval(NULL);
this->updateTitle(); this->updateTitle();
return true; return true;

View File

@ -175,14 +175,11 @@ private:
bool fSaveToPdf; bool fSaveToPdf;
SkAutoTUnref<SkDocument> fPDFDocument; SkAutoTUnref<SkDocument> fPDFDocument;
bool fDoAnimate;
bool fUseClip; bool fUseClip;
bool fNClip; bool fNClip;
bool fAnimating; bool fAnimating;
bool fRotate; bool fRotate;
SkScalar fRotateAnimTime;
bool fPerspAnim; bool fPerspAnim;
SkScalar fPerspAnimTime;
bool fRequestGrabImage; bool fRequestGrabImage;
bool fMeasureFPS; bool fMeasureFPS;
SkMSec fMeasureFPS_Time; SkMSec fMeasureFPS_Time;

View File

@ -1,11 +1,12 @@
/* /*
* Copyright 2011 Google Inc. * Copyright 2011 Google Inc.
* *
* Use of this source code is governed by a BSD-style license that can be * Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file. * found in the LICENSE file.
*/ */
#include "SampleCode.h" #include "SampleCode.h"
#include "SkAnimTimer.h"
#include "SkView.h" #include "SkView.h"
#include "SkCanvas.h" #include "SkCanvas.h"
#include "SkGradientShader.h" #include "SkGradientShader.h"
@ -198,8 +199,8 @@ protected:
canvas->EXPERIMENTAL_drawDrawable(fRootDrawable); canvas->EXPERIMENTAL_drawDrawable(fRootDrawable);
} }
bool onAnimatePulse(SkMSec curr, SkMSec prev) SK_OVERRIDE { bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
SkScalar angle = SkDoubleToScalar(fmod(curr * 0.36 / 24, 360)); SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360));
fAnimatingDrawable->setSweep(angle); fAnimatingDrawable->setSweep(angle);
return true; return true;
} }

View File

@ -1,11 +1,12 @@
/* /*
* Copyright 2011 Google Inc. * Copyright 2011 Google Inc.
* *
* Use of this source code is governed by a BSD-style license that can be * Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file. * found in the LICENSE file.
*/ */
#include "SampleCode.h" #include "SampleCode.h"
#include "SkAnimTimer.h"
#include "SkView.h" #include "SkView.h"
#include "SkCanvas.h" #include "SkCanvas.h"
#include "SkGradientShader.h" #include "SkGradientShader.h"
@ -75,15 +76,19 @@ class BitmapRectView : public SampleView {
bounce_pt(&fSrcPts[1], &fSrcVec[1], fSrcLimit); bounce_pt(&fSrcPts[1], &fSrcVec[1], fSrcLimit);
} }
public: void resetBounce() {
BitmapRectView() {
this->setBGColor(SK_ColorGRAY);
fSrcPts[0].set(0, 0); fSrcPts[0].set(0, 0);
fSrcPts[1].set(SCALAR_SIZE, SCALAR_SIZE); fSrcPts[1].set(SCALAR_SIZE, SCALAR_SIZE);
fSrcVec[0] = unit_vec(30); fSrcVec[0] = unit_vec(30);
fSrcVec[1] = unit_vec(107); fSrcVec[1] = unit_vec(107);
}
public:
BitmapRectView() {
this->setBGColor(SK_ColorGRAY);
this->resetBounce();
fSrcLimit.set(-SCALAR_SIZE/4, -SCALAR_SIZE/4, fSrcLimit.set(-SCALAR_SIZE/4, -SCALAR_SIZE/4,
SCALAR_SIZE*5/4, SCALAR_SIZE*5/4); SCALAR_SIZE*5/4, SCALAR_SIZE*5/4);
@ -98,8 +103,7 @@ public:
} }
protected: protected:
// overrides from SkEventSink bool onQuery(SkEvent* evt) SK_OVERRIDE {
virtual bool onQuery(SkEvent* evt) {
if (SampleCode::TitleQ(*evt)) { if (SampleCode::TitleQ(*evt)) {
SampleCode::TitleR(evt, "BitmapRect"); SampleCode::TitleR(evt, "BitmapRect");
return true; return true;
@ -107,7 +111,7 @@ protected:
return this->INHERITED::onQuery(evt); return this->INHERITED::onQuery(evt);
} }
virtual void onDrawContent(SkCanvas* canvas) { void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
SkRect srcR; SkRect srcR;
srcR.set(fSrcPts[0], fSrcPts[1]); srcR.set(fSrcPts[0], fSrcPts[1]);
srcR = SkRect::MakeXYWH(fSrcPts[0].fX, fSrcPts[0].fY, 32, 32); srcR = SkRect::MakeXYWH(fSrcPts[0].fX, fSrcPts[0].fY, 32, 32);
@ -130,13 +134,19 @@ protected:
canvas->drawBitmapRectToRect(bitmap, &srcR, fDstR[i], &paint); canvas->drawBitmapRectToRect(bitmap, &srcR, fDstR[i], &paint);
canvas->drawRect(fDstR[i], paint); canvas->drawRect(fDstR[i], paint);
} }
}
bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
if (timer.isStopped()) {
this->resetBounce();
} else if (timer.isRunning()) {
this->bounce(); this->bounce();
this->inval(NULL); }
return true;
} }
private: private:
typedef SkView INHERITED; typedef SampleView INHERITED;
}; };
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
@ -179,32 +189,28 @@ class BitmapRectView2 : public SampleView {
fSrcR.fRight = fSrcR.fLeft + width; fSrcR.fRight = fSrcR.fLeft + width;
} }
void resetBounce() {
fSrcR.iset(0, 0, fBitmap.height() * 3, fBitmap.height());
fDX = SK_Scalar1;
}
public: public:
BitmapRectView2() { BitmapRectView2() {
make_big_bitmap(&fBitmap); make_big_bitmap(&fBitmap);
this->setBGColor(SK_ColorGRAY); this->setBGColor(SK_ColorGRAY);
fSrcR.fLeft = 0; this->resetBounce();
fSrcR.fTop = 0;
fSrcR.fRight = SkIntToScalar(fBitmap.height()) * 3;
fSrcR.fBottom = SkIntToScalar(fBitmap.height());
fLimitR.set(0, 0, fLimitR.iset(0, 0, fBitmap.width(), fBitmap.height());
SkIntToScalar(fBitmap.width()),
SkIntToScalar(fBitmap.height()));
fDX = SK_Scalar1; fDstR[0] = SkRect::MakeXYWH(20, 20, 600, 200);
fDstR[0] = SkRect::MakeXYWH(SkIntToScalar(20), SkIntToScalar(20),
SkIntToScalar(600), SkIntToScalar(200));
fDstR[1] = fDstR[0]; fDstR[1] = fDstR[0];
fDstR[1].offset(0, fDstR[0].height() * 5/4); fDstR[1].offset(0, fDstR[0].height() * 5/4);
} }
protected: protected:
// overrides from SkEventSink bool onQuery(SkEvent* evt) SK_OVERRIDE {
virtual bool onQuery(SkEvent* evt) {
if (SampleCode::TitleQ(*evt)) { if (SampleCode::TitleQ(*evt)) {
SampleCode::TitleR(evt, "BigBitmapRect"); SampleCode::TitleR(evt, "BigBitmapRect");
return true; return true;
@ -212,7 +218,7 @@ protected:
return this->INHERITED::onQuery(evt); return this->INHERITED::onQuery(evt);
} }
virtual void onDrawContent(SkCanvas* canvas) { void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
SkPaint paint; SkPaint paint;
paint.setStyle(SkPaint::kStroke_Style); paint.setStyle(SkPaint::kStroke_Style);
paint.setColor(SK_ColorYELLOW); paint.setColor(SK_ColorYELLOW);
@ -222,13 +228,19 @@ protected:
canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR[i], &paint); canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR[i], &paint);
canvas->drawRect(fDstR[i], paint); canvas->drawRect(fDstR[i], paint);
} }
}
bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
if (timer.isStopped()) {
this->resetBounce();
} else if (timer.isRunning()) {
this->bounceMe(); this->bounceMe();
this->inval(NULL); }
return true;
} }
private: private:
typedef SkView INHERITED; typedef SampleView INHERITED;
}; };
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////

View File

@ -6,6 +6,7 @@
*/ */
#include "SampleCode.h" #include "SampleCode.h"
#include "SkAnimTimer.h"
#include "SkView.h" #include "SkView.h"
#include "SkCanvas.h" #include "SkCanvas.h"
#include "SkCamera.h" #include "SkCamera.h"
@ -88,10 +89,11 @@ protected:
} }
} }
bool onAnimatePulse(SkMSec curr, SkMSec prev) SK_OVERRIDE { bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
fRY += (curr - prev) * 0.09f; if (timer.isStopped()) {
if (fRY >= 360) {
fRY = 0; fRY = 0;
} else {
fRY = timer.scaled(90, 360);
} }
return true; return true;
} }

View File

@ -1,4 +1,3 @@
/* /*
* Copyright 2011 Google Inc. * Copyright 2011 Google Inc.
* *
@ -6,7 +5,6 @@
* found in the LICENSE file. * found in the LICENSE file.
*/ */
#ifndef SampleCode_DEFINED #ifndef SampleCode_DEFINED
#define SampleCode_DEFINED #define SampleCode_DEFINED
@ -15,7 +13,9 @@
#include "SkKey.h" #include "SkKey.h"
#include "SkView.h" #include "SkView.h"
#include "SkOSMenu.h" #include "SkOSMenu.h"
class GrContext; class GrContext;
class SkAnimTimer;
#define DEF_SAMPLE(code) \ #define DEF_SAMPLE(code) \
static SkView* SK_MACRO_APPEND_LINE(F_)() { code } \ static SkView* SK_MACRO_APPEND_LINE(F_)() { code } \
@ -36,16 +36,6 @@ public:
static bool FastTextQ(const SkEvent&); static bool FastTextQ(const SkEvent&);
private:
static SkMSec GetAnimTime();
static SkMSec GetAnimTimeDelta();
static SkScalar GetAnimSecondsDelta();
static SkScalar GetAnimScalar(SkScalar speedPerSec, SkScalar period = 0);
// gives a sinusoidal value between 0 and amplitude
static SkScalar GetAnimSinScalar(SkScalar amplitude,
SkScalar periodInSec,
SkScalar phaseInSec = 0);
friend class SampleWindow; friend class SampleWindow;
}; };
@ -120,7 +110,7 @@ public:
{} {}
void setBGColor(SkColor color) { fBGColor = color; } void setBGColor(SkColor color) { fBGColor = color; }
bool animatePulse(SkMSec curr, SkMSec prev) { return this->onAnimatePulse(curr, prev); } bool animate(const SkAnimTimer& timer) { return this->onAnimate(timer); }
static bool IsSampleView(SkView*); static bool IsSampleView(SkView*);
static bool SetRepeatDraw(SkView*, int count); static bool SetRepeatDraw(SkView*, int count);
@ -140,7 +130,7 @@ public:
protected: protected:
virtual void onDrawBackground(SkCanvas*); virtual void onDrawBackground(SkCanvas*);
virtual void onDrawContent(SkCanvas*) = 0; virtual void onDrawContent(SkCanvas*) = 0;
virtual bool onAnimatePulse(SkMSec curr, SkMSec prev) { return false; } virtual bool onAnimate(const SkAnimTimer&) { return false; }
// overrides // overrides
virtual bool onEvent(const SkEvent& evt); virtual bool onEvent(const SkEvent& evt);

View File

@ -6,6 +6,7 @@
*/ */
#include "SampleCode.h" #include "SampleCode.h"
#include "SkAnimTimer.h"
#include "SkView.h" #include "SkView.h"
#include "SkCanvas.h" #include "SkCanvas.h"
#include "SkGradientShader.h" #include "SkGradientShader.h"
@ -76,8 +77,8 @@ protected:
canvas->drawText(txt.c_str(), txt.size(), l + w/2 + w*DELTA_SCALE*delta, t + h + SK_Scalar1 * 10, paint); canvas->drawText(txt.c_str(), txt.size(), l + w/2 + w*DELTA_SCALE*delta, t + h + SK_Scalar1 * 10, paint);
} }
bool onAnimatePulse(SkMSec curr, SkMSec prev) SK_OVERRIDE { bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
fTime += (curr - prev) * 0.001f; fTime = SkDoubleToScalar(timer.secs() / 15);
return true; return true;
} }

View File

@ -6,6 +6,7 @@
*/ */
#include "SampleCode.h" #include "SampleCode.h"
#include "SkAnimTimer.h"
#include "SkView.h" #include "SkView.h"
#include "SkCanvas.h" #include "SkCanvas.h"
#include "SkCanvasDrawable.h" #include "SkCanvasDrawable.h"
@ -155,8 +156,8 @@ protected:
canvas->EXPERIMENTAL_drawDrawable(fRoot); canvas->EXPERIMENTAL_drawDrawable(fRoot);
} }
bool onAnimatePulse(SkMSec curr, SkMSec prev) SK_OVERRIDE { bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
fTime = curr; fTime = timer.msec();
for (int i = 0; i < N; ++i) { for (int i = 0; i < N; ++i) {
fArray[i].fDrawable->setTime(fTime); fArray[i].fDrawable->setTime(fTime);
} }

View File

@ -6,6 +6,7 @@
*/ */
#include "SampleCode.h" #include "SampleCode.h"
#include "SkAnimTimer.h"
#include "SkView.h" #include "SkView.h"
#include "SkCanvas.h" #include "SkCanvas.h"
#include "SkCornerPathEffect.h" #include "SkCornerPathEffect.h"
@ -221,7 +222,7 @@ protected:
canvas->drawBitmap(bm2, SkIntToScalar(10), SkIntToScalar(10), NULL); canvas->drawBitmap(bm2, SkIntToScalar(10), SkIntToScalar(10), NULL);
} }
bool onAnimatePulse(SkMSec curr, SkMSec prev) SK_OVERRIDE { bool onAnimate(const SkAnimTimer&) SK_OVERRIDE {
if (fDoAA) { if (fDoAA) {
fProcIndex = cycle_hairproc_index(fProcIndex); fProcIndex = cycle_hairproc_index(fProcIndex);
// todo: signal that we want to rebuild our TITLE // todo: signal that we want to rebuild our TITLE

View File

@ -1,11 +1,12 @@
/* /*
* Copyright 2011 Google Inc. * Copyright 2011 Google Inc.
* *
* Use of this source code is governed by a BSD-style license that can be * Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file. * found in the LICENSE file.
*/ */
#include "SampleCode.h" #include "SampleCode.h"
#include "SkAnimTimer.h"
#include "SkView.h" #include "SkView.h"
#include "SkCanvas.h" #include "SkCanvas.h"
#include "SkGradientShader.h" #include "SkGradientShader.h"
@ -158,8 +159,8 @@ protected:
canvas->drawPath(fPath, paint); canvas->drawPath(fPath, paint);
} }
bool onAnimatePulse(SkMSec curr, SkMSec prev) SK_OVERRIDE { bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
fPhase -= (curr - prev) * 0.04f; fPhase = timer.scaled(40);
return true; return true;
} }