Bug 1303094 - make SourceSurfaceSkia allocation fallible by using sk_malloc. r=mchang

MozReview-Commit-ID: 7l3fOG0kkq0
This commit is contained in:
Lee Salzman 2017-02-17 16:41:58 -05:00
Родитель d5636fd62d
Коммит f66ac66a97
1 изменённых файлов: 28 добавлений и 10 удалений

Просмотреть файл

@ -40,14 +40,36 @@ SourceSurfaceSkia::GetFormat() const
return mFormat;
}
static sk_sp<SkData>
MakeSkData(unsigned char* aData, const IntSize& aSize, int32_t aStride)
{
CheckedInt<size_t> size = aStride;
size *= aSize.height;
if (size.isValid()) {
void* mem = sk_malloc_flags(size.value(), 0);
if (mem) {
if (aData) {
memcpy(mem, aData, size.value());
}
return SkData::MakeFromMalloc(mem, size.value());
}
}
return nullptr;
}
bool
SourceSurfaceSkia::InitFromData(unsigned char* aData,
const IntSize &aSize,
int32_t aStride,
SurfaceFormat aFormat)
{
SkPixmap pixmap(MakeSkiaImageInfo(aSize, aFormat), aData, aStride);
mImage = SkImage::MakeRasterCopy(pixmap);
sk_sp<SkData> data = MakeSkData(aData, aSize, aStride);
if (!data) {
return false;
}
SkImageInfo info = MakeSkiaImageInfo(aSize, aFormat);
mImage = SkImage::MakeRasterData(info, data, aStride);
if (!mImage) {
return false;
}
@ -106,16 +128,12 @@ SourceSurfaceSkia::GetData()
#ifdef USE_SKIA_GPU
if (mImage->isTextureBacked()) {
sk_sp<SkImage> raster;
CheckedInt<size_t> size = mStride;
size *= mSize.height;
if (size.isValid()) {
if (sk_sp<SkData> data = SkData::MakeUninitialized(size.value())) {
if (sk_sp<SkData> data = MakeSkData(nullptr, mSize, mStride)) {
SkImageInfo info = MakeSkiaImageInfo(mSize, mFormat);
if (mImage->readPixels(info, data->writable_data(), mStride, 0, 0, SkImage::kDisallow_CachingHint)) {
raster = SkImage::MakeRasterData(info, data, mStride);
}
}
}
if (raster) {
mImage = raster;
} else {