Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / cc / blink / web_layer_impl.cc
blob48f27008e98ba0fb3faddeda6f38d717f85478de
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()
55 : layer_(Layer::Create(LayerSettings())), contents_opaque_is_fixed_(false) {
56 web_layer_client_ = nullptr;
57 layer_->SetLayerClient(this);
60 WebLayerImpl::WebLayerImpl(scoped_refptr<Layer> layer)
61 : layer_(layer), contents_opaque_is_fixed_(false) {
62 web_layer_client_ = nullptr;
63 layer_->SetLayerClient(this);
66 WebLayerImpl::~WebLayerImpl() {
67 if (animation_delegate_adapter_.get())
68 layer_->set_layer_animation_delegate(nullptr);
69 web_layer_client_ = nullptr;
72 // static
73 void WebLayerImpl::SetLayerSettings(const cc::LayerSettings& settings) {
74 g_layer_settings.Get() = settings;
77 // static
78 const cc::LayerSettings& WebLayerImpl::LayerSettings() {
79 return g_layer_settings.Get();
82 int WebLayerImpl::id() const {
83 return layer_->id();
86 void WebLayerImpl::invalidateRect(const blink::WebRect& rect) {
87 layer_->SetNeedsDisplayRect(rect);
90 void WebLayerImpl::invalidate() {
91 layer_->SetNeedsDisplay();
94 void WebLayerImpl::addChild(WebLayer* child) {
95 layer_->AddChild(static_cast<WebLayerImpl*>(child)->layer());
98 void WebLayerImpl::insertChild(WebLayer* child, size_t index) {
99 layer_->InsertChild(static_cast<WebLayerImpl*>(child)->layer(), index);
102 void WebLayerImpl::replaceChild(WebLayer* reference, WebLayer* new_layer) {
103 layer_->ReplaceChild(static_cast<WebLayerImpl*>(reference)->layer(),
104 static_cast<WebLayerImpl*>(new_layer)->layer());
107 void WebLayerImpl::removeFromParent() {
108 layer_->RemoveFromParent();
111 void WebLayerImpl::removeAllChildren() {
112 layer_->RemoveAllChildren();
115 void WebLayerImpl::setBounds(const WebSize& size) {
116 layer_->SetBounds(size);
119 WebSize WebLayerImpl::bounds() const {
120 return layer_->bounds();
123 void WebLayerImpl::setMasksToBounds(bool masks_to_bounds) {
124 layer_->SetMasksToBounds(masks_to_bounds);
127 bool WebLayerImpl::masksToBounds() const {
128 return layer_->masks_to_bounds();
131 void WebLayerImpl::setMaskLayer(WebLayer* maskLayer) {
132 layer_->SetMaskLayer(
133 maskLayer ? static_cast<WebLayerImpl*>(maskLayer)->layer() : 0);
136 void WebLayerImpl::setReplicaLayer(WebLayer* replica_layer) {
137 layer_->SetReplicaLayer(
138 replica_layer ? static_cast<WebLayerImpl*>(replica_layer)->layer() : 0);
141 void WebLayerImpl::setOpacity(float opacity) {
142 layer_->SetOpacity(opacity);
145 float WebLayerImpl::opacity() const {
146 return layer_->opacity();
149 void WebLayerImpl::setBlendMode(blink::WebBlendMode blend_mode) {
150 layer_->SetBlendMode(BlendModeToSkia(blend_mode));
153 blink::WebBlendMode WebLayerImpl::blendMode() const {
154 return BlendModeFromSkia(layer_->blend_mode());
157 void WebLayerImpl::setIsRootForIsolatedGroup(bool isolate) {
158 layer_->SetIsRootForIsolatedGroup(isolate);
161 bool WebLayerImpl::isRootForIsolatedGroup() {
162 return layer_->is_root_for_isolated_group();
165 void WebLayerImpl::setOpaque(bool opaque) {
166 if (contents_opaque_is_fixed_)
167 return;
168 layer_->SetContentsOpaque(opaque);
171 bool WebLayerImpl::opaque() const {
172 return layer_->contents_opaque();
175 void WebLayerImpl::setPosition(const WebFloatPoint& position) {
176 layer_->SetPosition(position);
179 WebFloatPoint WebLayerImpl::position() const {
180 return layer_->position();
183 void WebLayerImpl::setTransform(const SkMatrix44& matrix) {
184 gfx::Transform transform;
185 transform.matrix() = matrix;
186 layer_->SetTransform(transform);
189 void WebLayerImpl::setTransformOrigin(const blink::WebFloatPoint3D& point) {
190 gfx::Point3F gfx_point = point;
191 layer_->SetTransformOrigin(gfx_point);
194 blink::WebFloatPoint3D WebLayerImpl::transformOrigin() const {
195 return layer_->transform_origin();
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_->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::setScrollPositionDouble(blink::WebDoublePoint position) {
281 layer_->SetScrollOffset(gfx::ScrollOffset(position.x, position.y));
284 void WebLayerImpl::setScrollCompensationAdjustment(
285 blink::WebDoublePoint position) {
286 layer_->SetScrollCompensationAdjustment(
287 gfx::Vector2dF(position.x, position.y));
290 blink::WebDoublePoint WebLayerImpl::scrollPositionDouble() const {
291 return blink::WebDoublePoint(layer_->scroll_offset().x(),
292 layer_->scroll_offset().y());
295 void WebLayerImpl::setScrollClipLayer(WebLayer* clip_layer) {
296 if (!clip_layer) {
297 layer_->SetScrollClipLayerId(Layer::INVALID_ID);
298 return;
300 layer_->SetScrollClipLayerId(clip_layer->id());
303 bool WebLayerImpl::scrollable() const {
304 return layer_->scrollable();
307 void WebLayerImpl::setUserScrollable(bool horizontal, bool vertical) {
308 layer_->SetUserScrollable(horizontal, vertical);
311 bool WebLayerImpl::userScrollableHorizontal() const {
312 return layer_->user_scrollable_horizontal();
315 bool WebLayerImpl::userScrollableVertical() const {
316 return layer_->user_scrollable_vertical();
319 void WebLayerImpl::setHaveWheelEventHandlers(bool have_wheel_event_handlers) {
320 layer_->SetHaveWheelEventHandlers(have_wheel_event_handlers);
323 bool WebLayerImpl::haveWheelEventHandlers() const {
324 return layer_->have_wheel_event_handlers();
327 void WebLayerImpl::setHaveScrollEventHandlers(bool have_scroll_event_handlers) {
328 layer_->SetHaveScrollEventHandlers(have_scroll_event_handlers);
331 bool WebLayerImpl::haveScrollEventHandlers() const {
332 return layer_->have_scroll_event_handlers();
335 void WebLayerImpl::setShouldScrollOnMainThread(
336 bool should_scroll_on_main_thread) {
337 layer_->SetShouldScrollOnMainThread(should_scroll_on_main_thread);
340 bool WebLayerImpl::shouldScrollOnMainThread() const {
341 return layer_->should_scroll_on_main_thread();
344 void WebLayerImpl::setNonFastScrollableRegion(const WebVector<WebRect>& rects) {
345 cc::Region region;
346 for (size_t i = 0; i < rects.size(); ++i)
347 region.Union(rects[i]);
348 layer_->SetNonFastScrollableRegion(region);
351 WebVector<WebRect> WebLayerImpl::nonFastScrollableRegion() const {
352 size_t num_rects = 0;
353 for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region());
354 region_rects.has_rect();
355 region_rects.next())
356 ++num_rects;
358 WebVector<WebRect> result(num_rects);
359 size_t i = 0;
360 for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region());
361 region_rects.has_rect();
362 region_rects.next()) {
363 result[i] = region_rects.rect();
364 ++i;
366 return result;
369 void WebLayerImpl::setFrameTimingRequests(
370 const WebVector<std::pair<int64_t, WebRect>>& requests) {
371 std::vector<cc::FrameTimingRequest> frame_timing_requests(requests.size());
372 for (size_t i = 0; i < requests.size(); ++i) {
373 frame_timing_requests[i] = cc::FrameTimingRequest(
374 requests[i].first, gfx::Rect(requests[i].second));
376 layer_->SetFrameTimingRequests(frame_timing_requests);
379 WebVector<std::pair<int64_t, WebRect>> WebLayerImpl::frameTimingRequests()
380 const {
381 const std::vector<cc::FrameTimingRequest>& frame_timing_requests =
382 layer_->FrameTimingRequests();
384 size_t num_requests = frame_timing_requests.size();
386 WebVector<std::pair<int64_t, WebRect>> result(num_requests);
387 for (size_t i = 0; i < num_requests; ++i) {
388 result[i] = std::make_pair(frame_timing_requests[i].id(),
389 frame_timing_requests[i].rect());
391 return result;
394 void WebLayerImpl::setTouchEventHandlerRegion(const WebVector<WebRect>& rects) {
395 cc::Region region;
396 for (size_t i = 0; i < rects.size(); ++i)
397 region.Union(rects[i]);
398 layer_->SetTouchEventHandlerRegion(region);
401 WebVector<WebRect> WebLayerImpl::touchEventHandlerRegion() const {
402 size_t num_rects = 0;
403 for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region());
404 region_rects.has_rect();
405 region_rects.next())
406 ++num_rects;
408 WebVector<WebRect> result(num_rects);
409 size_t i = 0;
410 for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region());
411 region_rects.has_rect();
412 region_rects.next()) {
413 result[i] = region_rects.rect();
414 ++i;
416 return result;
419 static_assert(static_cast<ScrollBlocksOn>(blink::WebScrollBlocksOnNone) ==
420 SCROLL_BLOCKS_ON_NONE,
421 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
422 static_assert(static_cast<ScrollBlocksOn>(blink::WebScrollBlocksOnStartTouch) ==
423 SCROLL_BLOCKS_ON_START_TOUCH,
424 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
425 static_assert(static_cast<ScrollBlocksOn>(blink::WebScrollBlocksOnWheelEvent) ==
426 SCROLL_BLOCKS_ON_WHEEL_EVENT,
427 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
428 static_assert(
429 static_cast<ScrollBlocksOn>(blink::WebScrollBlocksOnScrollEvent) ==
430 SCROLL_BLOCKS_ON_SCROLL_EVENT,
431 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
433 void WebLayerImpl::setScrollBlocksOn(blink::WebScrollBlocksOn blocks) {
434 layer_->SetScrollBlocksOn(static_cast<ScrollBlocksOn>(blocks));
437 blink::WebScrollBlocksOn WebLayerImpl::scrollBlocksOn() const {
438 return static_cast<blink::WebScrollBlocksOn>(layer_->scroll_blocks_on());
441 void WebLayerImpl::setIsContainerForFixedPositionLayers(bool enable) {
442 layer_->SetIsContainerForFixedPositionLayers(enable);
445 bool WebLayerImpl::isContainerForFixedPositionLayers() const {
446 return layer_->IsContainerForFixedPositionLayers();
449 static blink::WebLayerPositionConstraint ToWebLayerPositionConstraint(
450 const cc::LayerPositionConstraint& constraint) {
451 blink::WebLayerPositionConstraint web_constraint;
452 web_constraint.isFixedPosition = constraint.is_fixed_position();
453 web_constraint.isFixedToRightEdge = constraint.is_fixed_to_right_edge();
454 web_constraint.isFixedToBottomEdge = constraint.is_fixed_to_bottom_edge();
455 return web_constraint;
458 static cc::LayerPositionConstraint ToLayerPositionConstraint(
459 const blink::WebLayerPositionConstraint& web_constraint) {
460 cc::LayerPositionConstraint constraint;
461 constraint.set_is_fixed_position(web_constraint.isFixedPosition);
462 constraint.set_is_fixed_to_right_edge(web_constraint.isFixedToRightEdge);
463 constraint.set_is_fixed_to_bottom_edge(web_constraint.isFixedToBottomEdge);
464 return constraint;
467 void WebLayerImpl::setPositionConstraint(
468 const blink::WebLayerPositionConstraint& constraint) {
469 layer_->SetPositionConstraint(ToLayerPositionConstraint(constraint));
472 blink::WebLayerPositionConstraint WebLayerImpl::positionConstraint() const {
473 return ToWebLayerPositionConstraint(layer_->position_constraint());
476 void WebLayerImpl::setScrollClient(blink::WebLayerScrollClient* scroll_client) {
477 if (scroll_client) {
478 layer_->set_did_scroll_callback(
479 base::Bind(&blink::WebLayerScrollClient::didScroll,
480 base::Unretained(scroll_client)));
481 } else {
482 layer_->set_did_scroll_callback(base::Closure());
486 bool WebLayerImpl::isOrphan() const {
487 return !layer_->layer_tree_host();
490 void WebLayerImpl::setWebLayerClient(blink::WebLayerClient* client) {
491 web_layer_client_ = client;
494 class TracedDebugInfo : public base::trace_event::ConvertableToTraceFormat {
495 public:
496 // This object takes ownership of the debug_info object.
497 explicit TracedDebugInfo(blink::WebGraphicsLayerDebugInfo* debug_info)
498 : debug_info_(debug_info) {}
499 void AppendAsTraceFormat(std::string* out) const override {
500 DCHECK(thread_checker_.CalledOnValidThread());
501 blink::WebString web_string;
502 debug_info_->appendAsTraceFormat(&web_string);
503 out->append(web_string.utf8());
506 private:
507 ~TracedDebugInfo() override {}
508 scoped_ptr<blink::WebGraphicsLayerDebugInfo> debug_info_;
509 base::ThreadChecker thread_checker_;
512 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
513 WebLayerImpl::TakeDebugInfo() {
514 if (!web_layer_client_)
515 return nullptr;
516 blink::WebGraphicsLayerDebugInfo* debug_info =
517 web_layer_client_->takeDebugInfoFor(this);
519 if (debug_info)
520 return new TracedDebugInfo(debug_info);
521 else
522 return nullptr;
525 void WebLayerImpl::setScrollParent(blink::WebLayer* parent) {
526 cc::Layer* scroll_parent = nullptr;
527 if (parent)
528 scroll_parent = static_cast<WebLayerImpl*>(parent)->layer();
529 layer_->SetScrollParent(scroll_parent);
532 void WebLayerImpl::setClipParent(blink::WebLayer* parent) {
533 cc::Layer* clip_parent = nullptr;
534 if (parent)
535 clip_parent = static_cast<WebLayerImpl*>(parent)->layer();
536 layer_->SetClipParent(clip_parent);
539 Layer* WebLayerImpl::layer() const {
540 return layer_.get();
543 void WebLayerImpl::SetContentsOpaqueIsFixed(bool fixed) {
544 contents_opaque_is_fixed_ = fixed;
547 } // namespace cc_blink