[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / cc / blink / web_layer_impl.cc
blob3ba9431171d333c280da57f1159fb2272d6dfaf9
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"
7 #include <utility>
8 #include <vector>
10 #include "base/bind.h"
11 #include "base/lazy_instance.h"
12 #include "base/strings/string_util.h"
13 #include "base/threading/thread_checker.h"
14 #include "base/trace_event/trace_event_impl.h"
15 #include "cc/animation/animation.h"
16 #include "cc/base/region.h"
17 #include "cc/base/switches.h"
18 #include "cc/blink/web_animation_impl.h"
19 #include "cc/blink/web_blend_mode.h"
20 #include "cc/blink/web_filter_operations_impl.h"
21 #include "cc/blink/web_to_cc_animation_delegate_adapter.h"
22 #include "cc/layers/layer.h"
23 #include "cc/layers/layer_position_constraint.h"
24 #include "cc/trees/layer_tree_host.h"
25 #include "third_party/WebKit/public/platform/WebFloatPoint.h"
26 #include "third_party/WebKit/public/platform/WebFloatRect.h"
27 #include "third_party/WebKit/public/platform/WebGraphicsLayerDebugInfo.h"
28 #include "third_party/WebKit/public/platform/WebLayerClient.h"
29 #include "third_party/WebKit/public/platform/WebLayerPositionConstraint.h"
30 #include "third_party/WebKit/public/platform/WebLayerScrollClient.h"
31 #include "third_party/WebKit/public/platform/WebSize.h"
32 #include "third_party/skia/include/utils/SkMatrix44.h"
33 #include "ui/gfx/geometry/rect_conversions.h"
34 #include "ui/gfx/geometry/vector2d_conversions.h"
36 using cc::Animation;
37 using cc::Layer;
38 using blink::WebLayer;
39 using blink::WebFloatPoint;
40 using blink::WebVector;
41 using blink::WebRect;
42 using blink::WebSize;
43 using blink::WebColor;
44 using blink::WebFilterOperations;
46 namespace cc_blink {
47 namespace {
49 base::LazyInstance<cc::LayerSettings> g_layer_settings =
50 LAZY_INSTANCE_INITIALIZER;
52 } // namespace
54 WebLayerImpl::WebLayerImpl() : layer_(Layer::Create(LayerSettings())) {
55 web_layer_client_ = nullptr;
56 layer_->SetLayerClient(this);
59 WebLayerImpl::WebLayerImpl(scoped_refptr<Layer> layer) : layer_(layer) {
60 web_layer_client_ = nullptr;
61 layer_->SetLayerClient(this);
64 WebLayerImpl::~WebLayerImpl() {
65 layer_->ClearRenderSurface();
66 if (animation_delegate_adapter_.get())
67 layer_->set_layer_animation_delegate(nullptr);
68 web_layer_client_ = nullptr;
71 // static
72 void WebLayerImpl::SetLayerSettings(const cc::LayerSettings& settings) {
73 g_layer_settings.Get() = settings;
76 // static
77 const cc::LayerSettings& WebLayerImpl::LayerSettings() {
78 return g_layer_settings.Get();
81 int WebLayerImpl::id() const {
82 return layer_->id();
85 void WebLayerImpl::invalidateRect(const blink::WebRect& rect) {
86 layer_->SetNeedsDisplayRect(rect);
89 void WebLayerImpl::invalidate() {
90 layer_->SetNeedsDisplay();
93 void WebLayerImpl::addChild(WebLayer* child) {
94 layer_->AddChild(static_cast<WebLayerImpl*>(child)->layer());
97 void WebLayerImpl::insertChild(WebLayer* child, size_t index) {
98 layer_->InsertChild(static_cast<WebLayerImpl*>(child)->layer(), index);
101 void WebLayerImpl::replaceChild(WebLayer* reference, WebLayer* new_layer) {
102 layer_->ReplaceChild(static_cast<WebLayerImpl*>(reference)->layer(),
103 static_cast<WebLayerImpl*>(new_layer)->layer());
106 void WebLayerImpl::removeFromParent() {
107 layer_->RemoveFromParent();
110 void WebLayerImpl::removeAllChildren() {
111 layer_->RemoveAllChildren();
114 void WebLayerImpl::setBounds(const WebSize& size) {
115 layer_->SetBounds(size);
118 WebSize WebLayerImpl::bounds() const {
119 return layer_->bounds();
122 void WebLayerImpl::setMasksToBounds(bool masks_to_bounds) {
123 layer_->SetMasksToBounds(masks_to_bounds);
126 bool WebLayerImpl::masksToBounds() const {
127 return layer_->masks_to_bounds();
130 void WebLayerImpl::setMaskLayer(WebLayer* maskLayer) {
131 layer_->SetMaskLayer(
132 maskLayer ? static_cast<WebLayerImpl*>(maskLayer)->layer() : 0);
135 void WebLayerImpl::setReplicaLayer(WebLayer* replica_layer) {
136 layer_->SetReplicaLayer(
137 replica_layer ? static_cast<WebLayerImpl*>(replica_layer)->layer() : 0);
140 void WebLayerImpl::setOpacity(float opacity) {
141 layer_->SetOpacity(opacity);
144 float WebLayerImpl::opacity() const {
145 return layer_->opacity();
148 void WebLayerImpl::setBlendMode(blink::WebBlendMode blend_mode) {
149 layer_->SetBlendMode(BlendModeToSkia(blend_mode));
152 blink::WebBlendMode WebLayerImpl::blendMode() const {
153 return BlendModeFromSkia(layer_->blend_mode());
156 void WebLayerImpl::setIsRootForIsolatedGroup(bool isolate) {
157 layer_->SetIsRootForIsolatedGroup(isolate);
160 bool WebLayerImpl::isRootForIsolatedGroup() {
161 return layer_->is_root_for_isolated_group();
164 void WebLayerImpl::setOpaque(bool opaque) {
165 layer_->SetContentsOpaque(opaque);
168 bool WebLayerImpl::opaque() const {
169 return layer_->contents_opaque();
172 void WebLayerImpl::setPosition(const WebFloatPoint& position) {
173 layer_->SetPosition(position);
176 WebFloatPoint WebLayerImpl::position() const {
177 return layer_->position();
180 void WebLayerImpl::setTransform(const SkMatrix44& matrix) {
181 gfx::Transform transform;
182 transform.matrix() = matrix;
183 layer_->SetTransform(transform);
186 void WebLayerImpl::setTransformOrigin(const blink::WebFloatPoint3D& point) {
187 gfx::Point3F gfx_point = point;
188 layer_->SetTransformOrigin(gfx_point);
191 blink::WebFloatPoint3D WebLayerImpl::transformOrigin() const {
192 return layer_->transform_origin();
195 SkMatrix44 WebLayerImpl::transform() const {
196 return layer_->transform().matrix();
199 void WebLayerImpl::setDrawsContent(bool draws_content) {
200 layer_->SetIsDrawable(draws_content);
203 bool WebLayerImpl::drawsContent() const {
204 return layer_->DrawsContent();
207 void WebLayerImpl::setShouldFlattenTransform(bool flatten) {
208 layer_->SetShouldFlattenTransform(flatten);
211 void WebLayerImpl::setRenderingContext(int context) {
212 layer_->Set3dSortingContextId(context);
215 void WebLayerImpl::setUseParentBackfaceVisibility(
216 bool use_parent_backface_visibility) {
217 layer_->set_use_parent_backface_visibility(use_parent_backface_visibility);
220 void WebLayerImpl::setBackgroundColor(WebColor color) {
221 layer_->SetBackgroundColor(color);
224 WebColor WebLayerImpl::backgroundColor() const {
225 return layer_->background_color();
228 void WebLayerImpl::setFilters(const WebFilterOperations& filters) {
229 const WebFilterOperationsImpl& filters_impl =
230 static_cast<const WebFilterOperationsImpl&>(filters);
231 layer_->SetFilters(filters_impl.AsFilterOperations());
234 void WebLayerImpl::setBackgroundFilters(const WebFilterOperations& filters) {
235 const WebFilterOperationsImpl& filters_impl =
236 static_cast<const WebFilterOperationsImpl&>(filters);
237 layer_->SetBackgroundFilters(filters_impl.AsFilterOperations());
240 void WebLayerImpl::setAnimationDelegate(
241 blink::WebCompositorAnimationDelegate* delegate) {
242 animation_delegate_adapter_.reset(
243 new WebToCCAnimationDelegateAdapter(delegate));
244 layer_->set_layer_animation_delegate(animation_delegate_adapter_.get());
247 bool WebLayerImpl::addAnimation(blink::WebCompositorAnimation* animation) {
248 bool result = layer_->AddAnimation(
249 static_cast<WebCompositorAnimationImpl*>(animation)->PassAnimation());
250 delete animation;
251 return result;
254 void WebLayerImpl::removeAnimation(int animation_id) {
255 layer_->RemoveAnimation(animation_id);
258 void WebLayerImpl::removeAnimation(
259 int animation_id,
260 blink::WebCompositorAnimation::TargetProperty target_property) {
261 layer_->RemoveAnimation(
262 animation_id, static_cast<Animation::TargetProperty>(target_property));
265 void WebLayerImpl::pauseAnimation(int animation_id, double time_offset) {
266 layer_->PauseAnimation(animation_id, time_offset);
269 bool WebLayerImpl::hasActiveAnimation() {
270 return layer_->HasActiveAnimation();
273 void WebLayerImpl::setForceRenderSurface(bool force_render_surface) {
274 layer_->SetForceRenderSurface(force_render_surface);
277 void WebLayerImpl::setScrollPositionDouble(blink::WebDoublePoint position) {
278 layer_->SetScrollOffset(gfx::ScrollOffset(position.x, position.y));
281 void WebLayerImpl::setScrollCompensationAdjustment(
282 blink::WebDoublePoint position) {
283 layer_->SetScrollCompensationAdjustment(
284 gfx::Vector2dF(position.x, position.y));
287 blink::WebDoublePoint WebLayerImpl::scrollPositionDouble() const {
288 return blink::WebDoublePoint(layer_->scroll_offset().x(),
289 layer_->scroll_offset().y());
292 void WebLayerImpl::setScrollClipLayer(WebLayer* clip_layer) {
293 if (!clip_layer) {
294 layer_->SetScrollClipLayerId(Layer::INVALID_ID);
295 return;
297 layer_->SetScrollClipLayerId(clip_layer->id());
300 bool WebLayerImpl::scrollable() const {
301 return layer_->scrollable();
304 void WebLayerImpl::setUserScrollable(bool horizontal, bool vertical) {
305 layer_->SetUserScrollable(horizontal, vertical);
308 bool WebLayerImpl::userScrollableHorizontal() const {
309 return layer_->user_scrollable_horizontal();
312 bool WebLayerImpl::userScrollableVertical() const {
313 return layer_->user_scrollable_vertical();
316 void WebLayerImpl::setHaveWheelEventHandlers(bool have_wheel_event_handlers) {
317 layer_->SetHaveWheelEventHandlers(have_wheel_event_handlers);
320 bool WebLayerImpl::haveWheelEventHandlers() const {
321 return layer_->have_wheel_event_handlers();
324 void WebLayerImpl::setHaveScrollEventHandlers(bool have_scroll_event_handlers) {
325 layer_->SetHaveScrollEventHandlers(have_scroll_event_handlers);
328 bool WebLayerImpl::haveScrollEventHandlers() const {
329 return layer_->have_scroll_event_handlers();
332 void WebLayerImpl::setShouldScrollOnMainThread(
333 bool should_scroll_on_main_thread) {
334 layer_->SetShouldScrollOnMainThread(should_scroll_on_main_thread);
337 bool WebLayerImpl::shouldScrollOnMainThread() const {
338 return layer_->should_scroll_on_main_thread();
341 void WebLayerImpl::setNonFastScrollableRegion(const WebVector<WebRect>& rects) {
342 cc::Region region;
343 for (size_t i = 0; i < rects.size(); ++i)
344 region.Union(rects[i]);
345 layer_->SetNonFastScrollableRegion(region);
348 WebVector<WebRect> WebLayerImpl::nonFastScrollableRegion() const {
349 size_t num_rects = 0;
350 for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region());
351 region_rects.has_rect();
352 region_rects.next())
353 ++num_rects;
355 WebVector<WebRect> result(num_rects);
356 size_t i = 0;
357 for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region());
358 region_rects.has_rect();
359 region_rects.next()) {
360 result[i] = region_rects.rect();
361 ++i;
363 return result;
366 void WebLayerImpl::setFrameTimingRequests(
367 const WebVector<std::pair<int64_t, WebRect>>& requests) {
368 std::vector<cc::FrameTimingRequest> frame_timing_requests(requests.size());
369 for (size_t i = 0; i < requests.size(); ++i) {
370 frame_timing_requests[i] = cc::FrameTimingRequest(
371 requests[i].first, gfx::Rect(requests[i].second));
373 layer_->SetFrameTimingRequests(frame_timing_requests);
376 WebVector<std::pair<int64_t, WebRect>> WebLayerImpl::frameTimingRequests()
377 const {
378 const std::vector<cc::FrameTimingRequest>& frame_timing_requests =
379 layer_->FrameTimingRequests();
381 size_t num_requests = frame_timing_requests.size();
383 WebVector<std::pair<int64_t, WebRect>> result(num_requests);
384 for (size_t i = 0; i < num_requests; ++i) {
385 result[i] = std::make_pair(frame_timing_requests[i].id(),
386 frame_timing_requests[i].rect());
388 return result;
391 void WebLayerImpl::setTouchEventHandlerRegion(const WebVector<WebRect>& rects) {
392 cc::Region region;
393 for (size_t i = 0; i < rects.size(); ++i)
394 region.Union(rects[i]);
395 layer_->SetTouchEventHandlerRegion(region);
398 WebVector<WebRect> WebLayerImpl::touchEventHandlerRegion() const {
399 size_t num_rects = 0;
400 for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region());
401 region_rects.has_rect();
402 region_rects.next())
403 ++num_rects;
405 WebVector<WebRect> result(num_rects);
406 size_t i = 0;
407 for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region());
408 region_rects.has_rect();
409 region_rects.next()) {
410 result[i] = region_rects.rect();
411 ++i;
413 return result;
416 static_assert(static_cast<ScrollBlocksOn>(blink::WebScrollBlocksOnNone) ==
417 SCROLL_BLOCKS_ON_NONE,
418 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
419 static_assert(static_cast<ScrollBlocksOn>(blink::WebScrollBlocksOnStartTouch) ==
420 SCROLL_BLOCKS_ON_START_TOUCH,
421 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
422 static_assert(static_cast<ScrollBlocksOn>(blink::WebScrollBlocksOnWheelEvent) ==
423 SCROLL_BLOCKS_ON_WHEEL_EVENT,
424 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
425 static_assert(
426 static_cast<ScrollBlocksOn>(blink::WebScrollBlocksOnScrollEvent) ==
427 SCROLL_BLOCKS_ON_SCROLL_EVENT,
428 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
430 void WebLayerImpl::setScrollBlocksOn(blink::WebScrollBlocksOn blocks) {
431 layer_->SetScrollBlocksOn(static_cast<ScrollBlocksOn>(blocks));
434 blink::WebScrollBlocksOn WebLayerImpl::scrollBlocksOn() const {
435 return static_cast<blink::WebScrollBlocksOn>(layer_->scroll_blocks_on());
438 void WebLayerImpl::setIsContainerForFixedPositionLayers(bool enable) {
439 layer_->SetIsContainerForFixedPositionLayers(enable);
442 bool WebLayerImpl::isContainerForFixedPositionLayers() const {
443 return layer_->IsContainerForFixedPositionLayers();
446 static blink::WebLayerPositionConstraint ToWebLayerPositionConstraint(
447 const cc::LayerPositionConstraint& constraint) {
448 blink::WebLayerPositionConstraint web_constraint;
449 web_constraint.isFixedPosition = constraint.is_fixed_position();
450 web_constraint.isFixedToRightEdge = constraint.is_fixed_to_right_edge();
451 web_constraint.isFixedToBottomEdge = constraint.is_fixed_to_bottom_edge();
452 return web_constraint;
455 static cc::LayerPositionConstraint ToLayerPositionConstraint(
456 const blink::WebLayerPositionConstraint& web_constraint) {
457 cc::LayerPositionConstraint constraint;
458 constraint.set_is_fixed_position(web_constraint.isFixedPosition);
459 constraint.set_is_fixed_to_right_edge(web_constraint.isFixedToRightEdge);
460 constraint.set_is_fixed_to_bottom_edge(web_constraint.isFixedToBottomEdge);
461 return constraint;
464 void WebLayerImpl::setPositionConstraint(
465 const blink::WebLayerPositionConstraint& constraint) {
466 layer_->SetPositionConstraint(ToLayerPositionConstraint(constraint));
469 blink::WebLayerPositionConstraint WebLayerImpl::positionConstraint() const {
470 return ToWebLayerPositionConstraint(layer_->position_constraint());
473 void WebLayerImpl::setScrollClient(blink::WebLayerScrollClient* scroll_client) {
474 if (scroll_client) {
475 layer_->set_did_scroll_callback(
476 base::Bind(&blink::WebLayerScrollClient::didScroll,
477 base::Unretained(scroll_client)));
478 } else {
479 layer_->set_did_scroll_callback(base::Closure());
483 bool WebLayerImpl::isOrphan() const {
484 return !layer_->layer_tree_host();
487 void WebLayerImpl::setWebLayerClient(blink::WebLayerClient* client) {
488 web_layer_client_ = client;
491 class TracedDebugInfo : public base::trace_event::ConvertableToTraceFormat {
492 public:
493 // This object takes ownership of the debug_info object.
494 explicit TracedDebugInfo(blink::WebGraphicsLayerDebugInfo* debug_info)
495 : debug_info_(debug_info) {}
496 void AppendAsTraceFormat(std::string* out) const override {
497 DCHECK(thread_checker_.CalledOnValidThread());
498 blink::WebString web_string;
499 debug_info_->appendAsTraceFormat(&web_string);
500 out->append(web_string.utf8());
503 private:
504 ~TracedDebugInfo() override {}
505 scoped_ptr<blink::WebGraphicsLayerDebugInfo> debug_info_;
506 base::ThreadChecker thread_checker_;
509 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
510 WebLayerImpl::TakeDebugInfo() {
511 if (!web_layer_client_)
512 return nullptr;
513 blink::WebGraphicsLayerDebugInfo* debug_info =
514 web_layer_client_->takeDebugInfoFor(this);
516 if (debug_info)
517 return new TracedDebugInfo(debug_info);
518 else
519 return nullptr;
522 void WebLayerImpl::setScrollParent(blink::WebLayer* parent) {
523 cc::Layer* scroll_parent = nullptr;
524 if (parent)
525 scroll_parent = static_cast<WebLayerImpl*>(parent)->layer();
526 layer_->SetScrollParent(scroll_parent);
529 void WebLayerImpl::setClipParent(blink::WebLayer* parent) {
530 cc::Layer* clip_parent = nullptr;
531 if (parent)
532 clip_parent = static_cast<WebLayerImpl*>(parent)->layer();
533 layer_->SetClipParent(clip_parent);
536 Layer* WebLayerImpl::layer() const {
537 return layer_.get();
540 } // namespace cc_blink