1 /* 2 * Copyright 2006 The Android Open Source Project 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef SkBitmap_DEFINED 9 #define SkBitmap_DEFINED 10 11 #include "SkColor.h" 12 #include "SkImageInfo.h" 13 #include "SkPixmap.h" 14 #include "SkPoint.h" 15 #include "SkRefCnt.h" 16 17 struct SkMask; 18 struct SkIRect; 19 struct SkRect; 20 class SkPaint; 21 class SkPixelRef; 22 class SkString; 23 24 /** \class SkBitmap 25 SkBitmap describes a two-dimensional raster pixel array. SkBitmap is built on 26 SkImageInfo, containing integer width and height, SkColorType and SkAlphaType 27 describing the pixel format, and SkColorSpace describing the range of colors. 28 SkBitmap points to SkPixelRef, which describes the physical array of pixels. 29 SkImageInfo bounds may be located anywhere fully inside SkPixelRef bounds. 30 31 SkBitmap can be drawn using SkCanvas. SkBitmap can be a drawing destination for SkCanvas 32 draw methods. SkBitmap flexibility as a pixel container limits some optimizations 33 available to the target platform. 34 35 If pixel array is primarily read-only, use SkImage for better performance. 36 If pixel array is primarily written to, use SkSurface for better performance. 37 38 Declaring SkBitmap const prevents altering SkImageInfo: the SkBitmap height, width, 39 and so on cannot change. It does not affect SkPixelRef: a caller may write its 40 pixels. Declaring SkBitmap const affects SkBitmap configuration, not its contents. 41 42 SkBitmap is not thread safe. Each thread must have its own copy of SkBitmap fields, 43 although threads may share the underlying pixel array. 44 */ 45 class SK_API SkBitmap { 46 public: 47 class SK_API Allocator; 48 49 /** Creates an empty SkBitmap without pixels, with kUnknown_SkColorType, 50 kUnknown_SkAlphaType, and with a width and height of zero. SkPixelRef origin is 51 set to (0, 0). SkBitmap is not volatile. 52 53 Use setInfo() to associate SkColorType, SkAlphaType, width, and height 54 after SkBitmap has been created. 55 56 @return empty SkBitmap 57 */ 58 SkBitmap(); 59 60 /** Copies settings from src to returned SkBitmap. Shares pixels if src has pixels 61 allocated, so both bitmaps reference the same pixels. 62 63 @param src SkBitmap to copy SkImageInfo, and share SkPixelRef 64 @return copy of src 65 */ 66 SkBitmap(const SkBitmap& src); 67 68 /** Copies settings from src to returned SkBitmap. Moves ownership of src pixels to 69 SkBitmap. 70 71 @param src SkBitmap to copy SkImageInfo, and reassign SkPixelRef 72 @return copy of src 73 */ 74 SkBitmap(SkBitmap&& src); 75 76 /** Decrements SkPixelRef reference count, if SkPixelRef is not nullptr. 77 */ 78 ~SkBitmap(); 79 80 /** Copies settings from src to returned SkBitmap. Shares pixels if src has pixels 81 allocated, so both bitmaps reference the same pixels. 82 83 @param src SkBitmap to copy SkImageInfo, and share SkPixelRef 84 @return copy of src 85 */ 86 SkBitmap& operator=(const SkBitmap& src); 87 88 /** Copies settings from src to returned SkBitmap. Moves ownership of src pixels to 89 SkBitmap. 90 91 @param src SkBitmap to copy SkImageInfo, and reassign SkPixelRef 92 @return copy of src 93 */ 94 SkBitmap& operator=(SkBitmap&& src); 95 96 /** Swaps the fields of the two bitmaps. 97 98 @param other SkBitmap exchanged with original 99 */ 100 void swap(SkBitmap& other); 101 102 /** Returns a constant reference to the SkPixmap holding the SkBitmap pixel 103 address, row bytes, and SkImageInfo. 104 105 @return reference to SkPixmap describing this SkBitmap 106 */ 107 const SkPixmap& pixmap() const { return fPixmap; } 108 109 /** Returns width, height, SkAlphaType, SkColorType, and SkColorSpace. 110 111 @return reference to SkImageInfo 112 */ 113 const SkImageInfo& info() const { return fPixmap.info(); } 114 115 /** Returns pixel count in each row. Should be equal or less than: 116 rowBytes() / info().bytesPerPixel(). 117 118 Maybe be less than pixelRef().width(). Will not exceed pixelRef().width() less 119 pixelRefOrigin().fX. 120 121 @return pixel width in SkImageInfo 122 */ 123 int width() const { return fPixmap.width(); } 124 125 /** Returns pixel row count. 126 127 Maybe be less than pixelRef().height(). Will not exceed pixelRef().height() less 128 pixelRefOrigin().fY. 129 130 @return pixel height in SkImageInfo 131 */ 132 int height() const { return fPixmap.height(); } 133 134 /** Returns SkColorType, one of: kUnknown_SkColorType, kAlpha_8_SkColorType, 135 kRGB_565_SkColorType, kARGB_4444_SkColorType, kRGBA_8888_SkColorType, 136 kBGRA_8888_SkColorType, kGray_8_SkColorType, kRGBA_F16_SkColorType. 137 138 @return SkColorType in SkImageInfo 139 */ 140 SkColorType colorType() const { return fPixmap.colorType(); } 141 142 /** Returns SkAlphaType, one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType, 143 kPremul_SkAlphaType, kUnpremul_SkAlphaType. 144 145 @return SkAlphaType in SkImageInfo 146 */ 147 SkAlphaType alphaType() const { return fPixmap.alphaType(); } 148 149 /** Returns SkColorSpace, the range of colors, associated with SkImageInfo. The 150 reference count of SkColorSpace is unchanged. The returned SkColorSpace is 151 immutable. 152 153 @return SkColorSpace in SkImageInfo, or nullptr 154 */ 155 SkColorSpace* colorSpace() const { return fPixmap.colorSpace(); } 156 157 /** Returns a smart pointer to SkColorSpace, the range of colors, associated with 158 SkImageInfo. The smart pointer tracks the number of objects sharing this 159 SkColorSpace reference so the memory is released when the owners destruct. 160 161 The returned SkColorSpace is immutable. 162 163 @return SkColorSpace in SkImageInfo wrapped in a smart pointer 164 */ 165 sk_sp<SkColorSpace> refColorSpace() const { return fPixmap.info().refColorSpace(); } 166 167 /** Returns number of bytes per pixel required by SkColorType. 168 Returns zero if colorType( is kUnknown_SkColorType. 169 170 @return bytes in pixel 171 */ 172 int bytesPerPixel() const { return fPixmap.info().bytesPerPixel(); } 173 174 /** Returns number of pixels that fit on row. Should be greater than or equal to 175 width(). 176 177 @return maximum pixels per row 178 */ 179 int rowBytesAsPixels() const { return fPixmap.rowBytesAsPixels(); } 180 181 /** Returns bit shift converting row bytes to row pixels. 182 Returns zero for kUnknown_SkColorType. 183 184 @return one of: 0, 1, 2, 3; left shift to convert pixels to bytes 185 */ 186 int shiftPerPixel() const { return fPixmap.shiftPerPixel(); } 187 188 /** Returns true if either width() or height() are zero. 189 190 Does not check if SkPixelRef is nullptr; call drawsNothing() to check width(), 191 height(), and SkPixelRef. 192 193 @return true if dimensions do not enclose area 194 */ 195 bool empty() const { return fPixmap.info().isEmpty(); } 196 197 /** Return true if SkPixelRef is nullptr. 198 199 Does not check if width() or height() are zero; call drawsNothing() to check 200 width(), height(), and SkPixelRef. 201 202 @return true if no SkPixelRef is associated 203 */ 204 bool isNull() const { return nullptr == fPixelRef; } 205 206 /** Return true if width() or height() are zero, or if SkPixelRef is nullptr. 207 If true, SkBitmap has no effect when drawn or drawn into. 208 209 @return true if drawing has no effect 210 */ 211 bool drawsNothing() const { 212 return this->empty() || this->isNull(); 213 } 214 215 /** Returns row bytes, the interval from one pixel row to the next. Row bytes 216 is at least as large as width() * info().bytesPerPixel(). 217 218 Returns zero if colorType() is kUnknown_SkColorType, or if row bytes supplied to 219 setInfo() is not large enough to hold a row of pixels. 220 221 @return byte length of pixel row 222 */ 223 size_t rowBytes() const { return fPixmap.rowBytes(); } 224 225 /** Sets SkAlphaType, if alphaType is compatible with SkColorType. 226 Returns true unless alphaType is kUnknown_SkAlphaType and current SkAlphaType 227 is not kUnknown_SkAlphaType. 228 229 Returns true if SkColorType is kUnknown_SkColorType. alphaType is ignored, and 230 SkAlphaType remains kUnknown_SkAlphaType. 231 232 Returns true if SkColorType is kRGB_565_SkColorType or kGray_8_SkColorType. 233 alphaType is ignored, and SkAlphaType remains kOpaque_SkAlphaType. 234 235 If SkColorType is kARGB_4444_SkColorType, kRGBA_8888_SkColorType, 236 kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: returns true unless 237 alphaType is kUnknown_SkAlphaType and SkAlphaType is not kUnknown_SkAlphaType. 238 If SkAlphaType is kUnknown_SkAlphaType, alphaType is ignored. 239 240 If SkColorType is kAlpha_8_SkColorType, returns true unless 241 alphaType is kUnknown_SkAlphaType and SkAlphaType is not kUnknown_SkAlphaType. 242 If SkAlphaType is kUnknown_SkAlphaType, alphaType is ignored. If alphaType is 243 kUnpremul_SkAlphaType, it is treated as kPremul_SkAlphaType. 244 245 This changes SkAlphaType in SkPixelRef; all bitmaps sharing SkPixelRef 246 are affected. 247 248 @param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType, 249 kPremul_SkAlphaType, kUnpremul_SkAlphaType 250 @return true if SkAlphaType is set 251 */ 252 bool setAlphaType(SkAlphaType alphaType); 253 254 /** Returns pixel address, the base address corresponding to the pixel origin. 255 256 @return pixel address 257 */ 258 void* getPixels() const { return fPixmap.writable_addr(); } 259 260 /** Returns minimum memory required for pixel storage. 261 Does not include unused memory on last row when rowBytesAsPixels() exceeds width(). 262 Returns zero if result does not fit in size_t. 263 Returns zero if height() or width() is 0. 264 Returns height() times rowBytes() if colorType() is kUnknown_SkColorType. 265 266 @return size in bytes of image buffer 267 */ 268 size_t computeByteSize() const { return fPixmap.computeByteSize(); } 269 270 /** Returns true if pixels can not change. 271 272 Most immutable SkBitmap checks trigger an assert only on debug builds. 273 274 @return true if pixels are immutable 275 */ 276 bool isImmutable() const; 277 278 /** Sets internal flag to mark SkBitmap as immutable. Once set, pixels can not change. 279 Any other bitmap sharing the same SkPixelRef are also marked as immutable. 280 Once SkPixelRef is marked immutable, the setting cannot be cleared. 281 282 Writing to immutable SkBitmap pixels triggers an assert on debug builds. 283 */ 284 void setImmutable(); 285 286 /** Returns true if SkAlphaType is kOpaque_SkAlphaType. 287 Does not check if SkColorType allows alpha, or if any pixel value has 288 transparency. 289 290 @return true if SkImageInfo describes opaque alpha 291 */ 292 bool isOpaque() const { 293 return SkAlphaTypeIsOpaque(this->alphaType()); 294 } 295 296 /** If true, provides a hint to caller that pixels should not 297 be cached. Only true if setIsVolatile() has been called to mark as volatile. 298 299 Volatile state is not shared by other bitmaps sharing the same SkPixelRef. 300 301 @return true if marked volatile 302 */ 303 bool isVolatile() const; 304 305 /** Sets if pixels should be read from SkPixelRef on every access. SkBitmap are not 306 volatile by default; a GPU back end may upload pixel values expecting them to be 307 accessed repeatedly. Marking temporary SkBitmap as volatile provides a hint to 308 SkBaseDevice that the SkBitmap pixels should not be cached. This can 309 improve performance by avoiding overhead and reducing resource 310 consumption on SkBaseDevice. 311 312 @param isVolatile true if backing pixels are temporary 313 */ 314 void setIsVolatile(bool isVolatile); 315 316 /** Resets to its initial state; all fields are set to zero, as if SkBitmap had 317 been initialized by SkBitmap(). 318 319 Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to 320 kUnknown_SkColorType; and SkAlphaType to kUnknown_SkAlphaType. 321 322 If SkPixelRef is allocated, its reference count is decreased by one, releasing 323 its memory if SkBitmap is the sole owner. 324 */ 325 void reset(); 326 327 /** Returns true if all pixels are opaque. SkColorType determines how pixels 328 are encoded, and whether pixel describes alpha. Returns true for SkColorType 329 without alpha in each pixel; for other SkColorType, returns true if all 330 pixels have alpha values equivalent to 1.0 or greater. 331 332 For SkColorType kRGB_565_SkColorType or kGray_8_SkColorType: always 333 returns true. For SkColorType kAlpha_8_SkColorType, kBGRA_8888_SkColorType, 334 kRGBA_8888_SkColorType: returns true if all pixel alpha values are 255. 335 For SkColorType kARGB_4444_SkColorType: returns true if all pixel alpha values are 15. 336 For kRGBA_F16_SkColorType: returns true if all pixel alpha values are 1.0 or 337 greater. 338 339 Returns false for kUnknown_SkColorType. 340 341 @param bm SkBitmap to check 342 @return true if all pixels have opaque values or SkColorType is opaque 343 */ 344 static bool ComputeIsOpaque(const SkBitmap& bm) { 345 return bm.pixmap().computeIsOpaque(); 346 } 347 348 /** Returns SkRect { 0, 0, width(), height() }. 349 350 @param bounds container for floating point rectangle 351 */ 352 void getBounds(SkRect* bounds) const; 353 354 /** Returns SkIRect { 0, 0, width(), height() }. 355 356 @param bounds container for integral rectangle 357 */ 358 void getBounds(SkIRect* bounds) const; 359 360 /** Returns SkIRect { 0, 0, width(), height() }. 361 362 @return integral rectangle from origin to width() and height() 363 */ 364 SkIRect bounds() const { return fPixmap.info().bounds(); } 365 366 /** Returns SkISize { width(), height() }. 367 368 @return integral size of width() and height() 369 */ 370 SkISize dimensions() const { return fPixmap.info().dimensions(); } 371 372 /** Returns the bounds of this bitmap, offset by its SkPixelRef origin. 373 374 @return bounds within SkPixelRef bounds 375 */ 376 SkIRect getSubset() const { 377 SkIPoint origin = this->pixelRefOrigin(); 378 return SkIRect::MakeXYWH(origin.x(), origin.y(), this->width(), this->height()); 379 } 380 381 /** Sets width, height, SkAlphaType, SkColorType, SkColorSpace, and optional 382 rowBytes. Frees pixels, and returns true if successful. 383 384 imageInfo.alphaType() may be altered to a value permitted by imageInfo.colorSpace(). 385 If imageInfo.colorType() is kUnknown_SkColorType, imageInfo.alphaType() is 386 set to kUnknown_SkAlphaType. 387 If imageInfo.colorType() is kAlpha_8_SkColorType and imageInfo.alphaType() is 388 kUnpremul_SkAlphaType, imageInfo.alphaType() is replaced by kPremul_SkAlphaType. 389 If imageInfo.colorType() is kRGB_565_SkColorType or kGray_8_SkColorType, 390 imageInfo.alphaType() is set to kOpaque_SkAlphaType. 391 If imageInfo.colorType() is kARGB_4444_SkColorType, kRGBA_8888_SkColorType, 392 kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: imageInfo.alphaType() remains 393 unchanged. 394 395 rowBytes must equal or exceed imageInfo.minRowBytes(). If imageInfo.colorSpace() is 396 kUnknown_SkColorType, rowBytes is ignored and treated as zero; for all other 397 SkColorSpace values, rowBytes of zero is treated as imageInfo.minRowBytes(). 398 399 Calls reset() and returns false if: 400 - rowBytes exceeds 31 bits 401 - imageInfo.width() is negative 402 - imageInfo.height() is negative 403 - rowBytes is positive and less than imageInfo.width() times imageInfo.bytesPerPixel() 404 405 @param imageInfo contains width, height, SkAlphaType, SkColorType, SkColorSpace 406 @param rowBytes imageInfo.minRowBytes or larger; or zero 407 @return true if SkImageInfo set successfully 408 */ 409 bool setInfo(const SkImageInfo& imageInfo, size_t rowBytes = 0); 410 411 /** \enum SkBitmap::AllocFlags 412 AllocFlags provides the option to zero pixel memory when allocated. 413 */ 414 enum AllocFlags { 415 /** Instructs tryAllocPixelsFlags() and allocPixelsFlags() to zero pixel memory. */ 416 kZeroPixels_AllocFlag = 1 << 0, 417 }; 418 419 /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel 420 memory. If flags is kZeroPixels_AllocFlag, memory is zeroed. 421 422 Returns false and calls reset() if SkImageInfo could not be set, or memory could 423 not be allocated, or memory could not optionally be zeroed. 424 425 On most platforms, allocating pixel memory may succeed even though there is 426 not sufficient memory to hold pixels; allocation does not take place 427 until the pixels are written to. The actual behavior depends on the platform 428 implementation of malloc(), if flags is zero, and calloc(), if flags is 429 kZeroPixels_AllocFlag. 430 431 Passing kZeroPixels_AllocFlag is usually faster than separately calling 432 eraseColor(SK_ColorTRANSPARENT). 433 434 @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace 435 @param flags kZeroPixels_AllocFlag, or zero 436 @return true if pixels allocation is successful 437 */ 438 bool SK_WARN_UNUSED_RESULT tryAllocPixelsFlags(const SkImageInfo& info, uint32_t flags); 439 440 /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel 441 memory. If flags is kZeroPixels_AllocFlag, memory is zeroed. 442 443 Aborts execution if SkImageInfo could not be set, or memory could 444 not be allocated, or memory could not optionally 445 be zeroed. Abort steps may be provided by the user at compile time by defining 446 SK_ABORT. 447 448 On most platforms, allocating pixel memory may succeed even though there is 449 not sufficient memory to hold pixels; allocation does not take place 450 until the pixels are written to. The actual behavior depends on the platform 451 implementation of malloc(), if flags is zero, and calloc(), if flags is 452 kZeroPixels_AllocFlag. 453 454 Passing kZeroPixels_AllocFlag is usually faster than separately calling 455 eraseColor(SK_ColorTRANSPARENT). 456 457 @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace 458 @param flags kZeroPixels_AllocFlag, or zero 459 */ 460 void allocPixelsFlags(const SkImageInfo& info, uint32_t flags) { 461 SkASSERT_RELEASE(this->tryAllocPixelsFlags(info, flags)); 462 } 463 464 /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel 465 memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(), 466 or equal zero. Pass in zero for rowBytes to compute the minimum valid value. 467 468 Returns false and calls reset() if SkImageInfo could not be set, or memory could 469 not be allocated. 470 471 On most platforms, allocating pixel memory may succeed even though there is 472 not sufficient memory to hold pixels; allocation does not take place 473 until the pixels are written to. The actual behavior depends on the platform 474 implementation of malloc(). 475 476 @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace 477 @param rowBytes size of pixel row or larger; may be zero 478 @return true if pixel storage is allocated 479 */ 480 bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info, size_t rowBytes); 481 482 /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel 483 memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(), 484 or equal zero. Pass in zero for rowBytes to compute the minimum valid value. 485 486 Aborts execution if SkImageInfo could not be set, or memory could 487 not be allocated. Abort steps may be provided by 488 the user at compile time by defining SK_ABORT. 489 490 On most platforms, allocating pixel memory may succeed even though there is 491 not sufficient memory to hold pixels; allocation does not take place 492 until the pixels are written to. The actual behavior depends on the platform 493 implementation of malloc(). 494 495 @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace 496 @param rowBytes size of pixel row or larger; may be zero 497 */ 498 void allocPixels(const SkImageInfo& info, size_t rowBytes) { 499 SkASSERT_RELEASE(this->tryAllocPixels(info, rowBytes)); 500 } 501 502 /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel 503 memory. 504 505 Returns false and calls reset() if SkImageInfo could not be set, or memory could 506 not be allocated. 507 508 On most platforms, allocating pixel memory may succeed even though there is 509 not sufficient memory to hold pixels; allocation does not take place 510 until the pixels are written to. The actual behavior depends on the platform 511 implementation of malloc(). 512 513 @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace 514 @return true if pixel storage is allocated 515 */ 516 bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info) { 517 return this->tryAllocPixels(info, info.minRowBytes()); 518 } 519 520 /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel 521 memory. 522 523 Aborts execution if SkImageInfo could not be set, or memory could 524 not be allocated. Abort steps may be provided by 525 the user at compile time by defining SK_ABORT. 526 527 On most platforms, allocating pixel memory may succeed even though there is 528 not sufficient memory to hold pixels; allocation does not take place 529 until the pixels are written to. The actual behavior depends on the platform 530 implementation of malloc(). 531 532 @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace 533 */ 534 void allocPixels(const SkImageInfo& info) { 535 this->allocPixels(info, info.minRowBytes()); 536 } 537 538 /** Sets SkImageInfo to width, height, and native SkColorType; and allocates 539 pixel memory. If isOpaque is true, sets SkImageInfo to kOpaque_SkAlphaType; 540 otherwise, sets to kPremul_SkAlphaType. 541 542 Calls reset() and returns false if width exceeds 29 bits or is negative, 543 or height is negative. 544 545 Returns false if allocation fails. 546 547 Use to create SkBitmap that matches SkPMColor, the native pixel arrangement on 548 the platform. SkBitmap drawn to output device skips converting its pixel format. 549 550 @param width pixel column count; must be zero or greater 551 @param height pixel row count; must be zero or greater 552 @param isOpaque true if pixels do not have transparency 553 @return true if pixel storage is allocated 554 */ 555 bool SK_WARN_UNUSED_RESULT tryAllocN32Pixels(int width, int height, bool isOpaque = false) { 556 SkImageInfo info = SkImageInfo::MakeN32(width, height, 557 isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); 558 return this->tryAllocPixels(info); 559 } 560 561 /** Sets SkImageInfo to width, height, and the native SkColorType; and allocates 562 pixel memory. If isOpaque is true, sets SkImageInfo to kPremul_SkAlphaType; 563 otherwise, sets to kOpaque_SkAlphaType. 564 565 Aborts if width exceeds 29 bits or is negative, or height is negative, or 566 allocation fails. Abort steps may be provided by the user at compile time by 567 defining SK_ABORT. 568 569 Use to create SkBitmap that matches SkPMColor, the native pixel arrangement on 570 the platform. SkBitmap drawn to output device skips converting its pixel format. 571 572 @param width pixel column count; must be zero or greater 573 @param height pixel row count; must be zero or greater 574 @param isOpaque true if pixels do not have transparency 575 */ 576 void allocN32Pixels(int width, int height, bool isOpaque = false) { 577 SkImageInfo info = SkImageInfo::MakeN32(width, height, 578 isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); 579 this->allocPixels(info); 580 } 581 582 /** Sets SkImageInfo to info following the rules in setInfo(), and creates SkPixelRef 583 containing pixels and rowBytes. releaseProc, if not nullptr, is called 584 immediately on failure or when pixels are no longer referenced. context may be 585 nullptr. 586 587 If SkImageInfo could not be set, or rowBytes is less than info.minRowBytes(): 588 calls releaseProc if present, calls reset(), and returns false. 589 590 Otherwise, if pixels equals nullptr: sets SkImageInfo, calls releaseProc if 591 present, returns true. 592 593 If SkImageInfo is set, pixels is not nullptr, and releaseProc is not nullptr: 594 when pixels are no longer referenced, calls releaseProc with pixels and context 595 as parameters. 596 597 @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace 598 @param pixels address or pixel storage; may be nullptr 599 @param rowBytes size of pixel row or larger 600 @param releaseProc function called when pixels can be deleted; may be nullptr 601 @param context caller state passed to releaseProc; may be nullptr 602 @return true if SkImageInfo is set to info 603 */ 604 bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, 605 void (*releaseProc)(void* addr, void* context), void* context); 606 607 /** Sets SkImageInfo to info following the rules in setInfo(), and creates SkPixelRef 608 containing pixels and rowBytes. 609 610 If SkImageInfo could not be set, or rowBytes is less than info.minRowBytes(): 611 calls reset(), and returns false. 612 613 Otherwise, if pixels equals nullptr: sets SkImageInfo, returns true. 614 615 Caller must ensure that pixels are valid for the lifetime of SkBitmap and SkPixelRef. 616 617 @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace 618 @param pixels address or pixel storage; may be nullptr 619 @param rowBytes size of pixel row or larger 620 @return true if SkImageInfo is set to info 621 */ 622 bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) { 623 return this->installPixels(info, pixels, rowBytes, nullptr, nullptr); 624 } 625 626 /** Sets SkImageInfo to pixmap.info() following the rules in setInfo(), and creates 627 SkPixelRef containing pixmap.addr() and pixmap.rowBytes(). 628 629 If SkImageInfo could not be set, or pixmap.rowBytes() is less than 630 SkImageInfo::minRowBytes: calls reset(), and returns false. 631 632 Otherwise, if pixmap.addr() equals nullptr: sets SkImageInfo, returns true. 633 634 Caller must ensure that pixmap is valid for the lifetime of SkBitmap and SkPixelRef. 635 636 @param pixmap SkImageInfo, pixel address, and rowBytes() 637 @return true if SkImageInfo was set to pixmap.info() 638 */ 639 bool installPixels(const SkPixmap& pixmap); 640 641 /** To be deprecated soon. 642 */ 643 bool installMaskPixels(const SkMask& mask); 644 645 /** Replaces SkPixelRef with pixels, preserving SkImageInfo and rowBytes(). 646 Sets SkPixelRef origin to (0, 0). 647 648 If pixels is nullptr, or if info().colorType equals kUnknown_SkColorType; 649 release reference to SkPixelRef, and set SkPixelRef to nullptr. 650 651 Caller is responsible for handling ownership pixel memory for the lifetime 652 of SkBitmap and SkPixelRef. 653 654 @param pixels address of pixel storage, managed by caller 655 */ 656 void setPixels(void* pixels); 657 658 /** Allocates pixel memory with HeapAllocator, and replaces existing SkPixelRef. 659 The allocation size is determined by SkImageInfo width, height, and SkColorType. 660 661 Returns false if info().colorType is kUnknown_SkColorType, or allocation fails. 662 663 @return true if the allocation succeeds 664 */ 665 bool SK_WARN_UNUSED_RESULT tryAllocPixels() { 666 return this->tryAllocPixels((Allocator*)nullptr); 667 } 668 669 /** Allocates pixel memory with HeapAllocator, and replaces existing SkPixelRef. 670 The allocation size is determined by SkImageInfo width, height, and SkColorType. 671 672 Aborts if info().colorType is kUnknown_SkColorType, or allocation fails. 673 Abort steps may be provided by the user at compile 674 time by defining SK_ABORT. 675 */ 676 void allocPixels() { 677 this->allocPixels((Allocator*)nullptr); 678 } 679 680 /** Allocates pixel memory with allocator, and replaces existing SkPixelRef. 681 The allocation size is determined by SkImageInfo width, height, and SkColorType. 682 If allocator is nullptr, use HeapAllocator instead. 683 684 Returns false if Allocator::allocPixelRef return false. 685 686 @param allocator instance of SkBitmap::Allocator instantiation 687 @return true if custom allocator reports success 688 */ 689 bool SK_WARN_UNUSED_RESULT tryAllocPixels(Allocator* allocator); 690 691 /** Allocates pixel memory with allocator, and replaces existing SkPixelRef. 692 The allocation size is determined by SkImageInfo width, height, and SkColorType. 693 If allocator is nullptr, use HeapAllocator instead. 694 695 Aborts if Allocator::allocPixelRef return false. Abort steps may be provided by 696 the user at compile time by defining SK_ABORT. 697 698 @param allocator instance of SkBitmap::Allocator instantiation 699 */ 700 void allocPixels(Allocator* allocator) { 701 SkASSERT_RELEASE(this->tryAllocPixels(allocator)); 702 } 703 704 /** Returns SkPixelRef, which contains: pixel base address; its dimensions; and 705 rowBytes(), the interval from one row to the next. Does not change SkPixelRef 706 reference count. SkPixelRef may be shared by multiple bitmaps. 707 If SkPixelRef has not been set, returns nullptr. 708 709 @return SkPixelRef, or nullptr 710 */ 711 SkPixelRef* pixelRef() const { return fPixelRef.get(); } 712 713 /** Returns origin of pixels within SkPixelRef. SkBitmap bounds is always contained 714 by SkPixelRef bounds, which may be the same size or larger. Multiple SkBitmap 715 can share the same SkPixelRef, where each SkBitmap has different bounds. 716 717 The returned origin added to SkBitmap dimensions equals or is smaller than the 718 SkPixelRef dimensions. 719 720 Returns (0, 0) if SkPixelRef is nullptr. 721 722 @return pixel origin within SkPixelRef 723 */ 724 SkIPoint pixelRefOrigin() const; 725 726 /** Replaces pixelRef and origin in SkBitmap. dx and dy specify the offset 727 within the SkPixelRef pixels for the top-left corner of the bitmap. 728 729 Asserts in debug builds if dx or dy are out of range. Pins dx and dy 730 to legal range in release builds. 731 732 The caller is responsible for ensuring that the pixels match the 733 SkColorType and SkAlphaType in SkImageInfo. 734 735 @param pixelRef SkPixelRef describing pixel address and rowBytes() 736 @param dx column offset in SkPixelRef for bitmap origin 737 @param dy row offset in SkPixelRef for bitmap origin 738 */ 739 void setPixelRef(sk_sp<SkPixelRef> pixelRef, int dx, int dy); 740 741 /** Returns true if SkBitmap is can be drawn. 742 743 @return true if getPixels() is not nullptr 744 */ 745 bool readyToDraw() const { 746 return this->getPixels() != nullptr; 747 } 748 749 /** Returns a unique value corresponding to the pixels in SkPixelRef. 750 Returns a different value after notifyPixelsChanged() has been called. 751 Returns zero if SkPixelRef is nullptr. 752 753 Determines if pixels have changed since last examined. 754 755 @return unique value for pixels in SkPixelRef 756 */ 757 uint32_t getGenerationID() const; 758 759 /** Marks that pixels in SkPixelRef have changed. Subsequent calls to 760 getGenerationID() return a different value. 761 */ 762 void notifyPixelsChanged() const; 763 764 /** Replaces pixel values with c. All pixels contained by bounds() are affected. 765 If the colorType() is kGray_8_SkColorType or k565_SkColorType, then color alpha 766 is ignored; RGB is treated as opaque. If colorType() is kAlpha_8_SkColorType, 767 then RGB is ignored. 768 769 @param c unpremultiplied color 770 */ 771 void eraseColor(SkColor c) const; 772 773 /** Replaces pixel values with unpremultiplied color built from a, r, g, and b. 774 All pixels contained by bounds() are affected. 775 If the colorType() is kGray_8_SkColorType or k565_SkColorType, then a 776 is ignored; r, g, and b are treated as opaque. If colorType() is kAlpha_8_SkColorType, 777 then r, g, and b are ignored. 778 779 @param a amount of color alpha, from fully transparent (0) to fully opaque (255) 780 @param r amount of color rgb red, from no red (0) to full red (255) 781 @param g amount of color rgb green, from no green (0) to full green (255) 782 @param b amount of color rgb blue, from no blue (0) to full blue (255) 783 */ 784 void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const { 785 this->eraseColor(SkColorSetARGB(a, r, g, b)); 786 } 787 788 /** Deprecated. Use eraseARGB() or eraseColor(). 789 790 @param r amount of red 791 @param g amount of green 792 @param b amount of blue 793 */ 794 SK_ATTR_DEPRECATED("use eraseARGB or eraseColor") 795 void eraseRGB(U8CPU r, U8CPU g, U8CPU b) const { 796 this->eraseARGB(0xFF, r, g, b); 797 } 798 799 /** Replaces pixel values inside area with c. If area does not intersect bounds(), 800 call has no effect. 801 802 If the colorType() is kGray_8_SkColorType or k565_SkColorType, then color alpha 803 is ignored; RGB is treated as opaque. If colorType() is kAlpha_8_SkColorType, 804 then RGB is ignored. 805 806 @param c unpremultiplied color 807 @param area rectangle to fill 808 */ 809 void erase(SkColor c, const SkIRect& area) const; 810 811 /** Deprecated. 812 */ 813 void eraseArea(const SkIRect& area, SkColor c) const { 814 this->erase(c, area); 815 } 816 817 /** Returns pixel at (x, y) as unpremultiplied color. 818 Returns black with alpha if SkColorType is kAlpha_8_SkColorType. 819 820 Input is not validated: out of bounds values of x or y trigger an assert() if 821 built with SK_DEBUG defined; and returns undefined values or may crash if 822 SK_RELEASE is defined. Fails if SkColorType is kUnknown_SkColorType or 823 pixel address is nullptr. 824 825 SkColorSpace in SkImageInfo is ignored. Some color precision may be lost in the 826 conversion to unpremultiplied color; original pixel data may have additional 827 precision. 828 829 @param x column index, zero or greater, and less than width() 830 @param y row index, zero or greater, and less than height() 831 @return pixel converted to unpremultiplied color 832 */ 833 SkColor getColor(int x, int y) const { 834 return this->pixmap().getColor(x, y); 835 } 836 837 /** Returns pixel address at (x, y). 838 839 Input is not validated: out of bounds values of x or y, or kUnknown_SkColorType, 840 trigger an assert() if built with SK_DEBUG defined. Returns nullptr if 841 SkColorType is kUnknown_SkColorType, or SkPixelRef is nullptr. 842 843 Performs a lookup of pixel size; for better performance, call 844 one of: getAddr8(), getAddr16(), or getAddr32(). 845 846 @param x column index, zero or greater, and less than width() 847 @param y row index, zero or greater, and less than height() 848 @return generic pointer to pixel 849 */ 850 void* getAddr(int x, int y) const; 851 852 /** Returns address at (x, y). 853 854 Input is not validated. Triggers an assert() if built with SK_DEBUG defined and: 855 - SkPixelRef is nullptr 856 - bytesPerPixel() is not four 857 - x is negative, or not less than width() 858 - y is negative, or not less than height() 859 860 @param x column index, zero or greater, and less than width() 861 @param y row index, zero or greater, and less than height() 862 @return unsigned 32-bit pointer to pixel at (x, y) 863 */ 864 inline uint32_t* getAddr32(int x, int y) const; 865 866 /** Returns address at (x, y). 867 868 Input is not validated. Triggers an assert() if built with SK_DEBUG defined and: 869 - SkPixelRef is nullptr 870 - bytesPerPixel() is not two 871 - x is negative, or not less than width() 872 - y is negative, or not less than height() 873 874 @param x column index, zero or greater, and less than width() 875 @param y row index, zero or greater, and less than height() 876 @return unsigned 16-bit pointer to pixel at (x, y) 877 */ 878 inline uint16_t* getAddr16(int x, int y) const; 879 880 /** Returns address at (x, y). 881 882 Input is not validated. Triggers an assert() if built with SK_DEBUG defined and: 883 - SkPixelRef is nullptr 884 - bytesPerPixel() is not one 885 - x is negative, or not less than width() 886 - y is negative, or not less than height() 887 888 @param x column index, zero or greater, and less than width() 889 @param y row index, zero or greater, and less than height() 890 @return unsigned 8-bit pointer to pixel at (x, y) 891 */ 892 inline uint8_t* getAddr8(int x, int y) const; 893 894 /** Shares SkPixelRef with dst. Pixels are not copied; SkBitmap and dst point 895 to the same pixels; dst bounds() are set to the intersection of subset 896 and the original bounds(). 897 898 subset may be larger than bounds(). Any area outside of bounds() is ignored. 899 900 Any contents of dst are discarded. isVolatile() setting is copied to dst. 901 dst is set to colorType(), alphaType(), and colorSpace(). 902 903 Return false if: 904 - dst is nullptr 905 - SkPixelRef is nullptr 906 - subset does not intersect bounds() 907 908 @param dst SkBitmap set to subset 909 @param subset rectangle of pixels to reference 910 @return true if dst is replaced by subset 911 */ 912 bool extractSubset(SkBitmap* dst, const SkIRect& subset) const; 913 914 /** Copies SkRect of pixels from SkBitmap pixels to dstPixels. Copy starts at (srcX, srcY), 915 and does not exceed SkBitmap (width(), height()). 916 917 dstInfo specifies width, height, SkColorType, SkAlphaType, and 918 SkColorSpace of destination. dstRowBytes specifics the gap from one destination 919 row to the next. Returns true if pixels are copied. Returns false if: 920 - dstInfo.addr() equals nullptr 921 - dstRowBytes is less than dstInfo.minRowBytes() 922 - SkPixelRef is nullptr 923 924 Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is 925 kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType() must match. 926 If SkBitmap colorType() is kGray_8_SkColorType, dstInfo.colorSpace() must match. 927 If SkBitmap alphaType() is kOpaque_SkAlphaType, dstInfo.alphaType() must 928 match. If SkBitmap colorSpace() is nullptr, dstInfo.colorSpace() must match. Returns 929 false if pixel conversion is not possible. 930 931 srcX and srcY may be negative to copy only top or left of source. Returns 932 false if width() or height() is zero or negative. 933 Returns false if abs(srcX) >= Bitmap width(), or if abs(srcY) >= Bitmap height(). 934 935 If behavior is SkTransferFunctionBehavior::kRespect: converts source 936 pixels to a linear space before converting to dstInfo. 937 If behavior is SkTransferFunctionBehavior::kIgnore: source 938 pixels are treated as if they are linear, regardless of how they are encoded. 939 940 @param dstInfo destination width, height, SkColorType, SkAlphaType, SkColorSpace 941 @param dstPixels destination pixel storage 942 @param dstRowBytes destination row length 943 @param srcX column index whose absolute value is less than width() 944 @param srcY row index whose absolute value is less than height() 945 @param behavior one of: SkTransferFunctionBehavior::kRespect, 946 SkTransferFunctionBehavior::kIgnore 947 @return true if pixels are copied to dstPixels 948 */ 949 bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, 950 int srcX, int srcY, SkTransferFunctionBehavior behavior) const; 951 952 /** Copies a SkRect of pixels from SkBitmap to dstPixels. Copy starts at (srcX, srcY), 953 and does not exceed SkBitmap (width(), height()). 954 955 dstInfo specifies width, height, SkColorType, SkAlphaType, and SkColorSpace of 956 destination. dstRowBytes specifics the gap from one destination row to the next. 957 Returns true if pixels are copied. Returns false if: 958 - dstInfo.addr() equals nullptr 959 - dstRowBytes is less than dstInfo.minRowBytes() 960 - SkPixelRef is nullptr 961 962 Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is 963 kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType() must match. 964 If SkBitmap colorType() is kGray_8_SkColorType, dstInfo.colorSpace() must match. 965 If SkBitmap alphaType() is kOpaque_SkAlphaType, dstInfo.alphaType() must 966 match. If SkBitmap colorSpace() is nullptr, dstInfo.colorSpace() must match. Returns 967 false if pixel conversion is not possible. 968 969 srcX and srcY may be negative to copy only top or left of source. Returns 970 false if width() or height() is zero or negative. 971 Returns false if abs(srcX) >= Bitmap width(), or if abs(srcY) >= Bitmap height(). 972 973 @param dstInfo destination width, height, SkColorType, SkAlphaType, SkColorSpace 974 @param dstPixels destination pixel storage 975 @param dstRowBytes destination row length 976 @param srcX column index whose absolute value is less than width() 977 @param srcY row index whose absolute value is less than height() 978 @return true if pixels are copied to dstPixels 979 */ 980 bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, 981 int srcX, int srcY) const { 982 return this->readPixels(dstInfo, dstPixels, dstRowBytes, srcX, srcY, 983 SkTransferFunctionBehavior::kRespect); 984 } 985 986 /** Copies a SkRect of pixels from SkBitmap to dst. Copy starts at (srcX, srcY), and 987 does not exceed SkBitmap (width(), height()). 988 989 dst specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage, 990 and row bytes of destination. dst.rowBytes() specifics the gap from one destination 991 row to the next. Returns true if pixels are copied. Returns false if: 992 - dst pixel storage equals nullptr 993 - dst.rowBytes is less than SkImageInfo::minRowBytes 994 - SkPixelRef is nullptr 995 996 Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is 997 kGray_8_SkColorType, or kAlpha_8_SkColorType; dst SkColorType must match. 998 If SkBitmap colorType() is kGray_8_SkColorType, dst SkColorSpace must match. 999 If SkBitmap alphaType() is kOpaque_SkAlphaType, dst SkAlphaType must 1000 match. If SkBitmap colorSpace() is nullptr, dst SkColorSpace must match. Returns 1001 false if pixel conversion is not possible. 1002 1003 srcX and srcY may be negative to copy only top or left of source. Returns 1004 false if width() or height() is zero or negative. 1005 Returns false if abs(srcX) >= Bitmap width(), or if abs(srcY) >= Bitmap height(). 1006 1007 @param dst destination SkPixmap: SkImageInfo, pixels, row bytes 1008 @param srcX column index whose absolute value is less than width() 1009 @param srcY row index whose absolute value is less than height() 1010 @return true if pixels are copied to dst 1011 */ 1012 bool readPixels(const SkPixmap& dst, int srcX, int srcY) const; 1013 1014 /** Copies a SkRect of pixels from SkBitmap to dst. Copy starts at (0, 0), and 1015 does not exceed SkBitmap (width(), height()). 1016 1017 dst specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage, 1018 and row bytes of destination. dst.rowBytes() specifics the gap from one destination 1019 row to the next. Returns true if pixels are copied. Returns false if: 1020 - dst pixel storage equals nullptr 1021 - dst.rowBytes is less than SkImageInfo::minRowBytes 1022 - SkPixelRef is nullptr 1023 1024 Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is 1025 kGray_8_SkColorType, or kAlpha_8_SkColorType; dst SkColorType must match. 1026 If SkBitmap colorType() is kGray_8_SkColorType, dst SkColorSpace must match. 1027 If SkBitmap alphaType() is kOpaque_SkAlphaType, dst SkAlphaType must 1028 match. If SkBitmap colorSpace() is nullptr, dst SkColorSpace must match. Returns 1029 false if pixel conversion is not possible. 1030 1031 @param dst destination SkPixmap: SkImageInfo, pixels, row bytes 1032 @return true if pixels are copied to dst 1033 */ 1034 bool readPixels(const SkPixmap& dst) const { 1035 return this->readPixels(dst, 0, 0); 1036 } 1037 1038 /** Copies a SkRect of pixels from src. Copy starts at (dstX, dstY), and does not exceed 1039 (src.width(), src.height()). 1040 1041 src specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage, 1042 and row bytes of source. src.rowBytes() specifics the gap from one source 1043 row to the next. Returns true if pixels are copied. Returns false if: 1044 - src pixel storage equals nullptr 1045 - src.rowBytes is less than SkImageInfo::minRowBytes 1046 - SkPixelRef is nullptr 1047 1048 Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is 1049 kGray_8_SkColorType, or kAlpha_8_SkColorType; src SkColorType must match. 1050 If SkBitmap colorType() is kGray_8_SkColorType, src SkColorSpace must match. 1051 If SkBitmap alphaType() is kOpaque_SkAlphaType, src SkAlphaType must 1052 match. If SkBitmap colorSpace() is nullptr, src SkColorSpace must match. Returns 1053 false if pixel conversion is not possible. 1054 1055 dstX and dstY may be negative to copy only top or left of source. Returns 1056 false if width() or height() is zero or negative. 1057 Returns false if abs(dstX) >= Bitmap width(), or if abs(dstY) >= Bitmap height(). 1058 1059 @param src source SkPixmap: SkImageInfo, pixels, row bytes 1060 @param dstX column index whose absolute value is less than width() 1061 @param dstY row index whose absolute value is less than height() 1062 @return true if src pixels are copied to SkBitmap 1063 */ 1064 bool writePixels(const SkPixmap& src, int dstX, int dstY) { 1065 return this->writePixels(src, dstX, dstY, SkTransferFunctionBehavior::kRespect); 1066 } 1067 1068 /** Copies a SkRect of pixels from src. Copy starts at (0, 0), and does not exceed 1069 (src.width(), src.height()). 1070 1071 src specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage, 1072 and row bytes of source. src.rowBytes() specifics the gap from one source 1073 row to the next. Returns true if pixels are copied. Returns false if: 1074 - src pixel storage equals nullptr 1075 - src.rowBytes is less than SkImageInfo::minRowBytes 1076 - SkPixelRef is nullptr 1077 1078 Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is 1079 kGray_8_SkColorType, or kAlpha_8_SkColorType; src SkColorType must match. 1080 If SkBitmap colorType() is kGray_8_SkColorType, src SkColorSpace must match. 1081 If SkBitmap alphaType() is kOpaque_SkAlphaType, src SkAlphaType must 1082 match. If SkBitmap colorSpace() is nullptr, src SkColorSpace must match. Returns 1083 false if pixel conversion is not possible. 1084 1085 @param src source SkPixmap: SkImageInfo, pixels, row bytes 1086 @return true if src pixels are copied to SkBitmap 1087 */ 1088 bool writePixels(const SkPixmap& src) { 1089 return this->writePixels(src, 0, 0); 1090 } 1091 1092 /** Copies a SkRect of pixels from src. Copy starts at (0, 0), and does not exceed 1093 (src.width(), src.height()). 1094 1095 src specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage, 1096 and row bytes of source. src.rowBytes() specifics the gap from one source 1097 row to the next. Returns true if pixels are copied. Returns false if: 1098 - src pixel storage equals nullptr 1099 - src.rowBytes is less than SkImageInfo::minRowBytes 1100 - SkPixelRef is nullptr 1101 1102 Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is 1103 kGray_8_SkColorType, or kAlpha_8_SkColorType; src SkColorType must match. 1104 If SkBitmap colorType() is kGray_8_SkColorType, src SkColorSpace must match. 1105 If SkBitmap alphaType() is kOpaque_SkAlphaType, src SkAlphaType must 1106 match. If SkBitmap colorSpace() is nullptr, src SkColorSpace must match. Returns 1107 false if pixel conversion is not possible. Returns false if width() or height() 1108 is zero or negative. 1109 1110 If behavior is SkTransferFunctionBehavior::kRespect: converts src 1111 pixels to a linear space before converting to SkImageInfo. 1112 If behavior is SkTransferFunctionBehavior::kIgnore: src 1113 pixels are treated as if they are linear, regardless of how they are encoded. 1114 1115 @param src source SkPixmap: SkImageInfo, pixels, row bytes 1116 @param x column index whose absolute value is less than width() 1117 @param y row index whose absolute value is less than height() 1118 @param behavior one of: SkTransferFunctionBehavior::kRespect, 1119 SkTransferFunctionBehavior::kIgnore 1120 @return true if src pixels are copied to SkBitmap 1121 */ 1122 bool writePixels(const SkPixmap& src, int x, int y, SkTransferFunctionBehavior behavior); 1123 1124 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK 1125 /** Android framework only. 1126 1127 @return true if setHasHardwareMipMap() has been called with true 1128 */ 1129 bool hasHardwareMipMap() const { 1130 return (fFlags & kHasHardwareMipMap_Flag) != 0; 1131 } 1132 1133 /** Android framework only. 1134 1135 @param hasHardwareMipMap sets state 1136 */ 1137 void setHasHardwareMipMap(bool hasHardwareMipMap) { 1138 if (hasHardwareMipMap) { 1139 fFlags |= kHasHardwareMipMap_Flag; 1140 } else { 1141 fFlags &= ~kHasHardwareMipMap_Flag; 1142 } 1143 } 1144 #endif 1145 1146 /** Sets dst to alpha described by pixels. Returns false if dst cannot be written to 1147 or dst pixels cannot be allocated. 1148 1149 Uses HeapAllocator to reserve memory for dst SkPixelRef. 1150 1151 @param dst holds SkPixelRef to fill with alpha layer 1152 @return true if alpha layer was constructed in dst SkPixelRef 1153 */ 1154 bool extractAlpha(SkBitmap* dst) const { 1155 return this->extractAlpha(dst, nullptr, nullptr, nullptr); 1156 } 1157 1158 /** Sets dst to alpha described by pixels. Returns false if dst cannot be written to 1159 or dst pixels cannot be allocated. 1160 1161 If paint is not nullptr and contains SkMaskFilter, SkMaskFilter 1162 generates mask alpha from SkBitmap. Uses HeapAllocator to reserve memory for dst 1163 SkPixelRef. Sets offset to top-left position for dst for alignment with SkBitmap; 1164 (0, 0) unless SkMaskFilter generates mask. 1165 1166 @param dst holds SkPixelRef to fill with alpha layer 1167 @param paint holds optional SkMaskFilter; may be nullptr 1168 @param offset top-left position for dst; may be nullptr 1169 @return true if alpha layer was constructed in dst SkPixelRef 1170 */ 1171 bool extractAlpha(SkBitmap* dst, const SkPaint* paint, 1172 SkIPoint* offset) const { 1173 return this->extractAlpha(dst, paint, nullptr, offset); 1174 } 1175 1176 /** Sets dst to alpha described by pixels. Returns false if dst cannot be written to 1177 or dst pixels cannot be allocated. 1178 1179 If paint is not nullptr and contains SkMaskFilter, SkMaskFilter 1180 generates mask alpha from SkBitmap. allocator may reference a custom allocation 1181 class or be set to nullptr to use HeapAllocator. Sets offset to top-left 1182 position for dst for alignment with SkBitmap; (0, 0) unless SkMaskFilter generates 1183 mask. 1184 1185 @param dst holds SkPixelRef to fill with alpha layer 1186 @param paint holds optional SkMaskFilter; may be nullptr 1187 @param allocator method to reserve memory for SkPixelRef; may be nullptr 1188 @param offset top-left position for dst; may be nullptr 1189 @return true if alpha layer was constructed in dst SkPixelRef 1190 */ 1191 bool extractAlpha(SkBitmap* dst, const SkPaint* paint, Allocator* allocator, 1192 SkIPoint* offset) const; 1193 1194 /** Copies SkBitmap pixel address, row bytes, and SkImageInfo to pixmap, if address 1195 is available, and returns true. If pixel address is not available, return 1196 false and leave pixmap unchanged. 1197 1198 pixmap contents become invalid on any future change to SkBitmap. 1199 1200 @param pixmap storage for pixel state if pixels are readable; otherwise, ignored 1201 @return true if SkBitmap has direct access to pixels 1202 */ 1203 bool peekPixels(SkPixmap* pixmap) const; 1204 1205 /** Asserts if internal values are illegal or inconsistent. Only available if 1206 SK_DEBUG is defined at compile time. 1207 */ 1208 SkDEBUGCODE(void validate() const;) 1209 1210 /** \class SkBitmap::Allocator 1211 Abstract subclass of HeapAllocator. 1212 */ 1213 class Allocator : public SkRefCnt { 1214 public: 1215 1216 /** Allocates the pixel memory for the bitmap, given its dimensions and 1217 SkColorType. Returns true on success, where success means either setPixels() 1218 or setPixelRef() was called. 1219 1220 @param bitmap SkBitmap containing SkImageInfo as input, and SkPixelRef as output 1221 @return true if SkPixelRef was allocated 1222 */ 1223 virtual bool allocPixelRef(SkBitmap* bitmap) = 0; 1224 private: 1225 typedef SkRefCnt INHERITED; 1226 }; 1227 1228 /** \class SkBitmap::HeapAllocator 1229 Subclass of SkBitmap::Allocator that returns a SkPixelRef that allocates its pixel 1230 memory from the heap. This is the default SkBitmap::Allocator invoked by 1231 allocPixels(). 1232 */ 1233 class HeapAllocator : public Allocator { 1234 public: 1235 1236 /** Allocates the pixel memory for the bitmap, given its dimensions and 1237 SkColorType. Returns true on success, where success means either setPixels() 1238 or setPixelRef() was called. 1239 1240 @param bitmap SkBitmap containing SkImageInfo as input, and SkPixelRef as output 1241 @return true if pixels are allocated 1242 */ 1243 bool allocPixelRef(SkBitmap* bitmap) override; 1244 }; 1245 1246 /** macro expands to: void toString(SkString* str) const; 1247 Creates string representation of SkBitmap. The representation is read by 1248 internal debugging tools. The interface and implementation may be 1249 suppressed by defining SK_IGNORE_TO_STRING. 1250 1251 @param str storage for string representation 1252 */ 1253 SK_TO_STRING_NONVIRT() 1254 1255 private: 1256 enum Flags { 1257 kImageIsVolatile_Flag = 0x02, 1258 #ifdef SK_BUILD_FOR_ANDROID 1259 /* A hint for the renderer responsible for drawing this bitmap 1260 * indicating that it should attempt to use mipmaps when this bitmap 1261 * is drawn scaled down. 1262 */ 1263 kHasHardwareMipMap_Flag = 0x08, 1264 #endif 1265 }; 1266 1267 sk_sp<SkPixelRef> fPixelRef; 1268 SkPixmap fPixmap; 1269 uint8_t fFlags; 1270 1271 friend class SkReadBuffer; // unflatten 1272 }; 1273 1274 /////////////////////////////////////////////////////////////////////////////// 1275 1276 inline uint32_t* SkBitmap::getAddr32(int x, int y) const { 1277 SkASSERT(fPixmap.addr()); 1278 return fPixmap.writable_addr32(x, y); 1279 } 1280 1281 inline uint16_t* SkBitmap::getAddr16(int x, int y) const { 1282 SkASSERT(fPixmap.addr()); 1283 return fPixmap.writable_addr16(x, y); 1284 } 1285 1286 inline uint8_t* SkBitmap::getAddr8(int x, int y) const { 1287 SkASSERT(fPixmap.addr()); 1288 return fPixmap.writable_addr8(x, y); 1289 } 1290 1291 #endif 1292