From 76113a9b7716748c70ea0ecf7aacbabe4cce5009 Mon Sep 17 00:00:00 2001 From: reed Date: Mon, 2 Feb 2015 12:55:02 -0800 Subject: [PATCH] add SkAnimTimer, SPACE = pause/resume, ESP = stop BUG=skia: Review URL: https://codereview.chromium.org/894083003 --- gm/SkAnimTimer.h | 139 ++++++++++++++++++++ gm/addarc.cpp | 5 +- gm/gm.cpp | 4 +- gm/gm.h | 6 +- samplecode/GMSampleView.h | 4 +- samplecode/SampleAnimBlur.cpp | 13 +- samplecode/SampleApp.cpp | 91 +++---------- samplecode/SampleApp.h | 3 - samplecode/SampleArc.cpp | 7 +- samplecode/SampleBitmapRect.cpp | 70 ++++++---- samplecode/SampleCamera.cpp | 8 +- samplecode/SampleCode.h | 18 +-- samplecode/SampleDegenerateTwoPtRadials.cpp | 5 +- samplecode/SampleHT.cpp | 5 +- samplecode/SampleHairline.cpp | 3 +- samplecode/SamplePathEffects.cpp | 7 +- 16 files changed, 241 insertions(+), 147 deletions(-) create mode 100644 gm/SkAnimTimer.h diff --git a/gm/SkAnimTimer.h b/gm/SkAnimTimer.h new file mode 100644 index 0000000000..725171a8fe --- /dev/null +++ b/gm/SkAnimTimer.h @@ -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 diff --git a/gm/addarc.cpp b/gm/addarc.cpp index 1386cdd0e7..e6fad25772 100644 --- a/gm/addarc.cpp +++ b/gm/addarc.cpp @@ -6,6 +6,7 @@ */ #include "gm.h" +#include "SkAnimTimer.h" #include "SkCanvas.h" #include "SkRandom.h" @@ -49,8 +50,8 @@ protected: } } - bool onAnimatePulse(SkMSec curr, SkMSec prev) SK_OVERRIDE { - fRotate = SkDoubleToScalar(fmod(curr * 0.001, 360)); + bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE { + fRotate = timer.scaled(1, 360); return true; } diff --git a/gm/gm.cpp b/gm/gm.cpp index c8109a7e26..8d269d977d 100644 --- a/gm/gm.cpp +++ b/gm/gm.cpp @@ -51,8 +51,8 @@ void GM::setBGColor(SkColor color) { fBGColor = color; } -bool GM::animatePulse(SkMSec curr, SkMSec prev) { - return this->onAnimatePulse(curr, prev); +bool GM::animate(const SkAnimTimer& timer) { + return this->onAnimate(timer); } ///////////////////////////////////////////////////////////////////////////////////////////// diff --git a/gm/gm.h b/gm/gm.h index c7dca6d276..e263528f90 100644 --- a/gm/gm.h +++ b/gm/gm.h @@ -16,6 +16,8 @@ #include "SkTRegistry.h" #include "sk_tool_utils.h" +class SkAnimTimer; + #if SK_SUPPORT_GPU #include "GrContext.h" #endif @@ -92,7 +94,7 @@ namespace skiagm { fStarterMatrix = matrix; } - bool animatePulse(SkMSec curr, SkMSec prev); + bool animate(const SkAnimTimer&); protected: /** 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 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(); } private: diff --git a/samplecode/GMSampleView.h b/samplecode/GMSampleView.h index a03628e05b..adaf23d43f 100644 --- a/samplecode/GMSampleView.h +++ b/samplecode/GMSampleView.h @@ -70,8 +70,8 @@ protected: fGM->drawBackground(canvas); } - bool onAnimatePulse(SkMSec curr, SkMSec prev) SK_OVERRIDE { - return fGM->animatePulse(curr, prev); + bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE { + return fGM->animate(timer); } private: diff --git a/samplecode/SampleAnimBlur.cpp b/samplecode/SampleAnimBlur.cpp index 527d4300e7..3c0be83768 100644 --- a/samplecode/SampleAnimBlur.cpp +++ b/samplecode/SampleAnimBlur.cpp @@ -1,21 +1,22 @@ - /* * Copyright 2012 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 "SkAnimTimer.h" #include "SkBlurMaskFilter.h" #include "SkColorPriv.h" #include "SkCanvas.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) { return 0; } - double t = (double)time / 1000.0 + phaseInSec; + double t = secs + phaseInSec; t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec; amplitude = SK_ScalarHalf * amplitude; return amplitude * SkDoubleToScalar(sin(t)) + amplitude; @@ -58,9 +59,9 @@ protected: } } - bool onAnimatePulse(SkMSec curr, SkMSec prev) SK_OVERRIDE { - fBlurSigma = get_anim_sin(curr, 100, 4, 5); - fCircleRadius = 3 + get_anim_sin(curr, 150, 25, 3); + bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE { + fBlurSigma = get_anim_sin(timer.secs(), 100, 4, 5); + fCircleRadius = 3 + get_anim_sin(timer.secs(), 150, 25, 3); return true; } diff --git a/samplecode/SampleApp.cpp b/samplecode/SampleApp.cpp index 5b8ed96905..cd5793f6a3 100644 --- a/samplecode/SampleApp.cpp +++ b/samplecode/SampleApp.cpp @@ -11,6 +11,7 @@ #include "Resources.h" #include "SampleCode.h" #include "SamplePipeControllers.h" +#include "SkAnimTimer.h" #include "SkCanvas.h" #include "SkCommandLineFlags.h" #include "SkData.h" @@ -102,6 +103,8 @@ static void post_event_to_sink(SkEvent* evt, SkEventSink* sink) { evt->setTargetID(sink->getSinkID())->post(); } +static SkAnimTimer gAnimTimer; + /////////////////////////////////////////////////////////////////////////////// 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 { kNo_Tiling, kAbs_128x128_Tiling, @@ -665,12 +635,7 @@ static bool curr_title(SkWindow* wind, SkString* title) { bool SampleWindow::sendAnimatePulse() { SkView* view = curr_view(this); if (SampleView::IsSampleView(view)) { - if (fDoAnimate) { - return ((SampleView*)view)->animatePulse(gAnimTime, gAnimTimePrev); - } else { - // 0 signals the view that we are no longer animating - ((SampleView*)view)->animatePulse(0, 0); - } + return ((SampleView*)view)->animate(gAnimTimer); } return false; } @@ -836,14 +801,11 @@ SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* dev fDeviceType = kANGLE_DeviceType; #endif - fDoAnimate = true; fUseClip = false; fNClip = false; fAnimating = false; fRotate = false; - fRotateAnimTime = 0; fPerspAnim = false; - fPerspAnimTime = 0; fRequestGrabImage = false; fPipeState = SkOSMenu::kOffState; 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. // this->updateTitle(); post_event_to_sink(new SkEvent(gUpdateWindowTitleEvtName), this); + + gAnimTimer.run(); } SampleWindow::~SampleWindow() { @@ -1050,15 +1014,7 @@ static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar #define YCLIP_N 8 void SampleWindow::draw(SkCanvas* canvas) { - // update the animation time - if (!gAnimTimePrev && !gAnimTime) { - // first time make delta be 0 - gAnimTime = SkTime::GetMSecs(); - gAnimTimePrev = gAnimTime; - } else { - gAnimTimePrev = gAnimTime; - gAnimTime = SkTime::GetMSecs(); - } + gAnimTimer.updateTime(); if (fGesture.isActive()) { this->updateMatrix(); @@ -1408,22 +1364,20 @@ void SampleWindow::afterChildren(SkCanvas* orig) { void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) { if (fRotate) { - fRotateAnimTime += SampleCode::GetAnimSecondsDelta(); - SkScalar cx = this->width() / 2; SkScalar cy = this->height() / 2; canvas->translate(cx, cy); - canvas->rotate(fRotateAnimTime * 10); + canvas->rotate(gAnimTimer.scaled(10)); canvas->translate(-cx, -cy); } if (fPerspAnim) { - fPerspAnimTime += SampleCode::GetAnimSecondsDelta(); + SkScalar secs = gAnimTimer.scaled(1); static const SkScalar gAnimPeriod = 10 * SK_Scalar1; static const SkScalar gAnimMag = SK_Scalar1 / 1000; - SkScalar t = SkScalarMod(fPerspAnimTime, gAnimPeriod); - if (SkScalarFloorToInt(SkScalarDiv(fPerspAnimTime, gAnimPeriod)) & 0x1) { + SkScalar t = SkScalarMod(secs, gAnimPeriod); + if (SkScalarFloorToInt(SkScalarDiv(secs, gAnimPeriod)) & 0x1) { t = gAnimPeriod - t; } t = 2 * t - gAnimPeriod; @@ -1669,20 +1623,6 @@ bool SampleWindow::onQuery(SkEvent* 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); bool SampleWindow::onHandleChar(SkUnichar uni) { @@ -1727,8 +1667,14 @@ bool SampleWindow::onHandleChar(SkUnichar uni) { } switch (uni) { + case 27: // ESC + gAnimTimer.stop(); + if (this->sendAnimatePulse()) { + this->inval(NULL); + } + break; case ' ': - fDoAnimate = !fDoAnimate; + gAnimTimer.togglePauseResume(); if (this->sendAnimatePulse()) { this->inval(NULL); } @@ -1769,7 +1715,6 @@ bool SampleWindow::onHandleChar(SkUnichar uni) { break; case 'r': fRotate = !fRotate; - fRotateAnimTime = 0; this->inval(NULL); this->updateTitle(); return true; diff --git a/samplecode/SampleApp.h b/samplecode/SampleApp.h index d75e1495c3..b6530a3d5f 100644 --- a/samplecode/SampleApp.h +++ b/samplecode/SampleApp.h @@ -175,14 +175,11 @@ private: bool fSaveToPdf; SkAutoTUnref fPDFDocument; - bool fDoAnimate; bool fUseClip; bool fNClip; bool fAnimating; bool fRotate; - SkScalar fRotateAnimTime; bool fPerspAnim; - SkScalar fPerspAnimTime; bool fRequestGrabImage; bool fMeasureFPS; SkMSec fMeasureFPS_Time; diff --git a/samplecode/SampleArc.cpp b/samplecode/SampleArc.cpp index 9f922ae2d6..3775de50c8 100644 --- a/samplecode/SampleArc.cpp +++ b/samplecode/SampleArc.cpp @@ -1,11 +1,12 @@ - /* * Copyright 2011 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 "SkAnimTimer.h" #include "SkView.h" #include "SkCanvas.h" #include "SkGradientShader.h" @@ -198,8 +199,8 @@ protected: canvas->EXPERIMENTAL_drawDrawable(fRootDrawable); } - bool onAnimatePulse(SkMSec curr, SkMSec prev) SK_OVERRIDE { - SkScalar angle = SkDoubleToScalar(fmod(curr * 0.36 / 24, 360)); + bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE { + SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360)); fAnimatingDrawable->setSweep(angle); return true; } diff --git a/samplecode/SampleBitmapRect.cpp b/samplecode/SampleBitmapRect.cpp index 006b919554..82d1098a35 100644 --- a/samplecode/SampleBitmapRect.cpp +++ b/samplecode/SampleBitmapRect.cpp @@ -1,11 +1,12 @@ - /* * Copyright 2011 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 "SkAnimTimer.h" #include "SkView.h" #include "SkCanvas.h" #include "SkGradientShader.h" @@ -75,15 +76,19 @@ class BitmapRectView : public SampleView { bounce_pt(&fSrcPts[1], &fSrcVec[1], fSrcLimit); } + void resetBounce() { + fSrcPts[0].set(0, 0); + fSrcPts[1].set(SCALAR_SIZE, SCALAR_SIZE); + + fSrcVec[0] = unit_vec(30); + fSrcVec[1] = unit_vec(107); + } + public: BitmapRectView() { this->setBGColor(SK_ColorGRAY); - fSrcPts[0].set(0, 0); - fSrcPts[1].set(SCALAR_SIZE, SCALAR_SIZE); - - fSrcVec[0] = unit_vec(30); - fSrcVec[1] = unit_vec(107); + this->resetBounce(); fSrcLimit.set(-SCALAR_SIZE/4, -SCALAR_SIZE/4, SCALAR_SIZE*5/4, SCALAR_SIZE*5/4); @@ -98,8 +103,7 @@ public: } protected: - // overrides from SkEventSink - virtual bool onQuery(SkEvent* evt) { + bool onQuery(SkEvent* evt) SK_OVERRIDE { if (SampleCode::TitleQ(*evt)) { SampleCode::TitleR(evt, "BitmapRect"); return true; @@ -107,7 +111,7 @@ protected: return this->INHERITED::onQuery(evt); } - virtual void onDrawContent(SkCanvas* canvas) { + void onDrawContent(SkCanvas* canvas) SK_OVERRIDE { SkRect srcR; srcR.set(fSrcPts[0], fSrcPts[1]); srcR = SkRect::MakeXYWH(fSrcPts[0].fX, fSrcPts[0].fY, 32, 32); @@ -130,13 +134,19 @@ protected: canvas->drawBitmapRectToRect(bitmap, &srcR, fDstR[i], &paint); canvas->drawRect(fDstR[i], paint); } + } - this->bounce(); - this->inval(NULL); + bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE { + if (timer.isStopped()) { + this->resetBounce(); + } else if (timer.isRunning()) { + this->bounce(); + } + return true; } private: - typedef SkView INHERITED; + typedef SampleView INHERITED; }; ////////////////////////////////////////////////////////////////////////////// @@ -179,32 +189,28 @@ class BitmapRectView2 : public SampleView { fSrcR.fRight = fSrcR.fLeft + width; } + void resetBounce() { + fSrcR.iset(0, 0, fBitmap.height() * 3, fBitmap.height()); + fDX = SK_Scalar1; + } + public: BitmapRectView2() { make_big_bitmap(&fBitmap); this->setBGColor(SK_ColorGRAY); - fSrcR.fLeft = 0; - fSrcR.fTop = 0; - fSrcR.fRight = SkIntToScalar(fBitmap.height()) * 3; - fSrcR.fBottom = SkIntToScalar(fBitmap.height()); + this->resetBounce(); - fLimitR.set(0, 0, - SkIntToScalar(fBitmap.width()), - SkIntToScalar(fBitmap.height())); + fLimitR.iset(0, 0, fBitmap.width(), fBitmap.height()); - fDX = SK_Scalar1; - - fDstR[0] = SkRect::MakeXYWH(SkIntToScalar(20), SkIntToScalar(20), - SkIntToScalar(600), SkIntToScalar(200)); + fDstR[0] = SkRect::MakeXYWH(20, 20, 600, 200); fDstR[1] = fDstR[0]; fDstR[1].offset(0, fDstR[0].height() * 5/4); } protected: - // overrides from SkEventSink - virtual bool onQuery(SkEvent* evt) { + bool onQuery(SkEvent* evt) SK_OVERRIDE { if (SampleCode::TitleQ(*evt)) { SampleCode::TitleR(evt, "BigBitmapRect"); return true; @@ -212,7 +218,7 @@ protected: return this->INHERITED::onQuery(evt); } - virtual void onDrawContent(SkCanvas* canvas) { + void onDrawContent(SkCanvas* canvas) SK_OVERRIDE { SkPaint paint; paint.setStyle(SkPaint::kStroke_Style); paint.setColor(SK_ColorYELLOW); @@ -222,13 +228,19 @@ protected: canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR[i], &paint); canvas->drawRect(fDstR[i], paint); } + } - this->bounceMe(); - this->inval(NULL); + bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE { + if (timer.isStopped()) { + this->resetBounce(); + } else if (timer.isRunning()) { + this->bounceMe(); + } + return true; } private: - typedef SkView INHERITED; + typedef SampleView INHERITED; }; ////////////////////////////////////////////////////////////////////////////// diff --git a/samplecode/SampleCamera.cpp b/samplecode/SampleCamera.cpp index 35fa719479..af9dbe8d65 100644 --- a/samplecode/SampleCamera.cpp +++ b/samplecode/SampleCamera.cpp @@ -6,6 +6,7 @@ */ #include "SampleCode.h" +#include "SkAnimTimer.h" #include "SkView.h" #include "SkCanvas.h" #include "SkCamera.h" @@ -88,10 +89,11 @@ protected: } } - bool onAnimatePulse(SkMSec curr, SkMSec prev) SK_OVERRIDE { - fRY += (curr - prev) * 0.09f; - if (fRY >= 360) { + bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE { + if (timer.isStopped()) { fRY = 0; + } else { + fRY = timer.scaled(90, 360); } return true; } diff --git a/samplecode/SampleCode.h b/samplecode/SampleCode.h index 02590f17a9..e65919a765 100644 --- a/samplecode/SampleCode.h +++ b/samplecode/SampleCode.h @@ -1,4 +1,3 @@ - /* * Copyright 2011 Google Inc. * @@ -6,7 +5,6 @@ * found in the LICENSE file. */ - #ifndef SampleCode_DEFINED #define SampleCode_DEFINED @@ -15,7 +13,9 @@ #include "SkKey.h" #include "SkView.h" #include "SkOSMenu.h" + class GrContext; +class SkAnimTimer; #define DEF_SAMPLE(code) \ static SkView* SK_MACRO_APPEND_LINE(F_)() { code } \ @@ -36,16 +36,6 @@ public: 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; }; @@ -120,7 +110,7 @@ public: {} 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 SetRepeatDraw(SkView*, int count); @@ -140,7 +130,7 @@ public: protected: virtual void onDrawBackground(SkCanvas*); virtual void onDrawContent(SkCanvas*) = 0; - virtual bool onAnimatePulse(SkMSec curr, SkMSec prev) { return false; } + virtual bool onAnimate(const SkAnimTimer&) { return false; } // overrides virtual bool onEvent(const SkEvent& evt); diff --git a/samplecode/SampleDegenerateTwoPtRadials.cpp b/samplecode/SampleDegenerateTwoPtRadials.cpp index 71c03b016a..dec985029e 100644 --- a/samplecode/SampleDegenerateTwoPtRadials.cpp +++ b/samplecode/SampleDegenerateTwoPtRadials.cpp @@ -6,6 +6,7 @@ */ #include "SampleCode.h" +#include "SkAnimTimer.h" #include "SkView.h" #include "SkCanvas.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); } - bool onAnimatePulse(SkMSec curr, SkMSec prev) SK_OVERRIDE { - fTime += (curr - prev) * 0.001f; + bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE { + fTime = SkDoubleToScalar(timer.secs() / 15); return true; } diff --git a/samplecode/SampleHT.cpp b/samplecode/SampleHT.cpp index 07605fe1ed..1436817e76 100644 --- a/samplecode/SampleHT.cpp +++ b/samplecode/SampleHT.cpp @@ -6,6 +6,7 @@ */ #include "SampleCode.h" +#include "SkAnimTimer.h" #include "SkView.h" #include "SkCanvas.h" #include "SkCanvasDrawable.h" @@ -155,8 +156,8 @@ protected: canvas->EXPERIMENTAL_drawDrawable(fRoot); } - bool onAnimatePulse(SkMSec curr, SkMSec prev) SK_OVERRIDE { - fTime = curr; + bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE { + fTime = timer.msec(); for (int i = 0; i < N; ++i) { fArray[i].fDrawable->setTime(fTime); } diff --git a/samplecode/SampleHairline.cpp b/samplecode/SampleHairline.cpp index 3c254472fa..76876339b4 100644 --- a/samplecode/SampleHairline.cpp +++ b/samplecode/SampleHairline.cpp @@ -6,6 +6,7 @@ */ #include "SampleCode.h" +#include "SkAnimTimer.h" #include "SkView.h" #include "SkCanvas.h" #include "SkCornerPathEffect.h" @@ -221,7 +222,7 @@ protected: canvas->drawBitmap(bm2, SkIntToScalar(10), SkIntToScalar(10), NULL); } - bool onAnimatePulse(SkMSec curr, SkMSec prev) SK_OVERRIDE { + bool onAnimate(const SkAnimTimer&) SK_OVERRIDE { if (fDoAA) { fProcIndex = cycle_hairproc_index(fProcIndex); // todo: signal that we want to rebuild our TITLE diff --git a/samplecode/SamplePathEffects.cpp b/samplecode/SamplePathEffects.cpp index f07cd6ce40..a5f91c296e 100644 --- a/samplecode/SamplePathEffects.cpp +++ b/samplecode/SamplePathEffects.cpp @@ -1,11 +1,12 @@ - /* * Copyright 2011 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 "SkAnimTimer.h" #include "SkView.h" #include "SkCanvas.h" #include "SkGradientShader.h" @@ -158,8 +159,8 @@ protected: canvas->drawPath(fPath, paint); } - bool onAnimatePulse(SkMSec curr, SkMSec prev) SK_OVERRIDE { - fPhase -= (curr - prev) * 0.04f; + bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE { + fPhase = timer.scaled(40); return true; }