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(const Image &image,
65 Context &context,
66 PropArray &&properties,
67 MemFlags flags,
68 const cl_image_format &format,
69 const ImageDescriptor &desc,
70 Memory *parent,
71 void *hostPtr);
72
73 const ContextPtr mContext;
74 const PropArray mProperties;
75 const MemFlags mFlags;
76 void *const mHostPtr = nullptr;
77 const MemoryPtr mParent;
78 const size_t mOffset = 0u;
79 rx::CLMemoryImpl::Ptr mImpl;
80 size_t mSize;
81
82 angle::SynchronizedValue<std::stack<CallbackData>> mDestructorCallbacks;
83 std::atomic<cl_uint> mMapCount;
84
85 friend class Buffer;
86 friend class Context;
87 };
88
getContext()89 inline const Context &Memory::getContext() const
90 {
91 return *mContext;
92 }
93
getProperties()94 inline const Memory::PropArray &Memory::getProperties() const
95 {
96 return mProperties;
97 }
98
getFlags()99 inline MemFlags Memory::getFlags() const
100 {
101 return mFlags;
102 }
103
getHostPtr()104 inline void *Memory::getHostPtr() const
105 {
106 return mHostPtr;
107 }
108
getParent()109 inline const MemoryPtr &Memory::getParent() const
110 {
111 return mParent;
112 }
113
getOffset()114 inline size_t Memory::getOffset() const
115 {
116 return mOffset;
117 }
118
getSize()119 inline size_t Memory::getSize() const
120 {
121 return mSize;
122 }
123
124 template <typename T>
getImpl()125 inline T &Memory::getImpl() const
126 {
127 return static_cast<T &>(*mImpl);
128 }
129
Cast(cl_mem memobj)130 inline Memory *Memory::Cast(cl_mem memobj)
131 {
132 return static_cast<Memory *>(memobj);
133 }
134
135 } // namespace cl
136
137 #endif // LIBANGLE_CLMEMORY_H_
138