Upstream version 10.39.225.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 #if ENABLE(OILPAN)
44     , m_destructionInProgress(false)
45 #endif
46     , m_boundElementArrayBuffer(nullptr)
47 {
48     m_vertexAttribState.resize(ctx->maxVertexAttribs());
49
50     switch (m_type) {
51     case VaoTypeDefault:
52         break;
53     default:
54         setObject(context()->webContext()->createVertexArrayOES());
55         break;
56     }
57 }
58
59 WebGLVertexArrayObjectOES::~WebGLVertexArrayObjectOES()
60 {
61 #if ENABLE(OILPAN)
62     m_destructionInProgress = true;
63 #endif
64
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.
70     //
71     // To keep the code regular, the trivial detach()ment is always
72     // performed.
73     detachAndDeleteObject();
74 }
75
76 void WebGLVertexArrayObjectOES::dispatchDetached(blink::WebGraphicsContext3D* context3d)
77 {
78     if (m_boundElementArrayBuffer)
79         m_boundElementArrayBuffer->onDetached(context3d);
80
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);
85     }
86 }
87
88 void WebGLVertexArrayObjectOES::deleteObjectImpl(blink::WebGraphicsContext3D* context3d, Platform3DObject object)
89 {
90     switch (m_type) {
91     case VaoTypeDefault:
92         break;
93     default:
94         context3d->deleteVertexArrayOES(object);
95         break;
96     }
97
98 #if ENABLE(OILPAN)
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
103     // detachment.
104     if (!m_destructionInProgress)
105         dispatchDetached(context3d);
106 #else
107     dispatchDetached(context3d);
108 #endif
109 }
110
111 void WebGLVertexArrayObjectOES::setElementArrayBuffer(PassRefPtrWillBeRawPtr<WebGLBuffer> buffer)
112 {
113     if (buffer)
114         buffer->onAttached();
115     if (m_boundElementArrayBuffer)
116         m_boundElementArrayBuffer->onDetached(context()->webContext());
117     m_boundElementArrayBuffer = buffer;
118 }
119
120 void WebGLVertexArrayObjectOES::setVertexAttribState(
121     GLuint index, GLsizei bytesPerElement, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLintptr offset, PassRefPtrWillBeRawPtr<WebGLBuffer> buffer)
122 {
123     GLsizei validatedStride = stride ? stride : bytesPerElement;
124
125     VertexAttribState& state = m_vertexAttribState[index];
126
127     if (buffer)
128         buffer->onAttached();
129     if (state.bufferBinding)
130         state.bufferBinding->onDetached(context()->webContext());
131
132     state.bufferBinding = buffer;
133     state.bytesPerElement = bytesPerElement;
134     state.size = size;
135     state.type = type;
136     state.normalized = normalized;
137     state.stride = validatedStride;
138     state.originalStride = stride;
139     state.offset = offset;
140 }
141
142 void WebGLVertexArrayObjectOES::unbindBuffer(PassRefPtrWillBeRawPtr<WebGLBuffer> buffer)
143 {
144     if (m_boundElementArrayBuffer == buffer) {
145         m_boundElementArrayBuffer->onDetached(context()->webContext());
146         m_boundElementArrayBuffer = nullptr;
147     }
148
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;
154         }
155     }
156 }
157
158 void WebGLVertexArrayObjectOES::setVertexAttribDivisor(GLuint index, GLuint divisor)
159 {
160     VertexAttribState& state = m_vertexAttribState[index];
161     state.divisor = divisor;
162 }
163
164 void WebGLVertexArrayObjectOES::VertexAttribState::trace(Visitor* visitor)
165 {
166     visitor->trace(bufferBinding);
167 }
168
169 void WebGLVertexArrayObjectOES::trace(Visitor* visitor)
170 {
171     visitor->trace(m_boundElementArrayBuffer);
172     visitor->trace(m_vertexAttribState);
173     WebGLContextObject::trace(visitor);
174 }
175
176 }