2011-10-31 14:18:20 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2015-01-31 15:51:14 +00:00
|
|
|
#include "SkShader.h"
|
2017-07-24 19:21:31 +00:00
|
|
|
#include "SkTraceEvent.h"
|
2011-10-31 14:18:20 +00:00
|
|
|
using namespace skiagm;
|
|
|
|
|
|
|
|
GM::GM() {
|
2014-02-07 21:13:11 +00:00
|
|
|
fMode = kGM_Mode;
|
2011-10-31 14:18:20 +00:00
|
|
|
fBGColor = SK_ColorWHITE;
|
2012-11-16 13:41:45 +00:00
|
|
|
fCanvasIsDeferred = false;
|
2012-11-27 15:15:58 +00:00
|
|
|
fHaveCalledOnceBeforeDraw = false;
|
2011-10-31 14:18:20 +00:00
|
|
|
}
|
2014-06-09 19:05:34 +00:00
|
|
|
|
2011-10-31 14:18:20 +00:00
|
|
|
GM::~GM() {}
|
|
|
|
|
|
|
|
void GM::draw(SkCanvas* canvas) {
|
2017-07-24 19:21:31 +00:00
|
|
|
TRACE_EVENT1("GM", TRACE_FUNC, "name", TRACE_STR_COPY(this->getName()));
|
2011-10-31 14:18:20 +00:00
|
|
|
this->drawBackground(canvas);
|
|
|
|
this->drawContent(canvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GM::drawContent(SkCanvas* canvas) {
|
2017-07-24 19:21:31 +00:00
|
|
|
TRACE_EVENT0("GM", TRACE_FUNC);
|
2012-11-27 15:15:58 +00:00
|
|
|
if (!fHaveCalledOnceBeforeDraw) {
|
|
|
|
fHaveCalledOnceBeforeDraw = true;
|
|
|
|
this->onOnceBeforeDraw();
|
|
|
|
}
|
2018-12-18 16:48:01 +00:00
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
2011-10-31 14:18:20 +00:00
|
|
|
this->onDraw(canvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GM::drawBackground(SkCanvas* canvas) {
|
2017-07-24 19:21:31 +00:00
|
|
|
TRACE_EVENT0("GM", TRACE_FUNC);
|
2012-11-27 15:15:58 +00:00
|
|
|
if (!fHaveCalledOnceBeforeDraw) {
|
|
|
|
fHaveCalledOnceBeforeDraw = true;
|
|
|
|
this->onOnceBeforeDraw();
|
|
|
|
}
|
2018-12-18 16:48:01 +00:00
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
2011-10-31 14:18:20 +00:00
|
|
|
this->onDrawBackground(canvas);
|
|
|
|
}
|
|
|
|
|
2014-02-26 23:01:57 +00:00
|
|
|
const char* GM::getName() {
|
2011-10-31 14:18:20 +00:00
|
|
|
if (fShortName.size() == 0) {
|
|
|
|
fShortName = this->onShortName();
|
|
|
|
}
|
|
|
|
return fShortName.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GM::setBGColor(SkColor color) {
|
|
|
|
fBGColor = color;
|
|
|
|
}
|
|
|
|
|
2015-02-02 20:55:02 +00:00
|
|
|
bool GM::animate(const SkAnimTimer& timer) {
|
|
|
|
return this->onAnimate(timer);
|
2015-02-02 03:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-10-31 14:18:20 +00:00
|
|
|
void GM::onDrawBackground(SkCanvas* canvas) {
|
2016-10-06 00:33:02 +00:00
|
|
|
canvas->drawColor(fBGColor, SkBlendMode::kSrc);
|
2011-10-31 14:18:20 +00:00
|
|
|
}
|
|
|
|
|
2012-01-03 17:20:38 +00:00
|
|
|
void GM::drawSizeBounds(SkCanvas* canvas, SkColor color) {
|
|
|
|
SkISize size = this->getISize();
|
|
|
|
SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()),
|
|
|
|
SkIntToScalar(size.height()));
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(color);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
}
|
|
|
|
|
2015-09-09 15:16:41 +00:00
|
|
|
void GM::DrawGpuOnlyMessage(SkCanvas* canvas) {
|
2015-01-31 15:51:14 +00:00
|
|
|
SkBitmap bmp;
|
|
|
|
bmp.allocN32Pixels(128, 64);
|
|
|
|
SkCanvas bmpCanvas(bmp);
|
|
|
|
bmpCanvas.drawColor(SK_ColorWHITE);
|
2019-01-07 21:01:22 +00:00
|
|
|
SkFont font(sk_tool_utils::create_portable_typeface(), 20);
|
2015-01-31 15:51:14 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorRED);
|
2019-01-07 21:01:22 +00:00
|
|
|
bmpCanvas.drawString("GPU Only", 20, 40, font, paint);
|
2015-01-31 15:51:14 +00:00
|
|
|
SkMatrix localM;
|
|
|
|
localM.setRotate(35.f);
|
|
|
|
localM.postTranslate(10.f, 0.f);
|
2016-03-09 02:50:00 +00:00
|
|
|
paint.setShader(SkShader::MakeBitmapShader(bmp, SkShader::kMirror_TileMode,
|
|
|
|
SkShader::kMirror_TileMode,
|
|
|
|
&localM));
|
2015-01-31 15:51:14 +00:00
|
|
|
paint.setFilterQuality(kMedium_SkFilterQuality);
|
|
|
|
canvas->drawPaint(paint);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-31 14:18:20 +00:00
|
|
|
// need to explicitly declare this, or we get some weird infinite loop llist
|
2014-06-18 21:32:48 +00:00
|
|
|
template GMRegistry* GMRegistry::gHead;
|
2015-01-12 23:27:46 +00:00
|
|
|
|
|
|
|
void skiagm::SimpleGM::onDraw(SkCanvas* canvas) {
|
|
|
|
fDrawProc(canvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize skiagm::SimpleGM::onISize() {
|
|
|
|
return fSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkString skiagm::SimpleGM::onShortName() {
|
|
|
|
return fName;
|
|
|
|
}
|
2018-10-31 14:49:38 +00:00
|
|
|
|
|
|
|
template <typename Fn>
|
|
|
|
static void mark(SkCanvas* canvas, SkScalar x, SkScalar y, Fn&& fn) {
|
|
|
|
SkPaint alpha;
|
|
|
|
alpha.setAlpha(0x50);
|
|
|
|
canvas->saveLayer(nullptr, &alpha);
|
|
|
|
canvas->translate(x,y);
|
|
|
|
canvas->scale(2,2);
|
|
|
|
fn();
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MarkGMGood(SkCanvas* canvas, SkScalar x, SkScalar y) {
|
|
|
|
mark(canvas, x,y, [&]{
|
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
// A green circle.
|
|
|
|
paint.setColor(SkColorSetRGB(27, 158, 119));
|
|
|
|
canvas->drawCircle(0,0, 12, paint);
|
|
|
|
|
|
|
|
// Cut out a check mark.
|
|
|
|
paint.setBlendMode(SkBlendMode::kSrc);
|
|
|
|
paint.setColor(0x00000000);
|
|
|
|
paint.setStrokeWidth(2);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawLine(-6, 0,
|
|
|
|
-1, 5, paint);
|
|
|
|
canvas->drawLine(-1, +5,
|
|
|
|
+7, -5, paint);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void MarkGMBad(SkCanvas* canvas, SkScalar x, SkScalar y) {
|
|
|
|
mark(canvas, x,y, [&] {
|
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
// A red circle.
|
|
|
|
paint.setColor(SkColorSetRGB(231, 41, 138));
|
|
|
|
canvas->drawCircle(0,0, 12, paint);
|
|
|
|
|
|
|
|
// Cut out an 'X'.
|
|
|
|
paint.setBlendMode(SkBlendMode::kSrc);
|
|
|
|
paint.setColor(0x00000000);
|
|
|
|
paint.setStrokeWidth(2);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawLine(-5,-5,
|
|
|
|
+5,+5, paint);
|
|
|
|
canvas->drawLine(+5,-5,
|
|
|
|
-5,+5, paint);
|
|
|
|
});
|
|
|
|
}
|