Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef WEBGL_BUFFER_H_
7 : #define WEBGL_BUFFER_H_
8 :
9 : #include <map>
10 :
11 : #include "GLDefs.h"
12 : #include "mozilla/LinkedList.h"
13 : #include "nsWrapperCache.h"
14 : #include "WebGLObjectModel.h"
15 : #include "WebGLTypes.h"
16 :
17 : namespace mozilla {
18 :
19 : class WebGLBuffer final
20 : : public nsWrapperCache
21 : , public WebGLRefCountedObject<WebGLBuffer>
22 : , public LinkedListElement<WebGLBuffer>
23 : {
24 : friend class WebGLContext;
25 : friend class WebGL2Context;
26 : friend class WebGLTexture;
27 :
28 : public:
29 : enum class Kind {
30 : Undefined,
31 : ElementArray,
32 : OtherData
33 : };
34 :
35 : WebGLBuffer(WebGLContext* webgl, GLuint buf);
36 :
37 : void SetContentAfterBind(GLenum target);
38 0 : Kind Content() const { return mContent; }
39 :
40 : void Delete();
41 :
42 : size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
43 :
44 0 : GLenum Usage() const { return mUsage; }
45 0 : size_t ByteLength() const { return mByteLength; }
46 :
47 : bool ValidateIndexedFetch(GLenum type, uint32_t max_allowed, size_t first, size_t count) const;
48 : bool ValidateRange(const char* funcName, size_t byteOffset, size_t byteLen) const;
49 :
50 0 : WebGLContext* GetParentObject() const {
51 0 : return mContext;
52 : }
53 :
54 : virtual JSObject* WrapObject(JSContext* cx, JS::Handle<JSObject*> givenProto) override;
55 :
56 : bool ValidateCanBindToTarget(const char* funcName, GLenum target);
57 : void BufferData(GLenum target, size_t size, const void* data, GLenum usage);
58 : void BufferSubData(GLenum target, size_t dstByteOffset, size_t dataLen,
59 : const void* data) const;
60 :
61 : ////
62 :
63 0 : static void AddBindCount(GLenum target, WebGLBuffer* buffer, int8_t addVal) {
64 0 : if (!buffer)
65 0 : return;
66 :
67 0 : if (target == LOCAL_GL_TRANSFORM_FEEDBACK_BUFFER) {
68 0 : MOZ_ASSERT_IF(addVal < 0, buffer->mTFBindCount >= size_t(-addVal));
69 0 : buffer->mTFBindCount += addVal;
70 : } else {
71 0 : MOZ_ASSERT_IF(addVal < 0, buffer->mNonTFBindCount >= size_t(-addVal));
72 0 : buffer->mNonTFBindCount += addVal;
73 : }
74 : }
75 :
76 0 : static void SetSlot(GLenum target, WebGLBuffer* newBuffer,
77 : WebGLRefPtr<WebGLBuffer>* const out_slot)
78 : {
79 0 : WebGLBuffer* const oldBuffer = *out_slot;
80 0 : AddBindCount(target, oldBuffer, -1);
81 0 : AddBindCount(target, newBuffer, +1);
82 0 : *out_slot = newBuffer;
83 0 : }
84 :
85 0 : bool IsBoundForTF() const { return bool(mTFBindCount); }
86 0 : bool IsBoundForNonTF() const { return bool(mNonTFBindCount); }
87 :
88 : ////
89 :
90 : const GLenum mGLName;
91 :
92 0 : NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(WebGLBuffer)
93 0 : NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(WebGLBuffer)
94 :
95 : protected:
96 : ~WebGLBuffer();
97 :
98 : void InvalidateCacheRange(size_t offset, size_t length) const;
99 :
100 : Kind mContent;
101 : GLenum mUsage;
102 : size_t mByteLength;
103 : size_t mTFBindCount;
104 : size_t mNonTFBindCount;
105 :
106 : struct IndexRange final {
107 : GLenum type;
108 : size_t first;
109 : size_t count;
110 :
111 0 : bool operator<(const IndexRange& x) const {
112 0 : if (type != x.type)
113 0 : return type < x.type;
114 :
115 0 : if (first != x.first)
116 0 : return first < x.first;
117 :
118 0 : return count < x.count;
119 : }
120 : };
121 :
122 : UniqueBuffer mIndexCache;
123 : mutable std::map<IndexRange, size_t> mIndexRanges;
124 : };
125 :
126 : } // namespace mozilla
127 :
128 : #endif // WEBGL_BUFFER_H_
|