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/gpu/graphite/dawn/DawnTypes.h" 15 #include "include/private/base/SkTArray.h" 16 #include "src/gpu/RefCntedCallback.h" 17 #include "src/gpu/graphite/Buffer.h" 18 #include "src/gpu/graphite/dawn/DawnAsyncWait.h" 19 #include "src/gpu/graphite/dawn/DawnSharedContext.h" 20 21 namespace skgpu::graphite { 22 23 class DawnBuffer : public Buffer { 24 public: 25 static sk_sp<DawnBuffer> Make(const DawnSharedContext*, 26 size_t size, 27 BufferType type, 28 AccessPattern); 29 30 bool isUnmappable() const override; 31 dawnBuffer()32 const wgpu::Buffer& dawnBuffer() const { return fBuffer; } 33 34 private: 35 DawnBuffer(const DawnSharedContext*, size_t size, wgpu::Buffer, void* mapAtCreationPtr); 36 37 #if defined(__EMSCRIPTEN__) 38 void prepareForReturnToCache(const std::function<void()>& takeRef) override; 39 void onAsyncMap(GpuFinishedProc, GpuFinishedContext) override; 40 #endif 41 void onMap() override; 42 void onUnmap() override; 43 44 template <typename StatusT, typename MessageT> 45 void mapCallback(StatusT status, MessageT message); 46 47 void freeGpuData() override; 48 dawnSharedContext()49 const DawnSharedContext* dawnSharedContext() const { 50 return static_cast<const DawnSharedContext*>(this->sharedContext()); 51 } 52 53 void setBackendLabel(char const* label) override; 54 55 wgpu::Buffer fBuffer; 56 SkMutex fAsyncMutex; 57 skia_private::TArray<sk_sp<RefCntedCallback>> fAsyncMapCallbacks SK_GUARDED_BY(fAsyncMutex); 58 }; 59 60 } // namespace skgpu::graphite 61 62 #endif // skgpu_graphite_DawnBuffer_DEFINED 63 64