OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/renderer/print_web_view_helper.h" | 5 #include "chrome/renderer/print_web_view_helper.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/process_util.h" | 10 #include "base/process_util.h" |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
83 DCHECK(metafile->context()); | 83 DCHECK(metafile->context()); |
84 skia::InitializeDC(metafile->context()); | 84 skia::InitializeDC(metafile->context()); |
85 | 85 |
86 int page_number = params.page_number; | 86 int page_number = params.page_number; |
87 | 87 |
88 // Calculate the dpi adjustment. | 88 // Calculate the dpi adjustment. |
89 float scale_factor = static_cast<float>(params.params.desired_dpi / | 89 float scale_factor = static_cast<float>(params.params.desired_dpi / |
90 params.params.dpi); | 90 params.params.dpi); |
91 | 91 |
92 // Render page for printing. | 92 // Render page for printing. |
93 RenderPage(params.params, &scale_factor, page_number, false, frame, | 93 metafile.reset(RenderPage(params.params, &scale_factor, page_number, false, |
94 &metafile); | 94 frame, metafile.get())); |
95 | 95 |
96 // Close the device context to retrieve the compiled metafile. | 96 // Close the device context to retrieve the compiled metafile. |
97 if (!metafile->FinishDocument()) | 97 if (!metafile->FinishDocument()) |
98 NOTREACHED(); | 98 NOTREACHED(); |
99 | 99 |
100 // Get the size of the compiled metafile. | 100 // Get the size of the compiled metafile. |
101 uint32 buf_size = metafile->GetDataSize(); | 101 uint32 buf_size = metafile->GetDataSize(); |
102 DCHECK_GT(buf_size, 128u); | 102 DCHECK_GT(buf_size, 128u); |
103 | 103 |
104 PrintHostMsg_DidPrintPage_Params page_params; | 104 PrintHostMsg_DidPrintPage_Params page_params; |
(...skipping 15 matching lines...) Expand all Loading... | |
120 | 120 |
121 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); | 121 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); |
122 } | 122 } |
123 | 123 |
124 void PrintWebViewHelper::RenderPreviewPage(int page_number) { | 124 void PrintWebViewHelper::RenderPreviewPage(int page_number) { |
125 PrintMsg_Print_Params print_params = print_preview_context_.print_params(); | 125 PrintMsg_Print_Params print_params = print_preview_context_.print_params(); |
126 // Calculate the dpi adjustment. | 126 // Calculate the dpi adjustment. |
127 float scale_factor = static_cast<float>(print_params.desired_dpi / | 127 float scale_factor = static_cast<float>(print_params.desired_dpi / |
128 print_params.dpi); | 128 print_params.dpi); |
129 | 129 |
130 // |metafile| is needed for RenderPage() below. |metafile| will not take the | |
131 // ownership of |print_preview_context_| metafile. | |
132 scoped_ptr<Metafile> metafile(print_preview_context_.metafile()); | |
133 | |
134 base::TimeTicks begin_time = base::TimeTicks::Now(); | 130 base::TimeTicks begin_time = base::TimeTicks::Now(); |
131 // In the preview flow, RenderPage will never return a new metafile. | |
Lei Zhang
2011/08/08 22:08:59
Should we add a DCHECK() to make sure this stays t
vandebo (ex-Chrome)
2011/08/08 22:11:29
Can we do that without introducing a warning? The
vandebo (ex-Chrome)
2011/08/09 00:22:47
Done.
| |
135 RenderPage(print_params, &scale_factor, page_number, true, | 132 RenderPage(print_params, &scale_factor, page_number, true, |
136 print_preview_context_.frame(), &metafile); | 133 print_preview_context_.frame(), print_preview_context_.metafile()); |
137 | 134 |
138 print_preview_context_.RenderedPreviewPage( | 135 print_preview_context_.RenderedPreviewPage( |
139 base::TimeTicks::Now() - begin_time); | 136 base::TimeTicks::Now() - begin_time); |
140 | 137 |
141 // Release since |print_preview_context_| is the real owner. | |
142 metafile.release(); | |
143 scoped_ptr<printing::Metafile> page_metafile; | 138 scoped_ptr<printing::Metafile> page_metafile; |
144 if (print_preview_context_.IsModifiable()) { | 139 if (print_preview_context_.IsModifiable()) { |
145 page_metafile.reset(reinterpret_cast<printing::PreviewMetafile*>( | 140 page_metafile.reset(reinterpret_cast<printing::PreviewMetafile*>( |
146 print_preview_context_.metafile())->GetMetafileForCurrentPage()); | 141 print_preview_context_.metafile())->GetMetafileForCurrentPage()); |
147 } | 142 } |
148 PreviewPageRendered(page_number, page_metafile.get()); | 143 PreviewPageRendered(page_number, page_metafile.get()); |
149 } | 144 } |
150 | 145 |
151 void PrintWebViewHelper::RenderPage( | 146 Metafile* PrintWebViewHelper::RenderPage( |
152 const PrintMsg_Print_Params& params, float* scale_factor, int page_number, | 147 const PrintMsg_Print_Params& params, float* scale_factor, int page_number, |
153 bool is_preview, WebFrame* frame, scoped_ptr<Metafile>* metafile) { | 148 bool is_preview, WebFrame* frame, Metafile* metafile) { |
154 PageSizeMargins page_layout_in_points; | 149 PageSizeMargins page_layout_in_points; |
155 GetPageSizeAndMarginsInPoints(frame, page_number, params, | 150 GetPageSizeAndMarginsInPoints(frame, page_number, params, |
156 &page_layout_in_points); | 151 &page_layout_in_points); |
157 | 152 |
158 int width; | 153 int width; |
159 int height; | 154 int height; |
160 if (is_preview) { | 155 if (is_preview) { |
161 int dpi = static_cast<int>(params.dpi); | 156 int dpi = static_cast<int>(params.dpi); |
162 int desired_dpi = printing::kPointsPerInch; | 157 int desired_dpi = printing::kPointsPerInch; |
163 width = ConvertUnit(params.page_size.width(), dpi, desired_dpi); | 158 width = ConvertUnit(params.page_size.width(), dpi, desired_dpi); |
164 height = ConvertUnit(params.page_size.height(), dpi, desired_dpi); | 159 height = ConvertUnit(params.page_size.height(), dpi, desired_dpi); |
165 } else { | 160 } else { |
166 // Since WebKit extends the page width depending on the magical scale factor | 161 // Since WebKit extends the page width depending on the magical scale factor |
167 // we make sure the canvas covers the worst case scenario (x2.0 currently). | 162 // we make sure the canvas covers the worst case scenario (x2.0 currently). |
168 // PrintContext will then set the correct clipping region. | 163 // PrintContext will then set the correct clipping region. |
169 width = static_cast<int>(page_layout_in_points.content_width * | 164 width = static_cast<int>(page_layout_in_points.content_width * |
170 params.max_shrink); | 165 params.max_shrink); |
171 height = static_cast<int>(page_layout_in_points.content_height * | 166 height = static_cast<int>(page_layout_in_points.content_height * |
172 params.max_shrink); | 167 params.max_shrink); |
173 } | 168 } |
174 | 169 |
175 gfx::Size page_size(width, height); | 170 gfx::Size page_size(width, height); |
176 gfx::Rect content_area( | 171 gfx::Rect content_area( |
177 static_cast<int>(page_layout_in_points.margin_left), | 172 static_cast<int>(page_layout_in_points.margin_left), |
178 static_cast<int>(page_layout_in_points.margin_top), | 173 static_cast<int>(page_layout_in_points.margin_top), |
179 static_cast<int>(page_layout_in_points.content_width), | 174 static_cast<int>(page_layout_in_points.content_width), |
180 static_cast<int>(page_layout_in_points.content_height)); | 175 static_cast<int>(page_layout_in_points.content_height)); |
181 SkDevice* device = (*metafile)->StartPageForVectorCanvas( | 176 SkDevice* device = metafile->StartPageForVectorCanvas( |
182 page_size, content_area, frame->getPrintPageShrink(page_number)); | 177 page_size, content_area, frame->getPrintPageShrink(page_number)); |
183 DCHECK(device); | 178 DCHECK(device); |
184 // The printPage method may take a reference to the canvas we pass down, so it | 179 // The printPage method may take a reference to the canvas we pass down, so it |
185 // can't be a stack object. | 180 // can't be a stack object. |
186 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); | 181 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); |
187 canvas->unref(); // SkRefPtr and new both took a reference. | 182 canvas->unref(); // SkRefPtr and new both took a reference. |
188 if (is_preview) { | 183 if (is_preview) |
189 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas.get(), | 184 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas.get(), metafile); |
190 metafile->get()); | |
191 } | |
192 | 185 |
193 float webkit_scale_factor = frame->printPage(page_number, canvas.get()); | 186 float webkit_scale_factor = frame->printPage(page_number, canvas.get()); |
194 if (*scale_factor <= 0 || webkit_scale_factor <= 0) { | 187 if (*scale_factor <= 0 || webkit_scale_factor <= 0) { |
195 NOTREACHED() << "Printing page " << page_number << " failed."; | 188 NOTREACHED() << "Printing page " << page_number << " failed."; |
196 } else { | 189 } else { |
197 // Update the dpi adjustment with the "page |scale_factor|" calculated in | 190 // Update the dpi adjustment with the "page |scale_factor|" calculated in |
198 // webkit. | 191 // webkit. |
199 *scale_factor /= webkit_scale_factor; | 192 *scale_factor /= webkit_scale_factor; |
200 } | 193 } |
201 | 194 |
202 bool result = (*metafile)->FinishPage(); | 195 bool result = metafile->FinishPage(); |
203 DCHECK(result); | 196 DCHECK(result); |
204 | 197 |
205 if (!params.supports_alpha_blend) { | 198 if (!params.supports_alpha_blend) { |
206 // PreviewMetafile (PDF) supports alpha blend, so we only hit this case | 199 // PreviewMetafile (PDF) supports alpha blend, so we only hit this case |
207 // for NativeMetafile. | 200 // for NativeMetafile. |
208 DCHECK(!is_preview); | 201 DCHECK(!is_preview); |
209 skia::PlatformDevice* platform_device = skia::GetPlatformDevice(device); | 202 skia::PlatformDevice* platform_device = skia::GetPlatformDevice(device); |
210 if (platform_device && platform_device->AlphaBlendUsed()) { | 203 if (platform_device && platform_device->AlphaBlendUsed()) { |
211 // Currently, we handle alpha blend transparency for a single page. | 204 // Currently, we handle alpha blend transparency for a single page. |
212 // Therefore, expecting a metafile with page count 1. | 205 // Therefore, expecting a metafile with page count 1. |
213 DCHECK_EQ(1U, (*metafile)->GetPageCount()); | 206 DCHECK_EQ(1U, metafile->GetPageCount()); |
214 | 207 |
215 // Close the device context to retrieve the compiled metafile. | 208 // Close the device context to retrieve the compiled metafile. |
216 if (!(*metafile)->FinishDocument()) | 209 if (!metafile->FinishDocument()) |
217 NOTREACHED(); | 210 NOTREACHED(); |
218 | 211 |
219 // Page used alpha blend, but printer doesn't support it. Rewrite the | 212 // Page used alpha blend, but printer doesn't support it. Rewrite the |
220 // metafile and flatten out the transparency. | 213 // metafile and flatten out the transparency. |
221 HDC bitmap_dc = CreateCompatibleDC(GetDC(NULL)); | 214 HDC bitmap_dc = CreateCompatibleDC(GetDC(NULL)); |
222 if (!bitmap_dc) | 215 if (!bitmap_dc) |
223 NOTREACHED() << "Bitmap DC creation failed"; | 216 NOTREACHED() << "Bitmap DC creation failed"; |
224 SetGraphicsMode(bitmap_dc, GM_ADVANCED); | 217 SetGraphicsMode(bitmap_dc, GM_ADVANCED); |
225 void* bits = NULL; | 218 void* bits = NULL; |
226 BITMAPINFO hdr; | 219 BITMAPINFO hdr; |
227 gfx::CreateBitmapHeader(width, height, &hdr.bmiHeader); | 220 gfx::CreateBitmapHeader(width, height, &hdr.bmiHeader); |
228 HBITMAP hbitmap = CreateDIBSection( | 221 HBITMAP hbitmap = CreateDIBSection( |
229 bitmap_dc, &hdr, DIB_RGB_COLORS, &bits, NULL, 0); | 222 bitmap_dc, &hdr, DIB_RGB_COLORS, &bits, NULL, 0); |
230 if (!hbitmap) | 223 if (!hbitmap) |
231 NOTREACHED() << "Raster bitmap creation for printing failed"; | 224 NOTREACHED() << "Raster bitmap creation for printing failed"; |
232 | 225 |
233 HGDIOBJ old_bitmap = SelectObject(bitmap_dc, hbitmap); | 226 HGDIOBJ old_bitmap = SelectObject(bitmap_dc, hbitmap); |
234 RECT rect = {0, 0, width, height }; | 227 RECT rect = {0, 0, width, height }; |
235 HBRUSH whiteBrush = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)); | 228 HBRUSH whiteBrush = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)); |
236 FillRect(bitmap_dc, &rect, whiteBrush); | 229 FillRect(bitmap_dc, &rect, whiteBrush); |
237 | 230 |
238 scoped_ptr<Metafile> metafile2(new printing::NativeMetafile); | 231 Metafile* metafile2(new printing::NativeMetafile); |
239 metafile2->Init(); | 232 metafile2->Init(); |
240 HDC hdc = metafile2->context(); | 233 HDC hdc = metafile2->context(); |
241 DCHECK(hdc); | 234 DCHECK(hdc); |
242 skia::InitializeDC(hdc); | 235 skia::InitializeDC(hdc); |
243 | 236 |
244 RECT metafile_bounds = (*metafile)->GetPageBounds(1).ToRECT(); | 237 RECT metafile_bounds = metafile->GetPageBounds(1).ToRECT(); |
245 // Process the old metafile, placing all non-AlphaBlend calls into the | 238 // Process the old metafile, placing all non-AlphaBlend calls into the |
246 // new metafile, and copying the results of all the AlphaBlend calls | 239 // new metafile, and copying the results of all the AlphaBlend calls |
247 // from the bitmap DC. | 240 // from the bitmap DC. |
248 EnumEnhMetaFile(hdc, | 241 EnumEnhMetaFile(hdc, |
249 (*metafile)->emf(), | 242 metafile->emf(), |
250 EnhMetaFileProc, | 243 EnhMetaFileProc, |
251 &bitmap_dc, | 244 &bitmap_dc, |
252 &metafile_bounds); | 245 &metafile_bounds); |
253 | 246 |
254 SelectObject(bitmap_dc, old_bitmap); | 247 SelectObject(bitmap_dc, old_bitmap); |
255 metafile->reset(metafile2.release()); | 248 return metafile2; |
256 } | 249 } |
257 } | 250 } |
251 return metafile; | |
258 } | 252 } |
259 | 253 |
260 bool PrintWebViewHelper::CopyMetafileDataToSharedMem( | 254 bool PrintWebViewHelper::CopyMetafileDataToSharedMem( |
261 Metafile* metafile, base::SharedMemoryHandle* shared_mem_handle) { | 255 Metafile* metafile, base::SharedMemoryHandle* shared_mem_handle) { |
262 uint32 buf_size = metafile->GetDataSize(); | 256 uint32 buf_size = metafile->GetDataSize(); |
263 base::SharedMemory shared_buf; | 257 base::SharedMemory shared_buf; |
264 // http://msdn2.microsoft.com/en-us/library/ms535522.aspx | 258 // http://msdn2.microsoft.com/en-us/library/ms535522.aspx |
265 // Windows 2000/XP: When a page in a spooled file exceeds approximately 350 | 259 // Windows 2000/XP: When a page in a spooled file exceeds approximately 350 |
266 // MB, it can fail to print and not send an error message. | 260 // MB, it can fail to print and not send an error message. |
267 if (buf_size >= 350*1024*1024) { | 261 if (buf_size >= 350*1024*1024) { |
(...skipping 13 matching lines...) Expand all Loading... | |
281 shared_buf.Unmap(); | 275 shared_buf.Unmap(); |
282 return false; | 276 return false; |
283 } | 277 } |
284 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); | 278 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); |
285 shared_buf.Unmap(); | 279 shared_buf.Unmap(); |
286 | 280 |
287 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, | 281 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, |
288 shared_mem_handle)); | 282 shared_mem_handle)); |
289 return true; | 283 return true; |
290 } | 284 } |
OLD | NEW |