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