| 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 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 SkIntToScalar(fWidth), SkIntToScalar(fHeight)); | 259 SkIntToScalar(fWidth), SkIntToScalar(fHeight)); |
| 260 } | 260 } |
| 261 | 261 |
| 262 void SkBitmap::getBounds(SkIRect* bounds) const { | 262 void SkBitmap::getBounds(SkIRect* bounds) const { |
| 263 SkASSERT(bounds); | 263 SkASSERT(bounds); |
| 264 bounds->set(0, 0, fWidth, fHeight); | 264 bounds->set(0, 0, fWidth, fHeight); |
| 265 } | 265 } |
| 266 | 266 |
| 267 /////////////////////////////////////////////////////////////////////////////// | 267 /////////////////////////////////////////////////////////////////////////////// |
| 268 | 268 |
| 269 void SkBitmap::setConfig(Config c, int width, int height, size_t rowBytes) { | 269 bool SkBitmap::setConfig(Config config, int width, int height, size_t rowBytes, |
| 270 this->freePixels(); | 270 SkAlphaType alphaType) { |
| 271 | |
| 272 if ((width | height) < 0) { | 271 if ((width | height) < 0) { |
| 273 goto err; | 272 goto ERROR; |
| 274 } | 273 } |
| 275 | |
| 276 if (rowBytes == 0) { | 274 if (rowBytes == 0) { |
| 277 rowBytes = SkBitmap::ComputeRowBytes(c, width); | 275 rowBytes = SkBitmap::ComputeRowBytes(config, width); |
| 278 if (0 == rowBytes && kNo_Config != c) { | 276 if (0 == rowBytes && kNo_Config != config) { |
| 279 goto err; | 277 goto ERROR; |
| 280 } | 278 } |
| 281 } | 279 } |
| 282 | 280 |
| 283 fConfig = SkToU8(c); | 281 // check for legal/supported config+alphaType combinations |
| 282 // |
| 283 switch (config) { |
| 284 case kNo_Config: |
| 285 alphaType = kIgnore_SkAlphaType; // canonicalize |
| 286 break; |
| 287 case kA1_Config: |
| 288 case kA8_Config: |
| 289 if (kUnpremul_SkAlphaType == alphaType) { |
| 290 alphaType = kPremul_SkAlphaType; |
| 291 } |
| 292 // fall-through |
| 293 case kIndex8_Config: |
| 294 case kARGB_4444_Config: |
| 295 case kARGB_8888_Config: |
| 296 if (kIgnore_SkAlphaType == alphaType) { |
| 297 goto ERROR; // not supported yet |
| 298 } |
| 299 break; |
| 300 case kRGB_565_Config: |
| 301 alphaType = kOpaque_SkAlphaType; // canonicalize |
| 302 break; |
| 303 } |
| 304 |
| 305 this->freePixels(); |
| 306 |
| 307 fConfig = SkToU8(config); |
| 308 fAlphaType = SkToU8(alphaType); |
| 284 fWidth = width; | 309 fWidth = width; |
| 285 fHeight = height; | 310 fHeight = height; |
| 286 fRowBytes = SkToU32(rowBytes); | 311 fRowBytes = SkToU32(rowBytes); |
| 287 | 312 |
| 288 fBytesPerPixel = (uint8_t)ComputeBytesPerPixel(c); | 313 fBytesPerPixel = (uint8_t)ComputeBytesPerPixel(config); |
| 289 | 314 |
| 290 SkDEBUGCODE(this->validate();) | 315 SkDEBUGCODE(this->validate();) |
| 291 return; | 316 return true; |
| 292 | 317 |
| 293 // if we got here, we had an error, so we reset the bitmap to empty | 318 // if we got here, we had an error, so we reset the bitmap to empty |
| 294 err: | 319 ERROR: |
| 295 this->reset(); | 320 this->reset(); |
| 321 return false; |
| 322 } |
| 323 |
| 324 void SkBitmap::setAlphaType(SkAlphaType alphaType) { |
| 325 // check for legal/supported config+alphaType combinations |
| 326 // |
| 327 switch (this->config()) { |
| 328 case kNo_Config: |
| 329 alphaType = kIgnore_SkAlphaType; // canonicalize |
| 330 break; |
| 331 case kA1_Config: |
| 332 case kA8_Config: |
| 333 if (kUnpremul_SkAlphaType == alphaType) { |
| 334 alphaType = kPremul_SkAlphaType; |
| 335 } |
| 336 // fall-through |
| 337 case kIndex8_Config: |
| 338 case kARGB_4444_Config: |
| 339 case kARGB_8888_Config: |
| 340 if (kIgnore_SkAlphaType == alphaType) { |
| 341 SkDEBUGFAIL("bad alphaType for existing config"); |
| 342 return; |
| 343 } |
| 344 break; |
| 345 case kRGB_565_Config: |
| 346 alphaType = kOpaque_SkAlphaType; // canonicalize |
| 347 break; |
| 348 } |
| 349 |
| 350 fAlphaType = SkToU8(alphaType); |
| 296 } | 351 } |
| 297 | 352 |
| 298 void SkBitmap::updatePixelsFromRef() const { | 353 void SkBitmap::updatePixelsFromRef() const { |
| 299 if (NULL != fPixelRef) { | 354 if (NULL != fPixelRef) { |
| 300 if (fPixelLockCount > 0) { | 355 if (fPixelLockCount > 0) { |
| 301 SkASSERT(fPixelRef->isLocked()); | 356 SkASSERT(fPixelRef->isLocked()); |
| 302 | 357 |
| 303 void* p = fPixelRef->pixels(); | 358 void* p = fPixelRef->pixels(); |
| 304 if (NULL != p) { | 359 if (NULL != p) { |
| 305 p = (char*)p + fPixelRefOffset; | 360 p = (char*)p + fPixelRefOffset; |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 550 | 605 |
| 551 case kRGB_565_Config: | 606 case kRGB_565_Config: |
| 552 return true; | 607 return true; |
| 553 | 608 |
| 554 default: | 609 default: |
| 555 SkDEBUGFAIL("unknown bitmap config pased to isOpaque"); | 610 SkDEBUGFAIL("unknown bitmap config pased to isOpaque"); |
| 556 return false; | 611 return false; |
| 557 } | 612 } |
| 558 } | 613 } |
| 559 | 614 |
| 615 #if 0 |
| 560 void SkBitmap::setIsOpaque(bool isOpaque) { | 616 void SkBitmap::setIsOpaque(bool isOpaque) { |
| 561 /* we record this regardless of fConfig, though it is ignored in | 617 /* we record this regardless of fConfig, though it is ignored in |
| 562 isOpaque() for configs that can't support per-pixel alpha. | 618 isOpaque() for configs that can't support per-pixel alpha. |
| 563 */ | 619 */ |
| 564 if (isOpaque) { | 620 if (isOpaque) { |
| 565 fFlags |= kImageIsOpaque_Flag; | 621 fFlags |= kImageIsOpaque_Flag; |
| 566 } else { | 622 } else { |
| 567 fFlags &= ~kImageIsOpaque_Flag; | 623 fFlags &= ~kImageIsOpaque_Flag; |
| 568 } | 624 } |
| 569 } | 625 } |
| 626 #endif |
| 570 | 627 |
| 571 bool SkBitmap::isVolatile() const { | 628 bool SkBitmap::isVolatile() const { |
| 572 return (fFlags & kImageIsVolatile_Flag) != 0; | 629 return (fFlags & kImageIsVolatile_Flag) != 0; |
| 573 } | 630 } |
| 574 | 631 |
| 575 void SkBitmap::setIsVolatile(bool isVolatile) { | 632 void SkBitmap::setIsVolatile(bool isVolatile) { |
| 576 if (isVolatile) { | 633 if (isVolatile) { |
| 577 fFlags |= kImageIsVolatile_Flag; | 634 fFlags |= kImageIsVolatile_Flag; |
| 578 } else { | 635 } else { |
| 579 fFlags &= ~kImageIsVolatile_Flag; | 636 fFlags &= ~kImageIsVolatile_Flag; |
| (...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 955 srcRect.set(0, 0, this->width(), this->height()); | 1012 srcRect.set(0, 0, this->width(), this->height()); |
| 956 if (!r.intersect(srcRect, subset)) { | 1013 if (!r.intersect(srcRect, subset)) { |
| 957 return false; // r is empty (i.e. no intersection) | 1014 return false; // r is empty (i.e. no intersection) |
| 958 } | 1015 } |
| 959 | 1016 |
| 960 if (fPixelRef->getTexture() != NULL) { | 1017 if (fPixelRef->getTexture() != NULL) { |
| 961 // Do a deep copy | 1018 // Do a deep copy |
| 962 SkPixelRef* pixelRef = fPixelRef->deepCopy(this->config(), &subset); | 1019 SkPixelRef* pixelRef = fPixelRef->deepCopy(this->config(), &subset); |
| 963 if (pixelRef != NULL) { | 1020 if (pixelRef != NULL) { |
| 964 SkBitmap dst; | 1021 SkBitmap dst; |
| 965 dst.setConfig(this->config(), subset.width(), subset.height()); | 1022 dst.setConfig(this->config(), subset.width(), subset.height(), 0, |
| 1023 this->isOpaque() ? |
| 1024 kOpaque_SkAlphaType : kPremul_SkAlphaType); |
| 966 dst.setIsVolatile(this->isVolatile()); | 1025 dst.setIsVolatile(this->isVolatile()); |
| 967 dst.setIsOpaque(this->isOpaque()); | |
| 968 dst.setPixelRef(pixelRef)->unref(); | 1026 dst.setPixelRef(pixelRef)->unref(); |
| 969 SkDEBUGCODE(dst.validate()); | 1027 SkDEBUGCODE(dst.validate()); |
| 970 result->swap(dst); | 1028 result->swap(dst); |
| 971 return true; | 1029 return true; |
| 972 } | 1030 } |
| 973 } | 1031 } |
| 974 | 1032 |
| 975 // If the upper left of the rectangle was outside the bounds of this SkBitma
p, we should have | 1033 // If the upper left of the rectangle was outside the bounds of this SkBitma
p, we should have |
| 976 // exited above. | 1034 // exited above. |
| 977 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width(
))); | 1035 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width(
))); |
| 978 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height(
))); | 1036 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height(
))); |
| 979 | 1037 |
| 980 size_t offset = get_sub_offset(*this, r.fLeft, r.fTop); | 1038 size_t offset = get_sub_offset(*this, r.fLeft, r.fTop); |
| 981 if (SUB_OFFSET_FAILURE == offset) { | 1039 if (SUB_OFFSET_FAILURE == offset) { |
| 982 return false; // config not supported | 1040 return false; // config not supported |
| 983 } | 1041 } |
| 984 | 1042 |
| 985 SkBitmap dst; | 1043 SkBitmap dst; |
| 986 dst.setConfig(this->config(), r.width(), r.height(), this->rowBytes()); | 1044 dst.setConfig(this->config(), r.width(), r.height(), this->rowBytes(), |
| 1045 this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType); |
| 987 dst.setIsVolatile(this->isVolatile()); | 1046 dst.setIsVolatile(this->isVolatile()); |
| 988 dst.setIsOpaque(this->isOpaque()); | |
| 989 | 1047 |
| 990 if (fPixelRef) { | 1048 if (fPixelRef) { |
| 991 // share the pixelref with a custom offset | 1049 // share the pixelref with a custom offset |
| 992 dst.setPixelRef(fPixelRef, fPixelRefOffset + offset); | 1050 dst.setPixelRef(fPixelRef, fPixelRefOffset + offset); |
| 993 } | 1051 } |
| 994 SkDEBUGCODE(dst.validate();) | 1052 SkDEBUGCODE(dst.validate();) |
| 995 | 1053 |
| 996 // we know we're good, so commit to result | 1054 // we know we're good, so commit to result |
| 997 result->swap(dst); | 1055 result->swap(dst); |
| 998 return true; | 1056 return true; |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1128 tmpDst.eraseColor(SK_ColorTRANSPARENT); | 1186 tmpDst.eraseColor(SK_ColorTRANSPARENT); |
| 1129 } | 1187 } |
| 1130 | 1188 |
| 1131 SkCanvas canvas(tmpDst); | 1189 SkCanvas canvas(tmpDst); |
| 1132 SkPaint paint; | 1190 SkPaint paint; |
| 1133 | 1191 |
| 1134 paint.setDither(true); | 1192 paint.setDither(true); |
| 1135 canvas.drawBitmap(*src, 0, 0, &paint); | 1193 canvas.drawBitmap(*src, 0, 0, &paint); |
| 1136 } | 1194 } |
| 1137 | 1195 |
| 1138 tmpDst.setIsOpaque(src->isOpaque()); | 1196 tmpDst.setAlphaType(src->isOpaque() ? |
| 1197 kOpaque_SkAlphaType : kPremul_SkAlphaType); |
| 1139 | 1198 |
| 1140 dst->swap(tmpDst); | 1199 dst->swap(tmpDst); |
| 1141 return true; | 1200 return true; |
| 1142 } | 1201 } |
| 1143 | 1202 |
| 1144 bool SkBitmap::deepCopyTo(SkBitmap* dst, Config dstConfig) const { | 1203 bool SkBitmap::deepCopyTo(SkBitmap* dst, Config dstConfig) const { |
| 1145 if (!this->canCopyTo(dstConfig)) { | 1204 if (!this->canCopyTo(dstConfig)) { |
| 1146 return false; | 1205 return false; |
| 1147 } | 1206 } |
| 1148 | 1207 |
| (...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1601 } | 1660 } |
| 1602 | 1661 |
| 1603 void SkBitmap::unflatten(SkFlattenableReadBuffer& buffer) { | 1662 void SkBitmap::unflatten(SkFlattenableReadBuffer& buffer) { |
| 1604 this->reset(); | 1663 this->reset(); |
| 1605 | 1664 |
| 1606 int width = buffer.readInt(); | 1665 int width = buffer.readInt(); |
| 1607 int height = buffer.readInt(); | 1666 int height = buffer.readInt(); |
| 1608 int rowBytes = buffer.readInt(); | 1667 int rowBytes = buffer.readInt(); |
| 1609 int config = buffer.readInt(); | 1668 int config = buffer.readInt(); |
| 1610 | 1669 |
| 1611 this->setConfig((Config)config, width, height, rowBytes); | 1670 bool isOpaque = buffer.readBool(); |
| 1612 this->setIsOpaque(buffer.readBool()); | 1671 this->setConfig((Config)config, width, height, rowBytes, |
| 1672 isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); |
| 1613 | 1673 |
| 1614 int reftype = buffer.readInt(); | 1674 int reftype = buffer.readInt(); |
| 1615 switch (reftype) { | 1675 switch (reftype) { |
| 1616 case SERIALIZE_PIXELTYPE_REF_DATA: { | 1676 case SERIALIZE_PIXELTYPE_REF_DATA: { |
| 1617 size_t offset = buffer.readUInt(); | 1677 size_t offset = buffer.readUInt(); |
| 1618 SkPixelRef* pr = buffer.readFlattenableT<SkPixelRef>(); | 1678 SkPixelRef* pr = buffer.readFlattenableT<SkPixelRef>(); |
| 1619 SkSafeUnref(this->setPixelRef(pr, offset)); | 1679 SkSafeUnref(this->setPixelRef(pr, offset)); |
| 1620 break; | 1680 break; |
| 1621 } | 1681 } |
| 1622 case SERIALIZE_PIXELTYPE_NONE: | 1682 case SERIALIZE_PIXELTYPE_NONE: |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1699 if (NULL != uri) { | 1759 if (NULL != uri) { |
| 1700 str->appendf(" uri:\"%s\"", uri); | 1760 str->appendf(" uri:\"%s\"", uri); |
| 1701 } else { | 1761 } else { |
| 1702 str->appendf(" pixelref:%p", pr); | 1762 str->appendf(" pixelref:%p", pr); |
| 1703 } | 1763 } |
| 1704 } | 1764 } |
| 1705 | 1765 |
| 1706 str->append(")"); | 1766 str->append(")"); |
| 1707 } | 1767 } |
| 1708 #endif | 1768 #endif |
| OLD | NEW |