2011-07-28 14:26:00 +00:00
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#ifndef SkMask_DEFINED
|
|
|
|
#define SkMask_DEFINED
|
|
|
|
|
|
|
|
#include "SkRect.h"
|
|
|
|
|
|
|
|
/** \class SkMask
|
|
|
|
SkMask is used to describe alpha bitmaps, either 1bit, 8bit, or
|
|
|
|
the 3-channel 3D format. These are passed to SkMaskFilter objects.
|
|
|
|
*/
|
|
|
|
struct SkMask {
|
|
|
|
enum Format {
|
|
|
|
kBW_Format, //!< 1bit per pixel mask (e.g. monochrome)
|
|
|
|
kA8_Format, //!< 8bits per pixel mask (e.g. antialiasing)
|
|
|
|
k3D_Format, //!< 3 8bit per pixl planes: alpha, mul, add
|
2010-12-20 18:26:13 +00:00
|
|
|
kARGB32_Format, //!< SkPMColor
|
2011-06-22 13:19:43 +00:00
|
|
|
kLCD16_Format, //!< 565 alpha for r/g/b
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
2009-07-21 17:41:32 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
enum {
|
2014-11-13 22:39:58 +00:00
|
|
|
kCountMaskFormats = kLCD16_Format + 1
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
uint8_t* fImage;
|
|
|
|
SkIRect fBounds;
|
2009-03-19 21:52:42 +00:00
|
|
|
uint32_t fRowBytes;
|
|
|
|
Format fFormat;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2009-04-24 12:43:40 +00:00
|
|
|
/** Returns true if the mask is empty: i.e. it has an empty bounds.
|
|
|
|
*/
|
|
|
|
bool isEmpty() const { return fBounds.isEmpty(); }
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Return the byte size of the mask, assuming only 1 plane.
|
2009-04-24 12:43:40 +00:00
|
|
|
Does not account for k3D_Format. For that, use computeTotalImageSize().
|
|
|
|
If there is an overflow of 32bits, then returns 0.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
size_t computeImageSize() const;
|
2009-03-19 21:52:42 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Return the byte size of the mask, taking into account
|
|
|
|
any extra planes (e.g. k3D_Format).
|
2009-04-24 12:43:40 +00:00
|
|
|
If there is an overflow of 32bits, then returns 0.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
size_t computeTotalImageSize() const;
|
|
|
|
|
|
|
|
/** Returns the address of the byte that holds the specified bit.
|
|
|
|
Asserts that the mask is kBW_Format, and that x,y are in range.
|
|
|
|
x,y are in the same coordiate space as fBounds.
|
|
|
|
*/
|
2009-03-19 21:52:42 +00:00
|
|
|
uint8_t* getAddr1(int x, int y) const {
|
2011-10-18 13:56:50 +00:00
|
|
|
SkASSERT(kBW_Format == fFormat);
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(fBounds.contains(x, y));
|
|
|
|
SkASSERT(fImage != NULL);
|
|
|
|
return fImage + ((x - fBounds.fLeft) >> 3) + (y - fBounds.fTop) * fRowBytes;
|
|
|
|
}
|
2009-03-19 21:52:42 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Returns the address of the specified byte.
|
|
|
|
Asserts that the mask is kA8_Format, and that x,y are in range.
|
|
|
|
x,y are in the same coordiate space as fBounds.
|
|
|
|
*/
|
2011-10-18 15:44:57 +00:00
|
|
|
uint8_t* getAddr8(int x, int y) const {
|
2011-10-18 13:56:50 +00:00
|
|
|
SkASSERT(kA8_Format == fFormat);
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(fBounds.contains(x, y));
|
|
|
|
SkASSERT(fImage != NULL);
|
|
|
|
return fImage + x - fBounds.fLeft + (y - fBounds.fTop) * fRowBytes;
|
|
|
|
}
|
|
|
|
|
2011-03-10 15:06:27 +00:00
|
|
|
/**
|
|
|
|
* Return the address of the specified 16bit mask. In the debug build,
|
|
|
|
* this asserts that the mask's format is kLCD16_Format, and that (x,y)
|
|
|
|
* are contained in the mask's fBounds.
|
|
|
|
*/
|
|
|
|
uint16_t* getAddrLCD16(int x, int y) const {
|
|
|
|
SkASSERT(kLCD16_Format == fFormat);
|
|
|
|
SkASSERT(fBounds.contains(x, y));
|
|
|
|
SkASSERT(fImage != NULL);
|
|
|
|
uint16_t* row = (uint16_t*)(fImage + (y - fBounds.fTop) * fRowBytes);
|
|
|
|
return row + (x - fBounds.fLeft);
|
|
|
|
}
|
|
|
|
|
2013-05-02 18:55:44 +00:00
|
|
|
/**
|
|
|
|
* Return the address of the specified 32bit mask. In the debug build,
|
|
|
|
* this asserts that the mask's format is 32bits, and that (x,y)
|
|
|
|
* are contained in the mask's fBounds.
|
|
|
|
*/
|
|
|
|
uint32_t* getAddr32(int x, int y) const {
|
2014-11-13 22:39:58 +00:00
|
|
|
SkASSERT(kARGB32_Format == fFormat);
|
2013-05-02 18:55:44 +00:00
|
|
|
SkASSERT(fBounds.contains(x, y));
|
|
|
|
SkASSERT(fImage != NULL);
|
|
|
|
uint32_t* row = (uint32_t*)(fImage + (y - fBounds.fTop) * fRowBytes);
|
|
|
|
return row + (x - fBounds.fLeft);
|
|
|
|
}
|
2013-05-03 07:01:00 +00:00
|
|
|
|
2011-10-18 17:29:44 +00:00
|
|
|
/**
|
|
|
|
* Returns the address of the specified pixel, computing the pixel-size
|
|
|
|
* at runtime based on the mask format. This will be slightly slower than
|
|
|
|
* using one of the routines where the format is implied by the name
|
2014-11-13 22:39:58 +00:00
|
|
|
* e.g. getAddr8 or getAddr32.
|
2011-10-18 17:29:44 +00:00
|
|
|
*
|
|
|
|
* x,y must be contained by the mask's bounds (this is asserted in the
|
|
|
|
* debug build, but not checked in the release build.)
|
|
|
|
*
|
|
|
|
* This should not be called with kBW_Format, as it will give unspecified
|
|
|
|
* results (and assert in the debug build).
|
|
|
|
*/
|
|
|
|
void* getAddr(int x, int y) const;
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
static uint8_t* AllocImage(size_t bytes);
|
|
|
|
static void FreeImage(void* image);
|
2011-03-10 15:06:27 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
enum CreateMode {
|
|
|
|
kJustComputeBounds_CreateMode, //!< compute bounds and return
|
|
|
|
kJustRenderImage_CreateMode, //!< render into preallocate mask
|
|
|
|
kComputeBoundsAndRenderImage_CreateMode //!< compute bounds, alloc image and render into it
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2011-10-18 17:35:26 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \class SkAutoMaskImage
|
|
|
|
*
|
|
|
|
* Stack class used to manage the fImage buffer in a SkMask.
|
|
|
|
* When this object loses scope, the buffer is freed with SkMask::FreeImage().
|
|
|
|
*/
|
|
|
|
class SkAutoMaskFreeImage {
|
|
|
|
public:
|
|
|
|
SkAutoMaskFreeImage(uint8_t* maskImage) {
|
|
|
|
fImage = maskImage;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-10-18 17:35:26 +00:00
|
|
|
~SkAutoMaskFreeImage() {
|
|
|
|
SkMask::FreeImage(fImage);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint8_t* fImage;
|
|
|
|
};
|
2013-11-18 16:03:59 +00:00
|
|
|
#define SkAutoMaskFreeImage(...) SK_REQUIRE_LOCAL_VAR(SkAutoMaskFreeImage)
|
2011-10-18 17:35:26 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#endif
|