diff options
-rw-r--r-- | chrome/renderer/print_web_view_helper_linux.cc | 3 | ||||
-rw-r--r-- | printing/native_metafile_skia_wrapper.cc | 55 | ||||
-rw-r--r-- | printing/native_metafile_skia_wrapper.h | 34 | ||||
-rw-r--r-- | printing/pdf_ps_metafile_cairo.cc | 12 | ||||
-rw-r--r-- | printing/pdf_ps_metafile_cairo.h | 4 | ||||
-rw-r--r-- | printing/pdf_ps_metafile_cairo_unittest.cc | 1 | ||||
-rw-r--r-- | printing/printing.gyp | 4 | ||||
-rw-r--r-- | webkit/plugins/ppapi/ppapi_plugin_instance.cc | 11 |
8 files changed, 100 insertions, 24 deletions
diff --git a/chrome/renderer/print_web_view_helper_linux.cc b/chrome/renderer/print_web_view_helper_linux.cc index 65370c7..98a2f80 100644 --- a/chrome/renderer/print_web_view_helper_linux.cc +++ b/chrome/renderer/print_web_view_helper_linux.cc @@ -10,6 +10,7 @@ #include "chrome/common/print_messages.h" #include "content/common/view_messages.h" #include "printing/native_metafile_factory.h" +#include "printing/native_metafile_skia_wrapper.h" #include "printing/native_metafile.h" #include "skia/ext/vector_canvas.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" @@ -228,6 +229,8 @@ void PrintWebViewHelper::PrintPage(const PrintMsg_PrintPage_Params& params, return; canvas->reset(new skia::VectorCanvas(device)); + printing::NativeMetafileSkiaWrapper::SetMetafileOnCanvas(canvas->get(), + metafile); frame->printPage(params.page_number, canvas->get()); // TODO(myhuang): We should handle transformation for paper margins. diff --git a/printing/native_metafile_skia_wrapper.cc b/printing/native_metafile_skia_wrapper.cc new file mode 100644 index 0000000..af6f446 --- /dev/null +++ b/printing/native_metafile_skia_wrapper.cc @@ -0,0 +1,55 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/logging.h" +#include "printing/native_metafile_skia_wrapper.h" +#include "third_party/skia/include/core/SkCanvas.h" +#include "third_party/skia/include/core/SkDevice.h" +#include "third_party/skia/include/core/SkRefDict.h" + +namespace printing { + +namespace { + +static const char* kNativeMetafileKey = "CrNativeMetafile"; + +SkRefDict& getRefDict(SkCanvas* canvas) { + DCHECK(canvas != NULL); + + SkDevice* device = canvas->getDevice(); + DCHECK(device != NULL); + return device->getRefDict(); +} + +} // namespace + + +// static +void NativeMetafileSkiaWrapper::SetMetafileOnCanvas(SkCanvas* canvas, + NativeMetafile* metafile) { + NativeMetafileSkiaWrapper* wrapper = NULL; + if (metafile) + wrapper = new NativeMetafileSkiaWrapper(metafile); + + SkRefDict& dict = getRefDict(canvas); + dict.set(kNativeMetafileKey, wrapper); + SkSafeUnref(wrapper); +} + +// static +NativeMetafile* NativeMetafileSkiaWrapper::GetMetafileFromCanvas( + SkCanvas* canvas) { + SkRefDict& dict = getRefDict(canvas); + SkRefCnt* value = dict.find(kNativeMetafileKey); + if (!value) + return NULL; + + return static_cast<NativeMetafileSkiaWrapper*>(value)->metafile_; +} + +NativeMetafileSkiaWrapper::NativeMetafileSkiaWrapper(NativeMetafile* metafile) + : metafile_(metafile) { +} + +} // namespace printing diff --git a/printing/native_metafile_skia_wrapper.h b/printing/native_metafile_skia_wrapper.h new file mode 100644 index 0000000..7eb2887 --- /dev/null +++ b/printing/native_metafile_skia_wrapper.h @@ -0,0 +1,34 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PRINTING_NATIVE_METAFILE_SKIA_WRAPPER_H_ +#define PRINTING_NATIVE_METAFILE_SKIA_WRAPPER_H_ + +#include "third_party/skia/include/core/SkRefCnt.h" + +class SkCanvas; + +namespace printing { + +class NativeMetafile; + +// A wrapper class with static methods to set and retrieve a NativeMetafile +// on an SkCanvas. The ownership of the metafile is not affected and it +// is the caller's responsibility to ensure that the metafile remains valid +// as long as the canvas. +class NativeMetafileSkiaWrapper : public SkRefCnt { + public: + static void SetMetafileOnCanvas(SkCanvas* canvas, NativeMetafile* metafile); + + static NativeMetafile* GetMetafileFromCanvas(SkCanvas* canvas); + + private: + explicit NativeMetafileSkiaWrapper(NativeMetafile* metafile); + + NativeMetafile* metafile_; +}; + +} // namespace printing + +#endif // PRINTING_NATIVE_METAFILE_SKIA_WRAPPER_H_ diff --git a/printing/pdf_ps_metafile_cairo.cc b/printing/pdf_ps_metafile_cairo.cc index fdbb335..a10b125 100644 --- a/printing/pdf_ps_metafile_cairo.cc +++ b/printing/pdf_ps_metafile_cairo.cc @@ -20,8 +20,6 @@ namespace { -const cairo_user_data_key_t kPdfMetafileKey = {0}; - // Tests if |surface| is valid. bool IsSurfaceValid(cairo_surface_t* surface) { return cairo_surface_status(surface) == CAIRO_STATUS_SUCCESS; @@ -63,10 +61,6 @@ cairo_status_t WriteCairoStream(void* dst_buffer, return CAIRO_STATUS_SUCCESS; } -void DestroyContextData(void* data) { - // Nothing to be done here. -} - } // namespace namespace printing { @@ -110,7 +104,6 @@ bool PdfPsMetafile::Init() { return false; } - cairo_set_user_data(context_, &kPdfMetafileKey, this, DestroyContextData); return true; } @@ -257,11 +250,6 @@ bool PdfPsMetafile::SaveToFD(const base::FileDescriptor& fd) const { } #endif // if defined(OS_CHROMEOS) -PdfPsMetafile* PdfPsMetafile::FromCairoContext(cairo_t* context) { - return reinterpret_cast<PdfPsMetafile*>( - cairo_get_user_data(context, &kPdfMetafileKey)); -} - void PdfPsMetafile::CleanUpAll() { CleanUpContext(&context_); CleanUpSurface(&surface_); diff --git a/printing/pdf_ps_metafile_cairo.h b/printing/pdf_ps_metafile_cairo.h index 2ae1f34..6d5cf5c 100644 --- a/printing/pdf_ps_metafile_cairo.h +++ b/printing/pdf_ps_metafile_cairo.h @@ -58,10 +58,6 @@ class PdfPsMetafile : public NativeMetafile { virtual bool SaveToFD(const base::FileDescriptor& fd) const; #endif // if defined(OS_CHROMEOS) - // Returns the PdfPsMetafile object that owns the given context. Returns NULL - // if the context was not created by a PdfPsMetafile object. - static PdfPsMetafile* FromCairoContext(cairo_t* context); - protected: PdfPsMetafile(); diff --git a/printing/pdf_ps_metafile_cairo_unittest.cc b/printing/pdf_ps_metafile_cairo_unittest.cc index d76d801..5b2bf2a 100644 --- a/printing/pdf_ps_metafile_cairo_unittest.cc +++ b/printing/pdf_ps_metafile_cairo_unittest.cc @@ -33,7 +33,6 @@ TEST_F(PdfPsTest, Pdf) { // Renders page 1. EXPECT_TRUE(pdf.StartPage(gfx::Size(72, 73), gfx::Point(4, 5), 1)); - EXPECT_EQ(printing::PdfPsMetafile::FromCairoContext(pdf.context()), &pdf); // In theory, we should use Cairo to draw something on |context|. EXPECT_TRUE(pdf.FinishPage()); diff --git a/printing/printing.gyp b/printing/printing.gyp index 049e752..0e504c4 100644 --- a/printing/printing.gyp +++ b/printing/printing.gyp @@ -94,6 +94,10 @@ 'sources/': [['exclude', '_posix\\.cc$']] }], ['OS=="linux" or OS=="freebsd" or OS=="openbsd"', { + 'sources': [ + 'native_metafile_skia_wrapper.cc', + 'native_metafile_skia_wrapper.h', + ], 'dependencies': [ # For FT_Init_FreeType and friends. '../build/linux/system.gyp:freetype2', diff --git a/webkit/plugins/ppapi/ppapi_plugin_instance.cc b/webkit/plugins/ppapi/ppapi_plugin_instance.cc index 0756308..d6a1da0 100644 --- a/webkit/plugins/ppapi/ppapi_plugin_instance.cc +++ b/webkit/plugins/ppapi/ppapi_plugin_instance.cc @@ -69,7 +69,7 @@ #endif #if defined(OS_LINUX) -#include "printing/pdf_ps_metafile_cairo.h" +#include "printing/native_metafile_skia_wrapper.h" #endif #if defined(OS_WIN) @@ -1180,15 +1180,12 @@ bool PluginInstance::PrintPDFOutput(PP_Resource print_output, bool ret = false; #if defined(OS_LINUX) - // On Linux we need to get the backing PdfPsMetafile and write the bits - // directly. - cairo_t* context = canvas->beginPlatformPaint(); + // On Linux we just set the final bits in the native metafile. printing::NativeMetafile* metafile = - printing::PdfPsMetafile::FromCairoContext(context); - DCHECK(metafile); + printing::NativeMetafileSkiaWrapper::GetMetafileFromCanvas(canvas); + DCHECK(metafile != NULL); if (metafile) ret = metafile->InitFromData(buffer->mapped_buffer(), buffer->size()); - canvas->endPlatformPaint(); #elif defined(OS_MACOSX) scoped_ptr<printing::NativeMetafile> metafile( printing::NativeMetafileFactory::Create()); |