8000 fix: ensure external memory adjustments are balanced by dsanders11 · Pull Request #33312 · electron/electron · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: ensure external memory adjustments are balanced #33312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions shell/common/api/electron_api_native_image.cc
8000
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ base::win::ScopedHICON ReadICOFromPath(int size, const base::FilePath& path) {

NativeImage::NativeImage(v8::Isolate* isolate, const gfx::Image& image)
: image_(image), isolate_(isolate) {
AdjustAmountOfExternalAllocatedMemory(true);
UpdateExternalAllocatedMemoryUsage();
}

#if defined(OS_WIN)
Expand All @@ -120,22 +120,27 @@ NativeImage::NativeImage(v8::Isolate* isolate, const base::FilePath& hicon_path)
electron::util::ReadImageSkiaFromICO(&image_skia, GetHICON(256));
image_ = gfx::Image(image_skia);

AdjustAmountOfExternalAllocatedMemory(true);
UpdateExternalAllocatedMemoryUsage();
}
#endif

NativeImage::~NativeImage() {
AdjustAmountOfExternalAllocatedMemory(false);
isolate_->AdjustAmountOfExternalAllocatedMemory(-memory_usage_);
}

void NativeImage::AdjustAmountOfExternalAllocatedMemory(bool add) {
void NativeImage::UpdateExternalAllocatedMemoryUsage() {
int32_t new_memory_usage = 0;

if (image_.HasRepresentation(gfx::Image::kImageRepSkia)) {
auto* const image_skia = image_.ToImageSkia();
if (!image_skia->isNull()) {
int64_t size = image_skia->bitmap()->computeByteSize();
isolate_->AdjustAmountOfExternalAllocatedMemory(add ? size : -size);
new_memory_usage = image_skia->bitmap()->computeByteSize();
}
}

isolate_->AdjustAmountOfExternalAllocatedMemory(new_memory_usage -
memory_usage_);
memory_usage_ = new_memory_usage;
}

// static
Expand Down
3 changes: 2 additions & 1 deletion shell/common/api/electron_api_native_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class NativeImage : public gin::Wrappable<NativeImage> {
float GetAspectRatio(const absl::optional<float> scale_factor);
void AddRepresentation(const gin_helper::Dictionary& options);

void AdjustAmountOfExternalAllocatedMemory(bool add);
void UpdateExternalAllocatedMemoryUsage();

// Mark the image as template image.
void SetTemplateImage(bool setAsTemplate);
Expand All @@ -132,6 +132,7 @@ class NativeImage : public gin::Wrappable<NativeImage> {
gfx::Image image_;

v8::Isolate* isolate_;
int32_t memory_usage_ = 0;

DISALLOW_COPY_AND_ASSIGN(NativeImage);
};
Expand Down
0