blob: 7b3cce50769080a42534b6524ea16b04f59d2c30 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkBitmap_DEFINED
9#define SkBitmap_DEFINED
10
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkColor.h"
djsollen@google.com64a0ec32012-06-12 15:17:27 +000012#include "SkColorTable.h"
reed@google.com3443fd82013-11-13 19:09:13 +000013#include "SkImageInfo.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkPoint.h"
15#include "SkRefCnt.h"
16
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000017struct SkMask;
reed@android.com8a1c16f2008-12-17 15:59:43 +000018struct SkIRect;
reed@google.comfb2d3c62012-03-15 21:18:11 +000019struct SkRect;
reed@android.com8a1c16f2008-12-17 15:59:43 +000020class SkPaint;
21class SkPixelRef;
reed@google.com9ebcac52014-01-24 18:53:42 +000022class SkPixelRefFactory;
reed@android.com8a1c16f2008-12-17 15:59:43 +000023class SkRegion;
robertphillips@google.com76f9e932013-01-15 20:17:47 +000024class SkString;
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +000025class GrTexture;
reed@android.comce4e53a2010-09-09 16:01:26 +000026
reed@android.com8a1c16f2008-12-17 15:59:43 +000027/** \class SkBitmap
28
29 The SkBitmap class specifies a raster bitmap. A bitmap has an integer width
reedddd014e2014-06-05 08:51:20 -070030 and height, and a format (colortype), and a pointer to the actual pixels.
tomhudson@google.com4b33d282011-04-27 15:39:30 +000031 Bitmaps can be drawn into a SkCanvas, but they are also used to specify the
32 target of a SkCanvas' drawing operations.
33 A const SkBitmap exposes getAddr(), which lets a caller write its pixels;
34 the constness is considered to apply to the bitmap's configuration, not
35 its contents.
reed@android.com8a1c16f2008-12-17 15:59:43 +000036*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000037class SK_API SkBitmap {
reed@android.com8a1c16f2008-12-17 15:59:43 +000038public:
commit-bot@chromium.orgeddb02c2013-11-25 15:44:37 +000039 class SK_API Allocator;
reed@android.com8a1c16f2008-12-17 15:59:43 +000040
reed@google.com5dd510f2012-05-16 13:04:22 +000041 /**
42 * Default construct creates a bitmap with zero width and height, and no pixels.
reedddd014e2014-06-05 08:51:20 -070043 * Its colortype is set to kUnknown_SkColorType.
reed@google.com5dd510f2012-05-16 13:04:22 +000044 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 SkBitmap();
reed@google.com5dd510f2012-05-16 13:04:22 +000046
47 /**
48 * Copy the settings from the src into this bitmap. If the src has pixels
49 * allocated, they will be shared, not copied, so that the two bitmaps will
50 * reference the same memory for the pixels. If a deep copy is needed,
51 * where the new bitmap has its own separate copy of the pixels, use
52 * deepCopyTo().
53 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 SkBitmap(const SkBitmap& src);
reed@google.com5dd510f2012-05-16 13:04:22 +000055
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 ~SkBitmap();
57
58 /** Copies the src bitmap into this bitmap. Ownership of the src bitmap's pixels remains
59 with the src bitmap.
60 */
61 SkBitmap& operator=(const SkBitmap& src);
62 /** Swap the fields of the two bitmaps. This routine is guaranteed to never fail or throw.
63 */
64 // This method is not exported to java.
65 void swap(SkBitmap& other);
weita@google.comf9ab99a2009-05-03 18:23:30 +000066
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000067 ///////////////////////////////////////////////////////////////////////////
68
69 const SkImageInfo& info() const { return fInfo; }
70
reede5ea5002014-09-03 11:54:58 -070071 int width() const { return fInfo.width(); }
72 int height() const { return fInfo.height(); }
73 SkColorType colorType() const { return fInfo.colorType(); }
74 SkAlphaType alphaType() const { return fInfo.alphaType(); }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000075
reedddd014e2014-06-05 08:51:20 -070076 /**
77 * Return the number of bytes per pixel based on the colortype. If the colortype is
78 * kUnknown_SkColorType, then 0 is returned.
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000079 */
80 int bytesPerPixel() const { return fInfo.bytesPerPixel(); }
81
reedddd014e2014-06-05 08:51:20 -070082 /**
83 * Return the rowbytes expressed as a number of pixels (like width and height).
84 * If the colortype is kUnknown_SkColorType, then 0 is returned.
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000085 */
86 int rowBytesAsPixels() const {
87 return fRowBytes >> this->shiftPerPixel();
88 }
89
reedddd014e2014-06-05 08:51:20 -070090 /**
91 * Return the shift amount per pixel (i.e. 0 for 1-byte per pixel, 1 for 2-bytes per pixel
92 * colortypes, 2 for 4-bytes per pixel colortypes). Return 0 for kUnknown_SkColorType.
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000093 */
94 int shiftPerPixel() const { return this->bytesPerPixel() >> 1; }
95
96 ///////////////////////////////////////////////////////////////////////////
97
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 /** Return true iff the bitmap has empty dimensions.
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +000099 * Hey! Before you use this, see if you really want to know drawsNothing() instead.
100 */
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000101 bool empty() const { return fInfo.isEmpty(); }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000102
djsollen@google.comc84b8332012-07-27 13:41:44 +0000103 /** Return true iff the bitmap has no pixelref. Note: this can return true even if the
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +0000104 * dimensions of the bitmap are > 0 (see empty()).
105 * Hey! Before you use this, see if you really want to know drawsNothing() instead.
106 */
djsollen@google.comc84b8332012-07-27 13:41:44 +0000107 bool isNull() const { return NULL == fPixelRef; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +0000109 /** Return true iff drawing this bitmap has no effect.
110 */
111 bool drawsNothing() const { return this->empty() || this->isNull(); }
112
reed@google.com44699382013-10-31 17:28:30 +0000113 /** Return the number of bytes between subsequent rows of the bitmap. */
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000114 size_t rowBytes() const { return fRowBytes; }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000115
reed@google.com383a6972013-10-21 14:00:07 +0000116 /**
117 * Set the bitmap's alphaType, returning true on success. If false is
118 * returned, then the specified new alphaType is incompatible with the
reedddd014e2014-06-05 08:51:20 -0700119 * colortype, and the current alphaType is unchanged.
commit-bot@chromium.org0e8d0d62014-01-27 15:41:07 +0000120 *
121 * Note: this changes the alphatype for the underlying pixels, which means
122 * that all bitmaps that might be sharing (subsets of) the pixels will
reed@google.comc1587f92014-01-28 16:05:39 +0000123 * be affected.
reed@google.com383a6972013-10-21 14:00:07 +0000124 */
125 bool setAlphaType(SkAlphaType);
126
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 /** Return the address of the pixels for this SkBitmap.
128 */
129 void* getPixels() const { return fPixels; }
130
131 /** Return the byte size of the pixels, based on the height and rowBytes.
132 Note this truncates the result to 32bits. Call getSize64() to detect
133 if the real size exceeds 32bits.
134 */
reede5ea5002014-09-03 11:54:58 -0700135 size_t getSize() const { return fInfo.height() * fRowBytes; }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000136
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000137 /** Return the number of bytes from the pointer returned by getPixels()
138 to the end of the allocated space in the buffer. Required in
epoger@google.comd27fe342012-12-07 15:27:27 +0000139 cases where extractSubset has been called.
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000140 */
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000141 size_t getSafeSize() const { return fInfo.getSafeSize(fRowBytes); }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000142
reed@google.com57212f92013-12-30 14:40:38 +0000143 /**
144 * Return the full size of the bitmap, in bytes.
145 */
146 int64_t computeSize64() const {
reede5ea5002014-09-03 11:54:58 -0700147 return sk_64_mul(fInfo.height(), fRowBytes);
reed@google.com57212f92013-12-30 14:40:38 +0000148 }
149
150 /**
151 * Return the number of bytes from the pointer returned by getPixels()
152 * to the end of the allocated space in the buffer. This may be smaller
153 * than computeSize64() if there is any rowbytes padding beyond the width.
154 */
155 int64_t computeSafeSize64() const {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000156 return fInfo.getSafeSize64(fRowBytes);
reed@google.com57212f92013-12-30 14:40:38 +0000157 }
158
junov@chromium.orgb0521292011-12-15 20:14:06 +0000159 /** Returns true if this bitmap is marked as immutable, meaning that the
160 contents of its pixels will not change for the lifetime of the bitmap.
161 */
162 bool isImmutable() const;
163
164 /** Marks this bitmap as immutable, meaning that the contents of its
165 pixels will not change for the lifetime of the bitmap and of the
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000166 underlying pixelref. This state can be set, but it cannot be
junov@chromium.orgb0521292011-12-15 20:14:06 +0000167 cleared once it is set. This state propagates to all other bitmaps
168 that share the same pixelref.
169 */
170 void setImmutable();
171
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 /** Returns true if the bitmap is opaque (has no translucent/transparent pixels).
173 */
reed@google.com383a6972013-10-21 14:00:07 +0000174 bool isOpaque() const {
175 return SkAlphaTypeIsOpaque(this->alphaType());
176 }
junov@google.com4ee7ae52011-06-30 17:30:49 +0000177
junov@google.com4ee7ae52011-06-30 17:30:49 +0000178 /** Returns true if the bitmap is volatile (i.e. should not be cached by devices.)
179 */
180 bool isVolatile() const;
181
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000182 /** Specify whether this bitmap is volatile. Bitmaps are not volatile by
junov@google.com4ee7ae52011-06-30 17:30:49 +0000183 default. Temporary bitmaps that are discarded after use should be
184 marked as volatile. This provides a hint to the device that the bitmap
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000185 should not be cached. Providing this hint when appropriate can
186 improve performance by avoiding unnecessary overhead and resource
junov@google.com4ee7ae52011-06-30 17:30:49 +0000187 consumption on the device.
188 */
189 void setIsVolatile(bool);
190
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191 /** Reset the bitmap to its initial state (see default constructor). If we are a (shared)
192 owner of the pixels, that ownership is decremented.
193 */
194 void reset();
195
reed@google.com86b2e432012-03-15 21:17:03 +0000196 /**
reed@google.com2a7579d2012-11-07 18:30:18 +0000197 * This will brute-force return true if all of the pixels in the bitmap
198 * are opaque. If it fails to read the pixels, or encounters an error,
199 * it will return false.
200 *
201 * Since this can be an expensive operation, the bitmap stores a flag for
commit-bot@chromium.org21a0b102013-11-12 18:16:34 +0000202 * this (isOpaque). Only call this if you need to compute this value from
203 * "unknown" pixels.
reed@google.com2a7579d2012-11-07 18:30:18 +0000204 */
205 static bool ComputeIsOpaque(const SkBitmap&);
206
207 /**
reed@google.com86b2e432012-03-15 21:17:03 +0000208 * Return the bitmap's bounds [0, 0, width, height] as an SkRect
209 */
210 void getBounds(SkRect* bounds) const;
reed@google.com80e14592012-03-16 14:58:07 +0000211 void getBounds(SkIRect* bounds) const;
reed@google.com86b2e432012-03-15 21:17:03 +0000212
halcanaryf622a6c2014-10-24 12:54:53 -0700213 SkIRect bounds() const { return fInfo.bounds(); }
214 SkISize dimensions() const { return fInfo.dimensions(); }
215
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000216 bool setInfo(const SkImageInfo&, size_t rowBytes = 0);
217
reed@google.com9230ea22013-12-09 22:01:03 +0000218 /**
reedbae704b2014-06-28 14:26:35 -0700219 * Allocate the bitmap's pixels to match the requested image info. If the Factory
reed@google.com9ebcac52014-01-24 18:53:42 +0000220 * is non-null, call it to allcoate the pixelref. If the ImageInfo requires
221 * a colortable, then ColorTable must be non-null, and will be ref'd.
222 * On failure, the bitmap will be set to empty and return false.
223 */
reed84825042014-09-02 12:50:45 -0700224 bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo&, SkPixelRefFactory*, SkColorTable*);
225
reed68331cd2014-12-18 12:48:16 -0800226 void allocPixels(const SkImageInfo& info, SkPixelRefFactory* factory, SkColorTable* ctable) {
reed84825042014-09-02 12:50:45 -0700227 if (!this->tryAllocPixels(info, factory, ctable)) {
reed68331cd2014-12-18 12:48:16 -0800228 sk_throw();
reed84825042014-09-02 12:50:45 -0700229 }
reed84825042014-09-02 12:50:45 -0700230 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000231
reed@google.com9ebcac52014-01-24 18:53:42 +0000232 /**
reedbae704b2014-06-28 14:26:35 -0700233 * Allocate the bitmap's pixels to match the requested image info and
234 * rowBytes. If the request cannot be met (e.g. the info is invalid or
235 * the requested rowBytes are not compatible with the info
236 * (e.g. rowBytes < info.minRowBytes() or rowBytes is not aligned with
237 * the pixel size specified by info.colorType()) then false is returned
238 * and the bitmap is set to empty.
239 */
reed84825042014-09-02 12:50:45 -0700240 bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info, size_t rowBytes);
reedbae704b2014-06-28 14:26:35 -0700241
reed68331cd2014-12-18 12:48:16 -0800242 void allocPixels(const SkImageInfo& info, size_t rowBytes) {
reed84825042014-09-02 12:50:45 -0700243 if (!this->tryAllocPixels(info, rowBytes)) {
reed68331cd2014-12-18 12:48:16 -0800244 sk_throw();
reed84825042014-09-02 12:50:45 -0700245 }
reed84825042014-09-02 12:50:45 -0700246 }
247
248 bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info) {
249 return this->tryAllocPixels(info, info.minRowBytes());
250 }
251
reed68331cd2014-12-18 12:48:16 -0800252 void allocPixels(const SkImageInfo& info) {
253 this->allocPixels(info, info.minRowBytes());
reed@google.com9ebcac52014-01-24 18:53:42 +0000254 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000255
reed84825042014-09-02 12:50:45 -0700256 bool SK_WARN_UNUSED_RESULT tryAllocN32Pixels(int width, int height, bool isOpaque = false) {
reede5ea5002014-09-03 11:54:58 -0700257 SkImageInfo info = SkImageInfo::MakeN32(width, height,
258 isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
reed84825042014-09-02 12:50:45 -0700259 return this->tryAllocPixels(info);
260 }
mtklein775b8192014-12-02 09:11:25 -0800261
reed68331cd2014-12-18 12:48:16 -0800262 void allocN32Pixels(int width, int height, bool isOpaque = false) {
reede5ea5002014-09-03 11:54:58 -0700263 SkImageInfo info = SkImageInfo::MakeN32(width, height,
264 isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
reed68331cd2014-12-18 12:48:16 -0800265 this->allocPixels(info);
reed@google.comeb9a46c2014-01-25 16:46:20 +0000266 }
mtklein775b8192014-12-02 09:11:25 -0800267
reed@google.comeb9a46c2014-01-25 16:46:20 +0000268 /**
reed@google.com9ebcac52014-01-24 18:53:42 +0000269 * Install a pixelref that wraps the specified pixels and rowBytes, and
270 * optional ReleaseProc and context. When the pixels are no longer
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000271 * referenced, if releaseProc is not null, it will be called with the
reed@google.com9ebcac52014-01-24 18:53:42 +0000272 * pixels and context as parameters.
273 * On failure, the bitmap will be set to empty and return false.
274 */
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000275 bool installPixels(const SkImageInfo&, void* pixels, size_t rowBytes, SkColorTable*,
276 void (*releaseProc)(void* addr, void* context), void* context);
277
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000278 /**
279 * Call installPixels with no ReleaseProc specified. This means that the
280 * caller must ensure that the specified pixels are valid for the lifetime
281 * of the created bitmap (and its pixelRef).
282 */
283 bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000284 return this->installPixels(info, pixels, rowBytes, NULL, NULL, NULL);
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000285 }
286
287 /**
288 * Calls installPixels() with the value in the SkMask. The caller must
289 * ensure that the specified mask pixels are valid for the lifetime
290 * of the created bitmap (and its pixelRef).
291 */
292 bool installMaskPixels(const SkMask&);
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000293
reed@android.com8a1c16f2008-12-17 15:59:43 +0000294 /** Use this to assign a new pixel address for an existing bitmap. This
295 will automatically release any pixelref previously installed. Only call
296 this if you are handling ownership/lifetime of the pixel memory.
weita@google.comf9ab99a2009-05-03 18:23:30 +0000297
reed@android.com8a1c16f2008-12-17 15:59:43 +0000298 If the bitmap retains a reference to the colortable (assuming it is
299 not null) it will take care of incrementing the reference count.
300
301 @param pixels Address for the pixels, managed by the caller.
302 @param ctable ColorTable (or null) that matches the specified pixels
303 */
304 void setPixels(void* p, SkColorTable* ctable = NULL);
305
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000306 /** Copies the bitmap's pixels to the location pointed at by dst and returns
307 true if possible, returns false otherwise.
308
bsalomon@google.comc6980972011-11-02 19:57:21 +0000309 In the case when the dstRowBytes matches the bitmap's rowBytes, the copy
310 may be made faster by copying over the dst's per-row padding (for all
311 rows but the last). By setting preserveDstPad to true the caller can
312 disable this optimization and ensure that pixels in the padding are not
313 overwritten.
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000314
315 Always returns false for RLE formats.
316
317 @param dst Location of destination buffer.
318 @param dstSize Size of destination buffer. Must be large enough to hold
319 pixels using indicated stride.
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000320 @param dstRowBytes Width of each line in the buffer. If 0, uses
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000321 bitmap's internal stride.
bsalomon@google.comc6980972011-11-02 19:57:21 +0000322 @param preserveDstPad Must we preserve padding in the dst
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000323 */
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000324 bool copyPixelsTo(void* const dst, size_t dstSize, size_t dstRowBytes = 0,
325 bool preserveDstPad = false) const;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000326
reed@android.com8a1c16f2008-12-17 15:59:43 +0000327 /** Use the standard HeapAllocator to create the pixelref that manages the
reedddd014e2014-06-05 08:51:20 -0700328 pixel memory. It will be sized based on the current ImageInfo.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000329 If this is called multiple times, a new pixelref object will be created
330 each time.
weita@google.comf9ab99a2009-05-03 18:23:30 +0000331
reed@android.com8a1c16f2008-12-17 15:59:43 +0000332 If the bitmap retains a reference to the colortable (assuming it is
333 not null) it will take care of incrementing the reference count.
334
335 @param ctable ColorTable (or null) to use with the pixels that will
reedddd014e2014-06-05 08:51:20 -0700336 be allocated. Only used if colortype == kIndex_8_SkColorType
reed@android.com8a1c16f2008-12-17 15:59:43 +0000337 @return true if the allocation succeeds. If not the pixelref field of
338 the bitmap will be unchanged.
339 */
reed84825042014-09-02 12:50:45 -0700340 bool SK_WARN_UNUSED_RESULT tryAllocPixels(SkColorTable* ctable = NULL) {
341 return this->tryAllocPixels(NULL, ctable);
342 }
343
reed68331cd2014-12-18 12:48:16 -0800344 void allocPixels(SkColorTable* ctable = NULL) {
345 this->allocPixels(NULL, ctable);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000346 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000347
reed@android.com8a1c16f2008-12-17 15:59:43 +0000348 /** Use the specified Allocator to create the pixelref that manages the
reedddd014e2014-06-05 08:51:20 -0700349 pixel memory. It will be sized based on the current ImageInfo.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000350 If this is called multiple times, a new pixelref object will be created
351 each time.
weita@google.comf9ab99a2009-05-03 18:23:30 +0000352
reed@android.com8a1c16f2008-12-17 15:59:43 +0000353 If the bitmap retains a reference to the colortable (assuming it is
354 not null) it will take care of incrementing the reference count.
weita@google.comf9ab99a2009-05-03 18:23:30 +0000355
reed@android.com8a1c16f2008-12-17 15:59:43 +0000356 @param allocator The Allocator to use to create a pixelref that can
reedddd014e2014-06-05 08:51:20 -0700357 manage the pixel memory for the current ImageInfo.
358 If allocator is NULL, the standard HeapAllocator will be used.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000359 @param ctable ColorTable (or null) to use with the pixels that will
reedddd014e2014-06-05 08:51:20 -0700360 be allocated. Only used if colortype == kIndex_8_SkColorType.
361 If it is non-null and the colortype is not indexed, it will
reed@android.com8a1c16f2008-12-17 15:59:43 +0000362 be ignored.
363 @return true if the allocation succeeds. If not the pixelref field of
364 the bitmap will be unchanged.
365 */
reed84825042014-09-02 12:50:45 -0700366 bool SK_WARN_UNUSED_RESULT tryAllocPixels(Allocator* allocator, SkColorTable* ctable);
367
reed68331cd2014-12-18 12:48:16 -0800368 void allocPixels(Allocator* allocator, SkColorTable* ctable) {
reed84825042014-09-02 12:50:45 -0700369 if (!this->tryAllocPixels(allocator, ctable)) {
reed68331cd2014-12-18 12:48:16 -0800370 sk_throw();
reed84825042014-09-02 12:50:45 -0700371 }
reed84825042014-09-02 12:50:45 -0700372 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000373
reed@google.com672588b2014-01-08 15:42:01 +0000374 /**
375 * Return the current pixelref object or NULL if there is none. This does
376 * not affect the refcount of the pixelref.
377 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000378 SkPixelRef* pixelRef() const { return fPixelRef; }
reed@google.com672588b2014-01-08 15:42:01 +0000379
380 /**
381 * A bitmap can reference a subset of a pixelref's pixels. That means the
382 * bitmap's width/height can be <= the dimensions of the pixelref. The
383 * pixelref origin is the x,y location within the pixelref's pixels for
384 * the bitmap's top/left corner. To be valid the following must be true:
385 *
386 * origin_x + bitmap_width <= pixelref_width
387 * origin_y + bitmap_height <= pixelref_height
388 *
389 * pixelRefOrigin() returns this origin, or (0,0) if there is no pixelRef.
390 */
391 SkIPoint pixelRefOrigin() const { return fPixelRefOrigin; }
392
393 /**
394 * Assign a pixelref and origin to the bitmap. Pixelrefs are reference,
395 * so the existing one (if any) will be unref'd and the new one will be
396 * ref'd. (x,y) specify the offset within the pixelref's pixels for the
397 * top/left corner of the bitmap. For a bitmap that encompases the entire
398 * pixels of the pixelref, these will be (0,0).
399 */
400 SkPixelRef* setPixelRef(SkPixelRef* pr, int dx, int dy);
401
402 SkPixelRef* setPixelRef(SkPixelRef* pr, const SkIPoint& origin) {
403 return this->setPixelRef(pr, origin.fX, origin.fY);
404 }
skia.committer@gmail.com1ae91112014-01-09 07:01:42 +0000405
reed@google.com672588b2014-01-08 15:42:01 +0000406 SkPixelRef* setPixelRef(SkPixelRef* pr) {
407 return this->setPixelRef(pr, 0, 0);
408 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000409
reed@android.com8a1c16f2008-12-17 15:59:43 +0000410 /** Call this to ensure that the bitmap points to the current pixel address
411 in the pixelref. Balance it with a call to unlockPixels(). These calls
412 are harmless if there is no pixelref.
413 */
414 void lockPixels() const;
415 /** When you are finished access the pixel memory, call this to balance a
416 previous call to lockPixels(). This allows pixelrefs that implement
417 cached/deferred image decoding to know when there are active clients of
418 a given image.
419 */
420 void unlockPixels() const;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000421
reed@google.com9c49bc32011-07-07 13:42:37 +0000422 /**
423 * Some bitmaps can return a copy of their pixels for lockPixels(), but
424 * that copy, if modified, will not be pushed back. These bitmaps should
425 * not be used as targets for a raster device/canvas (since all pixels
426 * modifications will be lost when unlockPixels() is called.)
427 */
428 bool lockPixelsAreWritable() const;
429
reed@android.com8a1c16f2008-12-17 15:59:43 +0000430 /** Call this to be sure that the bitmap is valid enough to be drawn (i.e.
reedddd014e2014-06-05 08:51:20 -0700431 it has non-null pixels, and if required by its colortype, it has a
reed@android.com8a1c16f2008-12-17 15:59:43 +0000432 non-null colortable. Returns true if all of the above are met.
433 */
434 bool readyToDraw() const {
435 return this->getPixels() != NULL &&
bsalomon49f085d2014-09-05 13:34:00 -0700436 (this->colorType() != kIndex_8_SkColorType || fColorTable);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000437 }
438
reed@android.comce4e53a2010-09-09 16:01:26 +0000439 /** Returns the pixelRef's texture, or NULL
440 */
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000441 GrTexture* getTexture() const;
reed@android.comce4e53a2010-09-09 16:01:26 +0000442
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000443 /** Return the bitmap's colortable, if it uses one (i.e. colorType is
444 Index_8) and the pixels are locked.
scroggo@google.com665b2cc2013-05-06 18:08:05 +0000445 Otherwise returns NULL. Does not affect the colortable's
reed@android.com8a1c16f2008-12-17 15:59:43 +0000446 reference count.
447 */
448 SkColorTable* getColorTable() const { return fColorTable; }
449
450 /** Returns a non-zero, unique value corresponding to the pixels in our
djsollen@google.comc84b8332012-07-27 13:41:44 +0000451 pixelref. Each time the pixels are changed (and notifyPixelsChanged
452 is called), a different generation ID will be returned. Finally, if
scroggo58edea82014-06-05 07:49:04 -0700453 there is no pixelRef then zero is returned.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000454 */
455 uint32_t getGenerationID() const;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000456
reed@android.com8a1c16f2008-12-17 15:59:43 +0000457 /** Call this if you have changed the contents of the pixels. This will in-
458 turn cause a different generation ID value to be returned from
459 getGenerationID().
460 */
461 void notifyPixelsChanged() const;
462
reed@google.com60d32352013-06-28 19:40:50 +0000463 /**
464 * Fill the entire bitmap with the specified color.
reedddd014e2014-06-05 08:51:20 -0700465 * If the bitmap's colortype does not support alpha (e.g. 565) then the alpha
466 * of the color is ignored (treated as opaque). If the colortype only supports
reed@google.com60d32352013-06-28 19:40:50 +0000467 * alpha (e.g. A1 or A8) then the color's r,g,b components are ignored.
468 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000469 void eraseColor(SkColor c) const {
470 this->eraseARGB(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c),
471 SkColorGetB(c));
472 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000473
reed@google.com60d32352013-06-28 19:40:50 +0000474 /**
475 * Fill the entire bitmap with the specified color.
reedddd014e2014-06-05 08:51:20 -0700476 * If the bitmap's colortype does not support alpha (e.g. 565) then the alpha
477 * of the color is ignored (treated as opaque). If the colortype only supports
reed@google.com60d32352013-06-28 19:40:50 +0000478 * alpha (e.g. A1 or A8) then the color's r,g,b components are ignored.
479 */
480 void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const;
481
reed@google.com44699382013-10-31 17:28:30 +0000482 SK_ATTR_DEPRECATED("use eraseARGB or eraseColor")
reed@google.com60d32352013-06-28 19:40:50 +0000483 void eraseRGB(U8CPU r, U8CPU g, U8CPU b) const {
484 this->eraseARGB(0xFF, r, g, b);
485 }
486
487 /**
488 * Fill the specified area of this bitmap with the specified color.
reedddd014e2014-06-05 08:51:20 -0700489 * If the bitmap's colortype does not support alpha (e.g. 565) then the alpha
490 * of the color is ignored (treated as opaque). If the colortype only supports
reed@google.com60d32352013-06-28 19:40:50 +0000491 * alpha (e.g. A1 or A8) then the color's r,g,b components are ignored.
492 */
493 void eraseArea(const SkIRect& area, SkColor c) const;
494
reed@android.com8a1c16f2008-12-17 15:59:43 +0000495 /** Scroll (a subset of) the contents of this bitmap by dx/dy. If there are
496 no pixels allocated (i.e. getPixels() returns null) the method will
scroggo@google.com6a9368d2012-08-22 16:19:52 +0000497 still update the inval region (if present). If the bitmap is immutable,
498 do nothing and return false.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000499
500 @param subset The subset of the bitmap to scroll/move. To scroll the
501 entire contents, specify [0, 0, width, height] or just
502 pass null.
503 @param dx The amount to scroll in X
504 @param dy The amount to scroll in Y
505 @param inval Optional (may be null). Returns the area of the bitmap that
506 was scrolled away. E.g. if dx = dy = 0, then inval would
507 be set to empty. If dx >= width or dy >= height, then
508 inval would be set to the entire bounds of the bitmap.
reedddd014e2014-06-05 08:51:20 -0700509 @return true if the scroll was doable. Will return false if the colortype is kUnkown or
510 if the bitmap is immutable.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000511 If no pixels are present (i.e. getPixels() returns false)
512 inval will still be updated, and true will be returned.
513 */
514 bool scrollRect(const SkIRect* subset, int dx, int dy,
515 SkRegion* inval = NULL) const;
516
reed@google.com3b521d02011-04-29 11:53:41 +0000517 /**
518 * Return the SkColor of the specified pixel. In most cases this will
reedddd014e2014-06-05 08:51:20 -0700519 * require un-premultiplying the color. Alpha only colortypes (e.g. kAlpha_8_SkColorType)
reed@google.com3b521d02011-04-29 11:53:41 +0000520 * return black with the appropriate alpha set. The value is undefined
reedddd014e2014-06-05 08:51:20 -0700521 * for kUnknown_SkColorType or if x or y are out of bounds, or if the bitmap
reed@google.com3b521d02011-04-29 11:53:41 +0000522 * does not have any pixels (or has not be locked with lockPixels()).
523 */
524 SkColor getColor(int x, int y) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000525
reed@android.com8a1c16f2008-12-17 15:59:43 +0000526 /** Returns the address of the specified pixel. This performs a runtime
527 check to know the size of the pixels, and will return the same answer
528 as the corresponding size-specific method (e.g. getAddr16). Since the
529 check happens at runtime, it is much slower than using a size-specific
530 version. Unlike the size-specific methods, this routine also checks if
531 getPixels() returns null, and returns that. The size-specific routines
532 perform a debugging assert that getPixels() is not null, but they do
533 not do any runtime checks.
534 */
535 void* getAddr(int x, int y) const;
536
537 /** Returns the address of the pixel specified by x,y for 32bit pixels.
reed@google.com3b521d02011-04-29 11:53:41 +0000538 * In debug build, this asserts that the pixels are allocated and locked,
reedddd014e2014-06-05 08:51:20 -0700539 * and that the colortype is 32-bit, however none of these checks are performed
reed@google.com3b521d02011-04-29 11:53:41 +0000540 * in the release build.
541 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000542 inline uint32_t* getAddr32(int x, int y) const;
reed@google.com3b521d02011-04-29 11:53:41 +0000543
reed@android.com8a1c16f2008-12-17 15:59:43 +0000544 /** Returns the address of the pixel specified by x,y for 16bit pixels.
reed@google.com3b521d02011-04-29 11:53:41 +0000545 * In debug build, this asserts that the pixels are allocated and locked,
reedddd014e2014-06-05 08:51:20 -0700546 * and that the colortype is 16-bit, however none of these checks are performed
reed@google.com3b521d02011-04-29 11:53:41 +0000547 * in the release build.
548 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000549 inline uint16_t* getAddr16(int x, int y) const;
reed@google.com3b521d02011-04-29 11:53:41 +0000550
reed@android.com8a1c16f2008-12-17 15:59:43 +0000551 /** Returns the address of the pixel specified by x,y for 8bit pixels.
reed@google.com3b521d02011-04-29 11:53:41 +0000552 * In debug build, this asserts that the pixels are allocated and locked,
reedddd014e2014-06-05 08:51:20 -0700553 * and that the colortype is 8-bit, however none of these checks are performed
reed@google.com3b521d02011-04-29 11:53:41 +0000554 * in the release build.
555 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000556 inline uint8_t* getAddr8(int x, int y) const;
reed@google.com3b521d02011-04-29 11:53:41 +0000557
reed@android.com8a1c16f2008-12-17 15:59:43 +0000558 /** Returns the color corresponding to the pixel specified by x,y for
reed@google.com3b521d02011-04-29 11:53:41 +0000559 * colortable based bitmaps.
560 * In debug build, this asserts that the pixels are allocated and locked,
reedddd014e2014-06-05 08:51:20 -0700561 * that the colortype is indexed, and that the colortable is allocated,
reed@google.com3b521d02011-04-29 11:53:41 +0000562 * however none of these checks are performed in the release build.
563 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000564 inline SkPMColor getIndex8Color(int x, int y) const;
565
epoger@google.com9ef2d832011-07-01 21:12:20 +0000566 /** Set dst to be a setset of this bitmap. If possible, it will share the
reedddd014e2014-06-05 08:51:20 -0700567 pixel memory, and just point into a subset of it. However, if the colortype
reed@android.com8a1c16f2008-12-17 15:59:43 +0000568 does not support this, a local copy will be made and associated with
569 the dst bitmap. If the subset rectangle, intersected with the bitmap's
reedddd014e2014-06-05 08:51:20 -0700570 dimensions is empty, or if there is an unsupported colortype, false will be
reed@android.com8a1c16f2008-12-17 15:59:43 +0000571 returned and dst will be untouched.
572 @param dst The bitmap that will be set to a subset of this bitmap
573 @param subset The rectangle of pixels in this bitmap that dst will
574 reference.
575 @return true if the subset copy was successfully made.
576 */
577 bool extractSubset(SkBitmap* dst, const SkIRect& subset) const;
578
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000579 /** Makes a deep copy of this bitmap, respecting the requested colorType,
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000580 * and allocating the dst pixels on the cpu.
581 * Returns false if either there is an error (i.e. the src does not have
582 * pixels) or the request cannot be satisfied (e.g. the src has per-pixel
reedddd014e2014-06-05 08:51:20 -0700583 * alpha, and the requested colortype does not support alpha).
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000584 * @param dst The bitmap to be sized and allocated
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000585 * @param ct The desired colorType for dst
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000586 * @param allocator Allocator used to allocate the pixelref for the dst
587 * bitmap. If this is null, the standard HeapAllocator
588 * will be used.
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000589 * @return true if the copy was made.
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000590 */
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000591 bool copyTo(SkBitmap* dst, SkColorType ct, Allocator* = NULL) const;
592
593 bool copyTo(SkBitmap* dst, Allocator* allocator = NULL) const {
594 return this->copyTo(dst, this->colorType(), allocator);
595 }
596
597 /**
reedb184f7f2014-07-13 04:32:32 -0700598 * Copy the bitmap's pixels into the specified buffer (pixels + rowBytes),
599 * converting them into the requested format (SkImageInfo). The src pixels are read
600 * starting at the specified (srcX,srcY) offset, relative to the top-left corner.
601 *
602 * The specified ImageInfo and (srcX,srcY) offset specifies a source rectangle
603 *
604 * srcR.setXYWH(srcX, srcY, dstInfo.width(), dstInfo.height());
605 *
606 * srcR is intersected with the bounds of the bitmap. If this intersection is not empty,
607 * then we have two sets of pixels (of equal size). Replace the dst pixels with the
608 * corresponding src pixels, performing any colortype/alphatype transformations needed
609 * (in the case where the src and dst have different colortypes or alphatypes).
610 *
611 * This call can fail, returning false, for several reasons:
612 * - If srcR does not intersect the bitmap bounds.
613 * - If the requested colortype/alphatype cannot be converted from the src's types.
614 * - If the src pixels are not available.
615 */
616 bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
617 int srcX, int srcY) const;
618
619 /**
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000620 * Returns true if this bitmap's pixels can be converted into the requested
621 * colorType, such that copyTo() could succeed.
622 */
623 bool canCopyTo(SkColorType colorType) const;
reed@android.com89bb83a2009-05-29 21:30:42 +0000624
commit-bot@chromium.org6285f4f2014-02-20 19:08:07 +0000625 /** Makes a deep copy of this bitmap, keeping the copied pixels
626 * in the same domain as the source: If the src pixels are allocated for
627 * the cpu, then so will the dst. If the src pixels are allocated on the
628 * gpu (typically as a texture), the it will do the same for the dst.
629 * If the request cannot be fulfilled, returns false and dst is unmodified.
630 */
631 bool deepCopyTo(SkBitmap* dst) const;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000632
djsollen@google.com4bd2bdb2013-03-08 18:35:13 +0000633#ifdef SK_BUILD_FOR_ANDROID
634 bool hasHardwareMipMap() const {
635 return (fFlags & kHasHardwareMipMap_Flag) != 0;
636 }
637
638 void setHasHardwareMipMap(bool hasHardwareMipMap) {
639 if (hasHardwareMipMap) {
640 fFlags |= kHasHardwareMipMap_Flag;
641 } else {
642 fFlags &= ~kHasHardwareMipMap_Flag;
643 }
644 }
645#endif
646
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000647 bool extractAlpha(SkBitmap* dst) const {
648 return this->extractAlpha(dst, NULL, NULL, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000649 }
650
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000651 bool extractAlpha(SkBitmap* dst, const SkPaint* paint,
djsollen@google.com57f49692011-02-23 20:46:31 +0000652 SkIPoint* offset) const {
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000653 return this->extractAlpha(dst, paint, NULL, offset);
djsollen@google.com57f49692011-02-23 20:46:31 +0000654 }
655
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000656 /** Set dst to contain alpha layer of this bitmap. If destination bitmap
657 fails to be initialized, e.g. because allocator can't allocate pixels
658 for it, dst will not be modified and false will be returned.
659
660 @param dst The bitmap to be filled with alpha layer
661 @param paint The paint to draw with
662 @param allocator Allocator used to allocate the pixelref for the dst
663 bitmap. If this is null, the standard HeapAllocator
664 will be used.
665 @param offset If not null, it is set to top-left coordinate to position
666 the returned bitmap so that it visually lines up with the
667 original
668 */
669 bool extractAlpha(SkBitmap* dst, const SkPaint* paint, Allocator* allocator,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000670 SkIPoint* offset) const;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000671
reed@android.com8a1c16f2008-12-17 15:59:43 +0000672 SkDEBUGCODE(void validate() const;)
673
674 class Allocator : public SkRefCnt {
675 public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000676 SK_DECLARE_INST_COUNT(Allocator)
677
reed@android.com8a1c16f2008-12-17 15:59:43 +0000678 /** Allocate the pixel memory for the bitmap, given its dimensions and
reedddd014e2014-06-05 08:51:20 -0700679 colortype. Return true on success, where success means either setPixels
reed@android.com8a1c16f2008-12-17 15:59:43 +0000680 or setPixelRef was called. The pixels need not be locked when this
reedddd014e2014-06-05 08:51:20 -0700681 returns. If the colortype requires a colortable, it also must be
reed@android.com8a1c16f2008-12-17 15:59:43 +0000682 installed via setColorTable. If false is returned, the bitmap and
683 colortable should be left unchanged.
684 */
685 virtual bool allocPixelRef(SkBitmap*, SkColorTable*) = 0;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000686 private:
687 typedef SkRefCnt INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000688 };
689
690 /** Subclass of Allocator that returns a pixelref that allocates its pixel
691 memory from the heap. This is the default Allocator invoked by
692 allocPixels().
693 */
694 class HeapAllocator : public Allocator {
695 public:
reed@google.com9ebcac52014-01-24 18:53:42 +0000696 virtual bool allocPixelRef(SkBitmap*, SkColorTable*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000697 };
698
699 class RLEPixels {
700 public:
701 RLEPixels(int width, int height);
702 virtual ~RLEPixels();
weita@google.comf9ab99a2009-05-03 18:23:30 +0000703
reed@android.com8a1c16f2008-12-17 15:59:43 +0000704 uint8_t* packedAtY(int y) const {
705 SkASSERT((unsigned)y < (unsigned)fHeight);
706 return fYPtrs[y];
707 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000708
reed@android.com8a1c16f2008-12-17 15:59:43 +0000709 // called by subclasses during creation
710 void setPackedAtY(int y, uint8_t* addr) {
711 SkASSERT((unsigned)y < (unsigned)fHeight);
712 fYPtrs[y] = addr;
713 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000714
reed@android.com8a1c16f2008-12-17 15:59:43 +0000715 private:
716 uint8_t** fYPtrs;
717 int fHeight;
718 };
weita@google.comf9ab99a2009-05-03 18:23:30 +0000719
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000720 SK_TO_STRING_NONVIRT()
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000721
reed@android.com8a1c16f2008-12-17 15:59:43 +0000722private:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000723 mutable SkPixelRef* fPixelRef;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000724 mutable int fPixelLockCount;
reed@google.com5f62ed72014-01-15 19:59:45 +0000725 // These are just caches from the locked pixelref
reed@android.com8a1c16f2008-12-17 15:59:43 +0000726 mutable void* fPixels;
727 mutable SkColorTable* fColorTable; // only meaningful for kIndex8
728
reed@google.com672588b2014-01-08 15:42:01 +0000729 SkIPoint fPixelRefOrigin;
730
reed@android.com8a1c16f2008-12-17 15:59:43 +0000731 enum Flags {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000732 kImageIsVolatile_Flag = 0x02,
djsollen@google.com4bd2bdb2013-03-08 18:35:13 +0000733#ifdef SK_BUILD_FOR_ANDROID
734 /* A hint for the renderer responsible for drawing this bitmap
735 * indicating that it should attempt to use mipmaps when this bitmap
736 * is drawn scaled down.
737 */
738 kHasHardwareMipMap_Flag = 0x08,
739#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000740 };
741
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000742 SkImageInfo fInfo;
743
scroggo@google.come5f48242013-02-25 21:47:41 +0000744 uint32_t fRowBytes;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000745
reed@android.com8a1c16f2008-12-17 15:59:43 +0000746 uint8_t fFlags;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000747
reed@google.com60d32352013-06-28 19:40:50 +0000748 void internalErase(const SkIRect&, U8CPU a, U8CPU r, U8CPU g, U8CPU b)const;
749
reed@android.com8a1c16f2008-12-17 15:59:43 +0000750 /* Unreference any pixelrefs or colortables
751 */
752 void freePixels();
753 void updatePixelsFromRef() const;
skia.committer@gmail.com3c134a92014-05-24 03:05:26 +0000754
commit-bot@chromium.org851155c2014-05-27 14:03:51 +0000755 void legacyUnflatten(SkReadBuffer&);
756
commit-bot@chromium.org968edca2014-05-23 13:21:55 +0000757 static void WriteRawPixels(SkWriteBuffer*, const SkBitmap&);
758 static bool ReadRawPixels(SkReadBuffer*, SkBitmap*);
weita@google.comf9ab99a2009-05-03 18:23:30 +0000759
commit-bot@chromium.org968edca2014-05-23 13:21:55 +0000760 friend class SkBitmapSource; // unflatten
761 friend class SkReadBuffer; // unflatten, rawpixels
762 friend class SkWriteBuffer; // rawpixels
reed@google.com4a551d42013-06-26 21:28:10 +0000763 friend struct SkBitmapProcState;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000764};
765
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +0000766class SkAutoLockPixels : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000767public:
reed@google.com9c49bc32011-07-07 13:42:37 +0000768 SkAutoLockPixels(const SkBitmap& bm, bool doLock = true) : fBitmap(bm) {
769 fDidLock = doLock;
770 if (doLock) {
771 bm.lockPixels();
772 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000773 }
774 ~SkAutoLockPixels() {
reed@google.com9c49bc32011-07-07 13:42:37 +0000775 if (fDidLock) {
776 fBitmap.unlockPixels();
777 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000778 }
779
780private:
781 const SkBitmap& fBitmap;
reed@google.com9c49bc32011-07-07 13:42:37 +0000782 bool fDidLock;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000783};
mtklein@google.com0f6dc212013-11-18 20:55:29 +0000784//TODO(mtklein): uncomment when 71713004 lands and Chromium's fixed.
785//#define SkAutoLockPixels(...) SK_REQUIRE_LOCAL_VAR(SkAutoLockPixels)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000786
reed@android.com8a1c16f2008-12-17 15:59:43 +0000787///////////////////////////////////////////////////////////////////////////////
788
789inline uint32_t* SkBitmap::getAddr32(int x, int y) const {
790 SkASSERT(fPixels);
reed@google.com69e64632014-03-14 20:48:05 +0000791 SkASSERT(4 == this->bytesPerPixel());
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000792 SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000793 return (uint32_t*)((char*)fPixels + y * fRowBytes + (x << 2));
794}
795
796inline uint16_t* SkBitmap::getAddr16(int x, int y) const {
797 SkASSERT(fPixels);
reed@google.com69e64632014-03-14 20:48:05 +0000798 SkASSERT(2 == this->bytesPerPixel());
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000799 SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000800 return (uint16_t*)((char*)fPixels + y * fRowBytes + (x << 1));
801}
802
803inline uint8_t* SkBitmap::getAddr8(int x, int y) const {
804 SkASSERT(fPixels);
reed@google.com69e64632014-03-14 20:48:05 +0000805 SkASSERT(1 == this->bytesPerPixel());
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000806 SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000807 return (uint8_t*)fPixels + y * fRowBytes + x;
808}
809
810inline SkPMColor SkBitmap::getIndex8Color(int x, int y) const {
811 SkASSERT(fPixels);
reed@google.com69e64632014-03-14 20:48:05 +0000812 SkASSERT(kIndex_8_SkColorType == this->colorType());
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000813 SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000814 SkASSERT(fColorTable);
815 return (*fColorTable)[*((const uint8_t*)fPixels + y * fRowBytes + x)];
816}
817
reed@android.com8a1c16f2008-12-17 15:59:43 +0000818#endif