0

Revert "Printing: Load the source PDF only once."

This reverts commit fe899c8e19.

BUG=669797
TBR=thestig@chromium.org

Review-Url: https://codereview.chromium.org/2541843005
Cr-Commit-Position: refs/heads/master@{#435646}
This commit is contained in:
tsepez
2016-12-01 09:56:10 -08:00
committed by Commit bot
parent e8ad7b81c2
commit abff09f770
11 changed files with 114 additions and 125 deletions

@ -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_Start(
Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles(
IPC::GetPlatformFileForTransit(pdf->GetPlatformFile(), false), settings_,
print_text_with_gdi));
}

@ -335,25 +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() {
std::string pdf_data;
ASSERT_TRUE(base::ReadFileToString(pdf_file_save_path_, &pdf_data));
int num_pages;
double max_width_in_points = 0;
void* pdf_handle = nullptr;
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,
&pdf_handle));
&num_pages, &max_width_in_points));
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_handle, i, &width_in_points, &height_in_points));
pdf_data.data(), pdf_data.size(), i, &width_in_points,
&height_in_points));
double width_in_pixels = ConvertUnitDouble(
width_in_points, kPointsPerInch, kDpi);
@ -384,8 +384,9 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest {
settings.area.size().GetArea());
ASSERT_TRUE(chrome_pdf::RenderPDFPageToBitmap(
pdf_handle, i, page_bitmap_data.data(), settings.area.size().width(),
settings.area.size().height(), settings.dpi, settings.autorotate));
pdf_data.data(), pdf_data.size(), i, page_bitmap_data.data(),
settings.area.size().width(), settings.area.size().height(),
settings.dpi, settings.autorotate));
FillPng(&page_bitmap_data, width_in_pixels, max_width_in_pixels,
settings.area.size().height());
bitmap_data.insert(bitmap_data.end(),
@ -393,7 +394,6 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest {
page_bitmap_data.end());
}
chrome_pdf::ReleasePDFHandle(pdf_handle);
CreatePng(bitmap_data, max_width_in_pixels, total_height_in_pixels);
}

@ -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_Start,
IPC_MESSAGE_CONTROL3(ChromeUtilityMsg_RenderPDFPagesToMetafiles,
IPC::PlatformFileForTransit /* input_file */,
printing::PdfRenderSettings /* settings */,
bool /* print_text_with_gdi */)

@ -97,7 +97,7 @@ class ServiceUtilityProcessHost::PdfToEmfState {
const printing::PdfRenderSettings& conversion_settings) {
if (!temp_dir_.CreateUniqueTempDir())
return false;
return host_->Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_Start(
return host_->Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles(
IPC::TakePlatformFileForTransit(std::move(pdf_file)),
conversion_settings, false /* print_text_with_gdi */));
}

@ -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_Start,
OnRenderPDFPagesToMetafileStart)
IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles,
OnRenderPDFPagesToMetafile)
IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage,
OnRenderPDFPagesToMetafileGetPage)
IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop,
@ -85,7 +85,7 @@ bool PrintingHandler::OnMessageReceived(const IPC::Message& message) {
}
#if defined(OS_WIN)
void PrintingHandler::OnRenderPDFPagesToMetafileStart(
void PrintingHandler::OnRenderPDFPagesToMetafile(
IPC::PlatformFileForTransit pdf_transit,
const PdfRenderSettings& settings,
bool print_text_with_gdi) {
@ -109,8 +109,6 @@ void PrintingHandler::OnRenderPDFPagesToMetafileGetPage(
}
void PrintingHandler::OnRenderPDFPagesToMetafileStop() {
chrome_pdf::ReleasePDFHandle(pdf_handle_);
pdf_handle_ = nullptr;
ReleaseProcessIfNeeded();
}
@ -136,8 +134,6 @@ 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;
@ -149,7 +145,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, &pdf_handle_)) {
&total_page_count, nullptr)) {
return 0;
}
return total_page_count;
@ -177,7 +173,7 @@ bool PrintingHandler::RenderPdfPageToMetafile(int page_number,
// to StartPage.
metafile.StartPage(gfx::Size(), gfx::Rect(), 1);
if (!chrome_pdf::RenderPDFPageToDC(
pdf_handle_, page_number, metafile.context(),
&pdf_data_.front(), pdf_data_.size(), page_number, metafile.context(),
pdf_rendering_settings_.dpi, pdf_rendering_settings_.area.x(),
pdf_rendering_settings_.area.y(),
pdf_rendering_settings_.area.width(),
@ -208,9 +204,8 @@ 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, &pdf_handle)) {
nullptr)) {
return false;
}
@ -219,14 +214,11 @@ 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())) {
chrome_pdf::ReleasePDFHandle(pdf_handle);
if (bytes_written != static_cast<int>(pwg_header.size()))
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;
@ -235,8 +227,9 @@ bool PrintingHandler::RenderPDFPagesToPWGRaster(
}
if (!chrome_pdf::RenderPDFPageToBitmap(
pdf_handle, page_number, image.pixel_data(), image.size().width(),
image.size().height(), settings.dpi, settings.autorotate)) {
data.data(), data_size, page_number, image.pixel_data(),
image.size().width(), image.size().height(), settings.dpi,
settings.autorotate)) {
return false;
}
@ -268,19 +261,14 @@ bool PrintingHandler::RenderPDFPagesToPWGRaster(
}
std::string pwg_page;
if (!encoder.EncodePage(image, header_info, &pwg_page)) {
ret = false;
break;
}
if (!encoder.EncodePage(image, header_info, &pwg_page))
return false;
bytes_written = bitmap_file.WriteAtCurrentPos(pwg_page.data(),
pwg_page.size());
if (bytes_written != static_cast<int>(pwg_page.size())) {
ret = false;
break;
}
if (bytes_written != static_cast<int>(pwg_page.size()))
return false;
}
chrome_pdf::ReleasePDFHandle(pdf_handle);
return ret;
return true;
}
void PrintingHandler::OnGetPrinterCapsAndDefaults(

@ -34,7 +34,7 @@ class PrintingHandler : public UtilityMessageHandler {
private:
// IPC message handlers.
#if defined(OS_WIN)
void OnRenderPDFPagesToMetafileStart(IPC::PlatformFileForTransit pdf_transit,
void OnRenderPDFPagesToMetafile(IPC::PlatformFileForTransit pdf_transit,
const PdfRenderSettings& settings,
bool print_text_with_gdi);
void OnRenderPDFPagesToMetafileGetPage(
@ -68,7 +68,6 @@ 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);

@ -82,7 +82,8 @@ const void* PPP_GetInterface(const char* interface_name) {
}
#if defined(OS_WIN)
bool RenderPDFPageToDC(void* pdf_handle,
bool RenderPDFPageToDC(const void* pdf_buffer,
int buffer_size,
int page_number,
HDC dc,
int dpi,
@ -106,8 +107,8 @@ bool RenderPDFPageToDC(void* pdf_handle,
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_handle, page_number, settings, dc);
bool ret = engine_exports->RenderPDFPageToDC(pdf_buffer, buffer_size,
page_number, settings, dc);
if (!g_sdk_initialized_via_pepper)
ShutdownSDK();
@ -127,49 +128,40 @@ void SetPDFUseGDIPrinting(bool enable) {
bool GetPDFDocInfo(const void* pdf_buffer,
int buffer_size,
int* page_count,
double* max_page_width,
void** pdf_handle) {
double* max_page_width) {
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, pdf_handle);
max_page_width);
if (!g_sdk_initialized_via_pepper)
ShutdownSDK();
return ret;
}
void ReleasePDFHandle(void* pdf_handle) {
if (!g_sdk_initialized_via_pepper) {
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,
bool GetPDFPageSizeByIndex(const void* pdf_buffer,
int pdf_buffer_size,
int page_number,
double* width,
double* height) {
if (!g_sdk_initialized_via_pepper) {
if (!InitializeSDK())
if (!chrome_pdf::InitializeSDK())
return false;
}
PDFEngineExports* engine_exports = PDFEngineExports::Get();
bool ret = engine_exports->GetPDFPageSizeByIndex(pdf_handle, page_number,
width, height);
chrome_pdf::PDFEngineExports* engine_exports =
chrome_pdf::PDFEngineExports::Get();
bool ret = engine_exports->GetPDFPageSizeByIndex(pdf_buffer, pdf_buffer_size,
page_number, width, height);
if (!g_sdk_initialized_via_pepper)
ShutdownSDK();
chrome_pdf::ShutdownSDK();
return ret;
}
bool RenderPDFPageToBitmap(void* pdf_handle,
bool RenderPDFPageToBitmap(const void* pdf_buffer,
int pdf_buffer_size,
int page_number,
void* bitmap_buffer,
int bitmap_width,
@ -184,8 +176,8 @@ bool RenderPDFPageToBitmap(void* pdf_handle,
PDFEngineExports::RenderingSettings settings(
dpi, dpi, pp::Rect(bitmap_width, bitmap_height), true, false, true, true,
autorotate);
bool ret = engine_exports->RenderPDFPageToBitmap(pdf_handle, page_number,
settings, bitmap_buffer);
bool ret = engine_exports->RenderPDFPageToBitmap(
pdf_buffer, pdf_buffer_size, page_number, settings, bitmap_buffer);
if (!g_sdk_initialized_via_pepper)
ShutdownSDK();

@ -36,7 +36,9 @@ void PPP_ShutdownModule();
const void* PPP_GetInterface(const char* interface_name);
#if defined(OS_WIN)
// |pdf_handle| is the handle to the PDF document.
// |pdf_buffer| is the buffer that contains the entire PDF document to be
// rendered.
// |buffer_size| is the size of |pdf_buffer| in bytes.
// |page_number| is the 0-based index of the page to be rendered.
// |dc| is the device context to render into.
// |dpi| and |dpi_y| is the resolution. If the value is -1, the dpi from the DC
@ -61,7 +63,8 @@ const void* PPP_GetInterface(const char* interface_name);
// |autorotate| specifies whether the final image should be rotated to match
// the output bound.
// Returns false if the document or the page number are not valid.
bool RenderPDFPageToDC(void* pdf_handle,
bool RenderPDFPageToDC(const void* pdf_buffer,
int buffer_size,
int page_number,
HDC dc,
int dpi,
@ -81,35 +84,32 @@ void SetPDFEnsureTypefaceCharactersAccessible(
void SetPDFUseGDIPrinting(bool enable);
#endif // defined(OS_WIN)
// All the out parameters are optional and can be NULL.
// |page_count| and |max_page_width| are optional and can be NULL.
// Returns false if the document is not valid.
// Returns true on success. In which case, if |pdf_handle| is not NULL, then
// the handle is guaranteed to be valid and not NULL. The caller takes
// ownership of |pdf_handle| and must call ReleasePDFHandle() on it when done.
bool GetPDFDocInfo(const void* pdf_buffer,
int buffer_size,
int* page_count,
double* max_page_width,
void** pdf_handle);
// Releases the handle received from GetPDFDocInfo().
// |pdf_handle| can be NULL.
void ReleasePDFHandle(void* pdf_handle);
double* max_page_width);
// Gets the dimensions of a specific page in a document.
// |pdf_handle| is the handle to the PDF document.
// |pdf_buffer| is the buffer that contains the entire PDF document to be
// rendered.
// |pdf_buffer_size| is the size of |pdf_buffer| in bytes.
// |page_number| is the page number that the function will get the dimensions
// of.
// |width| is the output for the width of the page in points.
// |height| is the output for the height of the page in points.
// Returns false if the document or the page number are not valid.
bool GetPDFPageSizeByIndex(void* pdf_handle,
bool GetPDFPageSizeByIndex(const void* pdf_buffer,
int pdf_buffer_size,
int page_number,
double* width,
double* height);
// Renders PDF page into 4-byte per pixel BGRA color bitmap.
// |pdf_handle| is the handle to the PDF document.
// |pdf_buffer| is the buffer that contains the entire PDF document to be
// rendered.
// |pdf_buffer_size| is the size of |pdf_buffer| in bytes.
// |page_number| is the 0-based index of the page to be rendered.
// |bitmap_buffer| is the output buffer for bitmap.
// |bitmap_width| is the width of the output bitmap.
@ -118,7 +118,8 @@ bool GetPDFPageSizeByIndex(void* pdf_handle,
// |autorotate| specifies whether the final image should be rotated to match
// the output bound.
// Returns false if the document or the page number are not valid.
bool RenderPDFPageToBitmap(void* pdf_handle,
bool RenderPDFPageToBitmap(const void* pdf_buffer,
int pdf_buffer_size,
int page_number,
void* bitmap_buffer,
int bitmap_width,

@ -333,8 +333,9 @@ class PDFEngineExports {
static PDFEngineExports* Get();
#if defined(OS_WIN)
// See the definitions of the corresponding functions in pdf.h for details.
virtual bool RenderPDFPageToDC(void* pdf_handle,
// See the definition of RenderPDFPageToDC in pdf.cc for details.
virtual bool RenderPDFPageToDC(const void* pdf_buffer,
int buffer_size,
int page_number,
const RenderingSettings& settings,
HDC dc) = 0;
@ -345,7 +346,9 @@ class PDFEngineExports {
virtual void SetPDFUseGDIPrinting(bool enable) = 0;
#endif // defined(OS_WIN)
virtual bool RenderPDFPageToBitmap(void* pdf_handle,
// See the definition of RenderPDFPageToBitmap in pdf.cc for details.
virtual bool RenderPDFPageToBitmap(const void* pdf_buffer,
int pdf_buffer_size,
int page_number,
const RenderingSettings& settings,
void* bitmap_buffer) = 0;
@ -353,12 +356,11 @@ class PDFEngineExports {
virtual bool GetPDFDocInfo(const void* pdf_buffer,
int buffer_size,
int* page_count,
double* max_page_width,
void** pdf_handle) = 0;
double* max_page_width) = 0;
virtual void ReleasePDFHandle(void* pdf_handle) = 0;
virtual bool GetPDFPageSizeByIndex(void* pdf_handle,
// See the definition of GetPDFPageSizeByIndex in pdf.cc for details.
virtual bool GetPDFPageSizeByIndex(const void* pdf_buffer,
int pdf_buffer_size,
int page_number,
double* width,
double* height) = 0;

@ -4007,17 +4007,19 @@ PDFEngineExports* PDFEngineExports::Get() {
}
#if defined(OS_WIN)
bool PDFiumEngineExports::RenderPDFPageToDC(void* pdf_handle,
bool PDFiumEngineExports::RenderPDFPageToDC(const void* pdf_buffer,
int buffer_size,
int page_number,
const RenderingSettings& settings,
HDC dc) {
FPDF_DOCUMENT doc = pdf_handle;
FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, buffer_size, nullptr);
if (!doc)
return false;
FPDF_PAGE page = FPDF_LoadPage(doc, page_number);
if (!page)
if (!page) {
FPDF_CloseDocument(doc);
return false;
}
RenderingSettings new_settings = settings;
// calculate the page size
if (new_settings.dpi_x == -1)
@ -4071,6 +4073,7 @@ bool PDFiumEngineExports::RenderPDFPageToDC(void* pdf_handle,
}
RestoreDC(dc, save_state);
FPDF_ClosePage(page);
FPDF_CloseDocument(doc);
return true;
}
@ -4086,16 +4089,20 @@ void PDFiumEngineExports::SetPDFUseGDIPrinting(bool enable) {
#endif // defined(OS_WIN)
bool PDFiumEngineExports::RenderPDFPageToBitmap(
void* pdf_handle,
const void* pdf_buffer,
int pdf_buffer_size,
int page_number,
const RenderingSettings& settings,
void* bitmap_buffer) {
FPDF_DOCUMENT doc = pdf_handle;
FPDF_DOCUMENT doc =
FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr);
if (!doc)
return false;
FPDF_PAGE page = FPDF_LoadPage(doc, page_number);
if (!page)
if (!page) {
FPDF_CloseDocument(doc);
return false;
}
pp::Rect dest;
int rotate = CalculatePosition(page, settings, &dest);
@ -4114,49 +4121,48 @@ bool PDFiumEngineExports::RenderPDFPageToBitmap(
FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
FPDFBitmap_Destroy(bitmap);
FPDF_ClosePage(page);
FPDF_CloseDocument(doc);
return true;
}
bool PDFiumEngineExports::GetPDFDocInfo(const void* pdf_buffer,
int buffer_size,
int* page_count,
double* max_page_width,
void** pdf_handle) {
double* max_page_width) {
FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, buffer_size, nullptr);
if (!doc)
return false;
int page_count_local = FPDF_GetPageCount(doc);
if (page_count)
if (page_count) {
*page_count = page_count_local;
}
if (max_page_width) {
*max_page_width = 0;
for (int page_number = 0; page_number < page_count_local; page_number++) {
double page_width = 0;
double page_height = 0;
FPDF_GetPageSizeByIndex(doc, page_number, &page_width, &page_height);
*max_page_width = std::max(*max_page_width, page_width);
if (page_width > *max_page_width) {
*max_page_width = page_width;
}
}
}
if (pdf_handle)
*pdf_handle = doc; // Caller takes ownership.
else
FPDF_CloseDocument(pdf_handle);
FPDF_CloseDocument(doc);
return true;
}
void PDFiumEngineExports::ReleasePDFHandle(void* pdf_handle) {
FPDF_CloseDocument(pdf_handle);
}
bool PDFiumEngineExports::GetPDFPageSizeByIndex(void* pdf_handle,
bool PDFiumEngineExports::GetPDFPageSizeByIndex(const void* pdf_buffer,
int pdf_buffer_size,
int page_number,
double* width,
double* height) {
FPDF_DOCUMENT doc = pdf_handle;
return doc && FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0;
FPDF_DOCUMENT doc =
FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr);
if (!doc)
return false;
bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0;
FPDF_CloseDocument(doc);
return success;
}
} // namespace chrome_pdf

@ -771,7 +771,8 @@ class PDFiumEngineExports : public PDFEngineExports {
// PDFEngineExports:
#if defined(OS_WIN)
bool RenderPDFPageToDC(void* pdf_handle,
bool RenderPDFPageToDC(const void* pdf_buffer,
int buffer_size,
int page_number,
const RenderingSettings& settings,
HDC dc) override;
@ -780,17 +781,17 @@ class PDFiumEngineExports : public PDFEngineExports {
void SetPDFUseGDIPrinting(bool enable) override;
#endif // defined(OS_WIN)
bool RenderPDFPageToBitmap(void* pdf_handle,
bool RenderPDFPageToBitmap(const void* pdf_buffer,
int pdf_buffer_size,
int page_number,
const RenderingSettings& settings,
void* bitmap_buffer) override;
bool GetPDFDocInfo(const void* pdf_buffer,
int buffer_size,
int* page_count,
double* max_page_width,
void** pdf_handle) override;
void ReleasePDFHandle(void* pdf_handle) override;
bool GetPDFPageSizeByIndex(void* pdf_handle,
double* max_page_width) override;
bool GetPDFPageSizeByIndex(const void* pdf_buffer,
int pdf_buffer_size,
int page_number,
double* width,
double* height) override;