1 /* 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 */ 26 struct 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 32 /* The LCD formats look like this in memory: 33 34 First, there's an A8 plane which contains the average alpha value for 35 each pixel. Because of this, the LCD formats can be passed directly 36 to functions which expect an A8 and everything will just work. 37 38 After that in memory, there's a bitmap of 32-bit values which have 39 been RGB order corrected for the current screen (based on the 40 settings in SkFontHost at the time of renderering). The alpha value 41 for each pixel is the maximum of the three alpha values. 42 43 kHorizontalLCD_Format has an extra column of pixels on the left and right 44 edges. kVerticalLCD_Format has an extra row at the top and bottom. 45 */ 46 47 kHorizontalLCD_Format, //!< 4 bytes/pixel: a/r/g/b 48 kVerticalLCD_Format, //!< 4 bytes/pixel: a/r/g/b 49 }; 50 51 enum { 52 kCountMaskFormats = kVerticalLCD_Format + 1 53 }; 54 55 uint8_t* fImage; 56 SkIRect fBounds; 57 uint32_t fRowBytes; 58 Format fFormat; 59 60 /** Returns true if the mask is empty: i.e. it has an empty bounds. 61 */ isEmptySkMask62 bool isEmpty() const { return fBounds.isEmpty(); } 63 64 /** Return the byte size of the mask, assuming only 1 plane. 65 Does not account for k3D_Format. For that, use computeTotalImageSize(). 66 If there is an overflow of 32bits, then returns 0. 67 */ 68 size_t computeImageSize() const; 69 70 /** Return the byte size of the mask, taking into account 71 any extra planes (e.g. k3D_Format). 72 If there is an overflow of 32bits, then returns 0. 73 */ 74 size_t computeTotalImageSize() const; 75 76 /** Returns the address of the byte that holds the specified bit. 77 Asserts that the mask is kBW_Format, and that x,y are in range. 78 x,y are in the same coordiate space as fBounds. 79 */ getAddr1SkMask80 uint8_t* getAddr1(int x, int y) const { 81 SkASSERT(fFormat == kBW_Format); 82 SkASSERT(fBounds.contains(x, y)); 83 SkASSERT(fImage != NULL); 84 return fImage + ((x - fBounds.fLeft) >> 3) + (y - fBounds.fTop) * fRowBytes; 85 } 86 87 /** Returns the address of the specified byte. 88 Asserts that the mask is kA8_Format, and that x,y are in range. 89 x,y are in the same coordiate space as fBounds. 90 */ getAddrSkMask91 uint8_t* getAddr(int x, int y) const { 92 SkASSERT(fFormat != kBW_Format); 93 SkASSERT(fBounds.contains(x, y)); 94 SkASSERT(fImage != NULL); 95 return fImage + x - fBounds.fLeft + (y - fBounds.fTop) * fRowBytes; 96 } 97 98 /** Return an address into the 32-bit plane of an LCD or VerticalLCD mask 99 for the given position. 100 */ getAddrLCDSkMask101 const uint32_t* getAddrLCD(int x, int y) const { 102 SkASSERT(fFormat == kHorizontalLCD_Format || fFormat == kVerticalLCD_Format); 103 SkASSERT(fImage != NULL); 104 105 return reinterpret_cast<const uint32_t*>(fImage + SkAlign4(fRowBytes * fBounds.height())) + 106 x - fBounds.fLeft + (y - fBounds.fTop) * rowWordsLCD(); 107 } 108 109 /** Return the number of 32-bit words in a row of the 32-bit plane of an 110 LCD or VerticalLCD mask. 111 */ rowWordsLCDSkMask112 unsigned rowWordsLCD() const { 113 SkASSERT(fFormat == kHorizontalLCD_Format || fFormat == kVerticalLCD_Format); 114 if (fFormat == kHorizontalLCD_Format) 115 return fBounds.width() + 2; 116 else 117 return fBounds.width(); 118 } 119 120 static uint8_t* AllocImage(size_t bytes); 121 static void FreeImage(void* image); 122 123 enum CreateMode { 124 kJustComputeBounds_CreateMode, //!< compute bounds and return 125 kJustRenderImage_CreateMode, //!< render into preallocate mask 126 kComputeBoundsAndRenderImage_CreateMode //!< compute bounds, alloc image and render into it 127 }; 128 FormatIsLCDSkMask129 static bool FormatIsLCD(Format fm) { 130 return kHorizontalLCD_Format == fm || kVerticalLCD_Format == fm; 131 } 132 }; 133 134 #endif 135 136