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