From cf559d7c72d131a2b43ac4731bc1880f6e7dbc98 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Tue, 1 Apr 2025 16:45:48 -0500 Subject: [PATCH] fix: leaked `gfx::Canvas` in `AutofillPopupView::OnPaint()` (#46413) * perf: avoid redundant call to popup_bounds_in_view() Co-authored-by: Charles Kerr * refactor: use a std::optional<> for paint_canvas local Co-authored-by: Charles Kerr * fix: fix leaked gfx::Canvas in AutofillPopupView::OnPaint() Co-authored-by: Charles Kerr * refactor: remove redundant get() call when testing smart pointer for nonempty Co-authored-by: Charles Kerr * refactor: remove unnecessary draw_canvas variable Co-authored-by: Charles Kerr * refactor: rename bitmap to offscreen_bitmap for symmetry Co-authored-by: Charles Kerr * refactor: avoid another redundant call to popup_bounds_in_view() Co-authored-by: Charles Kerr --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr --- shell/browser/ui/views/autofill_popup_view.cc | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/shell/browser/ui/views/autofill_popup_view.cc b/shell/browser/ui/views/autofill_popup_view.cc index da7527b41923..5ee412066165 100644 --- a/shell/browser/ui/views/autofill_popup_view.cc +++ b/shell/browser/ui/views/autofill_popup_view.cc @@ -5,6 +5,7 @@ #include "shell/browser/ui/views/autofill_popup_view.h" #include +#include #include #include "base/functional/bind.h" @@ -238,30 +239,33 @@ void AutofillPopupView::DoUpdateBoundsAndRedrawPopup() { void AutofillPopupView::OnPaint(gfx::Canvas* canvas) { if (!popup_ || static_cast(popup_->line_count()) != children().size()) return; - gfx::Canvas* draw_canvas = canvas; - SkBitmap bitmap; - std::unique_ptr paint_canvas; - if (view_proxy_.get()) { - bitmap.allocN32Pixels(popup_->popup_bounds_in_view().width(), - popup_->popup_bounds_in_view().height(), true); - paint_canvas = std::make_unique(bitmap); - draw_canvas = new gfx::Canvas(paint_canvas.get(), 1.0); + gfx::Rect offscreen_bounds; + SkBitmap offscreen_bitmap; + std::optional offscreen_paint_canvas; + std::optional offscreen_draw_canvas; + if (view_proxy_) { + offscreen_bounds = popup_->popup_bounds_in_view(); + offscreen_bitmap.allocN32Pixels(offscreen_bounds.width(), + offscreen_bounds.height(), true); + offscreen_paint_canvas.emplace(offscreen_bitmap); + offscreen_draw_canvas.emplace(&offscreen_paint_canvas.value(), 1.0); + canvas = &offscreen_draw_canvas.value(); } - draw_canvas->DrawColor( + canvas->DrawColor( GetColorProvider()->GetColor(ui::kColorResultsTableNormalBackground)); - OnPaintBorder(draw_canvas); + OnPaintBorder(canvas); for (int i = 0; i < popup_->line_count(); ++i) { gfx::Rect line_rect = popup_->GetRowBounds(i); - DrawAutofillEntry(draw_canvas, i, line_rect); + DrawAutofillEntry(canvas, i, line_rect); } - if (view_proxy_.get()) { - view_proxy_->SetBounds(popup_->popup_bounds_in_view()); - view_proxy_->SetBitmap(bitmap); + if (view_proxy_) { + view_proxy_->SetBounds(offscreen_bounds); + view_proxy_->SetBitmap(offscreen_bitmap); } }