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 config_to_colorType(SkBitmap::Config config, SkColorType* ctOut) { |
| 365 SkColorType ct; |
| 366 switch (config) { |
| 367 case SkBitmap::kA8_Config: |
| 368 ct = kAlpha_8_SkColorType; |
| 369 break; |
| 370 case SkBitmap::kIndex8_Config: |
| 371 ct = kIndex8_SkColorType; |
| 372 break; |
| 373 case SkBitmap::kRGB_565_Config: |
| 374 ct = kRGB_565_SkColorType; |
| 375 break; |
| 376 case SkBitmap::kARGB_4444_Config: |
| 377 ct = kARGB_4444_SkColorType; |
| 378 break; |
| 379 case SkBitmap::kARGB_8888_Config: |
| 380 ct = kPMColor_SkColorType; |
| 381 break; |
| 382 case SkBitmap::kNo_Config: |
| 383 default: |
| 384 return false; |
| 385 } |
| 386 if (ctOut) { |
| 387 *ctOut = ct; |
| 388 } |
| 389 return true; |
| 390 } |
| 391 |
| 392 bool SkBitmap::asImageInfo(SkImageInfo* info) const { |
| 393 SkColorType ct; |
| 394 if (!config_to_colorType(this->config(), &ct)) { |
| 395 return false; |
| 396 } |
| 397 if (info) { |
| 398 info->fWidth = fWidth; |
| 399 info->fHeight = fHeight; |
| 400 info->fAlphaType = this->alphaType(); |
| 401 info->fColorType = ct; |
| 402 } |
| 403 return true; |
| 404 } |
| 405 |
364 SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, size_t offset) { | 406 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 | 407 // do this first, we that we never have a non-zero offset with a null ref |
366 if (NULL == pr) { | 408 if (NULL == pr) { |
367 offset = 0; | 409 offset = 0; |
368 } | 410 } |
369 | 411 |
370 if (fPixelRef != pr || fPixelRefOffset != offset) { | 412 if (fPixelRef != pr || fPixelRefOffset != offset) { |
371 if (fPixelRef != pr) { | 413 if (fPixelRef != pr) { |
372 this->freePixels(); | 414 this->freePixels(); |
373 SkASSERT(NULL == fPixelRef); | 415 SkASSERT(NULL == fPixelRef); |
(...skipping 30 matching lines...) Expand all Loading... |
404 bool SkBitmap::lockPixelsAreWritable() const { | 446 bool SkBitmap::lockPixelsAreWritable() const { |
405 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false; | 447 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false; |
406 } | 448 } |
407 | 449 |
408 void SkBitmap::setPixels(void* p, SkColorTable* ctable) { | 450 void SkBitmap::setPixels(void* p, SkColorTable* ctable) { |
409 if (NULL == p) { | 451 if (NULL == p) { |
410 this->setPixelRef(NULL, 0); | 452 this->setPixelRef(NULL, 0); |
411 return; | 453 return; |
412 } | 454 } |
413 | 455 |
414 Sk64 size = this->getSize64(); | 456 SkImageInfo info; |
415 SkASSERT(!size.isNeg() && size.is32()); | 457 if (!this->asImageInfo(&info)) { |
| 458 this->setPixelRef(NULL, 0); |
| 459 return; |
| 460 } |
416 | 461 |
417 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 |
418 // since we're already allocated, we lockPixels right away | 470 // since we're already allocated, we lockPixels right away |
419 this->lockPixels(); | 471 this->lockPixels(); |
420 SkDEBUGCODE(this->validate();) | 472 SkDEBUGCODE(this->validate();) |
421 } | 473 } |
422 | 474 |
423 bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) { | 475 bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) { |
424 HeapAllocator stdalloc; | 476 HeapAllocator stdalloc; |
425 | 477 |
426 if (NULL == allocator) { | 478 if (NULL == allocator) { |
427 allocator = &stdalloc; | 479 allocator = &stdalloc; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 return fPixelRef ? fPixelRef->getTexture() : NULL; | 524 return fPixelRef ? fPixelRef->getTexture() : NULL; |
473 } | 525 } |
474 | 526 |
475 /////////////////////////////////////////////////////////////////////////////// | 527 /////////////////////////////////////////////////////////////////////////////// |
476 | 528 |
477 /** 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, |
478 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. |
479 */ | 531 */ |
480 bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst, | 532 bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst, |
481 SkColorTable* ctable) { | 533 SkColorTable* ctable) { |
482 Sk64 size = dst->getSize64(); | 534 SkImageInfo info; |
483 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) { |
484 return false; | 543 return false; |
485 } | 544 } |
486 | 545 |
487 void* addr = sk_malloc_flags(size.get32(), 0); // returns NULL on failure | 546 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 | 547 // since we're already allocated, we lockPixels right away |
494 dst->lockPixels(); | 548 dst->lockPixels(); |
495 return true; | 549 return true; |
496 } | 550 } |
497 | 551 |
498 /////////////////////////////////////////////////////////////////////////////// | 552 /////////////////////////////////////////////////////////////////////////////// |
499 | 553 |
500 size_t SkBitmap::getSafeSize() const { | 554 size_t SkBitmap::getSafeSize() const { |
501 // 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 |
502 // faster. The computation is meant to be identical. | 556 // faster. The computation is meant to be identical. |
(...skipping 1086 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1589 fHeight = height; | 1643 fHeight = height; |
1590 fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*)); | 1644 fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*)); |
1591 } | 1645 } |
1592 | 1646 |
1593 SkBitmap::RLEPixels::~RLEPixels() { | 1647 SkBitmap::RLEPixels::~RLEPixels() { |
1594 sk_free(fYPtrs); | 1648 sk_free(fYPtrs); |
1595 } | 1649 } |
1596 | 1650 |
1597 /////////////////////////////////////////////////////////////////////////////// | 1651 /////////////////////////////////////////////////////////////////////////////// |
1598 | 1652 |
| 1653 void SkImageInfo::unflatten(SkFlattenableReadBuffer& buffer) { |
| 1654 fWidth = buffer.read32(); |
| 1655 fHeight = buffer.read32(); |
| 1656 |
| 1657 uint32_t packed = buffer.read32(); |
| 1658 SkASSERT(0 == (packed >> 16)); |
| 1659 fAlphaType = (SkAlphaType)((packed >> 8) & 0xFF); |
| 1660 fColorType = (SkColorType)((packed >> 0) & 0xFF); |
| 1661 } |
| 1662 |
| 1663 void SkImageInfo::flatten(SkFlattenableWriteBuffer& buffer) const { |
| 1664 buffer.write32(fWidth); |
| 1665 buffer.write32(fHeight); |
| 1666 |
| 1667 SkASSERT(0 == (fAlphaType & ~0xFF)); |
| 1668 SkASSERT(0 == (fColorType & ~0xFF)); |
| 1669 uint32_t packed = (fAlphaType << 8) | fColorType; |
| 1670 buffer.write32(packed); |
| 1671 } |
| 1672 |
| 1673 /////////////////////////////////////////////////////////////////////////////// |
| 1674 |
1599 #ifdef SK_DEBUG | 1675 #ifdef SK_DEBUG |
1600 void SkBitmap::validate() const { | 1676 void SkBitmap::validate() const { |
1601 SkASSERT(fConfig < kConfigCount); | 1677 SkASSERT(fConfig < kConfigCount); |
1602 SkASSERT(fRowBytes >= (unsigned)ComputeRowBytes((Config)fConfig, fWidth)); | 1678 SkASSERT(fRowBytes >= (unsigned)ComputeRowBytes((Config)fConfig, fWidth)); |
1603 uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImm
utable_Flag; | 1679 uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImm
utable_Flag; |
1604 #ifdef SK_BUILD_FOR_ANDROID | 1680 #ifdef SK_BUILD_FOR_ANDROID |
1605 allFlags |= kHasHardwareMipMap_Flag; | 1681 allFlags |= kHasHardwareMipMap_Flag; |
1606 #endif | 1682 #endif |
1607 SkASSERT(fFlags <= allFlags); | 1683 SkASSERT(fFlags <= allFlags); |
1608 SkASSERT(fPixelLockCount >= 0); | 1684 SkASSERT(fPixelLockCount >= 0); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1654 if (NULL != uri) { | 1730 if (NULL != uri) { |
1655 str->appendf(" uri:\"%s\"", uri); | 1731 str->appendf(" uri:\"%s\"", uri); |
1656 } else { | 1732 } else { |
1657 str->appendf(" pixelref:%p", pr); | 1733 str->appendf(" pixelref:%p", pr); |
1658 } | 1734 } |
1659 } | 1735 } |
1660 | 1736 |
1661 str->append(")"); | 1737 str->append(")"); |
1662 } | 1738 } |
1663 #endif | 1739 #endif |
OLD | NEW |