OLD | NEW |
---|---|
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2008 The Android Open Source Project | 3 * Copyright 2008 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
354 SkASSERT(0 == fPixelLockCount); | 354 SkASSERT(0 == fPixelLockCount); |
355 fPixels = NULL; | 355 fPixels = NULL; |
356 if (fColorTable) { | 356 if (fColorTable) { |
357 fColorTable->unref(); | 357 fColorTable->unref(); |
358 fColorTable = NULL; | 358 fColorTable = NULL; |
359 } | 359 } |
360 } | 360 } |
361 } | 361 } |
362 } | 362 } |
363 | 363 |
364 static bool config2ColorType(SkBitmap::Config config, SkColorType* ctOut) { | |
scroggo
2013/12/06 14:50:33
style nit: underscores for static functions.
reed1
2013/12/06 15:48:46
Done.
| |
365 SkColorType ct; | |
366 switch (config) { | |
367 case SkBitmap::kNo_Config: | |
368 return false; | |
369 case SkBitmap::kA8_Config: | |
370 ct = kAlpha_8_SkColorType; | |
371 break; | |
372 case SkBitmap::kIndex8_Config: | |
373 ct = kIndex8_SkColorType; | |
374 break; | |
375 case SkBitmap::kRGB_565_Config: | |
376 ct = kRGB_565_SkColorType; | |
377 break; | |
378 case SkBitmap::kARGB_4444_Config: | |
379 ct = kARGB_4444_SkColorType; | |
380 break; | |
381 case SkBitmap::kARGB_8888_Config: | |
382 ct = kPMColor_SkColorType; | |
383 break; | |
384 } | |
385 if (ctOut) { | |
386 *ctOut = ct; | |
387 } | |
388 return true; | |
389 } | |
390 | |
391 bool SkBitmap::asImageInfo(SkImageInfo* info) const { | |
392 SkColorType ct; | |
393 if (!config2ColorType(this->config(), &ct)) { | |
394 return false; | |
395 } | |
396 if (info) { | |
397 info->fWidth = fWidth; | |
398 info->fHeight = fHeight; | |
399 info->fAlphaType = this->alphaType(); | |
400 info->fColorType = ct; | |
401 } | |
402 return true; | |
403 } | |
404 | |
364 SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, size_t offset) { | 405 SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, size_t offset) { |
365 // do this first, we that we never have a non-zero offset with a null ref | 406 // do this first, we that we never have a non-zero offset with a null ref |
366 if (NULL == pr) { | 407 if (NULL == pr) { |
367 offset = 0; | 408 offset = 0; |
368 } | 409 } |
369 | 410 |
370 if (fPixelRef != pr || fPixelRefOffset != offset) { | 411 if (fPixelRef != pr || fPixelRefOffset != offset) { |
371 if (fPixelRef != pr) { | 412 if (fPixelRef != pr) { |
372 this->freePixels(); | 413 this->freePixels(); |
373 SkASSERT(NULL == fPixelRef); | 414 SkASSERT(NULL == fPixelRef); |
(...skipping 30 matching lines...) Expand all Loading... | |
404 bool SkBitmap::lockPixelsAreWritable() const { | 445 bool SkBitmap::lockPixelsAreWritable() const { |
405 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false; | 446 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false; |
406 } | 447 } |
407 | 448 |
408 void SkBitmap::setPixels(void* p, SkColorTable* ctable) { | 449 void SkBitmap::setPixels(void* p, SkColorTable* ctable) { |
409 if (NULL == p) { | 450 if (NULL == p) { |
410 this->setPixelRef(NULL, 0); | 451 this->setPixelRef(NULL, 0); |
411 return; | 452 return; |
412 } | 453 } |
413 | 454 |
414 Sk64 size = this->getSize64(); | 455 SkImageInfo info; |
415 SkASSERT(!size.isNeg() && size.is32()); | 456 if (!this->asImageInfo(&info)) { |
457 this->setPixelRef(NULL, 0); | |
458 return; | |
459 } | |
416 | 460 |
417 this->setPixelRef(new SkMallocPixelRef(p, size.get32(), ctable, false))->unr ef(); | 461 SkPixelRef* pr = SkMallocPixelRef::NewDirect(info, p, fRowBytes, ctable); |
462 if (NULL == pr) { | |
463 this->setPixelRef(NULL, 0); | |
464 return; | |
465 } | |
466 | |
467 this->setPixelRef(pr)->unref(); | |
468 | |
418 // since we're already allocated, we lockPixels right away | 469 // since we're already allocated, we lockPixels right away |
419 this->lockPixels(); | 470 this->lockPixels(); |
420 SkDEBUGCODE(this->validate();) | 471 SkDEBUGCODE(this->validate();) |
421 } | 472 } |
422 | 473 |
423 bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) { | 474 bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) { |
424 HeapAllocator stdalloc; | 475 HeapAllocator stdalloc; |
425 | 476 |
426 if (NULL == allocator) { | 477 if (NULL == allocator) { |
427 allocator = &stdalloc; | 478 allocator = &stdalloc; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
472 return fPixelRef ? fPixelRef->getTexture() : NULL; | 523 return fPixelRef ? fPixelRef->getTexture() : NULL; |
473 } | 524 } |
474 | 525 |
475 /////////////////////////////////////////////////////////////////////////////// | 526 /////////////////////////////////////////////////////////////////////////////// |
476 | 527 |
477 /** We explicitly use the same allocator for our pixels that SkMask does, | 528 /** We explicitly use the same allocator for our pixels that SkMask does, |
478 so that we can freely assign memory allocated by one class to the other. | 529 so that we can freely assign memory allocated by one class to the other. |
479 */ | 530 */ |
480 bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst, | 531 bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst, |
481 SkColorTable* ctable) { | 532 SkColorTable* ctable) { |
482 Sk64 size = dst->getSize64(); | 533 SkImageInfo info; |
483 if (size.isNeg() || !size.is32()) { | 534 if (!dst->asImageInfo(&info)) { |
535 // SkDebugf("unsupported config for info %d\n", dst->config()); | |
536 return false; | |
537 } | |
538 | |
539 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, dst->rowBytes(), | |
540 ctable); | |
mtklein
2013/12/06 14:44:34
ctable looks so lonely on this line
| |
541 if (NULL == pr) { | |
484 return false; | 542 return false; |
485 } | 543 } |
486 | 544 |
487 void* addr = sk_malloc_flags(size.get32(), 0); // returns NULL on failure | 545 dst->setPixelRef(pr, 0)->unref(); |
488 if (NULL == addr) { | |
489 return false; | |
490 } | |
491 | |
492 dst->setPixelRef(new SkMallocPixelRef(addr, size.get32(), ctable))->unref(); | |
493 // since we're already allocated, we lockPixels right away | 546 // since we're already allocated, we lockPixels right away |
494 dst->lockPixels(); | 547 dst->lockPixels(); |
495 return true; | 548 return true; |
496 } | 549 } |
497 | 550 |
498 /////////////////////////////////////////////////////////////////////////////// | 551 /////////////////////////////////////////////////////////////////////////////// |
499 | 552 |
500 size_t SkBitmap::getSafeSize() const { | 553 size_t SkBitmap::getSafeSize() const { |
501 // This is intended to be a size_t version of ComputeSafeSize64(), just | 554 // This is intended to be a size_t version of ComputeSafeSize64(), just |
502 // faster. The computation is meant to be identical. | 555 // faster. The computation is meant to be identical. |
(...skipping 1086 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1589 fHeight = height; | 1642 fHeight = height; |
1590 fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*)); | 1643 fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*)); |
1591 } | 1644 } |
1592 | 1645 |
1593 SkBitmap::RLEPixels::~RLEPixels() { | 1646 SkBitmap::RLEPixels::~RLEPixels() { |
1594 sk_free(fYPtrs); | 1647 sk_free(fYPtrs); |
1595 } | 1648 } |
1596 | 1649 |
1597 /////////////////////////////////////////////////////////////////////////////// | 1650 /////////////////////////////////////////////////////////////////////////////// |
1598 | 1651 |
1652 void SkImageInfo::unflatten(SkFlattenableReadBuffer& buffer) { | |
1653 fWidth = buffer.read32(); | |
1654 fHeight = buffer.read32(); | |
1655 | |
1656 uint32_t packed = buffer.read32(); | |
1657 SkASSERT(0 == (packed >> 16)); | |
1658 fAlphaType = (SkAlphaType)((packed >> 8) & 0xFF); | |
1659 fColorType = (SkColorType)((packed >> 0) & 0xFF); | |
1660 } | |
1661 | |
1662 void SkImageInfo::flatten(SkFlattenableWriteBuffer& buffer) const { | |
1663 buffer.write32(fWidth); | |
1664 buffer.write32(fHeight); | |
1665 | |
1666 SkASSERT(0 == (fAlphaType & ~0xFF)); | |
1667 SkASSERT(0 == (fColorType & ~0xFF)); | |
1668 uint32_t packed = (fAlphaType << 8) | fColorType; | |
1669 buffer.write32(packed); | |
1670 } | |
1671 | |
1672 /////////////////////////////////////////////////////////////////////////////// | |
1673 | |
1599 #ifdef SK_DEBUG | 1674 #ifdef SK_DEBUG |
1600 void SkBitmap::validate() const { | 1675 void SkBitmap::validate() const { |
1601 SkASSERT(fConfig < kConfigCount); | 1676 SkASSERT(fConfig < kConfigCount); |
1602 SkASSERT(fRowBytes >= (unsigned)ComputeRowBytes((Config)fConfig, fWidth)); | 1677 SkASSERT(fRowBytes >= (unsigned)ComputeRowBytes((Config)fConfig, fWidth)); |
1603 uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImm utable_Flag; | 1678 uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImm utable_Flag; |
1604 #ifdef SK_BUILD_FOR_ANDROID | 1679 #ifdef SK_BUILD_FOR_ANDROID |
1605 allFlags |= kHasHardwareMipMap_Flag; | 1680 allFlags |= kHasHardwareMipMap_Flag; |
1606 #endif | 1681 #endif |
1607 SkASSERT(fFlags <= allFlags); | 1682 SkASSERT(fFlags <= allFlags); |
1608 SkASSERT(fPixelLockCount >= 0); | 1683 SkASSERT(fPixelLockCount >= 0); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1654 if (NULL != uri) { | 1729 if (NULL != uri) { |
1655 str->appendf(" uri:\"%s\"", uri); | 1730 str->appendf(" uri:\"%s\"", uri); |
1656 } else { | 1731 } else { |
1657 str->appendf(" pixelref:%p", pr); | 1732 str->appendf(" pixelref:%p", pr); |
1658 } | 1733 } |
1659 } | 1734 } |
1660 | 1735 |
1661 str->append(")"); | 1736 str->append(")"); |
1662 } | 1737 } |
1663 #endif | 1738 #endif |
OLD | NEW |