Re-land: C++ readability review
[chromium-blink-merge.git] / cc / blink / web_layer_impl.cc
blob3c688a6bbd41ecde8d412817f8177df7ebe0a7a7
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.push_back(cc::FrameTimingRequest(
369 requests[i].first, gfx::Rect(requests[i].second)));
371 layer_->SetFrameTimingRequests(frame_timing_requests);
374 void WebLayerImpl::setTouchEventHandlerRegion(const WebVector<WebRect>& rects) {
375 cc::Region region;
376 for (size_t i = 0; i < rects.size(); ++i)
377 region.Union(rects[i]);
378 layer_->SetTouchEventHandlerRegion(region);
381 WebVector<WebRect> WebLayerImpl::touchEventHandlerRegion() const {
382 size_t num_rects = 0;
383 for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region());
384 region_rects.has_rect();
385 region_rects.next())
386 ++num_rects;
388 WebVector<WebRect> result(num_rects);
389 size_t i = 0;
390 for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region());
391 region_rects.has_rect();
392 region_rects.next()) {
393 result[i] = region_rects.rect();
394 ++i;
396 return result;
399 static_assert(static_cast<ScrollBlocksOn>(blink::WebScrollBlocksOnNone) ==
400 SCROLL_BLOCKS_ON_NONE,
401 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
402 static_assert(static_cast<ScrollBlocksOn>(blink::WebScrollBlocksOnStartTouch) ==
403 SCROLL_BLOCKS_ON_START_TOUCH,
404 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
405 static_assert(static_cast<ScrollBlocksOn>(blink::WebScrollBlocksOnWheelEvent) ==
406 SCROLL_BLOCKS_ON_WHEEL_EVENT,
407 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
408 static_assert(
409 static_cast<ScrollBlocksOn>(blink::WebScrollBlocksOnScrollEvent) ==
410 SCROLL_BLOCKS_ON_SCROLL_EVENT,
411 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
413 void WebLayerImpl::setScrollBlocksOn(blink::WebScrollBlocksOn blocks) {
414 layer_->SetScrollBlocksOn(static_cast<ScrollBlocksOn>(blocks));
417 blink::WebScrollBlocksOn WebLayerImpl::scrollBlocksOn() const {
418 return static_cast<blink::WebScrollBlocksOn>(layer_->scroll_blocks_on());
421 void WebLayerImpl::setIsContainerForFixedPositionLayers(bool enable) {
422 layer_->SetIsContainerForFixedPositionLayers(enable);
425 bool WebLayerImpl::isContainerForFixedPositionLayers() const {
426 return layer_->IsContainerForFixedPositionLayers();
429 static blink::WebLayerPositionConstraint ToWebLayerPositionConstraint(
430 const cc::LayerPositionConstraint& constraint) {
431 blink::WebLayerPositionConstraint web_constraint;
432 web_constraint.isFixedPosition = constraint.is_fixed_position();
433 web_constraint.isFixedToRightEdge = constraint.is_fixed_to_right_edge();
434 web_constraint.isFixedToBottomEdge = constraint.is_fixed_to_bottom_edge();
435 return web_constraint;
438 static cc::LayerPositionConstraint ToLayerPositionConstraint(
439 const blink::WebLayerPositionConstraint& web_constraint) {
440 cc::LayerPositionConstraint constraint;
441 constraint.set_is_fixed_position(web_constraint.isFixedPosition);
442 constraint.set_is_fixed_to_right_edge(web_constraint.isFixedToRightEdge);
443 constraint.set_is_fixed_to_bottom_edge(web_constraint.isFixedToBottomEdge);
444 return constraint;
447 void WebLayerImpl::setPositionConstraint(
448 const blink::WebLayerPositionConstraint& constraint) {
449 layer_->SetPositionConstraint(ToLayerPositionConstraint(constraint));
452 blink::WebLayerPositionConstraint WebLayerImpl::positionConstraint() const {
453 return ToWebLayerPositionConstraint(layer_->position_constraint());
456 void WebLayerImpl::setScrollClient(blink::WebLayerScrollClient* scroll_client) {
457 if (scroll_client) {
458 layer_->set_did_scroll_callback(
459 base::Bind(&blink::WebLayerScrollClient::didScroll,
460 base::Unretained(scroll_client)));
461 } else {
462 layer_->set_did_scroll_callback(base::Closure());
466 bool WebLayerImpl::isOrphan() const {
467 return !layer_->layer_tree_host();
470 void WebLayerImpl::setWebLayerClient(blink::WebLayerClient* client) {
471 web_layer_client_ = client;
474 class TracedDebugInfo : public base::trace_event::ConvertableToTraceFormat {
475 public:
476 // This object takes ownership of the debug_info object.
477 explicit TracedDebugInfo(blink::WebGraphicsLayerDebugInfo* debug_info)
478 : debug_info_(debug_info) {}
479 void AppendAsTraceFormat(std::string* out) const override {
480 DCHECK(thread_checker_.CalledOnValidThread());
481 blink::WebString web_string;
482 debug_info_->appendAsTraceFormat(&web_string);
483 out->append(web_string.utf8());
486 private:
487 ~TracedDebugInfo() override {}
488 scoped_ptr<blink::WebGraphicsLayerDebugInfo> debug_info_;
489 base::ThreadChecker thread_checker_;
492 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
493 WebLayerImpl::TakeDebugInfo() {
494 if (!web_layer_client_)
495 return nullptr;
496 blink::WebGraphicsLayerDebugInfo* debug_info =
497 web_layer_client_->takeDebugInfoFor(this);
499 if (debug_info)
500 return new TracedDebugInfo(debug_info);
501 else
502 return nullptr;
505 void WebLayerImpl::setScrollParent(blink::WebLayer* parent) {
506 cc::Layer* scroll_parent = nullptr;
507 if (parent)
508 scroll_parent = static_cast<WebLayerImpl*>(parent)->layer();
509 layer_->SetScrollParent(scroll_parent);
512 void WebLayerImpl::setClipParent(blink::WebLayer* parent) {
513 cc::Layer* clip_parent = nullptr;
514 if (parent)
515 clip_parent = static_cast<WebLayerImpl*>(parent)->layer();
516 layer_->SetClipParent(clip_parent);
519 Layer* WebLayerImpl::layer() const {
520 return layer_.get();
523 } // namespace cc_blink