|
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 WEBGLELEMENTARRAYCACHE_H |
|
7 #define WEBGLELEMENTARRAYCACHE_H |
|
8 |
|
9 #include "mozilla/MemoryReporting.h" |
|
10 #include <stdint.h> |
|
11 #include "nscore.h" |
|
12 #include "GLDefs.h" |
|
13 |
|
14 namespace mozilla { |
|
15 |
|
16 template<typename T> |
|
17 struct WebGLElementArrayCacheTree; |
|
18 |
|
19 /* |
|
20 * WebGLElementArrayCache implements WebGL element array buffer validation for drawElements. |
|
21 * |
|
22 * Its exposes methods meant to be called by WebGL method implementations: |
|
23 * - Validate, to be called by WebGLContext::DrawElements, is where we use the cache |
|
24 * - BufferData and BufferSubData, to be called by eponymous WebGL methods, are how |
|
25 * data is fed into the cache |
|
26 * |
|
27 * Most of the implementation is hidden in the auxilary class template, WebGLElementArrayCacheTree. |
|
28 * Refer to its code for design comments. |
|
29 */ |
|
30 class WebGLElementArrayCache { |
|
31 |
|
32 public: |
|
33 bool BufferData(const void* ptr, size_t byteSize); |
|
34 void BufferSubData(size_t pos, const void* ptr, size_t updateByteSize); |
|
35 |
|
36 bool Validate(GLenum type, uint32_t maxAllowed, size_t first, size_t count, |
|
37 uint32_t* out_upperBound = nullptr); |
|
38 |
|
39 template<typename T> |
|
40 T Element(size_t i) const { return Elements<T>()[i]; } |
|
41 |
|
42 WebGLElementArrayCache() |
|
43 : mUntypedData(nullptr) |
|
44 , mByteSize(0) |
|
45 , mUint8Tree(nullptr) |
|
46 , mUint16Tree(nullptr) |
|
47 , mUint32Tree(nullptr) |
|
48 {} |
|
49 |
|
50 ~WebGLElementArrayCache(); |
|
51 |
|
52 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; |
|
53 |
|
54 private: |
|
55 |
|
56 template<typename T> |
|
57 bool Validate(uint32_t maxAllowed, size_t first, size_t count, |
|
58 uint32_t* out_upperBound); |
|
59 |
|
60 size_t ByteSize() const { |
|
61 return mByteSize; |
|
62 } |
|
63 |
|
64 template<typename T> |
|
65 const T* Elements() const { return static_cast<const T*>(mUntypedData); } |
|
66 template<typename T> |
|
67 T* Elements() { return static_cast<T*>(mUntypedData); } |
|
68 |
|
69 void InvalidateTrees(size_t firstByte, size_t lastByte); |
|
70 |
|
71 template<typename T> |
|
72 friend struct WebGLElementArrayCacheTree; |
|
73 template<typename T> |
|
74 friend struct TreeForType; |
|
75 |
|
76 void* mUntypedData; |
|
77 size_t mByteSize; |
|
78 WebGLElementArrayCacheTree<uint8_t>* mUint8Tree; |
|
79 WebGLElementArrayCacheTree<uint16_t>* mUint16Tree; |
|
80 WebGLElementArrayCacheTree<uint32_t>* mUint32Tree; |
|
81 }; |
|
82 |
|
83 |
|
84 } // end namespace mozilla |
|
85 |
|
86 #endif // WEBGLELEMENTARRAYCACHE_H |