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"
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/blink/web_animation_impl.h"
16 #include "cc/blink/web_blend_mode.h"
17 #include "cc/blink/web_filter_operations_impl.h"
18 #include "cc/blink/web_to_cc_animation_delegate_adapter.h"
19 #include "cc/layers/layer.h"
20 #include "cc/layers/layer_position_constraint.h"
21 #include "cc/trees/layer_tree_host.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 #include "ui/gfx/geometry/rect_conversions.h"
31 #include "ui/gfx/geometry/vector2d_conversions.h"
35 using blink::WebLayer
;
36 using blink::WebFloatPoint
;
37 using blink::WebVector
;
40 using blink::WebColor
;
41 using blink::WebFilterOperations
;
46 bool g_impl_side_painting_enabled
= false;
50 WebLayerImpl::WebLayerImpl() : layer_(Layer::Create()) {
51 web_layer_client_
= nullptr;
52 layer_
->SetLayerClient(this);
55 WebLayerImpl::WebLayerImpl(scoped_refptr
<Layer
> layer
) : layer_(layer
) {
56 web_layer_client_
= nullptr;
57 layer_
->SetLayerClient(this);
60 WebLayerImpl::~WebLayerImpl() {
61 layer_
->ClearRenderSurface();
62 layer_
->set_layer_animation_delegate(nullptr);
63 web_layer_client_
= nullptr;
67 bool WebLayerImpl::UsingPictureLayer() {
68 return g_impl_side_painting_enabled
;
72 void WebLayerImpl::SetImplSidePaintingEnabled(bool enabled
) {
73 g_impl_side_painting_enabled
= enabled
;
76 int WebLayerImpl::id() const {
80 void WebLayerImpl::invalidateRect(const blink::WebRect
& rect
) {
81 layer_
->SetNeedsDisplayRect(rect
);
84 void WebLayerImpl::invalidate() {
85 layer_
->SetNeedsDisplay();
88 void WebLayerImpl::addChild(WebLayer
* child
) {
89 layer_
->AddChild(static_cast<WebLayerImpl
*>(child
)->layer());
92 void WebLayerImpl::insertChild(WebLayer
* child
, size_t index
) {
93 layer_
->InsertChild(static_cast<WebLayerImpl
*>(child
)->layer(), index
);
96 void WebLayerImpl::replaceChild(WebLayer
* reference
, WebLayer
* new_layer
) {
97 layer_
->ReplaceChild(static_cast<WebLayerImpl
*>(reference
)->layer(),
98 static_cast<WebLayerImpl
*>(new_layer
)->layer());
101 void WebLayerImpl::removeFromParent() {
102 layer_
->RemoveFromParent();
105 void WebLayerImpl::removeAllChildren() {
106 layer_
->RemoveAllChildren();
109 void WebLayerImpl::setBounds(const WebSize
& size
) {
110 layer_
->SetBounds(size
);
113 WebSize
WebLayerImpl::bounds() const {
114 return layer_
->bounds();
117 void WebLayerImpl::setMasksToBounds(bool masks_to_bounds
) {
118 layer_
->SetMasksToBounds(masks_to_bounds
);
121 bool WebLayerImpl::masksToBounds() const {
122 return layer_
->masks_to_bounds();
125 void WebLayerImpl::setMaskLayer(WebLayer
* maskLayer
) {
126 layer_
->SetMaskLayer(
127 maskLayer
? static_cast<WebLayerImpl
*>(maskLayer
)->layer() : 0);
130 void WebLayerImpl::setReplicaLayer(WebLayer
* replica_layer
) {
131 layer_
->SetReplicaLayer(
132 replica_layer
? static_cast<WebLayerImpl
*>(replica_layer
)->layer() : 0);
135 void WebLayerImpl::setOpacity(float opacity
) {
136 layer_
->SetOpacity(opacity
);
139 float WebLayerImpl::opacity() const {
140 return layer_
->opacity();
143 void WebLayerImpl::setBlendMode(blink::WebBlendMode blend_mode
) {
144 layer_
->SetBlendMode(BlendModeToSkia(blend_mode
));
147 blink::WebBlendMode
WebLayerImpl::blendMode() const {
148 return BlendModeFromSkia(layer_
->blend_mode());
151 void WebLayerImpl::setIsRootForIsolatedGroup(bool isolate
) {
152 layer_
->SetIsRootForIsolatedGroup(isolate
);
155 bool WebLayerImpl::isRootForIsolatedGroup() {
156 return layer_
->is_root_for_isolated_group();
159 void WebLayerImpl::setOpaque(bool opaque
) {
160 layer_
->SetContentsOpaque(opaque
);
163 bool WebLayerImpl::opaque() const {
164 return layer_
->contents_opaque();
167 void WebLayerImpl::setPosition(const WebFloatPoint
& position
) {
168 layer_
->SetPosition(position
);
171 WebFloatPoint
WebLayerImpl::position() const {
172 return layer_
->position();
175 void WebLayerImpl::setTransform(const SkMatrix44
& matrix
) {
176 gfx::Transform transform
;
177 transform
.matrix() = matrix
;
178 layer_
->SetTransform(transform
);
181 void WebLayerImpl::setTransformOrigin(const blink::WebFloatPoint3D
& point
) {
182 gfx::Point3F gfx_point
= point
;
183 layer_
->SetTransformOrigin(gfx_point
);
186 blink::WebFloatPoint3D
WebLayerImpl::transformOrigin() const {
187 return layer_
->transform_origin();
190 SkMatrix44
WebLayerImpl::transform() const {
191 return layer_
->transform().matrix();
194 void WebLayerImpl::setDrawsContent(bool draws_content
) {
195 layer_
->SetIsDrawable(draws_content
);
198 bool WebLayerImpl::drawsContent() const {
199 return layer_
->DrawsContent();
202 void WebLayerImpl::setShouldFlattenTransform(bool flatten
) {
203 layer_
->SetShouldFlattenTransform(flatten
);
206 void WebLayerImpl::setRenderingContext(int context
) {
207 layer_
->Set3dSortingContextId(context
);
210 void WebLayerImpl::setUseParentBackfaceVisibility(
211 bool use_parent_backface_visibility
) {
212 layer_
->set_use_parent_backface_visibility(use_parent_backface_visibility
);
215 void WebLayerImpl::setBackgroundColor(WebColor color
) {
216 layer_
->SetBackgroundColor(color
);
219 WebColor
WebLayerImpl::backgroundColor() const {
220 return layer_
->background_color();
223 void WebLayerImpl::setFilters(const WebFilterOperations
& filters
) {
224 const WebFilterOperationsImpl
& filters_impl
=
225 static_cast<const WebFilterOperationsImpl
&>(filters
);
226 layer_
->SetFilters(filters_impl
.AsFilterOperations());
229 void WebLayerImpl::setBackgroundFilters(const WebFilterOperations
& filters
) {
230 const WebFilterOperationsImpl
& filters_impl
=
231 static_cast<const WebFilterOperationsImpl
&>(filters
);
232 layer_
->SetBackgroundFilters(filters_impl
.AsFilterOperations());
235 void WebLayerImpl::setAnimationDelegate(
236 blink::WebCompositorAnimationDelegate
* delegate
) {
237 animation_delegate_adapter_
.reset(
238 new WebToCCAnimationDelegateAdapter(delegate
));
239 layer_
->set_layer_animation_delegate(animation_delegate_adapter_
.get());
242 bool WebLayerImpl::addAnimation(blink::WebCompositorAnimation
* animation
) {
243 bool result
= layer_
->AddAnimation(
244 static_cast<WebCompositorAnimationImpl
*>(animation
)->PassAnimation());
249 void WebLayerImpl::removeAnimation(int animation_id
) {
250 layer_
->RemoveAnimation(animation_id
);
253 void WebLayerImpl::removeAnimation(
255 blink::WebCompositorAnimation::TargetProperty target_property
) {
256 layer_
->layer_animation_controller()->RemoveAnimation(
257 animation_id
, static_cast<Animation::TargetProperty
>(target_property
));
260 void WebLayerImpl::pauseAnimation(int animation_id
, double time_offset
) {
261 layer_
->PauseAnimation(animation_id
, time_offset
);
264 bool WebLayerImpl::hasActiveAnimation() {
265 return layer_
->HasActiveAnimation();
268 void WebLayerImpl::setForceRenderSurface(bool force_render_surface
) {
269 layer_
->SetForceRenderSurface(force_render_surface
);
272 void WebLayerImpl::setScrollPositionDouble(blink::WebDoublePoint position
) {
273 layer_
->SetScrollOffset(gfx::ScrollOffset(position
.x
, position
.y
));
276 blink::WebDoublePoint
WebLayerImpl::scrollPositionDouble() const {
277 return blink::WebDoublePoint(layer_
->scroll_offset().x(),
278 layer_
->scroll_offset().y());
281 void WebLayerImpl::setScrollClipLayer(WebLayer
* clip_layer
) {
283 layer_
->SetScrollClipLayerId(Layer::INVALID_ID
);
286 layer_
->SetScrollClipLayerId(clip_layer
->id());
289 bool WebLayerImpl::scrollable() const {
290 return layer_
->scrollable();
293 void WebLayerImpl::setUserScrollable(bool horizontal
, bool vertical
) {
294 layer_
->SetUserScrollable(horizontal
, vertical
);
297 bool WebLayerImpl::userScrollableHorizontal() const {
298 return layer_
->user_scrollable_horizontal();
301 bool WebLayerImpl::userScrollableVertical() const {
302 return layer_
->user_scrollable_vertical();
305 void WebLayerImpl::setHaveWheelEventHandlers(bool have_wheel_event_handlers
) {
306 layer_
->SetHaveWheelEventHandlers(have_wheel_event_handlers
);
309 bool WebLayerImpl::haveWheelEventHandlers() const {
310 return layer_
->have_wheel_event_handlers();
313 void WebLayerImpl::setHaveScrollEventHandlers(bool have_scroll_event_handlers
) {
314 layer_
->SetHaveScrollEventHandlers(have_scroll_event_handlers
);
317 bool WebLayerImpl::haveScrollEventHandlers() const {
318 return layer_
->have_scroll_event_handlers();
321 void WebLayerImpl::setShouldScrollOnMainThread(
322 bool should_scroll_on_main_thread
) {
323 layer_
->SetShouldScrollOnMainThread(should_scroll_on_main_thread
);
326 bool WebLayerImpl::shouldScrollOnMainThread() const {
327 return layer_
->should_scroll_on_main_thread();
330 void WebLayerImpl::setNonFastScrollableRegion(const WebVector
<WebRect
>& rects
) {
332 for (size_t i
= 0; i
< rects
.size(); ++i
)
333 region
.Union(rects
[i
]);
334 layer_
->SetNonFastScrollableRegion(region
);
337 WebVector
<WebRect
> WebLayerImpl::nonFastScrollableRegion() const {
338 size_t num_rects
= 0;
339 for (cc::Region::Iterator
region_rects(layer_
->non_fast_scrollable_region());
340 region_rects
.has_rect();
344 WebVector
<WebRect
> result(num_rects
);
346 for (cc::Region::Iterator
region_rects(layer_
->non_fast_scrollable_region());
347 region_rects
.has_rect();
348 region_rects
.next()) {
349 result
[i
] = region_rects
.rect();
355 void WebLayerImpl::setTouchEventHandlerRegion(const WebVector
<WebRect
>& rects
) {
357 for (size_t i
= 0; i
< rects
.size(); ++i
)
358 region
.Union(rects
[i
]);
359 layer_
->SetTouchEventHandlerRegion(region
);
362 WebVector
<WebRect
> WebLayerImpl::touchEventHandlerRegion() const {
363 size_t num_rects
= 0;
364 for (cc::Region::Iterator
region_rects(layer_
->touch_event_handler_region());
365 region_rects
.has_rect();
369 WebVector
<WebRect
> result(num_rects
);
371 for (cc::Region::Iterator
region_rects(layer_
->touch_event_handler_region());
372 region_rects
.has_rect();
373 region_rects
.next()) {
374 result
[i
] = region_rects
.rect();
380 void WebLayerImpl::setIsContainerForFixedPositionLayers(bool enable
) {
381 layer_
->SetIsContainerForFixedPositionLayers(enable
);
384 bool WebLayerImpl::isContainerForFixedPositionLayers() const {
385 return layer_
->IsContainerForFixedPositionLayers();
388 static blink::WebLayerPositionConstraint
ToWebLayerPositionConstraint(
389 const cc::LayerPositionConstraint
& constraint
) {
390 blink::WebLayerPositionConstraint web_constraint
;
391 web_constraint
.isFixedPosition
= constraint
.is_fixed_position();
392 web_constraint
.isFixedToRightEdge
= constraint
.is_fixed_to_right_edge();
393 web_constraint
.isFixedToBottomEdge
= constraint
.is_fixed_to_bottom_edge();
394 return web_constraint
;
397 static cc::LayerPositionConstraint
ToLayerPositionConstraint(
398 const blink::WebLayerPositionConstraint
& web_constraint
) {
399 cc::LayerPositionConstraint constraint
;
400 constraint
.set_is_fixed_position(web_constraint
.isFixedPosition
);
401 constraint
.set_is_fixed_to_right_edge(web_constraint
.isFixedToRightEdge
);
402 constraint
.set_is_fixed_to_bottom_edge(web_constraint
.isFixedToBottomEdge
);
406 void WebLayerImpl::setPositionConstraint(
407 const blink::WebLayerPositionConstraint
& constraint
) {
408 layer_
->SetPositionConstraint(ToLayerPositionConstraint(constraint
));
411 blink::WebLayerPositionConstraint
WebLayerImpl::positionConstraint() const {
412 return ToWebLayerPositionConstraint(layer_
->position_constraint());
415 void WebLayerImpl::setScrollClient(blink::WebLayerScrollClient
* scroll_client
) {
417 layer_
->set_did_scroll_callback(
418 base::Bind(&blink::WebLayerScrollClient::didScroll
,
419 base::Unretained(scroll_client
)));
421 layer_
->set_did_scroll_callback(base::Closure());
425 bool WebLayerImpl::isOrphan() const {
426 return !layer_
->layer_tree_host();
429 void WebLayerImpl::setWebLayerClient(blink::WebLayerClient
* client
) {
430 web_layer_client_
= client
;
433 class TracedDebugInfo
: public base::debug::ConvertableToTraceFormat
{
435 // This object takes ownership of the debug_info object.
436 explicit TracedDebugInfo(blink::WebGraphicsLayerDebugInfo
* debug_info
)
437 : debug_info_(debug_info
) {}
438 void AppendAsTraceFormat(std::string
* out
) const override
{
439 DCHECK(thread_checker_
.CalledOnValidThread());
440 blink::WebString web_string
;
441 debug_info_
->appendAsTraceFormat(&web_string
);
442 out
->append(web_string
.utf8());
446 ~TracedDebugInfo() override
{}
447 scoped_ptr
<blink::WebGraphicsLayerDebugInfo
> debug_info_
;
448 base::ThreadChecker thread_checker_
;
451 scoped_refptr
<base::debug::ConvertableToTraceFormat
>
452 WebLayerImpl::TakeDebugInfo() {
453 if (!web_layer_client_
)
455 blink::WebGraphicsLayerDebugInfo
* debug_info
=
456 web_layer_client_
->takeDebugInfoFor(this);
459 return new TracedDebugInfo(debug_info
);
464 void WebLayerImpl::setScrollParent(blink::WebLayer
* parent
) {
465 cc::Layer
* scroll_parent
= nullptr;
467 scroll_parent
= static_cast<WebLayerImpl
*>(parent
)->layer();
468 layer_
->SetScrollParent(scroll_parent
);
471 void WebLayerImpl::setClipParent(blink::WebLayer
* parent
) {
472 cc::Layer
* clip_parent
= nullptr;
474 clip_parent
= static_cast<WebLayerImpl
*>(parent
)->layer();
475 layer_
->SetClipParent(clip_parent
);
478 Layer
* WebLayerImpl::layer() const {
482 } // namespace cc_blink