Skip to content

Commit

Permalink
Unfork VectorPlatformCanvas.
Browse files Browse the repository at this point in the history
Unfork VectorPlatformCanvas by making NativeMetafile know how to create an appropriate VectorPlatformDevice. This will also be useful when we have multiple NativeMetafile implemenations (each requiring a different VectorPlatformDevices).

BUG=NONE
TEST=NONE

Review URL: http://codereview.chromium.org/6665046

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@78662 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
vandebo@chromium.org committed Mar 18, 2011
1 parent 2aba747 commit 063f8db
Show file tree
Hide file tree
Showing 19 changed files with 92 additions and 150 deletions.
13 changes: 5 additions & 8 deletions chrome/renderer/print_web_view_helper_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,14 @@ void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params,
margin_left_in_points,
content_height_in_points + margin_top_in_points +
margin_bottom_in_points);
gfx::Point content_origin(margin_left_in_points, margin_top_in_points);

cairo_t* cairo_context =
metafile->StartPage(page_size,
margin_top_in_points,
margin_left_in_points);
if (!cairo_context)
skia::PlatformDevice* device = metafile->StartPageForVectorCanvas(
page_size, content_origin, 1.0f);
if (!device)
return;

canvas->reset(new skia::VectorCanvas(cairo_context,
canvas_size.width(),
canvas_size.height()));
canvas->reset(new skia::VectorCanvas(device));
frame->printPage(params.page_number, canvas->get());

// TODO(myhuang): We should handle transformation for paper margins.
Expand Down
53 changes: 9 additions & 44 deletions chrome/renderer/print_web_view_helper_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ void PrintWebViewHelper::CreatePreviewDocument(
void PrintWebViewHelper::RenderPage(
const ViewMsg_Print_Params& params, float* scale_factor, int page_number,
WebFrame* frame, scoped_ptr<printing::NativeMetafile>* metafile) {
HDC hdc = (*metafile)->context();
DCHECK(hdc);
DCHECK(metafile->get()->context());

double content_width_in_points;
double content_height_in_points;
Expand All @@ -209,45 +208,12 @@ void PrintWebViewHelper::RenderPage(
int width = static_cast<int>(content_width_in_points * params.max_shrink);
int height = static_cast<int>(content_height_in_points * params.max_shrink);

bool result = (*metafile)->StartPage();
DCHECK(result);

#if 0
// TODO(maruel): This code is kept for testing until the 100% GDI drawing
// code is stable. maruels use this code's output as a reference when the
// GDI drawing code fails.

// Mix of Skia and GDI based.
skia::PlatformCanvas canvas(width, height, true);
canvas.drawARGB(255, 255, 255, 255, SkXfermode::kSrc_Mode);
float webkit_scale_factor = frame->printPage(page_number, &canvas);
if (*scale_factor <= 0 || webkit_scale_factor <= 0) {
NOTREACHED() << "Printing page " << page_number << " failed.";
} else {
// Update the dpi adjustment with the "page |scale_factor|" calculated in
// webkit.
*scale_factor /= webkit_scale_factor;
}

// Create a BMP v4 header that we can serialize.
BITMAPV4HEADER bitmap_header;
gfx::CreateBitmapV4Header(width, height, &bitmap_header);
const SkBitmap& src_bmp = canvas.getDevice()->accessBitmap(true);
SkAutoLockPixels src_lock(src_bmp);
int retval = StretchDIBits(hdc,
0,
0,
width, height,
0, 0,
width, height,
src_bmp.getPixels(),
reinterpret_cast<BITMAPINFO*>(&bitmap_header),
DIB_RGB_COLORS,
SRCCOPY);
DCHECK(retval != GDI_ERROR);
#else
// 100% GDI based.
skia::VectorCanvas canvas(hdc, width, height);
gfx::Size page_size(width, height);
gfx::Point content_origin;
skia::PlatformDevice* device = (*metafile)->StartPageForVectorCanvas(
page_size, content_origin, 1.0f);
DCHECK(device);
skia::VectorCanvas canvas(device);
float webkit_scale_factor = frame->printPage(page_number, &canvas);
if (*scale_factor <= 0 || webkit_scale_factor <= 0) {
NOTREACHED() << "Printing page " << page_number << " failed.";
Expand All @@ -256,13 +222,12 @@ void PrintWebViewHelper::RenderPage(
// webkit.
*scale_factor /= webkit_scale_factor;
}
#endif

result = (*metafile)->FinishPage();
bool result = (*metafile)->FinishPage();
DCHECK(result);

skia::VectorPlatformDevice* platform_device =
static_cast<skia::VectorPlatformDevice*>(canvas.getDevice());
static_cast<skia::VectorPlatformDevice*>(device);
if (platform_device->alpha_blend_used() && !params.supports_alpha_blend) {
// Close the device context to retrieve the compiled metafile.
if (!(*metafile)->Close())
Expand Down
13 changes: 13 additions & 0 deletions printing/emf_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "base/metrics/histogram.h"
#include "base/scoped_ptr.h"
#include "base/time.h"
#include "skia/ext/vector_platform_device_win.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/jpeg_codec.h"
#include "ui/gfx/codec/png_codec.h"
Expand Down Expand Up @@ -410,6 +411,18 @@ bool Emf::Record::SafePlayback(const XFORM* base_matrix) const {
return res;
}

skia::PlatformDevice* Emf::StartPageForVectorCanvas(
const gfx::Size& page_size, const gfx::Point& /*content_origin*/,
const float& scale_factor) {
DCHECK_EQ(1.0f, scale_factor); // We don't support scaling here.
if (!StartPage())
return NULL;

return skia::VectorPlatformDeviceFactory::CreateDevice(page_size.width(),
page_size.height(),
true, hdc_);
}

bool Emf::StartPage() {
DCHECK(hdc_);
if (!hdc_)
Expand Down
4 changes: 4 additions & 0 deletions printing/emf_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
class FilePath;

namespace gfx {
class Point;
class Rect;
}

Expand All @@ -33,6 +34,9 @@ class Emf : public NativeMetafile {
virtual bool Init() { return true; }
virtual bool InitFromData(const void* src_buffer, uint32 src_buffer_size);

virtual skia::PlatformDevice* StartPageForVectorCanvas(
const gfx::Size& page_size, const gfx::Point& content_origin,
const float& scale_factor);
virtual bool StartPage();
virtual bool FinishPage();
virtual bool Close();
Expand Down
11 changes: 11 additions & 0 deletions printing/native_metafile.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class Size;
class Point;
}

namespace skia {
class PlatformDevice;
}

#if defined(OS_CHROMEOS)
namespace base {
class FileDescriptor;
Expand All @@ -50,6 +54,13 @@ class NativeMetafile {
// Note: It should only be called from within the browser process.
virtual bool InitFromData(const void* src_buffer, uint32 src_buffer_size) = 0;

// This method calls StartPage and then returns an appropriate
// VectorPlatformDevice implementation bound to the context created by
// StartPage or NULL on error.
virtual skia::PlatformDevice* StartPageForVectorCanvas(
const gfx::Size& page_size, const gfx::Point& content_origin,
const float& scale_factor) = 0;

#if defined(OS_WIN)
// Inserts a custom GDICOMMENT records indicating StartPage/EndPage calls
// (since StartPage and EndPage do not work in a metafile DC). Only valid
Expand Down
7 changes: 7 additions & 0 deletions printing/pdf_metafile_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ bool PdfMetafile::InitFromData(const void* src_buffer, uint32 src_buffer_size) {
return true;
}

skia::PlatformDevice* PdfMetafile::StartPageForVectorCanvas(
const gfx::Size& page_size, const gfx::Point& content_origin,
const float& scale_factor) {
NOTIMPLEMENTED();
return NULL;
}

CGContextRef PdfMetafile::StartPage(const gfx::Size& page_size,
const gfx::Point& content_origin, const float& scale_factor) {
DCHECK(context_.get());
Expand Down
4 changes: 4 additions & 0 deletions printing/pdf_metafile_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class PdfMetafile : public NativeMetafile {
virtual bool Init();
virtual bool InitFromData(const void* src_buffer, uint32 src_buffer_size);

// Not implemented on mac.
skia::PlatformDevice* StartPageForVectorCanvas(
const gfx::Size& page_size, const gfx::Point& content_origin,
const float& scale_factor);
virtual CGContextRef StartPage(const gfx::Size& page_size,
const gfx::Point& content_origin,
const float& scale_factor);
Expand Down
13 changes: 13 additions & 0 deletions printing/pdf_ps_metafile_cairo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ bool PdfPsMetafile::SetRawData(const void* src_buffer,
return true;
}

skia::PlatformDevice* PdfPsMetafile::StartPageForVectorCanvas(
const gfx::Size& page_size, const gfx::Point& content_origin,
const float& scale_factor) {
DCHECK_EQ(1.0f, scale_factor); // We don't yet support scale_factor.
if (!StartPage(page_size, content_origin.y(), content_origin.x()))
return NULL;

return skia::VectorPlatformDeviceFactory::CreateDevice(context_,
page_size.width(),
page_size.height(),
true);
}

cairo_t* PdfPsMetafile::StartPage(const gfx::Size& page_size,
double margin_top_in_points,
double margin_left_in_points) {
Expand Down
3 changes: 3 additions & 0 deletions printing/pdf_ps_metafile_cairo.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class PdfPsMetafile : public NativeMetafile {
virtual bool Init();
virtual bool InitFromData(const void* src_buffer, uint32 src_buffer_size);

virtual skia::PlatformDevice* StartPageForVectorCanvas(
const gfx::Size& page_size, const gfx::Point& content_origin,
const float& scale_factor);
virtual cairo_t* StartPage(const gfx::Size& page_size,
double margin_top_in_points,
double margin_left_in_points);
Expand Down
10 changes: 5 additions & 5 deletions skia/ext/vector_canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

#include "skia/ext/vector_canvas.h"

namespace skia {
#include "skia/ext/vector_platform_device.h"

VectorCanvas::VectorCanvas()
: PlatformCanvas(SkNEW(VectorPlatformDeviceFactory)) {
}
namespace skia {

VectorCanvas::VectorCanvas(SkDeviceFactory* factory) : PlatformCanvas(factory) {
VectorCanvas::VectorCanvas(PlatformDevice* device)
: PlatformCanvas(device->getDeviceFactory()) {
setDevice(device)->unref(); // Created with refcount 1, and setDevice refs.
}

VectorCanvas::~VectorCanvas() {
Expand Down
25 changes: 4 additions & 21 deletions skia/ext/vector_canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,21 @@
#pragma once

#include "skia/ext/platform_canvas.h"
#include "skia/ext/vector_platform_device.h"

#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__)
typedef struct _cairo cairo_t;
#endif

namespace skia {

class PlatformDevice;

// This class is a specialization of the regular PlatformCanvas. It is designed
// to work with a VectorDevice to manage platform-specific drawing. It allows
// using both Skia operations and platform-specific operations. It *doesn't*
// support reading back from the bitmap backstore since it is not used.
class SK_API VectorCanvas : public PlatformCanvas {
public:
VectorCanvas();
explicit VectorCanvas(SkDeviceFactory* factory);
#if defined(WIN32)
VectorCanvas(HDC dc, int width, int height);
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__)
// Caller owns |context|. Ownership is not transferred.
VectorCanvas(cairo_t* context, int width, int height);
#endif
// Ownership of |device| is transfered to VectorCanvas.
explicit VectorCanvas(PlatformDevice* device);
virtual ~VectorCanvas();

// For two-part init, call if you use the no-argument constructor above
#if defined(WIN32)
bool initialize(HDC context, int width, int height);
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__)
// Ownership of |context| is not transferred.
bool initialize(cairo_t* context, int width, int height);
#endif

virtual SkBounder* setBounder(SkBounder* bounder);
virtual SkDrawFilter* setDrawFilter(SkDrawFilter* filter);

Expand Down
28 changes: 0 additions & 28 deletions skia/ext/vector_canvas_linux.cc

This file was deleted.

7 changes: 5 additions & 2 deletions skia/ext/vector_canvas_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "skia/ext/vector_canvas.h"
#include "skia/ext/vector_platform_device.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/effects/SkDashPathEffect.h"
#include "ui/gfx/codec/png_codec.h"
Expand Down Expand Up @@ -386,7 +387,8 @@ class VectorCanvasTest : public ImageTest {
size_ = size;
context_ = new Context();
bitmap_ = new Bitmap(*context_, size_, size_);
vcanvas_ = new VectorCanvas(context_->context(), size_, size_);
vcanvas_ = new VectorCanvas(VectorPlatformDeviceFactory::CreateDevice(
size_, size_, true, context_->context()));
pcanvas_ = new PlatformCanvas(size_, size_, false);

// Clear white.
Expand Down Expand Up @@ -452,7 +454,8 @@ TEST_F(VectorCanvasTest, Uninitialized) {

context_ = new Context();
bitmap_ = new Bitmap(*context_, size_, size_);
vcanvas_ = new VectorCanvas(context_->context(), size_, size_);
vcanvas_ = new VectorCanvas(VectorPlatformDeviceFactory::CreateDevice(
size_, size_, true, context_->context()));
pcanvas_ = new PlatformCanvas(size_, size_, false);

// VectorCanvas default initialization is black.
Expand Down
30 changes: 0 additions & 30 deletions skia/ext/vector_canvas_win.cc

This file was deleted.

6 changes: 3 additions & 3 deletions skia/ext/vector_platform_device_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ SkDevice* VectorPlatformDeviceFactory::newDevice(SkCanvas* ignored,
}

// static
SkDevice* VectorPlatformDeviceFactory::CreateDevice(cairo_t* context,
int width, int height,
bool isOpaque) {
PlatformDevice* VectorPlatformDeviceFactory::CreateDevice(cairo_t* context,
int width, int height,
bool isOpaque) {
// TODO(myhuang): Here we might also have similar issues as those on Windows
// (vector_canvas_win.cc, http://crbug.com/18382 & http://crbug.com/18383).
// Please note that is_opaque is true when we use this class for printing.
Expand Down
4 changes: 2 additions & 2 deletions skia/ext/vector_platform_device_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace skia {

class VectorPlatformDeviceFactory : public SkDeviceFactory {
public:
static SkDevice* CreateDevice(cairo_t* context, int width, int height,
bool isOpaque);
static PlatformDevice* CreateDevice(cairo_t* context, int width, int height,
bool isOpaque);

// Overridden from SkDeviceFactory:
virtual SkDevice* newDevice(SkCanvas* ignored, SkBitmap::Config config,
Expand Down
Loading

0 comments on commit 063f8db

Please sign in to comment.