SkBitmap.cpp revision 2cb1480ff8ae030946cb5f218f9c5cbc1e54c7a9
1 2/* 3 * Copyright 2008 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10#include "SkBitmap.h" 11#include "SkColorPriv.h" 12#include "SkDither.h" 13#include "SkFlattenable.h" 14#include "SkMallocPixelRef.h" 15#include "SkMask.h" 16#include "SkOrderedReadBuffer.h" 17#include "SkOrderedWriteBuffer.h" 18#include "SkPixelRef.h" 19#include "SkThread.h" 20#include "SkUnPreMultiply.h" 21#include "SkUtils.h" 22#include "SkPackBits.h" 23#include <new> 24 25SK_DEFINE_INST_COUNT(SkBitmap::Allocator) 26 27static bool isPos32Bits(const Sk64& value) { 28 return !value.isNeg() && value.is32(); 29} 30 31struct MipLevel { 32 void* fPixels; 33 uint32_t fRowBytes; 34 uint32_t fWidth, fHeight; 35}; 36 37struct SkBitmap::MipMap : SkNoncopyable { 38 int32_t fRefCnt; 39 int fLevelCount; 40// MipLevel fLevel[fLevelCount]; 41// Pixels[] 42 43 static MipMap* Alloc(int levelCount, size_t pixelSize) { 44 if (levelCount < 0) { 45 return NULL; 46 } 47 Sk64 size; 48 size.setMul(levelCount + 1, sizeof(MipLevel)); 49 size.add(sizeof(MipMap)); 50 size.add(SkToS32(pixelSize)); 51 if (!isPos32Bits(size)) { 52 return NULL; 53 } 54 MipMap* mm = (MipMap*)sk_malloc_throw(size.get32()); 55 mm->fRefCnt = 1; 56 mm->fLevelCount = levelCount; 57 return mm; 58 } 59 60 const MipLevel* levels() const { return (const MipLevel*)(this + 1); } 61 MipLevel* levels() { return (MipLevel*)(this + 1); } 62 63 const void* pixels() const { return levels() + fLevelCount; } 64 void* pixels() { return levels() + fLevelCount; } 65 66 void ref() { 67 if (SK_MaxS32 == sk_atomic_inc(&fRefCnt)) { 68 sk_throw(); 69 } 70 } 71 void unref() { 72 SkASSERT(fRefCnt > 0); 73 if (sk_atomic_dec(&fRefCnt) == 1) { 74 sk_free(this); 75 } 76 } 77}; 78 79/////////////////////////////////////////////////////////////////////////////// 80/////////////////////////////////////////////////////////////////////////////// 81 82SkBitmap::SkBitmap() { 83 sk_bzero(this, sizeof(*this)); 84} 85 86SkBitmap::SkBitmap(const SkBitmap& src) { 87 SkDEBUGCODE(src.validate();) 88 sk_bzero(this, sizeof(*this)); 89 *this = src; 90 SkDEBUGCODE(this->validate();) 91} 92 93SkBitmap::~SkBitmap() { 94 SkDEBUGCODE(this->validate();) 95 this->freePixels(); 96} 97 98SkBitmap& SkBitmap::operator=(const SkBitmap& src) { 99 if (this != &src) { 100 this->freePixels(); 101 memcpy(this, &src, sizeof(src)); 102 103 // inc src reference counts 104 SkSafeRef(src.fPixelRef); 105 SkSafeRef(src.fMipMap); 106 107 // we reset our locks if we get blown away 108 fPixelLockCount = 0; 109 110 /* The src could be in 3 states 111 1. no pixelref, in which case we just copy/ref the pixels/ctable 112 2. unlocked pixelref, pixels/ctable should be null 113 3. locked pixelref, we should lock the ref again ourselves 114 */ 115 if (NULL == fPixelRef) { 116 // leave fPixels as it is 117 SkSafeRef(fColorTable); // ref the user's ctable if present 118 } else { // we have a pixelref, so pixels/ctable reflect it 119 // ignore the values from the memcpy 120 fPixels = NULL; 121 fColorTable = NULL; 122 // Note that what to for genID is somewhat arbitrary. We have no 123 // way to track changes to raw pixels across multiple SkBitmaps. 124 // Would benefit from an SkRawPixelRef type created by 125 // setPixels. 126 // Just leave the memcpy'ed one but they'll get out of sync 127 // as soon either is modified. 128 } 129 } 130 131 SkDEBUGCODE(this->validate();) 132 return *this; 133} 134 135void SkBitmap::swap(SkBitmap& other) { 136 SkTSwap(fColorTable, other.fColorTable); 137 SkTSwap(fPixelRef, other.fPixelRef); 138 SkTSwap(fPixelRefOffset, other.fPixelRefOffset); 139 SkTSwap(fPixelLockCount, other.fPixelLockCount); 140 SkTSwap(fMipMap, other.fMipMap); 141 SkTSwap(fPixels, other.fPixels); 142 SkTSwap(fRowBytes, other.fRowBytes); 143 SkTSwap(fWidth, other.fWidth); 144 SkTSwap(fHeight, other.fHeight); 145 SkTSwap(fConfig, other.fConfig); 146 SkTSwap(fFlags, other.fFlags); 147 SkTSwap(fBytesPerPixel, other.fBytesPerPixel); 148 149 SkDEBUGCODE(this->validate();) 150} 151 152void SkBitmap::reset() { 153 this->freePixels(); 154 sk_bzero(this, sizeof(*this)); 155} 156 157int SkBitmap::ComputeBytesPerPixel(SkBitmap::Config config) { 158 int bpp; 159 switch (config) { 160 case kNo_Config: 161 case kA1_Config: 162 bpp = 0; // not applicable 163 break; 164 case kA8_Config: 165 case kIndex8_Config: 166 bpp = 1; 167 break; 168 case kRGB_565_Config: 169 case kARGB_4444_Config: 170 bpp = 2; 171 break; 172 case kARGB_8888_Config: 173 bpp = 4; 174 break; 175 default: 176 SkDEBUGFAIL("unknown config"); 177 bpp = 0; // error 178 break; 179 } 180 return bpp; 181} 182 183size_t SkBitmap::ComputeRowBytes(Config c, int width) { 184 if (width < 0) { 185 return 0; 186 } 187 188 Sk64 rowBytes; 189 rowBytes.setZero(); 190 191 switch (c) { 192 case kNo_Config: 193 break; 194 case kA1_Config: 195 rowBytes.set(width); 196 rowBytes.add(7); 197 rowBytes.shiftRight(3); 198 break; 199 case kA8_Config: 200 case kIndex8_Config: 201 rowBytes.set(width); 202 break; 203 case kRGB_565_Config: 204 case kARGB_4444_Config: 205 rowBytes.set(width); 206 rowBytes.shiftLeft(1); 207 break; 208 case kARGB_8888_Config: 209 rowBytes.set(width); 210 rowBytes.shiftLeft(2); 211 break; 212 default: 213 SkDEBUGFAIL("unknown config"); 214 break; 215 } 216 return isPos32Bits(rowBytes) ? rowBytes.get32() : 0; 217} 218 219Sk64 SkBitmap::ComputeSize64(Config c, int width, int height) { 220 Sk64 size; 221 size.setMul(SkToS32(SkBitmap::ComputeRowBytes(c, width)), height); 222 return size; 223} 224 225size_t SkBitmap::ComputeSize(Config c, int width, int height) { 226 Sk64 size = SkBitmap::ComputeSize64(c, width, height); 227 return isPos32Bits(size) ? size.get32() : 0; 228} 229 230Sk64 SkBitmap::ComputeSafeSize64(Config config, 231 uint32_t width, 232 uint32_t height, 233 size_t rowBytes) { 234 Sk64 safeSize; 235 safeSize.setZero(); 236 if (height > 0) { 237 // TODO: Handle the case where the return value from 238 // ComputeRowBytes is more than 31 bits. 239 safeSize.set(SkToS32(ComputeRowBytes(config, width))); 240 Sk64 sizeAllButLastRow; 241 sizeAllButLastRow.setMul(height - 1, SkToS32(rowBytes)); 242 safeSize.add(sizeAllButLastRow); 243 } 244 SkASSERT(!safeSize.isNeg()); 245 return safeSize; 246} 247 248size_t SkBitmap::ComputeSafeSize(Config config, 249 uint32_t width, 250 uint32_t height, 251 size_t rowBytes) { 252 Sk64 safeSize = ComputeSafeSize64(config, width, height, rowBytes); 253 return (safeSize.is32() ? safeSize.get32() : 0); 254} 255 256void SkBitmap::getBounds(SkRect* bounds) const { 257 SkASSERT(bounds); 258 bounds->set(0, 0, 259 SkIntToScalar(fWidth), SkIntToScalar(fHeight)); 260} 261 262void SkBitmap::getBounds(SkIRect* bounds) const { 263 SkASSERT(bounds); 264 bounds->set(0, 0, fWidth, fHeight); 265} 266 267/////////////////////////////////////////////////////////////////////////////// 268 269void SkBitmap::setConfig(Config c, int width, int height, size_t rowBytes) { 270 this->freePixels(); 271 272 if ((width | height) < 0) { 273 goto err; 274 } 275 276 if (rowBytes == 0) { 277 rowBytes = SkBitmap::ComputeRowBytes(c, width); 278 if (0 == rowBytes && kNo_Config != c) { 279 goto err; 280 } 281 } 282 283 fConfig = SkToU8(c); 284 fWidth = width; 285 fHeight = height; 286 fRowBytes = SkToU32(rowBytes); 287 288 fBytesPerPixel = (uint8_t)ComputeBytesPerPixel(c); 289 290 SkDEBUGCODE(this->validate();) 291 return; 292 293 // if we got here, we had an error, so we reset the bitmap to empty 294err: 295 this->reset(); 296} 297 298void SkBitmap::updatePixelsFromRef() const { 299 if (NULL != fPixelRef) { 300 if (fPixelLockCount > 0) { 301 SkASSERT(fPixelRef->isLocked()); 302 303 void* p = fPixelRef->pixels(); 304 if (NULL != p) { 305 p = (char*)p + fPixelRefOffset; 306 } 307 fPixels = p; 308 SkRefCnt_SafeAssign(fColorTable, fPixelRef->colorTable()); 309 } else { 310 SkASSERT(0 == fPixelLockCount); 311 fPixels = NULL; 312 if (fColorTable) { 313 fColorTable->unref(); 314 fColorTable = NULL; 315 } 316 } 317 } 318} 319 320SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, size_t offset) { 321 // do this first, we that we never have a non-zero offset with a null ref 322 if (NULL == pr) { 323 offset = 0; 324 } 325 326 if (fPixelRef != pr || fPixelRefOffset != offset) { 327 if (fPixelRef != pr) { 328 this->freePixels(); 329 SkASSERT(NULL == fPixelRef); 330 331 SkSafeRef(pr); 332 fPixelRef = pr; 333 } 334 fPixelRefOffset = offset; 335 this->updatePixelsFromRef(); 336 } 337 338 SkDEBUGCODE(this->validate();) 339 return pr; 340} 341 342void SkBitmap::lockPixels() const { 343 if (NULL != fPixelRef && 1 == ++fPixelLockCount) { 344 fPixelRef->lockPixels(); 345 this->updatePixelsFromRef(); 346 } 347 SkDEBUGCODE(this->validate();) 348} 349 350void SkBitmap::unlockPixels() const { 351 SkASSERT(NULL == fPixelRef || fPixelLockCount > 0); 352 353 if (NULL != fPixelRef && 0 == --fPixelLockCount) { 354 fPixelRef->unlockPixels(); 355 this->updatePixelsFromRef(); 356 } 357 SkDEBUGCODE(this->validate();) 358} 359 360bool SkBitmap::lockPixelsAreWritable() const { 361 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false; 362} 363 364void SkBitmap::setPixels(void* p, SkColorTable* ctable) { 365 if (NULL == p) { 366 this->setPixelRef(NULL, 0); 367 return; 368 } 369 370 Sk64 size = this->getSize64(); 371 SkASSERT(!size.isNeg() && size.is32()); 372 373 this->setPixelRef(new SkMallocPixelRef(p, size.get32(), ctable, false))->unref(); 374 // since we're already allocated, we lockPixels right away 375 this->lockPixels(); 376 SkDEBUGCODE(this->validate();) 377} 378 379bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) { 380 HeapAllocator stdalloc; 381 382 if (NULL == allocator) { 383 allocator = &stdalloc; 384 } 385 return allocator->allocPixelRef(this, ctable); 386} 387 388void SkBitmap::freePixels() { 389 // if we're gonna free the pixels, we certainly need to free the mipmap 390 this->freeMipMap(); 391 392 if (fColorTable) { 393 fColorTable->unref(); 394 fColorTable = NULL; 395 } 396 397 if (NULL != fPixelRef) { 398 if (fPixelLockCount > 0) { 399 fPixelRef->unlockPixels(); 400 } 401 fPixelRef->unref(); 402 fPixelRef = NULL; 403 fPixelRefOffset = 0; 404 } 405 fPixelLockCount = 0; 406 fPixels = NULL; 407} 408 409void SkBitmap::freeMipMap() { 410 if (fMipMap) { 411 fMipMap->unref(); 412 fMipMap = NULL; 413 } 414} 415 416uint32_t SkBitmap::getGenerationID() const { 417 return (fPixelRef) ? fPixelRef->getGenerationID() : 0; 418} 419 420void SkBitmap::notifyPixelsChanged() const { 421 SkASSERT(!this->isImmutable()); 422 if (fPixelRef) { 423 fPixelRef->notifyPixelsChanged(); 424 } 425} 426 427SkGpuTexture* SkBitmap::getTexture() const { 428 return fPixelRef ? fPixelRef->getTexture() : NULL; 429} 430 431/////////////////////////////////////////////////////////////////////////////// 432 433/** We explicitly use the same allocator for our pixels that SkMask does, 434 so that we can freely assign memory allocated by one class to the other. 435 */ 436bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst, 437 SkColorTable* ctable) { 438 Sk64 size = dst->getSize64(); 439 if (size.isNeg() || !size.is32()) { 440 return false; 441 } 442 443 void* addr = sk_malloc_flags(size.get32(), 0); // returns NULL on failure 444 if (NULL == addr) { 445 return false; 446 } 447 448 dst->setPixelRef(new SkMallocPixelRef(addr, size.get32(), ctable))->unref(); 449 // since we're already allocated, we lockPixels right away 450 dst->lockPixels(); 451 return true; 452} 453 454/////////////////////////////////////////////////////////////////////////////// 455 456size_t SkBitmap::getSafeSize() const { 457 // This is intended to be a size_t version of ComputeSafeSize64(), just 458 // faster. The computation is meant to be identical. 459 return (fHeight ? ((fHeight - 1) * fRowBytes) + 460 ComputeRowBytes(getConfig(), fWidth): 0); 461} 462 463Sk64 SkBitmap::getSafeSize64() const { 464 return ComputeSafeSize64(getConfig(), fWidth, fHeight, fRowBytes); 465} 466 467bool SkBitmap::copyPixelsTo(void* const dst, size_t dstSize, 468 size_t dstRowBytes, bool preserveDstPad) const { 469 470 if (0 == dstRowBytes) { 471 dstRowBytes = fRowBytes; 472 } 473 474 if (dstRowBytes < ComputeRowBytes(getConfig(), fWidth) || 475 dst == NULL || (getPixels() == NULL && pixelRef() == NULL)) 476 return false; 477 478 if (!preserveDstPad && static_cast<uint32_t>(dstRowBytes) == fRowBytes) { 479 size_t safeSize = getSafeSize(); 480 if (safeSize > dstSize || safeSize == 0) 481 return false; 482 else { 483 SkAutoLockPixels lock(*this); 484 // This implementation will write bytes beyond the end of each row, 485 // excluding the last row, if the bitmap's stride is greater than 486 // strictly required by the current config. 487 memcpy(dst, getPixels(), safeSize); 488 489 return true; 490 } 491 } else { 492 // If destination has different stride than us, then copy line by line. 493 if (ComputeSafeSize(getConfig(), fWidth, fHeight, dstRowBytes) > 494 dstSize) 495 return false; 496 else { 497 // Just copy what we need on each line. 498 size_t rowBytes = ComputeRowBytes(getConfig(), fWidth); 499 SkAutoLockPixels lock(*this); 500 const uint8_t* srcP = reinterpret_cast<const uint8_t*>(getPixels()); 501 uint8_t* dstP = reinterpret_cast<uint8_t*>(dst); 502 for (uint32_t row = 0; row < fHeight; 503 row++, srcP += fRowBytes, dstP += dstRowBytes) { 504 memcpy(dstP, srcP, rowBytes); 505 } 506 507 return true; 508 } 509 } 510} 511 512/////////////////////////////////////////////////////////////////////////////// 513 514bool SkBitmap::isImmutable() const { 515 return fPixelRef ? fPixelRef->isImmutable() : 516 fFlags & kImageIsImmutable_Flag; 517} 518 519void SkBitmap::setImmutable() { 520 if (fPixelRef) { 521 fPixelRef->setImmutable(); 522 } else { 523 fFlags |= kImageIsImmutable_Flag; 524 } 525} 526 527bool SkBitmap::isOpaque() const { 528 switch (fConfig) { 529 case kNo_Config: 530 return true; 531 532 case kA1_Config: 533 case kA8_Config: 534 case kARGB_4444_Config: 535 case kARGB_8888_Config: 536 return (fFlags & kImageIsOpaque_Flag) != 0; 537 538 case kIndex8_Config: { 539 uint32_t flags = 0; 540 541 this->lockPixels(); 542 // if lockPixels failed, we may not have a ctable ptr 543 if (fColorTable) { 544 flags = fColorTable->getFlags(); 545 } 546 this->unlockPixels(); 547 548 return (flags & SkColorTable::kColorsAreOpaque_Flag) != 0; 549 } 550 551 case kRGB_565_Config: 552 return true; 553 554 default: 555 SkDEBUGFAIL("unknown bitmap config pased to isOpaque"); 556 return false; 557 } 558} 559 560void SkBitmap::setIsOpaque(bool isOpaque) { 561 /* we record this regardless of fConfig, though it is ignored in 562 isOpaque() for configs that can't support per-pixel alpha. 563 */ 564 if (isOpaque) { 565 fFlags |= kImageIsOpaque_Flag; 566 } else { 567 fFlags &= ~kImageIsOpaque_Flag; 568 } 569} 570 571bool SkBitmap::isVolatile() const { 572 return (fFlags & kImageIsVolatile_Flag) != 0; 573} 574 575void SkBitmap::setIsVolatile(bool isVolatile) { 576 if (isVolatile) { 577 fFlags |= kImageIsVolatile_Flag; 578 } else { 579 fFlags &= ~kImageIsVolatile_Flag; 580 } 581} 582 583void* SkBitmap::getAddr(int x, int y) const { 584 SkASSERT((unsigned)x < (unsigned)this->width()); 585 SkASSERT((unsigned)y < (unsigned)this->height()); 586 587 char* base = (char*)this->getPixels(); 588 if (base) { 589 base += y * this->rowBytes(); 590 switch (this->config()) { 591 case SkBitmap::kARGB_8888_Config: 592 base += x << 2; 593 break; 594 case SkBitmap::kARGB_4444_Config: 595 case SkBitmap::kRGB_565_Config: 596 base += x << 1; 597 break; 598 case SkBitmap::kA8_Config: 599 case SkBitmap::kIndex8_Config: 600 base += x; 601 break; 602 case SkBitmap::kA1_Config: 603 base += x >> 3; 604 break; 605 default: 606 SkDEBUGFAIL("Can't return addr for config"); 607 base = NULL; 608 break; 609 } 610 } 611 return base; 612} 613 614SkColor SkBitmap::getColor(int x, int y) const { 615 SkASSERT((unsigned)x < (unsigned)this->width()); 616 SkASSERT((unsigned)y < (unsigned)this->height()); 617 618 switch (this->config()) { 619 case SkBitmap::kA1_Config: { 620 uint8_t* addr = this->getAddr1(x, y); 621 uint8_t mask = 1 << (7 - (x % 8)); 622 if (addr[0] & mask) { 623 return SK_ColorBLACK; 624 } else { 625 return 0; 626 } 627 } 628 case SkBitmap::kA8_Config: { 629 uint8_t* addr = this->getAddr8(x, y); 630 return SkColorSetA(0, addr[0]); 631 } 632 case SkBitmap::kIndex8_Config: { 633 SkPMColor c = this->getIndex8Color(x, y); 634 return SkUnPreMultiply::PMColorToColor(c); 635 } 636 case SkBitmap::kRGB_565_Config: { 637 uint16_t* addr = this->getAddr16(x, y); 638 return SkPixel16ToColor(addr[0]); 639 } 640 case SkBitmap::kARGB_4444_Config: { 641 uint16_t* addr = this->getAddr16(x, y); 642 SkPMColor c = SkPixel4444ToPixel32(addr[0]); 643 return SkUnPreMultiply::PMColorToColor(c); 644 } 645 case SkBitmap::kARGB_8888_Config: { 646 uint32_t* addr = this->getAddr32(x, y); 647 return SkUnPreMultiply::PMColorToColor(addr[0]); 648 } 649 case kNo_Config: 650 SkASSERT(false); 651 return 0; 652 } 653 SkASSERT(false); // Not reached. 654 return 0; 655} 656 657bool SkBitmap::ComputeIsOpaque(const SkBitmap& bm) { 658 SkAutoLockPixels alp(bm); 659 if (!bm.getPixels()) { 660 return false; 661 } 662 663 const int height = bm.height(); 664 const int width = bm.width(); 665 666 switch (bm.config()) { 667 case SkBitmap::kA1_Config: { 668 // TODO 669 } break; 670 case SkBitmap::kA8_Config: { 671 unsigned a = 0xFF; 672 for (int y = 0; y < height; ++y) { 673 const uint8_t* row = bm.getAddr8(0, y); 674 for (int x = 0; x < width; ++x) { 675 a &= row[x]; 676 } 677 if (0xFF != a) { 678 return false; 679 } 680 } 681 return true; 682 } break; 683 case SkBitmap::kIndex8_Config: { 684 SkAutoLockColors alc(bm); 685 const SkPMColor* table = alc.colors(); 686 if (!table) { 687 return false; 688 } 689 SkPMColor c = (SkPMColor)~0; 690 for (int i = bm.getColorTable()->count() - 1; i >= 0; --i) { 691 c &= table[i]; 692 } 693 return 0xFF == SkGetPackedA32(c); 694 } break; 695 case SkBitmap::kRGB_565_Config: 696 return true; 697 break; 698 case SkBitmap::kARGB_4444_Config: { 699 unsigned c = 0xFFFF; 700 for (int y = 0; y < height; ++y) { 701 const SkPMColor16* row = bm.getAddr16(0, y); 702 for (int x = 0; x < width; ++x) { 703 c &= row[x]; 704 } 705 if (0xF != SkGetPackedA4444(c)) { 706 return false; 707 } 708 } 709 return true; 710 } break; 711 case SkBitmap::kARGB_8888_Config: { 712 SkPMColor c = (SkPMColor)~0; 713 for (int y = 0; y < height; ++y) { 714 const SkPMColor* row = bm.getAddr32(0, y); 715 for (int x = 0; x < width; ++x) { 716 c &= row[x]; 717 } 718 if (0xFF != SkGetPackedA32(c)) { 719 return false; 720 } 721 } 722 return true; 723 } 724 default: 725 break; 726 } 727 return false; 728} 729 730 731/////////////////////////////////////////////////////////////////////////////// 732/////////////////////////////////////////////////////////////////////////////// 733 734static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) { 735 unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) | 736 (SkR32To4444(r) << SK_R4444_SHIFT) | 737 (SkG32To4444(g) << SK_G4444_SHIFT) | 738 (SkB32To4444(b) << SK_B4444_SHIFT); 739 return SkToU16(pixel); 740} 741 742void SkBitmap::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const { 743 SkDEBUGCODE(this->validate();) 744 745 if (0 == fWidth || 0 == fHeight || 746 kNo_Config == fConfig || kIndex8_Config == fConfig) { 747 return; 748 } 749 750 SkAutoLockPixels alp(*this); 751 // perform this check after the lock call 752 if (!this->readyToDraw()) { 753 return; 754 } 755 756 int height = fHeight; 757 const int width = fWidth; 758 const int rowBytes = fRowBytes; 759 760 // make rgb premultiplied 761 if (255 != a) { 762 r = SkAlphaMul(r, a); 763 g = SkAlphaMul(g, a); 764 b = SkAlphaMul(b, a); 765 } 766 767 switch (fConfig) { 768 case kA1_Config: { 769 uint8_t* p = (uint8_t*)fPixels; 770 const int count = (width + 7) >> 3; 771 a = (a >> 7) ? 0xFF : 0; 772 SkASSERT(count <= rowBytes); 773 while (--height >= 0) { 774 memset(p, a, count); 775 p += rowBytes; 776 } 777 break; 778 } 779 case kA8_Config: { 780 uint8_t* p = (uint8_t*)fPixels; 781 while (--height >= 0) { 782 memset(p, a, width); 783 p += rowBytes; 784 } 785 break; 786 } 787 case kARGB_4444_Config: 788 case kRGB_565_Config: { 789 uint16_t* p = (uint16_t*)fPixels; 790 uint16_t v; 791 792 if (kARGB_4444_Config == fConfig) { 793 v = pack_8888_to_4444(a, r, g, b); 794 } else { 795 v = SkPackRGB16(r >> (8 - SK_R16_BITS), 796 g >> (8 - SK_G16_BITS), 797 b >> (8 - SK_B16_BITS)); 798 } 799 while (--height >= 0) { 800 sk_memset16(p, v, width); 801 p = (uint16_t*)((char*)p + rowBytes); 802 } 803 break; 804 } 805 case kARGB_8888_Config: { 806 uint32_t* p = (uint32_t*)fPixels; 807 uint32_t v = SkPackARGB32(a, r, g, b); 808 809 while (--height >= 0) { 810 sk_memset32(p, v, width); 811 p = (uint32_t*)((char*)p + rowBytes); 812 } 813 break; 814 } 815 } 816 817 this->notifyPixelsChanged(); 818} 819 820////////////////////////////////////////////////////////////////////////////////////// 821////////////////////////////////////////////////////////////////////////////////////// 822 823#define SUB_OFFSET_FAILURE ((size_t)-1) 824 825/** 826 * Based on the Config and rowBytes() of bm, return the offset into an SkPixelRef of the pixel at 827 * (x, y). 828 * Note that the SkPixelRef does not need to be set yet. deepCopyTo takes advantage of this fact. 829 * Also note that (x, y) may be outside the range of (0 - width(), 0 - height()), so long as it is 830 * within the bounds of the SkPixelRef being used. 831 */ 832static size_t get_sub_offset(const SkBitmap& bm, int x, int y) { 833 switch (bm.getConfig()) { 834 case SkBitmap::kA8_Config: 835 case SkBitmap:: kIndex8_Config: 836 // x is fine as is for the calculation 837 break; 838 839 case SkBitmap::kRGB_565_Config: 840 case SkBitmap::kARGB_4444_Config: 841 x <<= 1; 842 break; 843 844 case SkBitmap::kARGB_8888_Config: 845 x <<= 2; 846 break; 847 848 case SkBitmap::kNo_Config: 849 case SkBitmap::kA1_Config: 850 default: 851 return SUB_OFFSET_FAILURE; 852 } 853 return y * bm.rowBytes() + x; 854} 855 856/** 857 * Using the pixelRefOffset(), rowBytes(), and Config of bm, determine the (x, y) coordinate of the 858 * upper left corner of bm relative to its SkPixelRef. 859 * x and y must be non-NULL. 860 */ 861bool get_upper_left_from_offset(SkBitmap::Config config, size_t offset, size_t rowBytes, 862 int32_t* x, int32_t* y); 863bool get_upper_left_from_offset(SkBitmap::Config config, size_t offset, size_t rowBytes, 864 int32_t* x, int32_t* y) { 865 SkASSERT(x != NULL && y != NULL); 866 if (0 == offset) { 867 *x = *y = 0; 868 return true; 869 } 870 // Use integer division to find the correct y position. 871 *y = SkToS32(offset / rowBytes); 872 // The remainder will be the x position, after we reverse get_sub_offset. 873 *x = SkToS32(offset % rowBytes); 874 switch (config) { 875 case SkBitmap::kA8_Config: 876 // Fall through. 877 case SkBitmap::kIndex8_Config: 878 // x is unmodified 879 break; 880 881 case SkBitmap::kRGB_565_Config: 882 // Fall through. 883 case SkBitmap::kARGB_4444_Config: 884 *x >>= 1; 885 break; 886 887 case SkBitmap::kARGB_8888_Config: 888 *x >>= 2; 889 break; 890 891 case SkBitmap::kNo_Config: 892 // Fall through. 893 case SkBitmap::kA1_Config: 894 // Fall through. 895 default: 896 return false; 897 } 898 return true; 899} 900 901static bool get_upper_left_from_offset(const SkBitmap& bm, int32_t* x, int32_t* y) { 902 return get_upper_left_from_offset(bm.config(), bm.pixelRefOffset(), bm.rowBytes(), x, y); 903} 904 905bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const { 906 SkDEBUGCODE(this->validate();) 907 908 if (NULL == result || NULL == fPixelRef) { 909 return false; // no src pixels 910 } 911 912 SkIRect srcRect, r; 913 srcRect.set(0, 0, this->width(), this->height()); 914 if (!r.intersect(srcRect, subset)) { 915 return false; // r is empty (i.e. no intersection) 916 } 917 918 if (fPixelRef->getTexture() != NULL) { 919 // Do a deep copy 920 SkPixelRef* pixelRef = fPixelRef->deepCopy(this->config(), &subset); 921 if (pixelRef != NULL) { 922 SkBitmap dst; 923 dst.setConfig(this->config(), subset.width(), subset.height()); 924 dst.setIsVolatile(this->isVolatile()); 925 dst.setIsOpaque(this->isOpaque()); 926 dst.setPixelRef(pixelRef)->unref(); 927 SkDEBUGCODE(dst.validate()); 928 result->swap(dst); 929 return true; 930 } 931 } 932 933 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have 934 // exited above. 935 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width())); 936 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height())); 937 938 size_t offset = get_sub_offset(*this, r.fLeft, r.fTop); 939 if (SUB_OFFSET_FAILURE == offset) { 940 return false; // config not supported 941 } 942 943 SkBitmap dst; 944 dst.setConfig(this->config(), r.width(), r.height(), this->rowBytes()); 945 dst.setIsVolatile(this->isVolatile()); 946 dst.setIsOpaque(this->isOpaque()); 947 948 if (fPixelRef) { 949 // share the pixelref with a custom offset 950 dst.setPixelRef(fPixelRef, fPixelRefOffset + offset); 951 } 952 SkDEBUGCODE(dst.validate();) 953 954 // we know we're good, so commit to result 955 result->swap(dst); 956 return true; 957} 958 959/////////////////////////////////////////////////////////////////////////////// 960 961#include "SkCanvas.h" 962#include "SkPaint.h" 963 964bool SkBitmap::canCopyTo(Config dstConfig) const { 965 if (this->getConfig() == kNo_Config) { 966 return false; 967 } 968 969 bool sameConfigs = (this->config() == dstConfig); 970 switch (dstConfig) { 971 case kA8_Config: 972 case kRGB_565_Config: 973 case kARGB_8888_Config: 974 break; 975 case kA1_Config: 976 case kIndex8_Config: 977 case kARGB_4444_Config: 978 if (!sameConfigs) { 979 return false; 980 } 981 break; 982 default: 983 return false; 984 } 985 986 // do not copy src if srcConfig == kA1_Config while dstConfig != kA1_Config 987 if (this->getConfig() == kA1_Config && !sameConfigs) { 988 return false; 989 } 990 991 return true; 992} 993 994bool SkBitmap::copyTo(SkBitmap* dst, Config dstConfig, Allocator* alloc) const { 995 if (!this->canCopyTo(dstConfig)) { 996 return false; 997 } 998 999 // if we have a texture, first get those pixels 1000 SkBitmap tmpSrc; 1001 const SkBitmap* src = this; 1002 1003 if (fPixelRef) { 1004 SkIRect subset; 1005 if (get_upper_left_from_offset(*this, &subset.fLeft, &subset.fTop)) { 1006 subset.fRight = subset.fLeft + fWidth; 1007 subset.fBottom = subset.fTop + fHeight; 1008 if (fPixelRef->readPixels(&tmpSrc, &subset)) { 1009 SkASSERT(tmpSrc.width() == this->width()); 1010 SkASSERT(tmpSrc.height() == this->height()); 1011 1012 // did we get lucky and we can just return tmpSrc? 1013 if (tmpSrc.config() == dstConfig && NULL == alloc) { 1014 dst->swap(tmpSrc); 1015 if (dst->pixelRef() && this->config() == dstConfig) { 1016 dst->pixelRef()->fGenerationID = fPixelRef->getGenerationID(); 1017 } 1018 return true; 1019 } 1020 1021 // fall through to the raster case 1022 src = &tmpSrc; 1023 } 1024 } 1025 } 1026 1027 // we lock this now, since we may need its colortable 1028 SkAutoLockPixels srclock(*src); 1029 if (!src->readyToDraw()) { 1030 return false; 1031 } 1032 1033 SkBitmap tmpDst; 1034 tmpDst.setConfig(dstConfig, src->width(), src->height()); 1035 1036 // allocate colortable if srcConfig == kIndex8_Config 1037 SkColorTable* ctable = (dstConfig == kIndex8_Config) ? 1038 new SkColorTable(*src->getColorTable()) : NULL; 1039 SkAutoUnref au(ctable); 1040 if (!tmpDst.allocPixels(alloc, ctable)) { 1041 return false; 1042 } 1043 1044 if (!tmpDst.readyToDraw()) { 1045 // allocator/lock failed 1046 return false; 1047 } 1048 1049 /* do memcpy for the same configs cases, else use drawing 1050 */ 1051 if (src->config() == dstConfig) { 1052 if (tmpDst.getSize() == src->getSize()) { 1053 memcpy(tmpDst.getPixels(), src->getPixels(), src->getSafeSize()); 1054 SkPixelRef* pixelRef = tmpDst.pixelRef(); 1055 if (pixelRef != NULL) { 1056 pixelRef->fGenerationID = this->getGenerationID(); 1057 } 1058 } else { 1059 const char* srcP = reinterpret_cast<const char*>(src->getPixels()); 1060 char* dstP = reinterpret_cast<char*>(tmpDst.getPixels()); 1061 // to be sure we don't read too much, only copy our logical pixels 1062 size_t bytesToCopy = tmpDst.width() * tmpDst.bytesPerPixel(); 1063 for (int y = 0; y < tmpDst.height(); y++) { 1064 memcpy(dstP, srcP, bytesToCopy); 1065 srcP += src->rowBytes(); 1066 dstP += tmpDst.rowBytes(); 1067 } 1068 } 1069 } else { 1070 // if the src has alpha, we have to clear the dst first 1071 if (!src->isOpaque()) { 1072 tmpDst.eraseColor(SK_ColorTRANSPARENT); 1073 } 1074 1075 SkCanvas canvas(tmpDst); 1076 SkPaint paint; 1077 1078 paint.setDither(true); 1079 canvas.drawBitmap(*src, 0, 0, &paint); 1080 } 1081 1082 tmpDst.setIsOpaque(src->isOpaque()); 1083 1084 dst->swap(tmpDst); 1085 return true; 1086} 1087 1088bool SkBitmap::deepCopyTo(SkBitmap* dst, Config dstConfig) const { 1089 if (!this->canCopyTo(dstConfig)) { 1090 return false; 1091 } 1092 1093 // If we have a PixelRef, and it supports deep copy, use it. 1094 // Currently supported only by texture-backed bitmaps. 1095 if (fPixelRef) { 1096 SkPixelRef* pixelRef = fPixelRef->deepCopy(dstConfig); 1097 if (pixelRef) { 1098 uint32_t rowBytes; 1099 if (dstConfig == fConfig) { 1100 pixelRef->fGenerationID = fPixelRef->getGenerationID(); 1101 // Use the same rowBytes as the original. 1102 rowBytes = fRowBytes; 1103 } else { 1104 // With the new config, an appropriate fRowBytes will be computed by setConfig. 1105 rowBytes = 0; 1106 } 1107 dst->setConfig(dstConfig, fWidth, fHeight, rowBytes); 1108 1109 size_t pixelRefOffset; 1110 if (0 == fPixelRefOffset || dstConfig == fConfig) { 1111 // Use the same offset as the original. 1112 pixelRefOffset = fPixelRefOffset; 1113 } else { 1114 // Find the correct offset in the new config. This needs to be done after calling 1115 // setConfig so dst's fConfig and fRowBytes have been set properly. 1116 int32_t x, y; 1117 if (!get_upper_left_from_offset(*this, &x, &y)) { 1118 return false; 1119 } 1120 pixelRefOffset = get_sub_offset(*dst, x, y); 1121 if (SUB_OFFSET_FAILURE == pixelRefOffset) { 1122 return false; 1123 } 1124 } 1125 dst->setPixelRef(pixelRef, pixelRefOffset)->unref(); 1126 return true; 1127 } 1128 } 1129 1130 if (this->getTexture()) { 1131 return false; 1132 } else { 1133 return this->copyTo(dst, dstConfig, NULL); 1134 } 1135} 1136 1137/////////////////////////////////////////////////////////////////////////////// 1138/////////////////////////////////////////////////////////////////////////////// 1139 1140static void downsampleby2_proc32(SkBitmap* dst, int x, int y, 1141 const SkBitmap& src) { 1142 x <<= 1; 1143 y <<= 1; 1144 const SkPMColor* p = src.getAddr32(x, y); 1145 const SkPMColor* baseP = p; 1146 SkPMColor c, ag, rb; 1147 1148 c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF; 1149 if (x < src.width() - 1) { 1150 p += 1; 1151 } 1152 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF; 1153 1154 p = baseP; 1155 if (y < src.height() - 1) { 1156 p += src.rowBytes() >> 2; 1157 } 1158 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF; 1159 if (x < src.width() - 1) { 1160 p += 1; 1161 } 1162 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF; 1163 1164 *dst->getAddr32(x >> 1, y >> 1) = 1165 ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00); 1166} 1167 1168static inline uint32_t expand16(U16CPU c) { 1169 return (c & ~SK_G16_MASK_IN_PLACE) | ((c & SK_G16_MASK_IN_PLACE) << 16); 1170} 1171 1172// returns dirt in the top 16bits, but we don't care, since we only 1173// store the low 16bits. 1174static inline U16CPU pack16(uint32_t c) { 1175 return (c & ~SK_G16_MASK_IN_PLACE) | ((c >> 16) & SK_G16_MASK_IN_PLACE); 1176} 1177 1178static void downsampleby2_proc16(SkBitmap* dst, int x, int y, 1179 const SkBitmap& src) { 1180 x <<= 1; 1181 y <<= 1; 1182 const uint16_t* p = src.getAddr16(x, y); 1183 const uint16_t* baseP = p; 1184 SkPMColor c; 1185 1186 c = expand16(*p); 1187 if (x < src.width() - 1) { 1188 p += 1; 1189 } 1190 c += expand16(*p); 1191 1192 p = baseP; 1193 if (y < src.height() - 1) { 1194 p += src.rowBytes() >> 1; 1195 } 1196 c += expand16(*p); 1197 if (x < src.width() - 1) { 1198 p += 1; 1199 } 1200 c += expand16(*p); 1201 1202 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)pack16(c >> 2); 1203} 1204 1205static uint32_t expand4444(U16CPU c) { 1206 return (c & 0xF0F) | ((c & ~0xF0F) << 12); 1207} 1208 1209static U16CPU collaps4444(uint32_t c) { 1210 return (c & 0xF0F) | ((c >> 12) & ~0xF0F); 1211} 1212 1213static void downsampleby2_proc4444(SkBitmap* dst, int x, int y, 1214 const SkBitmap& src) { 1215 x <<= 1; 1216 y <<= 1; 1217 const uint16_t* p = src.getAddr16(x, y); 1218 const uint16_t* baseP = p; 1219 uint32_t c; 1220 1221 c = expand4444(*p); 1222 if (x < src.width() - 1) { 1223 p += 1; 1224 } 1225 c += expand4444(*p); 1226 1227 p = baseP; 1228 if (y < src.height() - 1) { 1229 p += src.rowBytes() >> 1; 1230 } 1231 c += expand4444(*p); 1232 if (x < src.width() - 1) { 1233 p += 1; 1234 } 1235 c += expand4444(*p); 1236 1237 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)collaps4444(c >> 2); 1238} 1239 1240void SkBitmap::buildMipMap(bool forceRebuild) { 1241 if (forceRebuild) 1242 this->freeMipMap(); 1243 else if (fMipMap) 1244 return; // we're already built 1245 1246 SkASSERT(NULL == fMipMap); 1247 1248 void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src); 1249 1250 const SkBitmap::Config config = this->getConfig(); 1251 1252 switch (config) { 1253 case kARGB_8888_Config: 1254 proc = downsampleby2_proc32; 1255 break; 1256 case kRGB_565_Config: 1257 proc = downsampleby2_proc16; 1258 break; 1259 case kARGB_4444_Config: 1260 proc = downsampleby2_proc4444; 1261 break; 1262 case kIndex8_Config: 1263 case kA8_Config: 1264 default: 1265 return; // don't build mipmaps for these configs 1266 } 1267 1268 SkAutoLockPixels alp(*this); 1269 if (!this->readyToDraw()) { 1270 return; 1271 } 1272 1273 // whip through our loop to compute the exact size needed 1274 size_t size = 0; 1275 int maxLevels = 0; 1276 { 1277 int width = this->width(); 1278 int height = this->height(); 1279 for (;;) { 1280 width >>= 1; 1281 height >>= 1; 1282 if (0 == width || 0 == height) { 1283 break; 1284 } 1285 size += ComputeRowBytes(config, width) * height; 1286 maxLevels += 1; 1287 } 1288 } 1289 1290 // nothing to build 1291 if (0 == maxLevels) { 1292 return; 1293 } 1294 1295 SkBitmap srcBM(*this); 1296 srcBM.lockPixels(); 1297 if (!srcBM.readyToDraw()) { 1298 return; 1299 } 1300 1301 MipMap* mm = MipMap::Alloc(maxLevels, size); 1302 if (NULL == mm) { 1303 return; 1304 } 1305 1306 MipLevel* level = mm->levels(); 1307 uint8_t* addr = (uint8_t*)mm->pixels(); 1308 int width = this->width(); 1309 int height = this->height(); 1310 uint32_t rowBytes; 1311 SkBitmap dstBM; 1312 1313 for (int i = 0; i < maxLevels; i++) { 1314 width >>= 1; 1315 height >>= 1; 1316 rowBytes = SkToU32(ComputeRowBytes(config, width)); 1317 1318 level[i].fPixels = addr; 1319 level[i].fWidth = width; 1320 level[i].fHeight = height; 1321 level[i].fRowBytes = rowBytes; 1322 1323 dstBM.setConfig(config, width, height, rowBytes); 1324 dstBM.setPixels(addr); 1325 1326 srcBM.lockPixels(); 1327 for (int y = 0; y < height; y++) { 1328 for (int x = 0; x < width; x++) { 1329 proc(&dstBM, x, y, srcBM); 1330 } 1331 } 1332 srcBM.unlockPixels(); 1333 1334 srcBM = dstBM; 1335 addr += height * rowBytes; 1336 } 1337 SkASSERT(addr == (uint8_t*)mm->pixels() + size); 1338 fMipMap = mm; 1339} 1340 1341bool SkBitmap::hasMipMap() const { 1342 return fMipMap != NULL; 1343} 1344 1345int SkBitmap::extractMipLevel(SkBitmap* dst, SkFixed sx, SkFixed sy) { 1346 if (NULL == fMipMap) { 1347 return 0; 1348 } 1349 1350 int level = ComputeMipLevel(sx, sy) >> 16; 1351 SkASSERT(level >= 0); 1352 if (level <= 0) { 1353 return 0; 1354 } 1355 1356 if (level >= fMipMap->fLevelCount) { 1357 level = fMipMap->fLevelCount - 1; 1358 } 1359 if (dst) { 1360 const MipLevel& mip = fMipMap->levels()[level - 1]; 1361 dst->setConfig((SkBitmap::Config)this->config(), 1362 mip.fWidth, mip.fHeight, mip.fRowBytes); 1363 dst->setPixels(mip.fPixels); 1364 } 1365 return level; 1366} 1367 1368SkFixed SkBitmap::ComputeMipLevel(SkFixed sx, SkFixed sy) { 1369 sx = SkAbs32(sx); 1370 sy = SkAbs32(sy); 1371 if (sx < sy) { 1372 sx = sy; 1373 } 1374 if (sx < SK_Fixed1) { 1375 return 0; 1376 } 1377 int clz = SkCLZ(sx); 1378 SkASSERT(clz >= 1 && clz <= 15); 1379 return SkIntToFixed(15 - clz) + ((unsigned)(sx << (clz + 1)) >> 16); 1380} 1381 1382/////////////////////////////////////////////////////////////////////////////// 1383 1384static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha, 1385 int alphaRowBytes) { 1386 SkASSERT(alpha != NULL); 1387 SkASSERT(alphaRowBytes >= src.width()); 1388 1389 SkBitmap::Config config = src.getConfig(); 1390 int w = src.width(); 1391 int h = src.height(); 1392 size_t rb = src.rowBytes(); 1393 1394 SkAutoLockPixels alp(src); 1395 if (!src.readyToDraw()) { 1396 // zero out the alpha buffer and return 1397 while (--h >= 0) { 1398 memset(alpha, 0, w); 1399 alpha += alphaRowBytes; 1400 } 1401 return false; 1402 } 1403 1404 if (SkBitmap::kA8_Config == config && !src.isOpaque()) { 1405 const uint8_t* s = src.getAddr8(0, 0); 1406 while (--h >= 0) { 1407 memcpy(alpha, s, w); 1408 s += rb; 1409 alpha += alphaRowBytes; 1410 } 1411 } else if (SkBitmap::kARGB_8888_Config == config && !src.isOpaque()) { 1412 const SkPMColor* SK_RESTRICT s = src.getAddr32(0, 0); 1413 while (--h >= 0) { 1414 for (int x = 0; x < w; x++) { 1415 alpha[x] = SkGetPackedA32(s[x]); 1416 } 1417 s = (const SkPMColor*)((const char*)s + rb); 1418 alpha += alphaRowBytes; 1419 } 1420 } else if (SkBitmap::kARGB_4444_Config == config && !src.isOpaque()) { 1421 const SkPMColor16* SK_RESTRICT s = src.getAddr16(0, 0); 1422 while (--h >= 0) { 1423 for (int x = 0; x < w; x++) { 1424 alpha[x] = SkPacked4444ToA32(s[x]); 1425 } 1426 s = (const SkPMColor16*)((const char*)s + rb); 1427 alpha += alphaRowBytes; 1428 } 1429 } else if (SkBitmap::kIndex8_Config == config && !src.isOpaque()) { 1430 SkColorTable* ct = src.getColorTable(); 1431 if (ct) { 1432 const SkPMColor* SK_RESTRICT table = ct->lockColors(); 1433 const uint8_t* SK_RESTRICT s = src.getAddr8(0, 0); 1434 while (--h >= 0) { 1435 for (int x = 0; x < w; x++) { 1436 alpha[x] = SkGetPackedA32(table[s[x]]); 1437 } 1438 s += rb; 1439 alpha += alphaRowBytes; 1440 } 1441 ct->unlockColors(false); 1442 } 1443 } else { // src is opaque, so just fill alpha[] with 0xFF 1444 memset(alpha, 0xFF, h * alphaRowBytes); 1445 } 1446 return true; 1447} 1448 1449#include "SkPaint.h" 1450#include "SkMaskFilter.h" 1451#include "SkMatrix.h" 1452 1453bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint, 1454 Allocator *allocator, SkIPoint* offset) const { 1455 SkDEBUGCODE(this->validate();) 1456 1457 SkBitmap tmpBitmap; 1458 SkMatrix identity; 1459 SkMask srcM, dstM; 1460 1461 srcM.fBounds.set(0, 0, this->width(), this->height()); 1462 srcM.fRowBytes = SkAlign4(this->width()); 1463 srcM.fFormat = SkMask::kA8_Format; 1464 1465 SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL; 1466 1467 // compute our (larger?) dst bounds if we have a filter 1468 if (NULL != filter) { 1469 identity.reset(); 1470 srcM.fImage = NULL; 1471 if (!filter->filterMask(&dstM, srcM, identity, NULL)) { 1472 goto NO_FILTER_CASE; 1473 } 1474 dstM.fRowBytes = SkAlign4(dstM.fBounds.width()); 1475 } else { 1476 NO_FILTER_CASE: 1477 tmpBitmap.setConfig(SkBitmap::kA8_Config, this->width(), this->height(), 1478 srcM.fRowBytes); 1479 if (!tmpBitmap.allocPixels(allocator, NULL)) { 1480 // Allocation of pixels for alpha bitmap failed. 1481 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", 1482 tmpBitmap.width(), tmpBitmap.height()); 1483 return false; 1484 } 1485 GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes); 1486 if (offset) { 1487 offset->set(0, 0); 1488 } 1489 tmpBitmap.swap(*dst); 1490 return true; 1491 } 1492 srcM.fImage = SkMask::AllocImage(srcM.computeImageSize()); 1493 SkAutoMaskFreeImage srcCleanup(srcM.fImage); 1494 1495 GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes); 1496 if (!filter->filterMask(&dstM, srcM, identity, NULL)) { 1497 goto NO_FILTER_CASE; 1498 } 1499 SkAutoMaskFreeImage dstCleanup(dstM.fImage); 1500 1501 tmpBitmap.setConfig(SkBitmap::kA8_Config, dstM.fBounds.width(), 1502 dstM.fBounds.height(), dstM.fRowBytes); 1503 if (!tmpBitmap.allocPixels(allocator, NULL)) { 1504 // Allocation of pixels for alpha bitmap failed. 1505 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", 1506 tmpBitmap.width(), tmpBitmap.height()); 1507 return false; 1508 } 1509 memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize()); 1510 if (offset) { 1511 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop); 1512 } 1513 SkDEBUGCODE(tmpBitmap.validate();) 1514 1515 tmpBitmap.swap(*dst); 1516 return true; 1517} 1518 1519/////////////////////////////////////////////////////////////////////////////// 1520 1521enum { 1522 SERIALIZE_PIXELTYPE_NONE, 1523 SERIALIZE_PIXELTYPE_REF_DATA 1524}; 1525 1526void SkBitmap::flatten(SkFlattenableWriteBuffer& buffer) const { 1527 buffer.writeInt(fWidth); 1528 buffer.writeInt(fHeight); 1529 buffer.writeInt(fRowBytes); 1530 buffer.writeInt(fConfig); 1531 buffer.writeBool(this->isOpaque()); 1532 1533 if (fPixelRef) { 1534 if (fPixelRef->getFactory()) { 1535 buffer.writeInt(SERIALIZE_PIXELTYPE_REF_DATA); 1536 buffer.writeUInt(SkToU32(fPixelRefOffset)); 1537 buffer.writeFlattenable(fPixelRef); 1538 return; 1539 } 1540 // if we get here, we can't record the pixels 1541 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE); 1542 } else { 1543 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE); 1544 } 1545} 1546 1547void SkBitmap::unflatten(SkFlattenableReadBuffer& buffer) { 1548 this->reset(); 1549 1550 int width = buffer.readInt(); 1551 int height = buffer.readInt(); 1552 int rowBytes = buffer.readInt(); 1553 int config = buffer.readInt(); 1554 1555 this->setConfig((Config)config, width, height, rowBytes); 1556 this->setIsOpaque(buffer.readBool()); 1557 1558 int reftype = buffer.readInt(); 1559 switch (reftype) { 1560 case SERIALIZE_PIXELTYPE_REF_DATA: { 1561 size_t offset = buffer.readUInt(); 1562 SkPixelRef* pr = buffer.readFlattenableT<SkPixelRef>(); 1563 SkSafeUnref(this->setPixelRef(pr, offset)); 1564 break; 1565 } 1566 case SERIALIZE_PIXELTYPE_NONE: 1567 break; 1568 default: 1569 SkDEBUGFAIL("unrecognized pixeltype in serialized data"); 1570 sk_throw(); 1571 } 1572} 1573 1574/////////////////////////////////////////////////////////////////////////////// 1575 1576SkBitmap::RLEPixels::RLEPixels(int width, int height) { 1577 fHeight = height; 1578 fYPtrs = (uint8_t**)sk_malloc_throw(height * sizeof(uint8_t*)); 1579 sk_bzero(fYPtrs, height * sizeof(uint8_t*)); 1580} 1581 1582SkBitmap::RLEPixels::~RLEPixels() { 1583 sk_free(fYPtrs); 1584} 1585 1586/////////////////////////////////////////////////////////////////////////////// 1587 1588#ifdef SK_DEBUG 1589void SkBitmap::validate() const { 1590 SkASSERT(fConfig < kConfigCount); 1591 SkASSERT(fRowBytes >= (unsigned)ComputeRowBytes((Config)fConfig, fWidth)); 1592 uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImmutable_Flag; 1593#ifdef SK_BUILD_FOR_ANDROID 1594 allFlags |= kHasHardwareMipMap_Flag; 1595#endif 1596 SkASSERT(fFlags <= allFlags); 1597 SkASSERT(fPixelLockCount >= 0); 1598 SkASSERT(NULL == fColorTable || (unsigned)fColorTable->getRefCnt() < 10000); 1599 SkASSERT((uint8_t)ComputeBytesPerPixel((Config)fConfig) == fBytesPerPixel); 1600 1601#if 0 // these asserts are not thread-correct, so disable for now 1602 if (fPixelRef) { 1603 if (fPixelLockCount > 0) { 1604 SkASSERT(fPixelRef->isLocked()); 1605 } else { 1606 SkASSERT(NULL == fPixels); 1607 SkASSERT(NULL == fColorTable); 1608 } 1609 } 1610#endif 1611} 1612#endif 1613 1614#ifdef SK_DEVELOPER 1615void SkBitmap::toString(SkString* str) const { 1616 1617 static const char* gConfigNames[kConfigCount] = { 1618 "NONE", "A1", "A8", "INDEX8", "565", "4444", "8888", "RLE" 1619 }; 1620 1621 str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(), 1622 gConfigNames[this->config()]); 1623 1624 str->append(" ("); 1625 if (this->isOpaque()) { 1626 str->append("opaque"); 1627 } else { 1628 str->append("transparent"); 1629 } 1630 if (this->isImmutable()) { 1631 str->append(", immutable"); 1632 } else { 1633 str->append(", not-immutable"); 1634 } 1635 str->append(")"); 1636 1637 SkPixelRef* pr = this->pixelRef(); 1638 if (NULL == pr) { 1639 // show null or the explicit pixel address (rare) 1640 str->appendf(" pixels:%p", this->getPixels()); 1641 } else { 1642 const char* uri = pr->getURI(); 1643 if (NULL != uri) { 1644 str->appendf(" uri:\"%s\"", uri); 1645 } else { 1646 str->appendf(" pixelref:%p", pr); 1647 } 1648 } 1649 1650 str->append(")"); 1651} 1652#endif 1653