• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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"
13 #else
14 class GrContext;
15 #endif
16 
17 //////////////////////////////////////////////////////////////////////////////
18 
CharQ(const SkEvent & evt,SkUnichar * outUni)19 bool 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 
KeyQ(const SkEvent & evt,SkKey * outKey)29 bool 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 
TitleQ(const SkEvent & evt)39 bool SampleCode::TitleQ(const SkEvent& evt) {
40     return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
41 }
42 
TitleR(SkEvent * evt,const char title[])43 void SampleCode::TitleR(SkEvent* evt, const char title[]) {
44     SkASSERT(evt && TitleQ(*evt));
45     evt->setString(gTitleEvtName, title);
46 }
47 
RequestTitle(SkView * view,SkString * title)48 bool 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 
PrefSizeQ(const SkEvent & evt)57 bool SampleCode::PrefSizeQ(const SkEvent& evt) {
58     return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
59 }
60 
PrefSizeR(SkEvent * evt,SkScalar width,SkScalar height)61 void 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 
FastTextQ(const SkEvent & evt)69 bool SampleCode::FastTextQ(const SkEvent& evt) {
70     return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
71 }
72 
73 SkViewRegister* SkViewRegister::gHead;
SkViewRegister(SkViewFactory * fact)74 SkViewRegister::SkViewRegister(SkViewFactory* fact) : fFact(fact) {
75     fFact->ref();
76     fChain = gHead;
77     gHead = this;
78 }
79 
80 ///////////////////////////////////////////////////////////////////////////////
81 
SkFuncViewFactory(SkViewCreateFunc func)82 SkFuncViewFactory::SkFuncViewFactory(SkViewCreateFunc func)
83     : fCreateFunc(func) {
84 }
85 
operator ()() const86 SkView* SkFuncViewFactory::operator() () const {
87     return (*fCreateFunc)();
88 }
89 
90 #include "GMSampleView.h"
91 
SkGMSampleViewFactory(GMFactoryFunc func)92 SkGMSampleViewFactory::SkGMSampleViewFactory(GMFactoryFunc func)
93     : fFunc(func) {
94 }
95 
operator ()() const96 SkView* SkGMSampleViewFactory::operator() () const {
97     skiagm::GM* gm = fFunc(nullptr);
98     gm->setMode(skiagm::GM::kSample_Mode);
99     return new GMSampleView(gm);
100 }
101 
SkViewRegister(SkViewCreateFunc func)102 SkViewRegister::SkViewRegister(SkViewCreateFunc func) {
103     fFact = new SkFuncViewFactory(func);
104     fChain = gHead;
105     gHead = this;
106 }
107 
SkViewRegister(GMFactoryFunc func)108 SkViewRegister::SkViewRegister(GMFactoryFunc func) {
109     fFact = new SkGMSampleViewFactory(func);
110     fChain = gHead;
111     gHead = this;
112 }
113 
114 ///////////////////////////////////////////////////////////////////////////////
115 
116 static const char is_sample_view_tag[] = "sample-is-sample-view";
117 static const char repeat_count_tag[] = "sample-set-repeat-count";
118 
IsSampleView(SkView * view)119 bool SampleView::IsSampleView(SkView* view) {
120     SkEvent evt(is_sample_view_tag);
121     return view->doQuery(&evt);
122 }
123 
SetRepeatDraw(SkView * view,int count)124 bool SampleView::SetRepeatDraw(SkView* view, int count) {
125     SkEvent evt(repeat_count_tag);
126     evt.setFast32(count);
127     return view->doEvent(evt);
128 }
129 
onEvent(const SkEvent & evt)130 bool 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 
onQuery(SkEvent * evt)138 bool SampleView::onQuery(SkEvent* evt) {
139     if (evt->isType(is_sample_view_tag)) {
140         return true;
141     }
142     return this->INHERITED::onQuery(evt);
143 }
144 
onDraw(SkCanvas * canvas)145 void 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
156         // Ensure the GrContext doesn't combine GrDrawOps across draw loops.
157         if (GrContext* context = canvas->getGrContext()) {
158             context->flush();
159         }
160 #endif
161     }
162 }
163 
onDrawBackground(SkCanvas * canvas)164 void SampleView::onDrawBackground(SkCanvas* canvas) {
165     canvas->drawColor(fBGColor);
166 }
167 
168