• 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_CLIENT_DEVICE_H_
16 #define DAWNWIRE_CLIENT_DEVICE_H_
17 
18 #include <dawn/webgpu.h>
19 
20 #include "common/LinkedList.h"
21 #include "dawn_wire/WireCmd_autogen.h"
22 #include "dawn_wire/client/ApiObjects_autogen.h"
23 #include "dawn_wire/client/ObjectBase.h"
24 #include "dawn_wire/client/RequestTracker.h"
25 
26 #include <memory>
27 
28 namespace dawn_wire { namespace client {
29 
30     class Client;
31     class Queue;
32 
33     class Device final : public ObjectBase {
34       public:
35         Device(Client* client, uint32_t refcount, uint32_t id);
36         ~Device();
37 
38         void SetUncapturedErrorCallback(WGPUErrorCallback errorCallback, void* errorUserdata);
39         void SetLoggingCallback(WGPULoggingCallback errorCallback, void* errorUserdata);
40         void SetDeviceLostCallback(WGPUDeviceLostCallback errorCallback, void* errorUserdata);
41         void InjectError(WGPUErrorType type, const char* message);
42         void PushErrorScope(WGPUErrorFilter filter);
43         bool PopErrorScope(WGPUErrorCallback callback, void* userdata);
44         WGPUBuffer CreateBuffer(const WGPUBufferDescriptor* descriptor);
45         WGPUBuffer CreateErrorBuffer();
46         WGPUComputePipeline CreateComputePipeline(WGPUComputePipelineDescriptor const* descriptor);
47         void CreateComputePipelineAsync(WGPUComputePipelineDescriptor const* descriptor,
48                                         WGPUCreateComputePipelineAsyncCallback callback,
49                                         void* userdata);
50         void CreateRenderPipelineAsync(WGPURenderPipelineDescriptor const* descriptor,
51                                        WGPUCreateRenderPipelineAsyncCallback callback,
52                                        void* userdata);
53 
54         void HandleError(WGPUErrorType errorType, const char* message);
55         void HandleLogging(WGPULoggingType loggingType, const char* message);
56         void HandleDeviceLost(WGPUDeviceLostReason reason, const char* message);
57         bool OnPopErrorScopeCallback(uint64_t requestSerial,
58                                      WGPUErrorType type,
59                                      const char* message);
60         bool OnCreateComputePipelineAsyncCallback(uint64_t requestSerial,
61                                                   WGPUCreatePipelineAsyncStatus status,
62                                                   const char* message);
63         bool OnCreateRenderPipelineAsyncCallback(uint64_t requestSerial,
64                                                  WGPUCreatePipelineAsyncStatus status,
65                                                  const char* message);
66 
67         bool GetLimits(WGPUSupportedLimits* limits);
68         WGPUQueue GetQueue();
69 
70         void CancelCallbacksForDisconnect() override;
71 
72         std::weak_ptr<bool> GetAliveWeakPtr();
73 
74       private:
75         struct ErrorScopeData {
76             WGPUErrorCallback callback = nullptr;
77             void* userdata = nullptr;
78         };
79         RequestTracker<ErrorScopeData> mErrorScopes;
80         uint64_t mErrorScopeStackSize = 0;
81 
82         struct CreatePipelineAsyncRequest {
83             WGPUCreateComputePipelineAsyncCallback createComputePipelineAsyncCallback = nullptr;
84             WGPUCreateRenderPipelineAsyncCallback createRenderPipelineAsyncCallback = nullptr;
85             void* userdata = nullptr;
86             ObjectId pipelineObjectID;
87         };
88         RequestTracker<CreatePipelineAsyncRequest> mCreatePipelineAsyncRequests;
89 
90         WGPUErrorCallback mErrorCallback = nullptr;
91         WGPUDeviceLostCallback mDeviceLostCallback = nullptr;
92         WGPULoggingCallback mLoggingCallback = nullptr;
93         bool mDidRunLostCallback = false;
94         void* mErrorUserdata = nullptr;
95         void* mDeviceLostUserdata = nullptr;
96         void* mLoggingUserdata = nullptr;
97 
98         Queue* mQueue = nullptr;
99 
100         std::shared_ptr<bool> mIsAlive;
101     };
102 
103 }}  // namespace dawn_wire::client
104 
105 #endif  // DAWNWIRE_CLIENT_DEVICE_H_
106