|
1 /* -*- Mode: C++; tab-width: 20; 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/. */ |
|
5 |
|
6 #include "WebGLBuffer.h" |
|
7 |
|
8 #include "GLContext.h" |
|
9 #include "mozilla/dom/WebGLRenderingContextBinding.h" |
|
10 #include "WebGLContext.h" |
|
11 #include "WebGLElementArrayCache.h" |
|
12 |
|
13 using namespace mozilla; |
|
14 |
|
15 WebGLBuffer::WebGLBuffer(WebGLContext *context) |
|
16 : WebGLContextBoundObject(context) |
|
17 , mHasEverBeenBound(false) |
|
18 , mByteLength(0) |
|
19 , mTarget(LOCAL_GL_NONE) |
|
20 { |
|
21 SetIsDOMBinding(); |
|
22 mContext->MakeContextCurrent(); |
|
23 mContext->gl->fGenBuffers(1, &mGLName); |
|
24 mContext->mBuffers.insertBack(this); |
|
25 } |
|
26 |
|
27 WebGLBuffer::~WebGLBuffer() { |
|
28 DeleteOnce(); |
|
29 } |
|
30 |
|
31 void |
|
32 WebGLBuffer::Delete() { |
|
33 mContext->MakeContextCurrent(); |
|
34 mContext->gl->fDeleteBuffers(1, &mGLName); |
|
35 mByteLength = 0; |
|
36 mCache = nullptr; |
|
37 LinkedListElement<WebGLBuffer>::remove(); // remove from mContext->mBuffers |
|
38 } |
|
39 |
|
40 void |
|
41 WebGLBuffer::SetTarget(GLenum target) { |
|
42 mTarget = target; |
|
43 if (!mCache && mTarget == LOCAL_GL_ELEMENT_ARRAY_BUFFER) |
|
44 mCache = new WebGLElementArrayCache; |
|
45 } |
|
46 |
|
47 bool |
|
48 WebGLBuffer::ElementArrayCacheBufferData(const void* ptr, size_t buffer_size_in_bytes) { |
|
49 if (mTarget == LOCAL_GL_ELEMENT_ARRAY_BUFFER) |
|
50 return mCache->BufferData(ptr, buffer_size_in_bytes); |
|
51 return true; |
|
52 } |
|
53 |
|
54 void |
|
55 WebGLBuffer::ElementArrayCacheBufferSubData(size_t pos, const void* ptr, size_t update_size_in_bytes) { |
|
56 if (mTarget == LOCAL_GL_ELEMENT_ARRAY_BUFFER) |
|
57 mCache->BufferSubData(pos, ptr, update_size_in_bytes); |
|
58 } |
|
59 |
|
60 size_t |
|
61 WebGLBuffer::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const |
|
62 { |
|
63 size_t sizeOfCache = mCache ? mCache->SizeOfIncludingThis(aMallocSizeOf) : 0; |
|
64 return aMallocSizeOf(this) + sizeOfCache; |
|
65 } |
|
66 |
|
67 bool |
|
68 WebGLBuffer::Validate(GLenum type, uint32_t max_allowed, |
|
69 size_t first, size_t count, |
|
70 uint32_t* out_upperBound) |
|
71 { |
|
72 return mCache->Validate(type, max_allowed, first, count, out_upperBound); |
|
73 } |
|
74 |
|
75 |
|
76 JSObject* |
|
77 WebGLBuffer::WrapObject(JSContext *cx) { |
|
78 return dom::WebGLBufferBinding::Wrap(cx, this); |
|
79 } |
|
80 |
|
81 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(WebGLBuffer) |
|
82 |
|
83 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(WebGLBuffer, AddRef) |
|
84 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(WebGLBuffer, Release) |