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