michael@0: /* -*- Mode: C++; tab-width: 20; 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: #include "WebGLBuffer.h" michael@0: michael@0: #include "GLContext.h" michael@0: #include "mozilla/dom/WebGLRenderingContextBinding.h" michael@0: #include "WebGLContext.h" michael@0: #include "WebGLElementArrayCache.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: WebGLBuffer::WebGLBuffer(WebGLContext *context) michael@0: : WebGLContextBoundObject(context) michael@0: , mHasEverBeenBound(false) michael@0: , mByteLength(0) michael@0: , mTarget(LOCAL_GL_NONE) michael@0: { michael@0: SetIsDOMBinding(); michael@0: mContext->MakeContextCurrent(); michael@0: mContext->gl->fGenBuffers(1, &mGLName); michael@0: mContext->mBuffers.insertBack(this); michael@0: } michael@0: michael@0: WebGLBuffer::~WebGLBuffer() { michael@0: DeleteOnce(); michael@0: } michael@0: michael@0: void michael@0: WebGLBuffer::Delete() { michael@0: mContext->MakeContextCurrent(); michael@0: mContext->gl->fDeleteBuffers(1, &mGLName); michael@0: mByteLength = 0; michael@0: mCache = nullptr; michael@0: LinkedListElement::remove(); // remove from mContext->mBuffers michael@0: } michael@0: michael@0: void michael@0: WebGLBuffer::SetTarget(GLenum target) { michael@0: mTarget = target; michael@0: if (!mCache && mTarget == LOCAL_GL_ELEMENT_ARRAY_BUFFER) michael@0: mCache = new WebGLElementArrayCache; michael@0: } michael@0: michael@0: bool michael@0: WebGLBuffer::ElementArrayCacheBufferData(const void* ptr, size_t buffer_size_in_bytes) { michael@0: if (mTarget == LOCAL_GL_ELEMENT_ARRAY_BUFFER) michael@0: return mCache->BufferData(ptr, buffer_size_in_bytes); michael@0: return true; michael@0: } michael@0: michael@0: void michael@0: WebGLBuffer::ElementArrayCacheBufferSubData(size_t pos, const void* ptr, size_t update_size_in_bytes) { michael@0: if (mTarget == LOCAL_GL_ELEMENT_ARRAY_BUFFER) michael@0: mCache->BufferSubData(pos, ptr, update_size_in_bytes); michael@0: } michael@0: michael@0: size_t michael@0: WebGLBuffer::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const michael@0: { michael@0: size_t sizeOfCache = mCache ? mCache->SizeOfIncludingThis(aMallocSizeOf) : 0; michael@0: return aMallocSizeOf(this) + sizeOfCache; michael@0: } michael@0: michael@0: bool michael@0: WebGLBuffer::Validate(GLenum type, uint32_t max_allowed, michael@0: size_t first, size_t count, michael@0: uint32_t* out_upperBound) michael@0: { michael@0: return mCache->Validate(type, max_allowed, first, count, out_upperBound); michael@0: } michael@0: michael@0: michael@0: JSObject* michael@0: WebGLBuffer::WrapObject(JSContext *cx) { michael@0: return dom::WebGLBufferBinding::Wrap(cx, this); michael@0: } michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(WebGLBuffer) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(WebGLBuffer, AddRef) michael@0: NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(WebGLBuffer, Release)