Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / cc / blink / web_layer_impl.cc
blob8bad92a002e6b9bd13d3aca21f2b800da2ddeded
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 bool g_impl_side_painting_enabled = false;
51 } // namespace
53 WebLayerImpl::WebLayerImpl() : layer_(Layer::Create()) {
54 web_layer_client_ = nullptr;
55 layer_->SetLayerClient(this);
58 WebLayerImpl::WebLayerImpl(scoped_refptr<Layer> layer) : layer_(layer) {
59 web_layer_client_ = nullptr;
60 layer_->SetLayerClient(this);
63 WebLayerImpl::~WebLayerImpl() {
64 layer_->ClearRenderSurface();
65 layer_->set_layer_animation_delegate(nullptr);
66 web_layer_client_ = nullptr;
69 // static
70 bool WebLayerImpl::UsingPictureLayer() {
71 return g_impl_side_painting_enabled;
74 // static
75 void WebLayerImpl::SetImplSidePaintingEnabled(bool enabled) {
76 g_impl_side_painting_enabled = enabled;
79 int WebLayerImpl::id() const {
80 return layer_->id();
83 void WebLayerImpl::invalidateRect(const blink::WebRect& rect) {
84 layer_->SetNeedsDisplayRect(rect);
87 void WebLayerImpl::invalidate() {
88 layer_->SetNeedsDisplay();
91 void WebLayerImpl::addChild(WebLayer* child) {
92 layer_->AddChild(static_cast<WebLayerImpl*>(child)->layer());
95 void WebLayerImpl::insertChild(WebLayer* child, size_t index) {
96 layer_->InsertChild(static_cast<WebLayerImpl*>(child)->layer(), index);
99 void WebLayerImpl::replaceChild(WebLayer* reference, WebLayer* new_layer) {
100 layer_->ReplaceChild(static_cast<WebLayerImpl*>(reference)->layer(),
101 static_cast<WebLayerImpl*>(new_layer)->layer());
104 void WebLayerImpl::removeFromParent() {
105 layer_->RemoveFromParent();
108 void WebLayerImpl::removeAllChildren() {
109 layer_->RemoveAllChildren();
112 void WebLayerImpl::setBounds(const WebSize& size) {
113 layer_->SetBounds(size);
116 WebSize WebLayerImpl::bounds() const {
117 return layer_->bounds();
120 void WebLayerImpl::setMasksToBounds(bool masks_to_bounds) {
121 layer_->SetMasksToBounds(masks_to_bounds);
124 bool WebLayerImpl::masksToBounds() const {
125 return layer_->masks_to_bounds();
128 void WebLayerImpl::setMaskLayer(WebLayer* maskLayer) {
129 layer_->SetMaskLayer(
130 maskLayer ? static_cast<WebLayerImpl*>(maskLayer)->layer() : 0);
133 void WebLayerImpl::setReplicaLayer(WebLayer* replica_layer) {
134 layer_->SetReplicaLayer(
135 replica_layer ? static_cast<WebLayerImpl*>(replica_layer)->layer() : 0);
138 void WebLayerImpl::setOpacity(float opacity) {
139 layer_->SetOpacity(opacity);
142 float WebLayerImpl::opacity() const {
143 return layer_->opacity();
146 void WebLayerImpl::setBlendMode(blink::WebBlendMode blend_mode) {
147 layer_->SetBlendMode(BlendModeToSkia(blend_mode));
150 blink::WebBlendMode WebLayerImpl::blendMode() const {
151 return BlendModeFromSkia(layer_->blend_mode());
154 void WebLayerImpl::setIsRootForIsolatedGroup(bool isolate) {
155 layer_->SetIsRootForIsolatedGroup(isolate);
158 bool WebLayerImpl::isRootForIsolatedGroup() {
159 return layer_->is_root_for_isolated_group();
162 void WebLayerImpl::setOpaque(bool opaque) {
163 layer_->SetContentsOpaque(opaque);
166 bool WebLayerImpl::opaque() const {
167 return layer_->contents_opaque();
170 void WebLayerImpl::setPosition(const WebFloatPoint& position) {
171 layer_->SetPosition(position);
174 WebFloatPoint WebLayerImpl::position() const {
175 return layer_->position();
178 void WebLayerImpl::setTransform(const SkMatrix44& matrix) {
179 gfx::Transform transform;
180 transform.matrix() = matrix;
181 layer_->SetTransform(transform);
184 void WebLayerImpl::setTransformOrigin(const blink::WebFloatPoint3D& point) {
185 gfx::Point3F gfx_point = point;
186 layer_->SetTransformOrigin(gfx_point);
189 blink::WebFloatPoint3D WebLayerImpl::transformOrigin() const {
190 return layer_->transform_origin();
193 SkMatrix44 WebLayerImpl::transform() const {
194 return layer_->transform().matrix();
197 void WebLayerImpl::setDrawsContent(bool draws_content) {
198 layer_->SetIsDrawable(draws_content);
201 bool WebLayerImpl::drawsContent() const {
202 return layer_->DrawsContent();
205 void WebLayerImpl::setShouldFlattenTransform(bool flatten) {
206 layer_->SetShouldFlattenTransform(flatten);
209 void WebLayerImpl::setRenderingContext(int context) {
210 layer_->Set3dSortingContextId(context);
213 void WebLayerImpl::setUseParentBackfaceVisibility(
214 bool use_parent_backface_visibility) {
215 layer_->set_use_parent_backface_visibility(use_parent_backface_visibility);
218 void WebLayerImpl::setBackgroundColor(WebColor color) {
219 layer_->SetBackgroundColor(color);
222 WebColor WebLayerImpl::backgroundColor() const {
223 return layer_->background_color();
226 void WebLayerImpl::setFilters(const WebFilterOperations& filters) {
227 const WebFilterOperationsImpl& filters_impl =
228 static_cast<const WebFilterOperationsImpl&>(filters);
229 layer_->SetFilters(filters_impl.AsFilterOperations());
232 void WebLayerImpl::setBackgroundFilters(const WebFilterOperations& filters) {
233 const WebFilterOperationsImpl& filters_impl =
234 static_cast<const WebFilterOperationsImpl&>(filters);
235 layer_->SetBackgroundFilters(filters_impl.AsFilterOperations());
238 void WebLayerImpl::setAnimationDelegate(
239 blink::WebCompositorAnimationDelegate* delegate) {
240 animation_delegate_adapter_.reset(
241 new WebToCCAnimationDelegateAdapter(delegate));
242 layer_->set_layer_animation_delegate(animation_delegate_adapter_.get());
245 bool WebLayerImpl::addAnimation(blink::WebCompositorAnimation* animation) {
246 bool result = layer_->AddAnimation(
247 static_cast<WebCompositorAnimationImpl*>(animation)->PassAnimation());
248 delete animation;
249 return result;
252 void WebLayerImpl::removeAnimation(int animation_id) {
253 layer_->RemoveAnimation(animation_id);
256 void WebLayerImpl::removeAnimation(
257 int animation_id,
258 blink::WebCompositorAnimation::TargetProperty target_property) {
259 layer_->RemoveAnimation(
260 animation_id, static_cast<Animation::TargetProperty>(target_property));
263 void WebLayerImpl::pauseAnimation(int animation_id, double time_offset) {
264 layer_->PauseAnimation(animation_id, time_offset);
267 bool WebLayerImpl::hasActiveAnimation() {
268 return layer_->HasActiveAnimation();
271 void WebLayerImpl::setForceRenderSurface(bool force_render_surface) {
272 layer_->SetForceRenderSurface(force_render_surface);
275 void WebLayerImpl::setScrollPositionDouble(blink::WebDoublePoint position) {
276 layer_->SetScrollOffset(gfx::ScrollOffset(position.x, position.y));
279 void WebLayerImpl::setScrollCompensationAdjustment(
280 blink::WebDoublePoint position) {
281 layer_->SetScrollCompensationAdjustment(
282 gfx::Vector2dF(position.x, position.y));
285 blink::WebDoublePoint WebLayerImpl::scrollPositionDouble() const {
286 return blink::WebDoublePoint(layer_->scroll_offset().x(),
287 layer_->scroll_offset().y());
290 void WebLayerImpl::setScrollClipLayer(WebLayer* clip_layer) {
291 if (!clip_layer) {
292 layer_->SetScrollClipLayerId(Layer::INVALID_ID);
293 return;
295 layer_->SetScrollClipLayerId(clip_layer->id());
298 bool WebLayerImpl::scrollable() const {
299 return layer_->scrollable();
302 void WebLayerImpl::setUserScrollable(bool horizontal, bool vertical) {
303 layer_->SetUserScrollable(horizontal, vertical);
306 bool WebLayerImpl::userScrollableHorizontal() const {
307 return layer_->user_scrollable_horizontal();
310 bool WebLayerImpl::userScrollableVertical() const {
311 return layer_->user_scrollable_vertical();
314 void WebLayerImpl::setHaveWheelEventHandlers(bool have_wheel_event_handlers) {
315 layer_->SetHaveWheelEventHandlers(have_wheel_event_handlers);
318 bool WebLayerImpl::haveWheelEventHandlers() const {
319 return layer_->have_wheel_event_handlers();
322 void WebLayerImpl::setHaveScrollEventHandlers(bool have_scroll_event_handlers) {
323 layer_->SetHaveScrollEventHandlers(have_scroll_event_handlers);
326 bool WebLayerImpl::haveScrollEventHandlers() const {
327 return layer_->have_scroll_event_handlers();
330 void WebLayerImpl::setShouldScrollOnMainThread(
331 bool should_scroll_on_main_thread) {
332 layer_->SetShouldScrollOnMainThread(should_scroll_on_main_thread);
335 bool WebLayerImpl::shouldScrollOnMainThread() const {
336 return layer_->should_scroll_on_main_thread();
339 void WebLayerImpl::setNonFastScrollableRegion(const WebVector<WebRect>& rects) {
340 cc::Region region;
341 for (size_t i = 0; i < rects.size(); ++i)
342 region.Union(rects[i]);
343 layer_->SetNonFastScrollableRegion(region);
346 WebVector<WebRect> WebLayerImpl::nonFastScrollableRegion() const {
347 size_t num_rects = 0;
348 for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region());
349 region_rects.has_rect();
350 region_rects.next())
351 ++num_rects;
353 WebVector<WebRect> result(num_rects);
354 size_t i = 0;
355 for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region());
356 region_rects.has_rect();
357 region_rects.next()) {
358 result[i] = region_rects.rect();
359 ++i;
361 return result;
364 void WebLayerImpl::setFrameTimingRequests(
365 const WebVector<std::pair<int64_t, WebRect>>& requests) {
366 std::vector<cc::FrameTimingRequest> frame_timing_requests(requests.size());
367 for (size_t i = 0; i < requests.size(); ++i) {
368 frame_timing_requests[i] = cc::FrameTimingRequest(
369 requests[i].first, gfx::Rect(requests[i].second));
371 layer_->SetFrameTimingRequests(frame_timing_requests);
374 WebVector<std::pair<int64_t, WebRect>> WebLayerImpl::frameTimingRequests()
375 const {
376 const std::vector<cc::FrameTimingRequest>& frame_timing_requests =
377 layer_->FrameTimingRequests();
379 size_t num_requests = frame_timing_requests.size();
381 WebVector<std::pair<int64_t, WebRect>> result(num_requests);
382 for (size_t i = 0; i < num_requests; ++i) {
383 result[i] = std::make_pair(frame_timing_requests[i].id(),
384 frame_timing_requests[i].rect());
386 return result;
389 void WebLayerImpl::setTouchEventHandlerRegion(const WebVector<WebRect>& rects) {
390 cc::Region region;
391 for (size_t i = 0; i < rects.size(); ++i)
392 region.Union(rects[i]);
393 layer_->SetTouchEventHandlerRegion(region);
396 WebVector<WebRect> WebLayerImpl::touchEventHandlerRegion() const {
397 size_t num_rects = 0;
398 for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region());
399 region_rects.has_rect();
400 region_rects.next())
401 ++num_rects;
403 WebVector<WebRect> result(num_rects);
404 size_t i = 0;
405 for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region());
406 region_rects.has_rect();
407 region_rects.next()) {
408 result[i] = region_rects.rect();
409 ++i;
411 return result;
414 static_assert(static_cast<ScrollBlocksOn>(blink::WebScrollBlocksOnNone) ==
415 SCROLL_BLOCKS_ON_NONE,
416 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
417 static_assert(static_cast<ScrollBlocksOn>(blink::WebScrollBlocksOnStartTouch) ==
418 SCROLL_BLOCKS_ON_START_TOUCH,
419 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
420 static_assert(static_cast<ScrollBlocksOn>(blink::WebScrollBlocksOnWheelEvent) ==
421 SCROLL_BLOCKS_ON_WHEEL_EVENT,
422 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
423 static_assert(
424 static_cast<ScrollBlocksOn>(blink::WebScrollBlocksOnScrollEvent) ==
425 SCROLL_BLOCKS_ON_SCROLL_EVENT,
426 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
428 void WebLayerImpl::setScrollBlocksOn(blink::WebScrollBlocksOn blocks) {
429 layer_->SetScrollBlocksOn(static_cast<ScrollBlocksOn>(blocks));
432 blink::WebScrollBlocksOn WebLayerImpl::scrollBlocksOn() const {
433 return static_cast<blink::WebScrollBlocksOn>(layer_->scroll_blocks_on());
436 void WebLayerImpl::setIsContainerForFixedPositionLayers(bool enable) {
437 layer_->SetIsContainerForFixedPositionLayers(enable);
440 bool WebLayerImpl::isContainerForFixedPositionLayers() const {
441 return layer_->IsContainerForFixedPositionLayers();
444 static blink::WebLayerPositionConstraint ToWebLayerPositionConstraint(
445 const cc::LayerPositionConstraint& constraint) {
446 blink::WebLayerPositionConstraint web_constraint;
447 web_constraint.isFixedPosition = constraint.is_fixed_position();
448 web_constraint.isFixedToRightEdge = constraint.is_fixed_to_right_edge();
449 web_constraint.isFixedToBottomEdge = constraint.is_fixed_to_bottom_edge();
450 return web_constraint;
453 static cc::LayerPositionConstraint ToLayerPositionConstraint(
454 const blink::WebLayerPositionConstraint& web_constraint) {
455 cc::LayerPositionConstraint constraint;
456 constraint.set_is_fixed_position(web_constraint.isFixedPosition);
457 constraint.set_is_fixed_to_right_edge(web_constraint.isFixedToRightEdge);
458 constraint.set_is_fixed_to_bottom_edge(web_constraint.isFixedToBottomEdge);
459 return constraint;
462 void WebLayerImpl::setPositionConstraint(
463 const blink::WebLayerPositionConstraint& constraint) {
464 layer_->SetPositionConstraint(ToLayerPositionConstraint(constraint));
467 blink::WebLayerPositionConstraint WebLayerImpl::positionConstraint() const {
468 return ToWebLayerPositionConstraint(layer_->position_constraint());
471 void WebLayerImpl::setScrollClient(blink::WebLayerScrollClient* scroll_client) {
472 if (scroll_client) {
473 layer_->set_did_scroll_callback(
474 base::Bind(&blink::WebLayerScrollClient::didScroll,
475 base::Unretained(scroll_client)));
476 } else {
477 layer_->set_did_scroll_callback(base::Closure());
481 bool WebLayerImpl::isOrphan() const {
482 return !layer_->layer_tree_host();
485 void WebLayerImpl::setWebLayerClient(blink::WebLayerClient* client) {
486 web_layer_client_ = client;
489 class TracedDebugInfo : public base::trace_event::ConvertableToTraceFormat {
490 public:
491 // This object takes ownership of the debug_info object.
492 explicit TracedDebugInfo(blink::WebGraphicsLayerDebugInfo* debug_info)
493 : debug_info_(debug_info) {}
494 void AppendAsTraceFormat(std::string* out) const override {
495 DCHECK(thread_checker_.CalledOnValidThread());
496 blink::WebString web_string;
497 debug_info_->appendAsTraceFormat(&web_string);
498 out->append(web_string.utf8());
501 private:
502 ~TracedDebugInfo() override {}
503 scoped_ptr<blink::WebGraphicsLayerDebugInfo> debug_info_;
504 base::ThreadChecker thread_checker_;
507 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
508 WebLayerImpl::TakeDebugInfo() {
509 if (!web_layer_client_)
510 return nullptr;
511 blink::WebGraphicsLayerDebugInfo* debug_info =
512 web_layer_client_->takeDebugInfoFor(this);
514 if (debug_info)
515 return new TracedDebugInfo(debug_info);
516 else
517 return nullptr;
520 void WebLayerImpl::setScrollParent(blink::WebLayer* parent) {
521 cc::Layer* scroll_parent = nullptr;
522 if (parent)
523 scroll_parent = static_cast<WebLayerImpl*>(parent)->layer();
524 layer_->SetScrollParent(scroll_parent);
527 void WebLayerImpl::setClipParent(blink::WebLayer* parent) {
528 cc::Layer* clip_parent = nullptr;
529 if (parent)
530 clip_parent = static_cast<WebLayerImpl*>(parent)->layer();
531 layer_->SetClipParent(clip_parent);
534 Layer* WebLayerImpl::layer() const {
535 return layer_.get();
538 } // namespace cc_blink