reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 1 | // 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> |
dcheng | 31759da | 2016-04-21 01:26:31 | [diff] [blame] | 9 | #include <memory> |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 10 | #include <utility> |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 11 | |
| 12 | #include "base/callback.h" |
| 13 | #include "base/macros.h" |
jbauman | bd9586a9 | 2016-05-28 01:09:03 | [diff] [blame^] | 14 | #include "base/memory/ref_counted.h" |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 16 | #include "base/observer_list.h" |
jbauman | bd9586a9 | 2016-05-28 01:09:03 | [diff] [blame^] | 17 | #include "cc/surfaces/surface_factory_client.h" |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 18 | #include "third_party/skia/include/core/SkRegion.h" |
reveman | fca687e | 2016-05-10 21:44:48 | [diff] [blame] | 19 | #include "third_party/skia/include/core/SkXfermode.h" |
reveman | 4c94cf96 | 2015-12-03 06:49:43 | [diff] [blame] | 20 | #include "ui/aura/window.h" |
reveman | 2966d770 | 2016-02-12 02:09:54 | [diff] [blame] | 21 | #include "ui/aura/window_observer.h" |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 22 | #include "ui/compositor/compositor_observer.h" |
| 23 | #include "ui/gfx/geometry/rect.h" |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 24 | |
| 25 | namespace base { |
| 26 | namespace trace_event { |
| 27 | class TracedValue; |
| 28 | } |
| 29 | } |
| 30 | |
jbauman | bd9586a9 | 2016-05-28 01:09:03 | [diff] [blame^] | 31 | namespace cc { |
| 32 | class SurfaceFactory; |
| 33 | enum class SurfaceDrawStatus; |
| 34 | } |
| 35 | |
reveman | 2966d770 | 2016-02-12 02:09:54 | [diff] [blame] | 36 | namespace gfx { |
| 37 | class Path; |
| 38 | } |
| 39 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 40 | namespace exo { |
| 41 | class Buffer; |
| 42 | class SurfaceDelegate; |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 43 | class SurfaceObserver; |
jbauman | bd9586a9 | 2016-05-28 01:09:03 | [diff] [blame^] | 44 | class 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. |
| 50 | class 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 | }; |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 73 | |
| 74 | // This class represents a rectangular area that is displayed on the screen. |
| 75 | // It has a location, size and pixel contents. |
reveman | 2966d770 | 2016-02-12 02:09:54 | [diff] [blame] | 76 | class Surface : public aura::Window, |
| 77 | public aura::WindowObserver, |
| 78 | public ui::CompositorObserver { |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 79 | public: |
| 80 | Surface(); |
| 81 | ~Surface() override; |
| 82 | |
reveman | 39b32c87 | 2015-12-08 05:34:05 | [diff] [blame] | 83 | // Type-checking downcast routine. |
kinaba | d14ca03e | 2016-02-23 04:43:35 | [diff] [blame] | 84 | static Surface* AsSurface(const aura::Window* window); |
reveman | 39b32c87 | 2015-12-08 05:34:05 | [diff] [blame] | 85 | |
jbauman | bd9586a9 | 2016-05-28 01:09:03 | [diff] [blame^] | 86 | // Sets whether to put the contents in a SurfaceLayer or a TextureLayer. |
| 87 | static void SetUseSurfaceLayer(bool use_surface_layer); |
| 88 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 89 | // 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 | |
reveman | 2966d770 | 2016-02-12 02:09:54 | [diff] [blame] | 106 | // This sets the region of the surface that can receive pointer and touch |
| 107 | // events. |
| 108 | void SetInputRegion(const SkRegion& region); |
| 109 | |
reveman | 7efa4b0 | 2016-01-06 08:29:54 | [diff] [blame] | 110 | // 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 | |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 116 | // 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 | |
reveman | 642d8c33 | 2016-02-19 19:55:44 | [diff] [blame] | 124 | // This sets the surface viewport for scaling. |
| 125 | void SetViewport(const gfx::Size& viewport); |
| 126 | |
reveman | 8e32390 | 2016-05-23 21:55:36 | [diff] [blame] | 127 | // This sets the surface crop rectangle. |
| 128 | void SetCrop(const gfx::RectF& crop); |
| 129 | |
reveman | 85b7a56 | 2016-03-17 23:27:32 | [diff] [blame] | 130 | // 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 | |
reveman | fca687e | 2016-05-10 21:44:48 | [diff] [blame] | 134 | // 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 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 140 | // Surface state (damage regions, attached buffers, etc.) is double-buffered. |
| 141 | // A Commit() call atomically applies all pending state, replacing the |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 142 | // current state. Commit() is not guaranteed to be synchronous. See |
| 143 | // CommitSurfaceHierarchy() below. |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 144 | void Commit(); |
| 145 | |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 146 | // 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 | |
reveman | b947076 | 2016-04-10 03:49:24 | [diff] [blame] | 154 | // Returns the bounds of the current input region of surface. |
| 155 | gfx::Rect GetHitTestBounds() const; |
reveman | 2966d770 | 2016-02-12 02:09:54 | [diff] [blame] | 156 | |
| 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; |
reveman | 4c94cf96 | 2015-12-03 06:49:43 | [diff] [blame] | 166 | |
reveman | aabfd71 | 2016-05-13 01:40:20 | [diff] [blame] | 167 | // Returns the bounds of the surface area that is not know to be transparent. |
| 168 | gfx::Rect GetNonTransparentBounds() const; |
| 169 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 170 | // Set the surface delegate. |
| 171 | void SetSurfaceDelegate(SurfaceDelegate* delegate); |
| 172 | |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 173 | // 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 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 182 | // Returns a trace value representing the state of the surface. |
dcheng | 31759da | 2016-04-21 01:26:31 | [diff] [blame] | 183 | std::unique_ptr<base::trace_event::TracedValue> AsTracedValue() const; |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 184 | |
reveman | 5c353d5 | 2016-02-11 21:28:56 | [diff] [blame] | 185 | bool HasPendingDamageForTesting(const gfx::Rect& damage) const { |
| 186 | return pending_damage_.contains(gfx::RectToSkIRect(damage)); |
| 187 | } |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 188 | |
reveman | 2966d770 | 2016-02-12 02:09:54 | [diff] [blame] | 189 | // Overridden from aura::WindowObserver: |
| 190 | void OnWindowAddedToRootWindow(aura::Window* window) override; |
| 191 | void OnWindowRemovingFromRootWindow(aura::Window* window, |
| 192 | aura::Window* new_root) override; |
| 193 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 194 | // Overridden from ui::CompositorObserver: |
| 195 | void OnCompositingDidCommit(ui::Compositor* compositor) override; |
| 196 | void OnCompositingStarted(ui::Compositor* compositor, |
| 197 | base::TimeTicks start_time) override; |
reveman | ced21f86 | 2015-11-24 00:42:49 | [diff] [blame] | 198 | void OnCompositingEnded(ui::Compositor* compositor) override; |
| 199 | void OnCompositingAborted(ui::Compositor* compositor) override; |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 200 | void OnCompositingLockStateChanged(ui::Compositor* compositor) override {} |
| 201 | void OnCompositingShuttingDown(ui::Compositor* compositor) override; |
| 202 | |
jbauman | bd9586a9 | 2016-05-28 01:09:03 | [diff] [blame^] | 203 | void WillDraw(cc::SurfaceId surface_id); |
| 204 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 205 | private: |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 206 | bool needs_commit_surface_hierarchy() const { |
| 207 | return needs_commit_surface_hierarchy_; |
| 208 | } |
| 209 | |
jbauman | bd9586a9 | 2016-05-28 01:09:03 | [diff] [blame^] | 210 | // 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 | |
reveman | ced21f86 | 2015-11-24 00:42:49 | [diff] [blame] | 216 | // This returns true when the surface has some contents assigned to it. |
| 217 | bool has_contents() const { return !!current_buffer_; } |
| 218 | |
jbauman | bd9586a9 | 2016-05-28 01:09:03 | [diff] [blame^] | 219 | // 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 | |
reveman | ced21f86 | 2015-11-24 00:42:49 | [diff] [blame] | 223 | // 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_; |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 226 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 227 | // The buffer that will become the content of surface when Commit() is called. |
| 228 | base::WeakPtr<Buffer> pending_buffer_; |
| 229 | |
jbauman | bd9586a9 | 2016-05-28 01:09:03 | [diff] [blame^] | 230 | 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 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 240 | // The damage region to schedule paint for when Commit() is called. |
reveman | 5c353d5 | 2016-02-11 21:28:56 | [diff] [blame] | 241 | SkRegion pending_damage_; |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 242 | |
| 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 | |
reveman | 2966d770 | 2016-02-12 02:09:54 | [diff] [blame] | 256 | // The input region to take effect when Commit() is called. |
| 257 | SkRegion pending_input_region_; |
| 258 | |
reveman | 7efa4b0 | 2016-01-06 08:29:54 | [diff] [blame] | 259 | // The buffer scaling factor to take effect when Commit() is called. |
| 260 | float pending_buffer_scale_; |
| 261 | |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 262 | // 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 | |
reveman | 642d8c33 | 2016-02-19 19:55:44 | [diff] [blame] | 269 | // The viewport to take effect when Commit() is called. |
| 270 | gfx::Size pending_viewport_; |
| 271 | |
reveman | 8e32390 | 2016-05-23 21:55:36 | [diff] [blame] | 272 | // The crop rectangle to take effect when Commit() is called. |
| 273 | gfx::RectF pending_crop_; |
| 274 | |
reveman | 85b7a56 | 2016-03-17 23:27:32 | [diff] [blame] | 275 | // The secure output visibility state to take effect when Commit() is called. |
| 276 | bool pending_only_visible_on_secure_output_; |
| 277 | |
reveman | fca687e | 2016-05-10 21:44:48 | [diff] [blame] | 278 | // 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 | |
reveman | aabfd71 | 2016-05-13 01:40:20 | [diff] [blame] | 284 | // The active alpha state. |
| 285 | float alpha_; |
| 286 | |
reveman | ced21f86 | 2015-11-24 00:42:49 | [diff] [blame] | 287 | // The buffer that is currently set as content of surface. |
| 288 | base::WeakPtr<Buffer> current_buffer_; |
| 289 | |
reveman | 2966d770 | 2016-02-12 02:09:54 | [diff] [blame] | 290 | // The active input region used for hit testing. |
| 291 | SkRegion input_region_; |
| 292 | |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 293 | // 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 | |
reveman | 7cadea4 | 2016-02-05 20:14:38 | [diff] [blame] | 297 | // 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 | |
reveman | ced21f86 | 2015-11-24 00:42:49 | [diff] [blame] | 301 | // 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_; |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 304 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 305 | // 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 | |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 313 | // Surface observer list. Surface does not own the observers. |
| 314 | base::ObserverList<SurfaceObserver, true> observers_; |
| 315 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 316 | DISALLOW_COPY_AND_ASSIGN(Surface); |
| 317 | }; |
| 318 | |
| 319 | } // namespace exo |
| 320 | |
| 321 | #endif // COMPONENTS_EXO_SURFACE_H_ |