blob: 17b220a1917ff315aa9b4b06cb3f21222e947016 [file] [log] [blame]
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001/*
2 * Copyright 2011 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 "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
bsalomonb62da802015-01-31 07:51:14 -080010#include "SkShader.h"
Mike Kleinb323a5e2017-07-24 15:21:31 -040011#include "SkTraceEvent.h"
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000012using namespace skiagm;
13
14GM::GM() {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +000015 fMode = kGM_Mode;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000016 fBGColor = SK_ColorWHITE;
reed@google.comaef73612012-11-16 13:41:45 +000017 fCanvasIsDeferred = false;
reed@google.com7775d662012-11-27 15:15:58 +000018 fHaveCalledOnceBeforeDraw = false;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000019}
tfarina880914c2014-06-09 12:05:34 -070020
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000021GM::~GM() {}
22
23void GM::draw(SkCanvas* canvas) {
Mike Kleinb323a5e2017-07-24 15:21:31 -040024 TRACE_EVENT1("GM", TRACE_FUNC, "name", TRACE_STR_COPY(this->getName()));
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000025 this->drawBackground(canvas);
26 this->drawContent(canvas);
27}
28
29void GM::drawContent(SkCanvas* canvas) {
Mike Kleinb323a5e2017-07-24 15:21:31 -040030 TRACE_EVENT0("GM", TRACE_FUNC);
reed@google.com7775d662012-11-27 15:15:58 +000031 if (!fHaveCalledOnceBeforeDraw) {
32 fHaveCalledOnceBeforeDraw = true;
33 this->onOnceBeforeDraw();
34 }
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000035 this->onDraw(canvas);
36}
37
38void GM::drawBackground(SkCanvas* canvas) {
Mike Kleinb323a5e2017-07-24 15:21:31 -040039 TRACE_EVENT0("GM", TRACE_FUNC);
reed@google.com7775d662012-11-27 15:15:58 +000040 if (!fHaveCalledOnceBeforeDraw) {
41 fHaveCalledOnceBeforeDraw = true;
42 this->onOnceBeforeDraw();
43 }
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000044 this->onDrawBackground(canvas);
45}
46
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000047const char* GM::getName() {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000048 if (fShortName.size() == 0) {
49 fShortName = this->onShortName();
50 }
51 return fShortName.c_str();
52}
53
54void GM::setBGColor(SkColor color) {
55 fBGColor = color;
56}
57
reed76113a92015-02-02 12:55:02 -080058bool GM::animate(const SkAnimTimer& timer) {
59 return this->onAnimate(timer);
reedd9adfe62015-02-01 19:01:04 -080060}
61
62/////////////////////////////////////////////////////////////////////////////////////////////
63
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000064void GM::onDrawBackground(SkCanvas* canvas) {
reed374772b2016-10-05 17:33:02 -070065 canvas->drawColor(fBGColor, SkBlendMode::kSrc);
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000066}
67
reed@google.com2d6ef522012-01-03 17:20:38 +000068void GM::drawSizeBounds(SkCanvas* canvas, SkColor color) {
69 SkISize size = this->getISize();
70 SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()),
71 SkIntToScalar(size.height()));
72 SkPaint paint;
73 paint.setColor(color);
74 canvas->drawRect(r, paint);
75}
76
halcanary2a243382015-09-09 08:16:41 -070077void GM::DrawGpuOnlyMessage(SkCanvas* canvas) {
bsalomonb62da802015-01-31 07:51:14 -080078 SkBitmap bmp;
79 bmp.allocN32Pixels(128, 64);
80 SkCanvas bmpCanvas(bmp);
81 bmpCanvas.drawColor(SK_ColorWHITE);
82 SkPaint paint;
83 paint.setAntiAlias(true);
84 paint.setTextSize(20);
85 paint.setColor(SK_ColorRED);
caryclarkf597c422015-07-28 10:37:53 -070086 sk_tool_utils::set_portable_typeface(&paint);
mtkleindbfd7ab2016-09-01 11:24:54 -070087 constexpr char kTxt[] = "GPU Only";
Cary Clark2a475ea2017-04-28 15:35:12 -040088 bmpCanvas.drawString(kTxt, 20, 40, paint);
bsalomonb62da802015-01-31 07:51:14 -080089 SkMatrix localM;
90 localM.setRotate(35.f);
91 localM.postTranslate(10.f, 0.f);
reed8a21c9f2016-03-08 18:50:00 -080092 paint.setShader(SkShader::MakeBitmapShader(bmp, SkShader::kMirror_TileMode,
93 SkShader::kMirror_TileMode,
94 &localM));
bsalomonb62da802015-01-31 07:51:14 -080095 paint.setFilterQuality(kMedium_SkFilterQuality);
96 canvas->drawPaint(paint);
97 return;
98}
99
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000100// need to explicitly declare this, or we get some weird infinite loop llist
tfarinabcbc1782014-06-18 14:32:48 -0700101template GMRegistry* GMRegistry::gHead;
halcanaryf62c6342015-01-12 15:27:46 -0800102
103void skiagm::SimpleGM::onDraw(SkCanvas* canvas) {
104 fDrawProc(canvas);
105}
106
107SkISize skiagm::SimpleGM::onISize() {
108 return fSize;
109}
110
111SkString skiagm::SimpleGM::onShortName() {
112 return fName;
113}