• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <memory>
19 
20 #include "dawn_wire/Wire.h"
21 
22 namespace dawn_wire {
23 
24     namespace client {
25         class Client;
26         class MemoryTransferService;
27     }
28 
29     struct ReservedTexture {
30         DawnTexture texture;
31         uint32_t id;
32         uint32_t generation;
33     };
34 
35     struct DAWN_WIRE_EXPORT WireClientDescriptor {
36         CommandSerializer* serializer;
37         client::MemoryTransferService* memoryTransferService = nullptr;
38     };
39 
40     class DAWN_WIRE_EXPORT WireClient : public CommandHandler {
41       public:
42         WireClient(const WireClientDescriptor& descriptor);
43         ~WireClient();
44 
45         DawnDevice GetDevice() const;
46         DawnProcTable GetProcs() const;
47         const char* HandleCommands(const char* commands, size_t size) override final;
48 
49         ReservedTexture ReserveTexture(DawnDevice device);
50 
51       private:
52         std::unique_ptr<client::Client> mImpl;
53     };
54 
55     namespace client {
56         class DAWN_WIRE_EXPORT MemoryTransferService {
57           public:
58             class ReadHandle;
59             class WriteHandle;
60 
61             virtual ~MemoryTransferService();
62 
63             // Create a handle for reading server data.
64             // This may fail and return nullptr.
65             virtual ReadHandle* CreateReadHandle(size_t) = 0;
66 
67             // Create a handle for writing server data.
68             // This may fail and return nullptr.
69             virtual WriteHandle* CreateWriteHandle(size_t) = 0;
70 
71             // Imported memory implementation needs to override these to create Read/Write
72             // handles associated with a particular buffer. The client should receive a file
73             // descriptor for the buffer out-of-band.
74             virtual ReadHandle* CreateReadHandle(DawnBuffer, uint64_t offset, size_t size);
75             virtual WriteHandle* CreateWriteHandle(DawnBuffer, uint64_t offset, size_t size);
76 
77             class ReadHandle {
78               public:
79                 // Serialize the handle into |serializePointer| so it can be received by the server.
80                 // If |serializePointer| is nullptr, this returns the required serialization space.
81                 virtual size_t SerializeCreate(void* serializePointer = nullptr) = 0;
82 
83                 // Load initial data and open the handle for reading.
84                 // This function takes in the serialized result of
85                 // server::MemoryTransferService::ReadHandle::SerializeInitialData.
86                 // This function should write to |data| and |dataLength| the pointer and size of the
87                 // mapped data for reading. It must live at least until the ReadHandle is
88                 // destructed.
89                 virtual bool DeserializeInitialData(const void* deserializePointer,
90                                                     size_t deserializeSize,
91                                                     const void** data,
92                                                     size_t* dataLength) = 0;
93                 virtual ~ReadHandle();
94             };
95 
96             class WriteHandle {
97               public:
98                 // Serialize the handle into |serializePointer| so it can be received by the server.
99                 // If |serializePointer| is nullptr, this returns the required serialization space.
100                 virtual size_t SerializeCreate(void* serializePointer = nullptr) = 0;
101 
102                 // Open the handle for reading. The data returned should be zero-initialized.
103                 // The data returned must live at least until the WriteHandle is destructed.
104                 // On failure, the pointer returned should be null.
105                 virtual std::pair<void*, size_t> Open() = 0;
106 
107                 // Flush writes to the handle. This should serialize info to send updates to the
108                 // server.
109                 // If |serializePointer| is nullptr, this returns the required serialization space.
110                 virtual size_t SerializeFlush(void* serializePointer = nullptr) = 0;
111                 virtual ~WriteHandle();
112             };
113         };
114     }  // namespace client
115 
116 }  // namespace dawn_wire
117 
118 #endif  // DAWNWIRE_WIRECLIENT_H_
119