2 * Copyright (C) 2009 Apple 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.
30 #include "WebGLBuffer.h"
32 #include "ArrayBufferView.h"
33 #include "CheckedInt.h"
34 #include "WebGLRenderingContext.h"
38 PassRefPtr<WebGLBuffer> WebGLBuffer::create(WebGLRenderingContext* ctx)
40 return adoptRef(new WebGLBuffer(ctx));
43 WebGLBuffer::WebGLBuffer(WebGLRenderingContext* ctx)
47 , m_nextAvailableCacheEntry(0)
49 setObject(context()->graphicsContext3D()->createBuffer());
50 clearCachedMaxIndices();
53 void WebGLBuffer::deleteObjectImpl(Platform3DObject object)
55 context()->graphicsContext3D()->deleteBuffer(object);
58 bool WebGLBuffer::associateBufferDataImpl(ArrayBuffer* array, GC3Dintptr byteOffset, GC3Dsizeiptr byteLength)
60 if (byteLength < 0 || byteOffset < 0)
63 if (array && byteLength) {
64 CheckedInt<GC3Dintptr> checkedOffset(byteOffset);
65 CheckedInt<GC3Dsizeiptr> checkedLength(byteLength);
66 CheckedInt<GC3Dintptr> checkedMax = checkedOffset + checkedLength;
67 if (!checkedMax.valid() || checkedMax.value() > static_cast<int32_t>(array->byteLength()))
72 case GraphicsContext3D::ELEMENT_ARRAY_BUFFER:
73 m_byteLength = byteLength;
74 clearCachedMaxIndices();
76 m_elementArrayBuffer = ArrayBuffer::create(byteLength, 1);
77 if (!m_elementArrayBuffer) {
82 // We must always clone the incoming data because client-side
83 // modifications without calling bufferData or bufferSubData
84 // must never be able to change the validation results.
85 memcpy(static_cast<unsigned char*>(m_elementArrayBuffer->data()),
86 static_cast<unsigned char*>(array->data()) + byteOffset,
90 m_elementArrayBuffer = 0;
92 case GraphicsContext3D::ARRAY_BUFFER:
93 m_byteLength = byteLength;
100 bool WebGLBuffer::associateBufferData(GC3Dsizeiptr size)
104 return associateBufferDataImpl(0, 0, size);
107 bool WebGLBuffer::associateBufferData(ArrayBuffer* array)
111 return associateBufferDataImpl(array, 0, array->byteLength());
114 bool WebGLBuffer::associateBufferData(ArrayBufferView* array)
118 return associateBufferDataImpl(array->buffer().get(), array->byteOffset(), array->byteLength());
121 bool WebGLBuffer::associateBufferSubDataImpl(GC3Dintptr offset, ArrayBuffer* array, GC3Dintptr arrayByteOffset, GC3Dsizeiptr byteLength)
123 if (!array || offset < 0 || arrayByteOffset < 0 || byteLength < 0)
127 CheckedInt<GC3Dintptr> checkedBufferOffset(offset);
128 CheckedInt<GC3Dintptr> checkedArrayOffset(arrayByteOffset);
129 CheckedInt<GC3Dsizeiptr> checkedLength(byteLength);
130 CheckedInt<GC3Dintptr> checkedArrayMax = checkedArrayOffset + checkedLength;
131 CheckedInt<GC3Dintptr> checkedBufferMax = checkedBufferOffset + checkedLength;
132 if (!checkedArrayMax.valid() || checkedArrayMax.value() > static_cast<int32_t>(array->byteLength()) || !checkedBufferMax.valid() || checkedBufferMax.value() > m_byteLength)
137 case GraphicsContext3D::ELEMENT_ARRAY_BUFFER:
138 clearCachedMaxIndices();
140 if (!m_elementArrayBuffer)
142 memcpy(static_cast<unsigned char*>(m_elementArrayBuffer->data()) + offset,
143 static_cast<unsigned char*>(array->data()) + arrayByteOffset,
147 case GraphicsContext3D::ARRAY_BUFFER:
154 bool WebGLBuffer::associateBufferSubData(GC3Dintptr offset, ArrayBuffer* array)
158 return associateBufferSubDataImpl(offset, array, 0, array->byteLength());
161 bool WebGLBuffer::associateBufferSubData(GC3Dintptr offset, ArrayBufferView* array)
165 return associateBufferSubDataImpl(offset, array->buffer().get(), array->byteOffset(), array->byteLength());
168 GC3Dsizeiptr WebGLBuffer::byteLength() const
173 int WebGLBuffer::getCachedMaxIndex(GC3Denum type)
175 for (size_t i = 0; i < WTF_ARRAY_LENGTH(m_maxIndexCache); ++i)
176 if (m_maxIndexCache[i].type == type)
177 return m_maxIndexCache[i].maxIndex;
181 void WebGLBuffer::setCachedMaxIndex(GC3Denum type, int value)
183 size_t numEntries = WTF_ARRAY_LENGTH(m_maxIndexCache);
184 for (size_t i = 0; i < numEntries; ++i)
185 if (m_maxIndexCache[i].type == type) {
186 m_maxIndexCache[i].maxIndex = value;
189 m_maxIndexCache[m_nextAvailableCacheEntry].type = type;
190 m_maxIndexCache[m_nextAvailableCacheEntry].maxIndex = value;
191 m_nextAvailableCacheEntry = (m_nextAvailableCacheEntry + 1) % numEntries;
194 void WebGLBuffer::setTarget(GC3Denum target)
196 // In WebGL, a buffer is bound to one target in its lifetime
199 if (target == GraphicsContext3D::ARRAY_BUFFER || target == GraphicsContext3D::ELEMENT_ARRAY_BUFFER)
203 void WebGLBuffer::clearCachedMaxIndices()
205 memset(m_maxIndexCache, 0, sizeof(m_maxIndexCache));
210 #endif // ENABLE(WEBGL)