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 |
| 30 | and height, and a format (config), 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 | |
| 41 | enum Config { |
| 42 | kNo_Config, //!< bitmap has not been configured |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | kA8_Config, //!< 8-bits per pixel, with only alpha specified (0 is transparent, 0xFF is opaque) |
| 44 | kIndex8_Config, //!< 8-bits per pixel, using SkColorTable to specify the colors |
| 45 | kRGB_565_Config, //!< 16-bits per pixel, (see SkColorPriv.h for packing) |
| 46 | kARGB_4444_Config, //!< 16-bits per pixel, (see SkColorPriv.h for packing) |
| 47 | kARGB_8888_Config, //!< 32-bits per pixel, (see SkColorPriv.h for packing) |
reed@google.com | 62b2605 | 2013-06-25 21:23:54 +0000 | [diff] [blame] | 48 | }; |
skia.committer@gmail.com | 73a4b4f | 2013-06-26 07:00:59 +0000 | [diff] [blame] | 49 | |
reed@google.com | 62b2605 | 2013-06-25 21:23:54 +0000 | [diff] [blame] | 50 | // do not add this to the Config enum, otherwise the compiler will let us |
| 51 | // pass this as a valid parameter for Config. |
| 52 | enum { |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 53 | kConfigCount = kARGB_8888_Config + 1 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
reed@google.com | 5dd510f | 2012-05-16 13:04:22 +0000 | [diff] [blame] | 56 | /** |
| 57 | * Default construct creates a bitmap with zero width and height, and no pixels. |
| 58 | * Its config is set to kNo_Config. |
| 59 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | SkBitmap(); |
reed@google.com | 5dd510f | 2012-05-16 13:04:22 +0000 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Copy the settings from the src into this bitmap. If the src has pixels |
| 64 | * allocated, they will be shared, not copied, so that the two bitmaps will |
| 65 | * reference the same memory for the pixels. If a deep copy is needed, |
| 66 | * where the new bitmap has its own separate copy of the pixels, use |
| 67 | * deepCopyTo(). |
| 68 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | SkBitmap(const SkBitmap& src); |
reed@google.com | 5dd510f | 2012-05-16 13:04:22 +0000 | [diff] [blame] | 70 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | ~SkBitmap(); |
| 72 | |
| 73 | /** Copies the src bitmap into this bitmap. Ownership of the src bitmap's pixels remains |
| 74 | with the src bitmap. |
| 75 | */ |
| 76 | SkBitmap& operator=(const SkBitmap& src); |
| 77 | /** Swap the fields of the two bitmaps. This routine is guaranteed to never fail or throw. |
| 78 | */ |
| 79 | // This method is not exported to java. |
| 80 | void swap(SkBitmap& other); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 81 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 82 | /////////////////////////////////////////////////////////////////////////// |
| 83 | |
| 84 | const SkImageInfo& info() const { return fInfo; } |
| 85 | |
| 86 | int width() const { return fInfo.fWidth; } |
| 87 | int height() const { return fInfo.fHeight; } |
| 88 | SkColorType colorType() const { return fInfo.fColorType; } |
| 89 | SkAlphaType alphaType() const { return fInfo.fAlphaType; } |
| 90 | |
| 91 | /** Return the number of bytes per pixel based on the config. If the config |
| 92 | does not have at least 1 byte per (e.g. kA1_Config) then 0 is returned. |
| 93 | */ |
| 94 | int bytesPerPixel() const { return fInfo.bytesPerPixel(); } |
| 95 | |
| 96 | /** Return the rowbytes expressed as a number of pixels (like width and |
| 97 | height). Note, for 1-byte per pixel configs like kA8_Config, this will |
| 98 | return the same as rowBytes(). Is undefined for configs that are less |
| 99 | than 1-byte per pixel (e.g. kA1_Config) |
| 100 | */ |
| 101 | int rowBytesAsPixels() const { |
| 102 | return fRowBytes >> this->shiftPerPixel(); |
| 103 | } |
| 104 | |
| 105 | /** Return the shift amount per pixel (i.e. 0 for 1-byte per pixel, 1 for |
| 106 | 2-bytes per pixel configs, 2 for 4-bytes per pixel configs). Return 0 |
| 107 | for configs that are not at least 1-byte per pixel (e.g. kA1_Config |
| 108 | or kNo_Config) |
| 109 | */ |
| 110 | int shiftPerPixel() const { return this->bytesPerPixel() >> 1; } |
| 111 | |
| 112 | /////////////////////////////////////////////////////////////////////////// |
| 113 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | /** Return true iff the bitmap has empty dimensions. |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 115 | * Hey! Before you use this, see if you really want to know drawsNothing() instead. |
| 116 | */ |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 117 | bool empty() const { return fInfo.isEmpty(); } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 118 | |
djsollen@google.com | c84b833 | 2012-07-27 13:41:44 +0000 | [diff] [blame] | 119 | /** 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] | 120 | * dimensions of the bitmap are > 0 (see empty()). |
| 121 | * Hey! Before you use this, see if you really want to know drawsNothing() instead. |
| 122 | */ |
djsollen@google.com | c84b833 | 2012-07-27 13:41:44 +0000 | [diff] [blame] | 123 | bool isNull() const { return NULL == fPixelRef; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 125 | /** Return true iff drawing this bitmap has no effect. |
| 126 | */ |
| 127 | bool drawsNothing() const { return this->empty() || this->isNull(); } |
| 128 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 129 | /** Return the config for the bitmap. */ |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 130 | Config config() const; |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 131 | |
| 132 | SK_ATTR_DEPRECATED("use config()") |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 133 | Config getConfig() const { return this->config(); } |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 134 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 135 | /** Return the number of bytes between subsequent rows of the bitmap. */ |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 136 | size_t rowBytes() const { return fRowBytes; } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 137 | |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 138 | /** |
| 139 | * Set the bitmap's alphaType, returning true on success. If false is |
| 140 | * returned, then the specified new alphaType is incompatible with the |
| 141 | * Config, and the current alphaType is unchanged. |
commit-bot@chromium.org | 0e8d0d6 | 2014-01-27 15:41:07 +0000 | [diff] [blame] | 142 | * |
| 143 | * Note: this changes the alphatype for the underlying pixels, which means |
| 144 | * 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] | 145 | * be affected. |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 146 | */ |
| 147 | bool setAlphaType(SkAlphaType); |
| 148 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | /** Return the address of the pixels for this SkBitmap. |
| 150 | */ |
| 151 | void* getPixels() const { return fPixels; } |
| 152 | |
| 153 | /** Return the byte size of the pixels, based on the height and rowBytes. |
| 154 | Note this truncates the result to 32bits. Call getSize64() to detect |
| 155 | if the real size exceeds 32bits. |
| 156 | */ |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 157 | size_t getSize() const { return fInfo.fHeight * fRowBytes; } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 158 | |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 159 | /** Return the number of bytes from the pointer returned by getPixels() |
| 160 | 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] | 161 | cases where extractSubset has been called. |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 162 | */ |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 163 | size_t getSafeSize() const { return fInfo.getSafeSize(fRowBytes); } |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 164 | |
reed@google.com | 57212f9 | 2013-12-30 14:40:38 +0000 | [diff] [blame] | 165 | /** |
| 166 | * Return the full size of the bitmap, in bytes. |
| 167 | */ |
| 168 | int64_t computeSize64() const { |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 169 | return sk_64_mul(fInfo.fHeight, fRowBytes); |
reed@google.com | 57212f9 | 2013-12-30 14:40:38 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Return the number of bytes from the pointer returned by getPixels() |
| 174 | * to the end of the allocated space in the buffer. This may be smaller |
| 175 | * than computeSize64() if there is any rowbytes padding beyond the width. |
| 176 | */ |
| 177 | int64_t computeSafeSize64() const { |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 178 | return fInfo.getSafeSize64(fRowBytes); |
reed@google.com | 57212f9 | 2013-12-30 14:40:38 +0000 | [diff] [blame] | 179 | } |
| 180 | |
junov@chromium.org | b052129 | 2011-12-15 20:14:06 +0000 | [diff] [blame] | 181 | /** Returns true if this bitmap is marked as immutable, meaning that the |
| 182 | contents of its pixels will not change for the lifetime of the bitmap. |
| 183 | */ |
| 184 | bool isImmutable() const; |
| 185 | |
| 186 | /** Marks this bitmap as immutable, meaning that the contents of its |
| 187 | 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] | 188 | underlying pixelref. This state can be set, but it cannot be |
junov@chromium.org | b052129 | 2011-12-15 20:14:06 +0000 | [diff] [blame] | 189 | cleared once it is set. This state propagates to all other bitmaps |
| 190 | that share the same pixelref. |
| 191 | */ |
| 192 | void setImmutable(); |
| 193 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 194 | /** Returns true if the bitmap is opaque (has no translucent/transparent pixels). |
| 195 | */ |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 196 | bool isOpaque() const { |
| 197 | return SkAlphaTypeIsOpaque(this->alphaType()); |
| 198 | } |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 199 | |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 200 | /** Returns true if the bitmap is volatile (i.e. should not be cached by devices.) |
| 201 | */ |
| 202 | bool isVolatile() const; |
| 203 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 204 | /** Specify whether this bitmap is volatile. Bitmaps are not volatile by |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 205 | default. Temporary bitmaps that are discarded after use should be |
| 206 | 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] | 207 | should not be cached. Providing this hint when appropriate can |
| 208 | improve performance by avoiding unnecessary overhead and resource |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 209 | consumption on the device. |
| 210 | */ |
| 211 | void setIsVolatile(bool); |
| 212 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 213 | /** Reset the bitmap to its initial state (see default constructor). If we are a (shared) |
| 214 | owner of the pixels, that ownership is decremented. |
| 215 | */ |
| 216 | void reset(); |
| 217 | |
| 218 | /** Given a config and a width, this computes the optimal rowBytes value. This is called automatically |
| 219 | if you pass 0 for rowBytes to setConfig(). |
| 220 | */ |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 221 | static size_t ComputeRowBytes(Config c, int width); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 222 | |
| 223 | /** Return the bytes-per-pixel for the specified config. If the config is |
| 224 | not at least 1-byte per pixel, return 0, including for kNo_Config. |
| 225 | */ |
| 226 | static int ComputeBytesPerPixel(Config c); |
| 227 | |
| 228 | /** Return the shift-per-pixel for the specified config. If the config is |
| 229 | not at least 1-byte per pixel, return 0, including for kNo_Config. |
| 230 | */ |
| 231 | static int ComputeShiftPerPixel(Config c) { |
| 232 | return ComputeBytesPerPixel(c) >> 1; |
| 233 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 234 | |
reed@google.com | 57212f9 | 2013-12-30 14:40:38 +0000 | [diff] [blame] | 235 | static int64_t ComputeSize64(Config, int width, int height); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 236 | static size_t ComputeSize(Config, int width, int height); |
| 237 | |
reed@google.com | 86b2e43 | 2012-03-15 21:17:03 +0000 | [diff] [blame] | 238 | /** |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame] | 239 | * This will brute-force return true if all of the pixels in the bitmap |
| 240 | * are opaque. If it fails to read the pixels, or encounters an error, |
| 241 | * it will return false. |
| 242 | * |
| 243 | * 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] | 244 | * this (isOpaque). Only call this if you need to compute this value from |
| 245 | * "unknown" pixels. |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame] | 246 | */ |
| 247 | static bool ComputeIsOpaque(const SkBitmap&); |
| 248 | |
| 249 | /** |
reed@google.com | 86b2e43 | 2012-03-15 21:17:03 +0000 | [diff] [blame] | 250 | * Return the bitmap's bounds [0, 0, width, height] as an SkRect |
| 251 | */ |
| 252 | void getBounds(SkRect* bounds) const; |
reed@google.com | 80e1459 | 2012-03-16 14:58:07 +0000 | [diff] [blame] | 253 | void getBounds(SkIRect* bounds) const; |
reed@google.com | 86b2e43 | 2012-03-15 21:17:03 +0000 | [diff] [blame] | 254 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 255 | /** Set the bitmap's config and dimensions. If rowBytes is 0, then |
| 256 | ComputeRowBytes() is called to compute the optimal value. This resets |
| 257 | any pixel/colortable ownership, just like reset(). |
| 258 | */ |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 259 | bool setConfig(Config, int width, int height, size_t rowBytes, SkAlphaType); |
| 260 | |
| 261 | bool setConfig(Config config, int width, int height, size_t rowBytes = 0) { |
| 262 | return this->setConfig(config, width, height, rowBytes, |
| 263 | kPremul_SkAlphaType); |
| 264 | } |
| 265 | |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 266 | bool setConfig(const SkImageInfo& info, size_t rowBytes = 0); |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 267 | |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 268 | /** |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 269 | * Allocate a pixelref to match the specified image info. If the Factory |
| 270 | * is non-null, call it to allcoate the pixelref. If the ImageInfo requires |
| 271 | * a colortable, then ColorTable must be non-null, and will be ref'd. |
| 272 | * On failure, the bitmap will be set to empty and return false. |
| 273 | */ |
| 274 | bool allocPixels(const SkImageInfo&, SkPixelRefFactory*, SkColorTable*); |
skia.committer@gmail.com | d2ac07b | 2014-01-25 07:01:49 +0000 | [diff] [blame] | 275 | |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 276 | /** |
| 277 | * Allocate a pixelref to match the specified image info, using the default |
| 278 | * allocator. |
| 279 | * On success, the bitmap's pixels will be "locked", and return true. |
| 280 | * On failure, the bitmap will be set to empty and return false. |
| 281 | */ |
| 282 | bool allocPixels(const SkImageInfo& info) { |
| 283 | return this->allocPixels(info, NULL, NULL); |
| 284 | } |
skia.committer@gmail.com | d2ac07b | 2014-01-25 07:01:49 +0000 | [diff] [blame] | 285 | |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 286 | /** |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 287 | * Legacy helper function, which creates an SkImageInfo from the specified |
| 288 | * config and then calls allocPixels(info). |
| 289 | */ |
| 290 | bool allocConfigPixels(Config, int width, int height, bool isOpaque = false); |
| 291 | |
| 292 | bool allocN32Pixels(int width, int height, bool isOpaque = false) { |
| 293 | SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
| 294 | if (isOpaque) { |
| 295 | info.fAlphaType = kOpaque_SkAlphaType; |
| 296 | } |
| 297 | return this->allocPixels(info); |
| 298 | } |
| 299 | |
| 300 | /** |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 301 | * Install a pixelref that wraps the specified pixels and rowBytes, and |
| 302 | * optional ReleaseProc and context. When the pixels are no longer |
| 303 | * referenced, if ReleaseProc is not null, it will be called with the |
| 304 | * pixels and context as parameters. |
| 305 | * On failure, the bitmap will be set to empty and return false. |
| 306 | */ |
| 307 | bool installPixels(const SkImageInfo&, void* pixels, size_t rowBytes, |
| 308 | void (*ReleaseProc)(void* addr, void* context), |
| 309 | void* context); |
skia.committer@gmail.com | 31acdea | 2014-02-18 03:01:51 +0000 | [diff] [blame] | 310 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 311 | /** |
| 312 | * Call installPixels with no ReleaseProc specified. This means that the |
| 313 | * caller must ensure that the specified pixels are valid for the lifetime |
| 314 | * of the created bitmap (and its pixelRef). |
| 315 | */ |
| 316 | bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 317 | return this->installPixels(info, pixels, rowBytes, NULL, NULL); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Calls installPixels() with the value in the SkMask. The caller must |
| 322 | * ensure that the specified mask pixels are valid for the lifetime |
| 323 | * of the created bitmap (and its pixelRef). |
| 324 | */ |
| 325 | bool installMaskPixels(const SkMask&); |
skia.committer@gmail.com | d2ac07b | 2014-01-25 07:01:49 +0000 | [diff] [blame] | 326 | |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 327 | /** |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 328 | * DEPRECATED: call info(). |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 329 | */ |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 330 | bool asImageInfo(SkImageInfo* info) const { |
| 331 | // compatibility: return false for kUnknown |
| 332 | if (kUnknown_SkColorType == this->colorType()) { |
| 333 | return false; |
| 334 | } |
| 335 | if (info) { |
| 336 | *info = this->info(); |
| 337 | } |
| 338 | return true; |
| 339 | } |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 340 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 341 | /** Use this to assign a new pixel address for an existing bitmap. This |
| 342 | will automatically release any pixelref previously installed. Only call |
| 343 | this if you are handling ownership/lifetime of the pixel memory. |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 344 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 345 | If the bitmap retains a reference to the colortable (assuming it is |
| 346 | not null) it will take care of incrementing the reference count. |
| 347 | |
| 348 | @param pixels Address for the pixels, managed by the caller. |
| 349 | @param ctable ColorTable (or null) that matches the specified pixels |
| 350 | */ |
| 351 | void setPixels(void* p, SkColorTable* ctable = NULL); |
| 352 | |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 353 | /** Copies the bitmap's pixels to the location pointed at by dst and returns |
| 354 | true if possible, returns false otherwise. |
| 355 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 356 | In the case when the dstRowBytes matches the bitmap's rowBytes, the copy |
| 357 | may be made faster by copying over the dst's per-row padding (for all |
| 358 | rows but the last). By setting preserveDstPad to true the caller can |
| 359 | disable this optimization and ensure that pixels in the padding are not |
| 360 | overwritten. |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 361 | |
| 362 | Always returns false for RLE formats. |
| 363 | |
| 364 | @param dst Location of destination buffer. |
| 365 | @param dstSize Size of destination buffer. Must be large enough to hold |
| 366 | pixels using indicated stride. |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 367 | @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] | 368 | bitmap's internal stride. |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 369 | @param preserveDstPad Must we preserve padding in the dst |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 370 | */ |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 371 | bool copyPixelsTo(void* const dst, size_t dstSize, size_t dstRowBytes = 0, |
| 372 | bool preserveDstPad = false) const; |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 373 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 374 | /** Use the standard HeapAllocator to create the pixelref that manages the |
| 375 | pixel memory. It will be sized based on the current width/height/config. |
| 376 | If this is called multiple times, a new pixelref object will be created |
| 377 | each time. |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 378 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 379 | If the bitmap retains a reference to the colortable (assuming it is |
| 380 | not null) it will take care of incrementing the reference count. |
| 381 | |
| 382 | @param ctable ColorTable (or null) to use with the pixels that will |
| 383 | be allocated. Only used if config == Index8_Config |
| 384 | @return true if the allocation succeeds. If not the pixelref field of |
| 385 | the bitmap will be unchanged. |
| 386 | */ |
| 387 | bool allocPixels(SkColorTable* ctable = NULL) { |
| 388 | return this->allocPixels(NULL, ctable); |
| 389 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 390 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 391 | /** Use the specified Allocator to create the pixelref that manages the |
| 392 | pixel memory. It will be sized based on the current width/height/config. |
| 393 | If this is called multiple times, a new pixelref object will be created |
| 394 | each time. |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 395 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 396 | If the bitmap retains a reference to the colortable (assuming it is |
| 397 | not null) it will take care of incrementing the reference count. |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 398 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 399 | @param allocator The Allocator to use to create a pixelref that can |
| 400 | manage the pixel memory for the current |
| 401 | width/height/config. If allocator is NULL, the standard |
| 402 | HeapAllocator will be used. |
| 403 | @param ctable ColorTable (or null) to use with the pixels that will |
| 404 | be allocated. Only used if config == Index8_Config. |
| 405 | If it is non-null and the config is not Index8, it will |
| 406 | be ignored. |
| 407 | @return true if the allocation succeeds. If not the pixelref field of |
| 408 | the bitmap will be unchanged. |
| 409 | */ |
| 410 | bool allocPixels(Allocator* allocator, SkColorTable* ctable); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 411 | |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 412 | /** |
| 413 | * Return the current pixelref object or NULL if there is none. This does |
| 414 | * not affect the refcount of the pixelref. |
| 415 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 416 | SkPixelRef* pixelRef() const { return fPixelRef; } |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 417 | |
| 418 | /** |
| 419 | * A bitmap can reference a subset of a pixelref's pixels. That means the |
| 420 | * bitmap's width/height can be <= the dimensions of the pixelref. The |
| 421 | * pixelref origin is the x,y location within the pixelref's pixels for |
| 422 | * the bitmap's top/left corner. To be valid the following must be true: |
| 423 | * |
| 424 | * origin_x + bitmap_width <= pixelref_width |
| 425 | * origin_y + bitmap_height <= pixelref_height |
| 426 | * |
| 427 | * pixelRefOrigin() returns this origin, or (0,0) if there is no pixelRef. |
| 428 | */ |
| 429 | SkIPoint pixelRefOrigin() const { return fPixelRefOrigin; } |
| 430 | |
| 431 | /** |
| 432 | * Assign a pixelref and origin to the bitmap. Pixelrefs are reference, |
| 433 | * so the existing one (if any) will be unref'd and the new one will be |
| 434 | * ref'd. (x,y) specify the offset within the pixelref's pixels for the |
| 435 | * top/left corner of the bitmap. For a bitmap that encompases the entire |
| 436 | * pixels of the pixelref, these will be (0,0). |
| 437 | */ |
| 438 | SkPixelRef* setPixelRef(SkPixelRef* pr, int dx, int dy); |
| 439 | |
| 440 | SkPixelRef* setPixelRef(SkPixelRef* pr, const SkIPoint& origin) { |
| 441 | return this->setPixelRef(pr, origin.fX, origin.fY); |
| 442 | } |
skia.committer@gmail.com | 1ae9111 | 2014-01-09 07:01:42 +0000 | [diff] [blame] | 443 | |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 444 | SkPixelRef* setPixelRef(SkPixelRef* pr) { |
| 445 | return this->setPixelRef(pr, 0, 0); |
| 446 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 447 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 448 | /** Call this to ensure that the bitmap points to the current pixel address |
| 449 | in the pixelref. Balance it with a call to unlockPixels(). These calls |
| 450 | are harmless if there is no pixelref. |
| 451 | */ |
| 452 | void lockPixels() const; |
| 453 | /** When you are finished access the pixel memory, call this to balance a |
| 454 | previous call to lockPixels(). This allows pixelrefs that implement |
| 455 | cached/deferred image decoding to know when there are active clients of |
| 456 | a given image. |
| 457 | */ |
| 458 | void unlockPixels() const; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 459 | |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 460 | /** |
| 461 | * Some bitmaps can return a copy of their pixels for lockPixels(), but |
| 462 | * that copy, if modified, will not be pushed back. These bitmaps should |
| 463 | * not be used as targets for a raster device/canvas (since all pixels |
| 464 | * modifications will be lost when unlockPixels() is called.) |
| 465 | */ |
| 466 | bool lockPixelsAreWritable() const; |
| 467 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 468 | /** Call this to be sure that the bitmap is valid enough to be drawn (i.e. |
| 469 | it has non-null pixels, and if required by its config, it has a |
| 470 | non-null colortable. Returns true if all of the above are met. |
| 471 | */ |
| 472 | bool readyToDraw() const { |
| 473 | return this->getPixels() != NULL && |
reed@google.com | 69e6463 | 2014-03-14 20:48:05 +0000 | [diff] [blame] | 474 | (this->colorType() != kIndex_8_SkColorType || NULL != fColorTable); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 475 | } |
| 476 | |
reed@android.com | ce4e53a | 2010-09-09 16:01:26 +0000 | [diff] [blame] | 477 | /** Returns the pixelRef's texture, or NULL |
| 478 | */ |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 479 | GrTexture* getTexture() const; |
reed@android.com | ce4e53a | 2010-09-09 16:01:26 +0000 | [diff] [blame] | 480 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 481 | /** Return the bitmap's colortable, if it uses one (i.e. colorType is |
| 482 | Index_8) and the pixels are locked. |
scroggo@google.com | 665b2cc | 2013-05-06 18:08:05 +0000 | [diff] [blame] | 483 | Otherwise returns NULL. Does not affect the colortable's |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 484 | reference count. |
| 485 | */ |
| 486 | SkColorTable* getColorTable() const { return fColorTable; } |
| 487 | |
| 488 | /** 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] | 489 | pixelref. Each time the pixels are changed (and notifyPixelsChanged |
| 490 | is called), a different generation ID will be returned. Finally, if |
| 491 | their is no pixelRef then zero is returned. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 492 | */ |
| 493 | uint32_t getGenerationID() const; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 494 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 495 | /** Call this if you have changed the contents of the pixels. This will in- |
| 496 | turn cause a different generation ID value to be returned from |
| 497 | getGenerationID(). |
| 498 | */ |
| 499 | void notifyPixelsChanged() const; |
| 500 | |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 501 | /** |
| 502 | * Fill the entire bitmap with the specified color. |
| 503 | * If the bitmap's config does not support alpha (e.g. 565) then the alpha |
| 504 | * of the color is ignored (treated as opaque). If the config only supports |
| 505 | * alpha (e.g. A1 or A8) then the color's r,g,b components are ignored. |
| 506 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 507 | void eraseColor(SkColor c) const { |
| 508 | this->eraseARGB(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c), |
| 509 | SkColorGetB(c)); |
| 510 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 511 | |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 512 | /** |
| 513 | * Fill the entire bitmap with the specified color. |
| 514 | * If the bitmap's config does not support alpha (e.g. 565) then the alpha |
| 515 | * of the color is ignored (treated as opaque). If the config only supports |
| 516 | * alpha (e.g. A1 or A8) then the color's r,g,b components are ignored. |
| 517 | */ |
| 518 | void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const; |
| 519 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 520 | SK_ATTR_DEPRECATED("use eraseARGB or eraseColor") |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 521 | void eraseRGB(U8CPU r, U8CPU g, U8CPU b) const { |
| 522 | this->eraseARGB(0xFF, r, g, b); |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * Fill the specified area of this bitmap with the specified color. |
| 527 | * If the bitmap's config does not support alpha (e.g. 565) then the alpha |
| 528 | * of the color is ignored (treated as opaque). If the config only supports |
| 529 | * alpha (e.g. A1 or A8) then the color's r,g,b components are ignored. |
| 530 | */ |
| 531 | void eraseArea(const SkIRect& area, SkColor c) const; |
| 532 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 533 | /** Scroll (a subset of) the contents of this bitmap by dx/dy. If there are |
| 534 | no pixels allocated (i.e. getPixels() returns null) the method will |
scroggo@google.com | 6a9368d | 2012-08-22 16:19:52 +0000 | [diff] [blame] | 535 | still update the inval region (if present). If the bitmap is immutable, |
| 536 | do nothing and return false. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 537 | |
| 538 | @param subset The subset of the bitmap to scroll/move. To scroll the |
| 539 | entire contents, specify [0, 0, width, height] or just |
| 540 | pass null. |
| 541 | @param dx The amount to scroll in X |
| 542 | @param dy The amount to scroll in Y |
| 543 | @param inval Optional (may be null). Returns the area of the bitmap that |
| 544 | was scrolled away. E.g. if dx = dy = 0, then inval would |
| 545 | be set to empty. If dx >= width or dy >= height, then |
| 546 | inval would be set to the entire bounds of the bitmap. |
| 547 | @return true if the scroll was doable. Will return false if the bitmap |
| 548 | uses an unsupported config for scrolling (only kA8, |
| 549 | kIndex8, kRGB_565, kARGB_4444, kARGB_8888 are supported). |
| 550 | If no pixels are present (i.e. getPixels() returns false) |
| 551 | inval will still be updated, and true will be returned. |
| 552 | */ |
| 553 | bool scrollRect(const SkIRect* subset, int dx, int dy, |
| 554 | SkRegion* inval = NULL) const; |
| 555 | |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 556 | /** |
| 557 | * Return the SkColor of the specified pixel. In most cases this will |
| 558 | * require un-premultiplying the color. Alpha only configs (A1 and A8) |
| 559 | * return black with the appropriate alpha set. The value is undefined |
| 560 | * for kNone_Config or if x or y are out of bounds, or if the bitmap |
| 561 | * does not have any pixels (or has not be locked with lockPixels()). |
| 562 | */ |
| 563 | SkColor getColor(int x, int y) const; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 564 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 565 | /** Returns the address of the specified pixel. This performs a runtime |
| 566 | check to know the size of the pixels, and will return the same answer |
| 567 | as the corresponding size-specific method (e.g. getAddr16). Since the |
| 568 | check happens at runtime, it is much slower than using a size-specific |
| 569 | version. Unlike the size-specific methods, this routine also checks if |
| 570 | getPixels() returns null, and returns that. The size-specific routines |
| 571 | perform a debugging assert that getPixels() is not null, but they do |
| 572 | not do any runtime checks. |
| 573 | */ |
| 574 | void* getAddr(int x, int y) const; |
| 575 | |
| 576 | /** 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] | 577 | * In debug build, this asserts that the pixels are allocated and locked, |
| 578 | * and that the config is 32-bit, however none of these checks are performed |
| 579 | * in the release build. |
| 580 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 581 | inline uint32_t* getAddr32(int x, int y) const; |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 582 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 583 | /** 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] | 584 | * In debug build, this asserts that the pixels are allocated and locked, |
| 585 | * and that the config is 16-bit, however none of these checks are performed |
| 586 | * in the release build. |
| 587 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 588 | inline uint16_t* getAddr16(int x, int y) const; |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 589 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 590 | /** 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] | 591 | * In debug build, this asserts that the pixels are allocated and locked, |
| 592 | * and that the config is 8-bit, however none of these checks are performed |
| 593 | * in the release build. |
| 594 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 595 | inline uint8_t* getAddr8(int x, int y) const; |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 596 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 597 | /** 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] | 598 | * colortable based bitmaps. |
| 599 | * In debug build, this asserts that the pixels are allocated and locked, |
| 600 | * that the config is kIndex8, and that the colortable is allocated, |
| 601 | * however none of these checks are performed in the release build. |
| 602 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 603 | inline SkPMColor getIndex8Color(int x, int y) const; |
| 604 | |
epoger@google.com | 9ef2d83 | 2011-07-01 21:12:20 +0000 | [diff] [blame] | 605 | /** Set dst to be a setset of this bitmap. If possible, it will share the |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 606 | pixel memory, and just point into a subset of it. However, if the config |
| 607 | does not support this, a local copy will be made and associated with |
| 608 | the dst bitmap. If the subset rectangle, intersected with the bitmap's |
| 609 | dimensions is empty, or if there is an unsupported config, false will be |
| 610 | returned and dst will be untouched. |
| 611 | @param dst The bitmap that will be set to a subset of this bitmap |
| 612 | @param subset The rectangle of pixels in this bitmap that dst will |
| 613 | reference. |
| 614 | @return true if the subset copy was successfully made. |
| 615 | */ |
| 616 | bool extractSubset(SkBitmap* dst, const SkIRect& subset) const; |
| 617 | |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 618 | /** Makes a deep copy of this bitmap, respecting the requested colorType, |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 619 | * and allocating the dst pixels on the cpu. |
| 620 | * Returns false if either there is an error (i.e. the src does not have |
| 621 | * pixels) or the request cannot be satisfied (e.g. the src has per-pixel |
| 622 | * alpha, and the requested config does not support alpha). |
| 623 | * @param dst The bitmap to be sized and allocated |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 624 | * @param ct The desired colorType for dst |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 625 | * @param allocator Allocator used to allocate the pixelref for the dst |
| 626 | * bitmap. If this is null, the standard HeapAllocator |
| 627 | * will be used. |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 628 | * @return true if the copy was made. |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 629 | */ |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 630 | bool copyTo(SkBitmap* dst, SkColorType ct, Allocator* = NULL) const; |
| 631 | |
| 632 | bool copyTo(SkBitmap* dst, Allocator* allocator = NULL) const { |
| 633 | return this->copyTo(dst, this->colorType(), allocator); |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * Returns true if this bitmap's pixels can be converted into the requested |
| 638 | * colorType, such that copyTo() could succeed. |
| 639 | */ |
| 640 | bool canCopyTo(SkColorType colorType) const; |
reed@android.com | 89bb83a | 2009-05-29 21:30:42 +0000 | [diff] [blame] | 641 | |
commit-bot@chromium.org | 6285f4f | 2014-02-20 19:08:07 +0000 | [diff] [blame] | 642 | /** Makes a deep copy of this bitmap, keeping the copied pixels |
| 643 | * in the same domain as the source: If the src pixels are allocated for |
| 644 | * the cpu, then so will the dst. If the src pixels are allocated on the |
| 645 | * gpu (typically as a texture), the it will do the same for the dst. |
| 646 | * If the request cannot be fulfilled, returns false and dst is unmodified. |
| 647 | */ |
| 648 | bool deepCopyTo(SkBitmap* dst) const; |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 649 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 650 | SK_ATTR_DEPRECATED("use setFilterLevel on SkPaint") |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 651 | void buildMipMap(bool forceRebuild = false); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 652 | |
djsollen@google.com | 4bd2bdb | 2013-03-08 18:35:13 +0000 | [diff] [blame] | 653 | #ifdef SK_BUILD_FOR_ANDROID |
| 654 | bool hasHardwareMipMap() const { |
| 655 | return (fFlags & kHasHardwareMipMap_Flag) != 0; |
| 656 | } |
| 657 | |
| 658 | void setHasHardwareMipMap(bool hasHardwareMipMap) { |
| 659 | if (hasHardwareMipMap) { |
| 660 | fFlags |= kHasHardwareMipMap_Flag; |
| 661 | } else { |
| 662 | fFlags &= ~kHasHardwareMipMap_Flag; |
| 663 | } |
| 664 | } |
| 665 | #endif |
| 666 | |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 667 | bool extractAlpha(SkBitmap* dst) const { |
| 668 | return this->extractAlpha(dst, NULL, NULL, NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 669 | } |
| 670 | |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 671 | bool extractAlpha(SkBitmap* dst, const SkPaint* paint, |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 672 | SkIPoint* offset) const { |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 673 | return this->extractAlpha(dst, paint, NULL, offset); |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 674 | } |
| 675 | |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 676 | /** Set dst to contain alpha layer of this bitmap. If destination bitmap |
| 677 | fails to be initialized, e.g. because allocator can't allocate pixels |
| 678 | for it, dst will not be modified and false will be returned. |
| 679 | |
| 680 | @param dst The bitmap to be filled with alpha layer |
| 681 | @param paint The paint to draw with |
| 682 | @param allocator Allocator used to allocate the pixelref for the dst |
| 683 | bitmap. If this is null, the standard HeapAllocator |
| 684 | will be used. |
| 685 | @param offset If not null, it is set to top-left coordinate to position |
| 686 | the returned bitmap so that it visually lines up with the |
| 687 | original |
| 688 | */ |
| 689 | bool extractAlpha(SkBitmap* dst, const SkPaint* paint, Allocator* allocator, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 690 | SkIPoint* offset) const; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 691 | |
djsollen@google.com | 21830d9 | 2012-08-07 19:49:41 +0000 | [diff] [blame] | 692 | /** The following two functions provide the means to both flatten and |
| 693 | unflatten the bitmap AND its pixels into the provided buffer. |
| 694 | It is recommended that you do not call these functions directly, |
| 695 | but instead call the write/readBitmap functions on the respective |
| 696 | buffers as they can optimize the recording process and avoid recording |
| 697 | duplicate bitmaps and pixelRefs. |
| 698 | */ |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 699 | void flatten(SkWriteBuffer&) const; |
| 700 | void unflatten(SkReadBuffer&); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 701 | |
| 702 | SkDEBUGCODE(void validate() const;) |
| 703 | |
| 704 | class Allocator : public SkRefCnt { |
| 705 | public: |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 706 | SK_DECLARE_INST_COUNT(Allocator) |
| 707 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 708 | /** Allocate the pixel memory for the bitmap, given its dimensions and |
| 709 | config. Return true on success, where success means either setPixels |
| 710 | or setPixelRef was called. The pixels need not be locked when this |
| 711 | returns. If the config requires a colortable, it also must be |
| 712 | installed via setColorTable. If false is returned, the bitmap and |
| 713 | colortable should be left unchanged. |
| 714 | */ |
| 715 | virtual bool allocPixelRef(SkBitmap*, SkColorTable*) = 0; |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 716 | private: |
| 717 | typedef SkRefCnt INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 718 | }; |
| 719 | |
| 720 | /** Subclass of Allocator that returns a pixelref that allocates its pixel |
| 721 | memory from the heap. This is the default Allocator invoked by |
| 722 | allocPixels(). |
| 723 | */ |
| 724 | class HeapAllocator : public Allocator { |
| 725 | public: |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 726 | virtual bool allocPixelRef(SkBitmap*, SkColorTable*) SK_OVERRIDE; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 727 | }; |
| 728 | |
| 729 | class RLEPixels { |
| 730 | public: |
| 731 | RLEPixels(int width, int height); |
| 732 | virtual ~RLEPixels(); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 733 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 734 | uint8_t* packedAtY(int y) const { |
| 735 | SkASSERT((unsigned)y < (unsigned)fHeight); |
| 736 | return fYPtrs[y]; |
| 737 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 738 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 739 | // called by subclasses during creation |
| 740 | void setPackedAtY(int y, uint8_t* addr) { |
| 741 | SkASSERT((unsigned)y < (unsigned)fHeight); |
| 742 | fYPtrs[y] = addr; |
| 743 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 744 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 745 | private: |
| 746 | uint8_t** fYPtrs; |
| 747 | int fHeight; |
| 748 | }; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 749 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 750 | SK_TO_STRING_NONVIRT() |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 751 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 752 | private: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 753 | struct MipMap; |
| 754 | mutable MipMap* fMipMap; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 755 | |
| 756 | mutable SkPixelRef* fPixelRef; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 757 | mutable int fPixelLockCount; |
reed@google.com | 5f62ed7 | 2014-01-15 19:59:45 +0000 | [diff] [blame] | 758 | // These are just caches from the locked pixelref |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 759 | mutable void* fPixels; |
| 760 | mutable SkColorTable* fColorTable; // only meaningful for kIndex8 |
| 761 | |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 762 | SkIPoint fPixelRefOrigin; |
| 763 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 764 | enum Flags { |
junov@chromium.org | b052129 | 2011-12-15 20:14:06 +0000 | [diff] [blame] | 765 | kImageIsOpaque_Flag = 0x01, |
| 766 | kImageIsVolatile_Flag = 0x02, |
djsollen@google.com | 4bd2bdb | 2013-03-08 18:35:13 +0000 | [diff] [blame] | 767 | kImageIsImmutable_Flag = 0x04, |
| 768 | #ifdef SK_BUILD_FOR_ANDROID |
| 769 | /* A hint for the renderer responsible for drawing this bitmap |
| 770 | * indicating that it should attempt to use mipmaps when this bitmap |
| 771 | * is drawn scaled down. |
| 772 | */ |
| 773 | kHasHardwareMipMap_Flag = 0x08, |
| 774 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 775 | }; |
| 776 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 777 | SkImageInfo fInfo; |
| 778 | |
scroggo@google.com | e5f4824 | 2013-02-25 21:47:41 +0000 | [diff] [blame] | 779 | uint32_t fRowBytes; |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 780 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 781 | uint8_t fFlags; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 782 | |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 783 | void internalErase(const SkIRect&, U8CPU a, U8CPU r, U8CPU g, U8CPU b)const; |
| 784 | |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 785 | /* Internal computations for safe size. |
| 786 | */ |
reed@google.com | 57212f9 | 2013-12-30 14:40:38 +0000 | [diff] [blame] | 787 | static int64_t ComputeSafeSize64(Config config, |
| 788 | uint32_t width, |
| 789 | uint32_t height, |
| 790 | size_t rowBytes); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 791 | static size_t ComputeSafeSize(Config config, |
| 792 | uint32_t width, |
| 793 | uint32_t height, |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 794 | size_t rowBytes); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 795 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 796 | /* Unreference any pixelrefs or colortables |
| 797 | */ |
| 798 | void freePixels(); |
| 799 | void updatePixelsFromRef() const; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 800 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 801 | static SkFixed ComputeMipLevel(SkFixed sx, SkFixed dy); |
reed@google.com | 2c31a46 | 2013-06-26 21:09:37 +0000 | [diff] [blame] | 802 | |
| 803 | /** Given scale factors sx, sy, determine the miplevel available in the |
| 804 | bitmap, and return it (this is the amount to shift matrix iterators |
| 805 | by). If dst is not null, it is set to the correct level. |
| 806 | */ |
| 807 | int extractMipLevel(SkBitmap* dst, SkFixed sx, SkFixed sy); |
| 808 | bool hasMipMap() const; |
| 809 | void freeMipMap(); |
skia.committer@gmail.com | 1f3c738 | 2013-07-20 07:00:58 +0000 | [diff] [blame] | 810 | |
reed@google.com | 4a551d4 | 2013-06-26 21:28:10 +0000 | [diff] [blame] | 811 | friend struct SkBitmapProcState; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 812 | }; |
| 813 | |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame^] | 814 | class SkAutoLockPixels : SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 815 | public: |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 816 | SkAutoLockPixels(const SkBitmap& bm, bool doLock = true) : fBitmap(bm) { |
| 817 | fDidLock = doLock; |
| 818 | if (doLock) { |
| 819 | bm.lockPixels(); |
| 820 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 821 | } |
| 822 | ~SkAutoLockPixels() { |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 823 | if (fDidLock) { |
| 824 | fBitmap.unlockPixels(); |
| 825 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | private: |
| 829 | const SkBitmap& fBitmap; |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 830 | bool fDidLock; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 831 | }; |
mtklein@google.com | 0f6dc21 | 2013-11-18 20:55:29 +0000 | [diff] [blame] | 832 | //TODO(mtklein): uncomment when 71713004 lands and Chromium's fixed. |
| 833 | //#define SkAutoLockPixels(...) SK_REQUIRE_LOCAL_VAR(SkAutoLockPixels) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 834 | |
| 835 | /** Helper class that performs the lock/unlockColors calls on a colortable. |
| 836 | The destructor will call unlockColors(false) if it has a bitmap's colortable |
| 837 | */ |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame^] | 838 | class SkAutoLockColors : SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 839 | public: |
| 840 | /** Initialize with no bitmap. Call lockColors(bitmap) to lock bitmap's |
| 841 | colortable |
| 842 | */ |
| 843 | SkAutoLockColors() : fCTable(NULL), fColors(NULL) {} |
| 844 | /** Initialize with bitmap, locking its colortable if present |
| 845 | */ |
| 846 | explicit SkAutoLockColors(const SkBitmap& bm) { |
| 847 | fCTable = bm.getColorTable(); |
| 848 | fColors = fCTable ? fCTable->lockColors() : NULL; |
| 849 | } |
| 850 | /** Initialize with a colortable (may be null) |
| 851 | */ |
| 852 | explicit SkAutoLockColors(SkColorTable* ctable) { |
| 853 | fCTable = ctable; |
| 854 | fColors = ctable ? ctable->lockColors() : NULL; |
| 855 | } |
| 856 | ~SkAutoLockColors() { |
| 857 | if (fCTable) { |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 858 | fCTable->unlockColors(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 859 | } |
| 860 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 861 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 862 | /** Return the currently locked colors, or NULL if no bitmap's colortable |
| 863 | is currently locked. |
| 864 | */ |
| 865 | const SkPMColor* colors() const { return fColors; } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 866 | |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 867 | /** Locks the table and returns is colors (assuming ctable is not null) and |
| 868 | unlocks the previous table if one was present |
| 869 | */ |
| 870 | const SkPMColor* lockColors(SkColorTable* ctable) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 871 | if (fCTable) { |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 872 | fCTable->unlockColors(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 873 | } |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 874 | fCTable = ctable; |
| 875 | fColors = ctable ? ctable->lockColors() : NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 876 | return fColors; |
| 877 | } |
| 878 | |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 879 | const SkPMColor* lockColors(const SkBitmap& bm) { |
| 880 | return this->lockColors(bm.getColorTable()); |
| 881 | } |
| 882 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 883 | private: |
| 884 | SkColorTable* fCTable; |
| 885 | const SkPMColor* fColors; |
| 886 | }; |
commit-bot@chromium.org | e61a86c | 2013-11-18 16:03:59 +0000 | [diff] [blame] | 887 | #define SkAutoLockColors(...) SK_REQUIRE_LOCAL_VAR(SkAutoLockColors) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 888 | |
| 889 | /////////////////////////////////////////////////////////////////////////////// |
| 890 | |
| 891 | inline uint32_t* SkBitmap::getAddr32(int x, int y) const { |
| 892 | SkASSERT(fPixels); |
reed@google.com | 69e6463 | 2014-03-14 20:48:05 +0000 | [diff] [blame] | 893 | SkASSERT(4 == this->bytesPerPixel()); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 894 | SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 895 | return (uint32_t*)((char*)fPixels + y * fRowBytes + (x << 2)); |
| 896 | } |
| 897 | |
| 898 | inline uint16_t* SkBitmap::getAddr16(int x, int y) const { |
| 899 | SkASSERT(fPixels); |
reed@google.com | 69e6463 | 2014-03-14 20:48:05 +0000 | [diff] [blame] | 900 | SkASSERT(2 == this->bytesPerPixel()); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 901 | SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 902 | return (uint16_t*)((char*)fPixels + y * fRowBytes + (x << 1)); |
| 903 | } |
| 904 | |
| 905 | inline uint8_t* SkBitmap::getAddr8(int x, int y) const { |
| 906 | SkASSERT(fPixels); |
reed@google.com | 69e6463 | 2014-03-14 20:48:05 +0000 | [diff] [blame] | 907 | SkASSERT(1 == this->bytesPerPixel()); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 908 | SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 909 | return (uint8_t*)fPixels + y * fRowBytes + x; |
| 910 | } |
| 911 | |
| 912 | inline SkPMColor SkBitmap::getIndex8Color(int x, int y) const { |
| 913 | SkASSERT(fPixels); |
reed@google.com | 69e6463 | 2014-03-14 20:48:05 +0000 | [diff] [blame] | 914 | SkASSERT(kIndex_8_SkColorType == this->colorType()); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 915 | SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 916 | SkASSERT(fColorTable); |
| 917 | return (*fColorTable)[*((const uint8_t*)fPixels + y * fRowBytes + x)]; |
| 918 | } |
| 919 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 920 | /////////////////////////////////////////////////////////////////////////////// |
| 921 | // |
| 922 | // Helpers until we can fully deprecate SkBitmap::Config |
| 923 | // |
| 924 | extern SkBitmap::Config SkColorTypeToBitmapConfig(SkColorType); |
| 925 | extern SkColorType SkBitmapConfigToColorType(SkBitmap::Config); |
| 926 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 927 | #endif |