blob: 91ca1c688b71b0dfd0c85266c16c268857cb7d08 [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>
dcheng31759da2016-04-21 01:26:319#include <memory>
reveman70baca12016-05-31 20:35:3010#include <set>
reveman27fe2642015-11-20 06:33:3911#include <utility>
revemanb195f41d2015-11-19 22:16:4812
13#include "base/callback.h"
14#include "base/macros.h"
jbaumanbd9586a92016-05-28 01:09:0315#include "base/memory/ref_counted.h"
revemanb195f41d2015-11-19 22:16:4816#include "base/memory/weak_ptr.h"
reveman27fe2642015-11-20 06:33:3917#include "base/observer_list.h"
jbaumanbd9586a92016-05-28 01:09:0318#include "cc/surfaces/surface_factory_client.h"
revemanb195f41d2015-11-19 22:16:4819#include "third_party/skia/include/core/SkRegion.h"
revemanfca687e2016-05-10 21:44:4820#include "third_party/skia/include/core/SkXfermode.h"
reveman4c94cf962015-12-03 06:49:4321#include "ui/aura/window.h"
reveman2966d7702016-02-12 02:09:5422#include "ui/aura/window_observer.h"
revemanb195f41d2015-11-19 22:16:4823#include "ui/compositor/compositor_observer.h"
24#include "ui/gfx/geometry/rect.h"
revemanb195f41d2015-11-19 22:16:4825
26namespace base {
27namespace trace_event {
28class TracedValue;
29}
30}
31
jbaumanbd9586a92016-05-28 01:09:0332namespace cc {
33class SurfaceFactory;
34enum class SurfaceDrawStatus;
35}
36
reveman2966d7702016-02-12 02:09:5437namespace gfx {
38class Path;
39}
40
revemanb195f41d2015-11-19 22:16:4841namespace exo {
42class Buffer;
reveman70baca12016-05-31 20:35:3043class Pointer;
revemanb195f41d2015-11-19 22:16:4844class SurfaceDelegate;
reveman27fe2642015-11-20 06:33:3945class SurfaceObserver;
jbaumanbd9586a92016-05-28 01:09:0346class Surface;
47
reveman70baca12016-05-31 20:35:3048// The pointer class is currently the only cursor provider class but this can
49// change in the future when better hardware cursor support is added.
50using CursorProvider = Pointer;
51
jbaumanbd9586a92016-05-28 01:09:0352// This class owns the SurfaceFactory and keeps track of references to the
53// contents of Buffers. It's keeped alive by references from
54// release_callbacks_. It's destroyed when its owning Surface is destroyed and
55// the last outstanding release callback is called.
56class SurfaceFactoryOwner : public base::RefCounted<SurfaceFactoryOwner>,
57 public cc::SurfaceFactoryClient {
58 public:
59 SurfaceFactoryOwner();
60
61 // Overridden from cc::SurfaceFactoryClient:
62 void ReturnResources(const cc::ReturnedResourceArray& resources) override;
63 void WillDrawSurface(cc::SurfaceId id, const gfx::Rect& damage_rect) override;
64 void SetBeginFrameSource(cc::BeginFrameSource* begin_frame_source) override;
65
66 private:
67 friend class base::RefCounted<SurfaceFactoryOwner>;
68 friend class Surface;
69 ~SurfaceFactoryOwner() override;
70
71 std::map<int,
72 std::pair<scoped_refptr<SurfaceFactoryOwner>,
73 std::unique_ptr<cc::SingleReleaseCallback>>>
74 release_callbacks_;
75 std::unique_ptr<cc::SurfaceIdAllocator> id_allocator_;
76 std::unique_ptr<cc::SurfaceFactory> surface_factory_;
77 Surface* surface_;
78};
revemanb195f41d2015-11-19 22:16:4879
80// This class represents a rectangular area that is displayed on the screen.
81// It has a location, size and pixel contents.
reveman2966d7702016-02-12 02:09:5482class Surface : public aura::Window,
83 public aura::WindowObserver,
84 public ui::CompositorObserver {
revemanb195f41d2015-11-19 22:16:4885 public:
86 Surface();
87 ~Surface() override;
88
reveman39b32c872015-12-08 05:34:0589 // Type-checking downcast routine.
kinabad14ca03e2016-02-23 04:43:3590 static Surface* AsSurface(const aura::Window* window);
reveman39b32c872015-12-08 05:34:0591
jbaumanbd9586a92016-05-28 01:09:0392 // Sets whether to put the contents in a SurfaceLayer or a TextureLayer.
93 static void SetUseSurfaceLayer(bool use_surface_layer);
94
revemanb195f41d2015-11-19 22:16:4895 // Set a buffer as the content of this surface. A buffer can only be attached
96 // to one surface at a time.
97 void Attach(Buffer* buffer);
98
99 // Describe the regions where the pending buffer is different from the
100 // current surface contents, and where the surface therefore needs to be
101 // repainted.
102 void Damage(const gfx::Rect& rect);
103
104 // Request notification when the next frame is displayed. Useful for
105 // throttling redrawing operations, and driving animations.
106 using FrameCallback = base::Callback<void(base::TimeTicks frame_time)>;
107 void RequestFrameCallback(const FrameCallback& callback);
108
109 // This sets the region of the surface that contains opaque content.
110 void SetOpaqueRegion(const SkRegion& region);
111
reveman2966d7702016-02-12 02:09:54112 // This sets the region of the surface that can receive pointer and touch
113 // events.
114 void SetInputRegion(const SkRegion& region);
115
reveman7efa4b02016-01-06 08:29:54116 // This sets the scaling factor used to interpret the contents of the buffer
117 // attached to the surface. Note that if the scale is larger than 1, then you
118 // have to attach a buffer that is larger (by a factor of scale in each
119 // dimension) than the desired surface size.
120 void SetBufferScale(float scale);
121
reveman27fe2642015-11-20 06:33:39122 // Functions that control sub-surface state. All sub-surface state is
123 // double-buffered and will be applied when Commit() is called.
124 void AddSubSurface(Surface* sub_surface);
125 void RemoveSubSurface(Surface* sub_surface);
126 void SetSubSurfacePosition(Surface* sub_surface, const gfx::Point& position);
127 void PlaceSubSurfaceAbove(Surface* sub_surface, Surface* reference);
128 void PlaceSubSurfaceBelow(Surface* sub_surface, Surface* sibling);
129
reveman642d8c332016-02-19 19:55:44130 // This sets the surface viewport for scaling.
131 void SetViewport(const gfx::Size& viewport);
132
reveman8e323902016-05-23 21:55:36133 // This sets the surface crop rectangle.
134 void SetCrop(const gfx::RectF& crop);
135
reveman85b7a562016-03-17 23:27:32136 // This sets the only visible on secure output flag, preventing it from
137 // appearing in screenshots or from being viewed on non-secure displays.
138 void SetOnlyVisibleOnSecureOutput(bool only_visible_on_secure_output);
139
revemanfca687e2016-05-10 21:44:48140 // This sets the blend mode that will be used when drawing the surface.
141 void SetBlendMode(SkXfermode::Mode blend_mode);
142
143 // This sets the alpha value that will be applied to the whole surface.
144 void SetAlpha(float alpha);
145
revemanb195f41d2015-11-19 22:16:48146 // Surface state (damage regions, attached buffers, etc.) is double-buffered.
147 // A Commit() call atomically applies all pending state, replacing the
reveman27fe2642015-11-20 06:33:39148 // current state. Commit() is not guaranteed to be synchronous. See
149 // CommitSurfaceHierarchy() below.
revemanb195f41d2015-11-19 22:16:48150 void Commit();
151
reveman27fe2642015-11-20 06:33:39152 // This will synchronously commit all pending state of the surface and its
153 // descendants by recursively calling CommitSurfaceHierarchy() for each
154 // sub-surface with pending state.
155 void CommitSurfaceHierarchy();
156
157 // Returns true if surface is in synchronized mode.
158 bool IsSynchronized() const;
159
revemanb9470762016-04-10 03:49:24160 // Returns the bounds of the current input region of surface.
161 gfx::Rect GetHitTestBounds() const;
reveman2966d7702016-02-12 02:09:54162
163 // Returns true if |rect| intersects this surface's bounds.
164 bool HitTestRect(const gfx::Rect& rect) const;
165
166 // Returns true if the current input region is different than the surface
167 // bounds.
168 bool HasHitTestMask() const;
169
170 // Returns the current input region of surface in the form of a hit-test mask.
171 void GetHitTestMask(gfx::Path* mask) const;
reveman4c94cf962015-12-03 06:49:43172
revemanaabfd712016-05-13 01:40:20173 // Returns the bounds of the surface area that is not know to be transparent.
174 gfx::Rect GetNonTransparentBounds() const;
175
reveman70baca12016-05-31 20:35:30176 // Surface does not own cursor providers. It is the responsibility of the
177 // caller to remove the cursor provider before it is destroyed.
178 void RegisterCursorProvider(CursorProvider* provider);
179 void UnregisterCursorProvider(CursorProvider* provider);
180
181 // Returns true if surface has at least one cursor provider registered.
182 bool HasCursorProvider() const;
183
revemanb195f41d2015-11-19 22:16:48184 // Set the surface delegate.
185 void SetSurfaceDelegate(SurfaceDelegate* delegate);
186
reveman27fe2642015-11-20 06:33:39187 // Returns true if surface has been assigned a surface delegate.
188 bool HasSurfaceDelegate() const;
189
190 // Surface does not own observers. It is the responsibility of the observer
191 // to remove itself when it is done observing.
192 void AddSurfaceObserver(SurfaceObserver* observer);
193 void RemoveSurfaceObserver(SurfaceObserver* observer);
194 bool HasSurfaceObserver(const SurfaceObserver* observer) const;
195
revemanb195f41d2015-11-19 22:16:48196 // Returns a trace value representing the state of the surface.
dcheng31759da2016-04-21 01:26:31197 std::unique_ptr<base::trace_event::TracedValue> AsTracedValue() const;
revemanb195f41d2015-11-19 22:16:48198
reveman5c353d52016-02-11 21:28:56199 bool HasPendingDamageForTesting(const gfx::Rect& damage) const {
200 return pending_damage_.contains(gfx::RectToSkIRect(damage));
201 }
revemanb195f41d2015-11-19 22:16:48202
reveman2966d7702016-02-12 02:09:54203 // Overridden from aura::WindowObserver:
204 void OnWindowAddedToRootWindow(aura::Window* window) override;
205 void OnWindowRemovingFromRootWindow(aura::Window* window,
206 aura::Window* new_root) override;
207
revemanb195f41d2015-11-19 22:16:48208 // Overridden from ui::CompositorObserver:
209 void OnCompositingDidCommit(ui::Compositor* compositor) override;
210 void OnCompositingStarted(ui::Compositor* compositor,
211 base::TimeTicks start_time) override;
revemanced21f82015-11-24 00:42:49212 void OnCompositingEnded(ui::Compositor* compositor) override;
213 void OnCompositingAborted(ui::Compositor* compositor) override;
revemanb195f41d2015-11-19 22:16:48214 void OnCompositingLockStateChanged(ui::Compositor* compositor) override {}
215 void OnCompositingShuttingDown(ui::Compositor* compositor) override;
216
jbaumanbd9586a92016-05-28 01:09:03217 void WillDraw(cc::SurfaceId surface_id);
218
revemanb195f41d2015-11-19 22:16:48219 private:
reveman27fe2642015-11-20 06:33:39220 bool needs_commit_surface_hierarchy() const {
221 return needs_commit_surface_hierarchy_;
222 }
223
jbaumanbd9586a92016-05-28 01:09:03224 // Commit the current attached buffer to a TextureLayer.
225 void CommitLayerContents();
226
227 // Commit the current attached buffer to a SurfaceLayer.
228 void CommitSurfaceContents();
229
revemanced21f82015-11-24 00:42:49230 // This returns true when the surface has some contents assigned to it.
231 bool has_contents() const { return !!current_buffer_; }
232
jbaumanbd9586a92016-05-28 01:09:03233 // This is true if the buffer contents should be put in a SurfaceLayer
234 // rather than a TextureLayer.
235 static bool use_surface_layer_;
236
revemanced21f82015-11-24 00:42:49237 // This is true when Attach() has been called and new contents should take
238 // effect next time Commit() is called.
239 bool has_pending_contents_;
reveman27fe2642015-11-20 06:33:39240
revemanb195f41d2015-11-19 22:16:48241 // The buffer that will become the content of surface when Commit() is called.
242 base::WeakPtr<Buffer> pending_buffer_;
243
jbaumanbd9586a92016-05-28 01:09:03244 cc::SurfaceManager* surface_manager_;
245
246 scoped_refptr<SurfaceFactoryOwner> factory_owner_;
247
248 // The Surface Id currently attached to the window.
249 cc::SurfaceId surface_id_;
250
251 // The next resource id the buffer will be attached to.
252 int next_resource_id_ = 0;
253
revemanb195f41d2015-11-19 22:16:48254 // The damage region to schedule paint for when Commit() is called.
reveman5c353d52016-02-11 21:28:56255 SkRegion pending_damage_;
revemanb195f41d2015-11-19 22:16:48256
257 // These lists contains the callbacks to notify the client when it is a good
258 // time to start producing a new frame. These callbacks move to
259 // |frame_callbacks_| when Commit() is called. Later they are moved to
260 // |active_frame_callbacks_| when the effect of the Commit() is reflected in
261 // the compositor's active layer tree. The callbacks fire once we're notified
262 // that the compositor started drawing that active layer tree.
263 std::list<FrameCallback> pending_frame_callbacks_;
264 std::list<FrameCallback> frame_callbacks_;
265 std::list<FrameCallback> active_frame_callbacks_;
266
267 // The opaque region to take effect when Commit() is called.
268 SkRegion pending_opaque_region_;
269
reveman2966d7702016-02-12 02:09:54270 // The input region to take effect when Commit() is called.
271 SkRegion pending_input_region_;
272
reveman7efa4b02016-01-06 08:29:54273 // The buffer scaling factor to take effect when Commit() is called.
274 float pending_buffer_scale_;
275
reveman27fe2642015-11-20 06:33:39276 // The stack of sub-surfaces to take effect when Commit() is called.
277 // Bottom-most sub-surface at the front of the list and top-most sub-surface
278 // at the back.
279 using SubSurfaceEntry = std::pair<Surface*, gfx::Point>;
280 using SubSurfaceEntryList = std::list<SubSurfaceEntry>;
281 SubSurfaceEntryList pending_sub_surfaces_;
282
reveman642d8c332016-02-19 19:55:44283 // The viewport to take effect when Commit() is called.
284 gfx::Size pending_viewport_;
285
reveman8e323902016-05-23 21:55:36286 // The crop rectangle to take effect when Commit() is called.
287 gfx::RectF pending_crop_;
288
reveman85b7a562016-03-17 23:27:32289 // The secure output visibility state to take effect when Commit() is called.
290 bool pending_only_visible_on_secure_output_;
291
revemanfca687e2016-05-10 21:44:48292 // The blend mode state to take effect when Commit() is called.
293 SkXfermode::Mode pending_blend_mode_;
294
295 // The alpha state to take effect when Commit() is called.
296 float pending_alpha_;
297
revemanaabfd712016-05-13 01:40:20298 // The active alpha state.
299 float alpha_;
300
revemanced21f82015-11-24 00:42:49301 // The buffer that is currently set as content of surface.
302 base::WeakPtr<Buffer> current_buffer_;
303
reveman2966d7702016-02-12 02:09:54304 // The active input region used for hit testing.
305 SkRegion input_region_;
306
reveman27fe2642015-11-20 06:33:39307 // This is true if a call to Commit() as been made but
308 // CommitSurfaceHierarchy() has not yet been called.
309 bool needs_commit_surface_hierarchy_;
310
reveman7cadea42016-02-05 20:14:38311 // This is set when the compositing starts and passed to active frame
312 // callbacks when compositing successfully ends.
313 base::TimeTicks last_compositing_start_time_;
314
revemanced21f82015-11-24 00:42:49315 // This is true when the contents of the surface should be updated next time
316 // the compositor successfully ends compositing.
317 bool update_contents_after_successful_compositing_;
reveman27fe2642015-11-20 06:33:39318
revemanb195f41d2015-11-19 22:16:48319 // The compsitor being observer or null if not observing a compositor.
320 ui::Compositor* compositor_;
321
reveman70baca12016-05-31 20:35:30322 // Cursor providers. Surface does not own the cursor providers.
323 std::set<CursorProvider*> cursor_providers_;
324
revemanb195f41d2015-11-19 22:16:48325 // This can be set to have some functions delegated. E.g. ShellSurface class
326 // can set this to handle Commit() and apply any double buffered state it
327 // maintains.
328 SurfaceDelegate* delegate_;
329
reveman27fe2642015-11-20 06:33:39330 // Surface observer list. Surface does not own the observers.
331 base::ObserverList<SurfaceObserver, true> observers_;
332
revemanb195f41d2015-11-19 22:16:48333 DISALLOW_COPY_AND_ASSIGN(Surface);
334};
335
336} // namespace exo
337
338#endif // COMPONENTS_EXO_SURFACE_H_