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