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