|
| 1 | +// Copyright 2026 The Dawn & Tint Authors |
| 2 | +// |
| 3 | +// Redistribution and use in source and binary forms, with or without |
| 4 | +// modification, are permitted provided that the following conditions are met: |
| 5 | +// |
| 6 | +// 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | +// list of conditions and the following disclaimer. |
| 8 | +// |
| 9 | +// 2. Redistributions in binary form must reproduce the above copyright notice, |
| 10 | +// this list of conditions and the following disclaimer in the documentation |
| 11 | +// and/or other materials provided with the distribution. |
| 12 | +// |
| 13 | +// 3. Neither the name of the copyright holder nor the names of its |
| 14 | +// contributors may be used to endorse or promote products derived from |
| 15 | +// this software without specific prior written permission. |
| 16 | +// |
| 17 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 21 | +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 | +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 24 | +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 25 | +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | + |
| 28 | +#include "dawn/common/WGPUDeviceCallbackInfos.h" |
| 29 | + |
| 30 | +#include "dawn/common/Log.h" |
| 31 | + |
| 32 | +namespace dawn { |
| 33 | +namespace { |
| 34 | +// Default callback infos depending on the build type. |
| 35 | +#ifdef DAWN_ENABLE_ASSERTS |
| 36 | +static constexpr WGPUDeviceLostCallbackInfo kDefaultDeviceLostCallbackInfo = { |
| 37 | + nullptr, WGPUCallbackMode_AllowSpontaneous, |
| 38 | + [](WGPUDevice const*, WGPUDeviceLostReason, WGPUStringView, void*, void*) { |
| 39 | + static std::once_flag flag; |
| 40 | + std::call_once(flag, []() { |
| 41 | + dawn::WarningLog() << "No Dawn device lost callback was set. This is probably not " |
| 42 | + "intended. If you really want to ignore device lost " |
| 43 | + "and suppress this message, set the callback explicitly."; |
| 44 | + }); |
| 45 | + }, |
| 46 | + nullptr, nullptr}; |
| 47 | +static constexpr WGPUUncapturedErrorCallbackInfo kDefaultUncapturedErrorCallbackInfo = { |
| 48 | + nullptr, |
| 49 | + [](WGPUDevice const*, WGPUErrorType, WGPUStringView, void*, void*) { |
| 50 | + static std::once_flag flag; |
| 51 | + std::call_once(flag, []() { |
| 52 | + dawn::WarningLog() << "No Dawn device uncaptured error callback was set. This is " |
| 53 | + "probably not intended. If you really want to ignore errors " |
| 54 | + "and suppress this message, set the callback explicitly."; |
| 55 | + }); |
| 56 | + }, |
| 57 | + nullptr, nullptr}; |
| 58 | +static constexpr WGPULoggingCallbackInfo kDefaultLoggingCallbackInfo = { |
| 59 | + nullptr, |
| 60 | + [](WGPULoggingType, WGPUStringView, void*, void*) { |
| 61 | + static std::once_flag flag; |
| 62 | + std::call_once(flag, []() { |
| 63 | + dawn::WarningLog() << "No Dawn device logging callback callback was set. This is " |
| 64 | + "probably not intended. If you really want to ignore logs " |
| 65 | + "and suppress this message, set the callback explicitly."; |
| 66 | + }); |
| 67 | + }, |
| 68 | + nullptr, nullptr}; |
| 69 | +#else |
| 70 | +static constexpr WGPUDeviceLostCallbackInfo kDefaultDeviceLostCallbackInfo = { |
| 71 | + nullptr, WGPUCallbackMode_AllowSpontaneous, nullptr, nullptr, nullptr}; |
| 72 | +static constexpr WGPUUncapturedErrorCallbackInfo kDefaultUncapturedErrorCallbackInfo = { |
| 73 | + nullptr, nullptr, nullptr, nullptr}; |
| 74 | +static constexpr WGPULoggingCallbackInfo kDefaultLoggingCallbackInfo = {nullptr, nullptr, nullptr, |
| 75 | + nullptr}; |
| 76 | +#endif // DAWN_ENABLE_ASSERTS |
| 77 | + |
| 78 | +const WGPUUncapturedErrorCallbackInfo& GetUncapturedErrorCallbackInfoOrDefault( |
| 79 | + const WGPUDeviceDescriptor* descriptor) { |
| 80 | + if (descriptor != nullptr && descriptor->uncapturedErrorCallbackInfo.callback != nullptr) { |
| 81 | + return descriptor->uncapturedErrorCallbackInfo; |
| 82 | + } |
| 83 | + return kDefaultUncapturedErrorCallbackInfo; |
| 84 | +} |
| 85 | +} // namespace |
| 86 | + |
| 87 | +const WGPUDeviceLostCallbackInfo& GetDeviceLostCallbackInfoOrDefault( |
| 88 | + const WGPUDeviceDescriptor* descriptor) { |
| 89 | + if (descriptor != nullptr && descriptor->deviceLostCallbackInfo.callback != nullptr) { |
| 90 | + return descriptor->deviceLostCallbackInfo; |
| 91 | + } |
| 92 | + return kDefaultDeviceLostCallbackInfo; |
| 93 | +} |
| 94 | + |
| 95 | +WGPUDeviceCallbackInfos::CallbackInfos::CallbackInfos() = default; |
| 96 | + |
| 97 | +WGPUDeviceCallbackInfos::CallbackInfos::CallbackInfos(const WGPUUncapturedErrorCallbackInfo& error, |
| 98 | + const WGPULoggingCallbackInfo& logging) { |
| 99 | + if (error.callback != nullptr) { |
| 100 | + this->error = error; |
| 101 | + } |
| 102 | + if (logging.callback != nullptr) { |
| 103 | + this->logging = logging; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +WGPUDeviceCallbackInfos::WGPUDeviceCallbackInfos() = default; |
| 108 | + |
| 109 | +WGPUDeviceCallbackInfos::WGPUDeviceCallbackInfos(const WGPUDeviceDescriptor* descriptor) |
| 110 | + : mCallbackInfos(GetUncapturedErrorCallbackInfoOrDefault(descriptor), |
| 111 | + kDefaultLoggingCallbackInfo) {} |
| 112 | + |
| 113 | +void WGPUDeviceCallbackInfos::CallErrorCallback(WGPUDevice const* device, |
| 114 | + WGPUErrorType type, |
| 115 | + WGPUStringView message) { |
| 116 | + std::optional<WGPUUncapturedErrorCallbackInfo> callbackInfo; |
| 117 | + mCallbackInfos.Use<NotifyType::None>([&](auto callbackInfos) { |
| 118 | + callbackInfo = callbackInfos->error; |
| 119 | + if (callbackInfo) { |
| 120 | + callbackInfos->semaphore += 1; |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + // If we don't have a callback info, we can just return. |
| 125 | + if (!callbackInfo) { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + // Call the callback without holding the lock to prevent any re-entrant issues. |
| 130 | + DAWN_ASSERT(callbackInfo->callback != nullptr); |
| 131 | + callbackInfo->callback(device, type, message, callbackInfo->userdata1, callbackInfo->userdata2); |
| 132 | + |
| 133 | + mCallbackInfos.Use([&](auto callbackInfos) { |
| 134 | + DAWN_ASSERT(callbackInfos->semaphore > 0); |
| 135 | + callbackInfos->semaphore -= 1; |
| 136 | + }); |
| 137 | +} |
| 138 | + |
| 139 | +void WGPUDeviceCallbackInfos::CallLoggingCallback(WGPULoggingType type, WGPUStringView message) { |
| 140 | + std::optional<WGPULoggingCallbackInfo> callbackInfo; |
| 141 | + mCallbackInfos.Use<NotifyType::None>([&](auto callbackInfos) { |
| 142 | + callbackInfo = callbackInfos->logging; |
| 143 | + if (callbackInfo) { |
| 144 | + callbackInfos->semaphore += 1; |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + // If we don't have a callback info, we can just return. |
| 149 | + if (!callbackInfo) { |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + // Call the callback without holding the lock to prevent any re-entrant issues. |
| 154 | + DAWN_ASSERT(callbackInfo->callback != nullptr); |
| 155 | + callbackInfo->callback(type, message, callbackInfo->userdata1, callbackInfo->userdata2); |
| 156 | + |
| 157 | + mCallbackInfos.Use([&](auto callbackInfos) { |
| 158 | + DAWN_ASSERT(callbackInfos->semaphore > 0); |
| 159 | + callbackInfos->semaphore -= 1; |
| 160 | + }); |
| 161 | +} |
| 162 | + |
| 163 | +void WGPUDeviceCallbackInfos::SetLoggingCallbackInfo(const WGPULoggingCallbackInfo& callbackInfo) { |
| 164 | + mCallbackInfos.Use<NotifyType::None>( |
| 165 | + [&](auto callbackInfos) { callbackInfos->logging = callbackInfo; }); |
| 166 | +} |
| 167 | + |
| 168 | +void WGPUDeviceCallbackInfos::Clear() { |
| 169 | + mCallbackInfos.Use<NotifyType::None>([](auto callbackInfos) { |
| 170 | + callbackInfos->error = std::nullopt; |
| 171 | + callbackInfos->logging = std::nullopt; |
| 172 | + |
| 173 | + // The uncaptured error and logging callbacks are spontaneous and must not be called |
| 174 | + // after we call the device lost's |mCallback| below. Although we have cleared those |
| 175 | + // callbacks, we need to wait for any remaining outstanding callbacks to finish before |
| 176 | + // continuing. |
| 177 | + callbackInfos.Wait([](auto& x) { return x.semaphore == 0; }); |
| 178 | + }); |
| 179 | +} |
| 180 | + |
| 181 | +} // namespace dawn |
0 commit comments