SkBitmap.h 49 KB

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