blob: 3c688a6bbd41ecde8d412817f8177df7ebe0a7a7 [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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.
4
5#include "cc/blink/web_layer_impl.h"
6
Etienne Membrives386015a2015-02-19 17:27:12 +01007#include <utility>
8#include <vector>
9
James Robinson646469d2014-10-03 15:33:28 -070010#include "base/bind.h"
James Robinson646469d2014-10-03 15:33:28 -070011#include "base/lazy_instance.h"
12#include "base/strings/string_util.h"
13#include "base/threading/thread_checker.h"
Benjamin Lermancdfc88d2015-02-03 14:35:12 +010014#include "base/trace_event/trace_event_impl.h"
James Robinson646469d2014-10-03 15:33:28 -070015#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"
James Robinsone2ac7e82014-10-15 13:21:59 -070033#include "ui/gfx/geometry/rect_conversions.h"
James Robinson646469d2014-10-03 15:33:28 -070034#include "ui/gfx/geometry/vector2d_conversions.h"
35
36using cc::Animation;
37using cc::Layer;
38using blink::WebLayer;
39using blink::WebFloatPoint;
40using blink::WebVector;
41using blink::WebRect;
42using blink::WebSize;
43using blink::WebColor;
44using blink::WebFilterOperations;
45
46namespace cc_blink {
47namespace {
48
49bool g_impl_side_painting_enabled = false;
50
51} // namespace
52
53WebLayerImpl::WebLayerImpl() : layer_(Layer::Create()) {
James Robinsone2ac7e82014-10-15 13:21:59 -070054 web_layer_client_ = nullptr;
James Robinson646469d2014-10-03 15:33:28 -070055 layer_->SetLayerClient(this);
56}
57
58WebLayerImpl::WebLayerImpl(scoped_refptr<Layer> layer) : layer_(layer) {
James Robinsone2ac7e82014-10-15 13:21:59 -070059 web_layer_client_ = nullptr;
James Robinson646469d2014-10-03 15:33:28 -070060 layer_->SetLayerClient(this);
61}
62
63WebLayerImpl::~WebLayerImpl() {
64 layer_->ClearRenderSurface();
James Robinsone2ac7e82014-10-15 13:21:59 -070065 layer_->set_layer_animation_delegate(nullptr);
66 web_layer_client_ = nullptr;
James Robinson646469d2014-10-03 15:33:28 -070067}
68
69// static
70bool WebLayerImpl::UsingPictureLayer() {
71 return g_impl_side_painting_enabled;
72}
73
74// static
75void WebLayerImpl::SetImplSidePaintingEnabled(bool enabled) {
76 g_impl_side_painting_enabled = enabled;
77}
78
79int WebLayerImpl::id() const {
80 return layer_->id();
81}
82
James Robinsone2ac7e82014-10-15 13:21:59 -070083void WebLayerImpl::invalidateRect(const blink::WebRect& rect) {
James Robinson646469d2014-10-03 15:33:28 -070084 layer_->SetNeedsDisplayRect(rect);
85}
86
87void WebLayerImpl::invalidate() {
88 layer_->SetNeedsDisplay();
89}
90
91void WebLayerImpl::addChild(WebLayer* child) {
92 layer_->AddChild(static_cast<WebLayerImpl*>(child)->layer());
93}
94
95void WebLayerImpl::insertChild(WebLayer* child, size_t index) {
96 layer_->InsertChild(static_cast<WebLayerImpl*>(child)->layer(), index);
97}
98
99void WebLayerImpl::replaceChild(WebLayer* reference, WebLayer* new_layer) {
100 layer_->ReplaceChild(static_cast<WebLayerImpl*>(reference)->layer(),
101 static_cast<WebLayerImpl*>(new_layer)->layer());
102}
103
104void WebLayerImpl::removeFromParent() {
105 layer_->RemoveFromParent();
106}
107
108void WebLayerImpl::removeAllChildren() {
109 layer_->RemoveAllChildren();
110}
111
112void WebLayerImpl::setBounds(const WebSize& size) {
113 layer_->SetBounds(size);
114}
115
116WebSize WebLayerImpl::bounds() const {
117 return layer_->bounds();
118}
119
120void WebLayerImpl::setMasksToBounds(bool masks_to_bounds) {
121 layer_->SetMasksToBounds(masks_to_bounds);
122}
123
124bool WebLayerImpl::masksToBounds() const {
125 return layer_->masks_to_bounds();
126}
127
128void WebLayerImpl::setMaskLayer(WebLayer* maskLayer) {
129 layer_->SetMaskLayer(
130 maskLayer ? static_cast<WebLayerImpl*>(maskLayer)->layer() : 0);
131}
132
133void WebLayerImpl::setReplicaLayer(WebLayer* replica_layer) {
134 layer_->SetReplicaLayer(
135 replica_layer ? static_cast<WebLayerImpl*>(replica_layer)->layer() : 0);
136}
137
138void WebLayerImpl::setOpacity(float opacity) {
139 layer_->SetOpacity(opacity);
140}
141
142float WebLayerImpl::opacity() const {
143 return layer_->opacity();
144}
145
146void WebLayerImpl::setBlendMode(blink::WebBlendMode blend_mode) {
147 layer_->SetBlendMode(BlendModeToSkia(blend_mode));
148}
149
150blink::WebBlendMode WebLayerImpl::blendMode() const {
151 return BlendModeFromSkia(layer_->blend_mode());
152}
153
154void WebLayerImpl::setIsRootForIsolatedGroup(bool isolate) {
155 layer_->SetIsRootForIsolatedGroup(isolate);
156}
157
158bool WebLayerImpl::isRootForIsolatedGroup() {
159 return layer_->is_root_for_isolated_group();
160}
161
162void WebLayerImpl::setOpaque(bool opaque) {
163 layer_->SetContentsOpaque(opaque);
164}
165
166bool WebLayerImpl::opaque() const {
167 return layer_->contents_opaque();
168}
169
170void WebLayerImpl::setPosition(const WebFloatPoint& position) {
171 layer_->SetPosition(position);
172}
173
174WebFloatPoint WebLayerImpl::position() const {
175 return layer_->position();
176}
177
178void WebLayerImpl::setTransform(const SkMatrix44& matrix) {
179 gfx::Transform transform;
180 transform.matrix() = matrix;
181 layer_->SetTransform(transform);
182}
183
184void WebLayerImpl::setTransformOrigin(const blink::WebFloatPoint3D& point) {
185 gfx::Point3F gfx_point = point;
186 layer_->SetTransformOrigin(gfx_point);
187}
188
189blink::WebFloatPoint3D WebLayerImpl::transformOrigin() const {
190 return layer_->transform_origin();
191}
192
193SkMatrix44 WebLayerImpl::transform() const {
194 return layer_->transform().matrix();
195}
196
197void WebLayerImpl::setDrawsContent(bool draws_content) {
198 layer_->SetIsDrawable(draws_content);
199}
200
201bool WebLayerImpl::drawsContent() const {
202 return layer_->DrawsContent();
203}
204
205void WebLayerImpl::setShouldFlattenTransform(bool flatten) {
206 layer_->SetShouldFlattenTransform(flatten);
207}
208
209void WebLayerImpl::setRenderingContext(int context) {
210 layer_->Set3dSortingContextId(context);
211}
212
213void WebLayerImpl::setUseParentBackfaceVisibility(
214 bool use_parent_backface_visibility) {
215 layer_->set_use_parent_backface_visibility(use_parent_backface_visibility);
216}
217
218void WebLayerImpl::setBackgroundColor(WebColor color) {
219 layer_->SetBackgroundColor(color);
220}
221
222WebColor WebLayerImpl::backgroundColor() const {
223 return layer_->background_color();
224}
225
226void WebLayerImpl::setFilters(const WebFilterOperations& filters) {
227 const WebFilterOperationsImpl& filters_impl =
228 static_cast<const WebFilterOperationsImpl&>(filters);
229 layer_->SetFilters(filters_impl.AsFilterOperations());
230}
231
232void WebLayerImpl::setBackgroundFilters(const WebFilterOperations& filters) {
233 const WebFilterOperationsImpl& filters_impl =
234 static_cast<const WebFilterOperationsImpl&>(filters);
235 layer_->SetBackgroundFilters(filters_impl.AsFilterOperations());
236}
237
238void 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());
243}
244
245bool WebLayerImpl::addAnimation(blink::WebCompositorAnimation* animation) {
246 bool result = layer_->AddAnimation(
247 static_cast<WebCompositorAnimationImpl*>(animation)->PassAnimation());
248 delete animation;
249 return result;
250}
251
252void WebLayerImpl::removeAnimation(int animation_id) {
253 layer_->RemoveAnimation(animation_id);
254}
255
256void WebLayerImpl::removeAnimation(
257 int animation_id,
258 blink::WebCompositorAnimation::TargetProperty target_property) {
Benjamin Lerman507bb1a2015-02-26 13:15:17 +0100259 layer_->RemoveAnimation(
James Robinson646469d2014-10-03 15:33:28 -0700260 animation_id, static_cast<Animation::TargetProperty>(target_property));
261}
262
263void WebLayerImpl::pauseAnimation(int animation_id, double time_offset) {
264 layer_->PauseAnimation(animation_id, time_offset);
265}
266
267bool WebLayerImpl::hasActiveAnimation() {
268 return layer_->HasActiveAnimation();
269}
270
271void WebLayerImpl::setForceRenderSurface(bool force_render_surface) {
272 layer_->SetForceRenderSurface(force_render_surface);
273}
274
James Robinson646469d2014-10-03 15:33:28 -0700275void WebLayerImpl::setScrollPositionDouble(blink::WebDoublePoint position) {
276 layer_->SetScrollOffset(gfx::ScrollOffset(position.x, position.y));
277}
278
Benjamin Lermancdfc88d2015-02-03 14:35:12 +0100279void WebLayerImpl::setScrollCompensationAdjustment(
280 blink::WebDoublePoint position) {
281 layer_->SetScrollCompensationAdjustment(
282 gfx::Vector2dF(position.x, position.y));
283}
284
James Robinson646469d2014-10-03 15:33:28 -0700285blink::WebDoublePoint WebLayerImpl::scrollPositionDouble() const {
286 return blink::WebDoublePoint(layer_->scroll_offset().x(),
287 layer_->scroll_offset().y());
288}
289
290void WebLayerImpl::setScrollClipLayer(WebLayer* clip_layer) {
291 if (!clip_layer) {
292 layer_->SetScrollClipLayerId(Layer::INVALID_ID);
293 return;
294 }
295 layer_->SetScrollClipLayerId(clip_layer->id());
296}
297
298bool WebLayerImpl::scrollable() const {
299 return layer_->scrollable();
300}
301
302void WebLayerImpl::setUserScrollable(bool horizontal, bool vertical) {
303 layer_->SetUserScrollable(horizontal, vertical);
304}
305
306bool WebLayerImpl::userScrollableHorizontal() const {
307 return layer_->user_scrollable_horizontal();
308}
309
310bool WebLayerImpl::userScrollableVertical() const {
311 return layer_->user_scrollable_vertical();
312}
313
314void WebLayerImpl::setHaveWheelEventHandlers(bool have_wheel_event_handlers) {
315 layer_->SetHaveWheelEventHandlers(have_wheel_event_handlers);
316}
317
318bool WebLayerImpl::haveWheelEventHandlers() const {
319 return layer_->have_wheel_event_handlers();
320}
321
322void WebLayerImpl::setHaveScrollEventHandlers(bool have_scroll_event_handlers) {
323 layer_->SetHaveScrollEventHandlers(have_scroll_event_handlers);
324}
325
326bool WebLayerImpl::haveScrollEventHandlers() const {
327 return layer_->have_scroll_event_handlers();
328}
329
330void WebLayerImpl::setShouldScrollOnMainThread(
331 bool should_scroll_on_main_thread) {
332 layer_->SetShouldScrollOnMainThread(should_scroll_on_main_thread);
333}
334
335bool WebLayerImpl::shouldScrollOnMainThread() const {
336 return layer_->should_scroll_on_main_thread();
337}
338
339void 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);
344}
345
346WebVector<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;
352
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;
360 }
361 return result;
362}
363
Etienne Membrives386015a2015-02-19 17:27:12 +0100364void 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)));
370 }
371 layer_->SetFrameTimingRequests(frame_timing_requests);
372}
373
James Robinson646469d2014-10-03 15:33:28 -0700374void 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);
379}
380
381WebVector<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;
387
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;
395 }
396 return result;
397}
398
James Robinson7b766f42015-02-06 15:14:04 -0800399static_assert(static_cast<ScrollBlocksOn>(blink::WebScrollBlocksOnNone) ==
Benjamin Lermane8ca9b72015-02-24 16:42:13 +0100400 SCROLL_BLOCKS_ON_NONE,
James Robinson7b766f42015-02-06 15:14:04 -0800401 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
402static_assert(static_cast<ScrollBlocksOn>(blink::WebScrollBlocksOnStartTouch) ==
Benjamin Lermane8ca9b72015-02-24 16:42:13 +0100403 SCROLL_BLOCKS_ON_START_TOUCH,
James Robinson7b766f42015-02-06 15:14:04 -0800404 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
405static_assert(static_cast<ScrollBlocksOn>(blink::WebScrollBlocksOnWheelEvent) ==
Benjamin Lermane8ca9b72015-02-24 16:42:13 +0100406 SCROLL_BLOCKS_ON_WHEEL_EVENT,
James Robinson7b766f42015-02-06 15:14:04 -0800407 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
408static_assert(
409 static_cast<ScrollBlocksOn>(blink::WebScrollBlocksOnScrollEvent) ==
Benjamin Lermane8ca9b72015-02-24 16:42:13 +0100410 SCROLL_BLOCKS_ON_SCROLL_EVENT,
James Robinson7b766f42015-02-06 15:14:04 -0800411 "ScrollBlocksOn and WebScrollBlocksOn enums must match");
412
413void WebLayerImpl::setScrollBlocksOn(blink::WebScrollBlocksOn blocks) {
414 layer_->SetScrollBlocksOn(static_cast<ScrollBlocksOn>(blocks));
415}
416
417blink::WebScrollBlocksOn WebLayerImpl::scrollBlocksOn() const {
418 return static_cast<blink::WebScrollBlocksOn>(layer_->scroll_blocks_on());
419}
420
James Robinson646469d2014-10-03 15:33:28 -0700421void WebLayerImpl::setIsContainerForFixedPositionLayers(bool enable) {
422 layer_->SetIsContainerForFixedPositionLayers(enable);
423}
424
425bool WebLayerImpl::isContainerForFixedPositionLayers() const {
426 return layer_->IsContainerForFixedPositionLayers();
427}
428
429static 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;
436}
437
438static 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;
445}
446
447void WebLayerImpl::setPositionConstraint(
448 const blink::WebLayerPositionConstraint& constraint) {
449 layer_->SetPositionConstraint(ToLayerPositionConstraint(constraint));
450}
451
452blink::WebLayerPositionConstraint WebLayerImpl::positionConstraint() const {
453 return ToWebLayerPositionConstraint(layer_->position_constraint());
454}
455
456void 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());
463 }
464}
465
466bool WebLayerImpl::isOrphan() const {
467 return !layer_->layer_tree_host();
468}
469
470void WebLayerImpl::setWebLayerClient(blink::WebLayerClient* client) {
471 web_layer_client_ = client;
472}
473
Etienne Membrives386015a2015-02-19 17:27:12 +0100474class TracedDebugInfo : public base::trace_event::ConvertableToTraceFormat {
James Robinson646469d2014-10-03 15:33:28 -0700475 public:
476 // This object takes ownership of the debug_info object.
477 explicit TracedDebugInfo(blink::WebGraphicsLayerDebugInfo* debug_info)
478 : debug_info_(debug_info) {}
James Robinsone1b30cf2014-10-21 12:25:40 -0700479 void AppendAsTraceFormat(std::string* out) const override {
James Robinson646469d2014-10-03 15:33:28 -0700480 DCHECK(thread_checker_.CalledOnValidThread());
481 blink::WebString web_string;
482 debug_info_->appendAsTraceFormat(&web_string);
483 out->append(web_string.utf8());
484 }
485
486 private:
James Robinsone1b30cf2014-10-21 12:25:40 -0700487 ~TracedDebugInfo() override {}
James Robinson646469d2014-10-03 15:33:28 -0700488 scoped_ptr<blink::WebGraphicsLayerDebugInfo> debug_info_;
489 base::ThreadChecker thread_checker_;
490};
491
Etienne Membrives386015a2015-02-19 17:27:12 +0100492scoped_refptr<base::trace_event::ConvertableToTraceFormat>
James Robinson646469d2014-10-03 15:33:28 -0700493WebLayerImpl::TakeDebugInfo() {
494 if (!web_layer_client_)
James Robinsone2ac7e82014-10-15 13:21:59 -0700495 return nullptr;
James Robinson646469d2014-10-03 15:33:28 -0700496 blink::WebGraphicsLayerDebugInfo* debug_info =
497 web_layer_client_->takeDebugInfoFor(this);
498
499 if (debug_info)
500 return new TracedDebugInfo(debug_info);
501 else
James Robinsone2ac7e82014-10-15 13:21:59 -0700502 return nullptr;
James Robinson646469d2014-10-03 15:33:28 -0700503}
504
505void WebLayerImpl::setScrollParent(blink::WebLayer* parent) {
James Robinsone2ac7e82014-10-15 13:21:59 -0700506 cc::Layer* scroll_parent = nullptr;
James Robinson646469d2014-10-03 15:33:28 -0700507 if (parent)
508 scroll_parent = static_cast<WebLayerImpl*>(parent)->layer();
509 layer_->SetScrollParent(scroll_parent);
510}
511
512void WebLayerImpl::setClipParent(blink::WebLayer* parent) {
James Robinsone2ac7e82014-10-15 13:21:59 -0700513 cc::Layer* clip_parent = nullptr;
James Robinson646469d2014-10-03 15:33:28 -0700514 if (parent)
515 clip_parent = static_cast<WebLayerImpl*>(parent)->layer();
516 layer_->SetClipParent(clip_parent);
517}
518
519Layer* WebLayerImpl::layer() const {
520 return layer_.get();
521}
522
523} // namespace cc_blink