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