1 // Copyright 2020 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_wire/client/Queue.h" 16 17 #include "dawn_wire/client/Client.h" 18 #include "dawn_wire/client/Device.h" 19 20 namespace dawn_wire { namespace client { 21 ~Queue()22 Queue::~Queue() { 23 ClearAllCallbacks(WGPUQueueWorkDoneStatus_Unknown); 24 } 25 OnWorkDoneCallback(uint64_t requestSerial,WGPUQueueWorkDoneStatus status)26 bool Queue::OnWorkDoneCallback(uint64_t requestSerial, WGPUQueueWorkDoneStatus status) { 27 OnWorkDoneData request; 28 if (!mOnWorkDoneRequests.Acquire(requestSerial, &request)) { 29 return false; 30 } 31 32 request.callback(status, request.userdata); 33 return true; 34 } 35 OnSubmittedWorkDone(uint64_t signalValue,WGPUQueueWorkDoneCallback callback,void * userdata)36 void Queue::OnSubmittedWorkDone(uint64_t signalValue, 37 WGPUQueueWorkDoneCallback callback, 38 void* userdata) { 39 if (client->IsDisconnected()) { 40 callback(WGPUQueueWorkDoneStatus_DeviceLost, userdata); 41 return; 42 } 43 44 uint64_t serial = mOnWorkDoneRequests.Add({callback, userdata}); 45 46 QueueOnSubmittedWorkDoneCmd cmd; 47 cmd.queueId = this->id; 48 cmd.signalValue = signalValue; 49 cmd.requestSerial = serial; 50 51 client->SerializeCommand(cmd); 52 } 53 WriteBuffer(WGPUBuffer cBuffer,uint64_t bufferOffset,const void * data,size_t size)54 void Queue::WriteBuffer(WGPUBuffer cBuffer, 55 uint64_t bufferOffset, 56 const void* data, 57 size_t size) { 58 Buffer* buffer = FromAPI(cBuffer); 59 60 QueueWriteBufferCmd cmd; 61 cmd.queueId = id; 62 cmd.bufferId = buffer->id; 63 cmd.bufferOffset = bufferOffset; 64 cmd.data = static_cast<const uint8_t*>(data); 65 cmd.size = size; 66 67 client->SerializeCommand(cmd); 68 } 69 WriteTexture(const WGPUImageCopyTexture * destination,const void * data,size_t dataSize,const WGPUTextureDataLayout * dataLayout,const WGPUExtent3D * writeSize)70 void Queue::WriteTexture(const WGPUImageCopyTexture* destination, 71 const void* data, 72 size_t dataSize, 73 const WGPUTextureDataLayout* dataLayout, 74 const WGPUExtent3D* writeSize) { 75 QueueWriteTextureCmd cmd; 76 cmd.queueId = id; 77 cmd.destination = destination; 78 cmd.data = static_cast<const uint8_t*>(data); 79 cmd.dataSize = dataSize; 80 cmd.dataLayout = dataLayout; 81 cmd.writeSize = writeSize; 82 83 client->SerializeCommand(cmd); 84 } 85 CancelCallbacksForDisconnect()86 void Queue::CancelCallbacksForDisconnect() { 87 ClearAllCallbacks(WGPUQueueWorkDoneStatus_DeviceLost); 88 } 89 ClearAllCallbacks(WGPUQueueWorkDoneStatus status)90 void Queue::ClearAllCallbacks(WGPUQueueWorkDoneStatus status) { 91 mOnWorkDoneRequests.CloseAll([status](OnWorkDoneData* request) { 92 if (request->callback != nullptr) { 93 request->callback(status, request->userdata); 94 } 95 }); 96 } 97 98 }} // namespace dawn_wire::client 99