Home | History | Annotate | Download | only in compositor_bindings
      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.
      4 
      5 #include "webkit/renderer/compositor_bindings/web_layer_impl.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/strings/string_util.h"
      9 #include "cc/animation/animation.h"
     10 #include "cc/base/region.h"
     11 #include "cc/layers/layer.h"
     12 #include "cc/layers/layer_position_constraint.h"
     13 #include "third_party/WebKit/public/platform/WebCompositingReasons.h"
     14 #include "third_party/WebKit/public/platform/WebFloatPoint.h"
     15 #include "third_party/WebKit/public/platform/WebFloatRect.h"
     16 #include "third_party/WebKit/public/platform/WebLayerPositionConstraint.h"
     17 #include "third_party/WebKit/public/platform/WebLayerScrollClient.h"
     18 #include "third_party/WebKit/public/platform/WebSize.h"
     19 #include "third_party/skia/include/utils/SkMatrix44.h"
     20 #include "webkit/renderer/compositor_bindings/web_animation_impl.h"
     21 #include "webkit/renderer/compositor_bindings/web_filter_operations_impl.h"
     22 #include "webkit/renderer/compositor_bindings/web_to_cc_animation_delegate_adapter.h"
     23 
     24 using cc::Animation;
     25 using cc::Layer;
     26 using WebKit::WebLayer;
     27 using WebKit::WebFloatPoint;
     28 using WebKit::WebVector;
     29 using WebKit::WebRect;
     30 using WebKit::WebSize;
     31 using WebKit::WebColor;
     32 using WebKit::WebFilterOperations;
     33 
     34 namespace webkit {
     35 
     36 WebLayerImpl::WebLayerImpl() : layer_(Layer::Create()) {}
     37 
     38 WebLayerImpl::WebLayerImpl(scoped_refptr<Layer> layer) : layer_(layer) {}
     39 
     40 WebLayerImpl::~WebLayerImpl() {
     41   layer_->ClearRenderSurface();
     42   layer_->set_layer_animation_delegate(NULL);
     43 }
     44 
     45 int WebLayerImpl::id() const { return layer_->id(); }
     46 
     47 void WebLayerImpl::invalidateRect(const WebKit::WebFloatRect& rect) {
     48   layer_->SetNeedsDisplayRect(rect);
     49 }
     50 
     51 void WebLayerImpl::invalidate() { layer_->SetNeedsDisplay(); }
     52 
     53 void WebLayerImpl::addChild(WebLayer* child) {
     54   layer_->AddChild(static_cast<WebLayerImpl*>(child)->layer());
     55 }
     56 
     57 void WebLayerImpl::insertChild(WebLayer* child, size_t index) {
     58   layer_->InsertChild(static_cast<WebLayerImpl*>(child)->layer(), index);
     59 }
     60 
     61 void WebLayerImpl::replaceChild(WebLayer* reference, WebLayer* new_layer) {
     62   layer_->ReplaceChild(static_cast<WebLayerImpl*>(reference)->layer(),
     63                        static_cast<WebLayerImpl*>(new_layer)->layer());
     64 }
     65 
     66 void WebLayerImpl::removeFromParent() { layer_->RemoveFromParent(); }
     67 
     68 void WebLayerImpl::removeAllChildren() { layer_->RemoveAllChildren(); }
     69 
     70 void WebLayerImpl::setAnchorPoint(const WebFloatPoint& anchor_point) {
     71   layer_->SetAnchorPoint(anchor_point);
     72 }
     73 
     74 WebFloatPoint WebLayerImpl::anchorPoint() const {
     75   return layer_->anchor_point();
     76 }
     77 
     78 void WebLayerImpl::setAnchorPointZ(float anchor_point_z) {
     79   layer_->SetAnchorPointZ(anchor_point_z);
     80 }
     81 
     82 float WebLayerImpl::anchorPointZ() const { return layer_->anchor_point_z(); }
     83 
     84 void WebLayerImpl::setBounds(const WebSize& size) { layer_->SetBounds(size); }
     85 
     86 WebSize WebLayerImpl::bounds() const { return layer_->bounds(); }
     87 
     88 void WebLayerImpl::setMasksToBounds(bool masks_to_bounds) {
     89   layer_->SetMasksToBounds(masks_to_bounds);
     90 }
     91 
     92 bool WebLayerImpl::masksToBounds() const { return layer_->masks_to_bounds(); }
     93 
     94 void WebLayerImpl::setMaskLayer(WebLayer* maskLayer) {
     95   layer_->SetMaskLayer(
     96       maskLayer ? static_cast<WebLayerImpl*>(maskLayer)->layer() : 0);
     97 }
     98 
     99 void WebLayerImpl::setReplicaLayer(WebLayer* replica_layer) {
    100   layer_->SetReplicaLayer(
    101       replica_layer ? static_cast<WebLayerImpl*>(replica_layer)->layer() : 0);
    102 }
    103 
    104 void WebLayerImpl::setOpacity(float opacity) { layer_->SetOpacity(opacity); }
    105 
    106 float WebLayerImpl::opacity() const { return layer_->opacity(); }
    107 
    108 void WebLayerImpl::setOpaque(bool opaque) { layer_->SetContentsOpaque(opaque); }
    109 
    110 bool WebLayerImpl::opaque() const { return layer_->contents_opaque(); }
    111 
    112 void WebLayerImpl::setPosition(const WebFloatPoint& position) {
    113   layer_->SetPosition(position);
    114 }
    115 
    116 WebFloatPoint WebLayerImpl::position() const { return layer_->position(); }
    117 
    118 void WebLayerImpl::setSublayerTransform(const SkMatrix44& matrix) {
    119   gfx::Transform sub_layer_transform;
    120   sub_layer_transform.matrix() = matrix;
    121   layer_->SetSublayerTransform(sub_layer_transform);
    122 }
    123 
    124 SkMatrix44 WebLayerImpl::sublayerTransform() const {
    125   return layer_->sublayer_transform().matrix();
    126 }
    127 
    128 void WebLayerImpl::setTransform(const SkMatrix44& matrix) {
    129   gfx::Transform transform;
    130   transform.matrix() = matrix;
    131   layer_->SetTransform(transform);
    132 }
    133 
    134 SkMatrix44 WebLayerImpl::transform() const {
    135   return layer_->transform().matrix();
    136 }
    137 
    138 void WebLayerImpl::setDrawsContent(bool draws_content) {
    139   layer_->SetIsDrawable(draws_content);
    140 }
    141 
    142 bool WebLayerImpl::drawsContent() const { return layer_->DrawsContent(); }
    143 
    144 void WebLayerImpl::setPreserves3D(bool preserve3D) {
    145   layer_->SetPreserves3d(preserve3D);
    146 }
    147 
    148 void WebLayerImpl::setUseParentBackfaceVisibility(
    149     bool use_parent_backface_visibility) {
    150   layer_->set_use_parent_backface_visibility(use_parent_backface_visibility);
    151 }
    152 
    153 void WebLayerImpl::setBackgroundColor(WebColor color) {
    154   layer_->SetBackgroundColor(color);
    155 }
    156 
    157 WebColor WebLayerImpl::backgroundColor() const {
    158   return layer_->background_color();
    159 }
    160 
    161 void WebLayerImpl::setFilters(const WebFilterOperations& filters) {
    162   const WebFilterOperationsImpl& filters_impl =
    163       static_cast<const WebFilterOperationsImpl&>(filters);
    164   layer_->SetFilters(filters_impl.AsFilterOperations());
    165 }
    166 
    167 void WebLayerImpl::setBackgroundFilters(const WebFilterOperations& filters) {
    168   const WebFilterOperationsImpl& filters_impl =
    169       static_cast<const WebFilterOperationsImpl&>(filters);
    170   layer_->SetBackgroundFilters(filters_impl.AsFilterOperations());
    171 }
    172 
    173 void WebLayerImpl::setFilter(SkImageFilter* filter) {
    174   layer_->SetFilter(skia::SharePtr(filter));
    175 }
    176 
    177 void WebLayerImpl::setDebugName(WebKit::WebString name) {
    178   layer_->SetDebugName(UTF16ToASCII(name));
    179 }
    180 
    181 void WebLayerImpl::setCompositingReasons(
    182     WebKit::WebCompositingReasons reasons) {
    183   layer_->SetCompositingReasons(reasons);
    184 }
    185 
    186 void WebLayerImpl::setAnimationDelegate(
    187       WebKit::WebAnimationDelegate* delegate) {
    188   animation_delegate_adapter_.reset(
    189       new WebToCCAnimationDelegateAdapter(delegate));
    190   layer_->set_layer_animation_delegate(animation_delegate_adapter_.get());
    191 }
    192 
    193 bool WebLayerImpl::addAnimation(WebKit::WebAnimation* animation) {
    194   return layer_->AddAnimation(
    195       static_cast<WebAnimationImpl*>(animation)->CloneToAnimation());
    196 }
    197 
    198 void WebLayerImpl::removeAnimation(int animation_id) {
    199   layer_->RemoveAnimation(animation_id);
    200 }
    201 
    202 void WebLayerImpl::removeAnimation(
    203     int animation_id,
    204     WebKit::WebAnimation::TargetProperty target_property) {
    205   layer_->layer_animation_controller()->RemoveAnimation(
    206       animation_id,
    207       static_cast<Animation::TargetProperty>(target_property));
    208 }
    209 
    210 void WebLayerImpl::pauseAnimation(int animation_id, double time_offset) {
    211   layer_->PauseAnimation(animation_id, time_offset);
    212 }
    213 
    214 void WebLayerImpl::suspendAnimations(double monotonic_time) {
    215   layer_->SuspendAnimations(monotonic_time);
    216 }
    217 
    218 void WebLayerImpl::resumeAnimations(double monotonic_time) {
    219   layer_->ResumeAnimations(monotonic_time);
    220 }
    221 
    222 bool WebLayerImpl::hasActiveAnimation() { return layer_->HasActiveAnimation(); }
    223 
    224 void WebLayerImpl::setForceRenderSurface(bool force_render_surface) {
    225   layer_->SetForceRenderSurface(force_render_surface);
    226 }
    227 
    228 void WebLayerImpl::setScrollPosition(WebKit::WebPoint position) {
    229   layer_->SetScrollOffset(gfx::Point(position).OffsetFromOrigin());
    230 }
    231 
    232 WebKit::WebPoint WebLayerImpl::scrollPosition() const {
    233   return gfx::PointAtOffsetFromOrigin(layer_->scroll_offset());
    234 }
    235 
    236 void WebLayerImpl::setMaxScrollPosition(WebSize max_scroll_position) {
    237   layer_->SetMaxScrollOffset(max_scroll_position);
    238 }
    239 
    240 WebSize WebLayerImpl::maxScrollPosition() const {
    241   return layer_->max_scroll_offset();
    242 }
    243 
    244 void WebLayerImpl::setScrollable(bool scrollable) {
    245   layer_->SetScrollable(scrollable);
    246 }
    247 
    248 bool WebLayerImpl::scrollable() const { return layer_->scrollable(); }
    249 
    250 void WebLayerImpl::setHaveWheelEventHandlers(bool have_wheel_event_handlers) {
    251   layer_->SetHaveWheelEventHandlers(have_wheel_event_handlers);
    252 }
    253 
    254 bool WebLayerImpl::haveWheelEventHandlers() const {
    255   return layer_->have_wheel_event_handlers();
    256 }
    257 
    258 void WebLayerImpl::setShouldScrollOnMainThread(
    259     bool should_scroll_on_main_thread) {
    260   layer_->SetShouldScrollOnMainThread(should_scroll_on_main_thread);
    261 }
    262 
    263 bool WebLayerImpl::shouldScrollOnMainThread() const {
    264   return layer_->should_scroll_on_main_thread();
    265 }
    266 
    267 void WebLayerImpl::setNonFastScrollableRegion(const WebVector<WebRect>& rects) {
    268   cc::Region region;
    269   for (size_t i = 0; i < rects.size(); ++i)
    270     region.Union(rects[i]);
    271   layer_->SetNonFastScrollableRegion(region);
    272 }
    273 
    274 WebVector<WebRect> WebLayerImpl::nonFastScrollableRegion() const {
    275   size_t num_rects = 0;
    276   for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region());
    277        region_rects.has_rect();
    278        region_rects.next())
    279     ++num_rects;
    280 
    281   WebVector<WebRect> result(num_rects);
    282   size_t i = 0;
    283   for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region());
    284        region_rects.has_rect();
    285        region_rects.next()) {
    286     result[i] = region_rects.rect();
    287     ++i;
    288   }
    289   return result;
    290 }
    291 
    292 void WebLayerImpl::setTouchEventHandlerRegion(const WebVector<WebRect>& rects) {
    293   cc::Region region;
    294   for (size_t i = 0; i < rects.size(); ++i)
    295     region.Union(rects[i]);
    296   layer_->SetTouchEventHandlerRegion(region);
    297 }
    298 
    299 WebVector<WebRect> WebLayerImpl::touchEventHandlerRegion() const {
    300   size_t num_rects = 0;
    301   for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region());
    302        region_rects.has_rect();
    303        region_rects.next())
    304     ++num_rects;
    305 
    306   WebVector<WebRect> result(num_rects);
    307   size_t i = 0;
    308   for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region());
    309        region_rects.has_rect();
    310        region_rects.next()) {
    311     result[i] = region_rects.rect();
    312     ++i;
    313   }
    314   return result;
    315 }
    316 
    317 void WebLayerImpl::setIsContainerForFixedPositionLayers(bool enable) {
    318   layer_->SetIsContainerForFixedPositionLayers(enable);
    319 }
    320 
    321 bool WebLayerImpl::isContainerForFixedPositionLayers() const {
    322   return layer_->IsContainerForFixedPositionLayers();
    323 }
    324 
    325 static WebKit::WebLayerPositionConstraint ToWebLayerPositionConstraint(
    326     const cc::LayerPositionConstraint& constraint) {
    327   WebKit::WebLayerPositionConstraint web_constraint;
    328   web_constraint.isFixedPosition = constraint.is_fixed_position();
    329   web_constraint.isFixedToRightEdge = constraint.is_fixed_to_right_edge();
    330   web_constraint.isFixedToBottomEdge = constraint.is_fixed_to_bottom_edge();
    331   return web_constraint;
    332 }
    333 
    334 static cc::LayerPositionConstraint ToLayerPositionConstraint(
    335     const WebKit::WebLayerPositionConstraint& web_constraint) {
    336   cc::LayerPositionConstraint constraint;
    337   constraint.set_is_fixed_position(web_constraint.isFixedPosition);
    338   constraint.set_is_fixed_to_right_edge(web_constraint.isFixedToRightEdge);
    339   constraint.set_is_fixed_to_bottom_edge(web_constraint.isFixedToBottomEdge);
    340   return constraint;
    341 }
    342 
    343 void WebLayerImpl::setPositionConstraint(
    344     const WebKit::WebLayerPositionConstraint& constraint) {
    345   layer_->SetPositionConstraint(ToLayerPositionConstraint(constraint));
    346 }
    347 
    348 WebKit::WebLayerPositionConstraint WebLayerImpl::positionConstraint() const {
    349   return ToWebLayerPositionConstraint(layer_->position_constraint());
    350 }
    351 
    352 void WebLayerImpl::setScrollClient(
    353     WebKit::WebLayerScrollClient* scroll_client) {
    354   if (scroll_client) {
    355     layer_->set_did_scroll_callback(
    356         base::Bind(&WebKit::WebLayerScrollClient::didScroll,
    357                    base::Unretained(scroll_client)));
    358   } else {
    359     layer_->set_did_scroll_callback(base::Closure());
    360   }
    361 }
    362 
    363 bool WebLayerImpl::isOrphan() const { return !layer_->layer_tree_host(); }
    364 
    365 void WebLayerImpl::setWebLayerClient(WebKit::WebLayerClient* client) {
    366   web_layer_client_ = client;
    367 }
    368 
    369 Layer* WebLayerImpl::layer() const { return layer_.get(); }
    370 
    371 }  // namespace webkit
    372