/src/mozilla-central/dom/canvas/WebGLBuffer.h
Line | Count | Source (jump to first uncovered line) |
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 "CacheMap.h" |
12 | | #include "GLDefs.h" |
13 | | #include "mozilla/LinkedList.h" |
14 | | #include "nsWrapperCache.h" |
15 | | #include "WebGLObjectModel.h" |
16 | | #include "WebGLTypes.h" |
17 | | |
18 | | namespace mozilla { |
19 | | |
20 | | class WebGLBuffer final |
21 | | : public nsWrapperCache |
22 | | , public WebGLRefCountedObject<WebGLBuffer> |
23 | | , public LinkedListElement<WebGLBuffer> |
24 | | { |
25 | | friend class WebGLContext; |
26 | | friend class WebGL2Context; |
27 | | friend class WebGLTexture; |
28 | | |
29 | | public: |
30 | | enum class Kind { |
31 | | Undefined, |
32 | | ElementArray, |
33 | | OtherData |
34 | | }; |
35 | | |
36 | | WebGLBuffer(WebGLContext* webgl, GLuint buf); |
37 | | |
38 | | void SetContentAfterBind(GLenum target); |
39 | 0 | Kind Content() const { return mContent; } |
40 | | |
41 | | void Delete(); |
42 | | |
43 | | size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
44 | | |
45 | 0 | GLenum Usage() const { return mUsage; } |
46 | 0 | size_t ByteLength() const { return mByteLength; } |
47 | | |
48 | | Maybe<uint32_t> GetIndexedFetchMaxVert(GLenum type, uint64_t byteOffset, |
49 | | uint32_t indexCount) const; |
50 | | bool ValidateRange(size_t byteOffset, size_t byteLen) const; |
51 | | |
52 | 0 | WebGLContext* GetParentObject() const { |
53 | 0 | return mContext; |
54 | 0 | } |
55 | | |
56 | | virtual JSObject* WrapObject(JSContext* cx, JS::Handle<JSObject*> givenProto) override; |
57 | | |
58 | | bool ValidateCanBindToTarget(GLenum target); |
59 | | void BufferData(GLenum target, size_t size, const void* data, GLenum usage); |
60 | | void BufferSubData(GLenum target, size_t dstByteOffset, size_t dataLen, |
61 | | const void* data) const; |
62 | | |
63 | | //// |
64 | | |
65 | 0 | static void AddBindCount(GLenum target, WebGLBuffer* buffer, int8_t addVal) { |
66 | 0 | if (!buffer) |
67 | 0 | return; |
68 | 0 | |
69 | 0 | if (target == LOCAL_GL_TRANSFORM_FEEDBACK_BUFFER) { |
70 | 0 | MOZ_ASSERT_IF(addVal < 0, buffer->mTFBindCount >= size_t(-addVal)); |
71 | 0 | buffer->mTFBindCount += addVal; |
72 | 0 | buffer->mFetchInvalidator.InvalidateCaches(); |
73 | 0 | } else { |
74 | 0 | MOZ_ASSERT_IF(addVal < 0, buffer->mNonTFBindCount >= size_t(-addVal)); |
75 | 0 | buffer->mNonTFBindCount += addVal; |
76 | 0 | } |
77 | 0 | } |
78 | | |
79 | | static void SetSlot(GLenum target, WebGLBuffer* newBuffer, |
80 | | WebGLRefPtr<WebGLBuffer>* const out_slot) |
81 | 0 | { |
82 | 0 | WebGLBuffer* const oldBuffer = *out_slot; |
83 | 0 | AddBindCount(target, oldBuffer, -1); |
84 | 0 | AddBindCount(target, newBuffer, +1); |
85 | 0 | *out_slot = newBuffer; |
86 | 0 | } |
87 | | |
88 | 0 | bool IsBoundForTF() const { return bool(mTFBindCount); } |
89 | 0 | bool IsBoundForNonTF() const { return bool(mNonTFBindCount); } |
90 | | |
91 | | //// |
92 | | |
93 | | const GLenum mGLName; |
94 | | |
95 | | NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(WebGLBuffer) |
96 | | NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(WebGLBuffer) |
97 | | |
98 | | protected: |
99 | | ~WebGLBuffer(); |
100 | | |
101 | | void InvalidateCacheRange(uint64_t byteOffset, uint64_t byteLength) const; |
102 | | |
103 | | Kind mContent; |
104 | | GLenum mUsage; |
105 | | size_t mByteLength; |
106 | | size_t mTFBindCount; |
107 | | size_t mNonTFBindCount; |
108 | | mutable uint64_t mLastUpdateFenceId = 0; |
109 | | |
110 | | struct IndexRange final { |
111 | | GLenum type; |
112 | | uint64_t byteOffset; |
113 | | uint32_t indexCount; |
114 | | |
115 | 0 | bool operator<(const IndexRange& x) const { |
116 | 0 | if (type != x.type) |
117 | 0 | return type < x.type; |
118 | 0 | |
119 | 0 | if (byteOffset != x.byteOffset) |
120 | 0 | return byteOffset < x.byteOffset; |
121 | 0 | |
122 | 0 | return indexCount < x.indexCount; |
123 | 0 | } |
124 | | }; |
125 | | |
126 | | UniqueBuffer mIndexCache; |
127 | | mutable std::map<IndexRange, Maybe<uint32_t>> mIndexRanges; |
128 | | |
129 | | public: |
130 | | CacheMapInvalidator mFetchInvalidator; |
131 | | |
132 | | void ResetLastUpdateFenceId() const; |
133 | | }; |
134 | | |
135 | | } // namespace mozilla |
136 | | |
137 | | #endif // WEBGL_BUFFER_H_ |