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 dawnBuffer()31 const wgpu::Buffer& dawnBuffer() const { return fBuffer; } 32 33 private: 34 DawnBuffer(const DawnSharedContext*, size_t size, wgpu::Buffer, void* mapAtCreationPtr); 35 36 #if defined(__EMSCRIPTEN__) 37 bool prepareForReturnToCache(const std::function<void()>& takeRef) override; 38 void onAsyncMap(GpuFinishedProc, GpuFinishedContext) override; 39 #endif 40 void onMap() override; 41 void onUnmap() override; 42 43 template <typename StatusT, typename MessageT> 44 void mapCallback(StatusT status, MessageT message); 45 46 void freeGpuData() override; 47 dawnSharedContext()48 const DawnSharedContext* dawnSharedContext() const { 49 return static_cast<const DawnSharedContext*>(this->sharedContext()); 50 } 51 52 void setBackendLabel(char const* label) override; 53 54 wgpu::Buffer fBuffer; 55 SkMutex fAsyncMutex; 56 skia_private::TArray<sk_sp<RefCntedCallback>> fAsyncMapCallbacks SK_GUARDED_BY(fAsyncMutex); 57 }; 58 59 } // namespace skgpu::graphite 60 61 #endif // skgpu_graphite_DawnBuffer_DEFINED 62