1 /* 2 * Copyright (C) 2009 Apple Inc. All rights reserved. 3 * Copyright (C) 2009 Google Inc. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #ifndef WebGLByteArray_h 28 #define WebGLByteArray_h 29 30 #include "WebGLArray.h" 31 #include <limits> 32 #include <wtf/MathExtras.h> 33 #include <wtf/PassRefPtr.h> 34 #include <wtf/RefCounted.h> 35 36 namespace WebCore { 37 38 class WebGLArrayBuffer; 39 40 class WebGLByteArray : public WebGLArray { 41 public: isByteArray()42 virtual bool isByteArray() const { return true; } 43 44 static PassRefPtr<WebGLByteArray> create(unsigned length); 45 static PassRefPtr<WebGLByteArray> create(signed char* array, unsigned length); 46 static PassRefPtr<WebGLByteArray> create(PassRefPtr<WebGLArrayBuffer> buffer, int byteOffset, unsigned length); 47 data()48 char* data() { return static_cast<char*>(baseAddress()); } 49 50 virtual unsigned length() const; 51 virtual unsigned byteLength() const; 52 virtual PassRefPtr<WebGLArray> slice(unsigned offset, unsigned length); 53 set(unsigned index,double value)54 void set(unsigned index, double value) 55 { 56 if (index >= m_size) 57 return; 58 if (isnan(value)) // Clamp NaN to 0 59 value = 0; 60 if (value < std::numeric_limits<signed char>::min()) 61 value = std::numeric_limits<signed char>::min(); 62 else if (value > std::numeric_limits<signed char>::max()) 63 value = std::numeric_limits<signed char>::max(); 64 signed char* storage = static_cast<signed char*>(m_baseAddress); 65 storage[index] = static_cast<signed char>(value); 66 } 67 get(unsigned index,signed char & result)68 bool get(unsigned index, signed char& result) const 69 { 70 if (index >= m_size) 71 return false; 72 signed char* storage = static_cast<signed char*>(m_baseAddress); 73 result = storage[index]; 74 return true; 75 } 76 get(unsigned index)77 signed char get(unsigned index) const 78 { 79 return item(index); 80 } 81 item(unsigned index)82 signed char item(unsigned index) const 83 { 84 ASSERT(index < m_size); 85 signed char* storage = static_cast<signed char*>(m_baseAddress); 86 return storage[index]; 87 } 88 89 void set(WebGLByteArray* array, unsigned offset, ExceptionCode& ec); 90 91 private: 92 WebGLByteArray(PassRefPtr<WebGLArrayBuffer> buffer, 93 int offset, 94 unsigned length); 95 unsigned m_size; 96 }; 97 98 } // namespace WebCore 99 100 #endif // WebGLByteArray_h 101