initial import
[vuplus_webkit] / Source / WebCore / html / canvas / ArrayBufferView.h
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 #ifndef ArrayBufferView_h
27 #define ArrayBufferView_h
28
29 #include "ArrayBuffer.h"
30 #include "ExceptionCode.h"
31
32 #include <algorithm>
33 #include <limits.h>
34 #include <wtf/PassRefPtr.h>
35 #include <wtf/RefCounted.h>
36 #include <wtf/RefPtr.h>
37
38 namespace WebCore {
39
40 class ArrayBufferView : public RefCounted<ArrayBufferView> {
41   public:
42     virtual bool isByteArray() const { return false; }
43     virtual bool isUnsignedByteArray() const { return false; }
44     virtual bool isShortArray() const { return false; }
45     virtual bool isUnsignedShortArray() const { return false; }
46     virtual bool isIntArray() const { return false; }
47     virtual bool isUnsignedIntArray() const { return false; }
48     virtual bool isFloatArray() const { return false; }
49     virtual bool isDoubleArray() const { return false; }
50     virtual bool isDataView() const { return false; }
51
52     PassRefPtr<ArrayBuffer> buffer() const
53     {
54         return m_buffer;
55     }
56
57     void* baseAddress() const
58     {
59         return m_baseAddress;
60     }
61
62     unsigned byteOffset() const
63     {
64         return m_byteOffset;
65     }
66
67     virtual unsigned byteLength() const = 0;
68
69     virtual ~ArrayBufferView();
70
71   protected:
72     ArrayBufferView(PassRefPtr<ArrayBuffer> buffer, unsigned byteOffset);
73
74     void setImpl(ArrayBufferView* array, unsigned byteOffset, ExceptionCode& ec);
75
76     void setRangeImpl(const char* data, size_t dataByteLength, unsigned byteOffset, ExceptionCode& ec);
77
78     void zeroRangeImpl(unsigned byteOffset, size_t rangeByteLength, ExceptionCode& ec);
79
80     static void calculateOffsetAndLength(int start, int end, unsigned arraySize,
81                                          unsigned* offset, unsigned* length);
82
83     // Helper to verify that a given sub-range of an ArrayBuffer is
84     // within range.
85     template <typename T>
86     static bool verifySubRange(PassRefPtr<ArrayBuffer> buffer,
87                                unsigned byteOffset,
88                                unsigned numElements)
89     {
90         if (!buffer)
91             return false;
92         if (sizeof(T) > 1 && byteOffset % sizeof(T))
93             return false;
94         if (byteOffset > buffer->byteLength())
95             return false;
96         unsigned remainingElements = (buffer->byteLength() - byteOffset) / sizeof(T);
97         if (numElements > remainingElements)
98             return false;
99         return true;
100     }
101
102     // Input offset is in number of elements from this array's view;
103     // output offset is in number of bytes from the underlying buffer's view.
104     template <typename T>
105     static void clampOffsetAndNumElements(PassRefPtr<ArrayBuffer> buffer,
106                                           unsigned arrayByteOffset,
107                                           unsigned *offset,
108                                           unsigned *numElements)
109     {
110         unsigned maxOffset = (UINT_MAX - arrayByteOffset) / sizeof(T);
111         if (*offset > maxOffset) {
112             *offset = buffer->byteLength();
113             *numElements = 0;
114             return;
115         }
116         *offset = arrayByteOffset + *offset * sizeof(T);
117         *offset = std::min(buffer->byteLength(), *offset);
118         unsigned remainingElements = (buffer->byteLength() - *offset) / sizeof(T);
119         *numElements = std::min(remainingElements, *numElements);
120     }
121
122     // This is the address of the ArrayBuffer's storage, plus the byte offset.
123     void* m_baseAddress;
124
125     unsigned m_byteOffset;
126
127   private:
128     RefPtr<ArrayBuffer> m_buffer;
129 };
130
131 } // namespace WebCore
132
133 #endif // ArrayBufferView_h