18a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
38a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
68a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkBitmap_DEFINED
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkBitmap_DEFINED
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColor.h"
1264a0ec36555352ec31aa7c5a7630a5d042b010badjsollen@google.com#include "SkColorTable.h"
133443fd88862e25199973c2e98075d2e12ef21be0reed@google.com#include "SkImageInfo.h"
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPoint.h"
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRefCnt.h"
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17dac522589e9395b4654a1a708f1bd971f37f95a5commit-bot@chromium.orgstruct SkMask;
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstruct SkIRect;
19fb2d3c69816253ab7d2df576718c252ae1158c65reed@google.comstruct SkRect;
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkPaint;
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkPixelRef;
229ebcac54635cde63110d73ad7c43d70772e7872freed@google.comclass SkPixelRefFactory;
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkRegion;
2476f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.comclass SkString;
25b8d00db075b5ea09e353508a26ef5ced50722a6ccommit-bot@chromium.orgclass GrTexture;
26ce4e53a2347040a3c145768c193a7a7958c0d4ecreed@android.com
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \class SkBitmap
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    The SkBitmap class specifies a raster bitmap. A bitmap has an integer width
30ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    and height, and a format (colortype), and a pointer to the actual pixels.
314b33d28eb369622964d22d5cc4a6e3f0eb11a14ctomhudson@google.com    Bitmaps can be drawn into a SkCanvas, but they are also used to specify the
324b33d28eb369622964d22d5cc4a6e3f0eb11a14ctomhudson@google.com    target of a SkCanvas' drawing operations.
334b33d28eb369622964d22d5cc4a6e3f0eb11a14ctomhudson@google.com    A const SkBitmap exposes getAddr(), which lets a caller write its pixels;
344b33d28eb369622964d22d5cc4a6e3f0eb11a14ctomhudson@google.com    the constness is considered to apply to the bitmap's configuration, not
354b33d28eb369622964d22d5cc4a6e3f0eb11a14ctomhudson@google.com    its contents.
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
377ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgclass SK_API SkBitmap {
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
39eddb02c82d6e35a05f84798b23aac48a446c1aa6commit-bot@chromium.org    class SK_API Allocator;
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
41c3b3266b7db2f1a41d41ecac010c766b7ad8eebcreed#ifdef SK_SUPPORT_LEGACY_BITMAP_CONFIG
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    enum Config {
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kNo_Config,         //!< bitmap has not been configured
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kA8_Config,         //!< 8-bits per pixel, with only alpha specified (0 is transparent, 0xFF is opaque)
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kIndex8_Config,     //!< 8-bits per pixel, using SkColorTable to specify the colors
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kRGB_565_Config,    //!< 16-bits per pixel, (see SkColorPriv.h for packing)
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kARGB_4444_Config,  //!< 16-bits per pixel, (see SkColorPriv.h for packing)
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kARGB_8888_Config,  //!< 32-bits per pixel, (see SkColorPriv.h for packing)
4962b260557d0e9aa9c8a2c08968eddb63a989f3fcreed@google.com    };
5073a4b4f91fac1783b589eead5e82b0c0771644deskia.committer@gmail.com
5162b260557d0e9aa9c8a2c08968eddb63a989f3fcreed@google.com    // do not add this to the Config enum, otherwise the compiler will let us
5262b260557d0e9aa9c8a2c08968eddb63a989f3fcreed@google.com    // pass this as a valid parameter for Config.
5362b260557d0e9aa9c8a2c08968eddb63a989f3fcreed@google.com    enum {
542cb1480ff8ae030946cb5f218f9c5cbc1e54c7a9reed@google.com        kConfigCount = kARGB_8888_Config + 1
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
57ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    /** Return the config for the bitmap. */
58ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    Config  config() const;
59ddd014e65264e3f18b2d4305dd2fbc81549f5180reed
60ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    SK_ATTR_DEPRECATED("use config()")
61ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    Config  getConfig() const { return this->config(); }
62c3b3266b7db2f1a41d41ecac010c766b7ad8eebcreed#endif
63ddd014e65264e3f18b2d4305dd2fbc81549f5180reed
645dd510f1d277a31752a4c6046c4e42385605a393reed@google.com    /**
655dd510f1d277a31752a4c6046c4e42385605a393reed@google.com     *  Default construct creates a bitmap with zero width and height, and no pixels.
66ddd014e65264e3f18b2d4305dd2fbc81549f5180reed     *  Its colortype is set to kUnknown_SkColorType.
675dd510f1d277a31752a4c6046c4e42385605a393reed@google.com     */
688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBitmap();
695dd510f1d277a31752a4c6046c4e42385605a393reed@google.com
705dd510f1d277a31752a4c6046c4e42385605a393reed@google.com    /**
715dd510f1d277a31752a4c6046c4e42385605a393reed@google.com     *  Copy the settings from the src into this bitmap. If the src has pixels
725dd510f1d277a31752a4c6046c4e42385605a393reed@google.com     *  allocated, they will be shared, not copied, so that the two bitmaps will
735dd510f1d277a31752a4c6046c4e42385605a393reed@google.com     *  reference the same memory for the pixels. If a deep copy is needed,
745dd510f1d277a31752a4c6046c4e42385605a393reed@google.com     *  where the new bitmap has its own separate copy of the pixels, use
755dd510f1d277a31752a4c6046c4e42385605a393reed@google.com     *  deepCopyTo().
765dd510f1d277a31752a4c6046c4e42385605a393reed@google.com     */
778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBitmap(const SkBitmap& src);
785dd510f1d277a31752a4c6046c4e42385605a393reed@google.com
798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ~SkBitmap();
808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Copies the src bitmap into this bitmap. Ownership of the src bitmap's pixels remains
828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        with the src bitmap.
838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBitmap& operator=(const SkBitmap& src);
858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Swap the fields of the two bitmaps. This routine is guaranteed to never fail or throw.
868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    //  This method is not exported to java.
888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void swap(SkBitmap& other);
89f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
9061e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org    ///////////////////////////////////////////////////////////////////////////
9161e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org
9261e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org    const SkImageInfo& info() const { return fInfo; }
9361e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org
9461e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org    int width() const { return fInfo.fWidth; }
9561e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org    int height() const { return fInfo.fHeight; }
9661e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org    SkColorType colorType() const { return fInfo.fColorType; }
9761e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org    SkAlphaType alphaType() const { return fInfo.fAlphaType; }
9861e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org
99466f5f3e44e703ca58b43ac1c4ac3bfa0e1ff024commit-bot@chromium.org#ifdef SK_SUPPORT_LEGACY_ASIMAGEINFO
100466f5f3e44e703ca58b43ac1c4ac3bfa0e1ff024commit-bot@chromium.org    bool asImageInfo(SkImageInfo* info) const {
101466f5f3e44e703ca58b43ac1c4ac3bfa0e1ff024commit-bot@chromium.org        // compatibility: return false for kUnknown
102466f5f3e44e703ca58b43ac1c4ac3bfa0e1ff024commit-bot@chromium.org        if (kUnknown_SkColorType == this->colorType()) {
103466f5f3e44e703ca58b43ac1c4ac3bfa0e1ff024commit-bot@chromium.org            return false;
104466f5f3e44e703ca58b43ac1c4ac3bfa0e1ff024commit-bot@chromium.org        }
105466f5f3e44e703ca58b43ac1c4ac3bfa0e1ff024commit-bot@chromium.org        if (info) {
106466f5f3e44e703ca58b43ac1c4ac3bfa0e1ff024commit-bot@chromium.org            *info = this->info();
107466f5f3e44e703ca58b43ac1c4ac3bfa0e1ff024commit-bot@chromium.org        }
108466f5f3e44e703ca58b43ac1c4ac3bfa0e1ff024commit-bot@chromium.org        return true;
109466f5f3e44e703ca58b43ac1c4ac3bfa0e1ff024commit-bot@chromium.org    }
110466f5f3e44e703ca58b43ac1c4ac3bfa0e1ff024commit-bot@chromium.org#endif
111466f5f3e44e703ca58b43ac1c4ac3bfa0e1ff024commit-bot@chromium.org
112ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    /**
113ddd014e65264e3f18b2d4305dd2fbc81549f5180reed     *  Return the number of bytes per pixel based on the colortype. If the colortype is
114ddd014e65264e3f18b2d4305dd2fbc81549f5180reed     *  kUnknown_SkColorType, then 0 is returned.
11561e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org     */
11661e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org    int bytesPerPixel() const { return fInfo.bytesPerPixel(); }
11761e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org
118ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    /**
119ddd014e65264e3f18b2d4305dd2fbc81549f5180reed     *  Return the rowbytes expressed as a number of pixels (like width and height).
120ddd014e65264e3f18b2d4305dd2fbc81549f5180reed     *  If the colortype is kUnknown_SkColorType, then 0 is returned.
12161e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org     */
12261e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org    int rowBytesAsPixels() const {
12361e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org        return fRowBytes >> this->shiftPerPixel();
12461e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org    }
12561e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org
126ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    /**
127ddd014e65264e3f18b2d4305dd2fbc81549f5180reed     *  Return the shift amount per pixel (i.e. 0 for 1-byte per pixel, 1 for 2-bytes per pixel
128ddd014e65264e3f18b2d4305dd2fbc81549f5180reed     *  colortypes, 2 for 4-bytes per pixel colortypes). Return 0 for kUnknown_SkColorType.
12961e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org     */
13061e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org    int shiftPerPixel() const { return this->bytesPerPixel() >> 1; }
13161e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org
13261e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org    ///////////////////////////////////////////////////////////////////////////
13361e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org
1348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return true iff the bitmap has empty dimensions.
13550b393a768c0311b3210f723325fd27bf161136bcommit-bot@chromium.org     *  Hey!  Before you use this, see if you really want to know drawsNothing() instead.
13650b393a768c0311b3210f723325fd27bf161136bcommit-bot@chromium.org     */
13761e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org    bool empty() const { return fInfo.isEmpty(); }
138f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
139c84b8335ee4fd864c30a4703afc643cf4b5010d9djsollen@google.com    /** Return true iff the bitmap has no pixelref. Note: this can return true even if the
14050b393a768c0311b3210f723325fd27bf161136bcommit-bot@chromium.org     *  dimensions of the bitmap are > 0 (see empty()).
14150b393a768c0311b3210f723325fd27bf161136bcommit-bot@chromium.org     *  Hey!  Before you use this, see if you really want to know drawsNothing() instead.
14250b393a768c0311b3210f723325fd27bf161136bcommit-bot@chromium.org     */
143c84b8335ee4fd864c30a4703afc643cf4b5010d9djsollen@google.com    bool isNull() const { return NULL == fPixelRef; }
1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14550b393a768c0311b3210f723325fd27bf161136bcommit-bot@chromium.org    /** Return true iff drawing this bitmap has no effect.
14650b393a768c0311b3210f723325fd27bf161136bcommit-bot@chromium.org     */
14750b393a768c0311b3210f723325fd27bf161136bcommit-bot@chromium.org    bool drawsNothing() const { return this->empty() || this->isNull(); }
14850b393a768c0311b3210f723325fd27bf161136bcommit-bot@chromium.org
1494469938e92d779dff05e745559e67907bbf21e78reed@google.com    /** Return the number of bytes between subsequent rows of the bitmap. */
1500ba4bf427acbd7707d04192a98c77ef194a0f25ascroggo@google.com    size_t rowBytes() const { return fRowBytes; }
151f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
152383a697692cf46951fd451f6f4c3d03634a6a1cbreed@google.com    /**
153383a697692cf46951fd451f6f4c3d03634a6a1cbreed@google.com     *  Set the bitmap's alphaType, returning true on success. If false is
154383a697692cf46951fd451f6f4c3d03634a6a1cbreed@google.com     *  returned, then the specified new alphaType is incompatible with the
155ddd014e65264e3f18b2d4305dd2fbc81549f5180reed     *  colortype, and the current alphaType is unchanged.
1560e8d0d6cdc2e02946b099006d0a47e60764905e5commit-bot@chromium.org     *
1570e8d0d6cdc2e02946b099006d0a47e60764905e5commit-bot@chromium.org     *  Note: this changes the alphatype for the underlying pixels, which means
1580e8d0d6cdc2e02946b099006d0a47e60764905e5commit-bot@chromium.org     *  that all bitmaps that might be sharing (subsets of) the pixels will
159c1587f94a77eaafe257a6ecf504364d40362c66areed@google.com     *  be affected.
160383a697692cf46951fd451f6f4c3d03634a6a1cbreed@google.com     */
161383a697692cf46951fd451f6f4c3d03634a6a1cbreed@google.com    bool setAlphaType(SkAlphaType);
162383a697692cf46951fd451f6f4c3d03634a6a1cbreed@google.com
1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the address of the pixels for this SkBitmap.
1648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* getPixels() const { return fPixels; }
1668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the byte size of the pixels, based on the height and rowBytes.
1688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        Note this truncates the result to 32bits. Call getSize64() to detect
1698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if the real size exceeds 32bits.
1708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
17161e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org    size_t getSize() const { return fInfo.fHeight * fRowBytes; }
172f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
17386bff1f008308267c4ff75456b202a9cf60c6193wjmaclean@chromium.org    /** Return the number of bytes from the pointer returned by getPixels()
17486bff1f008308267c4ff75456b202a9cf60c6193wjmaclean@chromium.org        to the end of the allocated space in the buffer. Required in
175d27fe34d1eb7640bb17062aa8529283e297de18eepoger@google.com        cases where extractSubset has been called.
17686bff1f008308267c4ff75456b202a9cf60c6193wjmaclean@chromium.org    */
17761e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org    size_t getSafeSize() const { return fInfo.getSafeSize(fRowBytes); }
17886bff1f008308267c4ff75456b202a9cf60c6193wjmaclean@chromium.org
17957212f9469c8056bab3c85243dbb904e386eab95reed@google.com    /**
18057212f9469c8056bab3c85243dbb904e386eab95reed@google.com     *  Return the full size of the bitmap, in bytes.
18157212f9469c8056bab3c85243dbb904e386eab95reed@google.com     */
18257212f9469c8056bab3c85243dbb904e386eab95reed@google.com    int64_t computeSize64() const {
18361e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org        return sk_64_mul(fInfo.fHeight, fRowBytes);
18457212f9469c8056bab3c85243dbb904e386eab95reed@google.com    }
18557212f9469c8056bab3c85243dbb904e386eab95reed@google.com
18657212f9469c8056bab3c85243dbb904e386eab95reed@google.com    /**
18757212f9469c8056bab3c85243dbb904e386eab95reed@google.com     *  Return the number of bytes from the pointer returned by getPixels()
18857212f9469c8056bab3c85243dbb904e386eab95reed@google.com     *  to the end of the allocated space in the buffer. This may be smaller
18957212f9469c8056bab3c85243dbb904e386eab95reed@google.com     *  than computeSize64() if there is any rowbytes padding beyond the width.
19057212f9469c8056bab3c85243dbb904e386eab95reed@google.com     */
19157212f9469c8056bab3c85243dbb904e386eab95reed@google.com    int64_t computeSafeSize64() const {
19261e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org        return fInfo.getSafeSize64(fRowBytes);
19357212f9469c8056bab3c85243dbb904e386eab95reed@google.com    }
19457212f9469c8056bab3c85243dbb904e386eab95reed@google.com
195b052129a4121c1e3f051e374541a93d3d7a98064junov@chromium.org    /** Returns true if this bitmap is marked as immutable, meaning that the
196b052129a4121c1e3f051e374541a93d3d7a98064junov@chromium.org        contents of its pixels will not change for the lifetime of the bitmap.
197b052129a4121c1e3f051e374541a93d3d7a98064junov@chromium.org    */
198b052129a4121c1e3f051e374541a93d3d7a98064junov@chromium.org    bool isImmutable() const;
199b052129a4121c1e3f051e374541a93d3d7a98064junov@chromium.org
200b052129a4121c1e3f051e374541a93d3d7a98064junov@chromium.org    /** Marks this bitmap as immutable, meaning that the contents of its
201b052129a4121c1e3f051e374541a93d3d7a98064junov@chromium.org        pixels will not change for the lifetime of the bitmap and of the
202fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        underlying pixelref. This state can be set, but it cannot be
203b052129a4121c1e3f051e374541a93d3d7a98064junov@chromium.org        cleared once it is set. This state propagates to all other bitmaps
204b052129a4121c1e3f051e374541a93d3d7a98064junov@chromium.org        that share the same pixelref.
205b052129a4121c1e3f051e374541a93d3d7a98064junov@chromium.org    */
206b052129a4121c1e3f051e374541a93d3d7a98064junov@chromium.org    void setImmutable();
207b052129a4121c1e3f051e374541a93d3d7a98064junov@chromium.org
2088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns true if the bitmap is opaque (has no translucent/transparent pixels).
2098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
210383a697692cf46951fd451f6f4c3d03634a6a1cbreed@google.com    bool isOpaque() const {
211383a697692cf46951fd451f6f4c3d03634a6a1cbreed@google.com        return SkAlphaTypeIsOpaque(this->alphaType());
212383a697692cf46951fd451f6f4c3d03634a6a1cbreed@google.com    }
2134ee7ae5dcfe2055cfcfc21bf2cec1d790330eb4ajunov@google.com
2144ee7ae5dcfe2055cfcfc21bf2cec1d790330eb4ajunov@google.com    /** Returns true if the bitmap is volatile (i.e. should not be cached by devices.)
2154ee7ae5dcfe2055cfcfc21bf2cec1d790330eb4ajunov@google.com    */
2164ee7ae5dcfe2055cfcfc21bf2cec1d790330eb4ajunov@google.com    bool isVolatile() const;
2174ee7ae5dcfe2055cfcfc21bf2cec1d790330eb4ajunov@google.com
218fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    /** Specify whether this bitmap is volatile. Bitmaps are not volatile by
2194ee7ae5dcfe2055cfcfc21bf2cec1d790330eb4ajunov@google.com        default. Temporary bitmaps that are discarded after use should be
2204ee7ae5dcfe2055cfcfc21bf2cec1d790330eb4ajunov@google.com        marked as volatile. This provides a hint to the device that the bitmap
221fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        should not be cached. Providing this hint when appropriate can
222fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        improve performance by avoiding unnecessary overhead and resource
2234ee7ae5dcfe2055cfcfc21bf2cec1d790330eb4ajunov@google.com        consumption on the device.
2244ee7ae5dcfe2055cfcfc21bf2cec1d790330eb4ajunov@google.com    */
2254ee7ae5dcfe2055cfcfc21bf2cec1d790330eb4ajunov@google.com    void setIsVolatile(bool);
2264ee7ae5dcfe2055cfcfc21bf2cec1d790330eb4ajunov@google.com
2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Reset the bitmap to its initial state (see default constructor). If we are a (shared)
2288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        owner of the pixels, that ownership is decremented.
2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
2308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void reset();
2318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
232ddd014e65264e3f18b2d4305dd2fbc81549f5180reed#ifdef SK_SUPPORT_LEGACY_COMPUTE_CONFIG_SIZE
2338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Given a config and a width, this computes the optimal rowBytes value. This is called automatically
2348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if you pass 0 for rowBytes to setConfig().
2358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
2360ba4bf427acbd7707d04192a98c77ef194a0f25ascroggo@google.com    static size_t ComputeRowBytes(Config c, int width);
2378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the bytes-per-pixel for the specified config. If the config is
2398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        not at least 1-byte per pixel, return 0, including for kNo_Config.
2408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
2418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static int ComputeBytesPerPixel(Config c);
2428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the shift-per-pixel for the specified config. If the config is
2448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     not at least 1-byte per pixel, return 0, including for kNo_Config.
2458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
2468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static int ComputeShiftPerPixel(Config c) {
2478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return ComputeBytesPerPixel(c) >> 1;
2488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
249f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
25057212f9469c8056bab3c85243dbb904e386eab95reed@google.com    static int64_t ComputeSize64(Config, int width, int height);
2518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static size_t ComputeSize(Config, int width, int height);
252ddd014e65264e3f18b2d4305dd2fbc81549f5180reed#endif
2538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25486b2e43a33c486a0b150aa14fadf7af8323a11f5reed@google.com    /**
2552a7579d11796866b0026755ab52597d09f4509ccreed@google.com     *  This will brute-force return true if all of the pixels in the bitmap
2562a7579d11796866b0026755ab52597d09f4509ccreed@google.com     *  are opaque. If it fails to read the pixels, or encounters an error,
2572a7579d11796866b0026755ab52597d09f4509ccreed@google.com     *  it will return false.
2582a7579d11796866b0026755ab52597d09f4509ccreed@google.com     *
2592a7579d11796866b0026755ab52597d09f4509ccreed@google.com     *  Since this can be an expensive operation, the bitmap stores a flag for
26021a0b10d7c28e2ca123ddbe32fd8388687b3ef2ccommit-bot@chromium.org     *  this (isOpaque). Only call this if you need to compute this value from
26121a0b10d7c28e2ca123ddbe32fd8388687b3ef2ccommit-bot@chromium.org     *  "unknown" pixels.
2622a7579d11796866b0026755ab52597d09f4509ccreed@google.com     */
2632a7579d11796866b0026755ab52597d09f4509ccreed@google.com    static bool ComputeIsOpaque(const SkBitmap&);
2642a7579d11796866b0026755ab52597d09f4509ccreed@google.com
2652a7579d11796866b0026755ab52597d09f4509ccreed@google.com    /**
26686b2e43a33c486a0b150aa14fadf7af8323a11f5reed@google.com     *  Return the bitmap's bounds [0, 0, width, height] as an SkRect
26786b2e43a33c486a0b150aa14fadf7af8323a11f5reed@google.com     */
26886b2e43a33c486a0b150aa14fadf7af8323a11f5reed@google.com    void getBounds(SkRect* bounds) const;
26980e1459c32f5f3acbc688ae4f0e046aa283e8c3areed@google.com    void getBounds(SkIRect* bounds) const;
27086b2e43a33c486a0b150aa14fadf7af8323a11f5reed@google.com
2716c22573edb234ad14df947278cfed010669a39a7reed#ifdef SK_SUPPORT_LEGACY_SETCONFIG
2728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Set the bitmap's config and dimensions. If rowBytes is 0, then
2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        ComputeRowBytes() is called to compute the optimal value. This resets
2748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        any pixel/colortable ownership, just like reset().
2758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
276383a697692cf46951fd451f6f4c3d03634a6a1cbreed@google.com    bool setConfig(Config, int width, int height, size_t rowBytes, SkAlphaType);
277383a697692cf46951fd451f6f4c3d03634a6a1cbreed@google.com
278383a697692cf46951fd451f6f4c3d03634a6a1cbreed@google.com    bool setConfig(Config config, int width, int height, size_t rowBytes = 0) {
279383a697692cf46951fd451f6f4c3d03634a6a1cbreed@google.com        return this->setConfig(config, width, height, rowBytes,
280383a697692cf46951fd451f6f4c3d03634a6a1cbreed@google.com                               kPremul_SkAlphaType);
281383a697692cf46951fd451f6f4c3d03634a6a1cbreed@google.com    }
2826c22573edb234ad14df947278cfed010669a39a7reed#endif
283383a697692cf46951fd451f6f4c3d03634a6a1cbreed@google.com
284a3264e53ee3f3c5d6a2c813df7e44b5b96d207f2commit-bot@chromium.org    bool setInfo(const SkImageInfo&, size_t rowBytes = 0);
285a3264e53ee3f3c5d6a2c813df7e44b5b96d207f2commit-bot@chromium.org
286a3264e53ee3f3c5d6a2c813df7e44b5b96d207f2commit-bot@chromium.org#ifdef SK_SUPPORT_LEGACY_SETCONFIG_INFO
287a3264e53ee3f3c5d6a2c813df7e44b5b96d207f2commit-bot@chromium.org    bool setConfig(const SkImageInfo& info, size_t rowBytes = 0) {
288a3264e53ee3f3c5d6a2c813df7e44b5b96d207f2commit-bot@chromium.org        return this->setInfo(info, rowBytes);
289a3264e53ee3f3c5d6a2c813df7e44b5b96d207f2commit-bot@chromium.org    }
290a3264e53ee3f3c5d6a2c813df7e44b5b96d207f2commit-bot@chromium.org#endif
291383a697692cf46951fd451f6f4c3d03634a6a1cbreed@google.com
2929230ea29718bcf1a92a89a1a518fb896bbbe00cfreed@google.com    /**
2939ebcac54635cde63110d73ad7c43d70772e7872freed@google.com     *  Allocate a pixelref to match the specified image info. If the Factory
2949ebcac54635cde63110d73ad7c43d70772e7872freed@google.com     *  is non-null, call it to allcoate the pixelref. If the ImageInfo requires
2959ebcac54635cde63110d73ad7c43d70772e7872freed@google.com     *  a colortable, then ColorTable must be non-null, and will be ref'd.
2969ebcac54635cde63110d73ad7c43d70772e7872freed@google.com     *  On failure, the bitmap will be set to empty and return false.
2979ebcac54635cde63110d73ad7c43d70772e7872freed@google.com     */
2989ebcac54635cde63110d73ad7c43d70772e7872freed@google.com    bool allocPixels(const SkImageInfo&, SkPixelRefFactory*, SkColorTable*);
299d2ac07b64b7d46e81d00d389781f9f83810f0095skia.committer@gmail.com
3009ebcac54635cde63110d73ad7c43d70772e7872freed@google.com    /**
3019ebcac54635cde63110d73ad7c43d70772e7872freed@google.com     *  Allocate a pixelref to match the specified image info, using the default
3029ebcac54635cde63110d73ad7c43d70772e7872freed@google.com     *  allocator.
3039ebcac54635cde63110d73ad7c43d70772e7872freed@google.com     *  On success, the bitmap's pixels will be "locked", and return true.
3049ebcac54635cde63110d73ad7c43d70772e7872freed@google.com     *  On failure, the bitmap will be set to empty and return false.
3059ebcac54635cde63110d73ad7c43d70772e7872freed@google.com     */
3069ebcac54635cde63110d73ad7c43d70772e7872freed@google.com    bool allocPixels(const SkImageInfo& info) {
3079ebcac54635cde63110d73ad7c43d70772e7872freed@google.com        return this->allocPixels(info, NULL, NULL);
3089ebcac54635cde63110d73ad7c43d70772e7872freed@google.com    }
309d2ac07b64b7d46e81d00d389781f9f83810f0095skia.committer@gmail.com
310eb9a46cbbb475e862a084aa2224ec18d4ac5e95breed@google.com    bool allocN32Pixels(int width, int height, bool isOpaque = false) {
311eb9a46cbbb475e862a084aa2224ec18d4ac5e95breed@google.com        SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
312eb9a46cbbb475e862a084aa2224ec18d4ac5e95breed@google.com        if (isOpaque) {
313eb9a46cbbb475e862a084aa2224ec18d4ac5e95breed@google.com            info.fAlphaType = kOpaque_SkAlphaType;
314eb9a46cbbb475e862a084aa2224ec18d4ac5e95breed@google.com        }
315eb9a46cbbb475e862a084aa2224ec18d4ac5e95breed@google.com        return this->allocPixels(info);
316eb9a46cbbb475e862a084aa2224ec18d4ac5e95breed@google.com    }
317eb9a46cbbb475e862a084aa2224ec18d4ac5e95breed@google.com
318eb9a46cbbb475e862a084aa2224ec18d4ac5e95breed@google.com    /**
3199ebcac54635cde63110d73ad7c43d70772e7872freed@google.com     *  Install a pixelref that wraps the specified pixels and rowBytes, and
3209ebcac54635cde63110d73ad7c43d70772e7872freed@google.com     *  optional ReleaseProc and context. When the pixels are no longer
32100f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org     *  referenced, if releaseProc is not null, it will be called with the
3229ebcac54635cde63110d73ad7c43d70772e7872freed@google.com     *  pixels and context as parameters.
3239ebcac54635cde63110d73ad7c43d70772e7872freed@google.com     *  On failure, the bitmap will be set to empty and return false.
3249ebcac54635cde63110d73ad7c43d70772e7872freed@google.com     */
32500f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org    bool installPixels(const SkImageInfo&, void* pixels, size_t rowBytes, SkColorTable*,
32600f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org                       void (*releaseProc)(void* addr, void* context), void* context);
32700f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org
32800f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org#ifdef SK_SUPPORT_LEGACY_INSTALLPIXELSPARAMS
32900f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org    bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
33000f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org                       void (*releaseProc)(void* addr, void* context),
33100f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org                       void* context) {
33200f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org        return this->installPixels(info, pixels, rowBytes, NULL, releaseProc, context);
33300f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org    }
33400f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org#endif
33531acdeabce094e9d42f0d6db8eebcf788aaba11askia.committer@gmail.com
336dac522589e9395b4654a1a708f1bd971f37f95a5commit-bot@chromium.org    /**
337dac522589e9395b4654a1a708f1bd971f37f95a5commit-bot@chromium.org     *  Call installPixels with no ReleaseProc specified. This means that the
338dac522589e9395b4654a1a708f1bd971f37f95a5commit-bot@chromium.org     *  caller must ensure that the specified pixels are valid for the lifetime
339dac522589e9395b4654a1a708f1bd971f37f95a5commit-bot@chromium.org     *  of the created bitmap (and its pixelRef).
340dac522589e9395b4654a1a708f1bd971f37f95a5commit-bot@chromium.org     */
341dac522589e9395b4654a1a708f1bd971f37f95a5commit-bot@chromium.org    bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
34200f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org        return this->installPixels(info, pixels, rowBytes, NULL, NULL, NULL);
343dac522589e9395b4654a1a708f1bd971f37f95a5commit-bot@chromium.org    }
344dac522589e9395b4654a1a708f1bd971f37f95a5commit-bot@chromium.org
345dac522589e9395b4654a1a708f1bd971f37f95a5commit-bot@chromium.org    /**
346dac522589e9395b4654a1a708f1bd971f37f95a5commit-bot@chromium.org     *  Calls installPixels() with the value in the SkMask. The caller must
347dac522589e9395b4654a1a708f1bd971f37f95a5commit-bot@chromium.org     *  ensure that the specified mask pixels are valid for the lifetime
348dac522589e9395b4654a1a708f1bd971f37f95a5commit-bot@chromium.org     *  of the created bitmap (and its pixelRef).
349dac522589e9395b4654a1a708f1bd971f37f95a5commit-bot@chromium.org     */
350dac522589e9395b4654a1a708f1bd971f37f95a5commit-bot@chromium.org    bool installMaskPixels(const SkMask&);
351d2ac07b64b7d46e81d00d389781f9f83810f0095skia.committer@gmail.com
3528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Use this to assign a new pixel address for an existing bitmap. This
3538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        will automatically release any pixelref previously installed. Only call
3548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this if you are handling ownership/lifetime of the pixel memory.
355f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
3568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        If the bitmap retains a reference to the colortable (assuming it is
3578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        not null) it will take care of incrementing the reference count.
3588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param pixels   Address for the pixels, managed by the caller.
3608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param ctable   ColorTable (or null) that matches the specified pixels
3618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
3628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void setPixels(void* p, SkColorTable* ctable = NULL);
3638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
36486bff1f008308267c4ff75456b202a9cf60c6193wjmaclean@chromium.org    /** Copies the bitmap's pixels to the location pointed at by dst and returns
36586bff1f008308267c4ff75456b202a9cf60c6193wjmaclean@chromium.org        true if possible, returns false otherwise.
36686bff1f008308267c4ff75456b202a9cf60c6193wjmaclean@chromium.org
367c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com        In the case when the dstRowBytes matches the bitmap's rowBytes, the copy
368c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com        may be made faster by copying over the dst's per-row padding (for all
369c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com        rows but the last). By setting preserveDstPad to true the caller can
370c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com        disable this optimization and ensure that pixels in the padding are not
371c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com        overwritten.
37286bff1f008308267c4ff75456b202a9cf60c6193wjmaclean@chromium.org
37386bff1f008308267c4ff75456b202a9cf60c6193wjmaclean@chromium.org        Always returns false for RLE formats.
37486bff1f008308267c4ff75456b202a9cf60c6193wjmaclean@chromium.org
37586bff1f008308267c4ff75456b202a9cf60c6193wjmaclean@chromium.org        @param dst      Location of destination buffer.
37686bff1f008308267c4ff75456b202a9cf60c6193wjmaclean@chromium.org        @param dstSize  Size of destination buffer. Must be large enough to hold
37786bff1f008308267c4ff75456b202a9cf60c6193wjmaclean@chromium.org                        pixels using indicated stride.
3780ba4bf427acbd7707d04192a98c77ef194a0f25ascroggo@google.com        @param dstRowBytes  Width of each line in the buffer. If 0, uses
37986bff1f008308267c4ff75456b202a9cf60c6193wjmaclean@chromium.org                            bitmap's internal stride.
380c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com        @param preserveDstPad Must we preserve padding in the dst
38186bff1f008308267c4ff75456b202a9cf60c6193wjmaclean@chromium.org    */
3820ba4bf427acbd7707d04192a98c77ef194a0f25ascroggo@google.com    bool copyPixelsTo(void* const dst, size_t dstSize, size_t dstRowBytes = 0,
3830ba4bf427acbd7707d04192a98c77ef194a0f25ascroggo@google.com                      bool preserveDstPad = false) const;
38486bff1f008308267c4ff75456b202a9cf60c6193wjmaclean@chromium.org
3858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Use the standard HeapAllocator to create the pixelref that manages the
386ddd014e65264e3f18b2d4305dd2fbc81549f5180reed        pixel memory. It will be sized based on the current ImageInfo.
3878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        If this is called multiple times, a new pixelref object will be created
3888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        each time.
389f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
3908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        If the bitmap retains a reference to the colortable (assuming it is
3918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        not null) it will take care of incrementing the reference count.
3928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param ctable   ColorTable (or null) to use with the pixels that will
394ddd014e65264e3f18b2d4305dd2fbc81549f5180reed                        be allocated. Only used if colortype == kIndex_8_SkColorType
3958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @return true if the allocation succeeds. If not the pixelref field of
3968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                     the bitmap will be unchanged.
3978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
3988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool allocPixels(SkColorTable* ctable = NULL) {
3998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return this->allocPixels(NULL, ctable);
4008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
401f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
4028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Use the specified Allocator to create the pixelref that manages the
403ddd014e65264e3f18b2d4305dd2fbc81549f5180reed        pixel memory. It will be sized based on the current ImageInfo.
4048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        If this is called multiple times, a new pixelref object will be created
4058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        each time.
406f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
4078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        If the bitmap retains a reference to the colortable (assuming it is
4088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        not null) it will take care of incrementing the reference count.
409f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
4108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param allocator The Allocator to use to create a pixelref that can
411ddd014e65264e3f18b2d4305dd2fbc81549f5180reed                         manage the pixel memory for the current ImageInfo.
412ddd014e65264e3f18b2d4305dd2fbc81549f5180reed                         If allocator is NULL, the standard HeapAllocator will be used.
4138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param ctable   ColorTable (or null) to use with the pixels that will
414ddd014e65264e3f18b2d4305dd2fbc81549f5180reed                        be allocated. Only used if colortype == kIndex_8_SkColorType.
415ddd014e65264e3f18b2d4305dd2fbc81549f5180reed                        If it is non-null and the colortype is not indexed, it will
4168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        be ignored.
4178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @return true if the allocation succeeds. If not the pixelref field of
4188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                     the bitmap will be unchanged.
4198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
4208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool allocPixels(Allocator* allocator, SkColorTable* ctable);
421f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
422672588b684d484dce6ae251e9e163e4a46924322reed@google.com    /**
423672588b684d484dce6ae251e9e163e4a46924322reed@google.com     *  Return the current pixelref object or NULL if there is none. This does
424672588b684d484dce6ae251e9e163e4a46924322reed@google.com     *  not affect the refcount of the pixelref.
425672588b684d484dce6ae251e9e163e4a46924322reed@google.com     */
4268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPixelRef* pixelRef() const { return fPixelRef; }
427672588b684d484dce6ae251e9e163e4a46924322reed@google.com
428672588b684d484dce6ae251e9e163e4a46924322reed@google.com    /**
429672588b684d484dce6ae251e9e163e4a46924322reed@google.com     *  A bitmap can reference a subset of a pixelref's pixels. That means the
430672588b684d484dce6ae251e9e163e4a46924322reed@google.com     *  bitmap's width/height can be <= the dimensions of the pixelref. The
431672588b684d484dce6ae251e9e163e4a46924322reed@google.com     *  pixelref origin is the x,y location within the pixelref's pixels for
432672588b684d484dce6ae251e9e163e4a46924322reed@google.com     *  the bitmap's top/left corner. To be valid the following must be true:
433672588b684d484dce6ae251e9e163e4a46924322reed@google.com     *
434672588b684d484dce6ae251e9e163e4a46924322reed@google.com     *  origin_x + bitmap_width  <= pixelref_width
435672588b684d484dce6ae251e9e163e4a46924322reed@google.com     *  origin_y + bitmap_height <= pixelref_height
436672588b684d484dce6ae251e9e163e4a46924322reed@google.com     *
437672588b684d484dce6ae251e9e163e4a46924322reed@google.com     *  pixelRefOrigin() returns this origin, or (0,0) if there is no pixelRef.
438672588b684d484dce6ae251e9e163e4a46924322reed@google.com     */
439672588b684d484dce6ae251e9e163e4a46924322reed@google.com    SkIPoint pixelRefOrigin() const { return fPixelRefOrigin; }
440672588b684d484dce6ae251e9e163e4a46924322reed@google.com
441672588b684d484dce6ae251e9e163e4a46924322reed@google.com    /**
442672588b684d484dce6ae251e9e163e4a46924322reed@google.com     *  Assign a pixelref and origin to the bitmap. Pixelrefs are reference,
443672588b684d484dce6ae251e9e163e4a46924322reed@google.com     *  so the existing one (if any) will be unref'd and the new one will be
444672588b684d484dce6ae251e9e163e4a46924322reed@google.com     *  ref'd. (x,y) specify the offset within the pixelref's pixels for the
445672588b684d484dce6ae251e9e163e4a46924322reed@google.com     *  top/left corner of the bitmap. For a bitmap that encompases the entire
446672588b684d484dce6ae251e9e163e4a46924322reed@google.com     *  pixels of the pixelref, these will be (0,0).
447672588b684d484dce6ae251e9e163e4a46924322reed@google.com     */
448672588b684d484dce6ae251e9e163e4a46924322reed@google.com    SkPixelRef* setPixelRef(SkPixelRef* pr, int dx, int dy);
449672588b684d484dce6ae251e9e163e4a46924322reed@google.com
450672588b684d484dce6ae251e9e163e4a46924322reed@google.com    SkPixelRef* setPixelRef(SkPixelRef* pr, const SkIPoint& origin) {
451672588b684d484dce6ae251e9e163e4a46924322reed@google.com        return this->setPixelRef(pr, origin.fX, origin.fY);
452672588b684d484dce6ae251e9e163e4a46924322reed@google.com    }
4531ae9111f33db7640ce87d55156c1b6d491237db2skia.committer@gmail.com
454672588b684d484dce6ae251e9e163e4a46924322reed@google.com    SkPixelRef* setPixelRef(SkPixelRef* pr) {
455672588b684d484dce6ae251e9e163e4a46924322reed@google.com        return this->setPixelRef(pr, 0, 0);
456672588b684d484dce6ae251e9e163e4a46924322reed@google.com    }
457f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
4588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Call this to ensure that the bitmap points to the current pixel address
4598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        in the pixelref. Balance it with a call to unlockPixels(). These calls
4608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        are harmless if there is no pixelref.
4618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
4628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void lockPixels() const;
4638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** When you are finished access the pixel memory, call this to balance a
4648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        previous call to lockPixels(). This allows pixelrefs that implement
4658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        cached/deferred image decoding to know when there are active clients of
4668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        a given image.
4678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
4688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void unlockPixels() const;
469f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
4709c49bc3e643c435677727c1c0904c4a7cb7a6907reed@google.com    /**
4719c49bc3e643c435677727c1c0904c4a7cb7a6907reed@google.com     *  Some bitmaps can return a copy of their pixels for lockPixels(), but
4729c49bc3e643c435677727c1c0904c4a7cb7a6907reed@google.com     *  that copy, if modified, will not be pushed back. These bitmaps should
4739c49bc3e643c435677727c1c0904c4a7cb7a6907reed@google.com     *  not be used as targets for a raster device/canvas (since all pixels
4749c49bc3e643c435677727c1c0904c4a7cb7a6907reed@google.com     *  modifications will be lost when unlockPixels() is called.)
4759c49bc3e643c435677727c1c0904c4a7cb7a6907reed@google.com     */
4769c49bc3e643c435677727c1c0904c4a7cb7a6907reed@google.com    bool lockPixelsAreWritable() const;
4779c49bc3e643c435677727c1c0904c4a7cb7a6907reed@google.com
4788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Call this to be sure that the bitmap is valid enough to be drawn (i.e.
479ddd014e65264e3f18b2d4305dd2fbc81549f5180reed        it has non-null pixels, and if required by its colortype, it has a
4808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        non-null colortable. Returns true if all of the above are met.
4818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
4828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool readyToDraw() const {
4838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return this->getPixels() != NULL &&
48469e64637925d51f746560b2da21c4b0a10eed48areed@google.com               (this->colorType() != kIndex_8_SkColorType || NULL != fColorTable);
4858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
487ce4e53a2347040a3c145768c193a7a7958c0d4ecreed@android.com    /** Returns the pixelRef's texture, or NULL
488ce4e53a2347040a3c145768c193a7a7958c0d4ecreed@android.com     */
489b8d00db075b5ea09e353508a26ef5ced50722a6ccommit-bot@chromium.org    GrTexture* getTexture() const;
490ce4e53a2347040a3c145768c193a7a7958c0d4ecreed@android.com
49161e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org    /** Return the bitmap's colortable, if it uses one (i.e. colorType is
49261e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org        Index_8) and the pixels are locked.
493665b2cca88e25b821a69c9ce8a4fd7f551532012scroggo@google.com        Otherwise returns NULL. Does not affect the colortable's
4948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        reference count.
4958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
4968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkColorTable* getColorTable() const { return fColorTable; }
4978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns a non-zero, unique value corresponding to the pixels in our
499c84b8335ee4fd864c30a4703afc643cf4b5010d9djsollen@google.com        pixelref. Each time the pixels are changed (and notifyPixelsChanged
500c84b8335ee4fd864c30a4703afc643cf4b5010d9djsollen@google.com        is called), a different generation ID will be returned. Finally, if
50158edea89627d347010cadc26ce3c092a9265a8eescroggo        there is no pixelRef then zero is returned.
5028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
5038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint32_t getGenerationID() const;
504f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
5058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Call this if you have changed the contents of the pixels. This will in-
5068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        turn cause a different generation ID value to be returned from
5078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        getGenerationID().
5088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
5098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void notifyPixelsChanged() const;
5108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
51160d3235ab42bd1d32a070695ee30d6e3ec2fa222reed@google.com    /**
51260d3235ab42bd1d32a070695ee30d6e3ec2fa222reed@google.com     *  Fill the entire bitmap with the specified color.
513ddd014e65264e3f18b2d4305dd2fbc81549f5180reed     *  If the bitmap's colortype does not support alpha (e.g. 565) then the alpha
514ddd014e65264e3f18b2d4305dd2fbc81549f5180reed     *  of the color is ignored (treated as opaque). If the colortype only supports
51560d3235ab42bd1d32a070695ee30d6e3ec2fa222reed@google.com     *  alpha (e.g. A1 or A8) then the color's r,g,b components are ignored.
51660d3235ab42bd1d32a070695ee30d6e3ec2fa222reed@google.com     */
5178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void eraseColor(SkColor c) const {
5188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->eraseARGB(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c),
5198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        SkColorGetB(c));
5208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
521f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
52260d3235ab42bd1d32a070695ee30d6e3ec2fa222reed@google.com    /**
52360d3235ab42bd1d32a070695ee30d6e3ec2fa222reed@google.com     *  Fill the entire bitmap with the specified color.
524ddd014e65264e3f18b2d4305dd2fbc81549f5180reed     *  If the bitmap's colortype does not support alpha (e.g. 565) then the alpha
525ddd014e65264e3f18b2d4305dd2fbc81549f5180reed     *  of the color is ignored (treated as opaque). If the colortype only supports
52660d3235ab42bd1d32a070695ee30d6e3ec2fa222reed@google.com     *  alpha (e.g. A1 or A8) then the color's r,g,b components are ignored.
52760d3235ab42bd1d32a070695ee30d6e3ec2fa222reed@google.com     */
52860d3235ab42bd1d32a070695ee30d6e3ec2fa222reed@google.com    void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const;
52960d3235ab42bd1d32a070695ee30d6e3ec2fa222reed@google.com
5304469938e92d779dff05e745559e67907bbf21e78reed@google.com    SK_ATTR_DEPRECATED("use eraseARGB or eraseColor")
53160d3235ab42bd1d32a070695ee30d6e3ec2fa222reed@google.com    void eraseRGB(U8CPU r, U8CPU g, U8CPU b) const {
53260d3235ab42bd1d32a070695ee30d6e3ec2fa222reed@google.com        this->eraseARGB(0xFF, r, g, b);
53360d3235ab42bd1d32a070695ee30d6e3ec2fa222reed@google.com    }
53460d3235ab42bd1d32a070695ee30d6e3ec2fa222reed@google.com
53560d3235ab42bd1d32a070695ee30d6e3ec2fa222reed@google.com    /**
53660d3235ab42bd1d32a070695ee30d6e3ec2fa222reed@google.com     *  Fill the specified area of this bitmap with the specified color.
537ddd014e65264e3f18b2d4305dd2fbc81549f5180reed     *  If the bitmap's colortype does not support alpha (e.g. 565) then the alpha
538ddd014e65264e3f18b2d4305dd2fbc81549f5180reed     *  of the color is ignored (treated as opaque). If the colortype only supports
53960d3235ab42bd1d32a070695ee30d6e3ec2fa222reed@google.com     *  alpha (e.g. A1 or A8) then the color's r,g,b components are ignored.
54060d3235ab42bd1d32a070695ee30d6e3ec2fa222reed@google.com     */
54160d3235ab42bd1d32a070695ee30d6e3ec2fa222reed@google.com    void eraseArea(const SkIRect& area, SkColor c) const;
54260d3235ab42bd1d32a070695ee30d6e3ec2fa222reed@google.com
5438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Scroll (a subset of) the contents of this bitmap by dx/dy. If there are
5448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        no pixels allocated (i.e. getPixels() returns null) the method will
5456a9368d41154890b6c316fdae704bf56ca4c70ccscroggo@google.com        still update the inval region (if present). If the bitmap is immutable,
5466a9368d41154890b6c316fdae704bf56ca4c70ccscroggo@google.com        do nothing and return false.
5478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param subset The subset of the bitmap to scroll/move. To scroll the
5498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                      entire contents, specify [0, 0, width, height] or just
5508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                      pass null.
5518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param dx The amount to scroll in X
5528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param dy The amount to scroll in Y
5538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param inval Optional (may be null). Returns the area of the bitmap that
5548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                     was scrolled away. E.g. if dx = dy = 0, then inval would
5558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                     be set to empty. If dx >= width or dy >= height, then
5568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                     inval would be set to the entire bounds of the bitmap.
557ddd014e65264e3f18b2d4305dd2fbc81549f5180reed        @return true if the scroll was doable. Will return false if the colortype is kUnkown or
558ddd014e65264e3f18b2d4305dd2fbc81549f5180reed                     if the bitmap is immutable.
5598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                     If no pixels are present (i.e. getPixels() returns false)
5608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                     inval will still be updated, and true will be returned.
5618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
5628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool scrollRect(const SkIRect* subset, int dx, int dy,
5638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkRegion* inval = NULL) const;
5648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5653b521d05335a1bf76be3456feb9dbfba0a51b339reed@google.com    /**
5663b521d05335a1bf76be3456feb9dbfba0a51b339reed@google.com     *  Return the SkColor of the specified pixel.  In most cases this will
567ddd014e65264e3f18b2d4305dd2fbc81549f5180reed     *  require un-premultiplying the color.  Alpha only colortypes (e.g. kAlpha_8_SkColorType)
5683b521d05335a1bf76be3456feb9dbfba0a51b339reed@google.com     *  return black with the appropriate alpha set.  The value is undefined
569ddd014e65264e3f18b2d4305dd2fbc81549f5180reed     *  for kUnknown_SkColorType or if x or y are out of bounds, or if the bitmap
5703b521d05335a1bf76be3456feb9dbfba0a51b339reed@google.com     *  does not have any pixels (or has not be locked with lockPixels()).
5713b521d05335a1bf76be3456feb9dbfba0a51b339reed@google.com     */
5723b521d05335a1bf76be3456feb9dbfba0a51b339reed@google.com    SkColor getColor(int x, int y) const;
573fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
5748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the address of the specified pixel. This performs a runtime
5758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        check to know the size of the pixels, and will return the same answer
5768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        as the corresponding size-specific method (e.g. getAddr16). Since the
5778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        check happens at runtime, it is much slower than using a size-specific
5788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        version. Unlike the size-specific methods, this routine also checks if
5798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        getPixels() returns null, and returns that. The size-specific routines
5808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        perform a debugging assert that getPixels() is not null, but they do
5818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        not do any runtime checks.
5828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
5838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* getAddr(int x, int y) const;
5848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the address of the pixel specified by x,y for 32bit pixels.
5863b521d05335a1bf76be3456feb9dbfba0a51b339reed@google.com     *  In debug build, this asserts that the pixels are allocated and locked,
587ddd014e65264e3f18b2d4305dd2fbc81549f5180reed     *  and that the colortype is 32-bit, however none of these checks are performed
5883b521d05335a1bf76be3456feb9dbfba0a51b339reed@google.com     *  in the release build.
5893b521d05335a1bf76be3456feb9dbfba0a51b339reed@google.com     */
5908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    inline uint32_t* getAddr32(int x, int y) const;
5913b521d05335a1bf76be3456feb9dbfba0a51b339reed@google.com
5928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the address of the pixel specified by x,y for 16bit pixels.
5933b521d05335a1bf76be3456feb9dbfba0a51b339reed@google.com     *  In debug build, this asserts that the pixels are allocated and locked,
594ddd014e65264e3f18b2d4305dd2fbc81549f5180reed     *  and that the colortype is 16-bit, however none of these checks are performed
5953b521d05335a1bf76be3456feb9dbfba0a51b339reed@google.com     *  in the release build.
5963b521d05335a1bf76be3456feb9dbfba0a51b339reed@google.com     */
5978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    inline uint16_t* getAddr16(int x, int y) const;
5983b521d05335a1bf76be3456feb9dbfba0a51b339reed@google.com
5998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the address of the pixel specified by x,y for 8bit pixels.
6003b521d05335a1bf76be3456feb9dbfba0a51b339reed@google.com     *  In debug build, this asserts that the pixels are allocated and locked,
601ddd014e65264e3f18b2d4305dd2fbc81549f5180reed     *  and that the colortype is 8-bit, however none of these checks are performed
6023b521d05335a1bf76be3456feb9dbfba0a51b339reed@google.com     *  in the release build.
6033b521d05335a1bf76be3456feb9dbfba0a51b339reed@google.com     */
6048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    inline uint8_t* getAddr8(int x, int y) const;
6053b521d05335a1bf76be3456feb9dbfba0a51b339reed@google.com
6068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the color corresponding to the pixel specified by x,y for
6073b521d05335a1bf76be3456feb9dbfba0a51b339reed@google.com     *  colortable based bitmaps.
6083b521d05335a1bf76be3456feb9dbfba0a51b339reed@google.com     *  In debug build, this asserts that the pixels are allocated and locked,
609ddd014e65264e3f18b2d4305dd2fbc81549f5180reed     *  that the colortype is indexed, and that the colortable is allocated,
6103b521d05335a1bf76be3456feb9dbfba0a51b339reed@google.com     *  however none of these checks are performed in the release build.
6113b521d05335a1bf76be3456feb9dbfba0a51b339reed@google.com     */
6128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    inline SkPMColor getIndex8Color(int x, int y) const;
6138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6149ef2d8382f5238cc54d211e3cc228def7a93b133epoger@google.com    /** Set dst to be a setset of this bitmap. If possible, it will share the
615ddd014e65264e3f18b2d4305dd2fbc81549f5180reed        pixel memory, and just point into a subset of it. However, if the colortype
6168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        does not support this, a local copy will be made and associated with
6178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        the dst bitmap. If the subset rectangle, intersected with the bitmap's
618ddd014e65264e3f18b2d4305dd2fbc81549f5180reed        dimensions is empty, or if there is an unsupported colortype, false will be
6198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        returned and dst will be untouched.
6208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param dst  The bitmap that will be set to a subset of this bitmap
6218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param subset The rectangle of pixels in this bitmap that dst will
6228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                      reference.
6238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @return true if the subset copy was successfully made.
6248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
6258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool extractSubset(SkBitmap* dst, const SkIRect& subset) const;
6268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6278a2ad3cae710f05cca57e48dd1732d575dba2dc7commit-bot@chromium.org    /** Makes a deep copy of this bitmap, respecting the requested colorType,
628ef843cdcd1be858fe55cc99ff134ffbd080c9a51senorblanco@chromium.org     *  and allocating the dst pixels on the cpu.
629ef843cdcd1be858fe55cc99ff134ffbd080c9a51senorblanco@chromium.org     *  Returns false if either there is an error (i.e. the src does not have
630ef843cdcd1be858fe55cc99ff134ffbd080c9a51senorblanco@chromium.org     *  pixels) or the request cannot be satisfied (e.g. the src has per-pixel
631ddd014e65264e3f18b2d4305dd2fbc81549f5180reed     *  alpha, and the requested colortype does not support alpha).
632ef843cdcd1be858fe55cc99ff134ffbd080c9a51senorblanco@chromium.org     *  @param dst The bitmap to be sized and allocated
6338a2ad3cae710f05cca57e48dd1732d575dba2dc7commit-bot@chromium.org     *  @param ct The desired colorType for dst
634ef843cdcd1be858fe55cc99ff134ffbd080c9a51senorblanco@chromium.org     *  @param allocator Allocator used to allocate the pixelref for the dst
635ef843cdcd1be858fe55cc99ff134ffbd080c9a51senorblanco@chromium.org     *                   bitmap. If this is null, the standard HeapAllocator
636ef843cdcd1be858fe55cc99ff134ffbd080c9a51senorblanco@chromium.org     *                   will be used.
6378a2ad3cae710f05cca57e48dd1732d575dba2dc7commit-bot@chromium.org     *  @return true if the copy was made.
638ef843cdcd1be858fe55cc99ff134ffbd080c9a51senorblanco@chromium.org     */
6398a2ad3cae710f05cca57e48dd1732d575dba2dc7commit-bot@chromium.org    bool copyTo(SkBitmap* dst, SkColorType ct, Allocator* = NULL) const;
6408a2ad3cae710f05cca57e48dd1732d575dba2dc7commit-bot@chromium.org
6418a2ad3cae710f05cca57e48dd1732d575dba2dc7commit-bot@chromium.org    bool copyTo(SkBitmap* dst, Allocator* allocator = NULL) const {
6428a2ad3cae710f05cca57e48dd1732d575dba2dc7commit-bot@chromium.org        return this->copyTo(dst, this->colorType(), allocator);
6438a2ad3cae710f05cca57e48dd1732d575dba2dc7commit-bot@chromium.org    }
6448a2ad3cae710f05cca57e48dd1732d575dba2dc7commit-bot@chromium.org
6458a2ad3cae710f05cca57e48dd1732d575dba2dc7commit-bot@chromium.org    /**
6468a2ad3cae710f05cca57e48dd1732d575dba2dc7commit-bot@chromium.org     *  Returns true if this bitmap's pixels can be converted into the requested
6478a2ad3cae710f05cca57e48dd1732d575dba2dc7commit-bot@chromium.org     *  colorType, such that copyTo() could succeed.
6488a2ad3cae710f05cca57e48dd1732d575dba2dc7commit-bot@chromium.org     */
6498a2ad3cae710f05cca57e48dd1732d575dba2dc7commit-bot@chromium.org    bool canCopyTo(SkColorType colorType) const;
65089bb83abb420c126b02fcdd4047fd6574e9b73fcreed@android.com
6516285f4f7662853336b788d6ee3e177c396f7fb01commit-bot@chromium.org    /** Makes a deep copy of this bitmap, keeping the copied pixels
6526285f4f7662853336b788d6ee3e177c396f7fb01commit-bot@chromium.org     *  in the same domain as the source: If the src pixels are allocated for
6536285f4f7662853336b788d6ee3e177c396f7fb01commit-bot@chromium.org     *  the cpu, then so will the dst. If the src pixels are allocated on the
6546285f4f7662853336b788d6ee3e177c396f7fb01commit-bot@chromium.org     *  gpu (typically as a texture), the it will do the same for the dst.
6556285f4f7662853336b788d6ee3e177c396f7fb01commit-bot@chromium.org     *  If the request cannot be fulfilled, returns false and dst is unmodified.
6566285f4f7662853336b788d6ee3e177c396f7fb01commit-bot@chromium.org     */
6576285f4f7662853336b788d6ee3e177c396f7fb01commit-bot@chromium.org    bool deepCopyTo(SkBitmap* dst) const;
658ef843cdcd1be858fe55cc99ff134ffbd080c9a51senorblanco@chromium.org
6594bd2bdbf04f21237337616aa931e34d7c8991edcdjsollen@google.com#ifdef SK_BUILD_FOR_ANDROID
6604bd2bdbf04f21237337616aa931e34d7c8991edcdjsollen@google.com    bool hasHardwareMipMap() const {
6614bd2bdbf04f21237337616aa931e34d7c8991edcdjsollen@google.com        return (fFlags & kHasHardwareMipMap_Flag) != 0;
6624bd2bdbf04f21237337616aa931e34d7c8991edcdjsollen@google.com    }
6634bd2bdbf04f21237337616aa931e34d7c8991edcdjsollen@google.com
6644bd2bdbf04f21237337616aa931e34d7c8991edcdjsollen@google.com    void setHasHardwareMipMap(bool hasHardwareMipMap) {
6654bd2bdbf04f21237337616aa931e34d7c8991edcdjsollen@google.com        if (hasHardwareMipMap) {
6664bd2bdbf04f21237337616aa931e34d7c8991edcdjsollen@google.com            fFlags |= kHasHardwareMipMap_Flag;
6674bd2bdbf04f21237337616aa931e34d7c8991edcdjsollen@google.com        } else {
6684bd2bdbf04f21237337616aa931e34d7c8991edcdjsollen@google.com            fFlags &= ~kHasHardwareMipMap_Flag;
6694bd2bdbf04f21237337616aa931e34d7c8991edcdjsollen@google.com        }
6704bd2bdbf04f21237337616aa931e34d7c8991edcdjsollen@google.com    }
6714bd2bdbf04f21237337616aa931e34d7c8991edcdjsollen@google.com#endif
6724bd2bdbf04f21237337616aa931e34d7c8991edcdjsollen@google.com
673cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    bool extractAlpha(SkBitmap* dst) const {
674cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        return this->extractAlpha(dst, NULL, NULL, NULL);
6758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
6768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
677cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    bool extractAlpha(SkBitmap* dst, const SkPaint* paint,
67857f4969724a1dd88c8d9ae35a863e6cf621181d5djsollen@google.com                      SkIPoint* offset) const {
679cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        return this->extractAlpha(dst, paint, NULL, offset);
68057f4969724a1dd88c8d9ae35a863e6cf621181d5djsollen@google.com    }
68157f4969724a1dd88c8d9ae35a863e6cf621181d5djsollen@google.com
682cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    /** Set dst to contain alpha layer of this bitmap. If destination bitmap
683cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        fails to be initialized, e.g. because allocator can't allocate pixels
684cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        for it, dst will not be modified and false will be returned.
685cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
686cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        @param dst The bitmap to be filled with alpha layer
687cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        @param paint The paint to draw with
688cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        @param allocator Allocator used to allocate the pixelref for the dst
689cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com                         bitmap. If this is null, the standard HeapAllocator
690cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com                         will be used.
691cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        @param offset If not null, it is set to top-left coordinate to position
692cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com                      the returned bitmap so that it visually lines up with the
693cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com                      original
694cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    */
695cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    bool extractAlpha(SkBitmap* dst, const SkPaint* paint, Allocator* allocator,
6968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                      SkIPoint* offset) const;
697f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
6988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDEBUGCODE(void validate() const;)
6998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    class Allocator : public SkRefCnt {
7018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    public:
70215e9d3e66e161ce23df30bc13f8a0c87d196b463robertphillips@google.com        SK_DECLARE_INST_COUNT(Allocator)
70315e9d3e66e161ce23df30bc13f8a0c87d196b463robertphillips@google.com
7048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        /** Allocate the pixel memory for the bitmap, given its dimensions and
705ddd014e65264e3f18b2d4305dd2fbc81549f5180reed            colortype. Return true on success, where success means either setPixels
7068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            or setPixelRef was called. The pixels need not be locked when this
707ddd014e65264e3f18b2d4305dd2fbc81549f5180reed            returns. If the colortype requires a colortable, it also must be
7088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            installed via setColorTable. If false is returned, the bitmap and
7098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            colortable should be left unchanged.
7108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        */
7118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        virtual bool allocPixelRef(SkBitmap*, SkColorTable*) = 0;
71215e9d3e66e161ce23df30bc13f8a0c87d196b463robertphillips@google.com    private:
71315e9d3e66e161ce23df30bc13f8a0c87d196b463robertphillips@google.com        typedef SkRefCnt INHERITED;
7148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
7158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Subclass of Allocator that returns a pixelref that allocates its pixel
7178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        memory from the heap. This is the default Allocator invoked by
7188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        allocPixels().
7198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
7208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    class HeapAllocator : public Allocator {
7218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    public:
7229ebcac54635cde63110d73ad7c43d70772e7872freed@google.com        virtual bool allocPixelRef(SkBitmap*, SkColorTable*) SK_OVERRIDE;
7238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
7248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    class RLEPixels {
7268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    public:
7278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        RLEPixels(int width, int height);
7288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        virtual ~RLEPixels();
729f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
7308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        uint8_t* packedAtY(int y) const {
7318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT((unsigned)y < (unsigned)fHeight);
7328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return fYPtrs[y];
7338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
734f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
7358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // called by subclasses during creation
7368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        void setPackedAtY(int y, uint8_t* addr) {
7378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT((unsigned)y < (unsigned)fHeight);
7388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fYPtrs[y] = addr;
7398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
740f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
7418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    private:
7428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        uint8_t** fYPtrs;
7438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int       fHeight;
7448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
745f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
7460f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    SK_TO_STRING_NONVIRT()
74776f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
7488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
7498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    mutable SkPixelRef* fPixelRef;
7508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    mutable int         fPixelLockCount;
7515f62ed74342e44cb1b9671ffdac47c6a81416136reed@google.com    // These are just caches from the locked pixelref
7528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    mutable void*       fPixels;
7538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    mutable SkColorTable* fColorTable;    // only meaningful for kIndex8
7548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
755672588b684d484dce6ae251e9e163e4a46924322reed@google.com    SkIPoint    fPixelRefOrigin;
756672588b684d484dce6ae251e9e163e4a46924322reed@google.com
7578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    enum Flags {
758b052129a4121c1e3f051e374541a93d3d7a98064junov@chromium.org        kImageIsOpaque_Flag     = 0x01,
759b052129a4121c1e3f051e374541a93d3d7a98064junov@chromium.org        kImageIsVolatile_Flag   = 0x02,
7604bd2bdbf04f21237337616aa931e34d7c8991edcdjsollen@google.com        kImageIsImmutable_Flag  = 0x04,
7614bd2bdbf04f21237337616aa931e34d7c8991edcdjsollen@google.com#ifdef SK_BUILD_FOR_ANDROID
7624bd2bdbf04f21237337616aa931e34d7c8991edcdjsollen@google.com        /* A hint for the renderer responsible for drawing this bitmap
7634bd2bdbf04f21237337616aa931e34d7c8991edcdjsollen@google.com         * indicating that it should attempt to use mipmaps when this bitmap
7644bd2bdbf04f21237337616aa931e34d7c8991edcdjsollen@google.com         * is drawn scaled down.
7654bd2bdbf04f21237337616aa931e34d7c8991edcdjsollen@google.com         */
7664bd2bdbf04f21237337616aa931e34d7c8991edcdjsollen@google.com        kHasHardwareMipMap_Flag = 0x08,
7674bd2bdbf04f21237337616aa931e34d7c8991edcdjsollen@google.com#endif
7688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
7698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
77061e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org    SkImageInfo fInfo;
77161e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org
772e5f48243bdbed2662be7a31be0888abc273b09e8scroggo@google.com    uint32_t    fRowBytes;
77361e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org
7748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint8_t     fFlags;
7758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
77660d3235ab42bd1d32a070695ee30d6e3ec2fa222reed@google.com    void internalErase(const SkIRect&, U8CPU a, U8CPU r, U8CPU g, U8CPU b)const;
77760d3235ab42bd1d32a070695ee30d6e3ec2fa222reed@google.com
7788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /*  Unreference any pixelrefs or colortables
7798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
7808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void freePixels();
7818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void updatePixelsFromRef() const;
7823c134a97ed128ca95345e13619512df0f42e95f8skia.committer@gmail.com
783851155c28e18e3f5b702ef15ee7bfb12f3cbacdacommit-bot@chromium.org    void legacyUnflatten(SkReadBuffer&);
784851155c28e18e3f5b702ef15ee7bfb12f3cbacdacommit-bot@chromium.org
785968edcafa61442dc4f7f8ed8f89523d0f353e9fbcommit-bot@chromium.org    static void WriteRawPixels(SkWriteBuffer*, const SkBitmap&);
786968edcafa61442dc4f7f8ed8f89523d0f353e9fbcommit-bot@chromium.org    static bool ReadRawPixels(SkReadBuffer*, SkBitmap*);
787f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
788968edcafa61442dc4f7f8ed8f89523d0f353e9fbcommit-bot@chromium.org    friend class SkBitmapSource;    // unflatten
789968edcafa61442dc4f7f8ed8f89523d0f353e9fbcommit-bot@chromium.org    friend class SkReadBuffer;      // unflatten, rawpixels
790968edcafa61442dc4f7f8ed8f89523d0f353e9fbcommit-bot@chromium.org    friend class SkWriteBuffer;     // rawpixels
7914a551d49cf44397a99c73787f63409568cf85009reed@google.com    friend struct SkBitmapProcState;
7928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
7938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
794e3beb6bd7de7fa211681abbb0be58e80b19885e0commit-bot@chromium.orgclass SkAutoLockPixels : SkNoncopyable {
7958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
7969c49bc3e643c435677727c1c0904c4a7cb7a6907reed@google.com    SkAutoLockPixels(const SkBitmap& bm, bool doLock = true) : fBitmap(bm) {
7979c49bc3e643c435677727c1c0904c4a7cb7a6907reed@google.com        fDidLock = doLock;
7989c49bc3e643c435677727c1c0904c4a7cb7a6907reed@google.com        if (doLock) {
7999c49bc3e643c435677727c1c0904c4a7cb7a6907reed@google.com            bm.lockPixels();
8009c49bc3e643c435677727c1c0904c4a7cb7a6907reed@google.com        }
8018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ~SkAutoLockPixels() {
8039c49bc3e643c435677727c1c0904c4a7cb7a6907reed@google.com        if (fDidLock) {
8049c49bc3e643c435677727c1c0904c4a7cb7a6907reed@google.com            fBitmap.unlockPixels();
8059c49bc3e643c435677727c1c0904c4a7cb7a6907reed@google.com        }
8068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
8098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkBitmap& fBitmap;
8109c49bc3e643c435677727c1c0904c4a7cb7a6907reed@google.com    bool            fDidLock;
8118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
8120f6dc21e324a0829ed7f4ff4c4238f75f62683b5mtklein@google.com//TODO(mtklein): uncomment when 71713004 lands and Chromium's fixed.
8130f6dc21e324a0829ed7f4ff4c4238f75f62683b5mtklein@google.com//#define SkAutoLockPixels(...) SK_REQUIRE_LOCAL_VAR(SkAutoLockPixels)
8148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Helper class that performs the lock/unlockColors calls on a colortable.
8168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    The destructor will call unlockColors(false) if it has a bitmap's colortable
8178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
818e3beb6bd7de7fa211681abbb0be58e80b19885e0commit-bot@chromium.orgclass SkAutoLockColors : SkNoncopyable {
8198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
8208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Initialize with no bitmap. Call lockColors(bitmap) to lock bitmap's
8218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        colortable
8228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
8238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoLockColors() : fCTable(NULL), fColors(NULL) {}
8248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Initialize with bitmap, locking its colortable if present
8258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
8268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    explicit SkAutoLockColors(const SkBitmap& bm) {
8278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fCTable = bm.getColorTable();
8288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fColors = fCTable ? fCTable->lockColors() : NULL;
8298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Initialize with a colortable (may be null)
8318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
8328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    explicit SkAutoLockColors(SkColorTable* ctable) {
8338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fCTable = ctable;
8348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fColors = ctable ? ctable->lockColors() : NULL;
8358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ~SkAutoLockColors() {
8378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (fCTable) {
8380a6151d66cc32d91eca037c91e557158cf8a2be2reed@google.com            fCTable->unlockColors();
8398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
8408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
841f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
8428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the currently locked colors, or NULL if no bitmap's colortable
8438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        is currently locked.
8448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
8458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkPMColor* colors() const { return fColors; }
846f9ab99aaade8c451c0e9309b4c61a448373019e3weita@google.com
8471134426f6b1c4130e8dbe4c38a4aa976fbb18b4breed@android.com    /** Locks the table and returns is colors (assuming ctable is not null) and
8481134426f6b1c4130e8dbe4c38a4aa976fbb18b4breed@android.com        unlocks the previous table if one was present
8491134426f6b1c4130e8dbe4c38a4aa976fbb18b4breed@android.com     */
8501134426f6b1c4130e8dbe4c38a4aa976fbb18b4breed@android.com    const SkPMColor* lockColors(SkColorTable* ctable) {
8518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (fCTable) {
8520a6151d66cc32d91eca037c91e557158cf8a2be2reed@google.com            fCTable->unlockColors();
8538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
8541134426f6b1c4130e8dbe4c38a4aa976fbb18b4breed@android.com        fCTable = ctable;
8551134426f6b1c4130e8dbe4c38a4aa976fbb18b4breed@android.com        fColors = ctable ? ctable->lockColors() : NULL;
8568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return fColors;
8578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8591134426f6b1c4130e8dbe4c38a4aa976fbb18b4breed@android.com    const SkPMColor* lockColors(const SkBitmap& bm) {
8601134426f6b1c4130e8dbe4c38a4aa976fbb18b4breed@android.com        return this->lockColors(bm.getColorTable());
8611134426f6b1c4130e8dbe4c38a4aa976fbb18b4breed@android.com    }
8621134426f6b1c4130e8dbe4c38a4aa976fbb18b4breed@android.com
8638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
8648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkColorTable*    fCTable;
8658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkPMColor* fColors;
8668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
867e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org#define SkAutoLockColors(...) SK_REQUIRE_LOCAL_VAR(SkAutoLockColors)
8688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
8708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.cominline uint32_t* SkBitmap::getAddr32(int x, int y) const {
8728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(fPixels);
87369e64637925d51f746560b2da21c4b0a10eed48areed@google.com    SkASSERT(4 == this->bytesPerPixel());
87461e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org    SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height());
8758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (uint32_t*)((char*)fPixels + y * fRowBytes + (x << 2));
8768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
8778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.cominline uint16_t* SkBitmap::getAddr16(int x, int y) const {
8798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(fPixels);
88069e64637925d51f746560b2da21c4b0a10eed48areed@google.com    SkASSERT(2 == this->bytesPerPixel());
88161e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org    SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height());
8828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (uint16_t*)((char*)fPixels + y * fRowBytes + (x << 1));
8838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
8848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.cominline uint8_t* SkBitmap::getAddr8(int x, int y) const {
8868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(fPixels);
88769e64637925d51f746560b2da21c4b0a10eed48areed@google.com    SkASSERT(1 == this->bytesPerPixel());
88861e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org    SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height());
8898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (uint8_t*)fPixels + y * fRowBytes + x;
8908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
8918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.cominline SkPMColor SkBitmap::getIndex8Color(int x, int y) const {
8938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(fPixels);
89469e64637925d51f746560b2da21c4b0a10eed48areed@google.com    SkASSERT(kIndex_8_SkColorType == this->colorType());
89561e96cd44624c9faceb625519c1b29775b161f45commit-bot@chromium.org    SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height());
8968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(fColorTable);
8978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (*fColorTable)[*((const uint8_t*)fPixels + y * fRowBytes + x)];
8988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
8998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
900c3b3266b7db2f1a41d41ecac010c766b7ad8eebcreed#ifdef SK_SUPPORT_LEGACY_BITMAP_CONFIG
901c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org///////////////////////////////////////////////////////////////////////////////
902c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org//
903c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org// Helpers until we can fully deprecate SkBitmap::Config
904c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org//
905c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.orgextern SkBitmap::Config SkColorTypeToBitmapConfig(SkColorType);
906c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.orgextern SkColorType SkBitmapConfigToColorType(SkBitmap::Config);
907c3b3266b7db2f1a41d41ecac010c766b7ad8eebcreed#endif
908c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org
9098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
910