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 // CLBuffer.h: Defines the cl::Buffer class, which is a collection of elements.
7 
8 #ifndef LIBANGLE_CLBUFFER_H_
9 #define LIBANGLE_CLBUFFER_H_
10 
11 #include "libANGLE/CLMemory.h"
12 
13 namespace cl
14 {
15 
16 class Buffer final : public Memory
17 {
18   public:
19     // Front end entry functions, only called from OpenCL entry points
20 
21     static bool IsValid(const _cl_mem *buffer);
22 
23     cl_mem createSubBuffer(MemFlags flags,
24                            cl_buffer_create_type createType,
25                            const void *createInfo,
26                            cl_int &errorCode);
27 
28     bool isRegionValid(size_t offset, size_t size) const;
29     bool isRegionValid(const cl_buffer_region &region) const;
30 
31   public:
32     ~Buffer() override;
33 
34     MemObjectType getType() const final;
35 
36     bool isSubBuffer() const;
37 
38   private:
39     Buffer(Context &context,
40            PropArray &&properties,
41            MemFlags flags,
42            size_t size,
43            void *hostPtr,
44            cl_int &errorCode);
45 
46     Buffer(Buffer &parent, MemFlags flags, size_t offset, size_t size, cl_int &errorCode);
47 
48     friend class Object;
49 };
50 
IsValid(const _cl_mem * buffer)51 inline bool Buffer::IsValid(const _cl_mem *buffer)
52 {
53     return Memory::IsValid(buffer) && buffer->cast<Memory>().getType() == MemObjectType::Buffer;
54 }
55 
isRegionValid(size_t offset,size_t size)56 inline bool Buffer::isRegionValid(size_t offset, size_t size) const
57 {
58     return offset < mSize && offset + size <= mSize;
59 }
60 
isRegionValid(const cl_buffer_region & region)61 inline bool Buffer::isRegionValid(const cl_buffer_region &region) const
62 {
63     return region.origin < mSize && region.origin + region.size <= mSize;
64 }
65 
getType()66 inline MemObjectType Buffer::getType() const
67 {
68     return MemObjectType::Buffer;
69 }
70 
isSubBuffer()71 inline bool Buffer::isSubBuffer() const
72 {
73     return mParent != nullptr;
74 }
75 
76 }  // namespace cl
77 
78 #endif  // LIBANGLE_CLBUFFER_H_
79