blob: 8bb863aeee0e810e58f73740eb660f701bab06b5 [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"
jbauman2fdc0732016-06-07 00:55:3618#include "cc/resources/transferable_resource.h"
jbaumanbd9586a92016-05-28 01:09:0319#include "cc/surfaces/surface_factory_client.h"
revemanb195f41d2015-11-19 22:16:4820#include "third_party/skia/include/core/SkRegion.h"
revemanfca687e2016-05-10 21:44:4821#include "third_party/skia/include/core/SkXfermode.h"
reveman4c94cf962015-12-03 06:49:4322#include "ui/aura/window.h"
reveman2966d7702016-02-12 02:09:5423#include "ui/aura/window_observer.h"
revemanb195f41d2015-11-19 22:16:4824#include "ui/compositor/compositor_observer.h"
reveman56f345902016-06-06 03:58:2825#include "ui/compositor/layer_owner_delegate.h"
revemanb195f41d2015-11-19 22:16:4826#include "ui/gfx/geometry/rect.h"
revemanb195f41d2015-11-19 22:16:4827
28namespace base {
29namespace trace_event {
30class TracedValue;
31}
32}
33
jbaumanbd9586a92016-05-28 01:09:0334namespace cc {
35class SurfaceFactory;
36enum class SurfaceDrawStatus;
37}
38
reveman2966d7702016-02-12 02:09:5439namespace gfx {
40class Path;
41}
42
revemanb195f41d2015-11-19 22:16:4843namespace exo {
44class Buffer;
reveman70baca12016-05-31 20:35:3045class Pointer;
revemanb195f41d2015-11-19 22:16:4846class SurfaceDelegate;
reveman27fe2642015-11-20 06:33:3947class SurfaceObserver;
jbaumanbd9586a92016-05-28 01:09:0348class Surface;
49
reveman70baca12016-05-31 20:35:3050// The pointer class is currently the only cursor provider class but this can
51// change in the future when better hardware cursor support is added.
52using CursorProvider = Pointer;
53
jbaumanbd9586a92016-05-28 01:09:0354// This class owns the SurfaceFactory and keeps track of references to the
55// contents of Buffers. It's keeped alive by references from
56// release_callbacks_. It's destroyed when its owning Surface is destroyed and
57// the last outstanding release callback is called.
58class SurfaceFactoryOwner : public base::RefCounted<SurfaceFactoryOwner>,
59 public cc::SurfaceFactoryClient {
60 public:
61 SurfaceFactoryOwner();
62
63 // Overridden from cc::SurfaceFactoryClient:
64 void ReturnResources(const cc::ReturnedResourceArray& resources) override;
65 void WillDrawSurface(cc::SurfaceId id, const gfx::Rect& damage_rect) override;
66 void SetBeginFrameSource(cc::BeginFrameSource* begin_frame_source) override;
67
68 private:
69 friend class base::RefCounted<SurfaceFactoryOwner>;
70 friend class Surface;
71 ~SurfaceFactoryOwner() override;
72
73 std::map<int,
74 std::pair<scoped_refptr<SurfaceFactoryOwner>,
75 std::unique_ptr<cc::SingleReleaseCallback>>>
76 release_callbacks_;
77 std::unique_ptr<cc::SurfaceIdAllocator> id_allocator_;
78 std::unique_ptr<cc::SurfaceFactory> surface_factory_;
79 Surface* surface_;
80};
revemanb195f41d2015-11-19 22:16:4881
82// This class represents a rectangular area that is displayed on the screen.
83// It has a location, size and pixel contents.
jbaumane3526252016-06-09 18:43:0584class Surface : public aura::WindowObserver,
reveman56f345902016-06-06 03:58:2885 public ui::LayerOwnerDelegate,
reveman2966d7702016-02-12 02:09:5486 public ui::CompositorObserver {
revemanb195f41d2015-11-19 22:16:4887 public:
88 Surface();
89 ~Surface() override;
90
reveman39b32c872015-12-08 05:34:0591 // Type-checking downcast routine.
kinabad14ca03e2016-02-23 04:43:3592 static Surface* AsSurface(const aura::Window* window);
reveman39b32c872015-12-08 05:34:0593
jbaumanbd9586a92016-05-28 01:09:0394 // Sets whether to put the contents in a SurfaceLayer or a TextureLayer.
95 static void SetUseSurfaceLayer(bool use_surface_layer);
96
jbaumane3526252016-06-09 18:43:0597 aura::Window* window() { return window_.get(); }
98
revemanb195f41d2015-11-19 22:16:4899 // Set a buffer as the content of this surface. A buffer can only be attached
100 // to one surface at a time.
101 void Attach(Buffer* buffer);
102
103 // Describe the regions where the pending buffer is different from the
104 // current surface contents, and where the surface therefore needs to be
105 // repainted.
106 void Damage(const gfx::Rect& rect);
107
108 // Request notification when the next frame is displayed. Useful for
109 // throttling redrawing operations, and driving animations.
110 using FrameCallback = base::Callback<void(base::TimeTicks frame_time)>;
111 void RequestFrameCallback(const FrameCallback& callback);
112
113 // This sets the region of the surface that contains opaque content.
114 void SetOpaqueRegion(const SkRegion& region);
115
reveman2966d7702016-02-12 02:09:54116 // This sets the region of the surface that can receive pointer and touch
117 // events.
118 void SetInputRegion(const SkRegion& region);
119
reveman7efa4b02016-01-06 08:29:54120 // This sets the scaling factor used to interpret the contents of the buffer
121 // attached to the surface. Note that if the scale is larger than 1, then you
122 // have to attach a buffer that is larger (by a factor of scale in each
123 // dimension) than the desired surface size.
124 void SetBufferScale(float scale);
125
reveman27fe2642015-11-20 06:33:39126 // Functions that control sub-surface state. All sub-surface state is
127 // double-buffered and will be applied when Commit() is called.
128 void AddSubSurface(Surface* sub_surface);
129 void RemoveSubSurface(Surface* sub_surface);
130 void SetSubSurfacePosition(Surface* sub_surface, const gfx::Point& position);
131 void PlaceSubSurfaceAbove(Surface* sub_surface, Surface* reference);
132 void PlaceSubSurfaceBelow(Surface* sub_surface, Surface* sibling);
133
reveman642d8c332016-02-19 19:55:44134 // This sets the surface viewport for scaling.
135 void SetViewport(const gfx::Size& viewport);
136
reveman8e323902016-05-23 21:55:36137 // This sets the surface crop rectangle.
138 void SetCrop(const gfx::RectF& crop);
139
reveman85b7a562016-03-17 23:27:32140 // This sets the only visible on secure output flag, preventing it from
141 // appearing in screenshots or from being viewed on non-secure displays.
142 void SetOnlyVisibleOnSecureOutput(bool only_visible_on_secure_output);
143
revemanfca687e2016-05-10 21:44:48144 // This sets the blend mode that will be used when drawing the surface.
145 void SetBlendMode(SkXfermode::Mode blend_mode);
146
147 // This sets the alpha value that will be applied to the whole surface.
148 void SetAlpha(float alpha);
149
revemanb195f41d2015-11-19 22:16:48150 // Surface state (damage regions, attached buffers, etc.) is double-buffered.
151 // A Commit() call atomically applies all pending state, replacing the
reveman27fe2642015-11-20 06:33:39152 // current state. Commit() is not guaranteed to be synchronous. See
153 // CommitSurfaceHierarchy() below.
revemanb195f41d2015-11-19 22:16:48154 void Commit();
155
reveman27fe2642015-11-20 06:33:39156 // This will synchronously commit all pending state of the surface and its
157 // descendants by recursively calling CommitSurfaceHierarchy() for each
158 // sub-surface with pending state.
159 void CommitSurfaceHierarchy();
160
161 // Returns true if surface is in synchronized mode.
162 bool IsSynchronized() const;
163
revemanb9470762016-04-10 03:49:24164 // Returns the bounds of the current input region of surface.
165 gfx::Rect GetHitTestBounds() const;
reveman2966d7702016-02-12 02:09:54166
167 // Returns true if |rect| intersects this surface's bounds.
168 bool HitTestRect(const gfx::Rect& rect) const;
169
170 // Returns true if the current input region is different than the surface
171 // bounds.
172 bool HasHitTestMask() const;
173
174 // Returns the current input region of surface in the form of a hit-test mask.
175 void GetHitTestMask(gfx::Path* mask) const;
reveman4c94cf962015-12-03 06:49:43176
reveman70baca12016-05-31 20:35:30177 // Surface does not own cursor providers. It is the responsibility of the
178 // caller to remove the cursor provider before it is destroyed.
179 void RegisterCursorProvider(CursorProvider* provider);
180 void UnregisterCursorProvider(CursorProvider* provider);
181
182 // Returns true if surface has at least one cursor provider registered.
183 bool HasCursorProvider() const;
184
revemanb195f41d2015-11-19 22:16:48185 // Set the surface delegate.
186 void SetSurfaceDelegate(SurfaceDelegate* delegate);
187
reveman27fe2642015-11-20 06:33:39188 // Returns true if surface has been assigned a surface delegate.
189 bool HasSurfaceDelegate() const;
190
191 // Surface does not own observers. It is the responsibility of the observer
192 // to remove itself when it is done observing.
193 void AddSurfaceObserver(SurfaceObserver* observer);
194 void RemoveSurfaceObserver(SurfaceObserver* observer);
195 bool HasSurfaceObserver(const SurfaceObserver* observer) const;
196
revemanb195f41d2015-11-19 22:16:48197 // Returns a trace value representing the state of the surface.
dcheng31759da2016-04-21 01:26:31198 std::unique_ptr<base::trace_event::TracedValue> AsTracedValue() const;
revemanb195f41d2015-11-19 22:16:48199
reveman5c353d52016-02-11 21:28:56200 bool HasPendingDamageForTesting(const gfx::Rect& damage) const {
201 return pending_damage_.contains(gfx::RectToSkIRect(damage));
202 }
revemanb195f41d2015-11-19 22:16:48203
reveman2966d7702016-02-12 02:09:54204 // Overridden from aura::WindowObserver:
205 void OnWindowAddedToRootWindow(aura::Window* window) override;
206 void OnWindowRemovingFromRootWindow(aura::Window* window,
207 aura::Window* new_root) override;
208
reveman56f345902016-06-06 03:58:28209 // Overridden from ui::LayerOwnerDelegate:
210 void OnLayerRecreated(ui::Layer* old_layer, ui::Layer* new_layer) override;
211
revemanb195f41d2015-11-19 22:16:48212 // Overridden from ui::CompositorObserver:
213 void OnCompositingDidCommit(ui::Compositor* compositor) override;
214 void OnCompositingStarted(ui::Compositor* compositor,
215 base::TimeTicks start_time) override;
revemanced21f862015-11-24 00:42:49216 void OnCompositingEnded(ui::Compositor* compositor) override;
217 void OnCompositingAborted(ui::Compositor* compositor) override;
revemanb195f41d2015-11-19 22:16:48218 void OnCompositingLockStateChanged(ui::Compositor* compositor) override {}
219 void OnCompositingShuttingDown(ui::Compositor* compositor) override;
220
jbaumanbd9586a92016-05-28 01:09:03221 void WillDraw(cc::SurfaceId surface_id);
222
revemanb195f41d2015-11-19 22:16:48223 private:
reveman27fe2642015-11-20 06:33:39224 bool needs_commit_surface_hierarchy() const {
225 return needs_commit_surface_hierarchy_;
226 }
227
jbaumanbd9586a92016-05-28 01:09:03228 // Commit the current attached buffer to a TextureLayer.
reveman56f345902016-06-06 03:58:28229 void CommitTextureContents();
jbaumanbd9586a92016-05-28 01:09:03230
231 // Commit the current attached buffer to a SurfaceLayer.
232 void CommitSurfaceContents();
233
reveman56f345902016-06-06 03:58:28234 // Set TextureLayer contents to the current buffer.
235 void SetTextureLayerContents(ui::Layer* layer);
236
237 // Set SurfaceLayer contents to the current buffer.
238 void SetSurfaceLayerContents(ui::Layer* layer);
239
revemanced21f862015-11-24 00:42:49240 // This returns true when the surface has some contents assigned to it.
241 bool has_contents() const { return !!current_buffer_; }
242
jbaumanbd9586a92016-05-28 01:09:03243 // This is true if the buffer contents should be put in a SurfaceLayer
244 // rather than a TextureLayer.
245 static bool use_surface_layer_;
246
jbaumane3526252016-06-09 18:43:05247 // This window has the layer which contains the Surface contents.
248 std::unique_ptr<aura::Window> window_;
249
revemanced21f862015-11-24 00:42:49250 // This is true when Attach() has been called and new contents should take
251 // effect next time Commit() is called.
252 bool has_pending_contents_;
reveman27fe2642015-11-20 06:33:39253
revemanb195f41d2015-11-19 22:16:48254 // The buffer that will become the content of surface when Commit() is called.
255 base::WeakPtr<Buffer> pending_buffer_;
256
jbaumanbd9586a92016-05-28 01:09:03257 cc::SurfaceManager* surface_manager_;
258
259 scoped_refptr<SurfaceFactoryOwner> factory_owner_;
260
261 // The Surface Id currently attached to the window.
262 cc::SurfaceId surface_id_;
263
264 // The next resource id the buffer will be attached to.
jbauman2fdc0732016-06-07 00:55:36265 int next_resource_id_ = 1;
jbaumanbd9586a92016-05-28 01:09:03266
revemanb195f41d2015-11-19 22:16:48267 // The damage region to schedule paint for when Commit() is called.
reveman5c353d52016-02-11 21:28:56268 SkRegion pending_damage_;
revemanb195f41d2015-11-19 22:16:48269
270 // These lists contains the callbacks to notify the client when it is a good
271 // time to start producing a new frame. These callbacks move to
272 // |frame_callbacks_| when Commit() is called. Later they are moved to
273 // |active_frame_callbacks_| when the effect of the Commit() is reflected in
274 // the compositor's active layer tree. The callbacks fire once we're notified
275 // that the compositor started drawing that active layer tree.
276 std::list<FrameCallback> pending_frame_callbacks_;
277 std::list<FrameCallback> frame_callbacks_;
278 std::list<FrameCallback> active_frame_callbacks_;
279
280 // The opaque region to take effect when Commit() is called.
281 SkRegion pending_opaque_region_;
282
reveman2966d7702016-02-12 02:09:54283 // The input region to take effect when Commit() is called.
284 SkRegion pending_input_region_;
285
reveman7efa4b02016-01-06 08:29:54286 // The buffer scaling factor to take effect when Commit() is called.
287 float pending_buffer_scale_;
288
reveman27fe2642015-11-20 06:33:39289 // The stack of sub-surfaces to take effect when Commit() is called.
290 // Bottom-most sub-surface at the front of the list and top-most sub-surface
291 // at the back.
292 using SubSurfaceEntry = std::pair<Surface*, gfx::Point>;
293 using SubSurfaceEntryList = std::list<SubSurfaceEntry>;
294 SubSurfaceEntryList pending_sub_surfaces_;
295
reveman642d8c332016-02-19 19:55:44296 // The viewport to take effect when Commit() is called.
297 gfx::Size pending_viewport_;
298
reveman8e323902016-05-23 21:55:36299 // The crop rectangle to take effect when Commit() is called.
300 gfx::RectF pending_crop_;
301
reveman56f345902016-06-06 03:58:28302 // The active crop rectangle.
303 gfx::RectF crop_;
304
reveman85b7a562016-03-17 23:27:32305 // The secure output visibility state to take effect when Commit() is called.
306 bool pending_only_visible_on_secure_output_;
307
reveman56f345902016-06-06 03:58:28308 // The active secure output visibility state.
309 bool only_visible_on_secure_output_;
310
revemanfca687e2016-05-10 21:44:48311 // The blend mode state to take effect when Commit() is called.
312 SkXfermode::Mode pending_blend_mode_;
313
314 // The alpha state to take effect when Commit() is called.
315 float pending_alpha_;
316
reveman56f345902016-06-06 03:58:28317 // The active alpha state.
318 float alpha_;
319
revemanced21f862015-11-24 00:42:49320 // The buffer that is currently set as content of surface.
321 base::WeakPtr<Buffer> current_buffer_;
322
jbauman2fdc0732016-06-07 00:55:36323 // The last resource that was sent to a surface.
324 cc::TransferableResource current_resource_;
325
reveman2966d7702016-02-12 02:09:54326 // The active input region used for hit testing.
327 SkRegion input_region_;
328
reveman27fe2642015-11-20 06:33:39329 // This is true if a call to Commit() as been made but
330 // CommitSurfaceHierarchy() has not yet been called.
331 bool needs_commit_surface_hierarchy_;
332
reveman7cadea42016-02-05 20:14:38333 // This is set when the compositing starts and passed to active frame
334 // callbacks when compositing successfully ends.
335 base::TimeTicks last_compositing_start_time_;
336
revemanced21f862015-11-24 00:42:49337 // This is true when the contents of the surface should be updated next time
338 // the compositor successfully ends compositing.
339 bool update_contents_after_successful_compositing_;
reveman27fe2642015-11-20 06:33:39340
revemanb195f41d2015-11-19 22:16:48341 // The compsitor being observer or null if not observing a compositor.
342 ui::Compositor* compositor_;
343
reveman70baca12016-05-31 20:35:30344 // Cursor providers. Surface does not own the cursor providers.
345 std::set<CursorProvider*> cursor_providers_;
346
reveman14681352016-06-02 15:37:51347 // Texture size.
348 gfx::Size texture_size_in_dip_;
349
revemanb195f41d2015-11-19 22:16:48350 // This can be set to have some functions delegated. E.g. ShellSurface class
351 // can set this to handle Commit() and apply any double buffered state it
352 // maintains.
353 SurfaceDelegate* delegate_;
354
reveman27fe2642015-11-20 06:33:39355 // Surface observer list. Surface does not own the observers.
356 base::ObserverList<SurfaceObserver, true> observers_;
357
revemanb195f41d2015-11-19 22:16:48358 DISALLOW_COPY_AND_ASSIGN(Surface);
359};
360
361} // namespace exo
362
363#endif // COMPONENTS_EXO_SURFACE_H_