1 2 /* 3 * Copyright 2006 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10 #ifndef SkBitmap_DEFINED 11 #define SkBitmap_DEFINED 12 13 #include "Sk64.h" 14 #include "SkColor.h" 15 #include "SkColorTable.h" 16 #include "SkPoint.h" 17 #include "SkRefCnt.h" 18 19 struct SkIRect; 20 struct SkRect; 21 class SkPaint; 22 class SkPixelRef; 23 class SkRegion; 24 class SkString; 25 26 // This is an opaque class, not interpreted by skia 27 class SkGpuTexture; 28 29 /** \class SkBitmap 30 31 The SkBitmap class specifies a raster bitmap. A bitmap has an integer width 32 and height, and a format (config), and a pointer to the actual pixels. 33 Bitmaps can be drawn into a SkCanvas, but they are also used to specify the 34 target of a SkCanvas' drawing operations. 35 A const SkBitmap exposes getAddr(), which lets a caller write its pixels; 36 the constness is considered to apply to the bitmap's configuration, not 37 its contents. 38 */ 39 class SK_API SkBitmap { 40 public: 41 class Allocator; 42 43 enum Config { 44 kNo_Config, //!< bitmap has not been configured 45 /** 46 * 1-bit per pixel, (0 is transparent, 1 is opaque) 47 * Valid as a destination (target of a canvas), but not valid as a src. 48 * i.e. you can draw into a 1-bit bitmap, but you cannot draw from one. 49 */ 50 kA1_Config, 51 kA8_Config, //!< 8-bits per pixel, with only alpha specified (0 is transparent, 0xFF is opaque) 52 kIndex8_Config, //!< 8-bits per pixel, using SkColorTable to specify the colors 53 kRGB_565_Config, //!< 16-bits per pixel, (see SkColorPriv.h for packing) 54 kARGB_4444_Config, //!< 16-bits per pixel, (see SkColorPriv.h for packing) 55 kARGB_8888_Config, //!< 32-bits per pixel, (see SkColorPriv.h for packing) 56 /** 57 * Custom compressed format, not supported on all platforms. 58 * Cannot be used as a destination (target of a canvas). 59 * i.e. you may be able to draw from one, but you cannot draw into one. 60 */ 61 kRLE_Index8_Config, 62 63 kConfigCount 64 }; 65 66 /** 67 * Default construct creates a bitmap with zero width and height, and no pixels. 68 * Its config is set to kNo_Config. 69 */ 70 SkBitmap(); 71 72 /** 73 * Copy the settings from the src into this bitmap. If the src has pixels 74 * allocated, they will be shared, not copied, so that the two bitmaps will 75 * reference the same memory for the pixels. If a deep copy is needed, 76 * where the new bitmap has its own separate copy of the pixels, use 77 * deepCopyTo(). 78 */ 79 SkBitmap(const SkBitmap& src); 80 81 ~SkBitmap(); 82 83 /** Copies the src bitmap into this bitmap. Ownership of the src bitmap's pixels remains 84 with the src bitmap. 85 */ 86 SkBitmap& operator=(const SkBitmap& src); 87 /** Swap the fields of the two bitmaps. This routine is guaranteed to never fail or throw. 88 */ 89 // This method is not exported to java. 90 void swap(SkBitmap& other); 91 92 /** Return true iff the bitmap has empty dimensions. 93 */ 94 bool empty() const { return 0 == fWidth || 0 == fHeight; } 95 96 /** Return true iff the bitmap has no pixelref. Note: this can return true even if the 97 dimensions of the bitmap are > 0 (see empty()). 98 */ 99 bool isNull() const { return NULL == fPixelRef; } 100 101 /** Return the config for the bitmap. 102 */ 103 Config config() const { return (Config)fConfig; } 104 /** DEPRECATED, use config() 105 */ 106 Config getConfig() const { return this->config(); } 107 /** Return the bitmap's width, in pixels. 108 */ 109 int width() const { return fWidth; } 110 /** Return the bitmap's height, in pixels. 111 */ 112 int height() const { return fHeight; } 113 /** Return the number of bytes between subsequent rows of the bitmap. 114 */ 115 int rowBytes() const { return fRowBytes; } 116 117 /** Return the shift amount per pixel (i.e. 0 for 1-byte per pixel, 1 for 118 2-bytes per pixel configs, 2 for 4-bytes per pixel configs). Return 0 119 for configs that are not at least 1-byte per pixel (e.g. kA1_Config 120 or kNo_Config) 121 */ 122 int shiftPerPixel() const { return fBytesPerPixel >> 1; } 123 124 /** Return the number of bytes per pixel based on the config. If the config 125 does not have at least 1 byte per (e.g. kA1_Config) then 0 is returned. 126 */ 127 int bytesPerPixel() const { return fBytesPerPixel; } 128 129 /** Return the rowbytes expressed as a number of pixels (like width and 130 height). Note, for 1-byte per pixel configs like kA8_Config, this will 131 return the same as rowBytes(). Is undefined for configs that are less 132 than 1-byte per pixel (e.g. kA1_Config) 133 */ 134 int rowBytesAsPixels() const { return fRowBytes >> (fBytesPerPixel >> 1); } 135 136 /** Return the address of the pixels for this SkBitmap. 137 */ 138 void* getPixels() const { return fPixels; } 139 140 /** Return the byte size of the pixels, based on the height and rowBytes. 141 Note this truncates the result to 32bits. Call getSize64() to detect 142 if the real size exceeds 32bits. 143 */ 144 size_t getSize() const { return fHeight * fRowBytes; } 145 146 /** Return the number of bytes from the pointer returned by getPixels() 147 to the end of the allocated space in the buffer. Required in 148 cases where extractSubset has been called. 149 */ 150 size_t getSafeSize() const ; 151 152 /** Return the byte size of the pixels, based on the height and rowBytes. 153 This routine is slightly slower than getSize(), but does not truncate 154 the answer to 32bits. 155 */ 156 Sk64 getSize64() const { 157 Sk64 size; 158 size.setMul(fHeight, fRowBytes); 159 return size; 160 } 161 162 /** Same as getSafeSize(), but does not truncate the answer to 32bits. 163 */ 164 Sk64 getSafeSize64() const ; 165 166 /** Returns true if this bitmap is marked as immutable, meaning that the 167 contents of its pixels will not change for the lifetime of the bitmap. 168 */ 169 bool isImmutable() const; 170 171 /** Marks this bitmap as immutable, meaning that the contents of its 172 pixels will not change for the lifetime of the bitmap and of the 173 underlying pixelref. This state can be set, but it cannot be 174 cleared once it is set. This state propagates to all other bitmaps 175 that share the same pixelref. 176 */ 177 void setImmutable(); 178 179 /** Returns true if the bitmap is opaque (has no translucent/transparent pixels). 180 */ 181 bool isOpaque() const; 182 183 /** Specify if this bitmap's pixels are all opaque or not. Is only meaningful for configs 184 that support per-pixel alpha (RGB32, A1, A8). 185 */ 186 void setIsOpaque(bool); 187 188 /** Returns true if the bitmap is volatile (i.e. should not be cached by devices.) 189 */ 190 bool isVolatile() const; 191 192 /** Specify whether this bitmap is volatile. Bitmaps are not volatile by 193 default. Temporary bitmaps that are discarded after use should be 194 marked as volatile. This provides a hint to the device that the bitmap 195 should not be cached. Providing this hint when appropriate can 196 improve performance by avoiding unnecessary overhead and resource 197 consumption on the device. 198 */ 199 void setIsVolatile(bool); 200 201 /** Reset the bitmap to its initial state (see default constructor). If we are a (shared) 202 owner of the pixels, that ownership is decremented. 203 */ 204 void reset(); 205 206 /** Given a config and a width, this computes the optimal rowBytes value. This is called automatically 207 if you pass 0 for rowBytes to setConfig(). 208 */ 209 static int ComputeRowBytes(Config c, int width); 210 211 /** Return the bytes-per-pixel for the specified config. If the config is 212 not at least 1-byte per pixel, return 0, including for kNo_Config. 213 */ 214 static int ComputeBytesPerPixel(Config c); 215 216 /** Return the shift-per-pixel for the specified config. If the config is 217 not at least 1-byte per pixel, return 0, including for kNo_Config. 218 */ 219 static int ComputeShiftPerPixel(Config c) { 220 return ComputeBytesPerPixel(c) >> 1; 221 } 222 223 static Sk64 ComputeSize64(Config, int width, int height); 224 static size_t ComputeSize(Config, int width, int height); 225 226 /** 227 * This will brute-force return true if all of the pixels in the bitmap 228 * are opaque. If it fails to read the pixels, or encounters an error, 229 * it will return false. 230 * 231 * Since this can be an expensive operation, the bitmap stores a flag for 232 * this (isOpaque, setIsOpaque). Only call this if you need to compute this 233 * value from "unknown" pixels. 234 */ 235 static bool ComputeIsOpaque(const SkBitmap&); 236 237 /** 238 * Calls ComputeIsOpaque, and passes its result to setIsOpaque(). 239 */ 240 void computeAndSetOpaquePredicate() { 241 this->setIsOpaque(ComputeIsOpaque(*this)); 242 } 243 244 /** 245 * Return the bitmap's bounds [0, 0, width, height] as an SkRect 246 */ 247 void getBounds(SkRect* bounds) const; 248 void getBounds(SkIRect* bounds) const; 249 250 /** Set the bitmap's config and dimensions. If rowBytes is 0, then 251 ComputeRowBytes() is called to compute the optimal value. This resets 252 any pixel/colortable ownership, just like reset(). 253 */ 254 void setConfig(Config, int width, int height, int rowBytes = 0); 255 /** Use this to assign a new pixel address for an existing bitmap. This 256 will automatically release any pixelref previously installed. Only call 257 this if you are handling ownership/lifetime of the pixel memory. 258 259 If the bitmap retains a reference to the colortable (assuming it is 260 not null) it will take care of incrementing the reference count. 261 262 @param pixels Address for the pixels, managed by the caller. 263 @param ctable ColorTable (or null) that matches the specified pixels 264 */ 265 void setPixels(void* p, SkColorTable* ctable = NULL); 266 267 /** Copies the bitmap's pixels to the location pointed at by dst and returns 268 true if possible, returns false otherwise. 269 270 In the case when the dstRowBytes matches the bitmap's rowBytes, the copy 271 may be made faster by copying over the dst's per-row padding (for all 272 rows but the last). By setting preserveDstPad to true the caller can 273 disable this optimization and ensure that pixels in the padding are not 274 overwritten. 275 276 Always returns false for RLE formats. 277 278 @param dst Location of destination buffer. 279 @param dstSize Size of destination buffer. Must be large enough to hold 280 pixels using indicated stride. 281 @param dstRowBytes Width of each line in the buffer. If -1, uses 282 bitmap's internal stride. 283 @param preserveDstPad Must we preserve padding in the dst 284 */ 285 bool copyPixelsTo(void* const dst, size_t dstSize, int dstRowBytes = -1, 286 bool preserveDstPad = false) 287 const; 288 289 /** Use the standard HeapAllocator to create the pixelref that manages the 290 pixel memory. It will be sized based on the current width/height/config. 291 If this is called multiple times, a new pixelref object will be created 292 each time. 293 294 If the bitmap retains a reference to the colortable (assuming it is 295 not null) it will take care of incrementing the reference count. 296 297 @param ctable ColorTable (or null) to use with the pixels that will 298 be allocated. Only used if config == Index8_Config 299 @return true if the allocation succeeds. If not the pixelref field of 300 the bitmap will be unchanged. 301 */ 302 bool allocPixels(SkColorTable* ctable = NULL) { 303 return this->allocPixels(NULL, ctable); 304 } 305 306 /** Use the specified Allocator to create the pixelref that manages the 307 pixel memory. It will be sized based on the current width/height/config. 308 If this is called multiple times, a new pixelref object will be created 309 each time. 310 311 If the bitmap retains a reference to the colortable (assuming it is 312 not null) it will take care of incrementing the reference count. 313 314 @param allocator The Allocator to use to create a pixelref that can 315 manage the pixel memory for the current 316 width/height/config. If allocator is NULL, the standard 317 HeapAllocator will be used. 318 @param ctable ColorTable (or null) to use with the pixels that will 319 be allocated. Only used if config == Index8_Config. 320 If it is non-null and the config is not Index8, it will 321 be ignored. 322 @return true if the allocation succeeds. If not the pixelref field of 323 the bitmap will be unchanged. 324 */ 325 bool allocPixels(Allocator* allocator, SkColorTable* ctable); 326 327 /** Return the current pixelref object, if any 328 */ 329 SkPixelRef* pixelRef() const { return fPixelRef; } 330 /** Return the offset into the pixelref, if any. Will return 0 if there is 331 no pixelref installed. 332 */ 333 size_t pixelRefOffset() const { return fPixelRefOffset; } 334 /** Assign a pixelref and optional offset. Pixelrefs are reference counted, 335 so the existing one (if any) will be unref'd and the new one will be 336 ref'd. 337 */ 338 SkPixelRef* setPixelRef(SkPixelRef* pr, size_t offset = 0); 339 340 /** Call this to ensure that the bitmap points to the current pixel address 341 in the pixelref. Balance it with a call to unlockPixels(). These calls 342 are harmless if there is no pixelref. 343 */ 344 void lockPixels() const; 345 /** When you are finished access the pixel memory, call this to balance a 346 previous call to lockPixels(). This allows pixelrefs that implement 347 cached/deferred image decoding to know when there are active clients of 348 a given image. 349 */ 350 void unlockPixels() const; 351 352 /** 353 * Some bitmaps can return a copy of their pixels for lockPixels(), but 354 * that copy, if modified, will not be pushed back. These bitmaps should 355 * not be used as targets for a raster device/canvas (since all pixels 356 * modifications will be lost when unlockPixels() is called.) 357 */ 358 bool lockPixelsAreWritable() const; 359 360 /** Call this to be sure that the bitmap is valid enough to be drawn (i.e. 361 it has non-null pixels, and if required by its config, it has a 362 non-null colortable. Returns true if all of the above are met. 363 */ 364 bool readyToDraw() const { 365 return this->getPixels() != NULL && 366 ((this->config() != kIndex8_Config && 367 this->config() != kRLE_Index8_Config) || 368 fColorTable != NULL); 369 } 370 371 /** Returns the pixelRef's texture, or NULL 372 */ 373 SkGpuTexture* getTexture() const; 374 375 /** Return the bitmap's colortable (if any). Does not affect the colortable's 376 reference count. 377 */ 378 SkColorTable* getColorTable() const { return fColorTable; } 379 380 /** Returns a non-zero, unique value corresponding to the pixels in our 381 pixelref. Each time the pixels are changed (and notifyPixelsChanged 382 is called), a different generation ID will be returned. Finally, if 383 their is no pixelRef then zero is returned. 384 */ 385 uint32_t getGenerationID() const; 386 387 /** Call this if you have changed the contents of the pixels. This will in- 388 turn cause a different generation ID value to be returned from 389 getGenerationID(). 390 */ 391 void notifyPixelsChanged() const; 392 393 /** Initialize the bitmap's pixels with the specified color+alpha, automatically converting into the correct format 394 for the bitmap's config. If the config is kRGB_565_Config, then the alpha value is ignored. 395 If the config is kA8_Config, then the r,g,b parameters are ignored. 396 */ 397 void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const; 398 /** Initialize the bitmap's pixels with the specified color+alpha, automatically converting into the correct format 399 for the bitmap's config. If the config is kRGB_565_Config, then the alpha value is presumed 400 to be 0xFF. If the config is kA8_Config, then the r,g,b parameters are ignored and the 401 pixels are all set to 0xFF. 402 */ 403 void eraseRGB(U8CPU r, U8CPU g, U8CPU b) const { 404 this->eraseARGB(0xFF, r, g, b); 405 } 406 /** Initialize the bitmap's pixels with the specified color, automatically converting into the correct format 407 for the bitmap's config. If the config is kRGB_565_Config, then the color's alpha value is presumed 408 to be 0xFF. If the config is kA8_Config, then only the color's alpha value is used. 409 */ 410 void eraseColor(SkColor c) const { 411 this->eraseARGB(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c), 412 SkColorGetB(c)); 413 } 414 415 /** Scroll (a subset of) the contents of this bitmap by dx/dy. If there are 416 no pixels allocated (i.e. getPixels() returns null) the method will 417 still update the inval region (if present). If the bitmap is immutable, 418 do nothing and return false. 419 420 @param subset The subset of the bitmap to scroll/move. To scroll the 421 entire contents, specify [0, 0, width, height] or just 422 pass null. 423 @param dx The amount to scroll in X 424 @param dy The amount to scroll in Y 425 @param inval Optional (may be null). Returns the area of the bitmap that 426 was scrolled away. E.g. if dx = dy = 0, then inval would 427 be set to empty. If dx >= width or dy >= height, then 428 inval would be set to the entire bounds of the bitmap. 429 @return true if the scroll was doable. Will return false if the bitmap 430 uses an unsupported config for scrolling (only kA8, 431 kIndex8, kRGB_565, kARGB_4444, kARGB_8888 are supported). 432 If no pixels are present (i.e. getPixels() returns false) 433 inval will still be updated, and true will be returned. 434 */ 435 bool scrollRect(const SkIRect* subset, int dx, int dy, 436 SkRegion* inval = NULL) const; 437 438 /** 439 * Return the SkColor of the specified pixel. In most cases this will 440 * require un-premultiplying the color. Alpha only configs (A1 and A8) 441 * return black with the appropriate alpha set. The value is undefined 442 * for kNone_Config or if x or y are out of bounds, or if the bitmap 443 * does not have any pixels (or has not be locked with lockPixels()). 444 */ 445 SkColor getColor(int x, int y) const; 446 447 /** Returns the address of the specified pixel. This performs a runtime 448 check to know the size of the pixels, and will return the same answer 449 as the corresponding size-specific method (e.g. getAddr16). Since the 450 check happens at runtime, it is much slower than using a size-specific 451 version. Unlike the size-specific methods, this routine also checks if 452 getPixels() returns null, and returns that. The size-specific routines 453 perform a debugging assert that getPixels() is not null, but they do 454 not do any runtime checks. 455 */ 456 void* getAddr(int x, int y) const; 457 458 /** Returns the address of the pixel specified by x,y for 32bit pixels. 459 * In debug build, this asserts that the pixels are allocated and locked, 460 * and that the config is 32-bit, however none of these checks are performed 461 * in the release build. 462 */ 463 inline uint32_t* getAddr32(int x, int y) const; 464 465 /** Returns the address of the pixel specified by x,y for 16bit pixels. 466 * In debug build, this asserts that the pixels are allocated and locked, 467 * and that the config is 16-bit, however none of these checks are performed 468 * in the release build. 469 */ 470 inline uint16_t* getAddr16(int x, int y) const; 471 472 /** Returns the address of the pixel specified by x,y for 8bit pixels. 473 * In debug build, this asserts that the pixels are allocated and locked, 474 * and that the config is 8-bit, however none of these checks are performed 475 * in the release build. 476 */ 477 inline uint8_t* getAddr8(int x, int y) const; 478 479 /** Returns the address of the byte containing the pixel specified by x,y 480 * for 1bit pixels. 481 * In debug build, this asserts that the pixels are allocated and locked, 482 * and that the config is 1-bit, however none of these checks are performed 483 * in the release build. 484 */ 485 inline uint8_t* getAddr1(int x, int y) const; 486 487 /** Returns the color corresponding to the pixel specified by x,y for 488 * colortable based bitmaps. 489 * In debug build, this asserts that the pixels are allocated and locked, 490 * that the config is kIndex8, and that the colortable is allocated, 491 * however none of these checks are performed in the release build. 492 */ 493 inline SkPMColor getIndex8Color(int x, int y) const; 494 495 /** Set dst to be a setset of this bitmap. If possible, it will share the 496 pixel memory, and just point into a subset of it. However, if the config 497 does not support this, a local copy will be made and associated with 498 the dst bitmap. If the subset rectangle, intersected with the bitmap's 499 dimensions is empty, or if there is an unsupported config, false will be 500 returned and dst will be untouched. 501 @param dst The bitmap that will be set to a subset of this bitmap 502 @param subset The rectangle of pixels in this bitmap that dst will 503 reference. 504 @return true if the subset copy was successfully made. 505 */ 506 bool extractSubset(SkBitmap* dst, const SkIRect& subset) const; 507 508 /** Makes a deep copy of this bitmap, respecting the requested config, 509 * and allocating the dst pixels on the cpu. 510 * Returns false if either there is an error (i.e. the src does not have 511 * pixels) or the request cannot be satisfied (e.g. the src has per-pixel 512 * alpha, and the requested config does not support alpha). 513 * @param dst The bitmap to be sized and allocated 514 * @param c The desired config for dst 515 * @param allocator Allocator used to allocate the pixelref for the dst 516 * bitmap. If this is null, the standard HeapAllocator 517 * will be used. 518 * @return true if the copy could be made. 519 */ 520 bool copyTo(SkBitmap* dst, Config c, Allocator* allocator = NULL) const; 521 522 /** Makes a deep copy of this bitmap, respecting the requested config, and 523 * with custom allocation logic that will keep the copied pixels 524 * in the same domain as the source: If the src pixels are allocated for 525 * the cpu, then so will the dst. If the src pixels are allocated on the 526 * gpu (typically as a texture), the it will do the same for the dst. 527 * If the request cannot be fulfilled, returns false and dst is unmodified. 528 */ 529 bool deepCopyTo(SkBitmap* dst, Config c) const; 530 531 /** Returns true if this bitmap can be deep copied into the requested config 532 by calling copyTo(). 533 */ 534 bool canCopyTo(Config newConfig) const; 535 536 bool hasMipMap() const; 537 void buildMipMap(bool forceRebuild = false); 538 void freeMipMap(); 539 540 /** Given scale factors sx, sy, determine the miplevel available in the 541 bitmap, and return it (this is the amount to shift matrix iterators 542 by). If dst is not null, it is set to the correct level. 543 */ 544 int extractMipLevel(SkBitmap* dst, SkFixed sx, SkFixed sy); 545 546 #ifdef SK_BUILD_FOR_ANDROID 547 bool hasHardwareMipMap() const { 548 return fHasHardwareMipMap; 549 } 550 551 void setHasHardwareMipMap(bool hasHardwareMipMap) { 552 fHasHardwareMipMap = hasHardwareMipMap; 553 } 554 #endif 555 556 bool extractAlpha(SkBitmap* dst) const { 557 return this->extractAlpha(dst, NULL, NULL, NULL); 558 } 559 560 bool extractAlpha(SkBitmap* dst, const SkPaint* paint, 561 SkIPoint* offset) const { 562 return this->extractAlpha(dst, paint, NULL, offset); 563 } 564 565 /** Set dst to contain alpha layer of this bitmap. If destination bitmap 566 fails to be initialized, e.g. because allocator can't allocate pixels 567 for it, dst will not be modified and false will be returned. 568 569 @param dst The bitmap to be filled with alpha layer 570 @param paint The paint to draw with 571 @param allocator Allocator used to allocate the pixelref for the dst 572 bitmap. If this is null, the standard HeapAllocator 573 will be used. 574 @param offset If not null, it is set to top-left coordinate to position 575 the returned bitmap so that it visually lines up with the 576 original 577 */ 578 bool extractAlpha(SkBitmap* dst, const SkPaint* paint, Allocator* allocator, 579 SkIPoint* offset) const; 580 581 /** The following two functions provide the means to both flatten and 582 unflatten the bitmap AND its pixels into the provided buffer. 583 It is recommended that you do not call these functions directly, 584 but instead call the write/readBitmap functions on the respective 585 buffers as they can optimize the recording process and avoid recording 586 duplicate bitmaps and pixelRefs. 587 */ 588 void flatten(SkFlattenableWriteBuffer&) const; 589 void unflatten(SkFlattenableReadBuffer&); 590 591 SkDEBUGCODE(void validate() const;) 592 593 class Allocator : public SkRefCnt { 594 public: 595 SK_DECLARE_INST_COUNT(Allocator) 596 597 /** Allocate the pixel memory for the bitmap, given its dimensions and 598 config. Return true on success, where success means either setPixels 599 or setPixelRef was called. The pixels need not be locked when this 600 returns. If the config requires a colortable, it also must be 601 installed via setColorTable. If false is returned, the bitmap and 602 colortable should be left unchanged. 603 */ 604 virtual bool allocPixelRef(SkBitmap*, SkColorTable*) = 0; 605 private: 606 typedef SkRefCnt INHERITED; 607 }; 608 609 /** Subclass of Allocator that returns a pixelref that allocates its pixel 610 memory from the heap. This is the default Allocator invoked by 611 allocPixels(). 612 */ 613 class HeapAllocator : public Allocator { 614 public: 615 virtual bool allocPixelRef(SkBitmap*, SkColorTable*); 616 }; 617 618 class RLEPixels { 619 public: 620 RLEPixels(int width, int height); 621 virtual ~RLEPixels(); 622 623 uint8_t* packedAtY(int y) const { 624 SkASSERT((unsigned)y < (unsigned)fHeight); 625 return fYPtrs[y]; 626 } 627 628 // called by subclasses during creation 629 void setPackedAtY(int y, uint8_t* addr) { 630 SkASSERT((unsigned)y < (unsigned)fHeight); 631 fYPtrs[y] = addr; 632 } 633 634 private: 635 uint8_t** fYPtrs; 636 int fHeight; 637 }; 638 639 SkDEVCODE(void toString(SkString* str) const;) 640 641 private: 642 struct MipMap; 643 mutable MipMap* fMipMap; 644 645 mutable SkPixelRef* fPixelRef; 646 mutable size_t fPixelRefOffset; 647 mutable int fPixelLockCount; 648 // either user-specified (in which case it is not treated as mutable) 649 // or a cache of the returned value from fPixelRef->lockPixels() 650 mutable void* fPixels; 651 mutable SkColorTable* fColorTable; // only meaningful for kIndex8 652 653 enum Flags { 654 kImageIsOpaque_Flag = 0x01, 655 kImageIsVolatile_Flag = 0x02, 656 kImageIsImmutable_Flag = 0x04 657 }; 658 659 uint32_t fRowBytes; 660 uint32_t fWidth; 661 uint32_t fHeight; 662 uint8_t fConfig; 663 uint8_t fFlags; 664 uint8_t fBytesPerPixel; // based on config 665 666 #ifdef SK_BUILD_FOR_ANDROID 667 bool fHasHardwareMipMap; 668 #endif 669 670 /* Internal computations for safe size. 671 */ 672 static Sk64 ComputeSafeSize64(Config config, 673 uint32_t width, 674 uint32_t height, 675 uint32_t rowBytes); 676 static size_t ComputeSafeSize(Config config, 677 uint32_t width, 678 uint32_t height, 679 uint32_t rowBytes); 680 681 /* Unreference any pixelrefs or colortables 682 */ 683 void freePixels(); 684 void updatePixelsFromRef() const; 685 686 static SkFixed ComputeMipLevel(SkFixed sx, SkFixed dy); 687 }; 688 689 class SkAutoLockPixels : public SkNoncopyable { 690 public: 691 SkAutoLockPixels(const SkBitmap& bm, bool doLock = true) : fBitmap(bm) { 692 fDidLock = doLock; 693 if (doLock) { 694 bm.lockPixels(); 695 } 696 } 697 ~SkAutoLockPixels() { 698 if (fDidLock) { 699 fBitmap.unlockPixels(); 700 } 701 } 702 703 private: 704 const SkBitmap& fBitmap; 705 bool fDidLock; 706 }; 707 708 /** Helper class that performs the lock/unlockColors calls on a colortable. 709 The destructor will call unlockColors(false) if it has a bitmap's colortable 710 */ 711 class SkAutoLockColors : public SkNoncopyable { 712 public: 713 /** Initialize with no bitmap. Call lockColors(bitmap) to lock bitmap's 714 colortable 715 */ 716 SkAutoLockColors() : fCTable(NULL), fColors(NULL) {} 717 /** Initialize with bitmap, locking its colortable if present 718 */ 719 explicit SkAutoLockColors(const SkBitmap& bm) { 720 fCTable = bm.getColorTable(); 721 fColors = fCTable ? fCTable->lockColors() : NULL; 722 } 723 /** Initialize with a colortable (may be null) 724 */ 725 explicit SkAutoLockColors(SkColorTable* ctable) { 726 fCTable = ctable; 727 fColors = ctable ? ctable->lockColors() : NULL; 728 } 729 ~SkAutoLockColors() { 730 if (fCTable) { 731 fCTable->unlockColors(false); 732 } 733 } 734 735 /** Return the currently locked colors, or NULL if no bitmap's colortable 736 is currently locked. 737 */ 738 const SkPMColor* colors() const { return fColors; } 739 740 /** Locks the table and returns is colors (assuming ctable is not null) and 741 unlocks the previous table if one was present 742 */ 743 const SkPMColor* lockColors(SkColorTable* ctable) { 744 if (fCTable) { 745 fCTable->unlockColors(false); 746 } 747 fCTable = ctable; 748 fColors = ctable ? ctable->lockColors() : NULL; 749 return fColors; 750 } 751 752 const SkPMColor* lockColors(const SkBitmap& bm) { 753 return this->lockColors(bm.getColorTable()); 754 } 755 756 private: 757 SkColorTable* fCTable; 758 const SkPMColor* fColors; 759 }; 760 761 /////////////////////////////////////////////////////////////////////////////// 762 763 inline uint32_t* SkBitmap::getAddr32(int x, int y) const { 764 SkASSERT(fPixels); 765 SkASSERT(fConfig == kARGB_8888_Config); 766 SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight); 767 return (uint32_t*)((char*)fPixels + y * fRowBytes + (x << 2)); 768 } 769 770 inline uint16_t* SkBitmap::getAddr16(int x, int y) const { 771 SkASSERT(fPixels); 772 SkASSERT(fConfig == kRGB_565_Config || fConfig == kARGB_4444_Config); 773 SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight); 774 return (uint16_t*)((char*)fPixels + y * fRowBytes + (x << 1)); 775 } 776 777 inline uint8_t* SkBitmap::getAddr8(int x, int y) const { 778 SkASSERT(fPixels); 779 SkASSERT(fConfig == kA8_Config || fConfig == kIndex8_Config); 780 SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight); 781 return (uint8_t*)fPixels + y * fRowBytes + x; 782 } 783 784 inline SkPMColor SkBitmap::getIndex8Color(int x, int y) const { 785 SkASSERT(fPixels); 786 SkASSERT(fConfig == kIndex8_Config); 787 SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight); 788 SkASSERT(fColorTable); 789 return (*fColorTable)[*((const uint8_t*)fPixels + y * fRowBytes + x)]; 790 } 791 792 // returns the address of the byte that contains the x coordinate 793 inline uint8_t* SkBitmap::getAddr1(int x, int y) const { 794 SkASSERT(fPixels); 795 SkASSERT(fConfig == kA1_Config); 796 SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight); 797 return (uint8_t*)fPixels + y * fRowBytes + (x >> 3); 798 } 799 800 #endif 801