blob: 1b0598ff773588303a837255b05f9354ebba2291 [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"
bsalomonb62da802015-01-31 07:51:14 -08009#include "SkShader.h"
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000010using namespace skiagm;
11
12GM::GM() {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +000013 fMode = kGM_Mode;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000014 fBGColor = SK_ColorWHITE;
reed@google.comaef73612012-11-16 13:41:45 +000015 fCanvasIsDeferred = false;
reed@google.com7775d662012-11-27 15:15:58 +000016 fHaveCalledOnceBeforeDraw = false;
commit-bot@chromium.orgf4f9df42013-09-26 20:44:24 +000017 fStarterMatrix.reset();
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000018}
tfarina880914c2014-06-09 12:05:34 -070019
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000020GM::~GM() {}
21
22void GM::draw(SkCanvas* canvas) {
23 this->drawBackground(canvas);
24 this->drawContent(canvas);
25}
26
27void GM::drawContent(SkCanvas* canvas) {
reed@google.com7775d662012-11-27 15:15:58 +000028 if (!fHaveCalledOnceBeforeDraw) {
29 fHaveCalledOnceBeforeDraw = true;
30 this->onOnceBeforeDraw();
31 }
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000032 this->onDraw(canvas);
33}
34
35void GM::drawBackground(SkCanvas* canvas) {
reed@google.com7775d662012-11-27 15:15:58 +000036 if (!fHaveCalledOnceBeforeDraw) {
37 fHaveCalledOnceBeforeDraw = true;
38 this->onOnceBeforeDraw();
39 }
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000040 this->onDrawBackground(canvas);
41}
42
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000043const char* GM::getName() {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000044 if (fShortName.size() == 0) {
45 fShortName = this->onShortName();
46 }
47 return fShortName.c_str();
48}
49
50void GM::setBGColor(SkColor color) {
51 fBGColor = color;
52}
53
reed76113a92015-02-02 12:55:02 -080054bool GM::animate(const SkAnimTimer& timer) {
55 return this->onAnimate(timer);
reedd9adfe62015-02-01 19:01:04 -080056}
57
58/////////////////////////////////////////////////////////////////////////////////////////////
59
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000060void GM::onDrawBackground(SkCanvas* canvas) {
reed374772b2016-10-05 17:33:02 -070061 canvas->drawColor(fBGColor, SkBlendMode::kSrc);
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000062}
63
reed@google.com2d6ef522012-01-03 17:20:38 +000064void GM::drawSizeBounds(SkCanvas* canvas, SkColor color) {
65 SkISize size = this->getISize();
66 SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()),
67 SkIntToScalar(size.height()));
68 SkPaint paint;
69 paint.setColor(color);
70 canvas->drawRect(r, paint);
71}
72
halcanary2a243382015-09-09 08:16:41 -070073void GM::DrawGpuOnlyMessage(SkCanvas* canvas) {
bsalomonb62da802015-01-31 07:51:14 -080074 SkBitmap bmp;
75 bmp.allocN32Pixels(128, 64);
76 SkCanvas bmpCanvas(bmp);
77 bmpCanvas.drawColor(SK_ColorWHITE);
78 SkPaint paint;
79 paint.setAntiAlias(true);
80 paint.setTextSize(20);
81 paint.setColor(SK_ColorRED);
caryclarkf597c422015-07-28 10:37:53 -070082 sk_tool_utils::set_portable_typeface(&paint);
mtkleindbfd7ab2016-09-01 11:24:54 -070083 constexpr char kTxt[] = "GPU Only";
bsalomonb62da802015-01-31 07:51:14 -080084 bmpCanvas.drawText(kTxt, strlen(kTxt), 20, 40, paint);
85 SkMatrix localM;
86 localM.setRotate(35.f);
87 localM.postTranslate(10.f, 0.f);
reed8a21c9f2016-03-08 18:50:00 -080088 paint.setShader(SkShader::MakeBitmapShader(bmp, SkShader::kMirror_TileMode,
89 SkShader::kMirror_TileMode,
90 &localM));
bsalomonb62da802015-01-31 07:51:14 -080091 paint.setFilterQuality(kMedium_SkFilterQuality);
92 canvas->drawPaint(paint);
93 return;
94}
95
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000096// need to explicitly declare this, or we get some weird infinite loop llist
tfarinabcbc1782014-06-18 14:32:48 -070097template GMRegistry* GMRegistry::gHead;
halcanaryf62c6342015-01-12 15:27:46 -080098
99void skiagm::SimpleGM::onDraw(SkCanvas* canvas) {
100 fDrawProc(canvas);
101}
102
103SkISize skiagm::SimpleGM::onISize() {
104 return fSize;
105}
106
107SkString skiagm::SimpleGM::onShortName() {
108 return fName;
109}