blob: bb62e996ffe5d35ba147bad602b29f66da4fbef5 [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>
reveman70baca12016-05-31 20:35:309#include <set>
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"
danakjc7afae52017-06-20 21:12:4117#include "components/exo/layer_tree_frame_sink_holder.h"
Fady Samuelc645ffe2017-07-24 17:28:2018#include "components/viz/common/frame_sinks/begin_frame_source.h"
Fady Samuel4f7f0fb32017-07-28 15:33:3719#include "components/viz/common/resources/transferable_resource.h"
reedcc9c70f2016-11-22 04:26:0120#include "third_party/skia/include/core/SkBlendMode.h"
revemanb195f41d2015-11-19 22:16:4821#include "third_party/skia/include/core/SkRegion.h"
reveman4c94cf962015-12-03 06:49:4322#include "ui/aura/window.h"
revemanb195f41d2015-11-19 22:16:4823#include "ui/gfx/geometry/rect.h"
revemane295c662017-01-30 23:01:2124#include "ui/gfx/native_widget_types.h"
revemanb195f41d2015-11-19 22:16:4825
26namespace base {
27namespace trace_event {
28class TracedValue;
29}
30}
31
Peng Huang583c9dc62017-07-27 23:38:2832namespace cc {
33class CompositorFrame;
34}
35
reveman2966d7702016-02-12 02:09:5436namespace gfx {
37class Path;
38}
39
revemanb195f41d2015-11-19 22:16:4840namespace exo {
41class Buffer;
Peng Huang583c9dc62017-07-27 23:38:2842class LayerTreeFrameSinkHolder;
reveman70baca12016-05-31 20:35:3043class Pointer;
revemanb195f41d2015-11-19 22:16:4844class SurfaceDelegate;
reveman27fe2642015-11-20 06:33:3945class SurfaceObserver;
jbaumanbd9586a92016-05-28 01:09:0346class Surface;
47
jbaumanb362a892016-06-17 03:30:5648namespace subtle {
49class PropertyHelper;
50}
51
David Revemanfca309b2017-08-24 18:18:1152// Counter-clockwise rotations.
53enum class Transform { NORMAL, ROTATE_90, ROTATE_180, ROTATE_270 };
54
reveman70baca12016-05-31 20:35:3055// The pointer class is currently the only cursor provider class but this can
56// change in the future when better hardware cursor support is added.
57using CursorProvider = Pointer;
58
revemanb195f41d2015-11-19 22:16:4859// This class represents a rectangular area that is displayed on the screen.
60// It has a location, size and pixel contents.
Hans Wennborgaf90ff12017-09-04 19:46:0261class Surface final : public ui::PropertyHandler {
revemanb195f41d2015-11-19 22:16:4862 public:
reveman2d3815d2016-06-26 20:13:2563 using PropertyDeallocator = void (*)(int64_t value);
jbaumanb362a892016-06-17 03:30:5664
revemanb195f41d2015-11-19 22:16:4865 Surface();
Peng Huang583c9dc62017-07-27 23:38:2866 ~Surface();
revemanb195f41d2015-11-19 22:16:4867
reveman39b32c872015-12-08 05:34:0568 // Type-checking downcast routine.
kinabad14ca03e2016-02-23 04:43:3569 static Surface* AsSurface(const aura::Window* window);
reveman39b32c872015-12-08 05:34:0570
jbaumane3526252016-06-09 18:43:0571 aura::Window* window() { return window_.get(); }
72
revemanb195f41d2015-11-19 22:16:4873 // Set a buffer as the content of this surface. A buffer can only be attached
74 // to one surface at a time.
75 void Attach(Buffer* buffer);
76
77 // Describe the regions where the pending buffer is different from the
78 // current surface contents, and where the surface therefore needs to be
79 // repainted.
80 void Damage(const gfx::Rect& rect);
81
reveman211cf802017-01-10 00:30:5982 // Request notification when it's a good time to produce a new frame. Useful
83 // for throttling redrawing operations, and driving animations.
revemanb195f41d2015-11-19 22:16:4884 using FrameCallback = base::Callback<void(base::TimeTicks frame_time)>;
85 void RequestFrameCallback(const FrameCallback& callback);
86
reveman211cf802017-01-10 00:30:5987 // Request notification when the next frame is displayed. Useful for
88 // throttling redrawing operations, and driving animations.
89 using PresentationCallback =
90 base::Callback<void(base::TimeTicks presentation_time,
91 base::TimeDelta refresh)>;
92 void RequestPresentationCallback(const PresentationCallback& callback);
93
revemanb195f41d2015-11-19 22:16:4894 // This sets the region of the surface that contains opaque content.
95 void SetOpaqueRegion(const SkRegion& region);
96
reveman2966d7702016-02-12 02:09:5497 // This sets the region of the surface that can receive pointer and touch
98 // events.
99 void SetInputRegion(const SkRegion& region);
100
reveman7efa4b02016-01-06 08:29:54101 // This sets the scaling factor used to interpret the contents of the buffer
102 // attached to the surface. Note that if the scale is larger than 1, then you
103 // have to attach a buffer that is larger (by a factor of scale in each
104 // dimension) than the desired surface size.
105 void SetBufferScale(float scale);
106
David Revemanfca309b2017-08-24 18:18:11107 // This sets the transformation used to interpret the contents of the buffer
108 // attached to the surface.
109 void SetBufferTransform(Transform transform);
110
reveman27fe2642015-11-20 06:33:39111 // Functions that control sub-surface state. All sub-surface state is
112 // double-buffered and will be applied when Commit() is called.
113 void AddSubSurface(Surface* sub_surface);
114 void RemoveSubSurface(Surface* sub_surface);
115 void SetSubSurfacePosition(Surface* sub_surface, const gfx::Point& position);
116 void PlaceSubSurfaceAbove(Surface* sub_surface, Surface* reference);
117 void PlaceSubSurfaceBelow(Surface* sub_surface, Surface* sibling);
118
reveman642d8c332016-02-19 19:55:44119 // This sets the surface viewport for scaling.
120 void SetViewport(const gfx::Size& viewport);
121
reveman8e323902016-05-23 21:55:36122 // This sets the surface crop rectangle.
123 void SetCrop(const gfx::RectF& crop);
124
reveman85b7a562016-03-17 23:27:32125 // This sets the only visible on secure output flag, preventing it from
126 // appearing in screenshots or from being viewed on non-secure displays.
127 void SetOnlyVisibleOnSecureOutput(bool only_visible_on_secure_output);
128
revemanfca687e2016-05-10 21:44:48129 // This sets the blend mode that will be used when drawing the surface.
reedcc9c70f2016-11-22 04:26:01130 void SetBlendMode(SkBlendMode blend_mode);
revemanfca687e2016-05-10 21:44:48131
132 // This sets the alpha value that will be applied to the whole surface.
133 void SetAlpha(float alpha);
134
revemanb195f41d2015-11-19 22:16:48135 // Surface state (damage regions, attached buffers, etc.) is double-buffered.
136 // A Commit() call atomically applies all pending state, replacing the
reveman27fe2642015-11-20 06:33:39137 // current state. Commit() is not guaranteed to be synchronous. See
138 // CommitSurfaceHierarchy() below.
revemanb195f41d2015-11-19 22:16:48139 void Commit();
140
reveman27fe2642015-11-20 06:33:39141 // This will synchronously commit all pending state of the surface and its
142 // descendants by recursively calling CommitSurfaceHierarchy() for each
143 // sub-surface with pending state.
Peng Huang583c9dc62017-07-27 23:38:28144 void CommitSurfaceHierarchy(
145 const gfx::Point& origin,
Peng Huang583c9dc62017-07-27 23:38:28146 LayerTreeFrameSinkHolder* frame_sink_holder,
Peng Huang583c9dc62017-07-27 23:38:28147 std::list<FrameCallback>* frame_callbacks,
148 std::list<PresentationCallback>* presentation_callbacks);
reveman27fe2642015-11-20 06:33:39149
Peng Huang76f5fd02017-09-01 00:59:39150 void AppendSurfaceHierarchyContentsToFrame(
151 const gfx::Point& origin,
152 float device_scale_factor,
153 LayerTreeFrameSinkHolder* frame_sink_holder,
154 cc::CompositorFrame* frame);
155
reveman27fe2642015-11-20 06:33:39156 // Returns true if surface is in synchronized mode.
157 bool IsSynchronized() const;
158
revemanb9470762016-04-10 03:49:24159 // Returns the bounds of the current input region of surface.
160 gfx::Rect GetHitTestBounds() const;
reveman2966d7702016-02-12 02:09:54161
162 // Returns true if |rect| intersects this surface's bounds.
163 bool HitTestRect(const gfx::Rect& rect) const;
164
165 // Returns true if the current input region is different than the surface
166 // bounds.
167 bool HasHitTestMask() const;
168
169 // Returns the current input region of surface in the form of a hit-test mask.
170 void GetHitTestMask(gfx::Path* mask) const;
reveman4c94cf962015-12-03 06:49:43171
reveman70baca12016-05-31 20:35:30172 // Surface does not own cursor providers. It is the responsibility of the
173 // caller to remove the cursor provider before it is destroyed.
174 void RegisterCursorProvider(CursorProvider* provider);
175 void UnregisterCursorProvider(CursorProvider* provider);
176
revemane295c662017-01-30 23:01:21177 // Returns the cursor for the surface. If no cursor provider is registered
ergeeba7c622017-04-25 18:06:16178 // then CursorType::kNull is returned.
revemane295c662017-01-30 23:01:21179 gfx::NativeCursor GetCursor();
reveman70baca12016-05-31 20:35:30180
revemanb195f41d2015-11-19 22:16:48181 // Set the surface delegate.
182 void SetSurfaceDelegate(SurfaceDelegate* delegate);
183
reveman27fe2642015-11-20 06:33:39184 // Returns true if surface has been assigned a surface delegate.
185 bool HasSurfaceDelegate() const;
186
187 // Surface does not own observers. It is the responsibility of the observer
188 // to remove itself when it is done observing.
189 void AddSurfaceObserver(SurfaceObserver* observer);
190 void RemoveSurfaceObserver(SurfaceObserver* observer);
191 bool HasSurfaceObserver(const SurfaceObserver* observer) const;
192
revemanb195f41d2015-11-19 22:16:48193 // Returns a trace value representing the state of the surface.
dcheng31759da2016-04-21 01:26:31194 std::unique_ptr<base::trace_event::TracedValue> AsTracedValue() const;
revemanb195f41d2015-11-19 22:16:48195
eseckler599d86bb2017-03-15 09:02:55196 // Called when the begin frame source has changed.
Fady Samuelc645ffe2017-07-24 17:28:20197 void SetBeginFrameSource(viz::BeginFrameSource* begin_frame_source);
jbaumanbd9586a92016-05-28 01:09:03198
reveman15aee282016-11-04 19:09:20199 // Returns the active contents size.
Peng Huangb07b0652017-06-27 17:25:22200 const gfx::Size& content_size() const { return content_size_; }
jbaumanb362a892016-06-17 03:30:56201
kaznacheev8e270592017-05-25 06:13:26202 // Returns true if the associated window is in 'stylus-only' mode.
203 bool IsStylusOnly();
204
205 // Enables 'stylus-only' mode for the associated window.
206 void SetStylusOnly();
207
Peng Huang583c9dc62017-07-27 23:38:28208 // Recreates resources for the surface and sub surfaces.
209 void RecreateResources(LayerTreeFrameSinkHolder* frame_sink_holder);
reveman15aee282016-11-04 19:09:20210
Peng Huang583c9dc62017-07-27 23:38:28211 // Returns true if the surface's bounds should be filled opaquely.
212 bool FillsBoundsOpaquely() const;
reveman211cf802017-01-10 00:30:59213
reveman15aee282016-11-04 19:09:20214 bool HasPendingDamageForTesting(const gfx::Rect& damage) const {
215 return pending_damage_.contains(gfx::RectToSkIRect(damage));
216 }
217
revemanb195f41d2015-11-19 22:16:48218 private:
jbaumanf4c3f292016-06-11 00:57:33219 struct State {
220 State();
221 ~State();
222
223 bool operator==(const State& other);
224 bool operator!=(const State& other) { return !(*this == other); }
225
226 SkRegion opaque_region;
227 SkRegion input_region;
reveman2d3815d2016-06-26 20:13:25228 float buffer_scale = 1.0f;
David Revemanfca309b2017-08-24 18:18:11229 Transform buffer_transform = Transform::NORMAL;
jbaumanf4c3f292016-06-11 00:57:33230 gfx::Size viewport;
231 gfx::RectF crop;
232 bool only_visible_on_secure_output = false;
reedcc9c70f2016-11-22 04:26:01233 SkBlendMode blend_mode = SkBlendMode::kSrcOver;
jbaumanf4c3f292016-06-11 00:57:33234 float alpha = 1.0f;
jbauman45c06862016-06-23 19:35:02235 };
236 class BufferAttachment {
237 public:
238 BufferAttachment();
239 ~BufferAttachment();
240
241 BufferAttachment& operator=(BufferAttachment&& buffer);
242
243 base::WeakPtr<Buffer>& buffer();
244 const base::WeakPtr<Buffer>& buffer() const;
245 void Reset(base::WeakPtr<Buffer> buffer);
246
247 private:
248 base::WeakPtr<Buffer> buffer_;
249
250 DISALLOW_COPY_AND_ASSIGN(BufferAttachment);
jbaumanf4c3f292016-06-11 00:57:33251 };
252
jbaumanb362a892016-06-17 03:30:56253 friend class subtle::PropertyHelper;
254
jbauman45c06862016-06-23 19:35:02255 // Updates current_resource_ with a new resource id corresponding to the
256 // contents of the attached buffer (or id 0, if no buffer is attached).
257 // UpdateSurface must be called afterwards to ensure the release callback
258 // will be called.
Peng Huang583c9dc62017-07-27 23:38:28259 void UpdateResource(LayerTreeFrameSinkHolder* frame_sink_holder,
260 bool client_usage);
jbauman45c06862016-06-23 19:35:02261
Peng Huang583c9dc62017-07-27 23:38:28262 // Puts the current surface into a draw quad, and appends the draw quads into
263 // the |frame|.
264 void AppendContentsToFrame(const gfx::Point& origin,
Peng Huang76f5fd02017-09-01 00:59:39265 float device_scale_factor,
266 cc::CompositorFrame* frame);
jbauman45c06862016-06-23 19:35:02267
Peng Huang583c9dc62017-07-27 23:38:28268 void UpdateContentSize();
eseckler599d86bb2017-03-15 09:02:55269
revemanced21f862015-11-24 00:42:49270 // This returns true when the surface has some contents assigned to it.
jbauman45c06862016-06-23 19:35:02271 bool has_contents() const { return !!current_buffer_.buffer(); }
revemanced21f862015-11-24 00:42:49272
jbaumane3526252016-06-09 18:43:05273 // This window has the layer which contains the Surface contents.
274 std::unique_ptr<aura::Window> window_;
275
Peng Huang583c9dc62017-07-27 23:38:28276 // This true, if sub_surfaces_ has changes (order, position, etc).
277 bool sub_surfaces_changed_ = false;
jbaumanf4c3f292016-06-11 00:57:33278
jbaumanb362a892016-06-17 03:30:56279 // This is the size of the last committed contents.
280 gfx::Size content_size_;
281
revemanced21f862015-11-24 00:42:49282 // This is true when Attach() has been called and new contents should take
283 // effect next time Commit() is called.
reveman2d3815d2016-06-26 20:13:25284 bool has_pending_contents_ = false;
reveman27fe2642015-11-20 06:33:39285
revemanb195f41d2015-11-19 22:16:48286 // The buffer that will become the content of surface when Commit() is called.
jbauman45c06862016-06-23 19:35:02287 BufferAttachment pending_buffer_;
revemanb195f41d2015-11-19 22:16:48288
revemanb195f41d2015-11-19 22:16:48289 // The damage region to schedule paint for when Commit() is called.
reveman5c353d52016-02-11 21:28:56290 SkRegion pending_damage_;
revemanb195f41d2015-11-19 22:16:48291
Peng Huang76f5fd02017-09-01 00:59:39292 // The damage region which will be used by
293 // AppendSurfaceHierarchyContentsToFrame() to generate frame.
294 SkRegion damage_;
295
revemanb195f41d2015-11-19 22:16:48296 // These lists contains the callbacks to notify the client when it is a good
297 // time to start producing a new frame. These callbacks move to
298 // |frame_callbacks_| when Commit() is called. Later they are moved to
reveman15aee282016-11-04 19:09:20299 // |active_frame_callbacks_| when the effect of the Commit() is scheduled to
300 // be drawn. They fire at the first begin frame notification after this.
revemanb195f41d2015-11-19 22:16:48301 std::list<FrameCallback> pending_frame_callbacks_;
reveman211cf802017-01-10 00:30:59302
303 // These lists contains the callbacks to notify the client when surface
304 // contents have been presented. These callbacks move to
305 // |presentation_callbacks_| when Commit() is called. Later they are moved to
306 // |swapping_presentation_callbacks_| when the effect of the Commit() is
307 // scheduled to be drawn and then moved to |swapped_presentation_callbacks_|
308 // after receiving VSync parameters update for the previous frame. They fire
309 // at the next VSync parameters update after that.
310 std::list<PresentationCallback> pending_presentation_callbacks_;
revemanb195f41d2015-11-19 22:16:48311
jbaumanf4c3f292016-06-11 00:57:33312 // This is the state that has yet to be committed.
313 State pending_state_;
revemanb195f41d2015-11-19 22:16:48314
jbaumanf4c3f292016-06-11 00:57:33315 // This is the state that has been committed.
316 State state_;
reveman7efa4b02016-01-06 08:29:54317
reveman27fe2642015-11-20 06:33:39318 // The stack of sub-surfaces to take effect when Commit() is called.
319 // Bottom-most sub-surface at the front of the list and top-most sub-surface
320 // at the back.
321 using SubSurfaceEntry = std::pair<Surface*, gfx::Point>;
322 using SubSurfaceEntryList = std::list<SubSurfaceEntry>;
323 SubSurfaceEntryList pending_sub_surfaces_;
Peng Huang583c9dc62017-07-27 23:38:28324 SubSurfaceEntryList sub_surfaces_;
reveman27fe2642015-11-20 06:33:39325
revemanced21f862015-11-24 00:42:49326 // The buffer that is currently set as content of surface.
jbauman45c06862016-06-23 19:35:02327 BufferAttachment current_buffer_;
revemanced21f862015-11-24 00:42:49328
jbauman2fdc0732016-06-07 00:55:36329 // The last resource that was sent to a surface.
Fady Samuel4f7f0fb32017-07-28 15:33:37330 viz::TransferableResource current_resource_;
jbauman2fdc0732016-06-07 00:55:36331
revemanca132dc2017-01-31 22:35:54332 // Whether the last resource that was sent to a surface has an alpha channel.
333 bool current_resource_has_alpha_ = false;
334
reveman27fe2642015-11-20 06:33:39335 // This is true if a call to Commit() as been made but
336 // CommitSurfaceHierarchy() has not yet been called.
Peng Huang76f5fd02017-09-01 00:59:39337 bool needs_commit_surface_ = false;
reveman27fe2642015-11-20 06:33:39338
reveman7cadea42016-02-05 20:14:38339 // This is set when the compositing starts and passed to active frame
340 // callbacks when compositing successfully ends.
341 base::TimeTicks last_compositing_start_time_;
342
reveman70baca12016-05-31 20:35:30343 // Cursor providers. Surface does not own the cursor providers.
344 std::set<CursorProvider*> cursor_providers_;
345
revemanb195f41d2015-11-19 22:16:48346 // This can be set to have some functions delegated. E.g. ShellSurface class
347 // can set this to handle Commit() and apply any double buffered state it
348 // maintains.
reveman2d3815d2016-06-26 20:13:25349 SurfaceDelegate* delegate_ = nullptr;
revemanb195f41d2015-11-19 22:16:48350
reveman27fe2642015-11-20 06:33:39351 // Surface observer list. Surface does not own the observers.
352 base::ObserverList<SurfaceObserver, true> observers_;
353
revemanb195f41d2015-11-19 22:16:48354 DISALLOW_COPY_AND_ASSIGN(Surface);
355};
356
357} // namespace exo
358
359#endif // COMPONENTS_EXO_SURFACE_H_