Home | History | Annotate | Download | only in samplecode
      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 
     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 
     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 
     39 bool SampleCode::TitleQ(const SkEvent& evt) {
     40     return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
     41 }
     42 
     43 void SampleCode::TitleR(SkEvent* evt, const char title[]) {
     44     SkASSERT(evt && TitleQ(*evt));
     45     evt->setString(gTitleEvtName, title);
     46 }
     47 
     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 
     57 bool SampleCode::PrefSizeQ(const SkEvent& evt) {
     58     return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
     59 }
     60 
     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 
     69 bool SampleCode::FastTextQ(const SkEvent& evt) {
     70     return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
     71 }
     72 
     73 SkViewRegister* SkViewRegister::gHead;
     74 SkViewRegister::SkViewRegister(SkViewFactory* fact) : fFact(fact) {
     75     fFact->ref();
     76     fChain = gHead;
     77     gHead = this;
     78 }
     79 
     80 ///////////////////////////////////////////////////////////////////////////////
     81 
     82 SkFuncViewFactory::SkFuncViewFactory(SkViewCreateFunc func)
     83     : fCreateFunc(func) {
     84 }
     85 
     86 SkView* SkFuncViewFactory::operator() () const {
     87     return (*fCreateFunc)();
     88 }
     89 
     90 #include "GMSampleView.h"
     91 
     92 SkGMSampleViewFactory::SkGMSampleViewFactory(GMFactoryFunc func)
     93     : fFunc(func) {
     94 }
     95 
     96 SkView* SkGMSampleViewFactory::operator() () const {
     97     skiagm::GM* gm = fFunc(nullptr);
     98     gm->setMode(skiagm::GM::kSample_Mode);
     99     return new GMSampleView(gm);
    100 }
    101 
    102 SkViewRegister::SkViewRegister(SkViewCreateFunc func) {
    103     fFact = new SkFuncViewFactory(func);
    104     fChain = gHead;
    105     gHead = this;
    106 }
    107 
    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 
    119 bool SampleView::IsSampleView(SkView* view) {
    120     SkEvent evt(is_sample_view_tag);
    121     return view->doQuery(&evt);
    122 }
    123 
    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 
    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 
    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 
    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 
    164 void SampleView::onDrawBackground(SkCanvas* canvas) {
    165     canvas->drawColor(fBGColor);
    166 }
    167 
    168