blob: b72a5c59acadcf8a8d134aa2e0b4c9d6eca5c05b [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SkMask_DEFINED
18#define SkMask_DEFINED
19
20#include "SkRect.h"
21
22/** \class SkMask
23 SkMask is used to describe alpha bitmaps, either 1bit, 8bit, or
24 the 3-channel 3D format. These are passed to SkMaskFilter objects.
25*/
26struct SkMask {
27 enum Format {
28 kBW_Format, //!< 1bit per pixel mask (e.g. monochrome)
29 kA8_Format, //!< 8bits per pixel mask (e.g. antialiasing)
30 k3D_Format, //!< 3 8bit per pixl planes: alpha, mul, add
31 kLCD_Format //!< 3 bytes/pixel: r/g/b
32 };
33
34 enum {
35 kCountMaskFormats = kLCD_Format + 1
36 };
37
38 uint8_t* fImage;
39 SkIRect fBounds;
reed@android.com49f0ff22009-03-19 21:52:42 +000040 uint32_t fRowBytes;
41 Format fFormat;
reed@android.com8a1c16f2008-12-17 15:59:43 +000042
reed@android.com543ed932009-04-24 12:43:40 +000043 /** Returns true if the mask is empty: i.e. it has an empty bounds.
44 */
45 bool isEmpty() const { return fBounds.isEmpty(); }
46
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 /** Return the byte size of the mask, assuming only 1 plane.
reed@android.com543ed932009-04-24 12:43:40 +000048 Does not account for k3D_Format. For that, use computeTotalImageSize().
49 If there is an overflow of 32bits, then returns 0.
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 */
51 size_t computeImageSize() const;
reed@android.com49f0ff22009-03-19 21:52:42 +000052
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 /** Return the byte size of the mask, taking into account
54 any extra planes (e.g. k3D_Format).
reed@android.com543ed932009-04-24 12:43:40 +000055 If there is an overflow of 32bits, then returns 0.
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 */
57 size_t computeTotalImageSize() const;
58
59 /** Returns the address of the byte that holds the specified bit.
60 Asserts that the mask is kBW_Format, and that x,y are in range.
61 x,y are in the same coordiate space as fBounds.
62 */
reed@android.com49f0ff22009-03-19 21:52:42 +000063 uint8_t* getAddr1(int x, int y) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 SkASSERT(fFormat == kBW_Format);
65 SkASSERT(fBounds.contains(x, y));
66 SkASSERT(fImage != NULL);
67 return fImage + ((x - fBounds.fLeft) >> 3) + (y - fBounds.fTop) * fRowBytes;
68 }
reed@android.com49f0ff22009-03-19 21:52:42 +000069
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 /** Returns the address of the specified byte.
71 Asserts that the mask is kA8_Format, and that x,y are in range.
72 x,y are in the same coordiate space as fBounds.
73 */
reed@android.com49f0ff22009-03-19 21:52:42 +000074 uint8_t* getAddr(int x, int y) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 SkASSERT(fFormat != kBW_Format);
76 SkASSERT(fBounds.contains(x, y));
77 SkASSERT(fImage != NULL);
78 return fImage + x - fBounds.fLeft + (y - fBounds.fTop) * fRowBytes;
79 }
80
81 static uint8_t* AllocImage(size_t bytes);
82 static void FreeImage(void* image);
83
84 enum CreateMode {
85 kJustComputeBounds_CreateMode, //!< compute bounds and return
86 kJustRenderImage_CreateMode, //!< render into preallocate mask
87 kComputeBoundsAndRenderImage_CreateMode //!< compute bounds, alloc image and render into it
88 };
89};
90
91#endif
92