Commit 8eac88c4 authored by Mike Reed's avatar Mike Reed Committed by Commit Bot

switch over to computeByteSize for image buffer calcs

The older apis (e.g. getSize) are deprecated.

old: getSize()
- computed height * rowBytes, even for last row
- not check for overflow

new: computeByteSize()
- computes (height - 1)* rowBytes + width*pixel_size (skip last padding)
- safely returns max_size_t on overflow

TBR=

Bug: 
Cq-Include-Trybots: master.tryserver.chromium.linux:linux_site_isolation
Change-Id: I1ba4bbec58a25c12ae69c368a6da7c8f84056ec7
Reviewed-on: https://chromium-review.googlesource.com/710395
Commit-Queue: Mike Reed <reed@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarWill Harris <wfh@chromium.org>
Reviewed-by: default avatarAntoine Labour <piman@chromium.org>
Reviewed-by: default avatarNico Weber <thakis@chromium.org>
Reviewed-by: default avatarFlorin Malita <fmalita@chromium.org>
Cr-Commit-Position: refs/heads/master@{#508477}
parent ab6785c6
......@@ -319,7 +319,7 @@ ScreenshotTester::Result ScreenshotTester::CompareScreenshotsRegularly(
testing_result.similarity = result.result;
scoped_refptr<base::RefCountedBytes> diff_image(new base::RefCountedBytes);
diff_image->data().resize(result.rgbDiffBitmap.getSize());
diff_image->data().resize(result.rgbDiffBitmap.computeByteSize());
CHECK(gfx::PNGCodec::EncodeBGRASkBitmap(result.rgbDiffBitmap, false,
&diff_image->data()))
<< "Could not encode difference to PNG";
......
......@@ -33,8 +33,8 @@ bool AreImagesEqual(const gfx::ImageSkia& first, const gfx::ImageSkia& second) {
if (!first_bitmap || !second_bitmap)
return false;
const size_t size = first_bitmap->getSize();
if (second_bitmap->getSize() != size)
const size_t size = first_bitmap->computeByteSize();
if (second_bitmap->computeByteSize() != size)
return false;
uint8_t* first_data = reinterpret_cast<uint8_t*>(first_bitmap->getPixels());
......
......@@ -141,6 +141,6 @@ void DesktopMediaListBase::ScheduleNextRefresh() {
// static
uint32_t DesktopMediaListBase::GetImageHash(const gfx::Image& image) {
SkBitmap bitmap = image.AsBitmap();
uint32_t value = base::Hash(bitmap.getPixels(), bitmap.getSize());
uint32_t value = base::Hash(bitmap.getPixels(), bitmap.computeByteSize());
return value;
}
......@@ -552,13 +552,13 @@ TEST_F(BackFwdMenuModelTest, FaviconLoadTest) {
SkBitmap valid_icon_bitmap = *valid_icon.ToSkBitmap();
// Verify we did not get the default favicon.
EXPECT_NE(0, memcmp(default_icon_bitmap.getPixels(),
valid_icon_bitmap.getPixels(),
default_icon_bitmap.getSize()));
EXPECT_NE(
0, memcmp(default_icon_bitmap.getPixels(), valid_icon_bitmap.getPixels(),
default_icon_bitmap.computeByteSize()));
// Verify we did get the expected favicon.
EXPECT_EQ(0, memcmp(new_icon_bitmap.getPixels(),
valid_icon_bitmap.getPixels(),
new_icon_bitmap.getSize()));
EXPECT_EQ(0,
memcmp(new_icon_bitmap.getPixels(), valid_icon_bitmap.getPixels(),
new_icon_bitmap.computeByteSize()));
// Make sure the browser deconstructor doesn't have problems.
browser->tab_strip_model()->CloseAllTabs();
......
......@@ -52,7 +52,8 @@ void GetImageCheckSum(const gfx::ImageFamily& image, base::MD5Digest* digest) {
SkBitmap bitmap = it->AsBitmap();
base::StringPiece image_data(
reinterpret_cast<const char*>(bitmap.getPixels()), bitmap.getSize());
reinterpret_cast<const char*>(bitmap.getPixels()),
bitmap.computeByteSize());
base::MD5Update(&md5_context, image_data);
}
......
......@@ -17,7 +17,8 @@ struct StructTraits<arc::mojom::ArcBitmapDataView, SkBitmap> {
const SkImageInfo& info = r.info();
DCHECK_EQ(info.colorType(), kRGBA_8888_SkColorType);
return base::make_span(static_cast<uint8_t*>(r.getPixels()), r.getSize());
return base::make_span(static_cast<uint8_t*>(r.getPixels()),
r.computeByteSize());
}
static uint32_t width(const SkBitmap& r) { return r.width(); }
static uint32_t height(const SkBitmap& r) { return r.height(); }
......
......@@ -48,7 +48,7 @@ static base::android::ScopedJavaLocalRef<jobject> CreateJavaNavigationEntry(
ConvertUTF16ToJavaString(env, entry->GetTitle()));
ScopedJavaLocalRef<jobject> j_bitmap;
const content::FaviconStatus& status = entry->GetFavicon();
if (status.valid && status.image.ToSkBitmap()->getSize() > 0)
if (status.valid && status.image.ToSkBitmap()->computeByteSize() > 0)
j_bitmap = gfx::ConvertToJavaBitmap(status.image.ToSkBitmap());
return content::Java_NavigationControllerImpl_createNavigationEntry(
......
......@@ -74,9 +74,8 @@ bool DoImagesMatch(const gfx::Image& a, const gfx::Image& b) {
a_bitmap.height() != b_bitmap.height()) {
return false;
}
return memcmp(a_bitmap.getPixels(),
b_bitmap.getPixels(),
a_bitmap.getSize()) == 0;
return memcmp(a_bitmap.getPixels(), b_bitmap.getPixels(),
a_bitmap.computeByteSize()) == 0;
}
class MockScreenshotManager : public content::NavigationEntryScreenshotManager {
......
......@@ -104,7 +104,7 @@ TEST_F(ClipboardMessageFilterTest, SimpleImage) {
ui::Clipboard::GetBitmapFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
SkBitmap actual = clipboard()->ReadImage(ui::CLIPBOARD_TYPE_COPY_PASTE);
EXPECT_EQ(sizeof(bitmap_data), actual.getSize());
EXPECT_EQ(sizeof(bitmap_data), actual.computeByteSize());
EXPECT_EQ(0,
memcmp(bitmap_data, actual.getAddr32(0, 0), sizeof(bitmap_data)));
}
......
......@@ -50,7 +50,7 @@ TEST(IPCMessageTest, Bitmap) {
SkBitmap bitmap;
bitmap.allocN32Pixels(10, 5);
memset(bitmap.getPixels(), 'A', bitmap.getSize());
memset(bitmap.getPixels(), 'A', bitmap.computeByteSize());
IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
IPC::ParamTraits<SkBitmap>::Write(&msg, bitmap);
......@@ -63,9 +63,10 @@ TEST(IPCMessageTest, Bitmap) {
EXPECT_EQ(bitmap.width(), output.width());
EXPECT_EQ(bitmap.height(), output.height());
EXPECT_EQ(bitmap.rowBytes(), output.rowBytes());
EXPECT_EQ(bitmap.getSize(), output.getSize());
EXPECT_EQ(memcmp(bitmap.getPixels(), output.getPixels(), bitmap.getSize()),
0);
EXPECT_EQ(bitmap.computeByteSize(), output.computeByteSize());
EXPECT_EQ(
memcmp(bitmap.getPixels(), output.getPixels(), bitmap.computeByteSize()),
0);
// Also test the corrupt case.
IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL);
......@@ -76,7 +77,7 @@ TEST(IPCMessageTest, Bitmap) {
EXPECT_TRUE(iter.ReadData(&fixed_data, &fixed_data_size));
bad_msg.WriteData(fixed_data, fixed_data_size);
// Add some bogus pixel data.
const size_t bogus_pixels_size = bitmap.getSize() * 2;
const size_t bogus_pixels_size = bitmap.computeByteSize() * 2;
std::unique_ptr<char[]> bogus_pixels(new char[bogus_pixels_size]);
memset(bogus_pixels.get(), 'B', bogus_pixels_size);
bad_msg.WriteData(bogus_pixels.get(), bogus_pixels_size);
......
......@@ -175,7 +175,7 @@ void WebCursor::CreateCustomData(const SkBitmap& bitmap,
return;
// Fill custom_data directly with the NativeImage pixels.
custom_data->resize(bitmap.getSize());
custom_data->resize(bitmap.computeByteSize());
if (!custom_data->empty()) {
//This will divide color values by alpha (un-premultiply) if necessary
SkImageInfo dstInfo = bitmap.info().makeAlphaType(kUnpremul_SkAlphaType);
......
......@@ -220,7 +220,7 @@ void CanvasCaptureHandler::CreateNewFrame(const SkImage* image) {
pixmap.colorType() == kBGRA_8888_SkColorType) &&
pixmap.alphaType() == kUnpremul_SkAlphaType) {
source_ptr = static_cast<const uint8*>(pixmap.addr(0, 0));
source_length = pixmap.getSafeSize64();
source_length = pixmap.computeByteSize();
source_stride = pixmap.rowBytes();
image_size.set_width(pixmap.width());
image_size.set_height(pixmap.height());
......
......@@ -150,18 +150,19 @@ void HtmlVideoElementCapturerSource::sendNewFrame() {
(kN32_SkColorType == kRGBA_8888_SkColorType) ? libyuv::FOURCC_ABGR
: libyuv::FOURCC_ARGB;
if (frame && libyuv::ConvertToI420(
static_cast<uint8*>(bitmap_.getPixels()), bitmap_.getSize(),
frame->visible_data(media::VideoFrame::kYPlane),
frame->stride(media::VideoFrame::kYPlane),
frame->visible_data(media::VideoFrame::kUPlane),
frame->stride(media::VideoFrame::kUPlane),
frame->visible_data(media::VideoFrame::kVPlane),
frame->stride(media::VideoFrame::kVPlane), 0 /* crop_x */,
0 /* crop_y */, frame->visible_rect().size().width(),
frame->visible_rect().size().height(),
bitmap_.info().width(), bitmap_.info().height(),
libyuv::kRotate0, source_pixel_format) == 0) {
if (frame &&
libyuv::ConvertToI420(
static_cast<uint8*>(bitmap_.getPixels()), bitmap_.computeByteSize(),
frame->visible_data(media::VideoFrame::kYPlane),
frame->stride(media::VideoFrame::kYPlane),
frame->visible_data(media::VideoFrame::kUPlane),
frame->stride(media::VideoFrame::kUPlane),
frame->visible_data(media::VideoFrame::kVPlane),
frame->stride(media::VideoFrame::kVPlane), 0 /* crop_x */,
0 /* crop_y */, frame->visible_rect().size().width(),
frame->visible_rect().size().height(), bitmap_.info().width(),
bitmap_.info().height(), libyuv::kRotate0,
source_pixel_format) == 0) {
// Success!
io_task_runner_->PostTask(
FROM_HERE, base::BindOnce(new_frame_callback_, frame, current_time));
......
......@@ -227,7 +227,7 @@ bool ImageDataSimpleBackend::Init(PPB_ImageData_Impl* impl,
SkImageInfo::MakeN32Premul(impl->width(), impl->height()));
shared_memory_.reset(
RenderThread::Get()
->HostAllocateSharedMemoryBuffer(skia_bitmap_.getSize())
->HostAllocateSharedMemoryBuffer(skia_bitmap_.computeByteSize())
.release());
return !!shared_memory_.get();
}
......@@ -239,7 +239,7 @@ TransportDIB* ImageDataSimpleBackend::GetTransportDIB() const { return NULL; }
void* ImageDataSimpleBackend::Map() {
DCHECK(shared_memory_.get());
if (map_count_++ == 0) {
shared_memory_->Map(skia_bitmap_.getSize());
shared_memory_->Map(skia_bitmap_.computeByteSize());
skia_bitmap_.setPixels(shared_memory_->memory());
// Our platform bitmaps are set to opaque by default, which we don't want.
skia_bitmap_.setAlphaType(kPremul_SkAlphaType);
......@@ -256,7 +256,7 @@ void ImageDataSimpleBackend::Unmap() {
int32_t ImageDataSimpleBackend::GetSharedMemory(base::SharedMemory** shm,
uint32_t* byte_count) {
*byte_count = skia_bitmap_.getSize();
*byte_count = skia_bitmap_.computeByteSize();
*shm = shared_memory_.get();
return PP_OK;
}
......
......@@ -243,11 +243,11 @@ void SkiaBenchmarking::Rasterize(gin::Arguments* args) {
picture->picture->playback(&benchmarking_canvas, &controller);
blink::WebArrayBuffer buffer =
blink::WebArrayBuffer::Create(bitmap.getSize(), 1);
blink::WebArrayBuffer::Create(bitmap.computeByteSize(), 1);
uint32_t* packed_pixels = reinterpret_cast<uint32_t*>(bitmap.getPixels());
uint8_t* buffer_pixels = reinterpret_cast<uint8_t*>(buffer.Data());
// Swizzle from native Skia format to RGBA as we copy out.
for (size_t i = 0; i < bitmap.getSize(); i += 4) {
for (size_t i = 0; i < bitmap.computeByteSize(); i += 4) {
uint32_t c = packed_pixels[i >> 2];
buffer_pixels[i] = SkGetPackedR32(c);
buffer_pixels[i + 1] = SkGetPackedG32(c);
......
......@@ -862,9 +862,9 @@ void BlinkTestRunner::OnPixelsDumpCompleted(const SkBitmap& snapshot) {
// The snapshot arrives from the GPU process via shared memory. Because MSan
// can't track initializedness across processes, we must assure it that the
// pixels are in fact initialized.
MSAN_UNPOISON(snapshot.getPixels(), snapshot.getSize());
MSAN_UNPOISON(snapshot.getPixels(), snapshot.computeByteSize());
base::MD5Digest digest;
base::MD5Sum(snapshot.getPixels(), snapshot.getSize(), &digest);
base::MD5Sum(snapshot.getPixels(), snapshot.computeByteSize(), &digest);
std::string actual_pixel_hash = base::MD5DigestToBase16(digest);
if (actual_pixel_hash == test_config_->expected_pixel_hash) {
......
......@@ -68,7 +68,7 @@ SkBitmap DecodeImage(const base::FilePath& path) {
reinterpret_cast<const unsigned char*>(file_contents.data());
SkBitmap bitmap =
content::DecodeImage(data, gfx::Size(), file_contents.length());
if (bitmap.computeSize64() > kMaxImageCanvas)
if (bitmap.computeByteSize() > kMaxImageCanvas)
return SkBitmap();
return bitmap;
}
......
......@@ -35,7 +35,7 @@ void ResizeImage(SkBitmap* decoded_image,
// sizeof(skia::mojom::Bitmap::Data_) + pixel data array header (8 bytes)
// Use a bigger number in case we need padding at the end.
int64_t struct_size = sizeof(skia::mojom::Bitmap::Data_) + kPadding;
int64_t image_size = decoded_image->computeSize64();
int64_t image_size = decoded_image->computeByteSize();
int halves = 0;
while (struct_size + (image_size >> 2 * halves) > max_size_in_bytes)
halves++;
......
......@@ -128,8 +128,8 @@ TEST_F(ImageDecoderImplTest, DecodeImageSizeLimit) {
ASSERT_FALSE(request.bitmap().isNull());
// Check that image has been shrunk appropriately
EXPECT_LT(request.bitmap().computeSize64() + base_msg_size,
kTestMaxImageSize);
EXPECT_LT(request.bitmap().computeByteSize() + base_msg_size,
(uint64_t)kTestMaxImageSize);
// Android does its own image shrinking for memory conservation deeper in
// the decode, so more specific tests here won't work.
#if !defined(OS_ANDROID)
......
......@@ -1250,9 +1250,9 @@ TEST_F(StructTraitsTest, CopyOutputResult_Bitmap) {
SkBitmap expected_bitmap;
expected_bitmap.allocN32Pixels(7, 8, adobe_rgb);
expected_bitmap.eraseARGB(123, 213, 77, 33);
EXPECT_EQ(expected_bitmap.getSize(), out_bitmap.getSize());
EXPECT_EQ(expected_bitmap.computeByteSize(), out_bitmap.computeByteSize());
EXPECT_EQ(0, std::memcmp(expected_bitmap.getPixels(), out_bitmap.getPixels(),
expected_bitmap.getSize()));
expected_bitmap.computeByteSize()));
EXPECT_TRUE(SkColorSpace::Equals(expected_bitmap.colorSpace(),
out_bitmap.colorSpace()));
}
......
......@@ -224,10 +224,6 @@ SK_API void SkDebugf_FileLine(const char* file, int line, bool fatal,
#define SK_SUPPORT_LEGACY_TILED_BITMAPS
#endif
#ifndef SK_SUPPORT_LEGACY_SAFESIZE64
#define SK_SUPPORT_LEGACY_SAFESIZE64
#endif
#ifndef SK_COLOR_SPACE_XFORM_LEGACY_PIPELINE
#define SK_COLOR_SPACE_XFORM_LEGACY_PIPELINE
#endif
......
......@@ -8772,8 +8772,8 @@ static void NodeImageTestValidation(const IntSize& reference_bitmap_size,
EXPECT_EQ(reference_bitmap_size.Width(), drag_image->Size().Width());
EXPECT_EQ(reference_bitmap_size.Height(), drag_image->Size().Height());
const SkBitmap& drag_bitmap = drag_image->Bitmap();
EXPECT_EQ(
0, memcmp(bitmap.getPixels(), drag_bitmap.getPixels(), bitmap.getSize()));
EXPECT_EQ(0, memcmp(bitmap.getPixels(), drag_bitmap.getPixels(),
bitmap.computeByteSize()));
}
TEST_P(ParameterizedWebFrameTest, NodeImageTestCSSTransformDescendant) {
......
......@@ -31,7 +31,7 @@ RefPtr<SharedBuffer> ReadFile(const char* dir, const char* file_name) {
}
unsigned HashBitmap(const SkBitmap& bitmap) {
return StringHasher::HashMemory(bitmap.getPixels(), bitmap.getSize());
return StringHasher::HashMemory(bitmap.getPixels(), bitmap.computeByteSize());
}
static unsigned CreateDecodingBaseline(DecoderCreator create_decoder,
......
......@@ -452,9 +452,10 @@ SkBitmap ClipboardAndroid::ReadImage(ClipboardType type) const {
bmp.allocN32Pixels(size->width(), size->height());
DCHECK_EQ(sizeof(gfx::Size) + bmp.getSize(), input.size());
DCHECK_EQ(sizeof(gfx::Size) + bmp.computeByteSize(), input.size());
memcpy(bmp.getPixels(), input.data() + sizeof(gfx::Size), bmp.getSize());
memcpy(bmp.getPixels(), input.data() + sizeof(gfx::Size),
bmp.computeByteSize());
}
return bmp;
}
......@@ -540,7 +541,7 @@ void ClipboardAndroid::WriteBitmap(const SkBitmap& bitmap) {
std::string packed(reinterpret_cast<const char*>(&size), sizeof(size));
packed += std::string(static_cast<const char*>(bitmap.getPixels()),
bitmap.getSize());
bitmap.computeByteSize());
g_map.Get().Set(kBitmapFormat, packed);
}
......
......@@ -811,7 +811,7 @@ void ClipboardWin::WriteBitmap(const SkBitmap& bitmap) {
if (bits && source_hbitmap) {
// Copy the bitmap out of shared memory and into GDI
memcpy(bits, bitmap.getPixels(), bitmap.getSize());
memcpy(bits, bitmap.getPixels(), bitmap.computeByteSize());
// Now we have an HBITMAP, we can write it to the clipboard
WriteBitmapFromHandle(source_hbitmap,
......
......@@ -77,7 +77,7 @@ ScopedJavaLocalRef<jobject> ConvertToJavaBitmap(const SkBitmap* skbitmap) {
JavaBitmap dst_lock(jbitmap);
void* src_pixels = skbitmap->getPixels();
void* dst_pixels = dst_lock.pixels();
memcpy(dst_pixels, src_pixels, skbitmap->getSize());
memcpy(dst_pixels, src_pixels, skbitmap->computeByteSize());
return jbitmap;
}
......@@ -105,10 +105,10 @@ SkBitmap CreateSkBitmapFromJavaBitmap(const JavaBitmap& jbitmap) {
CHECK(false) << "Invalid Java bitmap format: " << jbitmap.format();
break;
}
CHECK_EQ(jbitmap.byte_count(), static_cast<int>(skbitmap.getSize()));
CHECK_EQ(jbitmap.byte_count(), static_cast<int>(skbitmap.computeByteSize()));
const void* src_pixels = jbitmap.pixels();
void* dst_pixels = skbitmap.getPixels();
memcpy(dst_pixels, src_pixels, skbitmap.getSize());
memcpy(dst_pixels, src_pixels, skbitmap.computeByteSize());
return skbitmap;
}
......
......@@ -41,7 +41,7 @@ struct SkBitmap_Data {
if (!bitmap->tryAllocPixels(
SkImageInfo::Make(width, height, color_type, alpha_type)))
return false;
if (pixels_size != bitmap->getSize())
if (pixels_size != bitmap->computeByteSize())
return false;
memcpy(bitmap->getPixels(), pixels, pixels_size);
return true;
......@@ -58,7 +58,7 @@ void ParamTraits<SkBitmap>::Write(base::Pickle* m, const SkBitmap& p) {
bmp_data.InitSkBitmapDataForTransfer(p);
m->WriteData(reinterpret_cast<const char*>(&bmp_data),
static_cast<int>(fixed_size));
size_t pixel_size = p.getSize();
size_t pixel_size = p.computeByteSize();
m->WriteData(reinterpret_cast<const char*>(p.getPixels()),
static_cast<int>(pixel_size));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment