aboutsummaryrefslogtreecommitdiffhomepage
path: root/samplecode/SampleCode.cpp
diff options
context:
space:
mode:
authorGravatar jvanverth <jvanverth@google.com>2016-06-16 09:52:35 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-06-16 09:52:35 -0700
commitc7027ab03f2e8bab4c74bc1b047642622d3d682b (patch)
tree72baa4e6345d6e3bbdb2b0165130a637a9778f19 /samplecode/SampleCode.cpp
parentc4ce6b592487305de251bbebaf8eeee38371b877 (diff)
Add samples to Viewer.
This adds support with animation, assuming the sample has implemented onAnimate. Event handling has not been implemented. BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2056343004 Committed: https://skia.googlesource.com/skia/+/76963e73704a42a18c29d6fbdcccb566e5c67658 Review-Url: https://codereview.chromium.org/2056343004
Diffstat (limited to 'samplecode/SampleCode.cpp')
-rw-r--r--samplecode/SampleCode.cpp170
1 files changed, 170 insertions, 0 deletions
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);
+}
+