• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2002 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 
7 // Buffer.h: Defines the gl::Buffer class, representing storage of vertex and/or
8 // index data. Implements GL buffer objects and related functionality.
9 // [OpenGL ES 2.0.24] section 2.9 page 21.
10 
11 #ifndef LIBANGLE_BUFFER_H_
12 #define LIBANGLE_BUFFER_H_
13 
14 #include "common/PackedEnums.h"
15 #include "common/angleutils.h"
16 #include "libANGLE/Debug.h"
17 #include "libANGLE/Error.h"
18 #include "libANGLE/IndexRangeCache.h"
19 #include "libANGLE/Observer.h"
20 #include "libANGLE/RefCountObject.h"
21 #include "libANGLE/angletypes.h"
22 
23 namespace rx
24 {
25 class BufferImpl;
26 class GLImplFactory;
27 }  // namespace rx
28 
29 namespace gl
30 {
31 class Buffer;
32 class Context;
33 
34 class BufferState final : angle::NonCopyable
35 {
36   public:
37     BufferState();
38     ~BufferState();
39 
getUsage()40     BufferUsage getUsage() const { return mUsage; }
getAccessFlags()41     GLbitfield getAccessFlags() const { return mAccessFlags; }
getAccess()42     GLenum getAccess() const { return mAccess; }
isMapped()43     GLboolean isMapped() const { return mMapped; }
getMapPointer()44     void *getMapPointer() const { return mMapPointer; }
getMapOffset()45     GLint64 getMapOffset() const { return mMapOffset; }
getMapLength()46     GLint64 getMapLength() const { return mMapLength; }
getSize()47     GLint64 getSize() const { return mSize; }
isBoundForTransformFeedback()48     bool isBoundForTransformFeedback() const { return mTransformFeedbackIndexedBindingCount != 0; }
getLabel()49     std::string getLabel() const { return mLabel; }
50 
51   private:
52     friend class Buffer;
53 
54     std::string mLabel;
55 
56     BufferUsage mUsage;
57     GLint64 mSize;
58     GLbitfield mAccessFlags;
59     GLenum mAccess;
60     GLboolean mMapped;
61     void *mMapPointer;
62     GLint64 mMapOffset;
63     GLint64 mMapLength;
64     int mBindingCount;
65     int mTransformFeedbackIndexedBindingCount;
66     int mTransformFeedbackGenericBindingCount;
67     GLboolean mImmutable;
68     GLbitfield mStorageExtUsageFlags;
69     GLboolean mExternal;
70 };
71 
72 // Vertex Array and Texture track buffer data updates.
73 struct ContentsObserver
74 {
75     static constexpr uint32_t kBufferTextureIndex = std::numeric_limits<uint32_t>::max();
76     uint32_t bufferIndex                          = 0;
77 
78     // VertexArray* (bufferIndex != kBufferTextureIndex) or Texture*
79     void *observer = nullptr;
80 };
81 
82 ANGLE_INLINE bool operator==(const ContentsObserver &lhs, const ContentsObserver &rhs)
83 {
84     return lhs.bufferIndex == rhs.bufferIndex && lhs.observer == rhs.observer;
85 }
86 
87 class Buffer final : public RefCountObject<BufferID>,
88                      public LabeledObject,
89                      public angle::ObserverInterface,
90                      public angle::Subject
91 {
92   public:
93     Buffer(rx::GLImplFactory *factory, BufferID id);
94     ~Buffer() override;
95     void onDestroy(const Context *context) override;
96 
97     angle::Result setLabel(const Context *context, const std::string &label) override;
98     const std::string &getLabel() const override;
99 
100     angle::Result bufferStorageExternal(Context *context,
101                                         BufferBinding target,
102                                         GLsizeiptr size,
103                                         GLeglClientBufferEXT clientBuffer,
104                                         GLbitfield flags);
105     angle::Result bufferStorage(Context *context,
106                                 BufferBinding target,
107                                 GLsizeiptr size,
108                                 const void *data,
109                                 GLbitfield flags);
110     angle::Result bufferData(Context *context,
111                              BufferBinding target,
112                              const void *data,
113                              GLsizeiptr size,
114                              BufferUsage usage);
115     angle::Result bufferSubData(const Context *context,
116                                 BufferBinding target,
117                                 const void *data,
118                                 GLsizeiptr size,
119                                 GLintptr offset);
120     angle::Result copyBufferSubData(const Context *context,
121                                     Buffer *source,
122                                     GLintptr sourceOffset,
123                                     GLintptr destOffset,
124                                     GLsizeiptr size);
125     angle::Result map(const Context *context, GLenum access);
126     angle::Result mapRange(const Context *context,
127                            GLintptr offset,
128                            GLsizeiptr length,
129                            GLbitfield access);
130     angle::Result unmap(const Context *context, GLboolean *result);
131 
132     // These are called when another operation changes Buffer data.
133     void onDataChanged();
134 
135     angle::Result getIndexRange(const gl::Context *context,
136                                 DrawElementsType type,
137                                 size_t offset,
138                                 size_t count,
139                                 bool primitiveRestartEnabled,
140                                 IndexRange *outRange) const;
getState()141     const BufferState &getState() const { return mState; }
getUsage()142     BufferUsage getUsage() const { return mState.mUsage; }
getAccessFlags()143     GLbitfield getAccessFlags() const { return mState.mAccessFlags; }
getAccess()144     GLenum getAccess() const { return mState.mAccess; }
isMapped()145     GLboolean isMapped() const { return mState.mMapped; }
isPersistentlyMapped()146     bool isPersistentlyMapped() const
147     {
148         return (mState.mStorageExtUsageFlags & GL_MAP_PERSISTENT_BIT_EXT) != 0;
149     }
getMapPointer()150     void *getMapPointer() const { return mState.mMapPointer; }
getMapOffset()151     GLint64 getMapOffset() const { return mState.mMapOffset; }
getMapLength()152     GLint64 getMapLength() const { return mState.mMapLength; }
getSize()153     GLint64 getSize() const { return mState.mSize; }
154     GLint64 getMemorySize() const;
isImmutable()155     GLboolean isImmutable() const { return mState.mImmutable; }
getStorageExtUsageFlags()156     GLbitfield getStorageExtUsageFlags() const { return mState.mStorageExtUsageFlags; }
157 
158     // Buffers are always initialized immediately when allocated
initState()159     InitState initState() const { return InitState::Initialized; }
160 
getImplementation()161     rx::BufferImpl *getImplementation() const { return mImpl; }
162 
163     // Note: we pass "isWebGL" to this function to clarify it's only valid if WebGL is enabled.
164     // We pass the boolean flag instead of the pointer because this header can't read Context.h.
hasWebGLXFBBindingConflict(bool isWebGL)165     ANGLE_INLINE bool hasWebGLXFBBindingConflict(bool isWebGL) const
166     {
167         if (!isWebGL)
168         {
169             return false;
170         }
171 
172         // The transform feedback generic binding point is not an indexed binding point but it also
173         // does not count as a non-transform-feedback use of the buffer, so we subtract it from the
174         // binding count when checking if the buffer is bound to a non-transform-feedback location.
175         // See https://crbug.com/853978
176         return mState.mTransformFeedbackIndexedBindingCount > 0 &&
177                mState.mTransformFeedbackIndexedBindingCount !=
178                    mState.mBindingCount - mState.mTransformFeedbackGenericBindingCount;
179     }
180 
181     bool isDoubleBoundForTransformFeedback() const;
182     void onTFBindingChanged(const Context *context, bool bound, bool indexed);
onNonTFBindingChanged(int incr)183     void onNonTFBindingChanged(int incr) { mState.mBindingCount += incr; }
184     angle::Result getSubData(const gl::Context *context,
185                              GLintptr offset,
186                              GLsizeiptr size,
187                              void *outData);
188 
189     // angle::ObserverInterface implementation.
190     void onSubjectStateChange(angle::SubjectIndex index, angle::SubjectMessage message) override;
191 
192     void addContentsObserver(VertexArray *vertexArray, uint32_t bufferIndex);
193     void removeContentsObserver(VertexArray *vertexArray, uint32_t bufferIndex);
194     void addContentsObserver(Texture *texture);
195     void removeContentsObserver(Texture *texture);
196     bool hasContentsObserver(Texture *texture) const;
197 
198   private:
199     angle::Result bufferDataImpl(Context *context,
200                                  BufferBinding target,
201                                  const void *data,
202                                  GLsizeiptr size,
203                                  BufferUsage usage,
204                                  GLbitfield flags);
205     angle::Result bufferExternalDataImpl(Context *context,
206                                          BufferBinding target,
207                                          GLeglClientBufferEXT clientBuffer,
208                                          GLsizeiptr size,
209                                          GLbitfield flags);
210 
211     void onContentsChange();
212     size_t getContentsObserverIndex(void *observer, uint32_t bufferIndex) const;
213     void removeContentsObserverImpl(void *observer, uint32_t bufferIndex);
214 
215     BufferState mState;
216     rx::BufferImpl *mImpl;
217     angle::ObserverBinding mImplObserver;
218 
219     angle::FastVector<ContentsObserver, angle::kMaxFixedObservers> mContentsObservers;
220     mutable IndexRangeCache mIndexRangeCache;
221 };
222 
223 }  // namespace gl
224 
225 #endif  // LIBANGLE_BUFFER_H_
226