| 1 |
/* |
| 2 |
* Copyright 2022 Google LLC |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef skgpu_graphite_DawnBuffer_DEFINED |
| 9 |
#define skgpu_graphite_DawnBuffer_DEFINED |
| 10 |
|
| 11 |
#include "webgpu/webgpu_cpp.h" // NO_G3_REWRITE |
| 12 |
|
| 13 |
#include "include/core/SkRefCnt.h" |
| 14 |
#include "include/private/base/SkTArray.h" |
| 15 |
#include "src/gpu/RefCntedCallback.h" |
| 16 |
#include "src/gpu/graphite/Buffer.h" |
| 17 |
#include "src/gpu/graphite/dawn/DawnAsyncWait.h" |
| 18 |
#include "src/gpu/graphite/dawn/DawnSharedContext.h" |
| 19 |
|
| 20 |
namespace skgpu::graphite { |
| 21 |
|
| 22 |
class DawnBuffer : public Buffer { |
| 23 |
public: |
| 24 |
static sk_sp<DawnBuffer> Make(const DawnSharedContext*, |
| 25 |
size_t size, |
| 26 |
BufferType type, |
| 27 |
AccessPattern); |
| 28 |
|
| 29 |
bool isUnmappable() const override; |
| 30 |
|
| 31 |
const wgpu::Buffer& dawnBuffer() const { return fBuffer; } |
| 32 |
|
| •33 |
const wgpu::BindGroup* getCachedSingleBufferBindGroup(size_t bindingSize) const; |
| 34 |
void addCachedSingleBufferBindGroup(wgpu::BindGroup, size_t bindingSize) const; |
| 35 |
|
| 36 |
private: |
| 37 |
DawnBuffer(const DawnSharedContext*, size_t size, wgpu::Buffer, void* mapAtCreationPtr); |
| 38 |
|
| 39 |
#if defined(__EMSCRIPTEN__) |
| 40 |
bool prepareForReturnToCache(Resource::TakeRefFunc takeRef, void* takeRefCtx) override; |
| 41 |
void onAsyncMap(GpuFinishedProc, GpuFinishedContext) override; |
| 42 |
#endif |
| 43 |
void onMap() override; |
| 44 |
void onUnmap() override; |
| 45 |
|
| 46 |
template <typename StatusT, typename MessageT> |
| 47 |
void mapCallback(StatusT status, MessageT message); |
| 48 |
|
| 49 |
void freeGpuData() override; |
| 50 |
|
| 51 |
const DawnSharedContext* dawnSharedContext() const { |
| 52 |
return static_cast<const DawnSharedContext*>(this->sharedContext()); |
| 53 |
} |
| 54 |
|
| 55 |
void setBackendLabel(char const* label) override; |
| 56 |
|
| 57 |
wgpu::Buffer fBuffer; |
| 58 |
SkMutex fAsyncMutex; |
| 59 |
skia_private::STArray<1, AutoCallback> fAsyncMapCallbacks SK_GUARDED_BY(fAsyncMutex); |
| •60 |
|
| 61 |
// By the time the command buffer requests a bind group, the provided Buffer pointer is const |
| 62 |
// so this attribute must be mutable to avoid a const_cast. |
| 63 |
mutable skia_private::TArray<std::pair<size_t, wgpu::BindGroup>> fCachedSingleBufferBindGroups; |
| 64 |
}; |
| 65 |
|
| 66 |
} // namespace skgpu::graphite |
| 67 |
|
| 68 |
#endif // skgpu_graphite_DawnBuffer_DEFINED |