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

Side by Side Diff: printing/pdf_metafile_skia.cc

Issue 821703005: Remove calls to deprecated SkPDFDevice and SkPDFDocuemnt. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 12 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
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 "base/containers/hash_tables.h" 7 #include "base/containers/hash_tables.h"
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/metrics/histogram.h" 9 #include "base/metrics/histogram.h"
10 #include "base/numerics/safe_conversions.h" 10 #include "base/numerics/safe_conversions.h"
11 #include "base/posix/eintr_wrapper.h" 11 #include "base/posix/eintr_wrapper.h"
12 #include "skia/ext/refptr.h" 12 #include "skia/ext/refptr.h"
13 #include "skia/ext/vector_canvas.h" 13 #include "skia/ext/vector_canvas.h"
14 #include "third_party/skia/include/core/SkData.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"
15 #include "third_party/skia/include/core/SkRefCnt.h" 18 #include "third_party/skia/include/core/SkRefCnt.h"
16 #include "third_party/skia/include/core/SkScalar.h" 19 #include "third_party/skia/include/core/SkScalar.h"
20 #include "third_party/skia/include/core/SkSize.h"
17 #include "third_party/skia/include/core/SkStream.h" 21 #include "third_party/skia/include/core/SkStream.h"
18 #include "third_party/skia/include/core/SkTypeface.h"
19 #include "third_party/skia/include/pdf/SkPDFDevice.h"
20 #include "third_party/skia/include/pdf/SkPDFDocument.h"
21 #include "ui/gfx/point.h" 22 #include "ui/gfx/point.h"
22 #include "ui/gfx/rect.h" 23 #include "ui/gfx/rect.h"
23 #include "ui/gfx/size.h" 24 #include "ui/gfx/size.h"
24 25
25 #if defined(OS_MACOSX) 26 #if defined(OS_MACOSX)
26 #include "printing/pdf_metafile_cg_mac.h" 27 #include "printing/pdf_metafile_cg_mac.h"
27 #endif 28 #endif
28 29
29 #if defined(OS_POSIX) 30 #if defined(OS_POSIX)
30 #include "base/file_descriptor_posix.h" 31 #include "base/file_descriptor_posix.h"
31 #endif 32 #endif
32 33
34 namespace {
35 // This struct represents all the data we need to draw and redraw this
36 // page into a SkDocument.
37 struct Page {
38 Page(const SkSize& page_size, const SkRect& content_area)
39 : page_size_(page_size),
40 content_area_(content_area),
41 content_(/*NULL*/) {}
42 SkSize page_size_;
43 SkRect content_area_;
44 skia::RefPtr<SkPicture> content_;
45 };
46 } // namespace
47
48 static SkSize ToSkSize(const gfx::Size& size) {
49 return SkSize::Make(SkIntToScalar(size.width()),
50 SkIntToScalar(size.height()));
51 }
52
53 static SkRect ToSkRect(const gfx::Rect& rect) {
reed1 2014/12/22 20:41:25 This exists in ui/gfx/skia_util.h, and perhaps som
hal.canary 2014/12/23 15:19:14 Done.
54 return SkRect::MakeLTRB(SkIntToScalar(rect.x()), SkIntToScalar(rect.y()),
55 SkIntToScalar(rect.right()),
56 SkIntToScalar(rect.bottom()));
57 }
58
59 static gfx::Size ToGfxSize(const SkSize& size) {
60 return gfx::Size(SkScalarTruncToInt(size.width()),
61 SkScalarTruncToInt(size.height()));
62 }
63
64 static bool WriteAssetToBuffer(SkStreamAsset* asset,
65 void* buffer,
66 size_t size) {
67 DCHECK(asset->getPosition() == 0); // Be kind: please rewind.
68 size_t length = asset->getLength();
69 if (length > size)
70 return false;
71 bool success = (length == asset->read(buffer, length));
72 (void)asset->rewind();
73 return success;
74 }
75
33 namespace printing { 76 namespace printing {
34 77
35 struct PdfMetafileSkiaData { 78 struct PdfMetafileSkiaData {
36 skia::RefPtr<SkPDFDevice> current_page_; 79 SkPictureRecorder recorder_; // Current recording
37 skia::RefPtr<SkCanvas> current_page_canvas_; 80
38 SkPDFDocument pdf_doc_; 81 std::vector<Page> pages_;
39 SkDynamicMemoryWStream pdf_stream_; 82 skia::RefPtr<SkStreamAsset> pdf_data_;
reed1 2014/12/22 20:41:25 Can pdf_data_ just be SkData?
hal.canary 2014/12/23 15:19:14 That would not be free like SkDynamicMemoryWStream
83
40 #if defined(OS_MACOSX) 84 #if defined(OS_MACOSX)
41 PdfMetafileCg pdf_cg_; 85 PdfMetafileCg pdf_cg_;
42 #endif 86 #endif
43 }; 87 };
44 88
45 PdfMetafileSkia::~PdfMetafileSkia() {} 89 PdfMetafileSkia::~PdfMetafileSkia() {}
46 90
47 bool PdfMetafileSkia::Init() { 91 bool PdfMetafileSkia::Init() {
48 return true; 92 return true;
49 } 93 }
94
95 // TODO(halcanary): Create a Metafile class that only stores data.
96 // Metafile::InitFromData is orthogonal to what the rest of
97 // PdfMetafileSkia does.
50 bool PdfMetafileSkia::InitFromData(const void* src_buffer, 98 bool PdfMetafileSkia::InitFromData(const void* src_buffer,
51 uint32 src_buffer_size) { 99 uint32 src_buffer_size) {
52 return data_->pdf_stream_.write(src_buffer, src_buffer_size); 100 if (data_->pdf_data_)
101 data_->pdf_data_.clear(); // free up RAM first.
102 SkDynamicMemoryWStream dynamic_memory;
reed1 2014/12/22 20:41:25 Use SkMemoryStream(src_buffer, src_buffer_size, tr
hal.canary 2014/12/23 15:19:14 Done.
103 if (!dynamic_memory.write(src_buffer, src_buffer_size))
104 return false;
105 data_->pdf_data_ = skia::AdoptRef(dynamic_memory.detachAsStream());
106 return true;
53 } 107 }
54 108
55 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size, 109 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size,
56 const gfx::Rect& content_area, 110 const gfx::Rect& content_area,
57 const float& scale_factor) { 111 const float& scale_factor) {
58 DCHECK(!data_->current_page_canvas_); 112 if (data_->recorder_.getRecordingCanvas())
113 this->FinishPage();
114 DCHECK(!data_->recorder_.getRecordingCanvas());
115 SkSize sk_page_size = ToSkSize(page_size);
116 data_->pages_.push_back(Page(sk_page_size, ToSkRect(content_area)));
59 117
60 // Adjust for the margins and apply the scale factor. 118 SkCanvas* recordingCanvas = data_->recorder_.beginRecording(
61 SkMatrix transform; 119 sk_page_size.width(), sk_page_size.height(), NULL, 0);
62 transform.setTranslate(SkIntToScalar(content_area.x()), 120 // recordingCanvas is owned by the data_->recorder_. No ref() necessary.
63 SkIntToScalar(content_area.y())); 121 if (!recordingCanvas)
64 transform.preScale(SkFloatToScalar(scale_factor), 122 return false;
65 SkFloatToScalar(scale_factor)); 123 recordingCanvas->scale(scale_factor, scale_factor);
66
67 SkISize pdf_page_size = SkISize::Make(page_size.width(), page_size.height());
68 SkISize pdf_content_size =
69 SkISize::Make(content_area.width(), content_area.height());
70
71 data_->current_page_ = skia::AdoptRef(
72 new SkPDFDevice(pdf_page_size, pdf_content_size, transform));
73 data_->current_page_canvas_ =
74 skia::AdoptRef(new SkCanvas(data_->current_page_.get()));
75 return true; 124 return true;
76 } 125 }
77 126
78 skia::VectorCanvas* PdfMetafileSkia::GetVectorCanvasForNewPage( 127 skia::VectorCanvas* PdfMetafileSkia::GetVectorCanvasForNewPage(
79 const gfx::Size& page_size, 128 const gfx::Size& page_size,
80 const gfx::Rect& content_area, 129 const gfx::Rect& content_area,
81 const float& scale_factor) { 130 const float& scale_factor) {
82 if (!StartPage(page_size, content_area, scale_factor)) 131 if (!StartPage(page_size, content_area, scale_factor))
83 return nullptr; 132 return nullptr;
84 return data_->current_page_canvas_.get(); 133 return data_->recorder_.getRecordingCanvas();
85 } 134 }
86 135
87 bool PdfMetafileSkia::FinishPage() { 136 bool PdfMetafileSkia::FinishPage() {
88 DCHECK(data_->current_page_canvas_); 137 if (!data_->recorder_.getRecordingCanvas())
89 DCHECK(data_->current_page_); 138 return false;
90 139 DCHECK(!(data_->pages_.back().content_));
91 data_->current_page_canvas_.clear(); // Unref SkCanvas. 140 data_->pages_.back().content_ =
92 data_->pdf_doc_.appendPage(data_->current_page_.get()); 141 skia::AdoptRef(data_->recorder_.endRecording());
93 return true; 142 return true;
94 } 143 }
95 144
96 bool PdfMetafileSkia::FinishDocument() { 145 bool PdfMetafileSkia::FinishDocument() {
97 // Don't do anything if we've already set the data in InitFromData. 146 // If we've already set the data in InitFromData, overwrite it.
98 if (data_->pdf_stream_.getOffset()) 147 if (data_->pdf_data_)
99 return true; 148 data_->pdf_data_.clear(); // Free up RAM first.
100 149
101 if (data_->current_page_canvas_) 150 if (data_->recorder_.getRecordingCanvas())
102 FinishPage(); 151 this->FinishPage();
103 152
104 data_->current_page_.clear(); 153 SkDynamicMemoryWStream pdf_stream;
154 skia::RefPtr<SkDocument> pdf_doc =
155 skia::AdoptRef(SkDocument::CreatePDF(&pdf_stream));
156 for (const auto& page : data_->pages_) {
157 SkCanvas* canvas = pdf_doc->beginPage(
158 page.page_size_.width(), page.page_size_.height(), &page.content_area_);
159 canvas->drawPicture(page.content_.get());
160 pdf_doc->endPage();
161 }
162 if (!pdf_doc->close())
163 return false;
105 164
106 int font_counts[SkAdvancedTypefaceMetrics::kOther_Font + 2]; 165 data_->pdf_data_ = skia::AdoptRef(pdf_stream.detachAsStream());
107 data_->pdf_doc_.getCountOfFontTypes(font_counts); 166 return true;
108 for (int type = 0;
109 type <= SkAdvancedTypefaceMetrics::kOther_Font + 1;
110 type++) {
111 for (int count = 0; count < font_counts[type]; count++) {
112 UMA_HISTOGRAM_ENUMERATION(
113 "PrintPreview.FontType", type,
114 SkAdvancedTypefaceMetrics::kOther_Font + 2);
115 }
116 }
117
118 return data_->pdf_doc_.emitPDF(&data_->pdf_stream_);
119 } 167 }
120 168
121 uint32 PdfMetafileSkia::GetDataSize() const { 169 uint32 PdfMetafileSkia::GetDataSize() const {
122 return base::checked_cast<uint32>(data_->pdf_stream_.getOffset()); 170 if (!data_->pdf_data_)
171 return 0;
172 return base::checked_cast<uint32>(data_->pdf_data_->getLength());
123 } 173 }
124 174
125 bool PdfMetafileSkia::GetData(void* dst_buffer, 175 bool PdfMetafileSkia::GetData(void* dst_buffer,
126 uint32 dst_buffer_size) const { 176 uint32 dst_buffer_size) const {
127 if (dst_buffer_size < GetDataSize()) 177 if (!data_->pdf_data_)
128 return false; 178 return false;
129 179 return WriteAssetToBuffer(data_->pdf_data_.get(), dst_buffer,
130 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); 180 base::checked_cast<size_t>(dst_buffer_size));
131 memcpy(dst_buffer, data->bytes(), dst_buffer_size);
132 return true;
133 } 181 }
134 182
135 gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const { 183 gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const {
136 // TODO(vandebo) add a method to get the page size for a given page to 184 if (page_number < data_->pages_.size())
137 // SkPDFDocument. 185 return gfx::Rect(ToGfxSize(data_->pages_[page_number].page_size_));
138 NOTIMPLEMENTED();
139 return gfx::Rect(); 186 return gfx::Rect();
140 } 187 }
141 188
142 unsigned int PdfMetafileSkia::GetPageCount() const { 189 unsigned int PdfMetafileSkia::GetPageCount() const {
143 // TODO(vandebo) add a method to get the number of pages to SkPDFDocument. 190 return base::checked_cast<unsigned int>(data_->pages_.size());
144 NOTIMPLEMENTED();
145 return 0;
146 } 191 }
147 192
148 gfx::NativeDrawingContext PdfMetafileSkia::context() const { 193 gfx::NativeDrawingContext PdfMetafileSkia::context() const {
149 NOTREACHED(); 194 NOTREACHED();
150 return NULL; 195 return NULL;
151 } 196 }
152 197
153 #if defined(OS_WIN) 198 #if defined(OS_WIN)
154 bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc, 199 bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc,
155 const RECT* rect) const { 200 const RECT* rect) const {
(...skipping 11 matching lines...) Expand all
167 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage, 212 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage,
168 the drawing of the PDF into the canvas may result in a rasterized output. 213 the drawing of the PDF into the canvas may result in a rasterized output.
169 PDFMetafileSkia::RenderPage should be not implemented as shown and instead 214 PDFMetafileSkia::RenderPage should be not implemented as shown and instead
170 should do something like the following CL in PluginInstance::PrintPDFOutput: 215 should do something like the following CL in PluginInstance::PrintPDFOutput:
171 http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_ instance.cc 216 http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_ instance.cc
172 */ 217 */
173 bool PdfMetafileSkia::RenderPage(unsigned int page_number, 218 bool PdfMetafileSkia::RenderPage(unsigned int page_number,
174 CGContextRef context, 219 CGContextRef context,
175 const CGRect rect, 220 const CGRect rect,
176 const MacRenderPageParams& params) const { 221 const MacRenderPageParams& params) const {
177 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); 222 DCHECK_GT(GetDataSize(), 0U);
178 if (data_->pdf_cg_.GetDataSize() == 0) { 223 if (data_->pdf_cg_.GetDataSize() == 0) {
179 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); 224 if (GetDataSize() == 0)
180 data_->pdf_cg_.InitFromData(data->bytes(), data->size()); 225 return false;
226 size_t length = data_->pdf_data_->getLength();
227 std::vector<uint8_t> buffer(length);
228 (void)WriteAssetToBuffer(data_->pdf_data_.get(), &buffer[0], length);
229 data_->pdf_cg_.InitFromData(&buffer[0], base::checked_cast<uint32>(length));
181 } 230 }
182 return data_->pdf_cg_.RenderPage(page_number, context, rect, params); 231 return data_->pdf_cg_.RenderPage(page_number, context, rect, params);
183 } 232 }
184 #endif 233 #endif
185 234
186 bool PdfMetafileSkia::SaveTo(base::File* file) const { 235 bool PdfMetafileSkia::SaveTo(base::File* file) const {
187 if (GetDataSize() == 0U) 236 if (GetDataSize() == 0U)
188 return false; 237 return false;
189 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); 238 DCHECK(data_->pdf_data_.get());
190 // TODO(halcanary): rewrite this function without extra data copy 239 SkStreamAsset* asset = data_->pdf_data_.get();
191 // using SkStreamAsset. 240 DCHECK_EQ(asset->getPosition(), 0U);
192 const char* ptr = reinterpret_cast<const char*>(data->data()); 241
193 int size = base::checked_cast<int>(data->size()); 242 const size_t maximum_buffer_size = 1024 * 1024;
194 return file->WriteAtCurrentPos(ptr, size) == size; 243 std::vector<char> buffer(std::min(maximum_buffer_size, asset->getLength()));
244 do {
245 size_t read_size = asset->read(&buffer[0], buffer.size());
246 if (read_size == 0)
247 break;
248 DCHECK_GE(buffer.size(), read_size);
249 if (!file->WriteAtCurrentPos(&buffer[0],
250 base::checked_cast<int>(read_size))) {
251 (void)asset->rewind();
252 return false;
253 }
254 } while (!asset->isAtEnd());
255
256 (void)asset->rewind();
257 return true;
195 } 258 }
196 259
197 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) 260 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
198 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const { 261 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const {
199 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); 262 DCHECK_GT(GetDataSize(), 0U);
200 263
201 if (fd.fd < 0) { 264 if (fd.fd < 0) {
202 DLOG(ERROR) << "Invalid file descriptor!"; 265 DLOG(ERROR) << "Invalid file descriptor!";
203 return false; 266 return false;
204 } 267 }
205 base::File file(fd.fd); 268 base::File file(fd.fd);
206 bool result = SaveTo(&file); 269 bool result = SaveTo(&file);
207 DLOG_IF(ERROR, !result) << "Failed to save file with fd " << fd.fd; 270 DLOG_IF(ERROR, !result) << "Failed to save file with fd " << fd.fd;
208 271
209 if (!fd.auto_close) 272 if (!fd.auto_close)
210 file.TakePlatformFile(); 273 file.TakePlatformFile();
211 return result; 274 return result;
212 } 275 }
213 #endif 276 #endif
214 277
215 PdfMetafileSkia::PdfMetafileSkia() : data_(new PdfMetafileSkiaData) { 278 PdfMetafileSkia::PdfMetafileSkia() : data_(new PdfMetafileSkiaData) {
216 } 279 }
217 280
218 scoped_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage() { 281 scoped_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage() {
219 scoped_ptr<PdfMetafileSkia> metafile; 282 // If we only ever need the metafile for the last page, should we
220 SkPDFDocument pdf_doc(SkPDFDocument::kDraftMode_Flags); 283 // only keep a handle on one SkPicture?
221 if (!pdf_doc.appendPage(data_->current_page_.get())) 284 scoped_ptr<PdfMetafileSkia> metafile(new PdfMetafileSkia);
285
286 if (data_->pages_.size() == 0)
222 return metafile.Pass(); 287 return metafile.Pass();
223 288
224 SkDynamicMemoryWStream pdf_stream; 289 if (data_->recorder_.getRecordingCanvas()) // page outstanding
225 if (!pdf_doc.emitPDF(&pdf_stream))
226 return metafile.Pass(); 290 return metafile.Pass();
227 291
228 SkAutoDataUnref data_copy(pdf_stream.copyToData()); 292 const Page& page = data_->pages_.back();
229 if (data_copy->size() == 0)
230 return scoped_ptr<PdfMetafileSkia>();
231 293
232 metafile.reset(new PdfMetafileSkia); 294 metafile->data_->pages_.push_back(page); // Copy page data;
233 if (!metafile->InitFromData(data_copy->bytes(), 295 // Should increment refcnt on page->content_.
234 base::checked_cast<uint32>(data_copy->size()))) { 296
297 if (!metafile->FinishDocument()) // Generate PDF.
235 metafile.reset(); 298 metafile.reset();
236 } 299
237 return metafile.Pass(); 300 return metafile.Pass();
238 } 301 }
239 302
240 } // namespace printing 303 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698