xref: /aosp_15_r20/external/skia/src/gpu/ganesh/d3d/GrD3DBuffer.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2020 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 GrD3DBuffer_DEFINED
9 
10 #define GrD3DBuffer_DEFINED
11 
12 #include "include/gpu/ganesh/d3d/GrD3DTypes.h"
13 #include "src/gpu/ganesh/GrGpuBuffer.h"
14 #include "src/gpu/ganesh/GrManagedResource.h"
15 
16 class GrD3DGpu;
17 
18 class GrD3DBuffer : public GrGpuBuffer {
19 public:
20     static sk_sp<GrD3DBuffer> Make(GrD3DGpu*, size_t size, GrGpuBufferType, GrAccessPattern);
21 
~GrD3DBuffer()22     ~GrD3DBuffer() override {}
23 
d3dResource()24     ID3D12Resource* d3dResource() const {
25         SkASSERT(fD3DResource);
26         return fD3DResource.get();
27     }
28 
29     void setResourceState(const GrD3DGpu* gpu, D3D12_RESOURCE_STATES newResourceState);
30 
31 protected:
32     GrD3DBuffer(GrD3DGpu*, size_t size, GrGpuBufferType, GrAccessPattern, gr_cp<ID3D12Resource>,
33                 sk_sp<GrD3DAlloc>, D3D12_RESOURCE_STATES, std::string_view label);
34 
35     void onAbandon() override;
36     void onRelease() override;
37 
38     D3D12_RESOURCE_STATES fResourceState;
39 
40 private:
41     void releaseResource();
42 
43     void onMap(MapType) override;
44     void onUnmap(MapType) override;
45     bool onClearToZero() override;
46     bool onUpdateData(const void* src, size_t offset, size_t size, bool preserve) override;
47 
48     void* internalMap(MapType, size_t offset, size_t size);
49     void internalUnmap(MapType, size_t offset, size_t size);
50 
51 #ifdef SK_DEBUG
52     void validate() const;
53 #endif
54 
55     void onSetLabel() override;
56 
getD3DGpu()57     GrD3DGpu* getD3DGpu() const {
58         SkASSERT(!this->wasDestroyed());
59         return reinterpret_cast<GrD3DGpu*>(this->getGpu());
60     }
61 
62     gr_cp<ID3D12Resource> fD3DResource;
63     sk_sp<GrD3DAlloc> fAlloc;
64     ID3D12Resource* fStagingBuffer = nullptr;
65     size_t fStagingOffset = 0;
66 
67     using INHERITED = GrGpuBuffer;
68 };
69 
70 #endif
71