Roll leveldb to r78 / 1.15.
[chromium-blink-merge.git] / webkit / renderer / compositor_bindings / web_layer_impl.cc
blobfdf719ea7cd96b34637f618dd08658347a99c4fb
1 // Copyright 2011 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 "webkit/renderer/compositor_bindings/web_layer_impl.h"
7 #include "base/bind.h"
8 #include "base/debug/trace_event_impl.h"
9 #include "base/strings/string_util.h"
10 #include "base/threading/thread_checker.h"
11 #include "cc/animation/animation.h"
12 #include "cc/base/region.h"
13 #include "cc/layers/layer.h"
14 #include "cc/layers/layer_position_constraint.h"
15 #include "third_party/WebKit/public/platform/WebCompositingReasons.h"
16 #include "third_party/WebKit/public/platform/WebFloatPoint.h"
17 #include "third_party/WebKit/public/platform/WebFloatRect.h"
18 #include "third_party/WebKit/public/platform/WebGraphicsLayerDebugInfo.h"
19 #include "third_party/WebKit/public/platform/WebLayerClient.h"
20 #include "third_party/WebKit/public/platform/WebLayerPositionConstraint.h"
21 #include "third_party/WebKit/public/platform/WebLayerScrollClient.h"
22 #include "third_party/WebKit/public/platform/WebSize.h"
23 #include "third_party/skia/include/utils/SkMatrix44.h"
24 #include "webkit/renderer/compositor_bindings/web_animation_impl.h"
25 #include "webkit/renderer/compositor_bindings/web_filter_operations_impl.h"
26 #include "webkit/renderer/compositor_bindings/web_to_cc_animation_delegate_adapter.h"
28 using cc::Animation;
29 using cc::Layer;
30 using blink::WebLayer;
31 using blink::WebFloatPoint;
32 using blink::WebVector;
33 using blink::WebRect;
34 using blink::WebSize;
35 using blink::WebColor;
36 using blink::WebFilterOperations;
38 namespace webkit {
40 WebLayerImpl::WebLayerImpl() : layer_(Layer::Create()) {
41 web_layer_client_ = NULL;
42 layer_->SetLayerClient(this);
45 WebLayerImpl::WebLayerImpl(scoped_refptr<Layer> layer) : layer_(layer) {
46 web_layer_client_ = NULL;
47 layer_->SetLayerClient(this);
50 WebLayerImpl::~WebLayerImpl() {
51 layer_->ClearRenderSurface();
52 layer_->set_layer_animation_delegate(NULL);
53 web_layer_client_ = NULL;
56 int WebLayerImpl::id() const { return layer_->id(); }
58 void WebLayerImpl::invalidateRect(const blink::WebFloatRect& rect) {
59 layer_->SetNeedsDisplayRect(rect);
62 void WebLayerImpl::invalidate() { layer_->SetNeedsDisplay(); }
64 void WebLayerImpl::addChild(WebLayer* child) {
65 layer_->AddChild(static_cast<WebLayerImpl*>(child)->layer());
68 void WebLayerImpl::insertChild(WebLayer* child, size_t index) {
69 layer_->InsertChild(static_cast<WebLayerImpl*>(child)->layer(), index);
72 void WebLayerImpl::replaceChild(WebLayer* reference, WebLayer* new_layer) {
73 layer_->ReplaceChild(static_cast<WebLayerImpl*>(reference)->layer(),
74 static_cast<WebLayerImpl*>(new_layer)->layer());
77 void WebLayerImpl::removeFromParent() { layer_->RemoveFromParent(); }
79 void WebLayerImpl::removeAllChildren() { layer_->RemoveAllChildren(); }
81 void WebLayerImpl::setAnchorPoint(const WebFloatPoint& anchor_point) {
82 layer_->SetAnchorPoint(anchor_point);
85 WebFloatPoint WebLayerImpl::anchorPoint() const {
86 return layer_->anchor_point();
89 void WebLayerImpl::setAnchorPointZ(float anchor_point_z) {
90 layer_->SetAnchorPointZ(anchor_point_z);
93 float WebLayerImpl::anchorPointZ() const { return layer_->anchor_point_z(); }
95 void WebLayerImpl::setBounds(const WebSize& size) { layer_->SetBounds(size); }
97 WebSize WebLayerImpl::bounds() const { return layer_->bounds(); }
99 void WebLayerImpl::setMasksToBounds(bool masks_to_bounds) {
100 layer_->SetMasksToBounds(masks_to_bounds);
103 bool WebLayerImpl::masksToBounds() const { return layer_->masks_to_bounds(); }
105 void WebLayerImpl::setMaskLayer(WebLayer* maskLayer) {
106 layer_->SetMaskLayer(
107 maskLayer ? static_cast<WebLayerImpl*>(maskLayer)->layer() : 0);
110 void WebLayerImpl::setReplicaLayer(WebLayer* replica_layer) {
111 layer_->SetReplicaLayer(
112 replica_layer ? static_cast<WebLayerImpl*>(replica_layer)->layer() : 0);
115 void WebLayerImpl::setOpacity(float opacity) { layer_->SetOpacity(opacity); }
117 float WebLayerImpl::opacity() const { return layer_->opacity(); }
119 void WebLayerImpl::setOpaque(bool opaque) { layer_->SetContentsOpaque(opaque); }
121 bool WebLayerImpl::opaque() const { return layer_->contents_opaque(); }
123 void WebLayerImpl::setPosition(const WebFloatPoint& position) {
124 layer_->SetPosition(position);
127 WebFloatPoint WebLayerImpl::position() const { return layer_->position(); }
129 void WebLayerImpl::setSublayerTransform(const SkMatrix44& matrix) {
130 gfx::Transform sub_layer_transform;
131 sub_layer_transform.matrix() = matrix;
132 layer_->SetSublayerTransform(sub_layer_transform);
135 SkMatrix44 WebLayerImpl::sublayerTransform() const {
136 return layer_->sublayer_transform().matrix();
139 void WebLayerImpl::setTransform(const SkMatrix44& matrix) {
140 gfx::Transform transform;
141 transform.matrix() = matrix;
142 layer_->SetTransform(transform);
145 SkMatrix44 WebLayerImpl::transform() const {
146 return layer_->transform().matrix();
149 void WebLayerImpl::setDrawsContent(bool draws_content) {
150 layer_->SetIsDrawable(draws_content);
153 bool WebLayerImpl::drawsContent() const { return layer_->DrawsContent(); }
155 void WebLayerImpl::setPreserves3D(bool preserve3D) {
156 layer_->SetPreserves3d(preserve3D);
159 void WebLayerImpl::setUseParentBackfaceVisibility(
160 bool use_parent_backface_visibility) {
161 layer_->set_use_parent_backface_visibility(use_parent_backface_visibility);
164 void WebLayerImpl::setBackgroundColor(WebColor color) {
165 layer_->SetBackgroundColor(color);
168 WebColor WebLayerImpl::backgroundColor() const {
169 return layer_->background_color();
172 void WebLayerImpl::setFilters(const WebFilterOperations& filters) {
173 const WebFilterOperationsImpl& filters_impl =
174 static_cast<const WebFilterOperationsImpl&>(filters);
175 layer_->SetFilters(filters_impl.AsFilterOperations());
178 void WebLayerImpl::setBackgroundFilters(const WebFilterOperations& filters) {
179 const WebFilterOperationsImpl& filters_impl =
180 static_cast<const WebFilterOperationsImpl&>(filters);
181 layer_->SetBackgroundFilters(filters_impl.AsFilterOperations());
184 void WebLayerImpl::setCompositingReasons(
185 blink::WebCompositingReasons reasons) {
186 layer_->SetCompositingReasons(reasons);
189 void WebLayerImpl::setAnimationDelegate(
190 blink::WebAnimationDelegate* delegate) {
191 animation_delegate_adapter_.reset(
192 new WebToCCAnimationDelegateAdapter(delegate));
193 layer_->set_layer_animation_delegate(animation_delegate_adapter_.get());
196 bool WebLayerImpl::addAnimation(blink::WebAnimation* animation) {
197 bool result = layer_->AddAnimation(
198 static_cast<WebAnimationImpl*>(animation)->PassAnimation());
199 delete animation;
200 return result;
203 void WebLayerImpl::removeAnimation(int animation_id) {
204 layer_->RemoveAnimation(animation_id);
207 void WebLayerImpl::removeAnimation(
208 int animation_id,
209 blink::WebAnimation::TargetProperty target_property) {
210 layer_->layer_animation_controller()->RemoveAnimation(
211 animation_id,
212 static_cast<Animation::TargetProperty>(target_property));
215 void WebLayerImpl::pauseAnimation(int animation_id, double time_offset) {
216 layer_->PauseAnimation(animation_id, time_offset);
219 bool WebLayerImpl::hasActiveAnimation() { return layer_->HasActiveAnimation(); }
221 void WebLayerImpl::setForceRenderSurface(bool force_render_surface) {
222 layer_->SetForceRenderSurface(force_render_surface);
225 void WebLayerImpl::setScrollPosition(blink::WebPoint position) {
226 layer_->SetScrollOffset(gfx::Point(position).OffsetFromOrigin());
229 blink::WebPoint WebLayerImpl::scrollPosition() const {
230 return gfx::PointAtOffsetFromOrigin(layer_->scroll_offset());
233 void WebLayerImpl::setMaxScrollPosition(WebSize max_scroll_position) {
234 layer_->SetMaxScrollOffset(max_scroll_position);
237 WebSize WebLayerImpl::maxScrollPosition() const {
238 return layer_->max_scroll_offset();
241 void WebLayerImpl::setScrollable(bool scrollable) {
242 layer_->SetScrollable(scrollable);
245 bool WebLayerImpl::scrollable() const { return layer_->scrollable(); }
247 void WebLayerImpl::setUserScrollable(bool horizontal, bool vertical) {
248 layer_->SetUserScrollable(horizontal, vertical);
251 bool WebLayerImpl::userScrollableHorizontal() const {
252 return layer_->user_scrollable_horizontal();
255 bool WebLayerImpl::userScrollableVertical() const {
256 return layer_->user_scrollable_vertical();
259 void WebLayerImpl::setHaveWheelEventHandlers(bool have_wheel_event_handlers) {
260 layer_->SetHaveWheelEventHandlers(have_wheel_event_handlers);
263 bool WebLayerImpl::haveWheelEventHandlers() const {
264 return layer_->have_wheel_event_handlers();
267 void WebLayerImpl::setShouldScrollOnMainThread(
268 bool should_scroll_on_main_thread) {
269 layer_->SetShouldScrollOnMainThread(should_scroll_on_main_thread);
272 bool WebLayerImpl::shouldScrollOnMainThread() const {
273 return layer_->should_scroll_on_main_thread();
276 void WebLayerImpl::setNonFastScrollableRegion(const WebVector<WebRect>& rects) {
277 cc::Region region;
278 for (size_t i = 0; i < rects.size(); ++i)
279 region.Union(rects[i]);
280 layer_->SetNonFastScrollableRegion(region);
283 WebVector<WebRect> WebLayerImpl::nonFastScrollableRegion() const {
284 size_t num_rects = 0;
285 for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region());
286 region_rects.has_rect();
287 region_rects.next())
288 ++num_rects;
290 WebVector<WebRect> result(num_rects);
291 size_t i = 0;
292 for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region());
293 region_rects.has_rect();
294 region_rects.next()) {
295 result[i] = region_rects.rect();
296 ++i;
298 return result;
301 void WebLayerImpl::setTouchEventHandlerRegion(const WebVector<WebRect>& rects) {
302 cc::Region region;
303 for (size_t i = 0; i < rects.size(); ++i)
304 region.Union(rects[i]);
305 layer_->SetTouchEventHandlerRegion(region);
308 WebVector<WebRect> WebLayerImpl::touchEventHandlerRegion() const {
309 size_t num_rects = 0;
310 for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region());
311 region_rects.has_rect();
312 region_rects.next())
313 ++num_rects;
315 WebVector<WebRect> result(num_rects);
316 size_t i = 0;
317 for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region());
318 region_rects.has_rect();
319 region_rects.next()) {
320 result[i] = region_rects.rect();
321 ++i;
323 return result;
326 void WebLayerImpl::setIsContainerForFixedPositionLayers(bool enable) {
327 layer_->SetIsContainerForFixedPositionLayers(enable);
330 bool WebLayerImpl::isContainerForFixedPositionLayers() const {
331 return layer_->IsContainerForFixedPositionLayers();
334 static blink::WebLayerPositionConstraint ToWebLayerPositionConstraint(
335 const cc::LayerPositionConstraint& constraint) {
336 blink::WebLayerPositionConstraint web_constraint;
337 web_constraint.isFixedPosition = constraint.is_fixed_position();
338 web_constraint.isFixedToRightEdge = constraint.is_fixed_to_right_edge();
339 web_constraint.isFixedToBottomEdge = constraint.is_fixed_to_bottom_edge();
340 return web_constraint;
343 static cc::LayerPositionConstraint ToLayerPositionConstraint(
344 const blink::WebLayerPositionConstraint& web_constraint) {
345 cc::LayerPositionConstraint constraint;
346 constraint.set_is_fixed_position(web_constraint.isFixedPosition);
347 constraint.set_is_fixed_to_right_edge(web_constraint.isFixedToRightEdge);
348 constraint.set_is_fixed_to_bottom_edge(web_constraint.isFixedToBottomEdge);
349 return constraint;
352 void WebLayerImpl::setPositionConstraint(
353 const blink::WebLayerPositionConstraint& constraint) {
354 layer_->SetPositionConstraint(ToLayerPositionConstraint(constraint));
357 blink::WebLayerPositionConstraint WebLayerImpl::positionConstraint() const {
358 return ToWebLayerPositionConstraint(layer_->position_constraint());
361 void WebLayerImpl::setScrollClient(
362 blink::WebLayerScrollClient* scroll_client) {
363 if (scroll_client) {
364 layer_->set_did_scroll_callback(
365 base::Bind(&blink::WebLayerScrollClient::didScroll,
366 base::Unretained(scroll_client)));
367 } else {
368 layer_->set_did_scroll_callback(base::Closure());
372 bool WebLayerImpl::isOrphan() const { return !layer_->layer_tree_host(); }
374 void WebLayerImpl::setWebLayerClient(blink::WebLayerClient* client) {
375 web_layer_client_ = client;
378 // TODO(chrishtr): move DebugName into this class.
379 class TracedDebugInfo : public base::debug::ConvertableToTraceFormat {
380 public:
381 // This object takes ownership of the debug_info object.
382 explicit TracedDebugInfo(blink::WebGraphicsLayerDebugInfo* debug_info) :
383 debug_info_(debug_info) {}
384 virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE {
385 DCHECK(thread_checker_.CalledOnValidThread());
386 blink::WebString web_string;
387 debug_info_->appendAsTraceFormat(&web_string);
388 out->append(web_string.utf8());
390 private:
391 virtual ~TracedDebugInfo() {}
392 scoped_ptr<blink::WebGraphicsLayerDebugInfo> debug_info_;
393 base::ThreadChecker thread_checker_;
396 scoped_refptr<base::debug::ConvertableToTraceFormat>
397 WebLayerImpl::TakeDebugInfo() {
398 if (!web_layer_client_)
399 return NULL;
400 blink::WebGraphicsLayerDebugInfo* debug_info =
401 web_layer_client_->takeDebugInfo();
403 if (debug_info)
404 return new TracedDebugInfo(debug_info);
405 else
406 return NULL;
409 std::string WebLayerImpl::DebugName() {
410 if (!web_layer_client_)
411 return std::string();
413 std::string name = web_layer_client_->debugName(this).utf8();
414 DCHECK(IsStringASCII(name));
415 return name;
418 void WebLayerImpl::setScrollParent(blink::WebLayer* parent) {
419 cc::Layer* scroll_parent = NULL;
420 if (parent)
421 scroll_parent = static_cast<WebLayerImpl*>(parent)->layer();
422 layer_->SetScrollParent(scroll_parent);
425 void WebLayerImpl::setClipParent(blink::WebLayer* parent) {
426 cc::Layer* clip_parent = NULL;
427 if (parent)
428 clip_parent = static_cast<WebLayerImpl*>(parent)->layer();
429 layer_->SetClipParent(clip_parent);
432 Layer* WebLayerImpl::layer() const { return layer_.get(); }
434 } // namespace webkit