diff --git a/shell/common/api/electron_api_native_image.cc b/shell/common/api/electron_api_native_image.cc index dc02006b195..6b88419f90c 100644 --- a/shell/common/api/electron_api_native_image.cc +++ b/shell/common/api/electron_api_native_image.cc @@ -12,6 +12,7 @@ #include "base/files/file_util.h" #include "base/logging.h" #include "base/memory/ref_counted_memory.h" +#include "base/numerics/safe_conversions.h" #include "base/strings/pattern.h" #include "base/strings/utf_string_conversions.h" #include "gin/arguments.h" @@ -147,13 +148,13 @@ NativeImage::~NativeImage() { } void NativeImage::UpdateExternalAllocatedMemoryUsage() { - int32_t new_memory_usage = 0; + int64_t new_memory_usage = 0; if (image_.HasRepresentation(gfx::Image::kImageRepSkia)) { auto* const image_skia = image_.ToImageSkia(); - if (!image_skia->isNull()) { - new_memory_usage = image_skia->bitmap()->computeByteSize(); - } + if (!image_skia->isNull()) + new_memory_usage = + base::as_signed(image_skia->bitmap()->computeByteSize()); } isolate_->AdjustAmountOfExternalAllocatedMemory(new_memory_usage - @@ -489,9 +490,8 @@ gin::Handle NativeImage::CreateFromBitmap( return gin::Handle(); } - unsigned int width = 0; - unsigned int height = 0; - double scale_factor = 1.; + int width = 0; + int height = 0; if (!options.Get("width", &width)) { thrower.ThrowError("width is required"); @@ -503,6 +503,9 @@ gin::Handle NativeImage::CreateFromBitmap( return gin::Handle(); } + if (width <= 0 || height <= 0) + return CreateEmpty(thrower.isolate()); + auto info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType); auto size_bytes = info.computeMinByteSize(); @@ -512,16 +515,12 @@ gin::Handle NativeImage::CreateFromBitmap( return gin::Handle(); } - options.Get("scaleFactor", &scale_factor); - - if (width == 0 || height == 0) { - return CreateEmpty(thrower.isolate()); - } - SkBitmap bitmap; bitmap.allocN32Pixels(width, height, false); bitmap.writePixels({info, buffer_data.data(), bitmap.rowBytes()}); + float scale_factor = 1.0F; + options.Get("scaleFactor", &scale_factor); gfx::ImageSkia image_skia = gfx::ImageSkia::CreateFromBitmap(bitmap, scale_factor); diff --git a/shell/common/api/electron_api_native_image.h b/shell/common/api/electron_api_native_image.h index ab8929a153f..a840c51445a 100644 --- a/shell/common/api/electron_api_native_image.h +++ b/shell/common/api/electron_api_native_image.h @@ -140,7 +140,7 @@ class NativeImage final : public gin::Wrappable { gfx::Image image_; raw_ptr isolate_; - int32_t memory_usage_ = 0; + int64_t memory_usage_ = 0; }; } // namespace electron::api