blob: 62021069ca91e6c40e8949227cb42ae548ce376d [file] [log] [blame]
jvanverthc7027ab2016-06-16 09:52:35 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SampleCode.h"
9#include "SkCanvas.h"
Brian Osman8ceee432017-12-01 10:52:28 -050010#include "SkString.h"
jvanverthc7027ab2016-06-16 09:52:35 -070011
12#if SK_SUPPORT_GPU
13# include "GrContext.h"
jvanverthc7027ab2016-06-16 09:52:35 -070014#else
15class GrContext;
16#endif
17
18//////////////////////////////////////////////////////////////////////////////
19
20bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
Brian Osman8ceee432017-12-01 10:52:28 -050021 if (evt.isType(gCharEvtName)) {
jvanverthc7027ab2016-06-16 09:52:35 -070022 if (outUni) {
23 *outUni = evt.getFast32();
24 }
25 return true;
26 }
27 return false;
28}
29
jvanverthc7027ab2016-06-16 09:52:35 -070030bool SampleCode::TitleQ(const SkEvent& evt) {
Brian Osman8ceee432017-12-01 10:52:28 -050031 return evt.isType(gTitleEvtName);
jvanverthc7027ab2016-06-16 09:52:35 -070032}
33
34void SampleCode::TitleR(SkEvent* evt, const char title[]) {
35 SkASSERT(evt && TitleQ(*evt));
36 evt->setString(gTitleEvtName, title);
37}
38
39bool SampleCode::RequestTitle(SkView* view, SkString* title) {
40 SkEvent evt(gTitleEvtName);
41 if (view->doQuery(&evt)) {
42 title->set(evt.findString(gTitleEvtName));
43 return true;
44 }
45 return false;
46}
47
jvanverthc7027ab2016-06-16 09:52:35 -070048SkViewRegister* SkViewRegister::gHead;
49SkViewRegister::SkViewRegister(SkViewFactory* fact) : fFact(fact) {
50 fFact->ref();
51 fChain = gHead;
52 gHead = this;
53}
54
55///////////////////////////////////////////////////////////////////////////////
56
57SkFuncViewFactory::SkFuncViewFactory(SkViewCreateFunc func)
58 : fCreateFunc(func) {
59}
60
61SkView* SkFuncViewFactory::operator() () const {
62 return (*fCreateFunc)();
63}
64
jvanverthc7027ab2016-06-16 09:52:35 -070065SkViewRegister::SkViewRegister(SkViewCreateFunc func) {
66 fFact = new SkFuncViewFactory(func);
67 fChain = gHead;
68 gHead = this;
69}
70
jvanverthc7027ab2016-06-16 09:52:35 -070071///////////////////////////////////////////////////////////////////////////////
72
73static const char is_sample_view_tag[] = "sample-is-sample-view";
jvanverthc7027ab2016-06-16 09:52:35 -070074
75bool SampleView::IsSampleView(SkView* view) {
76 SkEvent evt(is_sample_view_tag);
77 return view->doQuery(&evt);
78}
79
jvanverthc7027ab2016-06-16 09:52:35 -070080bool SampleView::onQuery(SkEvent* evt) {
81 if (evt->isType(is_sample_view_tag)) {
82 return true;
83 }
84 return this->INHERITED::onQuery(evt);
85}
86
87void SampleView::onDraw(SkCanvas* canvas) {
88 if (!fHaveCalledOnceBeforeDraw) {
89 fHaveCalledOnceBeforeDraw = true;
90 this->onOnceBeforeDraw();
91 }
92 this->onDrawBackground(canvas);
93
Brian Osmanede860e2017-11-22 16:36:07 -050094 SkAutoCanvasRestore acr(canvas, true);
95 this->onDrawContent(canvas);
jvanverthc7027ab2016-06-16 09:52:35 -070096#if SK_SUPPORT_GPU
Brian Osmanede860e2017-11-22 16:36:07 -050097 // Ensure the GrContext doesn't combine GrDrawOps across draw loops.
98 if (GrContext* context = canvas->getGrContext()) {
99 context->flush();
jvanverthc7027ab2016-06-16 09:52:35 -0700100 }
Brian Osmanede860e2017-11-22 16:36:07 -0500101#endif
jvanverthc7027ab2016-06-16 09:52:35 -0700102}
103
104void SampleView::onDrawBackground(SkCanvas* canvas) {
105 canvas->drawColor(fBGColor);
106}
107