• 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/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkFilterQuality.h"
13 #include "include/core/SkFont.h"
14 #include "include/core/SkFontTypes.h"
15 #include "include/core/SkMatrix.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkShader.h"
19 #include "include/core/SkTileMode.h"
20 #include "include/core/SkTypeface.h"
21 #include "include/gpu/GrContext.h"
22 #include "src/core/SkTraceEvent.h"
23 #include "tools/ToolUtils.h"
24 
25 #include <stdarg.h>
26 
27 class GrRenderTargetContext;
28 
29 using namespace skiagm;
30 
31 constexpr char GM::kErrorMsg_DrawSkippedGpuOnly[];
32 
draw_failure_message(SkCanvas * canvas,const char format[],...)33 static void draw_failure_message(SkCanvas* canvas, const char format[], ...)  {
34     SkString failureMsg;
35 
36     va_list argp;
37     va_start(argp, format);
38     failureMsg.appendVAList(format, argp);
39     va_end(argp);
40 
41     constexpr SkScalar kOffset = 5.0f;
42     canvas->drawColor(SkColorSetRGB(200,0,0));
43     SkFont font;
44     SkRect bounds;
45     font.measureText(failureMsg.c_str(), failureMsg.size(), SkTextEncoding::kUTF8, &bounds);
46     SkPaint textPaint(SkColors::kWhite);
47     canvas->drawString(failureMsg, kOffset, bounds.height() + kOffset, font, textPaint);
48 }
49 
draw_gpu_only_message(SkCanvas * canvas)50 static void draw_gpu_only_message(SkCanvas* canvas) {
51     SkBitmap bmp;
52     bmp.allocN32Pixels(128, 64);
53     SkCanvas bmpCanvas(bmp);
54     bmpCanvas.drawColor(SK_ColorWHITE);
55     SkFont  font(ToolUtils::create_portable_typeface(), 20);
56     SkPaint paint(SkColors::kRed);
57     bmpCanvas.drawString("GPU Only", 20, 40, font, paint);
58     SkMatrix localM;
59     localM.setRotate(35.f);
60     localM.postTranslate(10.f, 0.f);
61     paint.setShader(bmp.makeShader(SkTileMode::kMirror, SkTileMode::kMirror, &localM));
62     paint.setFilterQuality(kMedium_SkFilterQuality);
63     canvas->drawPaint(paint);
64 }
65 
GM(SkColor bgColor)66 GM::GM(SkColor bgColor) {
67     fMode = kGM_Mode;
68     fBGColor = bgColor;
69     fCanvasIsDeferred = false;
70     fHaveCalledOnceBeforeDraw = false;
71 }
72 
~GM()73 GM::~GM() {}
74 
draw(SkCanvas * canvas,SkString * errorMsg)75 DrawResult GM::draw(SkCanvas* canvas, SkString* errorMsg) {
76     TRACE_EVENT1("GM", TRACE_FUNC, "name", TRACE_STR_COPY(this->getName()));
77     this->drawBackground(canvas);
78     return this->drawContent(canvas, errorMsg);
79 }
80 
drawContent(SkCanvas * canvas,SkString * errorMsg)81 DrawResult GM::drawContent(SkCanvas* canvas, SkString* errorMsg) {
82     TRACE_EVENT0("GM", TRACE_FUNC);
83     if (!fHaveCalledOnceBeforeDraw) {
84         fHaveCalledOnceBeforeDraw = true;
85         this->onOnceBeforeDraw();
86     }
87     SkAutoCanvasRestore acr(canvas, true);
88     DrawResult drawResult = this->onDraw(canvas, errorMsg);
89     if (DrawResult::kOk != drawResult) {
90         if (DrawResult::kFail == drawResult) {
91             draw_failure_message(canvas, "DRAW FAILED: %s", errorMsg->c_str());
92         } else if (SkString(kErrorMsg_DrawSkippedGpuOnly) == *errorMsg) {
93             draw_gpu_only_message(canvas);
94         } else {
95             draw_failure_message(canvas, "DRAW SKIPPED: %s", errorMsg->c_str());
96         }
97     }
98     return drawResult;
99 }
100 
drawBackground(SkCanvas * canvas)101 void GM::drawBackground(SkCanvas* canvas) {
102     TRACE_EVENT0("GM", TRACE_FUNC);
103     if (!fHaveCalledOnceBeforeDraw) {
104         fHaveCalledOnceBeforeDraw = true;
105         this->onOnceBeforeDraw();
106     }
107     SkAutoCanvasRestore acr(canvas, true);
108     canvas->drawColor(fBGColor, SkBlendMode::kSrc);
109 }
110 
onDraw(SkCanvas * canvas,SkString * errorMsg)111 DrawResult GM::onDraw(SkCanvas* canvas, SkString* errorMsg) {
112     this->onDraw(canvas);
113     return DrawResult::kOk;
114 }
onDraw(SkCanvas *)115 void GM::onDraw(SkCanvas*) { SK_ABORT("Not implemented."); }
116 
117 
onISize()118 SkISize SimpleGM::onISize() { return fSize; }
onShortName()119 SkString SimpleGM::onShortName() { return fName; }
onDraw(SkCanvas * canvas,SkString * errorMsg)120 DrawResult SimpleGM::onDraw(SkCanvas* canvas, SkString* errorMsg) {
121     return fDrawProc(canvas, errorMsg);
122 }
123 
onISize()124 SkISize SimpleGpuGM::onISize() { return fSize; }
onShortName()125 SkString SimpleGpuGM::onShortName() { return fName; }
onDraw(GrContext * ctx,GrRenderTargetContext * rtc,SkCanvas * canvas,SkString * errorMsg)126 DrawResult SimpleGpuGM::onDraw(GrContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas,
127                                SkString* errorMsg) {
128     return fDrawProc(ctx, rtc, canvas, errorMsg);
129 }
130 
getName()131 const char* GM::getName() {
132     if (fShortName.size() == 0) {
133         fShortName = this->onShortName();
134     }
135     return fShortName.c_str();
136 }
137 
setBGColor(SkColor color)138 void GM::setBGColor(SkColor color) {
139     fBGColor = color;
140 }
141 
animate(double nanos)142 bool GM::animate(double nanos) { return this->onAnimate(nanos); }
143 
runAsBench() const144 bool GM::runAsBench() const { return false; }
modifyGrContextOptions(GrContextOptions * options)145 void GM::modifyGrContextOptions(GrContextOptions* options) {}
146 
onOnceBeforeDraw()147 void GM::onOnceBeforeDraw() {}
148 
onAnimate(double)149 bool GM::onAnimate(double /*nanos*/) { return false; }
150 
onChar(SkUnichar uni)151 bool GM::onChar(SkUnichar uni) { return false; }
152 
onGetControls(SkMetaData *)153 bool GM::onGetControls(SkMetaData*) { return false; }
154 
onSetControls(const SkMetaData &)155 void GM::onSetControls(const SkMetaData&) {}
156 
157 /////////////////////////////////////////////////////////////////////////////////////////////
158 
drawSizeBounds(SkCanvas * canvas,SkColor color)159 void GM::drawSizeBounds(SkCanvas* canvas, SkColor color) {
160     canvas->drawRect(SkRect::Make(this->getISize()), SkPaint(SkColor4f::FromColor(color)));
161 }
162 
163 // need to explicitly declare this, or we get some weird infinite loop llist
164 template GMRegistry* GMRegistry::gHead;
165 
onDraw(GrContext * ctx,GrRenderTargetContext * rtc,SkCanvas * canvas,SkString * errorMsg)166 DrawResult GpuGM::onDraw(GrContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas,
167                           SkString* errorMsg) {
168     this->onDraw(ctx, rtc, canvas);
169     return DrawResult::kOk;
170 }
onDraw(GrContext *,GrRenderTargetContext *,SkCanvas *)171 void GpuGM::onDraw(GrContext*, GrRenderTargetContext*, SkCanvas*) {
172     SK_ABORT("Not implemented.");
173 }
174 
onDraw(SkCanvas * canvas,SkString * errorMsg)175 DrawResult GpuGM::onDraw(SkCanvas* canvas, SkString* errorMsg) {
176     GrContext* ctx = canvas->getGrContext();
177     GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext();
178     if (!ctx || !rtc) {
179         *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
180         return DrawResult::kSkip;
181     }
182     if (ctx->abandoned()) {
183         *errorMsg = "GrContext abandoned.";
184         return DrawResult::kSkip;
185     }
186     return this->onDraw(ctx, rtc, canvas, errorMsg);
187 }
188 
189 template <typename Fn>
mark(SkCanvas * canvas,SkScalar x,SkScalar y,Fn && fn)190 static void mark(SkCanvas* canvas, SkScalar x, SkScalar y, Fn&& fn) {
191     SkPaint alpha;
192     alpha.setAlpha(0x50);
193     canvas->saveLayer(nullptr, &alpha);
194         canvas->translate(x,y);
195         canvas->scale(2,2);
196         fn();
197     canvas->restore();
198 }
199 
MarkGMGood(SkCanvas * canvas,SkScalar x,SkScalar y)200 void MarkGMGood(SkCanvas* canvas, SkScalar x, SkScalar y) {
201     mark(canvas, x,y, [&]{
202         // A green circle.
203         canvas->drawCircle(0, 0, 12, SkPaint(SkColor4f::FromColor(SkColorSetRGB(27, 158, 119))));
204 
205         // Cut out a check mark.
206         SkPaint paint(SkColors::kTransparent);
207         paint.setBlendMode(SkBlendMode::kSrc);
208         paint.setStrokeWidth(2);
209         paint.setStyle(SkPaint::kStroke_Style);
210         canvas->drawLine(-6, 0,
211                          -1, 5, paint);
212         canvas->drawLine(-1, +5,
213                          +7, -5, paint);
214     });
215 }
216 
MarkGMBad(SkCanvas * canvas,SkScalar x,SkScalar y)217 void MarkGMBad(SkCanvas* canvas, SkScalar x, SkScalar y) {
218     mark(canvas, x,y, [&] {
219         // A red circle.
220         canvas->drawCircle(0,0, 12, SkPaint(SkColor4f::FromColor(SkColorSetRGB(231, 41, 138))));
221 
222         // Cut out an 'X'.
223         SkPaint paint(SkColors::kTransparent);
224         paint.setBlendMode(SkBlendMode::kSrc);
225         paint.setStrokeWidth(2);
226         paint.setStyle(SkPaint::kStroke_Style);
227         canvas->drawLine(-5,-5,
228                          +5,+5, paint);
229         canvas->drawLine(+5,-5,
230                          -5,+5, paint);
231     });
232 }
233