Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(260)

Side by Side Diff: printing/pdf_metafile_skia.cc

Issue 2802093006: printing::Metafile: Simplify OS_MACOSX-specific code path (Closed)
Patch Set: vectoriz Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « printing/pdf_metafile_skia.h ('k') | printing/printed_document_mac.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "printing/pdf_metafile_skia.h" 5 #include "printing/pdf_metafile_skia.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/files/file.h" 12 #include "base/files/file.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/time/time.h" 14 #include "base/time/time.h"
15 #include "cc/paint/paint_canvas.h" 15 #include "cc/paint/paint_canvas.h"
16 #include "cc/paint/paint_record.h" 16 #include "cc/paint/paint_record.h"
17 #include "cc/paint/paint_recorder.h" 17 #include "cc/paint/paint_recorder.h"
18 #include "printing/print_settings.h" 18 #include "printing/print_settings.h"
19 #include "third_party/skia/include/core/SkDocument.h" 19 #include "third_party/skia/include/core/SkDocument.h"
20 #include "third_party/skia/include/core/SkStream.h" 20 #include "third_party/skia/include/core/SkStream.h"
21 // Note that headers in third_party/skia/src are fragile. This is 21 // Note that headers in third_party/skia/src are fragile. This is
22 // an experimental, fragile, and diagnostic-only document type. 22 // an experimental, fragile, and diagnostic-only document type.
23 #include "third_party/skia/src/utils/SkMultiPictureDocument.h" 23 #include "third_party/skia/src/utils/SkMultiPictureDocument.h"
24 #include "ui/gfx/geometry/safe_integer_conversions.h" 24 #include "ui/gfx/geometry/safe_integer_conversions.h"
25 #include "ui/gfx/skia_util.h" 25 #include "ui/gfx/skia_util.h"
26 26
27 #if defined(OS_MACOSX)
28 #include "printing/pdf_metafile_cg_mac.h"
29 #endif
30
31 #if defined(OS_POSIX)
32 #include "base/file_descriptor_posix.h"
33 #endif
34
35 namespace { 27 namespace {
36 28
37 bool WriteAssetToBuffer(const SkStreamAsset* asset, 29 bool WriteAssetToBuffer(const SkStreamAsset* asset,
38 void* buffer, 30 void* buffer,
39 size_t size) { 31 size_t size) {
40 // Calling duplicate() keeps original asset state unchanged. 32 // Calling duplicate() keeps original asset state unchanged.
41 std::unique_ptr<SkStreamAsset> assetCopy(asset->duplicate()); 33 std::unique_ptr<SkStreamAsset> assetCopy(asset->duplicate());
42 size_t length = assetCopy->getLength(); 34 size_t length = assetCopy->getLength();
43 if (length > size) 35 if (length > size)
44 return false; 36 return false;
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 89
98 std::vector<Page> pages_; 90 std::vector<Page> pages_;
99 std::unique_ptr<SkStreamAsset> pdf_data_; 91 std::unique_ptr<SkStreamAsset> pdf_data_;
100 92
101 // The scale factor is used because Blink occasionally calls 93 // The scale factor is used because Blink occasionally calls
102 // PaintCanvas::getTotalMatrix() even though the total matrix is not as 94 // PaintCanvas::getTotalMatrix() even though the total matrix is not as
103 // meaningful for a vector canvas as for a raster canvas. 95 // meaningful for a vector canvas as for a raster canvas.
104 float scale_factor_; 96 float scale_factor_;
105 SkSize size_; 97 SkSize size_;
106 SkiaDocumentType type_; 98 SkiaDocumentType type_;
107
108 #if defined(OS_MACOSX)
109 PdfMetafileCg pdf_cg_;
110 #endif
111 }; 99 };
112 100
113 PdfMetafileSkia::~PdfMetafileSkia() {} 101 PdfMetafileSkia::~PdfMetafileSkia() {}
114 102
115 bool PdfMetafileSkia::Init() { 103 bool PdfMetafileSkia::Init() {
116 return true; 104 return true;
117 } 105 }
118 106
119 // TODO(halcanary): Create a Metafile class that only stores data. 107 // TODO(halcanary): Create a Metafile class that only stores data.
120 // Metafile::InitFromData is orthogonal to what the rest of 108 // Metafile::InitFromData is orthogonal to what the rest of
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 bool PdfMetafileSkia::Playback(skia::NativeDrawingContext hdc, 236 bool PdfMetafileSkia::Playback(skia::NativeDrawingContext hdc,
249 const RECT* rect) const { 237 const RECT* rect) const {
250 NOTREACHED(); 238 NOTREACHED();
251 return false; 239 return false;
252 } 240 }
253 241
254 bool PdfMetafileSkia::SafePlayback(skia::NativeDrawingContext hdc) const { 242 bool PdfMetafileSkia::SafePlayback(skia::NativeDrawingContext hdc) const {
255 NOTREACHED(); 243 NOTREACHED();
256 return false; 244 return false;
257 } 245 }
258
259 #elif defined(OS_MACOSX)
260 /* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in
261 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage,
262 the drawing of the PDF into the canvas may result in a rasterized output.
263 PDFMetafileSkia::RenderPage should be not implemented as shown and instead
264 should do something like the following CL in PluginInstance::PrintPDFOutput:
265 http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_ instance.cc
266 */
267 bool PdfMetafileSkia::RenderPage(unsigned int page_number,
268 CGContextRef context,
269 const CGRect rect,
270 const MacRenderPageParams& params) const {
271 DCHECK_GT(GetDataSize(), 0U);
272 if (data_->pdf_cg_.GetDataSize() == 0) {
273 if (GetDataSize() == 0)
274 return false;
275 size_t length = data_->pdf_data_->getLength();
276 std::vector<uint8_t> buffer(length);
277 (void)WriteAssetToBuffer(data_->pdf_data_.get(), &buffer[0], length);
278 data_->pdf_cg_.InitFromData(&buffer[0], length);
279 }
280 return data_->pdf_cg_.RenderPage(page_number, context, rect, params);
281 }
282 #endif 246 #endif
283 247
284 bool PdfMetafileSkia::SaveTo(base::File* file) const { 248 bool PdfMetafileSkia::SaveTo(base::File* file) const {
285 if (GetDataSize() == 0U) 249 if (GetDataSize() == 0U)
286 return false; 250 return false;
287 251
288 // Calling duplicate() keeps original asset state unchanged. 252 // Calling duplicate() keeps original asset state unchanged.
289 std::unique_ptr<SkStreamAsset> asset(data_->pdf_data_->duplicate()); 253 std::unique_ptr<SkStreamAsset> asset(data_->pdf_data_->duplicate());
290 254
291 const size_t kMaximumBufferSize = 1024 * 1024; 255 const size_t kMaximumBufferSize = 1024 * 1024;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 287
324 metafile->data_->pages_.push_back(data_->pages_.back()); 288 metafile->data_->pages_.push_back(data_->pages_.back());
325 289
326 if (!metafile->FinishDocument()) // Generate PDF. 290 if (!metafile->FinishDocument()) // Generate PDF.
327 metafile.reset(); 291 metafile.reset();
328 292
329 return metafile; 293 return metafile;
330 } 294 }
331 295
332 } // namespace printing 296 } // namespace printing
OLDNEW
« no previous file with comments | « printing/pdf_metafile_skia.h ('k') | printing/printed_document_mac.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698