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