gfx/skia/trunk/include/core/SkBitmap.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial