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> |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 9 | #include <utility> |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 10 | |
| 11 | #include "base/callback.h" |
| 12 | #include "base/macros.h" |
| 13 | #include "base/memory/weak_ptr.h" |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 14 | #include "base/observer_list.h" |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 15 | #include "third_party/skia/include/core/SkRegion.h" |
reveman | 4c94cf96 | 2015-12-03 06:49:43 | [diff] [blame] | 16 | #include "ui/aura/window.h" |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 17 | #include "ui/compositor/compositor_observer.h" |
| 18 | #include "ui/gfx/geometry/rect.h" |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 19 | |
| 20 | namespace base { |
| 21 | namespace trace_event { |
| 22 | class TracedValue; |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | namespace exo { |
| 27 | class Buffer; |
| 28 | class SurfaceDelegate; |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 29 | class SurfaceObserver; |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 30 | |
| 31 | // This class represents a rectangular area that is displayed on the screen. |
| 32 | // It has a location, size and pixel contents. |
reveman | 4c94cf96 | 2015-12-03 06:49:43 | [diff] [blame] | 33 | class Surface : public aura::Window, public ui::CompositorObserver { |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 34 | public: |
| 35 | Surface(); |
| 36 | ~Surface() override; |
| 37 | |
reveman | 39b32c87 | 2015-12-08 05:34:05 | [diff] [blame] | 38 | // Type-checking downcast routine. |
| 39 | static Surface* AsSurface(aura::Window* window); |
| 40 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 41 | // Set a buffer as the content of this surface. A buffer can only be attached |
| 42 | // to one surface at a time. |
| 43 | void Attach(Buffer* buffer); |
| 44 | |
| 45 | // Describe the regions where the pending buffer is different from the |
| 46 | // current surface contents, and where the surface therefore needs to be |
| 47 | // repainted. |
| 48 | void Damage(const gfx::Rect& rect); |
| 49 | |
| 50 | // Request notification when the next frame is displayed. Useful for |
| 51 | // throttling redrawing operations, and driving animations. |
| 52 | using FrameCallback = base::Callback<void(base::TimeTicks frame_time)>; |
| 53 | void RequestFrameCallback(const FrameCallback& callback); |
| 54 | |
| 55 | // This sets the region of the surface that contains opaque content. |
| 56 | void SetOpaqueRegion(const SkRegion& region); |
| 57 | |
reveman | 7efa4b0 | 2016-01-06 08:29:54 | [diff] [blame] | 58 | // This sets the scaling factor used to interpret the contents of the buffer |
| 59 | // attached to the surface. Note that if the scale is larger than 1, then you |
| 60 | // have to attach a buffer that is larger (by a factor of scale in each |
| 61 | // dimension) than the desired surface size. |
| 62 | void SetBufferScale(float scale); |
| 63 | |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 64 | // Functions that control sub-surface state. All sub-surface state is |
| 65 | // double-buffered and will be applied when Commit() is called. |
| 66 | void AddSubSurface(Surface* sub_surface); |
| 67 | void RemoveSubSurface(Surface* sub_surface); |
| 68 | void SetSubSurfacePosition(Surface* sub_surface, const gfx::Point& position); |
| 69 | void PlaceSubSurfaceAbove(Surface* sub_surface, Surface* reference); |
| 70 | void PlaceSubSurfaceBelow(Surface* sub_surface, Surface* sibling); |
| 71 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 72 | // Surface state (damage regions, attached buffers, etc.) is double-buffered. |
| 73 | // A Commit() call atomically applies all pending state, replacing the |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 74 | // current state. Commit() is not guaranteed to be synchronous. See |
| 75 | // CommitSurfaceHierarchy() below. |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 76 | void Commit(); |
| 77 | |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 78 | // This will synchronously commit all pending state of the surface and its |
| 79 | // descendants by recursively calling CommitSurfaceHierarchy() for each |
| 80 | // sub-surface with pending state. |
| 81 | void CommitSurfaceHierarchy(); |
| 82 | |
| 83 | // Returns true if surface is in synchronized mode. |
| 84 | bool IsSynchronized() const; |
| 85 | |
reveman | 4c94cf96 | 2015-12-03 06:49:43 | [diff] [blame] | 86 | // Returns the preferred size of surface. |
| 87 | gfx::Size GetPreferredSize() const; |
| 88 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 89 | // Set the surface delegate. |
| 90 | void SetSurfaceDelegate(SurfaceDelegate* delegate); |
| 91 | |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 92 | // Returns true if surface has been assigned a surface delegate. |
| 93 | bool HasSurfaceDelegate() const; |
| 94 | |
| 95 | // Surface does not own observers. It is the responsibility of the observer |
| 96 | // to remove itself when it is done observing. |
| 97 | void AddSurfaceObserver(SurfaceObserver* observer); |
| 98 | void RemoveSurfaceObserver(SurfaceObserver* observer); |
| 99 | bool HasSurfaceObserver(const SurfaceObserver* observer) const; |
| 100 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 101 | // Returns a trace value representing the state of the surface. |
| 102 | scoped_refptr<base::trace_event::TracedValue> AsTracedValue() const; |
| 103 | |
| 104 | bool HasPendingDamageForTesting() const { return !pending_damage_.IsEmpty(); } |
| 105 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 106 | // Overridden from ui::CompositorObserver: |
| 107 | void OnCompositingDidCommit(ui::Compositor* compositor) override; |
| 108 | void OnCompositingStarted(ui::Compositor* compositor, |
| 109 | base::TimeTicks start_time) override; |
reveman | ced21f86 | 2015-11-24 00:42:49 | [diff] [blame] | 110 | void OnCompositingEnded(ui::Compositor* compositor) override; |
| 111 | void OnCompositingAborted(ui::Compositor* compositor) override; |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 112 | void OnCompositingLockStateChanged(ui::Compositor* compositor) override {} |
| 113 | void OnCompositingShuttingDown(ui::Compositor* compositor) override; |
| 114 | |
| 115 | private: |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 116 | bool needs_commit_surface_hierarchy() const { |
| 117 | return needs_commit_surface_hierarchy_; |
| 118 | } |
| 119 | |
reveman | ced21f86 | 2015-11-24 00:42:49 | [diff] [blame] | 120 | // This returns true when the surface has some contents assigned to it. |
| 121 | bool has_contents() const { return !!current_buffer_; } |
| 122 | |
| 123 | // This is true when Attach() has been called and new contents should take |
| 124 | // effect next time Commit() is called. |
| 125 | bool has_pending_contents_; |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 126 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 127 | // The buffer that will become the content of surface when Commit() is called. |
| 128 | base::WeakPtr<Buffer> pending_buffer_; |
| 129 | |
| 130 | // The damage region to schedule paint for when Commit() is called. |
| 131 | // TODO(reveman): Use SkRegion here after adding a version of |
| 132 | // ui::Layer::SchedulePaint that takes a SkRegion. |
| 133 | gfx::Rect pending_damage_; |
| 134 | |
| 135 | // These lists contains the callbacks to notify the client when it is a good |
| 136 | // time to start producing a new frame. These callbacks move to |
| 137 | // |frame_callbacks_| when Commit() is called. Later they are moved to |
| 138 | // |active_frame_callbacks_| when the effect of the Commit() is reflected in |
| 139 | // the compositor's active layer tree. The callbacks fire once we're notified |
| 140 | // that the compositor started drawing that active layer tree. |
| 141 | std::list<FrameCallback> pending_frame_callbacks_; |
| 142 | std::list<FrameCallback> frame_callbacks_; |
| 143 | std::list<FrameCallback> active_frame_callbacks_; |
| 144 | |
| 145 | // The opaque region to take effect when Commit() is called. |
| 146 | SkRegion pending_opaque_region_; |
| 147 | |
reveman | 7efa4b0 | 2016-01-06 08:29:54 | [diff] [blame] | 148 | // The buffer scaling factor to take effect when Commit() is called. |
| 149 | float pending_buffer_scale_; |
| 150 | |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 151 | // The stack of sub-surfaces to take effect when Commit() is called. |
| 152 | // Bottom-most sub-surface at the front of the list and top-most sub-surface |
| 153 | // at the back. |
| 154 | using SubSurfaceEntry = std::pair<Surface*, gfx::Point>; |
| 155 | using SubSurfaceEntryList = std::list<SubSurfaceEntry>; |
| 156 | SubSurfaceEntryList pending_sub_surfaces_; |
| 157 | |
reveman | ced21f86 | 2015-11-24 00:42:49 | [diff] [blame] | 158 | // The buffer that is currently set as content of surface. |
| 159 | base::WeakPtr<Buffer> current_buffer_; |
| 160 | |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 161 | // This is true if a call to Commit() as been made but |
| 162 | // CommitSurfaceHierarchy() has not yet been called. |
| 163 | bool needs_commit_surface_hierarchy_; |
| 164 | |
reveman | 7cadea4 | 2016-02-05 20:14:38 | [diff] [blame^] | 165 | // This is set when the compositing starts and passed to active frame |
| 166 | // callbacks when compositing successfully ends. |
| 167 | base::TimeTicks last_compositing_start_time_; |
| 168 | |
reveman | ced21f86 | 2015-11-24 00:42:49 | [diff] [blame] | 169 | // This is true when the contents of the surface should be updated next time |
| 170 | // the compositor successfully ends compositing. |
| 171 | bool update_contents_after_successful_compositing_; |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 172 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 173 | // The compsitor being observer or null if not observing a compositor. |
| 174 | ui::Compositor* compositor_; |
| 175 | |
| 176 | // This can be set to have some functions delegated. E.g. ShellSurface class |
| 177 | // can set this to handle Commit() and apply any double buffered state it |
| 178 | // maintains. |
| 179 | SurfaceDelegate* delegate_; |
| 180 | |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 181 | // Surface observer list. Surface does not own the observers. |
| 182 | base::ObserverList<SurfaceObserver, true> observers_; |
| 183 | |
reveman | b195f41d | 2015-11-19 22:16:48 | [diff] [blame] | 184 | DISALLOW_COPY_AND_ASSIGN(Surface); |
| 185 | }; |
| 186 | |
| 187 | } // namespace exo |
| 188 | |
| 189 | #endif // COMPONENTS_EXO_SURFACE_H_ |