• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     sk_tool_utils::set_portable_typeface(&paint);
83     static const char kTxt[] = "GPU Only";
84     bmpCanvas.drawText(kTxt, strlen(kTxt), 20, 40, paint);
85     SkMatrix localM;
86     localM.setRotate(35.f);
87     localM.postTranslate(10.f, 0.f);
88     SkAutoTUnref<SkShader> shader(SkShader::CreateBitmapShader(bmp, SkShader::kMirror_TileMode,
89                                                                SkShader::kMirror_TileMode,
90                                                                &localM));
91     paint.setShader(shader);
92     paint.setFilterQuality(kMedium_SkFilterQuality);
93     canvas->drawPaint(paint);
94     return;
95 }
96 
97 // need to explicitly declare this, or we get some weird infinite loop llist
98 template GMRegistry* GMRegistry::gHead;
99 
onDraw(SkCanvas * canvas)100 void skiagm::SimpleGM::onDraw(SkCanvas* canvas) {
101     fDrawProc(canvas);
102 }
103 
onISize()104 SkISize skiagm::SimpleGM::onISize() {
105     return fSize;
106 }
107 
onShortName()108 SkString skiagm::SimpleGM::onShortName() {
109     return fName;
110 }
111 
112