skia2/gm/verifiers/gmverifier.h
Kevin Lubick 27be14f2ca [includes] Prepare to remove SkColor<->SkImageInfo
This breaks out the SkAlphaType and SkColorType enums into
their own files, which lets us break the dependency between
SkColor and SkImageInfo.

It does not yet sever the link, but we plan to land this
so as to migrate clients to the new enum files if necessary.

This is estimated to save Chrome 230 MB of build size [1]
(about 0.1%) http://screen/4dMqPC9rvAyCk6y

[1] https://commondatastorage.googleapis.com/chromium-browser-clang/chrome_includes_2022-03-08_182556.html#view=edges&filter=%5Ethird_party%2Fskia%2Finclude%2Fcore%2FSkColor%5C.h%24&sort=asize&reverse=&includer=%5Ethird_party%2Fskia%2Finclude%2Fcore%2FSkColor%5C.h%24&included=&limit=1000

Change-Id: I331d414fe2996632ab11825c1092060ff5fe2ebd
Bug: 242216
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/517678
Reviewed-by: Ben Wagner <bungeman@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Kevin Lubick <kjlubick@google.com>
2022-03-09 18:40:44 +00:00

196 lines
5.8 KiB
C++

/*
* Copyright 2020 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef gmverifier_DEFINED
#define gmverifier_DEFINED
#include "include/core/SkColor.h"
#include "include/core/SkRect.h"
#include "include/core/SkString.h"
#include <vector>
class SkBitmap;
class SkColorInfo;
namespace skiagm {
class GM;
namespace verifiers {
/** Result type for GM verifiers. */
class VerifierResult {
public:
VerifierResult();
/** Returns true if the result is ok (non-error). */
bool ok() const;
/** Returns reference to any message associated with the result. */
const SkString& message() const;
/** Constructs an "ok" (non-error) result. */
static VerifierResult Ok();
/** Constructs a "fail" (error) result with a specific message. */
static VerifierResult Fail(const SkString& msg);
private:
/** Underlying error code. */
enum class Code {
kOk, kFail
};
/** Result code */
Code fCode;
/** Result message (may be empty). */
SkString fMessage;
/** Private constructor for a result with a specific code and message. */
VerifierResult(Code code, const SkString& msg);
};
/**
* Abstract base class for GM verifiers. A verifier checks the rendered output image of a GM.
*
* Different verifiers perform different types of transforms and checks. Verifiers may check the
* output of a GM against a given "golden" image which represents the correct output, or just
* check the output image of the GM by itself.
*
* Most verifiers have configurable fuzziness in the comparisons performed against the golden image.
*
* Subclasses should inherit from one of StandaloneVerifier or GoldImageVerifier instead of
* directly from this base class.
*/
class GMVerifier {
public:
GMVerifier() = delete;
virtual ~GMVerifier();
/** Returns the human-friendly name of the verifier. */
virtual SkString name() const = 0;
/** Returns true if this verifier needs the gold image as input. */
bool needsGoldImage() const;
/**
* Runs the verifier. This method should be used if the verifier needs the gold image as input.
*
* @param gold Bitmap containing the "correct" image.
* @param actual Bitmap containing rendered output of a GM.
* @return Ok if the verification passed, or an error if not.
*/
VerifierResult verify(const SkBitmap& gold, const SkBitmap& actual);
/**
* Runs the verifier.
*
* @param actual Bitmap containing rendered output of a GM.
* @return Ok if the verification passed, or an error if not.
*/
VerifierResult verify(const SkBitmap& actual);
/** Renders the GM using the "golden" configuration. This is common across all GMs/verifiers. */
static SkBitmap RenderGoldBmp(skiagm::GM* gm, const SkColorInfo& colorInfo);
/**
* Gets the color information that all verifier inputs should be transformed into.
*
* The primary reason for having a single shared colorspace/color type is making per-pixel
* comparisons easier. Both the image under test and gold image are transformed into a shared
* colorspace which allows for getting per-pixel colors in SkColor4f.
*/
static SkColorInfo VerifierColorInfo();
protected:
/** The type of input required for the verifier. */
enum class InputType {
kGoldImageRequired, kStandalone
};
/** Set depending if the verifier needs a golden image as an input. */
InputType fInputType;
/** Constructor. */
GMVerifier(InputType inputType);
/** Implementation of the verification. */
virtual VerifierResult verifyWithGold(
const SkIRect& region, const SkBitmap& gold, const SkBitmap& actual) = 0;
/** Implementation of the verification. */
virtual VerifierResult verify(const SkIRect& region, const SkBitmap& actual) = 0;
/** Returns an error result formatted appropriately. */
VerifierResult makeError(const SkString& msg) const;
};
/**
* A verifier that operates standalone on the given input image (no comparison against a golden
* image).
*/
class StandaloneVerifier : public GMVerifier {
public:
StandaloneVerifier() : GMVerifier(InputType::kStandalone) {}
protected:
VerifierResult verifyWithGold(const SkIRect&, const SkBitmap&, const SkBitmap&) final {
return makeError(SkString("Verifier does not accept gold image input"));
}
};
/**
* A verifier that operates compares input image against a golden image.
*/
class GoldImageVerifier : public GMVerifier {
public:
GoldImageVerifier() : GMVerifier(InputType::kGoldImageRequired) {}
protected:
VerifierResult verify(const SkIRect&, const SkBitmap&) final {
return makeError(SkString("Verifier does not accept standalone input"));
}
};
/** A list of GM verifiers. */
class VerifierList {
public:
/** Constructs a VerifierList with the given gm instance. */
explicit VerifierList(GM* gm);
/** Adds a verifier to the list of verifiers. */
void add(std::unique_ptr<GMVerifier> verifier);
/**
* Runs all verifiers against the given input. If any verifiers fail, returns the first error.
* Else, returns ok. This version can be used if no verifiers in the list require the gold
* image as input.
*/
VerifierResult verifyAll(const SkColorInfo& colorInfo, const SkBitmap& actual);
private:
/** The parent GM instance of this VerifierList. */
GM* fGM;
/** The list of verifiers. */
std::vector<std::unique_ptr<GMVerifier>> fVerifiers;
/** After running, set to the first verifier that failed, or nullptr if none failed. */
const GMVerifier* fFailedVerifier;
/** Returns true if any verifiers in the list need the gold image as input. */
bool needsGoldImage() const;
};
} // namespace verifiers
} // namespace skiagm
#endif