blob: 63eefabcfb108718a6a3026da7076827857c880f [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>
reveman27fe2642015-11-20 06:33:3910#include <utility>
revemanb195f41d2015-11-19 22:16:4811
12#include "base/callback.h"
13#include "base/macros.h"
jbaumanbd9586a92016-05-28 01:09:0314#include "base/memory/ref_counted.h"
revemanb195f41d2015-11-19 22:16:4815#include "base/memory/weak_ptr.h"
reveman27fe2642015-11-20 06:33:3916#include "base/observer_list.h"
jbaumanbd9586a92016-05-28 01:09:0317#include "cc/surfaces/surface_factory_client.h"
revemanb195f41d2015-11-19 22:16:4818#include "third_party/skia/include/core/SkRegion.h"
revemanfca687e2016-05-10 21:44:4819#include "third_party/skia/include/core/SkXfermode.h"
reveman4c94cf962015-12-03 06:49:4320#include "ui/aura/window.h"
reveman2966d7702016-02-12 02:09:5421#include "ui/aura/window_observer.h"
revemanb195f41d2015-11-19 22:16:4822#include "ui/compositor/compositor_observer.h"
23#include "ui/gfx/geometry/rect.h"
revemanb195f41d2015-11-19 22:16:4824
25namespace base {
26namespace trace_event {
27class TracedValue;
28}
29}
30
jbaumanbd9586a92016-05-28 01:09:0331namespace cc {
32class SurfaceFactory;
33enum class SurfaceDrawStatus;
34}
35
reveman2966d7702016-02-12 02:09:5436namespace gfx {
37class Path;
38}
39
revemanb195f41d2015-11-19 22:16:4840namespace exo {
41class Buffer;
42class SurfaceDelegate;
reveman27fe2642015-11-20 06:33:3943class SurfaceObserver;
jbaumanbd9586a92016-05-28 01:09:0344class Surface;
45
46// This class owns the SurfaceFactory and keeps track of references to the
47// contents of Buffers. It's keeped alive by references from
48// release_callbacks_. It's destroyed when its owning Surface is destroyed and
49// the last outstanding release callback is called.
50class SurfaceFactoryOwner : public base::RefCounted<SurfaceFactoryOwner>,
51 public cc::SurfaceFactoryClient {
52 public:
53 SurfaceFactoryOwner();
54
55 // Overridden from cc::SurfaceFactoryClient:
56 void ReturnResources(const cc::ReturnedResourceArray& resources) override;
57 void WillDrawSurface(cc::SurfaceId id, const gfx::Rect& damage_rect) override;
58 void SetBeginFrameSource(cc::BeginFrameSource* begin_frame_source) override;
59
60 private:
61 friend class base::RefCounted<SurfaceFactoryOwner>;
62 friend class Surface;
63 ~SurfaceFactoryOwner() override;
64
65 std::map<int,
66 std::pair<scoped_refptr<SurfaceFactoryOwner>,
67 std::unique_ptr<cc::SingleReleaseCallback>>>
68 release_callbacks_;
69 std::unique_ptr<cc::SurfaceIdAllocator> id_allocator_;
70 std::unique_ptr<cc::SurfaceFactory> surface_factory_;
71 Surface* surface_;
72};
revemanb195f41d2015-11-19 22:16:4873
74// This class represents a rectangular area that is displayed on the screen.
75// It has a location, size and pixel contents.
reveman2966d7702016-02-12 02:09:5476class Surface : public aura::Window,
77 public aura::WindowObserver,
78 public ui::CompositorObserver {
revemanb195f41d2015-11-19 22:16:4879 public:
80 Surface();
81 ~Surface() override;
82
reveman39b32c872015-12-08 05:34:0583 // Type-checking downcast routine.
kinabad14ca03e2016-02-23 04:43:3584 static Surface* AsSurface(const aura::Window* window);
reveman39b32c872015-12-08 05:34:0585
jbaumanbd9586a92016-05-28 01:09:0386 // Sets whether to put the contents in a SurfaceLayer or a TextureLayer.
87 static void SetUseSurfaceLayer(bool use_surface_layer);
88
revemanb195f41d2015-11-19 22:16:4889 // Set a buffer as the content of this surface. A buffer can only be attached
90 // to one surface at a time.
91 void Attach(Buffer* buffer);
92
93 // Describe the regions where the pending buffer is different from the
94 // current surface contents, and where the surface therefore needs to be
95 // repainted.
96 void Damage(const gfx::Rect& rect);
97
98 // Request notification when the next frame is displayed. Useful for
99 // throttling redrawing operations, and driving animations.
100 using FrameCallback = base::Callback<void(base::TimeTicks frame_time)>;
101 void RequestFrameCallback(const FrameCallback& callback);
102
103 // This sets the region of the surface that contains opaque content.
104 void SetOpaqueRegion(const SkRegion& region);
105
reveman2966d7702016-02-12 02:09:54106 // This sets the region of the surface that can receive pointer and touch
107 // events.
108 void SetInputRegion(const SkRegion& region);
109
reveman7efa4b02016-01-06 08:29:54110 // This sets the scaling factor used to interpret the contents of the buffer
111 // attached to the surface. Note that if the scale is larger than 1, then you
112 // have to attach a buffer that is larger (by a factor of scale in each
113 // dimension) than the desired surface size.
114 void SetBufferScale(float scale);
115
reveman27fe2642015-11-20 06:33:39116 // Functions that control sub-surface state. All sub-surface state is
117 // double-buffered and will be applied when Commit() is called.
118 void AddSubSurface(Surface* sub_surface);
119 void RemoveSubSurface(Surface* sub_surface);
120 void SetSubSurfacePosition(Surface* sub_surface, const gfx::Point& position);
121 void PlaceSubSurfaceAbove(Surface* sub_surface, Surface* reference);
122 void PlaceSubSurfaceBelow(Surface* sub_surface, Surface* sibling);
123
reveman642d8c332016-02-19 19:55:44124 // This sets the surface viewport for scaling.
125 void SetViewport(const gfx::Size& viewport);
126
reveman8e323902016-05-23 21:55:36127 // This sets the surface crop rectangle.
128 void SetCrop(const gfx::RectF& crop);
129
reveman85b7a562016-03-17 23:27:32130 // This sets the only visible on secure output flag, preventing it from
131 // appearing in screenshots or from being viewed on non-secure displays.
132 void SetOnlyVisibleOnSecureOutput(bool only_visible_on_secure_output);
133
revemanfca687e2016-05-10 21:44:48134 // This sets the blend mode that will be used when drawing the surface.
135 void SetBlendMode(SkXfermode::Mode blend_mode);
136
137 // This sets the alpha value that will be applied to the whole surface.
138 void SetAlpha(float alpha);
139
revemanb195f41d2015-11-19 22:16:48140 // Surface state (damage regions, attached buffers, etc.) is double-buffered.
141 // A Commit() call atomically applies all pending state, replacing the
reveman27fe2642015-11-20 06:33:39142 // current state. Commit() is not guaranteed to be synchronous. See
143 // CommitSurfaceHierarchy() below.
revemanb195f41d2015-11-19 22:16:48144 void Commit();
145
reveman27fe2642015-11-20 06:33:39146 // This will synchronously commit all pending state of the surface and its
147 // descendants by recursively calling CommitSurfaceHierarchy() for each
148 // sub-surface with pending state.
149 void CommitSurfaceHierarchy();
150
151 // Returns true if surface is in synchronized mode.
152 bool IsSynchronized() const;
153
revemanb9470762016-04-10 03:49:24154 // Returns the bounds of the current input region of surface.
155 gfx::Rect GetHitTestBounds() const;
reveman2966d7702016-02-12 02:09:54156
157 // Returns true if |rect| intersects this surface's bounds.
158 bool HitTestRect(const gfx::Rect& rect) const;
159
160 // Returns true if the current input region is different than the surface
161 // bounds.
162 bool HasHitTestMask() const;
163
164 // Returns the current input region of surface in the form of a hit-test mask.
165 void GetHitTestMask(gfx::Path* mask) const;
reveman4c94cf962015-12-03 06:49:43166
revemanaabfd712016-05-13 01:40:20167 // Returns the bounds of the surface area that is not know to be transparent.
168 gfx::Rect GetNonTransparentBounds() const;
169
revemanb195f41d2015-11-19 22:16:48170 // Set the surface delegate.
171 void SetSurfaceDelegate(SurfaceDelegate* delegate);
172
reveman27fe2642015-11-20 06:33:39173 // Returns true if surface has been assigned a surface delegate.
174 bool HasSurfaceDelegate() const;
175
176 // Surface does not own observers. It is the responsibility of the observer
177 // to remove itself when it is done observing.
178 void AddSurfaceObserver(SurfaceObserver* observer);
179 void RemoveSurfaceObserver(SurfaceObserver* observer);
180 bool HasSurfaceObserver(const SurfaceObserver* observer) const;
181
revemanb195f41d2015-11-19 22:16:48182 // Returns a trace value representing the state of the surface.
dcheng31759da2016-04-21 01:26:31183 std::unique_ptr<base::trace_event::TracedValue> AsTracedValue() const;
revemanb195f41d2015-11-19 22:16:48184
reveman5c353d52016-02-11 21:28:56185 bool HasPendingDamageForTesting(const gfx::Rect& damage) const {
186 return pending_damage_.contains(gfx::RectToSkIRect(damage));
187 }
revemanb195f41d2015-11-19 22:16:48188
reveman2966d7702016-02-12 02:09:54189 // Overridden from aura::WindowObserver:
190 void OnWindowAddedToRootWindow(aura::Window* window) override;
191 void OnWindowRemovingFromRootWindow(aura::Window* window,
192 aura::Window* new_root) override;
193
revemanb195f41d2015-11-19 22:16:48194 // Overridden from ui::CompositorObserver:
195 void OnCompositingDidCommit(ui::Compositor* compositor) override;
196 void OnCompositingStarted(ui::Compositor* compositor,
197 base::TimeTicks start_time) override;
revemanced21f862015-11-24 00:42:49198 void OnCompositingEnded(ui::Compositor* compositor) override;
199 void OnCompositingAborted(ui::Compositor* compositor) override;
revemanb195f41d2015-11-19 22:16:48200 void OnCompositingLockStateChanged(ui::Compositor* compositor) override {}
201 void OnCompositingShuttingDown(ui::Compositor* compositor) override;
202
jbaumanbd9586a92016-05-28 01:09:03203 void WillDraw(cc::SurfaceId surface_id);
204
revemanb195f41d2015-11-19 22:16:48205 private:
reveman27fe2642015-11-20 06:33:39206 bool needs_commit_surface_hierarchy() const {
207 return needs_commit_surface_hierarchy_;
208 }
209
jbaumanbd9586a92016-05-28 01:09:03210 // Commit the current attached buffer to a TextureLayer.
211 void CommitLayerContents();
212
213 // Commit the current attached buffer to a SurfaceLayer.
214 void CommitSurfaceContents();
215
revemanced21f862015-11-24 00:42:49216 // This returns true when the surface has some contents assigned to it.
217 bool has_contents() const { return !!current_buffer_; }
218
jbaumanbd9586a92016-05-28 01:09:03219 // This is true if the buffer contents should be put in a SurfaceLayer
220 // rather than a TextureLayer.
221 static bool use_surface_layer_;
222
revemanced21f862015-11-24 00:42:49223 // This is true when Attach() has been called and new contents should take
224 // effect next time Commit() is called.
225 bool has_pending_contents_;
reveman27fe2642015-11-20 06:33:39226
revemanb195f41d2015-11-19 22:16:48227 // The buffer that will become the content of surface when Commit() is called.
228 base::WeakPtr<Buffer> pending_buffer_;
229
jbaumanbd9586a92016-05-28 01:09:03230 cc::SurfaceManager* surface_manager_;
231
232 scoped_refptr<SurfaceFactoryOwner> factory_owner_;
233
234 // The Surface Id currently attached to the window.
235 cc::SurfaceId surface_id_;
236
237 // The next resource id the buffer will be attached to.
238 int next_resource_id_ = 0;
239
revemanb195f41d2015-11-19 22:16:48240 // The damage region to schedule paint for when Commit() is called.
reveman5c353d52016-02-11 21:28:56241 SkRegion pending_damage_;
revemanb195f41d2015-11-19 22:16:48242
243 // These lists contains the callbacks to notify the client when it is a good
244 // time to start producing a new frame. These callbacks move to
245 // |frame_callbacks_| when Commit() is called. Later they are moved to
246 // |active_frame_callbacks_| when the effect of the Commit() is reflected in
247 // the compositor's active layer tree. The callbacks fire once we're notified
248 // that the compositor started drawing that active layer tree.
249 std::list<FrameCallback> pending_frame_callbacks_;
250 std::list<FrameCallback> frame_callbacks_;
251 std::list<FrameCallback> active_frame_callbacks_;
252
253 // The opaque region to take effect when Commit() is called.
254 SkRegion pending_opaque_region_;
255
reveman2966d7702016-02-12 02:09:54256 // The input region to take effect when Commit() is called.
257 SkRegion pending_input_region_;
258
reveman7efa4b02016-01-06 08:29:54259 // The buffer scaling factor to take effect when Commit() is called.
260 float pending_buffer_scale_;
261
reveman27fe2642015-11-20 06:33:39262 // The stack of sub-surfaces to take effect when Commit() is called.
263 // Bottom-most sub-surface at the front of the list and top-most sub-surface
264 // at the back.
265 using SubSurfaceEntry = std::pair<Surface*, gfx::Point>;
266 using SubSurfaceEntryList = std::list<SubSurfaceEntry>;
267 SubSurfaceEntryList pending_sub_surfaces_;
268
reveman642d8c332016-02-19 19:55:44269 // The viewport to take effect when Commit() is called.
270 gfx::Size pending_viewport_;
271
reveman8e323902016-05-23 21:55:36272 // The crop rectangle to take effect when Commit() is called.
273 gfx::RectF pending_crop_;
274
reveman85b7a562016-03-17 23:27:32275 // The secure output visibility state to take effect when Commit() is called.
276 bool pending_only_visible_on_secure_output_;
277
revemanfca687e2016-05-10 21:44:48278 // The blend mode state to take effect when Commit() is called.
279 SkXfermode::Mode pending_blend_mode_;
280
281 // The alpha state to take effect when Commit() is called.
282 float pending_alpha_;
283
revemanaabfd712016-05-13 01:40:20284 // The active alpha state.
285 float alpha_;
286
revemanced21f862015-11-24 00:42:49287 // The buffer that is currently set as content of surface.
288 base::WeakPtr<Buffer> current_buffer_;
289
reveman2966d7702016-02-12 02:09:54290 // The active input region used for hit testing.
291 SkRegion input_region_;
292
reveman27fe2642015-11-20 06:33:39293 // This is true if a call to Commit() as been made but
294 // CommitSurfaceHierarchy() has not yet been called.
295 bool needs_commit_surface_hierarchy_;
296
reveman7cadea42016-02-05 20:14:38297 // This is set when the compositing starts and passed to active frame
298 // callbacks when compositing successfully ends.
299 base::TimeTicks last_compositing_start_time_;
300
revemanced21f862015-11-24 00:42:49301 // This is true when the contents of the surface should be updated next time
302 // the compositor successfully ends compositing.
303 bool update_contents_after_successful_compositing_;
reveman27fe2642015-11-20 06:33:39304
revemanb195f41d2015-11-19 22:16:48305 // The compsitor being observer or null if not observing a compositor.
306 ui::Compositor* compositor_;
307
308 // This can be set to have some functions delegated. E.g. ShellSurface class
309 // can set this to handle Commit() and apply any double buffered state it
310 // maintains.
311 SurfaceDelegate* delegate_;
312
reveman27fe2642015-11-20 06:33:39313 // Surface observer list. Surface does not own the observers.
314 base::ObserverList<SurfaceObserver, true> observers_;
315
revemanb195f41d2015-11-19 22:16:48316 DISALLOW_COPY_AND_ASSIGN(Surface);
317};
318
319} // namespace exo
320
321#endif // COMPONENTS_EXO_SURFACE_H_