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