OLD | NEW |
| (Empty) |
1 // Copyright 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "webkit/renderer/compositor_bindings/web_layer_impl.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/debug/trace_event_impl.h" | |
9 #include "base/lazy_instance.h" | |
10 #include "base/strings/string_util.h" | |
11 #include "base/threading/thread_checker.h" | |
12 #include "cc/animation/animation.h" | |
13 #include "cc/base/region.h" | |
14 #include "cc/base/switches.h" | |
15 #include "cc/layers/layer.h" | |
16 #include "cc/layers/layer_position_constraint.h" | |
17 #include "cc/trees/layer_tree_host.h" | |
18 #include "third_party/WebKit/public/platform/WebFloatPoint.h" | |
19 #include "third_party/WebKit/public/platform/WebFloatRect.h" | |
20 #include "third_party/WebKit/public/platform/WebGraphicsLayerDebugInfo.h" | |
21 #include "third_party/WebKit/public/platform/WebLayerClient.h" | |
22 #include "third_party/WebKit/public/platform/WebLayerPositionConstraint.h" | |
23 #include "third_party/WebKit/public/platform/WebLayerScrollClient.h" | |
24 #include "third_party/WebKit/public/platform/WebSize.h" | |
25 #include "third_party/skia/include/utils/SkMatrix44.h" | |
26 #include "webkit/renderer/compositor_bindings/web_animation_impl.h" | |
27 #include "webkit/renderer/compositor_bindings/web_blend_mode.h" | |
28 #include "webkit/renderer/compositor_bindings/web_filter_operations_impl.h" | |
29 #include "webkit/renderer/compositor_bindings/web_to_cc_animation_delegate_adapt
er.h" | |
30 | |
31 using cc::Animation; | |
32 using cc::Layer; | |
33 using blink::WebLayer; | |
34 using blink::WebFloatPoint; | |
35 using blink::WebVector; | |
36 using blink::WebRect; | |
37 using blink::WebSize; | |
38 using blink::WebColor; | |
39 using blink::WebFilterOperations; | |
40 | |
41 namespace webkit { | |
42 namespace { | |
43 | |
44 bool g_impl_side_painting_enabled = false; | |
45 | |
46 } // namespace | |
47 | |
48 WebLayerImpl::WebLayerImpl() : layer_(Layer::Create()) { | |
49 web_layer_client_ = NULL; | |
50 layer_->SetLayerClient(this); | |
51 } | |
52 | |
53 WebLayerImpl::WebLayerImpl(scoped_refptr<Layer> layer) : layer_(layer) { | |
54 web_layer_client_ = NULL; | |
55 layer_->SetLayerClient(this); | |
56 } | |
57 | |
58 WebLayerImpl::~WebLayerImpl() { | |
59 layer_->ClearRenderSurface(); | |
60 layer_->set_layer_animation_delegate(NULL); | |
61 web_layer_client_ = NULL; | |
62 } | |
63 | |
64 // static | |
65 bool WebLayerImpl::UsingPictureLayer() { | |
66 return g_impl_side_painting_enabled; | |
67 } | |
68 | |
69 // static | |
70 void WebLayerImpl::SetImplSidePaintingEnabled(bool enabled) { | |
71 g_impl_side_painting_enabled = enabled; | |
72 } | |
73 | |
74 int WebLayerImpl::id() const { return layer_->id(); } | |
75 | |
76 void WebLayerImpl::invalidateRect(const blink::WebFloatRect& rect) { | |
77 layer_->SetNeedsDisplayRect(rect); | |
78 } | |
79 | |
80 void WebLayerImpl::invalidate() { layer_->SetNeedsDisplay(); } | |
81 | |
82 void WebLayerImpl::addChild(WebLayer* child) { | |
83 layer_->AddChild(static_cast<WebLayerImpl*>(child)->layer()); | |
84 } | |
85 | |
86 void WebLayerImpl::insertChild(WebLayer* child, size_t index) { | |
87 layer_->InsertChild(static_cast<WebLayerImpl*>(child)->layer(), index); | |
88 } | |
89 | |
90 void WebLayerImpl::replaceChild(WebLayer* reference, WebLayer* new_layer) { | |
91 layer_->ReplaceChild(static_cast<WebLayerImpl*>(reference)->layer(), | |
92 static_cast<WebLayerImpl*>(new_layer)->layer()); | |
93 } | |
94 | |
95 void WebLayerImpl::removeFromParent() { layer_->RemoveFromParent(); } | |
96 | |
97 void WebLayerImpl::removeAllChildren() { layer_->RemoveAllChildren(); } | |
98 | |
99 void WebLayerImpl::setBounds(const WebSize& size) { layer_->SetBounds(size); } | |
100 | |
101 WebSize WebLayerImpl::bounds() const { return layer_->bounds(); } | |
102 | |
103 void WebLayerImpl::setMasksToBounds(bool masks_to_bounds) { | |
104 layer_->SetMasksToBounds(masks_to_bounds); | |
105 } | |
106 | |
107 bool WebLayerImpl::masksToBounds() const { return layer_->masks_to_bounds(); } | |
108 | |
109 void WebLayerImpl::setMaskLayer(WebLayer* maskLayer) { | |
110 layer_->SetMaskLayer( | |
111 maskLayer ? static_cast<WebLayerImpl*>(maskLayer)->layer() : 0); | |
112 } | |
113 | |
114 void WebLayerImpl::setReplicaLayer(WebLayer* replica_layer) { | |
115 layer_->SetReplicaLayer( | |
116 replica_layer ? static_cast<WebLayerImpl*>(replica_layer)->layer() : 0); | |
117 } | |
118 | |
119 void WebLayerImpl::setOpacity(float opacity) { layer_->SetOpacity(opacity); } | |
120 | |
121 float WebLayerImpl::opacity() const { return layer_->opacity(); } | |
122 | |
123 void WebLayerImpl::setBlendMode(blink::WebBlendMode blend_mode) { | |
124 layer_->SetBlendMode(BlendModeToSkia(blend_mode)); | |
125 } | |
126 | |
127 blink::WebBlendMode WebLayerImpl::blendMode() const { | |
128 return BlendModeFromSkia(layer_->blend_mode()); | |
129 } | |
130 | |
131 void WebLayerImpl::setIsRootForIsolatedGroup(bool isolate) { | |
132 layer_->SetIsRootForIsolatedGroup(isolate); | |
133 } | |
134 | |
135 bool WebLayerImpl::isRootForIsolatedGroup() { | |
136 return layer_->is_root_for_isolated_group(); | |
137 } | |
138 | |
139 void WebLayerImpl::setOpaque(bool opaque) { layer_->SetContentsOpaque(opaque); } | |
140 | |
141 bool WebLayerImpl::opaque() const { return layer_->contents_opaque(); } | |
142 | |
143 void WebLayerImpl::setPosition(const WebFloatPoint& position) { | |
144 layer_->SetPosition(position); | |
145 } | |
146 | |
147 WebFloatPoint WebLayerImpl::position() const { return layer_->position(); } | |
148 | |
149 void WebLayerImpl::setTransform(const SkMatrix44& matrix) { | |
150 gfx::Transform transform; | |
151 transform.matrix() = matrix; | |
152 layer_->SetTransform(transform); | |
153 } | |
154 | |
155 void WebLayerImpl::setTransformOrigin(const blink::WebFloatPoint3D& point) { | |
156 gfx::Point3F gfx_point = point; | |
157 layer_->SetTransformOrigin(gfx_point); | |
158 } | |
159 | |
160 blink::WebFloatPoint3D WebLayerImpl::transformOrigin() const { | |
161 return layer_->transform_origin(); | |
162 } | |
163 | |
164 void WebLayerImpl::setAnchorPoint(const blink::WebFloatPoint&) {} | |
165 | |
166 blink::WebFloatPoint WebLayerImpl::anchorPoint() const { | |
167 return blink::WebFloatPoint(); | |
168 } | |
169 | |
170 void WebLayerImpl::setAnchorPointZ(float) {} | |
171 | |
172 float WebLayerImpl::anchorPointZ() const { | |
173 return 0.f; | |
174 }; | |
175 | |
176 SkMatrix44 WebLayerImpl::transform() const { | |
177 return layer_->transform().matrix(); | |
178 } | |
179 | |
180 void WebLayerImpl::setDrawsContent(bool draws_content) { | |
181 layer_->SetIsDrawable(draws_content); | |
182 } | |
183 | |
184 bool WebLayerImpl::drawsContent() const { return layer_->DrawsContent(); } | |
185 | |
186 void WebLayerImpl::setShouldFlattenTransform(bool flatten) { | |
187 layer_->SetShouldFlattenTransform(flatten); | |
188 } | |
189 | |
190 void WebLayerImpl::setRenderingContext(int context) { | |
191 layer_->SetIs3dSorted(context != 0); | |
192 } | |
193 | |
194 void WebLayerImpl::setUseParentBackfaceVisibility( | |
195 bool use_parent_backface_visibility) { | |
196 layer_->set_use_parent_backface_visibility(use_parent_backface_visibility); | |
197 } | |
198 | |
199 void WebLayerImpl::setBackgroundColor(WebColor color) { | |
200 layer_->SetBackgroundColor(color); | |
201 } | |
202 | |
203 WebColor WebLayerImpl::backgroundColor() const { | |
204 return layer_->background_color(); | |
205 } | |
206 | |
207 void WebLayerImpl::setFilters(const WebFilterOperations& filters) { | |
208 const WebFilterOperationsImpl& filters_impl = | |
209 static_cast<const WebFilterOperationsImpl&>(filters); | |
210 layer_->SetFilters(filters_impl.AsFilterOperations()); | |
211 } | |
212 | |
213 void WebLayerImpl::setBackgroundFilters(const WebFilterOperations& filters) { | |
214 const WebFilterOperationsImpl& filters_impl = | |
215 static_cast<const WebFilterOperationsImpl&>(filters); | |
216 layer_->SetBackgroundFilters(filters_impl.AsFilterOperations()); | |
217 } | |
218 | |
219 void WebLayerImpl::setAnimationDelegate( | |
220 blink::WebAnimationDelegate* delegate) { | |
221 animation_delegate_adapter_.reset( | |
222 new WebToCCAnimationDelegateAdapter(delegate)); | |
223 layer_->set_layer_animation_delegate(animation_delegate_adapter_.get()); | |
224 } | |
225 | |
226 bool WebLayerImpl::addAnimation(blink::WebAnimation* animation) { | |
227 bool result = layer_->AddAnimation( | |
228 static_cast<WebAnimationImpl*>(animation)->PassAnimation()); | |
229 delete animation; | |
230 return result; | |
231 } | |
232 | |
233 void WebLayerImpl::removeAnimation(int animation_id) { | |
234 layer_->RemoveAnimation(animation_id); | |
235 } | |
236 | |
237 void WebLayerImpl::removeAnimation( | |
238 int animation_id, | |
239 blink::WebAnimation::TargetProperty target_property) { | |
240 layer_->layer_animation_controller()->RemoveAnimation( | |
241 animation_id, | |
242 static_cast<Animation::TargetProperty>(target_property)); | |
243 } | |
244 | |
245 void WebLayerImpl::pauseAnimation(int animation_id, double time_offset) { | |
246 layer_->PauseAnimation(animation_id, time_offset); | |
247 } | |
248 | |
249 bool WebLayerImpl::hasActiveAnimation() { return layer_->HasActiveAnimation(); } | |
250 | |
251 void WebLayerImpl::setForceRenderSurface(bool force_render_surface) { | |
252 layer_->SetForceRenderSurface(force_render_surface); | |
253 } | |
254 | |
255 void WebLayerImpl::setScrollPosition(blink::WebPoint position) { | |
256 layer_->SetScrollOffset(gfx::Point(position).OffsetFromOrigin()); | |
257 } | |
258 | |
259 blink::WebPoint WebLayerImpl::scrollPosition() const { | |
260 return gfx::PointAtOffsetFromOrigin(layer_->scroll_offset()); | |
261 } | |
262 | |
263 void WebLayerImpl::setScrollClipLayer(WebLayer* clip_layer) { | |
264 if (!clip_layer) { | |
265 layer_->SetScrollClipLayerId(Layer::INVALID_ID); | |
266 return; | |
267 } | |
268 layer_->SetScrollClipLayerId(clip_layer->id()); | |
269 } | |
270 | |
271 bool WebLayerImpl::scrollable() const { return layer_->scrollable(); } | |
272 | |
273 void WebLayerImpl::setUserScrollable(bool horizontal, bool vertical) { | |
274 layer_->SetUserScrollable(horizontal, vertical); | |
275 } | |
276 | |
277 bool WebLayerImpl::userScrollableHorizontal() const { | |
278 return layer_->user_scrollable_horizontal(); | |
279 } | |
280 | |
281 bool WebLayerImpl::userScrollableVertical() const { | |
282 return layer_->user_scrollable_vertical(); | |
283 } | |
284 | |
285 void WebLayerImpl::setHaveWheelEventHandlers(bool have_wheel_event_handlers) { | |
286 layer_->SetHaveWheelEventHandlers(have_wheel_event_handlers); | |
287 } | |
288 | |
289 bool WebLayerImpl::haveWheelEventHandlers() const { | |
290 return layer_->have_wheel_event_handlers(); | |
291 } | |
292 | |
293 void WebLayerImpl::setHaveScrollEventHandlers(bool have_scroll_event_handlers) { | |
294 layer_->SetHaveScrollEventHandlers(have_scroll_event_handlers); | |
295 } | |
296 | |
297 bool WebLayerImpl::haveScrollEventHandlers() const { | |
298 return layer_->have_scroll_event_handlers(); | |
299 } | |
300 | |
301 void WebLayerImpl::setShouldScrollOnMainThread( | |
302 bool should_scroll_on_main_thread) { | |
303 layer_->SetShouldScrollOnMainThread(should_scroll_on_main_thread); | |
304 } | |
305 | |
306 bool WebLayerImpl::shouldScrollOnMainThread() const { | |
307 return layer_->should_scroll_on_main_thread(); | |
308 } | |
309 | |
310 void WebLayerImpl::setNonFastScrollableRegion(const WebVector<WebRect>& rects) { | |
311 cc::Region region; | |
312 for (size_t i = 0; i < rects.size(); ++i) | |
313 region.Union(rects[i]); | |
314 layer_->SetNonFastScrollableRegion(region); | |
315 } | |
316 | |
317 WebVector<WebRect> WebLayerImpl::nonFastScrollableRegion() const { | |
318 size_t num_rects = 0; | |
319 for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region()); | |
320 region_rects.has_rect(); | |
321 region_rects.next()) | |
322 ++num_rects; | |
323 | |
324 WebVector<WebRect> result(num_rects); | |
325 size_t i = 0; | |
326 for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region()); | |
327 region_rects.has_rect(); | |
328 region_rects.next()) { | |
329 result[i] = region_rects.rect(); | |
330 ++i; | |
331 } | |
332 return result; | |
333 } | |
334 | |
335 void WebLayerImpl::setTouchEventHandlerRegion(const WebVector<WebRect>& rects) { | |
336 cc::Region region; | |
337 for (size_t i = 0; i < rects.size(); ++i) | |
338 region.Union(rects[i]); | |
339 layer_->SetTouchEventHandlerRegion(region); | |
340 } | |
341 | |
342 WebVector<WebRect> WebLayerImpl::touchEventHandlerRegion() const { | |
343 size_t num_rects = 0; | |
344 for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region()); | |
345 region_rects.has_rect(); | |
346 region_rects.next()) | |
347 ++num_rects; | |
348 | |
349 WebVector<WebRect> result(num_rects); | |
350 size_t i = 0; | |
351 for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region()); | |
352 region_rects.has_rect(); | |
353 region_rects.next()) { | |
354 result[i] = region_rects.rect(); | |
355 ++i; | |
356 } | |
357 return result; | |
358 } | |
359 | |
360 void WebLayerImpl::setIsContainerForFixedPositionLayers(bool enable) { | |
361 layer_->SetIsContainerForFixedPositionLayers(enable); | |
362 } | |
363 | |
364 bool WebLayerImpl::isContainerForFixedPositionLayers() const { | |
365 return layer_->IsContainerForFixedPositionLayers(); | |
366 } | |
367 | |
368 static blink::WebLayerPositionConstraint ToWebLayerPositionConstraint( | |
369 const cc::LayerPositionConstraint& constraint) { | |
370 blink::WebLayerPositionConstraint web_constraint; | |
371 web_constraint.isFixedPosition = constraint.is_fixed_position(); | |
372 web_constraint.isFixedToRightEdge = constraint.is_fixed_to_right_edge(); | |
373 web_constraint.isFixedToBottomEdge = constraint.is_fixed_to_bottom_edge(); | |
374 return web_constraint; | |
375 } | |
376 | |
377 static cc::LayerPositionConstraint ToLayerPositionConstraint( | |
378 const blink::WebLayerPositionConstraint& web_constraint) { | |
379 cc::LayerPositionConstraint constraint; | |
380 constraint.set_is_fixed_position(web_constraint.isFixedPosition); | |
381 constraint.set_is_fixed_to_right_edge(web_constraint.isFixedToRightEdge); | |
382 constraint.set_is_fixed_to_bottom_edge(web_constraint.isFixedToBottomEdge); | |
383 return constraint; | |
384 } | |
385 | |
386 void WebLayerImpl::setPositionConstraint( | |
387 const blink::WebLayerPositionConstraint& constraint) { | |
388 layer_->SetPositionConstraint(ToLayerPositionConstraint(constraint)); | |
389 } | |
390 | |
391 blink::WebLayerPositionConstraint WebLayerImpl::positionConstraint() const { | |
392 return ToWebLayerPositionConstraint(layer_->position_constraint()); | |
393 } | |
394 | |
395 void WebLayerImpl::setScrollClient( | |
396 blink::WebLayerScrollClient* scroll_client) { | |
397 if (scroll_client) { | |
398 layer_->set_did_scroll_callback( | |
399 base::Bind(&blink::WebLayerScrollClient::didScroll, | |
400 base::Unretained(scroll_client))); | |
401 } else { | |
402 layer_->set_did_scroll_callback(base::Closure()); | |
403 } | |
404 } | |
405 | |
406 bool WebLayerImpl::isOrphan() const { return !layer_->layer_tree_host(); } | |
407 | |
408 void WebLayerImpl::setWebLayerClient(blink::WebLayerClient* client) { | |
409 web_layer_client_ = client; | |
410 } | |
411 | |
412 class TracedDebugInfo : public base::debug::ConvertableToTraceFormat { | |
413 public: | |
414 // This object takes ownership of the debug_info object. | |
415 explicit TracedDebugInfo(blink::WebGraphicsLayerDebugInfo* debug_info) : | |
416 debug_info_(debug_info) {} | |
417 virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE { | |
418 DCHECK(thread_checker_.CalledOnValidThread()); | |
419 blink::WebString web_string; | |
420 debug_info_->appendAsTraceFormat(&web_string); | |
421 out->append(web_string.utf8()); | |
422 } | |
423 private: | |
424 virtual ~TracedDebugInfo() {} | |
425 scoped_ptr<blink::WebGraphicsLayerDebugInfo> debug_info_; | |
426 base::ThreadChecker thread_checker_; | |
427 }; | |
428 | |
429 scoped_refptr<base::debug::ConvertableToTraceFormat> | |
430 WebLayerImpl::TakeDebugInfo() { | |
431 if (!web_layer_client_) | |
432 return NULL; | |
433 blink::WebGraphicsLayerDebugInfo* debug_info = | |
434 web_layer_client_->takeDebugInfoFor(this); | |
435 | |
436 if (debug_info) | |
437 return new TracedDebugInfo(debug_info); | |
438 else | |
439 return NULL; | |
440 } | |
441 | |
442 void WebLayerImpl::setScrollParent(blink::WebLayer* parent) { | |
443 cc::Layer* scroll_parent = NULL; | |
444 if (parent) | |
445 scroll_parent = static_cast<WebLayerImpl*>(parent)->layer(); | |
446 layer_->SetScrollParent(scroll_parent); | |
447 } | |
448 | |
449 void WebLayerImpl::setClipParent(blink::WebLayer* parent) { | |
450 cc::Layer* clip_parent = NULL; | |
451 if (parent) | |
452 clip_parent = static_cast<WebLayerImpl*>(parent)->layer(); | |
453 layer_->SetClipParent(clip_parent); | |
454 } | |
455 | |
456 Layer* WebLayerImpl::layer() const { return layer_.get(); } | |
457 | |
458 } // namespace webkit | |
OLD | NEW |