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;
42 * Default construct creates a bitmap with zero width and height, and no pixels.
43 * Its colortype is set to kUnknown_SkColorType.
48 * Copy the settings from the src into this bitmap. If the src has pixels
49 * allocated, they will be shared, not copied, so that the two bitmaps will
50 * reference the same memory for the pixels. If a deep copy is needed,
51 * where the new bitmap has its own separate copy of the pixels, use
54 SkBitmap(const SkBitmap& src);
58 /** Copies the src bitmap into this bitmap. Ownership of the src bitmap's pixels remains
61 SkBitmap& operator=(const SkBitmap& src);
62 /** Swap the fields of the two bitmaps. This routine is guaranteed to never fail or throw.
64 // This method is not exported to java.
65 void swap(SkBitmap& other);
67 ///////////////////////////////////////////////////////////////////////////
69 const SkImageInfo& info() const { return fInfo; }
71 int width() const { return fInfo.width(); }
72 int height() const { return fInfo.height(); }
73 SkColorType colorType() const { return fInfo.colorType(); }
74 SkAlphaType alphaType() const { return fInfo.alphaType(); }
75 SkColorProfileType profileType() const { return fInfo.profileType(); }
78 * Return the number of bytes per pixel based on the colortype. If the colortype is
79 * kUnknown_SkColorType, then 0 is returned.
81 int bytesPerPixel() const { return fInfo.bytesPerPixel(); }
84 * Return the rowbytes expressed as a number of pixels (like width and height).
85 * If the colortype is kUnknown_SkColorType, then 0 is returned.
87 int rowBytesAsPixels() const {
88 return fRowBytes >> this->shiftPerPixel();
92 * Return the shift amount per pixel (i.e. 0 for 1-byte per pixel, 1 for 2-bytes per pixel
93 * colortypes, 2 for 4-bytes per pixel colortypes). Return 0 for kUnknown_SkColorType.
95 int shiftPerPixel() const { return this->bytesPerPixel() >> 1; }
97 ///////////////////////////////////////////////////////////////////////////
99 /** Return true iff the bitmap has empty dimensions.
100 * Hey! Before you use this, see if you really want to know drawsNothing() instead.
102 bool empty() const { return fInfo.isEmpty(); }
104 /** Return true iff the bitmap has no pixelref. Note: this can return true even if the
105 * dimensions of the bitmap are > 0 (see empty()).
106 * Hey! Before you use this, see if you really want to know drawsNothing() instead.
108 bool isNull() const { return NULL == fPixelRef; }
110 /** Return true iff drawing this bitmap has no effect.
112 bool drawsNothing() const { return this->empty() || this->isNull(); }
114 /** Return the number of bytes between subsequent rows of the bitmap. */
115 size_t rowBytes() const { return fRowBytes; }
118 * Set the bitmap's alphaType, returning true on success. If false is
119 * returned, then the specified new alphaType is incompatible with the
120 * colortype, and the current alphaType is unchanged.
122 * Note: this changes the alphatype for the underlying pixels, which means
123 * that all bitmaps that might be sharing (subsets of) the pixels will
126 bool setAlphaType(SkAlphaType);
128 /** Return the address of the pixels for this SkBitmap.
130 void* getPixels() const { return fPixels; }
132 /** Return the byte size of the pixels, based on the height and rowBytes.
133 Note this truncates the result to 32bits. Call getSize64() to detect
134 if the real size exceeds 32bits.
136 size_t getSize() const { return fInfo.height() * fRowBytes; }
138 /** Return the number of bytes from the pointer returned by getPixels()
139 to the end of the allocated space in the buffer. Required in
140 cases where extractSubset has been called.
142 size_t getSafeSize() const { return fInfo.getSafeSize(fRowBytes); }
145 * Return the full size of the bitmap, in bytes.
147 int64_t computeSize64() const {
148 return sk_64_mul(fInfo.height(), fRowBytes);
152 * Return the number of bytes from the pointer returned by getPixels()
153 * to the end of the allocated space in the buffer. This may be smaller
154 * than computeSize64() if there is any rowbytes padding beyond the width.
156 int64_t computeSafeSize64() const {
157 return fInfo.getSafeSize64(fRowBytes);
160 /** Returns true if this bitmap is marked as immutable, meaning that the
161 contents of its pixels will not change for the lifetime of the bitmap.
163 bool isImmutable() const;
165 /** Marks this bitmap as immutable, meaning that the contents of its
166 pixels will not change for the lifetime of the bitmap and of the
167 underlying pixelref. This state can be set, but it cannot be
168 cleared once it is set. This state propagates to all other bitmaps
169 that share the same pixelref.
173 /** Returns true if the bitmap is opaque (has no translucent/transparent pixels).
175 bool isOpaque() const {
176 return SkAlphaTypeIsOpaque(this->alphaType());
179 /** Returns true if the bitmap is volatile (i.e. should not be cached by devices.)
181 bool isVolatile() const;
183 /** Specify whether this bitmap is volatile. Bitmaps are not volatile by
184 default. Temporary bitmaps that are discarded after use should be
185 marked as volatile. This provides a hint to the device that the bitmap
186 should not be cached. Providing this hint when appropriate can
187 improve performance by avoiding unnecessary overhead and resource
188 consumption on the device.
190 void setIsVolatile(bool);
192 /** Reset the bitmap to its initial state (see default constructor). If we are a (shared)
193 owner of the pixels, that ownership is decremented.
198 * This will brute-force return true if all of the pixels in the bitmap
199 * are opaque. If it fails to read the pixels, or encounters an error,
200 * it will return false.
202 * Since this can be an expensive operation, the bitmap stores a flag for
203 * this (isOpaque). Only call this if you need to compute this value from
206 static bool ComputeIsOpaque(const SkBitmap&);
209 * Return the bitmap's bounds [0, 0, width, height] as an SkRect
211 void getBounds(SkRect* bounds) const;
212 void getBounds(SkIRect* bounds) const;
214 SkIRect bounds() const { return fInfo.bounds(); }
215 SkISize dimensions() const { return fInfo.dimensions(); }
217 bool setInfo(const SkImageInfo&, size_t rowBytes = 0);
220 * Allocate the bitmap's pixels to match the requested image info. If the Factory
221 * is non-null, call it to allcoate the pixelref. If the ImageInfo requires
222 * a colortable, then ColorTable must be non-null, and will be ref'd.
223 * On failure, the bitmap will be set to empty and return false.
225 bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo&, SkPixelRefFactory*, SkColorTable*);
227 void allocPixels(const SkImageInfo& info, SkPixelRefFactory* factory, SkColorTable* ctable) {
228 if (!this->tryAllocPixels(info, factory, ctable)) {
234 * Allocate the bitmap's pixels to match the requested image info and
235 * rowBytes. If the request cannot be met (e.g. the info is invalid or
236 * the requested rowBytes are not compatible with the info
237 * (e.g. rowBytes < info.minRowBytes() or rowBytes is not aligned with
238 * the pixel size specified by info.colorType()) then false is returned
239 * and the bitmap is set to empty.
241 bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info, size_t rowBytes);
243 void allocPixels(const SkImageInfo& info, size_t rowBytes) {
244 if (!this->tryAllocPixels(info, rowBytes)) {
249 bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info) {
250 return this->tryAllocPixels(info, info.minRowBytes());
253 void allocPixels(const SkImageInfo& info) {
254 this->allocPixels(info, info.minRowBytes());
257 bool SK_WARN_UNUSED_RESULT tryAllocN32Pixels(int width, int height, bool isOpaque = false) {
258 SkImageInfo info = SkImageInfo::MakeN32(width, height,
259 isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
260 return this->tryAllocPixels(info);
263 void allocN32Pixels(int width, int height, bool isOpaque = false) {
264 SkImageInfo info = SkImageInfo::MakeN32(width, height,
265 isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
266 this->allocPixels(info);
270 * Install a pixelref that wraps the specified pixels and rowBytes, and
271 * optional ReleaseProc and context. When the pixels are no longer
272 * referenced, if releaseProc is not null, it will be called with the
273 * pixels and context as parameters.
274 * On failure, the bitmap will be set to empty and return false.
276 bool installPixels(const SkImageInfo&, void* pixels, size_t rowBytes, SkColorTable*,
277 void (*releaseProc)(void* addr, void* context), void* context);
280 * Call installPixels with no ReleaseProc specified. This means that the
281 * caller must ensure that the specified pixels are valid for the lifetime
282 * of the created bitmap (and its pixelRef).
284 bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
285 return this->installPixels(info, pixels, rowBytes, NULL, NULL, NULL);
289 * Calls installPixels() with the value in the SkMask. The caller must
290 * ensure that the specified mask pixels are valid for the lifetime
291 * of the created bitmap (and its pixelRef).
293 bool installMaskPixels(const SkMask&);
295 /** Use this to assign a new pixel address for an existing bitmap. This
296 will automatically release any pixelref previously installed. Only call
297 this if you are handling ownership/lifetime of the pixel memory.
299 If the bitmap retains a reference to the colortable (assuming it is
300 not null) it will take care of incrementing the reference count.
302 @param pixels Address for the pixels, managed by the caller.
303 @param ctable ColorTable (or null) that matches the specified pixels
305 void setPixels(void* p, SkColorTable* ctable = NULL);
307 /** Copies the bitmap's pixels to the location pointed at by dst and returns
308 true if possible, returns false otherwise.
310 In the case when the dstRowBytes matches the bitmap's rowBytes, the copy
311 may be made faster by copying over the dst's per-row padding (for all
312 rows but the last). By setting preserveDstPad to true the caller can
313 disable this optimization and ensure that pixels in the padding are not
316 Always returns false for RLE formats.
318 @param dst Location of destination buffer.
319 @param dstSize Size of destination buffer. Must be large enough to hold
320 pixels using indicated stride.
321 @param dstRowBytes Width of each line in the buffer. If 0, uses
322 bitmap's internal stride.
323 @param preserveDstPad Must we preserve padding in the dst
325 bool copyPixelsTo(void* const dst, size_t dstSize, size_t dstRowBytes = 0,
326 bool preserveDstPad = false) const;
328 /** Use the standard HeapAllocator to create the pixelref that manages the
329 pixel memory. It will be sized based on the current ImageInfo.
330 If this is called multiple times, a new pixelref object will be created
333 If the bitmap retains a reference to the colortable (assuming it is
334 not null) it will take care of incrementing the reference count.
336 @param ctable ColorTable (or null) to use with the pixels that will
337 be allocated. Only used if colortype == kIndex_8_SkColorType
338 @return true if the allocation succeeds. If not the pixelref field of
339 the bitmap will be unchanged.
341 bool SK_WARN_UNUSED_RESULT tryAllocPixels(SkColorTable* ctable = NULL) {
342 return this->tryAllocPixels(NULL, ctable);
345 void allocPixels(SkColorTable* ctable = NULL) {
346 this->allocPixels(NULL, ctable);
349 /** Use the specified Allocator to create the pixelref that manages the
350 pixel memory. It will be sized based on the current ImageInfo.
351 If this is called multiple times, a new pixelref object will be created
354 If the bitmap retains a reference to the colortable (assuming it is
355 not null) it will take care of incrementing the reference count.
357 @param allocator The Allocator to use to create a pixelref that can
358 manage the pixel memory for the current ImageInfo.
359 If allocator is NULL, the standard HeapAllocator will be used.
360 @param ctable ColorTable (or null) to use with the pixels that will
361 be allocated. Only used if colortype == kIndex_8_SkColorType.
362 If it is non-null and the colortype is not indexed, it will
364 @return true if the allocation succeeds. If not the pixelref field of
365 the bitmap will be unchanged.
367 bool SK_WARN_UNUSED_RESULT tryAllocPixels(Allocator* allocator, SkColorTable* ctable);
369 void allocPixels(Allocator* allocator, SkColorTable* ctable) {
370 if (!this->tryAllocPixels(allocator, ctable)) {
376 * Return the current pixelref object or NULL if there is none. This does
377 * not affect the refcount of the pixelref.
379 SkPixelRef* pixelRef() const { return fPixelRef; }
382 * A bitmap can reference a subset of a pixelref's pixels. That means the
383 * bitmap's width/height can be <= the dimensions of the pixelref. The
384 * pixelref origin is the x,y location within the pixelref's pixels for
385 * the bitmap's top/left corner. To be valid the following must be true:
387 * origin_x + bitmap_width <= pixelref_width
388 * origin_y + bitmap_height <= pixelref_height
390 * pixelRefOrigin() returns this origin, or (0,0) if there is no pixelRef.
392 SkIPoint pixelRefOrigin() const { return fPixelRefOrigin; }
395 * Assign a pixelref and origin to the bitmap. Pixelrefs are reference,
396 * so the existing one (if any) will be unref'd and the new one will be
397 * ref'd. (x,y) specify the offset within the pixelref's pixels for the
398 * top/left corner of the bitmap. For a bitmap that encompases the entire
399 * pixels of the pixelref, these will be (0,0).
401 SkPixelRef* setPixelRef(SkPixelRef* pr, int dx, int dy);
403 SkPixelRef* setPixelRef(SkPixelRef* pr, const SkIPoint& origin) {
404 return this->setPixelRef(pr, origin.fX, origin.fY);
407 SkPixelRef* setPixelRef(SkPixelRef* pr) {
408 return this->setPixelRef(pr, 0, 0);
411 /** Call this to ensure that the bitmap points to the current pixel address
412 in the pixelref. Balance it with a call to unlockPixels(). These calls
413 are harmless if there is no pixelref.
415 void lockPixels() const;
416 /** When you are finished access the pixel memory, call this to balance a
417 previous call to lockPixels(). This allows pixelrefs that implement
418 cached/deferred image decoding to know when there are active clients of
421 void unlockPixels() const;
424 * Some bitmaps can return a copy of their pixels for lockPixels(), but
425 * that copy, if modified, will not be pushed back. These bitmaps should
426 * not be used as targets for a raster device/canvas (since all pixels
427 * modifications will be lost when unlockPixels() is called.)
429 bool lockPixelsAreWritable() const;
431 /** Call this to be sure that the bitmap is valid enough to be drawn (i.e.
432 it has non-null pixels, and if required by its colortype, it has a
433 non-null colortable. Returns true if all of the above are met.
435 bool readyToDraw() const {
436 return this->getPixels() != NULL &&
437 (this->colorType() != kIndex_8_SkColorType || fColorTable);
440 /** Returns the pixelRef's texture, or NULL
442 GrTexture* getTexture() const;
444 /** Return the bitmap's colortable, if it uses one (i.e. colorType is
445 Index_8) and the pixels are locked.
446 Otherwise returns NULL. Does not affect the colortable's
449 SkColorTable* getColorTable() const { return fColorTable; }
451 /** Returns a non-zero, unique value corresponding to the pixels in our
452 pixelref. Each time the pixels are changed (and notifyPixelsChanged
453 is called), a different generation ID will be returned. Finally, if
454 there is no pixelRef then zero is returned.
456 uint32_t getGenerationID() const;
458 /** Call this if you have changed the contents of the pixels. This will in-
459 turn cause a different generation ID value to be returned from
462 void notifyPixelsChanged() const;
465 * Fill the entire bitmap with the specified color.
466 * If the bitmap's colortype does not support alpha (e.g. 565) then the alpha
467 * of the color is ignored (treated as opaque). If the colortype only supports
468 * alpha (e.g. A1 or A8) then the color's r,g,b components are ignored.
470 void eraseColor(SkColor c) const {
471 this->eraseARGB(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c),
476 * Fill the entire bitmap with the specified color.
477 * If the bitmap's colortype does not support alpha (e.g. 565) then the alpha
478 * of the color is ignored (treated as opaque). If the colortype only supports
479 * alpha (e.g. A1 or A8) then the color's r,g,b components are ignored.
481 void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const;
483 SK_ATTR_DEPRECATED("use eraseARGB or eraseColor")
484 void eraseRGB(U8CPU r, U8CPU g, U8CPU b) const {
485 this->eraseARGB(0xFF, r, g, b);
489 * Fill the specified area of this bitmap with the specified color.
490 * If the bitmap's colortype does not support alpha (e.g. 565) then the alpha
491 * of the color is ignored (treated as opaque). If the colortype only supports
492 * alpha (e.g. A1 or A8) then the color's r,g,b components are ignored.
494 void eraseArea(const SkIRect& area, SkColor c) const;
496 /** Scroll (a subset of) the contents of this bitmap by dx/dy. If there are
497 no pixels allocated (i.e. getPixels() returns null) the method will
498 still update the inval region (if present). If the bitmap is immutable,
499 do nothing and return false.
501 @param subset The subset of the bitmap to scroll/move. To scroll the
502 entire contents, specify [0, 0, width, height] or just
504 @param dx The amount to scroll in X
505 @param dy The amount to scroll in Y
506 @param inval Optional (may be null). Returns the area of the bitmap that
507 was scrolled away. E.g. if dx = dy = 0, then inval would
508 be set to empty. If dx >= width or dy >= height, then
509 inval would be set to the entire bounds of the bitmap.
510 @return true if the scroll was doable. Will return false if the colortype is kUnkown or
511 if the bitmap is immutable.
512 If no pixels are present (i.e. getPixels() returns false)
513 inval will still be updated, and true will be returned.
515 bool scrollRect(const SkIRect* subset, int dx, int dy,
516 SkRegion* inval = NULL) const;
519 * Return the SkColor of the specified pixel. In most cases this will
520 * require un-premultiplying the color. Alpha only colortypes (e.g. kAlpha_8_SkColorType)
521 * return black with the appropriate alpha set. The value is undefined
522 * for kUnknown_SkColorType or if x or y are out of bounds, or if the bitmap
523 * does not have any pixels (or has not be locked with lockPixels()).
525 SkColor getColor(int x, int y) const;
527 /** Returns the address of the specified pixel. This performs a runtime
528 check to know the size of the pixels, and will return the same answer
529 as the corresponding size-specific method (e.g. getAddr16). Since the
530 check happens at runtime, it is much slower than using a size-specific
531 version. Unlike the size-specific methods, this routine also checks if
532 getPixels() returns null, and returns that. The size-specific routines
533 perform a debugging assert that getPixels() is not null, but they do
534 not do any runtime checks.
536 void* getAddr(int x, int y) const;
538 /** Returns the address of the pixel specified by x,y for 32bit pixels.
539 * In debug build, this asserts that the pixels are allocated and locked,
540 * and that the colortype is 32-bit, however none of these checks are performed
541 * in the release build.
543 inline uint32_t* getAddr32(int x, int y) const;
545 /** Returns the address of the pixel specified by x,y for 16bit pixels.
546 * In debug build, this asserts that the pixels are allocated and locked,
547 * and that the colortype is 16-bit, however none of these checks are performed
548 * in the release build.
550 inline uint16_t* getAddr16(int x, int y) const;
552 /** Returns the address of the pixel specified by x,y for 8bit pixels.
553 * In debug build, this asserts that the pixels are allocated and locked,
554 * and that the colortype is 8-bit, however none of these checks are performed
555 * in the release build.
557 inline uint8_t* getAddr8(int x, int y) const;
559 /** Returns the color corresponding to the pixel specified by x,y for
560 * colortable based bitmaps.
561 * In debug build, this asserts that the pixels are allocated and locked,
562 * that the colortype is indexed, and that the colortable is allocated,
563 * however none of these checks are performed in the release build.
565 inline SkPMColor getIndex8Color(int x, int y) const;
567 /** Set dst to be a setset of this bitmap. If possible, it will share the
568 pixel memory, and just point into a subset of it. However, if the colortype
569 does not support this, a local copy will be made and associated with
570 the dst bitmap. If the subset rectangle, intersected with the bitmap's
571 dimensions is empty, or if there is an unsupported colortype, false will be
572 returned and dst will be untouched.
573 @param dst The bitmap that will be set to a subset of this bitmap
574 @param subset The rectangle of pixels in this bitmap that dst will
576 @return true if the subset copy was successfully made.
578 bool extractSubset(SkBitmap* dst, const SkIRect& subset) const;
580 /** Makes a deep copy of this bitmap, respecting the requested colorType,
581 * and allocating the dst pixels on the cpu.
582 * Returns false if either there is an error (i.e. the src does not have
583 * pixels) or the request cannot be satisfied (e.g. the src has per-pixel
584 * alpha, and the requested colortype does not support alpha).
585 * @param dst The bitmap to be sized and allocated
586 * @param ct The desired colorType for dst
587 * @param allocator Allocator used to allocate the pixelref for the dst
588 * bitmap. If this is null, the standard HeapAllocator
590 * @return true if the copy was made.
592 bool copyTo(SkBitmap* dst, SkColorType ct, Allocator* = NULL) const;
594 bool copyTo(SkBitmap* dst, Allocator* allocator = NULL) const {
595 return this->copyTo(dst, this->colorType(), allocator);
599 * Copy the bitmap's pixels into the specified buffer (pixels + rowBytes),
600 * converting them into the requested format (SkImageInfo). The src pixels are read
601 * starting at the specified (srcX,srcY) offset, relative to the top-left corner.
603 * The specified ImageInfo and (srcX,srcY) offset specifies a source rectangle
605 * srcR.setXYWH(srcX, srcY, dstInfo.width(), dstInfo.height());
607 * srcR is intersected with the bounds of the bitmap. If this intersection is not empty,
608 * then we have two sets of pixels (of equal size). Replace the dst pixels with the
609 * corresponding src pixels, performing any colortype/alphatype transformations needed
610 * (in the case where the src and dst have different colortypes or alphatypes).
612 * This call can fail, returning false, for several reasons:
613 * - If srcR does not intersect the bitmap bounds.
614 * - If the requested colortype/alphatype cannot be converted from the src's types.
615 * - If the src pixels are not available.
617 bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
618 int srcX, int srcY) const;
621 * Returns true if this bitmap's pixels can be converted into the requested
622 * colorType, such that copyTo() could succeed.
624 bool canCopyTo(SkColorType colorType) const;
626 /** Makes a deep copy of this bitmap, keeping the copied pixels
627 * in the same domain as the source: If the src pixels are allocated for
628 * the cpu, then so will the dst. If the src pixels are allocated on the
629 * gpu (typically as a texture), the it will do the same for the dst.
630 * If the request cannot be fulfilled, returns false and dst is unmodified.
632 bool deepCopyTo(SkBitmap* dst) const;
634 #ifdef SK_BUILD_FOR_ANDROID
635 bool hasHardwareMipMap() const {
636 return (fFlags & kHasHardwareMipMap_Flag) != 0;
639 void setHasHardwareMipMap(bool hasHardwareMipMap) {
640 if (hasHardwareMipMap) {
641 fFlags |= kHasHardwareMipMap_Flag;
643 fFlags &= ~kHasHardwareMipMap_Flag;
648 bool extractAlpha(SkBitmap* dst) const {
649 return this->extractAlpha(dst, NULL, NULL, NULL);
652 bool extractAlpha(SkBitmap* dst, const SkPaint* paint,
653 SkIPoint* offset) const {
654 return this->extractAlpha(dst, paint, NULL, offset);
657 /** Set dst to contain alpha layer of this bitmap. If destination bitmap
658 fails to be initialized, e.g. because allocator can't allocate pixels
659 for it, dst will not be modified and false will be returned.
661 @param dst The bitmap to be filled with alpha layer
662 @param paint The paint to draw with
663 @param allocator Allocator used to allocate the pixelref for the dst
664 bitmap. If this is null, the standard HeapAllocator
666 @param offset If not null, it is set to top-left coordinate to position
667 the returned bitmap so that it visually lines up with the
670 bool extractAlpha(SkBitmap* dst, const SkPaint* paint, Allocator* allocator,
671 SkIPoint* offset) const;
673 SkDEBUGCODE(void validate() const;)
675 class Allocator : public SkRefCnt {
677 SK_DECLARE_INST_COUNT(Allocator)
679 /** Allocate the pixel memory for the bitmap, given its dimensions and
680 colortype. Return true on success, where success means either setPixels
681 or setPixelRef was called. The pixels need not be locked when this
682 returns. If the colortype requires a colortable, it also must be
683 installed via setColorTable. If false is returned, the bitmap and
684 colortable should be left unchanged.
686 virtual bool allocPixelRef(SkBitmap*, SkColorTable*) = 0;
688 typedef SkRefCnt INHERITED;
691 /** Subclass of Allocator that returns a pixelref that allocates its pixel
692 memory from the heap. This is the default Allocator invoked by
695 class HeapAllocator : public Allocator {
697 bool allocPixelRef(SkBitmap*, SkColorTable*) override;
702 RLEPixels(int width, int height);
703 virtual ~RLEPixels();
705 uint8_t* packedAtY(int y) const {
706 SkASSERT((unsigned)y < (unsigned)fHeight);
710 // called by subclasses during creation
711 void setPackedAtY(int y, uint8_t* addr) {
712 SkASSERT((unsigned)y < (unsigned)fHeight);
721 SK_TO_STRING_NONVIRT()
724 mutable SkPixelRef* fPixelRef;
725 mutable int fPixelLockCount;
726 // These are just caches from the locked pixelref
727 mutable void* fPixels;
728 mutable SkColorTable* fColorTable; // only meaningful for kIndex8
730 SkIPoint fPixelRefOrigin;
733 kImageIsVolatile_Flag = 0x02,
734 #ifdef SK_BUILD_FOR_ANDROID
735 /* A hint for the renderer responsible for drawing this bitmap
736 * indicating that it should attempt to use mipmaps when this bitmap
737 * is drawn scaled down.
739 kHasHardwareMipMap_Flag = 0x08,
749 void internalErase(const SkIRect&, U8CPU a, U8CPU r, U8CPU g, U8CPU b)const;
751 /* Unreference any pixelrefs or colortables
754 void updatePixelsFromRef() const;
756 static void WriteRawPixels(SkWriteBuffer*, const SkBitmap&);
757 static bool ReadRawPixels(SkReadBuffer*, SkBitmap*);
759 friend class SkBitmapSource; // unflatten
760 friend class SkReadBuffer; // unflatten, rawpixels
761 friend class SkWriteBuffer; // rawpixels
762 friend struct SkBitmapProcState;
765 class SkAutoLockPixels : SkNoncopyable {
767 SkAutoLockPixels(const SkBitmap& bm, bool doLock = true) : fBitmap(bm) {
773 ~SkAutoLockPixels() {
775 fBitmap.unlockPixels();
780 const SkBitmap& fBitmap;
783 //TODO(mtklein): uncomment when 71713004 lands and Chromium's fixed.
784 //#define SkAutoLockPixels(...) SK_REQUIRE_LOCAL_VAR(SkAutoLockPixels)
786 ///////////////////////////////////////////////////////////////////////////////
788 inline uint32_t* SkBitmap::getAddr32(int x, int y) const {
790 SkASSERT(4 == this->bytesPerPixel());
791 SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height());
792 return (uint32_t*)((char*)fPixels + y * fRowBytes + (x << 2));
795 inline uint16_t* SkBitmap::getAddr16(int x, int y) const {
797 SkASSERT(2 == this->bytesPerPixel());
798 SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height());
799 return (uint16_t*)((char*)fPixels + y * fRowBytes + (x << 1));
802 inline uint8_t* SkBitmap::getAddr8(int x, int y) const {
804 SkASSERT(1 == this->bytesPerPixel());
805 SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height());
806 return (uint8_t*)fPixels + y * fRowBytes + x;
809 inline SkPMColor SkBitmap::getIndex8Color(int x, int y) const {
811 SkASSERT(kIndex_8_SkColorType == this->colorType());
812 SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height());
813 SkASSERT(fColorTable);
814 return (*fColorTable)[*((const uint8_t*)fPixels + y * fRowBytes + x)];