Commit 819cd1ea authored by yiyix's avatar yiyix Committed by Chromium LUCI CQ

Fix unreasonable Canvas memory allocation

The maximum size allow for canvas initialization is 2147483647 (2^31-1).
When such a canvas is created, it requires 2^62 * (byte per pixel)
memory, or more than 1 terabyte of memory, to be allocate. It's not
reasonable to update canvas memory usage based on this number. In this
cl, I added a upper limit for the canvas size for memory usage
calculation only.

Bug: 1150554

Change-Id: I326a86fb804212b7f41f0f1cad153de35861b399
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2562825Reviewed-by: default avatarFernando Serboncini <fserb@chromium.org>
Commit-Queue: Yi Xu <yiyix@chromium.org>
Cr-Commit-Position: refs/heads/master@{#832102}
parent c27bba34
......@@ -113,6 +113,10 @@ constexpr int kDefaultCanvasHeight = 150;
constexpr int kUndefinedQualityValue = -1.0;
constexpr int kMinimumAccelerated2dCanvasSize = 128 * 129;
// A default size used for canvas memory allocation when canvas size is greater
// than 2^20.
constexpr uint32_t kMaximumCanvasSize = 2 << 20;
} // namespace
HTMLCanvasElement::HTMLCanvasElement(Document& document)
......@@ -1534,12 +1538,15 @@ void HTMLCanvasElement::UpdateMemoryUsage() {
const int bytes_per_pixel = ColorParams().BytesPerPixel();
intptr_t gpu_memory_usage = 0;
uint32_t canvas_width = std::min(kMaximumCanvasSize, width());
uint32_t canvas_height = std::min(kMaximumCanvasSize, height());
if (gpu_buffer_count) {
// Switch from cpu mode to gpu mode
base::CheckedNumeric<intptr_t> checked_usage =
gpu_buffer_count * bytes_per_pixel;
checked_usage *= width();
checked_usage *= height();
checked_usage *= canvas_width;
checked_usage *= canvas_height;
gpu_memory_usage =
checked_usage.ValueOrDefault(std::numeric_limits<intptr_t>::max());
}
......@@ -1548,8 +1555,8 @@ void HTMLCanvasElement::UpdateMemoryUsage() {
// in all cases.
base::CheckedNumeric<intptr_t> checked_usage =
non_gpu_buffer_count * bytes_per_pixel;
checked_usage *= width();
checked_usage *= height();
checked_usage *= canvas_width;
checked_usage *= canvas_height;
checked_usage += gpu_memory_usage;
intptr_t externally_allocated_memory =
checked_usage.ValueOrDefault(std::numeric_limits<intptr_t>::max());
......
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