2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "core/html/canvas/WebGLVertexArrayObjectOES.h"
30 #include "core/html/canvas/WebGLRenderingContextBase.h"
34 PassRefPtrWillBeRawPtr<WebGLVertexArrayObjectOES> WebGLVertexArrayObjectOES::create(WebGLRenderingContextBase* ctx, VaoType type)
36 return adoptRefWillBeNoop(new WebGLVertexArrayObjectOES(ctx, type));
39 WebGLVertexArrayObjectOES::WebGLVertexArrayObjectOES(WebGLRenderingContextBase* ctx, VaoType type)
40 : WebGLContextObject(ctx)
42 , m_hasEverBeenBound(false)
43 , m_boundElementArrayBuffer(nullptr)
45 ScriptWrappable::init(this);
46 m_vertexAttribState.resize(ctx->maxVertexAttribs());
52 setObject(context()->webContext()->createVertexArrayOES());
57 WebGLVertexArrayObjectOES::~WebGLVertexArrayObjectOES()
60 // These heap objects must not be accessed by deleteObjectImpl(),
61 // clear them out before a call to it is triggered below. The
62 // finalizers of these two (and their elements) will themselves
64 m_boundElementArrayBuffer.clear();
65 m_vertexAttribState.clear();
67 // Delete the platform framebuffer resource. Explicit detachment
68 // is for the benefit of Oilpan, where this vertex array object
69 // isn't detached when it and the WebGLRenderingContextBase object
70 // it is registered with are both finalized. Without Oilpan, the
71 // object will have been detached.
73 // To keep the code regular, the trivial detach()ment is always
75 detachAndDeleteObject();
78 void WebGLVertexArrayObjectOES::deleteObjectImpl(blink::WebGraphicsContext3D* context3d, Platform3DObject object)
84 context3d->deleteVertexArrayOES(object);
88 if (m_boundElementArrayBuffer)
89 m_boundElementArrayBuffer->onDetached(context3d);
91 for (size_t i = 0; i < m_vertexAttribState.size(); ++i) {
92 VertexAttribState& state = m_vertexAttribState[i];
93 if (state.bufferBinding)
94 state.bufferBinding->onDetached(context3d);
98 void WebGLVertexArrayObjectOES::setElementArrayBuffer(PassRefPtrWillBeRawPtr<WebGLBuffer> buffer)
101 buffer->onAttached();
102 if (m_boundElementArrayBuffer)
103 m_boundElementArrayBuffer->onDetached(context()->webContext());
104 m_boundElementArrayBuffer = buffer;
107 void WebGLVertexArrayObjectOES::setVertexAttribState(
108 GLuint index, GLsizei bytesPerElement, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLintptr offset, PassRefPtrWillBeRawPtr<WebGLBuffer> buffer)
110 GLsizei validatedStride = stride ? stride : bytesPerElement;
112 VertexAttribState& state = m_vertexAttribState[index];
115 buffer->onAttached();
116 if (state.bufferBinding)
117 state.bufferBinding->onDetached(context()->webContext());
119 state.bufferBinding = buffer;
120 state.bytesPerElement = bytesPerElement;
123 state.normalized = normalized;
124 state.stride = validatedStride;
125 state.originalStride = stride;
126 state.offset = offset;
129 void WebGLVertexArrayObjectOES::unbindBuffer(PassRefPtrWillBeRawPtr<WebGLBuffer> buffer)
131 if (m_boundElementArrayBuffer == buffer) {
132 m_boundElementArrayBuffer->onDetached(context()->webContext());
133 m_boundElementArrayBuffer = nullptr;
136 for (size_t i = 0; i < m_vertexAttribState.size(); ++i) {
137 VertexAttribState& state = m_vertexAttribState[i];
138 if (state.bufferBinding == buffer) {
139 buffer->onDetached(context()->webContext());
140 state.bufferBinding = nullptr;
145 void WebGLVertexArrayObjectOES::setVertexAttribDivisor(GLuint index, GLuint divisor)
147 VertexAttribState& state = m_vertexAttribState[index];
148 state.divisor = divisor;
151 void WebGLVertexArrayObjectOES::VertexAttribState::trace(Visitor* visitor)
153 visitor->trace(bufferBinding);
156 void WebGLVertexArrayObjectOES::trace(Visitor* visitor)
158 visitor->trace(m_boundElementArrayBuffer);
159 visitor->trace(m_vertexAttribState);
160 WebGLContextObject::trace(visitor);