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#include "gm/gm.h" 9cb93a386Sopenharmony_ci#include "gm/verifiers/gmverifier.h" 10cb93a386Sopenharmony_ci#include "include/core/SkBitmap.h" 11cb93a386Sopenharmony_ci#include "include/core/SkCanvas.h" 12cb93a386Sopenharmony_ci#include "include/core/SkSurface.h" 13cb93a386Sopenharmony_ci#include "include/effects/SkImageFilters.h" 14cb93a386Sopenharmony_ci#include "include/encode/SkPngEncoder.h" 15cb93a386Sopenharmony_ci#include "src/utils/SkOSPath.h" 16cb93a386Sopenharmony_ci 17cb93a386Sopenharmony_ci/** Checks the given VerifierResult. If it is not ok, returns it. */ 18cb93a386Sopenharmony_ci#define RETURN_NOT_OK(res) if (!(res).ok()) return (res) 19cb93a386Sopenharmony_ci 20cb93a386Sopenharmony_cinamespace skiagm { 21cb93a386Sopenharmony_cinamespace verifiers { 22cb93a386Sopenharmony_ci 23cb93a386Sopenharmony_ciVerifierResult::VerifierResult() : VerifierResult(Code::kOk, SkString("Ok")) {} 24cb93a386Sopenharmony_ci 25cb93a386Sopenharmony_ciVerifierResult::VerifierResult(VerifierResult::Code code, const SkString& msg) : 26cb93a386Sopenharmony_ci fCode(code), fMessage(msg) {} 27cb93a386Sopenharmony_ci 28cb93a386Sopenharmony_cibool VerifierResult::ok() const { 29cb93a386Sopenharmony_ci return fCode == Code::kOk; 30cb93a386Sopenharmony_ci} 31cb93a386Sopenharmony_ci 32cb93a386Sopenharmony_ciconst SkString& VerifierResult::message() const { 33cb93a386Sopenharmony_ci return fMessage; 34cb93a386Sopenharmony_ci} 35cb93a386Sopenharmony_ci 36cb93a386Sopenharmony_ciVerifierResult VerifierResult::Ok() { 37cb93a386Sopenharmony_ci return VerifierResult(Code::kOk, SkString("Ok")); 38cb93a386Sopenharmony_ci} 39cb93a386Sopenharmony_ci 40cb93a386Sopenharmony_ciVerifierResult VerifierResult::Fail(const SkString& msg) { 41cb93a386Sopenharmony_ci return VerifierResult(Code::kFail, msg); 42cb93a386Sopenharmony_ci} 43cb93a386Sopenharmony_ci 44cb93a386Sopenharmony_ciGMVerifier::GMVerifier(InputType inputType) : fInputType(inputType) {} 45cb93a386Sopenharmony_ci 46cb93a386Sopenharmony_ciGMVerifier::~GMVerifier() {} 47cb93a386Sopenharmony_ci 48cb93a386Sopenharmony_cibool GMVerifier::needsGoldImage() const { 49cb93a386Sopenharmony_ci return fInputType == InputType::kGoldImageRequired; 50cb93a386Sopenharmony_ci} 51cb93a386Sopenharmony_ci 52cb93a386Sopenharmony_ciVerifierResult GMVerifier::verify(const SkBitmap& gold, const SkBitmap& actual) { 53cb93a386Sopenharmony_ci // Call into specific implementation. 54cb93a386Sopenharmony_ci return verifyWithGold(actual.bounds(), gold, actual); 55cb93a386Sopenharmony_ci} 56cb93a386Sopenharmony_ci 57cb93a386Sopenharmony_ciVerifierResult GMVerifier::verify(const SkBitmap& actual) { 58cb93a386Sopenharmony_ci // Call into specific implementation. 59cb93a386Sopenharmony_ci return verify(actual.bounds(), actual); 60cb93a386Sopenharmony_ci} 61cb93a386Sopenharmony_ci 62cb93a386Sopenharmony_ciSkBitmap GMVerifier::RenderGoldBmp(skiagm::GM* gm, const SkColorInfo& colorInfo) { 63cb93a386Sopenharmony_ci SkASSERT(gm); 64cb93a386Sopenharmony_ci 65cb93a386Sopenharmony_ci // Call into the GM instance to get the initial image. 66cb93a386Sopenharmony_ci const SkISize size = gm->getISize(); 67cb93a386Sopenharmony_ci SkBitmap goldBmp; 68cb93a386Sopenharmony_ci goldBmp.allocPixels(SkImageInfo::Make(size, colorInfo)); 69cb93a386Sopenharmony_ci SkCanvas canvas(goldBmp); 70cb93a386Sopenharmony_ci 71cb93a386Sopenharmony_ci if (gm->gpuSetup(nullptr, &canvas) == DrawResult::kOk) { 72cb93a386Sopenharmony_ci gm->draw(&canvas); 73cb93a386Sopenharmony_ci } 74cb93a386Sopenharmony_ci 75cb93a386Sopenharmony_ci // Convert into common verifier colorspace. 76cb93a386Sopenharmony_ci SkBitmap goldVerifierBmp; 77cb93a386Sopenharmony_ci goldVerifierBmp.allocPixels(SkImageInfo::Make(size, VerifierColorInfo())); 78cb93a386Sopenharmony_ci SkCanvas verifierCanvas(goldVerifierBmp); 79cb93a386Sopenharmony_ci verifierCanvas.drawImage(goldBmp.asImage(), 0, 0); 80cb93a386Sopenharmony_ci 81cb93a386Sopenharmony_ci return goldVerifierBmp; 82cb93a386Sopenharmony_ci} 83cb93a386Sopenharmony_ci 84cb93a386Sopenharmony_ciSkColorInfo GMVerifier::VerifierColorInfo() { 85cb93a386Sopenharmony_ci return SkColorInfo( 86cb93a386Sopenharmony_ci kRGBA_F16_SkColorType, kPremul_SkAlphaType, 87cb93a386Sopenharmony_ci SkColorSpace::MakeRGB(SkNamedTransferFn::kRec2020, SkNamedGamut::kRec2020)); 88cb93a386Sopenharmony_ci} 89cb93a386Sopenharmony_ci 90cb93a386Sopenharmony_ciVerifierResult GMVerifier::makeError(const SkString& msg) const { 91cb93a386Sopenharmony_ci return VerifierResult::Fail(SkStringPrintf("[%s] %s", name().c_str(), msg.c_str())); 92cb93a386Sopenharmony_ci} 93cb93a386Sopenharmony_ci 94cb93a386Sopenharmony_ciVerifierList::VerifierList(GM* gm) : fGM(gm), fFailedVerifier(nullptr) {} 95cb93a386Sopenharmony_ci 96cb93a386Sopenharmony_civoid VerifierList::add(std::unique_ptr<GMVerifier> verifier) { 97cb93a386Sopenharmony_ci fVerifiers.push_back(std::move(verifier)); 98cb93a386Sopenharmony_ci} 99cb93a386Sopenharmony_ci 100cb93a386Sopenharmony_cibool VerifierList::needsGoldImage() const { 101cb93a386Sopenharmony_ci for (const auto& v : fVerifiers) { 102cb93a386Sopenharmony_ci if (v->needsGoldImage()) { 103cb93a386Sopenharmony_ci return true; 104cb93a386Sopenharmony_ci } 105cb93a386Sopenharmony_ci } 106cb93a386Sopenharmony_ci 107cb93a386Sopenharmony_ci return false; 108cb93a386Sopenharmony_ci} 109cb93a386Sopenharmony_ci 110cb93a386Sopenharmony_ciVerifierResult VerifierList::verifyAll(const SkColorInfo& colorInfo, const SkBitmap& actual) { 111cb93a386Sopenharmony_ci // Render the golden image if any verifiers need it. 112cb93a386Sopenharmony_ci SkBitmap goldBmp; 113cb93a386Sopenharmony_ci if (needsGoldImage()) { 114cb93a386Sopenharmony_ci goldBmp = GMVerifier::RenderGoldBmp(fGM, colorInfo); 115cb93a386Sopenharmony_ci } 116cb93a386Sopenharmony_ci 117cb93a386Sopenharmony_ci for (const auto& v : fVerifiers) { 118cb93a386Sopenharmony_ci fFailedVerifier = v.get(); 119cb93a386Sopenharmony_ci if (v->needsGoldImage()) { 120cb93a386Sopenharmony_ci RETURN_NOT_OK(v->verify(goldBmp, actual)); 121cb93a386Sopenharmony_ci } else { 122cb93a386Sopenharmony_ci RETURN_NOT_OK(v->verify(actual)); 123cb93a386Sopenharmony_ci } 124cb93a386Sopenharmony_ci } 125cb93a386Sopenharmony_ci 126cb93a386Sopenharmony_ci fFailedVerifier = nullptr; 127cb93a386Sopenharmony_ci return VerifierResult::Ok(); 128cb93a386Sopenharmony_ci} 129cb93a386Sopenharmony_ci 130cb93a386Sopenharmony_ci} // namespace verifiers 131cb93a386Sopenharmony_ci} // namespace skiagm 132