blob: a6d56064787c0083db4aaf4cd14acd9d87ab4526 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#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*/
19struct SkMask {
robertphillipse80eb922015-12-17 11:33:12 -080020 SkMask() : fImage(nullptr) {}
21
reed@android.com8a1c16f2008-12-17 15:59:43 +000022 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.comf2b98d62010-12-20 18:26:13 +000026 kARGB32_Format, //!< SkPMColor
caryclark@google.com1eeaf0b2011-06-22 13:19:43 +000027 kLCD16_Format, //!< 565 alpha for r/g/b
reed@android.com8a1c16f2008-12-17 15:59:43 +000028 };
agl@chromium.org309485b2009-07-21 17:41:32 +000029
reed@android.com8a1c16f2008-12-17 15:59:43 +000030 enum {
reedd54d3fc2014-11-13 14:39:58 -080031 kCountMaskFormats = kLCD16_Format + 1
reed@android.com8a1c16f2008-12-17 15:59:43 +000032 };
33
34 uint8_t* fImage;
35 SkIRect fBounds;
reed@android.com49f0ff22009-03-19 21:52:42 +000036 uint32_t fRowBytes;
37 Format fFormat;
reed@android.com8a1c16f2008-12-17 15:59:43 +000038
reed@android.com543ed932009-04-24 12:43:40 +000039 /** 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.com8a1c16f2008-12-17 15:59:43 +000043 /** Return the byte size of the mask, assuming only 1 plane.
reed@android.com543ed932009-04-24 12:43:40 +000044 Does not account for k3D_Format. For that, use computeTotalImageSize().
45 If there is an overflow of 32bits, then returns 0.
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 */
47 size_t computeImageSize() const;
reed@android.com49f0ff22009-03-19 21:52:42 +000048
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 /** Return the byte size of the mask, taking into account
50 any extra planes (e.g. k3D_Format).
reed@android.com543ed932009-04-24 12:43:40 +000051 If there is an overflow of 32bits, then returns 0.
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 */
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.com49f0ff22009-03-19 21:52:42 +000059 uint8_t* getAddr1(int x, int y) const {
reed@google.comedb606c2011-10-18 13:56:50 +000060 SkASSERT(kBW_Format == fFormat);
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 SkASSERT(fBounds.contains(x, y));
62 SkASSERT(fImage != NULL);
63 return fImage + ((x - fBounds.fLeft) >> 3) + (y - fBounds.fTop) * fRowBytes;
64 }
reed@android.com49f0ff22009-03-19 21:52:42 +000065
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 /** 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.com79891862011-10-18 15:44:57 +000070 uint8_t* getAddr8(int x, int y) const {
reed@google.comedb606c2011-10-18 13:56:50 +000071 SkASSERT(kA8_Format == fFormat);
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 SkASSERT(fBounds.contains(x, y));
73 SkASSERT(fImage != NULL);
74 return fImage + x - fBounds.fLeft + (y - fBounds.fTop) * fRowBytes;
75 }
76
reed@google.comf88d6762011-03-10 15:06:27 +000077 /**
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.com1eeaf0b2011-06-22 13:19:43 +000090 /**
91 * Return the address of the specified 32bit mask. In the debug build,
reed@google.com5bdfb332013-05-02 18:55:44 +000092 * 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 {
reedd54d3fc2014-11-13 14:39:58 -080096 SkASSERT(kARGB32_Format == fFormat);
reed@google.com5bdfb332013-05-02 18:55:44 +000097 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.com2fd42c42013-05-03 07:01:00 +0000102
reed@google.comf52e5552011-10-18 17:29:44 +0000103 /**
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
reedd54d3fc2014-11-13 14:39:58 -0800107 * e.g. getAddr8 or getAddr32.
reed@google.comf52e5552011-10-18 17:29:44 +0000108 *
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.com8a1c16f2008-12-17 15:59:43 +0000117 static uint8_t* AllocImage(size_t bytes);
118 static void FreeImage(void* image);
reed@google.comf88d6762011-03-10 15:06:27 +0000119
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 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.com8a1c16f2008-12-17 15:59:43 +0000125};
126
reed@google.com86b49d22011-10-18 17:35:26 +0000127///////////////////////////////////////////////////////////////////////////////
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 */
135class SkAutoMaskFreeImage {
136public:
137 SkAutoMaskFreeImage(uint8_t* maskImage) {
138 fImage = maskImage;
139 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000140
reed@google.com86b49d22011-10-18 17:35:26 +0000141 ~SkAutoMaskFreeImage() {
142 SkMask::FreeImage(fImage);
143 }
144
145private:
146 uint8_t* fImage;
147};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000148#define SkAutoMaskFreeImage(...) SK_REQUIRE_LOCAL_VAR(SkAutoMaskFreeImage)
reed@google.com86b49d22011-10-18 17:35:26 +0000149
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150#endif