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 DAWNPLATFORM_DAWNPLATFORM_H_ 16 #define DAWNPLATFORM_DAWNPLATFORM_H_ 17 18 #include "dawn_platform/dawn_platform_export.h" 19 20 #include <cstddef> 21 #include <cstdint> 22 #include <memory> 23 24 #include <dawn/webgpu.h> 25 26 namespace dawn_platform { 27 28 enum class TraceCategory { 29 General, // General trace events 30 Validation, // Dawn validation 31 Recording, // Native command recording 32 GPUWork, // Actual GPU work 33 }; 34 35 class DAWN_PLATFORM_EXPORT CachingInterface { 36 public: 37 CachingInterface(); 38 virtual ~CachingInterface(); 39 40 // LoadData has two modes. The first mode is used to get a value which 41 // corresponds to the |key|. The |valueOut| is a caller provided buffer 42 // allocated to the size |valueSize| which is loaded with data of the 43 // size returned. The second mode is used to query for the existence of 44 // the |key| where |valueOut| is nullptr and |valueSize| must be 0. 45 // The return size is non-zero if the |key| exists. 46 virtual size_t LoadData(const WGPUDevice device, 47 const void* key, 48 size_t keySize, 49 void* valueOut, 50 size_t valueSize) = 0; 51 52 // StoreData puts a |value| in the cache which corresponds to the |key|. 53 virtual void StoreData(const WGPUDevice device, 54 const void* key, 55 size_t keySize, 56 const void* value, 57 size_t valueSize) = 0; 58 59 private: 60 CachingInterface(const CachingInterface&) = delete; 61 CachingInterface& operator=(const CachingInterface&) = delete; 62 }; 63 64 class DAWN_PLATFORM_EXPORT WaitableEvent { 65 public: 66 WaitableEvent() = default; 67 virtual ~WaitableEvent() = default; 68 virtual void Wait() = 0; // Wait for completion 69 virtual bool IsComplete() = 0; // Non-blocking check if the event is complete 70 }; 71 72 using PostWorkerTaskCallback = void (*)(void* userdata); 73 74 class DAWN_PLATFORM_EXPORT WorkerTaskPool { 75 public: 76 WorkerTaskPool() = default; 77 virtual ~WorkerTaskPool() = default; 78 virtual std::unique_ptr<WaitableEvent> PostWorkerTask(PostWorkerTaskCallback, 79 void* userdata) = 0; 80 }; 81 82 class DAWN_PLATFORM_EXPORT Platform { 83 public: 84 Platform(); 85 virtual ~Platform(); 86 87 virtual const unsigned char* GetTraceCategoryEnabledFlag(TraceCategory category); 88 89 virtual double MonotonicallyIncreasingTime(); 90 91 virtual uint64_t AddTraceEvent(char phase, 92 const unsigned char* categoryGroupEnabled, 93 const char* name, 94 uint64_t id, 95 double timestamp, 96 int numArgs, 97 const char** argNames, 98 const unsigned char* argTypes, 99 const uint64_t* argValues, 100 unsigned char flags); 101 102 // The |fingerprint| is provided by Dawn to inform the client to discard the Dawn caches 103 // when the fingerprint changes. The returned CachingInterface is expected to outlive the 104 // device which uses it to persistently cache objects. 105 virtual CachingInterface* GetCachingInterface(const void* fingerprint, 106 size_t fingerprintSize); 107 virtual std::unique_ptr<WorkerTaskPool> CreateWorkerTaskPool(); 108 109 private: 110 Platform(const Platform&) = delete; 111 Platform& operator=(const Platform&) = delete; 112 }; 113 114 } // namespace dawn_platform 115 116 #endif // DAWNPLATFORM_DAWNPLATFORM_H_ 117