1 // Copyright 2019 The Dawn Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "dawn_native/ErrorScope.h" 16 17 #include "common/Assert.h" 18 19 namespace dawn_native { 20 21 namespace { 22 ErrorFilterToErrorType(wgpu::ErrorFilter filter)23 wgpu::ErrorType ErrorFilterToErrorType(wgpu::ErrorFilter filter) { 24 switch (filter) { 25 case wgpu::ErrorFilter::Validation: 26 return wgpu::ErrorType::Validation; 27 case wgpu::ErrorFilter::OutOfMemory: 28 return wgpu::ErrorType::OutOfMemory; 29 } 30 UNREACHABLE(); 31 } 32 33 } // namespace 34 ErrorScope(wgpu::ErrorFilter errorFilter)35 ErrorScope::ErrorScope(wgpu::ErrorFilter errorFilter) 36 : mMatchedErrorType(ErrorFilterToErrorType(errorFilter)) { 37 } 38 GetErrorType() const39 wgpu::ErrorType ErrorScope::GetErrorType() const { 40 return mCapturedError; 41 } 42 GetErrorMessage() const43 const char* ErrorScope::GetErrorMessage() const { 44 return mErrorMessage.c_str(); 45 } 46 Push(wgpu::ErrorFilter filter)47 void ErrorScopeStack::Push(wgpu::ErrorFilter filter) { 48 mScopes.push_back(ErrorScope(filter)); 49 } 50 Pop()51 ErrorScope ErrorScopeStack::Pop() { 52 ASSERT(!mScopes.empty()); 53 ErrorScope scope = std::move(mScopes.back()); 54 mScopes.pop_back(); 55 return scope; 56 } 57 Empty() const58 bool ErrorScopeStack::Empty() const { 59 return mScopes.empty(); 60 } 61 HandleError(wgpu::ErrorType type,const char * message)62 bool ErrorScopeStack::HandleError(wgpu::ErrorType type, const char* message) { 63 for (auto it = mScopes.rbegin(); it != mScopes.rend(); ++it) { 64 if (it->mMatchedErrorType != type) { 65 // Error filter does not match. Move on to the next scope. 66 continue; 67 } 68 69 // Filter matches. 70 // Record the error if the scope doesn't have one yet. 71 if (it->mCapturedError == wgpu::ErrorType::NoError) { 72 it->mCapturedError = type; 73 it->mErrorMessage = message; 74 } 75 76 if (type == wgpu::ErrorType::DeviceLost) { 77 if (it->mCapturedError != wgpu::ErrorType::DeviceLost) { 78 // DeviceLost overrides any other error that is not a DeviceLost. 79 it->mCapturedError = type; 80 it->mErrorMessage = message; 81 } 82 } else { 83 // Errors that are not device lost are captured and stop propogating. 84 return true; 85 } 86 } 87 88 // The error was not captured. 89 return false; 90 } 91 92 } // namespace dawn_native 93