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.
5 #include "cc/blink/web_layer_impl.h"
10 #include "base/bind.h"
11 #include "base/lazy_instance.h"
12 #include "base/strings/string_util.h"
13 #include "base/threading/thread_checker.h"
14 #include "base/trace_event/trace_event_impl.h"
15 #include "cc/animation/animation.h"
16 #include "cc/base/region.h"
17 #include "cc/base/switches.h"
18 #include "cc/blink/web_animation_impl.h"
19 #include "cc/blink/web_blend_mode.h"
20 #include "cc/blink/web_filter_operations_impl.h"
21 #include "cc/blink/web_to_cc_animation_delegate_adapter.h"
22 #include "cc/layers/layer.h"
23 #include "cc/layers/layer_position_constraint.h"
24 #include "cc/trees/layer_tree_host.h"
25 #include "third_party/WebKit/public/platform/WebFloatPoint.h"
26 #include "third_party/WebKit/public/platform/WebFloatRect.h"
27 #include "third_party/WebKit/public/platform/WebGraphicsLayerDebugInfo.h"
28 #include "third_party/WebKit/public/platform/WebLayerClient.h"
29 #include "third_party/WebKit/public/platform/WebLayerPositionConstraint.h"
30 #include "third_party/WebKit/public/platform/WebLayerScrollClient.h"
31 #include "third_party/WebKit/public/platform/WebSize.h"
32 #include "third_party/skia/include/utils/SkMatrix44.h"
33 #include "ui/gfx/geometry/rect_conversions.h"
34 #include "ui/gfx/geometry/vector2d_conversions.h"
38 using blink::WebLayer
;
39 using blink::WebFloatPoint
;
40 using blink::WebVector
;
43 using blink::WebColor
;
44 using blink::WebFilterOperations
;
49 base::LazyInstance
<cc::LayerSettings
> g_layer_settings
=
50 LAZY_INSTANCE_INITIALIZER
;
54 WebLayerImpl::WebLayerImpl() : layer_(Layer::Create(LayerSettings())) {
55 web_layer_client_
= nullptr;
56 layer_
->SetLayerClient(this);
59 WebLayerImpl::WebLayerImpl(scoped_refptr
<Layer
> layer
) : layer_(layer
) {
60 web_layer_client_
= nullptr;
61 layer_
->SetLayerClient(this);
64 WebLayerImpl::~WebLayerImpl() {
65 if (animation_delegate_adapter_
.get())
66 layer_
->set_layer_animation_delegate(nullptr);
67 web_layer_client_
= nullptr;
71 void WebLayerImpl::SetLayerSettings(const cc::LayerSettings
& settings
) {
72 g_layer_settings
.Get() = settings
;
76 const cc::LayerSettings
& WebLayerImpl::LayerSettings() {
77 return g_layer_settings
.Get();
80 int WebLayerImpl::id() const {
84 void WebLayerImpl::invalidateRect(const blink::WebRect
& rect
) {
85 layer_
->SetNeedsDisplayRect(rect
);
88 void WebLayerImpl::invalidate() {
89 layer_
->SetNeedsDisplay();
92 void WebLayerImpl::addChild(WebLayer
* child
) {
93 layer_
->AddChild(static_cast<WebLayerImpl
*>(child
)->layer());
96 void WebLayerImpl::insertChild(WebLayer
* child
, size_t index
) {
97 layer_
->InsertChild(static_cast<WebLayerImpl
*>(child
)->layer(), index
);
100 void WebLayerImpl::replaceChild(WebLayer
* reference
, WebLayer
* new_layer
) {
101 layer_
->ReplaceChild(static_cast<WebLayerImpl
*>(reference
)->layer(),
102 static_cast<WebLayerImpl
*>(new_layer
)->layer());
105 void WebLayerImpl::removeFromParent() {
106 layer_
->RemoveFromParent();
109 void WebLayerImpl::removeAllChildren() {
110 layer_
->RemoveAllChildren();
113 void WebLayerImpl::setBounds(const WebSize
& size
) {
114 layer_
->SetBounds(size
);
117 WebSize
WebLayerImpl::bounds() const {
118 return layer_
->bounds();
121 void WebLayerImpl::setMasksToBounds(bool masks_to_bounds
) {
122 layer_
->SetMasksToBounds(masks_to_bounds
);
125 bool WebLayerImpl::masksToBounds() const {
126 return layer_
->masks_to_bounds();
129 void WebLayerImpl::setMaskLayer(WebLayer
* maskLayer
) {
130 layer_
->SetMaskLayer(
131 maskLayer
? static_cast<WebLayerImpl
*>(maskLayer
)->layer() : 0);
134 void WebLayerImpl::setReplicaLayer(WebLayer
* replica_layer
) {
135 layer_
->SetReplicaLayer(
136 replica_layer
? static_cast<WebLayerImpl
*>(replica_layer
)->layer() : 0);
139 void WebLayerImpl::setOpacity(float opacity
) {
140 layer_
->SetOpacity(opacity
);
143 float WebLayerImpl::opacity() const {
144 return layer_
->opacity();
147 void WebLayerImpl::setBlendMode(blink::WebBlendMode blend_mode
) {
148 layer_
->SetBlendMode(BlendModeToSkia(blend_mode
));
151 blink::WebBlendMode
WebLayerImpl::blendMode() const {
152 return BlendModeFromSkia(layer_
->blend_mode());
155 void WebLayerImpl::setIsRootForIsolatedGroup(bool isolate
) {
156 layer_
->SetIsRootForIsolatedGroup(isolate
);
159 bool WebLayerImpl::isRootForIsolatedGroup() {
160 return layer_
->is_root_for_isolated_group();
163 void WebLayerImpl::setOpaque(bool opaque
) {
164 layer_
->SetContentsOpaque(opaque
);
167 bool WebLayerImpl::opaque() const {
168 return layer_
->contents_opaque();
171 void WebLayerImpl::setPosition(const WebFloatPoint
& position
) {
172 layer_
->SetPosition(position
);
175 WebFloatPoint
WebLayerImpl::position() const {
176 return layer_
->position();
179 void WebLayerImpl::setTransform(const SkMatrix44
& matrix
) {
180 gfx::Transform transform
;
181 transform
.matrix() = matrix
;
182 layer_
->SetTransform(transform
);
185 void WebLayerImpl::setTransformOrigin(const blink::WebFloatPoint3D
& point
) {
186 gfx::Point3F gfx_point
= point
;
187 layer_
->SetTransformOrigin(gfx_point
);
190 blink::WebFloatPoint3D
WebLayerImpl::transformOrigin() const {
191 return layer_
->transform_origin();
194 SkMatrix44
WebLayerImpl::transform() const {
195 return layer_
->transform().matrix();
198 void WebLayerImpl::setDrawsContent(bool draws_content
) {
199 layer_
->SetIsDrawable(draws_content
);
202 bool WebLayerImpl::drawsContent() const {
203 return layer_
->DrawsContent();
206 void WebLayerImpl::setShouldFlattenTransform(bool flatten
) {
207 layer_
->SetShouldFlattenTransform(flatten
);
210 void WebLayerImpl::setRenderingContext(int context
) {
211 layer_
->Set3dSortingContextId(context
);
214 void WebLayerImpl::setUseParentBackfaceVisibility(
215 bool use_parent_backface_visibility
) {
216 layer_
->set_use_parent_backface_visibility(use_parent_backface_visibility
);
219 void WebLayerImpl::setBackgroundColor(WebColor color
) {
220 layer_
->SetBackgroundColor(color
);
223 WebColor
WebLayerImpl::backgroundColor() const {
224 return layer_
->background_color();
227 void WebLayerImpl::setFilters(const WebFilterOperations
& filters
) {
228 const WebFilterOperationsImpl
& filters_impl
=
229 static_cast<const WebFilterOperationsImpl
&>(filters
);
230 layer_
->SetFilters(filters_impl
.AsFilterOperations());
233 void WebLayerImpl::setBackgroundFilters(const WebFilterOperations
& filters
) {
234 const WebFilterOperationsImpl
& filters_impl
=
235 static_cast<const WebFilterOperationsImpl
&>(filters
);
236 layer_
->SetBackgroundFilters(filters_impl
.AsFilterOperations());
239 void WebLayerImpl::setAnimationDelegate(
240 blink::WebCompositorAnimationDelegate
* delegate
) {
241 animation_delegate_adapter_
.reset(
242 new WebToCCAnimationDelegateAdapter(delegate
));
243 layer_
->set_layer_animation_delegate(animation_delegate_adapter_
.get());
246 bool WebLayerImpl::addAnimation(blink::WebCompositorAnimation
* animation
) {
247 bool result
= layer_
->AddAnimation(
248 static_cast<WebCompositorAnimationImpl
*>(animation
)->PassAnimation());
253 void WebLayerImpl::removeAnimation(int animation_id
) {
254 layer_
->RemoveAnimation(animation_id
);
257 void WebLayerImpl::removeAnimation(
259 blink::WebCompositorAnimation::TargetProperty target_property
) {
260 layer_
->RemoveAnimation(
261 animation_id
, static_cast<Animation::TargetProperty
>(target_property
));
264 void WebLayerImpl::pauseAnimation(int animation_id
, double time_offset
) {
265 layer_
->PauseAnimation(animation_id
, time_offset
);
268 bool WebLayerImpl::hasActiveAnimation() {
269 return layer_
->HasActiveAnimation();
272 void WebLayerImpl::setForceRenderSurface(bool force_render_surface
) {
273 layer_
->SetForceRenderSurface(force_render_surface
);
276 void WebLayerImpl::setScrollPositionDouble(blink::WebDoublePoint position
) {
277 layer_
->SetScrollOffset(gfx::ScrollOffset(position
.x
, position
.y
));
280 void WebLayerImpl::setScrollCompensationAdjustment(
281 blink::WebDoublePoint position
) {
282 layer_
->SetScrollCompensationAdjustment(
283 gfx::Vector2dF(position
.x
, position
.y
));
286 blink::WebDoublePoint
WebLayerImpl::scrollPositionDouble() const {
287 return blink::WebDoublePoint(layer_
->scroll_offset().x(),
288 layer_
->scroll_offset().y());
291 void WebLayerImpl::setScrollClipLayer(WebLayer
* clip_layer
) {
293 layer_
->SetScrollClipLayerId(Layer::INVALID_ID
);
296 layer_
->SetScrollClipLayerId(clip_layer
->id());
299 bool WebLayerImpl::scrollable() const {
300 return layer_
->scrollable();
303 void WebLayerImpl::setUserScrollable(bool horizontal
, bool vertical
) {
304 layer_
->SetUserScrollable(horizontal
, vertical
);
307 bool WebLayerImpl::userScrollableHorizontal() const {
308 return layer_
->user_scrollable_horizontal();
311 bool WebLayerImpl::userScrollableVertical() const {
312 return layer_
->user_scrollable_vertical();
315 void WebLayerImpl::setHaveWheelEventHandlers(bool have_wheel_event_handlers
) {
316 layer_
->SetHaveWheelEventHandlers(have_wheel_event_handlers
);
319 bool WebLayerImpl::haveWheelEventHandlers() const {
320 return layer_
->have_wheel_event_handlers();
323 void WebLayerImpl::setHaveScrollEventHandlers(bool have_scroll_event_handlers
) {
324 layer_
->SetHaveScrollEventHandlers(have_scroll_event_handlers
);
327 bool WebLayerImpl::haveScrollEventHandlers() const {
328 return layer_
->have_scroll_event_handlers();
331 void WebLayerImpl::setShouldScrollOnMainThread(
332 bool should_scroll_on_main_thread
) {
333 layer_
->SetShouldScrollOnMainThread(should_scroll_on_main_thread
);
336 bool WebLayerImpl::shouldScrollOnMainThread() const {
337 return layer_
->should_scroll_on_main_thread();
340 void WebLayerImpl::setNonFastScrollableRegion(const WebVector
<WebRect
>& rects
) {
342 for (size_t i
= 0; i
< rects
.size(); ++i
)
343 region
.Union(rects
[i
]);
344 layer_
->SetNonFastScrollableRegion(region
);
347 WebVector
<WebRect
> WebLayerImpl::nonFastScrollableRegion() const {
348 size_t num_rects
= 0;
349 for (cc::Region::Iterator
region_rects(layer_
->non_fast_scrollable_region());
350 region_rects
.has_rect();
354 WebVector
<WebRect
> result(num_rects
);
356 for (cc::Region::Iterator
region_rects(layer_
->non_fast_scrollable_region());
357 region_rects
.has_rect();
358 region_rects
.next()) {
359 result
[i
] = region_rects
.rect();
365 void WebLayerImpl::setFrameTimingRequests(
366 const WebVector
<std::pair
<int64_t, WebRect
>>& requests
) {
367 std::vector
<cc::FrameTimingRequest
> frame_timing_requests(requests
.size());
368 for (size_t i
= 0; i
< requests
.size(); ++i
) {
369 frame_timing_requests
[i
] = cc::FrameTimingRequest(
370 requests
[i
].first
, gfx::Rect(requests
[i
].second
));
372 layer_
->SetFrameTimingRequests(frame_timing_requests
);
375 WebVector
<std::pair
<int64_t, WebRect
>> WebLayerImpl::frameTimingRequests()
377 const std::vector
<cc::FrameTimingRequest
>& frame_timing_requests
=
378 layer_
->FrameTimingRequests();
380 size_t num_requests
= frame_timing_requests
.size();
382 WebVector
<std::pair
<int64_t, WebRect
>> result(num_requests
);
383 for (size_t i
= 0; i
< num_requests
; ++i
) {
384 result
[i
] = std::make_pair(frame_timing_requests
[i
].id(),
385 frame_timing_requests
[i
].rect());
390 void WebLayerImpl::setTouchEventHandlerRegion(const WebVector
<WebRect
>& rects
) {
392 for (size_t i
= 0; i
< rects
.size(); ++i
)
393 region
.Union(rects
[i
]);
394 layer_
->SetTouchEventHandlerRegion(region
);
397 WebVector
<WebRect
> WebLayerImpl::touchEventHandlerRegion() const {
398 size_t num_rects
= 0;
399 for (cc::Region::Iterator
region_rects(layer_
->touch_event_handler_region());
400 region_rects
.has_rect();
404 WebVector
<WebRect
> result(num_rects
);
406 for (cc::Region::Iterator
region_rects(layer_
->touch_event_handler_region());
407 region_rects
.has_rect();
408 region_rects
.next()) {
409 result
[i
] = region_rects
.rect();
415 static_assert(static_cast<ScrollBlocksOn
>(blink::WebScrollBlocksOnNone
) ==
416 SCROLL_BLOCKS_ON_NONE
,
417 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
418 static_assert(static_cast<ScrollBlocksOn
>(blink::WebScrollBlocksOnStartTouch
) ==
419 SCROLL_BLOCKS_ON_START_TOUCH
,
420 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
421 static_assert(static_cast<ScrollBlocksOn
>(blink::WebScrollBlocksOnWheelEvent
) ==
422 SCROLL_BLOCKS_ON_WHEEL_EVENT
,
423 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
425 static_cast<ScrollBlocksOn
>(blink::WebScrollBlocksOnScrollEvent
) ==
426 SCROLL_BLOCKS_ON_SCROLL_EVENT
,
427 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
429 void WebLayerImpl::setScrollBlocksOn(blink::WebScrollBlocksOn blocks
) {
430 layer_
->SetScrollBlocksOn(static_cast<ScrollBlocksOn
>(blocks
));
433 blink::WebScrollBlocksOn
WebLayerImpl::scrollBlocksOn() const {
434 return static_cast<blink::WebScrollBlocksOn
>(layer_
->scroll_blocks_on());
437 void WebLayerImpl::setIsContainerForFixedPositionLayers(bool enable
) {
438 layer_
->SetIsContainerForFixedPositionLayers(enable
);
441 bool WebLayerImpl::isContainerForFixedPositionLayers() const {
442 return layer_
->IsContainerForFixedPositionLayers();
445 static blink::WebLayerPositionConstraint
ToWebLayerPositionConstraint(
446 const cc::LayerPositionConstraint
& constraint
) {
447 blink::WebLayerPositionConstraint web_constraint
;
448 web_constraint
.isFixedPosition
= constraint
.is_fixed_position();
449 web_constraint
.isFixedToRightEdge
= constraint
.is_fixed_to_right_edge();
450 web_constraint
.isFixedToBottomEdge
= constraint
.is_fixed_to_bottom_edge();
451 return web_constraint
;
454 static cc::LayerPositionConstraint
ToLayerPositionConstraint(
455 const blink::WebLayerPositionConstraint
& web_constraint
) {
456 cc::LayerPositionConstraint constraint
;
457 constraint
.set_is_fixed_position(web_constraint
.isFixedPosition
);
458 constraint
.set_is_fixed_to_right_edge(web_constraint
.isFixedToRightEdge
);
459 constraint
.set_is_fixed_to_bottom_edge(web_constraint
.isFixedToBottomEdge
);
463 void WebLayerImpl::setPositionConstraint(
464 const blink::WebLayerPositionConstraint
& constraint
) {
465 layer_
->SetPositionConstraint(ToLayerPositionConstraint(constraint
));
468 blink::WebLayerPositionConstraint
WebLayerImpl::positionConstraint() const {
469 return ToWebLayerPositionConstraint(layer_
->position_constraint());
472 void WebLayerImpl::setScrollClient(blink::WebLayerScrollClient
* scroll_client
) {
474 layer_
->set_did_scroll_callback(
475 base::Bind(&blink::WebLayerScrollClient::didScroll
,
476 base::Unretained(scroll_client
)));
478 layer_
->set_did_scroll_callback(base::Closure());
482 bool WebLayerImpl::isOrphan() const {
483 return !layer_
->layer_tree_host();
486 void WebLayerImpl::setWebLayerClient(blink::WebLayerClient
* client
) {
487 web_layer_client_
= client
;
490 class TracedDebugInfo
: public base::trace_event::ConvertableToTraceFormat
{
492 // This object takes ownership of the debug_info object.
493 explicit TracedDebugInfo(blink::WebGraphicsLayerDebugInfo
* debug_info
)
494 : debug_info_(debug_info
) {}
495 void AppendAsTraceFormat(std::string
* out
) const override
{
496 DCHECK(thread_checker_
.CalledOnValidThread());
497 blink::WebString web_string
;
498 debug_info_
->appendAsTraceFormat(&web_string
);
499 out
->append(web_string
.utf8());
503 ~TracedDebugInfo() override
{}
504 scoped_ptr
<blink::WebGraphicsLayerDebugInfo
> debug_info_
;
505 base::ThreadChecker thread_checker_
;
508 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
509 WebLayerImpl::TakeDebugInfo() {
510 if (!web_layer_client_
)
512 blink::WebGraphicsLayerDebugInfo
* debug_info
=
513 web_layer_client_
->takeDebugInfoFor(this);
516 return new TracedDebugInfo(debug_info
);
521 void WebLayerImpl::setScrollParent(blink::WebLayer
* parent
) {
522 cc::Layer
* scroll_parent
= nullptr;
524 scroll_parent
= static_cast<WebLayerImpl
*>(parent
)->layer();
525 layer_
->SetScrollParent(scroll_parent
);
528 void WebLayerImpl::setClipParent(blink::WebLayer
* parent
) {
529 cc::Layer
* clip_parent
= nullptr;
531 clip_parent
= static_cast<WebLayerImpl
*>(parent
)->layer();
532 layer_
->SetClipParent(clip_parent
);
535 Layer
* WebLayerImpl::layer() const {
539 } // namespace cc_blink