Just add new safe size apis

Update skia (and then hide the older versions) to come later
Inspired by https://skia-review.googlesource.com/c/skia/+/52665

Bug: skia:
Change-Id: I15c7395557fb49c4163cb3b323b5428abd2c752d
Reviewed-on: https://skia-review.googlesource.com/53520
Reviewed-by: Florin Malita <fmalita@chromium.org>
Commit-Queue: Mike Reed <reed@google.com>
This commit is contained in:
Mike Reed 2017-09-29 13:41:58 -04:00 committed by Skia Commit-Bot
parent c739b7260a
commit e74dafc74d
5 changed files with 44 additions and 5 deletions

View File

@ -141,6 +141,12 @@ public:
*/
void* getPixels() const { return fPixels; }
/**
* Returns the size (in bytes) of the bitmap's image buffer.
* If the calculation overflows, or if the height is 0, this returns 0.
*/
size_t computeByteSize() const { return fInfo.computeByteSize(fRowBytes); }
/** Return the byte size of the pixels, based on the height and rowBytes.
Note this truncates the result to 32bits. Call getSize64() to detect
if the real size exceeds 32bits.

View File

@ -315,6 +315,21 @@ public:
return sk_64_asS32(size);
}
/**
* Returns the size (in bytes) of the image buffer that this info needs, given the specified
* rowBytes. The rowBytes must be >= this->minRowBytes().
* If the calculation overflows, or if the height is 0, this returns 0.
*/
size_t computeByteSize(size_t rowBytes) const;
/**
* Returns the minimum size (in bytes) of the image buffer that this info needs.
* If the calculation overflows, or if the height is 0, this returns 0.
*/
size_t computeMinByteSize() const {
return this->computeByteSize(this->minRowBytes());
}
bool validRowBytes(size_t rowBytes) const {
uint64_t rb = sk_64_mul(fWidth, this->bytesPerPixel());
return rowBytes >= rb;

View File

@ -229,6 +229,12 @@ public:
*/
size_t getSafeSize() const { return fInfo.getSafeSize(fRowBytes); }
/**
* Returns the size (in bytes) of the pixmap's image buffer.
* If the calculation overflows, or if the height is 0, this returns 0.
*/
size_t computeByteSize() const { return fInfo.computeByteSize(fRowBytes); }
/** Returns true if all pixels are opaque. SkColorType determines how pixels
are encoded, and whether pixel describes alpha. Returns true for SkColorType
without alpha in each pixel; for other SkColorType, returns true if all

View File

@ -6,6 +6,7 @@
*/
#include "SkImageInfo.h"
#include "SkSafeMath.h"
#include "SkReadBuffer.h"
#include "SkWriteBuffer.h"
@ -70,6 +71,16 @@ static SkColorType stored_to_live(unsigned stored) {
///////////////////////////////////////////////////////////////////////////////////////////////////
size_t SkImageInfo::computeByteSize(size_t rowBytes) const {
if (0 == fHeight) {
return 0;
}
SkSafeMath safe;
size_t bytes = safe.add(safe.mul(fHeight - 1, rowBytes),
safe.mul(fWidth, this->bytesPerPixel()));
return safe ? bytes : 0;
}
static bool alpha_type_is_valid(SkAlphaType alphaType) {
return (alphaType >= kUnknown_SkAlphaType) && (alphaType <= kLastEnum_SkAlphaType);
}

View File

@ -35,9 +35,9 @@ sk_sp<SkPixelRef> SkMallocPixelRef::MakeDirect(const SkImageInfo& info,
}
sk_sp<SkPixelRef> SkMallocPixelRef::MakeUsing(void*(*alloc)(size_t),
const SkImageInfo& info,
size_t requestedRowBytes) {
sk_sp<SkPixelRef> SkMallocPixelRef::MakeUsing(void*(*alloc)(size_t),
const SkImageInfo& info,
size_t requestedRowBytes) {
if (!is_valid(info)) {
return nullptr;
}
@ -65,13 +65,14 @@ sk_sp<SkPixelRef> SkMallocPixelRef::MakeDirect(const SkImageInfo& info,
size_t size = sk_64_asS32(bigSize);
SkASSERT(size >= info.getSafeSize(rowBytes));
SkASSERT(info.getSafeSize(rowBytes) == info.computeByteSize(rowBytes));
void* addr = alloc(size);
if (nullptr == addr) {
return nullptr;
}
return sk_sp<SkPixelRef>(new SkMallocPixelRef(info, addr, rowBytes,
sk_free_releaseproc, nullptr));
return sk_sp<SkPixelRef>(new SkMallocPixelRef(info, addr, rowBytes,
sk_free_releaseproc, nullptr));
}
sk_sp<SkPixelRef> SkMallocPixelRef::MakeAllocate(const SkImageInfo& info,