OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "math.h" | 5 #include "math.h" |
6 | 6 |
7 #include "sky/engine/config.h" | 7 #include "sky/engine/config.h" |
8 #include "sky/engine/core/painting/Canvas.h" | 8 #include "sky/engine/core/painting/Canvas.h" |
9 | 9 |
10 #include "sky/engine/core/dom/Document.h" | 10 #include "sky/engine/core/dom/Document.h" |
11 #include "sky/engine/core/dom/Element.h" | 11 #include "sky/engine/core/dom/Element.h" |
12 #include "sky/engine/core/painting/CanvasImage.h" | 12 #include "sky/engine/core/painting/CanvasImage.h" |
13 #include "sky/engine/core/painting/PaintingTasks.h" | 13 #include "sky/engine/core/painting/PaintingTasks.h" |
14 #include "sky/engine/platform/geometry/IntRect.h" | 14 #include "sky/engine/platform/geometry/IntRect.h" |
15 #include "third_party/skia/include/core/SkCanvas.h" | 15 #include "third_party/skia/include/core/SkCanvas.h" |
16 #include "third_party/skia/include/core/SkBitmap.h" | 16 #include "third_party/skia/include/core/SkBitmap.h" |
17 | 17 |
18 namespace blink { | 18 namespace blink { |
19 | 19 |
20 Canvas::Canvas(const FloatSize& size) | 20 Canvas::Canvas(SkCanvas* skCanvas) |
21 : m_size(size) | 21 : m_canvas(skCanvas) |
22 { | 22 { |
23 m_displayList = adoptRef(new DisplayList); | |
24 m_canvas = m_displayList->beginRecording(expandedIntSize(m_size)); | |
25 } | 23 } |
26 | 24 |
27 Canvas::~Canvas() | 25 Canvas::~Canvas() |
28 { | 26 { |
29 } | 27 } |
30 | 28 |
31 void Canvas::save() | 29 void Canvas::save() |
32 { | 30 { |
33 if (!m_canvas) | 31 if (!m_canvas) |
34 return; | 32 return; |
35 ASSERT(m_displayList->isRecording()); | |
36 m_canvas->save(); | 33 m_canvas->save(); |
37 } | 34 } |
38 | 35 |
39 void Canvas::saveLayer(const Rect& bounds, const Paint* paint) | 36 void Canvas::saveLayer(const Rect& bounds, const Paint* paint) |
40 { | 37 { |
41 if (!m_canvas) | 38 if (!m_canvas) |
42 return; | 39 return; |
43 ASSERT(m_displayList->isRecording()); | |
44 m_canvas->saveLayer(!bounds.is_null ? &bounds.sk_rect : nullptr, | 40 m_canvas->saveLayer(!bounds.is_null ? &bounds.sk_rect : nullptr, |
45 paint ? &paint->paint() : nullptr); | 41 paint ? &paint->paint() : nullptr); |
46 } | 42 } |
47 | 43 |
48 void Canvas::restore() | 44 void Canvas::restore() |
49 { | 45 { |
50 if (!m_canvas) | 46 if (!m_canvas) |
51 return; | 47 return; |
52 ASSERT(m_displayList->isRecording()); | |
53 m_canvas->restore(); | 48 m_canvas->restore(); |
54 } | 49 } |
55 | 50 |
56 void Canvas::translate(float dx, float dy) | 51 void Canvas::translate(float dx, float dy) |
57 { | 52 { |
58 if (!m_canvas) | 53 if (!m_canvas) |
59 return; | 54 return; |
60 ASSERT(m_displayList->isRecording()); | |
61 m_canvas->translate(dx, dy); | 55 m_canvas->translate(dx, dy); |
62 } | 56 } |
63 | 57 |
64 void Canvas::scale(float sx, float sy) | 58 void Canvas::scale(float sx, float sy) |
65 { | 59 { |
66 if (!m_canvas) | 60 if (!m_canvas) |
67 return; | 61 return; |
68 ASSERT(m_displayList->isRecording()); | |
69 m_canvas->scale(sx, sy); | 62 m_canvas->scale(sx, sy); |
70 } | 63 } |
71 | 64 |
72 void Canvas::rotate(float radians) | 65 void Canvas::rotate(float radians) |
73 { | 66 { |
74 if (!m_canvas) | 67 if (!m_canvas) |
75 return; | 68 return; |
76 ASSERT(m_displayList->isRecording()); | |
77 m_canvas->rotate(radians * 180.0/M_PI); | 69 m_canvas->rotate(radians * 180.0/M_PI); |
78 } | 70 } |
79 | 71 |
80 void Canvas::skew(float sx, float sy) | 72 void Canvas::skew(float sx, float sy) |
81 { | 73 { |
82 if (!m_canvas) | 74 if (!m_canvas) |
83 return; | 75 return; |
84 ASSERT(m_displayList->isRecording()); | |
85 m_canvas->skew(sx, sy); | 76 m_canvas->skew(sx, sy); |
86 } | 77 } |
87 | 78 |
88 void Canvas::concat(const Float32List& matrix4) | 79 void Canvas::concat(const Float32List& matrix4) |
89 { | 80 { |
90 if (!m_canvas) | 81 if (!m_canvas) |
91 return; | 82 return; |
92 ASSERT(m_displayList->isRecording()); | |
93 ASSERT(matrix4.data()); | 83 ASSERT(matrix4.data()); |
94 | 84 |
95 // TODO(mpcomplete): how can we raise an error in this case? | 85 // TODO(mpcomplete): how can we raise an error in this case? |
96 if (matrix4.num_elements() != 16) | 86 if (matrix4.num_elements() != 16) |
97 return; | 87 return; |
98 | 88 |
99 SkMatrix sk_matrix; | 89 SkMatrix sk_matrix; |
100 // Mappings from SkMatrix-index to input-index. | 90 // Mappings from SkMatrix-index to input-index. |
101 static const int kMappings[] = { | 91 static const int kMappings[] = { |
102 0, 4, 12, | 92 0, 4, 12, |
103 1, 5, 13, | 93 1, 5, 13, |
104 3, 7, 15, | 94 3, 7, 15, |
105 }; | 95 }; |
106 for (intptr_t i = 0; i < 9; ++i) | 96 for (intptr_t i = 0; i < 9; ++i) |
107 sk_matrix[i] = matrix4.data()[kMappings[i]]; | 97 sk_matrix[i] = matrix4.data()[kMappings[i]]; |
108 | 98 |
109 m_canvas->concat(sk_matrix); | 99 m_canvas->concat(sk_matrix); |
110 } | 100 } |
111 | 101 |
112 void Canvas::clipRect(const Rect& rect) | 102 void Canvas::clipRect(const Rect& rect) |
113 { | 103 { |
114 if (!m_canvas) | 104 if (!m_canvas) |
115 return; | 105 return; |
116 ASSERT(m_displayList->isRecording()); | |
117 m_canvas->clipRect(rect.sk_rect); | 106 m_canvas->clipRect(rect.sk_rect); |
118 } | 107 } |
119 | 108 |
120 void Canvas::clipRRect(const RRect* rrect) | 109 void Canvas::clipRRect(const RRect* rrect) |
121 { | 110 { |
122 if (!m_canvas) | 111 if (!m_canvas) |
123 return; | 112 return; |
124 ASSERT(m_displayList->isRecording()); | |
125 m_canvas->clipRRect(rrect->rrect()); | 113 m_canvas->clipRRect(rrect->rrect()); |
126 } | 114 } |
127 | 115 |
128 void Canvas::clipPath(const CanvasPath* path) | 116 void Canvas::clipPath(const CanvasPath* path) |
129 { | 117 { |
130 if (!m_canvas) | 118 if (!m_canvas) |
131 return; | 119 return; |
132 ASSERT(m_displayList->isRecording()); | |
133 m_canvas->clipPath(path->path()); | 120 m_canvas->clipPath(path->path()); |
134 } | 121 } |
135 | 122 |
136 void Canvas::drawLine(float x0, float y0, float x1, float y1, const Paint* paint
) | 123 void Canvas::drawLine(float x0, float y0, float x1, float y1, const Paint* paint
) |
137 { | 124 { |
138 if (!m_canvas) | 125 if (!m_canvas) |
139 return; | 126 return; |
140 ASSERT(paint); | 127 ASSERT(paint); |
141 ASSERT(m_displayList->isRecording()); | |
142 m_canvas->drawLine(x0, y0, x1, y1, paint->paint()); | 128 m_canvas->drawLine(x0, y0, x1, y1, paint->paint()); |
143 } | 129 } |
144 | 130 |
145 void Canvas::drawPicture(Picture* picture) | 131 void Canvas::drawPicture(Picture* picture) |
146 { | 132 { |
147 if (!m_canvas) | 133 if (!m_canvas) |
148 return; | 134 return; |
149 ASSERT(picture); | 135 ASSERT(picture); |
150 ASSERT(m_displayList->isRecording()); | |
151 m_canvas->drawPicture(picture->toSkia()); | 136 m_canvas->drawPicture(picture->toSkia()); |
152 } | 137 } |
153 | 138 |
154 void Canvas::drawPaint(const Paint* paint) | 139 void Canvas::drawPaint(const Paint* paint) |
155 { | 140 { |
156 if (!m_canvas) | 141 if (!m_canvas) |
157 return; | 142 return; |
158 ASSERT(paint); | 143 ASSERT(paint); |
159 ASSERT(m_displayList->isRecording()); | |
160 m_canvas->drawPaint(paint->paint()); | 144 m_canvas->drawPaint(paint->paint()); |
161 } | 145 } |
162 | 146 |
163 void Canvas::drawRect(const Rect& rect, const Paint* paint) | 147 void Canvas::drawRect(const Rect& rect, const Paint* paint) |
164 { | 148 { |
165 if (!m_canvas) | 149 if (!m_canvas) |
166 return; | 150 return; |
167 ASSERT(paint); | 151 ASSERT(paint); |
168 ASSERT(m_displayList->isRecording()); | |
169 m_canvas->drawRect(rect.sk_rect, paint->paint()); | 152 m_canvas->drawRect(rect.sk_rect, paint->paint()); |
170 } | 153 } |
171 | 154 |
172 void Canvas::drawRRect(const RRect* rrect, const Paint* paint) | 155 void Canvas::drawRRect(const RRect* rrect, const Paint* paint) |
173 { | 156 { |
174 if (!m_canvas) | 157 if (!m_canvas) |
175 return; | 158 return; |
176 ASSERT(rrect); | 159 ASSERT(rrect); |
177 ASSERT(paint); | 160 ASSERT(paint); |
178 ASSERT(m_displayList->isRecording()); | |
179 m_canvas->drawRRect(rrect->rrect(), paint->paint()); | 161 m_canvas->drawRRect(rrect->rrect(), paint->paint()); |
180 } | 162 } |
181 | 163 |
182 void Canvas::drawOval(const Rect& rect, const Paint* paint) | 164 void Canvas::drawOval(const Rect& rect, const Paint* paint) |
183 { | 165 { |
184 if (!m_canvas) | 166 if (!m_canvas) |
185 return; | 167 return; |
186 ASSERT(paint); | 168 ASSERT(paint); |
187 ASSERT(m_displayList->isRecording()); | |
188 m_canvas->drawOval(rect.sk_rect, paint->paint()); | 169 m_canvas->drawOval(rect.sk_rect, paint->paint()); |
189 } | 170 } |
190 | 171 |
191 void Canvas::drawCircle(float x, float y, float radius, const Paint* paint) | 172 void Canvas::drawCircle(float x, float y, float radius, const Paint* paint) |
192 { | 173 { |
193 if (!m_canvas) | 174 if (!m_canvas) |
194 return; | 175 return; |
195 ASSERT(paint); | 176 ASSERT(paint); |
196 ASSERT(m_displayList->isRecording()); | |
197 m_canvas->drawCircle(x, y, radius, paint->paint()); | 177 m_canvas->drawCircle(x, y, radius, paint->paint()); |
198 } | 178 } |
199 | 179 |
200 void Canvas::drawPath(const CanvasPath* path, const Paint* paint) | 180 void Canvas::drawPath(const CanvasPath* path, const Paint* paint) |
201 { | 181 { |
202 if (!m_canvas) | 182 if (!m_canvas) |
203 return; | 183 return; |
204 ASSERT(path); | 184 ASSERT(path); |
205 ASSERT(paint); | 185 ASSERT(paint); |
206 ASSERT(m_displayList->isRecording()); | |
207 m_canvas->drawPath(path->path(), paint->paint()); | 186 m_canvas->drawPath(path->path(), paint->paint()); |
208 } | 187 } |
209 | 188 |
210 void Canvas::drawImage(const CanvasImage* image, | 189 void Canvas::drawImage(const CanvasImage* image, |
211 float x, | 190 float x, |
212 float y, | 191 float y, |
213 const Paint* paint) { | 192 const Paint* paint) { |
214 if (!m_canvas) | 193 if (!m_canvas) |
215 return; | 194 return; |
216 ASSERT(image); | 195 ASSERT(image); |
217 ASSERT(m_displayList->isRecording()); | |
218 m_canvas->drawBitmap(image->bitmap(), x, y, &paint->paint()); | 196 m_canvas->drawBitmap(image->bitmap(), x, y, &paint->paint()); |
219 } | 197 } |
220 | 198 |
221 PassRefPtr<DisplayList> Canvas::finishRecording() | |
222 { | |
223 if (!isRecording()) | |
224 return nullptr; | |
225 m_canvas = nullptr; | |
226 m_displayList->endRecording(); | |
227 return m_displayList.release(); | |
228 } | |
229 | |
230 } // namespace blink | 199 } // namespace blink |
OLD | NEW |