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 "CheckedInt.h"
33 #include "WebGLContextGroup.h"
34 #include "WebGLRenderingContext.h"
35 #include <wtf/ArrayBufferView.h>
39 PassRefPtr<WebGLBuffer> WebGLBuffer::create(WebGLRenderingContext* ctx)
41 return adoptRef(new WebGLBuffer(ctx));
44 WebGLBuffer::WebGLBuffer(WebGLRenderingContext* ctx)
45 : WebGLSharedObject(ctx)
48 , m_nextAvailableCacheEntry(0)
50 setObject(ctx->graphicsContext3D()->createBuffer());
51 clearCachedMaxIndices();
54 WebGLBuffer::~WebGLBuffer()
59 void WebGLBuffer::deleteObjectImpl(GraphicsContext3D* context3d, Platform3DObject object)
61 context3d->deleteBuffer(object);
64 bool WebGLBuffer::associateBufferDataImpl(ArrayBuffer* array, GC3Dintptr byteOffset, GC3Dsizeiptr byteLength)
66 if (byteLength < 0 || byteOffset < 0)
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()))
78 case GraphicsContext3D::ELEMENT_ARRAY_BUFFER:
79 m_byteLength = byteLength;
80 clearCachedMaxIndices();
82 m_elementArrayBuffer = ArrayBuffer::create(byteLength, 1);
83 if (!m_elementArrayBuffer) {
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,
96 m_elementArrayBuffer = 0;
98 case GraphicsContext3D::ARRAY_BUFFER:
99 m_byteLength = byteLength;
106 bool WebGLBuffer::associateBufferData(GC3Dsizeiptr size)
110 return associateBufferDataImpl(0, 0, size);
113 bool WebGLBuffer::associateBufferData(ArrayBuffer* array)
117 return associateBufferDataImpl(array, 0, array->byteLength());
120 bool WebGLBuffer::associateBufferData(ArrayBufferView* array)
124 return associateBufferDataImpl(array->buffer().get(), array->byteOffset(), array->byteLength());
127 bool WebGLBuffer::associateBufferSubDataImpl(GC3Dintptr offset, ArrayBuffer* array, GC3Dintptr arrayByteOffset, GC3Dsizeiptr byteLength)
129 if (!array || offset < 0 || arrayByteOffset < 0 || byteLength < 0)
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)
143 case GraphicsContext3D::ELEMENT_ARRAY_BUFFER:
144 clearCachedMaxIndices();
146 if (!m_elementArrayBuffer)
148 memcpy(static_cast<unsigned char*>(m_elementArrayBuffer->data()) + offset,
149 static_cast<unsigned char*>(array->data()) + arrayByteOffset,
153 case GraphicsContext3D::ARRAY_BUFFER:
160 bool WebGLBuffer::associateBufferSubData(GC3Dintptr offset, ArrayBuffer* array)
164 return associateBufferSubDataImpl(offset, array, 0, array->byteLength());
167 bool WebGLBuffer::associateBufferSubData(GC3Dintptr offset, ArrayBufferView* array)
171 return associateBufferSubDataImpl(offset, array->buffer().get(), array->byteOffset(), array->byteLength());
174 GC3Dsizeiptr WebGLBuffer::byteLength() const
179 int WebGLBuffer::getCachedMaxIndex(GC3Denum type)
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;
187 void WebGLBuffer::setCachedMaxIndex(GC3Denum type, int value)
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;
195 m_maxIndexCache[m_nextAvailableCacheEntry].type = type;
196 m_maxIndexCache[m_nextAvailableCacheEntry].maxIndex = value;
197 m_nextAvailableCacheEntry = (m_nextAvailableCacheEntry + 1) % numEntries;
200 void WebGLBuffer::setTarget(GC3Denum target)
202 // In WebGL, a buffer is bound to one target in its lifetime
205 if (target == GraphicsContext3D::ARRAY_BUFFER || target == GraphicsContext3D::ELEMENT_ARRAY_BUFFER)
209 void WebGLBuffer::clearCachedMaxIndices()
211 memset(m_maxIndexCache, 0, sizeof(m_maxIndexCache));
216 #endif // ENABLE(WEBGL)