blob: 7be6aff614ce7bdeb3686350f047dde6cbd9e532 [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
reed@android.com8a1c16f2008-12-17 15:59:43 +000026 };
agl@chromium.org309485b2009-07-21 17:41:32 +000027
reed@android.com8a1c16f2008-12-17 15:59:43 +000028 enum {
reedd54d3fc2014-11-13 14:39:58 -080029 kCountMaskFormats = kLCD16_Format + 1
reed@android.com8a1c16f2008-12-17 15:59:43 +000030 };
31
32 uint8_t* fImage;
33 SkIRect fBounds;
reed@android.com49f0ff22009-03-19 21:52:42 +000034 uint32_t fRowBytes;
35 Format fFormat;
reed@android.com8a1c16f2008-12-17 15:59:43 +000036
reed@android.com543ed932009-04-24 12:43:40 +000037 /** 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.com8a1c16f2008-12-17 15:59:43 +000041 /** Return the byte size of the mask, assuming only 1 plane.
reed@android.com543ed932009-04-24 12:43:40 +000042 Does not account for k3D_Format. For that, use computeTotalImageSize().
43 If there is an overflow of 32bits, then returns 0.
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 */
45 size_t computeImageSize() const;
reed@android.com49f0ff22009-03-19 21:52:42 +000046
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 /** Return the byte size of the mask, taking into account
48 any extra planes (e.g. k3D_Format).
reed@android.com543ed932009-04-24 12:43:40 +000049 If there is an overflow of 32bits, then returns 0.
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 */
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.com49f0ff22009-03-19 21:52:42 +000057 uint8_t* getAddr1(int x, int y) const {
reed@google.comedb606c2011-10-18 13:56:50 +000058 SkASSERT(kBW_Format == fFormat);
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 SkASSERT(fBounds.contains(x, y));
60 SkASSERT(fImage != NULL);
61 return fImage + ((x - fBounds.fLeft) >> 3) + (y - fBounds.fTop) * fRowBytes;
62 }
reed@android.com49f0ff22009-03-19 21:52:42 +000063
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 /** 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.com79891862011-10-18 15:44:57 +000068 uint8_t* getAddr8(int x, int y) const {
reed@google.comedb606c2011-10-18 13:56:50 +000069 SkASSERT(kA8_Format == fFormat);
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 SkASSERT(fBounds.contains(x, y));
71 SkASSERT(fImage != NULL);
72 return fImage + x - fBounds.fLeft + (y - fBounds.fTop) * fRowBytes;
73 }
74
reed@google.comf88d6762011-03-10 15:06:27 +000075 /**
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.com1eeaf0b2011-06-22 13:19:43 +000088 /**
89 * Return the address of the specified 32bit mask. In the debug build,
reed@google.com5bdfb332013-05-02 18:55:44 +000090 * 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 {
reedd54d3fc2014-11-13 14:39:58 -080094 SkASSERT(kARGB32_Format == fFormat);
reed@google.com5bdfb332013-05-02 18:55:44 +000095 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.com2fd42c42013-05-03 07:01:00 +0000100
reed@google.comf52e5552011-10-18 17:29:44 +0000101 /**
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
reedd54d3fc2014-11-13 14:39:58 -0800105 * e.g. getAddr8 or getAddr32.
reed@google.comf52e5552011-10-18 17:29:44 +0000106 *
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.com8a1c16f2008-12-17 15:59:43 +0000115 static uint8_t* AllocImage(size_t bytes);
116 static void FreeImage(void* image);
reed@google.comf88d6762011-03-10 15:06:27 +0000117
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 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.com8a1c16f2008-12-17 15:59:43 +0000123};
124
reed@google.com86b49d22011-10-18 17:35:26 +0000125///////////////////////////////////////////////////////////////////////////////
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 */
133class SkAutoMaskFreeImage {
134public:
135 SkAutoMaskFreeImage(uint8_t* maskImage) {
136 fImage = maskImage;
137 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000138
reed@google.com86b49d22011-10-18 17:35:26 +0000139 ~SkAutoMaskFreeImage() {
140 SkMask::FreeImage(fImage);
141 }
142
143private:
144 uint8_t* fImage;
145};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000146#define SkAutoMaskFreeImage(...) SK_REQUIRE_LOCAL_VAR(SkAutoMaskFreeImage)
reed@google.com86b49d22011-10-18 17:35:26 +0000147
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148#endif