blob: 84c3945ba2352f620032918ce14deabbb69c9e88 [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"
10
11#if SK_SUPPORT_GPU
12# include "GrContext.h"
jvanverthc7027ab2016-06-16 09:52:35 -070013#else
14class GrContext;
15#endif
16
17//////////////////////////////////////////////////////////////////////////////
18
19bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
20 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
21 if (outUni) {
22 *outUni = evt.getFast32();
23 }
24 return true;
25 }
26 return false;
27}
28
29bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
30 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
31 if (outKey) {
32 *outKey = (SkKey)evt.getFast32();
33 }
34 return true;
35 }
36 return false;
37}
38
39bool SampleCode::TitleQ(const SkEvent& evt) {
40 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
41}
42
43void SampleCode::TitleR(SkEvent* evt, const char title[]) {
44 SkASSERT(evt && TitleQ(*evt));
45 evt->setString(gTitleEvtName, title);
46}
47
48bool SampleCode::RequestTitle(SkView* view, SkString* title) {
49 SkEvent evt(gTitleEvtName);
50 if (view->doQuery(&evt)) {
51 title->set(evt.findString(gTitleEvtName));
52 return true;
53 }
54 return false;
55}
56
57bool SampleCode::PrefSizeQ(const SkEvent& evt) {
58 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
59}
60
61void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
62 SkASSERT(evt && PrefSizeQ(*evt));
63 SkScalar size[2];
64 size[0] = width;
65 size[1] = height;
66 evt->setScalars(gPrefSizeEvtName, 2, size);
67}
68
69bool SampleCode::FastTextQ(const SkEvent& evt) {
70 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
71}
72
73SkViewRegister* SkViewRegister::gHead;
74SkViewRegister::SkViewRegister(SkViewFactory* fact) : fFact(fact) {
75 fFact->ref();
76 fChain = gHead;
77 gHead = this;
78}
79
80///////////////////////////////////////////////////////////////////////////////
81
82SkFuncViewFactory::SkFuncViewFactory(SkViewCreateFunc func)
83 : fCreateFunc(func) {
84}
85
86SkView* SkFuncViewFactory::operator() () const {
87 return (*fCreateFunc)();
88}
89
90#include "GMSampleView.h"
91
92SkGMSampleViewFactory::SkGMSampleViewFactory(GMFactoryFunc func)
93 : fFunc(func) {
94}
95
96SkView* SkGMSampleViewFactory::operator() () const {
97 skiagm::GM* gm = fFunc(nullptr);
98 gm->setMode(skiagm::GM::kSample_Mode);
99 return new GMSampleView(gm);
100}
101
102SkViewRegister::SkViewRegister(SkViewCreateFunc func) {
103 fFact = new SkFuncViewFactory(func);
104 fChain = gHead;
105 gHead = this;
106}
107
108SkViewRegister::SkViewRegister(GMFactoryFunc func) {
109 fFact = new SkGMSampleViewFactory(func);
110 fChain = gHead;
111 gHead = this;
112}
113
114///////////////////////////////////////////////////////////////////////////////
115
116static const char is_sample_view_tag[] = "sample-is-sample-view";
117static const char repeat_count_tag[] = "sample-set-repeat-count";
118
119bool SampleView::IsSampleView(SkView* view) {
120 SkEvent evt(is_sample_view_tag);
121 return view->doQuery(&evt);
122}
123
jvanverthc7027ab2016-06-16 09:52:35 -0700124bool SampleView::onEvent(const SkEvent& evt) {
125 if (evt.isType(repeat_count_tag)) {
126 fRepeatCount = evt.getFast32();
127 return true;
128 }
129 return this->INHERITED::onEvent(evt);
130}
131
132bool SampleView::onQuery(SkEvent* evt) {
133 if (evt->isType(is_sample_view_tag)) {
134 return true;
135 }
136 return this->INHERITED::onQuery(evt);
137}
138
139void SampleView::onDraw(SkCanvas* canvas) {
140 if (!fHaveCalledOnceBeforeDraw) {
141 fHaveCalledOnceBeforeDraw = true;
142 this->onOnceBeforeDraw();
143 }
144 this->onDrawBackground(canvas);
145
146 for (int i = 0; i < fRepeatCount; i++) {
147 SkAutoCanvasRestore acr(canvas, true);
148 this->onDrawContent(canvas);
149#if SK_SUPPORT_GPU
Brian Salomon09d994e2016-12-21 11:14:46 -0500150 // Ensure the GrContext doesn't combine GrDrawOps across draw loops.
jvanverthc7027ab2016-06-16 09:52:35 -0700151 if (GrContext* context = canvas->getGrContext()) {
152 context->flush();
153 }
154#endif
155 }
156}
157
158void SampleView::onDrawBackground(SkCanvas* canvas) {
159 canvas->drawColor(fBGColor);
160}
161