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/Spinlock.h"
16 #include "common/SynchronizedValue.h"
17 
18 #include <atomic>
19 #include <stack>
20 
21 namespace cl
22 {
23 
24 class Memory : public _cl_mem, public Object
25 {
26   public:
27     // Front end entry functions, only called from OpenCL entry points
28 
29     cl_int setDestructorCallback(MemoryCB pfnNotify, void *userData);
30 
31     cl_int getInfo(MemInfo name, size_t valueSize, void *value, size_t *valueSizeRet) const;
32 
33   public:
34     using PropArray = std::vector<cl_mem_properties>;
35 
36     ~Memory() override;
37 
38     virtual MemObjectType getType() const = 0;
39 
40     const Context &getContext() const;
41     const PropArray &getProperties() const;
42     MemFlags getFlags() const;
43     void *getHostPtr() const;
44     const MemoryPtr &getParent() const;
45     size_t getOffset() const;
46     size_t getSize() const;
47 
48     template <typename T = rx::CLMemoryImpl>
49     T &getImpl() const;
50 
51     static Memory *Cast(cl_mem memobj);
52 
53   protected:
54     using CallbackData = std::pair<MemoryCB, void *>;
55 
56     Memory(const Buffer &buffer,
57            Context &context,
58            PropArray &&properties,
59            MemFlags flags,
60            size_t size,
61            void *hostPtr,
62            cl_int &errorCode);
63 
64     Memory(const Buffer &buffer,
65            Buffer &parent,
66            MemFlags flags,
67            size_t offset,
68            size_t size,
69            cl_int &errorCode);
70 
71     Memory(const Image &image,
72            Context &context,
73            PropArray &&properties,
74            MemFlags flags,
75            const cl_image_format &format,
76            const ImageDescriptor &desc,
77            Memory *parent,
78            void *hostPtr,
79            cl_int &errorCode);
80 
81     const ContextPtr mContext;
82     const PropArray mProperties;
83     const MemFlags mFlags;
84     void *const mHostPtr = nullptr;
85     const MemoryPtr mParent;
86     const size_t mOffset = 0u;
87     const rx::CLMemoryImpl::Ptr mImpl;
88     const size_t mSize;
89 
90     angle::SynchronizedValue<std::stack<CallbackData>, angle::Spinlock> mDestructorCallbacks;
91     std::atomic<cl_uint> mMapCount;
92 
93     friend class Buffer;
94     friend class Context;
95 };
96 
getContext()97 inline const Context &Memory::getContext() const
98 {
99     return *mContext;
100 }
101 
getProperties()102 inline const Memory::PropArray &Memory::getProperties() const
103 {
104     return mProperties;
105 }
106 
getFlags()107 inline MemFlags Memory::getFlags() const
108 {
109     return mFlags;
110 }
111 
getHostPtr()112 inline void *Memory::getHostPtr() const
113 {
114     return mHostPtr;
115 }
116 
getParent()117 inline const MemoryPtr &Memory::getParent() const
118 {
119     return mParent;
120 }
121 
getOffset()122 inline size_t Memory::getOffset() const
123 {
124     return mOffset;
125 }
126 
getSize()127 inline size_t Memory::getSize() const
128 {
129     return mSize;
130 }
131 
132 template <typename T>
getImpl()133 inline T &Memory::getImpl() const
134 {
135     return static_cast<T &>(*mImpl);
136 }
137 
Cast(cl_mem memobj)138 inline Memory *Memory::Cast(cl_mem memobj)
139 {
140     return static_cast<Memory *>(memobj);
141 }
142 
143 }  // namespace cl
144 
145 #endif  // LIBANGLE_CLMEMORY_H_
146