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