1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2006 The Android Open Source Project 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci 8cb93a386Sopenharmony_ci#ifndef SkBitmap_DEFINED 9cb93a386Sopenharmony_ci#define SkBitmap_DEFINED 10cb93a386Sopenharmony_ci 11cb93a386Sopenharmony_ci#include "include/core/SkColor.h" 12cb93a386Sopenharmony_ci#include "include/core/SkImageInfo.h" 13cb93a386Sopenharmony_ci#include "include/core/SkMatrix.h" 14cb93a386Sopenharmony_ci#include "include/core/SkPixmap.h" 15cb93a386Sopenharmony_ci#include "include/core/SkPoint.h" 16cb93a386Sopenharmony_ci#include "include/core/SkRefCnt.h" 17cb93a386Sopenharmony_ci#include "include/core/SkShader.h" 18cb93a386Sopenharmony_ci#include "include/core/SkTileMode.h" 19cb93a386Sopenharmony_ci 20cb93a386Sopenharmony_ciclass SkBitmap; 21cb93a386Sopenharmony_cistruct SkMask; 22cb93a386Sopenharmony_ciclass SkMipmap; 23cb93a386Sopenharmony_cistruct SkIRect; 24cb93a386Sopenharmony_cistruct SkRect; 25cb93a386Sopenharmony_ciclass SkPaint; 26cb93a386Sopenharmony_ciclass SkPixelRef; 27cb93a386Sopenharmony_ciclass SkShader; 28cb93a386Sopenharmony_ci 29cb93a386Sopenharmony_ci/** \class SkBitmap 30cb93a386Sopenharmony_ci SkBitmap describes a two-dimensional raster pixel array. SkBitmap is built on 31cb93a386Sopenharmony_ci SkImageInfo, containing integer width and height, SkColorType and SkAlphaType 32cb93a386Sopenharmony_ci describing the pixel format, and SkColorSpace describing the range of colors. 33cb93a386Sopenharmony_ci SkBitmap points to SkPixelRef, which describes the physical array of pixels. 34cb93a386Sopenharmony_ci SkImageInfo bounds may be located anywhere fully inside SkPixelRef bounds. 35cb93a386Sopenharmony_ci 36cb93a386Sopenharmony_ci SkBitmap can be drawn using SkCanvas. SkBitmap can be a drawing destination for SkCanvas 37cb93a386Sopenharmony_ci draw member functions. SkBitmap flexibility as a pixel container limits some 38cb93a386Sopenharmony_ci optimizations available to the target platform. 39cb93a386Sopenharmony_ci 40cb93a386Sopenharmony_ci If pixel array is primarily read-only, use SkImage for better performance. 41cb93a386Sopenharmony_ci If pixel array is primarily written to, use SkSurface for better performance. 42cb93a386Sopenharmony_ci 43cb93a386Sopenharmony_ci Declaring SkBitmap const prevents altering SkImageInfo: the SkBitmap height, width, 44cb93a386Sopenharmony_ci and so on cannot change. It does not affect SkPixelRef: a caller may write its 45cb93a386Sopenharmony_ci pixels. Declaring SkBitmap const affects SkBitmap configuration, not its contents. 46cb93a386Sopenharmony_ci 47cb93a386Sopenharmony_ci SkBitmap is not thread safe. Each thread must have its own copy of SkBitmap fields, 48cb93a386Sopenharmony_ci although threads may share the underlying pixel array. 49cb93a386Sopenharmony_ci*/ 50cb93a386Sopenharmony_ciclass SK_API SkBitmap { 51cb93a386Sopenharmony_cipublic: 52cb93a386Sopenharmony_ci class SK_API Allocator; 53cb93a386Sopenharmony_ci 54cb93a386Sopenharmony_ci /** Creates an empty SkBitmap without pixels, with kUnknown_SkColorType, 55cb93a386Sopenharmony_ci kUnknown_SkAlphaType, and with a width and height of zero. SkPixelRef origin is 56cb93a386Sopenharmony_ci set to (0, 0). 57cb93a386Sopenharmony_ci 58cb93a386Sopenharmony_ci Use setInfo() to associate SkColorType, SkAlphaType, width, and height 59cb93a386Sopenharmony_ci after SkBitmap has been created. 60cb93a386Sopenharmony_ci 61cb93a386Sopenharmony_ci @return empty SkBitmap 62cb93a386Sopenharmony_ci 63cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_empty_constructor 64cb93a386Sopenharmony_ci */ 65cb93a386Sopenharmony_ci SkBitmap(); 66cb93a386Sopenharmony_ci 67cb93a386Sopenharmony_ci /** Copies settings from src to returned SkBitmap. Shares pixels if src has pixels 68cb93a386Sopenharmony_ci allocated, so both bitmaps reference the same pixels. 69cb93a386Sopenharmony_ci 70cb93a386Sopenharmony_ci @param src SkBitmap to copy SkImageInfo, and share SkPixelRef 71cb93a386Sopenharmony_ci @return copy of src 72cb93a386Sopenharmony_ci 73cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_copy_const_SkBitmap 74cb93a386Sopenharmony_ci */ 75cb93a386Sopenharmony_ci SkBitmap(const SkBitmap& src); 76cb93a386Sopenharmony_ci 77cb93a386Sopenharmony_ci /** Copies settings from src to returned SkBitmap. Moves ownership of src pixels to 78cb93a386Sopenharmony_ci SkBitmap. 79cb93a386Sopenharmony_ci 80cb93a386Sopenharmony_ci @param src SkBitmap to copy SkImageInfo, and reassign SkPixelRef 81cb93a386Sopenharmony_ci @return copy of src 82cb93a386Sopenharmony_ci 83cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_move_SkBitmap 84cb93a386Sopenharmony_ci */ 85cb93a386Sopenharmony_ci SkBitmap(SkBitmap&& src); 86cb93a386Sopenharmony_ci 87cb93a386Sopenharmony_ci /** Decrements SkPixelRef reference count, if SkPixelRef is not nullptr. 88cb93a386Sopenharmony_ci */ 89cb93a386Sopenharmony_ci ~SkBitmap(); 90cb93a386Sopenharmony_ci 91cb93a386Sopenharmony_ci /** Copies settings from src to returned SkBitmap. Shares pixels if src has pixels 92cb93a386Sopenharmony_ci allocated, so both bitmaps reference the same pixels. 93cb93a386Sopenharmony_ci 94cb93a386Sopenharmony_ci @param src SkBitmap to copy SkImageInfo, and share SkPixelRef 95cb93a386Sopenharmony_ci @return copy of src 96cb93a386Sopenharmony_ci 97cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_copy_operator 98cb93a386Sopenharmony_ci */ 99cb93a386Sopenharmony_ci SkBitmap& operator=(const SkBitmap& src); 100cb93a386Sopenharmony_ci 101cb93a386Sopenharmony_ci /** Copies settings from src to returned SkBitmap. Moves ownership of src pixels to 102cb93a386Sopenharmony_ci SkBitmap. 103cb93a386Sopenharmony_ci 104cb93a386Sopenharmony_ci @param src SkBitmap to copy SkImageInfo, and reassign SkPixelRef 105cb93a386Sopenharmony_ci @return copy of src 106cb93a386Sopenharmony_ci 107cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_move_operator 108cb93a386Sopenharmony_ci */ 109cb93a386Sopenharmony_ci SkBitmap& operator=(SkBitmap&& src); 110cb93a386Sopenharmony_ci 111cb93a386Sopenharmony_ci /** Swaps the fields of the two bitmaps. 112cb93a386Sopenharmony_ci 113cb93a386Sopenharmony_ci @param other SkBitmap exchanged with original 114cb93a386Sopenharmony_ci 115cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_swap 116cb93a386Sopenharmony_ci */ 117cb93a386Sopenharmony_ci void swap(SkBitmap& other); 118cb93a386Sopenharmony_ci 119cb93a386Sopenharmony_ci /** Returns a constant reference to the SkPixmap holding the SkBitmap pixel 120cb93a386Sopenharmony_ci address, row bytes, and SkImageInfo. 121cb93a386Sopenharmony_ci 122cb93a386Sopenharmony_ci @return reference to SkPixmap describing this SkBitmap 123cb93a386Sopenharmony_ci */ 124cb93a386Sopenharmony_ci const SkPixmap& pixmap() const { return fPixmap; } 125cb93a386Sopenharmony_ci 126cb93a386Sopenharmony_ci /** Returns width, height, SkAlphaType, SkColorType, and SkColorSpace. 127cb93a386Sopenharmony_ci 128cb93a386Sopenharmony_ci @return reference to SkImageInfo 129cb93a386Sopenharmony_ci */ 130cb93a386Sopenharmony_ci const SkImageInfo& info() const { return fPixmap.info(); } 131cb93a386Sopenharmony_ci 132cb93a386Sopenharmony_ci /** Returns pixel count in each row. Should be equal or less than 133cb93a386Sopenharmony_ci rowBytes() / info().bytesPerPixel(). 134cb93a386Sopenharmony_ci 135cb93a386Sopenharmony_ci May be less than pixelRef().width(). Will not exceed pixelRef().width() less 136cb93a386Sopenharmony_ci pixelRefOrigin().fX. 137cb93a386Sopenharmony_ci 138cb93a386Sopenharmony_ci @return pixel width in SkImageInfo 139cb93a386Sopenharmony_ci */ 140cb93a386Sopenharmony_ci int width() const { return fPixmap.width(); } 141cb93a386Sopenharmony_ci 142cb93a386Sopenharmony_ci /** Returns pixel row count. 143cb93a386Sopenharmony_ci 144cb93a386Sopenharmony_ci Maybe be less than pixelRef().height(). Will not exceed pixelRef().height() less 145cb93a386Sopenharmony_ci pixelRefOrigin().fY. 146cb93a386Sopenharmony_ci 147cb93a386Sopenharmony_ci @return pixel height in SkImageInfo 148cb93a386Sopenharmony_ci */ 149cb93a386Sopenharmony_ci int height() const { return fPixmap.height(); } 150cb93a386Sopenharmony_ci 151cb93a386Sopenharmony_ci SkColorType colorType() const { return fPixmap.colorType(); } 152cb93a386Sopenharmony_ci 153cb93a386Sopenharmony_ci SkAlphaType alphaType() const { return fPixmap.alphaType(); } 154cb93a386Sopenharmony_ci 155cb93a386Sopenharmony_ci /** Returns SkColorSpace, the range of colors, associated with SkImageInfo. The 156cb93a386Sopenharmony_ci reference count of SkColorSpace is unchanged. The returned SkColorSpace is 157cb93a386Sopenharmony_ci immutable. 158cb93a386Sopenharmony_ci 159cb93a386Sopenharmony_ci @return SkColorSpace in SkImageInfo, or nullptr 160cb93a386Sopenharmony_ci */ 161cb93a386Sopenharmony_ci SkColorSpace* colorSpace() const { return fPixmap.colorSpace(); } 162cb93a386Sopenharmony_ci 163cb93a386Sopenharmony_ci /** Returns smart pointer to SkColorSpace, the range of colors, associated with 164cb93a386Sopenharmony_ci SkImageInfo. The smart pointer tracks the number of objects sharing this 165cb93a386Sopenharmony_ci SkColorSpace reference so the memory is released when the owners destruct. 166cb93a386Sopenharmony_ci 167cb93a386Sopenharmony_ci The returned SkColorSpace is immutable. 168cb93a386Sopenharmony_ci 169cb93a386Sopenharmony_ci @return SkColorSpace in SkImageInfo wrapped in a smart pointer 170cb93a386Sopenharmony_ci */ 171cb93a386Sopenharmony_ci sk_sp<SkColorSpace> refColorSpace() const { return fPixmap.info().refColorSpace(); } 172cb93a386Sopenharmony_ci 173cb93a386Sopenharmony_ci /** Returns number of bytes per pixel required by SkColorType. 174cb93a386Sopenharmony_ci Returns zero if colorType( is kUnknown_SkColorType. 175cb93a386Sopenharmony_ci 176cb93a386Sopenharmony_ci @return bytes in pixel 177cb93a386Sopenharmony_ci */ 178cb93a386Sopenharmony_ci int bytesPerPixel() const { return fPixmap.info().bytesPerPixel(); } 179cb93a386Sopenharmony_ci 180cb93a386Sopenharmony_ci /** Returns number of pixels that fit on row. Should be greater than or equal to 181cb93a386Sopenharmony_ci width(). 182cb93a386Sopenharmony_ci 183cb93a386Sopenharmony_ci @return maximum pixels per row 184cb93a386Sopenharmony_ci */ 185cb93a386Sopenharmony_ci int rowBytesAsPixels() const { return fPixmap.rowBytesAsPixels(); } 186cb93a386Sopenharmony_ci 187cb93a386Sopenharmony_ci /** Returns bit shift converting row bytes to row pixels. 188cb93a386Sopenharmony_ci Returns zero for kUnknown_SkColorType. 189cb93a386Sopenharmony_ci 190cb93a386Sopenharmony_ci @return one of: 0, 1, 2, 3; left shift to convert pixels to bytes 191cb93a386Sopenharmony_ci */ 192cb93a386Sopenharmony_ci int shiftPerPixel() const { return fPixmap.shiftPerPixel(); } 193cb93a386Sopenharmony_ci 194cb93a386Sopenharmony_ci /** Returns true if either width() or height() are zero. 195cb93a386Sopenharmony_ci 196cb93a386Sopenharmony_ci Does not check if SkPixelRef is nullptr; call drawsNothing() to check width(), 197cb93a386Sopenharmony_ci height(), and SkPixelRef. 198cb93a386Sopenharmony_ci 199cb93a386Sopenharmony_ci @return true if dimensions do not enclose area 200cb93a386Sopenharmony_ci */ 201cb93a386Sopenharmony_ci bool empty() const { return fPixmap.info().isEmpty(); } 202cb93a386Sopenharmony_ci 203cb93a386Sopenharmony_ci /** Returns true if SkPixelRef is nullptr. 204cb93a386Sopenharmony_ci 205cb93a386Sopenharmony_ci Does not check if width() or height() are zero; call drawsNothing() to check 206cb93a386Sopenharmony_ci width(), height(), and SkPixelRef. 207cb93a386Sopenharmony_ci 208cb93a386Sopenharmony_ci @return true if no SkPixelRef is associated 209cb93a386Sopenharmony_ci */ 210cb93a386Sopenharmony_ci bool isNull() const { return nullptr == fPixelRef; } 211cb93a386Sopenharmony_ci 212cb93a386Sopenharmony_ci /** Returns true if width() or height() are zero, or if SkPixelRef is nullptr. 213cb93a386Sopenharmony_ci If true, SkBitmap has no effect when drawn or drawn into. 214cb93a386Sopenharmony_ci 215cb93a386Sopenharmony_ci @return true if drawing has no effect 216cb93a386Sopenharmony_ci */ 217cb93a386Sopenharmony_ci bool drawsNothing() const { 218cb93a386Sopenharmony_ci return this->empty() || this->isNull(); 219cb93a386Sopenharmony_ci } 220cb93a386Sopenharmony_ci 221cb93a386Sopenharmony_ci /** Returns row bytes, the interval from one pixel row to the next. Row bytes 222cb93a386Sopenharmony_ci is at least as large as: width() * info().bytesPerPixel(). 223cb93a386Sopenharmony_ci 224cb93a386Sopenharmony_ci Returns zero if colorType() is kUnknown_SkColorType, or if row bytes supplied to 225cb93a386Sopenharmony_ci setInfo() is not large enough to hold a row of pixels. 226cb93a386Sopenharmony_ci 227cb93a386Sopenharmony_ci @return byte length of pixel row 228cb93a386Sopenharmony_ci */ 229cb93a386Sopenharmony_ci size_t rowBytes() const { return fPixmap.rowBytes(); } 230cb93a386Sopenharmony_ci 231cb93a386Sopenharmony_ci /** Sets SkAlphaType, if alphaType is compatible with SkColorType. 232cb93a386Sopenharmony_ci Returns true unless alphaType is kUnknown_SkAlphaType and current SkAlphaType 233cb93a386Sopenharmony_ci is not kUnknown_SkAlphaType. 234cb93a386Sopenharmony_ci 235cb93a386Sopenharmony_ci Returns true if SkColorType is kUnknown_SkColorType. alphaType is ignored, and 236cb93a386Sopenharmony_ci SkAlphaType remains kUnknown_SkAlphaType. 237cb93a386Sopenharmony_ci 238cb93a386Sopenharmony_ci Returns true if SkColorType is kRGB_565_SkColorType or kGray_8_SkColorType. 239cb93a386Sopenharmony_ci alphaType is ignored, and SkAlphaType remains kOpaque_SkAlphaType. 240cb93a386Sopenharmony_ci 241cb93a386Sopenharmony_ci If SkColorType is kARGB_4444_SkColorType, kRGBA_8888_SkColorType, 242cb93a386Sopenharmony_ci kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: returns true unless 243cb93a386Sopenharmony_ci alphaType is kUnknown_SkAlphaType and SkAlphaType is not kUnknown_SkAlphaType. 244cb93a386Sopenharmony_ci If SkAlphaType is kUnknown_SkAlphaType, alphaType is ignored. 245cb93a386Sopenharmony_ci 246cb93a386Sopenharmony_ci If SkColorType is kAlpha_8_SkColorType, returns true unless 247cb93a386Sopenharmony_ci alphaType is kUnknown_SkAlphaType and SkAlphaType is not kUnknown_SkAlphaType. 248cb93a386Sopenharmony_ci If SkAlphaType is kUnknown_SkAlphaType, alphaType is ignored. If alphaType is 249cb93a386Sopenharmony_ci kUnpremul_SkAlphaType, it is treated as kPremul_SkAlphaType. 250cb93a386Sopenharmony_ci 251cb93a386Sopenharmony_ci This changes SkAlphaType in SkPixelRef; all bitmaps sharing SkPixelRef 252cb93a386Sopenharmony_ci are affected. 253cb93a386Sopenharmony_ci 254cb93a386Sopenharmony_ci @return true if SkAlphaType is set 255cb93a386Sopenharmony_ci 256cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_setAlphaType 257cb93a386Sopenharmony_ci */ 258cb93a386Sopenharmony_ci bool setAlphaType(SkAlphaType alphaType); 259cb93a386Sopenharmony_ci 260cb93a386Sopenharmony_ci /** Returns pixel address, the base address corresponding to the pixel origin. 261cb93a386Sopenharmony_ci 262cb93a386Sopenharmony_ci @return pixel address 263cb93a386Sopenharmony_ci */ 264cb93a386Sopenharmony_ci void* getPixels() const { return fPixmap.writable_addr(); } 265cb93a386Sopenharmony_ci 266cb93a386Sopenharmony_ci /** Returns minimum memory required for pixel storage. 267cb93a386Sopenharmony_ci Does not include unused memory on last row when rowBytesAsPixels() exceeds width(). 268cb93a386Sopenharmony_ci Returns SIZE_MAX if result does not fit in size_t. 269cb93a386Sopenharmony_ci Returns zero if height() or width() is 0. 270cb93a386Sopenharmony_ci Returns height() times rowBytes() if colorType() is kUnknown_SkColorType. 271cb93a386Sopenharmony_ci 272cb93a386Sopenharmony_ci @return size in bytes of image buffer 273cb93a386Sopenharmony_ci */ 274cb93a386Sopenharmony_ci size_t computeByteSize() const { return fPixmap.computeByteSize(); } 275cb93a386Sopenharmony_ci 276cb93a386Sopenharmony_ci /** Returns true if pixels can not change. 277cb93a386Sopenharmony_ci 278cb93a386Sopenharmony_ci Most immutable SkBitmap checks trigger an assert only on debug builds. 279cb93a386Sopenharmony_ci 280cb93a386Sopenharmony_ci @return true if pixels are immutable 281cb93a386Sopenharmony_ci 282cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_isImmutable 283cb93a386Sopenharmony_ci */ 284cb93a386Sopenharmony_ci bool isImmutable() const; 285cb93a386Sopenharmony_ci 286cb93a386Sopenharmony_ci /** Sets internal flag to mark SkBitmap as immutable. Once set, pixels can not change. 287cb93a386Sopenharmony_ci Any other bitmap sharing the same SkPixelRef are also marked as immutable. 288cb93a386Sopenharmony_ci Once SkPixelRef is marked immutable, the setting cannot be cleared. 289cb93a386Sopenharmony_ci 290cb93a386Sopenharmony_ci Writing to immutable SkBitmap pixels triggers an assert on debug builds. 291cb93a386Sopenharmony_ci 292cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_setImmutable 293cb93a386Sopenharmony_ci */ 294cb93a386Sopenharmony_ci void setImmutable(); 295cb93a386Sopenharmony_ci 296cb93a386Sopenharmony_ci /** Returns true if SkAlphaType is set to hint that all pixels are opaque; their 297cb93a386Sopenharmony_ci alpha value is implicitly or explicitly 1.0. If true, and all pixels are 298cb93a386Sopenharmony_ci not opaque, Skia may draw incorrectly. 299cb93a386Sopenharmony_ci 300cb93a386Sopenharmony_ci Does not check if SkColorType allows alpha, or if any pixel value has 301cb93a386Sopenharmony_ci transparency. 302cb93a386Sopenharmony_ci 303cb93a386Sopenharmony_ci @return true if SkImageInfo SkAlphaType is kOpaque_SkAlphaType 304cb93a386Sopenharmony_ci */ 305cb93a386Sopenharmony_ci bool isOpaque() const { 306cb93a386Sopenharmony_ci return SkAlphaTypeIsOpaque(this->alphaType()); 307cb93a386Sopenharmony_ci } 308cb93a386Sopenharmony_ci 309cb93a386Sopenharmony_ci /** Resets to its initial state; all fields are set to zero, as if SkBitmap had 310cb93a386Sopenharmony_ci been initialized by SkBitmap(). 311cb93a386Sopenharmony_ci 312cb93a386Sopenharmony_ci Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to 313cb93a386Sopenharmony_ci kUnknown_SkColorType; and SkAlphaType to kUnknown_SkAlphaType. 314cb93a386Sopenharmony_ci 315cb93a386Sopenharmony_ci If SkPixelRef is allocated, its reference count is decreased by one, releasing 316cb93a386Sopenharmony_ci its memory if SkBitmap is the sole owner. 317cb93a386Sopenharmony_ci 318cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_reset 319cb93a386Sopenharmony_ci */ 320cb93a386Sopenharmony_ci void reset(); 321cb93a386Sopenharmony_ci 322cb93a386Sopenharmony_ci /** Returns true if all pixels are opaque. SkColorType determines how pixels 323cb93a386Sopenharmony_ci are encoded, and whether pixel describes alpha. Returns true for SkColorType 324cb93a386Sopenharmony_ci without alpha in each pixel; for other SkColorType, returns true if all 325cb93a386Sopenharmony_ci pixels have alpha values equivalent to 1.0 or greater. 326cb93a386Sopenharmony_ci 327cb93a386Sopenharmony_ci For SkColorType kRGB_565_SkColorType or kGray_8_SkColorType: always 328cb93a386Sopenharmony_ci returns true. For SkColorType kAlpha_8_SkColorType, kBGRA_8888_SkColorType, 329cb93a386Sopenharmony_ci kRGBA_8888_SkColorType: returns true if all pixel alpha values are 255. 330cb93a386Sopenharmony_ci For SkColorType kARGB_4444_SkColorType: returns true if all pixel alpha values are 15. 331cb93a386Sopenharmony_ci For kRGBA_F16_SkColorType: returns true if all pixel alpha values are 1.0 or 332cb93a386Sopenharmony_ci greater. 333cb93a386Sopenharmony_ci 334cb93a386Sopenharmony_ci Returns false for kUnknown_SkColorType. 335cb93a386Sopenharmony_ci 336cb93a386Sopenharmony_ci @param bm SkBitmap to check 337cb93a386Sopenharmony_ci @return true if all pixels have opaque values or SkColorType is opaque 338cb93a386Sopenharmony_ci */ 339cb93a386Sopenharmony_ci static bool ComputeIsOpaque(const SkBitmap& bm) { 340cb93a386Sopenharmony_ci return bm.pixmap().computeIsOpaque(); 341cb93a386Sopenharmony_ci } 342cb93a386Sopenharmony_ci 343cb93a386Sopenharmony_ci /** Returns SkRect { 0, 0, width(), height() }. 344cb93a386Sopenharmony_ci 345cb93a386Sopenharmony_ci @param bounds container for floating point rectangle 346cb93a386Sopenharmony_ci 347cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_getBounds 348cb93a386Sopenharmony_ci */ 349cb93a386Sopenharmony_ci void getBounds(SkRect* bounds) const; 350cb93a386Sopenharmony_ci 351cb93a386Sopenharmony_ci /** Returns SkIRect { 0, 0, width(), height() }. 352cb93a386Sopenharmony_ci 353cb93a386Sopenharmony_ci @param bounds container for integral rectangle 354cb93a386Sopenharmony_ci 355cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_getBounds_2 356cb93a386Sopenharmony_ci */ 357cb93a386Sopenharmony_ci void getBounds(SkIRect* bounds) const; 358cb93a386Sopenharmony_ci 359cb93a386Sopenharmony_ci /** Returns SkIRect { 0, 0, width(), height() }. 360cb93a386Sopenharmony_ci 361cb93a386Sopenharmony_ci @return integral rectangle from origin to width() and height() 362cb93a386Sopenharmony_ci */ 363cb93a386Sopenharmony_ci SkIRect bounds() const { return fPixmap.info().bounds(); } 364cb93a386Sopenharmony_ci 365cb93a386Sopenharmony_ci /** Returns SkISize { width(), height() }. 366cb93a386Sopenharmony_ci 367cb93a386Sopenharmony_ci @return integral size of width() and height() 368cb93a386Sopenharmony_ci */ 369cb93a386Sopenharmony_ci SkISize dimensions() const { return fPixmap.info().dimensions(); } 370cb93a386Sopenharmony_ci 371cb93a386Sopenharmony_ci /** Returns the bounds of this bitmap, offset by its SkPixelRef origin. 372cb93a386Sopenharmony_ci 373cb93a386Sopenharmony_ci @return bounds within SkPixelRef bounds 374cb93a386Sopenharmony_ci */ 375cb93a386Sopenharmony_ci SkIRect getSubset() const { 376cb93a386Sopenharmony_ci SkIPoint origin = this->pixelRefOrigin(); 377cb93a386Sopenharmony_ci return SkIRect::MakeXYWH(origin.x(), origin.y(), this->width(), this->height()); 378cb93a386Sopenharmony_ci } 379cb93a386Sopenharmony_ci 380cb93a386Sopenharmony_ci /** Sets width, height, SkAlphaType, SkColorType, SkColorSpace, and optional 381cb93a386Sopenharmony_ci rowBytes. Frees pixels, and returns true if successful. 382cb93a386Sopenharmony_ci 383cb93a386Sopenharmony_ci imageInfo.alphaType() may be altered to a value permitted by imageInfo.colorSpace(). 384cb93a386Sopenharmony_ci If imageInfo.colorType() is kUnknown_SkColorType, imageInfo.alphaType() is 385cb93a386Sopenharmony_ci set to kUnknown_SkAlphaType. 386cb93a386Sopenharmony_ci If imageInfo.colorType() is kAlpha_8_SkColorType and imageInfo.alphaType() is 387cb93a386Sopenharmony_ci kUnpremul_SkAlphaType, imageInfo.alphaType() is replaced by kPremul_SkAlphaType. 388cb93a386Sopenharmony_ci If imageInfo.colorType() is kRGB_565_SkColorType or kGray_8_SkColorType, 389cb93a386Sopenharmony_ci imageInfo.alphaType() is set to kOpaque_SkAlphaType. 390cb93a386Sopenharmony_ci If imageInfo.colorType() is kARGB_4444_SkColorType, kRGBA_8888_SkColorType, 391cb93a386Sopenharmony_ci kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: imageInfo.alphaType() remains 392cb93a386Sopenharmony_ci unchanged. 393cb93a386Sopenharmony_ci 394cb93a386Sopenharmony_ci rowBytes must equal or exceed imageInfo.minRowBytes(). If imageInfo.colorSpace() is 395cb93a386Sopenharmony_ci kUnknown_SkColorType, rowBytes is ignored and treated as zero; for all other 396cb93a386Sopenharmony_ci SkColorSpace values, rowBytes of zero is treated as imageInfo.minRowBytes(). 397cb93a386Sopenharmony_ci 398cb93a386Sopenharmony_ci Calls reset() and returns false if: 399cb93a386Sopenharmony_ci - rowBytes exceeds 31 bits 400cb93a386Sopenharmony_ci - imageInfo.width() is negative 401cb93a386Sopenharmony_ci - imageInfo.height() is negative 402cb93a386Sopenharmony_ci - rowBytes is positive and less than imageInfo.width() times imageInfo.bytesPerPixel() 403cb93a386Sopenharmony_ci 404cb93a386Sopenharmony_ci @param imageInfo contains width, height, SkAlphaType, SkColorType, SkColorSpace 405cb93a386Sopenharmony_ci @param rowBytes imageInfo.minRowBytes() or larger; or zero 406cb93a386Sopenharmony_ci @return true if SkImageInfo set successfully 407cb93a386Sopenharmony_ci 408cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_setInfo 409cb93a386Sopenharmony_ci */ 410cb93a386Sopenharmony_ci bool setInfo(const SkImageInfo& imageInfo, size_t rowBytes = 0); 411cb93a386Sopenharmony_ci 412cb93a386Sopenharmony_ci /** \enum SkBitmap::AllocFlags 413cb93a386Sopenharmony_ci AllocFlags is obsolete. We always zero pixel memory when allocated. 414cb93a386Sopenharmony_ci */ 415cb93a386Sopenharmony_ci enum AllocFlags { 416cb93a386Sopenharmony_ci kZeroPixels_AllocFlag = 1 << 0, //!< zero pixel memory. No effect. This is the default. 417cb93a386Sopenharmony_ci }; 418cb93a386Sopenharmony_ci 419cb93a386Sopenharmony_ci /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel 420cb93a386Sopenharmony_ci memory. Memory is zeroed. 421cb93a386Sopenharmony_ci 422cb93a386Sopenharmony_ci Returns false and calls reset() if SkImageInfo could not be set, or memory could 423cb93a386Sopenharmony_ci not be allocated, or memory could not optionally be zeroed. 424cb93a386Sopenharmony_ci 425cb93a386Sopenharmony_ci On most platforms, allocating pixel memory may succeed even though there is 426cb93a386Sopenharmony_ci not sufficient memory to hold pixels; allocation does not take place 427cb93a386Sopenharmony_ci until the pixels are written to. The actual behavior depends on the platform 428cb93a386Sopenharmony_ci implementation of calloc(). 429cb93a386Sopenharmony_ci 430cb93a386Sopenharmony_ci @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace 431cb93a386Sopenharmony_ci @param flags kZeroPixels_AllocFlag, or zero 432cb93a386Sopenharmony_ci @return true if pixels allocation is successful 433cb93a386Sopenharmony_ci */ 434cb93a386Sopenharmony_ci bool SK_WARN_UNUSED_RESULT tryAllocPixelsFlags(const SkImageInfo& info, uint32_t flags); 435cb93a386Sopenharmony_ci 436cb93a386Sopenharmony_ci /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel 437cb93a386Sopenharmony_ci memory. Memory is zeroed. 438cb93a386Sopenharmony_ci 439cb93a386Sopenharmony_ci Aborts execution if SkImageInfo could not be set, or memory could 440cb93a386Sopenharmony_ci not be allocated, or memory could not optionally 441cb93a386Sopenharmony_ci be zeroed. Abort steps may be provided by the user at compile time by defining 442cb93a386Sopenharmony_ci SK_ABORT. 443cb93a386Sopenharmony_ci 444cb93a386Sopenharmony_ci On most platforms, allocating pixel memory may succeed even though there is 445cb93a386Sopenharmony_ci not sufficient memory to hold pixels; allocation does not take place 446cb93a386Sopenharmony_ci until the pixels are written to. The actual behavior depends on the platform 447cb93a386Sopenharmony_ci implementation of calloc(). 448cb93a386Sopenharmony_ci 449cb93a386Sopenharmony_ci @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace 450cb93a386Sopenharmony_ci @param flags kZeroPixels_AllocFlag, or zero 451cb93a386Sopenharmony_ci 452cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_allocPixelsFlags 453cb93a386Sopenharmony_ci */ 454cb93a386Sopenharmony_ci void allocPixelsFlags(const SkImageInfo& info, uint32_t flags); 455cb93a386Sopenharmony_ci 456cb93a386Sopenharmony_ci /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel 457cb93a386Sopenharmony_ci memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(), 458cb93a386Sopenharmony_ci or equal zero. Pass in zero for rowBytes to compute the minimum valid value. 459cb93a386Sopenharmony_ci 460cb93a386Sopenharmony_ci Returns false and calls reset() if SkImageInfo could not be set, or memory could 461cb93a386Sopenharmony_ci not be allocated. 462cb93a386Sopenharmony_ci 463cb93a386Sopenharmony_ci On most platforms, allocating pixel memory may succeed even though there is 464cb93a386Sopenharmony_ci not sufficient memory to hold pixels; allocation does not take place 465cb93a386Sopenharmony_ci until the pixels are written to. The actual behavior depends on the platform 466cb93a386Sopenharmony_ci implementation of malloc(). 467cb93a386Sopenharmony_ci 468cb93a386Sopenharmony_ci @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace 469cb93a386Sopenharmony_ci @param rowBytes size of pixel row or larger; may be zero 470cb93a386Sopenharmony_ci @return true if pixel storage is allocated 471cb93a386Sopenharmony_ci */ 472cb93a386Sopenharmony_ci bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info, size_t rowBytes); 473cb93a386Sopenharmony_ci 474cb93a386Sopenharmony_ci /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel 475cb93a386Sopenharmony_ci memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(), 476cb93a386Sopenharmony_ci or equal zero. Pass in zero for rowBytes to compute the minimum valid value. 477cb93a386Sopenharmony_ci 478cb93a386Sopenharmony_ci Aborts execution if SkImageInfo could not be set, or memory could 479cb93a386Sopenharmony_ci not be allocated. Abort steps may be provided by 480cb93a386Sopenharmony_ci the user at compile time by defining SK_ABORT. 481cb93a386Sopenharmony_ci 482cb93a386Sopenharmony_ci On most platforms, allocating pixel memory may succeed even though there is 483cb93a386Sopenharmony_ci not sufficient memory to hold pixels; allocation does not take place 484cb93a386Sopenharmony_ci until the pixels are written to. The actual behavior depends on the platform 485cb93a386Sopenharmony_ci implementation of malloc(). 486cb93a386Sopenharmony_ci 487cb93a386Sopenharmony_ci @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace 488cb93a386Sopenharmony_ci @param rowBytes size of pixel row or larger; may be zero 489cb93a386Sopenharmony_ci 490cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_allocPixels 491cb93a386Sopenharmony_ci */ 492cb93a386Sopenharmony_ci void allocPixels(const SkImageInfo& info, size_t rowBytes); 493cb93a386Sopenharmony_ci 494cb93a386Sopenharmony_ci /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel 495cb93a386Sopenharmony_ci memory. 496cb93a386Sopenharmony_ci 497cb93a386Sopenharmony_ci Returns false and calls reset() if SkImageInfo could not be set, or memory could 498cb93a386Sopenharmony_ci not be allocated. 499cb93a386Sopenharmony_ci 500cb93a386Sopenharmony_ci On most platforms, allocating pixel memory may succeed even though there is 501cb93a386Sopenharmony_ci not sufficient memory to hold pixels; allocation does not take place 502cb93a386Sopenharmony_ci until the pixels are written to. The actual behavior depends on the platform 503cb93a386Sopenharmony_ci implementation of malloc(). 504cb93a386Sopenharmony_ci 505cb93a386Sopenharmony_ci @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace 506cb93a386Sopenharmony_ci @return true if pixel storage is allocated 507cb93a386Sopenharmony_ci */ 508cb93a386Sopenharmony_ci bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info) { 509cb93a386Sopenharmony_ci return this->tryAllocPixels(info, info.minRowBytes()); 510cb93a386Sopenharmony_ci } 511cb93a386Sopenharmony_ci 512cb93a386Sopenharmony_ci /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel 513cb93a386Sopenharmony_ci memory. 514cb93a386Sopenharmony_ci 515cb93a386Sopenharmony_ci Aborts execution if SkImageInfo could not be set, or memory could 516cb93a386Sopenharmony_ci not be allocated. Abort steps may be provided by 517cb93a386Sopenharmony_ci the user at compile time by defining SK_ABORT. 518cb93a386Sopenharmony_ci 519cb93a386Sopenharmony_ci On most platforms, allocating pixel memory may succeed even though there is 520cb93a386Sopenharmony_ci not sufficient memory to hold pixels; allocation does not take place 521cb93a386Sopenharmony_ci until the pixels are written to. The actual behavior depends on the platform 522cb93a386Sopenharmony_ci implementation of malloc(). 523cb93a386Sopenharmony_ci 524cb93a386Sopenharmony_ci @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace 525cb93a386Sopenharmony_ci 526cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_allocPixels_2 527cb93a386Sopenharmony_ci */ 528cb93a386Sopenharmony_ci void allocPixels(const SkImageInfo& info); 529cb93a386Sopenharmony_ci 530cb93a386Sopenharmony_ci /** Sets SkImageInfo to width, height, and native color type; and allocates 531cb93a386Sopenharmony_ci pixel memory. If isOpaque is true, sets SkImageInfo to kOpaque_SkAlphaType; 532cb93a386Sopenharmony_ci otherwise, sets to kPremul_SkAlphaType. 533cb93a386Sopenharmony_ci 534cb93a386Sopenharmony_ci Calls reset() and returns false if width exceeds 29 bits or is negative, 535cb93a386Sopenharmony_ci or height is negative. 536cb93a386Sopenharmony_ci 537cb93a386Sopenharmony_ci Returns false if allocation fails. 538cb93a386Sopenharmony_ci 539cb93a386Sopenharmony_ci Use to create SkBitmap that matches SkPMColor, the native pixel arrangement on 540cb93a386Sopenharmony_ci the platform. SkBitmap drawn to output device skips converting its pixel format. 541cb93a386Sopenharmony_ci 542cb93a386Sopenharmony_ci @param width pixel column count; must be zero or greater 543cb93a386Sopenharmony_ci @param height pixel row count; must be zero or greater 544cb93a386Sopenharmony_ci @param isOpaque true if pixels do not have transparency 545cb93a386Sopenharmony_ci @return true if pixel storage is allocated 546cb93a386Sopenharmony_ci */ 547cb93a386Sopenharmony_ci bool SK_WARN_UNUSED_RESULT tryAllocN32Pixels(int width, int height, bool isOpaque = false); 548cb93a386Sopenharmony_ci 549cb93a386Sopenharmony_ci /** Sets SkImageInfo to width, height, and the native color type; and allocates 550cb93a386Sopenharmony_ci pixel memory. If isOpaque is true, sets SkImageInfo to kOpaque_SkAlphaType; 551cb93a386Sopenharmony_ci otherwise, sets to kPremul_SkAlphaType. 552cb93a386Sopenharmony_ci 553cb93a386Sopenharmony_ci Aborts if width exceeds 29 bits or is negative, or height is negative, or 554cb93a386Sopenharmony_ci allocation fails. Abort steps may be provided by the user at compile time by 555cb93a386Sopenharmony_ci defining SK_ABORT. 556cb93a386Sopenharmony_ci 557cb93a386Sopenharmony_ci Use to create SkBitmap that matches SkPMColor, the native pixel arrangement on 558cb93a386Sopenharmony_ci the platform. SkBitmap drawn to output device skips converting its pixel format. 559cb93a386Sopenharmony_ci 560cb93a386Sopenharmony_ci @param width pixel column count; must be zero or greater 561cb93a386Sopenharmony_ci @param height pixel row count; must be zero or greater 562cb93a386Sopenharmony_ci @param isOpaque true if pixels do not have transparency 563cb93a386Sopenharmony_ci 564cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_allocN32Pixels 565cb93a386Sopenharmony_ci */ 566cb93a386Sopenharmony_ci void allocN32Pixels(int width, int height, bool isOpaque = false); 567cb93a386Sopenharmony_ci 568cb93a386Sopenharmony_ci /** Sets SkImageInfo to info following the rules in setInfo(), and creates SkPixelRef 569cb93a386Sopenharmony_ci containing pixels and rowBytes. releaseProc, if not nullptr, is called 570cb93a386Sopenharmony_ci immediately on failure or when pixels are no longer referenced. context may be 571cb93a386Sopenharmony_ci nullptr. 572cb93a386Sopenharmony_ci 573cb93a386Sopenharmony_ci If SkImageInfo could not be set, or rowBytes is less than info.minRowBytes(): 574cb93a386Sopenharmony_ci calls releaseProc if present, calls reset(), and returns false. 575cb93a386Sopenharmony_ci 576cb93a386Sopenharmony_ci Otherwise, if pixels equals nullptr: sets SkImageInfo, calls releaseProc if 577cb93a386Sopenharmony_ci present, returns true. 578cb93a386Sopenharmony_ci 579cb93a386Sopenharmony_ci If SkImageInfo is set, pixels is not nullptr, and releaseProc is not nullptr: 580cb93a386Sopenharmony_ci when pixels are no longer referenced, calls releaseProc with pixels and context 581cb93a386Sopenharmony_ci as parameters. 582cb93a386Sopenharmony_ci 583cb93a386Sopenharmony_ci @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace 584cb93a386Sopenharmony_ci @param pixels address or pixel storage; may be nullptr 585cb93a386Sopenharmony_ci @param rowBytes size of pixel row or larger 586cb93a386Sopenharmony_ci @param releaseProc function called when pixels can be deleted; may be nullptr 587cb93a386Sopenharmony_ci @param context caller state passed to releaseProc; may be nullptr 588cb93a386Sopenharmony_ci @return true if SkImageInfo is set to info 589cb93a386Sopenharmony_ci */ 590cb93a386Sopenharmony_ci bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, 591cb93a386Sopenharmony_ci void (*releaseProc)(void* addr, void* context), void* context); 592cb93a386Sopenharmony_ci 593cb93a386Sopenharmony_ci /** Sets SkImageInfo to info following the rules in setInfo(), and creates SkPixelRef 594cb93a386Sopenharmony_ci containing pixels and rowBytes. 595cb93a386Sopenharmony_ci 596cb93a386Sopenharmony_ci If SkImageInfo could not be set, or rowBytes is less than info.minRowBytes(): 597cb93a386Sopenharmony_ci calls reset(), and returns false. 598cb93a386Sopenharmony_ci 599cb93a386Sopenharmony_ci Otherwise, if pixels equals nullptr: sets SkImageInfo, returns true. 600cb93a386Sopenharmony_ci 601cb93a386Sopenharmony_ci Caller must ensure that pixels are valid for the lifetime of SkBitmap and SkPixelRef. 602cb93a386Sopenharmony_ci 603cb93a386Sopenharmony_ci @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace 604cb93a386Sopenharmony_ci @param pixels address or pixel storage; may be nullptr 605cb93a386Sopenharmony_ci @param rowBytes size of pixel row or larger 606cb93a386Sopenharmony_ci @return true if SkImageInfo is set to info 607cb93a386Sopenharmony_ci */ 608cb93a386Sopenharmony_ci bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) { 609cb93a386Sopenharmony_ci return this->installPixels(info, pixels, rowBytes, nullptr, nullptr); 610cb93a386Sopenharmony_ci } 611cb93a386Sopenharmony_ci 612cb93a386Sopenharmony_ci /** Sets SkImageInfo to pixmap.info() following the rules in setInfo(), and creates 613cb93a386Sopenharmony_ci SkPixelRef containing pixmap.addr() and pixmap.rowBytes(). 614cb93a386Sopenharmony_ci 615cb93a386Sopenharmony_ci If SkImageInfo could not be set, or pixmap.rowBytes() is less than 616cb93a386Sopenharmony_ci SkImageInfo::minRowBytes(): calls reset(), and returns false. 617cb93a386Sopenharmony_ci 618cb93a386Sopenharmony_ci Otherwise, if pixmap.addr() equals nullptr: sets SkImageInfo, returns true. 619cb93a386Sopenharmony_ci 620cb93a386Sopenharmony_ci Caller must ensure that pixmap is valid for the lifetime of SkBitmap and SkPixelRef. 621cb93a386Sopenharmony_ci 622cb93a386Sopenharmony_ci @param pixmap SkImageInfo, pixel address, and rowBytes() 623cb93a386Sopenharmony_ci @return true if SkImageInfo was set to pixmap.info() 624cb93a386Sopenharmony_ci 625cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_installPixels_3 626cb93a386Sopenharmony_ci */ 627cb93a386Sopenharmony_ci bool installPixels(const SkPixmap& pixmap); 628cb93a386Sopenharmony_ci 629cb93a386Sopenharmony_ci /** Deprecated. 630cb93a386Sopenharmony_ci */ 631cb93a386Sopenharmony_ci bool installMaskPixels(const SkMask& mask); 632cb93a386Sopenharmony_ci 633cb93a386Sopenharmony_ci /** Replaces SkPixelRef with pixels, preserving SkImageInfo and rowBytes(). 634cb93a386Sopenharmony_ci Sets SkPixelRef origin to (0, 0). 635cb93a386Sopenharmony_ci 636cb93a386Sopenharmony_ci If pixels is nullptr, or if info().colorType() equals kUnknown_SkColorType; 637cb93a386Sopenharmony_ci release reference to SkPixelRef, and set SkPixelRef to nullptr. 638cb93a386Sopenharmony_ci 639cb93a386Sopenharmony_ci Caller is responsible for handling ownership pixel memory for the lifetime 640cb93a386Sopenharmony_ci of SkBitmap and SkPixelRef. 641cb93a386Sopenharmony_ci 642cb93a386Sopenharmony_ci @param pixels address of pixel storage, managed by caller 643cb93a386Sopenharmony_ci 644cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_setPixels 645cb93a386Sopenharmony_ci */ 646cb93a386Sopenharmony_ci void setPixels(void* pixels); 647cb93a386Sopenharmony_ci 648cb93a386Sopenharmony_ci /** Allocates pixel memory with HeapAllocator, and replaces existing SkPixelRef. 649cb93a386Sopenharmony_ci The allocation size is determined by SkImageInfo width, height, and SkColorType. 650cb93a386Sopenharmony_ci 651cb93a386Sopenharmony_ci Returns false if info().colorType() is kUnknown_SkColorType, or allocation fails. 652cb93a386Sopenharmony_ci 653cb93a386Sopenharmony_ci @return true if the allocation succeeds 654cb93a386Sopenharmony_ci */ 655cb93a386Sopenharmony_ci bool SK_WARN_UNUSED_RESULT tryAllocPixels() { 656cb93a386Sopenharmony_ci return this->tryAllocPixels((Allocator*)nullptr); 657cb93a386Sopenharmony_ci } 658cb93a386Sopenharmony_ci 659cb93a386Sopenharmony_ci /** Allocates pixel memory with HeapAllocator, and replaces existing SkPixelRef. 660cb93a386Sopenharmony_ci The allocation size is determined by SkImageInfo width, height, and SkColorType. 661cb93a386Sopenharmony_ci 662cb93a386Sopenharmony_ci Aborts if info().colorType() is kUnknown_SkColorType, or allocation fails. 663cb93a386Sopenharmony_ci Abort steps may be provided by the user at compile 664cb93a386Sopenharmony_ci time by defining SK_ABORT. 665cb93a386Sopenharmony_ci 666cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_allocPixels_3 667cb93a386Sopenharmony_ci */ 668cb93a386Sopenharmony_ci void allocPixels(); 669cb93a386Sopenharmony_ci 670cb93a386Sopenharmony_ci /** Allocates pixel memory with allocator, and replaces existing SkPixelRef. 671cb93a386Sopenharmony_ci The allocation size is determined by SkImageInfo width, height, and SkColorType. 672cb93a386Sopenharmony_ci If allocator is nullptr, use HeapAllocator instead. 673cb93a386Sopenharmony_ci 674cb93a386Sopenharmony_ci Returns false if Allocator::allocPixelRef return false. 675cb93a386Sopenharmony_ci 676cb93a386Sopenharmony_ci @param allocator instance of SkBitmap::Allocator instantiation 677cb93a386Sopenharmony_ci @return true if custom allocator reports success 678cb93a386Sopenharmony_ci */ 679cb93a386Sopenharmony_ci bool SK_WARN_UNUSED_RESULT tryAllocPixels(Allocator* allocator); 680cb93a386Sopenharmony_ci 681cb93a386Sopenharmony_ci /** Allocates pixel memory with allocator, and replaces existing SkPixelRef. 682cb93a386Sopenharmony_ci The allocation size is determined by SkImageInfo width, height, and SkColorType. 683cb93a386Sopenharmony_ci If allocator is nullptr, use HeapAllocator instead. 684cb93a386Sopenharmony_ci 685cb93a386Sopenharmony_ci Aborts if Allocator::allocPixelRef return false. Abort steps may be provided by 686cb93a386Sopenharmony_ci the user at compile time by defining SK_ABORT. 687cb93a386Sopenharmony_ci 688cb93a386Sopenharmony_ci @param allocator instance of SkBitmap::Allocator instantiation 689cb93a386Sopenharmony_ci 690cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_allocPixels_4 691cb93a386Sopenharmony_ci */ 692cb93a386Sopenharmony_ci void allocPixels(Allocator* allocator); 693cb93a386Sopenharmony_ci 694cb93a386Sopenharmony_ci /** Returns SkPixelRef, which contains: pixel base address; its dimensions; and 695cb93a386Sopenharmony_ci rowBytes(), the interval from one row to the next. Does not change SkPixelRef 696cb93a386Sopenharmony_ci reference count. SkPixelRef may be shared by multiple bitmaps. 697cb93a386Sopenharmony_ci If SkPixelRef has not been set, returns nullptr. 698cb93a386Sopenharmony_ci 699cb93a386Sopenharmony_ci @return SkPixelRef, or nullptr 700cb93a386Sopenharmony_ci */ 701cb93a386Sopenharmony_ci SkPixelRef* pixelRef() const { return fPixelRef.get(); } 702cb93a386Sopenharmony_ci 703cb93a386Sopenharmony_ci /** Returns origin of pixels within SkPixelRef. SkBitmap bounds is always contained 704cb93a386Sopenharmony_ci by SkPixelRef bounds, which may be the same size or larger. Multiple SkBitmap 705cb93a386Sopenharmony_ci can share the same SkPixelRef, where each SkBitmap has different bounds. 706cb93a386Sopenharmony_ci 707cb93a386Sopenharmony_ci The returned origin added to SkBitmap dimensions equals or is smaller than the 708cb93a386Sopenharmony_ci SkPixelRef dimensions. 709cb93a386Sopenharmony_ci 710cb93a386Sopenharmony_ci Returns (0, 0) if SkPixelRef is nullptr. 711cb93a386Sopenharmony_ci 712cb93a386Sopenharmony_ci @return pixel origin within SkPixelRef 713cb93a386Sopenharmony_ci 714cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_pixelRefOrigin 715cb93a386Sopenharmony_ci */ 716cb93a386Sopenharmony_ci SkIPoint pixelRefOrigin() const; 717cb93a386Sopenharmony_ci 718cb93a386Sopenharmony_ci /** Replaces pixelRef and origin in SkBitmap. dx and dy specify the offset 719cb93a386Sopenharmony_ci within the SkPixelRef pixels for the top-left corner of the bitmap. 720cb93a386Sopenharmony_ci 721cb93a386Sopenharmony_ci Asserts in debug builds if dx or dy are out of range. Pins dx and dy 722cb93a386Sopenharmony_ci to legal range in release builds. 723cb93a386Sopenharmony_ci 724cb93a386Sopenharmony_ci The caller is responsible for ensuring that the pixels match the 725cb93a386Sopenharmony_ci SkColorType and SkAlphaType in SkImageInfo. 726cb93a386Sopenharmony_ci 727cb93a386Sopenharmony_ci @param pixelRef SkPixelRef describing pixel address and rowBytes() 728cb93a386Sopenharmony_ci @param dx column offset in SkPixelRef for bitmap origin 729cb93a386Sopenharmony_ci @param dy row offset in SkPixelRef for bitmap origin 730cb93a386Sopenharmony_ci 731cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_setPixelRef 732cb93a386Sopenharmony_ci */ 733cb93a386Sopenharmony_ci void setPixelRef(sk_sp<SkPixelRef> pixelRef, int dx, int dy); 734cb93a386Sopenharmony_ci 735cb93a386Sopenharmony_ci /** Returns true if SkBitmap is can be drawn. 736cb93a386Sopenharmony_ci 737cb93a386Sopenharmony_ci @return true if getPixels() is not nullptr 738cb93a386Sopenharmony_ci */ 739cb93a386Sopenharmony_ci bool readyToDraw() const { 740cb93a386Sopenharmony_ci return this->getPixels() != nullptr; 741cb93a386Sopenharmony_ci } 742cb93a386Sopenharmony_ci 743cb93a386Sopenharmony_ci /** Returns a unique value corresponding to the pixels in SkPixelRef. 744cb93a386Sopenharmony_ci Returns a different value after notifyPixelsChanged() has been called. 745cb93a386Sopenharmony_ci Returns zero if SkPixelRef is nullptr. 746cb93a386Sopenharmony_ci 747cb93a386Sopenharmony_ci Determines if pixels have changed since last examined. 748cb93a386Sopenharmony_ci 749cb93a386Sopenharmony_ci @return unique value for pixels in SkPixelRef 750cb93a386Sopenharmony_ci 751cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_getGenerationID 752cb93a386Sopenharmony_ci */ 753cb93a386Sopenharmony_ci uint32_t getGenerationID() const; 754cb93a386Sopenharmony_ci 755cb93a386Sopenharmony_ci /** Marks that pixels in SkPixelRef have changed. Subsequent calls to 756cb93a386Sopenharmony_ci getGenerationID() return a different value. 757cb93a386Sopenharmony_ci 758cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_notifyPixelsChanged 759cb93a386Sopenharmony_ci */ 760cb93a386Sopenharmony_ci void notifyPixelsChanged() const; 761cb93a386Sopenharmony_ci 762cb93a386Sopenharmony_ci /** Replaces pixel values with c, interpreted as being in the sRGB SkColorSpace. 763cb93a386Sopenharmony_ci All pixels contained by bounds() are affected. If the colorType() is 764cb93a386Sopenharmony_ci kGray_8_SkColorType or kRGB_565_SkColorType, then alpha is ignored; RGB is 765cb93a386Sopenharmony_ci treated as opaque. If colorType() is kAlpha_8_SkColorType, then RGB is ignored. 766cb93a386Sopenharmony_ci 767cb93a386Sopenharmony_ci @param c unpremultiplied color 768cb93a386Sopenharmony_ci 769cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_eraseColor 770cb93a386Sopenharmony_ci */ 771cb93a386Sopenharmony_ci void eraseColor(SkColor c) const; 772cb93a386Sopenharmony_ci 773cb93a386Sopenharmony_ci /** Replaces pixel values with unpremultiplied color built from a, r, g, and b, 774cb93a386Sopenharmony_ci interpreted as being in the sRGB SkColorSpace. All pixels contained by 775cb93a386Sopenharmony_ci bounds() are affected. If the colorType() is kGray_8_SkColorType or 776cb93a386Sopenharmony_ci kRGB_565_SkColorType, then a is ignored; r, g, and b are treated as opaque. 777cb93a386Sopenharmony_ci If colorType() is kAlpha_8_SkColorType, then r, g, and b are ignored. 778cb93a386Sopenharmony_ci 779cb93a386Sopenharmony_ci @param a amount of alpha, from fully transparent (0) to fully opaque (255) 780cb93a386Sopenharmony_ci @param r amount of red, from no red (0) to full red (255) 781cb93a386Sopenharmony_ci @param g amount of green, from no green (0) to full green (255) 782cb93a386Sopenharmony_ci @param b amount of blue, from no blue (0) to full blue (255) 783cb93a386Sopenharmony_ci */ 784cb93a386Sopenharmony_ci void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const { 785cb93a386Sopenharmony_ci this->eraseColor(SkColorSetARGB(a, r, g, b)); 786cb93a386Sopenharmony_ci } 787cb93a386Sopenharmony_ci 788cb93a386Sopenharmony_ci /** Replaces pixel values inside area with c. interpreted as being in the sRGB 789cb93a386Sopenharmony_ci SkColorSpace. If area does not intersect bounds(), call has no effect. 790cb93a386Sopenharmony_ci 791cb93a386Sopenharmony_ci If the colorType() is kGray_8_SkColorType or kRGB_565_SkColorType, then alpha 792cb93a386Sopenharmony_ci is ignored; RGB is treated as opaque. If colorType() is kAlpha_8_SkColorType, 793cb93a386Sopenharmony_ci then RGB is ignored. 794cb93a386Sopenharmony_ci 795cb93a386Sopenharmony_ci @param c unpremultiplied color 796cb93a386Sopenharmony_ci @param area rectangle to fill 797cb93a386Sopenharmony_ci 798cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_erase 799cb93a386Sopenharmony_ci */ 800cb93a386Sopenharmony_ci void erase(SkColor c, const SkIRect& area) const; 801cb93a386Sopenharmony_ci 802cb93a386Sopenharmony_ci /** Deprecated. 803cb93a386Sopenharmony_ci */ 804cb93a386Sopenharmony_ci void eraseArea(const SkIRect& area, SkColor c) const { 805cb93a386Sopenharmony_ci this->erase(c, area); 806cb93a386Sopenharmony_ci } 807cb93a386Sopenharmony_ci 808cb93a386Sopenharmony_ci /** Returns pixel at (x, y) as unpremultiplied color. 809cb93a386Sopenharmony_ci Returns black with alpha if SkColorType is kAlpha_8_SkColorType. 810cb93a386Sopenharmony_ci 811cb93a386Sopenharmony_ci Input is not validated: out of bounds values of x or y trigger an assert() if 812cb93a386Sopenharmony_ci built with SK_DEBUG defined; and returns undefined values or may crash if 813cb93a386Sopenharmony_ci SK_RELEASE is defined. Fails if SkColorType is kUnknown_SkColorType or 814cb93a386Sopenharmony_ci pixel address is nullptr. 815cb93a386Sopenharmony_ci 816cb93a386Sopenharmony_ci SkColorSpace in SkImageInfo is ignored. Some color precision may be lost in the 817cb93a386Sopenharmony_ci conversion to unpremultiplied color; original pixel data may have additional 818cb93a386Sopenharmony_ci precision. 819cb93a386Sopenharmony_ci 820cb93a386Sopenharmony_ci @param x column index, zero or greater, and less than width() 821cb93a386Sopenharmony_ci @param y row index, zero or greater, and less than height() 822cb93a386Sopenharmony_ci @return pixel converted to unpremultiplied color 823cb93a386Sopenharmony_ci */ 824cb93a386Sopenharmony_ci SkColor getColor(int x, int y) const { 825cb93a386Sopenharmony_ci return this->pixmap().getColor(x, y); 826cb93a386Sopenharmony_ci } 827cb93a386Sopenharmony_ci 828cb93a386Sopenharmony_ci /** Look up the pixel at (x,y) and return its alpha component, normalized to [0..1]. 829cb93a386Sopenharmony_ci This is roughly equivalent to SkGetColorA(getColor()), but can be more efficent 830cb93a386Sopenharmony_ci (and more precise if the pixels store more than 8 bits per component). 831cb93a386Sopenharmony_ci 832cb93a386Sopenharmony_ci @param x column index, zero or greater, and less than width() 833cb93a386Sopenharmony_ci @param y row index, zero or greater, and less than height() 834cb93a386Sopenharmony_ci @return alpha converted to normalized float 835cb93a386Sopenharmony_ci */ 836cb93a386Sopenharmony_ci float getAlphaf(int x, int y) const { 837cb93a386Sopenharmony_ci return this->pixmap().getAlphaf(x, y); 838cb93a386Sopenharmony_ci } 839cb93a386Sopenharmony_ci 840cb93a386Sopenharmony_ci /** Returns pixel address at (x, y). 841cb93a386Sopenharmony_ci 842cb93a386Sopenharmony_ci Input is not validated: out of bounds values of x or y, or kUnknown_SkColorType, 843cb93a386Sopenharmony_ci trigger an assert() if built with SK_DEBUG defined. Returns nullptr if 844cb93a386Sopenharmony_ci SkColorType is kUnknown_SkColorType, or SkPixelRef is nullptr. 845cb93a386Sopenharmony_ci 846cb93a386Sopenharmony_ci Performs a lookup of pixel size; for better performance, call 847cb93a386Sopenharmony_ci one of: getAddr8(), getAddr16(), or getAddr32(). 848cb93a386Sopenharmony_ci 849cb93a386Sopenharmony_ci @param x column index, zero or greater, and less than width() 850cb93a386Sopenharmony_ci @param y row index, zero or greater, and less than height() 851cb93a386Sopenharmony_ci @return generic pointer to pixel 852cb93a386Sopenharmony_ci 853cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_getAddr 854cb93a386Sopenharmony_ci */ 855cb93a386Sopenharmony_ci void* getAddr(int x, int y) const; 856cb93a386Sopenharmony_ci 857cb93a386Sopenharmony_ci /** Returns address at (x, y). 858cb93a386Sopenharmony_ci 859cb93a386Sopenharmony_ci Input is not validated. Triggers an assert() if built with SK_DEBUG defined and: 860cb93a386Sopenharmony_ci - SkPixelRef is nullptr 861cb93a386Sopenharmony_ci - bytesPerPixel() is not four 862cb93a386Sopenharmony_ci - x is negative, or not less than width() 863cb93a386Sopenharmony_ci - y is negative, or not less than height() 864cb93a386Sopenharmony_ci 865cb93a386Sopenharmony_ci @param x column index, zero or greater, and less than width() 866cb93a386Sopenharmony_ci @param y row index, zero or greater, and less than height() 867cb93a386Sopenharmony_ci @return unsigned 32-bit pointer to pixel at (x, y) 868cb93a386Sopenharmony_ci */ 869cb93a386Sopenharmony_ci inline uint32_t* getAddr32(int x, int y) const; 870cb93a386Sopenharmony_ci 871cb93a386Sopenharmony_ci /** Returns address at (x, y). 872cb93a386Sopenharmony_ci 873cb93a386Sopenharmony_ci Input is not validated. Triggers an assert() if built with SK_DEBUG defined and: 874cb93a386Sopenharmony_ci - SkPixelRef is nullptr 875cb93a386Sopenharmony_ci - bytesPerPixel() is not two 876cb93a386Sopenharmony_ci - x is negative, or not less than width() 877cb93a386Sopenharmony_ci - y is negative, or not less than height() 878cb93a386Sopenharmony_ci 879cb93a386Sopenharmony_ci @param x column index, zero or greater, and less than width() 880cb93a386Sopenharmony_ci @param y row index, zero or greater, and less than height() 881cb93a386Sopenharmony_ci @return unsigned 16-bit pointer to pixel at (x, y) 882cb93a386Sopenharmony_ci */ 883cb93a386Sopenharmony_ci inline uint16_t* getAddr16(int x, int y) const; 884cb93a386Sopenharmony_ci 885cb93a386Sopenharmony_ci /** Returns address at (x, y). 886cb93a386Sopenharmony_ci 887cb93a386Sopenharmony_ci Input is not validated. Triggers an assert() if built with SK_DEBUG defined and: 888cb93a386Sopenharmony_ci - SkPixelRef is nullptr 889cb93a386Sopenharmony_ci - bytesPerPixel() is not one 890cb93a386Sopenharmony_ci - x is negative, or not less than width() 891cb93a386Sopenharmony_ci - y is negative, or not less than height() 892cb93a386Sopenharmony_ci 893cb93a386Sopenharmony_ci @param x column index, zero or greater, and less than width() 894cb93a386Sopenharmony_ci @param y row index, zero or greater, and less than height() 895cb93a386Sopenharmony_ci @return unsigned 8-bit pointer to pixel at (x, y) 896cb93a386Sopenharmony_ci */ 897cb93a386Sopenharmony_ci inline uint8_t* getAddr8(int x, int y) const; 898cb93a386Sopenharmony_ci 899cb93a386Sopenharmony_ci /** Shares SkPixelRef with dst. Pixels are not copied; SkBitmap and dst point 900cb93a386Sopenharmony_ci to the same pixels; dst bounds() are set to the intersection of subset 901cb93a386Sopenharmony_ci and the original bounds(). 902cb93a386Sopenharmony_ci 903cb93a386Sopenharmony_ci subset may be larger than bounds(). Any area outside of bounds() is ignored. 904cb93a386Sopenharmony_ci 905cb93a386Sopenharmony_ci Any contents of dst are discarded. 906cb93a386Sopenharmony_ci 907cb93a386Sopenharmony_ci Return false if: 908cb93a386Sopenharmony_ci - dst is nullptr 909cb93a386Sopenharmony_ci - SkPixelRef is nullptr 910cb93a386Sopenharmony_ci - subset does not intersect bounds() 911cb93a386Sopenharmony_ci 912cb93a386Sopenharmony_ci @param dst SkBitmap set to subset 913cb93a386Sopenharmony_ci @param subset rectangle of pixels to reference 914cb93a386Sopenharmony_ci @return true if dst is replaced by subset 915cb93a386Sopenharmony_ci 916cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_extractSubset 917cb93a386Sopenharmony_ci */ 918cb93a386Sopenharmony_ci bool extractSubset(SkBitmap* dst, const SkIRect& subset) const; 919cb93a386Sopenharmony_ci 920cb93a386Sopenharmony_ci /** Copies a SkRect of pixels from SkBitmap to dstPixels. Copy starts at (srcX, srcY), 921cb93a386Sopenharmony_ci and does not exceed SkBitmap (width(), height()). 922cb93a386Sopenharmony_ci 923cb93a386Sopenharmony_ci dstInfo specifies width, height, SkColorType, SkAlphaType, and SkColorSpace of 924cb93a386Sopenharmony_ci destination. dstRowBytes specifics the gap from one destination row to the next. 925cb93a386Sopenharmony_ci Returns true if pixels are copied. Returns false if: 926cb93a386Sopenharmony_ci - dstInfo has no address 927cb93a386Sopenharmony_ci - dstRowBytes is less than dstInfo.minRowBytes() 928cb93a386Sopenharmony_ci - SkPixelRef is nullptr 929cb93a386Sopenharmony_ci 930cb93a386Sopenharmony_ci Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is 931cb93a386Sopenharmony_ci kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType() must match. 932cb93a386Sopenharmony_ci If SkBitmap colorType() is kGray_8_SkColorType, dstInfo.colorSpace() must match. 933cb93a386Sopenharmony_ci If SkBitmap alphaType() is kOpaque_SkAlphaType, dstInfo.alphaType() must 934cb93a386Sopenharmony_ci match. If SkBitmap colorSpace() is nullptr, dstInfo.colorSpace() must match. Returns 935cb93a386Sopenharmony_ci false if pixel conversion is not possible. 936cb93a386Sopenharmony_ci 937cb93a386Sopenharmony_ci srcX and srcY may be negative to copy only top or left of source. Returns 938cb93a386Sopenharmony_ci false if width() or height() is zero or negative. 939cb93a386Sopenharmony_ci Returns false if abs(srcX) >= Bitmap width(), or if abs(srcY) >= Bitmap height(). 940cb93a386Sopenharmony_ci 941cb93a386Sopenharmony_ci @param dstInfo destination width, height, SkColorType, SkAlphaType, SkColorSpace 942cb93a386Sopenharmony_ci @param dstPixels destination pixel storage 943cb93a386Sopenharmony_ci @param dstRowBytes destination row length 944cb93a386Sopenharmony_ci @param srcX column index whose absolute value is less than width() 945cb93a386Sopenharmony_ci @param srcY row index whose absolute value is less than height() 946cb93a386Sopenharmony_ci @return true if pixels are copied to dstPixels 947cb93a386Sopenharmony_ci */ 948cb93a386Sopenharmony_ci bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, 949cb93a386Sopenharmony_ci int srcX, int srcY) const; 950cb93a386Sopenharmony_ci 951cb93a386Sopenharmony_ci /** Copies a SkRect of pixels from SkBitmap to dst. Copy starts at (srcX, srcY), and 952cb93a386Sopenharmony_ci does not exceed SkBitmap (width(), height()). 953cb93a386Sopenharmony_ci 954cb93a386Sopenharmony_ci dst specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage, 955cb93a386Sopenharmony_ci and row bytes of destination. dst.rowBytes() specifics the gap from one destination 956cb93a386Sopenharmony_ci row to the next. Returns true if pixels are copied. Returns false if: 957cb93a386Sopenharmony_ci - dst pixel storage equals nullptr 958cb93a386Sopenharmony_ci - dst.rowBytes is less than SkImageInfo::minRowBytes() 959cb93a386Sopenharmony_ci - SkPixelRef is nullptr 960cb93a386Sopenharmony_ci 961cb93a386Sopenharmony_ci Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is 962cb93a386Sopenharmony_ci kGray_8_SkColorType, or kAlpha_8_SkColorType; dst SkColorType must match. 963cb93a386Sopenharmony_ci If SkBitmap colorType() is kGray_8_SkColorType, dst SkColorSpace must match. 964cb93a386Sopenharmony_ci If SkBitmap alphaType() is kOpaque_SkAlphaType, dst SkAlphaType must 965cb93a386Sopenharmony_ci match. If SkBitmap colorSpace() is nullptr, dst SkColorSpace must match. Returns 966cb93a386Sopenharmony_ci false if pixel conversion is not possible. 967cb93a386Sopenharmony_ci 968cb93a386Sopenharmony_ci srcX and srcY may be negative to copy only top or left of source. Returns 969cb93a386Sopenharmony_ci false if width() or height() is zero or negative. 970cb93a386Sopenharmony_ci Returns false if abs(srcX) >= Bitmap width(), or if abs(srcY) >= Bitmap height(). 971cb93a386Sopenharmony_ci 972cb93a386Sopenharmony_ci @param dst destination SkPixmap: SkImageInfo, pixels, row bytes 973cb93a386Sopenharmony_ci @param srcX column index whose absolute value is less than width() 974cb93a386Sopenharmony_ci @param srcY row index whose absolute value is less than height() 975cb93a386Sopenharmony_ci @return true if pixels are copied to dst 976cb93a386Sopenharmony_ci 977cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_readPixels_2 978cb93a386Sopenharmony_ci */ 979cb93a386Sopenharmony_ci bool readPixels(const SkPixmap& dst, int srcX, int srcY) const; 980cb93a386Sopenharmony_ci 981cb93a386Sopenharmony_ci /** Copies a SkRect of pixels from SkBitmap to dst. Copy starts at (0, 0), and 982cb93a386Sopenharmony_ci does not exceed SkBitmap (width(), height()). 983cb93a386Sopenharmony_ci 984cb93a386Sopenharmony_ci dst specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage, 985cb93a386Sopenharmony_ci and row bytes of destination. dst.rowBytes() specifics the gap from one destination 986cb93a386Sopenharmony_ci row to the next. Returns true if pixels are copied. Returns false if: 987cb93a386Sopenharmony_ci - dst pixel storage equals nullptr 988cb93a386Sopenharmony_ci - dst.rowBytes is less than SkImageInfo::minRowBytes() 989cb93a386Sopenharmony_ci - SkPixelRef is nullptr 990cb93a386Sopenharmony_ci 991cb93a386Sopenharmony_ci Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is 992cb93a386Sopenharmony_ci kGray_8_SkColorType, or kAlpha_8_SkColorType; dst SkColorType must match. 993cb93a386Sopenharmony_ci If SkBitmap colorType() is kGray_8_SkColorType, dst SkColorSpace must match. 994cb93a386Sopenharmony_ci If SkBitmap alphaType() is kOpaque_SkAlphaType, dst SkAlphaType must 995cb93a386Sopenharmony_ci match. If SkBitmap colorSpace() is nullptr, dst SkColorSpace must match. Returns 996cb93a386Sopenharmony_ci false if pixel conversion is not possible. 997cb93a386Sopenharmony_ci 998cb93a386Sopenharmony_ci @param dst destination SkPixmap: SkImageInfo, pixels, row bytes 999cb93a386Sopenharmony_ci @return true if pixels are copied to dst 1000cb93a386Sopenharmony_ci */ 1001cb93a386Sopenharmony_ci bool readPixels(const SkPixmap& dst) const { 1002cb93a386Sopenharmony_ci return this->readPixels(dst, 0, 0); 1003cb93a386Sopenharmony_ci } 1004cb93a386Sopenharmony_ci 1005cb93a386Sopenharmony_ci /** Copies a SkRect of pixels from src. Copy starts at (dstX, dstY), and does not exceed 1006cb93a386Sopenharmony_ci (src.width(), src.height()). 1007cb93a386Sopenharmony_ci 1008cb93a386Sopenharmony_ci src specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage, 1009cb93a386Sopenharmony_ci and row bytes of source. src.rowBytes() specifics the gap from one source 1010cb93a386Sopenharmony_ci row to the next. Returns true if pixels are copied. Returns false if: 1011cb93a386Sopenharmony_ci - src pixel storage equals nullptr 1012cb93a386Sopenharmony_ci - src.rowBytes is less than SkImageInfo::minRowBytes() 1013cb93a386Sopenharmony_ci - SkPixelRef is nullptr 1014cb93a386Sopenharmony_ci 1015cb93a386Sopenharmony_ci Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is 1016cb93a386Sopenharmony_ci kGray_8_SkColorType, or kAlpha_8_SkColorType; src SkColorType must match. 1017cb93a386Sopenharmony_ci If SkBitmap colorType() is kGray_8_SkColorType, src SkColorSpace must match. 1018cb93a386Sopenharmony_ci If SkBitmap alphaType() is kOpaque_SkAlphaType, src SkAlphaType must 1019cb93a386Sopenharmony_ci match. If SkBitmap colorSpace() is nullptr, src SkColorSpace must match. Returns 1020cb93a386Sopenharmony_ci false if pixel conversion is not possible. 1021cb93a386Sopenharmony_ci 1022cb93a386Sopenharmony_ci dstX and dstY may be negative to copy only top or left of source. Returns 1023cb93a386Sopenharmony_ci false if width() or height() is zero or negative. 1024cb93a386Sopenharmony_ci Returns false if abs(dstX) >= Bitmap width(), or if abs(dstY) >= Bitmap height(). 1025cb93a386Sopenharmony_ci 1026cb93a386Sopenharmony_ci @param src source SkPixmap: SkImageInfo, pixels, row bytes 1027cb93a386Sopenharmony_ci @param dstX column index whose absolute value is less than width() 1028cb93a386Sopenharmony_ci @param dstY row index whose absolute value is less than height() 1029cb93a386Sopenharmony_ci @return true if src pixels are copied to SkBitmap 1030cb93a386Sopenharmony_ci 1031cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_writePixels 1032cb93a386Sopenharmony_ci */ 1033cb93a386Sopenharmony_ci bool writePixels(const SkPixmap& src, int dstX, int dstY); 1034cb93a386Sopenharmony_ci 1035cb93a386Sopenharmony_ci /** Copies a SkRect of pixels from src. Copy starts at (0, 0), and does not exceed 1036cb93a386Sopenharmony_ci (src.width(), src.height()). 1037cb93a386Sopenharmony_ci 1038cb93a386Sopenharmony_ci src specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage, 1039cb93a386Sopenharmony_ci and row bytes of source. src.rowBytes() specifics the gap from one source 1040cb93a386Sopenharmony_ci row to the next. Returns true if pixels are copied. Returns false if: 1041cb93a386Sopenharmony_ci - src pixel storage equals nullptr 1042cb93a386Sopenharmony_ci - src.rowBytes is less than SkImageInfo::minRowBytes() 1043cb93a386Sopenharmony_ci - SkPixelRef is nullptr 1044cb93a386Sopenharmony_ci 1045cb93a386Sopenharmony_ci Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is 1046cb93a386Sopenharmony_ci kGray_8_SkColorType, or kAlpha_8_SkColorType; src SkColorType must match. 1047cb93a386Sopenharmony_ci If SkBitmap colorType() is kGray_8_SkColorType, src SkColorSpace must match. 1048cb93a386Sopenharmony_ci If SkBitmap alphaType() is kOpaque_SkAlphaType, src SkAlphaType must 1049cb93a386Sopenharmony_ci match. If SkBitmap colorSpace() is nullptr, src SkColorSpace must match. Returns 1050cb93a386Sopenharmony_ci false if pixel conversion is not possible. 1051cb93a386Sopenharmony_ci 1052cb93a386Sopenharmony_ci @param src source SkPixmap: SkImageInfo, pixels, row bytes 1053cb93a386Sopenharmony_ci @return true if src pixels are copied to SkBitmap 1054cb93a386Sopenharmony_ci */ 1055cb93a386Sopenharmony_ci bool writePixels(const SkPixmap& src) { 1056cb93a386Sopenharmony_ci return this->writePixels(src, 0, 0); 1057cb93a386Sopenharmony_ci } 1058cb93a386Sopenharmony_ci 1059cb93a386Sopenharmony_ci /** Sets dst to alpha described by pixels. Returns false if dst cannot be written to 1060cb93a386Sopenharmony_ci or dst pixels cannot be allocated. 1061cb93a386Sopenharmony_ci 1062cb93a386Sopenharmony_ci Uses HeapAllocator to reserve memory for dst SkPixelRef. 1063cb93a386Sopenharmony_ci 1064cb93a386Sopenharmony_ci @param dst holds SkPixelRef to fill with alpha layer 1065cb93a386Sopenharmony_ci @return true if alpha layer was constructed in dst SkPixelRef 1066cb93a386Sopenharmony_ci */ 1067cb93a386Sopenharmony_ci bool extractAlpha(SkBitmap* dst) const { 1068cb93a386Sopenharmony_ci return this->extractAlpha(dst, nullptr, nullptr, nullptr); 1069cb93a386Sopenharmony_ci } 1070cb93a386Sopenharmony_ci 1071cb93a386Sopenharmony_ci /** Sets dst to alpha described by pixels. Returns false if dst cannot be written to 1072cb93a386Sopenharmony_ci or dst pixels cannot be allocated. 1073cb93a386Sopenharmony_ci 1074cb93a386Sopenharmony_ci If paint is not nullptr and contains SkMaskFilter, SkMaskFilter 1075cb93a386Sopenharmony_ci generates mask alpha from SkBitmap. Uses HeapAllocator to reserve memory for dst 1076cb93a386Sopenharmony_ci SkPixelRef. Sets offset to top-left position for dst for alignment with SkBitmap; 1077cb93a386Sopenharmony_ci (0, 0) unless SkMaskFilter generates mask. 1078cb93a386Sopenharmony_ci 1079cb93a386Sopenharmony_ci @param dst holds SkPixelRef to fill with alpha layer 1080cb93a386Sopenharmony_ci @param paint holds optional SkMaskFilter; may be nullptr 1081cb93a386Sopenharmony_ci @param offset top-left position for dst; may be nullptr 1082cb93a386Sopenharmony_ci @return true if alpha layer was constructed in dst SkPixelRef 1083cb93a386Sopenharmony_ci */ 1084cb93a386Sopenharmony_ci bool extractAlpha(SkBitmap* dst, const SkPaint* paint, 1085cb93a386Sopenharmony_ci SkIPoint* offset) const { 1086cb93a386Sopenharmony_ci return this->extractAlpha(dst, paint, nullptr, offset); 1087cb93a386Sopenharmony_ci } 1088cb93a386Sopenharmony_ci 1089cb93a386Sopenharmony_ci /** Sets dst to alpha described by pixels. Returns false if dst cannot be written to 1090cb93a386Sopenharmony_ci or dst pixels cannot be allocated. 1091cb93a386Sopenharmony_ci 1092cb93a386Sopenharmony_ci If paint is not nullptr and contains SkMaskFilter, SkMaskFilter 1093cb93a386Sopenharmony_ci generates mask alpha from SkBitmap. allocator may reference a custom allocation 1094cb93a386Sopenharmony_ci class or be set to nullptr to use HeapAllocator. Sets offset to top-left 1095cb93a386Sopenharmony_ci position for dst for alignment with SkBitmap; (0, 0) unless SkMaskFilter generates 1096cb93a386Sopenharmony_ci mask. 1097cb93a386Sopenharmony_ci 1098cb93a386Sopenharmony_ci @param dst holds SkPixelRef to fill with alpha layer 1099cb93a386Sopenharmony_ci @param paint holds optional SkMaskFilter; may be nullptr 1100cb93a386Sopenharmony_ci @param allocator function to reserve memory for SkPixelRef; may be nullptr 1101cb93a386Sopenharmony_ci @param offset top-left position for dst; may be nullptr 1102cb93a386Sopenharmony_ci @return true if alpha layer was constructed in dst SkPixelRef 1103cb93a386Sopenharmony_ci */ 1104cb93a386Sopenharmony_ci bool extractAlpha(SkBitmap* dst, const SkPaint* paint, Allocator* allocator, 1105cb93a386Sopenharmony_ci SkIPoint* offset) const; 1106cb93a386Sopenharmony_ci 1107cb93a386Sopenharmony_ci /** Copies SkBitmap pixel address, row bytes, and SkImageInfo to pixmap, if address 1108cb93a386Sopenharmony_ci is available, and returns true. If pixel address is not available, return 1109cb93a386Sopenharmony_ci false and leave pixmap unchanged. 1110cb93a386Sopenharmony_ci 1111cb93a386Sopenharmony_ci pixmap contents become invalid on any future change to SkBitmap. 1112cb93a386Sopenharmony_ci 1113cb93a386Sopenharmony_ci @param pixmap storage for pixel state if pixels are readable; otherwise, ignored 1114cb93a386Sopenharmony_ci @return true if SkBitmap has direct access to pixels 1115cb93a386Sopenharmony_ci 1116cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_peekPixels 1117cb93a386Sopenharmony_ci */ 1118cb93a386Sopenharmony_ci bool peekPixels(SkPixmap* pixmap) const; 1119cb93a386Sopenharmony_ci sk_sp<SkShader> makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions&, 1120cb93a386Sopenharmony_ci const SkMatrix* = nullptr) const; 1121cb93a386Sopenharmony_ci 1122cb93a386Sopenharmony_ci sk_sp<SkShader> makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions& sampling, 1123cb93a386Sopenharmony_ci const SkMatrix& localMatrix) const { 1124cb93a386Sopenharmony_ci return this->makeShader(tmx, tmy, sampling, &localMatrix); 1125cb93a386Sopenharmony_ci } 1126cb93a386Sopenharmony_ci 1127cb93a386Sopenharmony_ci sk_sp<SkShader> makeShader(const SkSamplingOptions& sampling, 1128cb93a386Sopenharmony_ci const SkMatrix* localMatrix = nullptr) const { 1129cb93a386Sopenharmony_ci return this->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, sampling, localMatrix); 1130cb93a386Sopenharmony_ci } 1131cb93a386Sopenharmony_ci 1132cb93a386Sopenharmony_ci sk_sp<SkShader> makeShader(const SkSamplingOptions& sampling, 1133cb93a386Sopenharmony_ci const SkMatrix& localMatrix) const { 1134cb93a386Sopenharmony_ci return this->makeShader(sampling, &localMatrix); 1135cb93a386Sopenharmony_ci } 1136cb93a386Sopenharmony_ci 1137cb93a386Sopenharmony_ci /** 1138cb93a386Sopenharmony_ci * Returns a new image from the bitmap. If the bitmap is marked immutable, this will 1139cb93a386Sopenharmony_ci * share the pixel buffer. If not, it will make a copy of the pixels for the image. 1140cb93a386Sopenharmony_ci */ 1141cb93a386Sopenharmony_ci sk_sp<SkImage> asImage() const; 1142cb93a386Sopenharmony_ci 1143cb93a386Sopenharmony_ci /** Asserts if internal values are illegal or inconsistent. Only available if 1144cb93a386Sopenharmony_ci SK_DEBUG is defined at compile time. 1145cb93a386Sopenharmony_ci */ 1146cb93a386Sopenharmony_ci SkDEBUGCODE(void validate() const;) 1147cb93a386Sopenharmony_ci 1148cb93a386Sopenharmony_ci /** \class SkBitmap::Allocator 1149cb93a386Sopenharmony_ci Abstract subclass of HeapAllocator. 1150cb93a386Sopenharmony_ci */ 1151cb93a386Sopenharmony_ci class Allocator : public SkRefCnt { 1152cb93a386Sopenharmony_ci public: 1153cb93a386Sopenharmony_ci 1154cb93a386Sopenharmony_ci /** Allocates the pixel memory for the bitmap, given its dimensions and 1155cb93a386Sopenharmony_ci SkColorType. Returns true on success, where success means either setPixels() 1156cb93a386Sopenharmony_ci or setPixelRef() was called. 1157cb93a386Sopenharmony_ci 1158cb93a386Sopenharmony_ci @param bitmap SkBitmap containing SkImageInfo as input, and SkPixelRef as output 1159cb93a386Sopenharmony_ci @return true if SkPixelRef was allocated 1160cb93a386Sopenharmony_ci */ 1161cb93a386Sopenharmony_ci virtual bool allocPixelRef(SkBitmap* bitmap) = 0; 1162cb93a386Sopenharmony_ci private: 1163cb93a386Sopenharmony_ci using INHERITED = SkRefCnt; 1164cb93a386Sopenharmony_ci }; 1165cb93a386Sopenharmony_ci 1166cb93a386Sopenharmony_ci /** \class SkBitmap::HeapAllocator 1167cb93a386Sopenharmony_ci Subclass of SkBitmap::Allocator that returns a SkPixelRef that allocates its pixel 1168cb93a386Sopenharmony_ci memory from the heap. This is the default SkBitmap::Allocator invoked by 1169cb93a386Sopenharmony_ci allocPixels(). 1170cb93a386Sopenharmony_ci */ 1171cb93a386Sopenharmony_ci class HeapAllocator : public Allocator { 1172cb93a386Sopenharmony_ci public: 1173cb93a386Sopenharmony_ci 1174cb93a386Sopenharmony_ci /** Allocates the pixel memory for the bitmap, given its dimensions and 1175cb93a386Sopenharmony_ci SkColorType. Returns true on success, where success means either setPixels() 1176cb93a386Sopenharmony_ci or setPixelRef() was called. 1177cb93a386Sopenharmony_ci 1178cb93a386Sopenharmony_ci @param bitmap SkBitmap containing SkImageInfo as input, and SkPixelRef as output 1179cb93a386Sopenharmony_ci @return true if pixels are allocated 1180cb93a386Sopenharmony_ci 1181cb93a386Sopenharmony_ci example: https://fiddle.skia.org/c/@Bitmap_HeapAllocator_allocPixelRef 1182cb93a386Sopenharmony_ci */ 1183cb93a386Sopenharmony_ci bool allocPixelRef(SkBitmap* bitmap) override; 1184cb93a386Sopenharmony_ci }; 1185cb93a386Sopenharmony_ci 1186cb93a386Sopenharmony_ciprivate: 1187cb93a386Sopenharmony_ci sk_sp<SkPixelRef> fPixelRef; 1188cb93a386Sopenharmony_ci SkPixmap fPixmap; 1189cb93a386Sopenharmony_ci sk_sp<SkMipmap> fMips; 1190cb93a386Sopenharmony_ci 1191cb93a386Sopenharmony_ci friend class SkImage_Raster; 1192cb93a386Sopenharmony_ci friend class SkReadBuffer; // unflatten 1193cb93a386Sopenharmony_ci}; 1194cb93a386Sopenharmony_ci 1195cb93a386Sopenharmony_ci/////////////////////////////////////////////////////////////////////////////// 1196cb93a386Sopenharmony_ci 1197cb93a386Sopenharmony_ciinline uint32_t* SkBitmap::getAddr32(int x, int y) const { 1198cb93a386Sopenharmony_ci SkASSERT(fPixmap.addr()); 1199cb93a386Sopenharmony_ci return fPixmap.writable_addr32(x, y); 1200cb93a386Sopenharmony_ci} 1201cb93a386Sopenharmony_ci 1202cb93a386Sopenharmony_ciinline uint16_t* SkBitmap::getAddr16(int x, int y) const { 1203cb93a386Sopenharmony_ci SkASSERT(fPixmap.addr()); 1204cb93a386Sopenharmony_ci return fPixmap.writable_addr16(x, y); 1205cb93a386Sopenharmony_ci} 1206cb93a386Sopenharmony_ci 1207cb93a386Sopenharmony_ciinline uint8_t* SkBitmap::getAddr8(int x, int y) const { 1208cb93a386Sopenharmony_ci SkASSERT(fPixmap.addr()); 1209cb93a386Sopenharmony_ci return fPixmap.writable_addr8(x, y); 1210cb93a386Sopenharmony_ci} 1211cb93a386Sopenharmony_ci 1212cb93a386Sopenharmony_ci#endif 1213