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 | 70baca1 | 2016-05-31 20:35:30 | [diff] [blame^] | 10 | #include <set> |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 11 | #include <utility> |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 12 | |
| 13 | #include "base/callback.h" |
| 14 | #include "base/macros.h" |
jbauman | bd9586a9 | 2016-05-28 01:09:03 | [diff] [blame] | 15 | #include "base/memory/ref_counted.h" |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 16 | #include "base/memory/weak_ptr.h" |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 17 | #include "base/observer_list.h" |
jbauman | bd9586a9 | 2016-05-28 01:09:03 | [diff] [blame] | 18 | #include "cc/surfaces/surface_factory_client.h" |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 19 | #include "third_party/skia/include/core/SkRegion.h" |
reveman | fca687e | 2016-05-10 21:44:48 | [diff] [blame] | 20 | #include "third_party/skia/include/core/SkXfermode.h" |
reveman | 4c94cf96 | 2015-12-03 06:49:43 | [diff] [blame] | 21 | #include "ui/aura/window.h" |
reveman | 2966d770 | 2016-02-12 02:09:54 | [diff] [blame] | 22 | #include "ui/aura/window_observer.h" |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 23 | #include "ui/compositor/compositor_observer.h" |
| 24 | #include "ui/gfx/geometry/rect.h" |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 25 | |
| 26 | namespace base { |
| 27 | namespace trace_event { |
| 28 | class TracedValue; |
| 29 | } |
| 30 | } |
| 31 | |
jbauman | bd9586a9 | 2016-05-28 01:09:03 | [diff] [blame] | 32 | namespace cc { |
| 33 | class SurfaceFactory; |
| 34 | enum class SurfaceDrawStatus; |
| 35 | } |
| 36 | |
reveman | 2966d770 | 2016-02-12 02:09:54 | [diff] [blame] | 37 | namespace gfx { |
| 38 | class Path; |
| 39 | } |
| 40 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 41 | namespace exo { |
| 42 | class Buffer; |
reveman | 70baca1 | 2016-05-31 20:35:30 | [diff] [blame^] | 43 | class Pointer; |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 44 | class SurfaceDelegate; |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 45 | class SurfaceObserver; |
jbauman | bd9586a9 | 2016-05-28 01:09:03 | [diff] [blame] | 46 | class Surface; |
| 47 | |
reveman | 70baca1 | 2016-05-31 20:35:30 | [diff] [blame^] | 48 | // 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. |
| 50 | using CursorProvider = Pointer; |
| 51 | |
jbauman | bd9586a9 | 2016-05-28 01:09:03 | [diff] [blame] | 52 | // 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. |
| 56 | class 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 | }; |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 79 | |
| 80 | // This class represents a rectangular area that is displayed on the screen. |
| 81 | // It has a location, size and pixel contents. |
reveman | 2966d770 | 2016-02-12 02:09:54 | [diff] [blame] | 82 | class Surface : public aura::Window, |
| 83 | public aura::WindowObserver, |
| 84 | public ui::CompositorObserver { |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 85 | public: |
| 86 | Surface(); |
| 87 | ~Surface() override; |
| 88 | |
reveman | 39b32c87 | 2015-12-08 05:34:05 | [diff] [blame] | 89 | // Type-checking downcast routine. |
kinaba | d14ca03e | 2016-02-23 04:43:35 | [diff] [blame] | 90 | static Surface* AsSurface(const aura::Window* window); |
reveman | 39b32c87 | 2015-12-08 05:34:05 | [diff] [blame] | 91 | |
jbauman | bd9586a9 | 2016-05-28 01:09:03 | [diff] [blame] | 92 | // Sets whether to put the contents in a SurfaceLayer or a TextureLayer. |
| 93 | static void SetUseSurfaceLayer(bool use_surface_layer); |
| 94 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 95 | // 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 | |
reveman | 2966d770 | 2016-02-12 02:09:54 | [diff] [blame] | 112 | // This sets the region of the surface that can receive pointer and touch |
| 113 | // events. |
| 114 | void SetInputRegion(const SkRegion& region); |
| 115 | |
reveman | 7efa4b0 | 2016-01-06 08:29:54 | [diff] [blame] | 116 | // 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 | |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 122 | // 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 | |
reveman | 642d8c33 | 2016-02-19 19:55:44 | [diff] [blame] | 130 | // This sets the surface viewport for scaling. |
| 131 | void SetViewport(const gfx::Size& viewport); |
| 132 | |
reveman | 8e32390 | 2016-05-23 21:55:36 | [diff] [blame] | 133 | // This sets the surface crop rectangle. |
| 134 | void SetCrop(const gfx::RectF& crop); |
| 135 | |
reveman | 85b7a56 | 2016-03-17 23:27:32 | [diff] [blame] | 136 | // 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 | |
reveman | fca687e | 2016-05-10 21:44:48 | [diff] [blame] | 140 | // 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 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 146 | // Surface state (damage regions, attached buffers, etc.) is double-buffered. |
| 147 | // A Commit() call atomically applies all pending state, replacing the |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 148 | // current state. Commit() is not guaranteed to be synchronous. See |
| 149 | // CommitSurfaceHierarchy() below. |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 150 | void Commit(); |
| 151 | |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 152 | // 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 | |
reveman | b947076 | 2016-04-10 03:49:24 | [diff] [blame] | 160 | // Returns the bounds of the current input region of surface. |
| 161 | gfx::Rect GetHitTestBounds() const; |
reveman | 2966d770 | 2016-02-12 02:09:54 | [diff] [blame] | 162 | |
| 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; |
reveman | 4c94cf96 | 2015-12-03 06:49:43 | [diff] [blame] | 172 | |
reveman | aabfd71 | 2016-05-13 01:40:20 | [diff] [blame] | 173 | // Returns the bounds of the surface area that is not know to be transparent. |
| 174 | gfx::Rect GetNonTransparentBounds() const; |
| 175 | |
reveman | 70baca1 | 2016-05-31 20:35:30 | [diff] [blame^] | 176 | // 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 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 184 | // Set the surface delegate. |
| 185 | void SetSurfaceDelegate(SurfaceDelegate* delegate); |
| 186 | |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 187 | // 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 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 196 | // Returns a trace value representing the state of the surface. |
dcheng | 31759da | 2016-04-21 01:26:31 | [diff] [blame] | 197 | std::unique_ptr<base::trace_event::TracedValue> AsTracedValue() const; |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 198 | |
reveman | 5c353d5 | 2016-02-11 21:28:56 | [diff] [blame] | 199 | bool HasPendingDamageForTesting(const gfx::Rect& damage) const { |
| 200 | return pending_damage_.contains(gfx::RectToSkIRect(damage)); |
| 201 | } |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 202 | |
reveman | 2966d770 | 2016-02-12 02:09:54 | [diff] [blame] | 203 | // Overridden from aura::WindowObserver: |
| 204 | void OnWindowAddedToRootWindow(aura::Window* window) override; |
| 205 | void OnWindowRemovingFromRootWindow(aura::Window* window, |
| 206 | aura::Window* new_root) override; |
| 207 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 208 | // Overridden from ui::CompositorObserver: |
| 209 | void OnCompositingDidCommit(ui::Compositor* compositor) override; |
| 210 | void OnCompositingStarted(ui::Compositor* compositor, |
| 211 | base::TimeTicks start_time) override; |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 212 | void OnCompositingEnded(ui::Compositor* compositor) override; |
| 213 | void OnCompositingAborted(ui::Compositor* compositor) override; |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 214 | void OnCompositingLockStateChanged(ui::Compositor* compositor) override {} |
| 215 | void OnCompositingShuttingDown(ui::Compositor* compositor) override; |
| 216 | |
jbauman | bd9586a9 | 2016-05-28 01:09:03 | [diff] [blame] | 217 | void WillDraw(cc::SurfaceId surface_id); |
| 218 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 219 | private: |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 220 | bool needs_commit_surface_hierarchy() const { |
| 221 | return needs_commit_surface_hierarchy_; |
| 222 | } |
| 223 | |
jbauman | bd9586a9 | 2016-05-28 01:09:03 | [diff] [blame] | 224 | // 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 | |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 230 | // This returns true when the surface has some contents assigned to it. |
| 231 | bool has_contents() const { return !!current_buffer_; } |
| 232 | |
jbauman | bd9586a9 | 2016-05-28 01:09:03 | [diff] [blame] | 233 | // 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 | |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 237 | // 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_; |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 240 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 241 | // The buffer that will become the content of surface when Commit() is called. |
| 242 | base::WeakPtr<Buffer> pending_buffer_; |
| 243 | |
jbauman | bd9586a9 | 2016-05-28 01:09:03 | [diff] [blame] | 244 | 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 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 254 | // The damage region to schedule paint for when Commit() is called. |
reveman | 5c353d5 | 2016-02-11 21:28:56 | [diff] [blame] | 255 | SkRegion pending_damage_; |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 256 | |
| 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 | |
reveman | 2966d770 | 2016-02-12 02:09:54 | [diff] [blame] | 270 | // The input region to take effect when Commit() is called. |
| 271 | SkRegion pending_input_region_; |
| 272 | |
reveman | 7efa4b0 | 2016-01-06 08:29:54 | [diff] [blame] | 273 | // The buffer scaling factor to take effect when Commit() is called. |
| 274 | float pending_buffer_scale_; |
| 275 | |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 276 | // 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 | |
reveman | 642d8c33 | 2016-02-19 19:55:44 | [diff] [blame] | 283 | // The viewport to take effect when Commit() is called. |
| 284 | gfx::Size pending_viewport_; |
| 285 | |
reveman | 8e32390 | 2016-05-23 21:55:36 | [diff] [blame] | 286 | // The crop rectangle to take effect when Commit() is called. |
| 287 | gfx::RectF pending_crop_; |
| 288 | |
reveman | 85b7a56 | 2016-03-17 23:27:32 | [diff] [blame] | 289 | // The secure output visibility state to take effect when Commit() is called. |
| 290 | bool pending_only_visible_on_secure_output_; |
| 291 | |
reveman | fca687e | 2016-05-10 21:44:48 | [diff] [blame] | 292 | // 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 | |
reveman | aabfd71 | 2016-05-13 01:40:20 | [diff] [blame] | 298 | // The active alpha state. |
| 299 | float alpha_; |
| 300 | |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 301 | // The buffer that is currently set as content of surface. |
| 302 | base::WeakPtr<Buffer> current_buffer_; |
| 303 | |
reveman | 2966d770 | 2016-02-12 02:09:54 | [diff] [blame] | 304 | // The active input region used for hit testing. |
| 305 | SkRegion input_region_; |
| 306 | |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 307 | // 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 | |
reveman | 7cadea4 | 2016-02-05 20:14:38 | [diff] [blame] | 311 | // 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 | |
reveman | ced21f8 | 2015-11-24 00:42:49 | [diff] [blame] | 315 | // 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_; |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 318 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 319 | // The compsitor being observer or null if not observing a compositor. |
| 320 | ui::Compositor* compositor_; |
| 321 | |
reveman | 70baca1 | 2016-05-31 20:35:30 | [diff] [blame^] | 322 | // Cursor providers. Surface does not own the cursor providers. |
| 323 | std::set<CursorProvider*> cursor_providers_; |
| 324 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 325 | // 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 | |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 330 | // Surface observer list. Surface does not own the observers. |
| 331 | base::ObserverList<SurfaceObserver, true> observers_; |
| 332 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 333 | DISALLOW_COPY_AND_ASSIGN(Surface); |
| 334 | }; |
| 335 | |
| 336 | } // namespace exo |
| 337 | |
| 338 | #endif // COMPONENTS_EXO_SURFACE_H_ |