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