| OLD | NEW |
| (Empty) |
| 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 "sky/viewer/cc/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 "sky/viewer/cc/web_animation_impl.h" | |
| 16 #include "sky/viewer/cc/web_blend_mode.h" | |
| 17 #include "sky/viewer/cc/web_filter_operations_impl.h" | |
| 18 #include "sky/viewer/cc/web_to_cc_animation_delegate_adapter.h" | |
| 19 #include "cc/layers/layer.h" | |
| 20 #include "cc/layers/layer_position_constraint.h" | |
| 21 #include "cc/trees/layer_tree_host.h" | |
| 22 #include "sky/engine/public/platform/WebFloatPoint.h" | |
| 23 #include "sky/engine/public/platform/WebFloatRect.h" | |
| 24 #include "sky/engine/public/platform/WebGraphicsLayerDebugInfo.h" | |
| 25 #include "sky/engine/public/platform/WebLayerClient.h" | |
| 26 #include "sky/engine/public/platform/WebLayerScrollClient.h" | |
| 27 #include "sky/engine/public/platform/WebSize.h" | |
| 28 #include "third_party/skia/include/utils/SkMatrix44.h" | |
| 29 #include "ui/gfx/geometry/rect_conversions.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 sky_viewer_cc { | |
| 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(gfx::ToEnclosingRect(rect)); | |
| 80 } | |
| 81 | |
| 82 void WebLayerImpl::invalidateRect(const blink::WebRect& rect) { | |
| 83 layer_->SetNeedsDisplayRect(rect); | |
| 84 } | |
| 85 | |
| 86 void WebLayerImpl::invalidate() { | |
| 87 layer_->SetNeedsDisplay(); | |
| 88 } | |
| 89 | |
| 90 void WebLayerImpl::addChild(WebLayer* child) { | |
| 91 layer_->AddChild(static_cast<WebLayerImpl*>(child)->layer()); | |
| 92 } | |
| 93 | |
| 94 void WebLayerImpl::insertChild(WebLayer* child, size_t index) { | |
| 95 layer_->InsertChild(static_cast<WebLayerImpl*>(child)->layer(), index); | |
| 96 } | |
| 97 | |
| 98 void WebLayerImpl::replaceChild(WebLayer* reference, WebLayer* new_layer) { | |
| 99 layer_->ReplaceChild(static_cast<WebLayerImpl*>(reference)->layer(), | |
| 100 static_cast<WebLayerImpl*>(new_layer)->layer()); | |
| 101 } | |
| 102 | |
| 103 void WebLayerImpl::removeFromParent() { | |
| 104 layer_->RemoveFromParent(); | |
| 105 } | |
| 106 | |
| 107 void WebLayerImpl::removeAllChildren() { | |
| 108 layer_->RemoveAllChildren(); | |
| 109 } | |
| 110 | |
| 111 void WebLayerImpl::setBounds(const WebSize& size) { | |
| 112 layer_->SetBounds(size); | |
| 113 } | |
| 114 | |
| 115 WebSize WebLayerImpl::bounds() const { | |
| 116 return layer_->bounds(); | |
| 117 } | |
| 118 | |
| 119 void WebLayerImpl::setMasksToBounds(bool masks_to_bounds) { | |
| 120 layer_->SetMasksToBounds(masks_to_bounds); | |
| 121 } | |
| 122 | |
| 123 bool WebLayerImpl::masksToBounds() const { | |
| 124 return layer_->masks_to_bounds(); | |
| 125 } | |
| 126 | |
| 127 void WebLayerImpl::setMaskLayer(WebLayer* maskLayer) { | |
| 128 layer_->SetMaskLayer( | |
| 129 maskLayer ? static_cast<WebLayerImpl*>(maskLayer)->layer() : 0); | |
| 130 } | |
| 131 | |
| 132 void WebLayerImpl::setReplicaLayer(WebLayer* replica_layer) { | |
| 133 layer_->SetReplicaLayer( | |
| 134 replica_layer ? static_cast<WebLayerImpl*>(replica_layer)->layer() : 0); | |
| 135 } | |
| 136 | |
| 137 void WebLayerImpl::setOpacity(float opacity) { | |
| 138 layer_->SetOpacity(opacity); | |
| 139 } | |
| 140 | |
| 141 float WebLayerImpl::opacity() const { | |
| 142 return layer_->opacity(); | |
| 143 } | |
| 144 | |
| 145 void WebLayerImpl::setBlendMode(blink::WebBlendMode blend_mode) { | |
| 146 layer_->SetBlendMode(BlendModeToSkia(blend_mode)); | |
| 147 } | |
| 148 | |
| 149 blink::WebBlendMode WebLayerImpl::blendMode() const { | |
| 150 return BlendModeFromSkia(layer_->blend_mode()); | |
| 151 } | |
| 152 | |
| 153 void WebLayerImpl::setOpaque(bool opaque) { | |
| 154 layer_->SetContentsOpaque(opaque); | |
| 155 } | |
| 156 | |
| 157 bool WebLayerImpl::opaque() const { | |
| 158 return layer_->contents_opaque(); | |
| 159 } | |
| 160 | |
| 161 void WebLayerImpl::setPosition(const WebFloatPoint& position) { | |
| 162 layer_->SetPosition(position); | |
| 163 } | |
| 164 | |
| 165 WebFloatPoint WebLayerImpl::position() const { | |
| 166 return layer_->position(); | |
| 167 } | |
| 168 | |
| 169 void WebLayerImpl::setTransform(const SkMatrix44& matrix) { | |
| 170 gfx::Transform transform; | |
| 171 transform.matrix() = matrix; | |
| 172 layer_->SetTransform(transform); | |
| 173 } | |
| 174 | |
| 175 void WebLayerImpl::setTransformOrigin(const blink::WebFloatPoint3D& point) { | |
| 176 gfx::Point3F gfx_point = point; | |
| 177 layer_->SetTransformOrigin(gfx_point); | |
| 178 } | |
| 179 | |
| 180 blink::WebFloatPoint3D WebLayerImpl::transformOrigin() const { | |
| 181 return layer_->transform_origin(); | |
| 182 } | |
| 183 | |
| 184 SkMatrix44 WebLayerImpl::transform() const { | |
| 185 return layer_->transform().matrix(); | |
| 186 } | |
| 187 | |
| 188 void WebLayerImpl::setDrawsContent(bool draws_content) { | |
| 189 layer_->SetIsDrawable(draws_content); | |
| 190 } | |
| 191 | |
| 192 bool WebLayerImpl::drawsContent() const { | |
| 193 return layer_->DrawsContent(); | |
| 194 } | |
| 195 | |
| 196 void WebLayerImpl::setShouldFlattenTransform(bool flatten) { | |
| 197 layer_->SetShouldFlattenTransform(flatten); | |
| 198 } | |
| 199 | |
| 200 void WebLayerImpl::setRenderingContext(int context) { | |
| 201 layer_->Set3dSortingContextId(context); | |
| 202 } | |
| 203 | |
| 204 void WebLayerImpl::setUseParentBackfaceVisibility( | |
| 205 bool use_parent_backface_visibility) { | |
| 206 layer_->set_use_parent_backface_visibility(use_parent_backface_visibility); | |
| 207 } | |
| 208 | |
| 209 void WebLayerImpl::setBackgroundColor(WebColor color) { | |
| 210 layer_->SetBackgroundColor(color); | |
| 211 } | |
| 212 | |
| 213 WebColor WebLayerImpl::backgroundColor() const { | |
| 214 return layer_->background_color(); | |
| 215 } | |
| 216 | |
| 217 void WebLayerImpl::setFilters(const WebFilterOperations& filters) { | |
| 218 const WebFilterOperationsImpl& filters_impl = | |
| 219 static_cast<const WebFilterOperationsImpl&>(filters); | |
| 220 layer_->SetFilters(filters_impl.AsFilterOperations()); | |
| 221 } | |
| 222 | |
| 223 void WebLayerImpl::setBackgroundFilters(const WebFilterOperations& filters) { | |
| 224 const WebFilterOperationsImpl& filters_impl = | |
| 225 static_cast<const WebFilterOperationsImpl&>(filters); | |
| 226 layer_->SetBackgroundFilters(filters_impl.AsFilterOperations()); | |
| 227 } | |
| 228 | |
| 229 void WebLayerImpl::setAnimationDelegate( | |
| 230 blink::WebCompositorAnimationDelegate* delegate) { | |
| 231 animation_delegate_adapter_.reset( | |
| 232 new WebToCCAnimationDelegateAdapter(delegate)); | |
| 233 layer_->set_layer_animation_delegate(animation_delegate_adapter_.get()); | |
| 234 } | |
| 235 | |
| 236 bool WebLayerImpl::addAnimation(blink::WebCompositorAnimation* animation) { | |
| 237 bool result = layer_->AddAnimation( | |
| 238 static_cast<WebCompositorAnimationImpl*>(animation)->PassAnimation()); | |
| 239 delete animation; | |
| 240 return result; | |
| 241 } | |
| 242 | |
| 243 void WebLayerImpl::removeAnimation(int animation_id) { | |
| 244 layer_->RemoveAnimation(animation_id); | |
| 245 } | |
| 246 | |
| 247 void WebLayerImpl::removeAnimation( | |
| 248 int animation_id, | |
| 249 blink::WebCompositorAnimation::TargetProperty target_property) { | |
| 250 layer_->layer_animation_controller()->RemoveAnimation( | |
| 251 animation_id, static_cast<Animation::TargetProperty>(target_property)); | |
| 252 } | |
| 253 | |
| 254 void WebLayerImpl::pauseAnimation(int animation_id, double time_offset) { | |
| 255 layer_->PauseAnimation(animation_id, time_offset); | |
| 256 } | |
| 257 | |
| 258 bool WebLayerImpl::hasActiveAnimation() { | |
| 259 return layer_->HasActiveAnimation(); | |
| 260 } | |
| 261 | |
| 262 void WebLayerImpl::setForceRenderSurface(bool force_render_surface) { | |
| 263 layer_->SetForceRenderSurface(force_render_surface); | |
| 264 } | |
| 265 | |
| 266 void WebLayerImpl::setScrollPosition(blink::WebPoint position) { | |
| 267 layer_->SetScrollOffset(gfx::ScrollOffset(position.x, position.y)); | |
| 268 } | |
| 269 | |
| 270 blink::WebPoint WebLayerImpl::scrollPosition() const { | |
| 271 return gfx::PointAtOffsetFromOrigin( | |
| 272 gfx::ScrollOffsetToFlooredVector2d(layer_->scroll_offset())); | |
| 273 } | |
| 274 | |
| 275 void WebLayerImpl::setScrollClipLayer(WebLayer* clip_layer) { | |
| 276 if (!clip_layer) { | |
| 277 layer_->SetScrollClipLayerId(Layer::INVALID_ID); | |
| 278 return; | |
| 279 } | |
| 280 layer_->SetScrollClipLayerId(clip_layer->id()); | |
| 281 } | |
| 282 | |
| 283 void WebLayerImpl::setScrollClient(blink::WebLayerScrollClient* scroll_client) { | |
| 284 if (scroll_client) { | |
| 285 layer_->set_did_scroll_callback( | |
| 286 base::Bind(&blink::WebLayerScrollClient::didScroll, | |
| 287 base::Unretained(scroll_client))); | |
| 288 } else { | |
| 289 layer_->set_did_scroll_callback(base::Closure()); | |
| 290 } | |
| 291 } | |
| 292 | |
| 293 bool WebLayerImpl::isOrphan() const { | |
| 294 return !layer_->layer_tree_host(); | |
| 295 } | |
| 296 | |
| 297 void WebLayerImpl::setWebLayerClient(blink::WebLayerClient* client) { | |
| 298 web_layer_client_ = client; | |
| 299 } | |
| 300 | |
| 301 class TracedDebugInfo : public base::debug::ConvertableToTraceFormat { | |
| 302 public: | |
| 303 // This object takes ownership of the debug_info object. | |
| 304 explicit TracedDebugInfo(blink::WebGraphicsLayerDebugInfo* debug_info) | |
| 305 : debug_info_(debug_info) {} | |
| 306 virtual void AppendAsTraceFormat(std::string* out) const override { | |
| 307 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 308 blink::WebString web_string; | |
| 309 debug_info_->appendAsTraceFormat(&web_string); | |
| 310 out->append(web_string.utf8()); | |
| 311 } | |
| 312 | |
| 313 private: | |
| 314 virtual ~TracedDebugInfo() {} | |
| 315 scoped_ptr<blink::WebGraphicsLayerDebugInfo> debug_info_; | |
| 316 base::ThreadChecker thread_checker_; | |
| 317 }; | |
| 318 | |
| 319 scoped_refptr<base::debug::ConvertableToTraceFormat> | |
| 320 WebLayerImpl::TakeDebugInfo() { | |
| 321 if (!web_layer_client_) | |
| 322 return NULL; | |
| 323 blink::WebGraphicsLayerDebugInfo* debug_info = | |
| 324 web_layer_client_->takeDebugInfoFor(this); | |
| 325 | |
| 326 if (debug_info) | |
| 327 return new TracedDebugInfo(debug_info); | |
| 328 else | |
| 329 return NULL; | |
| 330 } | |
| 331 | |
| 332 void WebLayerImpl::setScrollParent(blink::WebLayer* parent) { | |
| 333 cc::Layer* scroll_parent = NULL; | |
| 334 if (parent) | |
| 335 scroll_parent = static_cast<WebLayerImpl*>(parent)->layer(); | |
| 336 layer_->SetScrollParent(scroll_parent); | |
| 337 } | |
| 338 | |
| 339 void WebLayerImpl::setClipParent(blink::WebLayer* parent) { | |
| 340 cc::Layer* clip_parent = NULL; | |
| 341 if (parent) | |
| 342 clip_parent = static_cast<WebLayerImpl*>(parent)->layer(); | |
| 343 layer_->SetClipParent(clip_parent); | |
| 344 } | |
| 345 | |
| 346 Layer* WebLayerImpl::layer() const { | |
| 347 return layer_.get(); | |
| 348 } | |
| 349 | |
| 350 } // namespace sky_viewer_cc | |
| OLD | NEW |