blob: fc0efeeeb4b53fca6a0ddd7f40bb16407bc98f71 [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 Tabaka2251d822017-04-20 16:04:07 -070055 Layer() {}
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080056
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080057 // Releases any shared pointers and fence handles held by this instance.
58 void Reset();
59
60 // Sets up the layer to use a display surface as its content source. The Layer
Corey Tabaka2251d822017-04-20 16:04:07 -070061 // automatically handles ACQUIRE/RELEASE phases for the surface's buffer train
62 // every frame.
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080063 //
64 // |blending| receives HWC_BLENDING_* values.
65 // |transform| receives HWC_TRANSFORM_* values.
66 // |composition_type| receives either HWC_FRAMEBUFFER for most layers or
67 // HWC_FRAMEBUFFER_TARGET (unless you know what you are doing).
Corey Tabaka2251d822017-04-20 16:04:07 -070068 // |index| is the index of this surface in the DirectDisplaySurface array.
69 void Setup(const std::shared_ptr<DirectDisplaySurface>& surface,
Steven Thomas94e35b92017-07-26 18:48:28 -070070 const HWCDisplayMetrics& display_metrics, Hwc2::Composer* hidl,
Corey Tabaka2251d822017-04-20 16:04:07 -070071 HWC::BlendMode blending, HWC::Transform transform,
72 HWC::Composition composition_type, size_t z_roder);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080073
74 // Sets up the layer to use a direct buffer as its content source. No special
75 // handling of the buffer is performed; responsibility for updating or
76 // changing the buffer each frame is on the caller.
77 //
78 // |blending| receives HWC_BLENDING_* values.
79 // |transform| receives HWC_TRANSFORM_* values.
80 // |composition_type| receives either HWC_FRAMEBUFFER for most layers or
81 // HWC_FRAMEBUFFER_TARGET (unless you know what you are doing).
Steven Thomas94e35b92017-07-26 18:48:28 -070082 void Setup(const std::shared_ptr<IonBuffer>& buffer,
83 const HWCDisplayMetrics& display_metrics, Hwc2::Composer* hidl,
84 HWC::BlendMode blending, HWC::Transform transform,
85 HWC::Composition composition_type, size_t z_order);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080086
87 // Layers that use a direct IonBuffer should call this each frame to update
88 // which buffer will be used for the next PostLayers.
Corey Tabaka2251d822017-04-20 16:04:07 -070089 void UpdateBuffer(const std::shared_ptr<IonBuffer>& buffer);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080090
91 // Sets up the hardware composer layer for the next frame. When the layer is
92 // associated with a display surface, this method automatically ACQUIRES a new
93 // buffer if one is available.
94 void Prepare();
95
96 // After calling prepare, if this frame is to be dropped instead of passing
97 // along to the HWC, call Drop to close the contained fence(s).
98 void Drop();
99
100 // Performs fence bookkeeping after the frame has been posted to hardware
101 // composer.
102 void Finish(int release_fence_fd);
103
104 // Sets the blending for the layer. |blending| receives HWC_BLENDING_* values.
Corey Tabaka2251d822017-04-20 16:04:07 -0700105 void SetBlending(HWC::BlendMode blending);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800106
Corey Tabaka2251d822017-04-20 16:04:07 -0700107 // Sets the z-order of this layer
108 void SetZOrder(size_t z_order);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800109
110 // Gets the current IonBuffer associated with this layer. Ownership of the
111 // buffer DOES NOT pass to the caller and the pointer is not guaranteed to
112 // remain valid across calls to Layer::Setup(), Layer::Prepare(), or
113 // Layer::Reset(). YOU HAVE BEEN WARNED.
114 IonBuffer* GetBuffer();
115
Corey Tabaka2251d822017-04-20 16:04:07 -0700116 HWC::Composition GetCompositionType() const { return composition_type_; }
117 HWC::Layer GetLayerHandle() const { return hardware_composer_layer_; }
118 bool IsLayerSetup() const { return !source_.empty(); }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800119
120 // Applies all of the settings to this layer using the hwc functions
Steven Thomas94e35b92017-07-26 18:48:28 -0700121 void UpdateLayerSettings(const HWCDisplayMetrics& display_metrics);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800122
123 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
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800141 private:
Steven Thomas94e35b92017-07-26 18:48:28 -0700142 void CommonLayerSetup(const HWCDisplayMetrics& display_metrics);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800143
Steven Thomas94e35b92017-07-26 18:48:28 -0700144 Hwc2::Composer* hidl_ = nullptr;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800145
146 // The hardware composer layer and metrics to use during the prepare cycle.
Corey Tabaka2251d822017-04-20 16:04:07 -0700147 hwc2_layer_t hardware_composer_layer_ = 0;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800148
149 // Layer properties used to setup the hardware composer layer during the
150 // Prepare phase.
Corey Tabaka2251d822017-04-20 16:04:07 -0700151 size_t z_order_ = 0;
152 HWC::BlendMode blending_ = HWC::BlendMode::None;
153 HWC::Transform transform_ = HWC::Transform::None;
154 HWC::Composition composition_type_ = HWC::Composition::Invalid;
155 HWC::Composition target_composition_type_ = HWC::Composition::Device;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800156
Corey Tabaka2251d822017-04-20 16:04:07 -0700157 // State when the layer is connected to a surface. Provides the same interface
158 // as SourceBuffer to simplify internal use by Layer.
159 struct SourceSurface {
160 std::shared_ptr<DirectDisplaySurface> surface;
161 AcquiredBuffer acquired_buffer;
162 pdx::LocalHandle release_fence;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800163
Corey Tabaka2251d822017-04-20 16:04:07 -0700164 SourceSurface(const std::shared_ptr<DirectDisplaySurface>& surface)
165 : surface(surface) {}
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800166
Corey Tabaka2251d822017-04-20 16:04:07 -0700167 // Attempts to acquire a new buffer from the surface and return a tuple with
168 // width, height, buffer handle, and fence. If a new buffer is not available
169 // the previous buffer is returned or an empty value if no buffer has ever
170 // been posted. When a new buffer is acquired the previous buffer's release
171 // fence is passed out automatically.
172 std::tuple<int, int, sp<GraphicBuffer>, pdx::LocalHandle> Acquire() {
173 if (surface->IsBufferAvailable()) {
174 acquired_buffer.Release(std::move(release_fence));
175 acquired_buffer = surface->AcquireCurrentBuffer();
176 ATRACE_ASYNC_END("BufferPost", acquired_buffer.buffer()->id());
177 }
178 if (!acquired_buffer.IsEmpty()) {
179 return std::make_tuple(acquired_buffer.buffer()->width(),
180 acquired_buffer.buffer()->height(),
181 acquired_buffer.buffer()->buffer()->buffer(),
182 acquired_buffer.ClaimAcquireFence());
183 } else {
184 return std::make_tuple(0, 0, nullptr, pdx::LocalHandle{});
185 }
186 }
187
188 void Finish(pdx::LocalHandle fence) { release_fence = std::move(fence); }
189
190 // Gets a pointer to the current acquired buffer or returns nullptr if there
191 // isn't one.
192 IonBuffer* GetBuffer() {
193 if (acquired_buffer.IsAvailable())
194 return acquired_buffer.buffer()->buffer();
195 else
196 return nullptr;
197 }
198
199 // Returns the surface id of the surface.
Corey Tabaka0b485c92017-05-19 12:02:58 -0700200 int GetSurfaceId() const { return surface->surface_id(); }
201
202 // Returns the buffer id for the current buffer.
203 int GetBufferId() const {
204 if (acquired_buffer.IsAvailable())
205 return acquired_buffer.buffer()->id();
206 else
207 return -1;
208 }
Corey Tabaka2251d822017-04-20 16:04:07 -0700209 };
210
211 // State when the layer is connected to a buffer. Provides the same interface
212 // as SourceSurface to simplify internal use by Layer.
213 struct SourceBuffer {
214 std::shared_ptr<IonBuffer> buffer;
215
216 std::tuple<int, int, sp<GraphicBuffer>, pdx::LocalHandle> Acquire() {
217 if (buffer)
218 return std::make_tuple(buffer->width(), buffer->height(),
219 buffer->buffer(), pdx::LocalHandle{});
220 else
221 return std::make_tuple(0, 0, nullptr, pdx::LocalHandle{});
222 }
223
224 void Finish(pdx::LocalHandle /*fence*/) {}
225
226 IonBuffer* GetBuffer() { return buffer.get(); }
227
228 int GetSurfaceId() const { return -1; }
Corey Tabaka0b485c92017-05-19 12:02:58 -0700229 int GetBufferId() const { return -1; }
Corey Tabaka2251d822017-04-20 16:04:07 -0700230 };
231
232 // The underlying hardware composer layer is supplied buffers either from a
233 // surface buffer train or from a buffer directly.
234 pdx::rpc::Variant<SourceSurface, SourceBuffer> source_;
235
236 pdx::LocalHandle acquire_fence_;
237 bool surface_rect_functions_applied_ = false;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800238
239 Layer(const Layer&) = delete;
240 void operator=(const Layer&) = delete;
241};
242
243// HardwareComposer encapsulates the hardware composer HAL, exposing a
244// simplified API to post buffers to the display.
Steven Thomas050b2c82017-03-06 11:45:16 -0800245//
246// HardwareComposer is accessed by both the vr flinger dispatcher thread and the
247// surface flinger main thread, in addition to internally running a separate
248// thread for compositing/EDS and posting layers to the HAL. When changing how
249// variables are used or adding new state think carefully about which threads
250// will access the state and whether it needs to be synchronized.
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800251class HardwareComposer {
252 public:
253 // Type for vsync callback.
254 using VSyncCallback = std::function<void(int, int64_t, int64_t, uint32_t)>;
Corey Tabaka2251d822017-04-20 16:04:07 -0700255 using RequestDisplayCallback = std::function<void(bool)>;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800256
257 // Since there is no universal way to query the number of hardware layers,
258 // just set it to 4 for now.
Corey Tabaka2251d822017-04-20 16:04:07 -0700259 static constexpr size_t kMaxHardwareLayers = 4;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800260
261 HardwareComposer();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800262 ~HardwareComposer();
263
Steven Thomas94e35b92017-07-26 18:48:28 -0700264 bool Initialize(Hwc2::Composer* hidl,
265 RequestDisplayCallback request_display_callback);
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800266
267 bool IsInitialized() const { return initialized_; }
268
Steven Thomas050b2c82017-03-06 11:45:16 -0800269 // Start the post thread if there's work to do (i.e. visible layers). This
270 // should only be called from surface flinger's main thread.
271 void Enable();
272 // Pause the post thread, blocking until the post thread has signaled that
273 // it's paused. This should only be called from surface flinger's main thread.
274 void Disable();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800275
276 // Get the HMD display metrics for the current display.
Corey Tabaka2251d822017-04-20 16:04:07 -0700277 display::Metrics GetHmdDisplayMetrics() const;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800278
Corey Tabaka2251d822017-04-20 16:04:07 -0700279 std::string Dump();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800280
281 void SetVSyncCallback(VSyncCallback callback);
282
283 // Metrics of the logical display, which is always landscape.
284 int DisplayWidth() const { return display_metrics_.width; }
285 int DisplayHeight() const { return display_metrics_.height; }
286 HWCDisplayMetrics display_metrics() const { return display_metrics_; }
287
288 // Metrics of the native display, which depends on the specific hardware
289 // implementation of the display.
290 HWCDisplayMetrics native_display_metrics() const {
291 return native_display_metrics_;
292 }
293
Corey Tabaka2251d822017-04-20 16:04:07 -0700294 // Sets the display surfaces to compose the hardware layer stack.
Steven Thomas050b2c82017-03-06 11:45:16 -0800295 void SetDisplaySurfaces(
Corey Tabaka2251d822017-04-20 16:04:07 -0700296 std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800297
John Bates954796e2017-05-11 11:00:31 -0700298 int OnNewGlobalBuffer(DvrGlobalBufferKey key, IonBuffer& ion_buffer);
299 void OnDeletedGlobalBuffer(DvrGlobalBufferKey key);
300
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800301 private:
Steven Thomas94e35b92017-07-26 18:48:28 -0700302 HWC::Error GetDisplayAttribute(Hwc2::Composer* hidl, hwc2_display_t display,
303 hwc2_config_t config,
304 hwc2_attribute_t attributes,
305 int32_t* out_value) const;
306 HWC::Error GetDisplayMetrics(Hwc2::Composer* hidl, hwc2_display_t display,
307 hwc2_config_t config,
308 HWCDisplayMetrics* out_metrics) const;
309
310 HWC::Error EnableVsync(bool enabled);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800311
312 class ComposerCallback : public Hwc2::IComposerCallback {
313 public:
Steven Thomas94e35b92017-07-26 18:48:28 -0700314 ComposerCallback();
315 hardware::Return<void> onHotplug(Hwc2::Display display,
316 Connection conn) override;
317 hardware::Return<void> onRefresh(Hwc2::Display display) override;
318 hardware::Return<void> onVsync(Hwc2::Display display,
319 int64_t timestamp) override;
320 const pdx::LocalHandle& GetVsyncEventFd() const;
321 int64_t GetVsyncTime();
322 private:
323 std::mutex vsync_mutex_;
324 pdx::LocalHandle vsync_event_fd_;
325 int64_t vsync_time_ = -1;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800326 };
327
Corey Tabaka2251d822017-04-20 16:04:07 -0700328 HWC::Error Validate(hwc2_display_t display);
329 HWC::Error Present(hwc2_display_t display);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800330
331 void SetBacklightBrightness(int brightness);
332
Corey Tabaka2251d822017-04-20 16:04:07 -0700333 void PostLayers();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800334 void PostThread();
335
Corey Tabaka2251d822017-04-20 16:04:07 -0700336 // The post thread has two controlling states:
337 // 1. Idle: no work to do (no visible surfaces).
338 // 2. Suspended: explicitly halted (system is not in VR mode).
339 // When either #1 or #2 is true then the post thread is quiescent, otherwise
340 // it is active.
341 using PostThreadStateType = uint32_t;
342 struct PostThreadState {
343 enum : PostThreadStateType {
344 Active = 0,
345 Idle = (1 << 0),
346 Suspended = (1 << 1),
347 Quit = (1 << 2),
348 };
349 };
350
351 void UpdatePostThreadState(uint32_t state, bool suspend);
352
Steven Thomas050b2c82017-03-06 11:45:16 -0800353 // Blocks until either event_fd becomes readable, or we're interrupted by a
Steven Thomas94e35b92017-07-26 18:48:28 -0700354 // control thread, or timeout_ms is reached before any events occur. Any
355 // errors are returned as negative errno values, with -ETIMEDOUT returned in
356 // the case of a timeout. If we're interrupted, kPostThreadInterrupted will be
357 // returned.
Corey Tabaka2251d822017-04-20 16:04:07 -0700358 int PostThreadPollInterruptible(const pdx::LocalHandle& event_fd,
Steven Thomas94e35b92017-07-26 18:48:28 -0700359 int requested_events,
360 int timeout_ms);
Steven Thomas050b2c82017-03-06 11:45:16 -0800361
Steven Thomas94e35b92017-07-26 18:48:28 -0700362 // WaitForVSync and SleepUntil are blocking calls made on the post thread that
363 // can be interrupted by a control thread. If interrupted, these calls return
364 // kPostThreadInterrupted.
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800365 int ReadWaitPPState();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800366 int WaitForVSync(int64_t* timestamp);
367 int SleepUntil(int64_t wakeup_timestamp);
368
369 bool IsFramePendingInDriver() { return ReadWaitPPState() == 1; }
370
Corey Tabaka2251d822017-04-20 16:04:07 -0700371 // Reconfigures the layer stack if the display surfaces changed since the last
372 // frame. Called only from the post thread.
Steven Thomas050b2c82017-03-06 11:45:16 -0800373 bool UpdateLayerConfig();
Steven Thomas050b2c82017-03-06 11:45:16 -0800374
375 // Called on the post thread when the post thread is resumed.
376 void OnPostThreadResumed();
377 // Called on the post thread when the post thread is paused or quits.
378 void OnPostThreadPaused();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800379
John Bates954796e2017-05-11 11:00:31 -0700380 // Map the given shared memory buffer to our broadcast ring to track updates
381 // to the config parameters.
382 int MapConfigBuffer(IonBuffer& ion_buffer);
383 void ConfigBufferDeleted();
384 // Poll for config udpates.
385 void UpdateConfigBuffer();
386
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800387 bool initialized_;
388
Steven Thomas94e35b92017-07-26 18:48:28 -0700389 std::unique_ptr<Hwc2::Composer> hidl_;
390 sp<ComposerCallback> hidl_callback_;
Corey Tabaka2251d822017-04-20 16:04:07 -0700391 RequestDisplayCallback request_display_callback_;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800392
393 // Display metrics of the physical display.
394 HWCDisplayMetrics native_display_metrics_;
395 // Display metrics of the logical display, adjusted so that orientation is
396 // landscape.
397 HWCDisplayMetrics display_metrics_;
398 // Transform required to get from native to logical display orientation.
Corey Tabaka2251d822017-04-20 16:04:07 -0700399 HWC::Transform display_transform_ = HWC::Transform::None;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800400
Corey Tabaka2251d822017-04-20 16:04:07 -0700401 // Pending surface list. Set by the display service when DirectSurfaces are
402 // added, removed, or change visibility. Written by the message dispatch
403 // thread and read by the post thread.
404 std::vector<std::shared_ptr<DirectDisplaySurface>> pending_surfaces_;
Steven Thomas050b2c82017-03-06 11:45:16 -0800405
406 // The surfaces displayed by the post thread. Used exclusively by the post
407 // thread.
Corey Tabaka2251d822017-04-20 16:04:07 -0700408 std::vector<std::shared_ptr<DirectDisplaySurface>> display_surfaces_;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800409
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800410 // Layer array for handling buffer flow into hardware composer layers.
Corey Tabaka2251d822017-04-20 16:04:07 -0700411 std::array<Layer, kMaxHardwareLayers> layers_;
412 size_t active_layer_count_ = 0;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800413
414 // Handler to hook vsync events outside of this class.
415 VSyncCallback vsync_callback_;
416
Steven Thomas282a5ed2017-02-07 18:07:01 -0800417 // The layer posting thread. This thread wakes up a short time before vsync to
Corey Tabaka2251d822017-04-20 16:04:07 -0700418 // hand buffers to hardware composer.
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800419 std::thread post_thread_;
420
Corey Tabaka2251d822017-04-20 16:04:07 -0700421 // Post thread state machine and synchronization primitives.
Steven Thomas94e35b92017-07-26 18:48:28 -0700422 PostThreadStateType post_thread_state_{
423 PostThreadState::Idle | PostThreadState::Suspended};
Corey Tabaka2251d822017-04-20 16:04:07 -0700424 std::atomic<bool> post_thread_quiescent_{true};
425 bool post_thread_resumed_{false};
426 pdx::LocalHandle post_thread_event_fd_;
427 std::mutex post_thread_mutex_;
428 std::condition_variable post_thread_wait_;
429 std::condition_variable post_thread_ready_;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800430
431 // Backlight LED brightness sysfs node.
432 pdx::LocalHandle backlight_brightness_fd_;
433
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800434 // Primary display wait_pingpong state sysfs node.
435 pdx::LocalHandle primary_display_wait_pp_fd_;
436
437 // VSync sleep timerfd.
438 pdx::LocalHandle vsync_sleep_timer_fd_;
439
440 // The timestamp of the last vsync.
Corey Tabaka2251d822017-04-20 16:04:07 -0700441 int64_t last_vsync_timestamp_ = 0;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800442
443 // Vsync count since display on.
Corey Tabaka2251d822017-04-20 16:04:07 -0700444 uint32_t vsync_count_ = 0;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800445
446 // Counter tracking the number of skipped frames.
Corey Tabaka2251d822017-04-20 16:04:07 -0700447 int frame_skip_count_ = 0;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800448
449 // Fd array for tracking retire fences that are returned by hwc. This allows
450 // us to detect when the display driver begins queuing frames.
451 std::vector<pdx::LocalHandle> retire_fence_fds_;
452
Okan Arikan822b7102017-05-08 13:31:34 -0700453 // If we are publishing vsync data, we will put it here.
454 std::unique_ptr<CPUMappedBroadcastRing<DvrVsyncRing>> vsync_ring_;
Steven Thomas050b2c82017-03-06 11:45:16 -0800455
John Bates954796e2017-05-11 11:00:31 -0700456 // Broadcast ring for receiving config data from the DisplayManager.
Okan Arikan6f468c62017-05-31 14:48:30 -0700457 DvrConfigRing shared_config_ring_;
John Bates954796e2017-05-11 11:00:31 -0700458 uint32_t shared_config_ring_sequence_{0};
459 // Config buffer for reading from the post thread.
Okan Arikan6f468c62017-05-31 14:48:30 -0700460 DvrConfig post_thread_config_;
John Bates954796e2017-05-11 11:00:31 -0700461 std::mutex shared_config_mutex_;
462
Steven Thomas050b2c82017-03-06 11:45:16 -0800463 static constexpr int kPostThreadInterrupted = 1;
464
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800465 HardwareComposer(const HardwareComposer&) = delete;
466 void operator=(const HardwareComposer&) = delete;
467};
468
469} // namespace dvr
470} // namespace android
471
472#endif // ANDROID_DVR_SERVICES_DISPLAYD_HARDWARE_COMPOSER_H_