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 WEBGL_ELEMENT_ARRAY_CACHE_H
7 #define WEBGL_ELEMENT_ARRAY_CACHE_H
10 #include "mozilla/MemoryReporting.h"
11 #include "mozilla/Scoped.h"
19 struct WebGLElementArrayCacheTree
;
21 /* WebGLElementArrayCache implements WebGL element array buffer validation for
24 * Its exposes methods meant to be called by WebGL method implementations:
26 * - Validate, to be called by WebGLContext::DrawElements, is where we use the
29 * - BufferData and BufferSubData, to be called by eponymous WebGL methods, are
30 * how data is fed into the cache.
32 * Most of the implementation is hidden in the auxilary class template,
33 * WebGLElementArrayCacheTree. Refer to its code for design comments.
35 class WebGLElementArrayCache
{
37 bool BufferData(const void* ptr
, size_t byteLength
);
38 bool BufferSubData(size_t pos
, const void* ptr
, size_t updateByteSize
);
40 bool Validate(GLenum type
, uint32_t maxAllowed
, size_t first
, size_t count
,
41 uint32_t* const out_upperBound
);
44 T
Element(size_t i
) const { return Elements
<T
>()[i
]; }
46 WebGLElementArrayCache();
47 ~WebGLElementArrayCache();
49 size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf
) const;
50 bool BeenUsedWithMultipleTypes() const;
53 /* Returns true if a drawElements call with the given parameters should
54 * succeed, false otherwise.
56 * In other words, this returns true if all entries in the element array at
59 * first .. first+count-1
61 * are less than or equal to maxAllowed.
64 * maxAllowed: Maximum value to be allowed in the specificied portion of
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
71 * specified range, which is then guaranteed to be less
72 * than or equal to maxAllowed. upon failure, is set to
73 * the first value in the specified range, that is greater
77 bool Validate(uint32_t maxAllowed
, size_t first
, size_t count
,
78 uint32_t* const out_upperBound
);
81 const T
* Elements() const {
82 return reinterpret_cast<const T
*>(mBytes
.Elements());
86 T
* Elements() { return reinterpret_cast<T
*>(mBytes
.Elements()); }
88 bool UpdateTrees(size_t firstByte
, size_t lastByte
);
91 friend struct WebGLElementArrayCacheTree
;
93 friend struct TreeForType
;
95 FallibleTArray
<uint8_t> mBytes
;
96 ScopedDeletePtr
<WebGLElementArrayCacheTree
<uint8_t>> mUint8Tree
;
97 ScopedDeletePtr
<WebGLElementArrayCacheTree
<uint16_t>> mUint16Tree
;
98 ScopedDeletePtr
<WebGLElementArrayCacheTree
<uint32_t>> mUint32Tree
;
101 } // end namespace mozilla
103 #endif // WEBGL_ELEMENT_ARRAY_CACHE_H