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_WIRESERVER_H_ 16 #define DAWNWIRE_WIRESERVER_H_ 17 18 #include <memory> 19 20 #include "dawn_wire/Wire.h" 21 22 struct DawnProcTable; 23 24 namespace dawn_wire { 25 26 namespace server { 27 class Server; 28 class MemoryTransferService; 29 } // namespace server 30 31 struct DAWN_WIRE_EXPORT WireServerDescriptor { 32 const DawnProcTable* procs; 33 CommandSerializer* serializer; 34 server::MemoryTransferService* memoryTransferService = nullptr; 35 }; 36 37 class DAWN_WIRE_EXPORT WireServer : public CommandHandler { 38 public: 39 WireServer(const WireServerDescriptor& descriptor); 40 ~WireServer() override; 41 42 const volatile char* HandleCommands(const volatile char* commands, 43 size_t size) override final; 44 45 bool InjectTexture(WGPUTexture texture, 46 uint32_t id, 47 uint32_t generation, 48 uint32_t deviceId, 49 uint32_t deviceGeneration); 50 bool InjectSwapChain(WGPUSwapChain swapchain, 51 uint32_t id, 52 uint32_t generation, 53 uint32_t deviceId, 54 uint32_t deviceGeneration); 55 56 bool InjectDevice(WGPUDevice device, uint32_t id, uint32_t generation); 57 58 // Look up a device by (id, generation) pair. Returns nullptr if the generation 59 // has expired or the id is not found. 60 // The Wire does not have destroy hooks to allow an embedder to observe when an object 61 // has been destroyed, but in Chrome, we need to know the list of live devices so we 62 // can call device.Tick() on all of them periodically to ensure progress on asynchronous 63 // work is made. Getting this list can be done by tracking the (id, generation) of 64 // previously injected devices, and observing if GetDevice(id, generation) returns non-null. 65 WGPUDevice GetDevice(uint32_t id, uint32_t generation); 66 67 private: 68 std::unique_ptr<server::Server> mImpl; 69 }; 70 71 namespace server { 72 class DAWN_WIRE_EXPORT MemoryTransferService { 73 public: 74 MemoryTransferService(); 75 virtual ~MemoryTransferService(); 76 77 class ReadHandle; 78 class WriteHandle; 79 80 // Deserialize data to create Read/Write handles. These handles are for the client 81 // to Read/Write data. 82 virtual bool DeserializeReadHandle(const void* deserializePointer, 83 size_t deserializeSize, 84 ReadHandle** readHandle) = 0; 85 virtual bool DeserializeWriteHandle(const void* deserializePointer, 86 size_t deserializeSize, 87 WriteHandle** writeHandle) = 0; 88 89 class DAWN_WIRE_EXPORT ReadHandle { 90 public: 91 ReadHandle(); 92 virtual ~ReadHandle(); 93 94 // Return the size of the command serialized if 95 // SerializeDataUpdate is called with the same offset/size args 96 virtual size_t SizeOfSerializeDataUpdate(size_t offset, size_t size) = 0; 97 98 // Gets called when a MapReadCallback resolves. 99 // Serialize the data update for the range (offset, offset + size) into 100 // |serializePointer| to the client There could be nothing to be serialized (if 101 // using shared memory) 102 virtual void SerializeDataUpdate(const void* data, 103 size_t offset, 104 size_t size, 105 void* serializePointer) = 0; 106 107 private: 108 ReadHandle(const ReadHandle&) = delete; 109 ReadHandle& operator=(const ReadHandle&) = delete; 110 }; 111 112 class DAWN_WIRE_EXPORT WriteHandle { 113 public: 114 WriteHandle(); 115 virtual ~WriteHandle(); 116 117 // Set the target for writes from the client. DeserializeFlush should copy data 118 // into the target. 119 void SetTarget(void* data); 120 // Set Staging data length for OOB check 121 void SetDataLength(size_t dataLength); 122 123 // This function takes in the serialized result of 124 // client::MemoryTransferService::WriteHandle::SerializeDataUpdate. 125 // Needs to check potential offset/size OOB and overflow 126 virtual bool DeserializeDataUpdate(const void* deserializePointer, 127 size_t deserializeSize, 128 size_t offset, 129 size_t size) = 0; 130 131 protected: 132 void* mTargetData = nullptr; 133 size_t mDataLength = 0; 134 135 private: 136 WriteHandle(const WriteHandle&) = delete; 137 WriteHandle& operator=(const WriteHandle&) = delete; 138 }; 139 140 private: 141 MemoryTransferService(const MemoryTransferService&) = delete; 142 MemoryTransferService& operator=(const MemoryTransferService&) = delete; 143 }; 144 } // namespace server 145 146 } // namespace dawn_wire 147 148 #endif // DAWNWIRE_WIRESERVER_H_ 149