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/. */
6 #ifndef WEBGLELEMENTARRAYCACHE_H
7 #define WEBGLELEMENTARRAYCACHE_H
9 #include "mozilla/MemoryReporting.h"
10 #include "mozilla/Scoped.h"
19 struct WebGLElementArrayCacheTree
;
22 * WebGLElementArrayCache implements WebGL element array buffer validation for drawElements.
24 * Its exposes methods meant to be called by WebGL method implementations:
25 * - Validate, to be called by WebGLContext::DrawElements, is where we use the cache
26 * - BufferData and BufferSubData, to be called by eponymous WebGL methods, are how
27 * data is fed into the cache
29 * Most of the implementation is hidden in the auxilary class template, WebGLElementArrayCacheTree.
30 * Refer to its code for design comments.
32 class WebGLElementArrayCache
{
35 bool BufferData(const void* ptr
, size_t byteLength
);
36 bool BufferSubData(size_t pos
, const void* ptr
, size_t updateByteSize
);
38 bool Validate(GLenum type
, uint32_t maxAllowed
, size_t first
, size_t count
,
39 uint32_t* out_upperBound
);
42 T
Element(size_t i
) const { return Elements
<T
>()[i
]; }
44 WebGLElementArrayCache();
46 ~WebGLElementArrayCache();
48 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf
) const;
50 bool BeenUsedWithMultipleTypes() const;
55 * Returns true if a drawElements call with the given parameters should succeed,
58 * In other words, this returns true if all entries in the element array at positions
60 * first .. first+count-1
62 * are less than or equal to maxAllowed.
65 * maxAllowed: maximum value to be allowed in the specificied portion of the element array.
66 * first: start of the portion of the element array to consume.
67 * count: number of entries from the element array to consume.
70 * out_upperBound: upon success, is set to the actual maximum value in the specified range,
71 * which is then guaranteed to be less than or equal to maxAllowed.
72 * upon failure, is set to the first value in the specified range, that
73 * is greater than maxAllowed.
76 bool Validate(uint32_t maxAllowed
, size_t first
, size_t count
,
77 uint32_t* out_upperBound
);
80 const T
* Elements() const { return reinterpret_cast<const T
*>(mBytes
.Elements()); }
82 T
* Elements() { return reinterpret_cast<T
*>(mBytes
.Elements()); }
84 bool UpdateTrees(size_t firstByte
, size_t lastByte
);
87 friend struct WebGLElementArrayCacheTree
;
89 friend struct TreeForType
;
91 FallibleTArray
<uint8_t> mBytes
;
92 ScopedDeletePtr
<WebGLElementArrayCacheTree
<uint8_t>> mUint8Tree
;
93 ScopedDeletePtr
<WebGLElementArrayCacheTree
<uint16_t>> mUint16Tree
;
94 ScopedDeletePtr
<WebGLElementArrayCacheTree
<uint32_t>> mUint32Tree
;
98 } // end namespace mozilla
100 #endif // WEBGLELEMENTARRAYCACHE_H