Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / canvas / WebGLVertexArrayObjectOES.cpp
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25
26 #include "config.h"
27
28 #include "core/html/canvas/WebGLVertexArrayObjectOES.h"
29
30 #include "core/html/canvas/WebGLRenderingContextBase.h"
31
32 namespace blink {
33
34 PassRefPtrWillBeRawPtr<WebGLVertexArrayObjectOES> WebGLVertexArrayObjectOES::create(WebGLRenderingContextBase* ctx, VaoType type)
35 {
36     return adoptRefWillBeNoop(new WebGLVertexArrayObjectOES(ctx, type));
37 }
38
39 WebGLVertexArrayObjectOES::WebGLVertexArrayObjectOES(WebGLRenderingContextBase* ctx, VaoType type)
40     : WebGLContextObject(ctx)
41     , m_type(type)
42     , m_hasEverBeenBound(false)
43     , m_boundElementArrayBuffer(nullptr)
44 {
45     ScriptWrappable::init(this);
46     m_vertexAttribState.resize(ctx->maxVertexAttribs());
47
48     switch (m_type) {
49     case VaoTypeDefault:
50         break;
51     default:
52         setObject(context()->webContext()->createVertexArrayOES());
53         break;
54     }
55 }
56
57 WebGLVertexArrayObjectOES::~WebGLVertexArrayObjectOES()
58 {
59 #if ENABLE(OILPAN)
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
63     // handle detachment.
64     m_boundElementArrayBuffer.clear();
65     m_vertexAttribState.clear();
66 #endif
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.
72     //
73     // To keep the code regular, the trivial detach()ment is always
74     // performed.
75     detachAndDeleteObject();
76 }
77
78 void WebGLVertexArrayObjectOES::deleteObjectImpl(blink::WebGraphicsContext3D* context3d, Platform3DObject object)
79 {
80     switch (m_type) {
81     case VaoTypeDefault:
82         break;
83     default:
84         context3d->deleteVertexArrayOES(object);
85         break;
86     }
87
88     if (m_boundElementArrayBuffer)
89         m_boundElementArrayBuffer->onDetached(context3d);
90
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);
95     }
96 }
97
98 void WebGLVertexArrayObjectOES::setElementArrayBuffer(PassRefPtrWillBeRawPtr<WebGLBuffer> buffer)
99 {
100     if (buffer)
101         buffer->onAttached();
102     if (m_boundElementArrayBuffer)
103         m_boundElementArrayBuffer->onDetached(context()->webContext());
104     m_boundElementArrayBuffer = buffer;
105 }
106
107 void WebGLVertexArrayObjectOES::setVertexAttribState(
108     GLuint index, GLsizei bytesPerElement, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLintptr offset, PassRefPtrWillBeRawPtr<WebGLBuffer> buffer)
109 {
110     GLsizei validatedStride = stride ? stride : bytesPerElement;
111
112     VertexAttribState& state = m_vertexAttribState[index];
113
114     if (buffer)
115         buffer->onAttached();
116     if (state.bufferBinding)
117         state.bufferBinding->onDetached(context()->webContext());
118
119     state.bufferBinding = buffer;
120     state.bytesPerElement = bytesPerElement;
121     state.size = size;
122     state.type = type;
123     state.normalized = normalized;
124     state.stride = validatedStride;
125     state.originalStride = stride;
126     state.offset = offset;
127 }
128
129 void WebGLVertexArrayObjectOES::unbindBuffer(PassRefPtrWillBeRawPtr<WebGLBuffer> buffer)
130 {
131     if (m_boundElementArrayBuffer == buffer) {
132         m_boundElementArrayBuffer->onDetached(context()->webContext());
133         m_boundElementArrayBuffer = nullptr;
134     }
135
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;
141         }
142     }
143 }
144
145 void WebGLVertexArrayObjectOES::setVertexAttribDivisor(GLuint index, GLuint divisor)
146 {
147     VertexAttribState& state = m_vertexAttribState[index];
148     state.divisor = divisor;
149 }
150
151 void WebGLVertexArrayObjectOES::VertexAttribState::trace(Visitor* visitor)
152 {
153     visitor->trace(bufferBinding);
154 }
155
156 void WebGLVertexArrayObjectOES::trace(Visitor* visitor)
157 {
158     visitor->trace(m_boundElementArrayBuffer);
159     visitor->trace(m_vertexAttribState);
160     WebGLContextObject::trace(visitor);
161 }
162
163 }