Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions impeller/base/validation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ namespace impeller {

static std::atomic_int32_t sValidationLogsDisabledCount = 0;
static std::atomic_int32_t sValidationLogsAreFatal = 0;
static ValidationFailureCallback sValidationFailureCallback;

void ImpellerValidationErrorsSetFatal(bool fatal) {
sValidationLogsAreFatal = fatal;
}

void ImpellerValidationErrorsSetCallback(ValidationFailureCallback callback) {
sValidationFailureCallback = std::move(callback);
}

ScopedValidationDisable::ScopedValidationDisable() {
sValidationLogsDisabledCount++;
}
Expand Down Expand Up @@ -47,6 +52,10 @@ std::ostream& ValidationLog::GetStream() {
}

void ImpellerValidationBreak(const char* message, const char* file, int line) {
if (sValidationFailureCallback &&
sValidationFailureCallback(message, file, line)) {
return;
}
const auto severity =
ImpellerValidationErrorsAreFatal() ? fml::LOG_FATAL : fml::LOG_ERROR;
auto fml_log = fml::LogMessage{severity, file, line, nullptr};
Expand Down
16 changes: 16 additions & 0 deletions impeller/base/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef FLUTTER_IMPELLER_BASE_VALIDATION_H_
#define FLUTTER_IMPELLER_BASE_VALIDATION_H_

#include <functional>
#include <sstream>

namespace impeller {
Expand Down Expand Up @@ -37,6 +38,21 @@ void ImpellerValidationErrorsSetFatal(bool fatal);

bool ImpellerValidationErrorsAreFatal();

using ValidationFailureCallback =
std::function<bool(const char* message, const char* file, int line)>;

//------------------------------------------------------------------------------
/// @brief Sets a callback that callers (usually tests) can set to
/// intercept validation failures.
///
/// Returning true from the callback indicates that Impeller can
/// continue and avoid any default behavior on tripping validation
/// (which could include process termination).
///
/// @param[in] callback The callback
///
void ImpellerValidationErrorsSetCallback(ValidationFailureCallback callback);

struct ScopedValidationDisable {
ScopedValidationDisable();

Expand Down
23 changes: 19 additions & 4 deletions impeller/playground/playground_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,26 @@
namespace impeller {

PlaygroundTest::PlaygroundTest()
: Playground(PlaygroundSwitches{flutter::testing::GetArgsForProcess()}) {}
: Playground(PlaygroundSwitches{flutter::testing::GetArgsForProcess()}) {
ImpellerValidationErrorsSetCallback(
[](const char* message, const char* file, int line) -> bool {
// GTEST_MESSAGE_AT_ can only be used in a function that returns void.
// Hence the goofy lambda. The failure message and location will still
// be correct however.
//
// https://google.github.io/googletest/advanced.html#assertion-placement
[message, file, line]() -> void {
GTEST_MESSAGE_AT_(file, line, "Impeller Validation Error",
::testing::TestPartResult::kFatalFailure)
<< message;
}();
return true;
});
}

PlaygroundTest::~PlaygroundTest() = default;
PlaygroundTest::~PlaygroundTest() {
ImpellerValidationErrorsSetCallback(nullptr);
}

namespace {
bool DoesSupportWideGamutTests() {
Expand All @@ -36,8 +53,6 @@ void PlaygroundTest::SetUp() {
return;
}

ImpellerValidationErrorsSetFatal(true);

// Test names that end with "WideGamut" will render with wide gamut support.
std::string test_name = flutter::testing::GetCurrentTestName();
PlaygroundSwitches switches = switches_;
Expand Down