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.
5 #include "webkit/renderer/compositor_bindings/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/layers/layer.h"
16 #include "cc/layers/layer_position_constraint.h"
17 #include "third_party/WebKit/public/platform/WebFloatPoint.h"
18 #include "third_party/WebKit/public/platform/WebFloatRect.h"
19 #include "third_party/WebKit/public/platform/WebGraphicsLayerDebugInfo.h"
20 #include "third_party/WebKit/public/platform/WebLayerClient.h"
21 #include "third_party/WebKit/public/platform/WebLayerPositionConstraint.h"
22 #include "third_party/WebKit/public/platform/WebLayerScrollClient.h"
23 #include "third_party/WebKit/public/platform/WebSize.h"
24 #include "third_party/skia/include/utils/SkMatrix44.h"
25 #include "webkit/renderer/compositor_bindings/web_animation_impl.h"
26 #include "webkit/renderer/compositor_bindings/web_blend_mode.h"
27 #include "webkit/renderer/compositor_bindings/web_filter_operations_impl.h"
28 #include "webkit/renderer/compositor_bindings/web_to_cc_animation_delegate_adapter.h"
32 using blink::WebLayer;
33 using blink::WebFloatPoint;
34 using blink::WebVector;
37 using blink::WebColor;
38 using blink::WebFilterOperations;
43 struct ImplSidePaintingStatus {
44 ImplSidePaintingStatus()
45 : enabled(cc::switches::IsImplSidePaintingEnabled()) {
49 base::LazyInstance<ImplSidePaintingStatus> g_impl_side_painting_status =
50 LAZY_INSTANCE_INITIALIZER;
54 WebLayerImpl::WebLayerImpl() : layer_(Layer::Create()) {
55 web_layer_client_ = NULL;
56 layer_->SetLayerClient(this);
59 WebLayerImpl::WebLayerImpl(scoped_refptr<Layer> layer) : layer_(layer) {
60 web_layer_client_ = NULL;
61 layer_->SetLayerClient(this);
64 WebLayerImpl::~WebLayerImpl() {
65 layer_->ClearRenderSurface();
66 layer_->set_layer_animation_delegate(NULL);
67 web_layer_client_ = NULL;
71 bool WebLayerImpl::UsingPictureLayer() {
72 return g_impl_side_painting_status.Get().enabled;
75 int WebLayerImpl::id() const { return layer_->id(); }
77 void WebLayerImpl::invalidateRect(const blink::WebFloatRect& rect) {
78 layer_->SetNeedsDisplayRect(rect);
81 void WebLayerImpl::invalidate() { layer_->SetNeedsDisplay(); }
83 void WebLayerImpl::addChild(WebLayer* child) {
84 layer_->AddChild(static_cast<WebLayerImpl*>(child)->layer());
87 void WebLayerImpl::insertChild(WebLayer* child, size_t index) {
88 layer_->InsertChild(static_cast<WebLayerImpl*>(child)->layer(), index);
91 void WebLayerImpl::replaceChild(WebLayer* reference, WebLayer* new_layer) {
92 layer_->ReplaceChild(static_cast<WebLayerImpl*>(reference)->layer(),
93 static_cast<WebLayerImpl*>(new_layer)->layer());
96 void WebLayerImpl::removeFromParent() { layer_->RemoveFromParent(); }
98 void WebLayerImpl::removeAllChildren() { layer_->RemoveAllChildren(); }
100 void WebLayerImpl::setAnchorPoint(const WebFloatPoint& anchor_point) {
101 layer_->SetAnchorPoint(anchor_point);
104 WebFloatPoint WebLayerImpl::anchorPoint() const {
105 return layer_->anchor_point();
108 void WebLayerImpl::setAnchorPointZ(float anchor_point_z) {
109 layer_->SetAnchorPointZ(anchor_point_z);
112 float WebLayerImpl::anchorPointZ() const { return layer_->anchor_point_z(); }
114 void WebLayerImpl::setBounds(const WebSize& size) { layer_->SetBounds(size); }
116 WebSize WebLayerImpl::bounds() const { return layer_->bounds(); }
118 void WebLayerImpl::setMasksToBounds(bool masks_to_bounds) {
119 layer_->SetMasksToBounds(masks_to_bounds);
122 bool WebLayerImpl::masksToBounds() const { return layer_->masks_to_bounds(); }
124 void WebLayerImpl::setMaskLayer(WebLayer* maskLayer) {
125 layer_->SetMaskLayer(
126 maskLayer ? static_cast<WebLayerImpl*>(maskLayer)->layer() : 0);
129 void WebLayerImpl::setReplicaLayer(WebLayer* replica_layer) {
130 layer_->SetReplicaLayer(
131 replica_layer ? static_cast<WebLayerImpl*>(replica_layer)->layer() : 0);
134 void WebLayerImpl::setOpacity(float opacity) { layer_->SetOpacity(opacity); }
136 float WebLayerImpl::opacity() const { return layer_->opacity(); }
138 void WebLayerImpl::setBlendMode(blink::WebBlendMode blend_mode) {
139 layer_->SetBlendMode(BlendModeToSkia(blend_mode));
142 blink::WebBlendMode WebLayerImpl::blendMode() const {
143 return BlendModeFromSkia(layer_->blend_mode());
146 void WebLayerImpl::setIsRootForIsolatedGroup(bool isolate) {
147 layer_->SetIsRootForIsolatedGroup(isolate);
150 bool WebLayerImpl::isRootForIsolatedGroup() {
151 return layer_->is_root_for_isolated_group();
154 void WebLayerImpl::setOpaque(bool opaque) { layer_->SetContentsOpaque(opaque); }
156 bool WebLayerImpl::opaque() const { return layer_->contents_opaque(); }
158 void WebLayerImpl::setPosition(const WebFloatPoint& position) {
159 layer_->SetPosition(position);
162 WebFloatPoint WebLayerImpl::position() const { return layer_->position(); }
164 void WebLayerImpl::setSublayerTransform(const SkMatrix44& matrix) {
165 gfx::Transform sub_layer_transform;
166 sub_layer_transform.matrix() = matrix;
167 layer_->SetSublayerTransform(sub_layer_transform);
170 SkMatrix44 WebLayerImpl::sublayerTransform() const {
171 return layer_->sublayer_transform().matrix();
174 void WebLayerImpl::setTransform(const SkMatrix44& matrix) {
175 gfx::Transform transform;
176 transform.matrix() = matrix;
177 layer_->SetTransform(transform);
180 SkMatrix44 WebLayerImpl::transform() const {
181 return layer_->transform().matrix();
184 void WebLayerImpl::setDrawsContent(bool draws_content) {
185 layer_->SetIsDrawable(draws_content);
188 bool WebLayerImpl::drawsContent() const { return layer_->DrawsContent(); }
190 void WebLayerImpl::setPreserves3D(bool preserve3D) {
191 layer_->SetPreserves3d(preserve3D);
194 void WebLayerImpl::setUseParentBackfaceVisibility(
195 bool use_parent_backface_visibility) {
196 layer_->set_use_parent_backface_visibility(use_parent_backface_visibility);
199 void WebLayerImpl::setBackgroundColor(WebColor color) {
200 layer_->SetBackgroundColor(color);
203 WebColor WebLayerImpl::backgroundColor() const {
204 return layer_->background_color();
207 void WebLayerImpl::setFilters(const WebFilterOperations& filters) {
208 const WebFilterOperationsImpl& filters_impl =
209 static_cast<const WebFilterOperationsImpl&>(filters);
210 layer_->SetFilters(filters_impl.AsFilterOperations());
213 void WebLayerImpl::setBackgroundFilters(const WebFilterOperations& filters) {
214 const WebFilterOperationsImpl& filters_impl =
215 static_cast<const WebFilterOperationsImpl&>(filters);
216 layer_->SetBackgroundFilters(filters_impl.AsFilterOperations());
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());
226 bool WebLayerImpl::addAnimation(blink::WebAnimation* animation) {
227 bool result = layer_->AddAnimation(
228 static_cast<WebAnimationImpl*>(animation)->PassAnimation());
233 void WebLayerImpl::removeAnimation(int animation_id) {
234 layer_->RemoveAnimation(animation_id);
237 void WebLayerImpl::removeAnimation(
239 blink::WebAnimation::TargetProperty target_property) {
240 layer_->layer_animation_controller()->RemoveAnimation(
242 static_cast<Animation::TargetProperty>(target_property));
245 void WebLayerImpl::pauseAnimation(int animation_id, double time_offset) {
246 layer_->PauseAnimation(animation_id, time_offset);
249 bool WebLayerImpl::hasActiveAnimation() { return layer_->HasActiveAnimation(); }
251 void WebLayerImpl::setForceRenderSurface(bool force_render_surface) {
252 layer_->SetForceRenderSurface(force_render_surface);
255 void WebLayerImpl::setScrollPosition(blink::WebPoint position) {
256 layer_->SetScrollOffset(gfx::Point(position).OffsetFromOrigin());
259 blink::WebPoint WebLayerImpl::scrollPosition() const {
260 return gfx::PointAtOffsetFromOrigin(layer_->scroll_offset());
263 void WebLayerImpl::setMaxScrollPosition(WebSize max_scroll_position) {
264 layer_->SetMaxScrollOffset(max_scroll_position);
267 WebSize WebLayerImpl::maxScrollPosition() const {
268 return layer_->max_scroll_offset();
271 void WebLayerImpl::setScrollable(bool scrollable) {
272 layer_->SetScrollable(scrollable);
275 bool WebLayerImpl::scrollable() const { return layer_->scrollable(); }
277 void WebLayerImpl::setUserScrollable(bool horizontal, bool vertical) {
278 layer_->SetUserScrollable(horizontal, vertical);
281 bool WebLayerImpl::userScrollableHorizontal() const {
282 return layer_->user_scrollable_horizontal();
285 bool WebLayerImpl::userScrollableVertical() const {
286 return layer_->user_scrollable_vertical();
289 void WebLayerImpl::setHaveWheelEventHandlers(bool have_wheel_event_handlers) {
290 layer_->SetHaveWheelEventHandlers(have_wheel_event_handlers);
293 bool WebLayerImpl::haveWheelEventHandlers() const {
294 return layer_->have_wheel_event_handlers();
297 void WebLayerImpl::setShouldScrollOnMainThread(
298 bool should_scroll_on_main_thread) {
299 layer_->SetShouldScrollOnMainThread(should_scroll_on_main_thread);
302 bool WebLayerImpl::shouldScrollOnMainThread() const {
303 return layer_->should_scroll_on_main_thread();
306 void WebLayerImpl::setNonFastScrollableRegion(const WebVector<WebRect>& rects) {
308 for (size_t i = 0; i < rects.size(); ++i)
309 region.Union(rects[i]);
310 layer_->SetNonFastScrollableRegion(region);
313 WebVector<WebRect> WebLayerImpl::nonFastScrollableRegion() const {
314 size_t num_rects = 0;
315 for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region());
316 region_rects.has_rect();
320 WebVector<WebRect> result(num_rects);
322 for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region());
323 region_rects.has_rect();
324 region_rects.next()) {
325 result[i] = region_rects.rect();
331 void WebLayerImpl::setTouchEventHandlerRegion(const WebVector<WebRect>& rects) {
333 for (size_t i = 0; i < rects.size(); ++i)
334 region.Union(rects[i]);
335 layer_->SetTouchEventHandlerRegion(region);
338 WebVector<WebRect> WebLayerImpl::touchEventHandlerRegion() const {
339 size_t num_rects = 0;
340 for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region());
341 region_rects.has_rect();
345 WebVector<WebRect> result(num_rects);
347 for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region());
348 region_rects.has_rect();
349 region_rects.next()) {
350 result[i] = region_rects.rect();
356 void WebLayerImpl::setIsContainerForFixedPositionLayers(bool enable) {
357 layer_->SetIsContainerForFixedPositionLayers(enable);
360 bool WebLayerImpl::isContainerForFixedPositionLayers() const {
361 return layer_->IsContainerForFixedPositionLayers();
364 static blink::WebLayerPositionConstraint ToWebLayerPositionConstraint(
365 const cc::LayerPositionConstraint& constraint) {
366 blink::WebLayerPositionConstraint web_constraint;
367 web_constraint.isFixedPosition = constraint.is_fixed_position();
368 web_constraint.isFixedToRightEdge = constraint.is_fixed_to_right_edge();
369 web_constraint.isFixedToBottomEdge = constraint.is_fixed_to_bottom_edge();
370 return web_constraint;
373 static cc::LayerPositionConstraint ToLayerPositionConstraint(
374 const blink::WebLayerPositionConstraint& web_constraint) {
375 cc::LayerPositionConstraint constraint;
376 constraint.set_is_fixed_position(web_constraint.isFixedPosition);
377 constraint.set_is_fixed_to_right_edge(web_constraint.isFixedToRightEdge);
378 constraint.set_is_fixed_to_bottom_edge(web_constraint.isFixedToBottomEdge);
382 void WebLayerImpl::setPositionConstraint(
383 const blink::WebLayerPositionConstraint& constraint) {
384 layer_->SetPositionConstraint(ToLayerPositionConstraint(constraint));
387 blink::WebLayerPositionConstraint WebLayerImpl::positionConstraint() const {
388 return ToWebLayerPositionConstraint(layer_->position_constraint());
391 void WebLayerImpl::setScrollClient(
392 blink::WebLayerScrollClient* scroll_client) {
394 layer_->set_did_scroll_callback(
395 base::Bind(&blink::WebLayerScrollClient::didScroll,
396 base::Unretained(scroll_client)));
398 layer_->set_did_scroll_callback(base::Closure());
402 bool WebLayerImpl::isOrphan() const { return !layer_->layer_tree_host(); }
404 void WebLayerImpl::setWebLayerClient(blink::WebLayerClient* client) {
405 web_layer_client_ = client;
408 class TracedDebugInfo : public base::debug::ConvertableToTraceFormat {
410 // This object takes ownership of the debug_info object.
411 explicit TracedDebugInfo(blink::WebGraphicsLayerDebugInfo* debug_info) :
412 debug_info_(debug_info) {}
413 virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE {
414 DCHECK(thread_checker_.CalledOnValidThread());
415 blink::WebString web_string;
416 debug_info_->appendAsTraceFormat(&web_string);
417 out->append(web_string.utf8());
420 virtual ~TracedDebugInfo() {}
421 scoped_ptr<blink::WebGraphicsLayerDebugInfo> debug_info_;
422 base::ThreadChecker thread_checker_;
425 scoped_refptr<base::debug::ConvertableToTraceFormat>
426 WebLayerImpl::TakeDebugInfo() {
427 if (!web_layer_client_)
429 blink::WebGraphicsLayerDebugInfo* debug_info =
430 web_layer_client_->takeDebugInfoFor(this);
433 return new TracedDebugInfo(debug_info);
438 void WebLayerImpl::setScrollParent(blink::WebLayer* parent) {
439 cc::Layer* scroll_parent = NULL;
441 scroll_parent = static_cast<WebLayerImpl*>(parent)->layer();
442 layer_->SetScrollParent(scroll_parent);
445 void WebLayerImpl::setClipParent(blink::WebLayer* parent) {
446 cc::Layer* clip_parent = NULL;
448 clip_parent = static_cast<WebLayerImpl*>(parent)->layer();
449 layer_->SetClipParent(clip_parent);
452 Layer* WebLayerImpl::layer() const { return layer_.get(); }
454 } // namespace webkit