blob: 9ed4b22a1d7f232245313e99387d8271c35f8331 [file] [log] [blame]
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001#ifndef ANDROID_DVR_SERVICES_DISPLAYD_HARDWARE_COMPOSER_H_
2#define ANDROID_DVR_SERVICES_DISPLAYD_HARDWARE_COMPOSER_H_
3
Corey Tabaka2251d822017-04-20 16:04:07 -07004#include <ui/GraphicBuffer.h>
5#include "DisplayHardware/ComposerHal.h"
6#include "hwc_types.h"
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08007
Okan Arikan822b7102017-05-08 13:31:34 -07008#include <dvr/dvr_shared_buffers.h>
Corey Tabaka2251d822017-04-20 16:04:07 -07009#include <hardware/gralloc.h>
10#include <log/log.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080011
12#include <array>
13#include <condition_variable>
14#include <memory>
15#include <mutex>
16#include <thread>
17#include <tuple>
18#include <vector>
19
Okan Arikan6f468c62017-05-31 14:48:30 -070020#include <dvr/dvr_config.h>
Okan Arikan822b7102017-05-08 13:31:34 -070021#include <dvr/dvr_vsync.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080022#include <pdx/file_handle.h>
Corey Tabaka2251d822017-04-20 16:04:07 -070023#include <pdx/rpc/variant.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080024#include <private/dvr/buffer_hub_client.h>
Okan Arikan822b7102017-05-08 13:31:34 -070025#include <private/dvr/shared_buffer_helpers.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080026
27#include "acquired_buffer.h"
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080028#include "display_surface.h"
29
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080030// Hardware composer HAL doesn't define HWC_TRANSFORM_NONE as of this writing.
31#ifndef HWC_TRANSFORM_NONE
32#define HWC_TRANSFORM_NONE static_cast<hwc_transform_t>(0)
33#endif
34
35namespace android {
36namespace dvr {
37
38// Basic display metrics for physical displays. Dimensions and densities are
39// relative to the physical display orientation, which may be different from the
40// logical display orientation exposed to applications.
41struct HWCDisplayMetrics {
42 int width;
43 int height;
44 struct {
45 int x;
46 int y;
47 } dpi;
48 int vsync_period_ns;
49};
50
51// Layer represents the connection between a hardware composer layer and the
52// source supplying buffers for the layer's contents.
53class Layer {
54 public:
Corey Tabaka2c4aea32017-08-31 20:01:15 -070055 Layer() = default;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080056
57 // Sets up the layer to use a display surface as its content source. The Layer
Corey Tabaka2251d822017-04-20 16:04:07 -070058 // automatically handles ACQUIRE/RELEASE phases for the surface's buffer train
59 // every frame.
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080060 //
61 // |blending| receives HWC_BLENDING_* values.
62 // |transform| receives HWC_TRANSFORM_* values.
63 // |composition_type| receives either HWC_FRAMEBUFFER for most layers or
64 // HWC_FRAMEBUFFER_TARGET (unless you know what you are doing).
Corey Tabaka2251d822017-04-20 16:04:07 -070065 // |index| is the index of this surface in the DirectDisplaySurface array.
Corey Tabaka2c4aea32017-08-31 20:01:15 -070066 Layer(const std::shared_ptr<DirectDisplaySurface>& surface,
67 HWC::BlendMode blending, HWC::Transform transform,
68 HWC::Composition composition_type, size_t z_roder);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080069
70 // Sets up the layer to use a direct buffer as its content source. No special
71 // handling of the buffer is performed; responsibility for updating or
72 // changing the buffer each frame is on the caller.
73 //
74 // |blending| receives HWC_BLENDING_* values.
75 // |transform| receives HWC_TRANSFORM_* values.
76 // |composition_type| receives either HWC_FRAMEBUFFER for most layers or
77 // HWC_FRAMEBUFFER_TARGET (unless you know what you are doing).
Corey Tabaka2c4aea32017-08-31 20:01:15 -070078 Layer(const std::shared_ptr<IonBuffer>& buffer, HWC::BlendMode blending,
79 HWC::Transform transform, HWC::Composition composition_type,
80 size_t z_order);
81
82 Layer(Layer&&);
83 Layer& operator=(Layer&&);
84
85 ~Layer();
86
87 // Releases any shared pointers and fence handles held by this instance.
88 void Reset();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080089
90 // Layers that use a direct IonBuffer should call this each frame to update
91 // which buffer will be used for the next PostLayers.
Corey Tabaka2251d822017-04-20 16:04:07 -070092 void UpdateBuffer(const std::shared_ptr<IonBuffer>& buffer);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080093
94 // Sets up the hardware composer layer for the next frame. When the layer is
95 // associated with a display surface, this method automatically ACQUIRES a new
96 // buffer if one is available.
97 void Prepare();
98
99 // After calling prepare, if this frame is to be dropped instead of passing
100 // along to the HWC, call Drop to close the contained fence(s).
101 void Drop();
102
103 // Performs fence bookkeeping after the frame has been posted to hardware
104 // composer.
105 void Finish(int release_fence_fd);
106
107 // Sets the blending for the layer. |blending| receives HWC_BLENDING_* values.
Corey Tabaka2251d822017-04-20 16:04:07 -0700108 void SetBlending(HWC::BlendMode blending);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800109
Corey Tabaka2251d822017-04-20 16:04:07 -0700110 // Sets the z-order of this layer
111 void SetZOrder(size_t z_order);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800112
113 // Gets the current IonBuffer associated with this layer. Ownership of the
114 // buffer DOES NOT pass to the caller and the pointer is not guaranteed to
115 // remain valid across calls to Layer::Setup(), Layer::Prepare(), or
116 // Layer::Reset(). YOU HAVE BEEN WARNED.
117 IonBuffer* GetBuffer();
118
Corey Tabaka2251d822017-04-20 16:04:07 -0700119 HWC::Composition GetCompositionType() const { return composition_type_; }
120 HWC::Layer GetLayerHandle() const { return hardware_composer_layer_; }
121 bool IsLayerSetup() const { return !source_.empty(); }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800122
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800123 int GetSurfaceId() const {
Corey Tabaka2251d822017-04-20 16:04:07 -0700124 int surface_id = -1;
125 pdx::rpc::IfAnyOf<SourceSurface>::Call(
126 &source_, [&surface_id](const SourceSurface& surface_source) {
Corey Tabaka0b485c92017-05-19 12:02:58 -0700127 surface_id = surface_source.GetSurfaceId();
Corey Tabaka2251d822017-04-20 16:04:07 -0700128 });
129 return surface_id;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800130 }
131
Corey Tabaka0b485c92017-05-19 12:02:58 -0700132 int GetBufferId() const {
133 int buffer_id = -1;
134 pdx::rpc::IfAnyOf<SourceSurface>::Call(
135 &source_, [&buffer_id](const SourceSurface& surface_source) {
136 buffer_id = surface_source.GetBufferId();
137 });
138 return buffer_id;
139 }
140
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700141 // Compares Layers by surface id.
142 bool operator<(const Layer& other) const {
143 return GetSurfaceId() < other.GetSurfaceId();
144 }
Corey Tabakab3732f02017-09-16 00:58:54 -0700145 bool operator<(int surface_id) const { return GetSurfaceId() < surface_id; }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800146
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700147 // Sets the composer instance used by all Layer instances.
148 static void SetComposer(Hwc2::Composer* composer) { composer_ = composer; }
149
150 // Sets the display metrics used by all Layer instances.
151 static void SetDisplayMetrics(HWCDisplayMetrics display_metrics) {
152 display_metrics_ = display_metrics;
153 }
154
Steven Thomas6e8f7062017-11-22 14:15:29 -0800155 // Sets the display id used by all Layer instances.
156 static void SetDisplayId(hwc2_display_t display_id) {
157 display_id_ = display_id;
158 }
159
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700160 private:
161 void CommonLayerSetup();
162
163 // Applies all of the settings to this layer using the hwc functions
164 void UpdateLayerSettings();
165
166 // Applies visibility settings that may have changed.
167 void UpdateVisibilitySettings();
168
Corey Tabaka0d07cdd2017-09-28 11:15:50 -0700169 // Checks whether the buffer, given by id, is associated with the given slot
170 // in the HWC buffer cache. If the slot is not associated with the given
171 // buffer the cache is updated to establish the association and the buffer
172 // should be sent to HWC using setLayerBuffer. Returns true if the association
173 // was already established, false if not. A buffer_id of -1 is never
174 // associated and always returns false.
175 bool CheckAndUpdateCachedBuffer(std::size_t slot, int buffer_id);
176
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700177 // Composer instance shared by all instances of Layer. This must be set
178 // whenever a new instance of the Composer is created. This may be set to
179 // nullptr as long as there are no instances of Layer that might need to use
180 // it.
181 static Hwc2::Composer* composer_;
182
183 // Display metrics shared by all instances of Layer. This must be set at least
184 // once during VrFlinger initialization and is expected to remain constant
185 // thereafter.
186 static HWCDisplayMetrics display_metrics_;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800187
Steven Thomas6e8f7062017-11-22 14:15:29 -0800188 // Id of the primary display. Shared by all instances of Layer. This must be
189 // set whenever the primary display id changes. This can be left unset as long
190 // as there are no instances of Layer that might need to use it.
191 static hwc2_display_t display_id_;
192
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800193 // The hardware composer layer and metrics to use during the prepare cycle.
Corey Tabaka2251d822017-04-20 16:04:07 -0700194 hwc2_layer_t hardware_composer_layer_ = 0;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800195
196 // Layer properties used to setup the hardware composer layer during the
197 // Prepare phase.
Corey Tabaka2251d822017-04-20 16:04:07 -0700198 size_t z_order_ = 0;
199 HWC::BlendMode blending_ = HWC::BlendMode::None;
200 HWC::Transform transform_ = HWC::Transform::None;
201 HWC::Composition composition_type_ = HWC::Composition::Invalid;
202 HWC::Composition target_composition_type_ = HWC::Composition::Device;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800203
Corey Tabaka2251d822017-04-20 16:04:07 -0700204 // State when the layer is connected to a surface. Provides the same interface
205 // as SourceBuffer to simplify internal use by Layer.
206 struct SourceSurface {
207 std::shared_ptr<DirectDisplaySurface> surface;
208 AcquiredBuffer acquired_buffer;
209 pdx::LocalHandle release_fence;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800210
Corey Tabaka2251d822017-04-20 16:04:07 -0700211 SourceSurface(const std::shared_ptr<DirectDisplaySurface>& surface)
212 : surface(surface) {}
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800213
Corey Tabaka2251d822017-04-20 16:04:07 -0700214 // Attempts to acquire a new buffer from the surface and return a tuple with
215 // width, height, buffer handle, and fence. If a new buffer is not available
216 // the previous buffer is returned or an empty value if no buffer has ever
217 // been posted. When a new buffer is acquired the previous buffer's release
218 // fence is passed out automatically.
Corey Tabaka0d07cdd2017-09-28 11:15:50 -0700219 std::tuple<int, int, int, sp<GraphicBuffer>, pdx::LocalHandle, std::size_t>
220 Acquire() {
Corey Tabaka2251d822017-04-20 16:04:07 -0700221 if (surface->IsBufferAvailable()) {
222 acquired_buffer.Release(std::move(release_fence));
223 acquired_buffer = surface->AcquireCurrentBuffer();
224 ATRACE_ASYNC_END("BufferPost", acquired_buffer.buffer()->id());
225 }
226 if (!acquired_buffer.IsEmpty()) {
Corey Tabaka0d07cdd2017-09-28 11:15:50 -0700227 return std::make_tuple(
228 acquired_buffer.buffer()->width(),
229 acquired_buffer.buffer()->height(), acquired_buffer.buffer()->id(),
230 acquired_buffer.buffer()->buffer()->buffer(),
231 acquired_buffer.ClaimAcquireFence(), acquired_buffer.slot());
Corey Tabaka2251d822017-04-20 16:04:07 -0700232 } else {
Corey Tabaka0d07cdd2017-09-28 11:15:50 -0700233 return std::make_tuple(0, 0, -1, nullptr, pdx::LocalHandle{}, 0);
Corey Tabaka2251d822017-04-20 16:04:07 -0700234 }
235 }
236
237 void Finish(pdx::LocalHandle fence) { release_fence = std::move(fence); }
238
239 // Gets a pointer to the current acquired buffer or returns nullptr if there
240 // isn't one.
241 IonBuffer* GetBuffer() {
242 if (acquired_buffer.IsAvailable())
243 return acquired_buffer.buffer()->buffer();
244 else
245 return nullptr;
246 }
247
248 // Returns the surface id of the surface.
Corey Tabaka0b485c92017-05-19 12:02:58 -0700249 int GetSurfaceId() const { return surface->surface_id(); }
250
251 // Returns the buffer id for the current buffer.
252 int GetBufferId() const {
253 if (acquired_buffer.IsAvailable())
254 return acquired_buffer.buffer()->id();
255 else
256 return -1;
257 }
Corey Tabaka2251d822017-04-20 16:04:07 -0700258 };
259
260 // State when the layer is connected to a buffer. Provides the same interface
261 // as SourceSurface to simplify internal use by Layer.
262 struct SourceBuffer {
263 std::shared_ptr<IonBuffer> buffer;
264
Corey Tabaka0d07cdd2017-09-28 11:15:50 -0700265 std::tuple<int, int, int, sp<GraphicBuffer>, pdx::LocalHandle, std::size_t>
266 Acquire() {
Corey Tabaka2251d822017-04-20 16:04:07 -0700267 if (buffer)
Corey Tabaka0d07cdd2017-09-28 11:15:50 -0700268 return std::make_tuple(buffer->width(), buffer->height(), -1,
269 buffer->buffer(), pdx::LocalHandle{}, 0);
Corey Tabaka2251d822017-04-20 16:04:07 -0700270 else
Corey Tabaka0d07cdd2017-09-28 11:15:50 -0700271 return std::make_tuple(0, 0, -1, nullptr, pdx::LocalHandle{}, 0);
Corey Tabaka2251d822017-04-20 16:04:07 -0700272 }
273
274 void Finish(pdx::LocalHandle /*fence*/) {}
275
276 IonBuffer* GetBuffer() { return buffer.get(); }
277
278 int GetSurfaceId() const { return -1; }
Corey Tabaka0b485c92017-05-19 12:02:58 -0700279 int GetBufferId() const { return -1; }
Corey Tabaka2251d822017-04-20 16:04:07 -0700280 };
281
282 // The underlying hardware composer layer is supplied buffers either from a
283 // surface buffer train or from a buffer directly.
284 pdx::rpc::Variant<SourceSurface, SourceBuffer> source_;
285
286 pdx::LocalHandle acquire_fence_;
287 bool surface_rect_functions_applied_ = false;
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700288 bool pending_visibility_settings_ = true;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800289
Corey Tabaka0d07cdd2017-09-28 11:15:50 -0700290 // Map of buffer slot assignments that have already been established with HWC:
291 // slot -> buffer_id. When this map contains a matching slot and buffer_id the
292 // buffer argument to setLayerBuffer may be nullptr to avoid the cost of
293 // importing a buffer HWC already knows about.
294 std::map<std::size_t, int> cached_buffer_map_;
295
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800296 Layer(const Layer&) = delete;
297 void operator=(const Layer&) = delete;
298};
299
300// HardwareComposer encapsulates the hardware composer HAL, exposing a
301// simplified API to post buffers to the display.
Steven Thomas050b2c82017-03-06 11:45:16 -0800302//
303// HardwareComposer is accessed by both the vr flinger dispatcher thread and the
304// surface flinger main thread, in addition to internally running a separate
305// thread for compositing/EDS and posting layers to the HAL. When changing how
306// variables are used or adding new state think carefully about which threads
307// will access the state and whether it needs to be synchronized.
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800308class HardwareComposer {
309 public:
310 // Type for vsync callback.
Steven Thomas6e8f7062017-11-22 14:15:29 -0800311 using VSyncCallback = std::function<void(int64_t, int64_t, uint32_t)>;
Corey Tabaka2251d822017-04-20 16:04:07 -0700312 using RequestDisplayCallback = std::function<void(bool)>;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800313
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800314 HardwareComposer();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800315 ~HardwareComposer();
316
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700317 bool Initialize(Hwc2::Composer* composer,
Steven Thomas6e8f7062017-11-22 14:15:29 -0800318 hwc2_display_t primary_display_id,
Steven Thomasd7f49c52017-07-26 18:48:28 -0700319 RequestDisplayCallback request_display_callback);
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800320
321 bool IsInitialized() const { return initialized_; }
322
Steven Thomas050b2c82017-03-06 11:45:16 -0800323 // Start the post thread if there's work to do (i.e. visible layers). This
324 // should only be called from surface flinger's main thread.
325 void Enable();
326 // Pause the post thread, blocking until the post thread has signaled that
327 // it's paused. This should only be called from surface flinger's main thread.
328 void Disable();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800329
330 // Get the HMD display metrics for the current display.
Corey Tabaka2251d822017-04-20 16:04:07 -0700331 display::Metrics GetHmdDisplayMetrics() const;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800332
Corey Tabaka2251d822017-04-20 16:04:07 -0700333 std::string Dump();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800334
335 void SetVSyncCallback(VSyncCallback callback);
336
337 // Metrics of the logical display, which is always landscape.
338 int DisplayWidth() const { return display_metrics_.width; }
339 int DisplayHeight() const { return display_metrics_.height; }
340 HWCDisplayMetrics display_metrics() const { return display_metrics_; }
341
342 // Metrics of the native display, which depends on the specific hardware
343 // implementation of the display.
344 HWCDisplayMetrics native_display_metrics() const {
345 return native_display_metrics_;
346 }
347
Corey Tabaka2251d822017-04-20 16:04:07 -0700348 // Sets the display surfaces to compose the hardware layer stack.
Steven Thomas050b2c82017-03-06 11:45:16 -0800349 void SetDisplaySurfaces(
Corey Tabaka2251d822017-04-20 16:04:07 -0700350 std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800351
John Bates954796e2017-05-11 11:00:31 -0700352 int OnNewGlobalBuffer(DvrGlobalBufferKey key, IonBuffer& ion_buffer);
353 void OnDeletedGlobalBuffer(DvrGlobalBufferKey key);
354
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800355 private:
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700356 HWC::Error GetDisplayAttribute(Hwc2::Composer* composer,
357 hwc2_display_t display, hwc2_config_t config,
Steven Thomasd7f49c52017-07-26 18:48:28 -0700358 hwc2_attribute_t attributes,
359 int32_t* out_value) const;
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700360 HWC::Error GetDisplayMetrics(Hwc2::Composer* composer, hwc2_display_t display,
Steven Thomasd7f49c52017-07-26 18:48:28 -0700361 hwc2_config_t config,
362 HWCDisplayMetrics* out_metrics) const;
363
364 HWC::Error EnableVsync(bool enabled);
Corey Tabaka7024b8f2017-08-22 11:59:15 -0700365 HWC::Error SetPowerMode(bool active);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800366
367 class ComposerCallback : public Hwc2::IComposerCallback {
368 public:
Corey Tabakab3732f02017-09-16 00:58:54 -0700369 ComposerCallback() = default;
Steven Thomasd7f49c52017-07-26 18:48:28 -0700370 hardware::Return<void> onHotplug(Hwc2::Display display,
371 Connection conn) override;
372 hardware::Return<void> onRefresh(Hwc2::Display display) override;
373 hardware::Return<void> onVsync(Hwc2::Display display,
374 int64_t timestamp) override;
Corey Tabakab3732f02017-09-16 00:58:54 -0700375
Steven Thomas6e8f7062017-11-22 14:15:29 -0800376 bool HasDisplayId() { return has_display_id_; }
377 hwc2_display_t GetDisplayId() { return display_id_; }
378 pdx::Status<int64_t> GetVsyncTime();
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700379
Steven Thomasd7f49c52017-07-26 18:48:28 -0700380 private:
381 std::mutex vsync_mutex_;
Steven Thomas6e8f7062017-11-22 14:15:29 -0800382 bool has_display_id_ = false;
383 hwc2_display_t display_id_;
384 pdx::LocalHandle driver_vsync_event_fd_;
385 int64_t callback_vsync_timestamp_{0};
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800386 };
387
Corey Tabaka2251d822017-04-20 16:04:07 -0700388 HWC::Error Validate(hwc2_display_t display);
389 HWC::Error Present(hwc2_display_t display);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800390
391 void SetBacklightBrightness(int brightness);
392
Corey Tabaka2251d822017-04-20 16:04:07 -0700393 void PostLayers();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800394 void PostThread();
395
Corey Tabaka2251d822017-04-20 16:04:07 -0700396 // The post thread has two controlling states:
397 // 1. Idle: no work to do (no visible surfaces).
398 // 2. Suspended: explicitly halted (system is not in VR mode).
399 // When either #1 or #2 is true then the post thread is quiescent, otherwise
400 // it is active.
401 using PostThreadStateType = uint32_t;
402 struct PostThreadState {
403 enum : PostThreadStateType {
404 Active = 0,
405 Idle = (1 << 0),
406 Suspended = (1 << 1),
407 Quit = (1 << 2),
408 };
409 };
410
411 void UpdatePostThreadState(uint32_t state, bool suspend);
412
Steven Thomas050b2c82017-03-06 11:45:16 -0800413 // Blocks until either event_fd becomes readable, or we're interrupted by a
Steven Thomasd7f49c52017-07-26 18:48:28 -0700414 // control thread, or timeout_ms is reached before any events occur. Any
415 // errors are returned as negative errno values, with -ETIMEDOUT returned in
416 // the case of a timeout. If we're interrupted, kPostThreadInterrupted will be
417 // returned.
Corey Tabaka2251d822017-04-20 16:04:07 -0700418 int PostThreadPollInterruptible(const pdx::LocalHandle& event_fd,
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700419 int requested_events, int timeout_ms);
Steven Thomas050b2c82017-03-06 11:45:16 -0800420
Steven Thomasd7f49c52017-07-26 18:48:28 -0700421 // WaitForVSync and SleepUntil are blocking calls made on the post thread that
422 // can be interrupted by a control thread. If interrupted, these calls return
423 // kPostThreadInterrupted.
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800424 int ReadWaitPPState();
Corey Tabakab3732f02017-09-16 00:58:54 -0700425 pdx::Status<int64_t> WaitForVSync();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800426 int SleepUntil(int64_t wakeup_timestamp);
427
Corey Tabaka2251d822017-04-20 16:04:07 -0700428 // Reconfigures the layer stack if the display surfaces changed since the last
429 // frame. Called only from the post thread.
Steven Thomas050b2c82017-03-06 11:45:16 -0800430 bool UpdateLayerConfig();
Steven Thomas050b2c82017-03-06 11:45:16 -0800431
432 // Called on the post thread when the post thread is resumed.
433 void OnPostThreadResumed();
434 // Called on the post thread when the post thread is paused or quits.
435 void OnPostThreadPaused();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800436
John Bates954796e2017-05-11 11:00:31 -0700437 // Map the given shared memory buffer to our broadcast ring to track updates
438 // to the config parameters.
439 int MapConfigBuffer(IonBuffer& ion_buffer);
440 void ConfigBufferDeleted();
441 // Poll for config udpates.
442 void UpdateConfigBuffer();
443
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800444 bool initialized_;
Corey Tabaka7024b8f2017-08-22 11:59:15 -0700445 bool is_standalone_device_;
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800446
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700447 std::unique_ptr<Hwc2::Composer> composer_;
448 sp<ComposerCallback> composer_callback_;
Corey Tabaka2251d822017-04-20 16:04:07 -0700449 RequestDisplayCallback request_display_callback_;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800450
451 // Display metrics of the physical display.
452 HWCDisplayMetrics native_display_metrics_;
453 // Display metrics of the logical display, adjusted so that orientation is
454 // landscape.
455 HWCDisplayMetrics display_metrics_;
456 // Transform required to get from native to logical display orientation.
Corey Tabaka2251d822017-04-20 16:04:07 -0700457 HWC::Transform display_transform_ = HWC::Transform::None;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800458
Corey Tabaka2251d822017-04-20 16:04:07 -0700459 // Pending surface list. Set by the display service when DirectSurfaces are
460 // added, removed, or change visibility. Written by the message dispatch
461 // thread and read by the post thread.
462 std::vector<std::shared_ptr<DirectDisplaySurface>> pending_surfaces_;
Steven Thomas050b2c82017-03-06 11:45:16 -0800463
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700464 // Layer set for handling buffer flow into hardware composer layers. This
465 // vector must be sorted by surface_id in ascending order.
466 std::vector<Layer> layers_;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800467
468 // Handler to hook vsync events outside of this class.
469 VSyncCallback vsync_callback_;
470
Steven Thomas282a5ed2017-02-07 18:07:01 -0800471 // The layer posting thread. This thread wakes up a short time before vsync to
Corey Tabaka2251d822017-04-20 16:04:07 -0700472 // hand buffers to hardware composer.
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800473 std::thread post_thread_;
474
Corey Tabaka2251d822017-04-20 16:04:07 -0700475 // Post thread state machine and synchronization primitives.
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700476 PostThreadStateType post_thread_state_{PostThreadState::Idle |
477 PostThreadState::Suspended};
Corey Tabaka2251d822017-04-20 16:04:07 -0700478 std::atomic<bool> post_thread_quiescent_{true};
479 bool post_thread_resumed_{false};
480 pdx::LocalHandle post_thread_event_fd_;
481 std::mutex post_thread_mutex_;
482 std::condition_variable post_thread_wait_;
483 std::condition_variable post_thread_ready_;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800484
485 // Backlight LED brightness sysfs node.
486 pdx::LocalHandle backlight_brightness_fd_;
487
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800488 // VSync sleep timerfd.
489 pdx::LocalHandle vsync_sleep_timer_fd_;
490
491 // The timestamp of the last vsync.
Corey Tabaka2251d822017-04-20 16:04:07 -0700492 int64_t last_vsync_timestamp_ = 0;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800493
Corey Tabakab3732f02017-09-16 00:58:54 -0700494 // The number of vsync intervals to predict since the last vsync.
495 int vsync_prediction_interval_ = 1;
496
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800497 // Vsync count since display on.
Corey Tabaka2251d822017-04-20 16:04:07 -0700498 uint32_t vsync_count_ = 0;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800499
500 // Counter tracking the number of skipped frames.
Corey Tabaka2251d822017-04-20 16:04:07 -0700501 int frame_skip_count_ = 0;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800502
503 // Fd array for tracking retire fences that are returned by hwc. This allows
504 // us to detect when the display driver begins queuing frames.
505 std::vector<pdx::LocalHandle> retire_fence_fds_;
506
Okan Arikan822b7102017-05-08 13:31:34 -0700507 // If we are publishing vsync data, we will put it here.
508 std::unique_ptr<CPUMappedBroadcastRing<DvrVsyncRing>> vsync_ring_;
Steven Thomas050b2c82017-03-06 11:45:16 -0800509
John Bates954796e2017-05-11 11:00:31 -0700510 // Broadcast ring for receiving config data from the DisplayManager.
Okan Arikan6f468c62017-05-31 14:48:30 -0700511 DvrConfigRing shared_config_ring_;
John Bates954796e2017-05-11 11:00:31 -0700512 uint32_t shared_config_ring_sequence_{0};
513 // Config buffer for reading from the post thread.
Okan Arikan6f468c62017-05-31 14:48:30 -0700514 DvrConfig post_thread_config_;
John Bates954796e2017-05-11 11:00:31 -0700515 std::mutex shared_config_mutex_;
516
Steven Thomas050b2c82017-03-06 11:45:16 -0800517 static constexpr int kPostThreadInterrupted = 1;
518
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800519 HardwareComposer(const HardwareComposer&) = delete;
520 void operator=(const HardwareComposer&) = delete;
521};
522
523} // namespace dvr
524} // namespace android
525
526#endif // ANDROID_DVR_SERVICES_DISPLAYD_HARDWARE_COMPOSER_H_