blob: 93396d0c5570033b134bbc4c1da9925cdd838759 [file] [log] [blame]
revemanb195f41d2015-11-19 22:16:481// Copyright 2015 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 COMPONENTS_EXO_SURFACE_H_
6#define COMPONENTS_EXO_SURFACE_H_
7
8#include <list>
reveman27fe2642015-11-20 06:33:399#include <utility>
revemanb195f41d2015-11-19 22:16:4810
11#include "base/callback.h"
12#include "base/macros.h"
13#include "base/memory/weak_ptr.h"
reveman27fe2642015-11-20 06:33:3914#include "base/observer_list.h"
revemanb195f41d2015-11-19 22:16:4815#include "third_party/skia/include/core/SkRegion.h"
reveman4c94cf962015-12-03 06:49:4316#include "ui/aura/window.h"
revemanb195f41d2015-11-19 22:16:4817#include "ui/compositor/compositor_observer.h"
18#include "ui/gfx/geometry/rect.h"
revemanb195f41d2015-11-19 22:16:4819
20namespace base {
21namespace trace_event {
22class TracedValue;
23}
24}
25
26namespace exo {
27class Buffer;
28class SurfaceDelegate;
reveman27fe2642015-11-20 06:33:3929class SurfaceObserver;
revemanb195f41d2015-11-19 22:16:4830
31// This class represents a rectangular area that is displayed on the screen.
32// It has a location, size and pixel contents.
reveman4c94cf962015-12-03 06:49:4333class Surface : public aura::Window, public ui::CompositorObserver {
revemanb195f41d2015-11-19 22:16:4834 public:
35 Surface();
36 ~Surface() override;
37
reveman39b32c872015-12-08 05:34:0538 // Type-checking downcast routine.
39 static Surface* AsSurface(aura::Window* window);
40
revemanb195f41d2015-11-19 22:16:4841 // Set a buffer as the content of this surface. A buffer can only be attached
42 // to one surface at a time.
43 void Attach(Buffer* buffer);
44
45 // Describe the regions where the pending buffer is different from the
46 // current surface contents, and where the surface therefore needs to be
47 // repainted.
48 void Damage(const gfx::Rect& rect);
49
50 // Request notification when the next frame is displayed. Useful for
51 // throttling redrawing operations, and driving animations.
52 using FrameCallback = base::Callback<void(base::TimeTicks frame_time)>;
53 void RequestFrameCallback(const FrameCallback& callback);
54
55 // This sets the region of the surface that contains opaque content.
56 void SetOpaqueRegion(const SkRegion& region);
57
reveman7efa4b02016-01-06 08:29:5458 // This sets the scaling factor used to interpret the contents of the buffer
59 // attached to the surface. Note that if the scale is larger than 1, then you
60 // have to attach a buffer that is larger (by a factor of scale in each
61 // dimension) than the desired surface size.
62 void SetBufferScale(float scale);
63
reveman27fe2642015-11-20 06:33:3964 // Functions that control sub-surface state. All sub-surface state is
65 // double-buffered and will be applied when Commit() is called.
66 void AddSubSurface(Surface* sub_surface);
67 void RemoveSubSurface(Surface* sub_surface);
68 void SetSubSurfacePosition(Surface* sub_surface, const gfx::Point& position);
69 void PlaceSubSurfaceAbove(Surface* sub_surface, Surface* reference);
70 void PlaceSubSurfaceBelow(Surface* sub_surface, Surface* sibling);
71
revemanb195f41d2015-11-19 22:16:4872 // Surface state (damage regions, attached buffers, etc.) is double-buffered.
73 // A Commit() call atomically applies all pending state, replacing the
reveman27fe2642015-11-20 06:33:3974 // current state. Commit() is not guaranteed to be synchronous. See
75 // CommitSurfaceHierarchy() below.
revemanb195f41d2015-11-19 22:16:4876 void Commit();
77
reveman27fe2642015-11-20 06:33:3978 // This will synchronously commit all pending state of the surface and its
79 // descendants by recursively calling CommitSurfaceHierarchy() for each
80 // sub-surface with pending state.
81 void CommitSurfaceHierarchy();
82
83 // Returns true if surface is in synchronized mode.
84 bool IsSynchronized() const;
85
reveman4c94cf962015-12-03 06:49:4386 // Returns the preferred size of surface.
87 gfx::Size GetPreferredSize() const;
88
revemanb195f41d2015-11-19 22:16:4889 // Set the surface delegate.
90 void SetSurfaceDelegate(SurfaceDelegate* delegate);
91
reveman27fe2642015-11-20 06:33:3992 // Returns true if surface has been assigned a surface delegate.
93 bool HasSurfaceDelegate() const;
94
95 // Surface does not own observers. It is the responsibility of the observer
96 // to remove itself when it is done observing.
97 void AddSurfaceObserver(SurfaceObserver* observer);
98 void RemoveSurfaceObserver(SurfaceObserver* observer);
99 bool HasSurfaceObserver(const SurfaceObserver* observer) const;
100
revemanb195f41d2015-11-19 22:16:48101 // Returns a trace value representing the state of the surface.
102 scoped_refptr<base::trace_event::TracedValue> AsTracedValue() const;
103
104 bool HasPendingDamageForTesting() const { return !pending_damage_.IsEmpty(); }
105
revemanb195f41d2015-11-19 22:16:48106 // Overridden from ui::CompositorObserver:
107 void OnCompositingDidCommit(ui::Compositor* compositor) override;
108 void OnCompositingStarted(ui::Compositor* compositor,
109 base::TimeTicks start_time) override;
revemanced21f862015-11-24 00:42:49110 void OnCompositingEnded(ui::Compositor* compositor) override;
111 void OnCompositingAborted(ui::Compositor* compositor) override;
revemanb195f41d2015-11-19 22:16:48112 void OnCompositingLockStateChanged(ui::Compositor* compositor) override {}
113 void OnCompositingShuttingDown(ui::Compositor* compositor) override;
114
115 private:
reveman27fe2642015-11-20 06:33:39116 bool needs_commit_surface_hierarchy() const {
117 return needs_commit_surface_hierarchy_;
118 }
119
revemanced21f862015-11-24 00:42:49120 // This returns true when the surface has some contents assigned to it.
121 bool has_contents() const { return !!current_buffer_; }
122
123 // This is true when Attach() has been called and new contents should take
124 // effect next time Commit() is called.
125 bool has_pending_contents_;
reveman27fe2642015-11-20 06:33:39126
revemanb195f41d2015-11-19 22:16:48127 // The buffer that will become the content of surface when Commit() is called.
128 base::WeakPtr<Buffer> pending_buffer_;
129
130 // The damage region to schedule paint for when Commit() is called.
131 // TODO(reveman): Use SkRegion here after adding a version of
132 // ui::Layer::SchedulePaint that takes a SkRegion.
133 gfx::Rect pending_damage_;
134
135 // These lists contains the callbacks to notify the client when it is a good
136 // time to start producing a new frame. These callbacks move to
137 // |frame_callbacks_| when Commit() is called. Later they are moved to
138 // |active_frame_callbacks_| when the effect of the Commit() is reflected in
139 // the compositor's active layer tree. The callbacks fire once we're notified
140 // that the compositor started drawing that active layer tree.
141 std::list<FrameCallback> pending_frame_callbacks_;
142 std::list<FrameCallback> frame_callbacks_;
143 std::list<FrameCallback> active_frame_callbacks_;
144
145 // The opaque region to take effect when Commit() is called.
146 SkRegion pending_opaque_region_;
147
reveman7efa4b02016-01-06 08:29:54148 // The buffer scaling factor to take effect when Commit() is called.
149 float pending_buffer_scale_;
150
reveman27fe2642015-11-20 06:33:39151 // The stack of sub-surfaces to take effect when Commit() is called.
152 // Bottom-most sub-surface at the front of the list and top-most sub-surface
153 // at the back.
154 using SubSurfaceEntry = std::pair<Surface*, gfx::Point>;
155 using SubSurfaceEntryList = std::list<SubSurfaceEntry>;
156 SubSurfaceEntryList pending_sub_surfaces_;
157
revemanced21f862015-11-24 00:42:49158 // The buffer that is currently set as content of surface.
159 base::WeakPtr<Buffer> current_buffer_;
160
reveman27fe2642015-11-20 06:33:39161 // This is true if a call to Commit() as been made but
162 // CommitSurfaceHierarchy() has not yet been called.
163 bool needs_commit_surface_hierarchy_;
164
reveman7cadea42016-02-05 20:14:38165 // This is set when the compositing starts and passed to active frame
166 // callbacks when compositing successfully ends.
167 base::TimeTicks last_compositing_start_time_;
168
revemanced21f862015-11-24 00:42:49169 // This is true when the contents of the surface should be updated next time
170 // the compositor successfully ends compositing.
171 bool update_contents_after_successful_compositing_;
reveman27fe2642015-11-20 06:33:39172
revemanb195f41d2015-11-19 22:16:48173 // The compsitor being observer or null if not observing a compositor.
174 ui::Compositor* compositor_;
175
176 // This can be set to have some functions delegated. E.g. ShellSurface class
177 // can set this to handle Commit() and apply any double buffered state it
178 // maintains.
179 SurfaceDelegate* delegate_;
180
reveman27fe2642015-11-20 06:33:39181 // Surface observer list. Surface does not own the observers.
182 base::ObserverList<SurfaceObserver, true> observers_;
183
revemanb195f41d2015-11-19 22:16:48184 DISALLOW_COPY_AND_ASSIGN(Surface);
185};
186
187} // namespace exo
188
189#endif // COMPONENTS_EXO_SURFACE_H_