blob: a64c0e244af279e20299d42439289b019bc16cfa [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
124bool SampleView::SetRepeatDraw(SkView* view, int count) {
125 SkEvent evt(repeat_count_tag);
126 evt.setFast32(count);
127 return view->doEvent(evt);
128}
129
130bool SampleView::onEvent(const SkEvent& evt) {
131 if (evt.isType(repeat_count_tag)) {
132 fRepeatCount = evt.getFast32();
133 return true;
134 }
135 return this->INHERITED::onEvent(evt);
136}
137
138bool SampleView::onQuery(SkEvent* evt) {
139 if (evt->isType(is_sample_view_tag)) {
140 return true;
141 }
142 return this->INHERITED::onQuery(evt);
143}
144
145void SampleView::onDraw(SkCanvas* canvas) {
146 if (!fHaveCalledOnceBeforeDraw) {
147 fHaveCalledOnceBeforeDraw = true;
148 this->onOnceBeforeDraw();
149 }
150 this->onDrawBackground(canvas);
151
152 for (int i = 0; i < fRepeatCount; i++) {
153 SkAutoCanvasRestore acr(canvas, true);
154 this->onDrawContent(canvas);
155#if SK_SUPPORT_GPU
Brian Salomon09d994e2016-12-21 11:14:46 -0500156 // Ensure the GrContext doesn't combine GrDrawOps across draw loops.
jvanverthc7027ab2016-06-16 09:52:35 -0700157 if (GrContext* context = canvas->getGrContext()) {
158 context->flush();
159 }
160#endif
161 }
162}
163
164void SampleView::onDrawBackground(SkCanvas* canvas) {
165 canvas->drawColor(fBGColor);
166}
167