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