Home | History | Annotate | Download | only in samplecode
      1 /*
      2  * Copyright 2015 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 "GMSampleView.h"
      9 
     10 GMSampleView::GMSampleView(GM* gm) : fShowSize(false), fGM(gm) {}
     11 
     12 GMSampleView::~GMSampleView() {
     13     delete fGM;
     14 }
     15 
     16 SkEvent* GMSampleView::NewShowSizeEvt(bool doShowSize) {
     17     SkEvent* evt = new SkEvent("GMSampleView::showSize");
     18     evt->setFast32(doShowSize);
     19     return evt;
     20 }
     21 
     22 bool GMSampleView::onQuery(SkEvent* evt) {
     23     if (SampleCode::TitleQ(*evt)) {
     24         SkString name("GM:");
     25         name.append(fGM->getName());
     26         SampleCode::TitleR(evt, name.c_str());
     27         return true;
     28     }
     29 
     30     SkUnichar uni;
     31     if (SampleCode::CharQ(*evt, &uni)) {
     32         if (fGM->handleKey(uni)) {
     33             this->inval(nullptr);
     34             return true;
     35         }
     36     }
     37 
     38     return this->INHERITED::onQuery(evt);
     39 }
     40 
     41 bool GMSampleView::onEvent(const SkEvent& evt) {
     42     if (evt.isType("GMSampleView::showSize")) {
     43         fShowSize = SkToBool(evt.getFast32());
     44         return true;
     45     }
     46     return this->INHERITED::onEvent(evt);
     47 }
     48 
     49 #include "SkPicture.h"
     50 static sk_sp<SkPicture> round_trip_serialize(SkPicture* src) {
     51     return SkPicture::MakeFromData(src->serialize().get());
     52 }
     53 
     54 #include "SkPictureRecorder.h"
     55 void GMSampleView::onDrawContent(SkCanvas* canvas) {
     56     SkPictureRecorder recorder;
     57     SkCanvas* origCanvas = canvas;
     58 
     59     if (false) {
     60         SkISize size = fGM->getISize();
     61         canvas = recorder.beginRecording(SkRect::MakeIWH(size.width(), size.height()));
     62     }
     63 
     64     {
     65         SkAutoCanvasRestore acr(canvas, fShowSize);
     66         fGM->drawContent(canvas);
     67     }
     68 
     69     if (origCanvas != canvas) {
     70         sk_sp<SkPicture> pic = recorder.finishRecordingAsPicture();
     71         if (false) {
     72             pic = round_trip_serialize(pic.get());
     73         }
     74         origCanvas->drawPicture(pic);
     75         canvas = origCanvas;
     76     }
     77 
     78     if (fShowSize) {
     79         SkISize size = fGM->getISize();
     80         SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()),
     81                                   SkIntToScalar(size.height()));
     82         SkPaint paint;
     83         paint.setColor(0x40FF8833);
     84         canvas->drawRect(r, paint);
     85     }
     86 }
     87 
     88 void GMSampleView::onDrawBackground(SkCanvas* canvas) {
     89     fGM->drawBackground(canvas);
     90 }
     91 
     92 bool GMSampleView::onAnimate(const SkAnimTimer& timer) {
     93     return fGM->animate(timer);
     94 }
     95