jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 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" |
Brian Osman | 8ceee43 | 2017-12-01 10:52:28 -0500 | [diff] [blame] | 10 | #include "SkString.h" |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 11 | |
| 12 | #if SK_SUPPORT_GPU |
| 13 | # include "GrContext.h" |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 14 | #else |
| 15 | class GrContext; |
| 16 | #endif |
| 17 | |
| 18 | ////////////////////////////////////////////////////////////////////////////// |
| 19 | |
| 20 | bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) { |
Brian Osman | 8ceee43 | 2017-12-01 10:52:28 -0500 | [diff] [blame] | 21 | if (evt.isType(gCharEvtName)) { |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 22 | if (outUni) { |
| 23 | *outUni = evt.getFast32(); |
| 24 | } |
| 25 | return true; |
| 26 | } |
| 27 | return false; |
| 28 | } |
| 29 | |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 30 | bool SampleCode::TitleQ(const SkEvent& evt) { |
Brian Osman | 8ceee43 | 2017-12-01 10:52:28 -0500 | [diff] [blame] | 31 | return evt.isType(gTitleEvtName); |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void SampleCode::TitleR(SkEvent* evt, const char title[]) { |
| 35 | SkASSERT(evt && TitleQ(*evt)); |
| 36 | evt->setString(gTitleEvtName, title); |
| 37 | } |
| 38 | |
| 39 | bool 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 | |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 48 | SkViewRegister* SkViewRegister::gHead; |
| 49 | SkViewRegister::SkViewRegister(SkViewFactory* fact) : fFact(fact) { |
| 50 | fFact->ref(); |
| 51 | fChain = gHead; |
| 52 | gHead = this; |
| 53 | } |
| 54 | |
| 55 | /////////////////////////////////////////////////////////////////////////////// |
| 56 | |
| 57 | SkFuncViewFactory::SkFuncViewFactory(SkViewCreateFunc func) |
| 58 | : fCreateFunc(func) { |
| 59 | } |
| 60 | |
| 61 | SkView* SkFuncViewFactory::operator() () const { |
| 62 | return (*fCreateFunc)(); |
| 63 | } |
| 64 | |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 65 | SkViewRegister::SkViewRegister(SkViewCreateFunc func) { |
| 66 | fFact = new SkFuncViewFactory(func); |
| 67 | fChain = gHead; |
| 68 | gHead = this; |
| 69 | } |
| 70 | |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 71 | /////////////////////////////////////////////////////////////////////////////// |
| 72 | |
| 73 | static const char is_sample_view_tag[] = "sample-is-sample-view"; |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 74 | |
| 75 | bool SampleView::IsSampleView(SkView* view) { |
| 76 | SkEvent evt(is_sample_view_tag); |
| 77 | return view->doQuery(&evt); |
| 78 | } |
| 79 | |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 80 | bool SampleView::onQuery(SkEvent* evt) { |
| 81 | if (evt->isType(is_sample_view_tag)) { |
| 82 | return true; |
| 83 | } |
| 84 | return this->INHERITED::onQuery(evt); |
| 85 | } |
| 86 | |
| 87 | void SampleView::onDraw(SkCanvas* canvas) { |
| 88 | if (!fHaveCalledOnceBeforeDraw) { |
| 89 | fHaveCalledOnceBeforeDraw = true; |
| 90 | this->onOnceBeforeDraw(); |
| 91 | } |
| 92 | this->onDrawBackground(canvas); |
| 93 | |
Brian Osman | ede860e | 2017-11-22 16:36:07 -0500 | [diff] [blame] | 94 | SkAutoCanvasRestore acr(canvas, true); |
| 95 | this->onDrawContent(canvas); |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 96 | #if SK_SUPPORT_GPU |
Brian Osman | ede860e | 2017-11-22 16:36:07 -0500 | [diff] [blame] | 97 | // Ensure the GrContext doesn't combine GrDrawOps across draw loops. |
| 98 | if (GrContext* context = canvas->getGrContext()) { |
| 99 | context->flush(); |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 100 | } |
Brian Osman | ede860e | 2017-11-22 16:36:07 -0500 | [diff] [blame] | 101 | #endif |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void SampleView::onDrawBackground(SkCanvas* canvas) { |
| 105 | canvas->drawColor(fBGColor); |
| 106 | } |
| 107 | |