webgpu/dawn/include/dawn/platform/DawnPlatform.h
  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 INCLUDE_DAWN_PLATFORM_DAWNPLATFORM_H_
 16#define INCLUDE_DAWN_PLATFORM_DAWNPLATFORM_H_
 17
 18#include <cstddef>
 19#include <cstdint>
 20#include <memory>
 21
 22#include "dawn/platform/dawn_platform_export.h"
 23#include "dawn/webgpu.h"
 24
 25namespace dawn::platform {
 26
 27enum class TraceCategory {
 28    General,     // General trace events
 29    Validation,  // Dawn validation
 30    Recording,   // Native command recording
 31    GPUWork,     // Actual GPU work
 32};
 33
 34class DAWN_PLATFORM_EXPORT CachingInterface {
 35  public:
 36    CachingInterface();
 37    virtual ~CachingInterface();
 38
 39    // LoadData has two modes. The first mode is used to get a value which
 40    // corresponds to the |key|. The |valueOut| is a caller provided buffer
 41    // allocated to the size |valueSize| which is loaded with data of the
 42    // size returned. The second mode is used to query for the existence of
 43    // the |key| where |valueOut| is nullptr and |valueSize| must be 0.
 44    // The return size is non-zero if the |key| exists.
 45    virtual size_t LoadData(const void* key, size_t keySize, void* valueOut, size_t valueSize) = 0;
 46
 47    // StoreData puts a |value| in the cache which corresponds to the |key|.
 48    virtual void StoreData(const void* key,
 49                           size_t keySize,
 50                           const void* value,
 51                           size_t valueSize) = 0;
 52
 53  private:
 54    CachingInterface(const CachingInterface&) = delete;
 55    CachingInterface& operator=(const CachingInterface&) = delete;
 56};
 57
 58class DAWN_PLATFORM_EXPORT WaitableEvent {
 59  public:
 60    WaitableEvent() = default;
 61    virtual ~WaitableEvent() = default;
 62    virtual void Wait() = 0;        // Wait for completion
 63    virtual bool IsComplete() = 0;  // Non-blocking check if the event is complete
 64};
 65
 66using PostWorkerTaskCallback = void (*)(void* userdata);
 67
 68class DAWN_PLATFORM_EXPORT WorkerTaskPool {
 69  public:
 70    WorkerTaskPool() = default;
 71    virtual ~WorkerTaskPool() = default;
 72    virtual std::unique_ptr<WaitableEvent> PostWorkerTask(PostWorkerTaskCallback,
 73                                                          void* userdata) = 0;
 74};
 75
 76class DAWN_PLATFORM_EXPORT Platform {
 77  public:
 78    Platform();
 79    virtual ~Platform();
 80
 81    virtual const unsigned char* GetTraceCategoryEnabledFlag(TraceCategory category);
 82
 83    virtual double MonotonicallyIncreasingTime();
 84
 85    virtual uint64_t AddTraceEvent(char phase,
 86                                   const unsigned char* categoryGroupEnabled,
 87                                   const char* name,
 88                                   uint64_t id,
 89                                   double timestamp,
 90                                   int numArgs,
 91                                   const char** argNames,
 92                                   const unsigned char* argTypes,
 93                                   const uint64_t* argValues,
 94                                   unsigned char flags);
 95
 96    // Invoked to add a UMA histogram count-based sample
 97    virtual void HistogramCustomCounts(const char* name,
 98                                       int sample,
 99                                       int min,
100                                       int max,
101                                       int bucketCount);
102
103    // Invoked to add a UMA histogram count-based sample that requires high-performance
104    // counter (HPC) support.
105    virtual void HistogramCustomCountsHPC(const char* name,
106                                          int sample,
107                                          int min,
108                                          int max,
109                                          int bucketCount);
110
111    // Invoked to add a UMA histogram enumeration sample
112    virtual void HistogramEnumeration(const char* name, int sample, int boundaryValue);
113
114    // Invoked to add a UMA histogram sparse sample
115    virtual void HistogramSparse(const char* name, int sample);
116
117    // Invoked to add a UMA histogram boolean sample
118    virtual void HistogramBoolean(const char* name, bool sample);
119
120    // The returned CachingInterface is expected to outlive the device which uses it to persistently
121    // cache objects.
122    virtual CachingInterface* GetCachingInterface();
123
124    virtual std::unique_ptr<WorkerTaskPool> CreateWorkerTaskPool();
125
126  private:
127    Platform(const Platform&) = delete;
128    Platform& operator=(const Platform&) = delete;
129};
130
131}  // namespace dawn::platform
132
133// TODO(dawn:824): Remove once the deprecation period is passed.
134namespace dawn_platform = dawn::platform;
135
136#endif  // INCLUDE_DAWN_PLATFORM_DAWNPLATFORM_H_