2011-07-28 14:26:00 +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.
|
|
|
|
*/
|
2014-02-13 17:14:46 +00:00
|
|
|
|
2009-06-10 15:38:48 +00:00
|
|
|
#ifndef skiagm_DEFINED
|
|
|
|
#define skiagm_DEFINED
|
|
|
|
|
2020-02-04 21:09:08 +00:00
|
|
|
#include "gm/verifiers/gmverifier.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/private/SkMacros.h"
|
|
|
|
#include "tools/Registry.h"
|
2009-06-10 15:38:48 +00:00
|
|
|
|
2019-08-05 14:28:09 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2020-07-01 18:45:24 +00:00
|
|
|
class GrDirectContext;
|
2020-06-29 19:36:12 +00:00
|
|
|
class GrRecordingContext;
|
2019-05-01 21:28:53 +00:00
|
|
|
class SkCanvas;
|
2019-02-04 21:50:10 +00:00
|
|
|
class SkMetaData;
|
2015-05-27 20:23:23 +00:00
|
|
|
struct GrContextOptions;
|
2013-09-16 20:42:15 +00:00
|
|
|
|
2021-02-03 21:56:55 +00:00
|
|
|
#define DEF_GM(CODE) \
|
|
|
|
static skiagm::GMRegistry SK_MACRO_APPEND_COUNTER(REG_)( \
|
|
|
|
[]() { return std::unique_ptr<skiagm::GM>([]() { CODE; }()); });
|
2012-10-30 20:26:58 +00:00
|
|
|
|
2019-02-07 22:23:36 +00:00
|
|
|
// A Simple GM is a rendering test that does not store state between rendering calls or make use of
|
|
|
|
// the onOnceBeforeDraw() virtual; it consists of:
|
2015-09-09 15:16:41 +00:00
|
|
|
// * A name.
|
|
|
|
// * Prefered width and height.
|
|
|
|
// * Optionally, a background color (default is white).
|
2019-02-07 22:23:36 +00:00
|
|
|
// * A standalone function pointer that implements its onDraw method.
|
2015-09-09 15:16:41 +00:00
|
|
|
#define DEF_SIMPLE_GM(NAME, CANVAS, W, H) \
|
|
|
|
DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, SK_ColorWHITE, SkString(#NAME))
|
2019-02-07 22:23:36 +00:00
|
|
|
#define DEF_SIMPLE_GM_BG(NAME, CANVAS, W, H, BGCOLOR) \
|
2015-09-09 15:16:41 +00:00
|
|
|
DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, BGCOLOR, SkString(#NAME))
|
2019-02-07 22:23:36 +00:00
|
|
|
#define DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, BGCOLOR, NAME_STR) \
|
2019-02-07 23:20:09 +00:00
|
|
|
static void SK_MACRO_CONCAT(NAME,_GM_inner)(SkCanvas*); \
|
|
|
|
DEF_SIMPLE_GM_BG_NAME_CAN_FAIL(NAME, CANVAS,, W, H, BGCOLOR, NAME_STR) { \
|
|
|
|
SK_MACRO_CONCAT(NAME,_GM_inner)(CANVAS); \
|
|
|
|
return skiagm::DrawResult::kOk; \
|
|
|
|
} \
|
|
|
|
void SK_MACRO_CONCAT(NAME,_GM_inner)(SkCanvas* CANVAS)
|
|
|
|
|
|
|
|
#define DEF_SIMPLE_GM_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H) \
|
|
|
|
DEF_SIMPLE_GM_BG_NAME_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H, SK_ColorWHITE, SkString(#NAME))
|
|
|
|
#define DEF_SIMPLE_GM_BG_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H, BGCOLOR) \
|
|
|
|
DEF_SIMPLE_GM_BG_NAME_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H, BGCOLOR, SkString(#NAME))
|
|
|
|
#define DEF_SIMPLE_GM_BG_NAME_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H, BGCOLOR, NAME_STR) \
|
|
|
|
static skiagm::DrawResult SK_MACRO_CONCAT(NAME,_GM)(SkCanvas*, SkString*); \
|
|
|
|
DEF_GM(return new skiagm::SimpleGM(BGCOLOR, NAME_STR, {W,H}, SK_MACRO_CONCAT(NAME,_GM));) \
|
|
|
|
skiagm::DrawResult SK_MACRO_CONCAT(NAME,_GM)(SkCanvas* CANVAS, SkString* ERR_MSG)
|
|
|
|
|
2014-10-26 12:23:53 +00:00
|
|
|
|
2019-02-07 22:23:36 +00:00
|
|
|
// A Simple GpuGM makes direct GPU calls. Its onDraw hook that includes GPU objects as params, and
|
|
|
|
// is only invoked on GPU configs. Non-GPU configs automatically draw a GPU-only message and abort.
|
2021-07-21 19:39:51 +00:00
|
|
|
#define DEF_SIMPLE_GPU_GM(NAME, GR_CONTEXT, CANVAS, W, H) \
|
|
|
|
DEF_SIMPLE_GPU_GM_BG(NAME, GR_CONTEXT, CANVAS, W, H, SK_ColorWHITE)
|
|
|
|
|
|
|
|
#define DEF_SIMPLE_GPU_GM_BG(NAME, GR_CONTEXT, CANVAS, W, H, BGCOLOR) \
|
|
|
|
static void SK_MACRO_CONCAT(NAME,_GM_inner)(GrRecordingContext*, SkCanvas*); \
|
|
|
|
DEF_SIMPLE_GPU_GM_BG_CAN_FAIL(NAME, GR_CONTEXT, CANVAS, /* ERR_MSG */, W, H, BGCOLOR) { \
|
|
|
|
SK_MACRO_CONCAT(NAME,_GM_inner)(GR_CONTEXT, CANVAS); \
|
|
|
|
return skiagm::DrawResult::kOk; \
|
|
|
|
} \
|
|
|
|
void SK_MACRO_CONCAT(NAME,_GM_inner)(GrRecordingContext* GR_CONTEXT, SkCanvas* CANVAS)
|
|
|
|
|
|
|
|
#define DEF_SIMPLE_GPU_GM_CAN_FAIL(NAME, GR_CONTEXT, CANVAS, ERR_MSG, W, H) \
|
|
|
|
DEF_SIMPLE_GPU_GM_BG_CAN_FAIL(NAME, GR_CONTEXT, CANVAS, ERR_MSG, W, H, SK_ColorWHITE)
|
|
|
|
|
|
|
|
#define DEF_SIMPLE_GPU_GM_BG_CAN_FAIL(NAME, GR_CONTEXT, CANVAS, ERR_MSG, W, H, BGCOLOR) \
|
|
|
|
static skiagm::DrawResult SK_MACRO_CONCAT(NAME,_GM)( \
|
|
|
|
GrRecordingContext*, SkCanvas*, SkString*); \
|
|
|
|
DEF_GM(return new skiagm::SimpleGpuGM(BGCOLOR, SkString(#NAME), {W,H}, \
|
|
|
|
SK_MACRO_CONCAT(NAME,_GM));) \
|
|
|
|
skiagm::DrawResult SK_MACRO_CONCAT(NAME,_GM)( \
|
|
|
|
GrRecordingContext* GR_CONTEXT, SkCanvas* CANVAS, SkString* ERR_MSG)
|
2019-02-07 23:20:09 +00:00
|
|
|
|
2009-06-10 15:38:48 +00:00
|
|
|
namespace skiagm {
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
enum class DrawResult {
|
2020-06-25 15:41:19 +00:00
|
|
|
kOk, // Test drew successfully.
|
|
|
|
kFail, // Test failed to draw.
|
2019-02-07 23:20:09 +00:00
|
|
|
kSkip // Test is not applicable in this context and should be skipped.
|
|
|
|
};
|
|
|
|
|
2009-06-10 15:38:48 +00:00
|
|
|
class GM {
|
|
|
|
public:
|
2019-02-07 23:20:09 +00:00
|
|
|
using DrawResult = skiagm::DrawResult;
|
|
|
|
|
2019-02-07 22:23:36 +00:00
|
|
|
GM(SkColor backgroundColor = SK_ColorWHITE);
|
2009-06-10 15:38:48 +00:00
|
|
|
virtual ~GM();
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2014-02-07 21:13:11 +00:00
|
|
|
enum Mode {
|
|
|
|
kGM_Mode,
|
|
|
|
kSample_Mode,
|
|
|
|
kBench_Mode,
|
|
|
|
};
|
|
|
|
|
|
|
|
void setMode(Mode mode) { fMode = mode; }
|
|
|
|
Mode getMode() const { return fMode; }
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
static constexpr char kErrorMsg_DrawSkippedGpuOnly[] = "This test is for GPU configs only.";
|
|
|
|
|
2020-07-01 18:45:24 +00:00
|
|
|
DrawResult gpuSetup(GrDirectContext* context, SkCanvas* canvas) {
|
2020-06-25 17:26:22 +00:00
|
|
|
SkString errorMsg;
|
|
|
|
return this->gpuSetup(context, canvas, &errorMsg);
|
|
|
|
}
|
2020-07-01 18:45:24 +00:00
|
|
|
DrawResult gpuSetup(GrDirectContext*, SkCanvas*, SkString* errorMsg);
|
2020-06-25 16:38:53 +00:00
|
|
|
void gpuTeardown();
|
2020-06-16 15:11:33 +00:00
|
|
|
|
2020-06-25 15:41:19 +00:00
|
|
|
void onceBeforeDraw() {
|
|
|
|
if (!fHaveCalledOnceBeforeDraw) {
|
|
|
|
fHaveCalledOnceBeforeDraw = true;
|
|
|
|
this->onOnceBeforeDraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
DrawResult draw(SkCanvas* canvas) {
|
|
|
|
SkString errorMsg;
|
|
|
|
return this->draw(canvas, &errorMsg);
|
|
|
|
}
|
|
|
|
DrawResult draw(SkCanvas*, SkString* errorMsg);
|
|
|
|
|
2011-10-31 14:18:20 +00:00
|
|
|
void drawBackground(SkCanvas*);
|
2019-02-07 23:20:09 +00:00
|
|
|
DrawResult drawContent(SkCanvas* canvas) {
|
|
|
|
SkString errorMsg;
|
|
|
|
return this->drawContent(canvas, &errorMsg);
|
|
|
|
}
|
|
|
|
DrawResult drawContent(SkCanvas*, SkString* errorMsg);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2015-01-28 23:32:24 +00:00
|
|
|
SkISize getISize() { return this->onISize(); }
|
2014-02-26 23:01:57 +00:00
|
|
|
const char* getName();
|
2009-06-10 15:38:48 +00:00
|
|
|
|
2019-02-07 21:18:22 +00:00
|
|
|
virtual bool runAsBench() const;
|
2015-01-23 18:31:45 +00:00
|
|
|
|
2013-02-01 15:01:24 +00:00
|
|
|
SkScalar width() {
|
|
|
|
return SkIntToScalar(this->getISize().width());
|
|
|
|
}
|
|
|
|
SkScalar height() {
|
2013-04-12 22:14:03 +00:00
|
|
|
return SkIntToScalar(this->getISize().height());
|
2013-02-01 15:01:24 +00:00
|
|
|
}
|
|
|
|
|
2011-12-06 16:15:42 +00:00
|
|
|
SkColor getBGColor() const { return fBGColor; }
|
2011-10-31 14:18:20 +00:00
|
|
|
void setBGColor(SkColor);
|
2011-08-29 17:41:02 +00:00
|
|
|
|
2019-02-07 22:23:36 +00:00
|
|
|
// helper: fill a rect in the specified color based on the GM's getISize bounds.
|
2012-01-03 17:20:38 +00:00
|
|
|
void drawSizeBounds(SkCanvas*, SkColor);
|
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
bool animate(double /*nanos*/);
|
2019-07-08 18:55:15 +00:00
|
|
|
virtual bool onChar(SkUnichar);
|
2015-02-02 03:01:04 +00:00
|
|
|
|
2018-05-15 14:09:52 +00:00
|
|
|
bool getControls(SkMetaData* controls) { return this->onGetControls(controls); }
|
|
|
|
void setControls(const SkMetaData& controls) { this->onSetControls(controls); }
|
|
|
|
|
2019-06-05 17:54:39 +00:00
|
|
|
virtual void modifyGrContextOptions(GrContextOptions*);
|
2015-05-27 20:23:23 +00:00
|
|
|
|
2020-02-04 21:09:08 +00:00
|
|
|
virtual std::unique_ptr<verifiers::VerifierList> getVerifiers() const;
|
|
|
|
|
2015-09-09 15:16:41 +00:00
|
|
|
protected:
|
2020-06-16 15:11:33 +00:00
|
|
|
// onGpuSetup is called once before any other processing with a direct context.
|
2020-07-01 18:45:24 +00:00
|
|
|
virtual DrawResult onGpuSetup(GrDirectContext*, SkString*) { return DrawResult::kOk; }
|
2020-06-25 16:38:53 +00:00
|
|
|
virtual void onGpuTeardown() {}
|
2019-02-07 21:18:22 +00:00
|
|
|
virtual void onOnceBeforeDraw();
|
2020-06-16 15:11:33 +00:00
|
|
|
virtual DrawResult onDraw(SkCanvas*, SkString* errorMsg);
|
2019-02-07 21:18:22 +00:00
|
|
|
virtual void onDraw(SkCanvas*);
|
|
|
|
|
2012-03-20 17:40:58 +00:00
|
|
|
virtual SkISize onISize() = 0;
|
2009-06-21 00:49:18 +00:00
|
|
|
virtual SkString onShortName() = 0;
|
2015-01-23 19:07:07 +00:00
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
virtual bool onAnimate(double /*nanos*/);
|
2019-02-07 21:18:22 +00:00
|
|
|
virtual bool onGetControls(SkMetaData*);
|
|
|
|
virtual void onSetControls(const SkMetaData&);
|
2012-03-21 17:34:30 +00:00
|
|
|
|
2009-06-21 00:49:18 +00:00
|
|
|
private:
|
2020-06-26 14:10:49 +00:00
|
|
|
Mode fMode;
|
|
|
|
SkString fShortName;
|
|
|
|
SkColor fBGColor;
|
|
|
|
bool fHaveCalledOnceBeforeDraw = false;
|
|
|
|
bool fGpuSetup = false;
|
|
|
|
DrawResult fGpuSetupResult = DrawResult::kOk;
|
2009-06-10 15:38:48 +00:00
|
|
|
};
|
|
|
|
|
2019-08-05 14:28:09 +00:00
|
|
|
using GMFactory = std::unique_ptr<skiagm::GM> (*)();
|
|
|
|
using GMRegistry = sk_tools::Registry<GMFactory>;
|
2015-01-12 23:27:46 +00:00
|
|
|
|
2019-02-07 22:23:36 +00:00
|
|
|
// A GpuGM replaces the onDraw method with one that also accepts GPU objects alongside the
|
|
|
|
// SkCanvas. Its onDraw is only invoked on GPU configs; on non-GPU configs it will automatically
|
|
|
|
// draw a GPU-only message and abort.
|
|
|
|
class GpuGM : public GM {
|
2019-02-06 00:42:14 +00:00
|
|
|
public:
|
2019-02-07 22:23:36 +00:00
|
|
|
GpuGM(SkColor backgroundColor = SK_ColorWHITE) : GM(backgroundColor) {}
|
2020-02-04 21:09:08 +00:00
|
|
|
|
|
|
|
// TODO(tdenniston): Currently GpuGMs don't have verifiers (because they do not render on
|
|
|
|
// CPU), but we may want to be able to verify the output images standalone, without
|
|
|
|
// requiring a gold image for comparison.
|
|
|
|
std::unique_ptr<verifiers::VerifierList> getVerifiers() const override { return nullptr; }
|
|
|
|
|
2019-02-07 10:05:55 +00:00
|
|
|
private:
|
2019-02-07 23:20:09 +00:00
|
|
|
using GM::onDraw;
|
|
|
|
DrawResult onDraw(SkCanvas*, SkString* errorMsg) final;
|
2019-02-07 21:18:22 +00:00
|
|
|
|
2021-07-21 19:39:51 +00:00
|
|
|
virtual DrawResult onDraw(GrRecordingContext*, SkCanvas*, SkString* errorMsg);
|
|
|
|
virtual void onDraw(GrRecordingContext*, SkCanvas*);
|
2019-02-07 22:23:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// SimpleGM is intended for basic GMs that can define their entire implementation inside a
|
|
|
|
// single "draw" function pointer.
|
|
|
|
class SimpleGM : public GM {
|
|
|
|
public:
|
2019-02-07 23:20:09 +00:00
|
|
|
using DrawProc = DrawResult(*)(SkCanvas*, SkString*);
|
2021-07-21 19:39:51 +00:00
|
|
|
|
2019-02-07 22:23:36 +00:00
|
|
|
SimpleGM(SkColor bgColor, const SkString& name, const SkISize& size, DrawProc drawProc)
|
|
|
|
: GM(bgColor), fName(name), fSize(size), fDrawProc(drawProc) {}
|
|
|
|
|
|
|
|
private:
|
2019-02-07 21:18:22 +00:00
|
|
|
SkISize onISize() override;
|
|
|
|
SkString onShortName() override;
|
|
|
|
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override;
|
2019-02-07 22:23:36 +00:00
|
|
|
|
|
|
|
const SkString fName;
|
|
|
|
const SkISize fSize;
|
|
|
|
const DrawProc fDrawProc;
|
2019-02-06 00:42:14 +00:00
|
|
|
};
|
2019-02-07 22:23:36 +00:00
|
|
|
|
|
|
|
class SimpleGpuGM : public GpuGM {
|
|
|
|
public:
|
2021-07-21 19:39:51 +00:00
|
|
|
using DrawProc = DrawResult (*)(GrRecordingContext*, SkCanvas*, SkString* errorMsg);
|
|
|
|
|
2019-02-07 22:23:36 +00:00
|
|
|
SimpleGpuGM(SkColor bgColor, const SkString& name, const SkISize& size, DrawProc drawProc)
|
|
|
|
: GpuGM(bgColor), fName(name), fSize(size), fDrawProc(drawProc) {}
|
|
|
|
|
|
|
|
private:
|
2019-02-07 21:18:22 +00:00
|
|
|
SkISize onISize() override;
|
|
|
|
SkString onShortName() override;
|
2021-07-21 19:39:51 +00:00
|
|
|
DrawResult onDraw(GrRecordingContext*, SkCanvas*, SkString* errorMsg) override;
|
2019-02-07 22:23:36 +00:00
|
|
|
|
|
|
|
const SkString fName;
|
|
|
|
const SkISize fSize;
|
|
|
|
const DrawProc fDrawProc;
|
|
|
|
};
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|
2009-06-10 15:38:48 +00:00
|
|
|
|
2018-10-31 14:49:38 +00:00
|
|
|
void MarkGMGood(SkCanvas*, SkScalar x, SkScalar y);
|
|
|
|
void MarkGMBad (SkCanvas*, SkScalar x, SkScalar y);
|
|
|
|
|
2009-06-10 15:38:48 +00:00
|
|
|
#endif
|