Backed out changeset 58dbd2146e24 (bug 944961) for bustage.
[gecko.git] / content / canvas / src / WebGLElementArrayCache.h
blobe6ee10ea738cc2dcba40022fdd367826ff14f717
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 <stdint.h>
11 #include "nscore.h"
12 #include "GLDefs.h"
14 namespace mozilla {
16 template<typename T>
17 struct WebGLElementArrayCacheTree;
20 * WebGLElementArrayCache implements WebGL element array buffer validation for drawElements.
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
27 * Most of the implementation is hidden in the auxilary class template, WebGLElementArrayCacheTree.
28 * Refer to its code for design comments.
30 class WebGLElementArrayCache {
32 public:
33 bool BufferData(const void* ptr, size_t byteSize);
34 void BufferSubData(size_t pos, const void* ptr, size_t updateByteSize);
36 bool Validate(GLenum type, uint32_t maxAllowed, size_t first, size_t count);
38 template<typename T>
39 T Element(size_t i) const { return Elements<T>()[i]; }
41 WebGLElementArrayCache()
42 : mUntypedData(nullptr)
43 , mByteSize(0)
44 , mUint8Tree(nullptr)
45 , mUint16Tree(nullptr)
46 , mUint32Tree(nullptr)
49 ~WebGLElementArrayCache();
51 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
53 private:
55 template<typename T>
56 bool Validate(uint32_t maxAllowed, size_t first, size_t count);
58 size_t ByteSize() const {
59 return mByteSize;
62 template<typename T>
63 const T* Elements() const { return static_cast<const T*>(mUntypedData); }
64 template<typename T>
65 T* Elements() { return static_cast<T*>(mUntypedData); }
67 void InvalidateTrees(size_t firstByte, size_t lastByte);
69 template<typename T>
70 friend struct WebGLElementArrayCacheTree;
71 template<typename T>
72 friend struct TreeForType;
74 void* mUntypedData;
75 size_t mByteSize;
76 WebGLElementArrayCacheTree<uint8_t>* mUint8Tree;
77 WebGLElementArrayCacheTree<uint16_t>* mUint16Tree;
78 WebGLElementArrayCacheTree<uint32_t>* mUint32Tree;
82 } // end namespace mozilla
84 #endif // WEBGLELEMENTARRAYCACHE_H