1 //
2 // Copyright 2021 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // CLMemory.h: Defines the abstract cl::Memory class, which is a memory object
7 // and the base class for OpenCL objects such as Buffer, Image and Pipe.
8
9 #ifndef LIBANGLE_CLMEMORY_H_
10 #define LIBANGLE_CLMEMORY_H_
11
12 #include "libANGLE/CLObject.h"
13 #include "libANGLE/renderer/CLMemoryImpl.h"
14
15 #include "common/SynchronizedValue.h"
16
17 #include <atomic>
18 #include <stack>
19
20 namespace cl
21 {
22
23 class Memory : public _cl_mem, public Object
24 {
25 public:
26 // Front end entry functions, only called from OpenCL entry points
27
28 angle::Result setDestructorCallback(MemoryCB pfnNotify, void *userData);
29
30 angle::Result getInfo(MemInfo name, size_t valueSize, void *value, size_t *valueSizeRet) const;
31
32 public:
33 using PropArray = std::vector<cl_mem_properties>;
34
35 ~Memory() override;
36
37 virtual MemObjectType getType() const = 0;
38
39 const Context &getContext() const;
40 const PropArray &getProperties() const;
41 MemFlags getFlags() const;
42 void *getHostPtr() const;
43 const MemoryPtr &getParent() const;
44 size_t getOffset() const;
45 size_t getSize() const;
46
47 template <typename T = rx::CLMemoryImpl>
48 T &getImpl() const;
49
50 static Memory *Cast(cl_mem memobj);
51
52 protected:
53 using CallbackData = std::pair<MemoryCB, void *>;
54
55 Memory(const Buffer &buffer,
56 Context &context,
57 PropArray &&properties,
58 MemFlags flags,
59 size_t size,
60 void *hostPtr);
61
62 Memory(const Buffer &buffer, Buffer &parent, MemFlags flags, size_t offset, size_t size);
63
64 Memory(Context &context, PropArray &&properties, MemFlags flags, Memory *parent, void *hostPtr);
65
66 const ContextPtr mContext;
67 const PropArray mProperties;
68 const MemFlags mFlags;
69 void *const mHostPtr = nullptr;
70 const MemoryPtr mParent;
71 const size_t mOffset = 0u;
72 rx::CLMemoryImpl::Ptr mImpl;
73 size_t mSize;
74
75 angle::SynchronizedValue<std::stack<CallbackData>> mDestructorCallbacks;
76 std::atomic<cl_uint> mMapCount;
77
78 friend class Buffer;
79 friend class Context;
80 };
81
getContext()82 inline const Context &Memory::getContext() const
83 {
84 return *mContext;
85 }
86
getProperties()87 inline const Memory::PropArray &Memory::getProperties() const
88 {
89 return mProperties;
90 }
91
getFlags()92 inline MemFlags Memory::getFlags() const
93 {
94 return mFlags;
95 }
96
getHostPtr()97 inline void *Memory::getHostPtr() const
98 {
99 return mHostPtr;
100 }
101
getParent()102 inline const MemoryPtr &Memory::getParent() const
103 {
104 return mParent;
105 }
106
getOffset()107 inline size_t Memory::getOffset() const
108 {
109 return mOffset;
110 }
111
getSize()112 inline size_t Memory::getSize() const
113 {
114 return mSize;
115 }
116
117 template <typename T>
getImpl()118 inline T &Memory::getImpl() const
119 {
120 return static_cast<T &>(*mImpl);
121 }
122
Cast(cl_mem memobj)123 inline Memory *Memory::Cast(cl_mem memobj)
124 {
125 return static_cast<Memory *>(memobj);
126 }
127
128 } // namespace cl
129
130 #endif // LIBANGLE_CLMEMORY_H_
131