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