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