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