always zero SkMallocPixelRefs
I'm getting tired of trying to figure out where
clients screw up and forget to clear these buffers,
and I'd like a safer safety net for our own screw ups.
Bug: chromium:934161, many more
Change-Id: I6ada4c821da6dd173e54c6402c17d6946ff05fdf
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/207857
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Klein <mtklein@google.com>
diff --git a/include/core/SkBitmap.h b/include/core/SkBitmap.h
index 76f255d..43190a1 100644
--- a/include/core/SkBitmap.h
+++ b/include/core/SkBitmap.h
@@ -418,14 +418,14 @@
bool setInfo(const SkImageInfo& imageInfo, size_t rowBytes = 0);
/** \enum SkBitmap::AllocFlags
- AllocFlags provides the option to zero pixel memory when allocated.
+ AllocFlags is obsolete. We always zero pixel memory when allocated.
*/
enum AllocFlags {
- kZeroPixels_AllocFlag = 1 << 0, //!< zero pixel memory
+ kZeroPixels_AllocFlag = 1 << 0, //!< zero pixel memory. No effect. This is the default.
};
/** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel
- memory. If flags is kZeroPixels_AllocFlag, memory is zeroed.
+ memory. Memory is zeroed.
Returns false and calls reset() if SkImageInfo could not be set, or memory could
not be allocated, or memory could not optionally be zeroed.
@@ -433,11 +433,7 @@
On most platforms, allocating pixel memory may succeed even though there is
not sufficient memory to hold pixels; allocation does not take place
until the pixels are written to. The actual behavior depends on the platform
- implementation of malloc(), if flags is zero, and calloc(), if flags is
- kZeroPixels_AllocFlag.
-
- flags set to kZeroPixels_AllocFlag offers equal or better performance than
- subsequently calling eraseColor() with SK_ColorTRANSPARENT.
+ implementation of calloc().
@param info contains width, height, SkAlphaType, SkColorType, SkColorSpace
@param flags kZeroPixels_AllocFlag, or zero
@@ -446,7 +442,7 @@
bool SK_WARN_UNUSED_RESULT tryAllocPixelsFlags(const SkImageInfo& info, uint32_t flags);
/** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel
- memory. If flags is kZeroPixels_AllocFlag, memory is zeroed.
+ memory. Memory is zeroed.
Aborts execution if SkImageInfo could not be set, or memory could
not be allocated, or memory could not optionally
@@ -456,11 +452,7 @@
On most platforms, allocating pixel memory may succeed even though there is
not sufficient memory to hold pixels; allocation does not take place
until the pixels are written to. The actual behavior depends on the platform
- implementation of malloc(), if flags is zero, and calloc(), if flags is
- kZeroPixels_AllocFlag.
-
- flags set to kZeroPixels_AllocFlag offers equal or better performance than
- subsequently calling eraseColor() with SK_ColorTRANSPARENT.
+ implementation of calloc().
@param info contains width, height, SkAlphaType, SkColorType, SkColorSpace
@param flags kZeroPixels_AllocFlag, or zero
diff --git a/include/core/SkMallocPixelRef.h b/include/core/SkMallocPixelRef.h
index 5971a63..66acdcb 100644
--- a/include/core/SkMallocPixelRef.h
+++ b/include/core/SkMallocPixelRef.h
@@ -34,18 +34,13 @@
* If rowBytes is > 0, then it will be respected, or NULL will be returned
* if rowBytes is invalid for the specified info.
*
- * All pixel bytes are left uninitialized.
+ * All pixel bytes are zeroed.
*
* Returns NULL on failure.
*/
static sk_sp<SkPixelRef> MakeAllocate(const SkImageInfo&, size_t rowBytes);
/**
- * Identical to MakeAllocate, except all pixel bytes are zeroed.
- */
- static sk_sp<SkPixelRef> MakeZeroed(const SkImageInfo&, size_t rowBytes);
-
- /**
* Return a new SkMallocPixelRef with the provided pixel storage and
* rowBytes. On destruction, ReleaseProc will be called.
*
@@ -73,11 +68,6 @@
~SkMallocPixelRef() override;
private:
- // Uses alloc to implement NewAllocate or NewZeroed.
- static sk_sp<SkPixelRef> MakeUsing(void*(*alloc)(size_t),
- const SkImageInfo&,
- size_t rowBytes);
-
ReleaseProc fReleaseProc;
void* fReleaseProcContext;
diff --git a/src/core/SkBitmap.cpp b/src/core/SkBitmap.cpp
index 45c0218..33fe19d 100644
--- a/src/core/SkBitmap.cpp
+++ b/src/core/SkBitmap.cpp
@@ -286,9 +286,8 @@
// setInfo may have corrected info (e.g. 565 is always opaque).
const SkImageInfo& correctedInfo = this->info();
- sk_sp<SkPixelRef> pr = (allocFlags & kZeroPixels_AllocFlag) ?
- SkMallocPixelRef::MakeZeroed(correctedInfo, correctedInfo.minRowBytes()) :
- SkMallocPixelRef::MakeAllocate(correctedInfo, correctedInfo.minRowBytes());
+ sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(correctedInfo,
+ correctedInfo.minRowBytes());
if (!pr) {
return reset_return_false(this);
}
diff --git a/src/core/SkMallocPixelRef.cpp b/src/core/SkMallocPixelRef.cpp
index 8bef6c3..7f625cf 100644
--- a/src/core/SkMallocPixelRef.cpp
+++ b/src/core/SkMallocPixelRef.cpp
@@ -54,10 +54,7 @@
}
-sk_sp<SkPixelRef> SkMallocPixelRef::MakeUsing(void*(*allocProc)(size_t),
- const SkImageInfo& info,
- size_t requestedRowBytes) {
- size_t rowBytes = requestedRowBytes;
+sk_sp<SkPixelRef> SkMallocPixelRef::MakeAllocate(const SkImageInfo& info, size_t rowBytes) {
if (rowBytes == 0) {
rowBytes = info.minRowBytes();
// rowBytes can still be zero, if it overflowed (width * bytesPerPixel > size_t)
@@ -73,7 +70,7 @@
return nullptr;
}
}
- void* addr = allocProc(size);
+ void* addr = sk_calloc_canfail(size);
if (nullptr == addr) {
return nullptr;
}
@@ -82,15 +79,6 @@
sk_free_releaseproc, nullptr));
}
-sk_sp<SkPixelRef> SkMallocPixelRef::MakeAllocate(const SkImageInfo& info, size_t rowBytes) {
- return MakeUsing(sk_malloc_canfail, info, rowBytes);
-}
-
-sk_sp<SkPixelRef> SkMallocPixelRef::MakeZeroed(const SkImageInfo& info,
- size_t rowBytes) {
- return MakeUsing(sk_calloc_canfail, info, rowBytes);
-}
-
static void sk_data_releaseproc(void*, void* dataPtr) {
(static_cast<SkData*>(dataPtr))->unref();
}
diff --git a/src/core/SkSpecialSurface.cpp b/src/core/SkSpecialSurface.cpp
index a644003..1d9bad1 100644
--- a/src/core/SkSpecialSurface.cpp
+++ b/src/core/SkSpecialSurface.cpp
@@ -105,7 +105,7 @@
return nullptr;
}
- sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeZeroed(info, 0);
+ sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, 0);
if (!pr) {
return nullptr;
}
diff --git a/src/image/SkSurface_Raster.cpp b/src/image/SkSurface_Raster.cpp
index ad56bcb..7c03378a 100644
--- a/src/image/SkSurface_Raster.cpp
+++ b/src/image/SkSurface_Raster.cpp
@@ -189,7 +189,7 @@
return nullptr;
}
- sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeZeroed(info, rowBytes);
+ sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, rowBytes);
if (!pr) {
return nullptr;
}