LCOV - app.info - gfx/skia/include/core/SkBitmap.h

LCOV - code coverage report
| Current view: | directory - gfx/skia/include/core - SkBitmap.h (source / functions) |  | Found | Hit | Coverage |
| Test: | app.info | Lines: | 75 | 0 | 0.0 %
| Date: | 2012-07-07 | Functions: | 34 | 0 | 0.0 %

           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               0 :     bool isNull() const { return NULL == fPixels && NULL == fPixelRef; }
          95                 :
          96                 :     /** Return the config for the bitmap.
          97                 :     */
          98               0 :     Config  config() const { return (Config)fConfig; }
          99                 :     /** DEPRECATED, use config()
         100                 :     */
         101               0 :     Config  getConfig() const { return this->config(); }
         102                 :     /** Return the bitmap's width, in pixels.
         103                 :     */
         104               0 :     int width() const { return fWidth; }
         105                 :     /** Return the bitmap's height, in pixels.
         106                 :     */
         107               0 :     int height() const { return fHeight; }
         108                 :     /** Return the number of bytes between subsequent rows of the bitmap.
         109                 :     */
         110               0 :     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               0 :     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               0 :     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               0 :     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               0 :     Sk64 getSize64() const {
         152                 :         Sk64 size;
         153               0 :         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               0 :     bool allocPixels(SkColorTable* ctable = NULL) {
         274               0 :         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               0 :     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               0 :     bool readyToDraw() const {
         336               0 :         return this->getPixels() != NULL &&
         337               0 :                ((this->config() != kIndex8_Config &&
         338               0 :                  this->config() != kRLE_Index8_Config) ||
         339               0 :                        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               0 :     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               0 :     void eraseColor(SkColor c) const {
         382                 :         this->eraseARGB(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c),
         383               0 :                         SkColorGetB(c));
         384               0 :     }
         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                 :     bool extractAlpha(SkBitmap* dst) const {
         517                 :         return this->extractAlpha(dst, NULL, NULL, NULL);
         518                 :     }
         519                 :
         520                 :     bool extractAlpha(SkBitmap* dst, const SkPaint* paint,
         521                 :                       SkIPoint* offset) const {
         522                 :         return this->extractAlpha(dst, paint, NULL, offset);
         523                 :     }
         524                 :
         525                 :     /** Set dst to contain alpha layer of this bitmap. If destination bitmap
         526                 :         fails to be initialized, e.g. because allocator can't allocate pixels
         527                 :         for it, dst will not be modified and false will be returned.
         528                 :
         529                 :         @param dst The bitmap to be filled with alpha layer
         530                 :         @param paint The paint to draw with
         531                 :         @param allocator Allocator used to allocate the pixelref for the dst
         532                 :                          bitmap. If this is null, the standard HeapAllocator
         533                 :                          will be used.
         534                 :         @param offset If not null, it is set to top-left coordinate to position
         535                 :                       the returned bitmap so that it visually lines up with the
         536                 :                       original
         537                 :     */
         538                 :     bool extractAlpha(SkBitmap* dst, const SkPaint* paint, Allocator* allocator,
         539                 :                       SkIPoint* offset) const;
         540                 :
         541                 :     void flatten(SkFlattenableWriteBuffer&) const;
         542                 :     void unflatten(SkFlattenableReadBuffer&);
         543                 :
         544                 :     SkDEBUGCODE(void validate() const;)
         545                 :
         546               0 :     class Allocator : public SkRefCnt {
         547                 :     public:
         548                 :         /** Allocate the pixel memory for the bitmap, given its dimensions and
         549                 :             config. Return true on success, where success means either setPixels
         550                 :             or setPixelRef was called. The pixels need not be locked when this
         551                 :             returns. If the config requires a colortable, it also must be
         552                 :             installed via setColorTable. If false is returned, the bitmap and
         553                 :             colortable should be left unchanged.
         554                 :         */
         555                 :         virtual bool allocPixelRef(SkBitmap*, SkColorTable*) = 0;
         556                 :     };
         557                 :
         558                 :     /** Subclass of Allocator that returns a pixelref that allocates its pixel
         559                 :         memory from the heap. This is the default Allocator invoked by
         560                 :         allocPixels().
         561                 :     */
         562               0 :     class HeapAllocator : public Allocator {
         563                 :     public:
         564                 :         virtual bool allocPixelRef(SkBitmap*, SkColorTable*);
         565                 :     };
         566                 :
         567                 :     class RLEPixels {
         568                 :     public:
         569                 :         RLEPixels(int width, int height);
         570                 :         virtual ~RLEPixels();
         571                 :
         572               0 :         uint8_t* packedAtY(int y) const {
         573               0 :             SkASSERT((unsigned)y < (unsigned)fHeight);
         574               0 :             return fYPtrs[y];
         575                 :         }
         576                 :
         577                 :         // called by subclasses during creation
         578                 :         void setPackedAtY(int y, uint8_t* addr) {
         579                 :             SkASSERT((unsigned)y < (unsigned)fHeight);
         580                 :             fYPtrs[y] = addr;
         581                 :         }
         582                 :
         583                 :     private:
         584                 :         uint8_t** fYPtrs;
         585                 :         int       fHeight;
         586                 :     };
         587                 :
         588                 : private:
         589                 :     struct MipMap;
         590                 :     mutable MipMap* fMipMap;
         591                 :
         592                 :     mutable SkPixelRef* fPixelRef;
         593                 :     mutable size_t      fPixelRefOffset;
         594                 :     mutable int         fPixelLockCount;
         595                 :     // either user-specified (in which case it is not treated as mutable)
         596                 :     // or a cache of the returned value from fPixelRef->lockPixels()
         597                 :     mutable void*       fPixels;
         598                 :     mutable SkColorTable* fColorTable;    // only meaningful for kIndex8
         599                 :     // When there is no pixel ref (setPixels was called) we still need a
         600                 :     // gen id for SkDevice implementations that may cache a copy of the
         601                 :     // pixels (e.g. as a gpu texture)
         602                 :     mutable int         fRawPixelGenerationID;
         603                 :
         604                 :     enum Flags {
         605                 :         kImageIsOpaque_Flag     = 0x01,
         606                 :         kImageIsVolatile_Flag   = 0x02,
         607                 :         kImageIsImmutable_Flag  = 0x04
         608                 :     };
         609                 :
         610                 :     uint32_t    fRowBytes;
         611                 :     uint32_t    fWidth;
         612                 :     uint32_t    fHeight;
         613                 :     uint8_t     fConfig;
         614                 :     uint8_t     fFlags;
         615                 :     uint8_t     fBytesPerPixel; // based on config
         616                 :
         617                 :     /* Internal computations for safe size.
         618                 :     */
         619                 :     static Sk64 ComputeSafeSize64(Config config,
         620                 :                                   uint32_t width,
         621                 :                                   uint32_t height,
         622                 :                                   uint32_t rowBytes);
         623                 :     static size_t ComputeSafeSize(Config   config,
         624                 :                                   uint32_t width,
         625                 :                                   uint32_t height,
         626                 :                                   uint32_t rowBytes);
         627                 :
         628                 :     /*  Unreference any pixelrefs or colortables
         629                 :     */
         630                 :     void freePixels();
         631                 :     void updatePixelsFromRef() const;
         632                 :
         633                 :     static SkFixed ComputeMipLevel(SkFixed sx, SkFixed dy);
         634                 : };
         635                 :
         636                 : /** \class SkColorTable
         637                 :
         638                 :     SkColorTable holds an array SkPMColors (premultiplied 32-bit colors) used by
         639                 :     8-bit bitmaps, where the bitmap bytes are interpreted as indices into the colortable.
         640                 : */
         641                 : class SkColorTable : public SkRefCnt {
         642                 : public:
         643                 :     /** Makes a deep copy of colors.
         644                 :      */
         645                 :     SkColorTable(const SkColorTable& src);
         646                 :     /** Preallocates the colortable to have 'count' colors, which
         647                 :      *  are initially set to 0.
         648                 :     */
         649                 :     explicit SkColorTable(int count);
         650                 :     explicit SkColorTable(SkFlattenableReadBuffer&);
         651                 :     SkColorTable(const SkPMColor colors[], int count);
         652                 :     virtual ~SkColorTable();
         653                 :
         654                 :     enum Flags {
         655                 :         kColorsAreOpaque_Flag   = 0x01  //!< if set, all of the colors in the table are opaque (alpha==0xFF)
         656                 :     };
         657                 :     /** Returns the flag bits for the color table. These can be changed with setFlags().
         658                 :     */
         659               0 :     unsigned getFlags() const { return fFlags; }
         660                 :     /** Set the flags for the color table. See the Flags enum for possible values.
         661                 :     */
         662                 :     void    setFlags(unsigned flags);
         663                 :
         664                 :     bool isOpaque() const { return (fFlags & kColorsAreOpaque_Flag) != 0; }
         665                 :     void setIsOpaque(bool isOpaque);
         666                 :
         667                 :     /** Returns the number of colors in the table.
         668                 :     */
         669               0 :     int count() const { return fCount; }
         670                 :
         671                 :     /** Returns the specified color from the table. In the debug build, this asserts that
         672                 :         the index is in range (0 <= index < count).
         673                 :     */
         674               0 :     SkPMColor operator[](int index) const {
         675               0 :         SkASSERT(fColors != NULL && (unsigned)index < fCount);
         676               0 :         return fColors[index];
         677                 :     }
         678                 :
         679                 :     /** Specify the number of colors in the color table. This does not initialize the colors
         680                 :         to any value, just allocates memory for them. To initialize the values, either call
         681                 :         setColors(array, count), or follow setCount(count) with a call to
         682                 :         lockColors()/{set the values}/unlockColors(true).
         683                 :     */
         684                 : //    void    setColors(int count) { this->setColors(NULL, count); }
         685                 : //    void    setColors(const SkPMColor[], int count);
         686                 :
         687                 :     /** Return the array of colors for reading and/or writing. This must be
         688                 :         balanced by a call to unlockColors(changed?), telling the colortable if
         689                 :         the colors were changed during the lock.
         690                 :     */
         691               0 :     SkPMColor* lockColors() {
         692               0 :         SkDEBUGCODE(fColorLockCount += 1;)
         693               0 :         return fColors;
         694                 :     }
         695                 :     /** Balancing call to lockColors(). If the colors have been changed, pass true.
         696                 :     */
         697                 :     void unlockColors(bool changed);
         698                 :
         699                 :     /** Similar to lockColors(), lock16BitCache() returns the array of
         700                 :         RGB16 colors that mirror the 32bit colors. However, this function
         701                 :         will return null if kColorsAreOpaque_Flag is not set.
         702                 :         Also, unlike lockColors(), the returned array here cannot be modified.
         703                 :     */
         704                 :     const uint16_t* lock16BitCache();
         705                 :     /** Balancing call to lock16BitCache().
         706                 :     */
         707               0 :     void unlock16BitCache() {
         708               0 :         SkASSERT(f16BitCacheLockCount > 0);
         709               0 :         SkDEBUGCODE(f16BitCacheLockCount -= 1);
         710               0 :     }
         711                 :
         712                 :     void flatten(SkFlattenableWriteBuffer&) const;
         713                 :
         714                 : private:
         715                 :     SkPMColor*  fColors;
         716                 :     uint16_t*   f16BitCache;
         717                 :     uint16_t    fCount;
         718                 :     uint8_t     fFlags;
         719                 :     SkDEBUGCODE(int fColorLockCount;)
         720                 :     SkDEBUGCODE(int f16BitCacheLockCount;)
         721                 :
         722                 :     void inval16BitCache();
         723                 : };
         724                 :
         725                 : class SkAutoLockPixels : public SkNoncopyable {
         726                 : public:
         727               0 :     SkAutoLockPixels(const SkBitmap& bm, bool doLock = true) : fBitmap(bm) {
         728               0 :         fDidLock = doLock;
         729               0 :         if (doLock) {
         730               0 :             bm.lockPixels();
         731                 :         }
         732               0 :     }
         733               0 :     ~SkAutoLockPixels() {
         734               0 :         if (fDidLock) {
         735               0 :             fBitmap.unlockPixels();
         736                 :         }
         737               0 :     }
         738                 :
         739                 : private:
         740                 :     const SkBitmap& fBitmap;
         741                 :     bool            fDidLock;
         742                 : };
         743                 :
         744                 : /** Helper class that performs the lock/unlockColors calls on a colortable.
         745                 :     The destructor will call unlockColors(false) if it has a bitmap's colortable
         746                 : */
         747                 : class SkAutoLockColors : public SkNoncopyable {
         748                 : public:
         749                 :     /** Initialize with no bitmap. Call lockColors(bitmap) to lock bitmap's
         750                 :         colortable
         751                 :      */
         752                 :     SkAutoLockColors() : fCTable(NULL), fColors(NULL) {}
         753                 :     /** Initialize with bitmap, locking its colortable if present
         754                 :      */
         755                 :     explicit SkAutoLockColors(const SkBitmap& bm) {
         756                 :         fCTable = bm.getColorTable();
         757                 :         fColors = fCTable ? fCTable->lockColors() : NULL;
         758                 :     }
         759                 :     /** Initialize with a colortable (may be null)
         760                 :      */
         761                 :     explicit SkAutoLockColors(SkColorTable* ctable) {
         762                 :         fCTable = ctable;
         763                 :         fColors = ctable ? ctable->lockColors() : NULL;
         764                 :     }
         765                 :     ~SkAutoLockColors() {
         766                 :         if (fCTable) {
         767                 :             fCTable->unlockColors(false);
         768                 :         }
         769                 :     }
         770                 :
         771                 :     /** Return the currently locked colors, or NULL if no bitmap's colortable
         772                 :         is currently locked.
         773                 :     */
         774                 :     const SkPMColor* colors() const { return fColors; }
         775                 :
         776                 :     /** Locks the table and returns is colors (assuming ctable is not null) and
         777                 :         unlocks the previous table if one was present
         778                 :      */
         779                 :     const SkPMColor* lockColors(SkColorTable* ctable) {
         780                 :         if (fCTable) {
         781                 :             fCTable->unlockColors(false);
         782                 :         }
         783                 :         fCTable = ctable;
         784                 :         fColors = ctable ? ctable->lockColors() : NULL;
         785                 :         return fColors;
         786                 :     }
         787                 :
         788                 :     const SkPMColor* lockColors(const SkBitmap& bm) {
         789                 :         return this->lockColors(bm.getColorTable());
         790                 :     }
         791                 :
         792                 : private:
         793                 :     SkColorTable*    fCTable;
         794                 :     const SkPMColor* fColors;
         795                 : };
         796                 :
         797                 : ///////////////////////////////////////////////////////////////////////////////
         798                 :
         799               0 : inline uint32_t* SkBitmap::getAddr32(int x, int y) const {
         800               0 :     SkASSERT(fPixels);
         801               0 :     SkASSERT(fConfig == kARGB_8888_Config);
         802               0 :     SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight);
         803               0 :     return (uint32_t*)((char*)fPixels + y * fRowBytes + (x << 2));
         804                 : }
         805                 :
         806               0 : inline uint16_t* SkBitmap::getAddr16(int x, int y) const {
         807               0 :     SkASSERT(fPixels);
         808               0 :     SkASSERT(fConfig == kRGB_565_Config || fConfig == kARGB_4444_Config);
         809               0 :     SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight);
         810               0 :     return (uint16_t*)((char*)fPixels + y * fRowBytes + (x << 1));
         811                 : }
         812                 :
         813               0 : inline uint8_t* SkBitmap::getAddr8(int x, int y) const {
         814               0 :     SkASSERT(fPixels);
         815               0 :     SkASSERT(fConfig == kA8_Config || fConfig == kIndex8_Config);
         816               0 :     SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight);
         817               0 :     return (uint8_t*)fPixels + y * fRowBytes + x;
         818                 : }
         819                 :
         820               0 : inline SkPMColor SkBitmap::getIndex8Color(int x, int y) const {
         821               0 :     SkASSERT(fPixels);
         822               0 :     SkASSERT(fConfig == kIndex8_Config);
         823               0 :     SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight);
         824               0 :     SkASSERT(fColorTable);
         825               0 :     return (*fColorTable)[*((const uint8_t*)fPixels + y * fRowBytes + x)];
         826                 : }
         827                 :
         828                 : // returns the address of the byte that contains the x coordinate
         829               0 : inline uint8_t* SkBitmap::getAddr1(int x, int y) const {
         830               0 :     SkASSERT(fPixels);
         831               0 :     SkASSERT(fConfig == kA1_Config);
         832               0 :     SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight);
         833               0 :     return (uint8_t*)fPixels + y * fRowBytes + (x >> 3);
         834                 : }
         835                 :
         836                 : #endif

Generated by: LCOV version 1.7