skia2/gm/verifiers/gmverifier.cpp
Kevin Lubick 5e8f45faf1 [includes] Prepare for moving SkColorSpace to forward declare
This updates all our callsites in preparation for removing
the #include "include/core/SkColorSpace.h" from SkImageInfo.h

According to go/chrome-includes [1], this will save ~150MB
(0.07%) from the compilation size. I think SkColorSpace is
a big include because it loads the skcms header, which is
big.

The follow-on CL will remove that link, once clients have
been updated as well.

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

Change-Id: I1b5ff491ac495317b0e5af3a2082b080d43697ae
Bug: skia:13052
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/525639
Reviewed-by: Robert Phillips <robertphillips@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
Reviewed-by: Florin Malita <fmalita@google.com>
Commit-Queue: Kevin Lubick <kjlubick@google.com>
2022-03-31 19:50:10 +00:00

133 lines
3.7 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.
*/
#include "gm/gm.h"
#include "gm/verifiers/gmverifier.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColorSpace.h"
#include "include/core/SkSurface.h"
#include "include/effects/SkImageFilters.h"
#include "include/encode/SkPngEncoder.h"
#include "src/utils/SkOSPath.h"
/** Checks the given VerifierResult. If it is not ok, returns it. */
#define RETURN_NOT_OK(res) if (!(res).ok()) return (res)
namespace skiagm {
namespace verifiers {
VerifierResult::VerifierResult() : VerifierResult(Code::kOk, SkString("Ok")) {}
VerifierResult::VerifierResult(VerifierResult::Code code, const SkString& msg) :
fCode(code), fMessage(msg) {}
bool VerifierResult::ok() const {
return fCode == Code::kOk;
}
const SkString& VerifierResult::message() const {
return fMessage;
}
VerifierResult VerifierResult::Ok() {
return VerifierResult(Code::kOk, SkString("Ok"));
}
VerifierResult VerifierResult::Fail(const SkString& msg) {
return VerifierResult(Code::kFail, msg);
}
GMVerifier::GMVerifier(InputType inputType) : fInputType(inputType) {}
GMVerifier::~GMVerifier() {}
bool GMVerifier::needsGoldImage() const {
return fInputType == InputType::kGoldImageRequired;
}
VerifierResult GMVerifier::verify(const SkBitmap& gold, const SkBitmap& actual) {
// Call into specific implementation.
return verifyWithGold(actual.bounds(), gold, actual);
}
VerifierResult GMVerifier::verify(const SkBitmap& actual) {
// Call into specific implementation.
return verify(actual.bounds(), actual);
}
SkBitmap GMVerifier::RenderGoldBmp(skiagm::GM* gm, const SkColorInfo& colorInfo) {
SkASSERT(gm);
// Call into the GM instance to get the initial image.
const SkISize size = gm->getISize();
SkBitmap goldBmp;
goldBmp.allocPixels(SkImageInfo::Make(size, colorInfo));
SkCanvas canvas(goldBmp);
if (gm->gpuSetup(nullptr, &canvas) == DrawResult::kOk) {
gm->draw(&canvas);
}
// Convert into common verifier colorspace.
SkBitmap goldVerifierBmp;
goldVerifierBmp.allocPixels(SkImageInfo::Make(size, VerifierColorInfo()));
SkCanvas verifierCanvas(goldVerifierBmp);
verifierCanvas.drawImage(goldBmp.asImage(), 0, 0);
return goldVerifierBmp;
}
SkColorInfo GMVerifier::VerifierColorInfo() {
return SkColorInfo(
kRGBA_F16_SkColorType, kPremul_SkAlphaType,
SkColorSpace::MakeRGB(SkNamedTransferFn::kRec2020, SkNamedGamut::kRec2020));
}
VerifierResult GMVerifier::makeError(const SkString& msg) const {
return VerifierResult::Fail(SkStringPrintf("[%s] %s", name().c_str(), msg.c_str()));
}
VerifierList::VerifierList(GM* gm) : fGM(gm), fFailedVerifier(nullptr) {}
void VerifierList::add(std::unique_ptr<GMVerifier> verifier) {
fVerifiers.push_back(std::move(verifier));
}
bool VerifierList::needsGoldImage() const {
for (const auto& v : fVerifiers) {
if (v->needsGoldImage()) {
return true;
}
}
return false;
}
VerifierResult VerifierList::verifyAll(const SkColorInfo& colorInfo, const SkBitmap& actual) {
// Render the golden image if any verifiers need it.
SkBitmap goldBmp;
if (needsGoldImage()) {
goldBmp = GMVerifier::RenderGoldBmp(fGM, colorInfo);
}
for (const auto& v : fVerifiers) {
fFailedVerifier = v.get();
if (v->needsGoldImage()) {
RETURN_NOT_OK(v->verify(goldBmp, actual));
} else {
RETURN_NOT_OK(v->verify(actual));
}
}
fFailedVerifier = nullptr;
return VerifierResult::Ok();
}
} // namespace verifiers
} // namespace skiagm