OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkBitmap_DEFINED | 8 #ifndef SkBitmap_DEFINED |
9 #define SkBitmap_DEFINED | 9 #define SkBitmap_DEFINED |
10 | 10 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 | 72 |
73 /** Copies the src bitmap into this bitmap. Ownership of the src bitmap's pi
xels remains | 73 /** Copies the src bitmap into this bitmap. Ownership of the src bitmap's pi
xels remains |
74 with the src bitmap. | 74 with the src bitmap. |
75 */ | 75 */ |
76 SkBitmap& operator=(const SkBitmap& src); | 76 SkBitmap& operator=(const SkBitmap& src); |
77 /** Swap the fields of the two bitmaps. This routine is guaranteed to never
fail or throw. | 77 /** Swap the fields of the two bitmaps. This routine is guaranteed to never
fail or throw. |
78 */ | 78 */ |
79 // This method is not exported to java. | 79 // This method is not exported to java. |
80 void swap(SkBitmap& other); | 80 void swap(SkBitmap& other); |
81 | 81 |
82 /////////////////////////////////////////////////////////////////////////// | |
83 | |
84 const SkImageInfo& info() const { return fInfo; } | |
85 | |
86 int width() const { return fInfo.fWidth; } | |
87 int height() const { return fInfo.fHeight; } | |
88 SkColorType colorType() const { return fInfo.fColorType; } | |
89 SkAlphaType alphaType() const { return fInfo.fAlphaType; } | |
90 | |
91 /** Return the number of bytes per pixel based on the config. If the config | |
92 does not have at least 1 byte per (e.g. kA1_Config) then 0 is returned. | |
93 */ | |
94 int bytesPerPixel() const { return fInfo.bytesPerPixel(); } | |
95 | |
96 /** Return the rowbytes expressed as a number of pixels (like width and | |
97 height). Note, for 1-byte per pixel configs like kA8_Config, this will | |
98 return the same as rowBytes(). Is undefined for configs that are less | |
99 than 1-byte per pixel (e.g. kA1_Config) | |
100 */ | |
101 int rowBytesAsPixels() const { | |
102 return fRowBytes >> this->shiftPerPixel(); | |
103 } | |
104 | |
105 /** Return the shift amount per pixel (i.e. 0 for 1-byte per pixel, 1 for | |
106 2-bytes per pixel configs, 2 for 4-bytes per pixel configs). Return 0 | |
107 for configs that are not at least 1-byte per pixel (e.g. kA1_Config | |
108 or kNo_Config) | |
109 */ | |
110 int shiftPerPixel() const { return this->bytesPerPixel() >> 1; } | |
111 | |
112 /////////////////////////////////////////////////////////////////////////// | |
113 | |
114 /** Return true iff the bitmap has empty dimensions. | 82 /** Return true iff the bitmap has empty dimensions. |
115 * Hey! Before you use this, see if you really want to know drawsNothing()
instead. | 83 * Hey! Before you use this, see if you really want to know drawsNothing()
instead. |
116 */ | 84 */ |
117 bool empty() const { return fInfo.isEmpty(); } | 85 bool empty() const { return 0 == fWidth || 0 == fHeight; } |
118 | 86 |
119 /** Return true iff the bitmap has no pixelref. Note: this can return true e
ven if the | 87 /** Return true iff the bitmap has no pixelref. Note: this can return true e
ven if the |
120 * dimensions of the bitmap are > 0 (see empty()). | 88 * dimensions of the bitmap are > 0 (see empty()). |
121 * Hey! Before you use this, see if you really want to know drawsNothing()
instead. | 89 * Hey! Before you use this, see if you really want to know drawsNothing()
instead. |
122 */ | 90 */ |
123 bool isNull() const { return NULL == fPixelRef; } | 91 bool isNull() const { return NULL == fPixelRef; } |
124 | 92 |
125 /** Return true iff drawing this bitmap has no effect. | 93 /** Return true iff drawing this bitmap has no effect. |
126 */ | 94 */ |
127 bool drawsNothing() const { return this->empty() || this->isNull(); } | 95 bool drawsNothing() const { return this->empty() || this->isNull(); } |
128 | 96 |
129 /** Return the config for the bitmap. */ | 97 /** Return the config for the bitmap. */ |
130 Config config() const; | 98 Config config() const { return (Config)fConfig; } |
131 | 99 |
132 SK_ATTR_DEPRECATED("use config()") | 100 SK_ATTR_DEPRECATED("use config()") |
133 Config getConfig() const { return this->config(); } | 101 Config getConfig() const { return this->config(); } |
134 | 102 |
| 103 /** Return the bitmap's width, in pixels. */ |
| 104 int width() const { return fWidth; } |
| 105 |
| 106 /** Return the bitmap's height, in pixels. */ |
| 107 int height() const { return fHeight; } |
| 108 |
135 /** Return the number of bytes between subsequent rows of the bitmap. */ | 109 /** Return the number of bytes between subsequent rows of the bitmap. */ |
136 size_t rowBytes() const { return fRowBytes; } | 110 size_t rowBytes() const { return fRowBytes; } |
137 | 111 |
| 112 /** Return the shift amount per pixel (i.e. 0 for 1-byte per pixel, 1 for |
| 113 2-bytes per pixel configs, 2 for 4-bytes per pixel configs). Return 0 |
| 114 for configs that are not at least 1-byte per pixel (e.g. kA1_Config |
| 115 or kNo_Config) |
| 116 */ |
| 117 int shiftPerPixel() const { return fBytesPerPixel >> 1; } |
| 118 |
| 119 /** Return the number of bytes per pixel based on the config. If the config |
| 120 does not have at least 1 byte per (e.g. kA1_Config) then 0 is returned. |
| 121 */ |
| 122 int bytesPerPixel() const { return fBytesPerPixel; } |
| 123 |
| 124 /** Return the rowbytes expressed as a number of pixels (like width and |
| 125 height). Note, for 1-byte per pixel configs like kA8_Config, this will |
| 126 return the same as rowBytes(). Is undefined for configs that are less |
| 127 than 1-byte per pixel (e.g. kA1_Config) |
| 128 */ |
| 129 int rowBytesAsPixels() const { return fRowBytes >> (fBytesPerPixel >> 1); } |
| 130 |
| 131 SkAlphaType alphaType() const { return (SkAlphaType)fAlphaType; } |
| 132 |
138 /** | 133 /** |
139 * Set the bitmap's alphaType, returning true on success. If false is | 134 * Set the bitmap's alphaType, returning true on success. If false is |
140 * returned, then the specified new alphaType is incompatible with the | 135 * returned, then the specified new alphaType is incompatible with the |
141 * Config, and the current alphaType is unchanged. | 136 * Config, and the current alphaType is unchanged. |
142 * | 137 * |
143 * Note: this changes the alphatype for the underlying pixels, which means | 138 * Note: this changes the alphatype for the underlying pixels, which means |
144 * that all bitmaps that might be sharing (subsets of) the pixels will | 139 * that all bitmaps that might be sharing (subsets of) the pixels will |
145 * be affected. | 140 * be affected. |
146 */ | 141 */ |
147 bool setAlphaType(SkAlphaType); | 142 bool setAlphaType(SkAlphaType); |
148 | 143 |
149 /** Return the address of the pixels for this SkBitmap. | 144 /** Return the address of the pixels for this SkBitmap. |
150 */ | 145 */ |
151 void* getPixels() const { return fPixels; } | 146 void* getPixels() const { return fPixels; } |
152 | 147 |
153 /** Return the byte size of the pixels, based on the height and rowBytes. | 148 /** Return the byte size of the pixels, based on the height and rowBytes. |
154 Note this truncates the result to 32bits. Call getSize64() to detect | 149 Note this truncates the result to 32bits. Call getSize64() to detect |
155 if the real size exceeds 32bits. | 150 if the real size exceeds 32bits. |
156 */ | 151 */ |
157 size_t getSize() const { return fInfo.fHeight * fRowBytes; } | 152 size_t getSize() const { return fHeight * fRowBytes; } |
158 | 153 |
159 /** Return the number of bytes from the pointer returned by getPixels() | 154 /** Return the number of bytes from the pointer returned by getPixels() |
160 to the end of the allocated space in the buffer. Required in | 155 to the end of the allocated space in the buffer. Required in |
161 cases where extractSubset has been called. | 156 cases where extractSubset has been called. |
162 */ | 157 */ |
163 size_t getSafeSize() const { return fInfo.getSafeSize(fRowBytes); } | 158 size_t getSafeSize() const ; |
164 | 159 |
165 /** | 160 /** |
166 * Return the full size of the bitmap, in bytes. | 161 * Return the full size of the bitmap, in bytes. |
167 */ | 162 */ |
168 int64_t computeSize64() const { | 163 int64_t computeSize64() const { |
169 return sk_64_mul(fInfo.fHeight, fRowBytes); | 164 return sk_64_mul(fHeight, fRowBytes); |
170 } | 165 } |
171 | 166 |
172 /** | 167 /** |
173 * Return the number of bytes from the pointer returned by getPixels() | 168 * Return the number of bytes from the pointer returned by getPixels() |
174 * to the end of the allocated space in the buffer. This may be smaller | 169 * to the end of the allocated space in the buffer. This may be smaller |
175 * than computeSize64() if there is any rowbytes padding beyond the width. | 170 * than computeSize64() if there is any rowbytes padding beyond the width. |
176 */ | 171 */ |
177 int64_t computeSafeSize64() const { | 172 int64_t computeSafeSize64() const { |
178 return fInfo.getSafeSize64(fRowBytes); | 173 return ComputeSafeSize64((Config)fConfig, fWidth, fHeight, fRowBytes); |
179 } | 174 } |
180 | 175 |
181 /** Returns true if this bitmap is marked as immutable, meaning that the | 176 /** Returns true if this bitmap is marked as immutable, meaning that the |
182 contents of its pixels will not change for the lifetime of the bitmap. | 177 contents of its pixels will not change for the lifetime of the bitmap. |
183 */ | 178 */ |
184 bool isImmutable() const; | 179 bool isImmutable() const; |
185 | 180 |
186 /** Marks this bitmap as immutable, meaning that the contents of its | 181 /** Marks this bitmap as immutable, meaning that the contents of its |
187 pixels will not change for the lifetime of the bitmap and of the | 182 pixels will not change for the lifetime of the bitmap and of the |
188 underlying pixelref. This state can be set, but it cannot be | 183 underlying pixelref. This state can be set, but it cannot be |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 * optional ReleaseProc and context. When the pixels are no longer | 297 * optional ReleaseProc and context. When the pixels are no longer |
303 * referenced, if ReleaseProc is not null, it will be called with the | 298 * referenced, if ReleaseProc is not null, it will be called with the |
304 * pixels and context as parameters. | 299 * pixels and context as parameters. |
305 * On failure, the bitmap will be set to empty and return false. | 300 * On failure, the bitmap will be set to empty and return false. |
306 */ | 301 */ |
307 bool installPixels(const SkImageInfo&, void* pixels, size_t rowBytes, | 302 bool installPixels(const SkImageInfo&, void* pixels, size_t rowBytes, |
308 void (*ReleaseProc)(void* addr, void* context), | 303 void (*ReleaseProc)(void* addr, void* context), |
309 void* context); | 304 void* context); |
310 | 305 |
311 /** | 306 /** |
312 * DEPRECATED: call info(). | 307 * If the bitmap's config can be represented as SkImageInfo, return true, |
| 308 * and if info is not-null, set it to the bitmap's info. If it cannot be |
| 309 * represented as SkImageInfo, return false and ignore the info parameter. |
313 */ | 310 */ |
314 bool asImageInfo(SkImageInfo* info) const { | 311 bool asImageInfo(SkImageInfo* info) const; |
315 // compatibility: return false for kUnknown | |
316 if (kUnknown_SkColorType == this->colorType()) { | |
317 return false; | |
318 } | |
319 if (info) { | |
320 *info = this->info(); | |
321 } | |
322 return true; | |
323 } | |
324 | 312 |
325 /** Use this to assign a new pixel address for an existing bitmap. This | 313 /** Use this to assign a new pixel address for an existing bitmap. This |
326 will automatically release any pixelref previously installed. Only call | 314 will automatically release any pixelref previously installed. Only call |
327 this if you are handling ownership/lifetime of the pixel memory. | 315 this if you are handling ownership/lifetime of the pixel memory. |
328 | 316 |
329 If the bitmap retains a reference to the colortable (assuming it is | 317 If the bitmap retains a reference to the colortable (assuming it is |
330 not null) it will take care of incrementing the reference count. | 318 not null) it will take care of incrementing the reference count. |
331 | 319 |
332 @param pixels Address for the pixels, managed by the caller. | 320 @param pixels Address for the pixels, managed by the caller. |
333 @param ctable ColorTable (or null) that matches the specified pixels | 321 @param ctable ColorTable (or null) that matches the specified pixels |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 */ | 443 */ |
456 bool readyToDraw() const { | 444 bool readyToDraw() const { |
457 return this->getPixels() != NULL && | 445 return this->getPixels() != NULL && |
458 (this->config() != kIndex8_Config || NULL != fColorTable); | 446 (this->config() != kIndex8_Config || NULL != fColorTable); |
459 } | 447 } |
460 | 448 |
461 /** Returns the pixelRef's texture, or NULL | 449 /** Returns the pixelRef's texture, or NULL |
462 */ | 450 */ |
463 GrTexture* getTexture() const; | 451 GrTexture* getTexture() const; |
464 | 452 |
465 /** Return the bitmap's colortable, if it uses one (i.e. colorType is | 453 /** Return the bitmap's colortable, if it uses one (i.e. fConfig is |
466 Index_8) and the pixels are locked. | 454 kIndex8_Config) and the pixels are locked. |
467 Otherwise returns NULL. Does not affect the colortable's | 455 Otherwise returns NULL. Does not affect the colortable's |
468 reference count. | 456 reference count. |
469 */ | 457 */ |
470 SkColorTable* getColorTable() const { return fColorTable; } | 458 SkColorTable* getColorTable() const { return fColorTable; } |
471 | 459 |
472 /** Returns a non-zero, unique value corresponding to the pixels in our | 460 /** Returns a non-zero, unique value corresponding to the pixels in our |
473 pixelref. Each time the pixels are changed (and notifyPixelsChanged | 461 pixelref. Each time the pixels are changed (and notifyPixelsChanged |
474 is called), a different generation ID will be returned. Finally, if | 462 is called), a different generation ID will be returned. Finally, if |
475 their is no pixelRef then zero is returned. | 463 their is no pixelRef then zero is returned. |
476 */ | 464 */ |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
747 kImageIsImmutable_Flag = 0x04, | 735 kImageIsImmutable_Flag = 0x04, |
748 #ifdef SK_BUILD_FOR_ANDROID | 736 #ifdef SK_BUILD_FOR_ANDROID |
749 /* A hint for the renderer responsible for drawing this bitmap | 737 /* A hint for the renderer responsible for drawing this bitmap |
750 * indicating that it should attempt to use mipmaps when this bitmap | 738 * indicating that it should attempt to use mipmaps when this bitmap |
751 * is drawn scaled down. | 739 * is drawn scaled down. |
752 */ | 740 */ |
753 kHasHardwareMipMap_Flag = 0x08, | 741 kHasHardwareMipMap_Flag = 0x08, |
754 #endif | 742 #endif |
755 }; | 743 }; |
756 | 744 |
757 SkImageInfo fInfo; | |
758 | |
759 uint32_t fRowBytes; | 745 uint32_t fRowBytes; |
760 | 746 uint32_t fWidth; |
| 747 uint32_t fHeight; |
| 748 uint8_t fConfig; |
| 749 uint8_t fAlphaType; |
761 uint8_t fFlags; | 750 uint8_t fFlags; |
| 751 uint8_t fBytesPerPixel; // based on config |
762 | 752 |
763 void internalErase(const SkIRect&, U8CPU a, U8CPU r, U8CPU g, U8CPU b)const; | 753 void internalErase(const SkIRect&, U8CPU a, U8CPU r, U8CPU g, U8CPU b)const; |
764 | 754 |
765 /* Internal computations for safe size. | 755 /* Internal computations for safe size. |
766 */ | 756 */ |
767 static int64_t ComputeSafeSize64(Config config, | 757 static int64_t ComputeSafeSize64(Config config, |
768 uint32_t width, | 758 uint32_t width, |
769 uint32_t height, | 759 uint32_t height, |
770 size_t rowBytes); | 760 size_t rowBytes); |
771 static size_t ComputeSafeSize(Config config, | 761 static size_t ComputeSafeSize(Config config, |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
863 private: | 853 private: |
864 SkColorTable* fCTable; | 854 SkColorTable* fCTable; |
865 const SkPMColor* fColors; | 855 const SkPMColor* fColors; |
866 }; | 856 }; |
867 #define SkAutoLockColors(...) SK_REQUIRE_LOCAL_VAR(SkAutoLockColors) | 857 #define SkAutoLockColors(...) SK_REQUIRE_LOCAL_VAR(SkAutoLockColors) |
868 | 858 |
869 /////////////////////////////////////////////////////////////////////////////// | 859 /////////////////////////////////////////////////////////////////////////////// |
870 | 860 |
871 inline uint32_t* SkBitmap::getAddr32(int x, int y) const { | 861 inline uint32_t* SkBitmap::getAddr32(int x, int y) const { |
872 SkASSERT(fPixels); | 862 SkASSERT(fPixels); |
873 SkASSERT(this->config() == kARGB_8888_Config); | 863 SkASSERT(fConfig == kARGB_8888_Config); |
874 SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)th
is->height()); | 864 SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight); |
875 return (uint32_t*)((char*)fPixels + y * fRowBytes + (x << 2)); | 865 return (uint32_t*)((char*)fPixels + y * fRowBytes + (x << 2)); |
876 } | 866 } |
877 | 867 |
878 inline uint16_t* SkBitmap::getAddr16(int x, int y) const { | 868 inline uint16_t* SkBitmap::getAddr16(int x, int y) const { |
879 SkASSERT(fPixels); | 869 SkASSERT(fPixels); |
880 SkASSERT(this->config() == kRGB_565_Config || this->config() == kARGB_4444_C
onfig); | 870 SkASSERT(fConfig == kRGB_565_Config || fConfig == kARGB_4444_Config); |
881 SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)th
is->height()); | 871 SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight); |
882 return (uint16_t*)((char*)fPixels + y * fRowBytes + (x << 1)); | 872 return (uint16_t*)((char*)fPixels + y * fRowBytes + (x << 1)); |
883 } | 873 } |
884 | 874 |
885 inline uint8_t* SkBitmap::getAddr8(int x, int y) const { | 875 inline uint8_t* SkBitmap::getAddr8(int x, int y) const { |
886 SkASSERT(fPixels); | 876 SkASSERT(fPixels); |
887 SkASSERT(this->config() == kA8_Config || this->config() == kIndex8_Config); | 877 SkASSERT(fConfig == kA8_Config || fConfig == kIndex8_Config); |
888 SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)th
is->height()); | 878 SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight); |
889 return (uint8_t*)fPixels + y * fRowBytes + x; | 879 return (uint8_t*)fPixels + y * fRowBytes + x; |
890 } | 880 } |
891 | 881 |
892 inline SkPMColor SkBitmap::getIndex8Color(int x, int y) const { | 882 inline SkPMColor SkBitmap::getIndex8Color(int x, int y) const { |
893 SkASSERT(fPixels); | 883 SkASSERT(fPixels); |
894 SkASSERT(this->config() == kIndex8_Config); | 884 SkASSERT(fConfig == kIndex8_Config); |
895 SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)th
is->height()); | 885 SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight); |
896 SkASSERT(fColorTable); | 886 SkASSERT(fColorTable); |
897 return (*fColorTable)[*((const uint8_t*)fPixels + y * fRowBytes + x)]; | 887 return (*fColorTable)[*((const uint8_t*)fPixels + y * fRowBytes + x)]; |
898 } | 888 } |
899 | 889 |
900 #endif | 890 #endif |
OLD | NEW |