2019-09-10 23:19:11 +00:00
|
|
|
// Copyright 2019 The Dawn Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "dawn_native/ErrorScope.h"
|
|
|
|
|
|
|
|
#include "common/Assert.h"
|
|
|
|
|
|
|
|
namespace dawn_native {
|
|
|
|
|
2020-05-11 20:26:12 +00:00
|
|
|
ErrorScope::ErrorScope() : mIsRoot(true) {
|
|
|
|
}
|
2019-09-10 23:19:11 +00:00
|
|
|
|
2019-10-23 11:57:41 +00:00
|
|
|
ErrorScope::ErrorScope(wgpu::ErrorFilter errorFilter, ErrorScope* parent)
|
2020-05-11 20:26:12 +00:00
|
|
|
: RefCounted(), mErrorFilter(errorFilter), mParent(parent), mIsRoot(false) {
|
2020-11-18 09:10:22 +00:00
|
|
|
ASSERT(mParent != nullptr);
|
2019-09-10 23:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ErrorScope::~ErrorScope() {
|
2020-05-11 20:26:12 +00:00
|
|
|
if (!IsRoot()) {
|
|
|
|
RunNonRootCallback();
|
2019-09-10 23:19:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 11:57:41 +00:00
|
|
|
void ErrorScope::SetCallback(wgpu::ErrorCallback callback, void* userdata) {
|
2019-09-10 23:19:11 +00:00
|
|
|
mCallback = callback;
|
|
|
|
mUserdata = userdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorScope* ErrorScope::GetParent() {
|
|
|
|
return mParent.Get();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ErrorScope::IsRoot() const {
|
2020-05-11 20:26:12 +00:00
|
|
|
return mIsRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ErrorScope::RunNonRootCallback() {
|
|
|
|
ASSERT(!IsRoot());
|
|
|
|
|
|
|
|
if (mCallback != nullptr) {
|
|
|
|
// For non-root error scopes, the callback can run at most once.
|
|
|
|
mCallback(static_cast<WGPUErrorType>(mErrorType), mErrorMessage.c_str(), mUserdata);
|
|
|
|
mCallback = nullptr;
|
|
|
|
}
|
2019-09-10 23:19:11 +00:00
|
|
|
}
|
|
|
|
|
2019-10-23 11:57:41 +00:00
|
|
|
void ErrorScope::HandleError(wgpu::ErrorType type, const char* message) {
|
2019-09-10 23:19:11 +00:00
|
|
|
HandleErrorImpl(this, type, message);
|
|
|
|
}
|
|
|
|
|
2020-05-11 20:26:12 +00:00
|
|
|
void ErrorScope::UnlinkForShutdown() {
|
|
|
|
UnlinkForShutdownImpl(this);
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:19:11 +00:00
|
|
|
// static
|
2019-10-23 11:57:41 +00:00
|
|
|
void ErrorScope::HandleErrorImpl(ErrorScope* scope, wgpu::ErrorType type, const char* message) {
|
2019-09-10 23:19:11 +00:00
|
|
|
ErrorScope* currentScope = scope;
|
|
|
|
for (; !currentScope->IsRoot(); currentScope = currentScope->GetParent()) {
|
|
|
|
ASSERT(currentScope != nullptr);
|
|
|
|
|
|
|
|
bool consumed = false;
|
|
|
|
switch (type) {
|
2019-10-23 11:57:41 +00:00
|
|
|
case wgpu::ErrorType::Validation:
|
|
|
|
if (currentScope->mErrorFilter != wgpu::ErrorFilter::Validation) {
|
2019-09-10 23:19:11 +00:00
|
|
|
// Error filter does not match. Move on to the next scope.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
consumed = true;
|
|
|
|
break;
|
|
|
|
|
2019-10-23 11:57:41 +00:00
|
|
|
case wgpu::ErrorType::OutOfMemory:
|
|
|
|
if (currentScope->mErrorFilter != wgpu::ErrorFilter::OutOfMemory) {
|
2019-09-10 23:19:11 +00:00
|
|
|
// Error filter does not match. Move on to the next scope.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
consumed = true;
|
|
|
|
break;
|
|
|
|
|
2021-01-13 17:53:29 +00:00
|
|
|
// DeviceLost is fatal. All error scopes capture them.
|
2019-09-10 23:19:11 +00:00
|
|
|
// |consumed| is false because these should bubble to all scopes.
|
2019-10-23 11:57:41 +00:00
|
|
|
case wgpu::ErrorType::DeviceLost:
|
2019-09-10 23:19:11 +00:00
|
|
|
consumed = false;
|
2021-01-13 17:53:29 +00:00
|
|
|
if (currentScope->mErrorType != wgpu::ErrorType::DeviceLost) {
|
|
|
|
// DeviceLost overrides any other error that is not a DeviceLost.
|
|
|
|
currentScope->mErrorType = type;
|
|
|
|
currentScope->mErrorMessage = message;
|
|
|
|
}
|
2019-09-10 23:19:11 +00:00
|
|
|
break;
|
|
|
|
|
2021-01-13 17:53:29 +00:00
|
|
|
case wgpu::ErrorType::Unknown:
|
|
|
|
// Means the scope was destroyed before contained work finished.
|
|
|
|
// This happens when you destroy the device while there's pending work.
|
|
|
|
// That's handled in ErrorScope::UnlinkForShutdownImpl, not here.
|
2019-10-23 11:57:41 +00:00
|
|
|
case wgpu::ErrorType::NoError:
|
2021-01-13 17:53:29 +00:00
|
|
|
// Not considered an error, and should never be passed to HandleError.
|
2019-09-10 23:19:11 +00:00
|
|
|
UNREACHABLE();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record the error if the scope doesn't have one yet.
|
2019-10-23 11:57:41 +00:00
|
|
|
if (currentScope->mErrorType == wgpu::ErrorType::NoError) {
|
2019-09-10 23:19:11 +00:00
|
|
|
currentScope->mErrorType = type;
|
|
|
|
currentScope->mErrorMessage = message;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (consumed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The root error scope captures all uncaptured errors.
|
2021-01-13 17:53:29 +00:00
|
|
|
// Except, it should not capture device lost errors since those go to
|
|
|
|
// the device lost callback.
|
2019-09-10 23:19:11 +00:00
|
|
|
ASSERT(currentScope->IsRoot());
|
2021-01-13 17:53:29 +00:00
|
|
|
if (currentScope->mCallback && type != wgpu::ErrorType::DeviceLost) {
|
2019-10-23 11:57:41 +00:00
|
|
|
currentScope->mCallback(static_cast<WGPUErrorType>(type), message,
|
2019-09-10 23:19:11 +00:00
|
|
|
currentScope->mUserdata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 20:26:12 +00:00
|
|
|
// static
|
|
|
|
void ErrorScope::UnlinkForShutdownImpl(ErrorScope* scope) {
|
|
|
|
Ref<ErrorScope> currentScope = scope;
|
|
|
|
Ref<ErrorScope> parentScope = nullptr;
|
|
|
|
for (; !currentScope->IsRoot(); currentScope = parentScope.Get()) {
|
|
|
|
ASSERT(!currentScope->IsRoot());
|
2020-11-18 09:10:22 +00:00
|
|
|
ASSERT(currentScope != nullptr);
|
2020-05-11 20:26:12 +00:00
|
|
|
parentScope = std::move(currentScope->mParent);
|
2020-11-18 09:10:22 +00:00
|
|
|
ASSERT(parentScope != nullptr);
|
2020-05-11 20:26:12 +00:00
|
|
|
|
|
|
|
// On shutdown, error scopes that have yet to have a status get Unknown.
|
|
|
|
if (currentScope->mErrorType == wgpu::ErrorType::NoError) {
|
|
|
|
currentScope->mErrorType = wgpu::ErrorType::Unknown;
|
|
|
|
currentScope->mErrorMessage = "Error scope destroyed";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the callback if it hasn't run already.
|
|
|
|
currentScope->RunNonRootCallback();
|
2019-09-17 18:24:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:19:11 +00:00
|
|
|
} // namespace dawn_native
|