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
3 // found in the LICENSE file.
5 #include "printing/pdf_metafile_skia.h"
7 #include "base/containers/hash_tables.h"
8 #include "base/files/file_util.h"
9 #include "base/metrics/histogram.h"
10 #include "base/numerics/safe_conversions.h"
11 #include "base/posix/eintr_wrapper.h"
12 #include "skia/ext/refptr.h"
13 #include "skia/ext/vector_canvas.h"
14 #include "third_party/skia/include/core/SkData.h"
15 #include "third_party/skia/include/core/SkDocument.h"
16 #include "third_party/skia/include/core/SkPictureRecorder.h"
17 #include "third_party/skia/include/core/SkRect.h"
18 #include "third_party/skia/include/core/SkRefCnt.h"
19 #include "third_party/skia/include/core/SkScalar.h"
20 #include "third_party/skia/include/core/SkSize.h"
21 #include "third_party/skia/include/core/SkStream.h"
22 #include "ui/gfx/geometry/point.h"
23 #include "ui/gfx/geometry/rect.h"
24 #include "ui/gfx/geometry/size.h"
25 #include "ui/gfx/geometry/size_conversions.h"
26 #include "ui/gfx/skia_util.h"
28 #if defined(OS_MACOSX)
29 #include "printing/pdf_metafile_cg_mac.h"
33 #include "base/file_descriptor_posix.h"
37 // This struct represents all the data we need to draw and redraw this
38 // page into a SkDocument.
40 Page(const SkSize
& page_size
, const SkRect
& content_area
)
41 : page_size_(page_size
),
42 content_area_(content_area
),
46 skia::RefPtr
<SkPicture
> content_
;
50 static bool WriteAssetToBuffer(const SkStreamAsset
* asset
,
53 // Calling duplicate() keeps original asset state unchanged.
54 scoped_ptr
<SkStreamAsset
> assetCopy(asset
->duplicate());
55 size_t length
= assetCopy
->getLength();
58 return (length
== assetCopy
->read(buffer
, length
));
63 struct PdfMetafileSkiaData
{
64 SkPictureRecorder recorder_
; // Current recording
66 std::vector
<Page
> pages_
;
67 scoped_ptr
<SkStreamAsset
> pdf_data_
;
69 #if defined(OS_MACOSX)
70 PdfMetafileCg pdf_cg_
;
74 PdfMetafileSkia::~PdfMetafileSkia() {}
76 bool PdfMetafileSkia::Init() {
80 // TODO(halcanary): Create a Metafile class that only stores data.
81 // Metafile::InitFromData is orthogonal to what the rest of
82 // PdfMetafileSkia does.
83 bool PdfMetafileSkia::InitFromData(const void* src_buffer
,
84 uint32 src_buffer_size
) {
85 data_
->pdf_data_
.reset(new SkMemoryStream(src_buffer
, src_buffer_size
, true));
89 bool PdfMetafileSkia::StartPage(const gfx::Size
& page_size
,
90 const gfx::Rect
& content_area
,
91 const float& scale_factor
) {
92 if (data_
->recorder_
.getRecordingCanvas())
94 DCHECK(!data_
->recorder_
.getRecordingCanvas());
95 SkSize sk_page_size
= gfx::SizeFToSkSize(gfx::SizeF(page_size
));
96 data_
->pages_
.push_back(Page(sk_page_size
, gfx::RectToSkRect(content_area
)));
98 SkCanvas
* recordingCanvas
= data_
->recorder_
.beginRecording(
99 sk_page_size
.width(), sk_page_size
.height(), NULL
, 0);
100 // recordingCanvas is owned by the data_->recorder_. No ref() necessary.
101 if (!recordingCanvas
)
103 recordingCanvas
->scale(scale_factor
, scale_factor
);
107 skia::VectorCanvas
* PdfMetafileSkia::GetVectorCanvasForNewPage(
108 const gfx::Size
& page_size
,
109 const gfx::Rect
& content_area
,
110 const float& scale_factor
) {
111 if (!StartPage(page_size
, content_area
, scale_factor
))
113 return data_
->recorder_
.getRecordingCanvas();
116 bool PdfMetafileSkia::FinishPage() {
117 if (!data_
->recorder_
.getRecordingCanvas())
119 DCHECK(!(data_
->pages_
.back().content_
));
120 data_
->pages_
.back().content_
=
121 skia::AdoptRef(data_
->recorder_
.endRecording());
125 bool PdfMetafileSkia::FinishDocument() {
126 // If we've already set the data in InitFromData, leave it be.
127 if (data_
->pdf_data_
)
130 if (data_
->recorder_
.getRecordingCanvas())
133 SkDynamicMemoryWStream pdf_stream
;
134 skia::RefPtr
<SkDocument
> pdf_doc
=
135 skia::AdoptRef(SkDocument::CreatePDF(&pdf_stream
));
136 for (const auto& page
: data_
->pages_
) {
137 SkCanvas
* canvas
= pdf_doc
->beginPage(
138 page
.page_size_
.width(), page
.page_size_
.height(), &page
.content_area_
);
139 canvas
->drawPicture(page
.content_
.get());
142 if (!pdf_doc
->close())
145 data_
->pdf_data_
.reset(pdf_stream
.detachAsStream());
149 uint32
PdfMetafileSkia::GetDataSize() const {
150 if (!data_
->pdf_data_
)
152 return base::checked_cast
<uint32
>(data_
->pdf_data_
->getLength());
155 bool PdfMetafileSkia::GetData(void* dst_buffer
,
156 uint32 dst_buffer_size
) const {
157 if (!data_
->pdf_data_
)
159 return WriteAssetToBuffer(data_
->pdf_data_
.get(), dst_buffer
,
160 base::checked_cast
<size_t>(dst_buffer_size
));
163 gfx::Rect
PdfMetafileSkia::GetPageBounds(unsigned int page_number
) const {
164 if (page_number
< data_
->pages_
.size()) {
165 return gfx::Rect(gfx::ToFlooredSize(
166 gfx::SkSizeToSizeF(data_
->pages_
[page_number
].page_size_
)));
171 unsigned int PdfMetafileSkia::GetPageCount() const {
172 return base::checked_cast
<unsigned int>(data_
->pages_
.size());
175 gfx::NativeDrawingContext
PdfMetafileSkia::context() const {
181 bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc
,
182 const RECT
* rect
) const {
187 bool PdfMetafileSkia::SafePlayback(gfx::NativeDrawingContext hdc
) const {
192 #elif defined(OS_MACOSX)
193 /* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in
194 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage,
195 the drawing of the PDF into the canvas may result in a rasterized output.
196 PDFMetafileSkia::RenderPage should be not implemented as shown and instead
197 should do something like the following CL in PluginInstance::PrintPDFOutput:
198 http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_instance.cc
200 bool PdfMetafileSkia::RenderPage(unsigned int page_number
,
201 CGContextRef context
,
203 const MacRenderPageParams
& params
) const {
204 DCHECK_GT(GetDataSize(), 0U);
205 if (data_
->pdf_cg_
.GetDataSize() == 0) {
206 if (GetDataSize() == 0)
208 size_t length
= data_
->pdf_data_
->getLength();
209 std::vector
<uint8_t> buffer(length
);
210 (void)WriteAssetToBuffer(data_
->pdf_data_
.get(), &buffer
[0], length
);
211 data_
->pdf_cg_
.InitFromData(&buffer
[0], base::checked_cast
<uint32
>(length
));
213 return data_
->pdf_cg_
.RenderPage(page_number
, context
, rect
, params
);
217 bool PdfMetafileSkia::SaveTo(base::File
* file
) const {
218 if (GetDataSize() == 0U)
221 // Calling duplicate() keeps original asset state unchanged.
222 scoped_ptr
<SkStreamAsset
> asset(data_
->pdf_data_
->duplicate());
224 const size_t maximum_buffer_size
= 1024 * 1024;
225 std::vector
<char> buffer(std::min(maximum_buffer_size
, asset
->getLength()));
227 size_t read_size
= asset
->read(&buffer
[0], buffer
.size());
230 DCHECK_GE(buffer
.size(), read_size
);
231 if (!file
->WriteAtCurrentPos(&buffer
[0],
232 base::checked_cast
<int>(read_size
))) {
235 } while (!asset
->isAtEnd());
240 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
241 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor
& fd
) const {
242 DCHECK_GT(GetDataSize(), 0U);
245 DLOG(ERROR
) << "Invalid file descriptor!";
248 base::File
file(fd
.fd
);
249 bool result
= SaveTo(&file
);
250 DLOG_IF(ERROR
, !result
) << "Failed to save file with fd " << fd
.fd
;
253 file
.TakePlatformFile();
258 PdfMetafileSkia::PdfMetafileSkia() : data_(new PdfMetafileSkiaData
) {
261 scoped_ptr
<PdfMetafileSkia
> PdfMetafileSkia::GetMetafileForCurrentPage() {
262 // If we only ever need the metafile for the last page, should we
263 // only keep a handle on one SkPicture?
264 scoped_ptr
<PdfMetafileSkia
> metafile(new PdfMetafileSkia
);
266 if (data_
->pages_
.size() == 0)
267 return metafile
.Pass();
269 if (data_
->recorder_
.getRecordingCanvas()) // page outstanding
270 return metafile
.Pass();
272 const Page
& page
= data_
->pages_
.back();
274 metafile
->data_
->pages_
.push_back(page
);
276 if (!metafile
->FinishDocument()) // Generate PDF.
279 return metafile
.Pass();
282 } // namespace printing