Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: third_party/WebKit/Source/platform/graphics/ImageBuffer.cpp

Issue 1775613002: Throw a RangeError exception from get/createImageData when out of memory (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/modules/canvas2d/CanvasRenderingContext2DAPITest.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2008, Google Inc. All rights reserved. 2 * Copyright (c) 2008, Google Inc. All rights reserved.
3 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org> 3 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. 4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are 7 * modification, are permitted provided that the following conditions are
8 * met: 8 * met:
9 * 9 *
10 * * Redistributions of source code must retain the above copyright 10 * * Redistributions of source code must retain the above copyright
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 285
286 bool ImageBuffer::getImageData(Multiply multiplied, const IntRect& rect, WTF::Ar rayBufferContents& contents) const 286 bool ImageBuffer::getImageData(Multiply multiplied, const IntRect& rect, WTF::Ar rayBufferContents& contents) const
287 { 287 {
288 Checked<int, RecordOverflow> dataSize = 4; 288 Checked<int, RecordOverflow> dataSize = 4;
289 dataSize *= rect.width(); 289 dataSize *= rect.width();
290 dataSize *= rect.height(); 290 dataSize *= rect.height();
291 if (dataSize.hasOverflowed()) 291 if (dataSize.hasOverflowed())
292 return false; 292 return false;
293 293
294 if (!isSurfaceValid()) { 294 if (!isSurfaceValid()) {
295 WTF::ArrayBufferContents result(rect.width() * rect.height(), 4, WTF::Ar rayBufferContents::NotShared, WTF::ArrayBufferContents::ZeroInitialize); 295 size_t allocSizeInBytes = rect.width() * rect.height() * 4;
296 void* data;
297 WTF::ArrayBufferContents::allocateMemoryOrNull(allocSizeInBytes, WTF::Ar rayBufferContents::ZeroInitialize, data);
298 if (!data)
299 return false;
300 WTF::ArrayBufferContents result(data, allocSizeInBytes, WTF::ArrayBuffer Contents::NotShared);
296 result.transfer(contents); 301 result.transfer(contents);
297 return true; 302 return true;
298 } 303 }
299 304
300 ASSERT(canvas()); 305 ASSERT(canvas());
301 RefPtr<SkImage> snapshot = m_surface->newImageSnapshot(PreferNoAcceleration, SnapshotReasonGetImageData); 306 RefPtr<SkImage> snapshot = m_surface->newImageSnapshot(PreferNoAcceleration, SnapshotReasonGetImageData);
302 if (!snapshot) 307 if (!snapshot)
303 return false; 308 return false;
304 309
305 const bool mayHaveStrayArea = 310 const bool mayHaveStrayArea =
306 m_surface->isAccelerated() // GPU readback may fail silently 311 m_surface->isAccelerated() // GPU readback may fail silently
307 || rect.x() < 0 312 || rect.x() < 0
308 || rect.y() < 0 313 || rect.y() < 0
309 || rect.maxX() > m_surface->size().width() 314 || rect.maxX() > m_surface->size().width()
310 || rect.maxY() > m_surface->size().height(); 315 || rect.maxY() > m_surface->size().height();
311 WTF::ArrayBufferContents result( 316 size_t allocSizeInBytes = rect.width() * rect.height() * 4;
312 rect.width() * rect.height(), 4, 317 void* data;
313 WTF::ArrayBufferContents::NotShared, 318 WTF::ArrayBufferContents::allocateMemoryOrNull(allocSizeInBytes, mayHaveStra yArea ? WTF::ArrayBufferContents::ZeroInitialize : WTF::ArrayBufferContents::Don tInitialize, data);
Stephen White 2016/03/07 22:09:54 Nit: might be clearer to pull the initialize enum
314 mayHaveStrayArea 319 if (!data)
315 ? WTF::ArrayBufferContents::ZeroInitialize 320 return false;
316 : WTF::ArrayBufferContents::DontInitialize); 321 WTF::ArrayBufferContents result(data, allocSizeInBytes, WTF::ArrayBufferCont ents::NotShared);
317 322
318 SkAlphaType alphaType = (multiplied == Premultiplied) ? kPremul_SkAlphaType : kUnpremul_SkAlphaType; 323 SkAlphaType alphaType = (multiplied == Premultiplied) ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
319 SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), kRGBA_8888 _SkColorType, alphaType); 324 SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), kRGBA_8888 _SkColorType, alphaType);
320 325
321 snapshot->readPixels(info, result.data(), 4 * rect.width(), rect.x(), rect.y ()); 326 snapshot->readPixels(info, result.data(), 4 * rect.width(), rect.x(), rect.y ());
322 result.transfer(contents); 327 result.transfer(contents);
323 return true; 328 return true;
324 } 329 }
325 330
326 void ImageBuffer::putByteArray(Multiply multiplied, const unsigned char* source, const IntSize& sourceSize, const IntRect& sourceRect, const IntPoint& destPoint ) 331 void ImageBuffer::putByteArray(Multiply multiplied, const unsigned char* source, const IntSize& sourceSize, const IntRect& sourceRect, const IntPoint& destPoint )
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType)); 404 ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType));
400 405
401 Vector<unsigned char> result; 406 Vector<unsigned char> result;
402 if (!encodeImage(mimeType, quality, &result)) 407 if (!encodeImage(mimeType, quality, &result))
403 return "data:,"; 408 return "data:,";
404 409
405 return "data:" + mimeType + ";base64," + base64Encode(result); 410 return "data:" + mimeType + ";base64," + base64Encode(result);
406 } 411 }
407 412
408 } // namespace blink 413 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/canvas2d/CanvasRenderingContext2DAPITest.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698