1 // Copyright 2021 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 "src/dawn_node/binding/AsyncRunner.h" 16 17 #include <cassert> 18 #include <limits> 19 20 namespace wgpu { namespace binding { 21 AsyncRunner(Napi::Env env, wgpu::Device device)22 AsyncRunner::AsyncRunner(Napi::Env env, wgpu::Device device) : env_(env), device_(device) { 23 } 24 Begin()25 void AsyncRunner::Begin() { 26 assert(count_ != std::numeric_limits<decltype(count_)>::max()); 27 if (count_++ == 0) { 28 QueueTick(); 29 } 30 } 31 End()32 void AsyncRunner::End() { 33 assert(count_ > 0); 34 count_--; 35 } 36 QueueTick()37 void AsyncRunner::QueueTick() { 38 // TODO(crbug.com/dawn/1127): We probably want to reduce the frequency at which this gets 39 // called. 40 if (tick_queued_) { 41 return; 42 } 43 tick_queued_ = true; 44 env_.Global() 45 .Get("setImmediate") 46 .As<Napi::Function>() 47 .Call({ 48 // TODO(crbug.com/dawn/1127): Create once, reuse. 49 Napi::Function::New(env_, 50 [this](const Napi::CallbackInfo&) { 51 tick_queued_ = false; 52 if (count_ > 0) { 53 device_.Tick(); 54 QueueTick(); 55 } 56 }), 57 }); 58 } 59 60 }} // namespace wgpu::binding 61