1 /*
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"
9 #include "SkShader.h"
10 using namespace skiagm;
11
GM()12 GM::GM() {
13 fMode = kGM_Mode;
14 fBGColor = SK_ColorWHITE;
15 fCanvasIsDeferred = false;
16 fHaveCalledOnceBeforeDraw = false;
17 fStarterMatrix.reset();
18 }
19
~GM()20 GM::~GM() {}
21
draw(SkCanvas * canvas)22 void GM::draw(SkCanvas* canvas) {
23 this->drawBackground(canvas);
24 this->drawContent(canvas);
25 }
26
drawContent(SkCanvas * canvas)27 void GM::drawContent(SkCanvas* canvas) {
28 if (!fHaveCalledOnceBeforeDraw) {
29 fHaveCalledOnceBeforeDraw = true;
30 this->onOnceBeforeDraw();
31 }
32 this->onDraw(canvas);
33 }
34
drawBackground(SkCanvas * canvas)35 void GM::drawBackground(SkCanvas* canvas) {
36 if (!fHaveCalledOnceBeforeDraw) {
37 fHaveCalledOnceBeforeDraw = true;
38 this->onOnceBeforeDraw();
39 }
40 this->onDrawBackground(canvas);
41 }
42
getName()43 const char* GM::getName() {
44 if (fShortName.size() == 0) {
45 fShortName = this->onShortName();
46 }
47 return fShortName.c_str();
48 }
49
setBGColor(SkColor color)50 void GM::setBGColor(SkColor color) {
51 fBGColor = color;
52 }
53
animate(const SkAnimTimer & timer)54 bool GM::animate(const SkAnimTimer& timer) {
55 return this->onAnimate(timer);
56 }
57
58 /////////////////////////////////////////////////////////////////////////////////////////////
59
onDrawBackground(SkCanvas * canvas)60 void GM::onDrawBackground(SkCanvas* canvas) {
61 canvas->drawColor(fBGColor, SkXfermode::kSrc_Mode);
62 }
63
drawSizeBounds(SkCanvas * canvas,SkColor color)64 void 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
drawGpuOnlyMessage(SkCanvas * canvas)73 void GM::drawGpuOnlyMessage(SkCanvas* canvas) {
74 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);
82 static const char kTxt[] = "GPU Only";
83 bmpCanvas.drawText(kTxt, strlen(kTxt), 20, 40, paint);
84 SkMatrix localM;
85 localM.setRotate(35.f);
86 localM.postTranslate(10.f, 0.f);
87 SkAutoTUnref<SkShader> shader(SkShader::CreateBitmapShader(bmp, SkShader::kMirror_TileMode,
88 SkShader::kMirror_TileMode,
89 &localM));
90 paint.setShader(shader);
91 paint.setFilterQuality(kMedium_SkFilterQuality);
92 canvas->drawPaint(paint);
93 return;
94 }
95
96 // need to explicitly declare this, or we get some weird infinite loop llist
97 template GMRegistry* GMRegistry::gHead;
98
onDraw(SkCanvas * canvas)99 void skiagm::SimpleGM::onDraw(SkCanvas* canvas) {
100 fDrawProc(canvas);
101 }
102
onISize()103 SkISize skiagm::SimpleGM::onISize() {
104 return fSize;
105 }
106
onShortName()107 SkString skiagm::SimpleGM::onShortName() {
108 return fName;
109 }
110
111