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 "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_adapter.h"
33 using blink::WebLayer;
34 using blink::WebFloatPoint;
35 using blink::WebVector;
38 using blink::WebColor;
39 using blink::WebFilterOperations;
44 bool g_impl_side_painting_enabled = false;
48 WebLayerImpl::WebLayerImpl() : layer_(Layer::Create()) {
49 web_layer_client_ = NULL;
50 layer_->SetLayerClient(this);
53 WebLayerImpl::WebLayerImpl(scoped_refptr<Layer> layer) : layer_(layer) {
54 web_layer_client_ = NULL;
55 layer_->SetLayerClient(this);
58 WebLayerImpl::~WebLayerImpl() {
59 layer_->ClearRenderSurface();
60 layer_->set_layer_animation_delegate(NULL);
61 web_layer_client_ = NULL;
65 bool WebLayerImpl::UsingPictureLayer() {
66 return g_impl_side_painting_enabled;
70 void WebLayerImpl::SetImplSidePaintingEnabled(bool enabled) {
71 g_impl_side_painting_enabled = enabled;
74 int WebLayerImpl::id() const { return layer_->id(); }
76 void WebLayerImpl::invalidateRect(const blink::WebFloatRect& rect) {
77 layer_->SetNeedsDisplayRect(rect);
80 void WebLayerImpl::invalidate() { layer_->SetNeedsDisplay(); }
82 void WebLayerImpl::addChild(WebLayer* child) {
83 layer_->AddChild(static_cast<WebLayerImpl*>(child)->layer());
86 void WebLayerImpl::insertChild(WebLayer* child, size_t index) {
87 layer_->InsertChild(static_cast<WebLayerImpl*>(child)->layer(), index);
90 void WebLayerImpl::replaceChild(WebLayer* reference, WebLayer* new_layer) {
91 layer_->ReplaceChild(static_cast<WebLayerImpl*>(reference)->layer(),
92 static_cast<WebLayerImpl*>(new_layer)->layer());
95 void WebLayerImpl::removeFromParent() { layer_->RemoveFromParent(); }
97 void WebLayerImpl::removeAllChildren() { layer_->RemoveAllChildren(); }
99 void WebLayerImpl::setAnchorPoint(const WebFloatPoint& anchor_point) {
100 layer_->SetAnchorPoint(anchor_point);
103 WebFloatPoint WebLayerImpl::anchorPoint() const {
104 return layer_->anchor_point();
107 void WebLayerImpl::setAnchorPointZ(float anchor_point_z) {
108 layer_->SetAnchorPointZ(anchor_point_z);
111 float WebLayerImpl::anchorPointZ() const { return layer_->anchor_point_z(); }
113 void WebLayerImpl::setBounds(const WebSize& size) { layer_->SetBounds(size); }
115 WebSize WebLayerImpl::bounds() const { return layer_->bounds(); }
117 void WebLayerImpl::setMasksToBounds(bool masks_to_bounds) {
118 layer_->SetMasksToBounds(masks_to_bounds);
121 bool WebLayerImpl::masksToBounds() const { return layer_->masks_to_bounds(); }
123 void WebLayerImpl::setMaskLayer(WebLayer* maskLayer) {
124 layer_->SetMaskLayer(
125 maskLayer ? static_cast<WebLayerImpl*>(maskLayer)->layer() : 0);
128 void WebLayerImpl::setReplicaLayer(WebLayer* replica_layer) {
129 layer_->SetReplicaLayer(
130 replica_layer ? static_cast<WebLayerImpl*>(replica_layer)->layer() : 0);
133 void WebLayerImpl::setOpacity(float opacity) { layer_->SetOpacity(opacity); }
135 float WebLayerImpl::opacity() const { return layer_->opacity(); }
137 void WebLayerImpl::setBlendMode(blink::WebBlendMode blend_mode) {
138 layer_->SetBlendMode(BlendModeToSkia(blend_mode));
141 blink::WebBlendMode WebLayerImpl::blendMode() const {
142 return BlendModeFromSkia(layer_->blend_mode());
145 void WebLayerImpl::setIsRootForIsolatedGroup(bool isolate) {
146 layer_->SetIsRootForIsolatedGroup(isolate);
149 bool WebLayerImpl::isRootForIsolatedGroup() {
150 return layer_->is_root_for_isolated_group();
153 void WebLayerImpl::setOpaque(bool opaque) { layer_->SetContentsOpaque(opaque); }
155 bool WebLayerImpl::opaque() const { return layer_->contents_opaque(); }
157 void WebLayerImpl::setPosition(const WebFloatPoint& position) {
158 layer_->SetPosition(position);
161 WebFloatPoint WebLayerImpl::position() const { return layer_->position(); }
163 void WebLayerImpl::setTransform(const SkMatrix44& matrix) {
164 gfx::Transform transform;
165 transform.matrix() = matrix;
166 layer_->SetTransform(transform);
169 SkMatrix44 WebLayerImpl::transform() const {
170 return layer_->transform().matrix();
173 void WebLayerImpl::setDrawsContent(bool draws_content) {
174 layer_->SetIsDrawable(draws_content);
177 bool WebLayerImpl::drawsContent() const { return layer_->DrawsContent(); }
179 void WebLayerImpl::setShouldFlattenTransform(bool flatten) {
180 layer_->SetShouldFlattenTransform(flatten);
183 void WebLayerImpl::setRenderingContext(int context) {
184 layer_->SetIs3dSorted(context != 0);
187 void WebLayerImpl::setUseParentBackfaceVisibility(
188 bool use_parent_backface_visibility) {
189 layer_->set_use_parent_backface_visibility(use_parent_backface_visibility);
192 void WebLayerImpl::setBackgroundColor(WebColor color) {
193 layer_->SetBackgroundColor(color);
196 WebColor WebLayerImpl::backgroundColor() const {
197 return layer_->background_color();
200 void WebLayerImpl::setFilters(const WebFilterOperations& filters) {
201 const WebFilterOperationsImpl& filters_impl =
202 static_cast<const WebFilterOperationsImpl&>(filters);
203 layer_->SetFilters(filters_impl.AsFilterOperations());
206 void WebLayerImpl::setBackgroundFilters(const WebFilterOperations& filters) {
207 const WebFilterOperationsImpl& filters_impl =
208 static_cast<const WebFilterOperationsImpl&>(filters);
209 layer_->SetBackgroundFilters(filters_impl.AsFilterOperations());
212 void WebLayerImpl::setAnimationDelegate(
213 blink::WebAnimationDelegate* delegate) {
214 animation_delegate_adapter_.reset(
215 new WebToCCAnimationDelegateAdapter(delegate));
216 layer_->set_layer_animation_delegate(animation_delegate_adapter_.get());
219 bool WebLayerImpl::addAnimation(blink::WebAnimation* animation) {
220 bool result = layer_->AddAnimation(
221 static_cast<WebAnimationImpl*>(animation)->PassAnimation());
226 void WebLayerImpl::removeAnimation(int animation_id) {
227 layer_->RemoveAnimation(animation_id);
230 void WebLayerImpl::removeAnimation(
232 blink::WebAnimation::TargetProperty target_property) {
233 layer_->layer_animation_controller()->RemoveAnimation(
235 static_cast<Animation::TargetProperty>(target_property));
238 void WebLayerImpl::pauseAnimation(int animation_id, double time_offset) {
239 layer_->PauseAnimation(animation_id, time_offset);
242 bool WebLayerImpl::hasActiveAnimation() { return layer_->HasActiveAnimation(); }
244 void WebLayerImpl::setForceRenderSurface(bool force_render_surface) {
245 layer_->SetForceRenderSurface(force_render_surface);
248 void WebLayerImpl::setScrollPosition(blink::WebPoint position) {
249 layer_->SetScrollOffset(gfx::Point(position).OffsetFromOrigin());
252 blink::WebPoint WebLayerImpl::scrollPosition() const {
253 return gfx::PointAtOffsetFromOrigin(layer_->scroll_offset());
256 void WebLayerImpl::setScrollClipLayer(WebLayer* clip_layer) {
258 layer_->SetScrollClipLayerId(Layer::INVALID_ID);
261 layer_->SetScrollClipLayerId(clip_layer->id());
264 bool WebLayerImpl::scrollable() const { return layer_->scrollable(); }
266 void WebLayerImpl::setUserScrollable(bool horizontal, bool vertical) {
267 layer_->SetUserScrollable(horizontal, vertical);
270 bool WebLayerImpl::userScrollableHorizontal() const {
271 return layer_->user_scrollable_horizontal();
274 bool WebLayerImpl::userScrollableVertical() const {
275 return layer_->user_scrollable_vertical();
278 void WebLayerImpl::setHaveWheelEventHandlers(bool have_wheel_event_handlers) {
279 layer_->SetHaveWheelEventHandlers(have_wheel_event_handlers);
282 bool WebLayerImpl::haveWheelEventHandlers() const {
283 return layer_->have_wheel_event_handlers();
286 void WebLayerImpl::setHaveScrollEventHandlers(bool have_scroll_event_handlers) {
287 layer_->SetHaveScrollEventHandlers(have_scroll_event_handlers);
290 bool WebLayerImpl::haveScrollEventHandlers() const {
291 return layer_->have_scroll_event_handlers();
294 void WebLayerImpl::setShouldScrollOnMainThread(
295 bool should_scroll_on_main_thread) {
296 layer_->SetShouldScrollOnMainThread(should_scroll_on_main_thread);
299 bool WebLayerImpl::shouldScrollOnMainThread() const {
300 return layer_->should_scroll_on_main_thread();
303 void WebLayerImpl::setNonFastScrollableRegion(const WebVector<WebRect>& rects) {
305 for (size_t i = 0; i < rects.size(); ++i)
306 region.Union(rects[i]);
307 layer_->SetNonFastScrollableRegion(region);
310 WebVector<WebRect> WebLayerImpl::nonFastScrollableRegion() const {
311 size_t num_rects = 0;
312 for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region());
313 region_rects.has_rect();
317 WebVector<WebRect> result(num_rects);
319 for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region());
320 region_rects.has_rect();
321 region_rects.next()) {
322 result[i] = region_rects.rect();
328 void WebLayerImpl::setTouchEventHandlerRegion(const WebVector<WebRect>& rects) {
330 for (size_t i = 0; i < rects.size(); ++i)
331 region.Union(rects[i]);
332 layer_->SetTouchEventHandlerRegion(region);
335 WebVector<WebRect> WebLayerImpl::touchEventHandlerRegion() const {
336 size_t num_rects = 0;
337 for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region());
338 region_rects.has_rect();
342 WebVector<WebRect> result(num_rects);
344 for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region());
345 region_rects.has_rect();
346 region_rects.next()) {
347 result[i] = region_rects.rect();
353 void WebLayerImpl::setIsContainerForFixedPositionLayers(bool enable) {
354 layer_->SetIsContainerForFixedPositionLayers(enable);
357 bool WebLayerImpl::isContainerForFixedPositionLayers() const {
358 return layer_->IsContainerForFixedPositionLayers();
361 static blink::WebLayerPositionConstraint ToWebLayerPositionConstraint(
362 const cc::LayerPositionConstraint& constraint) {
363 blink::WebLayerPositionConstraint web_constraint;
364 web_constraint.isFixedPosition = constraint.is_fixed_position();
365 web_constraint.isFixedToRightEdge = constraint.is_fixed_to_right_edge();
366 web_constraint.isFixedToBottomEdge = constraint.is_fixed_to_bottom_edge();
367 return web_constraint;
370 static cc::LayerPositionConstraint ToLayerPositionConstraint(
371 const blink::WebLayerPositionConstraint& web_constraint) {
372 cc::LayerPositionConstraint constraint;
373 constraint.set_is_fixed_position(web_constraint.isFixedPosition);
374 constraint.set_is_fixed_to_right_edge(web_constraint.isFixedToRightEdge);
375 constraint.set_is_fixed_to_bottom_edge(web_constraint.isFixedToBottomEdge);
379 void WebLayerImpl::setPositionConstraint(
380 const blink::WebLayerPositionConstraint& constraint) {
381 layer_->SetPositionConstraint(ToLayerPositionConstraint(constraint));
384 blink::WebLayerPositionConstraint WebLayerImpl::positionConstraint() const {
385 return ToWebLayerPositionConstraint(layer_->position_constraint());
388 void WebLayerImpl::setScrollClient(
389 blink::WebLayerScrollClient* scroll_client) {
391 layer_->set_did_scroll_callback(
392 base::Bind(&blink::WebLayerScrollClient::didScroll,
393 base::Unretained(scroll_client)));
395 layer_->set_did_scroll_callback(base::Closure());
399 bool WebLayerImpl::isOrphan() const { return !layer_->layer_tree_host(); }
401 void WebLayerImpl::setWebLayerClient(blink::WebLayerClient* client) {
402 web_layer_client_ = client;
405 class TracedDebugInfo : public base::debug::ConvertableToTraceFormat {
407 // This object takes ownership of the debug_info object.
408 explicit TracedDebugInfo(blink::WebGraphicsLayerDebugInfo* debug_info) :
409 debug_info_(debug_info) {}
410 virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE {
411 DCHECK(thread_checker_.CalledOnValidThread());
412 blink::WebString web_string;
413 debug_info_->appendAsTraceFormat(&web_string);
414 out->append(web_string.utf8());
417 virtual ~TracedDebugInfo() {}
418 scoped_ptr<blink::WebGraphicsLayerDebugInfo> debug_info_;
419 base::ThreadChecker thread_checker_;
422 scoped_refptr<base::debug::ConvertableToTraceFormat>
423 WebLayerImpl::TakeDebugInfo() {
424 if (!web_layer_client_)
426 blink::WebGraphicsLayerDebugInfo* debug_info =
427 web_layer_client_->takeDebugInfoFor(this);
430 return new TracedDebugInfo(debug_info);
435 void WebLayerImpl::setScrollParent(blink::WebLayer* parent) {
436 cc::Layer* scroll_parent = NULL;
438 scroll_parent = static_cast<WebLayerImpl*>(parent)->layer();
439 layer_->SetScrollParent(scroll_parent);
442 void WebLayerImpl::setClipParent(blink::WebLayer* parent) {
443 cc::Layer* clip_parent = NULL;
445 clip_parent = static_cast<WebLayerImpl*>(parent)->layer();
446 layer_->SetClipParent(clip_parent);
449 Layer* WebLayerImpl::layer() const { return layer_.get(); }
451 } // namespace webkit