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