Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: ui/compositor/layer.h

Issue 851853002: It is time. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Trying to reup because the last upload failed. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/compositor/float_animation_curve_adapter.cc ('k') | ui/compositor/layer.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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/message_loop.h"
15 #include "cc/animation/animation_events.h"
16 #include "cc/animation/layer_animation_event_observer.h"
17 #include "cc/base/scoped_ptr_vector.h"
18 #include "cc/layers/content_layer_client.h"
19 #include "cc/layers/layer_client.h"
20 #include "cc/layers/surface_layer.h"
21 #include "cc/layers/texture_layer_client.h"
22 #include "cc/resources/texture_mailbox.h"
23 #include "cc/surfaces/surface_id.h"
24 #include "third_party/skia/include/core/SkColor.h"
25 #include "third_party/skia/include/core/SkRegion.h"
26 #include "ui/compositor/compositor.h"
27 #include "ui/compositor/layer_animation_delegate.h"
28 #include "ui/compositor/layer_delegate.h"
29 #include "ui/compositor/layer_type.h"
30 #include "ui/gfx/rect.h"
31 #include "ui/gfx/transform.h"
32
33 class SkCanvas;
34
35 namespace cc {
36 class ContentLayer;
37 class CopyOutputRequest;
38 class DelegatedFrameProvider;
39 class DelegatedRendererLayer;
40 class Layer;
41 class NinePatchLayer;
42 class ResourceUpdateQueue;
43 class SolidColorLayer;
44 class SurfaceLayer;
45 class TextureLayer;
46 struct ReturnedResource;
47 typedef std::vector<ReturnedResource> ReturnedResourceArray;
48 }
49
50 namespace ui {
51
52 class Compositor;
53 class LayerAnimator;
54 class LayerOwner;
55
56 // Layer manages a texture, transform and a set of child Layers. Any View that
57 // has enabled layers ends up creating a Layer to manage the texture.
58 // A Layer can also be created without a texture, in which case it renders
59 // nothing and is simply used as a node in a hierarchy of layers.
60 // Coordinate system used in layers is DIP (Density Independent Pixel)
61 // coordinates unless explicitly mentioned as pixel coordinates.
62 //
63 // NOTE: Unlike Views, each Layer does *not* own its child Layers. If you
64 // delete a Layer and it has children, the parent of each child Layer is set to
65 // NULL, but the children are not deleted.
66 class COMPOSITOR_EXPORT Layer
67 : public LayerAnimationDelegate,
68 NON_EXPORTED_BASE(public cc::ContentLayerClient),
69 NON_EXPORTED_BASE(public cc::TextureLayerClient),
70 NON_EXPORTED_BASE(public cc::LayerClient),
71 NON_EXPORTED_BASE(public cc::LayerAnimationEventObserver) {
72 public:
73 Layer();
74 explicit Layer(LayerType type);
75 ~Layer() override;
76
77 static bool UsingPictureLayer();
78
79 // Retrieves the Layer's compositor. The Layer will walk up its parent chain
80 // to locate it. Returns NULL if the Layer is not attached to a compositor.
81 Compositor* GetCompositor();
82
83 // Called by the compositor when the Layer is set as its root Layer. This can
84 // only ever be called on the root layer.
85 void SetCompositor(Compositor* compositor);
86
87 LayerDelegate* delegate() { return delegate_; }
88 void set_delegate(LayerDelegate* delegate) { delegate_ = delegate; }
89
90 LayerOwner* owner() { return owner_; }
91
92 // Adds a new Layer to this Layer.
93 void Add(Layer* child);
94
95 // Removes a Layer from this Layer.
96 void Remove(Layer* child);
97
98 // Stacks |child| above all other children.
99 void StackAtTop(Layer* child);
100
101 // Stacks |child| directly above |other|. Both must be children of this
102 // layer. Note that if |child| is initially stacked even higher, calling this
103 // method will result in |child| being lowered in the stacking order.
104 void StackAbove(Layer* child, Layer* other);
105
106 // Stacks |child| below all other children.
107 void StackAtBottom(Layer* child);
108
109 // Stacks |child| directly below |other|. Both must be children of this
110 // layer.
111 void StackBelow(Layer* child, Layer* other);
112
113 // Returns the child Layers.
114 const std::vector<Layer*>& children() const { return children_; }
115
116 // The parent.
117 const Layer* parent() const { return parent_; }
118 Layer* parent() { return parent_; }
119
120 LayerType type() const { return type_; }
121
122 // Returns true if this Layer contains |other| somewhere in its children.
123 bool Contains(const Layer* other) const;
124
125 // The layer's animator is responsible for causing automatic animations when
126 // properties are set. It also manages a queue of pending animations and
127 // handles blending of animations. The layer takes ownership of the animator.
128 void SetAnimator(LayerAnimator* animator);
129
130 // Returns the layer's animator. Creates a default animator of one has not
131 // been set. Will not return NULL.
132 LayerAnimator* GetAnimator();
133
134 // The transform, relative to the parent.
135 void SetTransform(const gfx::Transform& transform);
136 gfx::Transform transform() const;
137
138 // Return the target transform if animator is running, or the current
139 // transform otherwise.
140 gfx::Transform GetTargetTransform() const;
141
142 // The bounds, relative to the parent.
143 void SetBounds(const gfx::Rect& bounds);
144 const gfx::Rect& bounds() const { return bounds_; }
145
146 // The offset from our parent (stored in bounds.origin()) is an integer but we
147 // may need to be at a fractional pixel offset to align properly on screen.
148 void SetSubpixelPositionOffset(const gfx::Vector2dF offset);
149 const gfx::Vector2dF& subpixel_position_offset() const {
150 return subpixel_position_offset_;
151 }
152
153 // Return the target bounds if animator is running, or the current bounds
154 // otherwise.
155 gfx::Rect GetTargetBounds() const;
156
157 // Sets/gets whether or not drawing of child layers should be clipped to the
158 // bounds of this layer.
159 void SetMasksToBounds(bool masks_to_bounds);
160 bool GetMasksToBounds() const;
161
162 // The opacity of the layer. The opacity is applied to each pixel of the
163 // texture (resulting alpha = opacity * alpha).
164 float opacity() const;
165 void SetOpacity(float opacity);
166
167 // Returns the actual opacity, which the opacity of this layer multipled by
168 // the combined opacity of the parent.
169 float GetCombinedOpacity() const;
170
171 // Blur pixels by this amount in anything below the layer and visible through
172 // the layer.
173 int background_blur() const { return background_blur_radius_; }
174 void SetBackgroundBlur(int blur_radius);
175
176 // Saturate all pixels of this layer by this amount.
177 // This effect will get "combined" with the inverted,
178 // brightness and grayscale setting.
179 float layer_saturation() const { return layer_saturation_; }
180 void SetLayerSaturation(float saturation);
181
182 // Change the brightness of all pixels from this layer by this amount.
183 // This effect will get "combined" with the inverted, saturate
184 // and grayscale setting.
185 float layer_brightness() const { return layer_brightness_; }
186 void SetLayerBrightness(float brightness);
187
188 // Return the target brightness if animator is running, or the current
189 // brightness otherwise.
190 float GetTargetBrightness() const;
191
192 // Change the grayscale of all pixels from this layer by this amount.
193 // This effect will get "combined" with the inverted, saturate
194 // and brightness setting.
195 float layer_grayscale() const { return layer_grayscale_; }
196 void SetLayerGrayscale(float grayscale);
197
198 // Return the target grayscale if animator is running, or the current
199 // grayscale otherwise.
200 float GetTargetGrayscale() const;
201
202 // Zoom the background by a factor of |zoom|. The effect is blended along the
203 // edge across |inset| pixels.
204 void SetBackgroundZoom(float zoom, int inset);
205
206 // Set the shape of this layer.
207 SkRegion* alpha_shape() const { return alpha_shape_.get(); }
208 void SetAlphaShape(scoped_ptr<SkRegion> region);
209
210 // Invert the layer.
211 bool layer_inverted() const { return layer_inverted_; }
212 void SetLayerInverted(bool inverted);
213
214 // Return the target opacity if animator is running, or the current opacity
215 // otherwise.
216 float GetTargetOpacity() const;
217
218 // Set a layer mask for a layer.
219 // Note the provided layer mask can neither have a layer mask itself nor can
220 // it have any children. The ownership of |layer_mask| will not be
221 // transferred with this call.
222 // Furthermore: A mask layer can only be set to one layer.
223 void SetMaskLayer(Layer* layer_mask);
224 Layer* layer_mask_layer() { return layer_mask_; }
225
226 // Sets the visibility of the Layer. A Layer may be visible but not
227 // drawn. This happens if any ancestor of a Layer is not visible.
228 void SetVisible(bool visible);
229 bool visible() const { return visible_; }
230
231 // Returns the target visibility if the animator is running. Otherwise, it
232 // returns the current visibility.
233 bool GetTargetVisibility() const;
234
235 // Returns true if this Layer is drawn. A Layer is drawn only if all ancestors
236 // are visible.
237 bool IsDrawn() const;
238
239 // Returns true if this layer can have a texture (has_texture_ is true)
240 // and is not completely obscured by a child.
241 bool ShouldDraw() const;
242
243 // Converts a point from the coordinates of |source| to the coordinates of
244 // |target|. Necessarily, |source| and |target| must inhabit the same Layer
245 // tree.
246 static void ConvertPointToLayer(const Layer* source,
247 const Layer* target,
248 gfx::Point* point);
249
250 // Converts a transform to be relative to the given |ancestor|. Returns
251 // whether success (that is, whether the given ancestor was really an
252 // ancestor of this layer).
253 bool GetTargetTransformRelativeTo(const Layer* ancestor,
254 gfx::Transform* transform) const;
255
256 // See description in View for details
257 void SetFillsBoundsOpaquely(bool fills_bounds_opaquely);
258 bool fills_bounds_opaquely() const { return fills_bounds_opaquely_; }
259
260 // Set to true if this layer always paints completely within its bounds. If so
261 // we can omit an unnecessary clear, even if the layer is transparent.
262 void SetFillsBoundsCompletely(bool fills_bounds_completely);
263
264 const std::string& name() const { return name_; }
265 void set_name(const std::string& name) { name_ = name; }
266
267 // Set new TextureMailbox for this layer. Note that |mailbox| may hold a
268 // shared memory resource or an actual mailbox for a texture.
269 void SetTextureMailbox(const cc::TextureMailbox& mailbox,
270 scoped_ptr<cc::SingleReleaseCallback> release_callback,
271 gfx::Size texture_size_in_dip);
272 void SetTextureSize(gfx::Size texture_size_in_dip);
273
274 // Begins showing delegated frames from the |frame_provider|.
275 void SetShowDelegatedContent(cc::DelegatedFrameProvider* frame_provider,
276 gfx::Size frame_size_in_dip);
277
278 // Begins showing content from a surface with a particular id.
279 void SetShowSurface(cc::SurfaceId id,
280 const cc::SurfaceLayer::SatisfyCallback& satisfy_callback,
281 const cc::SurfaceLayer::RequireCallback& require_callback,
282 gfx::Size frame_size_in_dip);
283
284 bool has_external_content() {
285 return texture_layer_.get() || delegated_renderer_layer_.get() ||
286 surface_layer_.get();
287 }
288
289 // Show a solid color instead of delegated or surface contents.
290 void SetShowSolidColorContent();
291
292 // Sets the layer's fill color. May only be called for LAYER_SOLID_COLOR.
293 void SetColor(SkColor color);
294
295 // Updates the nine patch layer's bitmap, aperture and border. May only be
296 // called for LAYER_NINE_PATCH.
297 void UpdateNinePatchLayerBitmap(const SkBitmap& bitmap);
298 void UpdateNinePatchLayerAperture(const gfx::Rect& aperture);
299 void UpdateNinePatchLayerBorder(const gfx::Rect& border);
300
301 // Adds |invalid_rect| to the Layer's pending invalid rect and calls
302 // ScheduleDraw(). Returns false if the paint request is ignored.
303 bool SchedulePaint(const gfx::Rect& invalid_rect);
304
305 // Schedules a redraw of the layer tree at the compositor.
306 // Note that this _does not_ invalidate any region of this layer; use
307 // SchedulePaint() for that.
308 void ScheduleDraw();
309
310 // Uses damaged rectangles recorded in |damaged_region_| to invalidate the
311 // |cc_layer_|.
312 void SendDamagedRects();
313
314 const SkRegion& damaged_region() const { return damaged_region_; }
315
316 void CompleteAllAnimations();
317
318 // Suppresses painting the content by disconnecting |delegate_|.
319 void SuppressPaint();
320
321 // Notifies the layer that the device scale factor has changed.
322 void OnDeviceScaleFactorChanged(float device_scale_factor);
323
324 // Notifies the layer that one of its children has received a new
325 // delegated frame.
326 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip);
327
328 // Requets a copy of the layer's output as a texture or bitmap.
329 void RequestCopyOfOutput(scoped_ptr<cc::CopyOutputRequest> request);
330
331 // ContentLayerClient
332 void PaintContents(
333 SkCanvas* canvas,
334 const gfx::Rect& clip,
335 ContentLayerClient::GraphicsContextStatus gc_status) override;
336 scoped_refptr<cc::DisplayItemList> PaintContentsToDisplayList(
337 const gfx::Rect& clip,
338 GraphicsContextStatus gc_status) override;
339 bool FillsBoundsCompletely() const override;
340
341 cc::Layer* cc_layer() { return cc_layer_; }
342
343 // TextureLayerClient
344 bool PrepareTextureMailbox(
345 cc::TextureMailbox* mailbox,
346 scoped_ptr<cc::SingleReleaseCallback>* release_callback,
347 bool use_shared_memory) override;
348
349 float device_scale_factor() const { return device_scale_factor_; }
350
351 // Forces a render surface to be used on this layer. This has no positive
352 // impact, and is only used for benchmarking/testing purpose.
353 void SetForceRenderSurface(bool force);
354 bool force_render_surface() const { return force_render_surface_; }
355
356 // LayerClient
357 scoped_refptr<base::debug::ConvertableToTraceFormat> TakeDebugInfo() override;
358
359 // LayerAnimationEventObserver
360 void OnAnimationStarted(const cc::AnimationEvent& event) override;
361
362 // Whether this layer has animations waiting to get sent to its cc::Layer.
363 bool HasPendingThreadedAnimations() {
364 return pending_threaded_animations_.size() != 0;
365 }
366
367 // Triggers a call to SwitchToLayer.
368 void SwitchCCLayerForTest();
369
370 private:
371 friend class LayerOwner;
372
373 void CollectAnimators(std::vector<scoped_refptr<LayerAnimator> >* animators);
374
375 // Stacks |child| above or below |other|. Helper method for StackAbove() and
376 // StackBelow().
377 void StackRelativeTo(Layer* child, Layer* other, bool above);
378
379 bool ConvertPointForAncestor(const Layer* ancestor, gfx::Point* point) const;
380 bool ConvertPointFromAncestor(const Layer* ancestor, gfx::Point* point) const;
381
382 // Implementation of LayerAnimatorDelegate
383 void SetBoundsFromAnimation(const gfx::Rect& bounds) override;
384 void SetTransformFromAnimation(const gfx::Transform& transform) override;
385 void SetOpacityFromAnimation(float opacity) override;
386 void SetVisibilityFromAnimation(bool visibility) override;
387 void SetBrightnessFromAnimation(float brightness) override;
388 void SetGrayscaleFromAnimation(float grayscale) override;
389 void SetColorFromAnimation(SkColor color) override;
390 void ScheduleDrawForAnimation() override;
391 const gfx::Rect& GetBoundsForAnimation() const override;
392 gfx::Transform GetTransformForAnimation() const override;
393 float GetOpacityForAnimation() const override;
394 bool GetVisibilityForAnimation() const override;
395 float GetBrightnessForAnimation() const override;
396 float GetGrayscaleForAnimation() const override;
397 SkColor GetColorForAnimation() const override;
398 float GetDeviceScaleFactor() const override;
399 void AddThreadedAnimation(scoped_ptr<cc::Animation> animation) override;
400 void RemoveThreadedAnimation(int animation_id) override;
401 LayerAnimatorCollection* GetLayerAnimatorCollection() override;
402
403 // Creates a corresponding composited layer for |type_|.
404 void CreateCcLayer();
405
406 // Recomputes and sets to |cc_layer_|.
407 void RecomputeDrawsContentAndUVRect();
408 void RecomputePosition();
409
410 // Set all filters which got applied to the layer.
411 void SetLayerFilters();
412
413 // Set all filters which got applied to the layer background.
414 void SetLayerBackgroundFilters();
415
416 // Cleanup |cc_layer_| and replaces it with |new_layer|.
417 void SwitchToLayer(scoped_refptr<cc::Layer> new_layer);
418
419 // We cannot send animations to our cc_layer_ until we have been added to a
420 // layer tree. Instead, we hold on to these animations in
421 // pending_threaded_animations_, and expect SendPendingThreadedAnimations to
422 // be called once we have been added to a tree.
423 void SendPendingThreadedAnimations();
424
425 void AddAnimatorsInTreeToCollection(LayerAnimatorCollection* collection);
426 void RemoveAnimatorsInTreeFromCollection(LayerAnimatorCollection* collection);
427
428 // Returns whether the layer has an animating LayerAnimator.
429 bool IsAnimating() const;
430
431 const LayerType type_;
432
433 Compositor* compositor_;
434
435 Layer* parent_;
436
437 // This layer's children, in bottom-to-top stacking order.
438 std::vector<Layer*> children_;
439
440 gfx::Rect bounds_;
441 gfx::Vector2dF subpixel_position_offset_;
442
443 // Visibility of this layer. See SetVisible/IsDrawn for more details.
444 bool visible_;
445
446 bool force_render_surface_;
447
448 bool fills_bounds_opaquely_;
449 bool fills_bounds_completely_;
450
451 // Union of damaged rects, in pixel coordinates, to be used when
452 // compositor is ready to paint the content.
453 SkRegion damaged_region_;
454
455 int background_blur_radius_;
456
457 // Several variables which will change the visible representation of
458 // the layer.
459 float layer_saturation_;
460 float layer_brightness_;
461 float layer_grayscale_;
462 bool layer_inverted_;
463
464 // The associated mask layer with this layer.
465 Layer* layer_mask_;
466 // The back link from the mask layer to it's associated masked layer.
467 // We keep this reference for the case that if the mask layer gets deleted
468 // while attached to the main layer before the main layer is deleted.
469 Layer* layer_mask_back_link_;
470
471 // The zoom factor to scale the layer by. Zooming is disabled when this is
472 // set to 1.
473 float zoom_;
474
475 // Width of the border in pixels, where the scaling is blended.
476 int zoom_inset_;
477
478 // Shape of the window.
479 scoped_ptr<SkRegion> alpha_shape_;
480
481 std::string name_;
482
483 LayerDelegate* delegate_;
484
485 LayerOwner* owner_;
486
487 scoped_refptr<LayerAnimator> animator_;
488
489 // Animations that are passed to AddThreadedAnimation before this layer is
490 // added to a tree.
491 cc::ScopedPtrVector<cc::Animation> pending_threaded_animations_;
492
493 // Ownership of the layer is held through one of the strongly typed layer
494 // pointers, depending on which sort of layer this is.
495 scoped_refptr<cc::Layer> content_layer_;
496 scoped_refptr<cc::NinePatchLayer> nine_patch_layer_;
497 scoped_refptr<cc::TextureLayer> texture_layer_;
498 scoped_refptr<cc::SolidColorLayer> solid_color_layer_;
499 scoped_refptr<cc::DelegatedRendererLayer> delegated_renderer_layer_;
500 scoped_refptr<cc::SurfaceLayer> surface_layer_;
501 cc::Layer* cc_layer_;
502
503 // A cached copy of |Compositor::device_scale_factor()|.
504 float device_scale_factor_;
505
506 // The mailbox used by texture_layer_.
507 cc::TextureMailbox mailbox_;
508
509 // The callback to release the mailbox. This is only set after
510 // SetTextureMailbox is called, before we give it to the TextureLayer.
511 scoped_ptr<cc::SingleReleaseCallback> mailbox_release_callback_;
512
513 // The size of the frame or texture in DIP, set when SetShowDelegatedContent
514 // or SetTextureMailbox was called.
515 gfx::Size frame_size_in_dip_;
516
517 DISALLOW_COPY_AND_ASSIGN(Layer);
518 };
519
520 } // namespace ui
521
522 #endif // UI_COMPOSITOR_LAYER_H_
OLDNEW
« no previous file with comments | « ui/compositor/float_animation_curve_adapter.cc ('k') | ui/compositor/layer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698