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