blob: 8e3fd2b66eda2798e11d356248dfce4ffa8ef72a [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 {
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.comf2b98d62010-12-20 18:26:13 +000024 kARGB32_Format, //!< SkPMColor
caryclark@google.com1eeaf0b2011-06-22 13:19:43 +000025 kLCD16_Format, //!< 565 alpha for r/g/b
26 kLCD32_Format //!< 888 alpha for r/g/b
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 };
agl@chromium.org309485b2009-07-21 17:41:32 +000028
reed@android.com8a1c16f2008-12-17 15:59:43 +000029 enum {
reed@google.comeffc5012011-06-27 16:44:46 +000030 kCountMaskFormats = kLCD32_Format + 1
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 };
32
33 uint8_t* fImage;
34 SkIRect fBounds;
reed@android.com49f0ff22009-03-19 21:52:42 +000035 uint32_t fRowBytes;
36 Format fFormat;
reed@android.com8a1c16f2008-12-17 15:59:43 +000037
reed@android.com543ed932009-04-24 12:43:40 +000038 /** Returns true if the mask is empty: i.e. it has an empty bounds.
39 */
40 bool isEmpty() const { return fBounds.isEmpty(); }
41
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 /** Return the byte size of the mask, assuming only 1 plane.
reed@android.com543ed932009-04-24 12:43:40 +000043 Does not account for k3D_Format. For that, use computeTotalImageSize().
44 If there is an overflow of 32bits, then returns 0.
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 */
46 size_t computeImageSize() const;
reed@android.com49f0ff22009-03-19 21:52:42 +000047
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 /** Return the byte size of the mask, taking into account
49 any extra planes (e.g. k3D_Format).
reed@android.com543ed932009-04-24 12:43:40 +000050 If there is an overflow of 32bits, then returns 0.
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 */
52 size_t computeTotalImageSize() const;
53
54 /** Returns the address of the byte that holds the specified bit.
55 Asserts that the mask is kBW_Format, and that x,y are in range.
56 x,y are in the same coordiate space as fBounds.
57 */
reed@android.com49f0ff22009-03-19 21:52:42 +000058 uint8_t* getAddr1(int x, int y) const {
reed@google.comedb606c2011-10-18 13:56:50 +000059 SkASSERT(kBW_Format == fFormat);
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 SkASSERT(fBounds.contains(x, y));
61 SkASSERT(fImage != NULL);
62 return fImage + ((x - fBounds.fLeft) >> 3) + (y - fBounds.fTop) * fRowBytes;
63 }
reed@android.com49f0ff22009-03-19 21:52:42 +000064
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 /** Returns the address of the specified byte.
66 Asserts that the mask is kA8_Format, and that x,y are in range.
67 x,y are in the same coordiate space as fBounds.
68 */
reed@google.com79891862011-10-18 15:44:57 +000069 uint8_t* getAddr8(int x, int y) const {
reed@google.comedb606c2011-10-18 13:56:50 +000070 SkASSERT(kA8_Format == fFormat);
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 SkASSERT(fBounds.contains(x, y));
72 SkASSERT(fImage != NULL);
73 return fImage + x - fBounds.fLeft + (y - fBounds.fTop) * fRowBytes;
74 }
75
reed@google.comf88d6762011-03-10 15:06:27 +000076 /**
77 * Return the address of the specified 16bit mask. In the debug build,
78 * this asserts that the mask's format is kLCD16_Format, and that (x,y)
79 * are contained in the mask's fBounds.
80 */
81 uint16_t* getAddrLCD16(int x, int y) const {
82 SkASSERT(kLCD16_Format == fFormat);
83 SkASSERT(fBounds.contains(x, y));
84 SkASSERT(fImage != NULL);
85 uint16_t* row = (uint16_t*)(fImage + (y - fBounds.fTop) * fRowBytes);
86 return row + (x - fBounds.fLeft);
87 }
88
caryclark@google.com1eeaf0b2011-06-22 13:19:43 +000089 /**
90 * Return the address of the specified 32bit mask. In the debug build,
91 * this asserts that the mask's format is kLCD32_Format, and that (x,y)
92 * are contained in the mask's fBounds.
93 */
94 uint32_t* getAddrLCD32(int x, int y) const {
95 SkASSERT(kLCD32_Format == fFormat);
96 SkASSERT(fBounds.contains(x, y));
97 SkASSERT(fImage != NULL);
98 uint32_t* row = (uint32_t*)(fImage + (y - fBounds.fTop) * fRowBytes);
99 return row + (x - fBounds.fLeft);
100 }
101
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 static uint8_t* AllocImage(size_t bytes);
103 static void FreeImage(void* image);
reed@google.comf88d6762011-03-10 15:06:27 +0000104
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 enum CreateMode {
106 kJustComputeBounds_CreateMode, //!< compute bounds and return
107 kJustRenderImage_CreateMode, //!< render into preallocate mask
108 kComputeBoundsAndRenderImage_CreateMode //!< compute bounds, alloc image and render into it
109 };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110};
111
112#endif
113