diff options
Diffstat (limited to 'samplecode')
-rw-r--r-- | samplecode/ClockFaceView.cpp | 11 | ||||
-rw-r--r-- | samplecode/SampleApp.cpp | 157 | ||||
-rw-r--r-- | samplecode/SampleAtlas.cpp | 4 | ||||
-rw-r--r-- | samplecode/SampleClock.cpp | 4 | ||||
-rw-r--r-- | samplecode/SampleCode.cpp | 170 | ||||
-rw-r--r-- | samplecode/SampleCode.h | 9 |
6 files changed, 195 insertions, 160 deletions
diff --git a/samplecode/ClockFaceView.cpp b/samplecode/ClockFaceView.cpp index ef0fec7577..9cee95b84c 100644 --- a/samplecode/ClockFaceView.cpp +++ b/samplecode/ClockFaceView.cpp @@ -72,7 +72,14 @@ public: : Sk2DPathEffect(matrix), fRadius(radius), fPts(pts) {} SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Dot2DPathEffect) - + class Registrar { + public: + Registrar() { + SkFlattenable::Register("Dot2DPathEffect", + Dot2DPathEffect::CreateProc, + Dot2DPathEffect::GetFlattenableType()); + } + }; protected: void begin(const SkIRect& uvBounds, SkPath* dst) const override { if (fPts) { @@ -101,6 +108,8 @@ private: typedef Sk2DPathEffect INHERITED; }; +static Dot2DPathEffect::Registrar gReg0; + sk_sp<SkFlattenable> Dot2DPathEffect::CreateProc(SkReadBuffer& buffer) { SkMatrix matrix; buffer.readMatrix(&matrix); diff --git a/samplecode/SampleApp.cpp b/samplecode/SampleApp.cpp index 971044cf87..40ead7482f 100644 --- a/samplecode/SampleApp.cpp +++ b/samplecode/SampleApp.cpp @@ -400,45 +400,8 @@ static bool isInvalEvent(const SkEvent& evt) { } ////////////////// -SkFuncViewFactory::SkFuncViewFactory(SkViewCreateFunc func) - : fCreateFunc(func) { -} - -SkView* SkFuncViewFactory::operator() () const { - return (*fCreateFunc)(); -} - #include "GMSampleView.h" -SkGMSampleViewFactory::SkGMSampleViewFactory(GMFactoryFunc func) - : fFunc(func) { -} - -SkView* SkGMSampleViewFactory::operator() () const { - skiagm::GM* gm = fFunc(nullptr); - gm->setMode(skiagm::GM::kSample_Mode); - return new GMSampleView(gm); -} - -SkViewRegister* SkViewRegister::gHead; -SkViewRegister::SkViewRegister(SkViewFactory* fact) : fFact(fact) { - fFact->ref(); - fChain = gHead; - gHead = this; -} - -SkViewRegister::SkViewRegister(SkViewCreateFunc func) { - fFact = new SkFuncViewFactory(func); - fChain = gHead; - gHead = this; -} - -SkViewRegister::SkViewRegister(GMFactoryFunc func) { - fFact = new SkGMSampleViewFactory(func); - fChain = gHead; - gHead = this; -} - class AutoUnrefArray { public: AutoUnrefArray() {} @@ -567,72 +530,6 @@ private: typedef SkPaintFilterCanvas INHERITED; }; -////////////////////////////////////////////////////////////////////////////// - -#define MAX_ZOOM_LEVEL 8 -#define MIN_ZOOM_LEVEL -8 - -static const char gCharEvtName[] = "SampleCode_Char_Event"; -static const char gKeyEvtName[] = "SampleCode_Key_Event"; -static const char gTitleEvtName[] = "SampleCode_Title_Event"; -static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event"; -static const char gFastTextEvtName[] = "SampleCode_FastText_Event"; -static const char gUpdateWindowTitleEvtName[] = "SampleCode_UpdateWindowTitle"; - -bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) { - if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) { - if (outUni) { - *outUni = evt.getFast32(); - } - return true; - } - return false; -} - -bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) { - if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) { - if (outKey) { - *outKey = (SkKey)evt.getFast32(); - } - return true; - } - return false; -} - -bool SampleCode::TitleQ(const SkEvent& evt) { - return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1); -} - -void SampleCode::TitleR(SkEvent* evt, const char title[]) { - SkASSERT(evt && TitleQ(*evt)); - evt->setString(gTitleEvtName, title); -} - -bool SampleCode::RequestTitle(SkView* view, SkString* title) { - SkEvent evt(gTitleEvtName); - if (view->doQuery(&evt)) { - title->set(evt.findString(gTitleEvtName)); - return true; - } - return false; -} - -bool SampleCode::PrefSizeQ(const SkEvent& evt) { - return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1); -} - -void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) { - SkASSERT(evt && PrefSizeQ(*evt)); - SkScalar size[2]; - size[0] = width; - size[1] = height; - evt->setScalars(gPrefSizeEvtName, 2, size); -} - -bool SampleCode::FastTextQ(const SkEvent& evt) { - return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1); -} - /////////////////////////////////////////////////////////////////////////////// enum TilingMode { @@ -2214,60 +2111,6 @@ void SampleWindow::onSizeChange() { /////////////////////////////////////////////////////////////////////////////// -static const char is_sample_view_tag[] = "sample-is-sample-view"; -static const char repeat_count_tag[] = "sample-set-repeat-count"; - -bool SampleView::IsSampleView(SkView* view) { - SkEvent evt(is_sample_view_tag); - return view->doQuery(&evt); -} - -bool SampleView::SetRepeatDraw(SkView* view, int count) { - SkEvent evt(repeat_count_tag); - evt.setFast32(count); - return 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) { - if (evt->isType(is_sample_view_tag)) { - return true; - } - return this->INHERITED::onQuery(evt); -} - -void SampleView::onDraw(SkCanvas* canvas) { - if (!fHaveCalledOnceBeforeDraw) { - fHaveCalledOnceBeforeDraw = true; - this->onOnceBeforeDraw(); - } - this->onDrawBackground(canvas); - - for (int i = 0; i < fRepeatCount; i++) { - SkAutoCanvasRestore acr(canvas, true); - this->onDrawContent(canvas); -#if SK_SUPPORT_GPU - // Ensure the GrContext doesn't batch across draw loops. - if (GrContext* context = canvas->getGrContext()) { - context->flush(); - } -#endif - } -} - -void SampleView::onDrawBackground(SkCanvas* canvas) { - canvas->drawColor(fBGColor); -} - -/////////////////////////////////////////////////////////////////////////////// - template <typename T> void SkTBSort(T array[], int count) { for (int i = 1; i < count - 1; i++) { bool didSwap = false; diff --git a/samplecode/SampleAtlas.cpp b/samplecode/SampleAtlas.cpp index 7cf1bb3761..3daf312f6a 100644 --- a/samplecode/SampleAtlas.cpp +++ b/samplecode/SampleAtlas.cpp @@ -232,9 +232,11 @@ protected: void onDrawContent(SkCanvas* canvas) override { canvas->drawDrawable(fDrawable); - this->inval(nullptr); } + bool onAnimate(const SkAnimTimer&) override { + return true; + } #if 0 // TODO: switch over to use this for our animation bool onAnimate(const SkAnimTimer& timer) override { diff --git a/samplecode/SampleClock.cpp b/samplecode/SampleClock.cpp index ae7462d1dc..ff3a5b1927 100644 --- a/samplecode/SampleClock.cpp +++ b/samplecode/SampleClock.cpp @@ -215,8 +215,10 @@ protected: #endif canvas->restore(); + } - this->inval(nullptr); + bool onAnimate(const SkAnimTimer&) override { + return true; } private: diff --git a/samplecode/SampleCode.cpp b/samplecode/SampleCode.cpp new file mode 100644 index 0000000000..7b9c0ff223 --- /dev/null +++ b/samplecode/SampleCode.cpp @@ -0,0 +1,170 @@ +/* + * Copyright 2016 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 "SkCanvas.h" + +#if SK_SUPPORT_GPU +# include "GrContext.h" +# if SK_ANGLE +# include "gl/angle/GLTestContext_angle.h" +# endif +#else +class GrContext; +#endif + +////////////////////////////////////////////////////////////////////////////// + +bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) { + if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) { + if (outUni) { + *outUni = evt.getFast32(); + } + return true; + } + return false; +} + +bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) { + if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) { + if (outKey) { + *outKey = (SkKey)evt.getFast32(); + } + return true; + } + return false; +} + +bool SampleCode::TitleQ(const SkEvent& evt) { + return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1); +} + +void SampleCode::TitleR(SkEvent* evt, const char title[]) { + SkASSERT(evt && TitleQ(*evt)); + evt->setString(gTitleEvtName, title); +} + +bool SampleCode::RequestTitle(SkView* view, SkString* title) { + SkEvent evt(gTitleEvtName); + if (view->doQuery(&evt)) { + title->set(evt.findString(gTitleEvtName)); + return true; + } + return false; +} + +bool SampleCode::PrefSizeQ(const SkEvent& evt) { + return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1); +} + +void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) { + SkASSERT(evt && PrefSizeQ(*evt)); + SkScalar size[2]; + size[0] = width; + size[1] = height; + evt->setScalars(gPrefSizeEvtName, 2, size); +} + +bool SampleCode::FastTextQ(const SkEvent& evt) { + return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1); +} + +SkViewRegister* SkViewRegister::gHead; +SkViewRegister::SkViewRegister(SkViewFactory* fact) : fFact(fact) { + fFact->ref(); + fChain = gHead; + gHead = this; +} + +/////////////////////////////////////////////////////////////////////////////// + +SkFuncViewFactory::SkFuncViewFactory(SkViewCreateFunc func) + : fCreateFunc(func) { +} + +SkView* SkFuncViewFactory::operator() () const { + return (*fCreateFunc)(); +} + +#include "GMSampleView.h" + +SkGMSampleViewFactory::SkGMSampleViewFactory(GMFactoryFunc func) + : fFunc(func) { +} + +SkView* SkGMSampleViewFactory::operator() () const { + skiagm::GM* gm = fFunc(nullptr); + gm->setMode(skiagm::GM::kSample_Mode); + return new GMSampleView(gm); +} + +SkViewRegister::SkViewRegister(SkViewCreateFunc func) { + fFact = new SkFuncViewFactory(func); + fChain = gHead; + gHead = this; +} + +SkViewRegister::SkViewRegister(GMFactoryFunc func) { + fFact = new SkGMSampleViewFactory(func); + fChain = gHead; + gHead = this; +} + +/////////////////////////////////////////////////////////////////////////////// + +static const char is_sample_view_tag[] = "sample-is-sample-view"; +static const char repeat_count_tag[] = "sample-set-repeat-count"; + +bool SampleView::IsSampleView(SkView* view) { + SkEvent evt(is_sample_view_tag); + return view->doQuery(&evt); +} + +bool SampleView::SetRepeatDraw(SkView* view, int count) { + SkEvent evt(repeat_count_tag); + evt.setFast32(count); + return 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) { + if (evt->isType(is_sample_view_tag)) { + return true; + } + return this->INHERITED::onQuery(evt); +} + +void SampleView::onDraw(SkCanvas* canvas) { + if (!fHaveCalledOnceBeforeDraw) { + fHaveCalledOnceBeforeDraw = true; + this->onOnceBeforeDraw(); + } + this->onDrawBackground(canvas); + + for (int i = 0; i < fRepeatCount; i++) { + SkAutoCanvasRestore acr(canvas, true); + this->onDrawContent(canvas); +#if SK_SUPPORT_GPU + // Ensure the GrContext doesn't batch across draw loops. + if (GrContext* context = canvas->getGrContext()) { + context->flush(); + } +#endif + } +} + +void SampleView::onDrawBackground(SkCanvas* canvas) { + canvas->drawColor(fBGColor); +} + diff --git a/samplecode/SampleCode.h b/samplecode/SampleCode.h index e4ccfb2af6..fe72e4b970 100644 --- a/samplecode/SampleCode.h +++ b/samplecode/SampleCode.h @@ -21,6 +21,15 @@ class SkAnimTimer; static SkView* SK_MACRO_APPEND_LINE(F_)() { code } \ static SkViewRegister SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_)); +#define MAX_ZOOM_LEVEL 8 +#define MIN_ZOOM_LEVEL -8 + +static const char gCharEvtName[] = "SampleCode_Char_Event"; +static const char gKeyEvtName[] = "SampleCode_Key_Event"; +static const char gTitleEvtName[] = "SampleCode_Title_Event"; +static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event"; +static const char gFastTextEvtName[] = "SampleCode_FastText_Event"; +static const char gUpdateWindowTitleEvtName[] = "SampleCode_UpdateWindowTitle"; class SampleCode { public: |