reed | 0ab326f | 2015-02-21 09:36:50 -0800 | [diff] [blame] | 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) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 17 | SkEvent* evt = new SkEvent("GMSampleView::showSize"); |
reed | 0ab326f | 2015-02-21 09:36:50 -0800 | [diff] [blame] | 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 | } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 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 | |
reed | 0ab326f | 2015-02-21 09:36:50 -0800 | [diff] [blame] | 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 | |
reed | 0ccc62d | 2016-05-04 13:09:39 -0700 | [diff] [blame] | 49 | #include "SkPicture.h" |
reed | 0ccc62d | 2016-05-04 13:09:39 -0700 | [diff] [blame] | 50 | static sk_sp<SkPicture> round_trip_serialize(SkPicture* src) { |
reed | 39eaf5f | 2016-09-15 07:19:35 -0700 | [diff] [blame] | 51 | return SkPicture::MakeFromData(src->serialize().get()); |
reed | 0ccc62d | 2016-05-04 13:09:39 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | #include "SkPictureRecorder.h" |
reed | 0ab326f | 2015-02-21 09:36:50 -0800 | [diff] [blame] | 55 | void GMSampleView::onDrawContent(SkCanvas* canvas) { |
reed | 0ccc62d | 2016-05-04 13:09:39 -0700 | [diff] [blame] | 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 | |
reed | 0ab326f | 2015-02-21 09:36:50 -0800 | [diff] [blame] | 64 | { |
| 65 | SkAutoCanvasRestore acr(canvas, fShowSize); |
| 66 | fGM->drawContent(canvas); |
| 67 | } |
reed | 0ccc62d | 2016-05-04 13:09:39 -0700 | [diff] [blame] | 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 | |
reed | 0ab326f | 2015-02-21 09:36:50 -0800 | [diff] [blame] | 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 | } |