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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2020-02-04 21:09:08 +00:00
|
|
|
#include "gm/verifiers/gmverifier.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkBlendMode.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkFontTypes.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkShader.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTileMode.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
2020-07-01 16:55:01 +00:00
|
|
|
#include "include/gpu/GrRecordingContext.h"
|
2020-12-23 14:16:59 +00:00
|
|
|
#include "src/core/SkCanvasPriv.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkTraceEvent.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
|
|
|
|
#include <stdarg.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;
|
2019-05-07 19:38:46 +00:00
|
|
|
font.measureText(failureMsg.c_str(), failureMsg.size(), SkTextEncoding::kUTF8, &bounds);
|
2019-05-02 15:31:28 +00:00
|
|
|
SkPaint textPaint(SkColors::kWhite);
|
2019-02-07 23:20:09 +00:00
|
|
|
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-05-02 15:31:28 +00:00
|
|
|
SkPaint paint(SkColors::kRed);
|
2019-02-07 23:20:09 +00:00
|
|
|
bmpCanvas.drawString("GPU Only", 20, 40, font, paint);
|
|
|
|
SkMatrix localM;
|
|
|
|
localM.setRotate(35.f);
|
|
|
|
localM.postTranslate(10.f, 0.f);
|
2020-12-12 16:18:31 +00:00
|
|
|
paint.setShader(bmp.makeShader(SkTileMode::kMirror, SkTileMode::kMirror,
|
|
|
|
SkSamplingOptions(SkFilterMode::kLinear,
|
|
|
|
SkMipmapMode::kNearest),
|
|
|
|
localM));
|
2019-02-07 23:20:09 +00:00
|
|
|
canvas->drawPaint(paint);
|
|
|
|
}
|
|
|
|
|
2020-06-25 17:26:22 +00:00
|
|
|
static void handle_gm_failure(SkCanvas* canvas, DrawResult result, const SkString& errorMsg) {
|
|
|
|
if (DrawResult::kFail == result) {
|
|
|
|
draw_failure_message(canvas, "DRAW FAILED: %s", errorMsg.c_str());
|
|
|
|
} else if (SkString(GM::kErrorMsg_DrawSkippedGpuOnly) == errorMsg) {
|
|
|
|
draw_gpu_only_message(canvas);
|
|
|
|
} else {
|
|
|
|
draw_failure_message(canvas, "DRAW SKIPPED: %s", errorMsg.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
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() {}
|
|
|
|
|
2020-07-01 18:45:24 +00:00
|
|
|
DrawResult GM::gpuSetup(GrDirectContext* context, SkCanvas* canvas, SkString* errorMsg) {
|
2020-06-16 15:11:33 +00:00
|
|
|
TRACE_EVENT1("GM", TRACE_FUNC, "name", TRACE_STR_COPY(this->getName()));
|
2020-06-26 14:10:49 +00:00
|
|
|
if (!fGpuSetup) {
|
|
|
|
// When drawn in viewer, gpuSetup will be called multiple times with the same
|
|
|
|
// GrContext.
|
|
|
|
fGpuSetup = true;
|
|
|
|
fGpuSetupResult = this->onGpuSetup(context, errorMsg);
|
2020-06-25 17:26:22 +00:00
|
|
|
}
|
2020-06-26 14:10:49 +00:00
|
|
|
if (DrawResult::kOk != fGpuSetupResult) {
|
|
|
|
handle_gm_failure(canvas, fGpuSetupResult, *errorMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fGpuSetupResult;
|
2020-06-16 15:11:33 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 16:38:53 +00:00
|
|
|
void GM::gpuTeardown() {
|
|
|
|
this->onGpuTeardown();
|
2020-06-26 14:10:49 +00:00
|
|
|
|
|
|
|
// After 'gpuTeardown' a GM can be reused with a different GrContext. Reset the flag
|
|
|
|
// so 'onGpuSetup' will be called.
|
|
|
|
fGpuSetup = false;
|
2020-06-25 16:38:53 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2020-06-25 15:41:19 +00:00
|
|
|
this->onceBeforeDraw();
|
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) {
|
2020-06-25 17:26:22 +00:00
|
|
|
handle_gm_failure(canvas, drawResult, *errorMsg);
|
2019-02-07 23:20:09 +00:00
|
|
|
}
|
|
|
|
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);
|
2020-06-25 15:41:19 +00:00
|
|
|
this->onceBeforeDraw();
|
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; }
|
2021-07-21 19:39:51 +00:00
|
|
|
DrawResult SimpleGpuGM::onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) {
|
|
|
|
return fDrawProc(rContext, canvas, errorMsg);
|
2019-02-07 21:18:22 +00:00
|
|
|
}
|
|
|
|
|
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-07-11 20:32:53 +00:00
|
|
|
bool GM::animate(double nanos) { return this->onAnimate(nanos); }
|
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) {}
|
|
|
|
|
2020-02-04 21:09:08 +00:00
|
|
|
std::unique_ptr<verifiers::VerifierList> GM::getVerifiers() const {
|
|
|
|
// No verifiers by default.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-07 21:18:22 +00:00
|
|
|
void GM::onOnceBeforeDraw() {}
|
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
bool GM::onAnimate(double /*nanos*/) { return false; }
|
2019-07-08 18:55:15 +00:00
|
|
|
|
|
|
|
bool GM::onChar(SkUnichar uni) { return false; }
|
|
|
|
|
2019-02-07 21:18:22 +00:00
|
|
|
bool GM::onGetControls(SkMetaData*) { return false; }
|
2019-07-08 18:55:15 +00:00
|
|
|
|
2019-02-07 21:18:22 +00:00
|
|
|
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) {
|
2019-05-02 15:31:28 +00:00
|
|
|
canvas->drawRect(SkRect::Make(this->getISize()), SkPaint(SkColor4f::FromColor(color)));
|
2012-01-03 17:20:38 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-07-21 19:39:51 +00:00
|
|
|
DrawResult GpuGM::onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) {
|
|
|
|
this->onDraw(rContext, canvas);
|
2019-02-07 21:18:22 +00:00
|
|
|
return DrawResult::kOk;
|
|
|
|
}
|
2021-07-21 19:39:51 +00:00
|
|
|
void GpuGM::onDraw(GrRecordingContext*, SkCanvas*) {
|
2019-02-07 21:18:22 +00:00
|
|
|
SK_ABORT("Not implemented.");
|
|
|
|
}
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
DrawResult GpuGM::onDraw(SkCanvas* canvas, SkString* errorMsg) {
|
2020-06-29 19:36:12 +00:00
|
|
|
|
2021-07-21 19:39:51 +00:00
|
|
|
auto rContext = canvas->recordingContext();
|
|
|
|
if (!rContext) {
|
2019-02-07 23:20:09 +00:00
|
|
|
*errorMsg = kErrorMsg_DrawSkippedGpuOnly;
|
|
|
|
return DrawResult::kSkip;
|
2019-02-07 22:23:36 +00:00
|
|
|
}
|
2021-07-21 19:39:51 +00:00
|
|
|
if (rContext->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
|
|
|
}
|
2021-07-21 19:39:51 +00:00
|
|
|
return this->onDraw(rContext, 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, [&]{
|
|
|
|
// A green circle.
|
2019-05-02 15:31:28 +00:00
|
|
|
canvas->drawCircle(0, 0, 12, SkPaint(SkColor4f::FromColor(SkColorSetRGB(27, 158, 119))));
|
2018-10-31 14:49:38 +00:00
|
|
|
|
|
|
|
// Cut out a check mark.
|
2019-05-02 15:31:28 +00:00
|
|
|
SkPaint paint(SkColors::kTransparent);
|
2018-10-31 14:49:38 +00:00
|
|
|
paint.setBlendMode(SkBlendMode::kSrc);
|
|
|
|
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, [&] {
|
|
|
|
// A red circle.
|
2019-05-02 15:31:28 +00:00
|
|
|
canvas->drawCircle(0,0, 12, SkPaint(SkColor4f::FromColor(SkColorSetRGB(231, 41, 138))));
|
2018-10-31 14:49:38 +00:00
|
|
|
|
|
|
|
// Cut out an 'X'.
|
2019-05-02 15:31:28 +00:00
|
|
|
SkPaint paint(SkColors::kTransparent);
|
2018-10-31 14:49:38 +00:00
|
|
|
paint.setBlendMode(SkBlendMode::kSrc);
|
|
|
|
paint.setStrokeWidth(2);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawLine(-5,-5,
|
|
|
|
+5,+5, paint);
|
|
|
|
canvas->drawLine(+5,-5,
|
|
|
|
-5,+5, paint);
|
|
|
|
});
|
|
|
|
}
|