Flutter Engine
The Flutter Engine
Classes | Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Friends | List of all members
SkBitmap Class Reference

#include <SkBitmap.h>

Classes

class  HeapAllocator
 

Public Types

enum  AllocFlags { kZeroPixels_AllocFlag = 1 << 0 }
 

Public Member Functions

 SkBitmap ()
 
 SkBitmap (const SkBitmap &src)
 
 SkBitmap (SkBitmap &&src)
 
 ~SkBitmap ()
 
SkBitmapoperator= (const SkBitmap &src)
 
SkBitmapoperator= (SkBitmap &&src)
 
void swap (SkBitmap &other)
 
const SkPixmappixmap () const
 
const SkImageInfoinfo () const
 
int width () const
 
int height () const
 
SkColorType colorType () const
 
SkAlphaType alphaType () const
 
SkColorSpacecolorSpace () const
 
sk_sp< SkColorSpacerefColorSpace () const
 
int bytesPerPixel () const
 
int rowBytesAsPixels () const
 
int shiftPerPixel () const
 
bool empty () const
 
bool isNull () const
 
bool drawsNothing () const
 
size_t rowBytes () const
 
bool setAlphaType (SkAlphaType alphaType)
 
void setColorSpace (sk_sp< SkColorSpace > colorSpace)
 
void * getPixels () const
 
size_t computeByteSize () const
 
bool isImmutable () const
 
void setImmutable ()
 
bool isOpaque () const
 
void reset ()
 
void getBounds (SkRect *bounds) const
 
void getBounds (SkIRect *bounds) const
 
SkIRect bounds () const
 
SkISize dimensions () const
 
SkIRect getSubset () const
 
bool setInfo (const SkImageInfo &imageInfo, size_t rowBytes=0)
 
bool tryAllocPixelsFlags (const SkImageInfo &info, uint32_t flags)
 
void allocPixelsFlags (const SkImageInfo &info, uint32_t flags)
 
bool tryAllocPixels (const SkImageInfo &info, size_t rowBytes)
 
void allocPixels (const SkImageInfo &info, size_t rowBytes)
 
bool tryAllocPixels (const SkImageInfo &info)
 
void allocPixels (const SkImageInfo &info)
 
bool tryAllocN32Pixels (int width, int height, bool isOpaque=false)
 
void allocN32Pixels (int width, int height, bool isOpaque=false)
 
bool installPixels (const SkImageInfo &info, void *pixels, size_t rowBytes, void(*releaseProc)(void *addr, void *context), void *context)
 
bool installPixels (const SkImageInfo &info, void *pixels, size_t rowBytes)
 
bool installPixels (const SkPixmap &pixmap)
 
bool installMaskPixels (SkMaskBuilder &mask)
 
void setPixels (void *pixels)
 
bool tryAllocPixels ()
 
void allocPixels ()
 
bool tryAllocPixels (Allocator *allocator)
 
void allocPixels (Allocator *allocator)
 
SkPixelRefpixelRef () const
 
SkIPoint pixelRefOrigin () const
 
void setPixelRef (sk_sp< SkPixelRef > pixelRef, int dx, int dy)
 
bool readyToDraw () const
 
uint32_t getGenerationID () const
 
void notifyPixelsChanged () const
 
void eraseColor (SkColor4f) const
 
void eraseColor (SkColor c) const
 
void eraseARGB (U8CPU a, U8CPU r, U8CPU g, U8CPU b) const
 
void erase (SkColor4f c, const SkIRect &area) const
 
void erase (SkColor c, const SkIRect &area) const
 
void eraseArea (const SkIRect &area, SkColor c) const
 
SkColor getColor (int x, int y) const
 
SkColor4f getColor4f (int x, int y) const
 
float getAlphaf (int x, int y) const
 
void * getAddr (int x, int y) const
 
uint32_t * getAddr32 (int x, int y) const
 
uint16_t * getAddr16 (int x, int y) const
 
uint8_t * getAddr8 (int x, int y) const
 
bool extractSubset (SkBitmap *dst, const SkIRect &subset) const
 
bool readPixels (const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY) const
 
bool readPixels (const SkPixmap &dst, int srcX, int srcY) const
 
bool readPixels (const SkPixmap &dst) const
 
bool writePixels (const SkPixmap &src, int dstX, int dstY)
 
bool writePixels (const SkPixmap &src)
 
bool extractAlpha (SkBitmap *dst) const
 
bool extractAlpha (SkBitmap *dst, const SkPaint *paint, SkIPoint *offset) const
 
bool extractAlpha (SkBitmap *dst, const SkPaint *paint, Allocator *allocator, SkIPoint *offset) const
 
bool peekPixels (SkPixmap *pixmap) const
 
sk_sp< SkShadermakeShader (SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
 
sk_sp< SkShadermakeShader (SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &sampling, const SkMatrix &lm) const
 
sk_sp< SkShadermakeShader (const SkSamplingOptions &sampling, const SkMatrix &lm) const
 
sk_sp< SkShadermakeShader (const SkSamplingOptions &sampling, const SkMatrix *lm=nullptr) const
 
sk_sp< SkImageasImage () const
 

Static Public Member Functions

static bool ComputeIsOpaque (const SkBitmap &bm)
 

Public Attributes

class SK_API Allocator
 

Friends

class SkImage_Raster
 
class SkReadBuffer
 
class GrProxyProvider
 

Detailed Description

SkBitmap describes a two-dimensional raster pixel array. SkBitmap is built on SkImageInfo, containing integer width and height, SkColorType and SkAlphaType describing the pixel format, and SkColorSpace describing the range of colors. SkBitmap points to SkPixelRef, which describes the physical array of pixels. SkImageInfo bounds may be located anywhere fully inside SkPixelRef bounds.

SkBitmap can be drawn using SkCanvas. SkBitmap can be a drawing destination for SkCanvas draw member functions. SkBitmap flexibility as a pixel container limits some optimizations available to the target platform.

If pixel array is primarily read-only, use SkImage for better performance. If pixel array is primarily written to, use SkSurface for better performance.

Declaring SkBitmap const prevents altering SkImageInfo: the SkBitmap height, width, and so on cannot change. It does not affect SkPixelRef: a caller may write its pixels. Declaring SkBitmap const affects SkBitmap configuration, not its contents.

SkBitmap is not thread safe. Each thread must have its own copy of SkBitmap fields, although threads may share the underlying pixel array.

Definition at line 59 of file SkBitmap.h.

Member Enumeration Documentation

◆ AllocFlags

Enumerator
kZeroPixels_AllocFlag 

zero pixel memory. No effect. This is the default.

Definition at line 434 of file SkBitmap.h.

434 {
435 kZeroPixels_AllocFlag = 1 << 0, //!< zero pixel memory. No effect. This is the default.
436 };
@ kZeroPixels_AllocFlag
zero pixel memory. No effect. This is the default.
Definition: SkBitmap.h:435

Constructor & Destructor Documentation

◆ SkBitmap() [1/3]

SkBitmap::SkBitmap ( )

Creates an empty SkBitmap without pixels, with kUnknown_SkColorType, kUnknown_SkAlphaType, and with a width and height of zero. SkPixelRef origin is set to (0, 0).

Use setInfo() to associate SkColorType, SkAlphaType, width, and height after SkBitmap has been created.

Returns
empty SkBitmap

example: https://fiddle.skia.org/c/@Bitmap_empty_constructor

Definition at line 43 of file SkBitmap.cpp.

43{}

◆ SkBitmap() [2/3]

SkBitmap::SkBitmap ( const SkBitmap src)

Copies settings from src to returned SkBitmap. Shares pixels if src has pixels allocated, so both bitmaps reference the same pixels.

Parameters
srcSkBitmap to copy SkImageInfo, and share SkPixelRef
Returns
copy of src

example: https://fiddle.skia.org/c/@Bitmap_copy_const_SkBitmap

Definition at line 45 of file SkBitmap.cpp.

46 : fPixelRef (src.fPixelRef)
47 , fPixmap (src.fPixmap)
48 , fMips (src.fMips)
49{
50 SkDEBUGCODE(src.validate();)
51 SkDEBUGCODE(this->validate();)
52}
SkDEBUGCODE(SK_SPI) SkThreadID SkGetThreadID()

◆ SkBitmap() [3/3]

SkBitmap::SkBitmap ( SkBitmap &&  src)

Copies settings from src to returned SkBitmap. Moves ownership of src pixels to SkBitmap.

Parameters
srcSkBitmap to copy SkImageInfo, and reassign SkPixelRef
Returns
copy of src

example: https://fiddle.skia.org/c/@Bitmap_move_SkBitmap

Definition at line 54 of file SkBitmap.cpp.

55 : fPixelRef (std::move(other.fPixelRef))
56 , fPixmap (std::move(other.fPixmap))
57 , fMips (std::move(other.fMips))
58{
59 SkASSERT(!other.fPixelRef);
60 other.fPixmap.reset();
61}
#define SkASSERT(cond)
Definition: SkAssert.h:116

◆ ~SkBitmap()

SkBitmap::~SkBitmap ( )

Decrements SkPixelRef reference count, if SkPixelRef is not nullptr.

Definition at line 63 of file SkBitmap.cpp.

63{}

Member Function Documentation

◆ allocN32Pixels()

void SkBitmap::allocN32Pixels ( int  width,
int  height,
bool  isOpaque = false 
)

Sets SkImageInfo to width, height, and the native color type; and allocates pixel memory. If isOpaque is true, sets SkImageInfo to kOpaque_SkAlphaType; otherwise, sets to kPremul_SkAlphaType.

Aborts if width exceeds 29 bits or is negative, or height is negative, or allocation fails. Abort steps may be provided by the user at compile time by defining SK_ABORT.

Use to create SkBitmap that matches SkPMColor, the native pixel arrangement on the platform. SkBitmap drawn to output device skips converting its pixel format.

Parameters
widthpixel column count; must be zero or greater
heightpixel row count; must be zero or greater
isOpaquetrue if pixels do not have transparency

example: https://fiddle.skia.org/c/@Bitmap_allocN32Pixels

Definition at line 232 of file SkBitmap.cpp.

232 {
235 this->allocPixels(info);
236}
@ kOpaque_SkAlphaType
pixel is opaque
Definition: SkAlphaType.h:28
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
bool isOpaque() const
Definition: SkBitmap.h:324
int width() const
Definition: SkBitmap.h:149
void allocPixels()
Definition: SkBitmap.cpp:238
const SkImageInfo & info() const
Definition: SkBitmap.h:139
int height() const
Definition: SkBitmap.h:158
static SkImageInfo MakeN32(int width, int height, SkAlphaType at)

◆ allocPixels() [1/4]

void SkBitmap::allocPixels ( )

Allocates pixel memory with HeapAllocator, and replaces existing SkPixelRef. The allocation size is determined by SkImageInfo width, height, and SkColorType.

Aborts if info().colorType() is kUnknown_SkColorType, or allocation fails. Abort steps may be provided by the user at compile time by defining SK_ABORT.

example: https://fiddle.skia.org/c/@Bitmap_allocPixels_3

Definition at line 238 of file SkBitmap.cpp.

238 {
239 this->allocPixels((Allocator*)nullptr);
240}

◆ allocPixels() [2/4]

void SkBitmap::allocPixels ( Allocator allocator)

Allocates pixel memory with allocator, and replaces existing SkPixelRef. The allocation size is determined by SkImageInfo width, height, and SkColorType. If allocator is nullptr, use HeapAllocator instead.

Aborts if Allocator::allocPixelRef return false. Abort steps may be provided by the user at compile time by defining SK_ABORT.

Parameters
allocatorinstance of SkBitmap::Allocator instantiation

example: https://fiddle.skia.org/c/@Bitmap_allocPixels_4

Definition at line 242 of file SkBitmap.cpp.

242 {
243 if (!this->tryAllocPixels(allocator)) {
244 const SkImageInfo& info = this->info();
245 SK_ABORT("SkBitmap::tryAllocPixels failed "
246 "ColorType:%d AlphaType:%d [w:%d h:%d] rb:%zu",
247 info.colorType(), info.alphaType(), info.width(), info.height(), this->rowBytes());
248 }
249}
#define SK_ABORT(message,...)
Definition: SkAssert.h:70
bool tryAllocPixels()
Definition: SkBitmap.h:674
int width() const
Definition: SkImageInfo.h:365
SkAlphaType alphaType() const
Definition: SkImageInfo.h:375
SkColorType colorType() const
Definition: SkImageInfo.h:373
int height() const
Definition: SkImageInfo.h:371

◆ allocPixels() [3/4]

void SkBitmap::allocPixels ( const SkImageInfo info)

Sets SkImageInfo to info following the rules in setInfo() and allocates pixel memory.

Aborts execution if SkImageInfo could not be set, or memory could not be allocated. Abort steps may be provided by the user at compile time by defining SK_ABORT.

On most platforms, allocating pixel memory may succeed even though there is not sufficient memory to hold pixels; allocation does not take place until the pixels are written to. The actual behavior depends on the platform implementation of malloc().

Parameters
infocontains width, height, SkAlphaType, SkColorType, SkColorSpace

example: https://fiddle.skia.org/c/@Bitmap_allocPixels_2

Definition at line 265 of file SkBitmap.cpp.

265 {
266 this->allocPixels(info, info.minRowBytes());
267}
size_t minRowBytes() const
Definition: SkImageInfo.h:517

◆ allocPixels() [4/4]

void SkBitmap::allocPixels ( const SkImageInfo info,
size_t  rowBytes 
)

Sets SkImageInfo to info following the rules in setInfo() and allocates pixel memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(), or equal zero. Pass in zero for rowBytes to compute the minimum valid value.

Aborts execution if SkImageInfo could not be set, or memory could not be allocated. Abort steps may be provided by the user at compile time by defining SK_ABORT.

On most platforms, allocating pixel memory may succeed even though there is not sufficient memory to hold pixels; allocation does not take place until the pixels are written to. The actual behavior depends on the platform implementation of malloc().

Parameters
infocontains width, height, SkAlphaType, SkColorType, SkColorSpace
rowBytessize of pixel row or larger; may be zero

example: https://fiddle.skia.org/c/@Bitmap_allocPixels

Definition at line 258 of file SkBitmap.cpp.

258 {
260 "ColorType:%d AlphaType:%d [w:%d h:%d] rb:%zu",
262 this->rowBytes());
263}
#define SkASSERTF_RELEASE(cond, fmt,...)
Definition: SkAssert.h:103
size_t rowBytes() const
Definition: SkBitmap.h:238

◆ allocPixelsFlags()

void SkBitmap::allocPixelsFlags ( const SkImageInfo info,
uint32_t  flags 
)

Sets SkImageInfo to info following the rules in setInfo() and allocates pixel memory. Memory is zeroed.

Aborts execution if SkImageInfo could not be set, or memory could not be allocated, or memory could not optionally be zeroed. Abort steps may be provided by the user at compile time by defining SK_ABORT.

On most platforms, allocating pixel memory may succeed even though there is not sufficient memory to hold pixels; allocation does not take place until the pixels are written to. The actual behavior depends on the platform implementation of calloc().

Parameters
infocontains width, height, SkAlphaType, SkColorType, SkColorSpace
flagskZeroPixels_AllocFlag, or zero

example: https://fiddle.skia.org/c/@Bitmap_allocPixelsFlags

Definition at line 251 of file SkBitmap.cpp.

251 {
253 "ColorType:%d AlphaType:%d [w:%d h:%d] rb:%zu flags: 0x%x",
255 this->rowBytes(), flags);
256}
bool tryAllocPixelsFlags(const SkImageInfo &info, uint32_t flags)
Definition: SkBitmap.cpp:296
FlutterSemanticsFlag flags

◆ alphaType()

SkAlphaType SkBitmap::alphaType ( ) const
inline

Definition at line 162 of file SkBitmap.h.

162{ return fPixmap.alphaType(); }
SkAlphaType alphaType() const
Definition: SkPixmap.h:175

◆ asImage()

sk_sp< SkImage > SkBitmap::asImage ( ) const

Returns a new image from the bitmap. If the bitmap is marked immutable, this will share the pixel buffer. If not, it will make a copy of the pixels for the image.

Definition at line 645 of file SkBitmap.cpp.

645{ return SkImages::RasterFromBitmap(*this); }
SK_API sk_sp< SkImage > RasterFromBitmap(const SkBitmap &bitmap)

◆ bounds()

SkIRect SkBitmap::bounds ( ) const
inline

Returns SkIRect { 0, 0, width(), height() }.

Returns
integral rectangle from origin to width() and height()

Definition at line 382 of file SkBitmap.h.

382{ return fPixmap.info().bounds(); }
const SkImageInfo & info() const
Definition: SkPixmap.h:135
SkIRect bounds() const
Definition: SkImageInfo.h:427

◆ bytesPerPixel()

int SkBitmap::bytesPerPixel ( ) const
inline

Returns number of bytes per pixel required by SkColorType. Returns zero if colorType( is kUnknown_SkColorType.

Returns
bytes in pixel

Definition at line 187 of file SkBitmap.h.

187{ return fPixmap.info().bytesPerPixel(); }
int bytesPerPixel() const
Definition: SkImageInfo.h:492

◆ colorSpace()

SkColorSpace * SkBitmap::colorSpace ( ) const

Returns SkColorSpace, the range of colors, associated with SkImageInfo. The reference count of SkColorSpace is unchanged. The returned SkColorSpace is immutable.

Returns
SkColorSpace in SkImageInfo, or nullptr

Definition at line 108 of file SkBitmap.cpp.

108{ return fPixmap.colorSpace(); }
SkColorSpace * colorSpace() const
Definition: SkPixmap.cpp:61

◆ colorType()

SkColorType SkBitmap::colorType ( ) const
inline

Definition at line 160 of file SkBitmap.h.

160{ return fPixmap.colorType(); }
SkColorType colorType() const
Definition: SkPixmap.h:173

◆ computeByteSize()

size_t SkBitmap::computeByteSize ( ) const
inline

Returns minimum memory required for pixel storage. Does not include unused memory on last row when rowBytesAsPixels() exceeds width(). Returns SIZE_MAX if result does not fit in size_t. Returns zero if height() or width() is 0. Returns height() times rowBytes() if colorType() is kUnknown_SkColorType.

Returns
size in bytes of image buffer

Definition at line 293 of file SkBitmap.h.

293{ return fPixmap.computeByteSize(); }
size_t computeByteSize() const
Definition: SkPixmap.h:231

◆ ComputeIsOpaque()

static bool SkBitmap::ComputeIsOpaque ( const SkBitmap bm)
inlinestatic

Returns true if all pixels are opaque. SkColorType determines how pixels are encoded, and whether pixel describes alpha. Returns true for SkColorType without alpha in each pixel; for other SkColorType, returns true if all pixels have alpha values equivalent to 1.0 or greater.

For SkColorType kRGB_565_SkColorType or kGray_8_SkColorType: always returns true. For SkColorType kAlpha_8_SkColorType, kBGRA_8888_SkColorType, kRGBA_8888_SkColorType: returns true if all pixel alpha values are 255. For SkColorType kARGB_4444_SkColorType: returns true if all pixel alpha values are 15. For kRGBA_F16_SkColorType: returns true if all pixel alpha values are 1.0 or greater.

Returns false for kUnknown_SkColorType.

Parameters
bmSkBitmap to check
Returns
true if all pixels have opaque values or SkColorType is opaque

Definition at line 358 of file SkBitmap.h.

358 {
359 return bm.pixmap().computeIsOpaque();
360 }
const SkPixmap & pixmap() const
Definition: SkBitmap.h:133
bool computeIsOpaque() const
Definition: SkPixmap.cpp:577

◆ dimensions()

SkISize SkBitmap::dimensions ( ) const
inline

Returns SkISize { width(), height() }.

Returns
integral size of width() and height()

Definition at line 388 of file SkBitmap.h.

388{ return fPixmap.info().dimensions(); }
SkISize dimensions() const
Definition: SkImageInfo.h:421

◆ drawsNothing()

bool SkBitmap::drawsNothing ( ) const
inline

Returns true if width() or height() are zero, or if SkPixelRef is nullptr. If true, SkBitmap has no effect when drawn or drawn into.

Returns
true if drawing has no effect

Definition at line 226 of file SkBitmap.h.

226 {
227 return this->empty() || this->isNull();
228 }
bool empty() const
Definition: SkBitmap.h:210
bool isNull() const
Definition: SkBitmap.h:219

◆ empty()

bool SkBitmap::empty ( ) const
inline

Returns true if either width() or height() are zero.

Does not check if SkPixelRef is nullptr; call drawsNothing() to check width(), height(), and SkPixelRef.

Returns
true if dimensions do not enclose area

Definition at line 210 of file SkBitmap.h.

210{ return fPixmap.info().isEmpty(); }
bool isEmpty() const
Definition: SkImageInfo.h:399

◆ erase() [1/2]

void SkBitmap::erase ( SkColor  c,
const SkIRect area 
) const

Replaces pixel values inside area with c. interpreted as being in the sRGB SkColorSpace. If area does not intersect bounds(), call has no effect.

If the colorType() is kGray_8_SkColorType or kRGB_565_SkColorType, then alpha is ignored; RGB is treated as opaque. If colorType() is kAlpha_8_SkColorType, then RGB is ignored.

Input color is ultimately converted to an SkColor4f, so erase(SkColor4f c) will have higher color resolution.

Parameters
cunpremultiplied color
arearectangle to fill

example: https://fiddle.skia.org/c/@Bitmap_erase

Definition at line 438 of file SkBitmap.cpp.

438 {
439 this->erase(SkColor4f::FromColor(c), area);
440}
void erase(SkColor4f c, const SkIRect &area) const
Definition: SkBitmap.cpp:420

◆ erase() [2/2]

void SkBitmap::erase ( SkColor4f  c,
const SkIRect area 
) const

Replaces pixel values inside area with c. interpreted as being in the sRGB SkColorSpace. If area does not intersect bounds(), call has no effect.

If the colorType() is kGray_8_SkColorType or kRGB_565_SkColorType, then alpha is ignored; RGB is treated as opaque. If colorType() is kAlpha_8_SkColorType, then RGB is ignored.

Parameters
cunpremultiplied color
arearectangle to fill

example: https://fiddle.skia.org/c/@Bitmap_erase

Definition at line 420 of file SkBitmap.cpp.

420 {
421 SkDEBUGCODE(this->validate();)
422
423 if (kUnknown_SkColorType == this->colorType()) {
424 // TODO: can we ASSERT that we never get here?
425 return; // can't erase. Should we bzero so the memory is not uninitialized?
426 }
427
429 if (!this->peekPixels(&result)) {
430 return;
431 }
432
433 if (result.erase(c, &area)) {
434 this->notifyPixelsChanged();
435 }
436}
@ kUnknown_SkColorType
uninitialized
Definition: SkColorType.h:20
void notifyPixelsChanged() const
Definition: SkBitmap.cpp:365
SkColorType colorType() const
Definition: SkBitmap.h:160
bool peekPixels(SkPixmap *pixmap) const
Definition: SkBitmap.cpp:635
GAsyncResult * result

◆ eraseArea()

void SkBitmap::eraseArea ( const SkIRect area,
SkColor  c 
) const
inline

Deprecated.

Definition at line 854 of file SkBitmap.h.

854 {
855 this->erase(c, area);
856 }

◆ eraseARGB()

void SkBitmap::eraseARGB ( U8CPU  a,
U8CPU  r,
U8CPU  g,
U8CPU  b 
) const
inline

Replaces pixel values with unpremultiplied color built from a, r, g, and b, interpreted as being in the sRGB SkColorSpace. All pixels contained by bounds() are affected. If the colorType() is kGray_8_SkColorType or kRGB_565_SkColorType, then a is ignored; r, g, and b are treated as opaque. If colorType() is kAlpha_8_SkColorType, then r, g, and b are ignored.

Parameters
aamount of alpha, from fully transparent (0) to fully opaque (255)
ramount of red, from no red (0) to full red (255)
gamount of green, from no green (0) to full green (255)
bamount of blue, from no blue (0) to full blue (255)

Definition at line 817 of file SkBitmap.h.

817 {
818 this->eraseColor(SkColorSetARGB(a, r, g, b));
819 }
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColor.h:49
void eraseColor(SkColor4f) const
Definition: SkBitmap.cpp:442
static bool b
struct MyStruct a[10]

◆ eraseColor() [1/2]

void SkBitmap::eraseColor ( SkColor  c) const

Replaces pixel values with c, interpreted as being in the sRGB SkColorSpace. All pixels contained by bounds() are affected. If the colorType() is kGray_8_SkColorType or kRGB_565_SkColorType, then alpha is ignored; RGB is treated as opaque. If colorType() is kAlpha_8_SkColorType, then RGB is ignored.

Input color is ultimately converted to an SkColor4f, so eraseColor(SkColor4f c) will have higher color resolution.

Parameters
cunpremultiplied color.

example: https://fiddle.skia.org/c/@Bitmap_eraseColor

Definition at line 446 of file SkBitmap.cpp.

446 {
447 this->erase(SkColor4f::FromColor(c), SkIRect::MakeWH(this->width(), this->height()));
448}
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition: SkRect.h:56

◆ eraseColor() [2/2]

void SkBitmap::eraseColor ( SkColor4f  c) const

Replaces pixel values with c, interpreted as being in the sRGB SkColorSpace. All pixels contained by bounds() are affected. If the colorType() is kGray_8_SkColorType or kRGB_565_SkColorType, then alpha is ignored; RGB is treated as opaque. If colorType() is kAlpha_8_SkColorType, then RGB is ignored.

Parameters
cunpremultiplied color

example: https://fiddle.skia.org/c/@Bitmap_eraseColor

Definition at line 442 of file SkBitmap.cpp.

442 {
443 this->erase(c, SkIRect::MakeWH(this->width(), this->height()));
444}

◆ extractAlpha() [1/3]

bool SkBitmap::extractAlpha ( SkBitmap dst) const
inline

Sets dst to alpha described by pixels. Returns false if dst cannot be written to or dst pixels cannot be allocated.

Uses HeapAllocator to reserve memory for dst SkPixelRef.

Parameters
dstholds SkPixelRef to fill with alpha layer
Returns
true if alpha layer was constructed in dst SkPixelRef

Definition at line 1134 of file SkBitmap.h.

1134 {
1135 return this->extractAlpha(dst, nullptr, nullptr, nullptr);
1136 }
bool extractAlpha(SkBitmap *dst) const
Definition: SkBitmap.h:1134

◆ extractAlpha() [2/3]

bool SkBitmap::extractAlpha ( SkBitmap dst,
const SkPaint paint,
Allocator allocator,
SkIPoint offset 
) const

Sets dst to alpha described by pixels. Returns false if dst cannot be written to or dst pixels cannot be allocated.

If paint is not nullptr and contains SkMaskFilter, SkMaskFilter generates mask alpha from SkBitmap. allocator may reference a custom allocation class or be set to nullptr to use HeapAllocator. Sets offset to top-left position for dst for alignment with SkBitmap; (0, 0) unless SkMaskFilter generates mask.

Parameters
dstholds SkPixelRef to fill with alpha layer
paintholds optional SkMaskFilter; may be nullptr
allocatorfunction to reserve memory for SkPixelRef; may be nullptr
offsettop-left position for dst; may be nullptr
Returns
true if alpha layer was constructed in dst SkPixelRef

Definition at line 539 of file SkBitmap.cpp.

540 {
541 SkDEBUGCODE(this->validate();)
542
543 SkBitmap tmpBitmap;
545 SkMaskBuilder srcM, dstM;
546
547 if (this->width() == 0 || this->height() == 0) {
548 return false;
549 }
550 srcM.bounds().setWH(this->width(), this->height());
551 srcM.rowBytes() = SkAlign4(this->width());
552 srcM.format() = SkMask::kA8_Format;
553
554 SkMaskFilter* filter = paint ? paint->getMaskFilter() : nullptr;
555
556 // compute our (larger?) dst bounds if we have a filter
557 if (filter) {
558 identity.reset();
559 if (!as_MFB(filter)->filterMask(&dstM, srcM, identity, nullptr)) {
560 goto NO_FILTER_CASE;
561 }
562 dstM.rowBytes() = SkAlign4(dstM.fBounds.width());
563 } else {
564 NO_FILTER_CASE:
565 tmpBitmap.setInfo(SkImageInfo::MakeA8(this->width(), this->height()), srcM.fRowBytes);
566 if (!tmpBitmap.tryAllocPixels(allocator)) {
567 // Allocation of pixels for alpha bitmap failed.
568 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
569 tmpBitmap.width(), tmpBitmap.height());
570 return false;
571 }
572 GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes);
573 if (offset) {
574 offset->set(0, 0);
575 }
576 tmpBitmap.swap(*dst);
577 return true;
578 }
580 SkAutoMaskFreeImage srcCleanup(srcM.image());
581
582 GetBitmapAlpha(*this, srcM.image(), srcM.fRowBytes);
583 if (!as_MFB(filter)->filterMask(&dstM, srcM, identity, nullptr)) {
584 goto NO_FILTER_CASE;
585 }
586 SkAutoMaskFreeImage dstCleanup(dstM.image());
587
588 tmpBitmap.setInfo(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()),
589 dstM.fRowBytes);
590 if (!tmpBitmap.tryAllocPixels(allocator)) {
591 // Allocation of pixels for alpha bitmap failed.
592 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
593 tmpBitmap.width(), tmpBitmap.height());
594 return false;
595 }
596 memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize());
597 if (offset) {
598 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop);
599 }
600 SkDEBUGCODE(tmpBitmap.validate();)
601
602 tmpBitmap.swap(*dst);
603 return true;
604}
static constexpr T SkAlign4(T x)
Definition: SkAlign.h:16
static bool GetBitmapAlpha(const SkBitmap &src, uint8_t *SK_RESTRICT alpha, int alphaRowBytes)
Definition: SkBitmap.cpp:523
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
SkMaskFilterBase * as_MFB(SkMaskFilter *mf)
std::unique_ptr< uint8_t, SkFunctionObject< SkMaskBuilder::FreeImage > > SkAutoMaskFreeImage
Definition: SkMask.h:316
void swap(SkBitmap &other)
Definition: SkBitmap.cpp:86
const Paint & paint
Definition: color_source.cc:38
dst
Definition: cp.py:12
SeparatedVector2 offset
constexpr int32_t height() const
Definition: SkRect.h:165
int32_t fTop
smaller y-axis bounds
Definition: SkRect.h:34
constexpr int32_t width() const
Definition: SkRect.h:158
void setWH(int32_t width, int32_t height)
Definition: SkRect.h:275
int32_t fLeft
smaller x-axis bounds
Definition: SkRect.h:33
static SkImageInfo MakeA8(int width, int height)
Format & format()
Definition: SkMask.h:239
uint32_t & rowBytes()
Definition: SkMask.h:238
static uint8_t * AllocImage(size_t bytes, AllocType=kUninit_Alloc)
Definition: SkMask.cpp:45
SkIRect & bounds()
Definition: SkMask.h:237
uint8_t *& image()
Definition: SkMask.h:236
const uint32_t fRowBytes
Definition: SkMask.h:43
@ kA8_Format
8bits per pixel mask (e.g. antialiasing)
Definition: SkMask.h:28
uint8_t const *const fImage
Definition: SkMask.h:41
const SkIRect fBounds
Definition: SkMask.h:42
size_t computeImageSize() const
Definition: SkMask.cpp:30

◆ extractAlpha() [3/3]

bool SkBitmap::extractAlpha ( SkBitmap dst,
const SkPaint paint,
SkIPoint offset 
) const
inline

Sets dst to alpha described by pixels. Returns false if dst cannot be written to or dst pixels cannot be allocated.

If paint is not nullptr and contains SkMaskFilter, SkMaskFilter generates mask alpha from SkBitmap. Uses HeapAllocator to reserve memory for dst SkPixelRef. Sets offset to top-left position for dst for alignment with SkBitmap; (0, 0) unless SkMaskFilter generates mask.

Parameters
dstholds SkPixelRef to fill with alpha layer
paintholds optional SkMaskFilter; may be nullptr
offsettop-left position for dst; may be nullptr
Returns
true if alpha layer was constructed in dst SkPixelRef

Definition at line 1151 of file SkBitmap.h.

1152 {
1153 return this->extractAlpha(dst, paint, nullptr, offset);
1154 }

◆ extractSubset()

bool SkBitmap::extractSubset ( SkBitmap dst,
const SkIRect subset 
) const

Shares SkPixelRef with dst. Pixels are not copied; SkBitmap and dst point to the same pixels; dst bounds() are set to the intersection of subset and the original bounds().

subset may be larger than bounds(). Any area outside of bounds() is ignored.

Any contents of dst are discarded.

Return false if:

Parameters
dstSkBitmap set to subset
subsetrectangle of pixels to reference
Returns
true if dst is replaced by subset

example: https://fiddle.skia.org/c/@Bitmap_extractSubset

Definition at line 453 of file SkBitmap.cpp.

453 {
454 SkDEBUGCODE(this->validate();)
455
456 if (nullptr == result || !fPixelRef) {
457 return false; // no src pixels
458 }
459
460 SkIRect srcRect, r;
461 srcRect.setWH(this->width(), this->height());
462 if (!r.intersect(srcRect, subset)) {
463 return false; // r is empty (i.e. no intersection)
464 }
465
466 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
467 // exited above.
468 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
469 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
470
472 dst.setInfo(this->info().makeDimensions(r.size()), this->rowBytes());
473
474 if (fPixelRef) {
475 SkIPoint origin = this->pixelRefOrigin();
476 // share the pixelref with a custom offset
477 dst.setPixelRef(fPixelRef, origin.x() + r.fLeft, origin.y() + r.fTop);
478 }
479 SkDEBUGCODE(dst.validate();)
480
481 // we know we're good, so commit to result
482 result->swap(dst);
483 return true;
484}
SkIPoint pixelRefOrigin() const
Definition: SkBitmap.cpp:168
void setPixelRef(sk_sp< SkPixelRef > pixelRef, int dx, int dy)
Definition: SkBitmap.cpp:182
constexpr int32_t y() const
Definition: SkPoint_impl.h:52
constexpr int32_t x() const
Definition: SkPoint_impl.h:46
Definition: SkRect.h:32
bool intersect(const SkIRect &r)
Definition: SkRect.h:513
constexpr SkISize size() const
Definition: SkRect.h:172

◆ getAddr()

void * SkBitmap::getAddr ( int  x,
int  y 
) const

Returns pixel address at (x, y).

Input is not validated: out of bounds values of x or y, or kUnknown_SkColorType, trigger an assert() if built with SK_DEBUG defined. Returns nullptr if SkColorType is kUnknown_SkColorType, or SkPixelRef is nullptr.

Performs a lookup of pixel size; for better performance, call one of: getAddr8(), getAddr16(), or getAddr32().

Parameters
xcolumn index, zero or greater, and less than width()
yrow index, zero or greater, and less than height()
Returns
generic pointer to pixel

example: https://fiddle.skia.org/c/@Bitmap_getAddr

Definition at line 406 of file SkBitmap.cpp.

406 {
407 SkASSERT((unsigned)x < (unsigned)this->width());
408 SkASSERT((unsigned)y < (unsigned)this->height());
409
410 char* base = (char*)this->getPixels();
411 if (base) {
412 base += (y * this->rowBytes()) + (x << this->shiftPerPixel());
413 }
414 return base;
415}
int shiftPerPixel() const
Definition: SkBitmap.h:201
void * getPixels() const
Definition: SkBitmap.h:283
double y
double x

◆ getAddr16()

uint16_t * SkBitmap::getAddr16 ( int  x,
int  y 
) const
inline

Returns address at (x, y).

Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:

Parameters
xcolumn index, zero or greater, and less than width()
yrow index, zero or greater, and less than height()
Returns
unsigned 16-bit pointer to pixel at (x, y)

Definition at line 1265 of file SkBitmap.h.

1265 {
1266 SkASSERT(fPixmap.addr());
1267 return fPixmap.writable_addr16(x, y);
1268}
uint16_t * writable_addr16(int x, int y) const
Definition: SkPixmap.h:522
const void * addr() const
Definition: SkPixmap.h:153

◆ getAddr32()

uint32_t * SkBitmap::getAddr32 ( int  x,
int  y 
) const
inline

Returns address at (x, y).

Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:

Parameters
xcolumn index, zero or greater, and less than width()
yrow index, zero or greater, and less than height()
Returns
unsigned 32-bit pointer to pixel at (x, y)

Definition at line 1260 of file SkBitmap.h.

1260 {
1261 SkASSERT(fPixmap.addr());
1262 return fPixmap.writable_addr32(x, y);
1263}
uint32_t * writable_addr32(int x, int y) const
Definition: SkPixmap.h:537

◆ getAddr8()

uint8_t * SkBitmap::getAddr8 ( int  x,
int  y 
) const
inline

Returns address at (x, y).

Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:

Parameters
xcolumn index, zero or greater, and less than width()
yrow index, zero or greater, and less than height()
Returns
unsigned 8-bit pointer to pixel at (x, y)

Definition at line 1270 of file SkBitmap.h.

1270 {
1271 SkASSERT(fPixmap.addr());
1272 return fPixmap.writable_addr8(x, y);
1273}
uint8_t * writable_addr8(int x, int y) const
Definition: SkPixmap.h:508

◆ getAlphaf()

float SkBitmap::getAlphaf ( int  x,
int  y 
) const
inline

Look up the pixel at (x,y) and return its alpha component, normalized to [0..1]. This is roughly equivalent to SkGetColorA(getColor()), but can be more efficent (and more precise if the pixels store more than 8 bits per component).

Parameters
xcolumn index, zero or greater, and less than width()
yrow index, zero or greater, and less than height()
Returns
alpha converted to normalized float

Definition at line 903 of file SkBitmap.h.

903 {
904 return this->pixmap().getAlphaf(x, y);
905 }
float getAlphaf(int x, int y) const
Definition: SkPixmap.cpp:94

◆ getBounds() [1/2]

void SkBitmap::getBounds ( SkIRect bounds) const

Returns SkIRect { 0, 0, width(), height() }.

Parameters
boundscontainer for integral rectangle

example: https://fiddle.skia.org/c/@Bitmap_getBounds_2

Definition at line 103 of file SkBitmap.cpp.

103 {
105 *bounds = fPixmap.bounds();
106}
SkIRect bounds() const
Definition: SkBitmap.h:382
SkIRect bounds() const
Definition: SkPixmap.h:207

◆ getBounds() [2/2]

void SkBitmap::getBounds ( SkRect bounds) const

Returns SkRect { 0, 0, width(), height() }.

Parameters
boundscontainer for floating point rectangle

example: https://fiddle.skia.org/c/@Bitmap_getBounds

Definition at line 98 of file SkBitmap.cpp.

98 {
100 *bounds = SkRect::Make(this->dimensions());
101}
SkISize dimensions() const
Definition: SkBitmap.h:388
static SkRect Make(const SkISize &size)
Definition: SkRect.h:669

◆ getColor()

SkColor SkBitmap::getColor ( int  x,
int  y 
) const
inline

Returns pixel at (x, y) as unpremultiplied color. Returns black with alpha if SkColorType is kAlpha_8_SkColorType.

Input is not validated: out of bounds values of x or y trigger an assert() if built with SK_DEBUG defined; and returns undefined values or may crash if SK_RELEASE is defined. Fails if SkColorType is kUnknown_SkColorType or pixel address is nullptr.

SkColorSpace in SkImageInfo is ignored. Some color precision may be lost in the conversion to unpremultiplied color; original pixel data may have additional precision.

Parameters
xcolumn index, zero or greater, and less than width()
yrow index, zero or greater, and less than height()
Returns
pixel converted to unpremultiplied color

Definition at line 874 of file SkBitmap.h.

874 {
875 return this->pixmap().getColor(x, y);
876 }
SkColor getColor(int x, int y) const
Definition: SkPixmap.cpp:187

◆ getColor4f()

SkColor4f SkBitmap::getColor4f ( int  x,
int  y 
) const
inline

Returns pixel at (x, y) as unpremultiplied float color. Returns black with alpha if SkColorType is kAlpha_8_SkColorType.

Input is not validated: out of bounds values of x or y trigger an assert() if built with SK_DEBUG defined; and returns undefined values or may crash if SK_RELEASE is defined. Fails if SkColorType is kUnknown_SkColorType or pixel address is nullptr.

SkColorSpace in SkImageInfo is ignored. Some color precision may be lost in the conversion to unpremultiplied color.

Parameters
xcolumn index, zero or greater, and less than width()
yrow index, zero or greater, and less than height()
Returns
pixel converted to unpremultiplied color

Definition at line 893 of file SkBitmap.h.

893{ return this->pixmap().getColor4f(x, y); }
SkColor4f getColor4f(int x, int y) const
Definition: SkPixmap.cpp:388

◆ getGenerationID()

uint32_t SkBitmap::getGenerationID ( ) const

Returns a unique value corresponding to the pixels in SkPixelRef. Returns a different value after notifyPixelsChanged() has been called. Returns zero if SkPixelRef is nullptr.

Determines if pixels have changed since last examined.

Returns
unique value for pixels in SkPixelRef

example: https://fiddle.skia.org/c/@Bitmap_getGenerationID

Definition at line 361 of file SkBitmap.cpp.

361 {
362 return fPixelRef ? fPixelRef->getGenerationID() : 0;
363}
uint32_t getGenerationID() const
Definition: SkPixelRef.cpp:63

◆ getPixels()

void * SkBitmap::getPixels ( ) const
inline

Returns pixel address, the base address corresponding to the pixel origin.

Returns
pixel address

Definition at line 283 of file SkBitmap.h.

283{ return fPixmap.writable_addr(); }
void * writable_addr() const
Definition: SkPixmap.h:483

◆ getSubset()

SkIRect SkBitmap::getSubset ( ) const
inline

Returns the bounds of this bitmap, offset by its SkPixelRef origin.

Returns
bounds within SkPixelRef bounds

Definition at line 394 of file SkBitmap.h.

394 {
395 SkIPoint origin = this->pixelRefOrigin();
396 return SkIRect::MakeXYWH(origin.x(), origin.y(), this->width(), this->height());
397 }
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: SkRect.h:104

◆ height()

int SkBitmap::height ( ) const
inline

Returns pixel row count.

Maybe be less than pixelRef().height(). Will not exceed pixelRef().height() less pixelRefOrigin().fY.

Returns
pixel height in SkImageInfo

Definition at line 158 of file SkBitmap.h.

158{ return fPixmap.height(); }
int height() const
Definition: SkPixmap.h:166

◆ info()

const SkImageInfo & SkBitmap::info ( ) const
inline

Returns width, height, SkAlphaType, SkColorType, and SkColorSpace.

Returns
reference to SkImageInfo

Definition at line 139 of file SkBitmap.h.

139{ return fPixmap.info(); }

◆ installMaskPixels()

bool SkBitmap::installMaskPixels ( SkMaskBuilder mask)

Deprecated.

Definition at line 349 of file SkBitmap.cpp.

349 {
350 if (SkMask::kA8_Format != mask.fFormat) {
351 this->reset();
352 return false;
353 }
355 mask.fBounds.height()),
356 mask.image(), mask.fRowBytes);
357}
bool installPixels(const SkImageInfo &info, void *pixels, size_t rowBytes, void(*releaseProc)(void *addr, void *context), void *context)
Definition: SkBitmap.cpp:323
void reset()
Definition: SkBitmap.cpp:92
const Format fFormat
Definition: SkMask.h:44

◆ installPixels() [1/3]

bool SkBitmap::installPixels ( const SkImageInfo info,
void *  pixels,
size_t  rowBytes 
)
inline

Sets SkImageInfo to info following the rules in setInfo(), and creates SkPixelRef containing pixels and rowBytes.

If SkImageInfo could not be set, or rowBytes is less than info.minRowBytes(): calls reset(), and returns false.

Otherwise, if pixels equals nullptr: sets SkImageInfo, returns true.

Caller must ensure that pixels are valid for the lifetime of SkBitmap and SkPixelRef.

Parameters
infocontains width, height, SkAlphaType, SkColorType, SkColorSpace
pixelsaddress or pixel storage; may be nullptr
rowBytessize of pixel row or larger
Returns
true if SkImageInfo is set to info

Definition at line 627 of file SkBitmap.h.

627 {
628 return this->installPixels(info, pixels, rowBytes, nullptr, nullptr);
629 }

◆ installPixels() [2/3]

bool SkBitmap::installPixels ( const SkImageInfo info,
void *  pixels,
size_t  rowBytes,
void(*)(void *addr, void *context)  releaseProc,
void *  context 
)

Sets SkImageInfo to info following the rules in setInfo(), and creates SkPixelRef containing pixels and rowBytes. releaseProc, if not nullptr, is called immediately on failure or when pixels are no longer referenced. context may be nullptr.

If SkImageInfo could not be set, or rowBytes is less than info.minRowBytes(): calls releaseProc if present, calls reset(), and returns false.

Otherwise, if pixels equals nullptr: sets SkImageInfo, calls releaseProc if present, returns true.

If SkImageInfo is set, pixels is not nullptr, and releaseProc is not nullptr: when pixels are no longer referenced, calls releaseProc with pixels and context as parameters.

Parameters
infocontains width, height, SkAlphaType, SkColorType, SkColorSpace
pixelsaddress or pixel storage; may be nullptr
rowBytessize of pixel row or larger
releaseProcfunction called when pixels can be deleted; may be nullptr
contextcaller state passed to releaseProc; may be nullptr
Returns
true if SkImageInfo is set to info

Definition at line 323 of file SkBitmap.cpp.

324 {
325 if (!this->setInfo(requestedInfo, rb)) {
326 invoke_release_proc(releaseProc, pixels, context);
327 this->reset();
328 return false;
329 }
330 if (nullptr == pixels) {
331 invoke_release_proc(releaseProc, pixels, context);
332 return true; // we behaved as if they called setInfo()
333 }
334
335 // setInfo may have corrected info (e.g. 565 is always opaque).
336 const SkImageInfo& correctedInfo = this->info();
337 this->setPixelRef(
338 SkMakePixelRefWithProc(correctedInfo.width(), correctedInfo.height(),
339 rb, pixels, releaseProc, context), 0, 0);
340 SkDEBUGCODE(this->validate();)
341 return true;
342}
static void invoke_release_proc(void(*proc)(void *pixels, void *ctx), void *pixels, void *ctx)
Definition: SkBitmap.cpp:317
static void releaseProc(const void *ptr, void *context)
sk_sp< SkPixelRef > SkMakePixelRefWithProc(int width, int height, size_t rowBytes, void *addr, void(*releaseProc)(void *addr, void *ctx), void *ctx)
Definition: SkPixelRef.cpp:137
bool setInfo(const SkImageInfo &imageInfo, size_t rowBytes=0)
Definition: SkBitmap.cpp:114

◆ installPixels() [3/3]

bool SkBitmap::installPixels ( const SkPixmap pixmap)

Sets SkImageInfo to pixmap.info() following the rules in setInfo(), and creates SkPixelRef containing pixmap.addr() and pixmap.rowBytes().

If SkImageInfo could not be set, or pixmap.rowBytes() is less than SkImageInfo::minRowBytes(): calls reset(), and returns false.

Otherwise, if pixmap.addr() equals nullptr: sets SkImageInfo, returns true.

Caller must ensure that pixmap is valid for the lifetime of SkBitmap and SkPixelRef.

Parameters
pixmapSkImageInfo, pixel address, and rowBytes()
Returns
true if SkImageInfo was set to pixmap.info()

example: https://fiddle.skia.org/c/@Bitmap_installPixels_3

Definition at line 344 of file SkBitmap.cpp.

344 {
345 return this->installPixels(pixmap.info(), pixmap.writable_addr(), pixmap.rowBytes(),
346 nullptr, nullptr);
347}
size_t rowBytes() const
Definition: SkPixmap.h:145

◆ isImmutable()

bool SkBitmap::isImmutable ( ) const

Returns true if pixels can not change.

Most immutable SkBitmap checks trigger an assert only on debug builds.

Returns
true if pixels are immutable

example: https://fiddle.skia.org/c/@Bitmap_isImmutable

Definition at line 396 of file SkBitmap.cpp.

396 {
397 return fPixelRef ? fPixelRef->isImmutable() : false;
398}
bool isImmutable() const
Definition: SkPixelRef.h:55

◆ isNull()

bool SkBitmap::isNull ( ) const
inline

Returns true if SkPixelRef is nullptr.

Does not check if width() or height() are zero; call drawsNothing() to check width(), height(), and SkPixelRef.

Returns
true if no SkPixelRef is associated

Definition at line 219 of file SkBitmap.h.

219{ return nullptr == fPixelRef; }

◆ isOpaque()

bool SkBitmap::isOpaque ( ) const
inline

Returns true if SkAlphaType is set to hint that all pixels are opaque; their alpha value is implicitly or explicitly 1.0. If true, and all pixels are not opaque, Skia may draw incorrectly.

Does not check if SkColorType allows alpha, or if any pixel value has transparency.

Returns
true if SkImageInfo SkAlphaType is kOpaque_SkAlphaType

Definition at line 324 of file SkBitmap.h.

324 {
325 return SkAlphaTypeIsOpaque(this->alphaType());
326 }
static bool SkAlphaTypeIsOpaque(SkAlphaType at)
Definition: SkAlphaType.h:41
SkAlphaType alphaType() const
Definition: SkBitmap.h:162

◆ makeShader() [1/4]

sk_sp< SkShader > SkBitmap::makeShader ( const SkSamplingOptions sampling,
const SkMatrix lm 
) const

Defaults to clamp in both X and Y.

Definition at line 647 of file SkBitmap.cpp.

648 {
650 sampling, &lm);
651}
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkBitmap.cpp:669

◆ makeShader() [2/4]

sk_sp< SkShader > SkBitmap::makeShader ( const SkSamplingOptions sampling,
const SkMatrix lm = nullptr 
) const

Definition at line 653 of file SkBitmap.cpp.

654 {
656 sampling, lm);
657}

◆ makeShader() [3/4]

sk_sp< SkShader > SkBitmap::makeShader ( SkTileMode  tmx,
SkTileMode  tmy,
const SkSamplingOptions sampling,
const SkMatrix localMatrix = nullptr 
) const

Make a shader with the specified tiling, matrix and sampling.

Definition at line 669 of file SkBitmap.cpp.

671 {
672 if (lm && !lm->invert(nullptr)) {
673 return nullptr;
674 }
676 tmx, tmy, sampling, lm);
677}
SK_SPI sk_sp< SkImage > SkMakeImageFromRasterBitmap(const SkBitmap &, SkCopyPixelsMode)
@ kIfMutable_SkCopyPixelsMode
only copy src pixels if they are marked mutable
Definition: SkImagePriv.h:18
static sk_sp< SkShader > Make(sk_sp< SkImage >, SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix, bool clampAsIfUnpremul=false)
SkSamplingOptions sampling
Definition: SkRecords.h:337
SkTileMode tmy
SkTileMode tmx

◆ makeShader() [4/4]

sk_sp< SkShader > SkBitmap::makeShader ( SkTileMode  tmx,
SkTileMode  tmy,
const SkSamplingOptions sampling,
const SkMatrix lm 
) const

Definition at line 659 of file SkBitmap.cpp.

661 {
662 if (!lm.invert(nullptr)) {
663 return nullptr;
664 }
666 tmx, tmy, sampling, &lm);
667}
bool invert(SkMatrix *inverse) const
Definition: SkMatrix.h:1206

◆ notifyPixelsChanged()

void SkBitmap::notifyPixelsChanged ( ) const

Marks that pixels in SkPixelRef have changed. Subsequent calls to getGenerationID() return a different value.

example: https://fiddle.skia.org/c/@Bitmap_notifyPixelsChanged

Definition at line 365 of file SkBitmap.cpp.

365 {
366 SkASSERT(!this->isImmutable());
367 if (fPixelRef) {
368 fPixelRef->notifyPixelsChanged();
369 }
370}
bool isImmutable() const
Definition: SkBitmap.cpp:396
void notifyPixelsChanged()
Definition: SkPixelRef.cpp:102

◆ operator=() [1/2]

SkBitmap & SkBitmap::operator= ( const SkBitmap src)

Copies settings from src to returned SkBitmap. Shares pixels if src has pixels allocated, so both bitmaps reference the same pixels.

Parameters
srcSkBitmap to copy SkImageInfo, and share SkPixelRef
Returns
copy of src

example: https://fiddle.skia.org/c/@Bitmap_copy_operator

Definition at line 65 of file SkBitmap.cpp.

65 {
66 if (this != &src) {
67 fPixelRef = src.fPixelRef;
68 fPixmap = src.fPixmap;
69 fMips = src.fMips;
70 }
71 SkDEBUGCODE(this->validate();)
72 return *this;
73}

◆ operator=() [2/2]

SkBitmap & SkBitmap::operator= ( SkBitmap &&  src)

Copies settings from src to returned SkBitmap. Moves ownership of src pixels to SkBitmap.

Parameters
srcSkBitmap to copy SkImageInfo, and reassign SkPixelRef
Returns
copy of src

example: https://fiddle.skia.org/c/@Bitmap_move_operator

Definition at line 75 of file SkBitmap.cpp.

75 {
76 if (this != &other) {
77 fPixelRef = std::move(other.fPixelRef);
78 fPixmap = std::move(other.fPixmap);
79 fMips = std::move(other.fMips);
80 SkASSERT(!other.fPixelRef);
81 other.fPixmap.reset();
82 }
83 return *this;
84}

◆ peekPixels()

bool SkBitmap::peekPixels ( SkPixmap pixmap) const

Copies SkBitmap pixel address, row bytes, and SkImageInfo to pixmap, if address is available, and returns true. If pixel address is not available, return false and leave pixmap unchanged.

pixmap contents become invalid on any future change to SkBitmap.

Parameters
pixmapstorage for pixel state if pixels are readable; otherwise, ignored
Returns
true if SkBitmap has direct access to pixels

example: https://fiddle.skia.org/c/@Bitmap_peekPixels

Definition at line 635 of file SkBitmap.cpp.

635 {
636 if (this->getPixels()) {
637 if (pmap) {
638 *pmap = fPixmap;
639 }
640 return true;
641 }
642 return false;
643}

◆ pixelRef()

SkPixelRef * SkBitmap::pixelRef ( ) const
inline

Returns SkPixelRef, which contains: pixel base address; its dimensions; and rowBytes(), the interval from one row to the next. Does not change SkPixelRef reference count. SkPixelRef may be shared by multiple bitmaps. If SkPixelRef has not been set, returns nullptr.

Returns
SkPixelRef, or nullptr

Definition at line 720 of file SkBitmap.h.

720{ return fPixelRef.get(); }
T * get() const
Definition: SkRefCnt.h:303

◆ pixelRefOrigin()

SkIPoint SkBitmap::pixelRefOrigin ( ) const

Returns origin of pixels within SkPixelRef. SkBitmap bounds is always contained by SkPixelRef bounds, which may be the same size or larger. Multiple SkBitmap can share the same SkPixelRef, where each SkBitmap has different bounds.

The returned origin added to SkBitmap dimensions equals or is smaller than the SkPixelRef dimensions.

Returns (0, 0) if SkPixelRef is nullptr.

Returns
pixel origin within SkPixelRef

example: https://fiddle.skia.org/c/@Bitmap_pixelRefOrigin

Definition at line 168 of file SkBitmap.cpp.

168 {
169 const char* addr = (const char*)fPixmap.addr();
170 const char* pix = (const char*)(fPixelRef ? fPixelRef->pixels() : nullptr);
171 size_t rb = this->rowBytes();
172 if (!pix || 0 == rb) {
173 return {0, 0};
174 }
175 SkASSERT(this->bytesPerPixel() > 0);
176 SkASSERT(this->bytesPerPixel() == (1 << this->shiftPerPixel()));
177 SkASSERT(addr >= pix);
178 size_t off = addr - pix;
179 return {SkToS32((off % rb) >> this->shiftPerPixel()), SkToS32(off / rb)};
180}
constexpr int32_t SkToS32(S x)
Definition: SkTo.h:25
int bytesPerPixel() const
Definition: SkBitmap.h:187
void * pixels() const
Definition: SkPixelRef.h:36

◆ pixmap()

const SkPixmap & SkBitmap::pixmap ( ) const
inline

Returns a constant reference to the SkPixmap holding the SkBitmap pixel address, row bytes, and SkImageInfo.

Returns
reference to SkPixmap describing this SkBitmap

Definition at line 133 of file SkBitmap.h.

133{ return fPixmap; }

◆ readPixels() [1/3]

bool SkBitmap::readPixels ( const SkImageInfo dstInfo,
void *  dstPixels,
size_t  dstRowBytes,
int  srcX,
int  srcY 
) const

Copies a SkRect of pixels from SkBitmap to dstPixels. Copy starts at (srcX, srcY), and does not exceed SkBitmap (width(), height()).

dstInfo specifies width, height, SkColorType, SkAlphaType, and SkColorSpace of destination. dstRowBytes specifics the gap from one destination row to the next. Returns true if pixels are copied. Returns false if:

  • dstInfo has no address
  • dstRowBytes is less than dstInfo.minRowBytes()
  • SkPixelRef is nullptr

Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType() must match. If SkBitmap colorType() is kGray_8_SkColorType, dstInfo.colorSpace() must match. If SkBitmap alphaType() is kOpaque_SkAlphaType, dstInfo.alphaType() must match. If SkBitmap colorSpace() is nullptr, dstInfo.colorSpace() must match. Returns false if pixel conversion is not possible.

srcX and srcY may be negative to copy only top or left of source. Returns false if width() or height() is zero or negative. Returns false if abs(srcX) >= Bitmap width(), or if abs(srcY) >= Bitmap height().

Parameters
dstInfodestination width, height, SkColorType, SkAlphaType, SkColorSpace
dstPixelsdestination pixel storage
dstRowBytesdestination row length
srcXcolumn index whose absolute value is less than width()
srcYrow index whose absolute value is less than height()
Returns
true if pixels are copied to dstPixels

Definition at line 488 of file SkBitmap.cpp.

489 {
491 if (!this->peekPixels(&src)) {
492 return false;
493 }
494 return src.readPixels(requestedDstInfo, dstPixels, dstRB, x, y);
495}

◆ readPixels() [2/3]

bool SkBitmap::readPixels ( const SkPixmap dst) const
inline

Copies a SkRect of pixels from SkBitmap to dst. Copy starts at (0, 0), and does not exceed SkBitmap (width(), height()).

dst specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage, and row bytes of destination. dst.rowBytes() specifics the gap from one destination row to the next. Returns true if pixels are copied. Returns false if:

Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is kGray_8_SkColorType, or kAlpha_8_SkColorType; dst SkColorType must match. If SkBitmap colorType() is kGray_8_SkColorType, dst SkColorSpace must match. If SkBitmap alphaType() is kOpaque_SkAlphaType, dst SkAlphaType must match. If SkBitmap colorSpace() is nullptr, dst SkColorSpace must match. Returns false if pixel conversion is not possible.

Parameters
dstdestination SkPixmap: SkImageInfo, pixels, row bytes
Returns
true if pixels are copied to dst

Definition at line 1068 of file SkBitmap.h.

1068 {
1069 return this->readPixels(dst, 0, 0);
1070 }
bool readPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY) const
Definition: SkBitmap.cpp:488

◆ readPixels() [3/3]

bool SkBitmap::readPixels ( const SkPixmap dst,
int  srcX,
int  srcY 
) const

Copies a SkRect of pixels from SkBitmap to dst. Copy starts at (srcX, srcY), and does not exceed SkBitmap (width(), height()).

dst specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage, and row bytes of destination. dst.rowBytes() specifics the gap from one destination row to the next. Returns true if pixels are copied. Returns false if:

Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is kGray_8_SkColorType, or kAlpha_8_SkColorType; dst SkColorType must match. If SkBitmap colorType() is kGray_8_SkColorType, dst SkColorSpace must match. If SkBitmap alphaType() is kOpaque_SkAlphaType, dst SkAlphaType must match. If SkBitmap colorSpace() is nullptr, dst SkColorSpace must match. Returns false if pixel conversion is not possible.

srcX and srcY may be negative to copy only top or left of source. Returns false if width() or height() is zero or negative. Returns false if abs(srcX) >= Bitmap width(), or if abs(srcY) >= Bitmap height().

Parameters
dstdestination SkPixmap: SkImageInfo, pixels, row bytes
srcXcolumn index whose absolute value is less than width()
srcYrow index whose absolute value is less than height()
Returns
true if pixels are copied to dst

example: https://fiddle.skia.org/c/@Bitmap_readPixels_2

Definition at line 497 of file SkBitmap.cpp.

497 {
498 return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), srcX, srcY);
499}

◆ readyToDraw()

bool SkBitmap::readyToDraw ( ) const
inline

Returns true if SkBitmap is can be drawn.

Returns
true if getPixels() is not nullptr

Definition at line 758 of file SkBitmap.h.

758 {
759 return this->getPixels() != nullptr;
760 }

◆ refColorSpace()

sk_sp< SkColorSpace > SkBitmap::refColorSpace ( ) const

Returns smart pointer to SkColorSpace, the range of colors, associated with SkImageInfo. The smart pointer tracks the number of objects sharing this SkColorSpace reference so the memory is released when the owners destruct.

The returned SkColorSpace is immutable.

Returns
SkColorSpace in SkImageInfo wrapped in a smart pointer

Definition at line 110 of file SkBitmap.cpp.

110{ return fPixmap.info().refColorSpace(); }
sk_sp< SkColorSpace > refColorSpace() const

◆ reset()

void SkBitmap::reset ( )

Resets to its initial state; all fields are set to zero, as if SkBitmap had been initialized by SkBitmap().

Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to kUnknown_SkColorType; and SkAlphaType to kUnknown_SkAlphaType.

If SkPixelRef is allocated, its reference count is decreased by one, releasing its memory if SkBitmap is the sole owner.

example: https://fiddle.skia.org/c/@Bitmap_reset

Definition at line 92 of file SkBitmap.cpp.

92 {
93 fPixelRef = nullptr; // Free pixels.
94 fPixmap.reset();
95 fMips.reset();
96}
void reset()
Definition: SkPixmap.cpp:32
void reset(T *ptr=nullptr)
Definition: SkRefCnt.h:310

◆ rowBytes()

size_t SkBitmap::rowBytes ( ) const
inline

Returns row bytes, the interval from one pixel row to the next. Row bytes is at least as large as: width() * info().bytesPerPixel().

Returns zero if colorType() is kUnknown_SkColorType, or if row bytes supplied to setInfo() is not large enough to hold a row of pixels.

Returns
byte length of pixel row

Definition at line 238 of file SkBitmap.h.

238{ return fPixmap.rowBytes(); }

◆ rowBytesAsPixels()

int SkBitmap::rowBytesAsPixels ( ) const
inline

Returns number of pixels that fit on row. Should be greater than or equal to width().

Returns
maximum pixels per row

Definition at line 194 of file SkBitmap.h.

194{ return fPixmap.rowBytesAsPixels(); }
int rowBytesAsPixels() const
Definition: SkPixmap.h:214

◆ setAlphaType()

bool SkBitmap::setAlphaType ( SkAlphaType  alphaType)

Sets SkAlphaType, if alphaType is compatible with SkColorType. Returns true unless alphaType is kUnknown_SkAlphaType and current SkAlphaType is not kUnknown_SkAlphaType.

Returns true if SkColorType is kUnknown_SkColorType. alphaType is ignored, and SkAlphaType remains kUnknown_SkAlphaType.

Returns true if SkColorType is kRGB_565_SkColorType or kGray_8_SkColorType. alphaType is ignored, and SkAlphaType remains kOpaque_SkAlphaType.

If SkColorType is kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: returns true unless alphaType is kUnknown_SkAlphaType and SkAlphaType is not kUnknown_SkAlphaType. If SkAlphaType is kUnknown_SkAlphaType, alphaType is ignored.

If SkColorType is kAlpha_8_SkColorType, returns true unless alphaType is kUnknown_SkAlphaType and SkAlphaType is not kUnknown_SkAlphaType. If SkAlphaType is kUnknown_SkAlphaType, alphaType is ignored. If alphaType is kUnpremul_SkAlphaType, it is treated as kPremul_SkAlphaType.

This changes SkAlphaType in SkPixelRef; all bitmaps sharing SkPixelRef are affected.

Returns
true if SkAlphaType is set

example: https://fiddle.skia.org/c/@Bitmap_setAlphaType

Definition at line 148 of file SkBitmap.cpp.

148 {
149 if (!SkColorTypeValidateAlphaType(this->colorType(), newAlphaType, &newAlphaType)) {
150 return false;
151 }
152 if (this->alphaType() != newAlphaType) {
153 auto newInfo = fPixmap.info().makeAlphaType(newAlphaType);
154 fPixmap.reset(std::move(newInfo), fPixmap.addr(), fPixmap.rowBytes());
155 }
156 SkDEBUGCODE(this->validate();)
157 return true;
158}
SK_API bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType, SkAlphaType *canonical=nullptr)
SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const
Definition: SkImageInfo.h:466

◆ setColorSpace()

void SkBitmap::setColorSpace ( sk_sp< SkColorSpace colorSpace)

Sets the SkColorSpace associated with this SkBitmap.

The raw pixel data is not altered by this call; no conversion is performed.

This changes SkColorSpace in SkPixelRef; all bitmaps sharing SkPixelRef are affected.

Definition at line 160 of file SkBitmap.cpp.

160 {
161 if (this->colorSpace() != newColorSpace.get()) {
162 SkImageInfo newInfo = fPixmap.info().makeColorSpace(std::move(newColorSpace));
163 fPixmap.reset(std::move(newInfo), fPixmap.addr(), fPixmap.rowBytes());
164 }
165 SkDEBUGCODE(this->validate();)
166}
SkColorSpace * colorSpace() const
Definition: SkBitmap.cpp:108
SkImageInfo makeColorSpace(sk_sp< SkColorSpace > cs) const

◆ setImmutable()

void SkBitmap::setImmutable ( )

Sets internal flag to mark SkBitmap as immutable. Once set, pixels can not change. Any other bitmap sharing the same SkPixelRef are also marked as immutable. Once SkPixelRef is marked immutable, the setting cannot be cleared.

Writing to immutable SkBitmap pixels triggers an assert on debug builds.

example: https://fiddle.skia.org/c/@Bitmap_setImmutable

Definition at line 400 of file SkBitmap.cpp.

400 {
401 if (fPixelRef) {
402 fPixelRef->setImmutable();
403 }
404}
void setImmutable()
Definition: SkPixelRef.cpp:112

◆ setInfo()

bool SkBitmap::setInfo ( const SkImageInfo imageInfo,
size_t  rowBytes = 0 
)

Sets width, height, SkAlphaType, SkColorType, SkColorSpace, and optional rowBytes. Frees pixels, and returns true if successful.

imageInfo.alphaType() may be altered to a value permitted by imageInfo.colorSpace(). If imageInfo.colorType() is kUnknown_SkColorType, imageInfo.alphaType() is set to kUnknown_SkAlphaType. If imageInfo.colorType() is kAlpha_8_SkColorType and imageInfo.alphaType() is kUnpremul_SkAlphaType, imageInfo.alphaType() is replaced by kPremul_SkAlphaType. If imageInfo.colorType() is kRGB_565_SkColorType or kGray_8_SkColorType, imageInfo.alphaType() is set to kOpaque_SkAlphaType. If imageInfo.colorType() is kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: imageInfo.alphaType() remains unchanged.

rowBytes must equal or exceed imageInfo.minRowBytes(). If imageInfo.colorSpace() is kUnknown_SkColorType, rowBytes is ignored and treated as zero; for all other SkColorSpace values, rowBytes of zero is treated as imageInfo.minRowBytes().

Calls reset() and returns false if:

  • rowBytes exceeds 31 bits
  • imageInfo.width() is negative
  • imageInfo.height() is negative
  • rowBytes is positive and less than imageInfo.width() times imageInfo.bytesPerPixel()
Parameters
imageInfocontains width, height, SkAlphaType, SkColorType, SkColorSpace
rowBytesimageInfo.minRowBytes() or larger; or zero
Returns
true if SkImageInfo set successfully

example: https://fiddle.skia.org/c/@Bitmap_setInfo

Definition at line 114 of file SkBitmap.cpp.

114 {
115 SkAlphaType newAT = info.alphaType();
117 return reset_return_false(this);
118 }
119 // don't look at info.alphaType(), since newAT is the real value...
120
121 // require that rowBytes fit in 31bits
122 int64_t mrb = info.minRowBytes64();
123 if (!SkTFitsIn<int32_t>(mrb)) {
124 return reset_return_false(this);
125 }
126 if (!SkTFitsIn<int32_t>(rowBytes)) {
127 return reset_return_false(this);
128 }
129
130 if (info.width() < 0 || info.height() < 0) {
131 return reset_return_false(this);
132 }
133
135 rowBytes = 0;
136 } else if (0 == rowBytes) {
137 rowBytes = (size_t)mrb;
138 } else if (!info.validRowBytes(rowBytes)) {
139 return reset_return_false(this);
140 }
141
142 fPixelRef = nullptr; // Free pixels.
143 fPixmap.reset(info.makeAlphaType(newAT), nullptr, SkToU32(rowBytes));
144 SkDEBUGCODE(this->validate();)
145 return true;
146}
SkAlphaType
Definition: SkAlphaType.h:26
static bool reset_return_false(SkBitmap *bm)
Definition: SkBitmap.cpp:38
constexpr uint32_t SkToU32(S x)
Definition: SkTo.h:26
uint64_t minRowBytes64() const
Definition: SkImageInfo.h:507
bool validRowBytes(size_t rowBytes) const
Definition: SkImageInfo.h:598

◆ setPixelRef()

void SkBitmap::setPixelRef ( sk_sp< SkPixelRef pixelRef,
int  dx,
int  dy 
)

Replaces pixelRef and origin in SkBitmap. dx and dy specify the offset within the SkPixelRef pixels for the top-left corner of the bitmap.

Asserts in debug builds if dx or dy are out of range. Pins dx and dy to legal range in release builds.

The caller is responsible for ensuring that the pixels match the SkColorType and SkAlphaType in SkImageInfo.

Parameters
pixelRefSkPixelRef describing pixel address and rowBytes()
dxcolumn offset in SkPixelRef for bitmap origin
dyrow offset in SkPixelRef for bitmap origin

example: https://fiddle.skia.org/c/@Bitmap_setPixelRef

Definition at line 182 of file SkBitmap.cpp.

182 {
183#ifdef SK_DEBUG
184 if (pr) {
185 if (kUnknown_SkColorType != this->colorType()) {
186 SkASSERT(dx >= 0 && this->width() + dx <= pr->width());
187 SkASSERT(dy >= 0 && this->height() + dy <= pr->height());
188 }
189 }
190#endif
191 fPixelRef = kUnknown_SkColorType != this->colorType() ? std::move(pr) : nullptr;
192 void* p = nullptr;
193 size_t rowBytes = this->rowBytes();
194 // ignore dx,dy if there is no pixelref
195 if (fPixelRef) {
196 rowBytes = fPixelRef->rowBytes();
197 // TODO(reed): Enforce that PixelRefs must have non-null pixels.
198 p = fPixelRef->pixels();
199 if (p) {
200 p = (char*)p + dy * rowBytes + dx * this->bytesPerPixel();
201 }
202 }
203 fPixmap.reset(fPixmap.info(), p, rowBytes);
204 SkDEBUGCODE(this->validate();)
205}
size_t rowBytes() const
Definition: SkPixelRef.h:37
skia_private::AutoTArray< sk_sp< SkImageFilter > > filters TypedMatrix matrix TypedMatrix matrix SkScalar dx
Definition: SkRecords.h:208

◆ setPixels()

void SkBitmap::setPixels ( void *  pixels)

Replaces SkPixelRef with pixels, preserving SkImageInfo and rowBytes(). Sets SkPixelRef origin to (0, 0).

If pixels is nullptr, or if info().colorType() equals kUnknown_SkColorType; release reference to SkPixelRef, and set SkPixelRef to nullptr.

Caller is responsible for handling ownership pixel memory for the lifetime of SkBitmap and SkPixelRef.

Parameters
pixelsaddress of pixel storage, managed by caller

example: https://fiddle.skia.org/c/@Bitmap_setPixels

Definition at line 207 of file SkBitmap.cpp.

207 {
208 if (kUnknown_SkColorType == this->colorType()) {
209 p = nullptr;
210 }
211 size_t rb = this->rowBytes();
212 fPixmap.reset(fPixmap.info(), p, rb);
213 fPixelRef = p ? sk_make_sp<SkPixelRef>(this->width(), this->height(), p, rb) : nullptr;
214 SkDEBUGCODE(this->validate();)
215}

◆ shiftPerPixel()

int SkBitmap::shiftPerPixel ( ) const
inline

Returns bit shift converting row bytes to row pixels. Returns zero for kUnknown_SkColorType.

Returns
one of: 0, 1, 2, 3; left shift to convert pixels to bytes

Definition at line 201 of file SkBitmap.h.

201{ return fPixmap.shiftPerPixel(); }
int shiftPerPixel() const
Definition: SkPixmap.h:221

◆ swap()

void SkBitmap::swap ( SkBitmap other)

Swaps the fields of the two bitmaps.

Parameters
otherSkBitmap exchanged with original

example: https://fiddle.skia.org/c/@Bitmap_swap

Definition at line 86 of file SkBitmap.cpp.

86 {
87 using std::swap;
88 swap(*this, other);
89 SkDEBUGCODE(this->validate();)
90}
void swap(sk_sp< T > &a, sk_sp< T > &b)
Definition: SkRefCnt.h:341

◆ tryAllocN32Pixels()

bool SkBitmap::tryAllocN32Pixels ( int  width,
int  height,
bool  isOpaque = false 
)

Sets SkImageInfo to width, height, and native color type; and allocates pixel memory. If isOpaque is true, sets SkImageInfo to kOpaque_SkAlphaType; otherwise, sets to kPremul_SkAlphaType.

Calls reset() and returns false if width exceeds 29 bits or is negative, or height is negative.

Returns false if allocation fails.

Use to create SkBitmap that matches SkPMColor, the native pixel arrangement on the platform. SkBitmap drawn to output device skips converting its pixel format.

Parameters
widthpixel column count; must be zero or greater
heightpixel row count; must be zero or greater
isOpaquetrue if pixels do not have transparency
Returns
true if pixel storage is allocated

Definition at line 226 of file SkBitmap.cpp.

◆ tryAllocPixels() [1/4]

bool SkBitmap::tryAllocPixels ( )
inline

Allocates pixel memory with HeapAllocator, and replaces existing SkPixelRef. The allocation size is determined by SkImageInfo width, height, and SkColorType.

Returns false if info().colorType() is kUnknown_SkColorType, or allocation fails.

Returns
true if the allocation succeeds

Definition at line 674 of file SkBitmap.h.

674 {
675 return this->tryAllocPixels((Allocator*)nullptr);
676 }

◆ tryAllocPixels() [2/4]

bool SkBitmap::tryAllocPixels ( Allocator allocator)

Allocates pixel memory with allocator, and replaces existing SkPixelRef. The allocation size is determined by SkImageInfo width, height, and SkColorType. If allocator is nullptr, use HeapAllocator instead.

Returns false if Allocator::allocPixelRef return false.

Parameters
allocatorinstance of SkBitmap::Allocator instantiation
Returns
true if custom allocator reports success

Definition at line 217 of file SkBitmap.cpp.

217 {
218 HeapAllocator stdalloc;
219
220 if (nullptr == allocator) {
221 allocator = &stdalloc;
222 }
223 return allocator->allocPixelRef(this);
224}

◆ tryAllocPixels() [3/4]

bool SkBitmap::tryAllocPixels ( const SkImageInfo info)
inline

Sets SkImageInfo to info following the rules in setInfo() and allocates pixel memory.

Returns false and calls reset() if SkImageInfo could not be set, or memory could not be allocated.

On most platforms, allocating pixel memory may succeed even though there is not sufficient memory to hold pixels; allocation does not take place until the pixels are written to. The actual behavior depends on the platform implementation of malloc().

Parameters
infocontains width, height, SkAlphaType, SkColorType, SkColorSpace
Returns
true if pixel storage is allocated

Definition at line 527 of file SkBitmap.h.

527 {
528 return this->tryAllocPixels(info, info.minRowBytes());
529 }

◆ tryAllocPixels() [4/4]

bool SkBitmap::tryAllocPixels ( const SkImageInfo info,
size_t  rowBytes 
)

Sets SkImageInfo to info following the rules in setInfo() and allocates pixel memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(), or equal zero. Pass in zero for rowBytes to compute the minimum valid value.

Returns false and calls reset() if SkImageInfo could not be set, or memory could not be allocated.

On most platforms, allocating pixel memory may succeed even though there is not sufficient memory to hold pixels; allocation does not take place until the pixels are written to. The actual behavior depends on the platform implementation of malloc().

Parameters
infocontains width, height, SkAlphaType, SkColorType, SkColorSpace
rowBytessize of pixel row or larger; may be zero
Returns
true if pixel storage is allocated

Definition at line 271 of file SkBitmap.cpp.

271 {
272 if (!this->setInfo(requestedInfo, rowBytes)) {
273 return reset_return_false(this);
274 }
275
276 // setInfo may have corrected info (e.g. 565 is always opaque).
277 const SkImageInfo& correctedInfo = this->info();
278 if (kUnknown_SkColorType == correctedInfo.colorType()) {
279 return true;
280 }
281 // setInfo may have computed a valid rowbytes if 0 were passed in
282 rowBytes = this->rowBytes();
283
285 if (!pr) {
286 return reset_return_false(this);
287 }
288 this->setPixelRef(std::move(pr), 0, 0);
289 if (nullptr == this->getPixels()) {
290 return reset_return_false(this);
291 }
292 SkDEBUGCODE(this->validate();)
293 return true;
294}
SK_API sk_sp< SkPixelRef > MakeAllocate(const SkImageInfo &, size_t rowBytes)

◆ tryAllocPixelsFlags()

bool SkBitmap::tryAllocPixelsFlags ( const SkImageInfo info,
uint32_t  flags 
)

Sets SkImageInfo to info following the rules in setInfo() and allocates pixel memory. Memory is zeroed.

Returns false and calls reset() if SkImageInfo could not be set, or memory could not be allocated, or memory could not optionally be zeroed.

On most platforms, allocating pixel memory may succeed even though there is not sufficient memory to hold pixels; allocation does not take place until the pixels are written to. The actual behavior depends on the platform implementation of calloc().

Parameters
infocontains width, height, SkAlphaType, SkColorType, SkColorSpace
flagskZeroPixels_AllocFlag, or zero
Returns
true if pixels allocation is successful

Definition at line 296 of file SkBitmap.cpp.

296 {
297 if (!this->setInfo(requestedInfo)) {
298 return reset_return_false(this);
299 }
300
301 // setInfo may have corrected info (e.g. 565 is always opaque).
302 const SkImageInfo& correctedInfo = this->info();
303
305 correctedInfo.minRowBytes());
306 if (!pr) {
307 return reset_return_false(this);
308 }
309 this->setPixelRef(std::move(pr), 0, 0);
310 if (nullptr == this->getPixels()) {
311 return reset_return_false(this);
312 }
313 SkDEBUGCODE(this->validate();)
314 return true;
315}

◆ width()

int SkBitmap::width ( ) const
inline

Returns pixel count in each row. Should be equal or less than rowBytes() / info().bytesPerPixel().

May be less than pixelRef().width(). Will not exceed pixelRef().width() less pixelRefOrigin().fX.

Returns
pixel width in SkImageInfo

Definition at line 149 of file SkBitmap.h.

149{ return fPixmap.width(); }
int width() const
Definition: SkPixmap.h:160

◆ writePixels() [1/2]

bool SkBitmap::writePixels ( const SkPixmap src)
inline

Copies a SkRect of pixels from src. Copy starts at (0, 0), and does not exceed (src.width(), src.height()).

src specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage, and row bytes of source. src.rowBytes() specifics the gap from one source row to the next. Returns true if pixels are copied. Returns false if:

Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is kGray_8_SkColorType, or kAlpha_8_SkColorType; src SkColorType must match. If SkBitmap colorType() is kGray_8_SkColorType, src SkColorSpace must match. If SkBitmap alphaType() is kOpaque_SkAlphaType, src SkAlphaType must match. If SkBitmap colorSpace() is nullptr, src SkColorSpace must match. Returns false if pixel conversion is not possible.

Parameters
srcsource SkPixmap: SkImageInfo, pixels, row bytes
Returns
true if src pixels are copied to SkBitmap

Definition at line 1122 of file SkBitmap.h.

1122 {
1123 return this->writePixels(src, 0, 0);
1124 }
bool writePixels(const SkPixmap &src, int dstX, int dstY)
Definition: SkBitmap.cpp:501

◆ writePixels() [2/2]

bool SkBitmap::writePixels ( const SkPixmap src,
int  dstX,
int  dstY 
)

Copies a SkRect of pixels from src. Copy starts at (dstX, dstY), and does not exceed (src.width(), src.height()).

src specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage, and row bytes of source. src.rowBytes() specifics the gap from one source row to the next. Returns true if pixels are copied. Returns false if:

Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is kGray_8_SkColorType, or kAlpha_8_SkColorType; src SkColorType must match. If SkBitmap colorType() is kGray_8_SkColorType, src SkColorSpace must match. If SkBitmap alphaType() is kOpaque_SkAlphaType, src SkAlphaType must match. If SkBitmap colorSpace() is nullptr, src SkColorSpace must match. Returns false if pixel conversion is not possible.

dstX and dstY may be negative to copy only top or left of source. Returns false if width() or height() is zero or negative. Returns false if abs(dstX) >= Bitmap width(), or if abs(dstY) >= Bitmap height().

Parameters
srcsource SkPixmap: SkImageInfo, pixels, row bytes
dstXcolumn index whose absolute value is less than width()
dstYrow index whose absolute value is less than height()
Returns
true if src pixels are copied to SkBitmap

example: https://fiddle.skia.org/c/@Bitmap_writePixels

Definition at line 501 of file SkBitmap.cpp.

501 {
502 if (!SkImageInfoValidConversion(this->info(), src.info())) {
503 return false;
504 }
505
506 SkWritePixelsRec rec(src.info(), src.addr(), src.rowBytes(), dstX, dstY);
507 if (!rec.trim(this->width(), this->height())) {
508 return false;
509 }
510
511 void* dstPixels = this->getAddr(rec.fX, rec.fY);
512 const SkImageInfo dstInfo = this->info().makeDimensions(rec.fInfo.dimensions());
513 if (!SkConvertPixels(dstInfo, dstPixels, this->rowBytes(),
514 rec.fInfo, rec.fPixels, rec.fRowBytes)) {
515 return false;
516 }
517 this->notifyPixelsChanged();
518 return true;
519}
bool SkConvertPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRB, const SkImageInfo &srcInfo, const void *srcPixels, size_t srcRB)
static bool SkImageInfoValidConversion(const SkImageInfo &dst, const SkImageInfo &src)
void * getAddr(int x, int y) const
Definition: SkBitmap.cpp:406
SkImageInfo makeDimensions(SkISize newSize) const
Definition: SkImageInfo.h:454

Friends And Related Function Documentation

◆ GrProxyProvider

friend class GrProxyProvider
friend

Definition at line 1255 of file SkBitmap.h.

◆ SkImage_Raster

friend class SkImage_Raster
friend

Definition at line 1253 of file SkBitmap.h.

◆ SkReadBuffer

friend class SkReadBuffer
friend

Definition at line 1254 of file SkBitmap.h.

Member Data Documentation

◆ Allocator

Definition at line 61 of file SkBitmap.h.


The documentation for this class was generated from the following files: