blob: c8f125093744e8ff415db2c9044d105c907c1d2c [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"
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000011using namespace skiagm;
12
13GM::GM() {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +000014 fMode = kGM_Mode;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000015 fBGColor = SK_ColorWHITE;
reed@google.comaef73612012-11-16 13:41:45 +000016 fCanvasIsDeferred = false;
reed@google.com7775d662012-11-27 15:15:58 +000017 fHaveCalledOnceBeforeDraw = false;
commit-bot@chromium.orgf4f9df42013-09-26 20:44:24 +000018 fStarterMatrix.reset();
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) {
24 this->drawBackground(canvas);
25 this->drawContent(canvas);
26}
27
28void GM::drawContent(SkCanvas* canvas) {
reed@google.com7775d662012-11-27 15:15:58 +000029 if (!fHaveCalledOnceBeforeDraw) {
30 fHaveCalledOnceBeforeDraw = true;
31 this->onOnceBeforeDraw();
32 }
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000033 this->onDraw(canvas);
34}
35
36void GM::drawBackground(SkCanvas* canvas) {
reed@google.com7775d662012-11-27 15:15:58 +000037 if (!fHaveCalledOnceBeforeDraw) {
38 fHaveCalledOnceBeforeDraw = true;
39 this->onOnceBeforeDraw();
40 }
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000041 this->onDrawBackground(canvas);
42}
43
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000044const char* GM::getName() {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000045 if (fShortName.size() == 0) {
46 fShortName = this->onShortName();
47 }
48 return fShortName.c_str();
49}
50
51void GM::setBGColor(SkColor color) {
52 fBGColor = color;
53}
54
reed76113a92015-02-02 12:55:02 -080055bool GM::animate(const SkAnimTimer& timer) {
56 return this->onAnimate(timer);
reedd9adfe62015-02-01 19:01:04 -080057}
58
59/////////////////////////////////////////////////////////////////////////////////////////////
60
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000061void GM::onDrawBackground(SkCanvas* canvas) {
reed374772b2016-10-05 17:33:02 -070062 canvas->drawColor(fBGColor, SkBlendMode::kSrc);
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000063}
64
reed@google.com2d6ef522012-01-03 17:20:38 +000065void GM::drawSizeBounds(SkCanvas* canvas, SkColor color) {
66 SkISize size = this->getISize();
67 SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()),
68 SkIntToScalar(size.height()));
69 SkPaint paint;
70 paint.setColor(color);
71 canvas->drawRect(r, paint);
72}
73
halcanary2a243382015-09-09 08:16:41 -070074void GM::DrawGpuOnlyMessage(SkCanvas* canvas) {
bsalomonb62da802015-01-31 07:51:14 -080075 SkBitmap bmp;
76 bmp.allocN32Pixels(128, 64);
77 SkCanvas bmpCanvas(bmp);
78 bmpCanvas.drawColor(SK_ColorWHITE);
79 SkPaint paint;
80 paint.setAntiAlias(true);
81 paint.setTextSize(20);
82 paint.setColor(SK_ColorRED);
caryclarkf597c422015-07-28 10:37:53 -070083 sk_tool_utils::set_portable_typeface(&paint);
mtkleindbfd7ab2016-09-01 11:24:54 -070084 constexpr char kTxt[] = "GPU Only";
bsalomonb62da802015-01-31 07:51:14 -080085 bmpCanvas.drawText(kTxt, strlen(kTxt), 20, 40, paint);
86 SkMatrix localM;
87 localM.setRotate(35.f);
88 localM.postTranslate(10.f, 0.f);
reed8a21c9f2016-03-08 18:50:00 -080089 paint.setShader(SkShader::MakeBitmapShader(bmp, SkShader::kMirror_TileMode,
90 SkShader::kMirror_TileMode,
91 &localM));
bsalomonb62da802015-01-31 07:51:14 -080092 paint.setFilterQuality(kMedium_SkFilterQuality);
93 canvas->drawPaint(paint);
94 return;
95}
96
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000097// need to explicitly declare this, or we get some weird infinite loop llist
tfarinabcbc1782014-06-18 14:32:48 -070098template GMRegistry* GMRegistry::gHead;
halcanaryf62c6342015-01-12 15:27:46 -080099
100void skiagm::SimpleGM::onDraw(SkCanvas* canvas) {
101 fDrawProc(canvas);
102}
103
104SkISize skiagm::SimpleGM::onISize() {
105 return fSize;
106}
107
108SkString skiagm::SimpleGM::onShortName() {
109 return fName;
110}