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"
|
2019-02-07 22:23:36 +00:00
|
|
|
|
|
|
|
#include "GrContext.h"
|
2015-01-31 15:51:14 +00:00
|
|
|
#include "SkShader.h"
|
2017-07-24 19:21:31 +00:00
|
|
|
#include "SkTraceEvent.h"
|
2019-03-20 16:12:10 +00:00
|
|
|
#include "ToolUtils.h"
|
2011-10-31 14:18:20 +00:00
|
|
|
using namespace skiagm;
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
constexpr char GM::kErrorMsg_DrawSkippedGpuOnly[];
|
|
|
|
|
|
|
|
static void draw_failure_message(SkCanvas* canvas, const char format[], ...) {
|
|
|
|
SkString failureMsg;
|
|
|
|
|
|
|
|
va_list argp;
|
|
|
|
va_start(argp, format);
|
|
|
|
failureMsg.appendVAList(format, argp);
|
|
|
|
va_end(argp);
|
|
|
|
|
|
|
|
constexpr SkScalar kOffset = 5.0f;
|
|
|
|
canvas->drawColor(SkColorSetRGB(200,0,0));
|
|
|
|
SkFont font;
|
|
|
|
SkRect bounds;
|
|
|
|
font.measureText(failureMsg.c_str(), failureMsg.size(), kUTF8_SkTextEncoding, &bounds);
|
|
|
|
SkPaint textPaint;
|
|
|
|
textPaint.setColor(SK_ColorWHITE);
|
|
|
|
canvas->drawString(failureMsg, kOffset, bounds.height() + kOffset, font, textPaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_gpu_only_message(SkCanvas* canvas) {
|
|
|
|
SkBitmap bmp;
|
|
|
|
bmp.allocN32Pixels(128, 64);
|
|
|
|
SkCanvas bmpCanvas(bmp);
|
|
|
|
bmpCanvas.drawColor(SK_ColorWHITE);
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), 20);
|
2019-02-07 23:20:09 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
bmpCanvas.drawString("GPU Only", 20, 40, font, paint);
|
|
|
|
SkMatrix localM;
|
|
|
|
localM.setRotate(35.f);
|
|
|
|
localM.postTranslate(10.f, 0.f);
|
2019-04-08 17:20:23 +00:00
|
|
|
paint.setShader(bmp.makeShader(SkTileMode::kMirror, SkTileMode::kMirror, &localM));
|
2019-02-07 23:20:09 +00:00
|
|
|
paint.setFilterQuality(kMedium_SkFilterQuality);
|
|
|
|
canvas->drawPaint(paint);
|
|
|
|
}
|
|
|
|
|
2019-02-07 22:23:36 +00:00
|
|
|
GM::GM(SkColor bgColor) {
|
2014-02-07 21:13:11 +00:00
|
|
|
fMode = kGM_Mode;
|
2019-02-07 22:23:36 +00:00
|
|
|
fBGColor = bgColor;
|
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() {}
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
DrawResult GM::draw(SkCanvas* canvas, SkString* errorMsg) {
|
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);
|
2019-02-07 23:20:09 +00:00
|
|
|
return this->drawContent(canvas, errorMsg);
|
2011-10-31 14:18:20 +00:00
|
|
|
}
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
DrawResult GM::drawContent(SkCanvas* canvas, SkString* errorMsg) {
|
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);
|
2019-02-07 23:20:09 +00:00
|
|
|
DrawResult drawResult = this->onDraw(canvas, errorMsg);
|
|
|
|
if (DrawResult::kOk != drawResult) {
|
|
|
|
if (DrawResult::kFail == drawResult) {
|
|
|
|
draw_failure_message(canvas, "DRAW FAILED: %s", errorMsg->c_str());
|
|
|
|
} else if (SkString(kErrorMsg_DrawSkippedGpuOnly) == *errorMsg) {
|
|
|
|
draw_gpu_only_message(canvas);
|
|
|
|
} else {
|
|
|
|
draw_failure_message(canvas, "DRAW SKIPPED: %s", errorMsg->c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return drawResult;
|
2011-10-31 14:18:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2019-02-01 19:15:42 +00:00
|
|
|
canvas->drawColor(fBGColor, SkBlendMode::kSrc);
|
2011-10-31 14:18:20 +00:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:18:22 +00:00
|
|
|
DrawResult GM::onDraw(SkCanvas* canvas, SkString* errorMsg) {
|
|
|
|
this->onDraw(canvas);
|
|
|
|
return DrawResult::kOk;
|
|
|
|
}
|
|
|
|
void GM::onDraw(SkCanvas*) { SK_ABORT("Not implemented."); }
|
|
|
|
|
|
|
|
|
|
|
|
SkISize SimpleGM::onISize() { return fSize; }
|
|
|
|
SkString SimpleGM::onShortName() { return fName; }
|
|
|
|
DrawResult SimpleGM::onDraw(SkCanvas* canvas, SkString* errorMsg) {
|
|
|
|
return fDrawProc(canvas, errorMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize SimpleGpuGM::onISize() { return fSize; }
|
|
|
|
SkString SimpleGpuGM::onShortName() { return fName; }
|
|
|
|
DrawResult SimpleGpuGM::onDraw(GrContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas,
|
|
|
|
SkString* errorMsg) {
|
|
|
|
return fDrawProc(ctx, rtc, canvas, errorMsg);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-03-20 16:55:08 +00:00
|
|
|
bool GM::animate(const AnimTimer& timer) { return this->onAnimate(timer); }
|
2015-02-02 03:01:04 +00:00
|
|
|
|
2019-02-07 21:18:22 +00:00
|
|
|
bool GM::runAsBench() const { return false; }
|
|
|
|
void GM::modifyGrContextOptions(GrContextOptions* options) {}
|
|
|
|
|
|
|
|
void GM::onOnceBeforeDraw() {}
|
|
|
|
|
2019-03-20 16:55:08 +00:00
|
|
|
bool GM::onAnimate(const AnimTimer&) { return false; }
|
2019-02-07 21:18:22 +00:00
|
|
|
bool GM::onHandleKey(SkUnichar uni) { return false; }
|
|
|
|
bool GM::onGetControls(SkMetaData*) { return false; }
|
|
|
|
void GM::onSetControls(const SkMetaData&) {}
|
|
|
|
|
2015-02-02 03:01:04 +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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-02-07 21:18:22 +00:00
|
|
|
DrawResult GpuGM::onDraw(GrContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas,
|
|
|
|
SkString* errorMsg) {
|
|
|
|
this->onDraw(ctx, rtc, canvas);
|
|
|
|
return DrawResult::kOk;
|
|
|
|
}
|
|
|
|
void GpuGM::onDraw(GrContext*, GrRenderTargetContext*, SkCanvas*) {
|
|
|
|
SK_ABORT("Not implemented.");
|
|
|
|
}
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
DrawResult GpuGM::onDraw(SkCanvas* canvas, SkString* errorMsg) {
|
2019-02-07 22:23:36 +00:00
|
|
|
GrContext* ctx = canvas->getGrContext();
|
|
|
|
GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext();
|
|
|
|
if (!ctx || !rtc) {
|
2019-02-07 23:20:09 +00:00
|
|
|
*errorMsg = kErrorMsg_DrawSkippedGpuOnly;
|
|
|
|
return DrawResult::kSkip;
|
2019-02-07 22:23:36 +00:00
|
|
|
}
|
|
|
|
if (ctx->abandoned()) {
|
2019-02-07 23:20:09 +00:00
|
|
|
*errorMsg = "GrContext abandoned.";
|
2019-04-02 22:11:57 +00:00
|
|
|
return DrawResult::kSkip;
|
2019-02-07 22:23:36 +00:00
|
|
|
}
|
2019-02-07 23:20:09 +00:00
|
|
|
return this->onDraw(ctx, rtc, canvas, errorMsg);
|
2015-01-12 23:27:46 +00:00
|
|
|
}
|
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);
|
|
|
|
});
|
|
|
|
}
|