1 /*
2 * Copyright 2023 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "src/gpu/graphite/dawn/DawnErrorChecker.h"
9
10 #include "include/private/base/SkAssert.h"
11 #include "src/gpu/graphite/Log.h"
12 #include "src/gpu/graphite/dawn/DawnAsyncWait.h"
13 #include "src/gpu/graphite/dawn/DawnSharedContext.h"
14
15 namespace skgpu::graphite {
16 namespace {
17
18 constexpr const char* kErrorScopeNames[] = {"validation", "out-of-memory", "internal"};
19 constexpr DawnErrorType kErrorScopeTypes[] = {
20 DawnErrorType::kValidation, DawnErrorType::kOutOfMemory, DawnErrorType::kInternal};
21 static_assert(std::size(kErrorScopeNames) == std::size(kErrorScopeTypes));
22 constexpr int kScopeCount = std::size(kErrorScopeTypes);
23
24 } // namespace
25
DawnErrorChecker(const DawnSharedContext * sharedContext)26 DawnErrorChecker::DawnErrorChecker(const DawnSharedContext* sharedContext)
27 : fArmed(true), fSharedContext(sharedContext) {
28 fSharedContext->device().PushErrorScope(wgpu::ErrorFilter::Validation);
29 fSharedContext->device().PushErrorScope(wgpu::ErrorFilter::OutOfMemory);
30 fSharedContext->device().PushErrorScope(wgpu::ErrorFilter::Internal);
31 }
32
~DawnErrorChecker()33 DawnErrorChecker::~DawnErrorChecker() {
34 [[maybe_unused]] auto err = this->popErrorScopes();
35 SkASSERT(!fArmed);
36 SkASSERT(err == DawnErrorType::kNoError);
37 }
38
popErrorScopes()39 SkEnumBitMask<DawnErrorType> DawnErrorChecker::popErrorScopes() {
40 if (!fArmed) {
41 return DawnErrorType::kNoError;
42 }
43
44 struct ErrorState {
45 SkEnumBitMask<DawnErrorType> fError;
46 int fScopeIdx;
47 DawnAsyncWait fWait;
48
49 ErrorState(const DawnSharedContext* sharedContext)
50 : fError(DawnErrorType::kNoError)
51 , fScopeIdx(kScopeCount - 1)
52 , fWait(sharedContext) {}
53 } errorState(fSharedContext);
54
55 wgpu::ErrorCallback errorCallback = [](WGPUErrorType status, const char* msg, void* userData) {
56 ErrorState* errorState = static_cast<ErrorState*>(userData);
57 if (status != WGPUErrorType_NoError) {
58 SkASSERT(errorState->fScopeIdx >= 0);
59 const char* errorScopeName = kErrorScopeNames[errorState->fScopeIdx];
60 SKGPU_LOG_E("Failed in error scope (%s): %s", errorScopeName, msg);
61 errorState->fError |= kErrorScopeTypes[errorState->fScopeIdx];
62 }
63 errorState->fScopeIdx--;
64 errorState->fWait.signal();
65 };
66
67 // Pop all three error scopes:
68 // Internal
69 fSharedContext->device().PopErrorScope(errorCallback, &errorState);
70 errorState.fWait.busyWait();
71 errorState.fWait.reset();
72
73 // OutOfMemory
74 fSharedContext->device().PopErrorScope(errorCallback, &errorState);
75 errorState.fWait.busyWait();
76 errorState.fWait.reset();
77
78 // Validation
79 fSharedContext->device().PopErrorScope(errorCallback, &errorState);
80 errorState.fWait.busyWait();
81
82 fArmed = false;
83 return errorState.fError;
84 }
85
86 } // namespace skgpu::graphite
87