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 #ifndef DAWNWIRE_CLIENT_CLIENT_H_ 16 #define DAWNWIRE_CLIENT_CLIENT_H_ 17 18 #include <dawn/webgpu.h> 19 #include <dawn_wire/Wire.h> 20 21 #include "common/LinkedList.h" 22 #include "common/NonCopyable.h" 23 #include "dawn_wire/ChunkedCommandSerializer.h" 24 #include "dawn_wire/WireClient.h" 25 #include "dawn_wire/WireCmd_autogen.h" 26 #include "dawn_wire/WireDeserializeAllocator.h" 27 #include "dawn_wire/client/ClientBase_autogen.h" 28 29 namespace dawn_wire { namespace client { 30 31 class Device; 32 class MemoryTransferService; 33 34 class Client : public ClientBase { 35 public: 36 Client(CommandSerializer* serializer, MemoryTransferService* memoryTransferService); 37 ~Client() override; 38 39 // ChunkedCommandHandler implementation 40 const volatile char* HandleCommandsImpl(const volatile char* commands, 41 size_t size) override; 42 GetMemoryTransferService()43 MemoryTransferService* GetMemoryTransferService() const { 44 return mMemoryTransferService; 45 } 46 47 ReservedTexture ReserveTexture(WGPUDevice device); 48 ReservedSwapChain ReserveSwapChain(WGPUDevice device); 49 ReservedDevice ReserveDevice(); 50 51 void ReclaimTextureReservation(const ReservedTexture& reservation); 52 void ReclaimSwapChainReservation(const ReservedSwapChain& reservation); 53 void ReclaimDeviceReservation(const ReservedDevice& reservation); 54 55 template <typename Cmd> SerializeCommand(const Cmd & cmd)56 void SerializeCommand(const Cmd& cmd) { 57 mSerializer.SerializeCommand(cmd, *this); 58 } 59 60 template <typename Cmd, typename ExtraSizeSerializeFn> SerializeCommand(const Cmd & cmd,size_t extraSize,ExtraSizeSerializeFn && SerializeExtraSize)61 void SerializeCommand(const Cmd& cmd, 62 size_t extraSize, 63 ExtraSizeSerializeFn&& SerializeExtraSize) { 64 mSerializer.SerializeCommand(cmd, *this, extraSize, SerializeExtraSize); 65 } 66 67 void Disconnect(); 68 bool IsDisconnected() const; 69 70 template <typename T> TrackObject(T * object)71 void TrackObject(T* object) { 72 mObjects[ObjectTypeToTypeEnum<T>::value].Append(object); 73 } 74 75 private: 76 void DestroyAllObjects(); 77 78 #include "dawn_wire/client/ClientPrototypes_autogen.inc" 79 80 ChunkedCommandSerializer mSerializer; 81 WireDeserializeAllocator mAllocator; 82 MemoryTransferService* mMemoryTransferService = nullptr; 83 std::unique_ptr<MemoryTransferService> mOwnedMemoryTransferService = nullptr; 84 85 PerObjectType<LinkedList<ObjectBase>> mObjects; 86 bool mDisconnected = false; 87 }; 88 89 std::unique_ptr<MemoryTransferService> CreateInlineMemoryTransferService(); 90 91 }} // namespace dawn_wire::client 92 93 #endif // DAWNWIRE_CLIENT_CLIENT_H_ 94