xref: /aosp_15_r20/external/skia/src/gpu/graphite/Buffer.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2021 Google Inc.
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_Buffer_DEFINED
9 #define skgpu_graphite_Buffer_DEFINED
10 
11 #include "include/gpu/GpuTypes.h"
12 #include "src/gpu/graphite/Resource.h"
13 #include "src/gpu/graphite/ResourceTypes.h"
14 
15 namespace skgpu::graphite {
16 
17 class Buffer : public Resource {
18 public:
size()19     size_t size() const { return fSize; }
isProtected()20     Protected isProtected() const { return fIsProtected; }
21 
22     // TODO(b/262249983): Separate into mapRead(), mapWrite() methods.
23     // If the buffer is already mapped then pointer is returned. If an asyncMap() was started then
24     // it is waited on. Otherwise, a synchronous map is performed.
25     void* map();
26     // Starts a new asynchronous map.
27     void asyncMap(GpuFinishedProc = nullptr, GpuFinishedContext = nullptr);
28     // If the buffer is mapped then unmaps. If an async map is pending then it is cancelled.
29     void unmap();
30 
isMapped()31     bool isMapped() const { return fMapPtr; }
32 
33     // Returns true if mapped or an asyncMap was started and hasn't been completed or canceled.
34     virtual bool isUnmappable() const;
35 
getResourceType()36     const char* getResourceType() const override { return "Buffer"; }
37 
38 protected:
39     Buffer(const SharedContext* sharedContext,
40            size_t size,
41            Protected isProtected,
42            bool commandBufferRefsAsUsageRefs = false)
Resource(sharedContext,Ownership::kOwned,skgpu::Budgeted::kYes,size,commandBufferRefsAsUsageRefs)43             : Resource(sharedContext,
44                        Ownership::kOwned,
45                        skgpu::Budgeted::kYes,
46                        size,
47                        /*commandBufferRefsAsUsageRefs=*/commandBufferRefsAsUsageRefs)
48             , fSize(size)
49             , fIsProtected(isProtected) {}
50 
51     void* fMapPtr = nullptr;
52 
53 private:
54     virtual void onMap() = 0;
55     virtual void onAsyncMap(GpuFinishedProc, GpuFinishedContext);
56     virtual void onUnmap() = 0;
57 
58     size_t    fSize;
59     Protected fIsProtected;
60 };
61 
62 } // namespace skgpu::graphite
63 
64 #endif // skgpu_graphite_Buffer_DEFINED
65