blob: f9fcbc9208246dbb3eb655747676951ea4f70368 [file] [log] [blame]
[email protected]036e6c972012-01-03 18:48:051// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]adc93fa72011-06-21 19:47:392// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]116302fc2012-05-05 21:45:415#ifndef UI_COMPOSITOR_LAYER_H_
6#define UI_COMPOSITOR_LAYER_H_
[email protected]adc93fa72011-06-21 19:47:397
[email protected]33699e622011-11-17 18:29:308#include <string>
[email protected]adc93fa72011-06-21 19:47:399#include <vector>
10
[email protected]7fca53d42011-09-29 15:38:1211#include "base/compiler_specific.h"
[email protected]51f1b482011-06-23 16:52:1212#include "base/memory/ref_counted.h"
[email protected]7fca53d42011-09-29 15:38:1213#include "base/memory/scoped_ptr.h"
[email protected]00b86982011-09-01 00:02:0914#include "base/message_loop.h"
[email protected]2a6c5672012-04-23 19:37:0915#include "third_party/WebKit/Source/Platform/chromium/public/WebContentLayerClient.h"
[email protected]7ba5f4b62012-08-25 01:19:3616#include "third_party/WebKit/Source/Platform/chromium/public/WebExternalTextureLayerClient.h"
[email protected]2a6c5672012-04-23 19:37:0917#include "third_party/WebKit/Source/Platform/chromium/public/WebLayer.h"
[email protected]66efabe2012-08-18 03:06:0618#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"
[email protected]116302fc2012-05-05 21:45:4121#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"
[email protected]adc93fa72011-06-21 19:47:3927#include "ui/gfx/rect.h"
28#include "ui/gfx/transform.h"
29
[email protected]892ad8a2011-07-27 02:47:2230class SkCanvas;
[email protected]adc93fa72011-06-21 19:47:3931
32namespace ui {
33
34class Compositor;
[email protected]fe7074c62011-10-28 15:22:5435class LayerAnimator;
[email protected]adc93fa72011-06-21 19:47:3936class 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.
[email protected]28cd2bb2011-09-19 21:04:1940// 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.
[email protected]cd9a61c72012-05-08 19:16:5942// Coordinate system used in layers is DIP (Density Independent Pixel)
43// coordinates unless explicitly mentioned as pixel coordinates.
[email protected]adc93fa72011-06-21 19:47:3944//
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.
[email protected]2700daddd2012-07-13 19:35:3748class COMPOSITOR_EXPORT Layer
49 : public LayerAnimationDelegate,
[email protected]7ba5f4b62012-08-25 01:19:3650 NON_EXPORTED_BASE(public WebKit::WebContentLayerClient),
51 NON_EXPORTED_BASE(public WebKit::WebExternalTextureLayerClient) {
[email protected]adc93fa72011-06-21 19:47:3952 public:
[email protected]7ab3f272011-11-16 00:51:5653 Layer();
54 explicit Layer(LayerType type);
[email protected]8f2a15d2011-09-29 15:50:5955 virtual ~Layer();
[email protected]adc93fa72011-06-21 19:47:3956
[email protected]993d6b322011-09-27 19:14:3857 // 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
[email protected]00b86982011-09-01 00:02:0965 LayerDelegate* delegate() { return delegate_; }
66 void set_delegate(LayerDelegate* delegate) { delegate_ = delegate; }
67
[email protected]adc93fa72011-06-21 19:47:3968 // 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
[email protected]5e4e61f2011-11-22 16:55:2474 // Stacks |child| above all other children.
75 void StackAtTop(Layer* child);
[email protected]18dab372011-10-03 21:21:4476
[email protected]ebc335f2011-11-23 00:43:5177 // 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.
[email protected]5e4e61f2011-11-22 16:55:2480 void StackAbove(Layer* child, Layer* other);
[email protected]62dd7ea2011-11-08 15:51:4781
[email protected]44c6f8d2011-12-27 23:49:0482 // 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
[email protected]adc93fa72011-06-21 19:47:3989 // Returns the child Layers.
[email protected]25ae9a12011-10-12 14:55:2290 const std::vector<Layer*>& children() const { return children_; }
[email protected]adc93fa72011-06-21 19:47:3991
92 // The parent.
93 const Layer* parent() const { return parent_; }
94 Layer* parent() { return parent_; }
95
[email protected]036e6c972012-01-03 18:48:0596 LayerType type() const { return type_; }
97
[email protected]ad7258912011-08-29 20:33:5398 // Returns true if this Layer contains |other| somewhere in its children.
99 bool Contains(const Layer* other) const;
100
[email protected]fe7074c62011-10-28 15:22:54101 // 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();
[email protected]7fca53d42011-09-29 15:38:12109
[email protected]adc93fa72011-06-21 19:47:39110 // The transform, relative to the parent.
[email protected]fe7074c62011-10-28 15:22:54111 void SetTransform(const Transform& transform);
112 const Transform& transform() const { return transform_; }
113
114 // Return the target transform if animator is running, or the current
115 // transform otherwise.
116 Transform GetTargetTransform() const;
[email protected]adc93fa72011-06-21 19:47:39117
118 // The bounds, relative to the parent.
[email protected]c155c252011-07-29 16:17:55119 void SetBounds(const gfx::Rect& bounds);
[email protected]adc93fa72011-06-21 19:47:39120 const gfx::Rect& bounds() const { return bounds_; }
121
[email protected]5ba9d5f2011-10-20 01:58:33122 // Return the target bounds if animator is running, or the current bounds
123 // otherwise.
124 gfx::Rect GetTargetBounds() const;
125
[email protected]7adee632012-03-15 19:15:08126 // 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
[email protected]7fca53d42011-09-29 15:38:12131 // 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
[email protected]fb4d9d12012-08-24 00:44:54136 // Returns the actual opacity, which the opacity of this layer multipled by
137 // the combined opacity of the parent.
138 float GetCombinedOpacity() const;
139
[email protected]2a6c5672012-04-23 19:37:09140 // 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
[email protected]e5da6622012-05-30 19:58:16145 // Saturate all pixels of this layer by this amount.
[email protected]815d0c382012-07-21 08:13:44146 // This effect will get "combined" with the inverted,
147 // brightness and grayscale setting.
[email protected]e5da6622012-05-30 19:58:16148 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.
[email protected]815d0c382012-07-21 08:13:44152 // This effect will get "combined" with the inverted, saturate
153 // and grayscale setting.
[email protected]e5da6622012-05-30 19:58:16154 float layer_brightness() const { return layer_brightness_; }
155 void SetLayerBrightness(float brightness);
156
[email protected]815d0c382012-07-21 08:13:44157 // 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
[email protected]f48075d2012-05-24 11:06:51171 // Invert the layer.
[email protected]e5da6622012-05-30 19:58:16172 bool layer_inverted() const { return layer_inverted_; }
173 void SetLayerInverted(bool inverted);
[email protected]f48075d2012-05-24 11:06:51174
[email protected]3f1f5e6a2011-11-11 15:09:02175 // Return the target opacity if animator is running, or the current opacity
[email protected]fe7074c62011-10-28 15:22:54176 // otherwise.
177 float GetTargetOpacity() const;
178
[email protected]498e623c2012-07-12 21:12:42179 // 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
[email protected]e4e8afef2011-10-05 13:58:33187 // 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.
[email protected]993d6b322011-09-27 19:14:38189 void SetVisible(bool visible);
[email protected]f941f47a2011-10-15 18:44:51190 bool visible() const { return visible_; }
[email protected]3aa43942011-09-13 20:59:53191
[email protected]a67935f2012-02-10 14:26:24192 // Returns the target visibility if the animator is running. Otherwise, it
193 // returns the current visibility.
194 bool GetTargetVisibility() const;
195
[email protected]e4e8afef2011-10-05 13:58:33196 // Returns true if this Layer is drawn. A Layer is drawn only if all ancestors
197 // are visible.
198 bool IsDrawn() const;
199
[email protected]c1f67302011-09-27 14:18:09200 // Returns true if this layer can have a texture (has_texture_ is true)
201 // and is not completely obscured by a child.
[email protected]332749032011-10-22 00:32:46202 bool ShouldDraw() const;
[email protected]c1f67302011-09-27 14:18:09203
[email protected]ad7258912011-08-29 20:33:53204 // 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
[email protected]c155c252011-07-29 16:17:55211 // See description in View for details
212 void SetFillsBoundsOpaquely(bool fills_bounds_opaquely);
213 bool fills_bounds_opaquely() const { return fills_bounds_opaquely_; }
214
[email protected]33699e622011-11-17 18:29:30215 const std::string& name() const { return name_; }
216 void set_name(const std::string& name) { name_ = name; }
217
[email protected]536987c82011-06-28 03:46:08218 const ui::Texture* texture() const { return texture_.get(); }
[email protected]1cbbee3c2011-06-24 12:32:19219
[email protected]6aa614a2012-01-19 22:13:38220 // Assigns a new external texture. |texture| can be NULL to disable external
221 // updates.
[email protected]28cd2bb2011-09-19 21:04:19222 void SetExternalTexture(ui::Texture* texture);
[email protected]b6ea1c12012-09-13 17:28:50223 ui::Texture* external_texture() { return texture_.get(); }
[email protected]28cd2bb2011-09-19 21:04:19224
[email protected]da7584c2012-01-28 03:19:12225 // Sets the layer's fill color. May only be called for LAYER_SOLID_COLOR.
226 void SetColor(SkColor color);
227
[email protected]870119a2011-09-30 18:13:22228 // Adds |invalid_rect| to the Layer's pending invalid rect and calls
[email protected]f78649ea2012-02-23 18:39:04229 // ScheduleDraw(). Returns false if the paint request is ignored.
230 bool SchedulePaint(const gfx::Rect& invalid_rect);
[email protected]00b86982011-09-01 00:02:09231
[email protected]870119a2011-09-30 18:13:22232 // Schedules a redraw of the layer tree at the compositor.
[email protected]35470cc2012-02-23 23:04:31233 // Note that this _does not_ invalidate any region of this layer; use
234 // SchedulePaint() for that.
[email protected]870119a2011-09-30 18:13:22235 void ScheduleDraw();
236
[email protected]cedc3952012-03-06 06:15:55237 // Sends damaged rectangles recorded in |damaged_region_| to
238 // |compostior_| to repaint the content.
239 void SendDamagedRects();
[email protected]f78649ea2012-02-23 18:39:04240
241 // Suppresses painting the content by disgarding damaged region and ignoring
242 // new paint requests.
243 void SuppressPaint();
244
[email protected]cd9a61c72012-05-08 19:16:59245 // Notifies the layer that the device scale factor has changed.
246 void OnDeviceScaleFactorChanged(float device_scale_factor);
247
[email protected]2486dce2012-05-23 17:18:19248 // 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| and the texture will be scaled in accelerated
251 // mode. Set to false if the delegate handles scaling and the texture is
252 // the correct pixel size.
253 void set_scale_content(bool scale_content) { scale_content_ = scale_content; }
[email protected]cd9a61c72012-05-08 19:16:59254
[email protected]84ff1e92012-06-13 02:58:21255 // Returns true if the layer scales its content.
256 bool scale_content() const { return scale_content_; }
257
[email protected]a97527b2011-09-14 15:44:38258 // Sometimes the Layer is being updated by something other than SetCanvas
[email protected]396ecd52011-11-22 00:08:52259 // (e.g. the GPU process on UI_COMPOSITOR_IMAGE_TRANSPORT).
[email protected]a97527b2011-09-14 15:44:38260 bool layer_updated_externally() const { return layer_updated_externally_; }
261
[email protected]332749032011-10-22 00:32:46262 // WebContentLayerClient
[email protected]fde5d7d22012-06-16 02:25:19263 virtual void paintContents(WebKit::WebCanvas*,
[email protected]4e41fda2012-06-19 21:31:35264 const WebKit::WebRect& clip,
[email protected]768f6e132012-06-29 13:16:23265 WebKit::WebFloatRect& opaque);
[email protected]332749032011-10-22 00:32:46266
[email protected]66efabe2012-08-18 03:06:06267 WebKit::WebLayer* web_layer() { return web_layer_; }
[email protected]332749032011-10-22 00:32:46268
[email protected]7ba5f4b62012-08-25 01:19:36269 // WebExternalTextureLayerClient
270 virtual unsigned prepareTexture(
271 WebKit::WebTextureUpdater& /* updater */) OVERRIDE;
272 virtual WebKit::WebGraphicsContext3D* context() OVERRIDE;
273
[email protected]cd9a61c72012-05-08 19:16:59274 float device_scale_factor() const { return device_scale_factor_; }
275
[email protected]09c01c12012-05-26 00:07:19276 // Forces a render surface to be used on this layer. This has no positive
277 // impact, and is only used for benchmarking/testing purpose.
278 void SetForceRenderSurface(bool force);
279 bool force_render_surface() const { return force_render_surface_; }
280
[email protected]adc93fa72011-06-21 19:47:39281 private:
[email protected]44c6f8d2011-12-27 23:49:04282 // Stacks |child| above or below |other|. Helper method for StackAbove() and
283 // StackBelow().
284 void StackRelativeTo(Layer* child, Layer* other, bool above);
285
[email protected]ad7258912011-08-29 20:33:53286 bool ConvertPointForAncestor(const Layer* ancestor, gfx::Point* point) const;
287 bool ConvertPointFromAncestor(const Layer* ancestor, gfx::Point* point) const;
288
289 bool GetTransformRelativeTo(const Layer* ancestor,
290 Transform* transform) const;
291
[email protected]b4bb9ca2011-09-23 20:53:14292 // The only externally updated layers are ones that get their pixels from
293 // WebKit and WebKit does not produce valid alpha values. All other layers
294 // should have valid alpha.
295 bool has_valid_alpha_channel() const { return !layer_updated_externally_; }
296
[email protected]7fca53d42011-09-29 15:38:12297 // Following are invoked from the animation or if no animation exists to
298 // update the values immediately.
299 void SetBoundsImmediately(const gfx::Rect& bounds);
300 void SetTransformImmediately(const ui::Transform& transform);
301 void SetOpacityImmediately(float opacity);
[email protected]a67935f2012-02-10 14:26:24302 void SetVisibilityImmediately(bool visibility);
[email protected]815d0c382012-07-21 08:13:44303 void SetBrightnessImmediately(float brightness);
304 void SetGrayscaleImmediately(float grayscale);
[email protected]7fca53d42011-09-29 15:38:12305
[email protected]fe7074c62011-10-28 15:22:54306 // Implementation of LayerAnimatorDelegate
307 virtual void SetBoundsFromAnimation(const gfx::Rect& bounds) OVERRIDE;
308 virtual void SetTransformFromAnimation(const Transform& transform) OVERRIDE;
309 virtual void SetOpacityFromAnimation(float opacity) OVERRIDE;
[email protected]a67935f2012-02-10 14:26:24310 virtual void SetVisibilityFromAnimation(bool visibility) OVERRIDE;
[email protected]815d0c382012-07-21 08:13:44311 virtual void SetBrightnessFromAnimation(float brightness) OVERRIDE;
312 virtual void SetGrayscaleFromAnimation(float grayscale) OVERRIDE;
[email protected]fe7074c62011-10-28 15:22:54313 virtual void ScheduleDrawForAnimation() OVERRIDE;
314 virtual const gfx::Rect& GetBoundsForAnimation() const OVERRIDE;
315 virtual const Transform& GetTransformForAnimation() const OVERRIDE;
316 virtual float GetOpacityForAnimation() const OVERRIDE;
[email protected]a67935f2012-02-10 14:26:24317 virtual bool GetVisibilityForAnimation() const OVERRIDE;
[email protected]815d0c382012-07-21 08:13:44318 virtual float GetBrightnessForAnimation() const OVERRIDE;
319 virtual float GetGrayscaleForAnimation() const OVERRIDE;
[email protected]7fca53d42011-09-29 15:38:12320
[email protected]332749032011-10-22 00:32:46321 void CreateWebLayer();
322 void RecomputeTransform();
[email protected]b9616d592011-11-14 20:04:42323 void RecomputeDrawsContentAndUVRect();
[email protected]a77ac8802011-11-26 01:59:56324 void RecomputeDebugBorderColor();
[email protected]332749032011-10-22 00:32:46325
[email protected]e5da6622012-05-30 19:58:16326 // Set all filters which got applied to the layer.
327 void SetLayerFilters();
328
[email protected]993d6b322011-09-27 19:14:38329 const LayerType type_;
330
[email protected]adc93fa72011-06-21 19:47:39331 Compositor* compositor_;
332
[email protected]51f1b482011-06-23 16:52:12333 scoped_refptr<ui::Texture> texture_;
[email protected]adc93fa72011-06-21 19:47:39334
335 Layer* parent_;
336
[email protected]ebc335f2011-11-23 00:43:51337 // This layer's children, in bottom-to-top stacking order.
[email protected]adc93fa72011-06-21 19:47:39338 std::vector<Layer*> children_;
339
340 ui::Transform transform_;
341
342 gfx::Rect bounds_;
343
[email protected]e4e8afef2011-10-05 13:58:33344 // Visibility of this layer. See SetVisible/IsDrawn for more details.
[email protected]3aa43942011-09-13 20:59:53345 bool visible_;
346
[email protected]09c01c12012-05-26 00:07:19347 bool force_render_surface_;
348
[email protected]c155c252011-07-29 16:17:55349 bool fills_bounds_opaquely_;
350
[email protected]a97527b2011-09-14 15:44:38351 // If true the layer is always up to date.
352 bool layer_updated_externally_;
353
[email protected]cd9a61c72012-05-08 19:16:59354 // Union of damaged rects, in pixel coordinates, to be used when
355 // compositor is ready to paint the content.
[email protected]cedc3952012-03-06 06:15:55356 SkRegion damaged_region_;
[email protected]f78649ea2012-02-23 18:39:04357
[email protected]b4bb9ca2011-09-23 20:53:14358 float opacity_;
[email protected]2a6c5672012-04-23 19:37:09359 int background_blur_radius_;
[email protected]e5da6622012-05-30 19:58:16360
361 // Several variables which will change the visible representation of
362 // the layer.
363 float layer_saturation_;
364 float layer_brightness_;
[email protected]815d0c382012-07-21 08:13:44365 float layer_grayscale_;
[email protected]e5da6622012-05-30 19:58:16366 bool layer_inverted_;
[email protected]b4bb9ca2011-09-23 20:53:14367
[email protected]498e623c2012-07-12 21:12:42368 // The associated mask layer with this layer.
369 Layer* layer_mask_;
370 // The back link from the mask layer to it's associated masked layer.
371 // We keep this reference for the case that if the mask layer gets deleted
372 // while attached to the main layer before the main layer is deleted.
373 Layer* layer_mask_back_link_;
374
[email protected]33699e622011-11-17 18:29:30375 std::string name_;
376
[email protected]00b86982011-09-01 00:02:09377 LayerDelegate* delegate_;
378
[email protected]5d86a112012-09-23 00:21:58379 scoped_refptr<LayerAnimator> animator_;
[email protected]7fca53d42011-09-29 15:38:12380
[email protected]66efabe2012-08-18 03:06:06381 // Ownership of the layer is held through one of the strongly typed layer
382 // pointers, depending on which sort of layer this is.
383 scoped_ptr<WebKit::WebContentLayer> content_layer_;
384 scoped_ptr<WebKit::WebExternalTextureLayer> texture_layer_;
385 scoped_ptr<WebKit::WebSolidColorLayer> solid_color_layer_;
386 WebKit::WebLayer* web_layer_;
[email protected]4b58d552011-10-26 00:54:46387 bool web_layer_is_accelerated_;
[email protected]a77ac8802011-11-26 01:59:56388 bool show_debug_borders_;
[email protected]332749032011-10-22 00:32:46389
[email protected]2486dce2012-05-23 17:18:19390 // If true, the layer scales the canvas and the texture with the device scale
391 // factor as appropriate. When true, the texture size is in DIP.
392 bool scale_content_;
[email protected]cd9a61c72012-05-08 19:16:59393
394 // A cached copy of |Compositor::device_scale_factor()|.
395 float device_scale_factor_;
396
[email protected]adc93fa72011-06-21 19:47:39397 DISALLOW_COPY_AND_ASSIGN(Layer);
398};
399
400} // namespace ui
401
[email protected]116302fc2012-05-05 21:45:41402#endif // UI_COMPOSITOR_LAYER_H_