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 "gfx/rect.h" | 7 #include "gfx/rect.h" |
8 | 8 |
9 namespace { | 9 namespace { |
10 | 10 |
(...skipping 18 matching lines...) Expand all Loading... |
29 | 29 |
30 namespace gfx { | 30 namespace gfx { |
31 | 31 |
32 // static | 32 // static |
33 ID2D1Factory* CanvasDirect2D::d2d1_factory_ = NULL; | 33 ID2D1Factory* CanvasDirect2D::d2d1_factory_ = NULL; |
34 | 34 |
35 //////////////////////////////////////////////////////////////////////////////// | 35 //////////////////////////////////////////////////////////////////////////////// |
36 // CanvasDirect2D, public: | 36 // CanvasDirect2D, public: |
37 | 37 |
38 CanvasDirect2D::CanvasDirect2D(ID2D1RenderTarget* rt) : rt_(rt) { | 38 CanvasDirect2D::CanvasDirect2D(ID2D1RenderTarget* rt) : rt_(rt) { |
| 39 // A RenderState entry is pushed onto the stack to track the clip count prior |
| 40 // to any calls to Save*(). |
| 41 state_.push(RenderState()); |
39 rt_->BeginDraw(); | 42 rt_->BeginDraw(); |
40 } | 43 } |
41 | 44 |
42 CanvasDirect2D::~CanvasDirect2D() { | 45 CanvasDirect2D::~CanvasDirect2D() { |
| 46 // Unwind any clips that were pushed outside of any Save*()/Restore() pairs. |
| 47 int clip_count = state_.top().clip_count; |
| 48 for (int i = 0; i < clip_count; ++i) |
| 49 rt_->PopAxisAlignedClip(); |
43 rt_->EndDraw(); | 50 rt_->EndDraw(); |
44 } | 51 } |
45 | 52 |
46 // static | 53 // static |
47 ID2D1Factory* CanvasDirect2D::GetD2D1Factory() { | 54 ID2D1Factory* CanvasDirect2D::GetD2D1Factory() { |
48 if (!d2d1_factory_) | 55 if (!d2d1_factory_) |
49 D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &d2d1_factory_); | 56 D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &d2d1_factory_); |
50 return d2d1_factory_; | 57 return d2d1_factory_; |
51 } | 58 } |
52 | 59 |
53 //////////////////////////////////////////////////////////////////////////////// | 60 //////////////////////////////////////////////////////////////////////////////// |
54 // CanvasDirect2D, Canvas implementation: | 61 // CanvasDirect2D, Canvas implementation: |
55 | 62 |
56 void CanvasDirect2D::Save() { | 63 void CanvasDirect2D::Save() { |
57 if (!drawing_state_block_) | 64 SaveInternal(NULL); |
58 GetD2D1Factory()->CreateDrawingStateBlock(drawing_state_block_.Receive()); | |
59 rt_->SaveDrawingState(drawing_state_block_.get()); | |
60 layers_.push(NULL); | |
61 } | 65 } |
62 | 66 |
63 void CanvasDirect2D::SaveLayerAlpha(uint8 alpha) { | 67 void CanvasDirect2D::SaveLayerAlpha(uint8 alpha) { |
64 SaveLayerAlpha(alpha, gfx::Rect()); | 68 SaveLayerAlpha(alpha, gfx::Rect()); |
65 } | 69 } |
66 | 70 |
67 void CanvasDirect2D::SaveLayerAlpha(uint8 alpha, | 71 void CanvasDirect2D::SaveLayerAlpha(uint8 alpha, |
68 const gfx::Rect& layer_bounds) { | 72 const gfx::Rect& layer_bounds) { |
69 D2D1_RECT_F bounds = D2D1::InfiniteRect(); | 73 D2D1_RECT_F bounds = D2D1::InfiniteRect(); |
70 if (!layer_bounds.IsEmpty()) | 74 if (!layer_bounds.IsEmpty()) |
71 bounds = RectToRectF(layer_bounds); | 75 bounds = RectToRectF(layer_bounds); |
72 ID2D1Layer* layer = NULL; | 76 ID2D1Layer* layer = NULL; |
73 HRESULT hr = rt_->CreateLayer(NULL, &layer); | 77 HRESULT hr = rt_->CreateLayer(NULL, &layer); |
74 if (SUCCEEDED(hr)) { | 78 if (SUCCEEDED(hr)) { |
75 rt_->PushLayer(D2D1::LayerParameters(bounds, | 79 rt_->PushLayer(D2D1::LayerParameters(bounds, |
76 NULL, | 80 NULL, |
77 D2D1_ANTIALIAS_MODE_PER_PRIMITIVE, | 81 D2D1_ANTIALIAS_MODE_PER_PRIMITIVE, |
78 D2D1::IdentityMatrix(), | 82 D2D1::IdentityMatrix(), |
79 static_cast<float>(alpha) / 0xFF, | 83 static_cast<float>(alpha) / 0xFF, |
80 NULL, | 84 NULL, |
81 D2D1_LAYER_OPTIONS_NONE), | 85 D2D1_LAYER_OPTIONS_NONE), |
82 layer); | 86 layer); |
83 } | 87 } |
84 layers_.push(layer); | 88 SaveInternal(layer); |
85 } | 89 } |
86 | 90 |
87 void CanvasDirect2D::Restore() { | 91 void CanvasDirect2D::Restore() { |
88 ID2D1Layer* layer = layers_.top(); | 92 ID2D1Layer* layer = state_.top().layer; |
89 if (layer) { | 93 if (layer) { |
90 rt_->PopLayer(); | 94 rt_->PopLayer(); |
91 layer->Release(); | 95 layer->Release(); |
92 } | 96 } |
93 layers_.pop(); | 97 |
| 98 int clip_count = state_.top().clip_count; |
| 99 for (int i = 0; i < clip_count; ++i) |
| 100 rt_->PopAxisAlignedClip(); |
| 101 |
| 102 state_.pop(); |
| 103 // The state_ stack should never be empty - we should always have at least one |
| 104 // entry to hold a clip count when there is no active save/restore entry. |
| 105 CHECK(!state_.empty()) << "Called Restore() once too often!"; |
| 106 |
94 rt_->RestoreDrawingState(drawing_state_block_); | 107 rt_->RestoreDrawingState(drawing_state_block_); |
95 } | 108 } |
96 | 109 |
97 bool CanvasDirect2D::GetClipRect(gfx::Rect* clip_rect) { | |
98 return false; | |
99 } | |
100 | |
101 bool CanvasDirect2D::ClipRectInt(int x, int y, int w, int h) { | 110 bool CanvasDirect2D::ClipRectInt(int x, int y, int w, int h) { |
102 return false; | 111 rt_->PushAxisAlignedClip(RectToRectF(x, y, w, h), |
103 } | 112 D2D1_ANTIALIAS_MODE_PER_PRIMITIVE); |
104 | 113 // Increment the clip count so the call to PushAxisAlignedClip() can be |
105 bool CanvasDirect2D::IntersectsClipRectInt(int x, int y, int w, int h) { | 114 // balanced with a call to PopAxisAlignedClip in the next Restore(). |
106 return false; | 115 ++state_.top().clip_count; |
| 116 return w > 0 && h > 0; |
107 } | 117 } |
108 | 118 |
109 void CanvasDirect2D::TranslateInt(int x, int y) { | 119 void CanvasDirect2D::TranslateInt(int x, int y) { |
| 120 rt_->SetTransform(D2D1::Matrix3x2F::Translation(static_cast<float>(x), |
| 121 static_cast<float>(y))); |
110 } | 122 } |
111 | 123 |
112 void CanvasDirect2D::ScaleInt(int x, int y) { | 124 void CanvasDirect2D::ScaleInt(int x, int y) { |
| 125 rt_->SetTransform(D2D1::Matrix3x2F::Scale(static_cast<float>(x), |
| 126 static_cast<float>(y))); |
113 } | 127 } |
114 | 128 |
115 void CanvasDirect2D::FillRectInt(int x, int y, int w, int h, | 129 void CanvasDirect2D::FillRectInt(int x, int y, int w, int h, |
116 const SkPaint& paint) { | 130 const SkPaint& paint) { |
117 } | 131 } |
118 | 132 |
119 void CanvasDirect2D::FillRectInt(const SkColor& color, int x, int y, int w, | 133 void CanvasDirect2D::FillRectInt(const SkColor& color, int x, int y, int w, |
120 int h) { | 134 int h) { |
121 ScopedComPtr<ID2D1SolidColorBrush> solid_brush; | 135 ScopedComPtr<ID2D1SolidColorBrush> solid_brush; |
122 rt_->CreateSolidColorBrush(SkColorToColorF(color), solid_brush.Receive()); | 136 rt_->CreateSolidColorBrush(SkColorToColorF(color), solid_brush.Receive()); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 } | 223 } |
210 | 224 |
211 CanvasSkia* CanvasDirect2D::AsCanvasSkia() { | 225 CanvasSkia* CanvasDirect2D::AsCanvasSkia() { |
212 return NULL; | 226 return NULL; |
213 } | 227 } |
214 | 228 |
215 const CanvasSkia* CanvasDirect2D::AsCanvasSkia() const { | 229 const CanvasSkia* CanvasDirect2D::AsCanvasSkia() const { |
216 return NULL; | 230 return NULL; |
217 } | 231 } |
218 | 232 |
| 233 //////////////////////////////////////////////////////////////////////////////// |
| 234 // CanvasDirect2D, private: |
| 235 |
| 236 void CanvasDirect2D::SaveInternal(ID2D1Layer* layer) { |
| 237 if (!drawing_state_block_) |
| 238 GetD2D1Factory()->CreateDrawingStateBlock(drawing_state_block_.Receive()); |
| 239 rt_->SaveDrawingState(drawing_state_block_.get()); |
| 240 state_.push(RenderState(layer)); |
| 241 } |
| 242 |
219 } // namespace gfx | 243 } // namespace gfx |
OLD | NEW |