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)
44 , m_destructionInProgress(false)
46 , m_boundElementArrayBuffer(nullptr)
48 m_vertexAttribState.resize(ctx->maxVertexAttribs());
54 setObject(context()->webContext()->createVertexArrayOES());
59 WebGLVertexArrayObjectOES::~WebGLVertexArrayObjectOES()
62 m_destructionInProgress = true;
65 // Delete the platform framebuffer resource. Explicit detachment
66 // is for the benefit of Oilpan, where this vertex array object
67 // isn't detached when it and the WebGLRenderingContextBase object
68 // it is registered with are both finalized. Without Oilpan, the
69 // object will have been detached.
71 // To keep the code regular, the trivial detach()ment is always
73 detachAndDeleteObject();
76 void WebGLVertexArrayObjectOES::dispatchDetached(blink::WebGraphicsContext3D* context3d)
78 if (m_boundElementArrayBuffer)
79 m_boundElementArrayBuffer->onDetached(context3d);
81 for (size_t i = 0; i < m_vertexAttribState.size(); ++i) {
82 VertexAttribState& state = m_vertexAttribState[i];
83 if (state.bufferBinding)
84 state.bufferBinding->onDetached(context3d);
88 void WebGLVertexArrayObjectOES::deleteObjectImpl(blink::WebGraphicsContext3D* context3d, Platform3DObject object)
94 context3d->deleteVertexArrayOES(object);
99 // These m_boundElementArrayBuffer and m_vertexAttribState heap
100 // objects must not be accessed during destruction in the oilpan
101 // build. They could have been already finalized. The finalizers
102 // of these objects (and their elements) will themselves handle
104 if (!m_destructionInProgress)
105 dispatchDetached(context3d);
107 dispatchDetached(context3d);
111 void WebGLVertexArrayObjectOES::setElementArrayBuffer(PassRefPtrWillBeRawPtr<WebGLBuffer> buffer)
114 buffer->onAttached();
115 if (m_boundElementArrayBuffer)
116 m_boundElementArrayBuffer->onDetached(context()->webContext());
117 m_boundElementArrayBuffer = buffer;
120 void WebGLVertexArrayObjectOES::setVertexAttribState(
121 GLuint index, GLsizei bytesPerElement, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLintptr offset, PassRefPtrWillBeRawPtr<WebGLBuffer> buffer)
123 GLsizei validatedStride = stride ? stride : bytesPerElement;
125 VertexAttribState& state = m_vertexAttribState[index];
128 buffer->onAttached();
129 if (state.bufferBinding)
130 state.bufferBinding->onDetached(context()->webContext());
132 state.bufferBinding = buffer;
133 state.bytesPerElement = bytesPerElement;
136 state.normalized = normalized;
137 state.stride = validatedStride;
138 state.originalStride = stride;
139 state.offset = offset;
142 void WebGLVertexArrayObjectOES::unbindBuffer(PassRefPtrWillBeRawPtr<WebGLBuffer> buffer)
144 if (m_boundElementArrayBuffer == buffer) {
145 m_boundElementArrayBuffer->onDetached(context()->webContext());
146 m_boundElementArrayBuffer = nullptr;
149 for (size_t i = 0; i < m_vertexAttribState.size(); ++i) {
150 VertexAttribState& state = m_vertexAttribState[i];
151 if (state.bufferBinding == buffer) {
152 buffer->onDetached(context()->webContext());
153 state.bufferBinding = nullptr;
158 void WebGLVertexArrayObjectOES::setVertexAttribDivisor(GLuint index, GLuint divisor)
160 VertexAttribState& state = m_vertexAttribState[index];
161 state.divisor = divisor;
164 void WebGLVertexArrayObjectOES::VertexAttribState::trace(Visitor* visitor)
166 visitor->trace(bufferBinding);
169 void WebGLVertexArrayObjectOES::trace(Visitor* visitor)
171 visitor->trace(m_boundElementArrayBuffer);
172 visitor->trace(m_vertexAttribState);
173 WebGLContextObject::trace(visitor);