0

[PDF] Add Thumbnail::CalculateImageSize() to avoid unneeded memory alloc

Currently, several places instantiate the Thumbnail class, which
allocates memory for the thumbnail image data, only to never use the
allocated memory. This is because the Thumbnail instances are being used
solely to get the image size.

Add a dedicated CalculateImageSize() method for doing just that, so
callers that only care about the image size can get it without
instantiating a Thumbnail.

Change-Id: I3f49c4e5902f3832448fb973e481e34e27ce03b5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6290593
Reviewed-by: Alan Screen <awscreen@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1423294}
This commit is contained in:
Lei Zhang
2025-02-21 11:57:31 -08:00
committed by Chromium LUCI CQ
parent da911aec7a
commit 0b2f74c6fe
6 changed files with 39 additions and 27 deletions

@ -93,6 +93,10 @@ std::optional<Rotation> GetRotationFromRawValue(int rotation) {
}
}
gfx::SizeF GetPageSizeInPoints(FPDF_PAGE page) {
return gfx::SizeF(FPDF_GetPageWidthF(page), FPDF_GetPageHeightF(page));
}
gfx::RectF FloatPageRectToPixelRect(FPDF_PAGE page, const gfx::RectF& input) {
int output_width = FPDF_GetPageWidthF(page);
int output_height = FPDF_GetPageHeightF(page);
@ -769,7 +773,7 @@ gfx::RectF PDFiumPage::GetBoundingBox() {
}
// Page width and height are already swapped based on page rotation.
gfx::SizeF page_size(FPDF_GetPageWidthF(page), FPDF_GetPageHeightF(page));
gfx::SizeF page_size = GetPageSizeInPoints(page);
// Start with bounds with the left and bottom values at the max possible
// bounds and the right and top values at the min possible bounds. Bounds are
@ -1834,7 +1838,7 @@ Thumbnail PDFiumPage::GenerateThumbnail(float device_pixel_ratio) {
const bool has_alpha = !!FPDFPage_HasTransparency(page);
const int format = has_alpha ? FPDFBitmap_BGRA : FPDFBitmap_BGRx;
Thumbnail thumbnail = CreateThumbnail(device_pixel_ratio);
Thumbnail thumbnail(GetPageSizeInPoints(page), device_pixel_ratio);
const gfx::Size& image_size = thumbnail.image_size();
// Create and initialize the bitmap.
@ -1865,7 +1869,10 @@ Thumbnail PDFiumPage::GenerateThumbnail(float device_pixel_ratio) {
#if BUILDFLAG(ENABLE_PDF_INK2)
gfx::Size PDFiumPage::GetThumbnailSize(float device_pixel_ratio) {
return CreateThumbnail(device_pixel_ratio).image_size();
CHECK(available());
FPDF_PAGE page = GetPage();
return Thumbnail::CalculateImageSize(GetPageSizeInPoints(page),
device_pixel_ratio);
}
#endif
@ -1874,15 +1881,6 @@ void PDFiumPage::GenerateAndSendThumbnail(float device_pixel_ratio,
std::move(send_callback).Run(GenerateThumbnail(device_pixel_ratio));
}
Thumbnail PDFiumPage::CreateThumbnail(float device_pixel_ratio) {
CHECK(available());
FPDF_PAGE page = GetPage();
return Thumbnail(
gfx::SizeF(FPDF_GetPageWidthF(page), FPDF_GetPageHeightF(page)),
device_pixel_ratio);
}
void PDFiumPage::MarkAvailable() {
available_ = true;

@ -464,11 +464,6 @@ class PDFiumPage {
void GenerateAndSendThumbnail(float device_pixel_ratio,
SendThumbnailCallback send_callback);
// Creates a `Thumbnail` for a given `device_pixel_ratio` using this page's
// size. The caller is responsible for rendering the page content into the
// thumbnail.
Thumbnail CreateThumbnail(float device_pixel_ratio);
raw_ptr<PDFiumEngine> engine_;
ScopedFPDFPage page_;
ScopedFPDFTextPage text_page_;