layer.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2012 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#ifndef UI_COMPOSITOR_LAYER_H_ 6#define UI_COMPOSITOR_LAYER_H_ 7 8#include <string> 9#include <vector> 10 11#include "base/compiler_specific.h" 12#include "base/memory/ref_counted.h" 13#include "base/memory/scoped_ptr.h" 14#include "base/message_loop.h" 15#include "third_party/WebKit/Source/Platform/chromium/public/WebContentLayerClient.h" 16#include "third_party/WebKit/Source/Platform/chromium/public/WebExternalTextureLayerClient.h" 17#include "third_party/WebKit/Source/Platform/chromium/public/WebLayer.h" 18#include "third_party/WebKit/Source/Platform/chromium/public/WebContentLayer.h" 19#include "third_party/WebKit/Source/Platform/chromium/public/WebSolidColorLayer.h" 20#include "third_party/WebKit/Source/Platform/chromium/public/WebExternalTextureLayer.h" 21#include "third_party/skia/include/core/SkColor.h" 22#include "third_party/skia/include/core/SkRegion.h" 23#include "ui/compositor/compositor.h" 24#include "ui/compositor/layer_animation_delegate.h" 25#include "ui/compositor/layer_delegate.h" 26#include "ui/compositor/layer_type.h" 27#include "ui/gfx/rect.h" 28#include "ui/gfx/transform.h" 29 30class SkCanvas; 31 32namespace ui { 33 34class Compositor; 35class LayerAnimator; 36class Texture; 37 38// Layer manages a texture, transform and a set of child Layers. Any View that 39// has enabled layers ends up creating a Layer to manage the texture. 40// A Layer can also be created without a texture, in which case it renders 41// nothing and is simply used as a node in a hierarchy of layers. 42// Coordinate system used in layers is DIP (Density Independent Pixel) 43// coordinates unless explicitly mentioned as pixel coordinates. 44// 45// NOTE: unlike Views, each Layer does *not* own its children views. If you 46// delete a Layer and it has children, the parent of each child layer is set to 47// NULL, but the children are not deleted. 48class COMPOSITOR_EXPORT Layer 49 : public LayerAnimationDelegate, 50 NON_EXPORTED_BASE(public WebKit::WebContentLayerClient), 51 NON_EXPORTED_BASE(public WebKit::WebExternalTextureLayerClient) { 52 public: 53 Layer(); 54 explicit Layer(LayerType type); 55 virtual ~Layer(); 56 57 // Retrieves the Layer's compositor. The Layer will walk up its parent chain 58 // to locate it. Returns NULL if the Layer is not attached to a compositor. 59 Compositor* GetCompositor(); 60 61 // Called by the compositor when the Layer is set as its root Layer. This can 62 // only ever be called on the root layer. 63 void SetCompositor(Compositor* compositor); 64 65 LayerDelegate* delegate() { return delegate_; } 66 void set_delegate(LayerDelegate* delegate) { delegate_ = delegate; } 67 68 // Adds a new Layer to this Layer. 69 void Add(Layer* child); 70 71 // Removes a Layer from this Layer. 72 void Remove(Layer* child); 73 74 // Stacks |child| above all other children. 75 void StackAtTop(Layer* child); 76 77 // Stacks |child| directly above |other|. Both must be children of this 78 // layer. Note that if |child| is initially stacked even higher, calling this 79 // method will result in |child| being lowered in the stacking order. 80 void StackAbove(Layer* child, Layer* other); 81 82 // Stacks |child| below all other children. 83 void StackAtBottom(Layer* child); 84 85 // Stacks |child| directly below |other|. Both must be children of this 86 // layer. 87 void StackBelow(Layer* child, Layer* other); 88 89 // Returns the child Layers. 90 const std::vector<Layer*>& children() const { return children_; } 91 92 // The parent. 93 const Layer* parent() const { return parent_; } 94 Layer* parent() { return parent_; } 95 96 LayerType type() const { return type_; } 97 98 // Returns true if this Layer contains |other| somewhere in its children. 99 bool Contains(const Layer* other) const; 100 101 // The layer's animator is responsible for causing automatic animations when 102 // properties are set. It also manages a queue of pending animations and 103 // handles blending of animations. The layer takes ownership of the animator. 104 void SetAnimator(LayerAnimator* animator); 105 106 // Returns the layer's animator. Creates a default animator of one has not 107 // been set. Will not return NULL. 108 LayerAnimator* GetAnimator(); 109 110 // The transform, relative to the parent. 111 void SetTransform(const gfx::Transform& transform); 112 const gfx::Transform& transform() const { return transform_; } 113 114 // Return the target transform if animator is running, or the current 115 // transform otherwise. 116 gfx::Transform GetTargetTransform() const; 117 118 // The bounds, relative to the parent. 119 void SetBounds(const gfx::Rect& bounds); 120 const gfx::Rect& bounds() const { return bounds_; } 121 122 // Return the target bounds if animator is running, or the current bounds 123 // otherwise. 124 gfx::Rect GetTargetBounds() const; 125 126 // Sets/gets whether or not drawing of child layers should be clipped to the 127 // bounds of this layer. 128 void SetMasksToBounds(bool masks_to_bounds); 129 bool GetMasksToBounds() const; 130 131 // The opacity of the layer. The opacity is applied to each pixel of the 132 // texture (resulting alpha = opacity * alpha). 133 float opacity() const { return opacity_; } 134 void SetOpacity(float opacity); 135 136 // Returns the actual opacity, which the opacity of this layer multipled by 137 // the combined opacity of the parent. 138 float GetCombinedOpacity() const; 139 140 // Blur pixels by this amount in anything below the layer and visible through 141 // the layer. 142 int background_blur() const { return background_blur_radius_; } 143 void SetBackgroundBlur(int blur_radius); 144 145 // Saturate all pixels of this layer by this amount. 146 // This effect will get "combined" with the inverted, 147 // brightness and grayscale setting. 148 float layer_saturation() const { return layer_saturation_; } 149 void SetLayerSaturation(float saturation); 150 151 // Change the brightness of all pixels from this layer by this amount. 152 // This effect will get "combined" with the inverted, saturate 153 // and grayscale setting. 154 float layer_brightness() const { return layer_brightness_; } 155 void SetLayerBrightness(float brightness); 156 157 // Return the target brightness if animator is running, or the current 158 // brightness otherwise. 159 float GetTargetBrightness() const; 160 161 // Change the grayscale of all pixels from this layer by this amount. 162 // This effect will get "combined" with the inverted, saturate 163 // and brightness setting. 164 float layer_grayscale() const { return layer_grayscale_; } 165 void SetLayerGrayscale(float grayscale); 166 167 // Return the target grayscale if animator is running, or the current 168 // grayscale otherwise. 169 float GetTargetGrayscale() const; 170 171 // Invert the layer. 172 bool layer_inverted() const { return layer_inverted_; } 173 void SetLayerInverted(bool inverted); 174 175 // Return the target opacity if animator is running, or the current opacity 176 // otherwise. 177 float GetTargetOpacity() const; 178 179 // Set a layer mask for a layer. 180 // Note the provided layer mask can neither have a layer mask itself nor can 181 // it have any children. The ownership of |layer_mask| will not be 182 // transferred with this call. 183 // Furthermore: A mask layer can only be set to one layer. 184 void SetMaskLayer(Layer* layer_mask); 185 Layer* layer_mask_layer() { return layer_mask_; } 186 187 // Sets the visibility of the Layer. A Layer may be visible but not 188 // drawn. This happens if any ancestor of a Layer is not visible. 189 void SetVisible(bool visible); 190 bool visible() const { return visible_; } 191 192 // Returns the target visibility if the animator is running. Otherwise, it 193 // returns the current visibility. 194 bool GetTargetVisibility() const; 195 196 // Returns true if this Layer is drawn. A Layer is drawn only if all ancestors 197 // are visible. 198 bool IsDrawn() const; 199 200 // Returns true if this layer can have a texture (has_texture_ is true) 201 // and is not completely obscured by a child. 202 bool ShouldDraw() const; 203 204 // Converts a point from the coordinates of |source| to the coordinates of 205 // |target|. Necessarily, |source| and |target| must inhabit the same Layer 206 // tree. 207 static void ConvertPointToLayer(const Layer* source, 208 const Layer* target, 209 gfx::Point* point); 210 211 // See description in View for details 212 void SetFillsBoundsOpaquely(bool fills_bounds_opaquely); 213 bool fills_bounds_opaquely() const { return fills_bounds_opaquely_; } 214 215 const std::string& name() const { return name_; } 216 void set_name(const std::string& name) { name_ = name; } 217 218 const ui::Texture* texture() const { return texture_.get(); } 219 220 // Assigns a new external texture. |texture| can be NULL to disable external 221 // updates. 222 void SetExternalTexture(ui::Texture* texture); 223 ui::Texture* external_texture() { return texture_.get(); } 224 225 // Sets the layer's fill color. May only be called for LAYER_SOLID_COLOR. 226 void SetColor(SkColor color); 227 228 // Adds |invalid_rect| to the Layer's pending invalid rect and calls 229 // ScheduleDraw(). Returns false if the paint request is ignored. 230 bool SchedulePaint(const gfx::Rect& invalid_rect); 231 232 // Schedules a redraw of the layer tree at the compositor. 233 // Note that this _does not_ invalidate any region of this layer; use 234 // SchedulePaint() for that. 235 void ScheduleDraw(); 236 237 // Sends damaged rectangles recorded in |damaged_region_| to 238 // |compostior_| to repaint the content. 239 void SendDamagedRects(); 240 241 // Suppresses painting the content by disgarding damaged region and ignoring 242 // new paint requests. 243 void SuppressPaint(); 244 245 // Notifies the layer that the device scale factor has changed. 246 void OnDeviceScaleFactorChanged(float device_scale_factor); 247 248 // Sets whether the layer should scale its content. If true, the canvas will 249 // be scaled in software rendering mode before it is passed to 250 // |LayerDelegate::OnPaint|. 251 // Set to false if the delegate handles scaling. 252 // NOTE: if this is called during |LayerDelegate::OnPaint|, the new value will 253 // not apply to the canvas passed to the pending draw. 254 void set_scale_content(bool scale_content) { scale_content_ = scale_content; } 255 256 // Returns true if the layer scales its content. 257 bool scale_content() const { return scale_content_; } 258 259 // Sometimes the Layer is being updated by something other than SetCanvas 260 // (e.g. the GPU process on UI_COMPOSITOR_IMAGE_TRANSPORT). 261 bool layer_updated_externally() const { return layer_updated_externally_; } 262 263 // WebContentLayerClient 264 virtual void paintContents(WebKit::WebCanvas*, 265 const WebKit::WebRect& clip, 266#if WEBCONTENTLAYERCLIENT_HAS_CANPAINTLCDTEXT 267 bool can_paint_lcd_text, 268#endif // WEBCONTENTLAYERCLIENT_HAS_CANPAINTLCDTEXT 269 WebKit::WebFloatRect& opaque) OVERRIDE; 270 271 WebKit::WebLayer* web_layer() { return web_layer_; } 272 273 // WebExternalTextureLayerClient 274 virtual unsigned prepareTexture( 275 WebKit::WebTextureUpdater& /* updater */) OVERRIDE; 276 virtual WebKit::WebGraphicsContext3D* context() OVERRIDE; 277 278 float device_scale_factor() const { return device_scale_factor_; } 279 280 // Forces a render surface to be used on this layer. This has no positive 281 // impact, and is only used for benchmarking/testing purpose. 282 void SetForceRenderSurface(bool force); 283 bool force_render_surface() const { return force_render_surface_; } 284 285 private: 286 // Stacks |child| above or below |other|. Helper method for StackAbove() and 287 // StackBelow(). 288 void StackRelativeTo(Layer* child, Layer* other, bool above); 289 290 bool ConvertPointForAncestor(const Layer* ancestor, gfx::Point* point) const; 291 bool ConvertPointFromAncestor(const Layer* ancestor, gfx::Point* point) const; 292 293 bool GetTransformRelativeTo(const Layer* ancestor, 294 gfx::Transform* transform) const; 295 296 // The only externally updated layers are ones that get their pixels from 297 // WebKit and WebKit does not produce valid alpha values. All other layers 298 // should have valid alpha. 299 bool has_valid_alpha_channel() const { return !layer_updated_externally_; } 300 301 // Following are invoked from the animation or if no animation exists to 302 // update the values immediately. 303 void SetBoundsImmediately(const gfx::Rect& bounds); 304 void SetTransformImmediately(const gfx::Transform& transform); 305 void SetOpacityImmediately(float opacity); 306 void SetVisibilityImmediately(bool visibility); 307 void SetBrightnessImmediately(float brightness); 308 void SetGrayscaleImmediately(float grayscale); 309 void SetColorImmediately(SkColor color); 310 311 // Implementation of LayerAnimatorDelegate 312 virtual void SetBoundsFromAnimation(const gfx::Rect& bounds) OVERRIDE; 313 virtual void SetTransformFromAnimation( 314 const gfx::Transform& transform) OVERRIDE; 315 virtual void SetOpacityFromAnimation(float opacity) OVERRIDE; 316 virtual void SetVisibilityFromAnimation(bool visibility) OVERRIDE; 317 virtual void SetBrightnessFromAnimation(float brightness) OVERRIDE; 318 virtual void SetGrayscaleFromAnimation(float grayscale) OVERRIDE; 319 virtual void SetColorFromAnimation(SkColor color) OVERRIDE; 320 virtual void ScheduleDrawForAnimation() OVERRIDE; 321 virtual const gfx::Rect& GetBoundsForAnimation() const OVERRIDE; 322 virtual const gfx::Transform& GetTransformForAnimation() const OVERRIDE; 323 virtual float GetOpacityForAnimation() const OVERRIDE; 324 virtual bool GetVisibilityForAnimation() const OVERRIDE; 325 virtual float GetBrightnessForAnimation() const OVERRIDE; 326 virtual float GetGrayscaleForAnimation() const OVERRIDE; 327 virtual SkColor GetColorForAnimation() const OVERRIDE; 328 329 void CreateWebLayer(); 330 void RecomputeTransform(); 331 void RecomputeDrawsContentAndUVRect(); 332 void RecomputeDebugBorderColor(); 333 334 // Set all filters which got applied to the layer. 335 void SetLayerFilters(); 336 337 const LayerType type_; 338 339 Compositor* compositor_; 340 341 scoped_refptr<ui::Texture> texture_; 342 343 Layer* parent_; 344 345 // This layer's children, in bottom-to-top stacking order. 346 std::vector<Layer*> children_; 347 348 gfx::Transform transform_; 349 350 gfx::Rect bounds_; 351 352 // Visibility of this layer. See SetVisible/IsDrawn for more details. 353 bool visible_; 354 355 bool force_render_surface_; 356 357 bool fills_bounds_opaquely_; 358 359 // If true the layer is always up to date. 360 bool layer_updated_externally_; 361 362 // Union of damaged rects, in pixel coordinates, to be used when 363 // compositor is ready to paint the content. 364 SkRegion damaged_region_; 365 366 float opacity_; 367 int background_blur_radius_; 368 369 // Several variables which will change the visible representation of 370 // the layer. 371 float layer_saturation_; 372 float layer_brightness_; 373 float layer_grayscale_; 374 bool layer_inverted_; 375 376 // The associated mask layer with this layer. 377 Layer* layer_mask_; 378 // The back link from the mask layer to it's associated masked layer. 379 // We keep this reference for the case that if the mask layer gets deleted 380 // while attached to the main layer before the main layer is deleted. 381 Layer* layer_mask_back_link_; 382 383 std::string name_; 384 385 LayerDelegate* delegate_; 386 387 scoped_refptr<LayerAnimator> animator_; 388 389 // Ownership of the layer is held through one of the strongly typed layer 390 // pointers, depending on which sort of layer this is. 391 scoped_ptr<WebKit::WebContentLayer> content_layer_; 392 scoped_ptr<WebKit::WebExternalTextureLayer> texture_layer_; 393 scoped_ptr<WebKit::WebSolidColorLayer> solid_color_layer_; 394 WebKit::WebLayer* web_layer_; 395 bool web_layer_is_accelerated_; 396 bool show_debug_borders_; 397 398 // If true, the layer scales the canvas and the texture with the device scale 399 // factor as appropriate. When true, the texture size is in DIP. 400 bool scale_content_; 401 402 // A cached copy of |Compositor::device_scale_factor()|. 403 float device_scale_factor_; 404 405 DISALLOW_COPY_AND_ASSIGN(Layer); 406}; 407 408} // namespace ui 409 410#endif // UI_COMPOSITOR_LAYER_H_ 411