| 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 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 bool SkBitmap::lockPixelsAreWritable() const { | 446 bool SkBitmap::lockPixelsAreWritable() const { |
| 447 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false; | 447 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false; |
| 448 } | 448 } |
| 449 | 449 |
| 450 void SkBitmap::setPixels(void* p, SkColorTable* ctable) { | 450 void SkBitmap::setPixels(void* p, SkColorTable* ctable) { |
| 451 if (NULL == p) { | 451 if (NULL == p) { |
| 452 this->setPixelRef(NULL, 0); | 452 this->setPixelRef(NULL, 0); |
| 453 return; | 453 return; |
| 454 } | 454 } |
| 455 | 455 |
| 456 SkImageInfo info; | 456 Sk64 size = this->getSize64(); |
| 457 if (!this->asImageInfo(&info)) { | 457 SkASSERT(!size.isNeg() && size.is32()); |
| 458 this->setPixelRef(NULL, 0); | |
| 459 return; | |
| 460 } | |
| 461 | 458 |
| 462 SkPixelRef* pr = SkMallocPixelRef::NewDirect(info, p, fRowBytes, ctable); | 459 this->setPixelRef(new SkMallocPixelRef(p, size.get32(), ctable, false))->unr
ef(); |
| 463 if (NULL == pr) { | |
| 464 this->setPixelRef(NULL, 0); | |
| 465 return; | |
| 466 } | |
| 467 | |
| 468 this->setPixelRef(pr)->unref(); | |
| 469 | |
| 470 // since we're already allocated, we lockPixels right away | 460 // since we're already allocated, we lockPixels right away |
| 471 this->lockPixels(); | 461 this->lockPixels(); |
| 472 SkDEBUGCODE(this->validate();) | 462 SkDEBUGCODE(this->validate();) |
| 473 } | 463 } |
| 474 | 464 |
| 475 bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) { | 465 bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) { |
| 476 HeapAllocator stdalloc; | 466 HeapAllocator stdalloc; |
| 477 | 467 |
| 478 if (NULL == allocator) { | 468 if (NULL == allocator) { |
| 479 allocator = &stdalloc; | 469 allocator = &stdalloc; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 524 return fPixelRef ? fPixelRef->getTexture() : NULL; | 514 return fPixelRef ? fPixelRef->getTexture() : NULL; |
| 525 } | 515 } |
| 526 | 516 |
| 527 /////////////////////////////////////////////////////////////////////////////// | 517 /////////////////////////////////////////////////////////////////////////////// |
| 528 | 518 |
| 529 /** We explicitly use the same allocator for our pixels that SkMask does, | 519 /** We explicitly use the same allocator for our pixels that SkMask does, |
| 530 so that we can freely assign memory allocated by one class to the other. | 520 so that we can freely assign memory allocated by one class to the other. |
| 531 */ | 521 */ |
| 532 bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst, | 522 bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst, |
| 533 SkColorTable* ctable) { | 523 SkColorTable* ctable) { |
| 534 SkImageInfo info; | 524 Sk64 size = dst->getSize64(); |
| 535 if (!dst->asImageInfo(&info)) { | 525 if (size.isNeg() || !size.is32()) { |
| 536 // SkDebugf("unsupported config for info %d\n", dst->config()); | |
| 537 return false; | |
| 538 } | |
| 539 | |
| 540 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, dst->rowBytes(), | |
| 541 ctable); | |
| 542 if (NULL == pr) { | |
| 543 return false; | 526 return false; |
| 544 } | 527 } |
| 545 | 528 |
| 546 dst->setPixelRef(pr, 0)->unref(); | 529 void* addr = sk_malloc_flags(size.get32(), 0); // returns NULL on failure |
| 530 if (NULL == addr) { |
| 531 return false; |
| 532 } |
| 533 |
| 534 dst->setPixelRef(new SkMallocPixelRef(addr, size.get32(), ctable))->unref(); |
| 547 // since we're already allocated, we lockPixels right away | 535 // since we're already allocated, we lockPixels right away |
| 548 dst->lockPixels(); | 536 dst->lockPixels(); |
| 549 return true; | 537 return true; |
| 550 } | 538 } |
| 551 | 539 |
| 552 /////////////////////////////////////////////////////////////////////////////// | 540 /////////////////////////////////////////////////////////////////////////////// |
| 553 | 541 |
| 554 size_t SkBitmap::getSafeSize() const { | 542 size_t SkBitmap::getSafeSize() const { |
| 555 // This is intended to be a size_t version of ComputeSafeSize64(), just | 543 // This is intended to be a size_t version of ComputeSafeSize64(), just |
| 556 // faster. The computation is meant to be identical. | 544 // faster. The computation is meant to be identical. |
| (...skipping 1089 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1646 fHeight = height; | 1634 fHeight = height; |
| 1647 fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*)); | 1635 fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*)); |
| 1648 } | 1636 } |
| 1649 | 1637 |
| 1650 SkBitmap::RLEPixels::~RLEPixels() { | 1638 SkBitmap::RLEPixels::~RLEPixels() { |
| 1651 sk_free(fYPtrs); | 1639 sk_free(fYPtrs); |
| 1652 } | 1640 } |
| 1653 | 1641 |
| 1654 /////////////////////////////////////////////////////////////////////////////// | 1642 /////////////////////////////////////////////////////////////////////////////// |
| 1655 | 1643 |
| 1656 void SkImageInfo::unflatten(SkFlattenableReadBuffer& buffer) { | |
| 1657 fWidth = buffer.read32(); | |
| 1658 fHeight = buffer.read32(); | |
| 1659 | |
| 1660 uint32_t packed = buffer.read32(); | |
| 1661 SkASSERT(0 == (packed >> 16)); | |
| 1662 fAlphaType = (SkAlphaType)((packed >> 8) & 0xFF); | |
| 1663 fColorType = (SkColorType)((packed >> 0) & 0xFF); | |
| 1664 } | |
| 1665 | |
| 1666 void SkImageInfo::flatten(SkFlattenableWriteBuffer& buffer) const { | |
| 1667 buffer.write32(fWidth); | |
| 1668 buffer.write32(fHeight); | |
| 1669 | |
| 1670 SkASSERT(0 == (fAlphaType & ~0xFF)); | |
| 1671 SkASSERT(0 == (fColorType & ~0xFF)); | |
| 1672 uint32_t packed = (fAlphaType << 8) | fColorType; | |
| 1673 buffer.write32(packed); | |
| 1674 } | |
| 1675 | |
| 1676 /////////////////////////////////////////////////////////////////////////////// | |
| 1677 | |
| 1678 #ifdef SK_DEBUG | 1644 #ifdef SK_DEBUG |
| 1679 void SkBitmap::validate() const { | 1645 void SkBitmap::validate() const { |
| 1680 SkASSERT(fConfig < kConfigCount); | 1646 SkASSERT(fConfig < kConfigCount); |
| 1681 SkASSERT(fRowBytes >= (unsigned)ComputeRowBytes((Config)fConfig, fWidth)); | 1647 SkASSERT(fRowBytes >= (unsigned)ComputeRowBytes((Config)fConfig, fWidth)); |
| 1682 uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImm
utable_Flag; | 1648 uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImm
utable_Flag; |
| 1683 #ifdef SK_BUILD_FOR_ANDROID | 1649 #ifdef SK_BUILD_FOR_ANDROID |
| 1684 allFlags |= kHasHardwareMipMap_Flag; | 1650 allFlags |= kHasHardwareMipMap_Flag; |
| 1685 #endif | 1651 #endif |
| 1686 SkASSERT(fFlags <= allFlags); | 1652 SkASSERT(fFlags <= allFlags); |
| 1687 SkASSERT(fPixelLockCount >= 0); | 1653 SkASSERT(fPixelLockCount >= 0); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1733 if (NULL != uri) { | 1699 if (NULL != uri) { |
| 1734 str->appendf(" uri:\"%s\"", uri); | 1700 str->appendf(" uri:\"%s\"", uri); |
| 1735 } else { | 1701 } else { |
| 1736 str->appendf(" pixelref:%p", pr); | 1702 str->appendf(" pixelref:%p", pr); |
| 1737 } | 1703 } |
| 1738 } | 1704 } |
| 1739 | 1705 |
| 1740 str->append(")"); | 1706 str->append(")"); |
| 1741 } | 1707 } |
| 1742 #endif | 1708 #endif |
| OLD | NEW |