michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef WEBGLELEMENTARRAYCACHE_H michael@0: #define WEBGLELEMENTARRAYCACHE_H michael@0: michael@0: #include "mozilla/MemoryReporting.h" michael@0: #include michael@0: #include "nscore.h" michael@0: #include "GLDefs.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: template michael@0: struct WebGLElementArrayCacheTree; michael@0: michael@0: /* michael@0: * WebGLElementArrayCache implements WebGL element array buffer validation for drawElements. michael@0: * michael@0: * Its exposes methods meant to be called by WebGL method implementations: michael@0: * - Validate, to be called by WebGLContext::DrawElements, is where we use the cache michael@0: * - BufferData and BufferSubData, to be called by eponymous WebGL methods, are how michael@0: * data is fed into the cache michael@0: * michael@0: * Most of the implementation is hidden in the auxilary class template, WebGLElementArrayCacheTree. michael@0: * Refer to its code for design comments. michael@0: */ michael@0: class WebGLElementArrayCache { michael@0: michael@0: public: michael@0: bool BufferData(const void* ptr, size_t byteSize); michael@0: void BufferSubData(size_t pos, const void* ptr, size_t updateByteSize); michael@0: michael@0: bool Validate(GLenum type, uint32_t maxAllowed, size_t first, size_t count, michael@0: uint32_t* out_upperBound = nullptr); michael@0: michael@0: template michael@0: T Element(size_t i) const { return Elements()[i]; } michael@0: michael@0: WebGLElementArrayCache() michael@0: : mUntypedData(nullptr) michael@0: , mByteSize(0) michael@0: , mUint8Tree(nullptr) michael@0: , mUint16Tree(nullptr) michael@0: , mUint32Tree(nullptr) michael@0: {} michael@0: michael@0: ~WebGLElementArrayCache(); michael@0: michael@0: size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; michael@0: michael@0: private: michael@0: michael@0: template michael@0: bool Validate(uint32_t maxAllowed, size_t first, size_t count, michael@0: uint32_t* out_upperBound); michael@0: michael@0: size_t ByteSize() const { michael@0: return mByteSize; michael@0: } michael@0: michael@0: template michael@0: const T* Elements() const { return static_cast(mUntypedData); } michael@0: template michael@0: T* Elements() { return static_cast(mUntypedData); } michael@0: michael@0: void InvalidateTrees(size_t firstByte, size_t lastByte); michael@0: michael@0: template michael@0: friend struct WebGLElementArrayCacheTree; michael@0: template michael@0: friend struct TreeForType; michael@0: michael@0: void* mUntypedData; michael@0: size_t mByteSize; michael@0: WebGLElementArrayCacheTree* mUint8Tree; michael@0: WebGLElementArrayCacheTree* mUint16Tree; michael@0: WebGLElementArrayCacheTree* mUint32Tree; michael@0: }; michael@0: michael@0: michael@0: } // end namespace mozilla michael@0: michael@0: #endif // WEBGLELEMENTARRAYCACHE_H