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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/include/core/SkBitmap.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,927 @@
     1.4 +/*
     1.5 + * Copyright 2006 The Android Open Source Project
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#ifndef SkBitmap_DEFINED
    1.12 +#define SkBitmap_DEFINED
    1.13 +
    1.14 +#include "SkColor.h"
    1.15 +#include "SkColorTable.h"
    1.16 +#include "SkImageInfo.h"
    1.17 +#include "SkPoint.h"
    1.18 +#include "SkRefCnt.h"
    1.19 +
    1.20 +struct SkMask;
    1.21 +struct SkIRect;
    1.22 +struct SkRect;
    1.23 +class SkPaint;
    1.24 +class SkPixelRef;
    1.25 +class SkPixelRefFactory;
    1.26 +class SkRegion;
    1.27 +class SkString;
    1.28 +class GrTexture;
    1.29 +
    1.30 +/** \class SkBitmap
    1.31 +
    1.32 +    The SkBitmap class specifies a raster bitmap. A bitmap has an integer width
    1.33 +    and height, and a format (config), and a pointer to the actual pixels.
    1.34 +    Bitmaps can be drawn into a SkCanvas, but they are also used to specify the
    1.35 +    target of a SkCanvas' drawing operations.
    1.36 +    A const SkBitmap exposes getAddr(), which lets a caller write its pixels;
    1.37 +    the constness is considered to apply to the bitmap's configuration, not
    1.38 +    its contents.
    1.39 +*/
    1.40 +class SK_API SkBitmap {
    1.41 +public:
    1.42 +    class SK_API Allocator;
    1.43 +
    1.44 +    enum Config {
    1.45 +        kNo_Config,         //!< bitmap has not been configured
    1.46 +        kA8_Config,         //!< 8-bits per pixel, with only alpha specified (0 is transparent, 0xFF is opaque)
    1.47 +        kIndex8_Config,     //!< 8-bits per pixel, using SkColorTable to specify the colors
    1.48 +        kRGB_565_Config,    //!< 16-bits per pixel, (see SkColorPriv.h for packing)
    1.49 +        kARGB_4444_Config,  //!< 16-bits per pixel, (see SkColorPriv.h for packing)
    1.50 +        kARGB_8888_Config,  //!< 32-bits per pixel, (see SkColorPriv.h for packing)
    1.51 +    };
    1.52 +
    1.53 +    // do not add this to the Config enum, otherwise the compiler will let us
    1.54 +    // pass this as a valid parameter for Config.
    1.55 +    enum {
    1.56 +        kConfigCount = kARGB_8888_Config + 1
    1.57 +    };
    1.58 +
    1.59 +    /**
    1.60 +     *  Default construct creates a bitmap with zero width and height, and no pixels.
    1.61 +     *  Its config is set to kNo_Config.
    1.62 +     */
    1.63 +    SkBitmap();
    1.64 +
    1.65 +    /**
    1.66 +     *  Copy the settings from the src into this bitmap. If the src has pixels
    1.67 +     *  allocated, they will be shared, not copied, so that the two bitmaps will
    1.68 +     *  reference the same memory for the pixels. If a deep copy is needed,
    1.69 +     *  where the new bitmap has its own separate copy of the pixels, use
    1.70 +     *  deepCopyTo().
    1.71 +     */
    1.72 +    SkBitmap(const SkBitmap& src);
    1.73 +
    1.74 +    ~SkBitmap();
    1.75 +
    1.76 +    /** Copies the src bitmap into this bitmap. Ownership of the src bitmap's pixels remains
    1.77 +        with the src bitmap.
    1.78 +    */
    1.79 +    SkBitmap& operator=(const SkBitmap& src);
    1.80 +    /** Swap the fields of the two bitmaps. This routine is guaranteed to never fail or throw.
    1.81 +    */
    1.82 +    //  This method is not exported to java.
    1.83 +    void swap(SkBitmap& other);
    1.84 +
    1.85 +    ///////////////////////////////////////////////////////////////////////////
    1.86 +
    1.87 +    const SkImageInfo& info() const { return fInfo; }
    1.88 +
    1.89 +    int width() const { return fInfo.fWidth; }
    1.90 +    int height() const { return fInfo.fHeight; }
    1.91 +    SkColorType colorType() const { return fInfo.fColorType; }
    1.92 +    SkAlphaType alphaType() const { return fInfo.fAlphaType; }
    1.93 +
    1.94 +    /** Return the number of bytes per pixel based on the config. If the config
    1.95 +     does not have at least 1 byte per (e.g. kA1_Config) then 0 is returned.
    1.96 +     */
    1.97 +    int bytesPerPixel() const { return fInfo.bytesPerPixel(); }
    1.98 +
    1.99 +    /** Return the rowbytes expressed as a number of pixels (like width and
   1.100 +     height). Note, for 1-byte per pixel configs like kA8_Config, this will
   1.101 +     return the same as rowBytes(). Is undefined for configs that are less
   1.102 +     than 1-byte per pixel (e.g. kA1_Config)
   1.103 +     */
   1.104 +    int rowBytesAsPixels() const {
   1.105 +        return fRowBytes >> this->shiftPerPixel();
   1.106 +    }
   1.107 +
   1.108 +    /** Return the shift amount per pixel (i.e. 0 for 1-byte per pixel, 1 for
   1.109 +     2-bytes per pixel configs, 2 for 4-bytes per pixel configs). Return 0
   1.110 +     for configs that are not at least 1-byte per pixel (e.g. kA1_Config
   1.111 +     or kNo_Config)
   1.112 +     */
   1.113 +    int shiftPerPixel() const { return this->bytesPerPixel() >> 1; }
   1.114 +
   1.115 +    ///////////////////////////////////////////////////////////////////////////
   1.116 +
   1.117 +    /** Return true iff the bitmap has empty dimensions.
   1.118 +     *  Hey!  Before you use this, see if you really want to know drawsNothing() instead.
   1.119 +     */
   1.120 +    bool empty() const { return fInfo.isEmpty(); }
   1.121 +
   1.122 +    /** Return true iff the bitmap has no pixelref. Note: this can return true even if the
   1.123 +     *  dimensions of the bitmap are > 0 (see empty()).
   1.124 +     *  Hey!  Before you use this, see if you really want to know drawsNothing() instead.
   1.125 +     */
   1.126 +    bool isNull() const { return NULL == fPixelRef; }
   1.127 +
   1.128 +    /** Return true iff drawing this bitmap has no effect.
   1.129 +     */
   1.130 +    bool drawsNothing() const { return this->empty() || this->isNull(); }
   1.131 +
   1.132 +    /** Return the config for the bitmap. */
   1.133 +    Config  config() const;
   1.134 +
   1.135 +    SK_ATTR_DEPRECATED("use config()")
   1.136 +    Config  getConfig() const { return this->config(); }
   1.137 +
   1.138 +    /** Return the number of bytes between subsequent rows of the bitmap. */
   1.139 +    size_t rowBytes() const { return fRowBytes; }
   1.140 +
   1.141 +    /**
   1.142 +     *  Set the bitmap's alphaType, returning true on success. If false is
   1.143 +     *  returned, then the specified new alphaType is incompatible with the
   1.144 +     *  Config, and the current alphaType is unchanged.
   1.145 +     *
   1.146 +     *  Note: this changes the alphatype for the underlying pixels, which means
   1.147 +     *  that all bitmaps that might be sharing (subsets of) the pixels will
   1.148 +     *  be affected.
   1.149 +     */
   1.150 +    bool setAlphaType(SkAlphaType);
   1.151 +
   1.152 +    /** Return the address of the pixels for this SkBitmap.
   1.153 +    */
   1.154 +    void* getPixels() const { return fPixels; }
   1.155 +
   1.156 +    /** Return the byte size of the pixels, based on the height and rowBytes.
   1.157 +        Note this truncates the result to 32bits. Call getSize64() to detect
   1.158 +        if the real size exceeds 32bits.
   1.159 +    */
   1.160 +    size_t getSize() const { return fInfo.fHeight * fRowBytes; }
   1.161 +
   1.162 +    /** Return the number of bytes from the pointer returned by getPixels()
   1.163 +        to the end of the allocated space in the buffer. Required in
   1.164 +        cases where extractSubset has been called.
   1.165 +    */
   1.166 +    size_t getSafeSize() const { return fInfo.getSafeSize(fRowBytes); }
   1.167 +
   1.168 +    /**
   1.169 +     *  Return the full size of the bitmap, in bytes.
   1.170 +     */
   1.171 +    int64_t computeSize64() const {
   1.172 +        return sk_64_mul(fInfo.fHeight, fRowBytes);
   1.173 +    }
   1.174 +
   1.175 +    /**
   1.176 +     *  Return the number of bytes from the pointer returned by getPixels()
   1.177 +     *  to the end of the allocated space in the buffer. This may be smaller
   1.178 +     *  than computeSize64() if there is any rowbytes padding beyond the width.
   1.179 +     */
   1.180 +    int64_t computeSafeSize64() const {
   1.181 +        return fInfo.getSafeSize64(fRowBytes);
   1.182 +    }
   1.183 +
   1.184 +    /** Returns true if this bitmap is marked as immutable, meaning that the
   1.185 +        contents of its pixels will not change for the lifetime of the bitmap.
   1.186 +    */
   1.187 +    bool isImmutable() const;
   1.188 +
   1.189 +    /** Marks this bitmap as immutable, meaning that the contents of its
   1.190 +        pixels will not change for the lifetime of the bitmap and of the
   1.191 +        underlying pixelref. This state can be set, but it cannot be
   1.192 +        cleared once it is set. This state propagates to all other bitmaps
   1.193 +        that share the same pixelref.
   1.194 +    */
   1.195 +    void setImmutable();
   1.196 +
   1.197 +    /** Returns true if the bitmap is opaque (has no translucent/transparent pixels).
   1.198 +    */
   1.199 +    bool isOpaque() const {
   1.200 +        return SkAlphaTypeIsOpaque(this->alphaType());
   1.201 +    }
   1.202 +
   1.203 +    /** Returns true if the bitmap is volatile (i.e. should not be cached by devices.)
   1.204 +    */
   1.205 +    bool isVolatile() const;
   1.206 +
   1.207 +    /** Specify whether this bitmap is volatile. Bitmaps are not volatile by
   1.208 +        default. Temporary bitmaps that are discarded after use should be
   1.209 +        marked as volatile. This provides a hint to the device that the bitmap
   1.210 +        should not be cached. Providing this hint when appropriate can
   1.211 +        improve performance by avoiding unnecessary overhead and resource
   1.212 +        consumption on the device.
   1.213 +    */
   1.214 +    void setIsVolatile(bool);
   1.215 +
   1.216 +    /** Reset the bitmap to its initial state (see default constructor). If we are a (shared)
   1.217 +        owner of the pixels, that ownership is decremented.
   1.218 +    */
   1.219 +    void reset();
   1.220 +
   1.221 +    /** Given a config and a width, this computes the optimal rowBytes value. This is called automatically
   1.222 +        if you pass 0 for rowBytes to setConfig().
   1.223 +    */
   1.224 +    static size_t ComputeRowBytes(Config c, int width);
   1.225 +
   1.226 +    /** Return the bytes-per-pixel for the specified config. If the config is
   1.227 +        not at least 1-byte per pixel, return 0, including for kNo_Config.
   1.228 +    */
   1.229 +    static int ComputeBytesPerPixel(Config c);
   1.230 +
   1.231 +    /** Return the shift-per-pixel for the specified config. If the config is
   1.232 +     not at least 1-byte per pixel, return 0, including for kNo_Config.
   1.233 +     */
   1.234 +    static int ComputeShiftPerPixel(Config c) {
   1.235 +        return ComputeBytesPerPixel(c) >> 1;
   1.236 +    }
   1.237 +
   1.238 +    static int64_t ComputeSize64(Config, int width, int height);
   1.239 +    static size_t ComputeSize(Config, int width, int height);
   1.240 +
   1.241 +    /**
   1.242 +     *  This will brute-force return true if all of the pixels in the bitmap
   1.243 +     *  are opaque. If it fails to read the pixels, or encounters an error,
   1.244 +     *  it will return false.
   1.245 +     *
   1.246 +     *  Since this can be an expensive operation, the bitmap stores a flag for
   1.247 +     *  this (isOpaque). Only call this if you need to compute this value from
   1.248 +     *  "unknown" pixels.
   1.249 +     */
   1.250 +    static bool ComputeIsOpaque(const SkBitmap&);
   1.251 +
   1.252 +    /**
   1.253 +     *  Return the bitmap's bounds [0, 0, width, height] as an SkRect
   1.254 +     */
   1.255 +    void getBounds(SkRect* bounds) const;
   1.256 +    void getBounds(SkIRect* bounds) const;
   1.257 +
   1.258 +    /** Set the bitmap's config and dimensions. If rowBytes is 0, then
   1.259 +        ComputeRowBytes() is called to compute the optimal value. This resets
   1.260 +        any pixel/colortable ownership, just like reset().
   1.261 +    */
   1.262 +    bool setConfig(Config, int width, int height, size_t rowBytes, SkAlphaType);
   1.263 +
   1.264 +    bool setConfig(Config config, int width, int height, size_t rowBytes = 0) {
   1.265 +        return this->setConfig(config, width, height, rowBytes,
   1.266 +                               kPremul_SkAlphaType);
   1.267 +    }
   1.268 +
   1.269 +    bool setConfig(const SkImageInfo& info, size_t rowBytes = 0);
   1.270 +
   1.271 +    /**
   1.272 +     *  Allocate a pixelref to match the specified image info. If the Factory
   1.273 +     *  is non-null, call it to allcoate the pixelref. If the ImageInfo requires
   1.274 +     *  a colortable, then ColorTable must be non-null, and will be ref'd.
   1.275 +     *  On failure, the bitmap will be set to empty and return false.
   1.276 +     */
   1.277 +    bool allocPixels(const SkImageInfo&, SkPixelRefFactory*, SkColorTable*);
   1.278 +
   1.279 +    /**
   1.280 +     *  Allocate a pixelref to match the specified image info, using the default
   1.281 +     *  allocator.
   1.282 +     *  On success, the bitmap's pixels will be "locked", and return true.
   1.283 +     *  On failure, the bitmap will be set to empty and return false.
   1.284 +     */
   1.285 +    bool allocPixels(const SkImageInfo& info) {
   1.286 +        return this->allocPixels(info, NULL, NULL);
   1.287 +    }
   1.288 +
   1.289 +    /**
   1.290 +     *  Legacy helper function, which creates an SkImageInfo from the specified
   1.291 +     *  config and then calls allocPixels(info).
   1.292 +     */
   1.293 +    bool allocConfigPixels(Config, int width, int height, bool isOpaque = false);
   1.294 +
   1.295 +    bool allocN32Pixels(int width, int height, bool isOpaque = false) {
   1.296 +        SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
   1.297 +        if (isOpaque) {
   1.298 +            info.fAlphaType = kOpaque_SkAlphaType;
   1.299 +        }
   1.300 +        return this->allocPixels(info);
   1.301 +    }
   1.302 +
   1.303 +    /**
   1.304 +     *  Install a pixelref that wraps the specified pixels and rowBytes, and
   1.305 +     *  optional ReleaseProc and context. When the pixels are no longer
   1.306 +     *  referenced, if ReleaseProc is not null, it will be called with the
   1.307 +     *  pixels and context as parameters.
   1.308 +     *  On failure, the bitmap will be set to empty and return false.
   1.309 +     */
   1.310 +    bool installPixels(const SkImageInfo&, void* pixels, size_t rowBytes,
   1.311 +                       void (*ReleaseProc)(void* addr, void* context),
   1.312 +                       void* context);
   1.313 +
   1.314 +    /**
   1.315 +     *  Call installPixels with no ReleaseProc specified. This means that the
   1.316 +     *  caller must ensure that the specified pixels are valid for the lifetime
   1.317 +     *  of the created bitmap (and its pixelRef).
   1.318 +     */
   1.319 +    bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
   1.320 +        return this->installPixels(info, pixels, rowBytes, NULL, NULL);
   1.321 +    }
   1.322 +
   1.323 +    /**
   1.324 +     *  Calls installPixels() with the value in the SkMask. The caller must
   1.325 +     *  ensure that the specified mask pixels are valid for the lifetime
   1.326 +     *  of the created bitmap (and its pixelRef).
   1.327 +     */
   1.328 +    bool installMaskPixels(const SkMask&);
   1.329 +
   1.330 +    /**
   1.331 +     *  DEPRECATED: call info().
   1.332 +     */
   1.333 +    bool asImageInfo(SkImageInfo* info) const {
   1.334 +        // compatibility: return false for kUnknown
   1.335 +        if (kUnknown_SkColorType == this->colorType()) {
   1.336 +            return false;
   1.337 +        }
   1.338 +        if (info) {
   1.339 +            *info = this->info();
   1.340 +        }
   1.341 +        return true;
   1.342 +    }
   1.343 +
   1.344 +    /** Use this to assign a new pixel address for an existing bitmap. This
   1.345 +        will automatically release any pixelref previously installed. Only call
   1.346 +        this if you are handling ownership/lifetime of the pixel memory.
   1.347 +
   1.348 +        If the bitmap retains a reference to the colortable (assuming it is
   1.349 +        not null) it will take care of incrementing the reference count.
   1.350 +
   1.351 +        @param pixels   Address for the pixels, managed by the caller.
   1.352 +        @param ctable   ColorTable (or null) that matches the specified pixels
   1.353 +    */
   1.354 +    void setPixels(void* p, SkColorTable* ctable = NULL);
   1.355 +
   1.356 +    /** Copies the bitmap's pixels to the location pointed at by dst and returns
   1.357 +        true if possible, returns false otherwise.
   1.358 +
   1.359 +        In the case when the dstRowBytes matches the bitmap's rowBytes, the copy
   1.360 +        may be made faster by copying over the dst's per-row padding (for all
   1.361 +        rows but the last). By setting preserveDstPad to true the caller can
   1.362 +        disable this optimization and ensure that pixels in the padding are not
   1.363 +        overwritten.
   1.364 +
   1.365 +        Always returns false for RLE formats.
   1.366 +
   1.367 +        @param dst      Location of destination buffer.
   1.368 +        @param dstSize  Size of destination buffer. Must be large enough to hold
   1.369 +                        pixels using indicated stride.
   1.370 +        @param dstRowBytes  Width of each line in the buffer. If 0, uses
   1.371 +                            bitmap's internal stride.
   1.372 +        @param preserveDstPad Must we preserve padding in the dst
   1.373 +    */
   1.374 +    bool copyPixelsTo(void* const dst, size_t dstSize, size_t dstRowBytes = 0,
   1.375 +                      bool preserveDstPad = false) const;
   1.376 +
   1.377 +    /** Use the standard HeapAllocator to create the pixelref that manages the
   1.378 +        pixel memory. It will be sized based on the current width/height/config.
   1.379 +        If this is called multiple times, a new pixelref object will be created
   1.380 +        each time.
   1.381 +
   1.382 +        If the bitmap retains a reference to the colortable (assuming it is
   1.383 +        not null) it will take care of incrementing the reference count.
   1.384 +
   1.385 +        @param ctable   ColorTable (or null) to use with the pixels that will
   1.386 +                        be allocated. Only used if config == Index8_Config
   1.387 +        @return true if the allocation succeeds. If not the pixelref field of
   1.388 +                     the bitmap will be unchanged.
   1.389 +    */
   1.390 +    bool allocPixels(SkColorTable* ctable = NULL) {
   1.391 +        return this->allocPixels(NULL, ctable);
   1.392 +    }
   1.393 +
   1.394 +    /** Use the specified Allocator to create the pixelref that manages the
   1.395 +        pixel memory. It will be sized based on the current width/height/config.
   1.396 +        If this is called multiple times, a new pixelref object will be created
   1.397 +        each time.
   1.398 +
   1.399 +        If the bitmap retains a reference to the colortable (assuming it is
   1.400 +        not null) it will take care of incrementing the reference count.
   1.401 +
   1.402 +        @param allocator The Allocator to use to create a pixelref that can
   1.403 +                         manage the pixel memory for the current
   1.404 +                         width/height/config. If allocator is NULL, the standard
   1.405 +                         HeapAllocator will be used.
   1.406 +        @param ctable   ColorTable (or null) to use with the pixels that will
   1.407 +                        be allocated. Only used if config == Index8_Config.
   1.408 +                        If it is non-null and the config is not Index8, it will
   1.409 +                        be ignored.
   1.410 +        @return true if the allocation succeeds. If not the pixelref field of
   1.411 +                     the bitmap will be unchanged.
   1.412 +    */
   1.413 +    bool allocPixels(Allocator* allocator, SkColorTable* ctable);
   1.414 +
   1.415 +    /**
   1.416 +     *  Return the current pixelref object or NULL if there is none. This does
   1.417 +     *  not affect the refcount of the pixelref.
   1.418 +     */
   1.419 +    SkPixelRef* pixelRef() const { return fPixelRef; }
   1.420 +
   1.421 +    /**
   1.422 +     *  A bitmap can reference a subset of a pixelref's pixels. That means the
   1.423 +     *  bitmap's width/height can be <= the dimensions of the pixelref. The
   1.424 +     *  pixelref origin is the x,y location within the pixelref's pixels for
   1.425 +     *  the bitmap's top/left corner. To be valid the following must be true:
   1.426 +     *
   1.427 +     *  origin_x + bitmap_width  <= pixelref_width
   1.428 +     *  origin_y + bitmap_height <= pixelref_height
   1.429 +     *
   1.430 +     *  pixelRefOrigin() returns this origin, or (0,0) if there is no pixelRef.
   1.431 +     */
   1.432 +    SkIPoint pixelRefOrigin() const { return fPixelRefOrigin; }
   1.433 +
   1.434 +    /**
   1.435 +     *  Assign a pixelref and origin to the bitmap. Pixelrefs are reference,
   1.436 +     *  so the existing one (if any) will be unref'd and the new one will be
   1.437 +     *  ref'd. (x,y) specify the offset within the pixelref's pixels for the
   1.438 +     *  top/left corner of the bitmap. For a bitmap that encompases the entire
   1.439 +     *  pixels of the pixelref, these will be (0,0).
   1.440 +     */
   1.441 +    SkPixelRef* setPixelRef(SkPixelRef* pr, int dx, int dy);
   1.442 +
   1.443 +    SkPixelRef* setPixelRef(SkPixelRef* pr, const SkIPoint& origin) {
   1.444 +        return this->setPixelRef(pr, origin.fX, origin.fY);
   1.445 +    }
   1.446 +
   1.447 +    SkPixelRef* setPixelRef(SkPixelRef* pr) {
   1.448 +        return this->setPixelRef(pr, 0, 0);
   1.449 +    }
   1.450 +
   1.451 +    /** Call this to ensure that the bitmap points to the current pixel address
   1.452 +        in the pixelref. Balance it with a call to unlockPixels(). These calls
   1.453 +        are harmless if there is no pixelref.
   1.454 +    */
   1.455 +    void lockPixels() const;
   1.456 +    /** When you are finished access the pixel memory, call this to balance a
   1.457 +        previous call to lockPixels(). This allows pixelrefs that implement
   1.458 +        cached/deferred image decoding to know when there are active clients of
   1.459 +        a given image.
   1.460 +    */
   1.461 +    void unlockPixels() const;
   1.462 +
   1.463 +    /**
   1.464 +     *  Some bitmaps can return a copy of their pixels for lockPixels(), but
   1.465 +     *  that copy, if modified, will not be pushed back. These bitmaps should
   1.466 +     *  not be used as targets for a raster device/canvas (since all pixels
   1.467 +     *  modifications will be lost when unlockPixels() is called.)
   1.468 +     */
   1.469 +    bool lockPixelsAreWritable() const;
   1.470 +
   1.471 +    /** Call this to be sure that the bitmap is valid enough to be drawn (i.e.
   1.472 +        it has non-null pixels, and if required by its config, it has a
   1.473 +        non-null colortable. Returns true if all of the above are met.
   1.474 +    */
   1.475 +    bool readyToDraw() const {
   1.476 +        return this->getPixels() != NULL &&
   1.477 +               (this->colorType() != kIndex_8_SkColorType || NULL != fColorTable);
   1.478 +    }
   1.479 +
   1.480 +    /** Returns the pixelRef's texture, or NULL
   1.481 +     */
   1.482 +    GrTexture* getTexture() const;
   1.483 +
   1.484 +    /** Return the bitmap's colortable, if it uses one (i.e. colorType is
   1.485 +        Index_8) and the pixels are locked.
   1.486 +        Otherwise returns NULL. Does not affect the colortable's
   1.487 +        reference count.
   1.488 +    */
   1.489 +    SkColorTable* getColorTable() const { return fColorTable; }
   1.490 +
   1.491 +    /** Returns a non-zero, unique value corresponding to the pixels in our
   1.492 +        pixelref. Each time the pixels are changed (and notifyPixelsChanged
   1.493 +        is called), a different generation ID will be returned. Finally, if
   1.494 +        their is no pixelRef then zero is returned.
   1.495 +    */
   1.496 +    uint32_t getGenerationID() const;
   1.497 +
   1.498 +    /** Call this if you have changed the contents of the pixels. This will in-
   1.499 +        turn cause a different generation ID value to be returned from
   1.500 +        getGenerationID().
   1.501 +    */
   1.502 +    void notifyPixelsChanged() const;
   1.503 +
   1.504 +    /**
   1.505 +     *  Fill the entire bitmap with the specified color.
   1.506 +     *  If the bitmap's config does not support alpha (e.g. 565) then the alpha
   1.507 +     *  of the color is ignored (treated as opaque). If the config only supports
   1.508 +     *  alpha (e.g. A1 or A8) then the color's r,g,b components are ignored.
   1.509 +     */
   1.510 +    void eraseColor(SkColor c) const {
   1.511 +        this->eraseARGB(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c),
   1.512 +                        SkColorGetB(c));
   1.513 +    }
   1.514 +
   1.515 +    /**
   1.516 +     *  Fill the entire bitmap with the specified color.
   1.517 +     *  If the bitmap's config does not support alpha (e.g. 565) then the alpha
   1.518 +     *  of the color is ignored (treated as opaque). If the config only supports
   1.519 +     *  alpha (e.g. A1 or A8) then the color's r,g,b components are ignored.
   1.520 +     */
   1.521 +    void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const;
   1.522 +
   1.523 +    SK_ATTR_DEPRECATED("use eraseARGB or eraseColor")
   1.524 +    void eraseRGB(U8CPU r, U8CPU g, U8CPU b) const {
   1.525 +        this->eraseARGB(0xFF, r, g, b);
   1.526 +    }
   1.527 +
   1.528 +    /**
   1.529 +     *  Fill the specified area of this bitmap with the specified color.
   1.530 +     *  If the bitmap's config does not support alpha (e.g. 565) then the alpha
   1.531 +     *  of the color is ignored (treated as opaque). If the config only supports
   1.532 +     *  alpha (e.g. A1 or A8) then the color's r,g,b components are ignored.
   1.533 +     */
   1.534 +    void eraseArea(const SkIRect& area, SkColor c) const;
   1.535 +
   1.536 +    /** Scroll (a subset of) the contents of this bitmap by dx/dy. If there are
   1.537 +        no pixels allocated (i.e. getPixels() returns null) the method will
   1.538 +        still update the inval region (if present). If the bitmap is immutable,
   1.539 +        do nothing and return false.
   1.540 +
   1.541 +        @param subset The subset of the bitmap to scroll/move. To scroll the
   1.542 +                      entire contents, specify [0, 0, width, height] or just
   1.543 +                      pass null.
   1.544 +        @param dx The amount to scroll in X
   1.545 +        @param dy The amount to scroll in Y
   1.546 +        @param inval Optional (may be null). Returns the area of the bitmap that
   1.547 +                     was scrolled away. E.g. if dx = dy = 0, then inval would
   1.548 +                     be set to empty. If dx >= width or dy >= height, then
   1.549 +                     inval would be set to the entire bounds of the bitmap.
   1.550 +        @return true if the scroll was doable. Will return false if the bitmap
   1.551 +                     uses an unsupported config for scrolling (only kA8,
   1.552 +                     kIndex8, kRGB_565, kARGB_4444, kARGB_8888 are supported).
   1.553 +                     If no pixels are present (i.e. getPixels() returns false)
   1.554 +                     inval will still be updated, and true will be returned.
   1.555 +    */
   1.556 +    bool scrollRect(const SkIRect* subset, int dx, int dy,
   1.557 +                    SkRegion* inval = NULL) const;
   1.558 +
   1.559 +    /**
   1.560 +     *  Return the SkColor of the specified pixel.  In most cases this will
   1.561 +     *  require un-premultiplying the color.  Alpha only configs (A1 and A8)
   1.562 +     *  return black with the appropriate alpha set.  The value is undefined
   1.563 +     *  for kNone_Config or if x or y are out of bounds, or if the bitmap
   1.564 +     *  does not have any pixels (or has not be locked with lockPixels()).
   1.565 +     */
   1.566 +    SkColor getColor(int x, int y) const;
   1.567 +
   1.568 +    /** Returns the address of the specified pixel. This performs a runtime
   1.569 +        check to know the size of the pixels, and will return the same answer
   1.570 +        as the corresponding size-specific method (e.g. getAddr16). Since the
   1.571 +        check happens at runtime, it is much slower than using a size-specific
   1.572 +        version. Unlike the size-specific methods, this routine also checks if
   1.573 +        getPixels() returns null, and returns that. The size-specific routines
   1.574 +        perform a debugging assert that getPixels() is not null, but they do
   1.575 +        not do any runtime checks.
   1.576 +    */
   1.577 +    void* getAddr(int x, int y) const;
   1.578 +
   1.579 +    /** Returns the address of the pixel specified by x,y for 32bit pixels.
   1.580 +     *  In debug build, this asserts that the pixels are allocated and locked,
   1.581 +     *  and that the config is 32-bit, however none of these checks are performed
   1.582 +     *  in the release build.
   1.583 +     */
   1.584 +    inline uint32_t* getAddr32(int x, int y) const;
   1.585 +
   1.586 +    /** Returns the address of the pixel specified by x,y for 16bit pixels.
   1.587 +     *  In debug build, this asserts that the pixels are allocated and locked,
   1.588 +     *  and that the config is 16-bit, however none of these checks are performed
   1.589 +     *  in the release build.
   1.590 +     */
   1.591 +    inline uint16_t* getAddr16(int x, int y) const;
   1.592 +
   1.593 +    /** Returns the address of the pixel specified by x,y for 8bit pixels.
   1.594 +     *  In debug build, this asserts that the pixels are allocated and locked,
   1.595 +     *  and that the config is 8-bit, however none of these checks are performed
   1.596 +     *  in the release build.
   1.597 +     */
   1.598 +    inline uint8_t* getAddr8(int x, int y) const;
   1.599 +
   1.600 +    /** Returns the color corresponding to the pixel specified by x,y for
   1.601 +     *  colortable based bitmaps.
   1.602 +     *  In debug build, this asserts that the pixels are allocated and locked,
   1.603 +     *  that the config is kIndex8, and that the colortable is allocated,
   1.604 +     *  however none of these checks are performed in the release build.
   1.605 +     */
   1.606 +    inline SkPMColor getIndex8Color(int x, int y) const;
   1.607 +
   1.608 +    /** Set dst to be a setset of this bitmap. If possible, it will share the
   1.609 +        pixel memory, and just point into a subset of it. However, if the config
   1.610 +        does not support this, a local copy will be made and associated with
   1.611 +        the dst bitmap. If the subset rectangle, intersected with the bitmap's
   1.612 +        dimensions is empty, or if there is an unsupported config, false will be
   1.613 +        returned and dst will be untouched.
   1.614 +        @param dst  The bitmap that will be set to a subset of this bitmap
   1.615 +        @param subset The rectangle of pixels in this bitmap that dst will
   1.616 +                      reference.
   1.617 +        @return true if the subset copy was successfully made.
   1.618 +    */
   1.619 +    bool extractSubset(SkBitmap* dst, const SkIRect& subset) const;
   1.620 +
   1.621 +    /** Makes a deep copy of this bitmap, respecting the requested colorType,
   1.622 +     *  and allocating the dst pixels on the cpu.
   1.623 +     *  Returns false if either there is an error (i.e. the src does not have
   1.624 +     *  pixels) or the request cannot be satisfied (e.g. the src has per-pixel
   1.625 +     *  alpha, and the requested config does not support alpha).
   1.626 +     *  @param dst The bitmap to be sized and allocated
   1.627 +     *  @param ct The desired colorType for dst
   1.628 +     *  @param allocator Allocator used to allocate the pixelref for the dst
   1.629 +     *                   bitmap. If this is null, the standard HeapAllocator
   1.630 +     *                   will be used.
   1.631 +     *  @return true if the copy was made.
   1.632 +     */
   1.633 +    bool copyTo(SkBitmap* dst, SkColorType ct, Allocator* = NULL) const;
   1.634 +
   1.635 +    bool copyTo(SkBitmap* dst, Allocator* allocator = NULL) const {
   1.636 +        return this->copyTo(dst, this->colorType(), allocator);
   1.637 +    }
   1.638 +
   1.639 +    /**
   1.640 +     *  Returns true if this bitmap's pixels can be converted into the requested
   1.641 +     *  colorType, such that copyTo() could succeed.
   1.642 +     */
   1.643 +    bool canCopyTo(SkColorType colorType) const;
   1.644 +
   1.645 +    /** Makes a deep copy of this bitmap, keeping the copied pixels
   1.646 +     *  in the same domain as the source: If the src pixels are allocated for
   1.647 +     *  the cpu, then so will the dst. If the src pixels are allocated on the
   1.648 +     *  gpu (typically as a texture), the it will do the same for the dst.
   1.649 +     *  If the request cannot be fulfilled, returns false and dst is unmodified.
   1.650 +     */
   1.651 +    bool deepCopyTo(SkBitmap* dst) const;
   1.652 +
   1.653 +    SK_ATTR_DEPRECATED("use setFilterLevel on SkPaint")
   1.654 +    void buildMipMap(bool forceRebuild = false);
   1.655 +
   1.656 +#ifdef SK_BUILD_FOR_ANDROID
   1.657 +    bool hasHardwareMipMap() const {
   1.658 +        return (fFlags & kHasHardwareMipMap_Flag) != 0;
   1.659 +    }
   1.660 +
   1.661 +    void setHasHardwareMipMap(bool hasHardwareMipMap) {
   1.662 +        if (hasHardwareMipMap) {
   1.663 +            fFlags |= kHasHardwareMipMap_Flag;
   1.664 +        } else {
   1.665 +            fFlags &= ~kHasHardwareMipMap_Flag;
   1.666 +        }
   1.667 +    }
   1.668 +#endif
   1.669 +
   1.670 +    bool extractAlpha(SkBitmap* dst) const {
   1.671 +        return this->extractAlpha(dst, NULL, NULL, NULL);
   1.672 +    }
   1.673 +
   1.674 +    bool extractAlpha(SkBitmap* dst, const SkPaint* paint,
   1.675 +                      SkIPoint* offset) const {
   1.676 +        return this->extractAlpha(dst, paint, NULL, offset);
   1.677 +    }
   1.678 +
   1.679 +    /** Set dst to contain alpha layer of this bitmap. If destination bitmap
   1.680 +        fails to be initialized, e.g. because allocator can't allocate pixels
   1.681 +        for it, dst will not be modified and false will be returned.
   1.682 +
   1.683 +        @param dst The bitmap to be filled with alpha layer
   1.684 +        @param paint The paint to draw with
   1.685 +        @param allocator Allocator used to allocate the pixelref for the dst
   1.686 +                         bitmap. If this is null, the standard HeapAllocator
   1.687 +                         will be used.
   1.688 +        @param offset If not null, it is set to top-left coordinate to position
   1.689 +                      the returned bitmap so that it visually lines up with the
   1.690 +                      original
   1.691 +    */
   1.692 +    bool extractAlpha(SkBitmap* dst, const SkPaint* paint, Allocator* allocator,
   1.693 +                      SkIPoint* offset) const;
   1.694 +
   1.695 +    /** The following two functions provide the means to both flatten and
   1.696 +        unflatten the bitmap AND its pixels into the provided buffer.
   1.697 +        It is recommended that you do not call these functions directly,
   1.698 +        but instead call the write/readBitmap functions on the respective
   1.699 +        buffers as they can optimize the recording process and avoid recording
   1.700 +        duplicate bitmaps and pixelRefs.
   1.701 +     */
   1.702 +    void flatten(SkWriteBuffer&) const;
   1.703 +    void unflatten(SkReadBuffer&);
   1.704 +
   1.705 +    SkDEBUGCODE(void validate() const;)
   1.706 +
   1.707 +    class Allocator : public SkRefCnt {
   1.708 +    public:
   1.709 +        SK_DECLARE_INST_COUNT(Allocator)
   1.710 +
   1.711 +        /** Allocate the pixel memory for the bitmap, given its dimensions and
   1.712 +            config. Return true on success, where success means either setPixels
   1.713 +            or setPixelRef was called. The pixels need not be locked when this
   1.714 +            returns. If the config requires a colortable, it also must be
   1.715 +            installed via setColorTable. If false is returned, the bitmap and
   1.716 +            colortable should be left unchanged.
   1.717 +        */
   1.718 +        virtual bool allocPixelRef(SkBitmap*, SkColorTable*) = 0;
   1.719 +    private:
   1.720 +        typedef SkRefCnt INHERITED;
   1.721 +    };
   1.722 +
   1.723 +    /** Subclass of Allocator that returns a pixelref that allocates its pixel
   1.724 +        memory from the heap. This is the default Allocator invoked by
   1.725 +        allocPixels().
   1.726 +    */
   1.727 +    class HeapAllocator : public Allocator {
   1.728 +    public:
   1.729 +        virtual bool allocPixelRef(SkBitmap*, SkColorTable*) SK_OVERRIDE;
   1.730 +    };
   1.731 +
   1.732 +    class RLEPixels {
   1.733 +    public:
   1.734 +        RLEPixels(int width, int height);
   1.735 +        virtual ~RLEPixels();
   1.736 +
   1.737 +        uint8_t* packedAtY(int y) const {
   1.738 +            SkASSERT((unsigned)y < (unsigned)fHeight);
   1.739 +            return fYPtrs[y];
   1.740 +        }
   1.741 +
   1.742 +        // called by subclasses during creation
   1.743 +        void setPackedAtY(int y, uint8_t* addr) {
   1.744 +            SkASSERT((unsigned)y < (unsigned)fHeight);
   1.745 +            fYPtrs[y] = addr;
   1.746 +        }
   1.747 +
   1.748 +    private:
   1.749 +        uint8_t** fYPtrs;
   1.750 +        int       fHeight;
   1.751 +    };
   1.752 +
   1.753 +    SK_TO_STRING_NONVIRT()
   1.754 +
   1.755 +private:
   1.756 +    struct MipMap;
   1.757 +    mutable MipMap* fMipMap;
   1.758 +
   1.759 +    mutable SkPixelRef* fPixelRef;
   1.760 +    mutable int         fPixelLockCount;
   1.761 +    // These are just caches from the locked pixelref
   1.762 +    mutable void*       fPixels;
   1.763 +    mutable SkColorTable* fColorTable;    // only meaningful for kIndex8
   1.764 +
   1.765 +    SkIPoint    fPixelRefOrigin;
   1.766 +
   1.767 +    enum Flags {
   1.768 +        kImageIsOpaque_Flag     = 0x01,
   1.769 +        kImageIsVolatile_Flag   = 0x02,
   1.770 +        kImageIsImmutable_Flag  = 0x04,
   1.771 +#ifdef SK_BUILD_FOR_ANDROID
   1.772 +        /* A hint for the renderer responsible for drawing this bitmap
   1.773 +         * indicating that it should attempt to use mipmaps when this bitmap
   1.774 +         * is drawn scaled down.
   1.775 +         */
   1.776 +        kHasHardwareMipMap_Flag = 0x08,
   1.777 +#endif
   1.778 +    };
   1.779 +
   1.780 +    SkImageInfo fInfo;
   1.781 +
   1.782 +    uint32_t    fRowBytes;
   1.783 +
   1.784 +    uint8_t     fFlags;
   1.785 +
   1.786 +    void internalErase(const SkIRect&, U8CPU a, U8CPU r, U8CPU g, U8CPU b)const;
   1.787 +
   1.788 +    /* Internal computations for safe size.
   1.789 +    */
   1.790 +    static int64_t ComputeSafeSize64(Config   config,
   1.791 +                                     uint32_t width,
   1.792 +                                     uint32_t height,
   1.793 +                                     size_t   rowBytes);
   1.794 +    static size_t ComputeSafeSize(Config   config,
   1.795 +                                  uint32_t width,
   1.796 +                                  uint32_t height,
   1.797 +                                  size_t   rowBytes);
   1.798 +
   1.799 +    /*  Unreference any pixelrefs or colortables
   1.800 +    */
   1.801 +    void freePixels();
   1.802 +    void updatePixelsFromRef() const;
   1.803 +
   1.804 +    static SkFixed ComputeMipLevel(SkFixed sx, SkFixed dy);
   1.805 +
   1.806 +    /** Given scale factors sx, sy, determine the miplevel available in the
   1.807 +     bitmap, and return it (this is the amount to shift matrix iterators
   1.808 +     by). If dst is not null, it is set to the correct level.
   1.809 +     */
   1.810 +    int extractMipLevel(SkBitmap* dst, SkFixed sx, SkFixed sy);
   1.811 +    bool hasMipMap() const;
   1.812 +    void freeMipMap();
   1.813 +
   1.814 +    friend struct SkBitmapProcState;
   1.815 +};
   1.816 +
   1.817 +class SkAutoLockPixels : public SkNoncopyable {
   1.818 +public:
   1.819 +    SkAutoLockPixels(const SkBitmap& bm, bool doLock = true) : fBitmap(bm) {
   1.820 +        fDidLock = doLock;
   1.821 +        if (doLock) {
   1.822 +            bm.lockPixels();
   1.823 +        }
   1.824 +    }
   1.825 +    ~SkAutoLockPixels() {
   1.826 +        if (fDidLock) {
   1.827 +            fBitmap.unlockPixels();
   1.828 +        }
   1.829 +    }
   1.830 +
   1.831 +private:
   1.832 +    const SkBitmap& fBitmap;
   1.833 +    bool            fDidLock;
   1.834 +};
   1.835 +//TODO(mtklein): uncomment when 71713004 lands and Chromium's fixed.
   1.836 +//#define SkAutoLockPixels(...) SK_REQUIRE_LOCAL_VAR(SkAutoLockPixels)
   1.837 +
   1.838 +/** Helper class that performs the lock/unlockColors calls on a colortable.
   1.839 +    The destructor will call unlockColors(false) if it has a bitmap's colortable
   1.840 +*/
   1.841 +class SkAutoLockColors : public SkNoncopyable {
   1.842 +public:
   1.843 +    /** Initialize with no bitmap. Call lockColors(bitmap) to lock bitmap's
   1.844 +        colortable
   1.845 +     */
   1.846 +    SkAutoLockColors() : fCTable(NULL), fColors(NULL) {}
   1.847 +    /** Initialize with bitmap, locking its colortable if present
   1.848 +     */
   1.849 +    explicit SkAutoLockColors(const SkBitmap& bm) {
   1.850 +        fCTable = bm.getColorTable();
   1.851 +        fColors = fCTable ? fCTable->lockColors() : NULL;
   1.852 +    }
   1.853 +    /** Initialize with a colortable (may be null)
   1.854 +     */
   1.855 +    explicit SkAutoLockColors(SkColorTable* ctable) {
   1.856 +        fCTable = ctable;
   1.857 +        fColors = ctable ? ctable->lockColors() : NULL;
   1.858 +    }
   1.859 +    ~SkAutoLockColors() {
   1.860 +        if (fCTable) {
   1.861 +            fCTable->unlockColors();
   1.862 +        }
   1.863 +    }
   1.864 +
   1.865 +    /** Return the currently locked colors, or NULL if no bitmap's colortable
   1.866 +        is currently locked.
   1.867 +    */
   1.868 +    const SkPMColor* colors() const { return fColors; }
   1.869 +
   1.870 +    /** Locks the table and returns is colors (assuming ctable is not null) and
   1.871 +        unlocks the previous table if one was present
   1.872 +     */
   1.873 +    const SkPMColor* lockColors(SkColorTable* ctable) {
   1.874 +        if (fCTable) {
   1.875 +            fCTable->unlockColors();
   1.876 +        }
   1.877 +        fCTable = ctable;
   1.878 +        fColors = ctable ? ctable->lockColors() : NULL;
   1.879 +        return fColors;
   1.880 +    }
   1.881 +
   1.882 +    const SkPMColor* lockColors(const SkBitmap& bm) {
   1.883 +        return this->lockColors(bm.getColorTable());
   1.884 +    }
   1.885 +
   1.886 +private:
   1.887 +    SkColorTable*    fCTable;
   1.888 +    const SkPMColor* fColors;
   1.889 +};
   1.890 +#define SkAutoLockColors(...) SK_REQUIRE_LOCAL_VAR(SkAutoLockColors)
   1.891 +
   1.892 +///////////////////////////////////////////////////////////////////////////////
   1.893 +
   1.894 +inline uint32_t* SkBitmap::getAddr32(int x, int y) const {
   1.895 +    SkASSERT(fPixels);
   1.896 +    SkASSERT(4 == this->bytesPerPixel());
   1.897 +    SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height());
   1.898 +    return (uint32_t*)((char*)fPixels + y * fRowBytes + (x << 2));
   1.899 +}
   1.900 +
   1.901 +inline uint16_t* SkBitmap::getAddr16(int x, int y) const {
   1.902 +    SkASSERT(fPixels);
   1.903 +    SkASSERT(2 == this->bytesPerPixel());
   1.904 +    SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height());
   1.905 +    return (uint16_t*)((char*)fPixels + y * fRowBytes + (x << 1));
   1.906 +}
   1.907 +
   1.908 +inline uint8_t* SkBitmap::getAddr8(int x, int y) const {
   1.909 +    SkASSERT(fPixels);
   1.910 +    SkASSERT(1 == this->bytesPerPixel());
   1.911 +    SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height());
   1.912 +    return (uint8_t*)fPixels + y * fRowBytes + x;
   1.913 +}
   1.914 +
   1.915 +inline SkPMColor SkBitmap::getIndex8Color(int x, int y) const {
   1.916 +    SkASSERT(fPixels);
   1.917 +    SkASSERT(kIndex_8_SkColorType == this->colorType());
   1.918 +    SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height());
   1.919 +    SkASSERT(fColorTable);
   1.920 +    return (*fColorTable)[*((const uint8_t*)fPixels + y * fRowBytes + x)];
   1.921 +}
   1.922 +
   1.923 +///////////////////////////////////////////////////////////////////////////////
   1.924 +//
   1.925 +// Helpers until we can fully deprecate SkBitmap::Config
   1.926 +//
   1.927 +extern SkBitmap::Config SkColorTypeToBitmapConfig(SkColorType);
   1.928 +extern SkColorType SkBitmapConfigToColorType(SkBitmap::Config);
   1.929 +
   1.930 +#endif

mercurial