SkBitmap.h 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /*
  2. * Copyright 2006 The Android Open Source Project
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. /* Generated by tools/bookmaker from include/core/SkBitmap.h and docs/SkBitmap_Reference.bmh
  8. on 2018-09-13 13:59:55. Additional documentation and examples can be found at:
  9. https://skia.org/user/api/SkBitmap_Reference
  10. You may edit either file directly. Structural changes to public interfaces require
  11. editing both files. After editing docs/SkBitmap_Reference.bmh, run:
  12. bookmaker -b docs -i include/core/SkBitmap.h -p
  13. to create an updated version of this file.
  14. */
  15. #ifndef SkBitmap_DEFINED
  16. #define SkBitmap_DEFINED
  17. #include "SkColor.h"
  18. #include "SkImageInfo.h"
  19. #include "SkPixmap.h"
  20. #include "SkPoint.h"
  21. #include "SkRefCnt.h"
  22. struct SkMask;
  23. struct SkIRect;
  24. struct SkRect;
  25. class SkPaint;
  26. class SkPixelRef;
  27. class SkString;
  28. /** \class SkBitmap
  29. SkBitmap describes a two-dimensional raster pixel array. SkBitmap is built on
  30. SkImageInfo, containing integer width and height, SkColorType and SkAlphaType
  31. describing the pixel format, and SkColorSpace describing the range of colors.
  32. SkBitmap points to SkPixelRef, which describes the physical array of pixels.
  33. SkImageInfo bounds may be located anywhere fully inside SkPixelRef bounds.
  34. SkBitmap can be drawn using SkCanvas. SkBitmap can be a drawing destination for SkCanvas
  35. draw member functions. SkBitmap flexibility as a pixel container limits some
  36. optimizations available to the target platform.
  37. If pixel array is primarily read-only, use SkImage for better performance.
  38. If pixel array is primarily written to, use SkSurface for better performance.
  39. Declaring SkBitmap const prevents altering SkImageInfo: the SkBitmap height, width,
  40. and so on cannot change. It does not affect SkPixelRef: a caller may write its
  41. pixels. Declaring SkBitmap const affects SkBitmap configuration, not its contents.
  42. SkBitmap is not thread safe. Each thread must have its own copy of SkBitmap fields,
  43. although threads may share the underlying pixel array.
  44. */
  45. class SK_API SkBitmap {
  46. public:
  47. class SK_API Allocator;
  48. /** Creates an empty SkBitmap without pixels, with kUnknown_SkColorType,
  49. kUnknown_SkAlphaType, and with a width and height of zero. SkPixelRef origin is
  50. set to (0, 0). SkBitmap is not volatile.
  51. Use setInfo() to associate SkColorType, SkAlphaType, width, and height
  52. after SkBitmap has been created.
  53. @return empty SkBitmap
  54. */
  55. SkBitmap();
  56. /** Copies settings from src to returned SkBitmap. Shares pixels if src has pixels
  57. allocated, so both bitmaps reference the same pixels.
  58. @param src SkBitmap to copy SkImageInfo, and share SkPixelRef
  59. @return copy of src
  60. */
  61. SkBitmap(const SkBitmap& src);
  62. /** Copies settings from src to returned SkBitmap. Moves ownership of src pixels to
  63. SkBitmap.
  64. @param src SkBitmap to copy SkImageInfo, and reassign SkPixelRef
  65. @return copy of src
  66. */
  67. SkBitmap(SkBitmap&& src);
  68. /** Decrements SkPixelRef reference count, if SkPixelRef is not nullptr.
  69. */
  70. ~SkBitmap();
  71. /** Copies settings from src to returned SkBitmap. Shares pixels if src has pixels
  72. allocated, so both bitmaps reference the same pixels.
  73. @param src SkBitmap to copy SkImageInfo, and share SkPixelRef
  74. @return copy of src
  75. */
  76. SkBitmap& operator=(const SkBitmap& src);
  77. /** Copies settings from src to returned SkBitmap. Moves ownership of src pixels to
  78. SkBitmap.
  79. @param src SkBitmap to copy SkImageInfo, and reassign SkPixelRef
  80. @return copy of src
  81. */
  82. SkBitmap& operator=(SkBitmap&& src);
  83. /** Swaps the fields of the two bitmaps.
  84. @param other SkBitmap exchanged with original
  85. */
  86. void swap(SkBitmap& other);
  87. /** Returns a constant reference to the SkPixmap holding the SkBitmap pixel
  88. address, row bytes, and SkImageInfo.
  89. @return reference to SkPixmap describing this SkBitmap
  90. */
  91. const SkPixmap& pixmap() const { return fPixmap; }
  92. /** Returns width, height, SkAlphaType, SkColorType, and SkColorSpace.
  93. @return reference to SkImageInfo
  94. */
  95. const SkImageInfo& info() const { return fPixmap.info(); }
  96. /** Returns pixel count in each row. Should be equal or less than
  97. rowBytes() / info().bytesPerPixel().
  98. May be less than pixelRef().width(). Will not exceed pixelRef().width() less
  99. pixelRefOrigin().fX.
  100. @return pixel width in SkImageInfo
  101. */
  102. int width() const { return fPixmap.width(); }
  103. /** Returns pixel row count.
  104. Maybe be less than pixelRef().height(). Will not exceed pixelRef().height() less
  105. pixelRefOrigin().fY.
  106. @return pixel height in SkImageInfo
  107. */
  108. int height() const { return fPixmap.height(); }
  109. /** Returns SkColorType, one of:
  110. kUnknown_SkColorType, kAlpha_8_SkColorType, kRGB_565_SkColorType,
  111. kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kRGB_888x_SkColorType,
  112. kBGRA_8888_SkColorType, kRGBA_1010102_SkColorType, kRGB_101010x_SkColorType,
  113. kGray_8_SkColorType, kRGBA_F16_SkColorType.
  114. @return SkColorType in SkImageInfo
  115. */
  116. SkColorType colorType() const { return fPixmap.colorType(); }
  117. /** Returns SkAlphaType, one of:
  118. kUnknown_SkAlphaType, kOpaque_SkAlphaType, kPremul_SkAlphaType,
  119. kUnpremul_SkAlphaType.
  120. @return SkAlphaType in SkImageInfo
  121. */
  122. SkAlphaType alphaType() const { return fPixmap.alphaType(); }
  123. /** Returns SkColorSpace, the range of colors, associated with SkImageInfo. The
  124. reference count of SkColorSpace is unchanged. The returned SkColorSpace is
  125. immutable.
  126. @return SkColorSpace in SkImageInfo, or nullptr
  127. */
  128. SkColorSpace* colorSpace() const { return fPixmap.colorSpace(); }
  129. /** Returns smart pointer to SkColorSpace, the range of colors, associated with
  130. SkImageInfo. The smart pointer tracks the number of objects sharing this
  131. SkColorSpace reference so the memory is released when the owners destruct.
  132. The returned SkColorSpace is immutable.
  133. @return SkColorSpace in SkImageInfo wrapped in a smart pointer
  134. */
  135. sk_sp<SkColorSpace> refColorSpace() const { return fPixmap.info().refColorSpace(); }
  136. /** Returns number of bytes per pixel required by SkColorType.
  137. Returns zero if colorType( is kUnknown_SkColorType.
  138. @return bytes in pixel
  139. */
  140. int bytesPerPixel() const { return fPixmap.info().bytesPerPixel(); }
  141. /** Returns number of pixels that fit on row. Should be greater than or equal to
  142. width().
  143. @return maximum pixels per row
  144. */
  145. int rowBytesAsPixels() const { return fPixmap.rowBytesAsPixels(); }
  146. /** Returns bit shift converting row bytes to row pixels.
  147. Returns zero for kUnknown_SkColorType.
  148. @return one of: 0, 1, 2, 3; left shift to convert pixels to bytes
  149. */
  150. int shiftPerPixel() const { return fPixmap.shiftPerPixel(); }
  151. /** Returns true if either width() or height() are zero.
  152. Does not check if SkPixelRef is nullptr; call drawsNothing() to check width(),
  153. height(), and SkPixelRef.
  154. @return true if dimensions do not enclose area
  155. */
  156. bool empty() const { return fPixmap.info().isEmpty(); }
  157. /** Returns true if SkPixelRef is nullptr.
  158. Does not check if width() or height() are zero; call drawsNothing() to check
  159. width(), height(), and SkPixelRef.
  160. @return true if no SkPixelRef is associated
  161. */
  162. bool isNull() const { return nullptr == fPixelRef; }
  163. /** Returns true if width() or height() are zero, or if SkPixelRef is nullptr.
  164. If true, SkBitmap has no effect when drawn or drawn into.
  165. @return true if drawing has no effect
  166. */
  167. bool drawsNothing() const {
  168. return this->empty() || this->isNull();
  169. }
  170. /** Returns row bytes, the interval from one pixel row to the next. Row bytes
  171. is at least as large as: width() * info().bytesPerPixel().
  172. Returns zero if colorType() is kUnknown_SkColorType, or if row bytes supplied to
  173. setInfo() is not large enough to hold a row of pixels.
  174. @return byte length of pixel row
  175. */
  176. size_t rowBytes() const { return fPixmap.rowBytes(); }
  177. /** Sets SkAlphaType, if alphaType is compatible with SkColorType.
  178. Returns true unless alphaType is kUnknown_SkAlphaType and current SkAlphaType
  179. is not kUnknown_SkAlphaType.
  180. Returns true if SkColorType is kUnknown_SkColorType. alphaType is ignored, and
  181. SkAlphaType remains kUnknown_SkAlphaType.
  182. Returns true if SkColorType is kRGB_565_SkColorType or kGray_8_SkColorType.
  183. alphaType is ignored, and SkAlphaType remains kOpaque_SkAlphaType.
  184. If SkColorType is kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
  185. kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: returns true unless
  186. alphaType is kUnknown_SkAlphaType and SkAlphaType is not kUnknown_SkAlphaType.
  187. If SkAlphaType is kUnknown_SkAlphaType, alphaType is ignored.
  188. If SkColorType is kAlpha_8_SkColorType, returns true unless
  189. alphaType is kUnknown_SkAlphaType and SkAlphaType is not kUnknown_SkAlphaType.
  190. If SkAlphaType is kUnknown_SkAlphaType, alphaType is ignored. If alphaType is
  191. kUnpremul_SkAlphaType, it is treated as kPremul_SkAlphaType.
  192. This changes SkAlphaType in SkPixelRef; all bitmaps sharing SkPixelRef
  193. are affected.
  194. @param alphaType one of:
  195. kUnknown_SkAlphaType, kOpaque_SkAlphaType, kPremul_SkAlphaType,
  196. kUnpremul_SkAlphaType
  197. @return true if SkAlphaType is set
  198. */
  199. bool setAlphaType(SkAlphaType alphaType);
  200. /** Returns pixel address, the base address corresponding to the pixel origin.
  201. @return pixel address
  202. */
  203. void* getPixels() const { return fPixmap.writable_addr(); }
  204. /** Returns minimum memory required for pixel storage.
  205. Does not include unused memory on last row when rowBytesAsPixels() exceeds width().
  206. Returns zero if result does not fit in size_t.
  207. Returns zero if height() or width() is 0.
  208. Returns height() times rowBytes() if colorType() is kUnknown_SkColorType.
  209. @return size in bytes of image buffer
  210. */
  211. size_t computeByteSize() const { return fPixmap.computeByteSize(); }
  212. /** Returns true if pixels can not change.
  213. Most immutable SkBitmap checks trigger an assert only on debug builds.
  214. @return true if pixels are immutable
  215. */
  216. bool isImmutable() const;
  217. /** Sets internal flag to mark SkBitmap as immutable. Once set, pixels can not change.
  218. Any other bitmap sharing the same SkPixelRef are also marked as immutable.
  219. Once SkPixelRef is marked immutable, the setting cannot be cleared.
  220. Writing to immutable SkBitmap pixels triggers an assert on debug builds.
  221. */
  222. void setImmutable();
  223. /** Returns true if SkAlphaType is set to hint that all pixels are opaque; their
  224. alpha value is implicitly or explicitly 1.0. If true, and all pixels are
  225. not opaque, Skia may draw incorrectly.
  226. Does not check if SkColorType allows alpha, or if any pixel value has
  227. transparency.
  228. @return true if SkImageInfo SkAlphaType is kOpaque_SkAlphaType
  229. */
  230. bool isOpaque() const {
  231. return SkAlphaTypeIsOpaque(this->alphaType());
  232. }
  233. /** Provides a hint to caller that pixels should not be cached. Only true if
  234. setIsVolatile() has been called to mark as volatile.
  235. Volatile state is not shared by other bitmaps sharing the same SkPixelRef.
  236. @return true if marked volatile
  237. */
  238. bool isVolatile() const;
  239. /** Sets if pixels should be read from SkPixelRef on every access. SkBitmap are not
  240. volatile by default; a GPU back end may upload pixel values expecting them to be
  241. accessed repeatedly. Marking temporary SkBitmap as volatile provides a hint to
  242. SkBaseDevice that the SkBitmap pixels should not be cached. This can
  243. improve performance by avoiding overhead and reducing resource
  244. consumption on SkBaseDevice.
  245. @param isVolatile true if backing pixels are temporary
  246. */
  247. void setIsVolatile(bool isVolatile);
  248. /** Resets to its initial state; all fields are set to zero, as if SkBitmap had
  249. been initialized by SkBitmap().
  250. Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to
  251. kUnknown_SkColorType; and SkAlphaType to kUnknown_SkAlphaType.
  252. If SkPixelRef is allocated, its reference count is decreased by one, releasing
  253. its memory if SkBitmap is the sole owner.
  254. */
  255. void reset();
  256. /** Returns true if all pixels are opaque. SkColorType determines how pixels
  257. are encoded, and whether pixel describes alpha. Returns true for SkColorType
  258. without alpha in each pixel; for other SkColorType, returns true if all
  259. pixels have alpha values equivalent to 1.0 or greater.
  260. For SkColorType kRGB_565_SkColorType or kGray_8_SkColorType: always
  261. returns true. For SkColorType kAlpha_8_SkColorType, kBGRA_8888_SkColorType,
  262. kRGBA_8888_SkColorType: returns true if all pixel alpha values are 255.
  263. For SkColorType kARGB_4444_SkColorType: returns true if all pixel alpha values are 15.
  264. For kRGBA_F16_SkColorType: returns true if all pixel alpha values are 1.0 or
  265. greater.
  266. Returns false for kUnknown_SkColorType.
  267. @param bm SkBitmap to check
  268. @return true if all pixels have opaque values or SkColorType is opaque
  269. */
  270. static bool ComputeIsOpaque(const SkBitmap& bm) {
  271. return bm.pixmap().computeIsOpaque();
  272. }
  273. /** Returns SkRect { 0, 0, width(), height() }.
  274. @param bounds container for floating point rectangle
  275. */
  276. void getBounds(SkRect* bounds) const;
  277. /** Returns SkIRect { 0, 0, width(), height() }.
  278. @param bounds container for integral rectangle
  279. */
  280. void getBounds(SkIRect* bounds) const;
  281. /** Returns SkIRect { 0, 0, width(), height() }.
  282. @return integral rectangle from origin to width() and height()
  283. */
  284. SkIRect bounds() const { return fPixmap.info().bounds(); }
  285. /** Returns SkISize { width(), height() }.
  286. @return integral size of width() and height()
  287. */
  288. SkISize dimensions() const { return fPixmap.info().dimensions(); }
  289. /** Returns the bounds of this bitmap, offset by its SkPixelRef origin.
  290. @return bounds within SkPixelRef bounds
  291. */
  292. SkIRect getSubset() const {
  293. SkIPoint origin = this->pixelRefOrigin();
  294. return SkIRect::MakeXYWH(origin.x(), origin.y(), this->width(), this->height());
  295. }
  296. /** Sets width, height, SkAlphaType, SkColorType, SkColorSpace, and optional
  297. rowBytes. Frees pixels, and returns true if successful.
  298. imageInfo.alphaType() may be altered to a value permitted by imageInfo.colorSpace().
  299. If imageInfo.colorType() is kUnknown_SkColorType, imageInfo.alphaType() is
  300. set to kUnknown_SkAlphaType.
  301. If imageInfo.colorType() is kAlpha_8_SkColorType and imageInfo.alphaType() is
  302. kUnpremul_SkAlphaType, imageInfo.alphaType() is replaced by kPremul_SkAlphaType.
  303. If imageInfo.colorType() is kRGB_565_SkColorType or kGray_8_SkColorType,
  304. imageInfo.alphaType() is set to kOpaque_SkAlphaType.
  305. If imageInfo.colorType() is kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
  306. kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: imageInfo.alphaType() remains
  307. unchanged.
  308. rowBytes must equal or exceed imageInfo.minRowBytes(). If imageInfo.colorSpace() is
  309. kUnknown_SkColorType, rowBytes is ignored and treated as zero; for all other
  310. SkColorSpace values, rowBytes of zero is treated as imageInfo.minRowBytes().
  311. Calls reset() and returns false if:
  312. - rowBytes exceeds 31 bits
  313. - imageInfo.width() is negative
  314. - imageInfo.height() is negative
  315. - rowBytes is positive and less than imageInfo.width() times imageInfo.bytesPerPixel()
  316. @param imageInfo contains width, height, SkAlphaType, SkColorType, SkColorSpace
  317. @param rowBytes imageInfo.minRowBytes() or larger; or zero
  318. @return true if SkImageInfo set successfully
  319. */
  320. bool setInfo(const SkImageInfo& imageInfo, size_t rowBytes = 0);
  321. /** \enum SkBitmap::AllocFlags
  322. AllocFlags provides the option to zero pixel memory when allocated.
  323. */
  324. enum AllocFlags {
  325. kZeroPixels_AllocFlag = 1 << 0, //!< zero pixel memory
  326. };
  327. /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel
  328. memory. If flags is kZeroPixels_AllocFlag, memory is zeroed.
  329. Returns false and calls reset() if SkImageInfo could not be set, or memory could
  330. not be allocated, or memory could not optionally be zeroed.
  331. On most platforms, allocating pixel memory may succeed even though there is
  332. not sufficient memory to hold pixels; allocation does not take place
  333. until the pixels are written to. The actual behavior depends on the platform
  334. implementation of malloc(), if flags is zero, and calloc(), if flags is
  335. kZeroPixels_AllocFlag.
  336. flags set to kZeroPixels_AllocFlag offers equal or better performance than
  337. subsequently calling eraseColor() with SK_ColorTRANSPARENT.
  338. @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace
  339. @param flags kZeroPixels_AllocFlag, or zero
  340. @return true if pixels allocation is successful
  341. */
  342. bool SK_WARN_UNUSED_RESULT tryAllocPixelsFlags(const SkImageInfo& info, uint32_t flags);
  343. /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel
  344. memory. If flags is kZeroPixels_AllocFlag, memory is zeroed.
  345. Aborts execution if SkImageInfo could not be set, or memory could
  346. not be allocated, or memory could not optionally
  347. be zeroed. Abort steps may be provided by the user at compile time by defining
  348. SK_ABORT.
  349. On most platforms, allocating pixel memory may succeed even though there is
  350. not sufficient memory to hold pixels; allocation does not take place
  351. until the pixels are written to. The actual behavior depends on the platform
  352. implementation of malloc(), if flags is zero, and calloc(), if flags is
  353. kZeroPixels_AllocFlag.
  354. flags set to kZeroPixels_AllocFlag offers equal or better performance than
  355. subsequently calling eraseColor() with SK_ColorTRANSPARENT.
  356. @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace
  357. @param flags kZeroPixels_AllocFlag, or zero
  358. */
  359. void allocPixelsFlags(const SkImageInfo& info, uint32_t flags) {
  360. SkASSERT_RELEASE(this->tryAllocPixelsFlags(info, flags));
  361. }
  362. /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel
  363. memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(),
  364. or equal zero. Pass in zero for rowBytes to compute the minimum valid value.
  365. Returns false and calls reset() if SkImageInfo could not be set, or memory could
  366. not be allocated.
  367. On most platforms, allocating pixel memory may succeed even though there is
  368. not sufficient memory to hold pixels; allocation does not take place
  369. until the pixels are written to. The actual behavior depends on the platform
  370. implementation of malloc().
  371. @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace
  372. @param rowBytes size of pixel row or larger; may be zero
  373. @return true if pixel storage is allocated
  374. */
  375. bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info, size_t rowBytes);
  376. /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel
  377. memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(),
  378. or equal zero. Pass in zero for rowBytes to compute the minimum valid value.
  379. Aborts execution if SkImageInfo could not be set, or memory could
  380. not be allocated. Abort steps may be provided by
  381. the user at compile time by defining SK_ABORT.
  382. On most platforms, allocating pixel memory may succeed even though there is
  383. not sufficient memory to hold pixels; allocation does not take place
  384. until the pixels are written to. The actual behavior depends on the platform
  385. implementation of malloc().
  386. @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace
  387. @param rowBytes size of pixel row or larger; may be zero
  388. */
  389. void allocPixels(const SkImageInfo& info, size_t rowBytes) {
  390. SkASSERT_RELEASE(this->tryAllocPixels(info, rowBytes));
  391. }
  392. /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel
  393. memory.
  394. Returns false and calls reset() if SkImageInfo could not be set, or memory could
  395. not be allocated.
  396. On most platforms, allocating pixel memory may succeed even though there is
  397. not sufficient memory to hold pixels; allocation does not take place
  398. until the pixels are written to. The actual behavior depends on the platform
  399. implementation of malloc().
  400. @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace
  401. @return true if pixel storage is allocated
  402. */
  403. bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info) {
  404. return this->tryAllocPixels(info, info.minRowBytes());
  405. }
  406. /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel
  407. memory.
  408. Aborts execution if SkImageInfo could not be set, or memory could
  409. not be allocated. Abort steps may be provided by
  410. the user at compile time by defining SK_ABORT.
  411. On most platforms, allocating pixel memory may succeed even though there is
  412. not sufficient memory to hold pixels; allocation does not take place
  413. until the pixels are written to. The actual behavior depends on the platform
  414. implementation of malloc().
  415. @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace
  416. */
  417. void allocPixels(const SkImageInfo& info) {
  418. this->allocPixels(info, info.minRowBytes());
  419. }
  420. /** Sets SkImageInfo to width, height, and native color type; and allocates
  421. pixel memory. If isOpaque is true, sets SkImageInfo to kOpaque_SkAlphaType;
  422. otherwise, sets to kPremul_SkAlphaType.
  423. Calls reset() and returns false if width exceeds 29 bits or is negative,
  424. or height is negative.
  425. Returns false if allocation fails.
  426. Use to create SkBitmap that matches SkPMColor, the native pixel arrangement on
  427. the platform. SkBitmap drawn to output device skips converting its pixel format.
  428. @param width pixel column count; must be zero or greater
  429. @param height pixel row count; must be zero or greater
  430. @param isOpaque true if pixels do not have transparency
  431. @return true if pixel storage is allocated
  432. */
  433. bool SK_WARN_UNUSED_RESULT tryAllocN32Pixels(int width, int height, bool isOpaque = false) {
  434. SkImageInfo info = SkImageInfo::MakeN32(width, height,
  435. isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
  436. return this->tryAllocPixels(info);
  437. }
  438. /** Sets SkImageInfo to width, height, and the native color type; and allocates
  439. pixel memory. If isOpaque is true, sets SkImageInfo to kPremul_SkAlphaType;
  440. otherwise, sets to kOpaque_SkAlphaType.
  441. Aborts if width exceeds 29 bits or is negative, or height is negative, or
  442. allocation fails. Abort steps may be provided by the user at compile time by
  443. defining SK_ABORT.
  444. Use to create SkBitmap that matches SkPMColor, the native pixel arrangement on
  445. the platform. SkBitmap drawn to output device skips converting its pixel format.
  446. @param width pixel column count; must be zero or greater
  447. @param height pixel row count; must be zero or greater
  448. @param isOpaque true if pixels do not have transparency
  449. */
  450. void allocN32Pixels(int width, int height, bool isOpaque = false) {
  451. SkImageInfo info = SkImageInfo::MakeN32(width, height,
  452. isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
  453. this->allocPixels(info);
  454. }
  455. /** Sets SkImageInfo to info following the rules in setInfo(), and creates SkPixelRef
  456. containing pixels and rowBytes. releaseProc, if not nullptr, is called
  457. immediately on failure or when pixels are no longer referenced. context may be
  458. nullptr.
  459. If SkImageInfo could not be set, or rowBytes is less than info.minRowBytes():
  460. calls releaseProc if present, calls reset(), and returns false.
  461. Otherwise, if pixels equals nullptr: sets SkImageInfo, calls releaseProc if
  462. present, returns true.
  463. If SkImageInfo is set, pixels is not nullptr, and releaseProc is not nullptr:
  464. when pixels are no longer referenced, calls releaseProc with pixels and context
  465. as parameters.
  466. @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace
  467. @param pixels address or pixel storage; may be nullptr
  468. @param rowBytes size of pixel row or larger
  469. @param releaseProc function called when pixels can be deleted; may be nullptr
  470. @param context caller state passed to releaseProc; may be nullptr
  471. @return true if SkImageInfo is set to info
  472. */
  473. bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
  474. void (*releaseProc)(void* addr, void* context), void* context);
  475. /** Sets SkImageInfo to info following the rules in setInfo(), and creates SkPixelRef
  476. containing pixels and rowBytes.
  477. If SkImageInfo could not be set, or rowBytes is less than info.minRowBytes():
  478. calls reset(), and returns false.
  479. Otherwise, if pixels equals nullptr: sets SkImageInfo, returns true.
  480. Caller must ensure that pixels are valid for the lifetime of SkBitmap and SkPixelRef.
  481. @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace
  482. @param pixels address or pixel storage; may be nullptr
  483. @param rowBytes size of pixel row or larger
  484. @return true if SkImageInfo is set to info
  485. */
  486. bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
  487. return this->installPixels(info, pixels, rowBytes, nullptr, nullptr);
  488. }
  489. /** Sets SkImageInfo to pixmap.info() following the rules in setInfo(), and creates
  490. SkPixelRef containing pixmap.addr() and pixmap.rowBytes().
  491. If SkImageInfo could not be set, or pixmap.rowBytes() is less than
  492. SkImageInfo::minRowBytes(): calls reset(), and returns false.
  493. Otherwise, if pixmap.addr() equals nullptr: sets SkImageInfo, returns true.
  494. Caller must ensure that pixmap is valid for the lifetime of SkBitmap and SkPixelRef.
  495. @param pixmap SkImageInfo, pixel address, and rowBytes()
  496. @return true if SkImageInfo was set to pixmap.info()
  497. */
  498. bool installPixels(const SkPixmap& pixmap);
  499. /** Deprecated.
  500. */
  501. bool installMaskPixels(const SkMask& mask);
  502. /** Replaces SkPixelRef with pixels, preserving SkImageInfo and rowBytes().
  503. Sets SkPixelRef origin to (0, 0).
  504. If pixels is nullptr, or if info().colorType() equals kUnknown_SkColorType;
  505. release reference to SkPixelRef, and set SkPixelRef to nullptr.
  506. Caller is responsible for handling ownership pixel memory for the lifetime
  507. of SkBitmap and SkPixelRef.
  508. @param pixels address of pixel storage, managed by caller
  509. */
  510. void setPixels(void* pixels);
  511. /** Allocates pixel memory with HeapAllocator, and replaces existing SkPixelRef.
  512. The allocation size is determined by SkImageInfo width, height, and SkColorType.
  513. Returns false if info().colorType() is kUnknown_SkColorType, or allocation fails.
  514. @return true if the allocation succeeds
  515. */
  516. bool SK_WARN_UNUSED_RESULT tryAllocPixels() {
  517. return this->tryAllocPixels((Allocator*)nullptr);
  518. }
  519. /** Allocates pixel memory with HeapAllocator, and replaces existing SkPixelRef.
  520. The allocation size is determined by SkImageInfo width, height, and SkColorType.
  521. Aborts if info().colorType() is kUnknown_SkColorType, or allocation fails.
  522. Abort steps may be provided by the user at compile
  523. time by defining SK_ABORT.
  524. */
  525. void allocPixels() {
  526. this->allocPixels((Allocator*)nullptr);
  527. }
  528. /** Allocates pixel memory with allocator, and replaces existing SkPixelRef.
  529. The allocation size is determined by SkImageInfo width, height, and SkColorType.
  530. If allocator is nullptr, use HeapAllocator instead.
  531. Returns false if Allocator::allocPixelRef return false.
  532. @param allocator instance of SkBitmap::Allocator instantiation
  533. @return true if custom allocator reports success
  534. */
  535. bool SK_WARN_UNUSED_RESULT tryAllocPixels(Allocator* allocator);
  536. /** Allocates pixel memory with allocator, and replaces existing SkPixelRef.
  537. The allocation size is determined by SkImageInfo width, height, and SkColorType.
  538. If allocator is nullptr, use HeapAllocator instead.
  539. Aborts if Allocator::allocPixelRef return false. Abort steps may be provided by
  540. the user at compile time by defining SK_ABORT.
  541. @param allocator instance of SkBitmap::Allocator instantiation
  542. */
  543. void allocPixels(Allocator* allocator) {
  544. SkASSERT_RELEASE(this->tryAllocPixels(allocator));
  545. }
  546. /** Returns SkPixelRef, which contains: pixel base address; its dimensions; and
  547. rowBytes(), the interval from one row to the next. Does not change SkPixelRef
  548. reference count. SkPixelRef may be shared by multiple bitmaps.
  549. If SkPixelRef has not been set, returns nullptr.
  550. @return SkPixelRef, or nullptr
  551. */
  552. SkPixelRef* pixelRef() const { return fPixelRef.get(); }
  553. /** Returns origin of pixels within SkPixelRef. SkBitmap bounds is always contained
  554. by SkPixelRef bounds, which may be the same size or larger. Multiple SkBitmap
  555. can share the same SkPixelRef, where each SkBitmap has different bounds.
  556. The returned origin added to SkBitmap dimensions equals or is smaller than the
  557. SkPixelRef dimensions.
  558. Returns (0, 0) if SkPixelRef is nullptr.
  559. @return pixel origin within SkPixelRef
  560. */
  561. SkIPoint pixelRefOrigin() const;
  562. /** Replaces pixelRef and origin in SkBitmap. dx and dy specify the offset
  563. within the SkPixelRef pixels for the top-left corner of the bitmap.
  564. Asserts in debug builds if dx or dy are out of range. Pins dx and dy
  565. to legal range in release builds.
  566. The caller is responsible for ensuring that the pixels match the
  567. SkColorType and SkAlphaType in SkImageInfo.
  568. @param pixelRef SkPixelRef describing pixel address and rowBytes()
  569. @param dx column offset in SkPixelRef for bitmap origin
  570. @param dy row offset in SkPixelRef for bitmap origin
  571. */
  572. void setPixelRef(sk_sp<SkPixelRef> pixelRef, int dx, int dy);
  573. /** Returns true if SkBitmap is can be drawn.
  574. @return true if getPixels() is not nullptr
  575. */
  576. bool readyToDraw() const {
  577. return this->getPixels() != nullptr;
  578. }
  579. /** Returns a unique value corresponding to the pixels in SkPixelRef.
  580. Returns a different value after notifyPixelsChanged() has been called.
  581. Returns zero if SkPixelRef is nullptr.
  582. Determines if pixels have changed since last examined.
  583. @return unique value for pixels in SkPixelRef
  584. */
  585. uint32_t getGenerationID() const;
  586. /** Marks that pixels in SkPixelRef have changed. Subsequent calls to
  587. getGenerationID() return a different value.
  588. */
  589. void notifyPixelsChanged() const;
  590. /** Replaces pixel values with c. All pixels contained by bounds() are affected.
  591. If the colorType() is kGray_8_SkColorType or kRGB_565_SkColorType, then alpha
  592. is ignored; RGB is treated as opaque. If colorType() is kAlpha_8_SkColorType,
  593. then RGB is ignored.
  594. @param c unpremultiplied color
  595. */
  596. void eraseColor(SkColor c) const;
  597. /** Replaces pixel values with unpremultiplied color built from a, r, g, and b.
  598. All pixels contained by bounds() are affected.
  599. If the colorType() is kGray_8_SkColorType or kRGB_565_SkColorType, then a
  600. is ignored; r, g, and b are treated as opaque. If colorType() is kAlpha_8_SkColorType,
  601. then r, g, and b are ignored.
  602. @param a amount of alpha, from fully transparent (0) to fully opaque (255)
  603. @param r amount of red, from no red (0) to full red (255)
  604. @param g amount of green, from no green (0) to full green (255)
  605. @param b amount of blue, from no blue (0) to full blue (255)
  606. */
  607. void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
  608. this->eraseColor(SkColorSetARGB(a, r, g, b));
  609. }
  610. /** Replaces pixel values inside area with c. If area does not intersect bounds(),
  611. call has no effect.
  612. If the colorType() is kGray_8_SkColorType or kRGB_565_SkColorType, then alpha
  613. is ignored; RGB is treated as opaque. If colorType() is kAlpha_8_SkColorType,
  614. then RGB is ignored.
  615. @param c unpremultiplied color
  616. @param area rectangle to fill
  617. */
  618. void erase(SkColor c, const SkIRect& area) const;
  619. /** Deprecated.
  620. */
  621. void eraseArea(const SkIRect& area, SkColor c) const {
  622. this->erase(c, area);
  623. }
  624. /** Returns pixel at (x, y) as unpremultiplied color.
  625. Returns black with alpha if SkColorType is kAlpha_8_SkColorType.
  626. Input is not validated: out of bounds values of x or y trigger an assert() if
  627. built with SK_DEBUG defined; and returns undefined values or may crash if
  628. SK_RELEASE is defined. Fails if SkColorType is kUnknown_SkColorType or
  629. pixel address is nullptr.
  630. SkColorSpace in SkImageInfo is ignored. Some color precision may be lost in the
  631. conversion to unpremultiplied color; original pixel data may have additional
  632. precision.
  633. @param x column index, zero or greater, and less than width()
  634. @param y row index, zero or greater, and less than height()
  635. @return pixel converted to unpremultiplied color
  636. */
  637. SkColor getColor(int x, int y) const {
  638. return this->pixmap().getColor(x, y);
  639. }
  640. /** Look up the pixel at (x,y) and return its alpha component, normalized to [0..1].
  641. This is roughly equivalent to SkGetColorA(getColor()), but can be more efficent
  642. (and more precise if the pixels store more than 8 bits per component).
  643. @param x column index, zero or greater, and less than width()
  644. @param y row index, zero or greater, and less than height()
  645. @return alpha converted to normalized float
  646. */
  647. float getAlphaf(int x, int y) const {
  648. return this->pixmap().getAlphaf(x, y);
  649. }
  650. /** Returns pixel address at (x, y).
  651. Input is not validated: out of bounds values of x or y, or kUnknown_SkColorType,
  652. trigger an assert() if built with SK_DEBUG defined. Returns nullptr if
  653. SkColorType is kUnknown_SkColorType, or SkPixelRef is nullptr.
  654. Performs a lookup of pixel size; for better performance, call
  655. one of: getAddr8(), getAddr16(), or getAddr32().
  656. @param x column index, zero or greater, and less than width()
  657. @param y row index, zero or greater, and less than height()
  658. @return generic pointer to pixel
  659. */
  660. void* getAddr(int x, int y) const;
  661. /** Returns address at (x, y).
  662. Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
  663. - SkPixelRef is nullptr
  664. - bytesPerPixel() is not four
  665. - x is negative, or not less than width()
  666. - y is negative, or not less than height()
  667. @param x column index, zero or greater, and less than width()
  668. @param y row index, zero or greater, and less than height()
  669. @return unsigned 32-bit pointer to pixel at (x, y)
  670. */
  671. inline uint32_t* getAddr32(int x, int y) const;
  672. /** Returns address at (x, y).
  673. Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
  674. - SkPixelRef is nullptr
  675. - bytesPerPixel() is not two
  676. - x is negative, or not less than width()
  677. - y is negative, or not less than height()
  678. @param x column index, zero or greater, and less than width()
  679. @param y row index, zero or greater, and less than height()
  680. @return unsigned 16-bit pointer to pixel at (x, y)
  681. */
  682. inline uint16_t* getAddr16(int x, int y) const;
  683. /** Returns address at (x, y).
  684. Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
  685. - SkPixelRef is nullptr
  686. - bytesPerPixel() is not one
  687. - x is negative, or not less than width()
  688. - y is negative, or not less than height()
  689. @param x column index, zero or greater, and less than width()
  690. @param y row index, zero or greater, and less than height()
  691. @return unsigned 8-bit pointer to pixel at (x, y)
  692. */
  693. inline uint8_t* getAddr8(int x, int y) const;
  694. /** Shares SkPixelRef with dst. Pixels are not copied; SkBitmap and dst point
  695. to the same pixels; dst bounds() are set to the intersection of subset
  696. and the original bounds().
  697. subset may be larger than bounds(). Any area outside of bounds() is ignored.
  698. Any contents of dst are discarded. isVolatile() setting is copied to dst.
  699. dst is set to colorType(), alphaType(), and colorSpace().
  700. Return false if:
  701. - dst is nullptr
  702. - SkPixelRef is nullptr
  703. - subset does not intersect bounds()
  704. @param dst SkBitmap set to subset
  705. @param subset rectangle of pixels to reference
  706. @return true if dst is replaced by subset
  707. */
  708. bool extractSubset(SkBitmap* dst, const SkIRect& subset) const;
  709. /** Copies a SkRect of pixels from SkBitmap to dstPixels. Copy starts at (srcX, srcY),
  710. and does not exceed SkBitmap (width(), height()).
  711. dstInfo specifies width, height, SkColorType, SkAlphaType, and SkColorSpace of
  712. destination. dstRowBytes specifics the gap from one destination row to the next.
  713. Returns true if pixels are copied. Returns false if:
  714. - dstInfo has no address
  715. - dstRowBytes is less than dstInfo.minRowBytes()
  716. - SkPixelRef is nullptr
  717. Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is
  718. kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType() must match.
  719. If SkBitmap colorType() is kGray_8_SkColorType, dstInfo.colorSpace() must match.
  720. If SkBitmap alphaType() is kOpaque_SkAlphaType, dstInfo.alphaType() must
  721. match. If SkBitmap colorSpace() is nullptr, dstInfo.colorSpace() must match. Returns
  722. false if pixel conversion is not possible.
  723. srcX and srcY may be negative to copy only top or left of source. Returns
  724. false if width() or height() is zero or negative.
  725. Returns false if abs(srcX) >= Bitmap width(), or if abs(srcY) >= Bitmap height().
  726. @param dstInfo destination width, height, SkColorType, SkAlphaType, SkColorSpace
  727. @param dstPixels destination pixel storage
  728. @param dstRowBytes destination row length
  729. @param srcX column index whose absolute value is less than width()
  730. @param srcY row index whose absolute value is less than height()
  731. @return true if pixels are copied to dstPixels
  732. */
  733. bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
  734. int srcX, int srcY) const;
  735. /** Copies a SkRect of pixels from SkBitmap to dst. Copy starts at (srcX, srcY), and
  736. does not exceed SkBitmap (width(), height()).
  737. dst specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage,
  738. and row bytes of destination. dst.rowBytes() specifics the gap from one destination
  739. row to the next. Returns true if pixels are copied. Returns false if:
  740. - dst pixel storage equals nullptr
  741. - dst.rowBytes is less than SkImageInfo::minRowBytes()
  742. - SkPixelRef is nullptr
  743. Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is
  744. kGray_8_SkColorType, or kAlpha_8_SkColorType; dst SkColorType must match.
  745. If SkBitmap colorType() is kGray_8_SkColorType, dst SkColorSpace must match.
  746. If SkBitmap alphaType() is kOpaque_SkAlphaType, dst SkAlphaType must
  747. match. If SkBitmap colorSpace() is nullptr, dst SkColorSpace must match. Returns
  748. false if pixel conversion is not possible.
  749. srcX and srcY may be negative to copy only top or left of source. Returns
  750. false if width() or height() is zero or negative.
  751. Returns false if abs(srcX) >= Bitmap width(), or if abs(srcY) >= Bitmap height().
  752. @param dst destination SkPixmap: SkImageInfo, pixels, row bytes
  753. @param srcX column index whose absolute value is less than width()
  754. @param srcY row index whose absolute value is less than height()
  755. @return true if pixels are copied to dst
  756. */
  757. bool readPixels(const SkPixmap& dst, int srcX, int srcY) const;
  758. /** Copies a SkRect of pixels from SkBitmap to dst. Copy starts at (0, 0), and
  759. does not exceed SkBitmap (width(), height()).
  760. dst specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage,
  761. and row bytes of destination. dst.rowBytes() specifics the gap from one destination
  762. row to the next. Returns true if pixels are copied. Returns false if:
  763. - dst pixel storage equals nullptr
  764. - dst.rowBytes is less than SkImageInfo::minRowBytes()
  765. - SkPixelRef is nullptr
  766. Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is
  767. kGray_8_SkColorType, or kAlpha_8_SkColorType; dst SkColorType must match.
  768. If SkBitmap colorType() is kGray_8_SkColorType, dst SkColorSpace must match.
  769. If SkBitmap alphaType() is kOpaque_SkAlphaType, dst SkAlphaType must
  770. match. If SkBitmap colorSpace() is nullptr, dst SkColorSpace must match. Returns
  771. false if pixel conversion is not possible.
  772. @param dst destination SkPixmap: SkImageInfo, pixels, row bytes
  773. @return true if pixels are copied to dst
  774. */
  775. bool readPixels(const SkPixmap& dst) const {
  776. return this->readPixels(dst, 0, 0);
  777. }
  778. /** Copies a SkRect of pixels from src. Copy starts at (dstX, dstY), and does not exceed
  779. (src.width(), src.height()).
  780. src specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage,
  781. and row bytes of source. src.rowBytes() specifics the gap from one source
  782. row to the next. Returns true if pixels are copied. Returns false if:
  783. - src pixel storage equals nullptr
  784. - src.rowBytes is less than SkImageInfo::minRowBytes()
  785. - SkPixelRef is nullptr
  786. Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is
  787. kGray_8_SkColorType, or kAlpha_8_SkColorType; src SkColorType must match.
  788. If SkBitmap colorType() is kGray_8_SkColorType, src SkColorSpace must match.
  789. If SkBitmap alphaType() is kOpaque_SkAlphaType, src SkAlphaType must
  790. match. If SkBitmap colorSpace() is nullptr, src SkColorSpace must match. Returns
  791. false if pixel conversion is not possible.
  792. dstX and dstY may be negative to copy only top or left of source. Returns
  793. false if width() or height() is zero or negative.
  794. Returns false if abs(dstX) >= Bitmap width(), or if abs(dstY) >= Bitmap height().
  795. @param src source SkPixmap: SkImageInfo, pixels, row bytes
  796. @param dstX column index whose absolute value is less than width()
  797. @param dstY row index whose absolute value is less than height()
  798. @return true if src pixels are copied to SkBitmap
  799. */
  800. bool writePixels(const SkPixmap& src, int dstX, int dstY);
  801. /** Copies a SkRect of pixels from src. Copy starts at (0, 0), and does not exceed
  802. (src.width(), src.height()).
  803. src specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage,
  804. and row bytes of source. src.rowBytes() specifics the gap from one source
  805. row to the next. Returns true if pixels are copied. Returns false if:
  806. - src pixel storage equals nullptr
  807. - src.rowBytes is less than SkImageInfo::minRowBytes()
  808. - SkPixelRef is nullptr
  809. Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is
  810. kGray_8_SkColorType, or kAlpha_8_SkColorType; src SkColorType must match.
  811. If SkBitmap colorType() is kGray_8_SkColorType, src SkColorSpace must match.
  812. If SkBitmap alphaType() is kOpaque_SkAlphaType, src SkAlphaType must
  813. match. If SkBitmap colorSpace() is nullptr, src SkColorSpace must match. Returns
  814. false if pixel conversion is not possible.
  815. @param src source SkPixmap: SkImageInfo, pixels, row bytes
  816. @return true if src pixels are copied to SkBitmap
  817. */
  818. bool writePixels(const SkPixmap& src) {
  819. return this->writePixels(src, 0, 0);
  820. }
  821. /** Sets dst to alpha described by pixels. Returns false if dst cannot be written to
  822. or dst pixels cannot be allocated.
  823. Uses HeapAllocator to reserve memory for dst SkPixelRef.
  824. @param dst holds SkPixelRef to fill with alpha layer
  825. @return true if alpha layer was constructed in dst SkPixelRef
  826. */
  827. bool extractAlpha(SkBitmap* dst) const {
  828. return this->extractAlpha(dst, nullptr, nullptr, nullptr);
  829. }
  830. /** Sets dst to alpha described by pixels. Returns false if dst cannot be written to
  831. or dst pixels cannot be allocated.
  832. If paint is not nullptr and contains SkMaskFilter, SkMaskFilter
  833. generates mask alpha from SkBitmap. Uses HeapAllocator to reserve memory for dst
  834. SkPixelRef. Sets offset to top-left position for dst for alignment with SkBitmap;
  835. (0, 0) unless SkMaskFilter generates mask.
  836. @param dst holds SkPixelRef to fill with alpha layer
  837. @param paint holds optional SkMaskFilter; may be nullptr
  838. @param offset top-left position for dst; may be nullptr
  839. @return true if alpha layer was constructed in dst SkPixelRef
  840. */
  841. bool extractAlpha(SkBitmap* dst, const SkPaint* paint,
  842. SkIPoint* offset) const {
  843. return this->extractAlpha(dst, paint, nullptr, offset);
  844. }
  845. /** Sets dst to alpha described by pixels. Returns false if dst cannot be written to
  846. or dst pixels cannot be allocated.
  847. If paint is not nullptr and contains SkMaskFilter, SkMaskFilter
  848. generates mask alpha from SkBitmap. allocator may reference a custom allocation
  849. class or be set to nullptr to use HeapAllocator. Sets offset to top-left
  850. position for dst for alignment with SkBitmap; (0, 0) unless SkMaskFilter generates
  851. mask.
  852. @param dst holds SkPixelRef to fill with alpha layer
  853. @param paint holds optional SkMaskFilter; may be nullptr
  854. @param allocator function to reserve memory for SkPixelRef; may be nullptr
  855. @param offset top-left position for dst; may be nullptr
  856. @return true if alpha layer was constructed in dst SkPixelRef
  857. */
  858. bool extractAlpha(SkBitmap* dst, const SkPaint* paint, Allocator* allocator,
  859. SkIPoint* offset) const;
  860. /** Copies SkBitmap pixel address, row bytes, and SkImageInfo to pixmap, if address
  861. is available, and returns true. If pixel address is not available, return
  862. false and leave pixmap unchanged.
  863. pixmap contents become invalid on any future change to SkBitmap.
  864. @param pixmap storage for pixel state if pixels are readable; otherwise, ignored
  865. @return true if SkBitmap has direct access to pixels
  866. */
  867. bool peekPixels(SkPixmap* pixmap) const;
  868. /** Asserts if internal values are illegal or inconsistent. Only available if
  869. SK_DEBUG is defined at compile time.
  870. */
  871. SkDEBUGCODE(void validate() const;)
  872. /** \class SkBitmap::Allocator
  873. Abstract subclass of HeapAllocator.
  874. */
  875. class Allocator : public SkRefCnt {
  876. public:
  877. /** Allocates the pixel memory for the bitmap, given its dimensions and
  878. SkColorType. Returns true on success, where success means either setPixels()
  879. or setPixelRef() was called.
  880. @param bitmap SkBitmap containing SkImageInfo as input, and SkPixelRef as output
  881. @return true if SkPixelRef was allocated
  882. */
  883. virtual bool allocPixelRef(SkBitmap* bitmap) = 0;
  884. private:
  885. typedef SkRefCnt INHERITED;
  886. };
  887. /** \class SkBitmap::HeapAllocator
  888. Subclass of SkBitmap::Allocator that returns a SkPixelRef that allocates its pixel
  889. memory from the heap. This is the default SkBitmap::Allocator invoked by
  890. allocPixels().
  891. */
  892. class HeapAllocator : public Allocator {
  893. public:
  894. /** Allocates the pixel memory for the bitmap, given its dimensions and
  895. SkColorType. Returns true on success, where success means either setPixels()
  896. or setPixelRef() was called.
  897. @param bitmap SkBitmap containing SkImageInfo as input, and SkPixelRef as output
  898. @return true if pixels are allocated
  899. */
  900. bool allocPixelRef(SkBitmap* bitmap) override;
  901. };
  902. private:
  903. enum Flags {
  904. kImageIsVolatile_Flag = 0x02,
  905. };
  906. sk_sp<SkPixelRef> fPixelRef;
  907. SkPixmap fPixmap;
  908. uint8_t fFlags;
  909. friend class SkReadBuffer; // unflatten
  910. };
  911. ///////////////////////////////////////////////////////////////////////////////
  912. inline uint32_t* SkBitmap::getAddr32(int x, int y) const {
  913. SkASSERT(fPixmap.addr());
  914. return fPixmap.writable_addr32(x, y);
  915. }
  916. inline uint16_t* SkBitmap::getAddr16(int x, int y) const {
  917. SkASSERT(fPixmap.addr());
  918. return fPixmap.writable_addr16(x, y);
  919. }
  920. inline uint8_t* SkBitmap::getAddr8(int x, int y) const {
  921. SkASSERT(fPixmap.addr());
  922. return fPixmap.writable_addr8(x, y);
  923. }
  924. #endif