Rename Animate as Begin(Main)Frame
[chromium-blink-merge.git] / content / renderer / compositor_bindings / web_layer_impl.cc
blobaeb7e0e6c36ad74790162c2902706be1ae2815c6
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 "content/renderer/compositor_bindings/web_layer_impl.h"
7 #include "base/bind.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 "content/renderer/compositor_bindings/web_animation_impl.h"
19 #include "content/renderer/compositor_bindings/web_blend_mode.h"
20 #include "content/renderer/compositor_bindings/web_filter_operations_impl.h"
21 #include "content/renderer/compositor_bindings/web_to_cc_animation_delegate_adapter.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"
31 using cc::Animation;
32 using cc::Layer;
33 using blink::WebLayer;
34 using blink::WebFloatPoint;
35 using blink::WebVector;
36 using blink::WebRect;
37 using blink::WebSize;
38 using blink::WebColor;
39 using blink::WebFilterOperations;
41 namespace content {
42 namespace {
44 bool g_impl_side_painting_enabled = false;
46 } // namespace
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;
64 // static
65 bool WebLayerImpl::UsingPictureLayer() {
66 return g_impl_side_painting_enabled;
69 // static
70 void WebLayerImpl::SetImplSidePaintingEnabled(bool enabled) {
71 g_impl_side_painting_enabled = enabled;
74 int WebLayerImpl::id() const {
75 return layer_->id();
78 void WebLayerImpl::invalidateRect(const blink::WebFloatRect& rect) {
79 layer_->SetNeedsDisplayRect(rect);
82 void WebLayerImpl::invalidate() {
83 layer_->SetNeedsDisplay();
86 void WebLayerImpl::addChild(WebLayer* child) {
87 layer_->AddChild(static_cast<WebLayerImpl*>(child)->layer());
90 void WebLayerImpl::insertChild(WebLayer* child, size_t index) {
91 layer_->InsertChild(static_cast<WebLayerImpl*>(child)->layer(), index);
94 void WebLayerImpl::replaceChild(WebLayer* reference, WebLayer* new_layer) {
95 layer_->ReplaceChild(static_cast<WebLayerImpl*>(reference)->layer(),
96 static_cast<WebLayerImpl*>(new_layer)->layer());
99 void WebLayerImpl::removeFromParent() {
100 layer_->RemoveFromParent();
103 void WebLayerImpl::removeAllChildren() {
104 layer_->RemoveAllChildren();
107 void WebLayerImpl::setBounds(const WebSize& size) { layer_->SetBounds(size); }
109 WebSize WebLayerImpl::bounds() const {
110 return layer_->bounds();
113 void WebLayerImpl::setMasksToBounds(bool masks_to_bounds) {
114 layer_->SetMasksToBounds(masks_to_bounds);
117 bool WebLayerImpl::masksToBounds() const {
118 return layer_->masks_to_bounds();
121 void WebLayerImpl::setMaskLayer(WebLayer* maskLayer) {
122 layer_->SetMaskLayer(
123 maskLayer ? static_cast<WebLayerImpl*>(maskLayer)->layer() : 0);
126 void WebLayerImpl::setReplicaLayer(WebLayer* replica_layer) {
127 layer_->SetReplicaLayer(
128 replica_layer ? static_cast<WebLayerImpl*>(replica_layer)->layer() : 0);
131 void WebLayerImpl::setOpacity(float opacity) {
132 layer_->SetOpacity(opacity);
135 float WebLayerImpl::opacity() const {
136 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) {
156 layer_->SetContentsOpaque(opaque);
159 bool WebLayerImpl::opaque() const {
160 return layer_->contents_opaque();
163 void WebLayerImpl::setPosition(const WebFloatPoint& position) {
164 layer_->SetPosition(position);
167 WebFloatPoint WebLayerImpl::position() const {
168 return layer_->position();
171 void WebLayerImpl::setTransform(const SkMatrix44& matrix) {
172 gfx::Transform transform;
173 transform.matrix() = matrix;
174 layer_->SetTransform(transform);
177 void WebLayerImpl::setTransformOrigin(const blink::WebFloatPoint3D& point) {
178 gfx::Point3F gfx_point = point;
179 layer_->SetTransformOrigin(gfx_point);
182 blink::WebFloatPoint3D WebLayerImpl::transformOrigin() const {
183 return layer_->transform_origin();
186 void WebLayerImpl::setAnchorPoint(const blink::WebFloatPoint&) {}
188 blink::WebFloatPoint WebLayerImpl::anchorPoint() const {
189 return blink::WebFloatPoint();
192 void WebLayerImpl::setAnchorPointZ(float) {}
194 float WebLayerImpl::anchorPointZ() const {
195 return 0.f;
198 SkMatrix44 WebLayerImpl::transform() const {
199 return layer_->transform().matrix();
202 void WebLayerImpl::setDrawsContent(bool draws_content) {
203 layer_->SetIsDrawable(draws_content);
206 bool WebLayerImpl::drawsContent() const {
207 return layer_->DrawsContent();
210 void WebLayerImpl::setShouldFlattenTransform(bool flatten) {
211 layer_->SetShouldFlattenTransform(flatten);
214 void WebLayerImpl::setRenderingContext(int context) {
215 layer_->Set3dSortingContextId(context);
218 void WebLayerImpl::setUseParentBackfaceVisibility(
219 bool use_parent_backface_visibility) {
220 layer_->set_use_parent_backface_visibility(use_parent_backface_visibility);
223 void WebLayerImpl::setBackgroundColor(WebColor color) {
224 layer_->SetBackgroundColor(color);
227 WebColor WebLayerImpl::backgroundColor() const {
228 return layer_->background_color();
231 void WebLayerImpl::setFilters(const WebFilterOperations& filters) {
232 const WebFilterOperationsImpl& filters_impl =
233 static_cast<const WebFilterOperationsImpl&>(filters);
234 layer_->SetFilters(filters_impl.AsFilterOperations());
237 void WebLayerImpl::setBackgroundFilters(const WebFilterOperations& filters) {
238 const WebFilterOperationsImpl& filters_impl =
239 static_cast<const WebFilterOperationsImpl&>(filters);
240 layer_->SetBackgroundFilters(filters_impl.AsFilterOperations());
243 void WebLayerImpl::setAnimationDelegate(
244 blink::WebCompositorAnimationDelegate* delegate) {
245 animation_delegate_adapter_.reset(
246 new WebToCCAnimationDelegateAdapter(delegate));
247 layer_->set_layer_animation_delegate(animation_delegate_adapter_.get());
250 bool WebLayerImpl::addAnimation(blink::WebCompositorAnimation* animation) {
251 bool result = layer_->AddAnimation(
252 static_cast<WebCompositorAnimationImpl*>(animation)->PassAnimation());
253 delete animation;
254 return result;
257 void WebLayerImpl::removeAnimation(int animation_id) {
258 layer_->RemoveAnimation(animation_id);
261 void WebLayerImpl::removeAnimation(
262 int animation_id,
263 blink::WebCompositorAnimation::TargetProperty target_property) {
264 layer_->layer_animation_controller()->RemoveAnimation(
265 animation_id, static_cast<Animation::TargetProperty>(target_property));
268 void WebLayerImpl::pauseAnimation(int animation_id, double time_offset) {
269 layer_->PauseAnimation(animation_id, time_offset);
272 bool WebLayerImpl::hasActiveAnimation() {
273 return layer_->HasActiveAnimation();
276 void WebLayerImpl::setForceRenderSurface(bool force_render_surface) {
277 layer_->SetForceRenderSurface(force_render_surface);
280 void WebLayerImpl::setScrollPosition(blink::WebPoint position) {
281 layer_->SetScrollOffset(gfx::Point(position).OffsetFromOrigin());
284 blink::WebPoint WebLayerImpl::scrollPosition() const {
285 return gfx::PointAtOffsetFromOrigin(layer_->scroll_offset());
288 void WebLayerImpl::setScrollClipLayer(WebLayer* clip_layer) {
289 if (!clip_layer) {
290 layer_->SetScrollClipLayerId(Layer::INVALID_ID);
291 return;
293 layer_->SetScrollClipLayerId(clip_layer->id());
296 bool WebLayerImpl::scrollable() const {
297 return layer_->scrollable();
300 void WebLayerImpl::setUserScrollable(bool horizontal, bool vertical) {
301 layer_->SetUserScrollable(horizontal, vertical);
304 bool WebLayerImpl::userScrollableHorizontal() const {
305 return layer_->user_scrollable_horizontal();
308 bool WebLayerImpl::userScrollableVertical() const {
309 return layer_->user_scrollable_vertical();
312 void WebLayerImpl::setHaveWheelEventHandlers(bool have_wheel_event_handlers) {
313 layer_->SetHaveWheelEventHandlers(have_wheel_event_handlers);
316 bool WebLayerImpl::haveWheelEventHandlers() const {
317 return layer_->have_wheel_event_handlers();
320 void WebLayerImpl::setHaveScrollEventHandlers(bool have_scroll_event_handlers) {
321 layer_->SetHaveScrollEventHandlers(have_scroll_event_handlers);
324 bool WebLayerImpl::haveScrollEventHandlers() const {
325 return layer_->have_scroll_event_handlers();
328 void WebLayerImpl::setShouldScrollOnMainThread(
329 bool should_scroll_on_main_thread) {
330 layer_->SetShouldScrollOnMainThread(should_scroll_on_main_thread);
333 bool WebLayerImpl::shouldScrollOnMainThread() const {
334 return layer_->should_scroll_on_main_thread();
337 void WebLayerImpl::setNonFastScrollableRegion(const WebVector<WebRect>& rects) {
338 cc::Region region;
339 for (size_t i = 0; i < rects.size(); ++i)
340 region.Union(rects[i]);
341 layer_->SetNonFastScrollableRegion(region);
344 WebVector<WebRect> WebLayerImpl::nonFastScrollableRegion() const {
345 size_t num_rects = 0;
346 for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region());
347 region_rects.has_rect();
348 region_rects.next())
349 ++num_rects;
351 WebVector<WebRect> result(num_rects);
352 size_t i = 0;
353 for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region());
354 region_rects.has_rect();
355 region_rects.next()) {
356 result[i] = region_rects.rect();
357 ++i;
359 return result;
362 void WebLayerImpl::setTouchEventHandlerRegion(const WebVector<WebRect>& rects) {
363 cc::Region region;
364 for (size_t i = 0; i < rects.size(); ++i)
365 region.Union(rects[i]);
366 layer_->SetTouchEventHandlerRegion(region);
369 WebVector<WebRect> WebLayerImpl::touchEventHandlerRegion() const {
370 size_t num_rects = 0;
371 for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region());
372 region_rects.has_rect();
373 region_rects.next())
374 ++num_rects;
376 WebVector<WebRect> result(num_rects);
377 size_t i = 0;
378 for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region());
379 region_rects.has_rect();
380 region_rects.next()) {
381 result[i] = region_rects.rect();
382 ++i;
384 return result;
387 void WebLayerImpl::setIsContainerForFixedPositionLayers(bool enable) {
388 layer_->SetIsContainerForFixedPositionLayers(enable);
391 bool WebLayerImpl::isContainerForFixedPositionLayers() const {
392 return layer_->IsContainerForFixedPositionLayers();
395 static blink::WebLayerPositionConstraint ToWebLayerPositionConstraint(
396 const cc::LayerPositionConstraint& constraint) {
397 blink::WebLayerPositionConstraint web_constraint;
398 web_constraint.isFixedPosition = constraint.is_fixed_position();
399 web_constraint.isFixedToRightEdge = constraint.is_fixed_to_right_edge();
400 web_constraint.isFixedToBottomEdge = constraint.is_fixed_to_bottom_edge();
401 return web_constraint;
404 static cc::LayerPositionConstraint ToLayerPositionConstraint(
405 const blink::WebLayerPositionConstraint& web_constraint) {
406 cc::LayerPositionConstraint constraint;
407 constraint.set_is_fixed_position(web_constraint.isFixedPosition);
408 constraint.set_is_fixed_to_right_edge(web_constraint.isFixedToRightEdge);
409 constraint.set_is_fixed_to_bottom_edge(web_constraint.isFixedToBottomEdge);
410 return constraint;
413 void WebLayerImpl::setPositionConstraint(
414 const blink::WebLayerPositionConstraint& constraint) {
415 layer_->SetPositionConstraint(ToLayerPositionConstraint(constraint));
418 blink::WebLayerPositionConstraint WebLayerImpl::positionConstraint() const {
419 return ToWebLayerPositionConstraint(layer_->position_constraint());
422 void WebLayerImpl::setScrollClient(blink::WebLayerScrollClient* scroll_client) {
423 if (scroll_client) {
424 layer_->set_did_scroll_callback(
425 base::Bind(&blink::WebLayerScrollClient::didScroll,
426 base::Unretained(scroll_client)));
427 } else {
428 layer_->set_did_scroll_callback(base::Closure());
432 bool WebLayerImpl::isOrphan() const {
433 return !layer_->layer_tree_host();
436 void WebLayerImpl::setWebLayerClient(blink::WebLayerClient* client) {
437 web_layer_client_ = client;
440 class TracedDebugInfo : public base::debug::ConvertableToTraceFormat {
441 public:
442 // This object takes ownership of the debug_info object.
443 explicit TracedDebugInfo(blink::WebGraphicsLayerDebugInfo* debug_info)
444 : debug_info_(debug_info) {}
445 virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE {
446 DCHECK(thread_checker_.CalledOnValidThread());
447 blink::WebString web_string;
448 debug_info_->appendAsTraceFormat(&web_string);
449 out->append(web_string.utf8());
452 private:
453 virtual ~TracedDebugInfo() {}
454 scoped_ptr<blink::WebGraphicsLayerDebugInfo> debug_info_;
455 base::ThreadChecker thread_checker_;
458 scoped_refptr<base::debug::ConvertableToTraceFormat>
459 WebLayerImpl::TakeDebugInfo() {
460 if (!web_layer_client_)
461 return NULL;
462 blink::WebGraphicsLayerDebugInfo* debug_info =
463 web_layer_client_->takeDebugInfoFor(this);
465 if (debug_info)
466 return new TracedDebugInfo(debug_info);
467 else
468 return NULL;
471 void WebLayerImpl::setScrollParent(blink::WebLayer* parent) {
472 cc::Layer* scroll_parent = NULL;
473 if (parent)
474 scroll_parent = static_cast<WebLayerImpl*>(parent)->layer();
475 layer_->SetScrollParent(scroll_parent);
478 void WebLayerImpl::setClipParent(blink::WebLayer* parent) {
479 cc::Layer* clip_parent = NULL;
480 if (parent)
481 clip_parent = static_cast<WebLayerImpl*>(parent)->layer();
482 layer_->SetClipParent(clip_parent);
485 Layer* WebLayerImpl::layer() const {
486 return layer_.get();
489 } // namespace content