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 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 case kARGB_4444_SkColorType: | 199 case kARGB_4444_SkColorType: |
200 proc_check = downsample4444; | 200 proc_check = downsample4444; |
201 proc_nocheck = proc_check; | 201 proc_nocheck = proc_check; |
202 break; | 202 break; |
203 case kAlpha_8_SkColorType: | 203 case kAlpha_8_SkColorType: |
204 case kGray_8_SkColorType: | 204 case kGray_8_SkColorType: |
205 proc_check = downsample8_check; | 205 proc_check = downsample8_check; |
206 proc_nocheck = downsample8_nocheck; | 206 proc_nocheck = downsample8_nocheck; |
207 break; | 207 break; |
208 default: | 208 default: |
209 return NULL; // don't build mipmaps for any other colortypes (yet) | 209 return nullptr; // don't build mipmaps for any other colortypes (yet
) |
210 } | 210 } |
211 | 211 |
212 // whip through our loop to compute the exact size needed | 212 // whip through our loop to compute the exact size needed |
213 size_t size = 0; | 213 size_t size = 0; |
214 int countLevels = 0; | 214 int countLevels = 0; |
215 { | 215 { |
216 int width = src.width(); | 216 int width = src.width(); |
217 int height = src.height(); | 217 int height = src.height(); |
218 for (;;) { | 218 for (;;) { |
219 width >>= 1; | 219 width >>= 1; |
220 height >>= 1; | 220 height >>= 1; |
221 if (0 == width || 0 == height) { | 221 if (0 == width || 0 == height) { |
222 break; | 222 break; |
223 } | 223 } |
224 size += SkColorTypeMinRowBytes(ct, width) * height; | 224 size += SkColorTypeMinRowBytes(ct, width) * height; |
225 countLevels += 1; | 225 countLevels += 1; |
226 } | 226 } |
227 } | 227 } |
228 if (0 == countLevels) { | 228 if (0 == countLevels) { |
229 return NULL; | 229 return nullptr; |
230 } | 230 } |
231 | 231 |
232 size_t storageSize = SkMipMap::AllocLevelsSize(countLevels, size); | 232 size_t storageSize = SkMipMap::AllocLevelsSize(countLevels, size); |
233 if (0 == storageSize) { | 233 if (0 == storageSize) { |
234 return NULL; | 234 return nullptr; |
235 } | 235 } |
236 | 236 |
237 SkAutoPixmapUnlock srcUnlocker; | 237 SkAutoPixmapUnlock srcUnlocker; |
238 if (!src.requestLock(&srcUnlocker)) { | 238 if (!src.requestLock(&srcUnlocker)) { |
239 return NULL; | 239 return nullptr; |
240 } | 240 } |
241 const SkPixmap& srcPixmap = srcUnlocker.pixmap(); | 241 const SkPixmap& srcPixmap = srcUnlocker.pixmap(); |
242 // Try to catch where we might have returned NULL for src crbug.com/492818 | 242 // Try to catch where we might have returned nullptr for src crbug.com/49281
8 |
243 if (NULL == srcPixmap.addr()) { | 243 if (nullptr == srcPixmap.addr()) { |
244 sk_throw(); | 244 sk_throw(); |
245 } | 245 } |
246 | 246 |
247 SkMipMap* mipmap; | 247 SkMipMap* mipmap; |
248 if (fact) { | 248 if (fact) { |
249 SkDiscardableMemory* dm = fact(storageSize); | 249 SkDiscardableMemory* dm = fact(storageSize); |
250 if (NULL == dm) { | 250 if (nullptr == dm) { |
251 return NULL; | 251 return nullptr; |
252 } | 252 } |
253 mipmap = new SkMipMap(storageSize, dm); | 253 mipmap = new SkMipMap(storageSize, dm); |
254 } else { | 254 } else { |
255 mipmap = new SkMipMap(sk_malloc_throw(storageSize), storageSize); | 255 mipmap = new SkMipMap(sk_malloc_throw(storageSize), storageSize); |
256 } | 256 } |
257 | 257 |
258 // init | 258 // init |
259 mipmap->fCount = countLevels; | 259 mipmap->fCount = countLevels; |
260 mipmap->fLevels = (Level*)mipmap->writable_data(); | 260 mipmap->fLevels = (Level*)mipmap->writable_data(); |
261 | 261 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 addr += height * rowBytes; | 314 addr += height * rowBytes; |
315 } | 315 } |
316 SkASSERT(addr == baseAddr + size); | 316 SkASSERT(addr == baseAddr + size); |
317 | 317 |
318 return mipmap; | 318 return mipmap; |
319 } | 319 } |
320 | 320 |
321 /////////////////////////////////////////////////////////////////////////////// | 321 /////////////////////////////////////////////////////////////////////////////// |
322 | 322 |
323 bool SkMipMap::extractLevel(SkScalar scale, Level* levelPtr) const { | 323 bool SkMipMap::extractLevel(SkScalar scale, Level* levelPtr) const { |
324 if (NULL == fLevels) { | 324 if (nullptr == fLevels) { |
325 return false; | 325 return false; |
326 } | 326 } |
327 | 327 |
328 if (scale >= SK_Scalar1 || scale <= 0 || !SkScalarIsFinite(scale)) { | 328 if (scale >= SK_Scalar1 || scale <= 0 || !SkScalarIsFinite(scale)) { |
329 return false; | 329 return false; |
330 } | 330 } |
331 | 331 |
332 SkScalar L = -SkScalarLog2(scale); | 332 SkScalar L = -SkScalarLog2(scale); |
333 if (!SkScalarIsFinite(L)) { | 333 if (!SkScalarIsFinite(L)) { |
334 return false; | 334 return false; |
335 } | 335 } |
336 SkASSERT(L >= 0); | 336 SkASSERT(L >= 0); |
337 // int rndLevel = SkScalarRoundToInt(L); | 337 // int rndLevel = SkScalarRoundToInt(L); |
338 int level = SkScalarFloorToInt(L); | 338 int level = SkScalarFloorToInt(L); |
339 // SkDebugf("mipmap scale=%g L=%g level=%d rndLevel=%d\n", scale, L, level, r
ndLevel); | 339 // SkDebugf("mipmap scale=%g L=%g level=%d rndLevel=%d\n", scale, L, level, r
ndLevel); |
340 | 340 |
341 SkASSERT(level >= 0); | 341 SkASSERT(level >= 0); |
342 if (level <= 0) { | 342 if (level <= 0) { |
343 return false; | 343 return false; |
344 } | 344 } |
345 | 345 |
346 if (level > fCount) { | 346 if (level > fCount) { |
347 level = fCount; | 347 level = fCount; |
348 } | 348 } |
349 if (levelPtr) { | 349 if (levelPtr) { |
350 *levelPtr = fLevels[level - 1]; | 350 *levelPtr = fLevels[level - 1]; |
351 } | 351 } |
352 return true; | 352 return true; |
353 } | 353 } |
OLD | NEW |