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 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
361 SkASSERT(0 == fPixelLockCount); | 361 SkASSERT(0 == fPixelLockCount); |
362 fPixels = NULL; | 362 fPixels = NULL; |
363 if (fColorTable) { | 363 if (fColorTable) { |
364 fColorTable->unref(); | 364 fColorTable->unref(); |
365 fColorTable = NULL; | 365 fColorTable = NULL; |
366 } | 366 } |
367 } | 367 } |
368 } | 368 } |
369 } | 369 } |
370 | 370 |
371 bool SkBitmap::asImageInfo(SkImageInfo* info) const { | |
372 SkColorType ct; | |
373 switch (this->config()) { | |
374 case kNo_Config: | |
375 case kA1_Config: | |
376 return false; | |
377 case kA8_Config: | |
378 ct = kAlpha_8_SkColorType; | |
379 break; | |
380 case kIndex8_Config: | |
381 ct = kIndex8_SkColorType; | |
382 break; | |
383 case kRGB_565_Config: | |
384 ct = kRGB_565_SkColorType; | |
385 break; | |
386 case kARGB_4444_Config: | |
387 ct = kARGB_4444_SkColorType; | |
388 break; | |
389 case kARGB_8888_Config: | |
390 ct = kPMColor_SkColorType; | |
391 break; | |
392 } | |
393 if (info) { | |
394 info->fWidth = fWidth; | |
395 info->fHeight = fHeight; | |
396 info->fAlphaType = this->alphaType(); | |
397 info->fColorType = ct; | |
398 } | |
399 return true; | |
400 } | |
401 | |
371 SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, size_t offset) { | 402 SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, size_t offset) { |
372 // do this first, we that we never have a non-zero offset with a null ref | 403 // do this first, we that we never have a non-zero offset with a null ref |
373 if (NULL == pr) { | 404 if (NULL == pr) { |
374 offset = 0; | 405 offset = 0; |
375 } | 406 } |
376 | 407 |
377 if (fPixelRef != pr || fPixelRefOffset != offset) { | 408 if (fPixelRef != pr || fPixelRefOffset != offset) { |
378 if (fPixelRef != pr) { | 409 if (fPixelRef != pr) { |
379 this->freePixels(); | 410 this->freePixels(); |
380 SkASSERT(NULL == fPixelRef); | 411 SkASSERT(NULL == fPixelRef); |
(...skipping 30 matching lines...) Expand all Loading... | |
411 bool SkBitmap::lockPixelsAreWritable() const { | 442 bool SkBitmap::lockPixelsAreWritable() const { |
412 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false; | 443 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false; |
413 } | 444 } |
414 | 445 |
415 void SkBitmap::setPixels(void* p, SkColorTable* ctable) { | 446 void SkBitmap::setPixels(void* p, SkColorTable* ctable) { |
416 if (NULL == p) { | 447 if (NULL == p) { |
417 this->setPixelRef(NULL, 0); | 448 this->setPixelRef(NULL, 0); |
418 return; | 449 return; |
419 } | 450 } |
420 | 451 |
421 Sk64 size = this->getSize64(); | 452 SkImageInfo info; |
422 SkASSERT(!size.isNeg() && size.is32()); | 453 if (!this->asImageInfo(&info)) { |
454 this->setPixelRef(NULL, 0); | |
455 return; | |
456 } | |
423 | 457 |
424 this->setPixelRef(new SkMallocPixelRef(p, size.get32(), ctable, false))->unr ef(); | 458 SkPixelRef* pr = SkMallocPixelRef::Create(info, p, fRowBytes, ctable); |
459 if (NULL == pr) { | |
460 this->setPixelRef(NULL, 0); | |
461 return; | |
462 } | |
463 | |
464 this->setPixelRef(pr)->unref(); | |
465 | |
425 // since we're already allocated, we lockPixels right away | 466 // since we're already allocated, we lockPixels right away |
426 this->lockPixels(); | 467 this->lockPixels(); |
427 SkDEBUGCODE(this->validate();) | 468 SkDEBUGCODE(this->validate();) |
428 } | 469 } |
429 | 470 |
430 bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) { | 471 bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) { |
431 HeapAllocator stdalloc; | 472 HeapAllocator stdalloc; |
432 | 473 |
433 if (NULL == allocator) { | 474 if (NULL == allocator) { |
434 allocator = &stdalloc; | 475 allocator = &stdalloc; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
479 return fPixelRef ? fPixelRef->getTexture() : NULL; | 520 return fPixelRef ? fPixelRef->getTexture() : NULL; |
480 } | 521 } |
481 | 522 |
482 /////////////////////////////////////////////////////////////////////////////// | 523 /////////////////////////////////////////////////////////////////////////////// |
483 | 524 |
484 /** We explicitly use the same allocator for our pixels that SkMask does, | 525 /** We explicitly use the same allocator for our pixels that SkMask does, |
485 so that we can freely assign memory allocated by one class to the other. | 526 so that we can freely assign memory allocated by one class to the other. |
486 */ | 527 */ |
487 bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst, | 528 bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst, |
488 SkColorTable* ctable) { | 529 SkColorTable* ctable) { |
489 Sk64 size = dst->getSize64(); | 530 SkImageInfo info; |
490 if (size.isNeg() || !size.is32()) { | 531 if (!dst->asImageInfo(&info)) { |
532 // SkDebugf("unsupported config for info %d\n", dst->config()); | |
533 return false; | |
534 } | |
535 | |
536 SkPixelRef* pr = SkMallocPixelRef::Allocate(info, dst->rowBytes(), ctable); | |
537 if (NULL == pr) { | |
491 return false; | 538 return false; |
492 } | 539 } |
493 | 540 |
494 void* addr = sk_malloc_flags(size.get32(), 0); // returns NULL on failure | 541 dst->setPixelRef(pr, 0)->unref(); |
495 if (NULL == addr) { | |
496 return false; | |
497 } | |
498 | |
499 dst->setPixelRef(new SkMallocPixelRef(addr, size.get32(), ctable))->unref(); | |
500 // since we're already allocated, we lockPixels right away | 542 // since we're already allocated, we lockPixels right away |
501 dst->lockPixels(); | 543 dst->lockPixels(); |
502 return true; | 544 return true; |
503 } | 545 } |
504 | 546 |
505 /////////////////////////////////////////////////////////////////////////////// | 547 /////////////////////////////////////////////////////////////////////////////// |
506 | 548 |
507 size_t SkBitmap::getSafeSize() const { | 549 size_t SkBitmap::getSafeSize() const { |
508 // This is intended to be a size_t version of ComputeSafeSize64(), just | 550 // This is intended to be a size_t version of ComputeSafeSize64(), just |
509 // faster. The computation is meant to be identical. | 551 // faster. The computation is meant to be identical. |
(...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1014 if (this->config() == kNo_Config) { | 1056 if (this->config() == kNo_Config) { |
1015 return false; | 1057 return false; |
1016 } | 1058 } |
1017 | 1059 |
1018 bool sameConfigs = (this->config() == dstConfig); | 1060 bool sameConfigs = (this->config() == dstConfig); |
1019 switch (dstConfig) { | 1061 switch (dstConfig) { |
1020 case kA8_Config: | 1062 case kA8_Config: |
1021 case kRGB_565_Config: | 1063 case kRGB_565_Config: |
1022 case kARGB_8888_Config: | 1064 case kARGB_8888_Config: |
1023 break; | 1065 break; |
1024 case kA1_Config: | |
1025 case kIndex8_Config: | 1066 case kIndex8_Config: |
1026 if (!sameConfigs) { | 1067 if (!sameConfigs) { |
1027 return false; | 1068 return false; |
1028 } | 1069 } |
1029 break; | 1070 break; |
1030 case kARGB_4444_Config: | 1071 case kARGB_4444_Config: |
1031 return sameConfigs || kARGB_8888_Config == this->config(); | 1072 return sameConfigs || kARGB_8888_Config == this->config(); |
1073 case kA1_Config: | |
scroggo
2013/11/20 21:04:28
This change seems to be unrelated.
reed1
2013/11/20 21:27:18
It was needed to keep unittests passing, given tha
scroggo
2013/11/20 21:35:19
sgtm. Can you add the deprecation of A1 to the cha
| |
1032 default: | 1074 default: |
1033 return false; | 1075 return false; |
1034 } | 1076 } |
1035 | 1077 |
1036 // do not copy src if srcConfig == kA1_Config while dstConfig != kA1_Config | 1078 // do not copy src if srcConfig == kA1_Config while dstConfig != kA1_Config |
1037 if (this->config() == kA1_Config && !sameConfigs) { | 1079 if (this->config() == kA1_Config && !sameConfigs) { |
1038 return false; | 1080 return false; |
1039 } | 1081 } |
1040 | 1082 |
1041 return true; | 1083 return true; |
(...skipping 667 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1709 if (NULL != uri) { | 1751 if (NULL != uri) { |
1710 str->appendf(" uri:\"%s\"", uri); | 1752 str->appendf(" uri:\"%s\"", uri); |
1711 } else { | 1753 } else { |
1712 str->appendf(" pixelref:%p", pr); | 1754 str->appendf(" pixelref:%p", pr); |
1713 } | 1755 } |
1714 } | 1756 } |
1715 | 1757 |
1716 str->append(")"); | 1758 str->append(")"); |
1717 } | 1759 } |
1718 #endif | 1760 #endif |
OLD | NEW |