Debian Codegraph @

include/core/SkBitmap.h

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