|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | + |
| 18 | +#include "WebGPUHandles.h" |
| 19 | + |
| 20 | +#include <chrono> |
| 21 | + |
| 22 | + |
| 23 | +namespace filament::backend { |
| 24 | + |
| 25 | +void WGPUTimerQuery::beginTimeElapsedQuery(WGPUTimerQuery* query) { |
| 26 | + query->status->elapsed = 0; |
| 27 | + query->status->available.store(false); |
| 28 | + |
| 29 | + // Capture the timer query status via a weak_ptr because the MetalTimerQuery could be destroyed |
| 30 | + // before the block executes. |
| 31 | + std::weak_ptr<WGPUTimerQuery::Status> status = query->status; |
| 32 | + |
| 33 | + if (auto s = status.lock()) { |
| 34 | + s->elapsed = std::chrono::steady_clock::now().time_since_epoch().count(); |
| 35 | + } |
| 36 | + |
| 37 | + ; |
| 38 | +} |
| 39 | + |
| 40 | +void WGPUTimerQuery::endTimeElapsedQuery(WGPUTimerQuery* query) { |
| 41 | + // Capture the timer query status via a weak_ptr because the WGPUTimerQuery could be destroyed |
| 42 | + // before the block executes. |
| 43 | + std::weak_ptr<WGPUTimerQuery::Status> status = query->status; |
| 44 | + if (auto s = status.lock()) { |
| 45 | + s->elapsed = std::chrono::steady_clock::now().time_since_epoch().count() - s->elapsed; |
| 46 | + s->available.store(true); |
| 47 | + } |
| 48 | + |
| 49 | +} |
| 50 | + |
| 51 | +bool WGPUTimerQuery::getQueryResult(WGPUTimerQuery* query, uint64_t* outElapsedTime) { |
| 52 | + if (!query->status->available.load()) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + if (outElapsedTime) { |
| 56 | + *outElapsedTime = query->status->elapsed; |
| 57 | + } |
| 58 | + return true; |
| 59 | +} |
| 60 | + |
| 61 | +} // namespace filament::backend |
0 commit comments