Skip to content

Commit

Permalink
Printing: Load the source PDF only once.
Browse files Browse the repository at this point in the history
Instead of loading the PDF once per operation, which is slightly more
than once per page.

Review-Url: https://codereview.chromium.org/2508563003
Cr-Commit-Position: refs/heads/master@{#432594}
  • Loading branch information
leizleiz authored and Commit bot committed Nov 16, 2016
1 parent 54cd9da commit fe899c8
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 150 deletions.
2 changes: 1 addition & 1 deletion chrome/browser/printing/pdf_to_emf_converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ void PdfToEmfUtilityProcessHostClient::OnTempPdfReady(bool print_text_with_gdi,
if (!utility_process_host_ || !pdf)
return OnFailed();
// Should reply with OnPageCount().
Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles(
Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_Start(
IPC::GetPlatformFileForTransit(pdf->GetPlatformFile(), false), settings_,
print_text_with_gdi));
}
Expand Down
36 changes: 14 additions & 22 deletions chrome/browser/printing/print_preview_pdf_generated_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -335,29 +335,25 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest {
// Converts the PDF to a PNG file so that the layout test can do an image
// diff on this image and a reference image.
void PdfToPng() {
int num_pages;
double max_width_in_points = 0;
std::vector<uint8_t> bitmap_data;
double total_height_in_pixels = 0;
std::string pdf_data;

ASSERT_TRUE(base::ReadFileToString(pdf_file_save_path_, &pdf_data));
ASSERT_TRUE(chrome_pdf::GetPDFDocInfo(pdf_data.data(),
pdf_data.size(),
&num_pages,
&max_width_in_points));

int num_pages;
double max_width_in_points = 0;
void* pdf_handle = nullptr;
ASSERT_TRUE(chrome_pdf::GetPDFDocInfo(pdf_data.data(), pdf_data.size(),
&num_pages, &max_width_in_points,
&pdf_handle));
ASSERT_GT(num_pages, 0);
double max_width_in_pixels =
ConvertUnitDouble(max_width_in_points, kPointsPerInch, kDpi);

std::vector<uint8_t> bitmap_data;
double total_height_in_pixels = 0;
for (int i = 0; i < num_pages; ++i) {
double width_in_points, height_in_points;
ASSERT_TRUE(chrome_pdf::GetPDFPageSizeByIndex(pdf_data.data(),
pdf_data.size(),
i,
&width_in_points,
&height_in_points));
ASSERT_TRUE(chrome_pdf::GetPDFPageSizeByIndex(
pdf_handle, i, &width_in_points, &height_in_points));

double width_in_pixels = ConvertUnitDouble(
width_in_points, kPointsPerInch, kDpi);
Expand Down Expand Up @@ -388,14 +384,9 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest {
kColorChannels * settings.area().size().GetArea());

ASSERT_TRUE(chrome_pdf::RenderPDFPageToBitmap(
pdf_data.data(),
pdf_data.size(),
i,
page_bitmap_data.data(),
settings.area().size().width(),
settings.area().size().height(),
settings.dpi(),
true));
pdf_handle, i, page_bitmap_data.data(),
settings.area().size().width(), settings.area().size().height(),
settings.dpi(), true));
FillPng(&page_bitmap_data,
width_in_pixels,
max_width_in_pixels,
Expand All @@ -405,6 +396,7 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest {
page_bitmap_data.end());
}

chrome_pdf::ReleasePDFHandle(pdf_handle);
CreatePng(bitmap_data, max_width_in_pixels, total_height_in_pixels);
}

Expand Down
2 changes: 1 addition & 1 deletion chrome/common/chrome_utility_printing_messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ IPC_MESSAGE_CONTROL1(ChromeUtilityMsg_GetPrinterSemanticCapsAndDefaults,
// Tell the utility process to start rendering the given PDF into a metafile.
// Utility process would be alive until
// ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop message.
IPC_MESSAGE_CONTROL3(ChromeUtilityMsg_RenderPDFPagesToMetafiles,
IPC_MESSAGE_CONTROL3(ChromeUtilityMsg_RenderPDFPagesToMetafiles_Start,
IPC::PlatformFileForTransit /* input_file */,
printing::PdfRenderSettings /* settings */,
bool /* print_text_with_gdi */)
Expand Down
2 changes: 1 addition & 1 deletion chrome/service/service_utility_process_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class ServiceUtilityProcessHost::PdfToEmfState {
const printing::PdfRenderSettings& conversion_settings) {
if (!temp_dir_.CreateUniqueTempDir())
return false;
return host_->Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles(
return host_->Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_Start(
IPC::TakePlatformFileForTransit(std::move(pdf_file)),
conversion_settings, false /* print_text_with_gdi */));
}
Expand Down
63 changes: 32 additions & 31 deletions chrome/utility/printing_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ bool PrintingHandler::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(PrintingHandler, message)
#if defined(OS_WIN)
IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles,
OnRenderPDFPagesToMetafile)
IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_Start,
OnRenderPDFPagesToMetafileStart)
IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage,
OnRenderPDFPagesToMetafileGetPage)
IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop,
Expand All @@ -85,7 +85,7 @@ bool PrintingHandler::OnMessageReceived(const IPC::Message& message) {
}

#if defined(OS_WIN)
void PrintingHandler::OnRenderPDFPagesToMetafile(
void PrintingHandler::OnRenderPDFPagesToMetafileStart(
IPC::PlatformFileForTransit pdf_transit,
const PdfRenderSettings& settings,
bool print_text_with_gdi) {
Expand All @@ -109,6 +109,8 @@ void PrintingHandler::OnRenderPDFPagesToMetafileGetPage(
}

void PrintingHandler::OnRenderPDFPagesToMetafileStop() {
chrome_pdf::ReleasePDFHandle(pdf_handle_);
pdf_handle_ = nullptr;
ReleaseProcessIfNeeded();
}

Expand All @@ -134,6 +136,8 @@ void PrintingHandler::OnRenderPDFPagesToPWGRaster(

#if defined(OS_WIN)
int PrintingHandler::LoadPDF(base::File pdf_file) {
DCHECK(!pdf_handle_);

int64_t length64 = pdf_file.GetLength();
if (length64 <= 0 || length64 > std::numeric_limits<int>::max())
return 0;
Expand All @@ -145,7 +149,7 @@ int PrintingHandler::LoadPDF(base::File pdf_file) {

int total_page_count = 0;
if (!chrome_pdf::GetPDFDocInfo(&pdf_data_.front(), pdf_data_.size(),
&total_page_count, nullptr)) {
&total_page_count, nullptr, &pdf_handle_)) {
return 0;
}
return total_page_count;
Expand Down Expand Up @@ -174,19 +178,11 @@ bool PrintingHandler::RenderPdfPageToMetafile(int page_number,
// to StartPage.
metafile.StartPage(gfx::Size(), gfx::Rect(), 1);
if (!chrome_pdf::RenderPDFPageToDC(
&pdf_data_.front(),
pdf_data_.size(),
page_number,
metafile.context(),
pdf_rendering_settings_.dpi(),
pdf_rendering_settings_.area().x(),
pdf_handle_, page_number, metafile.context(),
pdf_rendering_settings_.dpi(), pdf_rendering_settings_.area().x(),
pdf_rendering_settings_.area().y(),
pdf_rendering_settings_.area().width(),
pdf_rendering_settings_.area().height(),
true,
false,
true,
true,
pdf_rendering_settings_.area().height(), true, false, true, true,
pdf_rendering_settings_.autorotate())) {
return false;
}
Expand Down Expand Up @@ -215,8 +211,9 @@ bool PrintingHandler::RenderPDFPagesToPWGRaster(
return false;

int total_page_count = 0;
void* pdf_handle = nullptr;
if (!chrome_pdf::GetPDFDocInfo(data.data(), data_size, &total_page_count,
nullptr)) {
nullptr, &pdf_handle)) {
return false;
}

Expand All @@ -225,27 +222,26 @@ bool PrintingHandler::RenderPDFPagesToPWGRaster(
encoder.EncodeDocumentHeader(&pwg_header);
int bytes_written = bitmap_file.WriteAtCurrentPos(pwg_header.data(),
pwg_header.size());
if (bytes_written != static_cast<int>(pwg_header.size()))
if (bytes_written != static_cast<int>(pwg_header.size())) {
chrome_pdf::ReleasePDFHandle(pdf_handle);
return false;
}

cloud_print::BitmapImage image(settings.area().size(),
cloud_print::BitmapImage::BGRA);
bool ret = true;
for (int i = 0; i < total_page_count; ++i) {
int page_number = i;

if (bitmap_settings.reverse_page_order) {
page_number = total_page_count - 1 - page_number;
}

if (!chrome_pdf::RenderPDFPageToBitmap(data.data(),
data_size,
page_number,
image.pixel_data(),
image.size().width(),
image.size().height(),
settings.dpi(),
autoupdate)) {
return false;
if (!chrome_pdf::RenderPDFPageToBitmap(
pdf_handle, page_number, image.pixel_data(), image.size().width(),
image.size().height(), settings.dpi(), autoupdate)) {
ret = false;
break;
}

cloud_print::PwgHeaderInfo header_info;
Expand Down Expand Up @@ -276,14 +272,19 @@ bool PrintingHandler::RenderPDFPagesToPWGRaster(
}

std::string pwg_page;
if (!encoder.EncodePage(image, header_info, &pwg_page))
return false;
if (!encoder.EncodePage(image, header_info, &pwg_page)) {
ret = false;
break;
}
bytes_written = bitmap_file.WriteAtCurrentPos(pwg_page.data(),
pwg_page.size());
if (bytes_written != static_cast<int>(pwg_page.size()))
return false;
if (bytes_written != static_cast<int>(pwg_page.size())) {
ret = false;
break;
}
}
return true;
chrome_pdf::ReleasePDFHandle(pdf_handle);
return ret;
}

void PrintingHandler::OnGetPrinterCapsAndDefaults(
Expand Down
3 changes: 2 additions & 1 deletion chrome/utility/printing_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class PrintingHandler : public UtilityMessageHandler {
private:
// IPC message handlers.
#if defined(OS_WIN)
void OnRenderPDFPagesToMetafile(IPC::PlatformFileForTransit pdf_transit,
void OnRenderPDFPagesToMetafileStart(IPC::PlatformFileForTransit pdf_transit,
const PdfRenderSettings& settings,
bool print_text_with_gdi);
void OnRenderPDFPagesToMetafileGetPage(
Expand Down Expand Up @@ -70,6 +70,7 @@ class PrintingHandler : public UtilityMessageHandler {
#if defined(OS_WIN)
std::vector<char> pdf_data_;
PdfRenderSettings pdf_rendering_settings_;
void* pdf_handle_ = nullptr;
#endif

DISALLOW_COPY_AND_ASSIGN(PrintingHandler);
Expand Down
53 changes: 32 additions & 21 deletions pdf/pdf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ const void* PPP_GetInterface(const char* interface_name) {
}

#if defined(OS_WIN)
bool RenderPDFPageToDC(const void* pdf_buffer,
int buffer_size,
bool RenderPDFPageToDC(void* pdf_handle,
int page_number,
HDC dc,
int dpi,
Expand All @@ -107,8 +106,8 @@ bool RenderPDFPageToDC(const void* pdf_buffer,
pp::Rect(bounds_origin_x, bounds_origin_y, bounds_width, bounds_height),
fit_to_bounds, stretch_to_bounds, keep_aspect_ratio, center_in_bounds,
autorotate);
bool ret = engine_exports->RenderPDFPageToDC(pdf_buffer, buffer_size,
page_number, settings, dc);
bool ret =
engine_exports->RenderPDFPageToDC(pdf_handle, page_number, settings, dc);
if (!g_sdk_initialized_via_pepper)
ShutdownSDK();

Expand All @@ -126,39 +125,51 @@ void SetPDFUseGDIPrinting(bool enable) {
#endif // defined(OS_WIN)

bool GetPDFDocInfo(const void* pdf_buffer,
int buffer_size, int* page_count,
double* max_page_width) {
int buffer_size,
int* page_count,
double* max_page_width,
void** pdf_handle) {
if (!g_sdk_initialized_via_pepper) {
if (!InitializeSDK())
return false;
}
PDFEngineExports* engine_exports = PDFEngineExports::Get();
bool ret = engine_exports->GetPDFDocInfo(
pdf_buffer, buffer_size, page_count, max_page_width);
bool ret = engine_exports->GetPDFDocInfo(pdf_buffer, buffer_size, page_count,
max_page_width, pdf_handle);
if (!g_sdk_initialized_via_pepper)
ShutdownSDK();

return ret;
}

bool GetPDFPageSizeByIndex(const void* pdf_buffer,
int pdf_buffer_size, int page_number,
double* width, double* height) {
void ReleasePDFHandle(void* pdf_handle) {
if (!g_sdk_initialized_via_pepper) {
if (!chrome_pdf::InitializeSDK())
if (!InitializeSDK())
return;
}
PDFEngineExports* engine_exports = PDFEngineExports::Get();
engine_exports->ReleasePDFHandle(pdf_handle);
if (!g_sdk_initialized_via_pepper)
ShutdownSDK();
}

bool GetPDFPageSizeByIndex(void* pdf_handle,
int page_number,
double* width,
double* height) {
if (!g_sdk_initialized_via_pepper) {
if (!InitializeSDK())
return false;
}
chrome_pdf::PDFEngineExports* engine_exports =
chrome_pdf::PDFEngineExports::Get();
bool ret = engine_exports->GetPDFPageSizeByIndex(
pdf_buffer, pdf_buffer_size, page_number, width, height);
PDFEngineExports* engine_exports = PDFEngineExports::Get();
bool ret = engine_exports->GetPDFPageSizeByIndex(pdf_handle, page_number,
width, height);
if (!g_sdk_initialized_via_pepper)
chrome_pdf::ShutdownSDK();
ShutdownSDK();
return ret;
}

bool RenderPDFPageToBitmap(const void* pdf_buffer,
int pdf_buffer_size,
bool RenderPDFPageToBitmap(void* pdf_handle,
int page_number,
void* bitmap_buffer,
int bitmap_width,
Expand All @@ -173,8 +184,8 @@ bool RenderPDFPageToBitmap(const void* pdf_buffer,
PDFEngineExports::RenderingSettings settings(
dpi, dpi, pp::Rect(bitmap_width, bitmap_height), true, false, true, true,
autorotate);
bool ret = engine_exports->RenderPDFPageToBitmap(
pdf_buffer, pdf_buffer_size, page_number, settings, bitmap_buffer);
bool ret = engine_exports->RenderPDFPageToBitmap(pdf_handle, page_number,
settings, bitmap_buffer);
if (!g_sdk_initialized_via_pepper)
ShutdownSDK();

Expand Down
Loading

0 comments on commit fe899c8

Please sign in to comment.