OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "gfx/canvas_direct2d.h" | 5 #include "gfx/canvas_direct2d.h" |
6 | 6 |
| 7 #include "base/scoped_ptr.h" |
7 #include "gfx/rect.h" | 8 #include "gfx/rect.h" |
8 | 9 |
9 namespace { | 10 namespace { |
10 | 11 |
11 // Converts a SkColor to a ColorF. | 12 // Converts a SkColor to a ColorF. |
12 D2D1_COLOR_F SkColorToColorF(SkColor color) { | 13 D2D1_COLOR_F SkColorToColorF(SkColor color) { |
13 return D2D1::ColorF(static_cast<float>(SkColorGetR(color)) / 0xFF, | 14 return D2D1::ColorF(static_cast<float>(SkColorGetR(color)) / 0xFF, |
14 static_cast<float>(SkColorGetG(color)) / 0xFF, | 15 static_cast<float>(SkColorGetG(color)) / 0xFF, |
15 static_cast<float>(SkColorGetB(color)) / 0xFF, | 16 static_cast<float>(SkColorGetB(color)) / 0xFF, |
16 static_cast<float>(SkColorGetA(color)) / 0xFF); | 17 static_cast<float>(SkColorGetA(color)) / 0xFF); |
(...skipping 20 matching lines...) Expand all Loading... |
37 case gfx::Canvas::TileMode_Mirror: | 38 case gfx::Canvas::TileMode_Mirror: |
38 return D2D1_EXTEND_MODE_MIRROR; | 39 return D2D1_EXTEND_MODE_MIRROR; |
39 case gfx::Canvas::TileMode_Repeat: | 40 case gfx::Canvas::TileMode_Repeat: |
40 return D2D1_EXTEND_MODE_WRAP; | 41 return D2D1_EXTEND_MODE_WRAP; |
41 default: | 42 default: |
42 NOTREACHED() << "Invalid TileMode"; | 43 NOTREACHED() << "Invalid TileMode"; |
43 } | 44 } |
44 return D2D1_EXTEND_MODE_CLAMP; | 45 return D2D1_EXTEND_MODE_CLAMP; |
45 } | 46 } |
46 | 47 |
| 48 ID2D1GradientStopCollection* CreateGradientStopCollection( |
| 49 ID2D1RenderTarget* render_target, |
| 50 const SkColor colors[], |
| 51 const float positions[], |
| 52 size_t position_count, |
| 53 gfx::Canvas::TileMode tile_mode) { |
| 54 scoped_array<D2D1_GRADIENT_STOP> gradient_stops( |
| 55 new D2D1_GRADIENT_STOP[position_count]); |
| 56 for (size_t i = 0; i < position_count; ++i) { |
| 57 gradient_stops[i].color = SkColorToColorF(colors[i]); |
| 58 gradient_stops[i].position = positions[i]; |
| 59 } |
| 60 ID2D1GradientStopCollection* gradient_stop_collection = NULL; |
| 61 HRESULT hr = render_target->CreateGradientStopCollection( |
| 62 gradient_stops.get(), |
| 63 position_count, |
| 64 D2D1_GAMMA_2_2, |
| 65 TileModeToExtendMode(tile_mode), |
| 66 &gradient_stop_collection); |
| 67 return SUCCEEDED(hr) ? gradient_stop_collection : NULL; |
| 68 } |
| 69 |
47 // A platform wrapper for a Direct2D brush that makes sure the underlying | 70 // A platform wrapper for a Direct2D brush that makes sure the underlying |
48 // ID2D1Brush COM object is released when this object is destroyed. | 71 // ID2D1Brush COM object is released when this object is destroyed. |
49 class Direct2DBrush : public gfx::Brush { | 72 class Direct2DBrush : public gfx::Brush { |
50 public: | 73 public: |
51 explicit Direct2DBrush(ID2D1Brush* brush) : brush_(brush) { | 74 explicit Direct2DBrush(ID2D1Brush* brush) : brush_(brush) { |
52 } | 75 } |
53 | 76 |
54 ID2D1Brush* brush() const { return brush_.get(); } | 77 ID2D1Brush* brush() const { return brush_.get(); } |
55 | 78 |
56 private: | 79 private: |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 interop_rt_.release(); | 296 interop_rt_.release(); |
274 } | 297 } |
275 | 298 |
276 Brush* CanvasDirect2D::CreateLinearGradientBrush( | 299 Brush* CanvasDirect2D::CreateLinearGradientBrush( |
277 const gfx::Point& start_point, | 300 const gfx::Point& start_point, |
278 const gfx::Point& end_point, | 301 const gfx::Point& end_point, |
279 const SkColor colors[], | 302 const SkColor colors[], |
280 const float positions[], | 303 const float positions[], |
281 size_t position_count, | 304 size_t position_count, |
282 TileMode tile_mode) { | 305 TileMode tile_mode) { |
283 ID2D1GradientStopCollection* gradient_stop_collection = NULL; | 306 ScopedComPtr<ID2D1GradientStopCollection> gradient_stop_collection( |
284 D2D1_GRADIENT_STOP* gradient_stops = new D2D1_GRADIENT_STOP[position_count]; | 307 CreateGradientStopCollection(rt_, colors, positions, position_count, |
285 for (size_t i = 0; i < position_count; ++i) { | 308 tile_mode)); |
286 gradient_stops[i].color = SkColorToColorF(colors[i]); | 309 if (!gradient_stop_collection.get()) |
287 gradient_stops[i].position = positions[i]; | |
288 } | |
289 HRESULT hr = rt_->CreateGradientStopCollection(gradient_stops, | |
290 position_count, | |
291 D2D1_GAMMA_2_2, | |
292 TileModeToExtendMode(tile_mode), | |
293 &gradient_stop_collection); | |
294 if (FAILED(hr)) | |
295 return NULL; | 310 return NULL; |
296 | 311 |
297 ID2D1LinearGradientBrush* brush = NULL; | 312 ID2D1LinearGradientBrush* brush = NULL; |
298 hr = rt_->CreateLinearGradientBrush( | 313 HRESULT hr = rt_->CreateLinearGradientBrush( |
299 D2D1::LinearGradientBrushProperties(PointToPoint2F(start_point), | 314 D2D1::LinearGradientBrushProperties(PointToPoint2F(start_point), |
300 PointToPoint2F(end_point)), | 315 PointToPoint2F(end_point)), |
301 gradient_stop_collection, | 316 gradient_stop_collection, |
302 &brush); | 317 &brush); |
| 318 return SUCCEEDED(hr) ? new Direct2DBrush(brush) : NULL; |
| 319 } |
303 | 320 |
304 return new Direct2DBrush(brush); | 321 Brush* CanvasDirect2D::CreateRadialGradientBrush( |
| 322 const gfx::Point& center_point, |
| 323 float radius, |
| 324 const SkColor colors[], |
| 325 const float positions[], |
| 326 size_t position_count, |
| 327 TileMode tile_mode) { |
| 328 ScopedComPtr<ID2D1GradientStopCollection> gradient_stop_collection( |
| 329 CreateGradientStopCollection(rt_, colors, positions, position_count, |
| 330 tile_mode)); |
| 331 if (!gradient_stop_collection.get()) |
| 332 return NULL; |
| 333 |
| 334 ID2D1RadialGradientBrush* brush = NULL; |
| 335 HRESULT hr = rt_->CreateRadialGradientBrush( |
| 336 D2D1::RadialGradientBrushProperties(PointToPoint2F(center_point), |
| 337 PointToPoint2F(gfx::Point()), |
| 338 radius, |
| 339 radius), |
| 340 gradient_stop_collection, |
| 341 &brush); |
| 342 return SUCCEEDED(hr) ? new Direct2DBrush(brush) : NULL; |
| 343 } |
| 344 |
| 345 Brush* CanvasDirect2D::CreateBitmapBrush( |
| 346 const SkBitmap& bitmap, |
| 347 TileMode tile_mode_x, |
| 348 TileMode tile_mode_y) { |
| 349 ScopedComPtr<ID2D1Bitmap> d2d1_bitmap; |
| 350 HRESULT hr = rt_->CreateBitmap( |
| 351 D2D1::SizeU(bitmap.width(), bitmap.height()), |
| 352 NULL, |
| 353 NULL, |
| 354 D2D1::BitmapProperties( |
| 355 D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, |
| 356 D2D1_ALPHA_MODE_IGNORE)), |
| 357 d2d1_bitmap.Receive()); |
| 358 bitmap.lockPixels(); |
| 359 d2d1_bitmap->CopyFromMemory(NULL, bitmap.getPixels(), bitmap.rowBytes()); |
| 360 bitmap.unlockPixels(); |
| 361 |
| 362 ID2D1BitmapBrush* brush = NULL; |
| 363 hr = rt_->CreateBitmapBrush( |
| 364 d2d1_bitmap, |
| 365 D2D1::BitmapBrushProperties(TileModeToExtendMode(tile_mode_x), |
| 366 TileModeToExtendMode(tile_mode_y)), |
| 367 D2D1::BrushProperties(), |
| 368 &brush); |
| 369 return SUCCEEDED(hr) ? new Direct2DBrush(brush) : NULL; |
305 } | 370 } |
306 | 371 |
307 CanvasSkia* CanvasDirect2D::AsCanvasSkia() { | 372 CanvasSkia* CanvasDirect2D::AsCanvasSkia() { |
308 return NULL; | 373 return NULL; |
309 } | 374 } |
310 | 375 |
311 const CanvasSkia* CanvasDirect2D::AsCanvasSkia() const { | 376 const CanvasSkia* CanvasDirect2D::AsCanvasSkia() const { |
312 return NULL; | 377 return NULL; |
313 } | 378 } |
314 | 379 |
315 //////////////////////////////////////////////////////////////////////////////// | 380 //////////////////////////////////////////////////////////////////////////////// |
316 // CanvasDirect2D, private: | 381 // CanvasDirect2D, private: |
317 | 382 |
318 void CanvasDirect2D::SaveInternal(ID2D1Layer* layer) { | 383 void CanvasDirect2D::SaveInternal(ID2D1Layer* layer) { |
319 if (!drawing_state_block_) | 384 if (!drawing_state_block_) |
320 GetD2D1Factory()->CreateDrawingStateBlock(drawing_state_block_.Receive()); | 385 GetD2D1Factory()->CreateDrawingStateBlock(drawing_state_block_.Receive()); |
321 rt_->SaveDrawingState(drawing_state_block_.get()); | 386 rt_->SaveDrawingState(drawing_state_block_.get()); |
322 state_.push(RenderState(layer)); | 387 state_.push(RenderState(layer)); |
323 } | 388 } |
324 | 389 |
325 } // namespace gfx | 390 } // namespace gfx |
OLD | NEW |