• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* libs/graphics/animator/SkTypedArray.cpp
2 **
3 ** Copyright 2006, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #include "SkTypedArray.h"
19 
SkTypedArray()20 SkTypedArray::SkTypedArray() : fType(SkType_Unknown) {
21 }
22 
SkTypedArray(SkDisplayTypes type)23 SkTypedArray::SkTypedArray(SkDisplayTypes type) : fType(type) {
24 }
25 
getIndex(int index,SkOperand * operand)26 bool SkTypedArray::getIndex(int index, SkOperand* operand) {
27     if (index >= count()) {
28         SkASSERT(0);
29         return false;
30     }
31     *operand = begin()[index];
32     return true;
33 }
34 
35 
36 #if SK_SMALLER_ARRAY_TEMPLATE_EXPERIMENT == 1
SkDS32Array()37 SkDS32Array::SkDS32Array()
38 {
39     fReserve = fCount = 0;
40     fArray = NULL;
41 #ifdef SK_DEBUG
42     fData = NULL;
43 #endif
44 }
45 
SkDS32Array(const SkDS32Array & src)46 SkDS32Array::SkDS32Array(const SkDS32Array& src)
47 {
48     fReserve = fCount = 0;
49     fArray = NULL;
50 #ifdef SK_DEBUG
51     fData = NULL;
52 #endif
53     SkDS32Array tmp(src.fArray, src.fCount);
54     this->swap(tmp);
55 }
56 
SkDS32Array(const int32_t src[],U16CPU count)57 SkDS32Array::SkDS32Array(const int32_t src[], U16CPU count)
58 {
59     SkASSERT(src || count == 0);
60 
61     fReserve = fCount = 0;
62     fArray = NULL;
63 #ifdef SK_DEBUG
64     fData = NULL;
65 #endif
66     if (count)
67     {
68         fArray = (int32_t*)sk_malloc_throw(count * sizeof(int32_t));
69 #ifdef SK_DEBUG
70         fData = (int32_t (*)[kDebugArraySize]) fArray;
71 #endif
72         memcpy(fArray, src, sizeof(int32_t) * count);
73         fReserve = fCount = SkToU16(count);
74     }
75 }
76 
operator =(const SkDS32Array & src)77 SkDS32Array& SkDS32Array::operator=(const SkDS32Array& src)
78 {
79     if (this != &src)
80     {
81         if (src.fCount > fReserve)
82         {
83             SkDS32Array tmp(src.fArray, src.fCount);
84             this->swap(tmp);
85         }
86         else
87         {
88             memcpy(fArray, src.fArray, sizeof(int32_t) * src.fCount);
89             fCount = src.fCount;
90         }
91     }
92     return *this;
93 }
94 
operator ==(const SkDS32Array & a,const SkDS32Array & b)95 int operator==(const SkDS32Array& a, const SkDS32Array& b)
96 {
97     return a.fCount == b.fCount &&
98             (a.fCount == 0 || !memcmp(a.fArray, b.fArray, a.fCount * sizeof(int32_t)));
99 }
100 
swap(SkDS32Array & other)101 void SkDS32Array::swap(SkDS32Array& other)
102 {
103     SkTSwap(fArray, other.fArray);
104 #ifdef SK_DEBUG
105     SkTSwap(fData, other.fData);
106 #endif
107     SkTSwap(fReserve, other.fReserve);
108     SkTSwap(fCount, other.fCount);
109 }
110 
append(U16CPU count,const int32_t * src)111 int32_t* SkDS32Array::append(U16CPU count, const int32_t* src)
112 {
113     unsigned oldCount = fCount;
114     if (count)
115     {
116         SkASSERT(src == NULL || fArray == NULL ||
117                 src + count <= fArray || fArray + count <= src);
118 
119         this->growBy(count);
120         if (src)
121             memcpy(fArray + oldCount, src, sizeof(int32_t) * count);
122     }
123     return fArray + oldCount;
124 }
125 
find(const int32_t & elem) const126 int SkDS32Array::find(const int32_t& elem) const
127 {
128     const int32_t* iter = fArray;
129     const int32_t* stop = fArray + fCount;
130 
131     for (; iter < stop; iter++)
132     {
133         if (*iter == elem)
134             return (int) (iter - fArray);
135     }
136     return -1;
137 }
138 
growBy(U16CPU extra)139 void SkDS32Array::growBy(U16CPU extra)
140 {
141     SkASSERT(extra);
142     SkASSERT(fCount + extra <= 0xFFFF);
143 
144     if (fCount + extra > fReserve)
145     {
146         size_t size = fCount + extra + 4;
147         size += size >> 2;
148         int32_t* array = (int32_t*)sk_malloc_throw(size * sizeof(int32_t));
149         memcpy(array, fArray, fCount * sizeof(int32_t));
150 
151         sk_free(fArray);
152         fArray = array;
153 #ifdef SK_DEBUG
154         fData = (int32_t (*)[kDebugArraySize]) fArray;
155 #endif
156         fReserve = SkToU16((U16CPU)size);
157     }
158     fCount = SkToU16(fCount + extra);
159 }
160 
insert(U16CPU index,U16CPU count,const int32_t * src)161 int32_t* SkDS32Array::insert(U16CPU index, U16CPU count, const int32_t* src)
162 {
163     SkASSERT(count);
164     int oldCount = fCount;
165     this->growBy(count);
166     int32_t* dst = fArray + index;
167     memmove(dst + count, dst, sizeof(int32_t) * (oldCount - index));
168     if (src)
169         memcpy(dst, src, sizeof(int32_t) * count);
170     return dst;
171 }
172 
173 
rfind(const int32_t & elem) const174     int SkDS32Array::rfind(const int32_t& elem) const
175     {
176         const int32_t* iter = fArray + fCount;
177         const int32_t* stop = fArray;
178 
179         while (iter > stop)
180         {
181             if (*--iter == elem)
182                 return (int) (iter - stop);
183         }
184         return -1;
185     }
186 
187 #endif
188