diff options
author | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-23 22:23:59 +0000 |
---|---|---|
committer | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-23 22:23:59 +0000 |
commit | 0f4fe844bd7ca7f0fa512a2e786d9debb2831526 (patch) | |
tree | f803b71c036a9fb0286298a366e682f65325d58d /gfx/canvas_direct2d.cc | |
parent | 04dd9d9de68e20da43e7e2f13a550ea7c8ca6fa3 (diff) | |
download | chromium_src-0f4fe844bd7ca7f0fa512a2e786d9debb2831526.zip chromium_src-0f4fe844bd7ca7f0fa512a2e786d9debb2831526.tar.gz chromium_src-0f4fe844bd7ca7f0fa512a2e786d9debb2831526.tar.bz2 |
Add support for brushes to gfx::Canvas... right now just LinearGradientBrush.
A native brush is created by gfx::Canvas::CreateLinearGradientBrush wrapped in a gfx::Brush. When gfx::Brush is deleted the platform wrapper frees the native brush.
BUG=none
TEST=see unittest.
Review URL: http://codereview.chromium.org/3038019
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@53534 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'gfx/canvas_direct2d.cc')
-rw-r--r-- | gfx/canvas_direct2d.cc | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/gfx/canvas_direct2d.cc b/gfx/canvas_direct2d.cc index b187455..e18272c 100644 --- a/gfx/canvas_direct2d.cc +++ b/gfx/canvas_direct2d.cc @@ -25,6 +25,41 @@ D2D1_RECT_F RectToRectF(const gfx::Rect& rect) { return RectToRectF(rect.x(), rect.y(), rect.width(), rect.height()); } +D2D1_POINT_2F PointToPoint2F(const gfx::Point& point) { + return D2D1::Point2F(static_cast<float>(point.x()), + static_cast<float>(point.y())); +} + +D2D1_EXTEND_MODE TileModeToExtendMode(gfx::Canvas::TileMode tile_mode) { + switch (tile_mode) { + case gfx::Canvas::TileMode_Clamp: + return D2D1_EXTEND_MODE_CLAMP; + case gfx::Canvas::TileMode_Mirror: + return D2D1_EXTEND_MODE_MIRROR; + case gfx::Canvas::TileMode_Repeat: + return D2D1_EXTEND_MODE_WRAP; + default: + NOTREACHED() << "Invalid TileMode"; + } + return D2D1_EXTEND_MODE_CLAMP; +} + +// A platform wrapper for a Direct2D brush that makes sure the underlying +// ID2D1Brush COM object is released when this object is destroyed. +class Direct2DBrush : public gfx::Brush { + public: + explicit Direct2DBrush(ID2D1Brush* brush) : brush_(brush) { + } + + ID2D1Brush* brush() const { return brush_.get(); } + + private: + ScopedComPtr<ID2D1Brush> brush_; + + DISALLOW_COPY_AND_ASSIGN(Direct2DBrush); +}; + + } // namespace namespace gfx { @@ -147,6 +182,12 @@ void CanvasDirect2D::FillRectInt(const SkColor& color, int x, int y, int w, rt_->FillRectangle(RectToRectF(x, y, w, h), solid_brush); } +void CanvasDirect2D::FillRectInt(const gfx::Brush* brush, int x, int y, int w, + int h) { + const Direct2DBrush* d2d_brush = static_cast<const Direct2DBrush*>(brush); + rt_->FillRectangle(RectToRectF(x, y, w, h), d2d_brush->brush()); +} + void CanvasDirect2D::DrawRectInt(const SkColor& color, int x, int y, int w, int h) { @@ -232,6 +273,37 @@ void CanvasDirect2D::EndPlatformPaint() { interop_rt_.release(); } +Brush* CanvasDirect2D::CreateLinearGradientBrush( + const gfx::Point& start_point, + const gfx::Point& end_point, + const SkColor colors[], + const float positions[], + size_t position_count, + TileMode tile_mode) { + ID2D1GradientStopCollection* gradient_stop_collection = NULL; + D2D1_GRADIENT_STOP* gradient_stops = new D2D1_GRADIENT_STOP[position_count]; + for (size_t i = 0; i < position_count; ++i) { + gradient_stops[i].color = SkColorToColorF(colors[i]); + gradient_stops[i].position = positions[i]; + } + HRESULT hr = rt_->CreateGradientStopCollection(gradient_stops, + position_count, + D2D1_GAMMA_2_2, + TileModeToExtendMode(tile_mode), + &gradient_stop_collection); + if (FAILED(hr)) + return NULL; + + ID2D1LinearGradientBrush* brush = NULL; + hr = rt_->CreateLinearGradientBrush( + D2D1::LinearGradientBrushProperties(PointToPoint2F(start_point), + PointToPoint2F(end_point)), + gradient_stop_collection, + &brush); + + return new Direct2DBrush(brush); +} + CanvasSkia* CanvasDirect2D::AsCanvasSkia() { return NULL; } |