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 "SkColorTable.h" 13 #include "SkImageInfo.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 SkPixelRefFactory; 23 class SkRegion; 24 class SkString; 25 class GrTexture; 26 27 /** \class SkBitmap 28 29 The SkBitmap class specifies a raster bitmap. A bitmap has an integer width 30 and height, and a format (colortype), and a pointer to the actual pixels. 31 Bitmaps can be drawn into a SkCanvas, but they are also used to specify the 32 target of a SkCanvas' drawing operations. 33 A const SkBitmap exposes getAddr(), which lets a caller write its pixels; 34 the constness is considered to apply to the bitmap's configuration, not 35 its contents. 36 */ 37 class SK_API SkBitmap { 38 public: 39 class SK_API Allocator; 40 41 #ifdef SK_SUPPORT_LEGACY_BITMAP_CONFIG 42 enum Config { 43 kNo_Config, //!< bitmap has not been configured 44 kA8_Config, //!< 8-bits per pixel, with only alpha specified (0 is transparent, 0xFF is opaque) 45 kIndex8_Config, //!< 8-bits per pixel, using SkColorTable to specify the colors 46 kRGB_565_Config, //!< 16-bits per pixel, (see SkColorPriv.h for packing) 47 kARGB_4444_Config, //!< 16-bits per pixel, (see SkColorPriv.h for packing) 48 kARGB_8888_Config, //!< 32-bits per pixel, (see SkColorPriv.h for packing) 49 }; 50 51 // do not add this to the Config enum, otherwise the compiler will let us 52 // pass this as a valid parameter for Config. 53 enum { 54 kConfigCount = kARGB_8888_Config + 1 55 }; 56 57 /** Return the config for the bitmap. */ 58 Config config() const; 59 60 SK_ATTR_DEPRECATED("use config()") 61 Config getConfig() const { return this->config(); } 62 #endif 63 64 /** 65 * Default construct creates a bitmap with zero width and height, and no pixels. 66 * Its colortype is set to kUnknown_SkColorType. 67 */ 68 SkBitmap(); 69 70 /** 71 * Copy the settings from the src into this bitmap. If the src has pixels 72 * allocated, they will be shared, not copied, so that the two bitmaps will 73 * reference the same memory for the pixels. If a deep copy is needed, 74 * where the new bitmap has its own separate copy of the pixels, use 75 * deepCopyTo(). 76 */ 77 SkBitmap(const SkBitmap& src); 78 79 ~SkBitmap(); 80 81 /** Copies the src bitmap into this bitmap. Ownership of the src bitmap's pixels remains 82 with the src bitmap. 83 */ 84 SkBitmap& operator=(const SkBitmap& src); 85 /** Swap the fields of the two bitmaps. This routine is guaranteed to never fail or throw. 86 */ 87 // This method is not exported to java. 88 void swap(SkBitmap& other); 89 90 /////////////////////////////////////////////////////////////////////////// 91 92 const SkImageInfo& info() const { return fInfo; } 93 94 int width() const { return fInfo.fWidth; } 95 int height() const { return fInfo.fHeight; } 96 SkColorType colorType() const { return fInfo.fColorType; } 97 SkAlphaType alphaType() const { return fInfo.fAlphaType; } 98 99 #ifdef SK_SUPPORT_LEGACY_ASIMAGEINFO 100 bool asImageInfo(SkImageInfo* info) const { 101 // compatibility: return false for kUnknown 102 if (kUnknown_SkColorType == this->colorType()) { 103 return false; 104 } 105 if (info) { 106 *info = this->info(); 107 } 108 return true; 109 } 110 #endif 111 112 /** 113 * Return the number of bytes per pixel based on the colortype. If the colortype is 114 * kUnknown_SkColorType, then 0 is returned. 115 */ 116 int bytesPerPixel() const { return fInfo.bytesPerPixel(); } 117 118 /** 119 * Return the rowbytes expressed as a number of pixels (like width and height). 120 * If the colortype is kUnknown_SkColorType, then 0 is returned. 121 */ 122 int rowBytesAsPixels() const { 123 return fRowBytes >> this->shiftPerPixel(); 124 } 125 126 /** 127 * Return the shift amount per pixel (i.e. 0 for 1-byte per pixel, 1 for 2-bytes per pixel 128 * colortypes, 2 for 4-bytes per pixel colortypes). Return 0 for kUnknown_SkColorType. 129 */ 130 int shiftPerPixel() const { return this->bytesPerPixel() >> 1; } 131 132 /////////////////////////////////////////////////////////////////////////// 133 134 /** Return true iff the bitmap has empty dimensions. 135 * Hey! Before you use this, see if you really want to know drawsNothing() instead. 136 */ 137 bool empty() const { return fInfo.isEmpty(); } 138 139 /** Return true iff the bitmap has no pixelref. Note: this can return true even if the 140 * dimensions of the bitmap are > 0 (see empty()). 141 * Hey! Before you use this, see if you really want to know drawsNothing() instead. 142 */ 143 bool isNull() const { return NULL == fPixelRef; } 144 145 /** Return true iff drawing this bitmap has no effect. 146 */ 147 bool drawsNothing() const { return this->empty() || this->isNull(); } 148 149 /** Return the number of bytes between subsequent rows of the bitmap. */ 150 size_t rowBytes() const { return fRowBytes; } 151 152 /** 153 * Set the bitmap's alphaType, returning true on success. If false is 154 * returned, then the specified new alphaType is incompatible with the 155 * colortype, and the current alphaType is unchanged. 156 * 157 * Note: this changes the alphatype for the underlying pixels, which means 158 * that all bitmaps that might be sharing (subsets of) the pixels will 159 * be affected. 160 */ 161 bool setAlphaType(SkAlphaType); 162 163 /** Return the address of the pixels for this SkBitmap. 164 */ 165 void* getPixels() const { return fPixels; } 166 167 /** Return the byte size of the pixels, based on the height and rowBytes. 168 Note this truncates the result to 32bits. Call getSize64() to detect 169 if the real size exceeds 32bits. 170 */ 171 size_t getSize() const { return fInfo.fHeight * fRowBytes; } 172 173 /** Return the number of bytes from the pointer returned by getPixels() 174 to the end of the allocated space in the buffer. Required in 175 cases where extractSubset has been called. 176 */ 177 size_t getSafeSize() const { return fInfo.getSafeSize(fRowBytes); } 178 179 /** 180 * Return the full size of the bitmap, in bytes. 181 */ 182 int64_t computeSize64() const { 183 return sk_64_mul(fInfo.fHeight, fRowBytes); 184 } 185 186 /** 187 * Return the number of bytes from the pointer returned by getPixels() 188 * to the end of the allocated space in the buffer. This may be smaller 189 * than computeSize64() if there is any rowbytes padding beyond the width. 190 */ 191 int64_t computeSafeSize64() const { 192 return fInfo.getSafeSize64(fRowBytes); 193 } 194 195 /** Returns true if this bitmap is marked as immutable, meaning that the 196 contents of its pixels will not change for the lifetime of the bitmap. 197 */ 198 bool isImmutable() const; 199 200 /** Marks this bitmap as immutable, meaning that the contents of its 201 pixels will not change for the lifetime of the bitmap and of the 202 underlying pixelref. This state can be set, but it cannot be 203 cleared once it is set. This state propagates to all other bitmaps 204 that share the same pixelref. 205 */ 206 void setImmutable(); 207 208 /** Returns true if the bitmap is opaque (has no translucent/transparent pixels). 209 */ 210 bool isOpaque() const { 211 return SkAlphaTypeIsOpaque(this->alphaType()); 212 } 213 214 /** Returns true if the bitmap is volatile (i.e. should not be cached by devices.) 215 */ 216 bool isVolatile() const; 217 218 /** Specify whether this bitmap is volatile. Bitmaps are not volatile by 219 default. Temporary bitmaps that are discarded after use should be 220 marked as volatile. This provides a hint to the device that the bitmap 221 should not be cached. Providing this hint when appropriate can 222 improve performance by avoiding unnecessary overhead and resource 223 consumption on the device. 224 */ 225 void setIsVolatile(bool); 226 227 /** Reset the bitmap to its initial state (see default constructor). If we are a (shared) 228 owner of the pixels, that ownership is decremented. 229 */ 230 void reset(); 231 232 #ifdef SK_SUPPORT_LEGACY_COMPUTE_CONFIG_SIZE 233 /** Given a config and a width, this computes the optimal rowBytes value. This is called automatically 234 if you pass 0 for rowBytes to setConfig(). 235 */ 236 static size_t ComputeRowBytes(Config c, int width); 237 238 /** Return the bytes-per-pixel for the specified config. If the config is 239 not at least 1-byte per pixel, return 0, including for kNo_Config. 240 */ 241 static int ComputeBytesPerPixel(Config c); 242 243 /** Return the shift-per-pixel for the specified config. If the config is 244 not at least 1-byte per pixel, return 0, including for kNo_Config. 245 */ 246 static int ComputeShiftPerPixel(Config c) { 247 return ComputeBytesPerPixel(c) >> 1; 248 } 249 250 static int64_t ComputeSize64(Config, int width, int height); 251 static size_t ComputeSize(Config, int width, int height); 252 #endif 253 254 /** 255 * This will brute-force return true if all of the pixels in the bitmap 256 * are opaque. If it fails to read the pixels, or encounters an error, 257 * it will return false. 258 * 259 * Since this can be an expensive operation, the bitmap stores a flag for 260 * this (isOpaque). Only call this if you need to compute this value from 261 * "unknown" pixels. 262 */ 263 static bool ComputeIsOpaque(const SkBitmap&); 264 265 /** 266 * Return the bitmap's bounds [0, 0, width, height] as an SkRect 267 */ 268 void getBounds(SkRect* bounds) const; 269 void getBounds(SkIRect* bounds) const; 270 271 #ifdef SK_SUPPORT_LEGACY_SETCONFIG 272 /** Set the bitmap's config and dimensions. If rowBytes is 0, then 273 ComputeRowBytes() is called to compute the optimal value. This resets 274 any pixel/colortable ownership, just like reset(). 275 */ 276 bool setConfig(Config, int width, int height, size_t rowBytes, SkAlphaType); 277 278 bool setConfig(Config config, int width, int height, size_t rowBytes = 0) { 279 return this->setConfig(config, width, height, rowBytes, 280 kPremul_SkAlphaType); 281 } 282 #endif 283 284 bool setInfo(const SkImageInfo&, size_t rowBytes = 0); 285 286 #ifdef SK_SUPPORT_LEGACY_SETCONFIG_INFO 287 bool setConfig(const SkImageInfo& info, size_t rowBytes = 0) { 288 return this->setInfo(info, rowBytes); 289 } 290 #endif 291 292 /** 293 * Allocate a pixelref to match the specified image info. If the Factory 294 * is non-null, call it to allcoate the pixelref. If the ImageInfo requires 295 * a colortable, then ColorTable must be non-null, and will be ref'd. 296 * On failure, the bitmap will be set to empty and return false. 297 */ 298 bool allocPixels(const SkImageInfo&, SkPixelRefFactory*, SkColorTable*); 299 300 /** 301 * Allocate a pixelref to match the specified image info, using the default 302 * allocator. 303 * On success, the bitmap's pixels will be "locked", and return true. 304 * On failure, the bitmap will be set to empty and return false. 305 */ 306 bool allocPixels(const SkImageInfo& info) { 307 return this->allocPixels(info, NULL, NULL); 308 } 309 310 bool allocN32Pixels(int width, int height, bool isOpaque = false) { 311 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); 312 if (isOpaque) { 313 info.fAlphaType = kOpaque_SkAlphaType; 314 } 315 return this->allocPixels(info); 316 } 317 318 /** 319 * Install a pixelref that wraps the specified pixels and rowBytes, and 320 * optional ReleaseProc and context. When the pixels are no longer 321 * referenced, if releaseProc is not null, it will be called with the 322 * pixels and context as parameters. 323 * On failure, the bitmap will be set to empty and return false. 324 */ 325 bool installPixels(const SkImageInfo&, void* pixels, size_t rowBytes, SkColorTable*, 326 void (*releaseProc)(void* addr, void* context), void* context); 327 328 #ifdef SK_SUPPORT_LEGACY_INSTALLPIXELSPARAMS 329 bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, 330 void (*releaseProc)(void* addr, void* context), 331 void* context) { 332 return this->installPixels(info, pixels, rowBytes, NULL, releaseProc, context); 333 } 334 #endif 335 336 /** 337 * Call installPixels with no ReleaseProc specified. This means that the 338 * caller must ensure that the specified pixels are valid for the lifetime 339 * of the created bitmap (and its pixelRef). 340 */ 341 bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) { 342 return this->installPixels(info, pixels, rowBytes, NULL, NULL, NULL); 343 } 344 345 /** 346 * Calls installPixels() with the value in the SkMask. The caller must 347 * ensure that the specified mask pixels are valid for the lifetime 348 * of the created bitmap (and its pixelRef). 349 */ 350 bool installMaskPixels(const SkMask&); 351 352 /** Use this to assign a new pixel address for an existing bitmap. This 353 will automatically release any pixelref previously installed. Only call 354 this if you are handling ownership/lifetime of the pixel memory. 355 356 If the bitmap retains a reference to the colortable (assuming it is 357 not null) it will take care of incrementing the reference count. 358 359 @param pixels Address for the pixels, managed by the caller. 360 @param ctable ColorTable (or null) that matches the specified pixels 361 */ 362 void setPixels(void* p, SkColorTable* ctable = NULL); 363 364 /** Copies the bitmap's pixels to the location pointed at by dst and returns 365 true if possible, returns false otherwise. 366 367 In the case when the dstRowBytes matches the bitmap's rowBytes, the copy 368 may be made faster by copying over the dst's per-row padding (for all 369 rows but the last). By setting preserveDstPad to true the caller can 370 disable this optimization and ensure that pixels in the padding are not 371 overwritten. 372 373 Always returns false for RLE formats. 374 375 @param dst Location of destination buffer. 376 @param dstSize Size of destination buffer. Must be large enough to hold 377 pixels using indicated stride. 378 @param dstRowBytes Width of each line in the buffer. If 0, uses 379 bitmap's internal stride. 380 @param preserveDstPad Must we preserve padding in the dst 381 */ 382 bool copyPixelsTo(void* const dst, size_t dstSize, size_t dstRowBytes = 0, 383 bool preserveDstPad = false) const; 384 385 /** Use the standard HeapAllocator to create the pixelref that manages the 386 pixel memory. It will be sized based on the current ImageInfo. 387 If this is called multiple times, a new pixelref object will be created 388 each time. 389 390 If the bitmap retains a reference to the colortable (assuming it is 391 not null) it will take care of incrementing the reference count. 392 393 @param ctable ColorTable (or null) to use with the pixels that will 394 be allocated. Only used if colortype == kIndex_8_SkColorType 395 @return true if the allocation succeeds. If not the pixelref field of 396 the bitmap will be unchanged. 397 */ 398 bool allocPixels(SkColorTable* ctable = NULL) { 399 return this->allocPixels(NULL, ctable); 400 } 401 402 /** Use the specified Allocator to create the pixelref that manages the 403 pixel memory. It will be sized based on the current ImageInfo. 404 If this is called multiple times, a new pixelref object will be created 405 each time. 406 407 If the bitmap retains a reference to the colortable (assuming it is 408 not null) it will take care of incrementing the reference count. 409 410 @param allocator The Allocator to use to create a pixelref that can 411 manage the pixel memory for the current ImageInfo. 412 If allocator is NULL, the standard HeapAllocator will be used. 413 @param ctable ColorTable (or null) to use with the pixels that will 414 be allocated. Only used if colortype == kIndex_8_SkColorType. 415 If it is non-null and the colortype is not indexed, it will 416 be ignored. 417 @return true if the allocation succeeds. If not the pixelref field of 418 the bitmap will be unchanged. 419 */ 420 bool allocPixels(Allocator* allocator, SkColorTable* ctable); 421 422 /** 423 * Return the current pixelref object or NULL if there is none. This does 424 * not affect the refcount of the pixelref. 425 */ 426 SkPixelRef* pixelRef() const { return fPixelRef; } 427 428 /** 429 * A bitmap can reference a subset of a pixelref's pixels. That means the 430 * bitmap's width/height can be <= the dimensions of the pixelref. The 431 * pixelref origin is the x,y location within the pixelref's pixels for 432 * the bitmap's top/left corner. To be valid the following must be true: 433 * 434 * origin_x + bitmap_width <= pixelref_width 435 * origin_y + bitmap_height <= pixelref_height 436 * 437 * pixelRefOrigin() returns this origin, or (0,0) if there is no pixelRef. 438 */ 439 SkIPoint pixelRefOrigin() const { return fPixelRefOrigin; } 440 441 /** 442 * Assign a pixelref and origin to the bitmap. Pixelrefs are reference, 443 * so the existing one (if any) will be unref'd and the new one will be 444 * ref'd. (x,y) specify the offset within the pixelref's pixels for the 445 * top/left corner of the bitmap. For a bitmap that encompases the entire 446 * pixels of the pixelref, these will be (0,0). 447 */ 448 SkPixelRef* setPixelRef(SkPixelRef* pr, int dx, int dy); 449 450 SkPixelRef* setPixelRef(SkPixelRef* pr, const SkIPoint& origin) { 451 return this->setPixelRef(pr, origin.fX, origin.fY); 452 } 453 454 SkPixelRef* setPixelRef(SkPixelRef* pr) { 455 return this->setPixelRef(pr, 0, 0); 456 } 457 458 /** Call this to ensure that the bitmap points to the current pixel address 459 in the pixelref. Balance it with a call to unlockPixels(). These calls 460 are harmless if there is no pixelref. 461 */ 462 void lockPixels() const; 463 /** When you are finished access the pixel memory, call this to balance a 464 previous call to lockPixels(). This allows pixelrefs that implement 465 cached/deferred image decoding to know when there are active clients of 466 a given image. 467 */ 468 void unlockPixels() const; 469 470 /** 471 * Some bitmaps can return a copy of their pixels for lockPixels(), but 472 * that copy, if modified, will not be pushed back. These bitmaps should 473 * not be used as targets for a raster device/canvas (since all pixels 474 * modifications will be lost when unlockPixels() is called.) 475 */ 476 bool lockPixelsAreWritable() const; 477 478 /** Call this to be sure that the bitmap is valid enough to be drawn (i.e. 479 it has non-null pixels, and if required by its colortype, it has a 480 non-null colortable. Returns true if all of the above are met. 481 */ 482 bool readyToDraw() const { 483 return this->getPixels() != NULL && 484 (this->colorType() != kIndex_8_SkColorType || NULL != fColorTable); 485 } 486 487 /** Returns the pixelRef's texture, or NULL 488 */ 489 GrTexture* getTexture() const; 490 491 /** Return the bitmap's colortable, if it uses one (i.e. colorType is 492 Index_8) and the pixels are locked. 493 Otherwise returns NULL. Does not affect the colortable's 494 reference count. 495 */ 496 SkColorTable* getColorTable() const { return fColorTable; } 497 498 /** Returns a non-zero, unique value corresponding to the pixels in our 499 pixelref. Each time the pixels are changed (and notifyPixelsChanged 500 is called), a different generation ID will be returned. Finally, if 501 there is no pixelRef then zero is returned. 502 */ 503 uint32_t getGenerationID() const; 504 505 /** Call this if you have changed the contents of the pixels. This will in- 506 turn cause a different generation ID value to be returned from 507 getGenerationID(). 508 */ 509 void notifyPixelsChanged() const; 510 511 /** 512 * Fill the entire bitmap with the specified color. 513 * If the bitmap's colortype does not support alpha (e.g. 565) then the alpha 514 * of the color is ignored (treated as opaque). If the colortype only supports 515 * alpha (e.g. A1 or A8) then the color's r,g,b components are ignored. 516 */ 517 void eraseColor(SkColor c) const { 518 this->eraseARGB(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c), 519 SkColorGetB(c)); 520 } 521 522 /** 523 * Fill the entire bitmap with the specified color. 524 * If the bitmap's colortype does not support alpha (e.g. 565) then the alpha 525 * of the color is ignored (treated as opaque). If the colortype only supports 526 * alpha (e.g. A1 or A8) then the color's r,g,b components are ignored. 527 */ 528 void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const; 529 530 SK_ATTR_DEPRECATED("use eraseARGB or eraseColor") 531 void eraseRGB(U8CPU r, U8CPU g, U8CPU b) const { 532 this->eraseARGB(0xFF, r, g, b); 533 } 534 535 /** 536 * Fill the specified area of this bitmap with the specified color. 537 * If the bitmap's colortype does not support alpha (e.g. 565) then the alpha 538 * of the color is ignored (treated as opaque). If the colortype only supports 539 * alpha (e.g. A1 or A8) then the color's r,g,b components are ignored. 540 */ 541 void eraseArea(const SkIRect& area, SkColor c) const; 542 543 /** Scroll (a subset of) the contents of this bitmap by dx/dy. If there are 544 no pixels allocated (i.e. getPixels() returns null) the method will 545 still update the inval region (if present). If the bitmap is immutable, 546 do nothing and return false. 547 548 @param subset The subset of the bitmap to scroll/move. To scroll the 549 entire contents, specify [0, 0, width, height] or just 550 pass null. 551 @param dx The amount to scroll in X 552 @param dy The amount to scroll in Y 553 @param inval Optional (may be null). Returns the area of the bitmap that 554 was scrolled away. E.g. if dx = dy = 0, then inval would 555 be set to empty. If dx >= width or dy >= height, then 556 inval would be set to the entire bounds of the bitmap. 557 @return true if the scroll was doable. Will return false if the colortype is kUnkown or 558 if the bitmap is immutable. 559 If no pixels are present (i.e. getPixels() returns false) 560 inval will still be updated, and true will be returned. 561 */ 562 bool scrollRect(const SkIRect* subset, int dx, int dy, 563 SkRegion* inval = NULL) const; 564 565 /** 566 * Return the SkColor of the specified pixel. In most cases this will 567 * require un-premultiplying the color. Alpha only colortypes (e.g. kAlpha_8_SkColorType) 568 * return black with the appropriate alpha set. The value is undefined 569 * for kUnknown_SkColorType or if x or y are out of bounds, or if the bitmap 570 * does not have any pixels (or has not be locked with lockPixels()). 571 */ 572 SkColor getColor(int x, int y) const; 573 574 /** Returns the address of the specified pixel. This performs a runtime 575 check to know the size of the pixels, and will return the same answer 576 as the corresponding size-specific method (e.g. getAddr16). Since the 577 check happens at runtime, it is much slower than using a size-specific 578 version. Unlike the size-specific methods, this routine also checks if 579 getPixels() returns null, and returns that. The size-specific routines 580 perform a debugging assert that getPixels() is not null, but they do 581 not do any runtime checks. 582 */ 583 void* getAddr(int x, int y) const; 584 585 /** Returns the address of the pixel specified by x,y for 32bit pixels. 586 * In debug build, this asserts that the pixels are allocated and locked, 587 * and that the colortype is 32-bit, however none of these checks are performed 588 * in the release build. 589 */ 590 inline uint32_t* getAddr32(int x, int y) const; 591 592 /** Returns the address of the pixel specified by x,y for 16bit pixels. 593 * In debug build, this asserts that the pixels are allocated and locked, 594 * and that the colortype is 16-bit, however none of these checks are performed 595 * in the release build. 596 */ 597 inline uint16_t* getAddr16(int x, int y) const; 598 599 /** Returns the address of the pixel specified by x,y for 8bit pixels. 600 * In debug build, this asserts that the pixels are allocated and locked, 601 * and that the colortype is 8-bit, however none of these checks are performed 602 * in the release build. 603 */ 604 inline uint8_t* getAddr8(int x, int y) const; 605 606 /** Returns the color corresponding to the pixel specified by x,y for 607 * colortable based bitmaps. 608 * In debug build, this asserts that the pixels are allocated and locked, 609 * that the colortype is indexed, and that the colortable is allocated, 610 * however none of these checks are performed in the release build. 611 */ 612 inline SkPMColor getIndex8Color(int x, int y) const; 613 614 /** Set dst to be a setset of this bitmap. If possible, it will share the 615 pixel memory, and just point into a subset of it. However, if the colortype 616 does not support this, a local copy will be made and associated with 617 the dst bitmap. If the subset rectangle, intersected with the bitmap's 618 dimensions is empty, or if there is an unsupported colortype, false will be 619 returned and dst will be untouched. 620 @param dst The bitmap that will be set to a subset of this bitmap 621 @param subset The rectangle of pixels in this bitmap that dst will 622 reference. 623 @return true if the subset copy was successfully made. 624 */ 625 bool extractSubset(SkBitmap* dst, const SkIRect& subset) const; 626 627 /** Makes a deep copy of this bitmap, respecting the requested colorType, 628 * and allocating the dst pixels on the cpu. 629 * Returns false if either there is an error (i.e. the src does not have 630 * pixels) or the request cannot be satisfied (e.g. the src has per-pixel 631 * alpha, and the requested colortype does not support alpha). 632 * @param dst The bitmap to be sized and allocated 633 * @param ct The desired colorType for dst 634 * @param allocator Allocator used to allocate the pixelref for the dst 635 * bitmap. If this is null, the standard HeapAllocator 636 * will be used. 637 * @return true if the copy was made. 638 */ 639 bool copyTo(SkBitmap* dst, SkColorType ct, Allocator* = NULL) const; 640 641 bool copyTo(SkBitmap* dst, Allocator* allocator = NULL) const { 642 return this->copyTo(dst, this->colorType(), allocator); 643 } 644 645 /** 646 * Returns true if this bitmap's pixels can be converted into the requested 647 * colorType, such that copyTo() could succeed. 648 */ 649 bool canCopyTo(SkColorType colorType) const; 650 651 /** Makes a deep copy of this bitmap, keeping the copied pixels 652 * in the same domain as the source: If the src pixels are allocated for 653 * the cpu, then so will the dst. If the src pixels are allocated on the 654 * gpu (typically as a texture), the it will do the same for the dst. 655 * If the request cannot be fulfilled, returns false and dst is unmodified. 656 */ 657 bool deepCopyTo(SkBitmap* dst) const; 658 659 #ifdef SK_BUILD_FOR_ANDROID 660 bool hasHardwareMipMap() const { 661 return (fFlags & kHasHardwareMipMap_Flag) != 0; 662 } 663 664 void setHasHardwareMipMap(bool hasHardwareMipMap) { 665 if (hasHardwareMipMap) { 666 fFlags |= kHasHardwareMipMap_Flag; 667 } else { 668 fFlags &= ~kHasHardwareMipMap_Flag; 669 } 670 } 671 #endif 672 673 bool extractAlpha(SkBitmap* dst) const { 674 return this->extractAlpha(dst, NULL, NULL, NULL); 675 } 676 677 bool extractAlpha(SkBitmap* dst, const SkPaint* paint, 678 SkIPoint* offset) const { 679 return this->extractAlpha(dst, paint, NULL, offset); 680 } 681 682 /** Set dst to contain alpha layer of this bitmap. If destination bitmap 683 fails to be initialized, e.g. because allocator can't allocate pixels 684 for it, dst will not be modified and false will be returned. 685 686 @param dst The bitmap to be filled with alpha layer 687 @param paint The paint to draw with 688 @param allocator Allocator used to allocate the pixelref for the dst 689 bitmap. If this is null, the standard HeapAllocator 690 will be used. 691 @param offset If not null, it is set to top-left coordinate to position 692 the returned bitmap so that it visually lines up with the 693 original 694 */ 695 bool extractAlpha(SkBitmap* dst, const SkPaint* paint, Allocator* allocator, 696 SkIPoint* offset) const; 697 698 SkDEBUGCODE(void validate() const;) 699 700 class Allocator : public SkRefCnt { 701 public: 702 SK_DECLARE_INST_COUNT(Allocator) 703 704 /** Allocate the pixel memory for the bitmap, given its dimensions and 705 colortype. Return true on success, where success means either setPixels 706 or setPixelRef was called. The pixels need not be locked when this 707 returns. If the colortype requires a colortable, it also must be 708 installed via setColorTable. If false is returned, the bitmap and 709 colortable should be left unchanged. 710 */ 711 virtual bool allocPixelRef(SkBitmap*, SkColorTable*) = 0; 712 private: 713 typedef SkRefCnt INHERITED; 714 }; 715 716 /** Subclass of Allocator that returns a pixelref that allocates its pixel 717 memory from the heap. This is the default Allocator invoked by 718 allocPixels(). 719 */ 720 class HeapAllocator : public Allocator { 721 public: 722 virtual bool allocPixelRef(SkBitmap*, SkColorTable*) SK_OVERRIDE; 723 }; 724 725 class RLEPixels { 726 public: 727 RLEPixels(int width, int height); 728 virtual ~RLEPixels(); 729 730 uint8_t* packedAtY(int y) const { 731 SkASSERT((unsigned)y < (unsigned)fHeight); 732 return fYPtrs[y]; 733 } 734 735 // called by subclasses during creation 736 void setPackedAtY(int y, uint8_t* addr) { 737 SkASSERT((unsigned)y < (unsigned)fHeight); 738 fYPtrs[y] = addr; 739 } 740 741 private: 742 uint8_t** fYPtrs; 743 int fHeight; 744 }; 745 746 SK_TO_STRING_NONVIRT() 747 748 private: 749 mutable SkPixelRef* fPixelRef; 750 mutable int fPixelLockCount; 751 // These are just caches from the locked pixelref 752 mutable void* fPixels; 753 mutable SkColorTable* fColorTable; // only meaningful for kIndex8 754 755 SkIPoint fPixelRefOrigin; 756 757 enum Flags { 758 kImageIsOpaque_Flag = 0x01, 759 kImageIsVolatile_Flag = 0x02, 760 kImageIsImmutable_Flag = 0x04, 761 #ifdef SK_BUILD_FOR_ANDROID 762 /* A hint for the renderer responsible for drawing this bitmap 763 * indicating that it should attempt to use mipmaps when this bitmap 764 * is drawn scaled down. 765 */ 766 kHasHardwareMipMap_Flag = 0x08, 767 #endif 768 }; 769 770 SkImageInfo fInfo; 771 772 uint32_t fRowBytes; 773 774 uint8_t fFlags; 775 776 void internalErase(const SkIRect&, U8CPU a, U8CPU r, U8CPU g, U8CPU b)const; 777 778 /* Unreference any pixelrefs or colortables 779 */ 780 void freePixels(); 781 void updatePixelsFromRef() const; 782 783 void legacyUnflatten(SkReadBuffer&); 784 785 static void WriteRawPixels(SkWriteBuffer*, const SkBitmap&); 786 static bool ReadRawPixels(SkReadBuffer*, SkBitmap*); 787 788 friend class SkBitmapSource; // unflatten 789 friend class SkReadBuffer; // unflatten, rawpixels 790 friend class SkWriteBuffer; // rawpixels 791 friend struct SkBitmapProcState; 792 }; 793 794 class SkAutoLockPixels : SkNoncopyable { 795 public: 796 SkAutoLockPixels(const SkBitmap& bm, bool doLock = true) : fBitmap(bm) { 797 fDidLock = doLock; 798 if (doLock) { 799 bm.lockPixels(); 800 } 801 } 802 ~SkAutoLockPixels() { 803 if (fDidLock) { 804 fBitmap.unlockPixels(); 805 } 806 } 807 808 private: 809 const SkBitmap& fBitmap; 810 bool fDidLock; 811 }; 812 //TODO(mtklein): uncomment when 71713004 lands and Chromium's fixed. 813 //#define SkAutoLockPixels(...) SK_REQUIRE_LOCAL_VAR(SkAutoLockPixels) 814 815 /** Helper class that performs the lock/unlockColors calls on a colortable. 816 The destructor will call unlockColors(false) if it has a bitmap's colortable 817 */ 818 class SkAutoLockColors : SkNoncopyable { 819 public: 820 /** Initialize with no bitmap. Call lockColors(bitmap) to lock bitmap's 821 colortable 822 */ 823 SkAutoLockColors() : fCTable(NULL), fColors(NULL) {} 824 /** Initialize with bitmap, locking its colortable if present 825 */ 826 explicit SkAutoLockColors(const SkBitmap& bm) { 827 fCTable = bm.getColorTable(); 828 fColors = fCTable ? fCTable->lockColors() : NULL; 829 } 830 /** Initialize with a colortable (may be null) 831 */ 832 explicit SkAutoLockColors(SkColorTable* ctable) { 833 fCTable = ctable; 834 fColors = ctable ? ctable->lockColors() : NULL; 835 } 836 ~SkAutoLockColors() { 837 if (fCTable) { 838 fCTable->unlockColors(); 839 } 840 } 841 842 /** Return the currently locked colors, or NULL if no bitmap's colortable 843 is currently locked. 844 */ 845 const SkPMColor* colors() const { return fColors; } 846 847 /** Locks the table and returns is colors (assuming ctable is not null) and 848 unlocks the previous table if one was present 849 */ 850 const SkPMColor* lockColors(SkColorTable* ctable) { 851 if (fCTable) { 852 fCTable->unlockColors(); 853 } 854 fCTable = ctable; 855 fColors = ctable ? ctable->lockColors() : NULL; 856 return fColors; 857 } 858 859 const SkPMColor* lockColors(const SkBitmap& bm) { 860 return this->lockColors(bm.getColorTable()); 861 } 862 863 private: 864 SkColorTable* fCTable; 865 const SkPMColor* fColors; 866 }; 867 #define SkAutoLockColors(...) SK_REQUIRE_LOCAL_VAR(SkAutoLockColors) 868 869 /////////////////////////////////////////////////////////////////////////////// 870 871 inline uint32_t* SkBitmap::getAddr32(int x, int y) const { 872 SkASSERT(fPixels); 873 SkASSERT(4 == this->bytesPerPixel()); 874 SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height()); 875 return (uint32_t*)((char*)fPixels + y * fRowBytes + (x << 2)); 876 } 877 878 inline uint16_t* SkBitmap::getAddr16(int x, int y) const { 879 SkASSERT(fPixels); 880 SkASSERT(2 == this->bytesPerPixel()); 881 SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height()); 882 return (uint16_t*)((char*)fPixels + y * fRowBytes + (x << 1)); 883 } 884 885 inline uint8_t* SkBitmap::getAddr8(int x, int y) const { 886 SkASSERT(fPixels); 887 SkASSERT(1 == this->bytesPerPixel()); 888 SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height()); 889 return (uint8_t*)fPixels + y * fRowBytes + x; 890 } 891 892 inline SkPMColor SkBitmap::getIndex8Color(int x, int y) const { 893 SkASSERT(fPixels); 894 SkASSERT(kIndex_8_SkColorType == this->colorType()); 895 SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height()); 896 SkASSERT(fColorTable); 897 return (*fColorTable)[*((const uint8_t*)fPixels + y * fRowBytes + x)]; 898 } 899 900 #ifdef SK_SUPPORT_LEGACY_BITMAP_CONFIG 901 /////////////////////////////////////////////////////////////////////////////// 902 // 903 // Helpers until we can fully deprecate SkBitmap::Config 904 // 905 extern SkBitmap::Config SkColorTypeToBitmapConfig(SkColorType); 906 extern SkColorType SkBitmapConfigToColorType(SkBitmap::Config); 907 #endif 908 909 #endif 910