[Release] Webkit-EFL Ver. 2.0_beta_118996_0.6.24
[framework/web/webkit-efl.git] / Source / WebCore / html / canvas / WebGLBuffer.cpp
1 /*
2  * Copyright (C) 2009 Apple 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 #if ENABLE(WEBGL)
29
30 #include "WebGLBuffer.h"
31
32 #include "CheckedInt.h"
33 #include "WebGLContextGroup.h"
34 #include "WebGLRenderingContext.h"
35 #include <wtf/ArrayBufferView.h>
36
37 namespace WebCore {
38
39 PassRefPtr<WebGLBuffer> WebGLBuffer::create(WebGLRenderingContext* ctx)
40 {
41     return adoptRef(new WebGLBuffer(ctx));
42 }
43
44 WebGLBuffer::WebGLBuffer(WebGLRenderingContext* ctx)
45     : WebGLSharedObject(ctx)
46     , m_target(0)
47     , m_byteLength(0)
48     , m_nextAvailableCacheEntry(0)
49 {
50     setObject(ctx->graphicsContext3D()->createBuffer());
51     clearCachedMaxIndices();
52 }
53
54 WebGLBuffer::~WebGLBuffer()
55 {
56     deleteObject(0);
57 }
58
59 void WebGLBuffer::deleteObjectImpl(GraphicsContext3D* context3d, Platform3DObject object)
60 {
61       context3d->deleteBuffer(object);
62 }
63
64 bool WebGLBuffer::associateBufferDataImpl(ArrayBuffer* array, GC3Dintptr byteOffset, GC3Dsizeiptr byteLength)
65 {
66     if (byteLength < 0 || byteOffset < 0)
67         return false;
68
69     if (array && byteLength) {
70         CheckedInt<GC3Dintptr> checkedOffset(byteOffset);
71         CheckedInt<GC3Dsizeiptr> checkedLength(byteLength);
72         CheckedInt<GC3Dintptr> checkedMax = checkedOffset + checkedLength;
73         if (!checkedMax.valid() || checkedMax.value() > static_cast<int32_t>(array->byteLength()))
74             return false;
75     }
76
77     switch (m_target) {
78     case GraphicsContext3D::ELEMENT_ARRAY_BUFFER:
79         m_byteLength = byteLength;
80         clearCachedMaxIndices();
81         if (byteLength) {
82             m_elementArrayBuffer = ArrayBuffer::create(byteLength, 1);
83             if (!m_elementArrayBuffer) {
84                 m_byteLength = 0;
85                 return false;
86             }
87             if (array) {
88                 // We must always clone the incoming data because client-side
89                 // modifications without calling bufferData or bufferSubData
90                 // must never be able to change the validation results.
91                 memcpy(static_cast<unsigned char*>(m_elementArrayBuffer->data()),
92                        static_cast<unsigned char*>(array->data()) + byteOffset,
93                        byteLength);
94             }
95         } else
96             m_elementArrayBuffer = 0;
97         return true;
98     case GraphicsContext3D::ARRAY_BUFFER:
99         m_byteLength = byteLength;
100         return true;
101     default:
102         return false;
103     }
104 }
105
106 bool WebGLBuffer::associateBufferData(GC3Dsizeiptr size)
107 {
108     if (size < 0)
109         return false;
110     return associateBufferDataImpl(0, 0, size);
111 }
112
113 bool WebGLBuffer::associateBufferData(ArrayBuffer* array)
114 {
115     if (!array)
116         return false;
117     return associateBufferDataImpl(array, 0, array->byteLength());
118 }
119
120 bool WebGLBuffer::associateBufferData(ArrayBufferView* array)
121 {
122     if (!array)
123         return false;
124     return associateBufferDataImpl(array->buffer().get(), array->byteOffset(), array->byteLength());
125 }
126
127 bool WebGLBuffer::associateBufferSubDataImpl(GC3Dintptr offset, ArrayBuffer* array, GC3Dintptr arrayByteOffset, GC3Dsizeiptr byteLength)
128 {
129     if (!array || offset < 0 || arrayByteOffset < 0 || byteLength < 0)
130         return false;
131
132     if (byteLength) {
133         CheckedInt<GC3Dintptr> checkedBufferOffset(offset);
134         CheckedInt<GC3Dintptr> checkedArrayOffset(arrayByteOffset);
135         CheckedInt<GC3Dsizeiptr> checkedLength(byteLength);
136         CheckedInt<GC3Dintptr> checkedArrayMax = checkedArrayOffset + checkedLength;
137         CheckedInt<GC3Dintptr> checkedBufferMax = checkedBufferOffset + checkedLength;
138         if (!checkedArrayMax.valid() || checkedArrayMax.value() > static_cast<int32_t>(array->byteLength()) || !checkedBufferMax.valid() || checkedBufferMax.value() > m_byteLength)
139             return false;
140     }
141
142     switch (m_target) {
143     case GraphicsContext3D::ELEMENT_ARRAY_BUFFER:
144         clearCachedMaxIndices();
145         if (byteLength) {
146             if (!m_elementArrayBuffer)
147                 return false;
148             memcpy(static_cast<unsigned char*>(m_elementArrayBuffer->data()) + offset,
149                    static_cast<unsigned char*>(array->data()) + arrayByteOffset,
150                    byteLength);
151         }
152         return true;
153     case GraphicsContext3D::ARRAY_BUFFER:
154         return true;
155     default:
156         return false;
157     }
158 }
159
160 bool WebGLBuffer::associateBufferSubData(GC3Dintptr offset, ArrayBuffer* array)
161 {
162     if (!array)
163         return false;
164     return associateBufferSubDataImpl(offset, array, 0, array->byteLength());
165 }
166
167 bool WebGLBuffer::associateBufferSubData(GC3Dintptr offset, ArrayBufferView* array)
168 {
169     if (!array)
170         return false;
171     return associateBufferSubDataImpl(offset, array->buffer().get(), array->byteOffset(), array->byteLength());
172 }
173
174 GC3Dsizeiptr WebGLBuffer::byteLength() const
175 {
176     return m_byteLength;
177 }
178
179 int WebGLBuffer::getCachedMaxIndex(GC3Denum type)
180 {
181     for (size_t i = 0; i < WTF_ARRAY_LENGTH(m_maxIndexCache); ++i)
182         if (m_maxIndexCache[i].type == type)
183             return m_maxIndexCache[i].maxIndex;
184     return -1;
185 }
186
187 void WebGLBuffer::setCachedMaxIndex(GC3Denum type, int value)
188 {
189     size_t numEntries = WTF_ARRAY_LENGTH(m_maxIndexCache);
190     for (size_t i = 0; i < numEntries; ++i)
191         if (m_maxIndexCache[i].type == type) {
192             m_maxIndexCache[i].maxIndex = value;
193             return;
194         }
195     m_maxIndexCache[m_nextAvailableCacheEntry].type = type;
196     m_maxIndexCache[m_nextAvailableCacheEntry].maxIndex = value;
197     m_nextAvailableCacheEntry = (m_nextAvailableCacheEntry + 1) % numEntries;
198 }
199
200 void WebGLBuffer::setTarget(GC3Denum target)
201 {
202     // In WebGL, a buffer is bound to one target in its lifetime
203     if (m_target)
204         return;
205     if (target == GraphicsContext3D::ARRAY_BUFFER || target == GraphicsContext3D::ELEMENT_ARRAY_BUFFER)
206         m_target = target;
207 }
208
209 void WebGLBuffer::clearCachedMaxIndices()
210 {
211     memset(m_maxIndexCache, 0, sizeof(m_maxIndexCache));
212 }
213
214 }
215
216 #endif // ENABLE(WEBGL)