|
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(3D_CANVAS) |
|
29 |
|
30 #include "WebGLBuffer.h" |
|
31 #include "WebGLRenderingContext.h" |
|
32 |
|
33 namespace WebCore { |
|
34 |
|
35 PassRefPtr<WebGLBuffer> WebGLBuffer::create(WebGLRenderingContext* ctx) |
|
36 { |
|
37 return adoptRef(new WebGLBuffer(ctx)); |
|
38 } |
|
39 |
|
40 WebGLBuffer::WebGLBuffer(WebGLRenderingContext* ctx) |
|
41 : CanvasObject(ctx) |
|
42 , m_target(0) |
|
43 , m_byteLength(0) |
|
44 , m_nextAvailableCacheEntry(0) |
|
45 { |
|
46 setObject(context()->graphicsContext3D()->createBuffer()); |
|
47 clearCachedMaxIndices(); |
|
48 } |
|
49 |
|
50 void WebGLBuffer::_deleteObject(Platform3DObject object) |
|
51 { |
|
52 context()->graphicsContext3D()->deleteBuffer(object); |
|
53 } |
|
54 |
|
55 bool WebGLBuffer::associateBufferData(int size) |
|
56 { |
|
57 if (!m_target) |
|
58 return false; |
|
59 |
|
60 if (m_target == GraphicsContext3D::ELEMENT_ARRAY_BUFFER) { |
|
61 m_byteLength = size; |
|
62 clearCachedMaxIndices(); |
|
63 m_elementArrayBuffer = ArrayBuffer::create(size, 1); |
|
64 if (!m_elementArrayBuffer) { |
|
65 m_byteLength = 0; |
|
66 return false; |
|
67 } |
|
68 return true; |
|
69 } else if (m_target == GraphicsContext3D::ARRAY_BUFFER) { |
|
70 m_byteLength = size; |
|
71 return true; |
|
72 } |
|
73 |
|
74 return false; |
|
75 } |
|
76 |
|
77 bool WebGLBuffer::associateBufferData(ArrayBuffer* array) |
|
78 { |
|
79 if (!m_target) |
|
80 return false; |
|
81 if (!array) |
|
82 return false; |
|
83 |
|
84 if (m_target == GraphicsContext3D::ELEMENT_ARRAY_BUFFER) { |
|
85 clearCachedMaxIndices(); |
|
86 m_byteLength = array->byteLength(); |
|
87 // We must always clone the incoming data because client-side |
|
88 // modifications without calling bufferData or bufferSubData |
|
89 // must never be able to change the validation results. |
|
90 m_elementArrayBuffer = ArrayBuffer::create(array); |
|
91 if (!m_elementArrayBuffer) { |
|
92 m_byteLength = 0; |
|
93 return false; |
|
94 } |
|
95 return true; |
|
96 } |
|
97 |
|
98 if (m_target == GraphicsContext3D::ARRAY_BUFFER) { |
|
99 m_byteLength = array->byteLength(); |
|
100 return true; |
|
101 } |
|
102 |
|
103 return false; |
|
104 } |
|
105 |
|
106 bool WebGLBuffer::associateBufferData(ArrayBufferView* array) |
|
107 { |
|
108 if (!m_target) |
|
109 return false; |
|
110 if (!array) |
|
111 return false; |
|
112 |
|
113 if (m_target == GraphicsContext3D::ELEMENT_ARRAY_BUFFER) { |
|
114 clearCachedMaxIndices(); |
|
115 m_byteLength = array->byteLength(); |
|
116 // We must always clone the incoming data because client-side |
|
117 // modifications without calling bufferData or bufferSubData |
|
118 // must never be able to change the validation results. |
|
119 m_elementArrayBuffer = ArrayBuffer::create(array->buffer().get()); |
|
120 if (!m_elementArrayBuffer) { |
|
121 m_byteLength = 0; |
|
122 return false; |
|
123 } |
|
124 return true; |
|
125 } |
|
126 |
|
127 if (m_target == GraphicsContext3D::ARRAY_BUFFER) { |
|
128 m_byteLength = array->byteLength(); |
|
129 return true; |
|
130 } |
|
131 |
|
132 return false; |
|
133 } |
|
134 |
|
135 bool WebGLBuffer::associateBufferSubData(long offset, ArrayBuffer* array) |
|
136 { |
|
137 if (!m_target) |
|
138 return false; |
|
139 if (!array) |
|
140 return false; |
|
141 |
|
142 if (m_target == GraphicsContext3D::ELEMENT_ARRAY_BUFFER) { |
|
143 clearCachedMaxIndices(); |
|
144 |
|
145 // We need to protect against integer overflow with these tests |
|
146 if (offset < 0) |
|
147 return false; |
|
148 |
|
149 unsigned long uoffset = static_cast<unsigned long>(offset); |
|
150 if (uoffset > m_byteLength || array->byteLength() > m_byteLength - uoffset) |
|
151 return false; |
|
152 |
|
153 if (!m_elementArrayBuffer) |
|
154 return false; |
|
155 |
|
156 memcpy(static_cast<unsigned char*>(m_elementArrayBuffer->data()) + offset, |
|
157 static_cast<unsigned char*>(array->data()), |
|
158 array->byteLength()); |
|
159 return true; |
|
160 } |
|
161 |
|
162 if (m_target == GraphicsContext3D::ARRAY_BUFFER) |
|
163 return array->byteLength() + offset <= m_byteLength; |
|
164 |
|
165 return false; |
|
166 } |
|
167 |
|
168 bool WebGLBuffer::associateBufferSubData(long offset, ArrayBufferView* array) |
|
169 { |
|
170 if (!m_target) |
|
171 return false; |
|
172 if (!array) |
|
173 return false; |
|
174 |
|
175 if (m_target == GraphicsContext3D::ELEMENT_ARRAY_BUFFER) { |
|
176 clearCachedMaxIndices(); |
|
177 |
|
178 // We need to protect against integer overflow with these tests |
|
179 if (offset < 0) |
|
180 return false; |
|
181 |
|
182 unsigned long uoffset = static_cast<unsigned long>(offset); |
|
183 if (uoffset > m_byteLength || array->byteLength() > m_byteLength - uoffset) |
|
184 return false; |
|
185 |
|
186 if (!m_elementArrayBuffer) |
|
187 return false; |
|
188 |
|
189 memcpy(static_cast<unsigned char*>(m_elementArrayBuffer->data()) + offset, array->baseAddress(), array->byteLength()); |
|
190 return true; |
|
191 } |
|
192 |
|
193 if (m_target == GraphicsContext3D::ARRAY_BUFFER) |
|
194 return array->byteLength() + offset <= m_byteLength; |
|
195 |
|
196 return false; |
|
197 } |
|
198 |
|
199 unsigned WebGLBuffer::byteLength() const |
|
200 { |
|
201 return m_byteLength; |
|
202 } |
|
203 |
|
204 long WebGLBuffer::getCachedMaxIndex(unsigned long type) |
|
205 { |
|
206 size_t numEntries = sizeof(m_maxIndexCache) / sizeof(MaxIndexCacheEntry); |
|
207 for (size_t i = 0; i < numEntries; i++) |
|
208 if (m_maxIndexCache[i].type == type) |
|
209 return m_maxIndexCache[i].maxIndex; |
|
210 return -1; |
|
211 } |
|
212 |
|
213 void WebGLBuffer::setCachedMaxIndex(unsigned long type, long value) |
|
214 { |
|
215 int numEntries = sizeof(m_maxIndexCache) / sizeof(MaxIndexCacheEntry); |
|
216 for (int i = 0; i < numEntries; i++) |
|
217 if (m_maxIndexCache[i].type == type) { |
|
218 m_maxIndexCache[i].maxIndex = value; |
|
219 return; |
|
220 } |
|
221 m_maxIndexCache[m_nextAvailableCacheEntry].type = type; |
|
222 m_maxIndexCache[m_nextAvailableCacheEntry].maxIndex = value; |
|
223 m_nextAvailableCacheEntry = (m_nextAvailableCacheEntry + 1) % numEntries; |
|
224 } |
|
225 |
|
226 void WebGLBuffer::setTarget(unsigned long target) |
|
227 { |
|
228 // In WebGL, a buffer is bound to one target in its lifetime |
|
229 if (m_target) |
|
230 return; |
|
231 if (target == GraphicsContext3D::ARRAY_BUFFER || target == GraphicsContext3D::ELEMENT_ARRAY_BUFFER) |
|
232 m_target = target; |
|
233 } |
|
234 |
|
235 void WebGLBuffer::clearCachedMaxIndices() |
|
236 { |
|
237 memset(m_maxIndexCache, 0, sizeof(m_maxIndexCache)); |
|
238 } |
|
239 |
|
240 } |
|
241 |
|
242 #endif // ENABLE(3D_CANVAS) |