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