1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2020 Google Inc. 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci 8cb93a386Sopenharmony_ci#ifndef gmverifier_DEFINED 9cb93a386Sopenharmony_ci#define gmverifier_DEFINED 10cb93a386Sopenharmony_ci 11cb93a386Sopenharmony_ci#include "include/core/SkColor.h" 12cb93a386Sopenharmony_ci#include "include/core/SkRect.h" 13cb93a386Sopenharmony_ci#include "include/core/SkString.h" 14cb93a386Sopenharmony_ci 15cb93a386Sopenharmony_ci#include <vector> 16cb93a386Sopenharmony_ci 17cb93a386Sopenharmony_ciclass SkBitmap; 18cb93a386Sopenharmony_ci 19cb93a386Sopenharmony_cinamespace skiagm { 20cb93a386Sopenharmony_ci 21cb93a386Sopenharmony_ciclass GM; 22cb93a386Sopenharmony_ci 23cb93a386Sopenharmony_cinamespace verifiers { 24cb93a386Sopenharmony_ci 25cb93a386Sopenharmony_ci/** Result type for GM verifiers. */ 26cb93a386Sopenharmony_ciclass VerifierResult { 27cb93a386Sopenharmony_cipublic: 28cb93a386Sopenharmony_ci VerifierResult(); 29cb93a386Sopenharmony_ci 30cb93a386Sopenharmony_ci /** Returns true if the result is ok (non-error). */ 31cb93a386Sopenharmony_ci bool ok() const; 32cb93a386Sopenharmony_ci 33cb93a386Sopenharmony_ci /** Returns reference to any message associated with the result. */ 34cb93a386Sopenharmony_ci const SkString& message() const; 35cb93a386Sopenharmony_ci 36cb93a386Sopenharmony_ci /** Constructs an "ok" (non-error) result. */ 37cb93a386Sopenharmony_ci static VerifierResult Ok(); 38cb93a386Sopenharmony_ci 39cb93a386Sopenharmony_ci /** Constructs a "fail" (error) result with a specific message. */ 40cb93a386Sopenharmony_ci static VerifierResult Fail(const SkString& msg); 41cb93a386Sopenharmony_ci 42cb93a386Sopenharmony_ciprivate: 43cb93a386Sopenharmony_ci /** Underlying error code. */ 44cb93a386Sopenharmony_ci enum class Code { 45cb93a386Sopenharmony_ci kOk, kFail 46cb93a386Sopenharmony_ci }; 47cb93a386Sopenharmony_ci 48cb93a386Sopenharmony_ci /** Result code */ 49cb93a386Sopenharmony_ci Code fCode; 50cb93a386Sopenharmony_ci 51cb93a386Sopenharmony_ci /** Result message (may be empty). */ 52cb93a386Sopenharmony_ci SkString fMessage; 53cb93a386Sopenharmony_ci 54cb93a386Sopenharmony_ci /** Private constructor for a result with a specific code and message. */ 55cb93a386Sopenharmony_ci VerifierResult(Code code, const SkString& msg); 56cb93a386Sopenharmony_ci}; 57cb93a386Sopenharmony_ci 58cb93a386Sopenharmony_ci/** 59cb93a386Sopenharmony_ci * Abstract base class for GM verifiers. A verifier checks the rendered output image of a GM. 60cb93a386Sopenharmony_ci * 61cb93a386Sopenharmony_ci * Different verifiers perform different types of transforms and checks. Verifiers may check the 62cb93a386Sopenharmony_ci * output of a GM against a given "golden" image which represents the correct output, or just 63cb93a386Sopenharmony_ci * check the output image of the GM by itself. 64cb93a386Sopenharmony_ci * 65cb93a386Sopenharmony_ci * Most verifiers have configurable fuzziness in the comparisons performed against the golden image. 66cb93a386Sopenharmony_ci * 67cb93a386Sopenharmony_ci * Subclasses should inherit from one of StandaloneVerifier or GoldImageVerifier instead of 68cb93a386Sopenharmony_ci * directly from this base class. 69cb93a386Sopenharmony_ci */ 70cb93a386Sopenharmony_ciclass GMVerifier { 71cb93a386Sopenharmony_cipublic: 72cb93a386Sopenharmony_ci GMVerifier() = delete; 73cb93a386Sopenharmony_ci 74cb93a386Sopenharmony_ci virtual ~GMVerifier(); 75cb93a386Sopenharmony_ci 76cb93a386Sopenharmony_ci /** Returns the human-friendly name of the verifier. */ 77cb93a386Sopenharmony_ci virtual SkString name() const = 0; 78cb93a386Sopenharmony_ci 79cb93a386Sopenharmony_ci /** Returns true if this verifier needs the gold image as input. */ 80cb93a386Sopenharmony_ci bool needsGoldImage() const; 81cb93a386Sopenharmony_ci 82cb93a386Sopenharmony_ci /** 83cb93a386Sopenharmony_ci * Runs the verifier. This method should be used if the verifier needs the gold image as input. 84cb93a386Sopenharmony_ci * 85cb93a386Sopenharmony_ci * @param gold Bitmap containing the "correct" image. 86cb93a386Sopenharmony_ci * @param actual Bitmap containing rendered output of a GM. 87cb93a386Sopenharmony_ci * @return Ok if the verification passed, or an error if not. 88cb93a386Sopenharmony_ci */ 89cb93a386Sopenharmony_ci VerifierResult verify(const SkBitmap& gold, const SkBitmap& actual); 90cb93a386Sopenharmony_ci 91cb93a386Sopenharmony_ci /** 92cb93a386Sopenharmony_ci * Runs the verifier. 93cb93a386Sopenharmony_ci * 94cb93a386Sopenharmony_ci * @param actual Bitmap containing rendered output of a GM. 95cb93a386Sopenharmony_ci * @return Ok if the verification passed, or an error if not. 96cb93a386Sopenharmony_ci */ 97cb93a386Sopenharmony_ci VerifierResult verify(const SkBitmap& actual); 98cb93a386Sopenharmony_ci 99cb93a386Sopenharmony_ci /** Renders the GM using the "golden" configuration. This is common across all GMs/verifiers. */ 100cb93a386Sopenharmony_ci static SkBitmap RenderGoldBmp(skiagm::GM* gm, const SkColorInfo& colorInfo); 101cb93a386Sopenharmony_ci 102cb93a386Sopenharmony_ci /** 103cb93a386Sopenharmony_ci * Gets the color information that all verifier inputs should be transformed into. 104cb93a386Sopenharmony_ci * 105cb93a386Sopenharmony_ci * The primary reason for having a single shared colorspace/color type is making per-pixel 106cb93a386Sopenharmony_ci * comparisons easier. Both the image under test and gold image are transformed into a shared 107cb93a386Sopenharmony_ci * colorspace which allows for getting per-pixel colors in SkColor4f. 108cb93a386Sopenharmony_ci */ 109cb93a386Sopenharmony_ci static SkColorInfo VerifierColorInfo(); 110cb93a386Sopenharmony_ci 111cb93a386Sopenharmony_ciprotected: 112cb93a386Sopenharmony_ci /** The type of input required for the verifier. */ 113cb93a386Sopenharmony_ci enum class InputType { 114cb93a386Sopenharmony_ci kGoldImageRequired, kStandalone 115cb93a386Sopenharmony_ci }; 116cb93a386Sopenharmony_ci 117cb93a386Sopenharmony_ci /** Set depending if the verifier needs a golden image as an input. */ 118cb93a386Sopenharmony_ci InputType fInputType; 119cb93a386Sopenharmony_ci 120cb93a386Sopenharmony_ci /** Constructor. */ 121cb93a386Sopenharmony_ci GMVerifier(InputType inputType); 122cb93a386Sopenharmony_ci 123cb93a386Sopenharmony_ci /** Implementation of the verification. */ 124cb93a386Sopenharmony_ci virtual VerifierResult verifyWithGold( 125cb93a386Sopenharmony_ci const SkIRect& region, const SkBitmap& gold, const SkBitmap& actual) = 0; 126cb93a386Sopenharmony_ci 127cb93a386Sopenharmony_ci /** Implementation of the verification. */ 128cb93a386Sopenharmony_ci virtual VerifierResult verify(const SkIRect& region, const SkBitmap& actual) = 0; 129cb93a386Sopenharmony_ci 130cb93a386Sopenharmony_ci /** Returns an error result formatted appropriately. */ 131cb93a386Sopenharmony_ci VerifierResult makeError(const SkString& msg) const; 132cb93a386Sopenharmony_ci}; 133cb93a386Sopenharmony_ci 134cb93a386Sopenharmony_ci/** 135cb93a386Sopenharmony_ci * A verifier that operates standalone on the given input image (no comparison against a golden 136cb93a386Sopenharmony_ci * image). 137cb93a386Sopenharmony_ci */ 138cb93a386Sopenharmony_ciclass StandaloneVerifier : public GMVerifier { 139cb93a386Sopenharmony_cipublic: 140cb93a386Sopenharmony_ci StandaloneVerifier() : GMVerifier(InputType::kStandalone) {} 141cb93a386Sopenharmony_ci 142cb93a386Sopenharmony_ciprotected: 143cb93a386Sopenharmony_ci VerifierResult verifyWithGold(const SkIRect&, const SkBitmap&, const SkBitmap&) final { 144cb93a386Sopenharmony_ci return makeError(SkString("Verifier does not accept gold image input")); 145cb93a386Sopenharmony_ci } 146cb93a386Sopenharmony_ci}; 147cb93a386Sopenharmony_ci 148cb93a386Sopenharmony_ci/** 149cb93a386Sopenharmony_ci * A verifier that operates compares input image against a golden image. 150cb93a386Sopenharmony_ci */ 151cb93a386Sopenharmony_ciclass GoldImageVerifier : public GMVerifier { 152cb93a386Sopenharmony_cipublic: 153cb93a386Sopenharmony_ci GoldImageVerifier() : GMVerifier(InputType::kGoldImageRequired) {} 154cb93a386Sopenharmony_ci 155cb93a386Sopenharmony_ciprotected: 156cb93a386Sopenharmony_ci VerifierResult verify(const SkIRect&, const SkBitmap&) final { 157cb93a386Sopenharmony_ci return makeError(SkString("Verifier does not accept standalone input")); 158cb93a386Sopenharmony_ci } 159cb93a386Sopenharmony_ci}; 160cb93a386Sopenharmony_ci 161cb93a386Sopenharmony_ci/** A list of GM verifiers. */ 162cb93a386Sopenharmony_ciclass VerifierList { 163cb93a386Sopenharmony_cipublic: 164cb93a386Sopenharmony_ci /** Constructs a VerifierList with the given gm instance. */ 165cb93a386Sopenharmony_ci explicit VerifierList(GM* gm); 166cb93a386Sopenharmony_ci 167cb93a386Sopenharmony_ci /** Adds a verifier to the list of verifiers. */ 168cb93a386Sopenharmony_ci void add(std::unique_ptr<GMVerifier> verifier); 169cb93a386Sopenharmony_ci 170cb93a386Sopenharmony_ci /** 171cb93a386Sopenharmony_ci * Runs all verifiers against the given input. If any verifiers fail, returns the first error. 172cb93a386Sopenharmony_ci * Else, returns ok. This version can be used if no verifiers in the list require the gold 173cb93a386Sopenharmony_ci * image as input. 174cb93a386Sopenharmony_ci */ 175cb93a386Sopenharmony_ci VerifierResult verifyAll(const SkColorInfo& colorInfo, const SkBitmap& actual); 176cb93a386Sopenharmony_ci 177cb93a386Sopenharmony_ciprivate: 178cb93a386Sopenharmony_ci /** The parent GM instance of this VerifierList. */ 179cb93a386Sopenharmony_ci GM* fGM; 180cb93a386Sopenharmony_ci 181cb93a386Sopenharmony_ci /** The list of verifiers. */ 182cb93a386Sopenharmony_ci std::vector<std::unique_ptr<GMVerifier>> fVerifiers; 183cb93a386Sopenharmony_ci 184cb93a386Sopenharmony_ci /** After running, set to the first verifier that failed, or nullptr if none failed. */ 185cb93a386Sopenharmony_ci const GMVerifier* fFailedVerifier; 186cb93a386Sopenharmony_ci 187cb93a386Sopenharmony_ci /** Returns true if any verifiers in the list need the gold image as input. */ 188cb93a386Sopenharmony_ci bool needsGoldImage() const; 189cb93a386Sopenharmony_ci}; 190cb93a386Sopenharmony_ci 191cb93a386Sopenharmony_ci} // namespace verifiers 192cb93a386Sopenharmony_ci} // namespace skiagm 193cb93a386Sopenharmony_ci 194cb93a386Sopenharmony_ci#endif 195