2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2006 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkBitmap_DEFINED
|
|
|
|
#define SkBitmap_DEFINED
|
|
|
|
|
|
|
|
#include "SkColor.h"
|
2012-06-12 15:17:27 +00:00
|
|
|
#include "SkColorTable.h"
|
2013-11-13 19:09:13 +00:00
|
|
|
#include "SkImageInfo.h"
|
2015-05-22 15:06:21 +00:00
|
|
|
#include "SkPixmap.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkPoint.h"
|
|
|
|
#include "SkRefCnt.h"
|
|
|
|
|
2014-02-17 21:21:46 +00:00
|
|
|
struct SkMask;
|
2008-12-17 15:59:43 +00:00
|
|
|
struct SkIRect;
|
2012-03-15 21:18:11 +00:00
|
|
|
struct SkRect;
|
2008-12-17 15:59:43 +00:00
|
|
|
class SkPaint;
|
|
|
|
class SkPixelRef;
|
2014-01-24 18:53:42 +00:00
|
|
|
class SkPixelRefFactory;
|
2008-12-17 15:59:43 +00:00
|
|
|
class SkRegion;
|
2013-01-15 20:17:47 +00:00
|
|
|
class SkString;
|
2013-06-26 19:18:23 +00:00
|
|
|
class GrTexture;
|
2010-09-09 16:01:26 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** \class SkBitmap
|
|
|
|
|
|
|
|
The SkBitmap class specifies a raster bitmap. A bitmap has an integer width
|
2014-06-05 15:51:20 +00:00
|
|
|
and height, and a format (colortype), and a pointer to the actual pixels.
|
2011-04-27 15:39:30 +00:00
|
|
|
Bitmaps can be drawn into a SkCanvas, but they are also used to specify the
|
|
|
|
target of a SkCanvas' drawing operations.
|
|
|
|
A const SkBitmap exposes getAddr(), which lets a caller write its pixels;
|
|
|
|
the constness is considered to apply to the bitmap's configuration, not
|
|
|
|
its contents.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2011-03-15 21:27:08 +00:00
|
|
|
class SK_API SkBitmap {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2013-11-25 15:44:37 +00:00
|
|
|
class SK_API Allocator;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2012-05-16 13:04:22 +00:00
|
|
|
/**
|
|
|
|
* Default construct creates a bitmap with zero width and height, and no pixels.
|
2014-06-05 15:51:20 +00:00
|
|
|
* Its colortype is set to kUnknown_SkColorType.
|
2012-05-16 13:04:22 +00:00
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
SkBitmap();
|
2012-05-16 13:04:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy the settings from the src into this bitmap. If the src has pixels
|
|
|
|
* allocated, they will be shared, not copied, so that the two bitmaps will
|
|
|
|
* reference the same memory for the pixels. If a deep copy is needed,
|
|
|
|
* where the new bitmap has its own separate copy of the pixels, use
|
|
|
|
* deepCopyTo().
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
SkBitmap(const SkBitmap& src);
|
2012-05-16 13:04:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
~SkBitmap();
|
|
|
|
|
|
|
|
/** Copies the src bitmap into this bitmap. Ownership of the src bitmap's pixels remains
|
|
|
|
with the src bitmap.
|
|
|
|
*/
|
|
|
|
SkBitmap& operator=(const SkBitmap& src);
|
|
|
|
/** Swap the fields of the two bitmaps. This routine is guaranteed to never fail or throw.
|
|
|
|
*/
|
|
|
|
// This method is not exported to java.
|
|
|
|
void swap(SkBitmap& other);
|
2009-05-03 18:23:30 +00:00
|
|
|
|
2014-02-11 18:21:45 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
const SkImageInfo& info() const { return fInfo; }
|
|
|
|
|
2014-09-03 18:54:58 +00:00
|
|
|
int width() const { return fInfo.width(); }
|
|
|
|
int height() const { return fInfo.height(); }
|
|
|
|
SkColorType colorType() const { return fInfo.colorType(); }
|
|
|
|
SkAlphaType alphaType() const { return fInfo.alphaType(); }
|
2014-12-22 16:31:49 +00:00
|
|
|
SkColorProfileType profileType() const { return fInfo.profileType(); }
|
2014-02-11 18:21:45 +00:00
|
|
|
|
2014-06-05 15:51:20 +00:00
|
|
|
/**
|
|
|
|
* Return the number of bytes per pixel based on the colortype. If the colortype is
|
|
|
|
* kUnknown_SkColorType, then 0 is returned.
|
2014-02-11 18:21:45 +00:00
|
|
|
*/
|
|
|
|
int bytesPerPixel() const { return fInfo.bytesPerPixel(); }
|
|
|
|
|
2014-06-05 15:51:20 +00:00
|
|
|
/**
|
|
|
|
* Return the rowbytes expressed as a number of pixels (like width and height).
|
|
|
|
* If the colortype is kUnknown_SkColorType, then 0 is returned.
|
2014-02-11 18:21:45 +00:00
|
|
|
*/
|
|
|
|
int rowBytesAsPixels() const {
|
|
|
|
return fRowBytes >> this->shiftPerPixel();
|
|
|
|
}
|
|
|
|
|
2014-06-05 15:51:20 +00:00
|
|
|
/**
|
|
|
|
* Return the shift amount per pixel (i.e. 0 for 1-byte per pixel, 1 for 2-bytes per pixel
|
|
|
|
* colortypes, 2 for 4-bytes per pixel colortypes). Return 0 for kUnknown_SkColorType.
|
2014-02-11 18:21:45 +00:00
|
|
|
*/
|
|
|
|
int shiftPerPixel() const { return this->bytesPerPixel() >> 1; }
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Return true iff the bitmap has empty dimensions.
|
2014-02-10 18:29:10 +00:00
|
|
|
* Hey! Before you use this, see if you really want to know drawsNothing() instead.
|
|
|
|
*/
|
2014-02-11 18:21:45 +00:00
|
|
|
bool empty() const { return fInfo.isEmpty(); }
|
2009-05-03 18:23:30 +00:00
|
|
|
|
2012-07-27 13:41:44 +00:00
|
|
|
/** Return true iff the bitmap has no pixelref. Note: this can return true even if the
|
2014-02-10 18:29:10 +00:00
|
|
|
* dimensions of the bitmap are > 0 (see empty()).
|
|
|
|
* Hey! Before you use this, see if you really want to know drawsNothing() instead.
|
|
|
|
*/
|
2012-07-27 13:41:44 +00:00
|
|
|
bool isNull() const { return NULL == fPixelRef; }
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2014-02-10 18:29:10 +00:00
|
|
|
/** Return true iff drawing this bitmap has no effect.
|
|
|
|
*/
|
|
|
|
bool drawsNothing() const { return this->empty() || this->isNull(); }
|
|
|
|
|
2013-10-31 17:28:30 +00:00
|
|
|
/** Return the number of bytes between subsequent rows of the bitmap. */
|
2013-02-25 16:02:36 +00:00
|
|
|
size_t rowBytes() const { return fRowBytes; }
|
2009-05-03 18:23:30 +00:00
|
|
|
|
2013-10-21 14:00:07 +00:00
|
|
|
/**
|
|
|
|
* Set the bitmap's alphaType, returning true on success. If false is
|
|
|
|
* returned, then the specified new alphaType is incompatible with the
|
2014-06-05 15:51:20 +00:00
|
|
|
* colortype, and the current alphaType is unchanged.
|
2014-01-27 15:41:07 +00:00
|
|
|
*
|
|
|
|
* Note: this changes the alphatype for the underlying pixels, which means
|
|
|
|
* that all bitmaps that might be sharing (subsets of) the pixels will
|
2014-01-28 16:05:39 +00:00
|
|
|
* be affected.
|
2013-10-21 14:00:07 +00:00
|
|
|
*/
|
|
|
|
bool setAlphaType(SkAlphaType);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Return the address of the pixels for this SkBitmap.
|
|
|
|
*/
|
|
|
|
void* getPixels() const { return fPixels; }
|
|
|
|
|
|
|
|
/** Return the byte size of the pixels, based on the height and rowBytes.
|
|
|
|
Note this truncates the result to 32bits. Call getSize64() to detect
|
|
|
|
if the real size exceeds 32bits.
|
|
|
|
*/
|
2014-09-03 18:54:58 +00:00
|
|
|
size_t getSize() const { return fInfo.height() * fRowBytes; }
|
2009-05-03 18:23:30 +00:00
|
|
|
|
2010-11-16 20:22:41 +00:00
|
|
|
/** Return the number of bytes from the pointer returned by getPixels()
|
|
|
|
to the end of the allocated space in the buffer. Required in
|
2012-12-07 15:27:27 +00:00
|
|
|
cases where extractSubset has been called.
|
2010-11-16 20:22:41 +00:00
|
|
|
*/
|
2014-02-11 18:21:45 +00:00
|
|
|
size_t getSafeSize() const { return fInfo.getSafeSize(fRowBytes); }
|
2010-11-16 20:22:41 +00:00
|
|
|
|
2013-12-30 14:40:38 +00:00
|
|
|
/**
|
|
|
|
* Return the full size of the bitmap, in bytes.
|
|
|
|
*/
|
|
|
|
int64_t computeSize64() const {
|
2014-09-03 18:54:58 +00:00
|
|
|
return sk_64_mul(fInfo.height(), fRowBytes);
|
2013-12-30 14:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the number of bytes from the pointer returned by getPixels()
|
|
|
|
* to the end of the allocated space in the buffer. This may be smaller
|
|
|
|
* than computeSize64() if there is any rowbytes padding beyond the width.
|
|
|
|
*/
|
|
|
|
int64_t computeSafeSize64() const {
|
2014-02-11 18:21:45 +00:00
|
|
|
return fInfo.getSafeSize64(fRowBytes);
|
2013-12-30 14:40:38 +00:00
|
|
|
}
|
|
|
|
|
2011-12-15 20:14:06 +00:00
|
|
|
/** Returns true if this bitmap is marked as immutable, meaning that the
|
|
|
|
contents of its pixels will not change for the lifetime of the bitmap.
|
|
|
|
*/
|
|
|
|
bool isImmutable() const;
|
|
|
|
|
|
|
|
/** Marks this bitmap as immutable, meaning that the contents of its
|
|
|
|
pixels will not change for the lifetime of the bitmap and of the
|
2012-08-23 18:09:54 +00:00
|
|
|
underlying pixelref. This state can be set, but it cannot be
|
2011-12-15 20:14:06 +00:00
|
|
|
cleared once it is set. This state propagates to all other bitmaps
|
|
|
|
that share the same pixelref.
|
|
|
|
*/
|
|
|
|
void setImmutable();
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Returns true if the bitmap is opaque (has no translucent/transparent pixels).
|
|
|
|
*/
|
2013-10-21 14:00:07 +00:00
|
|
|
bool isOpaque() const {
|
|
|
|
return SkAlphaTypeIsOpaque(this->alphaType());
|
|
|
|
}
|
2011-06-30 17:30:49 +00:00
|
|
|
|
|
|
|
/** Returns true if the bitmap is volatile (i.e. should not be cached by devices.)
|
|
|
|
*/
|
|
|
|
bool isVolatile() const;
|
|
|
|
|
2012-08-23 18:09:54 +00:00
|
|
|
/** Specify whether this bitmap is volatile. Bitmaps are not volatile by
|
2011-06-30 17:30:49 +00:00
|
|
|
default. Temporary bitmaps that are discarded after use should be
|
|
|
|
marked as volatile. This provides a hint to the device that the bitmap
|
2012-08-23 18:09:54 +00:00
|
|
|
should not be cached. Providing this hint when appropriate can
|
|
|
|
improve performance by avoiding unnecessary overhead and resource
|
2011-06-30 17:30:49 +00:00
|
|
|
consumption on the device.
|
|
|
|
*/
|
|
|
|
void setIsVolatile(bool);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Reset the bitmap to its initial state (see default constructor). If we are a (shared)
|
|
|
|
owner of the pixels, that ownership is decremented.
|
|
|
|
*/
|
|
|
|
void reset();
|
|
|
|
|
2012-11-07 18:30:18 +00:00
|
|
|
/**
|
|
|
|
* This will brute-force return true if all of the pixels in the bitmap
|
|
|
|
* are opaque. If it fails to read the pixels, or encounters an error,
|
|
|
|
* it will return false.
|
|
|
|
*
|
|
|
|
* Since this can be an expensive operation, the bitmap stores a flag for
|
2013-11-12 18:16:34 +00:00
|
|
|
* this (isOpaque). Only call this if you need to compute this value from
|
|
|
|
* "unknown" pixels.
|
2012-11-07 18:30:18 +00:00
|
|
|
*/
|
|
|
|
static bool ComputeIsOpaque(const SkBitmap&);
|
|
|
|
|
2012-03-15 21:17:03 +00:00
|
|
|
/**
|
|
|
|
* Return the bitmap's bounds [0, 0, width, height] as an SkRect
|
|
|
|
*/
|
|
|
|
void getBounds(SkRect* bounds) const;
|
2012-03-16 14:58:07 +00:00
|
|
|
void getBounds(SkIRect* bounds) const;
|
2012-03-15 21:17:03 +00:00
|
|
|
|
2014-10-24 19:54:53 +00:00
|
|
|
SkIRect bounds() const { return fInfo.bounds(); }
|
|
|
|
SkISize dimensions() const { return fInfo.dimensions(); }
|
2015-09-10 21:33:38 +00:00
|
|
|
// Returns the bounds of this bitmap, offset by its pixelref origin.
|
|
|
|
SkIRect getSubset() const {
|
|
|
|
return SkIRect::MakeXYWH(fPixelRefOrigin.x(), fPixelRefOrigin.y(),
|
|
|
|
fInfo.width(), fInfo.height());
|
|
|
|
}
|
2014-10-24 19:54:53 +00:00
|
|
|
|
2014-05-30 13:26:10 +00:00
|
|
|
bool setInfo(const SkImageInfo&, size_t rowBytes = 0);
|
|
|
|
|
2014-01-24 18:53:42 +00:00
|
|
|
/**
|
2014-06-28 21:26:35 +00:00
|
|
|
* Allocate the bitmap's pixels to match the requested image info. If the Factory
|
2014-01-24 18:53:42 +00:00
|
|
|
* is non-null, call it to allcoate the pixelref. If the ImageInfo requires
|
|
|
|
* a colortable, then ColorTable must be non-null, and will be ref'd.
|
|
|
|
* On failure, the bitmap will be set to empty and return false.
|
|
|
|
*/
|
2014-09-02 19:50:45 +00:00
|
|
|
bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo&, SkPixelRefFactory*, SkColorTable*);
|
|
|
|
|
2014-12-18 20:48:16 +00:00
|
|
|
void allocPixels(const SkImageInfo& info, SkPixelRefFactory* factory, SkColorTable* ctable) {
|
2014-09-02 19:50:45 +00:00
|
|
|
if (!this->tryAllocPixels(info, factory, ctable)) {
|
2014-12-18 20:48:16 +00:00
|
|
|
sk_throw();
|
2014-09-02 19:50:45 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-25 07:01:49 +00:00
|
|
|
|
2014-06-28 21:26:35 +00:00
|
|
|
/**
|
|
|
|
* Allocate the bitmap's pixels to match the requested image info and
|
|
|
|
* rowBytes. If the request cannot be met (e.g. the info is invalid or
|
|
|
|
* the requested rowBytes are not compatible with the info
|
|
|
|
* (e.g. rowBytes < info.minRowBytes() or rowBytes is not aligned with
|
|
|
|
* the pixel size specified by info.colorType()) then false is returned
|
|
|
|
* and the bitmap is set to empty.
|
|
|
|
*/
|
2014-09-02 19:50:45 +00:00
|
|
|
bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info, size_t rowBytes);
|
2014-06-28 21:26:35 +00:00
|
|
|
|
2014-12-18 20:48:16 +00:00
|
|
|
void allocPixels(const SkImageInfo& info, size_t rowBytes) {
|
2014-09-02 19:50:45 +00:00
|
|
|
if (!this->tryAllocPixels(info, rowBytes)) {
|
2014-12-18 20:48:16 +00:00
|
|
|
sk_throw();
|
2014-09-02 19:50:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info) {
|
|
|
|
return this->tryAllocPixels(info, info.minRowBytes());
|
|
|
|
}
|
|
|
|
|
2014-12-18 20:48:16 +00:00
|
|
|
void allocPixels(const SkImageInfo& info) {
|
|
|
|
this->allocPixels(info, info.minRowBytes());
|
2014-01-24 18:53:42 +00:00
|
|
|
}
|
2014-01-25 07:01:49 +00:00
|
|
|
|
2014-09-02 19:50:45 +00:00
|
|
|
bool SK_WARN_UNUSED_RESULT tryAllocN32Pixels(int width, int height, bool isOpaque = false) {
|
2014-09-03 18:54:58 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32(width, height,
|
|
|
|
isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
|
2014-09-02 19:50:45 +00:00
|
|
|
return this->tryAllocPixels(info);
|
|
|
|
}
|
2014-12-02 17:11:25 +00:00
|
|
|
|
2014-12-18 20:48:16 +00:00
|
|
|
void allocN32Pixels(int width, int height, bool isOpaque = false) {
|
2014-09-03 18:54:58 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32(width, height,
|
|
|
|
isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
|
2014-12-18 20:48:16 +00:00
|
|
|
this->allocPixels(info);
|
2014-01-25 16:46:20 +00:00
|
|
|
}
|
2014-12-02 17:11:25 +00:00
|
|
|
|
2014-01-24 18:53:42 +00:00
|
|
|
/**
|
|
|
|
* Install a pixelref that wraps the specified pixels and rowBytes, and
|
|
|
|
* optional ReleaseProc and context. When the pixels are no longer
|
2014-05-29 15:57:20 +00:00
|
|
|
* referenced, if releaseProc is not null, it will be called with the
|
2014-01-24 18:53:42 +00:00
|
|
|
* pixels and context as parameters.
|
|
|
|
* On failure, the bitmap will be set to empty and return false.
|
2015-05-30 20:41:15 +00:00
|
|
|
*
|
|
|
|
* If specified, the releaseProc will always be called, even on failure. It is also possible
|
|
|
|
* for success but the releaseProc is immediately called (e.g. valid Info but NULL pixels).
|
2014-01-24 18:53:42 +00:00
|
|
|
*/
|
2014-05-29 15:57:20 +00:00
|
|
|
bool installPixels(const SkImageInfo&, void* pixels, size_t rowBytes, SkColorTable*,
|
|
|
|
void (*releaseProc)(void* addr, void* context), void* context);
|
|
|
|
|
2014-02-17 21:21:46 +00:00
|
|
|
/**
|
|
|
|
* Call installPixels with no ReleaseProc specified. This means that the
|
|
|
|
* caller must ensure that the specified pixels are valid for the lifetime
|
|
|
|
* of the created bitmap (and its pixelRef).
|
|
|
|
*/
|
|
|
|
bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
|
2014-05-29 15:57:20 +00:00
|
|
|
return this->installPixels(info, pixels, rowBytes, NULL, NULL, NULL);
|
2014-02-17 21:21:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls installPixels() with the value in the SkMask. The caller must
|
|
|
|
* ensure that the specified mask pixels are valid for the lifetime
|
|
|
|
* of the created bitmap (and its pixelRef).
|
|
|
|
*/
|
|
|
|
bool installMaskPixels(const SkMask&);
|
2014-01-25 07:01:49 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Use this to assign a new pixel address for an existing bitmap. This
|
|
|
|
will automatically release any pixelref previously installed. Only call
|
|
|
|
this if you are handling ownership/lifetime of the pixel memory.
|
2009-05-03 18:23:30 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
If the bitmap retains a reference to the colortable (assuming it is
|
|
|
|
not null) it will take care of incrementing the reference count.
|
|
|
|
|
|
|
|
@param pixels Address for the pixels, managed by the caller.
|
|
|
|
@param ctable ColorTable (or null) that matches the specified pixels
|
|
|
|
*/
|
|
|
|
void setPixels(void* p, SkColorTable* ctable = NULL);
|
|
|
|
|
2010-11-16 20:22:41 +00:00
|
|
|
/** Copies the bitmap's pixels to the location pointed at by dst and returns
|
|
|
|
true if possible, returns false otherwise.
|
|
|
|
|
2011-11-02 19:57:21 +00:00
|
|
|
In the case when the dstRowBytes matches the bitmap's rowBytes, the copy
|
|
|
|
may be made faster by copying over the dst's per-row padding (for all
|
|
|
|
rows but the last). By setting preserveDstPad to true the caller can
|
|
|
|
disable this optimization and ensure that pixels in the padding are not
|
|
|
|
overwritten.
|
2010-11-16 20:22:41 +00:00
|
|
|
|
|
|
|
Always returns false for RLE formats.
|
|
|
|
|
|
|
|
@param dst Location of destination buffer.
|
|
|
|
@param dstSize Size of destination buffer. Must be large enough to hold
|
|
|
|
pixels using indicated stride.
|
2013-02-25 16:02:36 +00:00
|
|
|
@param dstRowBytes Width of each line in the buffer. If 0, uses
|
2010-11-16 20:22:41 +00:00
|
|
|
bitmap's internal stride.
|
2011-11-02 19:57:21 +00:00
|
|
|
@param preserveDstPad Must we preserve padding in the dst
|
2010-11-16 20:22:41 +00:00
|
|
|
*/
|
2013-02-25 16:02:36 +00:00
|
|
|
bool copyPixelsTo(void* const dst, size_t dstSize, size_t dstRowBytes = 0,
|
|
|
|
bool preserveDstPad = false) const;
|
2010-11-16 20:22:41 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Use the standard HeapAllocator to create the pixelref that manages the
|
2014-06-05 15:51:20 +00:00
|
|
|
pixel memory. It will be sized based on the current ImageInfo.
|
2008-12-17 15:59:43 +00:00
|
|
|
If this is called multiple times, a new pixelref object will be created
|
|
|
|
each time.
|
2009-05-03 18:23:30 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
If the bitmap retains a reference to the colortable (assuming it is
|
|
|
|
not null) it will take care of incrementing the reference count.
|
|
|
|
|
|
|
|
@param ctable ColorTable (or null) to use with the pixels that will
|
2014-06-05 15:51:20 +00:00
|
|
|
be allocated. Only used if colortype == kIndex_8_SkColorType
|
2008-12-17 15:59:43 +00:00
|
|
|
@return true if the allocation succeeds. If not the pixelref field of
|
|
|
|
the bitmap will be unchanged.
|
|
|
|
*/
|
2014-09-02 19:50:45 +00:00
|
|
|
bool SK_WARN_UNUSED_RESULT tryAllocPixels(SkColorTable* ctable = NULL) {
|
|
|
|
return this->tryAllocPixels(NULL, ctable);
|
|
|
|
}
|
|
|
|
|
2014-12-18 20:48:16 +00:00
|
|
|
void allocPixels(SkColorTable* ctable = NULL) {
|
|
|
|
this->allocPixels(NULL, ctable);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2009-05-03 18:23:30 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Use the specified Allocator to create the pixelref that manages the
|
2014-06-05 15:51:20 +00:00
|
|
|
pixel memory. It will be sized based on the current ImageInfo.
|
2008-12-17 15:59:43 +00:00
|
|
|
If this is called multiple times, a new pixelref object will be created
|
|
|
|
each time.
|
2009-05-03 18:23:30 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
If the bitmap retains a reference to the colortable (assuming it is
|
|
|
|
not null) it will take care of incrementing the reference count.
|
2009-05-03 18:23:30 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
@param allocator The Allocator to use to create a pixelref that can
|
2014-06-05 15:51:20 +00:00
|
|
|
manage the pixel memory for the current ImageInfo.
|
|
|
|
If allocator is NULL, the standard HeapAllocator will be used.
|
2008-12-17 15:59:43 +00:00
|
|
|
@param ctable ColorTable (or null) to use with the pixels that will
|
2014-06-05 15:51:20 +00:00
|
|
|
be allocated. Only used if colortype == kIndex_8_SkColorType.
|
|
|
|
If it is non-null and the colortype is not indexed, it will
|
2008-12-17 15:59:43 +00:00
|
|
|
be ignored.
|
|
|
|
@return true if the allocation succeeds. If not the pixelref field of
|
|
|
|
the bitmap will be unchanged.
|
|
|
|
*/
|
2014-09-02 19:50:45 +00:00
|
|
|
bool SK_WARN_UNUSED_RESULT tryAllocPixels(Allocator* allocator, SkColorTable* ctable);
|
|
|
|
|
2014-12-18 20:48:16 +00:00
|
|
|
void allocPixels(Allocator* allocator, SkColorTable* ctable) {
|
2014-09-02 19:50:45 +00:00
|
|
|
if (!this->tryAllocPixels(allocator, ctable)) {
|
2014-12-18 20:48:16 +00:00
|
|
|
sk_throw();
|
2014-09-02 19:50:45 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-03 18:23:30 +00:00
|
|
|
|
2014-01-08 15:42:01 +00:00
|
|
|
/**
|
|
|
|
* Return the current pixelref object or NULL if there is none. This does
|
|
|
|
* not affect the refcount of the pixelref.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPixelRef* pixelRef() const { return fPixelRef; }
|
2014-01-08 15:42:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A bitmap can reference a subset of a pixelref's pixels. That means the
|
|
|
|
* bitmap's width/height can be <= the dimensions of the pixelref. The
|
|
|
|
* pixelref origin is the x,y location within the pixelref's pixels for
|
|
|
|
* the bitmap's top/left corner. To be valid the following must be true:
|
|
|
|
*
|
|
|
|
* origin_x + bitmap_width <= pixelref_width
|
|
|
|
* origin_y + bitmap_height <= pixelref_height
|
|
|
|
*
|
|
|
|
* pixelRefOrigin() returns this origin, or (0,0) if there is no pixelRef.
|
|
|
|
*/
|
|
|
|
SkIPoint pixelRefOrigin() const { return fPixelRefOrigin; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assign a pixelref and origin to the bitmap. Pixelrefs are reference,
|
|
|
|
* so the existing one (if any) will be unref'd and the new one will be
|
|
|
|
* ref'd. (x,y) specify the offset within the pixelref's pixels for the
|
|
|
|
* top/left corner of the bitmap. For a bitmap that encompases the entire
|
|
|
|
* pixels of the pixelref, these will be (0,0).
|
|
|
|
*/
|
|
|
|
SkPixelRef* setPixelRef(SkPixelRef* pr, int dx, int dy);
|
|
|
|
|
|
|
|
SkPixelRef* setPixelRef(SkPixelRef* pr, const SkIPoint& origin) {
|
|
|
|
return this->setPixelRef(pr, origin.fX, origin.fY);
|
|
|
|
}
|
2014-01-09 07:01:42 +00:00
|
|
|
|
2014-01-08 15:42:01 +00:00
|
|
|
SkPixelRef* setPixelRef(SkPixelRef* pr) {
|
|
|
|
return this->setPixelRef(pr, 0, 0);
|
|
|
|
}
|
2009-05-03 18:23:30 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Call this to ensure that the bitmap points to the current pixel address
|
|
|
|
in the pixelref. Balance it with a call to unlockPixels(). These calls
|
|
|
|
are harmless if there is no pixelref.
|
|
|
|
*/
|
|
|
|
void lockPixels() const;
|
|
|
|
/** When you are finished access the pixel memory, call this to balance a
|
|
|
|
previous call to lockPixels(). This allows pixelrefs that implement
|
|
|
|
cached/deferred image decoding to know when there are active clients of
|
|
|
|
a given image.
|
|
|
|
*/
|
|
|
|
void unlockPixels() const;
|
2009-05-03 18:23:30 +00:00
|
|
|
|
2011-07-07 13:42:37 +00:00
|
|
|
/**
|
|
|
|
* Some bitmaps can return a copy of their pixels for lockPixels(), but
|
|
|
|
* that copy, if modified, will not be pushed back. These bitmaps should
|
|
|
|
* not be used as targets for a raster device/canvas (since all pixels
|
|
|
|
* modifications will be lost when unlockPixels() is called.)
|
|
|
|
*/
|
|
|
|
bool lockPixelsAreWritable() const;
|
|
|
|
|
2015-05-22 15:06:21 +00:00
|
|
|
bool requestLock(SkAutoPixmapUnlock* result) const;
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Call this to be sure that the bitmap is valid enough to be drawn (i.e.
|
2014-06-05 15:51:20 +00:00
|
|
|
it has non-null pixels, and if required by its colortype, it has a
|
2008-12-17 15:59:43 +00:00
|
|
|
non-null colortable. Returns true if all of the above are met.
|
|
|
|
*/
|
|
|
|
bool readyToDraw() const {
|
|
|
|
return this->getPixels() != NULL &&
|
2014-09-05 20:34:00 +00:00
|
|
|
(this->colorType() != kIndex_8_SkColorType || fColorTable);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2010-09-09 16:01:26 +00:00
|
|
|
/** Returns the pixelRef's texture, or NULL
|
|
|
|
*/
|
2013-06-26 19:18:23 +00:00
|
|
|
GrTexture* getTexture() const;
|
2010-09-09 16:01:26 +00:00
|
|
|
|
2014-02-11 18:21:45 +00:00
|
|
|
/** Return the bitmap's colortable, if it uses one (i.e. colorType is
|
|
|
|
Index_8) and the pixels are locked.
|
2013-05-06 18:08:05 +00:00
|
|
|
Otherwise returns NULL. Does not affect the colortable's
|
2008-12-17 15:59:43 +00:00
|
|
|
reference count.
|
|
|
|
*/
|
|
|
|
SkColorTable* getColorTable() const { return fColorTable; }
|
|
|
|
|
|
|
|
/** Returns a non-zero, unique value corresponding to the pixels in our
|
2012-07-27 13:41:44 +00:00
|
|
|
pixelref. Each time the pixels are changed (and notifyPixelsChanged
|
|
|
|
is called), a different generation ID will be returned. Finally, if
|
2014-06-05 14:49:04 +00:00
|
|
|
there is no pixelRef then zero is returned.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
uint32_t getGenerationID() const;
|
2009-05-03 18:23:30 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Call this if you have changed the contents of the pixels. This will in-
|
|
|
|
turn cause a different generation ID value to be returned from
|
|
|
|
getGenerationID().
|
|
|
|
*/
|
|
|
|
void notifyPixelsChanged() const;
|
|
|
|
|
2013-06-28 19:40:50 +00:00
|
|
|
/**
|
|
|
|
* Fill the entire bitmap with the specified color.
|
2014-06-05 15:51:20 +00:00
|
|
|
* If the bitmap's colortype does not support alpha (e.g. 565) then the alpha
|
|
|
|
* of the color is ignored (treated as opaque). If the colortype only supports
|
2013-06-28 19:40:50 +00:00
|
|
|
* alpha (e.g. A1 or A8) then the color's r,g,b components are ignored.
|
|
|
|
*/
|
2015-06-08 17:22:22 +00:00
|
|
|
void eraseColor(SkColor c) const;
|
2009-05-03 18:23:30 +00:00
|
|
|
|
2013-06-28 19:40:50 +00:00
|
|
|
/**
|
|
|
|
* Fill the entire bitmap with the specified color.
|
2014-06-05 15:51:20 +00:00
|
|
|
* If the bitmap's colortype does not support alpha (e.g. 565) then the alpha
|
|
|
|
* of the color is ignored (treated as opaque). If the colortype only supports
|
2013-06-28 19:40:50 +00:00
|
|
|
* alpha (e.g. A1 or A8) then the color's r,g,b components are ignored.
|
|
|
|
*/
|
2015-06-08 17:22:22 +00:00
|
|
|
void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
|
|
|
|
this->eraseColor(SkColorSetARGB(a, r, g, b));
|
|
|
|
}
|
2013-06-28 19:40:50 +00:00
|
|
|
|
2013-10-31 17:28:30 +00:00
|
|
|
SK_ATTR_DEPRECATED("use eraseARGB or eraseColor")
|
2013-06-28 19:40:50 +00:00
|
|
|
void eraseRGB(U8CPU r, U8CPU g, U8CPU b) const {
|
|
|
|
this->eraseARGB(0xFF, r, g, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fill the specified area of this bitmap with the specified color.
|
2014-06-05 15:51:20 +00:00
|
|
|
* If the bitmap's colortype does not support alpha (e.g. 565) then the alpha
|
|
|
|
* of the color is ignored (treated as opaque). If the colortype only supports
|
2013-06-28 19:40:50 +00:00
|
|
|
* alpha (e.g. A1 or A8) then the color's r,g,b components are ignored.
|
|
|
|
*/
|
2015-06-08 17:22:22 +00:00
|
|
|
void erase(SkColor c, const SkIRect& area) const;
|
|
|
|
|
|
|
|
// DEPRECATED
|
|
|
|
void eraseArea(const SkIRect& area, SkColor c) const {
|
|
|
|
this->erase(c, area);
|
|
|
|
}
|
2013-06-28 19:40:50 +00:00
|
|
|
|
2011-04-29 11:53:41 +00:00
|
|
|
/**
|
|
|
|
* Return the SkColor of the specified pixel. In most cases this will
|
2014-06-05 15:51:20 +00:00
|
|
|
* require un-premultiplying the color. Alpha only colortypes (e.g. kAlpha_8_SkColorType)
|
2011-04-29 11:53:41 +00:00
|
|
|
* return black with the appropriate alpha set. The value is undefined
|
2014-06-05 15:51:20 +00:00
|
|
|
* for kUnknown_SkColorType or if x or y are out of bounds, or if the bitmap
|
2011-04-29 11:53:41 +00:00
|
|
|
* does not have any pixels (or has not be locked with lockPixels()).
|
|
|
|
*/
|
|
|
|
SkColor getColor(int x, int y) const;
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Returns the address of the specified pixel. This performs a runtime
|
|
|
|
check to know the size of the pixels, and will return the same answer
|
|
|
|
as the corresponding size-specific method (e.g. getAddr16). Since the
|
|
|
|
check happens at runtime, it is much slower than using a size-specific
|
|
|
|
version. Unlike the size-specific methods, this routine also checks if
|
|
|
|
getPixels() returns null, and returns that. The size-specific routines
|
|
|
|
perform a debugging assert that getPixels() is not null, but they do
|
|
|
|
not do any runtime checks.
|
|
|
|
*/
|
|
|
|
void* getAddr(int x, int y) const;
|
|
|
|
|
|
|
|
/** Returns the address of the pixel specified by x,y for 32bit pixels.
|
2011-04-29 11:53:41 +00:00
|
|
|
* In debug build, this asserts that the pixels are allocated and locked,
|
2014-06-05 15:51:20 +00:00
|
|
|
* and that the colortype is 32-bit, however none of these checks are performed
|
2011-04-29 11:53:41 +00:00
|
|
|
* in the release build.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
inline uint32_t* getAddr32(int x, int y) const;
|
2011-04-29 11:53:41 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Returns the address of the pixel specified by x,y for 16bit pixels.
|
2011-04-29 11:53:41 +00:00
|
|
|
* In debug build, this asserts that the pixels are allocated and locked,
|
2014-06-05 15:51:20 +00:00
|
|
|
* and that the colortype is 16-bit, however none of these checks are performed
|
2011-04-29 11:53:41 +00:00
|
|
|
* in the release build.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
inline uint16_t* getAddr16(int x, int y) const;
|
2011-04-29 11:53:41 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Returns the address of the pixel specified by x,y for 8bit pixels.
|
2011-04-29 11:53:41 +00:00
|
|
|
* In debug build, this asserts that the pixels are allocated and locked,
|
2014-06-05 15:51:20 +00:00
|
|
|
* and that the colortype is 8-bit, however none of these checks are performed
|
2011-04-29 11:53:41 +00:00
|
|
|
* in the release build.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
inline uint8_t* getAddr8(int x, int y) const;
|
2011-04-29 11:53:41 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Returns the color corresponding to the pixel specified by x,y for
|
2011-04-29 11:53:41 +00:00
|
|
|
* colortable based bitmaps.
|
|
|
|
* In debug build, this asserts that the pixels are allocated and locked,
|
2014-06-05 15:51:20 +00:00
|
|
|
* that the colortype is indexed, and that the colortable is allocated,
|
2011-04-29 11:53:41 +00:00
|
|
|
* however none of these checks are performed in the release build.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
inline SkPMColor getIndex8Color(int x, int y) const;
|
|
|
|
|
2011-07-01 21:12:20 +00:00
|
|
|
/** Set dst to be a setset of this bitmap. If possible, it will share the
|
2014-06-05 15:51:20 +00:00
|
|
|
pixel memory, and just point into a subset of it. However, if the colortype
|
2008-12-17 15:59:43 +00:00
|
|
|
does not support this, a local copy will be made and associated with
|
|
|
|
the dst bitmap. If the subset rectangle, intersected with the bitmap's
|
2014-06-05 15:51:20 +00:00
|
|
|
dimensions is empty, or if there is an unsupported colortype, false will be
|
2008-12-17 15:59:43 +00:00
|
|
|
returned and dst will be untouched.
|
|
|
|
@param dst The bitmap that will be set to a subset of this bitmap
|
|
|
|
@param subset The rectangle of pixels in this bitmap that dst will
|
|
|
|
reference.
|
|
|
|
@return true if the subset copy was successfully made.
|
|
|
|
*/
|
|
|
|
bool extractSubset(SkBitmap* dst, const SkIRect& subset) const;
|
|
|
|
|
2014-02-23 03:59:35 +00:00
|
|
|
/** Makes a deep copy of this bitmap, respecting the requested colorType,
|
2011-12-02 19:11:17 +00:00
|
|
|
* and allocating the dst pixels on the cpu.
|
|
|
|
* Returns false if either there is an error (i.e. the src does not have
|
|
|
|
* pixels) or the request cannot be satisfied (e.g. the src has per-pixel
|
2014-06-05 15:51:20 +00:00
|
|
|
* alpha, and the requested colortype does not support alpha).
|
2011-12-02 19:11:17 +00:00
|
|
|
* @param dst The bitmap to be sized and allocated
|
2014-02-23 03:59:35 +00:00
|
|
|
* @param ct The desired colorType for dst
|
2011-12-02 19:11:17 +00:00
|
|
|
* @param allocator Allocator used to allocate the pixelref for the dst
|
|
|
|
* bitmap. If this is null, the standard HeapAllocator
|
|
|
|
* will be used.
|
2014-02-23 03:59:35 +00:00
|
|
|
* @return true if the copy was made.
|
2011-12-02 19:11:17 +00:00
|
|
|
*/
|
2014-02-23 03:59:35 +00:00
|
|
|
bool copyTo(SkBitmap* dst, SkColorType ct, Allocator* = NULL) const;
|
|
|
|
|
|
|
|
bool copyTo(SkBitmap* dst, Allocator* allocator = NULL) const {
|
|
|
|
return this->copyTo(dst, this->colorType(), allocator);
|
|
|
|
}
|
|
|
|
|
2014-07-13 11:32:32 +00:00
|
|
|
/**
|
|
|
|
* Copy the bitmap's pixels into the specified buffer (pixels + rowBytes),
|
|
|
|
* converting them into the requested format (SkImageInfo). The src pixels are read
|
|
|
|
* starting at the specified (srcX,srcY) offset, relative to the top-left corner.
|
|
|
|
*
|
|
|
|
* The specified ImageInfo and (srcX,srcY) offset specifies a source rectangle
|
|
|
|
*
|
|
|
|
* srcR.setXYWH(srcX, srcY, dstInfo.width(), dstInfo.height());
|
|
|
|
*
|
|
|
|
* srcR is intersected with the bounds of the bitmap. If this intersection is not empty,
|
|
|
|
* then we have two sets of pixels (of equal size). Replace the dst pixels with the
|
|
|
|
* corresponding src pixels, performing any colortype/alphatype transformations needed
|
|
|
|
* (in the case where the src and dst have different colortypes or alphatypes).
|
|
|
|
*
|
|
|
|
* This call can fail, returning false, for several reasons:
|
|
|
|
* - If srcR does not intersect the bitmap bounds.
|
|
|
|
* - If the requested colortype/alphatype cannot be converted from the src's types.
|
|
|
|
* - If the src pixels are not available.
|
|
|
|
*/
|
|
|
|
bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
|
|
|
|
int srcX, int srcY) const;
|
|
|
|
|
2014-02-23 03:59:35 +00:00
|
|
|
/**
|
|
|
|
* Returns true if this bitmap's pixels can be converted into the requested
|
|
|
|
* colorType, such that copyTo() could succeed.
|
|
|
|
*/
|
|
|
|
bool canCopyTo(SkColorType colorType) const;
|
2009-05-29 21:30:42 +00:00
|
|
|
|
2014-02-20 19:08:07 +00:00
|
|
|
/** Makes a deep copy of this bitmap, keeping the copied pixels
|
|
|
|
* in the same domain as the source: If the src pixels are allocated for
|
|
|
|
* the cpu, then so will the dst. If the src pixels are allocated on the
|
|
|
|
* gpu (typically as a texture), the it will do the same for the dst.
|
|
|
|
* If the request cannot be fulfilled, returns false and dst is unmodified.
|
|
|
|
*/
|
|
|
|
bool deepCopyTo(SkBitmap* dst) const;
|
2011-12-02 19:11:17 +00:00
|
|
|
|
2013-03-08 18:35:13 +00:00
|
|
|
#ifdef SK_BUILD_FOR_ANDROID
|
|
|
|
bool hasHardwareMipMap() const {
|
|
|
|
return (fFlags & kHasHardwareMipMap_Flag) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setHasHardwareMipMap(bool hasHardwareMipMap) {
|
|
|
|
if (hasHardwareMipMap) {
|
|
|
|
fFlags |= kHasHardwareMipMap_Flag;
|
|
|
|
} else {
|
|
|
|
fFlags &= ~kHasHardwareMipMap_Flag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-14 20:30:14 +00:00
|
|
|
bool extractAlpha(SkBitmap* dst) const {
|
|
|
|
return this->extractAlpha(dst, NULL, NULL, NULL);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2011-03-14 20:30:14 +00:00
|
|
|
bool extractAlpha(SkBitmap* dst, const SkPaint* paint,
|
2011-02-23 20:46:31 +00:00
|
|
|
SkIPoint* offset) const {
|
2011-03-14 20:30:14 +00:00
|
|
|
return this->extractAlpha(dst, paint, NULL, offset);
|
2011-02-23 20:46:31 +00:00
|
|
|
}
|
|
|
|
|
2011-03-14 20:30:14 +00:00
|
|
|
/** Set dst to contain alpha layer of this bitmap. If destination bitmap
|
|
|
|
fails to be initialized, e.g. because allocator can't allocate pixels
|
|
|
|
for it, dst will not be modified and false will be returned.
|
|
|
|
|
|
|
|
@param dst The bitmap to be filled with alpha layer
|
|
|
|
@param paint The paint to draw with
|
|
|
|
@param allocator Allocator used to allocate the pixelref for the dst
|
|
|
|
bitmap. If this is null, the standard HeapAllocator
|
|
|
|
will be used.
|
|
|
|
@param offset If not null, it is set to top-left coordinate to position
|
|
|
|
the returned bitmap so that it visually lines up with the
|
|
|
|
original
|
|
|
|
*/
|
|
|
|
bool extractAlpha(SkBitmap* dst, const SkPaint* paint, Allocator* allocator,
|
2008-12-17 15:59:43 +00:00
|
|
|
SkIPoint* offset) const;
|
2009-05-03 18:23:30 +00:00
|
|
|
|
2015-06-05 13:58:22 +00:00
|
|
|
/**
|
|
|
|
* If the pixels are available from this bitmap (w/o locking) return true, and fill out the
|
|
|
|
* specified pixmap (if not null). If the pixels are not available (either because there are
|
|
|
|
* none, or becuase accessing them would require locking or other machinary) return false and
|
|
|
|
* ignore the pixmap parameter.
|
|
|
|
*
|
|
|
|
* Note: if this returns true, the results (in the pixmap) are only valid until the bitmap
|
|
|
|
* is changed in anyway, in which case the results are invalid.
|
|
|
|
*/
|
|
|
|
bool peekPixels(SkPixmap*) const;
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkDEBUGCODE(void validate() const;)
|
|
|
|
|
|
|
|
class Allocator : public SkRefCnt {
|
|
|
|
public:
|
|
|
|
/** Allocate the pixel memory for the bitmap, given its dimensions and
|
2014-06-05 15:51:20 +00:00
|
|
|
colortype. Return true on success, where success means either setPixels
|
2008-12-17 15:59:43 +00:00
|
|
|
or setPixelRef was called. The pixels need not be locked when this
|
2014-06-05 15:51:20 +00:00
|
|
|
returns. If the colortype requires a colortable, it also must be
|
2008-12-17 15:59:43 +00:00
|
|
|
installed via setColorTable. If false is returned, the bitmap and
|
|
|
|
colortable should be left unchanged.
|
|
|
|
*/
|
|
|
|
virtual bool allocPixelRef(SkBitmap*, SkColorTable*) = 0;
|
2012-06-21 20:25:03 +00:00
|
|
|
private:
|
|
|
|
typedef SkRefCnt INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Subclass of Allocator that returns a pixelref that allocates its pixel
|
|
|
|
memory from the heap. This is the default Allocator invoked by
|
|
|
|
allocPixels().
|
|
|
|
*/
|
|
|
|
class HeapAllocator : public Allocator {
|
|
|
|
public:
|
2015-03-26 01:17:31 +00:00
|
|
|
bool allocPixelRef(SkBitmap*, SkColorTable*) override;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class RLEPixels {
|
|
|
|
public:
|
|
|
|
RLEPixels(int width, int height);
|
|
|
|
virtual ~RLEPixels();
|
2009-05-03 18:23:30 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
uint8_t* packedAtY(int y) const {
|
|
|
|
SkASSERT((unsigned)y < (unsigned)fHeight);
|
|
|
|
return fYPtrs[y];
|
|
|
|
}
|
2009-05-03 18:23:30 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// called by subclasses during creation
|
|
|
|
void setPackedAtY(int y, uint8_t* addr) {
|
|
|
|
SkASSERT((unsigned)y < (unsigned)fHeight);
|
|
|
|
fYPtrs[y] = addr;
|
|
|
|
}
|
2009-05-03 18:23:30 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
uint8_t** fYPtrs;
|
|
|
|
int fHeight;
|
|
|
|
};
|
2009-05-03 18:23:30 +00:00
|
|
|
|
2014-03-13 18:02:17 +00:00
|
|
|
SK_TO_STRING_NONVIRT()
|
2013-01-15 20:17:47 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
mutable SkPixelRef* fPixelRef;
|
|
|
|
mutable int fPixelLockCount;
|
2014-01-15 19:59:45 +00:00
|
|
|
// These are just caches from the locked pixelref
|
2008-12-17 15:59:43 +00:00
|
|
|
mutable void* fPixels;
|
|
|
|
mutable SkColorTable* fColorTable; // only meaningful for kIndex8
|
|
|
|
|
2014-01-08 15:42:01 +00:00
|
|
|
SkIPoint fPixelRefOrigin;
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
enum Flags {
|
2011-12-15 20:14:06 +00:00
|
|
|
kImageIsVolatile_Flag = 0x02,
|
2013-03-08 18:35:13 +00:00
|
|
|
#ifdef SK_BUILD_FOR_ANDROID
|
|
|
|
/* A hint for the renderer responsible for drawing this bitmap
|
|
|
|
* indicating that it should attempt to use mipmaps when this bitmap
|
|
|
|
* is drawn scaled down.
|
|
|
|
*/
|
|
|
|
kHasHardwareMipMap_Flag = 0x08,
|
|
|
|
#endif
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
2014-02-11 18:21:45 +00:00
|
|
|
SkImageInfo fInfo;
|
2013-02-25 21:47:41 +00:00
|
|
|
uint32_t fRowBytes;
|
2008-12-17 15:59:43 +00:00
|
|
|
uint8_t fFlags;
|
|
|
|
|
|
|
|
/* Unreference any pixelrefs or colortables
|
|
|
|
*/
|
|
|
|
void freePixels();
|
|
|
|
void updatePixelsFromRef() const;
|
2014-05-24 03:05:26 +00:00
|
|
|
|
2014-05-23 13:21:55 +00:00
|
|
|
static void WriteRawPixels(SkWriteBuffer*, const SkBitmap&);
|
|
|
|
static bool ReadRawPixels(SkReadBuffer*, SkBitmap*);
|
2009-05-03 18:23:30 +00:00
|
|
|
|
2014-05-23 13:21:55 +00:00
|
|
|
friend class SkReadBuffer; // unflatten, rawpixels
|
|
|
|
friend class SkWriteBuffer; // rawpixels
|
2013-06-26 21:28:10 +00:00
|
|
|
friend struct SkBitmapProcState;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
2014-04-07 19:34:38 +00:00
|
|
|
class SkAutoLockPixels : SkNoncopyable {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2011-07-07 13:42:37 +00:00
|
|
|
SkAutoLockPixels(const SkBitmap& bm, bool doLock = true) : fBitmap(bm) {
|
|
|
|
fDidLock = doLock;
|
|
|
|
if (doLock) {
|
|
|
|
bm.lockPixels();
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
~SkAutoLockPixels() {
|
2011-07-07 13:42:37 +00:00
|
|
|
if (fDidLock) {
|
|
|
|
fBitmap.unlockPixels();
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const SkBitmap& fBitmap;
|
2011-07-07 13:42:37 +00:00
|
|
|
bool fDidLock;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
2013-11-18 20:55:29 +00:00
|
|
|
//TODO(mtklein): uncomment when 71713004 lands and Chromium's fixed.
|
|
|
|
//#define SkAutoLockPixels(...) SK_REQUIRE_LOCAL_VAR(SkAutoLockPixels)
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
inline uint32_t* SkBitmap::getAddr32(int x, int y) const {
|
|
|
|
SkASSERT(fPixels);
|
2014-03-14 20:48:05 +00:00
|
|
|
SkASSERT(4 == this->bytesPerPixel());
|
2014-02-11 18:21:45 +00:00
|
|
|
SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height());
|
2008-12-17 15:59:43 +00:00
|
|
|
return (uint32_t*)((char*)fPixels + y * fRowBytes + (x << 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint16_t* SkBitmap::getAddr16(int x, int y) const {
|
|
|
|
SkASSERT(fPixels);
|
2014-03-14 20:48:05 +00:00
|
|
|
SkASSERT(2 == this->bytesPerPixel());
|
2014-02-11 18:21:45 +00:00
|
|
|
SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height());
|
2008-12-17 15:59:43 +00:00
|
|
|
return (uint16_t*)((char*)fPixels + y * fRowBytes + (x << 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint8_t* SkBitmap::getAddr8(int x, int y) const {
|
|
|
|
SkASSERT(fPixels);
|
2014-03-14 20:48:05 +00:00
|
|
|
SkASSERT(1 == this->bytesPerPixel());
|
2014-02-11 18:21:45 +00:00
|
|
|
SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height());
|
2008-12-17 15:59:43 +00:00
|
|
|
return (uint8_t*)fPixels + y * fRowBytes + x;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline SkPMColor SkBitmap::getIndex8Color(int x, int y) const {
|
|
|
|
SkASSERT(fPixels);
|
2014-03-14 20:48:05 +00:00
|
|
|
SkASSERT(kIndex_8_SkColorType == this->colorType());
|
2014-02-11 18:21:45 +00:00
|
|
|
SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)this->height());
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(fColorTable);
|
|
|
|
return (*fColorTable)[*((const uint8_t*)fPixels + y * fRowBytes + x)];
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|