blob: 19cbcc8b5763abc6a20222cf6572b41f5e0ea949 [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"
9
10GMSampleView::GMSampleView(GM* gm) : fShowSize(false), fGM(gm) {}
11
12GMSampleView::~GMSampleView() {
13 delete fGM;
14}
15
16SkEvent* GMSampleView::NewShowSizeEvt(bool doShowSize) {
halcanary385fe4d2015-08-26 13:07:48 -070017 SkEvent* evt = new SkEvent("GMSampleView::showSize");
reed0ab326f2015-02-21 09:36:50 -080018 evt->setFast32(doShowSize);
19 return evt;
20}
21
22bool 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 }
robertphillips05a4cf52016-09-08 09:02:43 -070029
30 SkUnichar uni;
31 if (SampleCode::CharQ(*evt, &uni)) {
32 if (fGM->handleKey(uni)) {
33 this->inval(nullptr);
34 return true;
35 }
36 }
37
reed0ab326f2015-02-21 09:36:50 -080038 return this->INHERITED::onQuery(evt);
39}
40
41bool 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
reed0ccc62d2016-05-04 13:09:39 -070049#include "SkPicture.h"
reed0ccc62d2016-05-04 13:09:39 -070050static sk_sp<SkPicture> round_trip_serialize(SkPicture* src) {
reed39eaf5f2016-09-15 07:19:35 -070051 return SkPicture::MakeFromData(src->serialize().get());
reed0ccc62d2016-05-04 13:09:39 -070052}
53
54#include "SkPictureRecorder.h"
reed0ab326f2015-02-21 09:36:50 -080055void GMSampleView::onDrawContent(SkCanvas* canvas) {
reed0ccc62d2016-05-04 13:09:39 -070056 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
reed0ab326f2015-02-21 09:36:50 -080064 {
65 SkAutoCanvasRestore acr(canvas, fShowSize);
66 fGM->drawContent(canvas);
67 }
reed0ccc62d2016-05-04 13:09:39 -070068
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
reed0ab326f2015-02-21 09:36:50 -080078 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
88void GMSampleView::onDrawBackground(SkCanvas* canvas) {
89 fGM->drawBackground(canvas);
90}
91
92bool GMSampleView::onAnimate(const SkAnimTimer& timer) {
93 return fGM->animate(timer);
94}