/* -*- 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 "mozilla/Scoped.h" #include "nsTArray.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 byteLength); bool 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); template T Element(size_t i) const { return Elements()[i]; } WebGLElementArrayCache(); ~WebGLElementArrayCache(); size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; bool BeenUsedWithMultipleTypes() const; private: /* * Returns true if a drawElements call with the given parameters should succeed, * false otherwise. * * In other words, this returns true if all entries in the element array at positions * * first .. first+count-1 * * are less than or equal to maxAllowed. * * Input parameters: * maxAllowed: maximum value to be allowed in the specificied portion of the element array. * first: start of the portion of the element array to consume. * count: number of entries from the element array to consume. * * Output parameter: * out_upperBound: upon success, is set to the actual maximum value in the specified range, * which is then guaranteed to be less than or equal to maxAllowed. * upon failure, is set to the first value in the specified range, that * is greater than maxAllowed. */ template bool Validate(uint32_t maxAllowed, size_t first, size_t count, uint32_t* out_upperBound); template const T* Elements() const { return reinterpret_cast(mBytes.Elements()); } template T* Elements() { return reinterpret_cast(mBytes.Elements()); } bool UpdateTrees(size_t firstByte, size_t lastByte); template friend struct WebGLElementArrayCacheTree; template friend struct TreeForType; FallibleTArray mBytes; ScopedDeletePtr> mUint8Tree; ScopedDeletePtr> mUint16Tree; ScopedDeletePtr> mUint32Tree; }; } // end namespace mozilla #endif // WEBGLELEMENTARRAYCACHE_H