epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkMask_DEFINED |
| 11 | #define SkMask_DEFINED |
| 12 | |
| 13 | #include "SkRect.h" |
| 14 | |
| 15 | /** \class SkMask |
| 16 | SkMask is used to describe alpha bitmaps, either 1bit, 8bit, or |
| 17 | the 3-channel 3D format. These are passed to SkMaskFilter objects. |
| 18 | */ |
| 19 | struct SkMask { |
| 20 | enum Format { |
| 21 | kBW_Format, //!< 1bit per pixel mask (e.g. monochrome) |
| 22 | kA8_Format, //!< 8bits per pixel mask (e.g. antialiasing) |
| 23 | k3D_Format, //!< 3 8bit per pixl planes: alpha, mul, add |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 24 | kARGB32_Format, //!< SkPMColor |
caryclark@google.com | 1eeaf0b | 2011-06-22 13:19:43 +0000 | [diff] [blame] | 25 | kLCD16_Format, //!< 565 alpha for r/g/b |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | }; |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 27 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | enum { |
reed | d54d3fc | 2014-11-13 14:39:58 -0800 | [diff] [blame] | 29 | kCountMaskFormats = kLCD16_Format + 1 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | uint8_t* fImage; |
| 33 | SkIRect fBounds; |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 34 | uint32_t fRowBytes; |
| 35 | Format fFormat; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | |
reed@android.com | 543ed93 | 2009-04-24 12:43:40 +0000 | [diff] [blame] | 37 | /** Returns true if the mask is empty: i.e. it has an empty bounds. |
| 38 | */ |
| 39 | bool isEmpty() const { return fBounds.isEmpty(); } |
| 40 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | /** Return the byte size of the mask, assuming only 1 plane. |
reed@android.com | 543ed93 | 2009-04-24 12:43:40 +0000 | [diff] [blame] | 42 | Does not account for k3D_Format. For that, use computeTotalImageSize(). |
| 43 | If there is an overflow of 32bits, then returns 0. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | */ |
| 45 | size_t computeImageSize() const; |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 46 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | /** Return the byte size of the mask, taking into account |
| 48 | any extra planes (e.g. k3D_Format). |
reed@android.com | 543ed93 | 2009-04-24 12:43:40 +0000 | [diff] [blame] | 49 | If there is an overflow of 32bits, then returns 0. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | */ |
| 51 | size_t computeTotalImageSize() const; |
| 52 | |
| 53 | /** Returns the address of the byte that holds the specified bit. |
| 54 | Asserts that the mask is kBW_Format, and that x,y are in range. |
| 55 | x,y are in the same coordiate space as fBounds. |
| 56 | */ |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 57 | uint8_t* getAddr1(int x, int y) const { |
reed@google.com | edb606c | 2011-10-18 13:56:50 +0000 | [diff] [blame] | 58 | SkASSERT(kBW_Format == fFormat); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | SkASSERT(fBounds.contains(x, y)); |
| 60 | SkASSERT(fImage != NULL); |
| 61 | return fImage + ((x - fBounds.fLeft) >> 3) + (y - fBounds.fTop) * fRowBytes; |
| 62 | } |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 63 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | /** Returns the address of the specified byte. |
| 65 | Asserts that the mask is kA8_Format, and that x,y are in range. |
| 66 | x,y are in the same coordiate space as fBounds. |
| 67 | */ |
reed@google.com | 7989186 | 2011-10-18 15:44:57 +0000 | [diff] [blame] | 68 | uint8_t* getAddr8(int x, int y) const { |
reed@google.com | edb606c | 2011-10-18 13:56:50 +0000 | [diff] [blame] | 69 | SkASSERT(kA8_Format == fFormat); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | SkASSERT(fBounds.contains(x, y)); |
| 71 | SkASSERT(fImage != NULL); |
| 72 | return fImage + x - fBounds.fLeft + (y - fBounds.fTop) * fRowBytes; |
| 73 | } |
| 74 | |
reed@google.com | f88d676 | 2011-03-10 15:06:27 +0000 | [diff] [blame] | 75 | /** |
| 76 | * Return the address of the specified 16bit mask. In the debug build, |
| 77 | * this asserts that the mask's format is kLCD16_Format, and that (x,y) |
| 78 | * are contained in the mask's fBounds. |
| 79 | */ |
| 80 | uint16_t* getAddrLCD16(int x, int y) const { |
| 81 | SkASSERT(kLCD16_Format == fFormat); |
| 82 | SkASSERT(fBounds.contains(x, y)); |
| 83 | SkASSERT(fImage != NULL); |
| 84 | uint16_t* row = (uint16_t*)(fImage + (y - fBounds.fTop) * fRowBytes); |
| 85 | return row + (x - fBounds.fLeft); |
| 86 | } |
| 87 | |
caryclark@google.com | 1eeaf0b | 2011-06-22 13:19:43 +0000 | [diff] [blame] | 88 | /** |
| 89 | * Return the address of the specified 32bit mask. In the debug build, |
reed@google.com | 5bdfb33 | 2013-05-02 18:55:44 +0000 | [diff] [blame] | 90 | * this asserts that the mask's format is 32bits, and that (x,y) |
| 91 | * are contained in the mask's fBounds. |
| 92 | */ |
| 93 | uint32_t* getAddr32(int x, int y) const { |
reed | d54d3fc | 2014-11-13 14:39:58 -0800 | [diff] [blame] | 94 | SkASSERT(kARGB32_Format == fFormat); |
reed@google.com | 5bdfb33 | 2013-05-02 18:55:44 +0000 | [diff] [blame] | 95 | SkASSERT(fBounds.contains(x, y)); |
| 96 | SkASSERT(fImage != NULL); |
| 97 | uint32_t* row = (uint32_t*)(fImage + (y - fBounds.fTop) * fRowBytes); |
| 98 | return row + (x - fBounds.fLeft); |
| 99 | } |
skia.committer@gmail.com | 2fd42c4 | 2013-05-03 07:01:00 +0000 | [diff] [blame] | 100 | |
reed@google.com | f52e555 | 2011-10-18 17:29:44 +0000 | [diff] [blame] | 101 | /** |
| 102 | * Returns the address of the specified pixel, computing the pixel-size |
| 103 | * at runtime based on the mask format. This will be slightly slower than |
| 104 | * using one of the routines where the format is implied by the name |
reed | d54d3fc | 2014-11-13 14:39:58 -0800 | [diff] [blame] | 105 | * e.g. getAddr8 or getAddr32. |
reed@google.com | f52e555 | 2011-10-18 17:29:44 +0000 | [diff] [blame] | 106 | * |
| 107 | * x,y must be contained by the mask's bounds (this is asserted in the |
| 108 | * debug build, but not checked in the release build.) |
| 109 | * |
| 110 | * This should not be called with kBW_Format, as it will give unspecified |
| 111 | * results (and assert in the debug build). |
| 112 | */ |
| 113 | void* getAddr(int x, int y) const; |
| 114 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | static uint8_t* AllocImage(size_t bytes); |
| 116 | static void FreeImage(void* image); |
reed@google.com | f88d676 | 2011-03-10 15:06:27 +0000 | [diff] [blame] | 117 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | enum CreateMode { |
| 119 | kJustComputeBounds_CreateMode, //!< compute bounds and return |
| 120 | kJustRenderImage_CreateMode, //!< render into preallocate mask |
| 121 | kComputeBoundsAndRenderImage_CreateMode //!< compute bounds, alloc image and render into it |
| 122 | }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
reed@google.com | 86b49d2 | 2011-10-18 17:35:26 +0000 | [diff] [blame] | 125 | /////////////////////////////////////////////////////////////////////////////// |
| 126 | |
| 127 | /** |
| 128 | * \class SkAutoMaskImage |
| 129 | * |
| 130 | * Stack class used to manage the fImage buffer in a SkMask. |
| 131 | * When this object loses scope, the buffer is freed with SkMask::FreeImage(). |
| 132 | */ |
| 133 | class SkAutoMaskFreeImage { |
| 134 | public: |
| 135 | SkAutoMaskFreeImage(uint8_t* maskImage) { |
| 136 | fImage = maskImage; |
| 137 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 138 | |
reed@google.com | 86b49d2 | 2011-10-18 17:35:26 +0000 | [diff] [blame] | 139 | ~SkAutoMaskFreeImage() { |
| 140 | SkMask::FreeImage(fImage); |
| 141 | } |
| 142 | |
| 143 | private: |
| 144 | uint8_t* fImage; |
| 145 | }; |
commit-bot@chromium.org | e61a86c | 2013-11-18 16:03:59 +0000 | [diff] [blame] | 146 | #define SkAutoMaskFreeImage(...) SK_REQUIRE_LOCAL_VAR(SkAutoMaskFreeImage) |
reed@google.com | 86b49d2 | 2011-10-18 17:35:26 +0000 | [diff] [blame] | 147 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | #endif |