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_WIRECLIENT_H_ 16 #define DAWNWIRE_WIRECLIENT_H_ 17 18 #include "dawn/dawn_proc_table.h" 19 #include "dawn_wire/Wire.h" 20 21 #include <memory> 22 #include <vector> 23 24 namespace dawn_wire { 25 26 namespace client { 27 class Client; 28 class MemoryTransferService; 29 30 DAWN_WIRE_EXPORT const DawnProcTable& GetProcs(); 31 } // namespace client 32 33 struct ReservedTexture { 34 WGPUTexture texture; 35 uint32_t id; 36 uint32_t generation; 37 uint32_t deviceId; 38 uint32_t deviceGeneration; 39 }; 40 41 struct ReservedSwapChain { 42 WGPUSwapChain swapchain; 43 uint32_t id; 44 uint32_t generation; 45 uint32_t deviceId; 46 uint32_t deviceGeneration; 47 }; 48 49 struct ReservedDevice { 50 WGPUDevice device; 51 uint32_t id; 52 uint32_t generation; 53 }; 54 55 struct DAWN_WIRE_EXPORT WireClientDescriptor { 56 CommandSerializer* serializer; 57 client::MemoryTransferService* memoryTransferService = nullptr; 58 }; 59 60 class DAWN_WIRE_EXPORT WireClient : public CommandHandler { 61 public: 62 WireClient(const WireClientDescriptor& descriptor); 63 ~WireClient() override; 64 65 const volatile char* HandleCommands(const volatile char* commands, 66 size_t size) override final; 67 68 ReservedTexture ReserveTexture(WGPUDevice device); 69 ReservedSwapChain ReserveSwapChain(WGPUDevice device); 70 ReservedDevice ReserveDevice(); 71 72 void ReclaimTextureReservation(const ReservedTexture& reservation); 73 void ReclaimSwapChainReservation(const ReservedSwapChain& reservation); 74 void ReclaimDeviceReservation(const ReservedDevice& reservation); 75 76 // Disconnects the client. 77 // Commands allocated after this point will not be sent. 78 void Disconnect(); 79 80 private: 81 std::unique_ptr<client::Client> mImpl; 82 }; 83 84 namespace client { 85 class DAWN_WIRE_EXPORT MemoryTransferService { 86 public: 87 MemoryTransferService(); 88 virtual ~MemoryTransferService(); 89 90 class ReadHandle; 91 class WriteHandle; 92 93 // Create a handle for reading server data. 94 // This may fail and return nullptr. 95 virtual ReadHandle* CreateReadHandle(size_t) = 0; 96 97 // Create a handle for writing server data. 98 // This may fail and return nullptr. 99 virtual WriteHandle* CreateWriteHandle(size_t) = 0; 100 101 class DAWN_WIRE_EXPORT ReadHandle { 102 public: 103 ReadHandle(); 104 virtual ~ReadHandle(); 105 106 // Get the required serialization size for SerializeCreate 107 virtual size_t SerializeCreateSize() = 0; 108 109 // Serialize the handle into |serializePointer| so it can be received by the server. 110 virtual void SerializeCreate(void* serializePointer) = 0; 111 112 // Simply return the base address of the allocation (without applying any offset) 113 // Returns nullptr if the allocation failed. 114 // The data must live at least until the ReadHandle is destructued 115 virtual const void* GetData() = 0; 116 117 // Gets called when a MapReadCallback resolves. 118 // deserialize the data update and apply 119 // it to the range (offset, offset + size) of allocation 120 // There could be nothing to be deserialized (if using shared memory) 121 // Needs to check potential offset/size OOB and overflow 122 virtual bool DeserializeDataUpdate(const void* deserializePointer, 123 size_t deserializeSize, 124 size_t offset, 125 size_t size) = 0; 126 127 private: 128 ReadHandle(const ReadHandle&) = delete; 129 ReadHandle& operator=(const ReadHandle&) = delete; 130 }; 131 132 class DAWN_WIRE_EXPORT WriteHandle { 133 public: 134 WriteHandle(); 135 virtual ~WriteHandle(); 136 137 // Get the required serialization size for SerializeCreate 138 virtual size_t SerializeCreateSize() = 0; 139 140 // Serialize the handle into |serializePointer| so it can be received by the server. 141 virtual void SerializeCreate(void* serializePointer) = 0; 142 143 // Simply return the base address of the allocation (without applying any offset) 144 // The data returned should be zero-initialized. 145 // The data returned must live at least until the WriteHandle is destructed. 146 // On failure, the pointer returned should be null. 147 virtual void* GetData() = 0; 148 149 // Get the required serialization size for SerializeDataUpdate 150 virtual size_t SizeOfSerializeDataUpdate(size_t offset, size_t size) = 0; 151 152 // Serialize a command to send the modified contents of 153 // the subrange (offset, offset + size) of the allocation at buffer unmap 154 // This subrange is always the whole mapped region for now 155 // There could be nothing to be serialized (if using shared memory) 156 virtual void SerializeDataUpdate(void* serializePointer, 157 size_t offset, 158 size_t size) = 0; 159 160 private: 161 WriteHandle(const WriteHandle&) = delete; 162 WriteHandle& operator=(const WriteHandle&) = delete; 163 }; 164 165 private: 166 MemoryTransferService(const MemoryTransferService&) = delete; 167 MemoryTransferService& operator=(const MemoryTransferService&) = delete; 168 }; 169 170 // Backdoor to get the order of the ProcMap for testing 171 DAWN_WIRE_EXPORT std::vector<const char*> GetProcMapNamesForTesting(); 172 } // namespace client 173 } // namespace dawn_wire 174 175 #endif // DAWNWIRE_WIRECLIENT_H_ 176