SkBitmap.cpp revision 44977485bdac75c055c3fa638f118874ccd2d22f
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 "SkImagePriv.h" 15#include "SkMallocPixelRef.h" 16#include "SkMask.h" 17#include "SkReadBuffer.h" 18#include "SkWriteBuffer.h" 19#include "SkPixelRef.h" 20#include "SkThread.h" 21#include "SkUnPreMultiply.h" 22#include "SkUtils.h" 23#include "SkValidationUtils.h" 24#include "SkPackBits.h" 25#include <new> 26 27static bool reset_return_false(SkBitmap* bm) { 28 bm->reset(); 29 return false; 30} 31 32SkBitmap::SkBitmap() { 33 sk_bzero(this, sizeof(*this)); 34} 35 36SkBitmap::SkBitmap(const SkBitmap& src) { 37 SkDEBUGCODE(src.validate();) 38 sk_bzero(this, sizeof(*this)); 39 *this = src; 40 SkDEBUGCODE(this->validate();) 41} 42 43SkBitmap::~SkBitmap() { 44 SkDEBUGCODE(this->validate();) 45 this->freePixels(); 46} 47 48SkBitmap& SkBitmap::operator=(const SkBitmap& src) { 49 if (this != &src) { 50 this->freePixels(); 51 memcpy(this, &src, sizeof(src)); 52 53 // inc src reference counts 54 SkSafeRef(src.fPixelRef); 55 56 // we reset our locks if we get blown away 57 fPixelLockCount = 0; 58 59 if (fPixelRef) { 60 // ignore the values from the memcpy 61 fPixels = NULL; 62 fColorTable = NULL; 63 // Note that what to for genID is somewhat arbitrary. We have no 64 // way to track changes to raw pixels across multiple SkBitmaps. 65 // Would benefit from an SkRawPixelRef type created by 66 // setPixels. 67 // Just leave the memcpy'ed one but they'll get out of sync 68 // as soon either is modified. 69 } 70 } 71 72 SkDEBUGCODE(this->validate();) 73 return *this; 74} 75 76void SkBitmap::swap(SkBitmap& other) { 77 SkTSwap(fColorTable, other.fColorTable); 78 SkTSwap(fPixelRef, other.fPixelRef); 79 SkTSwap(fPixelRefOrigin, other.fPixelRefOrigin); 80 SkTSwap(fPixelLockCount, other.fPixelLockCount); 81 SkTSwap(fPixels, other.fPixels); 82 SkTSwap(fInfo, other.fInfo); 83 SkTSwap(fRowBytes, other.fRowBytes); 84 SkTSwap(fFlags, other.fFlags); 85 86 SkDEBUGCODE(this->validate();) 87} 88 89void SkBitmap::reset() { 90 this->freePixels(); 91 sk_bzero(this, sizeof(*this)); 92} 93 94void SkBitmap::getBounds(SkRect* bounds) const { 95 SkASSERT(bounds); 96 bounds->set(0, 0, 97 SkIntToScalar(fInfo.width()), SkIntToScalar(fInfo.height())); 98} 99 100void SkBitmap::getBounds(SkIRect* bounds) const { 101 SkASSERT(bounds); 102 bounds->set(0, 0, fInfo.width(), fInfo.height()); 103} 104 105/////////////////////////////////////////////////////////////////////////////// 106 107bool SkBitmap::setInfo(const SkImageInfo& info, size_t rowBytes) { 108 SkAlphaType newAT = info.alphaType(); 109 if (!SkColorTypeValidateAlphaType(info.colorType(), info.alphaType(), &newAT)) { 110 return reset_return_false(this); 111 } 112 // don't look at info.alphaType(), since newAT is the real value... 113 114 // require that rowBytes fit in 31bits 115 int64_t mrb = info.minRowBytes64(); 116 if ((int32_t)mrb != mrb) { 117 return reset_return_false(this); 118 } 119 if ((int64_t)rowBytes != (int32_t)rowBytes) { 120 return reset_return_false(this); 121 } 122 123 if (info.width() < 0 || info.height() < 0) { 124 return reset_return_false(this); 125 } 126 127 if (kUnknown_SkColorType == info.colorType()) { 128 rowBytes = 0; 129 } else if (0 == rowBytes) { 130 rowBytes = (size_t)mrb; 131 } else if (!info.validRowBytes(rowBytes)) { 132 return reset_return_false(this); 133 } 134 135 this->freePixels(); 136 137 fInfo = info.makeAlphaType(newAT); 138 fRowBytes = SkToU32(rowBytes); 139 return true; 140} 141 142bool SkBitmap::setAlphaType(SkAlphaType newAlphaType) { 143 if (!SkColorTypeValidateAlphaType(fInfo.colorType(), newAlphaType, &newAlphaType)) { 144 return false; 145 } 146 if (fInfo.alphaType() != newAlphaType) { 147 fInfo = fInfo.makeAlphaType(newAlphaType); 148 if (fPixelRef) { 149 fPixelRef->changeAlphaType(newAlphaType); 150 } 151 } 152 return true; 153} 154 155void SkBitmap::updatePixelsFromRef() const { 156 if (fPixelRef) { 157 if (fPixelLockCount > 0) { 158 SkASSERT(fPixelRef->isLocked()); 159 160 void* p = fPixelRef->pixels(); 161 if (p) { 162 p = (char*)p 163 + fPixelRefOrigin.fY * fRowBytes 164 + fPixelRefOrigin.fX * fInfo.bytesPerPixel(); 165 } 166 fPixels = p; 167 fColorTable = fPixelRef->colorTable(); 168 } else { 169 SkASSERT(0 == fPixelLockCount); 170 fPixels = NULL; 171 fColorTable = NULL; 172 } 173 } 174} 175 176SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, int dx, int dy) { 177#ifdef SK_DEBUG 178 if (pr) { 179 if (kUnknown_SkColorType != fInfo.colorType()) { 180 const SkImageInfo& prInfo = pr->info(); 181 SkASSERT(fInfo.width() <= prInfo.width()); 182 SkASSERT(fInfo.height() <= prInfo.height()); 183 SkASSERT(fInfo.colorType() == prInfo.colorType()); 184 switch (prInfo.alphaType()) { 185 case kUnknown_SkAlphaType: 186 SkASSERT(fInfo.alphaType() == kUnknown_SkAlphaType); 187 break; 188 case kOpaque_SkAlphaType: 189 case kPremul_SkAlphaType: 190 SkASSERT(fInfo.alphaType() == kOpaque_SkAlphaType || 191 fInfo.alphaType() == kPremul_SkAlphaType); 192 break; 193 case kUnpremul_SkAlphaType: 194 SkASSERT(fInfo.alphaType() == kOpaque_SkAlphaType || 195 fInfo.alphaType() == kUnpremul_SkAlphaType); 196 break; 197 } 198 } 199 } 200#endif 201 202 if (pr) { 203 const SkImageInfo& info = pr->info(); 204 fPixelRefOrigin.set(SkPin32(dx, 0, info.width()), SkPin32(dy, 0, info.height())); 205 } else { 206 // ignore dx,dy if there is no pixelref 207 fPixelRefOrigin.setZero(); 208 } 209 210 if (fPixelRef != pr) { 211 this->freePixels(); 212 SkASSERT(NULL == fPixelRef); 213 214 SkSafeRef(pr); 215 fPixelRef = pr; 216 this->updatePixelsFromRef(); 217 } 218 219 SkDEBUGCODE(this->validate();) 220 return pr; 221} 222 223void SkBitmap::lockPixels() const { 224 if (fPixelRef && 0 == sk_atomic_inc(&fPixelLockCount)) { 225 fPixelRef->lockPixels(); 226 this->updatePixelsFromRef(); 227 } 228 SkDEBUGCODE(this->validate();) 229} 230 231void SkBitmap::unlockPixels() const { 232 SkASSERT(NULL == fPixelRef || fPixelLockCount > 0); 233 234 if (fPixelRef && 1 == sk_atomic_dec(&fPixelLockCount)) { 235 fPixelRef->unlockPixels(); 236 this->updatePixelsFromRef(); 237 } 238 SkDEBUGCODE(this->validate();) 239} 240 241bool SkBitmap::lockPixelsAreWritable() const { 242 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false; 243} 244 245void SkBitmap::setPixels(void* p, SkColorTable* ctable) { 246 if (NULL == p) { 247 this->setPixelRef(NULL); 248 return; 249 } 250 251 if (kUnknown_SkColorType == fInfo.colorType()) { 252 this->setPixelRef(NULL); 253 return; 254 } 255 256 SkPixelRef* pr = SkMallocPixelRef::NewDirect(fInfo, p, fRowBytes, ctable); 257 if (NULL == pr) { 258 this->setPixelRef(NULL); 259 return; 260 } 261 262 this->setPixelRef(pr)->unref(); 263 264 // since we're already allocated, we lockPixels right away 265 this->lockPixels(); 266 SkDEBUGCODE(this->validate();) 267} 268 269bool SkBitmap::tryAllocPixels(Allocator* allocator, SkColorTable* ctable) { 270 HeapAllocator stdalloc; 271 272 if (NULL == allocator) { 273 allocator = &stdalloc; 274 } 275 return allocator->allocPixelRef(this, ctable); 276} 277 278/////////////////////////////////////////////////////////////////////////////// 279 280bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, size_t rowBytes) { 281 if (kIndex_8_SkColorType == requestedInfo.colorType()) { 282 return reset_return_false(this); 283 } 284 if (!this->setInfo(requestedInfo, rowBytes)) { 285 return reset_return_false(this); 286 } 287 288 // setInfo may have corrected info (e.g. 565 is always opaque). 289 const SkImageInfo& correctedInfo = this->info(); 290 // setInfo may have computed a valid rowbytes if 0 were passed in 291 rowBytes = this->rowBytes(); 292 293 SkMallocPixelRef::PRFactory defaultFactory; 294 295 SkPixelRef* pr = defaultFactory.create(correctedInfo, rowBytes, NULL); 296 if (NULL == pr) { 297 return reset_return_false(this); 298 } 299 this->setPixelRef(pr)->unref(); 300 301 // TODO: lockPixels could/should return bool or void*/NULL 302 this->lockPixels(); 303 if (NULL == this->getPixels()) { 304 return reset_return_false(this); 305 } 306 return true; 307} 308 309bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, SkPixelRefFactory* factory, 310 SkColorTable* ctable) { 311 if (kIndex_8_SkColorType == requestedInfo.colorType() && NULL == ctable) { 312 return reset_return_false(this); 313 } 314 if (!this->setInfo(requestedInfo)) { 315 return reset_return_false(this); 316 } 317 318 // setInfo may have corrected info (e.g. 565 is always opaque). 319 const SkImageInfo& correctedInfo = this->info(); 320 321 SkMallocPixelRef::PRFactory defaultFactory; 322 if (NULL == factory) { 323 factory = &defaultFactory; 324 } 325 326 SkPixelRef* pr = factory->create(correctedInfo, correctedInfo.minRowBytes(), ctable); 327 if (NULL == pr) { 328 return reset_return_false(this); 329 } 330 this->setPixelRef(pr)->unref(); 331 332 // TODO: lockPixels could/should return bool or void*/NULL 333 this->lockPixels(); 334 if (NULL == this->getPixels()) { 335 return reset_return_false(this); 336 } 337 return true; 338} 339 340bool SkBitmap::installPixels(const SkImageInfo& requestedInfo, void* pixels, size_t rb, 341 SkColorTable* ct, void (*releaseProc)(void* addr, void* context), 342 void* context) { 343 if (!this->setInfo(requestedInfo, rb)) { 344 this->reset(); 345 return false; 346 } 347 348 // setInfo may have corrected info (e.g. 565 is always opaque). 349 const SkImageInfo& correctedInfo = this->info(); 350 351 SkPixelRef* pr = SkMallocPixelRef::NewWithProc(correctedInfo, rb, ct, pixels, releaseProc, 352 context); 353 if (!pr) { 354 this->reset(); 355 return false; 356 } 357 358 this->setPixelRef(pr)->unref(); 359 360 // since we're already allocated, we lockPixels right away 361 this->lockPixels(); 362 SkDEBUGCODE(this->validate();) 363 return true; 364} 365 366bool SkBitmap::installMaskPixels(const SkMask& mask) { 367 if (SkMask::kA8_Format != mask.fFormat) { 368 this->reset(); 369 return false; 370 } 371 return this->installPixels(SkImageInfo::MakeA8(mask.fBounds.width(), 372 mask.fBounds.height()), 373 mask.fImage, mask.fRowBytes); 374} 375 376/////////////////////////////////////////////////////////////////////////////// 377 378void SkBitmap::freePixels() { 379 if (fPixelRef) { 380 if (fPixelLockCount > 0) { 381 fPixelRef->unlockPixels(); 382 } 383 fPixelRef->unref(); 384 fPixelRef = NULL; 385 fPixelRefOrigin.setZero(); 386 } 387 fPixelLockCount = 0; 388 fPixels = NULL; 389 fColorTable = NULL; 390} 391 392uint32_t SkBitmap::getGenerationID() const { 393 return (fPixelRef) ? fPixelRef->getGenerationID() : 0; 394} 395 396void SkBitmap::notifyPixelsChanged() const { 397 SkASSERT(!this->isImmutable()); 398 if (fPixelRef) { 399 fPixelRef->notifyPixelsChanged(); 400 } 401} 402 403GrTexture* SkBitmap::getTexture() const { 404 return fPixelRef ? fPixelRef->getTexture() : NULL; 405} 406 407/////////////////////////////////////////////////////////////////////////////// 408 409/** We explicitly use the same allocator for our pixels that SkMask does, 410 so that we can freely assign memory allocated by one class to the other. 411 */ 412bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst, 413 SkColorTable* ctable) { 414 const SkImageInfo info = dst->info(); 415 if (kUnknown_SkColorType == info.colorType()) { 416// SkDebugf("unsupported config for info %d\n", dst->config()); 417 return false; 418 } 419 420 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, dst->rowBytes(), ctable); 421 if (NULL == pr) { 422 return false; 423 } 424 425 dst->setPixelRef(pr)->unref(); 426 // since we're already allocated, we lockPixels right away 427 dst->lockPixels(); 428 return true; 429} 430 431/////////////////////////////////////////////////////////////////////////////// 432 433bool SkBitmap::copyPixelsTo(void* const dst, size_t dstSize, 434 size_t dstRowBytes, bool preserveDstPad) const { 435 436 if (0 == dstRowBytes) { 437 dstRowBytes = fRowBytes; 438 } 439 440 if (dstRowBytes < fInfo.minRowBytes() || 441 dst == NULL || (getPixels() == NULL && pixelRef() == NULL)) { 442 return false; 443 } 444 445 if (!preserveDstPad && static_cast<uint32_t>(dstRowBytes) == fRowBytes) { 446 size_t safeSize = this->getSafeSize(); 447 if (safeSize > dstSize || safeSize == 0) 448 return false; 449 else { 450 SkAutoLockPixels lock(*this); 451 // This implementation will write bytes beyond the end of each row, 452 // excluding the last row, if the bitmap's stride is greater than 453 // strictly required by the current config. 454 memcpy(dst, getPixels(), safeSize); 455 456 return true; 457 } 458 } else { 459 // If destination has different stride than us, then copy line by line. 460 if (fInfo.getSafeSize(dstRowBytes) > dstSize) { 461 return false; 462 } else { 463 // Just copy what we need on each line. 464 size_t rowBytes = fInfo.minRowBytes(); 465 SkAutoLockPixels lock(*this); 466 const uint8_t* srcP = reinterpret_cast<const uint8_t*>(getPixels()); 467 uint8_t* dstP = reinterpret_cast<uint8_t*>(dst); 468 for (int row = 0; row < fInfo.height(); row++, srcP += fRowBytes, dstP += dstRowBytes) { 469 memcpy(dstP, srcP, rowBytes); 470 } 471 472 return true; 473 } 474 } 475} 476 477/////////////////////////////////////////////////////////////////////////////// 478 479bool SkBitmap::isImmutable() const { 480 return fPixelRef ? fPixelRef->isImmutable() : false; 481} 482 483void SkBitmap::setImmutable() { 484 if (fPixelRef) { 485 fPixelRef->setImmutable(); 486 } 487} 488 489bool SkBitmap::isVolatile() const { 490 return (fFlags & kImageIsVolatile_Flag) != 0; 491} 492 493void SkBitmap::setIsVolatile(bool isVolatile) { 494 if (isVolatile) { 495 fFlags |= kImageIsVolatile_Flag; 496 } else { 497 fFlags &= ~kImageIsVolatile_Flag; 498 } 499} 500 501void* SkBitmap::getAddr(int x, int y) const { 502 SkASSERT((unsigned)x < (unsigned)this->width()); 503 SkASSERT((unsigned)y < (unsigned)this->height()); 504 505 char* base = (char*)this->getPixels(); 506 if (base) { 507 base += y * this->rowBytes(); 508 switch (this->colorType()) { 509 case kRGBA_8888_SkColorType: 510 case kBGRA_8888_SkColorType: 511 base += x << 2; 512 break; 513 case kARGB_4444_SkColorType: 514 case kRGB_565_SkColorType: 515 base += x << 1; 516 break; 517 case kAlpha_8_SkColorType: 518 case kIndex_8_SkColorType: 519 base += x; 520 break; 521 default: 522 SkDEBUGFAIL("Can't return addr for config"); 523 base = NULL; 524 break; 525 } 526 } 527 return base; 528} 529 530SkColor SkBitmap::getColor(int x, int y) const { 531 SkASSERT((unsigned)x < (unsigned)this->width()); 532 SkASSERT((unsigned)y < (unsigned)this->height()); 533 534 switch (this->colorType()) { 535 case kAlpha_8_SkColorType: { 536 uint8_t* addr = this->getAddr8(x, y); 537 return SkColorSetA(0, addr[0]); 538 } 539 case kIndex_8_SkColorType: { 540 SkPMColor c = this->getIndex8Color(x, y); 541 return SkUnPreMultiply::PMColorToColor(c); 542 } 543 case kRGB_565_SkColorType: { 544 uint16_t* addr = this->getAddr16(x, y); 545 return SkPixel16ToColor(addr[0]); 546 } 547 case kARGB_4444_SkColorType: { 548 uint16_t* addr = this->getAddr16(x, y); 549 SkPMColor c = SkPixel4444ToPixel32(addr[0]); 550 return SkUnPreMultiply::PMColorToColor(c); 551 } 552 case kBGRA_8888_SkColorType: 553 case kRGBA_8888_SkColorType: { 554 uint32_t* addr = this->getAddr32(x, y); 555 return SkUnPreMultiply::PMColorToColor(addr[0]); 556 } 557 default: 558 SkASSERT(false); 559 return 0; 560 } 561 SkASSERT(false); // Not reached. 562 return 0; 563} 564 565bool SkBitmap::ComputeIsOpaque(const SkBitmap& bm) { 566 SkAutoLockPixels alp(bm); 567 if (!bm.getPixels()) { 568 return false; 569 } 570 571 const int height = bm.height(); 572 const int width = bm.width(); 573 574 switch (bm.colorType()) { 575 case kAlpha_8_SkColorType: { 576 unsigned a = 0xFF; 577 for (int y = 0; y < height; ++y) { 578 const uint8_t* row = bm.getAddr8(0, y); 579 for (int x = 0; x < width; ++x) { 580 a &= row[x]; 581 } 582 if (0xFF != a) { 583 return false; 584 } 585 } 586 return true; 587 } break; 588 case kIndex_8_SkColorType: { 589 if (!bm.getColorTable()) { 590 return false; 591 } 592 const SkPMColor* table = bm.getColorTable()->readColors(); 593 SkPMColor c = (SkPMColor)~0; 594 for (int i = bm.getColorTable()->count() - 1; i >= 0; --i) { 595 c &= table[i]; 596 } 597 return 0xFF == SkGetPackedA32(c); 598 } break; 599 case kRGB_565_SkColorType: 600 return true; 601 break; 602 case kARGB_4444_SkColorType: { 603 unsigned c = 0xFFFF; 604 for (int y = 0; y < height; ++y) { 605 const SkPMColor16* row = bm.getAddr16(0, y); 606 for (int x = 0; x < width; ++x) { 607 c &= row[x]; 608 } 609 if (0xF != SkGetPackedA4444(c)) { 610 return false; 611 } 612 } 613 return true; 614 } break; 615 case kBGRA_8888_SkColorType: 616 case kRGBA_8888_SkColorType: { 617 SkPMColor c = (SkPMColor)~0; 618 for (int y = 0; y < height; ++y) { 619 const SkPMColor* row = bm.getAddr32(0, y); 620 for (int x = 0; x < width; ++x) { 621 c &= row[x]; 622 } 623 if (0xFF != SkGetPackedA32(c)) { 624 return false; 625 } 626 } 627 return true; 628 } 629 default: 630 break; 631 } 632 return false; 633} 634 635 636/////////////////////////////////////////////////////////////////////////////// 637/////////////////////////////////////////////////////////////////////////////// 638 639static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) { 640 unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) | 641 (SkR32To4444(r) << SK_R4444_SHIFT) | 642 (SkG32To4444(g) << SK_G4444_SHIFT) | 643 (SkB32To4444(b) << SK_B4444_SHIFT); 644 return SkToU16(pixel); 645} 646 647void SkBitmap::internalErase(const SkIRect& area, 648 U8CPU a, U8CPU r, U8CPU g, U8CPU b) const { 649#ifdef SK_DEBUG 650 SkDEBUGCODE(this->validate();) 651 SkASSERT(!area.isEmpty()); 652 { 653 SkIRect total = { 0, 0, this->width(), this->height() }; 654 SkASSERT(total.contains(area)); 655 } 656#endif 657 658 switch (fInfo.colorType()) { 659 case kUnknown_SkColorType: 660 case kIndex_8_SkColorType: 661 return; // can't erase. Should we bzero so the memory is not uninitialized? 662 default: 663 break; 664 } 665 666 SkAutoLockPixels alp(*this); 667 // perform this check after the lock call 668 if (!this->readyToDraw()) { 669 return; 670 } 671 672 int height = area.height(); 673 const int width = area.width(); 674 const int rowBytes = fRowBytes; 675 676 switch (this->colorType()) { 677 case kAlpha_8_SkColorType: { 678 uint8_t* p = this->getAddr8(area.fLeft, area.fTop); 679 while (--height >= 0) { 680 memset(p, a, width); 681 p += rowBytes; 682 } 683 break; 684 } 685 case kARGB_4444_SkColorType: 686 case kRGB_565_SkColorType: { 687 uint16_t* p = this->getAddr16(area.fLeft, area.fTop);; 688 uint16_t v; 689 690 // make rgb premultiplied 691 if (255 != a) { 692 r = SkAlphaMul(r, a); 693 g = SkAlphaMul(g, a); 694 b = SkAlphaMul(b, a); 695 } 696 697 if (kARGB_4444_SkColorType == this->colorType()) { 698 v = pack_8888_to_4444(a, r, g, b); 699 } else { 700 v = SkPackRGB16(r >> (8 - SK_R16_BITS), 701 g >> (8 - SK_G16_BITS), 702 b >> (8 - SK_B16_BITS)); 703 } 704 while (--height >= 0) { 705 sk_memset16(p, v, width); 706 p = (uint16_t*)((char*)p + rowBytes); 707 } 708 break; 709 } 710 case kBGRA_8888_SkColorType: 711 case kRGBA_8888_SkColorType: { 712 uint32_t* p = this->getAddr32(area.fLeft, area.fTop); 713 714 if (255 != a && kPremul_SkAlphaType == this->alphaType()) { 715 r = SkAlphaMul(r, a); 716 g = SkAlphaMul(g, a); 717 b = SkAlphaMul(b, a); 718 } 719 uint32_t v = kRGBA_8888_SkColorType == this->colorType() ? 720 SkPackARGB_as_RGBA(a, r, g, b) : SkPackARGB_as_BGRA(a, r, g, b); 721 722 while (--height >= 0) { 723 sk_memset32(p, v, width); 724 p = (uint32_t*)((char*)p + rowBytes); 725 } 726 break; 727 } 728 default: 729 return; // no change, so don't call notifyPixelsChanged() 730 } 731 732 this->notifyPixelsChanged(); 733} 734 735void SkBitmap::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const { 736 SkIRect area = { 0, 0, this->width(), this->height() }; 737 if (!area.isEmpty()) { 738 this->internalErase(area, a, r, g, b); 739 } 740} 741 742void SkBitmap::eraseArea(const SkIRect& rect, SkColor c) const { 743 SkIRect area = { 0, 0, this->width(), this->height() }; 744 if (area.intersect(rect)) { 745 this->internalErase(area, SkColorGetA(c), SkColorGetR(c), 746 SkColorGetG(c), SkColorGetB(c)); 747 } 748} 749 750////////////////////////////////////////////////////////////////////////////////////// 751////////////////////////////////////////////////////////////////////////////////////// 752 753bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const { 754 SkDEBUGCODE(this->validate();) 755 756 if (NULL == result || NULL == fPixelRef) { 757 return false; // no src pixels 758 } 759 760 SkIRect srcRect, r; 761 srcRect.set(0, 0, this->width(), this->height()); 762 if (!r.intersect(srcRect, subset)) { 763 return false; // r is empty (i.e. no intersection) 764 } 765 766 if (fPixelRef->getTexture() != NULL) { 767 // Do a deep copy 768 SkPixelRef* pixelRef = fPixelRef->deepCopy(this->colorType(), this->profileType(), &subset); 769 if (pixelRef != NULL) { 770 SkBitmap dst; 771 dst.setInfo(SkImageInfo::Make(subset.width(), subset.height(), 772 this->colorType(), this->alphaType())); 773 dst.setIsVolatile(this->isVolatile()); 774 dst.setPixelRef(pixelRef)->unref(); 775 SkDEBUGCODE(dst.validate()); 776 result->swap(dst); 777 return true; 778 } 779 } 780 781 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have 782 // exited above. 783 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width())); 784 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height())); 785 786 SkBitmap dst; 787 dst.setInfo(SkImageInfo::Make(r.width(), r.height(), this->colorType(), this->alphaType()), 788 this->rowBytes()); 789 dst.setIsVolatile(this->isVolatile()); 790 791 if (fPixelRef) { 792 SkIPoint origin = fPixelRefOrigin; 793 origin.fX += r.fLeft; 794 origin.fY += r.fTop; 795 // share the pixelref with a custom offset 796 dst.setPixelRef(fPixelRef, origin); 797 } 798 SkDEBUGCODE(dst.validate();) 799 800 // we know we're good, so commit to result 801 result->swap(dst); 802 return true; 803} 804 805/////////////////////////////////////////////////////////////////////////////// 806 807#include "SkCanvas.h" 808#include "SkPaint.h" 809 810bool SkBitmap::canCopyTo(SkColorType dstColorType) const { 811 const SkColorType srcCT = this->colorType(); 812 813 if (srcCT == kUnknown_SkColorType) { 814 return false; 815 } 816 817 bool sameConfigs = (srcCT == dstColorType); 818 switch (dstColorType) { 819 case kAlpha_8_SkColorType: 820 case kRGB_565_SkColorType: 821 case kRGBA_8888_SkColorType: 822 case kBGRA_8888_SkColorType: 823 break; 824 case kIndex_8_SkColorType: 825 if (!sameConfigs) { 826 return false; 827 } 828 break; 829 case kARGB_4444_SkColorType: 830 return sameConfigs || kN32_SkColorType == srcCT || kIndex_8_SkColorType == srcCT; 831 default: 832 return false; 833 } 834 return true; 835} 836 837#include "SkConfig8888.h" 838 839bool SkBitmap::readPixels(const SkImageInfo& requestedDstInfo, void* dstPixels, size_t dstRB, 840 int x, int y) const { 841 if (kUnknown_SkColorType == requestedDstInfo.colorType()) { 842 return false; 843 } 844 if (NULL == dstPixels || dstRB < requestedDstInfo.minRowBytes()) { 845 return false; 846 } 847 if (0 == requestedDstInfo.width() || 0 == requestedDstInfo.height()) { 848 return false; 849 } 850 851 SkIRect srcR = SkIRect::MakeXYWH(x, y, requestedDstInfo.width(), requestedDstInfo.height()); 852 if (!srcR.intersect(0, 0, this->width(), this->height())) { 853 return false; 854 } 855 856 // the intersect may have shrunk info's logical size 857 const SkImageInfo dstInfo = requestedDstInfo.makeWH(srcR.width(), srcR.height()); 858 859 // if x or y are negative, then we have to adjust pixels 860 if (x > 0) { 861 x = 0; 862 } 863 if (y > 0) { 864 y = 0; 865 } 866 // here x,y are either 0 or negative 867 dstPixels = ((char*)dstPixels - y * dstRB - x * dstInfo.bytesPerPixel()); 868 869 ////////////// 870 871 SkAutoLockPixels alp(*this); 872 873 // since we don't stop creating un-pixeled devices yet, check for no pixels here 874 if (NULL == this->getPixels()) { 875 return false; 876 } 877 878 const SkImageInfo srcInfo = this->info().makeWH(dstInfo.width(), dstInfo.height()); 879 880 const void* srcPixels = this->getAddr(srcR.x(), srcR.y()); 881 return SkPixelInfo::CopyPixels(dstInfo, dstPixels, dstRB, srcInfo, srcPixels, this->rowBytes(), 882 this->getColorTable()); 883} 884 885bool SkBitmap::copyTo(SkBitmap* dst, SkColorType dstColorType, Allocator* alloc) const { 886 if (!this->canCopyTo(dstColorType)) { 887 return false; 888 } 889 890 // if we have a texture, first get those pixels 891 SkBitmap tmpSrc; 892 const SkBitmap* src = this; 893 894 if (fPixelRef) { 895 SkIRect subset; 896 subset.setXYWH(fPixelRefOrigin.fX, fPixelRefOrigin.fY, 897 fInfo.width(), fInfo.height()); 898 if (fPixelRef->readPixels(&tmpSrc, &subset)) { 899 if (fPixelRef->info().alphaType() == kUnpremul_SkAlphaType) { 900 // FIXME: The only meaningful implementation of readPixels 901 // (GrPixelRef) assumes premultiplied pixels. 902 return false; 903 } 904 SkASSERT(tmpSrc.width() == this->width()); 905 SkASSERT(tmpSrc.height() == this->height()); 906 907 // did we get lucky and we can just return tmpSrc? 908 if (tmpSrc.colorType() == dstColorType && NULL == alloc) { 909 dst->swap(tmpSrc); 910 // If the result is an exact copy, clone the gen ID. 911 if (dst->pixelRef() && dst->pixelRef()->info() == fPixelRef->info()) { 912 dst->pixelRef()->cloneGenID(*fPixelRef); 913 } 914 return true; 915 } 916 917 // fall through to the raster case 918 src = &tmpSrc; 919 } 920 } 921 922 // we lock this now, since we may need its colortable 923 SkAutoLockPixels srclock(*src); 924 if (!src->readyToDraw()) { 925 return false; 926 } 927 928 // The only way to be readyToDraw is if fPixelRef is non NULL. 929 SkASSERT(fPixelRef != NULL); 930 931 const SkImageInfo dstInfo = src->info().makeColorType(dstColorType); 932 933 SkBitmap tmpDst; 934 if (!tmpDst.setInfo(dstInfo)) { 935 return false; 936 } 937 938 // allocate colortable if srcConfig == kIndex8_Config 939 SkAutoTUnref<SkColorTable> ctable; 940 if (dstColorType == kIndex_8_SkColorType) { 941 ctable.reset(SkRef(src->getColorTable())); 942 } 943 if (!tmpDst.tryAllocPixels(alloc, ctable)) { 944 return false; 945 } 946 947 if (!tmpDst.readyToDraw()) { 948 // allocator/lock failed 949 return false; 950 } 951 952 // pixelRef must be non NULL or tmpDst.readyToDraw() would have 953 // returned false. 954 SkASSERT(tmpDst.pixelRef() != NULL); 955 956 if (!src->readPixels(tmpDst.info(), tmpDst.getPixels(), tmpDst.rowBytes(), 0, 0)) { 957 return false; 958 } 959 960 // (for BitmapHeap) Clone the pixelref genID even though we have a new pixelref. 961 // The old copyTo impl did this, so we continue it for now. 962 // 963 // TODO: should we ignore rowbytes (i.e. getSize)? Then it could just be 964 // if (src_pixelref->info == dst_pixelref->info) 965 // 966 if (src->colorType() == dstColorType && tmpDst.getSize() == src->getSize()) { 967 SkPixelRef* dstPixelRef = tmpDst.pixelRef(); 968 if (dstPixelRef->info() == fPixelRef->info()) { 969 dstPixelRef->cloneGenID(*fPixelRef); 970 } 971 } 972 973 dst->swap(tmpDst); 974 return true; 975} 976 977bool SkBitmap::deepCopyTo(SkBitmap* dst) const { 978 const SkColorType dstCT = this->colorType(); 979 const SkColorProfileType dstPT = this->profileType(); 980 981 if (!this->canCopyTo(dstCT)) { 982 return false; 983 } 984 985 // If we have a PixelRef, and it supports deep copy, use it. 986 // Currently supported only by texture-backed bitmaps. 987 if (fPixelRef) { 988 SkPixelRef* pixelRef = fPixelRef->deepCopy(dstCT, dstPT, NULL); 989 if (pixelRef) { 990 uint32_t rowBytes; 991 if (this->colorType() == dstCT && this->profileType() == dstPT) { 992 // Since there is no subset to pass to deepCopy, and deepCopy 993 // succeeded, the new pixel ref must be identical. 994 SkASSERT(fPixelRef->info() == pixelRef->info()); 995 pixelRef->cloneGenID(*fPixelRef); 996 // Use the same rowBytes as the original. 997 rowBytes = fRowBytes; 998 } else { 999 // With the new config, an appropriate fRowBytes will be computed by setInfo. 1000 rowBytes = 0; 1001 } 1002 1003 const SkImageInfo info = fInfo.makeColorType(dstCT); 1004 if (!dst->setInfo(info, rowBytes)) { 1005 return false; 1006 } 1007 dst->setPixelRef(pixelRef, fPixelRefOrigin)->unref(); 1008 return true; 1009 } 1010 } 1011 1012 if (this->getTexture()) { 1013 return false; 1014 } else { 1015 return this->copyTo(dst, dstCT, NULL); 1016 } 1017} 1018 1019/////////////////////////////////////////////////////////////////////////////// 1020 1021static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha, 1022 int alphaRowBytes) { 1023 SkASSERT(alpha != NULL); 1024 SkASSERT(alphaRowBytes >= src.width()); 1025 1026 SkColorType colorType = src.colorType(); 1027 int w = src.width(); 1028 int h = src.height(); 1029 size_t rb = src.rowBytes(); 1030 1031 SkAutoLockPixels alp(src); 1032 if (!src.readyToDraw()) { 1033 // zero out the alpha buffer and return 1034 while (--h >= 0) { 1035 memset(alpha, 0, w); 1036 alpha += alphaRowBytes; 1037 } 1038 return false; 1039 } 1040 1041 if (kAlpha_8_SkColorType == colorType && !src.isOpaque()) { 1042 const uint8_t* s = src.getAddr8(0, 0); 1043 while (--h >= 0) { 1044 memcpy(alpha, s, w); 1045 s += rb; 1046 alpha += alphaRowBytes; 1047 } 1048 } else if (kN32_SkColorType == colorType && !src.isOpaque()) { 1049 const SkPMColor* SK_RESTRICT s = src.getAddr32(0, 0); 1050 while (--h >= 0) { 1051 for (int x = 0; x < w; x++) { 1052 alpha[x] = SkGetPackedA32(s[x]); 1053 } 1054 s = (const SkPMColor*)((const char*)s + rb); 1055 alpha += alphaRowBytes; 1056 } 1057 } else if (kARGB_4444_SkColorType == colorType && !src.isOpaque()) { 1058 const SkPMColor16* SK_RESTRICT s = src.getAddr16(0, 0); 1059 while (--h >= 0) { 1060 for (int x = 0; x < w; x++) { 1061 alpha[x] = SkPacked4444ToA32(s[x]); 1062 } 1063 s = (const SkPMColor16*)((const char*)s + rb); 1064 alpha += alphaRowBytes; 1065 } 1066 } else if (kIndex_8_SkColorType == colorType && !src.isOpaque()) { 1067 SkColorTable* ct = src.getColorTable(); 1068 if (ct) { 1069 const SkPMColor* SK_RESTRICT table = ct->readColors(); 1070 const uint8_t* SK_RESTRICT s = src.getAddr8(0, 0); 1071 while (--h >= 0) { 1072 for (int x = 0; x < w; x++) { 1073 alpha[x] = SkGetPackedA32(table[s[x]]); 1074 } 1075 s += rb; 1076 alpha += alphaRowBytes; 1077 } 1078 } 1079 } else { // src is opaque, so just fill alpha[] with 0xFF 1080 memset(alpha, 0xFF, h * alphaRowBytes); 1081 } 1082 return true; 1083} 1084 1085#include "SkPaint.h" 1086#include "SkMaskFilter.h" 1087#include "SkMatrix.h" 1088 1089bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint, 1090 Allocator *allocator, SkIPoint* offset) const { 1091 SkDEBUGCODE(this->validate();) 1092 1093 SkBitmap tmpBitmap; 1094 SkMatrix identity; 1095 SkMask srcM, dstM; 1096 1097 srcM.fBounds.set(0, 0, this->width(), this->height()); 1098 srcM.fRowBytes = SkAlign4(this->width()); 1099 srcM.fFormat = SkMask::kA8_Format; 1100 1101 SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL; 1102 1103 // compute our (larger?) dst bounds if we have a filter 1104 if (filter) { 1105 identity.reset(); 1106 srcM.fImage = NULL; 1107 if (!filter->filterMask(&dstM, srcM, identity, NULL)) { 1108 goto NO_FILTER_CASE; 1109 } 1110 dstM.fRowBytes = SkAlign4(dstM.fBounds.width()); 1111 } else { 1112 NO_FILTER_CASE: 1113 tmpBitmap.setInfo(SkImageInfo::MakeA8(this->width(), this->height()), srcM.fRowBytes); 1114 if (!tmpBitmap.tryAllocPixels(allocator, NULL)) { 1115 // Allocation of pixels for alpha bitmap failed. 1116 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", 1117 tmpBitmap.width(), tmpBitmap.height()); 1118 return false; 1119 } 1120 GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes); 1121 if (offset) { 1122 offset->set(0, 0); 1123 } 1124 tmpBitmap.swap(*dst); 1125 return true; 1126 } 1127 srcM.fImage = SkMask::AllocImage(srcM.computeImageSize()); 1128 SkAutoMaskFreeImage srcCleanup(srcM.fImage); 1129 1130 GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes); 1131 if (!filter->filterMask(&dstM, srcM, identity, NULL)) { 1132 goto NO_FILTER_CASE; 1133 } 1134 SkAutoMaskFreeImage dstCleanup(dstM.fImage); 1135 1136 tmpBitmap.setInfo(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()), 1137 dstM.fRowBytes); 1138 if (!tmpBitmap.tryAllocPixels(allocator, NULL)) { 1139 // Allocation of pixels for alpha bitmap failed. 1140 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", 1141 tmpBitmap.width(), tmpBitmap.height()); 1142 return false; 1143 } 1144 memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize()); 1145 if (offset) { 1146 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop); 1147 } 1148 SkDEBUGCODE(tmpBitmap.validate();) 1149 1150 tmpBitmap.swap(*dst); 1151 return true; 1152} 1153 1154/////////////////////////////////////////////////////////////////////////////// 1155 1156void SkBitmap::WriteRawPixels(SkWriteBuffer* buffer, const SkBitmap& bitmap) { 1157 const SkImageInfo info = bitmap.info(); 1158 SkAutoLockPixels alp(bitmap); 1159 if (0 == info.width() || 0 == info.height() || NULL == bitmap.getPixels()) { 1160 buffer->writeUInt(0); // instead of snugRB, signaling no pixels 1161 return; 1162 } 1163 1164 const size_t snugRB = info.width() * info.bytesPerPixel(); 1165 const char* src = (const char*)bitmap.getPixels(); 1166 const size_t ramRB = bitmap.rowBytes(); 1167 1168 buffer->write32(SkToU32(snugRB)); 1169 info.flatten(*buffer); 1170 1171 const size_t size = snugRB * info.height(); 1172 SkAutoMalloc storage(size); 1173 char* dst = (char*)storage.get(); 1174 for (int y = 0; y < info.height(); ++y) { 1175 memcpy(dst, src, snugRB); 1176 dst += snugRB; 1177 src += ramRB; 1178 } 1179 buffer->writeByteArray(storage.get(), size); 1180 1181 SkColorTable* ct = bitmap.getColorTable(); 1182 if (kIndex_8_SkColorType == info.colorType() && ct) { 1183 buffer->writeBool(true); 1184 ct->writeToBuffer(*buffer); 1185 } else { 1186 buffer->writeBool(false); 1187 } 1188} 1189 1190bool SkBitmap::ReadRawPixels(SkReadBuffer* buffer, SkBitmap* bitmap) { 1191 const size_t snugRB = buffer->readUInt(); 1192 if (0 == snugRB) { // no pixels 1193 return false; 1194 } 1195 1196 SkImageInfo info; 1197 info.unflatten(*buffer); 1198 1199 // If there was an error reading "info", don't use it to compute minRowBytes() 1200 if (!buffer->validate(true)) { 1201 return false; 1202 } 1203 1204 const size_t ramRB = info.minRowBytes(); 1205 const int height = SkMax32(info.height(), 0); 1206 const uint64_t snugSize = sk_64_mul(snugRB, height); 1207 const uint64_t ramSize = sk_64_mul(ramRB, height); 1208 static const uint64_t max_size_t = (size_t)(-1); 1209 if (!buffer->validate((snugSize <= ramSize) && (ramSize <= max_size_t))) { 1210 return false; 1211 } 1212 1213 SkAutoDataUnref data(SkData::NewUninitialized(SkToSizeT(ramSize))); 1214 char* dst = (char*)data->writable_data(); 1215 buffer->readByteArray(dst, SkToSizeT(snugSize)); 1216 1217 if (snugSize != ramSize) { 1218 const char* srcRow = dst + snugRB * (height - 1); 1219 char* dstRow = dst + ramRB * (height - 1); 1220 for (int y = height - 1; y >= 1; --y) { 1221 memmove(dstRow, srcRow, snugRB); 1222 srcRow -= snugRB; 1223 dstRow -= ramRB; 1224 } 1225 SkASSERT(srcRow == dstRow); // first row does not need to be moved 1226 } 1227 1228 SkAutoTUnref<SkColorTable> ctable; 1229 if (buffer->readBool()) { 1230 ctable.reset(SkNEW_ARGS(SkColorTable, (*buffer))); 1231 } 1232 1233 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewWithData(info, info.minRowBytes(), 1234 ctable.get(), data.get())); 1235 if (!pr.get()) { 1236 return false; 1237 } 1238 bitmap->setInfo(pr->info()); 1239 bitmap->setPixelRef(pr, 0, 0); 1240 return true; 1241} 1242 1243enum { 1244 SERIALIZE_PIXELTYPE_NONE, 1245 SERIALIZE_PIXELTYPE_REF_DATA 1246}; 1247 1248void SkBitmap::legacyUnflatten(SkReadBuffer& buffer) { 1249#ifdef SK_SUPPORT_LEGACY_PIXELREF_UNFLATTENABLE 1250 this->reset(); 1251 1252 SkImageInfo info; 1253 info.unflatten(buffer); 1254 size_t rowBytes = buffer.readInt(); 1255 if (!buffer.validate((info.width() >= 0) && (info.height() >= 0) && 1256 SkColorTypeIsValid(info.fColorType) && 1257 SkAlphaTypeIsValid(info.fAlphaType) && 1258 SkColorTypeValidateAlphaType(info.fColorType, info.fAlphaType) && 1259 info.validRowBytes(rowBytes))) { 1260 return; 1261 } 1262 1263 bool configIsValid = this->setInfo(info, rowBytes); 1264 buffer.validate(configIsValid); 1265 1266 int reftype = buffer.readInt(); 1267 if (buffer.validate((SERIALIZE_PIXELTYPE_REF_DATA == reftype) || 1268 (SERIALIZE_PIXELTYPE_NONE == reftype))) { 1269 switch (reftype) { 1270 case SERIALIZE_PIXELTYPE_REF_DATA: { 1271 SkIPoint origin; 1272 origin.fX = buffer.readInt(); 1273 origin.fY = buffer.readInt(); 1274 size_t offset = origin.fY * rowBytes + origin.fX * info.bytesPerPixel(); 1275 SkPixelRef* pr = buffer.readFlattenable<SkPixelRef>(); 1276 if (!buffer.validate((NULL == pr) || 1277 (pr->getAllocatedSizeInBytes() >= (offset + this->getSafeSize())))) { 1278 origin.setZero(); 1279 } 1280 SkSafeUnref(this->setPixelRef(pr, origin)); 1281 break; 1282 } 1283 case SERIALIZE_PIXELTYPE_NONE: 1284 break; 1285 default: 1286 SkDEBUGFAIL("unrecognized pixeltype in serialized data"); 1287 sk_throw(); 1288 } 1289 } 1290#else 1291 sk_throw(); 1292#endif 1293} 1294 1295/////////////////////////////////////////////////////////////////////////////// 1296 1297SkBitmap::RLEPixels::RLEPixels(int width, int height) { 1298 fHeight = height; 1299 fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*)); 1300} 1301 1302SkBitmap::RLEPixels::~RLEPixels() { 1303 sk_free(fYPtrs); 1304} 1305 1306/////////////////////////////////////////////////////////////////////////////// 1307 1308#ifdef SK_DEBUG 1309void SkBitmap::validate() const { 1310 fInfo.validate(); 1311 1312 // ImageInfo may not require this, but Bitmap ensures that opaque-only 1313 // colorTypes report opaque for their alphatype 1314 if (kRGB_565_SkColorType == fInfo.colorType()) { 1315 SkASSERT(kOpaque_SkAlphaType == fInfo.alphaType()); 1316 } 1317 1318 SkASSERT(fInfo.validRowBytes(fRowBytes)); 1319 uint8_t allFlags = kImageIsVolatile_Flag; 1320#ifdef SK_BUILD_FOR_ANDROID 1321 allFlags |= kHasHardwareMipMap_Flag; 1322#endif 1323 SkASSERT((~allFlags & fFlags) == 0); 1324 SkASSERT(fPixelLockCount >= 0); 1325 1326 if (fPixels) { 1327 SkASSERT(fPixelRef); 1328 SkASSERT(fPixelLockCount > 0); 1329 SkASSERT(fPixelRef->isLocked()); 1330 SkASSERT(fPixelRef->rowBytes() == fRowBytes); 1331 SkASSERT(fPixelRefOrigin.fX >= 0); 1332 SkASSERT(fPixelRefOrigin.fY >= 0); 1333 SkASSERT(fPixelRef->info().width() >= (int)this->width() + fPixelRefOrigin.fX); 1334 SkASSERT(fPixelRef->info().height() >= (int)this->height() + fPixelRefOrigin.fY); 1335 SkASSERT(fPixelRef->rowBytes() >= fInfo.minRowBytes()); 1336 } else { 1337 SkASSERT(NULL == fColorTable); 1338 } 1339} 1340#endif 1341 1342#ifndef SK_IGNORE_TO_STRING 1343void SkBitmap::toString(SkString* str) const { 1344 1345 static const char* gColorTypeNames[kLastEnum_SkColorType + 1] = { 1346 "UNKNOWN", "A8", "565", "4444", "RGBA", "BGRA", "INDEX8", 1347 }; 1348 1349 str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(), 1350 gColorTypeNames[this->colorType()]); 1351 1352 str->append(" ("); 1353 if (this->isOpaque()) { 1354 str->append("opaque"); 1355 } else { 1356 str->append("transparent"); 1357 } 1358 if (this->isImmutable()) { 1359 str->append(", immutable"); 1360 } else { 1361 str->append(", not-immutable"); 1362 } 1363 str->append(")"); 1364 1365 SkPixelRef* pr = this->pixelRef(); 1366 if (NULL == pr) { 1367 // show null or the explicit pixel address (rare) 1368 str->appendf(" pixels:%p", this->getPixels()); 1369 } else { 1370 const char* uri = pr->getURI(); 1371 if (uri) { 1372 str->appendf(" uri:\"%s\"", uri); 1373 } else { 1374 str->appendf(" pixelref:%p", pr); 1375 } 1376 } 1377 1378 str->append(")"); 1379} 1380#endif 1381 1382/////////////////////////////////////////////////////////////////////////////// 1383 1384#ifdef SK_DEBUG 1385void SkImageInfo::validate() const { 1386 SkASSERT(fWidth >= 0); 1387 SkASSERT(fHeight >= 0); 1388 SkASSERT(SkColorTypeIsValid(fColorType)); 1389 SkASSERT(SkAlphaTypeIsValid(fAlphaType)); 1390} 1391#endif 1392