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