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. 4 5 #include "content/renderer/compositor_bindings/web_layer_impl.h" 6 7 #include "base/bind.h" 8 #include "base/debug/trace_event_impl.h" 9 #include "base/lazy_instance.h" 10 #include "base/strings/string_util.h" 11 #include "base/threading/thread_checker.h" 12 #include "cc/animation/animation.h" 13 #include "cc/base/region.h" 14 #include "cc/base/switches.h" 15 #include "cc/layers/layer.h" 16 #include "cc/layers/layer_position_constraint.h" 17 #include "cc/trees/layer_tree_host.h" 18 #include "content/renderer/compositor_bindings/web_animation_impl.h" 19 #include "content/renderer/compositor_bindings/web_blend_mode.h" 20 #include "content/renderer/compositor_bindings/web_filter_operations_impl.h" 21 #include "content/renderer/compositor_bindings/web_to_cc_animation_delegate_adapter.h" 22 #include "third_party/WebKit/public/platform/WebFloatPoint.h" 23 #include "third_party/WebKit/public/platform/WebFloatRect.h" 24 #include "third_party/WebKit/public/platform/WebGraphicsLayerDebugInfo.h" 25 #include "third_party/WebKit/public/platform/WebLayerClient.h" 26 #include "third_party/WebKit/public/platform/WebLayerPositionConstraint.h" 27 #include "third_party/WebKit/public/platform/WebLayerScrollClient.h" 28 #include "third_party/WebKit/public/platform/WebSize.h" 29 #include "third_party/skia/include/utils/SkMatrix44.h" 30 31 using cc::Animation; 32 using cc::Layer; 33 using blink::WebLayer; 34 using blink::WebFloatPoint; 35 using blink::WebVector; 36 using blink::WebRect; 37 using blink::WebSize; 38 using blink::WebColor; 39 using blink::WebFilterOperations; 40 41 namespace content { 42 namespace { 43 44 bool g_impl_side_painting_enabled = false; 45 46 } // namespace 47 48 WebLayerImpl::WebLayerImpl() : layer_(Layer::Create()) { 49 web_layer_client_ = NULL; 50 layer_->SetLayerClient(this); 51 } 52 53 WebLayerImpl::WebLayerImpl(scoped_refptr<Layer> layer) : layer_(layer) { 54 web_layer_client_ = NULL; 55 layer_->SetLayerClient(this); 56 } 57 58 WebLayerImpl::~WebLayerImpl() { 59 layer_->ClearRenderSurface(); 60 layer_->set_layer_animation_delegate(NULL); 61 web_layer_client_ = NULL; 62 } 63 64 // static 65 bool WebLayerImpl::UsingPictureLayer() { 66 return g_impl_side_painting_enabled; 67 } 68 69 // static 70 void WebLayerImpl::SetImplSidePaintingEnabled(bool enabled) { 71 g_impl_side_painting_enabled = enabled; 72 } 73 74 int WebLayerImpl::id() const { 75 return layer_->id(); 76 } 77 78 void WebLayerImpl::invalidateRect(const blink::WebFloatRect& rect) { 79 layer_->SetNeedsDisplayRect(rect); 80 } 81 82 void WebLayerImpl::invalidate() { 83 layer_->SetNeedsDisplay(); 84 } 85 86 void WebLayerImpl::addChild(WebLayer* child) { 87 layer_->AddChild(static_cast<WebLayerImpl*>(child)->layer()); 88 } 89 90 void WebLayerImpl::insertChild(WebLayer* child, size_t index) { 91 layer_->InsertChild(static_cast<WebLayerImpl*>(child)->layer(), index); 92 } 93 94 void WebLayerImpl::replaceChild(WebLayer* reference, WebLayer* new_layer) { 95 layer_->ReplaceChild(static_cast<WebLayerImpl*>(reference)->layer(), 96 static_cast<WebLayerImpl*>(new_layer)->layer()); 97 } 98 99 void WebLayerImpl::removeFromParent() { 100 layer_->RemoveFromParent(); 101 } 102 103 void WebLayerImpl::removeAllChildren() { 104 layer_->RemoveAllChildren(); 105 } 106 107 void WebLayerImpl::setBounds(const WebSize& size) { layer_->SetBounds(size); } 108 109 WebSize WebLayerImpl::bounds() const { 110 return layer_->bounds(); 111 } 112 113 void WebLayerImpl::setMasksToBounds(bool masks_to_bounds) { 114 layer_->SetMasksToBounds(masks_to_bounds); 115 } 116 117 bool WebLayerImpl::masksToBounds() const { 118 return layer_->masks_to_bounds(); 119 } 120 121 void WebLayerImpl::setMaskLayer(WebLayer* maskLayer) { 122 layer_->SetMaskLayer( 123 maskLayer ? static_cast<WebLayerImpl*>(maskLayer)->layer() : 0); 124 } 125 126 void WebLayerImpl::setReplicaLayer(WebLayer* replica_layer) { 127 layer_->SetReplicaLayer( 128 replica_layer ? static_cast<WebLayerImpl*>(replica_layer)->layer() : 0); 129 } 130 131 void WebLayerImpl::setOpacity(float opacity) { 132 layer_->SetOpacity(opacity); 133 } 134 135 float WebLayerImpl::opacity() const { 136 return layer_->opacity(); 137 } 138 139 void WebLayerImpl::setBlendMode(blink::WebBlendMode blend_mode) { 140 layer_->SetBlendMode(BlendModeToSkia(blend_mode)); 141 } 142 143 blink::WebBlendMode WebLayerImpl::blendMode() const { 144 return BlendModeFromSkia(layer_->blend_mode()); 145 } 146 147 void WebLayerImpl::setIsRootForIsolatedGroup(bool isolate) { 148 layer_->SetIsRootForIsolatedGroup(isolate); 149 } 150 151 bool WebLayerImpl::isRootForIsolatedGroup() { 152 return layer_->is_root_for_isolated_group(); 153 } 154 155 void WebLayerImpl::setOpaque(bool opaque) { 156 layer_->SetContentsOpaque(opaque); 157 } 158 159 bool WebLayerImpl::opaque() const { 160 return layer_->contents_opaque(); 161 } 162 163 void WebLayerImpl::setPosition(const WebFloatPoint& position) { 164 layer_->SetPosition(position); 165 } 166 167 WebFloatPoint WebLayerImpl::position() const { 168 return layer_->position(); 169 } 170 171 void WebLayerImpl::setTransform(const SkMatrix44& matrix) { 172 gfx::Transform transform; 173 transform.matrix() = matrix; 174 layer_->SetTransform(transform); 175 } 176 177 void WebLayerImpl::setTransformOrigin(const blink::WebFloatPoint3D& point) { 178 gfx::Point3F gfx_point = point; 179 layer_->SetTransformOrigin(gfx_point); 180 } 181 182 blink::WebFloatPoint3D WebLayerImpl::transformOrigin() const { 183 return layer_->transform_origin(); 184 } 185 186 void WebLayerImpl::setAnchorPoint(const blink::WebFloatPoint&) {} 187 188 blink::WebFloatPoint WebLayerImpl::anchorPoint() const { 189 return blink::WebFloatPoint(); 190 } 191 192 void WebLayerImpl::setAnchorPointZ(float) {} 193 194 float WebLayerImpl::anchorPointZ() const { 195 return 0.f; 196 }; 197 198 SkMatrix44 WebLayerImpl::transform() const { 199 return layer_->transform().matrix(); 200 } 201 202 void WebLayerImpl::setDrawsContent(bool draws_content) { 203 layer_->SetIsDrawable(draws_content); 204 } 205 206 bool WebLayerImpl::drawsContent() const { 207 return layer_->DrawsContent(); 208 } 209 210 void WebLayerImpl::setShouldFlattenTransform(bool flatten) { 211 layer_->SetShouldFlattenTransform(flatten); 212 } 213 214 void WebLayerImpl::setRenderingContext(int context) { 215 layer_->Set3dSortingContextId(context); 216 } 217 218 void WebLayerImpl::setUseParentBackfaceVisibility( 219 bool use_parent_backface_visibility) { 220 layer_->set_use_parent_backface_visibility(use_parent_backface_visibility); 221 } 222 223 void WebLayerImpl::setBackgroundColor(WebColor color) { 224 layer_->SetBackgroundColor(color); 225 } 226 227 WebColor WebLayerImpl::backgroundColor() const { 228 return layer_->background_color(); 229 } 230 231 void WebLayerImpl::setFilters(const WebFilterOperations& filters) { 232 const WebFilterOperationsImpl& filters_impl = 233 static_cast<const WebFilterOperationsImpl&>(filters); 234 layer_->SetFilters(filters_impl.AsFilterOperations()); 235 } 236 237 void WebLayerImpl::setBackgroundFilters(const WebFilterOperations& filters) { 238 const WebFilterOperationsImpl& filters_impl = 239 static_cast<const WebFilterOperationsImpl&>(filters); 240 layer_->SetBackgroundFilters(filters_impl.AsFilterOperations()); 241 } 242 243 void WebLayerImpl::setAnimationDelegate(blink::WebAnimationDelegate* delegate) { 244 animation_delegate_adapter_.reset( 245 new WebToCCAnimationDelegateAdapter(delegate)); 246 layer_->set_layer_animation_delegate(animation_delegate_adapter_.get()); 247 } 248 249 bool WebLayerImpl::addAnimation(blink::WebAnimation* animation) { 250 bool result = layer_->AddAnimation( 251 static_cast<WebAnimationImpl*>(animation)->PassAnimation()); 252 delete animation; 253 return result; 254 } 255 256 void WebLayerImpl::removeAnimation(int animation_id) { 257 layer_->RemoveAnimation(animation_id); 258 } 259 260 void WebLayerImpl::removeAnimation( 261 int animation_id, 262 blink::WebAnimation::TargetProperty target_property) { 263 layer_->layer_animation_controller()->RemoveAnimation( 264 animation_id, static_cast<Animation::TargetProperty>(target_property)); 265 } 266 267 void WebLayerImpl::pauseAnimation(int animation_id, double time_offset) { 268 layer_->PauseAnimation(animation_id, time_offset); 269 } 270 271 bool WebLayerImpl::hasActiveAnimation() { 272 return layer_->HasActiveAnimation(); 273 } 274 275 void WebLayerImpl::setForceRenderSurface(bool force_render_surface) { 276 layer_->SetForceRenderSurface(force_render_surface); 277 } 278 279 void WebLayerImpl::setScrollPosition(blink::WebPoint position) { 280 layer_->SetScrollOffset(gfx::Point(position).OffsetFromOrigin()); 281 } 282 283 blink::WebPoint WebLayerImpl::scrollPosition() const { 284 return gfx::PointAtOffsetFromOrigin(layer_->scroll_offset()); 285 } 286 287 void WebLayerImpl::setScrollClipLayer(WebLayer* clip_layer) { 288 if (!clip_layer) { 289 layer_->SetScrollClipLayerId(Layer::INVALID_ID); 290 return; 291 } 292 layer_->SetScrollClipLayerId(clip_layer->id()); 293 } 294 295 bool WebLayerImpl::scrollable() const { 296 return layer_->scrollable(); 297 } 298 299 void WebLayerImpl::setUserScrollable(bool horizontal, bool vertical) { 300 layer_->SetUserScrollable(horizontal, vertical); 301 } 302 303 bool WebLayerImpl::userScrollableHorizontal() const { 304 return layer_->user_scrollable_horizontal(); 305 } 306 307 bool WebLayerImpl::userScrollableVertical() const { 308 return layer_->user_scrollable_vertical(); 309 } 310 311 void WebLayerImpl::setHaveWheelEventHandlers(bool have_wheel_event_handlers) { 312 layer_->SetHaveWheelEventHandlers(have_wheel_event_handlers); 313 } 314 315 bool WebLayerImpl::haveWheelEventHandlers() const { 316 return layer_->have_wheel_event_handlers(); 317 } 318 319 void WebLayerImpl::setHaveScrollEventHandlers(bool have_scroll_event_handlers) { 320 layer_->SetHaveScrollEventHandlers(have_scroll_event_handlers); 321 } 322 323 bool WebLayerImpl::haveScrollEventHandlers() const { 324 return layer_->have_scroll_event_handlers(); 325 } 326 327 void WebLayerImpl::setShouldScrollOnMainThread( 328 bool should_scroll_on_main_thread) { 329 layer_->SetShouldScrollOnMainThread(should_scroll_on_main_thread); 330 } 331 332 bool WebLayerImpl::shouldScrollOnMainThread() const { 333 return layer_->should_scroll_on_main_thread(); 334 } 335 336 void WebLayerImpl::setNonFastScrollableRegion(const WebVector<WebRect>& rects) { 337 cc::Region region; 338 for (size_t i = 0; i < rects.size(); ++i) 339 region.Union(rects[i]); 340 layer_->SetNonFastScrollableRegion(region); 341 } 342 343 WebVector<WebRect> WebLayerImpl::nonFastScrollableRegion() const { 344 size_t num_rects = 0; 345 for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region()); 346 region_rects.has_rect(); 347 region_rects.next()) 348 ++num_rects; 349 350 WebVector<WebRect> result(num_rects); 351 size_t i = 0; 352 for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region()); 353 region_rects.has_rect(); 354 region_rects.next()) { 355 result[i] = region_rects.rect(); 356 ++i; 357 } 358 return result; 359 } 360 361 void WebLayerImpl::setTouchEventHandlerRegion(const WebVector<WebRect>& rects) { 362 cc::Region region; 363 for (size_t i = 0; i < rects.size(); ++i) 364 region.Union(rects[i]); 365 layer_->SetTouchEventHandlerRegion(region); 366 } 367 368 WebVector<WebRect> WebLayerImpl::touchEventHandlerRegion() const { 369 size_t num_rects = 0; 370 for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region()); 371 region_rects.has_rect(); 372 region_rects.next()) 373 ++num_rects; 374 375 WebVector<WebRect> result(num_rects); 376 size_t i = 0; 377 for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region()); 378 region_rects.has_rect(); 379 region_rects.next()) { 380 result[i] = region_rects.rect(); 381 ++i; 382 } 383 return result; 384 } 385 386 void WebLayerImpl::setIsContainerForFixedPositionLayers(bool enable) { 387 layer_->SetIsContainerForFixedPositionLayers(enable); 388 } 389 390 bool WebLayerImpl::isContainerForFixedPositionLayers() const { 391 return layer_->IsContainerForFixedPositionLayers(); 392 } 393 394 static blink::WebLayerPositionConstraint ToWebLayerPositionConstraint( 395 const cc::LayerPositionConstraint& constraint) { 396 blink::WebLayerPositionConstraint web_constraint; 397 web_constraint.isFixedPosition = constraint.is_fixed_position(); 398 web_constraint.isFixedToRightEdge = constraint.is_fixed_to_right_edge(); 399 web_constraint.isFixedToBottomEdge = constraint.is_fixed_to_bottom_edge(); 400 return web_constraint; 401 } 402 403 static cc::LayerPositionConstraint ToLayerPositionConstraint( 404 const blink::WebLayerPositionConstraint& web_constraint) { 405 cc::LayerPositionConstraint constraint; 406 constraint.set_is_fixed_position(web_constraint.isFixedPosition); 407 constraint.set_is_fixed_to_right_edge(web_constraint.isFixedToRightEdge); 408 constraint.set_is_fixed_to_bottom_edge(web_constraint.isFixedToBottomEdge); 409 return constraint; 410 } 411 412 void WebLayerImpl::setPositionConstraint( 413 const blink::WebLayerPositionConstraint& constraint) { 414 layer_->SetPositionConstraint(ToLayerPositionConstraint(constraint)); 415 } 416 417 blink::WebLayerPositionConstraint WebLayerImpl::positionConstraint() const { 418 return ToWebLayerPositionConstraint(layer_->position_constraint()); 419 } 420 421 void WebLayerImpl::setScrollClient(blink::WebLayerScrollClient* scroll_client) { 422 if (scroll_client) { 423 layer_->set_did_scroll_callback( 424 base::Bind(&blink::WebLayerScrollClient::didScroll, 425 base::Unretained(scroll_client))); 426 } else { 427 layer_->set_did_scroll_callback(base::Closure()); 428 } 429 } 430 431 bool WebLayerImpl::isOrphan() const { 432 return !layer_->layer_tree_host(); 433 } 434 435 void WebLayerImpl::setWebLayerClient(blink::WebLayerClient* client) { 436 web_layer_client_ = client; 437 } 438 439 class TracedDebugInfo : public base::debug::ConvertableToTraceFormat { 440 public: 441 // This object takes ownership of the debug_info object. 442 explicit TracedDebugInfo(blink::WebGraphicsLayerDebugInfo* debug_info) 443 : debug_info_(debug_info) {} 444 virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE { 445 DCHECK(thread_checker_.CalledOnValidThread()); 446 blink::WebString web_string; 447 debug_info_->appendAsTraceFormat(&web_string); 448 out->append(web_string.utf8()); 449 } 450 451 private: 452 virtual ~TracedDebugInfo() {} 453 scoped_ptr<blink::WebGraphicsLayerDebugInfo> debug_info_; 454 base::ThreadChecker thread_checker_; 455 }; 456 457 scoped_refptr<base::debug::ConvertableToTraceFormat> 458 WebLayerImpl::TakeDebugInfo() { 459 if (!web_layer_client_) 460 return NULL; 461 blink::WebGraphicsLayerDebugInfo* debug_info = 462 web_layer_client_->takeDebugInfoFor(this); 463 464 if (debug_info) 465 return new TracedDebugInfo(debug_info); 466 else 467 return NULL; 468 } 469 470 void WebLayerImpl::setScrollParent(blink::WebLayer* parent) { 471 cc::Layer* scroll_parent = NULL; 472 if (parent) 473 scroll_parent = static_cast<WebLayerImpl*>(parent)->layer(); 474 layer_->SetScrollParent(scroll_parent); 475 } 476 477 void WebLayerImpl::setClipParent(blink::WebLayer* parent) { 478 cc::Layer* clip_parent = NULL; 479 if (parent) 480 clip_parent = static_cast<WebLayerImpl*>(parent)->layer(); 481 layer_->SetClipParent(clip_parent); 482 } 483 484 Layer* WebLayerImpl::layer() const { 485 return layer_.get(); 486 } 487 488 } // namespace content 489 490