reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef SkBitmap_DEFINED |
| 9 | #define SkBitmap_DEFINED |
| 10 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | #include "SkColor.h" |
djsollen@google.com | 64a0ec3 | 2012-06-12 15:17:27 +0000 | [diff] [blame] | 12 | #include "SkColorTable.h" |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 13 | #include "SkImageInfo.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkPoint.h" |
| 15 | #include "SkRefCnt.h" |
| 16 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 17 | struct SkMask; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | struct SkIRect; |
reed@google.com | fb2d3c6 | 2012-03-15 21:18:11 +0000 | [diff] [blame] | 19 | struct SkRect; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | class SkPaint; |
| 21 | class SkPixelRef; |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 22 | class SkPixelRefFactory; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | class SkRegion; |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 24 | class SkString; |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 25 | class GrTexture; |
reed@android.com | ce4e53a | 2010-09-09 16:01:26 +0000 | [diff] [blame] | 26 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | /** \class SkBitmap |
| 28 | |
| 29 | The SkBitmap class specifies a raster bitmap. A bitmap has an integer width |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 30 | and height, and a format (colortype), and a pointer to the actual pixels. |
tomhudson@google.com | 4b33d28 | 2011-04-27 15:39:30 +0000 | [diff] [blame] | 31 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 37 | class SK_API SkBitmap { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | public: |
commit-bot@chromium.org | eddb02c | 2013-11-25 15:44:37 +0000 | [diff] [blame] | 39 | class SK_API Allocator; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | |
reed@google.com | 5dd510f | 2012-05-16 13:04:22 +0000 | [diff] [blame] | 41 | /** |
| 42 | * Default construct creates a bitmap with zero width and height, and no pixels. |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 43 | * Its colortype is set to kUnknown_SkColorType. |
reed@google.com | 5dd510f | 2012-05-16 13:04:22 +0000 | [diff] [blame] | 44 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | SkBitmap(); |
reed@google.com | 5dd510f | 2012-05-16 13:04:22 +0000 | [diff] [blame] | 46 | |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | SkBitmap(const SkBitmap& src); |
reed@google.com | 5dd510f | 2012-05-16 13:04:22 +0000 | [diff] [blame] | 55 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | ~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.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 66 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 67 | /////////////////////////////////////////////////////////////////////////// |
| 68 | |
| 69 | const SkImageInfo& info() const { return fInfo; } |
| 70 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 71 | 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.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 75 | |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 76 | /** |
| 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.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 79 | */ |
| 80 | int bytesPerPixel() const { return fInfo.bytesPerPixel(); } |
| 81 | |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 82 | /** |
| 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.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 85 | */ |
| 86 | int rowBytesAsPixels() const { |
| 87 | return fRowBytes >> this->shiftPerPixel(); |
| 88 | } |
| 89 | |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 90 | /** |
| 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.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 93 | */ |
| 94 | int shiftPerPixel() const { return this->bytesPerPixel() >> 1; } |
| 95 | |
| 96 | /////////////////////////////////////////////////////////////////////////// |
| 97 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | /** Return true iff the bitmap has empty dimensions. |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 99 | * Hey! Before you use this, see if you really want to know drawsNothing() instead. |
| 100 | */ |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 101 | bool empty() const { return fInfo.isEmpty(); } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 102 | |
djsollen@google.com | c84b833 | 2012-07-27 13:41:44 +0000 | [diff] [blame] | 103 | /** Return true iff the bitmap has no pixelref. Note: this can return true even if the |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 104 | * 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.com | c84b833 | 2012-07-27 13:41:44 +0000 | [diff] [blame] | 107 | bool isNull() const { return NULL == fPixelRef; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 | |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 109 | /** Return true iff drawing this bitmap has no effect. |
| 110 | */ |
| 111 | bool drawsNothing() const { return this->empty() || this->isNull(); } |
| 112 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 113 | /** Return the number of bytes between subsequent rows of the bitmap. */ |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 114 | size_t rowBytes() const { return fRowBytes; } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 115 | |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 116 | /** |
| 117 | * Set the bitmap's alphaType, returning true on success. If false is |
| 118 | * returned, then the specified new alphaType is incompatible with the |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 119 | * colortype, and the current alphaType is unchanged. |
commit-bot@chromium.org | 0e8d0d6 | 2014-01-27 15:41:07 +0000 | [diff] [blame] | 120 | * |
| 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.com | c1587f9 | 2014-01-28 16:05:39 +0000 | [diff] [blame] | 123 | * be affected. |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 124 | */ |
| 125 | bool setAlphaType(SkAlphaType); |
| 126 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 127 | /** 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 | */ |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 135 | size_t getSize() const { return fInfo.height() * fRowBytes; } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 136 | |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 137 | /** 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.com | d27fe34 | 2012-12-07 15:27:27 +0000 | [diff] [blame] | 139 | cases where extractSubset has been called. |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 140 | */ |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 141 | size_t getSafeSize() const { return fInfo.getSafeSize(fRowBytes); } |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 142 | |
reed@google.com | 57212f9 | 2013-12-30 14:40:38 +0000 | [diff] [blame] | 143 | /** |
| 144 | * Return the full size of the bitmap, in bytes. |
| 145 | */ |
| 146 | int64_t computeSize64() const { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 147 | return sk_64_mul(fInfo.height(), fRowBytes); |
reed@google.com | 57212f9 | 2013-12-30 14:40:38 +0000 | [diff] [blame] | 148 | } |
| 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.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 156 | return fInfo.getSafeSize64(fRowBytes); |
reed@google.com | 57212f9 | 2013-12-30 14:40:38 +0000 | [diff] [blame] | 157 | } |
| 158 | |
junov@chromium.org | b052129 | 2011-12-15 20:14:06 +0000 | [diff] [blame] | 159 | /** 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 166 | underlying pixelref. This state can be set, but it cannot be |
junov@chromium.org | b052129 | 2011-12-15 20:14:06 +0000 | [diff] [blame] | 167 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 172 | /** Returns true if the bitmap is opaque (has no translucent/transparent pixels). |
| 173 | */ |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 174 | bool isOpaque() const { |
| 175 | return SkAlphaTypeIsOpaque(this->alphaType()); |
| 176 | } |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 177 | |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 178 | /** Returns true if the bitmap is volatile (i.e. should not be cached by devices.) |
| 179 | */ |
| 180 | bool isVolatile() const; |
| 181 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 182 | /** Specify whether this bitmap is volatile. Bitmaps are not volatile by |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 183 | 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 185 | should not be cached. Providing this hint when appropriate can |
| 186 | improve performance by avoiding unnecessary overhead and resource |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 187 | consumption on the device. |
| 188 | */ |
| 189 | void setIsVolatile(bool); |
| 190 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 191 | /** 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.com | 86b2e43 | 2012-03-15 21:17:03 +0000 | [diff] [blame] | 196 | /** |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame] | 197 | * 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.org | 21a0b10 | 2013-11-12 18:16:34 +0000 | [diff] [blame] | 202 | * this (isOpaque). Only call this if you need to compute this value from |
| 203 | * "unknown" pixels. |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame] | 204 | */ |
| 205 | static bool ComputeIsOpaque(const SkBitmap&); |
| 206 | |
| 207 | /** |
reed@google.com | 86b2e43 | 2012-03-15 21:17:03 +0000 | [diff] [blame] | 208 | * Return the bitmap's bounds [0, 0, width, height] as an SkRect |
| 209 | */ |
| 210 | void getBounds(SkRect* bounds) const; |
reed@google.com | 80e1459 | 2012-03-16 14:58:07 +0000 | [diff] [blame] | 211 | void getBounds(SkIRect* bounds) const; |
reed@google.com | 86b2e43 | 2012-03-15 21:17:03 +0000 | [diff] [blame] | 212 | |
halcanary | f622a6c | 2014-10-24 12:54:53 -0700 | [diff] [blame] | 213 | SkIRect bounds() const { return fInfo.bounds(); } |
| 214 | SkISize dimensions() const { return fInfo.dimensions(); } |
| 215 | |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 216 | bool setInfo(const SkImageInfo&, size_t rowBytes = 0); |
| 217 | |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 218 | /** |
reed | bae704b | 2014-06-28 14:26:35 -0700 | [diff] [blame] | 219 | * Allocate the bitmap's pixels to match the requested image info. If the Factory |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 220 | * 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 | */ |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 224 | bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo&, SkPixelRefFactory*, SkColorTable*); |
| 225 | |
reed | 68331cd | 2014-12-18 12:48:16 -0800 | [diff] [blame] | 226 | void allocPixels(const SkImageInfo& info, SkPixelRefFactory* factory, SkColorTable* ctable) { |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 227 | if (!this->tryAllocPixels(info, factory, ctable)) { |
reed | 68331cd | 2014-12-18 12:48:16 -0800 | [diff] [blame] | 228 | sk_throw(); |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 229 | } |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 230 | } |
skia.committer@gmail.com | d2ac07b | 2014-01-25 07:01:49 +0000 | [diff] [blame] | 231 | |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 232 | /** |
reed | bae704b | 2014-06-28 14:26:35 -0700 | [diff] [blame] | 233 | * 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 | */ |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 240 | bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info, size_t rowBytes); |
reed | bae704b | 2014-06-28 14:26:35 -0700 | [diff] [blame] | 241 | |
reed | 68331cd | 2014-12-18 12:48:16 -0800 | [diff] [blame] | 242 | void allocPixels(const SkImageInfo& info, size_t rowBytes) { |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 243 | if (!this->tryAllocPixels(info, rowBytes)) { |
reed | 68331cd | 2014-12-18 12:48:16 -0800 | [diff] [blame] | 244 | sk_throw(); |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 245 | } |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info) { |
| 249 | return this->tryAllocPixels(info, info.minRowBytes()); |
| 250 | } |
| 251 | |
reed | 68331cd | 2014-12-18 12:48:16 -0800 | [diff] [blame] | 252 | void allocPixels(const SkImageInfo& info) { |
| 253 | this->allocPixels(info, info.minRowBytes()); |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 254 | } |
skia.committer@gmail.com | d2ac07b | 2014-01-25 07:01:49 +0000 | [diff] [blame] | 255 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 256 | bool SK_WARN_UNUSED_RESULT tryAllocN32Pixels(int width, int height, bool isOpaque = false) { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 257 | SkImageInfo info = SkImageInfo::MakeN32(width, height, |
| 258 | isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 259 | return this->tryAllocPixels(info); |
| 260 | } |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 261 | |
reed | 68331cd | 2014-12-18 12:48:16 -0800 | [diff] [blame] | 262 | void allocN32Pixels(int width, int height, bool isOpaque = false) { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 263 | SkImageInfo info = SkImageInfo::MakeN32(width, height, |
| 264 | isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); |
reed | 68331cd | 2014-12-18 12:48:16 -0800 | [diff] [blame] | 265 | this->allocPixels(info); |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 266 | } |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 267 | |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 268 | /** |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 269 | * 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.org | 00f8d6c | 2014-05-29 15:57:20 +0000 | [diff] [blame] | 271 | * referenced, if releaseProc is not null, it will be called with the |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 272 | * pixels and context as parameters. |
| 273 | * On failure, the bitmap will be set to empty and return false. |
| 274 | */ |
commit-bot@chromium.org | 00f8d6c | 2014-05-29 15:57:20 +0000 | [diff] [blame] | 275 | bool installPixels(const SkImageInfo&, void* pixels, size_t rowBytes, SkColorTable*, |
| 276 | void (*releaseProc)(void* addr, void* context), void* context); |
| 277 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 278 | /** |
| 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.org | 00f8d6c | 2014-05-29 15:57:20 +0000 | [diff] [blame] | 284 | return this->installPixels(info, pixels, rowBytes, NULL, NULL, NULL); |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 285 | } |
| 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.com | d2ac07b | 2014-01-25 07:01:49 +0000 | [diff] [blame] | 293 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 294 | /** 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.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 297 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 298 | 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.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 306 | /** Copies the bitmap's pixels to the location pointed at by dst and returns |
| 307 | true if possible, returns false otherwise. |
| 308 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 309 | 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.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 314 | |
| 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.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 320 | @param dstRowBytes Width of each line in the buffer. If 0, uses |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 321 | bitmap's internal stride. |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 322 | @param preserveDstPad Must we preserve padding in the dst |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 323 | */ |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 324 | bool copyPixelsTo(void* const dst, size_t dstSize, size_t dstRowBytes = 0, |
| 325 | bool preserveDstPad = false) const; |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 326 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 327 | /** Use the standard HeapAllocator to create the pixelref that manages the |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 328 | pixel memory. It will be sized based on the current ImageInfo. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 329 | If this is called multiple times, a new pixelref object will be created |
| 330 | each time. |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 331 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 332 | 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 |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 336 | be allocated. Only used if colortype == kIndex_8_SkColorType |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 337 | @return true if the allocation succeeds. If not the pixelref field of |
| 338 | the bitmap will be unchanged. |
| 339 | */ |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 340 | bool SK_WARN_UNUSED_RESULT tryAllocPixels(SkColorTable* ctable = NULL) { |
| 341 | return this->tryAllocPixels(NULL, ctable); |
| 342 | } |
| 343 | |
reed | 68331cd | 2014-12-18 12:48:16 -0800 | [diff] [blame] | 344 | void allocPixels(SkColorTable* ctable = NULL) { |
| 345 | this->allocPixels(NULL, ctable); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 346 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 347 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 348 | /** Use the specified Allocator to create the pixelref that manages the |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 349 | pixel memory. It will be sized based on the current ImageInfo. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 350 | If this is called multiple times, a new pixelref object will be created |
| 351 | each time. |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 352 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 353 | 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.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 355 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 356 | @param allocator The Allocator to use to create a pixelref that can |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 357 | manage the pixel memory for the current ImageInfo. |
| 358 | If allocator is NULL, the standard HeapAllocator will be used. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 359 | @param ctable ColorTable (or null) to use with the pixels that will |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 360 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 362 | be ignored. |
| 363 | @return true if the allocation succeeds. If not the pixelref field of |
| 364 | the bitmap will be unchanged. |
| 365 | */ |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 366 | bool SK_WARN_UNUSED_RESULT tryAllocPixels(Allocator* allocator, SkColorTable* ctable); |
| 367 | |
reed | 68331cd | 2014-12-18 12:48:16 -0800 | [diff] [blame] | 368 | void allocPixels(Allocator* allocator, SkColorTable* ctable) { |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 369 | if (!this->tryAllocPixels(allocator, ctable)) { |
reed | 68331cd | 2014-12-18 12:48:16 -0800 | [diff] [blame] | 370 | sk_throw(); |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 371 | } |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 372 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 373 | |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 374 | /** |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 378 | SkPixelRef* pixelRef() const { return fPixelRef; } |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 379 | |
| 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.com | 1ae9111 | 2014-01-09 07:01:42 +0000 | [diff] [blame] | 405 | |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 406 | SkPixelRef* setPixelRef(SkPixelRef* pr) { |
| 407 | return this->setPixelRef(pr, 0, 0); |
| 408 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 409 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 410 | /** 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.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 421 | |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 422 | /** |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 430 | /** Call this to be sure that the bitmap is valid enough to be drawn (i.e. |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 431 | it has non-null pixels, and if required by its colortype, it has a |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 432 | non-null colortable. Returns true if all of the above are met. |
| 433 | */ |
| 434 | bool readyToDraw() const { |
| 435 | return this->getPixels() != NULL && |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 436 | (this->colorType() != kIndex_8_SkColorType || fColorTable); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 437 | } |
| 438 | |
reed@android.com | ce4e53a | 2010-09-09 16:01:26 +0000 | [diff] [blame] | 439 | /** Returns the pixelRef's texture, or NULL |
| 440 | */ |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 441 | GrTexture* getTexture() const; |
reed@android.com | ce4e53a | 2010-09-09 16:01:26 +0000 | [diff] [blame] | 442 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 443 | /** Return the bitmap's colortable, if it uses one (i.e. colorType is |
| 444 | Index_8) and the pixels are locked. |
scroggo@google.com | 665b2cc | 2013-05-06 18:08:05 +0000 | [diff] [blame] | 445 | Otherwise returns NULL. Does not affect the colortable's |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 446 | 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.com | c84b833 | 2012-07-27 13:41:44 +0000 | [diff] [blame] | 451 | pixelref. Each time the pixels are changed (and notifyPixelsChanged |
| 452 | is called), a different generation ID will be returned. Finally, if |
scroggo | 58edea8 | 2014-06-05 07:49:04 -0700 | [diff] [blame] | 453 | there is no pixelRef then zero is returned. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 454 | */ |
| 455 | uint32_t getGenerationID() const; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 456 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 457 | /** 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.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 463 | /** |
| 464 | * Fill the entire bitmap with the specified color. |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 465 | * 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.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 467 | * alpha (e.g. A1 or A8) then the color's r,g,b components are ignored. |
| 468 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 469 | void eraseColor(SkColor c) const { |
| 470 | this->eraseARGB(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c), |
| 471 | SkColorGetB(c)); |
| 472 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 473 | |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 474 | /** |
| 475 | * Fill the entire bitmap with the specified color. |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 476 | * 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.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 478 | * 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.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 482 | SK_ATTR_DEPRECATED("use eraseARGB or eraseColor") |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 483 | 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. |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 489 | * 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.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 491 | * 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 495 | /** 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.com | 6a9368d | 2012-08-22 16:19:52 +0000 | [diff] [blame] | 497 | still update the inval region (if present). If the bitmap is immutable, |
| 498 | do nothing and return false. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 499 | |
| 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. |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 509 | @return true if the scroll was doable. Will return false if the colortype is kUnkown or |
| 510 | if the bitmap is immutable. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 511 | 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.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 517 | /** |
| 518 | * Return the SkColor of the specified pixel. In most cases this will |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 519 | * require un-premultiplying the color. Alpha only colortypes (e.g. kAlpha_8_SkColorType) |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 520 | * return black with the appropriate alpha set. The value is undefined |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 521 | * for kUnknown_SkColorType or if x or y are out of bounds, or if the bitmap |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 522 | * does not have any pixels (or has not be locked with lockPixels()). |
| 523 | */ |
| 524 | SkColor getColor(int x, int y) const; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 525 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 526 | /** 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.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 538 | * In debug build, this asserts that the pixels are allocated and locked, |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 539 | * and that the colortype is 32-bit, however none of these checks are performed |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 540 | * in the release build. |
| 541 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 542 | inline uint32_t* getAddr32(int x, int y) const; |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 543 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 544 | /** Returns the address of the pixel specified by x,y for 16bit pixels. |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 545 | * In debug build, this asserts that the pixels are allocated and locked, |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 546 | * and that the colortype is 16-bit, however none of these checks are performed |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 547 | * in the release build. |
| 548 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 549 | inline uint16_t* getAddr16(int x, int y) const; |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 550 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 551 | /** Returns the address of the pixel specified by x,y for 8bit pixels. |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 552 | * In debug build, this asserts that the pixels are allocated and locked, |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 553 | * and that the colortype is 8-bit, however none of these checks are performed |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 554 | * in the release build. |
| 555 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 556 | inline uint8_t* getAddr8(int x, int y) const; |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 557 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 558 | /** Returns the color corresponding to the pixel specified by x,y for |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 559 | * colortable based bitmaps. |
| 560 | * In debug build, this asserts that the pixels are allocated and locked, |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 561 | * that the colortype is indexed, and that the colortable is allocated, |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 562 | * however none of these checks are performed in the release build. |
| 563 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 564 | inline SkPMColor getIndex8Color(int x, int y) const; |
| 565 | |
epoger@google.com | 9ef2d83 | 2011-07-01 21:12:20 +0000 | [diff] [blame] | 566 | /** Set dst to be a setset of this bitmap. If possible, it will share the |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 567 | pixel memory, and just point into a subset of it. However, if the colortype |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 568 | 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 |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 570 | dimensions is empty, or if there is an unsupported colortype, false will be |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 571 | 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.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 579 | /** Makes a deep copy of this bitmap, respecting the requested colorType, |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 580 | * 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 |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 583 | * alpha, and the requested colortype does not support alpha). |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 584 | * @param dst The bitmap to be sized and allocated |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 585 | * @param ct The desired colorType for dst |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 586 | * @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.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 589 | * @return true if the copy was made. |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 590 | */ |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 591 | 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 | /** |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 598 | * 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.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 620 | * 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.com | 89bb83a | 2009-05-29 21:30:42 +0000 | [diff] [blame] | 624 | |
commit-bot@chromium.org | 6285f4f | 2014-02-20 19:08:07 +0000 | [diff] [blame] | 625 | /** 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.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 632 | |
djsollen@google.com | 4bd2bdb | 2013-03-08 18:35:13 +0000 | [diff] [blame] | 633 | #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.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 647 | bool extractAlpha(SkBitmap* dst) const { |
| 648 | return this->extractAlpha(dst, NULL, NULL, NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 649 | } |
| 650 | |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 651 | bool extractAlpha(SkBitmap* dst, const SkPaint* paint, |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 652 | SkIPoint* offset) const { |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 653 | return this->extractAlpha(dst, paint, NULL, offset); |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 654 | } |
| 655 | |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 656 | /** 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 670 | SkIPoint* offset) const; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 671 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 672 | SkDEBUGCODE(void validate() const;) |
| 673 | |
| 674 | class Allocator : public SkRefCnt { |
| 675 | public: |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 676 | SK_DECLARE_INST_COUNT(Allocator) |
| 677 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 678 | /** Allocate the pixel memory for the bitmap, given its dimensions and |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 679 | colortype. Return true on success, where success means either setPixels |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 680 | or setPixelRef was called. The pixels need not be locked when this |
reed | ddd014e | 2014-06-05 08:51:20 -0700 | [diff] [blame] | 681 | returns. If the colortype requires a colortable, it also must be |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 682 | 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.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 686 | private: |
| 687 | typedef SkRefCnt INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 688 | }; |
| 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.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 696 | virtual bool allocPixelRef(SkBitmap*, SkColorTable*) SK_OVERRIDE; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 697 | }; |
| 698 | |
| 699 | class RLEPixels { |
| 700 | public: |
| 701 | RLEPixels(int width, int height); |
| 702 | virtual ~RLEPixels(); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 703 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 704 | uint8_t* packedAtY(int y) const { |
| 705 | SkASSERT((unsigned)y < (unsigned)fHeight); |
| 706 | return fYPtrs[y]; |
| 707 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 708 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 709 | // 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.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 714 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 715 | private: |
| 716 | uint8_t** fYPtrs; |
| 717 | int fHeight; |
| 718 | }; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 719 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 720 | SK_TO_STRING_NONVIRT() |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 721 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 722 | private: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 723 | mutable SkPixelRef* fPixelRef; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 724 | mutable int fPixelLockCount; |
reed@google.com | 5f62ed7 | 2014-01-15 19:59:45 +0000 | [diff] [blame] | 725 | // These are just caches from the locked pixelref |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 726 | mutable void* fPixels; |
| 727 | mutable SkColorTable* fColorTable; // only meaningful for kIndex8 |
| 728 | |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 729 | SkIPoint fPixelRefOrigin; |
| 730 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 731 | enum Flags { |
junov@chromium.org | b052129 | 2011-12-15 20:14:06 +0000 | [diff] [blame] | 732 | kImageIsVolatile_Flag = 0x02, |
djsollen@google.com | 4bd2bdb | 2013-03-08 18:35:13 +0000 | [diff] [blame] | 733 | #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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 740 | }; |
| 741 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 742 | SkImageInfo fInfo; |
| 743 | |
scroggo@google.com | e5f4824 | 2013-02-25 21:47:41 +0000 | [diff] [blame] | 744 | uint32_t fRowBytes; |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 745 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 746 | uint8_t fFlags; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 747 | |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 748 | void internalErase(const SkIRect&, U8CPU a, U8CPU r, U8CPU g, U8CPU b)const; |
| 749 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 750 | /* Unreference any pixelrefs or colortables |
| 751 | */ |
| 752 | void freePixels(); |
| 753 | void updatePixelsFromRef() const; |
skia.committer@gmail.com | 3c134a9 | 2014-05-24 03:05:26 +0000 | [diff] [blame] | 754 | |
commit-bot@chromium.org | 851155c | 2014-05-27 14:03:51 +0000 | [diff] [blame] | 755 | void legacyUnflatten(SkReadBuffer&); |
| 756 | |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 757 | static void WriteRawPixels(SkWriteBuffer*, const SkBitmap&); |
| 758 | static bool ReadRawPixels(SkReadBuffer*, SkBitmap*); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 759 | |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 760 | friend class SkBitmapSource; // unflatten |
| 761 | friend class SkReadBuffer; // unflatten, rawpixels |
| 762 | friend class SkWriteBuffer; // rawpixels |
reed@google.com | 4a551d4 | 2013-06-26 21:28:10 +0000 | [diff] [blame] | 763 | friend struct SkBitmapProcState; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 764 | }; |
| 765 | |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 766 | class SkAutoLockPixels : SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 767 | public: |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 768 | SkAutoLockPixels(const SkBitmap& bm, bool doLock = true) : fBitmap(bm) { |
| 769 | fDidLock = doLock; |
| 770 | if (doLock) { |
| 771 | bm.lockPixels(); |
| 772 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 773 | } |
| 774 | ~SkAutoLockPixels() { |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 775 | if (fDidLock) { |
| 776 | fBitmap.unlockPixels(); |
| 777 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | private: |
| 781 | const SkBitmap& fBitmap; |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 782 | bool fDidLock; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 783 | }; |
mtklein@google.com | 0f6dc21 | 2013-11-18 20:55:29 +0000 | [diff] [blame] | 784 | //TODO(mtklein): uncomment when 71713004 lands and Chromium's fixed. |
| 785 | //#define SkAutoLockPixels(...) SK_REQUIRE_LOCAL_VAR(SkAutoLockPixels) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 786 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 787 | /////////////////////////////////////////////////////////////////////////////// |
| 788 | |
| 789 | inline uint32_t* SkBitmap::getAddr32(int x, int y) const { |
| 790 | SkASSERT(fPixels); |
reed@google.com | 69e6463 | 2014-03-14 20:48:05 +0000 | [diff] [blame] | 791 | SkASSERT(4 == this->bytesPerPixel()); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 792 | SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 793 | return (uint32_t*)((char*)fPixels + y * fRowBytes + (x << 2)); |
| 794 | } |
| 795 | |
| 796 | inline uint16_t* SkBitmap::getAddr16(int x, int y) const { |
| 797 | SkASSERT(fPixels); |
reed@google.com | 69e6463 | 2014-03-14 20:48:05 +0000 | [diff] [blame] | 798 | SkASSERT(2 == this->bytesPerPixel()); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 799 | SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 800 | return (uint16_t*)((char*)fPixels + y * fRowBytes + (x << 1)); |
| 801 | } |
| 802 | |
| 803 | inline uint8_t* SkBitmap::getAddr8(int x, int y) const { |
| 804 | SkASSERT(fPixels); |
reed@google.com | 69e6463 | 2014-03-14 20:48:05 +0000 | [diff] [blame] | 805 | SkASSERT(1 == this->bytesPerPixel()); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 806 | SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 807 | return (uint8_t*)fPixels + y * fRowBytes + x; |
| 808 | } |
| 809 | |
| 810 | inline SkPMColor SkBitmap::getIndex8Color(int x, int y) const { |
| 811 | SkASSERT(fPixels); |
reed@google.com | 69e6463 | 2014-03-14 20:48:05 +0000 | [diff] [blame] | 812 | SkASSERT(kIndex_8_SkColorType == this->colorType()); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 813 | SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 814 | SkASSERT(fColorTable); |
| 815 | return (*fColorTable)[*((const uint8_t*)fPixels + y * fRowBytes + x)]; |
| 816 | } |
| 817 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 818 | #endif |