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 struct ImplSidePaintingStatus {
45 ImplSidePaintingStatus()
46 : enabled(cc::switches::IsImplSidePaintingEnabled()) {
50 base::LazyInstance<ImplSidePaintingStatus> g_impl_side_painting_status =
51 LAZY_INSTANCE_INITIALIZER;
55 WebLayerImpl::WebLayerImpl() : layer_(Layer::Create()) {
56 web_layer_client_ = NULL;
57 layer_->SetLayerClient(this);
60 WebLayerImpl::WebLayerImpl(scoped_refptr<Layer> layer) : layer_(layer) {
61 web_layer_client_ = NULL;
62 layer_->SetLayerClient(this);
65 WebLayerImpl::~WebLayerImpl() {
66 layer_->ClearRenderSurface();
67 layer_->set_layer_animation_delegate(NULL);
68 web_layer_client_ = NULL;
72 bool WebLayerImpl::UsingPictureLayer() {
73 return g_impl_side_painting_status.Get().enabled;
76 int WebLayerImpl::id() const { return layer_->id(); }
78 void WebLayerImpl::invalidateRect(const blink::WebFloatRect& rect) {
79 layer_->SetNeedsDisplayRect(rect);
82 void WebLayerImpl::invalidate() { layer_->SetNeedsDisplay(); }
84 void WebLayerImpl::addChild(WebLayer* child) {
85 layer_->AddChild(static_cast<WebLayerImpl*>(child)->layer());
88 void WebLayerImpl::insertChild(WebLayer* child, size_t index) {
89 layer_->InsertChild(static_cast<WebLayerImpl*>(child)->layer(), index);
92 void WebLayerImpl::replaceChild(WebLayer* reference, WebLayer* new_layer) {
93 layer_->ReplaceChild(static_cast<WebLayerImpl*>(reference)->layer(),
94 static_cast<WebLayerImpl*>(new_layer)->layer());
97 void WebLayerImpl::removeFromParent() { layer_->RemoveFromParent(); }
99 void WebLayerImpl::removeAllChildren() { layer_->RemoveAllChildren(); }
101 void WebLayerImpl::setAnchorPoint(const WebFloatPoint& anchor_point) {
102 layer_->SetAnchorPoint(anchor_point);
105 WebFloatPoint WebLayerImpl::anchorPoint() const {
106 return layer_->anchor_point();
109 void WebLayerImpl::setAnchorPointZ(float anchor_point_z) {
110 layer_->SetAnchorPointZ(anchor_point_z);
113 float WebLayerImpl::anchorPointZ() const { return layer_->anchor_point_z(); }
115 void WebLayerImpl::setBounds(const WebSize& size) { layer_->SetBounds(size); }
117 WebSize WebLayerImpl::bounds() const { return layer_->bounds(); }
119 void WebLayerImpl::setMasksToBounds(bool masks_to_bounds) {
120 layer_->SetMasksToBounds(masks_to_bounds);
123 bool WebLayerImpl::masksToBounds() const { 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) { layer_->SetOpacity(opacity); }
137 float WebLayerImpl::opacity() const { return layer_->opacity(); }
139 void WebLayerImpl::setBlendMode(blink::WebBlendMode blend_mode) {
140 layer_->SetBlendMode(BlendModeToSkia(blend_mode));
143 blink::WebBlendMode WebLayerImpl::blendMode() const {
144 return BlendModeFromSkia(layer_->blend_mode());
147 void WebLayerImpl::setIsRootForIsolatedGroup(bool isolate) {
148 layer_->SetIsRootForIsolatedGroup(isolate);
151 bool WebLayerImpl::isRootForIsolatedGroup() {
152 return layer_->is_root_for_isolated_group();
155 void WebLayerImpl::setOpaque(bool opaque) { layer_->SetContentsOpaque(opaque); }
157 bool WebLayerImpl::opaque() const { return layer_->contents_opaque(); }
159 void WebLayerImpl::setPosition(const WebFloatPoint& position) {
160 layer_->SetPosition(position);
163 WebFloatPoint WebLayerImpl::position() const { return layer_->position(); }
165 void WebLayerImpl::setTransform(const SkMatrix44& matrix) {
166 gfx::Transform transform;
167 transform.matrix() = matrix;
168 layer_->SetTransform(transform);
171 SkMatrix44 WebLayerImpl::transform() const {
172 return layer_->transform().matrix();
175 void WebLayerImpl::setDrawsContent(bool draws_content) {
176 layer_->SetIsDrawable(draws_content);
179 bool WebLayerImpl::drawsContent() const { return layer_->DrawsContent(); }
181 void WebLayerImpl::setShouldFlattenTransform(bool flatten) {
182 layer_->SetShouldFlattenTransform(flatten);
185 void WebLayerImpl::setRenderingContext(int context) {
186 layer_->SetIs3dSorted(context != 0);
189 void WebLayerImpl::setUseParentBackfaceVisibility(
190 bool use_parent_backface_visibility) {
191 layer_->set_use_parent_backface_visibility(use_parent_backface_visibility);
194 void WebLayerImpl::setBackgroundColor(WebColor color) {
195 layer_->SetBackgroundColor(color);
198 WebColor WebLayerImpl::backgroundColor() const {
199 return layer_->background_color();
202 void WebLayerImpl::setFilters(const WebFilterOperations& filters) {
203 const WebFilterOperationsImpl& filters_impl =
204 static_cast<const WebFilterOperationsImpl&>(filters);
205 layer_->SetFilters(filters_impl.AsFilterOperations());
208 void WebLayerImpl::setBackgroundFilters(const WebFilterOperations& filters) {
209 const WebFilterOperationsImpl& filters_impl =
210 static_cast<const WebFilterOperationsImpl&>(filters);
211 layer_->SetBackgroundFilters(filters_impl.AsFilterOperations());
214 void WebLayerImpl::setAnimationDelegate(
215 blink::WebAnimationDelegate* delegate) {
216 animation_delegate_adapter_.reset(
217 new WebToCCAnimationDelegateAdapter(delegate));
218 layer_->set_layer_animation_delegate(animation_delegate_adapter_.get());
221 bool WebLayerImpl::addAnimation(blink::WebAnimation* animation) {
222 bool result = layer_->AddAnimation(
223 static_cast<WebAnimationImpl*>(animation)->PassAnimation());
228 void WebLayerImpl::removeAnimation(int animation_id) {
229 layer_->RemoveAnimation(animation_id);
232 void WebLayerImpl::removeAnimation(
234 blink::WebAnimation::TargetProperty target_property) {
235 layer_->layer_animation_controller()->RemoveAnimation(
237 static_cast<Animation::TargetProperty>(target_property));
240 void WebLayerImpl::pauseAnimation(int animation_id, double time_offset) {
241 layer_->PauseAnimation(animation_id, time_offset);
244 bool WebLayerImpl::hasActiveAnimation() { return layer_->HasActiveAnimation(); }
246 void WebLayerImpl::setForceRenderSurface(bool force_render_surface) {
247 layer_->SetForceRenderSurface(force_render_surface);
250 void WebLayerImpl::setScrollPosition(blink::WebPoint position) {
251 layer_->SetScrollOffset(gfx::Point(position).OffsetFromOrigin());
254 blink::WebPoint WebLayerImpl::scrollPosition() const {
255 return gfx::PointAtOffsetFromOrigin(layer_->scroll_offset());
258 WebSize WebLayerImpl::maxScrollPosition() const {
259 return layer_->MaxScrollOffset();
262 void WebLayerImpl::setScrollClipLayer(WebLayer* clip_layer) {
263 cc::Layer* cc_clip_layer =
264 clip_layer ? static_cast<WebLayerImpl*>(clip_layer)->layer() : 0;
265 layer_->SetScrollClipLayerId(cc_clip_layer->id());
268 bool WebLayerImpl::scrollable() const { return layer_->scrollable(); }
270 void WebLayerImpl::setUserScrollable(bool horizontal, bool vertical) {
271 layer_->SetUserScrollable(horizontal, vertical);
274 bool WebLayerImpl::userScrollableHorizontal() const {
275 return layer_->user_scrollable_horizontal();
278 bool WebLayerImpl::userScrollableVertical() const {
279 return layer_->user_scrollable_vertical();
282 void WebLayerImpl::setHaveWheelEventHandlers(bool have_wheel_event_handlers) {
283 layer_->SetHaveWheelEventHandlers(have_wheel_event_handlers);
286 bool WebLayerImpl::haveWheelEventHandlers() const {
287 return layer_->have_wheel_event_handlers();
290 void WebLayerImpl::setShouldScrollOnMainThread(
291 bool should_scroll_on_main_thread) {
292 layer_->SetShouldScrollOnMainThread(should_scroll_on_main_thread);
295 bool WebLayerImpl::shouldScrollOnMainThread() const {
296 return layer_->should_scroll_on_main_thread();
299 void WebLayerImpl::setNonFastScrollableRegion(const WebVector<WebRect>& rects) {
301 for (size_t i = 0; i < rects.size(); ++i)
302 region.Union(rects[i]);
303 layer_->SetNonFastScrollableRegion(region);
306 WebVector<WebRect> WebLayerImpl::nonFastScrollableRegion() const {
307 size_t num_rects = 0;
308 for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region());
309 region_rects.has_rect();
313 WebVector<WebRect> result(num_rects);
315 for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region());
316 region_rects.has_rect();
317 region_rects.next()) {
318 result[i] = region_rects.rect();
324 void WebLayerImpl::setTouchEventHandlerRegion(const WebVector<WebRect>& rects) {
326 for (size_t i = 0; i < rects.size(); ++i)
327 region.Union(rects[i]);
328 layer_->SetTouchEventHandlerRegion(region);
331 WebVector<WebRect> WebLayerImpl::touchEventHandlerRegion() const {
332 size_t num_rects = 0;
333 for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region());
334 region_rects.has_rect();
338 WebVector<WebRect> result(num_rects);
340 for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region());
341 region_rects.has_rect();
342 region_rects.next()) {
343 result[i] = region_rects.rect();
349 void WebLayerImpl::setIsContainerForFixedPositionLayers(bool enable) {
350 layer_->SetIsContainerForFixedPositionLayers(enable);
353 bool WebLayerImpl::isContainerForFixedPositionLayers() const {
354 return layer_->IsContainerForFixedPositionLayers();
357 static blink::WebLayerPositionConstraint ToWebLayerPositionConstraint(
358 const cc::LayerPositionConstraint& constraint) {
359 blink::WebLayerPositionConstraint web_constraint;
360 web_constraint.isFixedPosition = constraint.is_fixed_position();
361 web_constraint.isFixedToRightEdge = constraint.is_fixed_to_right_edge();
362 web_constraint.isFixedToBottomEdge = constraint.is_fixed_to_bottom_edge();
363 return web_constraint;
366 static cc::LayerPositionConstraint ToLayerPositionConstraint(
367 const blink::WebLayerPositionConstraint& web_constraint) {
368 cc::LayerPositionConstraint constraint;
369 constraint.set_is_fixed_position(web_constraint.isFixedPosition);
370 constraint.set_is_fixed_to_right_edge(web_constraint.isFixedToRightEdge);
371 constraint.set_is_fixed_to_bottom_edge(web_constraint.isFixedToBottomEdge);
375 void WebLayerImpl::setPositionConstraint(
376 const blink::WebLayerPositionConstraint& constraint) {
377 layer_->SetPositionConstraint(ToLayerPositionConstraint(constraint));
380 blink::WebLayerPositionConstraint WebLayerImpl::positionConstraint() const {
381 return ToWebLayerPositionConstraint(layer_->position_constraint());
384 void WebLayerImpl::setScrollClient(
385 blink::WebLayerScrollClient* scroll_client) {
387 layer_->set_did_scroll_callback(
388 base::Bind(&blink::WebLayerScrollClient::didScroll,
389 base::Unretained(scroll_client)));
391 layer_->set_did_scroll_callback(base::Closure());
395 bool WebLayerImpl::isOrphan() const { return !layer_->layer_tree_host(); }
397 void WebLayerImpl::setWebLayerClient(blink::WebLayerClient* client) {
398 web_layer_client_ = client;
401 class TracedDebugInfo : public base::debug::ConvertableToTraceFormat {
403 // This object takes ownership of the debug_info object.
404 explicit TracedDebugInfo(blink::WebGraphicsLayerDebugInfo* debug_info) :
405 debug_info_(debug_info) {}
406 virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE {
407 DCHECK(thread_checker_.CalledOnValidThread());
408 blink::WebString web_string;
409 debug_info_->appendAsTraceFormat(&web_string);
410 out->append(web_string.utf8());
413 virtual ~TracedDebugInfo() {}
414 scoped_ptr<blink::WebGraphicsLayerDebugInfo> debug_info_;
415 base::ThreadChecker thread_checker_;
418 scoped_refptr<base::debug::ConvertableToTraceFormat>
419 WebLayerImpl::TakeDebugInfo() {
420 if (!web_layer_client_)
422 blink::WebGraphicsLayerDebugInfo* debug_info =
423 web_layer_client_->takeDebugInfoFor(this);
426 return new TracedDebugInfo(debug_info);
431 void WebLayerImpl::setScrollParent(blink::WebLayer* parent) {
432 cc::Layer* scroll_parent = NULL;
434 scroll_parent = static_cast<WebLayerImpl*>(parent)->layer();
435 layer_->SetScrollParent(scroll_parent);
438 void WebLayerImpl::setClipParent(blink::WebLayer* parent) {
439 cc::Layer* clip_parent = NULL;
441 clip_parent = static_cast<WebLayerImpl*>(parent)->layer();
442 layer_->SetClipParent(clip_parent);
445 Layer* WebLayerImpl::layer() const { return layer_.get(); }
447 } // namespace webkit