xref: /aosp_15_r20/external/skia/src/gpu/ganesh/GrStagingBufferManager.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 GrStagingBufferManager_DEFINED
9 #define GrStagingBufferManager_DEFINED
10 
11 #include "include/core/SkRefCnt.h"
12 #include "src/gpu/ganesh/GrGpuBuffer.h"
13 
14 #include <cstddef>
15 #include <utility>
16 #include <vector>
17 
18 class GrGpu;
19 
20 class GrStagingBufferManager {
21 public:
GrStagingBufferManager(GrGpu * gpu)22     GrStagingBufferManager(GrGpu* gpu) : fGpu(gpu) {}
23 
24     struct Slice {
SliceSlice25         Slice() {}
SliceSlice26         Slice(GrGpuBuffer* buffer, size_t offset, void* offsetMapPtr)
27                 : fBuffer(buffer), fOffset(offset), fOffsetMapPtr(offsetMapPtr) {}
28         GrGpuBuffer* fBuffer = nullptr;
29         size_t fOffset = 0;
30         void* fOffsetMapPtr = nullptr;
31     };
32 
33     Slice allocateStagingBufferSlice(size_t size, size_t requiredAlignment = 1);
34 
35     // This call is used to move all the buffers off of the manager and to backend gpu by calling
36     // the virtual GrGpu::takeOwnershipOfBuffer on each buffer. This is called during
37     // submitToGpu. It is up to the backend to take refs to the buffers in their implemented
38     // takeOwnershipOfBuffer implementation if they need to. After this call returns the
39     // manager will have released all refs to its buffers.
40     void detachBuffers();
41 
hasBuffers()42     bool hasBuffers() { return !fBuffers.empty(); }
43 
reset()44     void reset() {
45         for (size_t i = 0; i < fBuffers.size(); ++i) {
46             fBuffers[i].fBuffer->unmap();
47         }
48         fBuffers.clear();
49     }
50 
51 private:
52     struct StagingBuffer {
StagingBufferStagingBuffer53         StagingBuffer(sk_sp<GrGpuBuffer> buffer, void* mapPtr)
54                 : fBuffer(std::move(buffer))
55                 , fMapPtr(mapPtr) {}
56 
57         sk_sp<GrGpuBuffer> fBuffer;
58         void* fMapPtr;
59         size_t fOffset = 0;
60 
remainingStagingBuffer61         size_t remaining() { return fBuffer->size() - fOffset; }
62     };
63 
64     std::vector<StagingBuffer> fBuffers;
65     GrGpu* fGpu;
66 };
67 
68 #endif
69 
70