blob: 81009c03bbead7a0c421830523407adbdccc0f61 [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 std::list<FrameCallback>* frame_callbacks,
147 std::list<PresentationCallback>* presentation_callbacks);
reveman27fe2642015-11-20 06:33:39148
Peng Huang76f5fd02017-09-01 00:59:39149 void AppendSurfaceHierarchyContentsToFrame(
150 const gfx::Point& origin,
151 float device_scale_factor,
152 LayerTreeFrameSinkHolder* frame_sink_holder,
153 cc::CompositorFrame* frame);
154
reveman27fe2642015-11-20 06:33:39155 // Returns true if surface is in synchronized mode.
156 bool IsSynchronized() const;
157
revemanb9470762016-04-10 03:49:24158 // Returns the bounds of the current input region of surface.
159 gfx::Rect GetHitTestBounds() const;
reveman2966d7702016-02-12 02:09:54160
161 // Returns true if |rect| intersects this surface's bounds.
162 bool HitTestRect(const gfx::Rect& rect) const;
163
164 // Returns true if the current input region is different than the surface
165 // bounds.
166 bool HasHitTestMask() const;
167
168 // Returns the current input region of surface in the form of a hit-test mask.
169 void GetHitTestMask(gfx::Path* mask) const;
reveman4c94cf962015-12-03 06:49:43170
reveman70baca12016-05-31 20:35:30171 // Surface does not own cursor providers. It is the responsibility of the
172 // caller to remove the cursor provider before it is destroyed.
173 void RegisterCursorProvider(CursorProvider* provider);
174 void UnregisterCursorProvider(CursorProvider* provider);
175
revemane295c662017-01-30 23:01:21176 // Returns the cursor for the surface. If no cursor provider is registered
ergeeba7c622017-04-25 18:06:16177 // then CursorType::kNull is returned.
revemane295c662017-01-30 23:01:21178 gfx::NativeCursor GetCursor();
reveman70baca12016-05-31 20:35:30179
revemanb195f41d2015-11-19 22:16:48180 // Set the surface delegate.
181 void SetSurfaceDelegate(SurfaceDelegate* delegate);
182
reveman27fe2642015-11-20 06:33:39183 // Returns true if surface has been assigned a surface delegate.
184 bool HasSurfaceDelegate() const;
185
186 // Surface does not own observers. It is the responsibility of the observer
187 // to remove itself when it is done observing.
188 void AddSurfaceObserver(SurfaceObserver* observer);
189 void RemoveSurfaceObserver(SurfaceObserver* observer);
190 bool HasSurfaceObserver(const SurfaceObserver* observer) const;
191
revemanb195f41d2015-11-19 22:16:48192 // Returns a trace value representing the state of the surface.
dcheng31759da2016-04-21 01:26:31193 std::unique_ptr<base::trace_event::TracedValue> AsTracedValue() const;
revemanb195f41d2015-11-19 22:16:48194
eseckler599d86bb2017-03-15 09:02:55195 // Called when the begin frame source has changed.
Fady Samuelc645ffe2017-07-24 17:28:20196 void SetBeginFrameSource(viz::BeginFrameSource* begin_frame_source);
jbaumanbd9586a92016-05-28 01:09:03197
reveman15aee282016-11-04 19:09:20198 // Returns the active contents size.
Peng Huangb07b0652017-06-27 17:25:22199 const gfx::Size& content_size() const { return content_size_; }
jbaumanb362a892016-06-17 03:30:56200
kaznacheev8e270592017-05-25 06:13:26201 // Returns true if the associated window is in 'stylus-only' mode.
202 bool IsStylusOnly();
203
204 // Enables 'stylus-only' mode for the associated window.
205 void SetStylusOnly();
206
Peng Huangc51f7aba2017-09-05 16:00:39207 // Notify surface that resources and subsurfaces' resources have been lost.
208 void SurfaceHierarchyResourcesLost();
reveman15aee282016-11-04 19:09:20209
Peng Huang583c9dc62017-07-27 23:38:28210 // Returns true if the surface's bounds should be filled opaquely.
211 bool FillsBoundsOpaquely() const;
reveman211cf802017-01-10 00:30:59212
reveman15aee282016-11-04 19:09:20213 bool HasPendingDamageForTesting(const gfx::Rect& damage) const {
214 return pending_damage_.contains(gfx::RectToSkIRect(damage));
215 }
216
revemanb195f41d2015-11-19 22:16:48217 private:
jbaumanf4c3f292016-06-11 00:57:33218 struct State {
219 State();
220 ~State();
221
222 bool operator==(const State& other);
223 bool operator!=(const State& other) { return !(*this == other); }
224
225 SkRegion opaque_region;
226 SkRegion input_region;
reveman2d3815d2016-06-26 20:13:25227 float buffer_scale = 1.0f;
David Revemanfca309b2017-08-24 18:18:11228 Transform buffer_transform = Transform::NORMAL;
jbaumanf4c3f292016-06-11 00:57:33229 gfx::Size viewport;
230 gfx::RectF crop;
231 bool only_visible_on_secure_output = false;
reedcc9c70f2016-11-22 04:26:01232 SkBlendMode blend_mode = SkBlendMode::kSrcOver;
jbaumanf4c3f292016-06-11 00:57:33233 float alpha = 1.0f;
jbauman45c06862016-06-23 19:35:02234 };
235 class BufferAttachment {
236 public:
237 BufferAttachment();
238 ~BufferAttachment();
239
240 BufferAttachment& operator=(BufferAttachment&& buffer);
241
242 base::WeakPtr<Buffer>& buffer();
243 const base::WeakPtr<Buffer>& buffer() const;
244 void Reset(base::WeakPtr<Buffer> buffer);
245
246 private:
247 base::WeakPtr<Buffer> buffer_;
248
249 DISALLOW_COPY_AND_ASSIGN(BufferAttachment);
jbaumanf4c3f292016-06-11 00:57:33250 };
251
jbaumanb362a892016-06-17 03:30:56252 friend class subtle::PropertyHelper;
253
jbauman45c06862016-06-23 19:35:02254 // Updates current_resource_ with a new resource id corresponding to the
255 // contents of the attached buffer (or id 0, if no buffer is attached).
256 // UpdateSurface must be called afterwards to ensure the release callback
257 // will be called.
Peng Huangc51f7aba2017-09-05 16:00:39258 void UpdateResource(LayerTreeFrameSinkHolder* frame_sink_holder);
jbauman45c06862016-06-23 19:35:02259
Peng Huang583c9dc62017-07-27 23:38:28260 // Puts the current surface into a draw quad, and appends the draw quads into
261 // the |frame|.
262 void AppendContentsToFrame(const gfx::Point& origin,
Peng Huang76f5fd02017-09-01 00:59:39263 float device_scale_factor,
264 cc::CompositorFrame* frame);
jbauman45c06862016-06-23 19:35:02265
Peng Huangc51f7aba2017-09-05 16:00:39266 // Update surface content size base on current buffer size.
Peng Huang583c9dc62017-07-27 23:38:28267 void UpdateContentSize();
eseckler599d86bb2017-03-15 09:02:55268
revemanced21f862015-11-24 00:42:49269 // This returns true when the surface has some contents assigned to it.
jbauman45c06862016-06-23 19:35:02270 bool has_contents() const { return !!current_buffer_.buffer(); }
revemanced21f862015-11-24 00:42:49271
jbaumane3526252016-06-09 18:43:05272 // This window has the layer which contains the Surface contents.
273 std::unique_ptr<aura::Window> window_;
274
Peng Huang583c9dc62017-07-27 23:38:28275 // This true, if sub_surfaces_ has changes (order, position, etc).
276 bool sub_surfaces_changed_ = false;
jbaumanf4c3f292016-06-11 00:57:33277
jbaumanb362a892016-06-17 03:30:56278 // This is the size of the last committed contents.
279 gfx::Size content_size_;
280
revemanced21f862015-11-24 00:42:49281 // This is true when Attach() has been called and new contents should take
282 // effect next time Commit() is called.
reveman2d3815d2016-06-26 20:13:25283 bool has_pending_contents_ = false;
reveman27fe2642015-11-20 06:33:39284
revemanb195f41d2015-11-19 22:16:48285 // The buffer that will become the content of surface when Commit() is called.
jbauman45c06862016-06-23 19:35:02286 BufferAttachment pending_buffer_;
revemanb195f41d2015-11-19 22:16:48287
288 // The damage region to schedule paint for when Commit() is called.
reveman5c353d52016-02-11 21:28:56289 SkRegion pending_damage_;
revemanb195f41d2015-11-19 22:16:48290
Peng Huang76f5fd02017-09-01 00:59:39291 // The damage region which will be used by
292 // AppendSurfaceHierarchyContentsToFrame() to generate frame.
293 SkRegion damage_;
294
revemanb195f41d2015-11-19 22:16:48295 // These lists contains the callbacks to notify the client when it is a good
296 // time to start producing a new frame. These callbacks move to
297 // |frame_callbacks_| when Commit() is called. Later they are moved to
reveman15aee282016-11-04 19:09:20298 // |active_frame_callbacks_| when the effect of the Commit() is scheduled to
299 // be drawn. They fire at the first begin frame notification after this.
revemanb195f41d2015-11-19 22:16:48300 std::list<FrameCallback> pending_frame_callbacks_;
reveman211cf802017-01-10 00:30:59301
302 // These lists contains the callbacks to notify the client when surface
303 // contents have been presented. These callbacks move to
304 // |presentation_callbacks_| when Commit() is called. Later they are moved to
305 // |swapping_presentation_callbacks_| when the effect of the Commit() is
306 // scheduled to be drawn and then moved to |swapped_presentation_callbacks_|
307 // after receiving VSync parameters update for the previous frame. They fire
308 // at the next VSync parameters update after that.
309 std::list<PresentationCallback> pending_presentation_callbacks_;
revemanb195f41d2015-11-19 22:16:48310
jbaumanf4c3f292016-06-11 00:57:33311 // This is the state that has yet to be committed.
312 State pending_state_;
revemanb195f41d2015-11-19 22:16:48313
jbaumanf4c3f292016-06-11 00:57:33314 // This is the state that has been committed.
315 State state_;
reveman7efa4b02016-01-06 08:29:54316
reveman27fe2642015-11-20 06:33:39317 // The stack of sub-surfaces to take effect when Commit() is called.
318 // Bottom-most sub-surface at the front of the list and top-most sub-surface
319 // at the back.
320 using SubSurfaceEntry = std::pair<Surface*, gfx::Point>;
321 using SubSurfaceEntryList = std::list<SubSurfaceEntry>;
322 SubSurfaceEntryList pending_sub_surfaces_;
Peng Huang583c9dc62017-07-27 23:38:28323 SubSurfaceEntryList sub_surfaces_;
reveman27fe2642015-11-20 06:33:39324
revemanced21f862015-11-24 00:42:49325 // The buffer that is currently set as content of surface.
jbauman45c06862016-06-23 19:35:02326 BufferAttachment current_buffer_;
revemanced21f862015-11-24 00:42:49327
jbauman2fdc0732016-06-07 00:55:36328 // The last resource that was sent to a surface.
Fady Samuel4f7f0fb32017-07-28 15:33:37329 viz::TransferableResource current_resource_;
jbauman2fdc0732016-06-07 00:55:36330
revemanca132dc2017-01-31 22:35:54331 // Whether the last resource that was sent to a surface has an alpha channel.
332 bool current_resource_has_alpha_ = false;
333
reveman27fe2642015-11-20 06:33:39334 // This is true if a call to Commit() as been made but
335 // CommitSurfaceHierarchy() has not yet been called.
Peng Huang76f5fd02017-09-01 00:59:39336 bool needs_commit_surface_ = false;
reveman27fe2642015-11-20 06:33:39337
Peng Huangc51f7aba2017-09-05 16:00:39338 // This is true if UpdateResources() should be called.
339 bool needs_update_resource_ = true;
340
reveman7cadea42016-02-05 20:14:38341 // This is set when the compositing starts and passed to active frame
342 // callbacks when compositing successfully ends.
343 base::TimeTicks last_compositing_start_time_;
344
reveman70baca12016-05-31 20:35:30345 // Cursor providers. Surface does not own the cursor providers.
346 std::set<CursorProvider*> cursor_providers_;
347
revemanb195f41d2015-11-19 22:16:48348 // This can be set to have some functions delegated. E.g. ShellSurface class
349 // can set this to handle Commit() and apply any double buffered state it
350 // maintains.
reveman2d3815d2016-06-26 20:13:25351 SurfaceDelegate* delegate_ = nullptr;
revemanb195f41d2015-11-19 22:16:48352
reveman27fe2642015-11-20 06:33:39353 // Surface observer list. Surface does not own the observers.
354 base::ObserverList<SurfaceObserver, true> observers_;
355
revemanb195f41d2015-11-19 22:16:48356 DISALLOW_COPY_AND_ASSIGN(Surface);
357};
358
359} // namespace exo
360
361#endif // COMPONENTS_EXO_SURFACE_H_