OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkMipMap.h" | 8 #include "SkMipMap.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 int64_t size = sk_64_mul(levelCount + 1, sizeof(Level)) + pixelSize; | 116 int64_t size = sk_64_mul(levelCount + 1, sizeof(Level)) + pixelSize; |
117 if (!sk_64_isS32(size)) { | 117 if (!sk_64_isS32(size)) { |
118 return NULL; | 118 return NULL; |
119 } | 119 } |
120 return (Level*)sk_malloc_throw(sk_64_asS32(size)); | 120 return (Level*)sk_malloc_throw(sk_64_asS32(size)); |
121 } | 121 } |
122 | 122 |
123 SkMipMap* SkMipMap::Build(const SkBitmap& src) { | 123 SkMipMap* SkMipMap::Build(const SkBitmap& src) { |
124 void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src); | 124 void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src); |
125 | 125 |
126 const SkBitmap::Config config = src.config(); | 126 const SkColorType ct = src.colorType(); |
127 switch (config) { | 127 const SkAlphaType at = src.alphaType(); |
128 case SkBitmap::kARGB_8888_Config: | 128 switch (ct) { |
| 129 case kRGBA_8888_SkColorType: |
| 130 case kBGRA_8888_SkColorType: |
129 proc = downsampleby2_proc32; | 131 proc = downsampleby2_proc32; |
130 break; | 132 break; |
131 case SkBitmap::kRGB_565_Config: | 133 case kRGB_565_SkColorType: |
132 proc = downsampleby2_proc16; | 134 proc = downsampleby2_proc16; |
133 break; | 135 break; |
134 case SkBitmap::kARGB_4444_Config: | 136 case kARGB_4444_SkColorType: |
135 proc = downsampleby2_proc4444; | 137 proc = downsampleby2_proc4444; |
136 break; | 138 break; |
137 case SkBitmap::kIndex8_Config: | |
138 case SkBitmap::kA8_Config: | |
139 default: | 139 default: |
140 return NULL; // don't build mipmaps for these configs | 140 return NULL; // don't build mipmaps for any other colortypes (yet) |
141 } | 141 } |
142 | 142 |
143 SkAutoLockPixels alp(src); | 143 SkAutoLockPixels alp(src); |
144 if (!src.readyToDraw()) { | 144 if (!src.readyToDraw()) { |
145 return NULL; | 145 return NULL; |
146 } | 146 } |
147 | 147 |
148 // whip through our loop to compute the exact size needed | 148 // whip through our loop to compute the exact size needed |
149 size_t size = 0; | 149 size_t size = 0; |
150 int countLevels = 0; | 150 int countLevels = 0; |
151 { | 151 { |
152 int width = src.width(); | 152 int width = src.width(); |
153 int height = src.height(); | 153 int height = src.height(); |
154 for (;;) { | 154 for (;;) { |
155 width >>= 1; | 155 width >>= 1; |
156 height >>= 1; | 156 height >>= 1; |
157 if (0 == width || 0 == height) { | 157 if (0 == width || 0 == height) { |
158 break; | 158 break; |
159 } | 159 } |
160 size += SkBitmap::ComputeRowBytes(config, width) * height; | 160 size += SkColorTypeMinRowBytes(ct, width) * height; |
161 countLevels += 1; | 161 countLevels += 1; |
162 } | 162 } |
163 } | 163 } |
164 if (0 == countLevels) { | 164 if (0 == countLevels) { |
165 return NULL; | 165 return NULL; |
166 } | 166 } |
167 | 167 |
168 Level* levels = SkMipMap::AllocLevels(countLevels, size); | 168 Level* levels = SkMipMap::AllocLevels(countLevels, size); |
169 if (NULL == levels) { | 169 if (NULL == levels) { |
170 return NULL; | 170 return NULL; |
171 } | 171 } |
172 | 172 |
173 uint8_t* baseAddr = (uint8_t*)&levels[countLevels]; | 173 uint8_t* baseAddr = (uint8_t*)&levels[countLevels]; |
174 uint8_t* addr = baseAddr; | 174 uint8_t* addr = baseAddr; |
175 int width = src.width(); | 175 int width = src.width(); |
176 int height = src.height(); | 176 int height = src.height(); |
177 uint32_t rowBytes; | 177 uint32_t rowBytes; |
178 SkBitmap srcBM(src); | 178 SkBitmap srcBM(src); |
179 | 179 |
180 for (int i = 0; i < countLevels; ++i) { | 180 for (int i = 0; i < countLevels; ++i) { |
181 width >>= 1; | 181 width >>= 1; |
182 height >>= 1; | 182 height >>= 1; |
183 rowBytes = SkToU32(SkBitmap::ComputeRowBytes(config, width)); | 183 rowBytes = SkToU32(SkColorTypeMinRowBytes(ct, width)); |
184 | 184 |
185 levels[i].fPixels = addr; | 185 levels[i].fPixels = addr; |
186 levels[i].fWidth = width; | 186 levels[i].fWidth = width; |
187 levels[i].fHeight = height; | 187 levels[i].fHeight = height; |
188 levels[i].fRowBytes = rowBytes; | 188 levels[i].fRowBytes = rowBytes; |
189 levels[i].fScale = (float)width / src.width(); | 189 levels[i].fScale = (float)width / src.width(); |
190 | 190 |
191 SkBitmap dstBM; | 191 SkBitmap dstBM; |
192 dstBM.setConfig(config, width, height, rowBytes); | 192 dstBM.installPixels(SkImageInfo::Make(width, height, ct, at), addr, rowB
ytes); |
193 dstBM.setPixels(addr); | |
194 | 193 |
195 srcBM.lockPixels(); | 194 srcBM.lockPixels(); |
196 for (int y = 0; y < height; y++) { | 195 for (int y = 0; y < height; y++) { |
197 for (int x = 0; x < width; x++) { | 196 for (int x = 0; x < width; x++) { |
198 proc(&dstBM, x, y, srcBM); | 197 proc(&dstBM, x, y, srcBM); |
199 } | 198 } |
200 } | 199 } |
201 srcBM.unlockPixels(); | 200 srcBM.unlockPixels(); |
202 | 201 |
203 srcBM = dstBM; | 202 srcBM = dstBM; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 } | 246 } |
248 | 247 |
249 if (level > fCount) { | 248 if (level > fCount) { |
250 level = fCount; | 249 level = fCount; |
251 } | 250 } |
252 if (levelPtr) { | 251 if (levelPtr) { |
253 *levelPtr = fLevels[level - 1]; | 252 *levelPtr = fLevels[level - 1]; |
254 } | 253 } |
255 return true; | 254 return true; |
256 } | 255 } |
OLD | NEW |