blob: ffa33cedfc299a72fce3f006604e3cab1a17d325 [file] [log] [blame]
reed0ab326f2015-02-21 09:36:50 -08001/*
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 Reedc090c642017-05-16 10:39:06 -04009#include "SkData.h"
reed0ab326f2015-02-21 09:36:50 -080010
11GMSampleView::GMSampleView(GM* gm) : fShowSize(false), fGM(gm) {}
12
13GMSampleView::~GMSampleView() {
14 delete fGM;
15}
16
17SkEvent* GMSampleView::NewShowSizeEvt(bool doShowSize) {
halcanary385fe4d2015-08-26 13:07:48 -070018 SkEvent* evt = new SkEvent("GMSampleView::showSize");
reed0ab326f2015-02-21 09:36:50 -080019 evt->setFast32(doShowSize);
20 return evt;
21}
22
23bool 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 }
robertphillips05a4cf52016-09-08 09:02:43 -070030
31 SkUnichar uni;
32 if (SampleCode::CharQ(*evt, &uni)) {
33 if (fGM->handleKey(uni)) {
34 this->inval(nullptr);
35 return true;
36 }
37 }
38
reed0ab326f2015-02-21 09:36:50 -080039 return this->INHERITED::onQuery(evt);
40}
41
42bool 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
reed0ccc62d2016-05-04 13:09:39 -070050#include "SkPicture.h"
reed0ccc62d2016-05-04 13:09:39 -070051static sk_sp<SkPicture> round_trip_serialize(SkPicture* src) {
reed39eaf5f2016-09-15 07:19:35 -070052 return SkPicture::MakeFromData(src->serialize().get());
reed0ccc62d2016-05-04 13:09:39 -070053}
54
55#include "SkPictureRecorder.h"
reed0ab326f2015-02-21 09:36:50 -080056void GMSampleView::onDrawContent(SkCanvas* canvas) {
reed0ccc62d2016-05-04 13:09:39 -070057 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
reed0ab326f2015-02-21 09:36:50 -080065 {
66 SkAutoCanvasRestore acr(canvas, fShowSize);
67 fGM->drawContent(canvas);
68 }
reed0ccc62d2016-05-04 13:09:39 -070069
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
reed0ab326f2015-02-21 09:36:50 -080079 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
89void GMSampleView::onDrawBackground(SkCanvas* canvas) {
90 fGM->drawBackground(canvas);
91}
92
93bool GMSampleView::onAnimate(const SkAnimTimer& timer) {
94 return fGM->animate(timer);
95}