2 * Copyright 2006 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkBitmap_DEFINED
9 #define SkBitmap_DEFINED
12 #include "SkColorTable.h"
13 #include "SkImageInfo.h"
22 class SkPixelRefFactory;
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
37 class SK_API SkBitmap {
39 class SK_API Allocator;
41 #ifdef SK_SUPPORT_LEGACY_BITMAP_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)
51 // do not add this to the Config enum, otherwise the compiler will let us
52 // pass this as a valid parameter for Config.
54 kConfigCount = kARGB_8888_Config + 1
57 /** Return the config for the bitmap. */
58 Config config() const;
60 SK_ATTR_DEPRECATED("use config()")
61 Config getConfig() const { return this->config(); }
65 * Default construct creates a bitmap with zero width and height, and no pixels.
66 * Its colortype is set to kUnknown_SkColorType.
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
77 SkBitmap(const SkBitmap& src);
81 /** Copies the src bitmap into this bitmap. Ownership of the src bitmap's pixels remains
84 SkBitmap& operator=(const SkBitmap& src);
85 /** Swap the fields of the two bitmaps. This routine is guaranteed to never fail or throw.
87 // This method is not exported to java.
88 void swap(SkBitmap& other);
90 ///////////////////////////////////////////////////////////////////////////
92 const SkImageInfo& info() const { return fInfo; }
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; }
99 #ifdef SK_SUPPORT_LEGACY_ASIMAGEINFO
100 bool asImageInfo(SkImageInfo* info) const {
101 // compatibility: return false for kUnknown
102 if (kUnknown_SkColorType == this->colorType()) {
106 *info = this->info();
113 * Return the number of bytes per pixel based on the colortype. If the colortype is
114 * kUnknown_SkColorType, then 0 is returned.
116 int bytesPerPixel() const { return fInfo.bytesPerPixel(); }
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.
122 int rowBytesAsPixels() const {
123 return fRowBytes >> this->shiftPerPixel();
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.
130 int shiftPerPixel() const { return this->bytesPerPixel() >> 1; }
132 ///////////////////////////////////////////////////////////////////////////
134 /** Return true iff the bitmap has empty dimensions.
135 * Hey! Before you use this, see if you really want to know drawsNothing() instead.
137 bool empty() const { return fInfo.isEmpty(); }
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.
143 bool isNull() const { return NULL == fPixelRef; }
145 /** Return true iff drawing this bitmap has no effect.
147 bool drawsNothing() const { return this->empty() || this->isNull(); }
149 /** Return the number of bytes between subsequent rows of the bitmap. */
150 size_t rowBytes() const { return fRowBytes; }
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.
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
161 bool setAlphaType(SkAlphaType);
163 /** Return the address of the pixels for this SkBitmap.
165 void* getPixels() const { return fPixels; }
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.
171 size_t getSize() const { return fInfo.fHeight * fRowBytes; }
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.
177 size_t getSafeSize() const { return fInfo.getSafeSize(fRowBytes); }
180 * Return the full size of the bitmap, in bytes.
182 int64_t computeSize64() const {
183 return sk_64_mul(fInfo.fHeight, fRowBytes);
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.
191 int64_t computeSafeSize64() const {
192 return fInfo.getSafeSize64(fRowBytes);
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.
198 bool isImmutable() const;
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.
208 /** Returns true if the bitmap is opaque (has no translucent/transparent pixels).
210 bool isOpaque() const {
211 return SkAlphaTypeIsOpaque(this->alphaType());
214 /** Returns true if the bitmap is volatile (i.e. should not be cached by devices.)
216 bool isVolatile() const;
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.
225 void setIsVolatile(bool);
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.
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().
236 static size_t ComputeRowBytes(Config c, int width);
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.
241 static int ComputeBytesPerPixel(Config c);
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.
246 static int ComputeShiftPerPixel(Config c) {
247 return ComputeBytesPerPixel(c) >> 1;
250 static int64_t ComputeSize64(Config, int width, int height);
251 static size_t ComputeSize(Config, int width, int height);
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.
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
263 static bool ComputeIsOpaque(const SkBitmap&);
266 * Return the bitmap's bounds [0, 0, width, height] as an SkRect
268 void getBounds(SkRect* bounds) const;
269 void getBounds(SkIRect* bounds) const;
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().
276 bool setConfig(Config, int width, int height, size_t rowBytes, SkAlphaType);
278 bool setConfig(Config config, int width, int height, size_t rowBytes = 0) {
279 return this->setConfig(config, width, height, rowBytes,
280 kPremul_SkAlphaType);
284 bool setInfo(const SkImageInfo&, size_t rowBytes = 0);
286 #ifdef SK_SUPPORT_LEGACY_SETCONFIG_INFO
287 bool setConfig(const SkImageInfo& info, size_t rowBytes = 0) {
288 return this->setInfo(info, rowBytes);
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.
298 bool allocPixels(const SkImageInfo&, SkPixelRefFactory*, SkColorTable*);
301 * Allocate a pixelref to match the specified image info, using the default
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.
306 bool allocPixels(const SkImageInfo& info) {
307 return this->allocPixels(info, NULL, NULL);
310 bool allocN32Pixels(int width, int height, bool isOpaque = false) {
311 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
313 info.fAlphaType = kOpaque_SkAlphaType;
315 return this->allocPixels(info);
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.
325 bool installPixels(const SkImageInfo&, void* pixels, size_t rowBytes, SkColorTable*,
326 void (*releaseProc)(void* addr, void* context), void* context);
328 #ifdef SK_SUPPORT_LEGACY_INSTALLPIXELSPARAMS
329 bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
330 void (*releaseProc)(void* addr, void* context),
332 return this->installPixels(info, pixels, rowBytes, NULL, releaseProc, context);
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).
341 bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
342 return this->installPixels(info, pixels, rowBytes, NULL, NULL, NULL);
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).
350 bool installMaskPixels(const SkMask&);
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.
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.
359 @param pixels Address for the pixels, managed by the caller.
360 @param ctable ColorTable (or null) that matches the specified pixels
362 void setPixels(void* p, SkColorTable* ctable = NULL);
364 /** Copies the bitmap's pixels to the location pointed at by dst and returns
365 true if possible, returns false otherwise.
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
373 Always returns false for RLE formats.
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
382 bool copyPixelsTo(void* const dst, size_t dstSize, size_t dstRowBytes = 0,
383 bool preserveDstPad = false) const;
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
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.
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.
398 bool allocPixels(SkColorTable* ctable = NULL) {
399 return this->allocPixels(NULL, ctable);
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
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.
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
417 @return true if the allocation succeeds. If not the pixelref field of
418 the bitmap will be unchanged.
420 bool allocPixels(Allocator* allocator, SkColorTable* ctable);
423 * Return the current pixelref object or NULL if there is none. This does
424 * not affect the refcount of the pixelref.
426 SkPixelRef* pixelRef() const { return fPixelRef; }
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:
434 * origin_x + bitmap_width <= pixelref_width
435 * origin_y + bitmap_height <= pixelref_height
437 * pixelRefOrigin() returns this origin, or (0,0) if there is no pixelRef.
439 SkIPoint pixelRefOrigin() const { return fPixelRefOrigin; }
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).
448 SkPixelRef* setPixelRef(SkPixelRef* pr, int dx, int dy);
450 SkPixelRef* setPixelRef(SkPixelRef* pr, const SkIPoint& origin) {
451 return this->setPixelRef(pr, origin.fX, origin.fY);
454 SkPixelRef* setPixelRef(SkPixelRef* pr) {
455 return this->setPixelRef(pr, 0, 0);
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.
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
468 void unlockPixels() const;
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.)
476 bool lockPixelsAreWritable() const;
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.
482 bool readyToDraw() const {
483 return this->getPixels() != NULL &&
484 (this->colorType() != kIndex_8_SkColorType || NULL != fColorTable);
487 /** Returns the pixelRef's texture, or NULL
489 GrTexture* getTexture() const;
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
496 SkColorTable* getColorTable() const { return fColorTable; }
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.
503 uint32_t getGenerationID() const;
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
509 void notifyPixelsChanged() const;
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.
517 void eraseColor(SkColor c) const {
518 this->eraseARGB(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c),
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.
528 void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const;
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);
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.
541 void eraseArea(const SkIRect& area, SkColor c) const;
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.
548 @param subset The subset of the bitmap to scroll/move. To scroll the
549 entire contents, specify [0, 0, width, height] or just
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.
562 bool scrollRect(const SkIRect* subset, int dx, int dy,
563 SkRegion* inval = NULL) const;
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()).
572 SkColor getColor(int x, int y) const;
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.
583 void* getAddr(int x, int y) const;
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.
590 inline uint32_t* getAddr32(int x, int y) const;
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.
597 inline uint16_t* getAddr16(int x, int y) const;
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.
604 inline uint8_t* getAddr8(int x, int y) const;
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.
612 inline SkPMColor getIndex8Color(int x, int y) const;
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
623 @return true if the subset copy was successfully made.
625 bool extractSubset(SkBitmap* dst, const SkIRect& subset) const;
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
637 * @return true if the copy was made.
639 bool copyTo(SkBitmap* dst, SkColorType ct, Allocator* = NULL) const;
641 bool copyTo(SkBitmap* dst, Allocator* allocator = NULL) const {
642 return this->copyTo(dst, this->colorType(), allocator);
646 * Returns true if this bitmap's pixels can be converted into the requested
647 * colorType, such that copyTo() could succeed.
649 bool canCopyTo(SkColorType colorType) const;
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.
657 bool deepCopyTo(SkBitmap* dst) const;
659 #ifdef SK_BUILD_FOR_ANDROID
660 bool hasHardwareMipMap() const {
661 return (fFlags & kHasHardwareMipMap_Flag) != 0;
664 void setHasHardwareMipMap(bool hasHardwareMipMap) {
665 if (hasHardwareMipMap) {
666 fFlags |= kHasHardwareMipMap_Flag;
668 fFlags &= ~kHasHardwareMipMap_Flag;
673 bool extractAlpha(SkBitmap* dst) const {
674 return this->extractAlpha(dst, NULL, NULL, NULL);
677 bool extractAlpha(SkBitmap* dst, const SkPaint* paint,
678 SkIPoint* offset) const {
679 return this->extractAlpha(dst, paint, NULL, offset);
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.
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
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
695 bool extractAlpha(SkBitmap* dst, const SkPaint* paint, Allocator* allocator,
696 SkIPoint* offset) const;
698 SkDEBUGCODE(void validate() const;)
700 class Allocator : public SkRefCnt {
702 SK_DECLARE_INST_COUNT(Allocator)
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.
711 virtual bool allocPixelRef(SkBitmap*, SkColorTable*) = 0;
713 typedef SkRefCnt INHERITED;
716 /** Subclass of Allocator that returns a pixelref that allocates its pixel
717 memory from the heap. This is the default Allocator invoked by
720 class HeapAllocator : public Allocator {
722 virtual bool allocPixelRef(SkBitmap*, SkColorTable*) SK_OVERRIDE;
727 RLEPixels(int width, int height);
728 virtual ~RLEPixels();
730 uint8_t* packedAtY(int y) const {
731 SkASSERT((unsigned)y < (unsigned)fHeight);
735 // called by subclasses during creation
736 void setPackedAtY(int y, uint8_t* addr) {
737 SkASSERT((unsigned)y < (unsigned)fHeight);
746 SK_TO_STRING_NONVIRT()
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
755 SkIPoint fPixelRefOrigin;
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.
766 kHasHardwareMipMap_Flag = 0x08,
776 void internalErase(const SkIRect&, U8CPU a, U8CPU r, U8CPU g, U8CPU b)const;
778 /* Unreference any pixelrefs or colortables
781 void updatePixelsFromRef() const;
783 void legacyUnflatten(SkReadBuffer&);
785 static void WriteRawPixels(SkWriteBuffer*, const SkBitmap&);
786 static bool ReadRawPixels(SkReadBuffer*, SkBitmap*);
788 friend class SkBitmapSource; // unflatten
789 friend class SkReadBuffer; // unflatten, rawpixels
790 friend class SkWriteBuffer; // rawpixels
791 friend struct SkBitmapProcState;
794 class SkAutoLockPixels : SkNoncopyable {
796 SkAutoLockPixels(const SkBitmap& bm, bool doLock = true) : fBitmap(bm) {
802 ~SkAutoLockPixels() {
804 fBitmap.unlockPixels();
809 const SkBitmap& fBitmap;
812 //TODO(mtklein): uncomment when 71713004 lands and Chromium's fixed.
813 //#define SkAutoLockPixels(...) SK_REQUIRE_LOCAL_VAR(SkAutoLockPixels)
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
818 class SkAutoLockColors : SkNoncopyable {
820 /** Initialize with no bitmap. Call lockColors(bitmap) to lock bitmap's
823 SkAutoLockColors() : fCTable(NULL), fColors(NULL) {}
824 /** Initialize with bitmap, locking its colortable if present
826 explicit SkAutoLockColors(const SkBitmap& bm) {
827 fCTable = bm.getColorTable();
828 fColors = fCTable ? fCTable->lockColors() : NULL;
830 /** Initialize with a colortable (may be null)
832 explicit SkAutoLockColors(SkColorTable* ctable) {
834 fColors = ctable ? ctable->lockColors() : NULL;
836 ~SkAutoLockColors() {
838 fCTable->unlockColors();
842 /** Return the currently locked colors, or NULL if no bitmap's colortable
845 const SkPMColor* colors() const { return fColors; }
847 /** Locks the table and returns is colors (assuming ctable is not null) and
848 unlocks the previous table if one was present
850 const SkPMColor* lockColors(SkColorTable* ctable) {
852 fCTable->unlockColors();
855 fColors = ctable ? ctable->lockColors() : NULL;
859 const SkPMColor* lockColors(const SkBitmap& bm) {
860 return this->lockColors(bm.getColorTable());
864 SkColorTable* fCTable;
865 const SkPMColor* fColors;
867 #define SkAutoLockColors(...) SK_REQUIRE_LOCAL_VAR(SkAutoLockColors)
869 ///////////////////////////////////////////////////////////////////////////////
871 inline uint32_t* SkBitmap::getAddr32(int x, int y) const {
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));
878 inline uint16_t* SkBitmap::getAddr16(int x, int y) const {
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));
885 inline uint8_t* SkBitmap::getAddr8(int x, int y) const {
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;
892 inline SkPMColor SkBitmap::getIndex8Color(int x, int y) const {
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)];
900 #ifdef SK_SUPPORT_LEGACY_BITMAP_CONFIG
901 ///////////////////////////////////////////////////////////////////////////////
903 // Helpers until we can fully deprecate SkBitmap::Config
905 extern SkBitmap::Config SkColorTypeToBitmapConfig(SkColorType);
906 extern SkColorType SkBitmapConfigToColorType(SkBitmap::Config);