Revert of Adding StringPiece read/write support to pickle. (patchset #4 id:60001...
[chromium-blink-merge.git] / printing / pdf_metafile_skia.cc
blobef8e3750a6298d6f320de4a5c2017d1b62521fef
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 "third_party/skia/include/core/SkData.h"
14 #include "third_party/skia/include/core/SkDocument.h"
15 #include "third_party/skia/include/core/SkPictureRecorder.h"
16 #include "third_party/skia/include/core/SkRect.h"
17 #include "third_party/skia/include/core/SkRefCnt.h"
18 #include "third_party/skia/include/core/SkScalar.h"
19 #include "third_party/skia/include/core/SkSize.h"
20 #include "third_party/skia/include/core/SkStream.h"
21 #include "ui/gfx/geometry/point.h"
22 #include "ui/gfx/geometry/rect.h"
23 #include "ui/gfx/geometry/size.h"
24 #include "ui/gfx/geometry/size_conversions.h"
25 #include "ui/gfx/skia_util.h"
27 #if defined(OS_MACOSX)
28 #include "printing/pdf_metafile_cg_mac.h"
29 #endif
31 #if defined(OS_POSIX)
32 #include "base/file_descriptor_posix.h"
33 #endif
35 namespace {
36 // This struct represents all the data we need to draw and redraw this
37 // page into a SkDocument.
38 struct Page {
39 Page(const SkSize& page_size, const SkRect& content_area)
40 : page_size_(page_size),
41 content_area_(content_area),
42 content_(/*NULL*/) {}
43 SkSize page_size_;
44 SkRect content_area_;
45 skia::RefPtr<SkPicture> content_;
47 } // namespace
49 static bool WriteAssetToBuffer(const SkStreamAsset* asset,
50 void* buffer,
51 size_t size) {
52 // Calling duplicate() keeps original asset state unchanged.
53 scoped_ptr<SkStreamAsset> assetCopy(asset->duplicate());
54 size_t length = assetCopy->getLength();
55 if (length > size)
56 return false;
57 return (length == assetCopy->read(buffer, length));
60 namespace printing {
62 struct PdfMetafileSkiaData {
63 SkPictureRecorder recorder_; // Current recording
65 std::vector<Page> pages_;
66 scoped_ptr<SkStreamAsset> pdf_data_;
68 #if defined(OS_MACOSX)
69 PdfMetafileCg pdf_cg_;
70 #endif
73 PdfMetafileSkia::~PdfMetafileSkia() {}
75 bool PdfMetafileSkia::Init() {
76 return true;
79 // TODO(halcanary): Create a Metafile class that only stores data.
80 // Metafile::InitFromData is orthogonal to what the rest of
81 // PdfMetafileSkia does.
82 bool PdfMetafileSkia::InitFromData(const void* src_buffer,
83 uint32 src_buffer_size) {
84 data_->pdf_data_.reset(new SkMemoryStream(src_buffer, src_buffer_size, true));
85 return true;
88 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size,
89 const gfx::Rect& content_area,
90 const float& scale_factor) {
91 if (data_->recorder_.getRecordingCanvas())
92 this->FinishPage();
93 DCHECK(!data_->recorder_.getRecordingCanvas());
94 SkSize sk_page_size = gfx::SizeFToSkSize(gfx::SizeF(page_size));
95 data_->pages_.push_back(Page(sk_page_size, gfx::RectToSkRect(content_area)));
97 SkCanvas* recordingCanvas = data_->recorder_.beginRecording(
98 sk_page_size.width(), sk_page_size.height(), NULL, 0);
99 // recordingCanvas is owned by the data_->recorder_. No ref() necessary.
100 if (!recordingCanvas)
101 return false;
102 recordingCanvas->scale(scale_factor, scale_factor);
103 return true;
106 skia::PlatformCanvas* PdfMetafileSkia::GetVectorCanvasForNewPage(
107 const gfx::Size& page_size,
108 const gfx::Rect& content_area,
109 const float& scale_factor) {
110 if (!StartPage(page_size, content_area, scale_factor))
111 return nullptr;
112 return data_->recorder_.getRecordingCanvas();
115 bool PdfMetafileSkia::FinishPage() {
116 if (!data_->recorder_.getRecordingCanvas())
117 return false;
118 DCHECK(!(data_->pages_.back().content_));
119 data_->pages_.back().content_ =
120 skia::AdoptRef(data_->recorder_.endRecording());
121 return true;
124 bool PdfMetafileSkia::FinishDocument() {
125 // If we've already set the data in InitFromData, leave it be.
126 if (data_->pdf_data_)
127 return false;
129 if (data_->recorder_.getRecordingCanvas())
130 this->FinishPage();
132 SkDynamicMemoryWStream pdf_stream;
133 skia::RefPtr<SkDocument> pdf_doc =
134 skia::AdoptRef(SkDocument::CreatePDF(&pdf_stream));
135 for (const auto& page : data_->pages_) {
136 SkCanvas* canvas = pdf_doc->beginPage(
137 page.page_size_.width(), page.page_size_.height(), &page.content_area_);
138 canvas->drawPicture(page.content_.get());
139 pdf_doc->endPage();
141 if (!pdf_doc->close())
142 return false;
144 data_->pdf_data_.reset(pdf_stream.detachAsStream());
145 return true;
148 uint32 PdfMetafileSkia::GetDataSize() const {
149 if (!data_->pdf_data_)
150 return 0;
151 return base::checked_cast<uint32>(data_->pdf_data_->getLength());
154 bool PdfMetafileSkia::GetData(void* dst_buffer,
155 uint32 dst_buffer_size) const {
156 if (!data_->pdf_data_)
157 return false;
158 return WriteAssetToBuffer(data_->pdf_data_.get(), dst_buffer,
159 base::checked_cast<size_t>(dst_buffer_size));
162 gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const {
163 if (page_number < data_->pages_.size()) {
164 return gfx::Rect(gfx::ToFlooredSize(
165 gfx::SkSizeToSizeF(data_->pages_[page_number].page_size_)));
167 return gfx::Rect();
170 unsigned int PdfMetafileSkia::GetPageCount() const {
171 return base::checked_cast<unsigned int>(data_->pages_.size());
174 gfx::NativeDrawingContext PdfMetafileSkia::context() const {
175 NOTREACHED();
176 return NULL;
179 #if defined(OS_WIN)
180 bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc,
181 const RECT* rect) const {
182 NOTREACHED();
183 return false;
186 bool PdfMetafileSkia::SafePlayback(gfx::NativeDrawingContext hdc) const {
187 NOTREACHED();
188 return false;
191 #elif defined(OS_MACOSX)
192 /* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in
193 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage,
194 the drawing of the PDF into the canvas may result in a rasterized output.
195 PDFMetafileSkia::RenderPage should be not implemented as shown and instead
196 should do something like the following CL in PluginInstance::PrintPDFOutput:
197 http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_instance.cc
199 bool PdfMetafileSkia::RenderPage(unsigned int page_number,
200 CGContextRef context,
201 const CGRect rect,
202 const MacRenderPageParams& params) const {
203 DCHECK_GT(GetDataSize(), 0U);
204 if (data_->pdf_cg_.GetDataSize() == 0) {
205 if (GetDataSize() == 0)
206 return false;
207 size_t length = data_->pdf_data_->getLength();
208 std::vector<uint8_t> buffer(length);
209 (void)WriteAssetToBuffer(data_->pdf_data_.get(), &buffer[0], length);
210 data_->pdf_cg_.InitFromData(&buffer[0], base::checked_cast<uint32>(length));
212 return data_->pdf_cg_.RenderPage(page_number, context, rect, params);
214 #endif
216 bool PdfMetafileSkia::SaveTo(base::File* file) const {
217 if (GetDataSize() == 0U)
218 return false;
220 // Calling duplicate() keeps original asset state unchanged.
221 scoped_ptr<SkStreamAsset> asset(data_->pdf_data_->duplicate());
223 const size_t maximum_buffer_size = 1024 * 1024;
224 std::vector<char> buffer(std::min(maximum_buffer_size, asset->getLength()));
225 do {
226 size_t read_size = asset->read(&buffer[0], buffer.size());
227 if (read_size == 0)
228 break;
229 DCHECK_GE(buffer.size(), read_size);
230 if (!file->WriteAtCurrentPos(&buffer[0],
231 base::checked_cast<int>(read_size))) {
232 return false;
234 } while (!asset->isAtEnd());
236 return true;
239 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
240 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const {
241 DCHECK_GT(GetDataSize(), 0U);
243 if (fd.fd < 0) {
244 DLOG(ERROR) << "Invalid file descriptor!";
245 return false;
247 base::File file(fd.fd);
248 bool result = SaveTo(&file);
249 DLOG_IF(ERROR, !result) << "Failed to save file with fd " << fd.fd;
251 if (!fd.auto_close)
252 file.TakePlatformFile();
253 return result;
255 #endif
257 PdfMetafileSkia::PdfMetafileSkia() : data_(new PdfMetafileSkiaData) {
260 scoped_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage() {
261 // If we only ever need the metafile for the last page, should we
262 // only keep a handle on one SkPicture?
263 scoped_ptr<PdfMetafileSkia> metafile(new PdfMetafileSkia);
265 if (data_->pages_.size() == 0)
266 return metafile.Pass();
268 if (data_->recorder_.getRecordingCanvas()) // page outstanding
269 return metafile.Pass();
271 const Page& page = data_->pages_.back();
273 metafile->data_->pages_.push_back(page);
275 if (!metafile->FinishDocument()) // Generate PDF.
276 metafile.reset();
278 return metafile.Pass();
281 } // namespace printing