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::asImageInfo(SkImageInfo* info) const { |
| 367 SkColorType ct; |
| 368 switch (this->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 (info) { |
| 388 info->fWidth = fWidth; |
| 389 info->fHeight = fHeight; |
| 390 info->fAlphaType = this->alphaType(); |
| 391 info->fColorType = ct; |
| 392 } |
| 393 return true; |
| 394 } |
| 395 |
366 SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, size_t offset) { | 396 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 | 397 // do this first, we that we never have a non-zero offset with a null ref |
368 if (NULL == pr) { | 398 if (NULL == pr) { |
369 offset = 0; | 399 offset = 0; |
370 } | 400 } |
371 | 401 |
372 if (fPixelRef != pr || fPixelRefOffset != offset) { | 402 if (fPixelRef != pr || fPixelRefOffset != offset) { |
373 if (fPixelRef != pr) { | 403 if (fPixelRef != pr) { |
374 this->freePixels(); | 404 this->freePixels(); |
375 SkASSERT(NULL == fPixelRef); | 405 SkASSERT(NULL == fPixelRef); |
(...skipping 30 matching lines...) Expand all Loading... |
406 bool SkBitmap::lockPixelsAreWritable() const { | 436 bool SkBitmap::lockPixelsAreWritable() const { |
407 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false; | 437 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false; |
408 } | 438 } |
409 | 439 |
410 void SkBitmap::setPixels(void* p, SkColorTable* ctable) { | 440 void SkBitmap::setPixels(void* p, SkColorTable* ctable) { |
411 if (NULL == p) { | 441 if (NULL == p) { |
412 this->setPixelRef(NULL, 0); | 442 this->setPixelRef(NULL, 0); |
413 return; | 443 return; |
414 } | 444 } |
415 | 445 |
416 Sk64 size = this->getSize64(); | 446 SkImageInfo info; |
417 SkASSERT(!size.isNeg() && size.is32()); | 447 if (!this->asImageInfo(&info)) { |
| 448 this->setPixelRef(NULL, 0); |
| 449 return; |
| 450 } |
418 | 451 |
419 this->setPixelRef(new SkMallocPixelRef(p, size.get32(), ctable, false))->unr
ef(); | 452 SkPixelRef* pr = SkMallocPixelRef::NewDirect(info, p, fRowBytes, ctable); |
| 453 if (NULL == pr) { |
| 454 this->setPixelRef(NULL, 0); |
| 455 return; |
| 456 } |
| 457 |
| 458 this->setPixelRef(pr)->unref(); |
| 459 |
420 // since we're already allocated, we lockPixels right away | 460 // since we're already allocated, we lockPixels right away |
421 this->lockPixels(); | 461 this->lockPixels(); |
422 SkDEBUGCODE(this->validate();) | 462 SkDEBUGCODE(this->validate();) |
423 } | 463 } |
424 | 464 |
425 bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) { | 465 bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) { |
426 HeapAllocator stdalloc; | 466 HeapAllocator stdalloc; |
427 | 467 |
428 if (NULL == allocator) { | 468 if (NULL == allocator) { |
429 allocator = &stdalloc; | 469 allocator = &stdalloc; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 return fPixelRef ? fPixelRef->getTexture() : NULL; | 514 return fPixelRef ? fPixelRef->getTexture() : NULL; |
475 } | 515 } |
476 | 516 |
477 /////////////////////////////////////////////////////////////////////////////// | 517 /////////////////////////////////////////////////////////////////////////////// |
478 | 518 |
479 /** 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, |
480 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. |
481 */ | 521 */ |
482 bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst, | 522 bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst, |
483 SkColorTable* ctable) { | 523 SkColorTable* ctable) { |
484 Sk64 size = dst->getSize64(); | 524 SkImageInfo info; |
485 if (size.isNeg() || !size.is32()) { | 525 if (!dst->asImageInfo(&info)) { |
| 526 // SkDebugf("unsupported config for info %d\n", dst->config()); |
| 527 return false; |
| 528 } |
| 529 |
| 530 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, dst->rowBytes(), |
| 531 ctable); |
| 532 if (NULL == pr) { |
486 return false; | 533 return false; |
487 } | 534 } |
488 | 535 |
489 void* addr = sk_malloc_flags(size.get32(), 0); // returns NULL on failure | 536 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 | 537 // since we're already allocated, we lockPixels right away |
496 dst->lockPixels(); | 538 dst->lockPixels(); |
497 return true; | 539 return true; |
498 } | 540 } |
499 | 541 |
500 /////////////////////////////////////////////////////////////////////////////// | 542 /////////////////////////////////////////////////////////////////////////////// |
501 | 543 |
502 size_t SkBitmap::getSafeSize() const { | 544 size_t SkBitmap::getSafeSize() const { |
503 // This is intended to be a size_t version of ComputeSafeSize64(), just | 545 // This is intended to be a size_t version of ComputeSafeSize64(), just |
504 // faster. The computation is meant to be identical. | 546 // faster. The computation is meant to be identical. |
(...skipping 1081 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1586 fHeight = height; | 1628 fHeight = height; |
1587 fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*)); | 1629 fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*)); |
1588 } | 1630 } |
1589 | 1631 |
1590 SkBitmap::RLEPixels::~RLEPixels() { | 1632 SkBitmap::RLEPixels::~RLEPixels() { |
1591 sk_free(fYPtrs); | 1633 sk_free(fYPtrs); |
1592 } | 1634 } |
1593 | 1635 |
1594 /////////////////////////////////////////////////////////////////////////////// | 1636 /////////////////////////////////////////////////////////////////////////////// |
1595 | 1637 |
| 1638 void SkImageInfo::read(SkFlattenableReadBuffer& buffer) { |
| 1639 fWidth = buffer.read32(); |
| 1640 fHeight = buffer.read32(); |
| 1641 |
| 1642 uint32_t packed = buffer.read32(); |
| 1643 SkASSERT(0 == (packed >> 16)); |
| 1644 fAlphaType = (SkAlphaType)((packed >> 8) & 0xFF); |
| 1645 fColorType = (SkColorType)((packed >> 0) & 0xFF); |
| 1646 } |
| 1647 |
| 1648 void SkImageInfo::write(SkFlattenableWriteBuffer& buffer) const { |
| 1649 buffer.write32(fWidth); |
| 1650 buffer.write32(fHeight); |
| 1651 |
| 1652 SkASSERT(0 == (fAlphaType & ~0xFF)); |
| 1653 SkASSERT(0 == (fColorType & ~0xFF)); |
| 1654 uint32_t packed = (fAlphaType << 8) | fColorType; |
| 1655 buffer.write32(packed); |
| 1656 } |
| 1657 |
| 1658 /////////////////////////////////////////////////////////////////////////////// |
| 1659 |
1596 #ifdef SK_DEBUG | 1660 #ifdef SK_DEBUG |
1597 void SkBitmap::validate() const { | 1661 void SkBitmap::validate() const { |
1598 SkASSERT(fConfig < kConfigCount); | 1662 SkASSERT(fConfig < kConfigCount); |
1599 SkASSERT(fRowBytes >= (unsigned)ComputeRowBytes((Config)fConfig, fWidth)); | 1663 SkASSERT(fRowBytes >= (unsigned)ComputeRowBytes((Config)fConfig, fWidth)); |
1600 uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImm
utable_Flag; | 1664 uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImm
utable_Flag; |
1601 #ifdef SK_BUILD_FOR_ANDROID | 1665 #ifdef SK_BUILD_FOR_ANDROID |
1602 allFlags |= kHasHardwareMipMap_Flag; | 1666 allFlags |= kHasHardwareMipMap_Flag; |
1603 #endif | 1667 #endif |
1604 SkASSERT(fFlags <= allFlags); | 1668 SkASSERT(fFlags <= allFlags); |
1605 SkASSERT(fPixelLockCount >= 0); | 1669 SkASSERT(fPixelLockCount >= 0); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1651 if (NULL != uri) { | 1715 if (NULL != uri) { |
1652 str->appendf(" uri:\"%s\"", uri); | 1716 str->appendf(" uri:\"%s\"", uri); |
1653 } else { | 1717 } else { |
1654 str->appendf(" pixelref:%p", pr); | 1718 str->appendf(" pixelref:%p", pr); |
1655 } | 1719 } |
1656 } | 1720 } |
1657 | 1721 |
1658 str->append(")"); | 1722 str->append(")"); |
1659 } | 1723 } |
1660 #endif | 1724 #endif |
OLD | NEW |