| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1 | #ifndef ANDROID_DVR_SERVICES_DISPLAYD_HARDWARE_COMPOSER_H_ | 
|  | 2 | #define ANDROID_DVR_SERVICES_DISPLAYD_HARDWARE_COMPOSER_H_ | 
|  | 3 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 4 | #include <ui/GraphicBuffer.h> | 
|  | 5 | #include "DisplayHardware/ComposerHal.h" | 
|  | 6 | #include "hwc_types.h" | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 7 |  | 
| Okan Arikan | 822b710 | 2017-05-08 13:31:34 -0700 | [diff] [blame] | 8 | #include <dvr/dvr_shared_buffers.h> | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 9 | #include <hardware/gralloc.h> | 
|  | 10 | #include <log/log.h> | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 11 |  | 
|  | 12 | #include <array> | 
|  | 13 | #include <condition_variable> | 
|  | 14 | #include <memory> | 
|  | 15 | #include <mutex> | 
|  | 16 | #include <thread> | 
|  | 17 | #include <tuple> | 
|  | 18 | #include <vector> | 
|  | 19 |  | 
| Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 20 | #include <dvr/dvr_config.h> | 
| Okan Arikan | 822b710 | 2017-05-08 13:31:34 -0700 | [diff] [blame] | 21 | #include <dvr/dvr_vsync.h> | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 22 | #include <pdx/file_handle.h> | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 23 | #include <pdx/rpc/variant.h> | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 24 | #include <private/dvr/buffer_hub_client.h> | 
| Okan Arikan | 822b710 | 2017-05-08 13:31:34 -0700 | [diff] [blame] | 25 | #include <private/dvr/shared_buffer_helpers.h> | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 26 |  | 
|  | 27 | #include "acquired_buffer.h" | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 28 | #include "display_surface.h" | 
|  | 29 |  | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 30 | // 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 |  | 
|  | 35 | namespace android { | 
|  | 36 | namespace 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. | 
|  | 41 | struct 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. | 
|  | 53 | class Layer { | 
|  | 54 | public: | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 55 | Layer() {} | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 56 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 57 | // Sets up the global state used by all Layer instances. This must be called | 
|  | 58 | // before using any Layer methods. | 
|  | 59 | static void InitializeGlobals(Hwc2::Composer* hwc2_hidl, | 
|  | 60 | const HWCDisplayMetrics* metrics); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 61 |  | 
|  | 62 | // Releases any shared pointers and fence handles held by this instance. | 
|  | 63 | void Reset(); | 
|  | 64 |  | 
|  | 65 | // Sets up the layer to use a display surface as its content source. The Layer | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 66 | // automatically handles ACQUIRE/RELEASE phases for the surface's buffer train | 
|  | 67 | // every frame. | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 68 | // | 
|  | 69 | // |blending| receives HWC_BLENDING_* values. | 
|  | 70 | // |transform| receives HWC_TRANSFORM_* values. | 
|  | 71 | // |composition_type| receives either HWC_FRAMEBUFFER for most layers or | 
|  | 72 | // HWC_FRAMEBUFFER_TARGET (unless you know what you are doing). | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 73 | // |index| is the index of this surface in the DirectDisplaySurface array. | 
|  | 74 | void Setup(const std::shared_ptr<DirectDisplaySurface>& surface, | 
|  | 75 | HWC::BlendMode blending, HWC::Transform transform, | 
|  | 76 | HWC::Composition composition_type, size_t z_roder); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 77 |  | 
|  | 78 | // Sets up the layer to use a direct buffer as its content source. No special | 
|  | 79 | // handling of the buffer is performed; responsibility for updating or | 
|  | 80 | // changing the buffer each frame is on the caller. | 
|  | 81 | // | 
|  | 82 | // |blending| receives HWC_BLENDING_* values. | 
|  | 83 | // |transform| receives HWC_TRANSFORM_* values. | 
|  | 84 | // |composition_type| receives either HWC_FRAMEBUFFER for most layers or | 
|  | 85 | // HWC_FRAMEBUFFER_TARGET (unless you know what you are doing). | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 86 | void Setup(const std::shared_ptr<IonBuffer>& buffer, HWC::BlendMode blending, | 
|  | 87 | HWC::Transform transform, HWC::Composition composition_type, | 
|  | 88 | size_t z_order); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 89 |  | 
|  | 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 Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 92 | void UpdateBuffer(const std::shared_ptr<IonBuffer>& buffer); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 93 |  | 
|  | 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 Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 108 | void SetBlending(HWC::BlendMode blending); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 109 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 110 | // Sets the z-order of this layer | 
|  | 111 | void SetZOrder(size_t z_order); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 112 |  | 
|  | 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 Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 119 | HWC::Composition GetCompositionType() const { return composition_type_; } | 
|  | 120 | HWC::Layer GetLayerHandle() const { return hardware_composer_layer_; } | 
|  | 121 | bool IsLayerSetup() const { return !source_.empty(); } | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 122 |  | 
|  | 123 | // Applies all of the settings to this layer using the hwc functions | 
|  | 124 | void UpdateLayerSettings(); | 
|  | 125 |  | 
|  | 126 | int GetSurfaceId() const { | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 127 | int surface_id = -1; | 
|  | 128 | pdx::rpc::IfAnyOf<SourceSurface>::Call( | 
|  | 129 | &source_, [&surface_id](const SourceSurface& surface_source) { | 
| Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 130 | surface_id = surface_source.GetSurfaceId(); | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 131 | }); | 
|  | 132 | return surface_id; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 133 | } | 
|  | 134 |  | 
| Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 135 | int GetBufferId() const { | 
|  | 136 | int buffer_id = -1; | 
|  | 137 | pdx::rpc::IfAnyOf<SourceSurface>::Call( | 
|  | 138 | &source_, [&buffer_id](const SourceSurface& surface_source) { | 
|  | 139 | buffer_id = surface_source.GetBufferId(); | 
|  | 140 | }); | 
|  | 141 | return buffer_id; | 
|  | 142 | } | 
|  | 143 |  | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 144 | private: | 
|  | 145 | void CommonLayerSetup(); | 
|  | 146 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 147 | static Hwc2::Composer* hwc2_hidl_; | 
|  | 148 | static const HWCDisplayMetrics* display_metrics_; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 149 |  | 
|  | 150 | // The hardware composer layer and metrics to use during the prepare cycle. | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 151 | hwc2_layer_t hardware_composer_layer_ = 0; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 152 |  | 
|  | 153 | // Layer properties used to setup the hardware composer layer during the | 
|  | 154 | // Prepare phase. | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 155 | size_t z_order_ = 0; | 
|  | 156 | HWC::BlendMode blending_ = HWC::BlendMode::None; | 
|  | 157 | HWC::Transform transform_ = HWC::Transform::None; | 
|  | 158 | HWC::Composition composition_type_ = HWC::Composition::Invalid; | 
|  | 159 | HWC::Composition target_composition_type_ = HWC::Composition::Device; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 160 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 161 | // State when the layer is connected to a surface. Provides the same interface | 
|  | 162 | // as SourceBuffer to simplify internal use by Layer. | 
|  | 163 | struct SourceSurface { | 
|  | 164 | std::shared_ptr<DirectDisplaySurface> surface; | 
|  | 165 | AcquiredBuffer acquired_buffer; | 
|  | 166 | pdx::LocalHandle release_fence; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 167 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 168 | SourceSurface(const std::shared_ptr<DirectDisplaySurface>& surface) | 
|  | 169 | : surface(surface) {} | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 170 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 171 | // Attempts to acquire a new buffer from the surface and return a tuple with | 
|  | 172 | // width, height, buffer handle, and fence. If a new buffer is not available | 
|  | 173 | // the previous buffer is returned or an empty value if no buffer has ever | 
|  | 174 | // been posted. When a new buffer is acquired the previous buffer's release | 
|  | 175 | // fence is passed out automatically. | 
|  | 176 | std::tuple<int, int, sp<GraphicBuffer>, pdx::LocalHandle> Acquire() { | 
|  | 177 | if (surface->IsBufferAvailable()) { | 
|  | 178 | acquired_buffer.Release(std::move(release_fence)); | 
|  | 179 | acquired_buffer = surface->AcquireCurrentBuffer(); | 
|  | 180 | ATRACE_ASYNC_END("BufferPost", acquired_buffer.buffer()->id()); | 
|  | 181 | } | 
|  | 182 | if (!acquired_buffer.IsEmpty()) { | 
|  | 183 | return std::make_tuple(acquired_buffer.buffer()->width(), | 
|  | 184 | acquired_buffer.buffer()->height(), | 
|  | 185 | acquired_buffer.buffer()->buffer()->buffer(), | 
|  | 186 | acquired_buffer.ClaimAcquireFence()); | 
|  | 187 | } else { | 
|  | 188 | return std::make_tuple(0, 0, nullptr, pdx::LocalHandle{}); | 
|  | 189 | } | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | void Finish(pdx::LocalHandle fence) { release_fence = std::move(fence); } | 
|  | 193 |  | 
|  | 194 | // Gets a pointer to the current acquired buffer or returns nullptr if there | 
|  | 195 | // isn't one. | 
|  | 196 | IonBuffer* GetBuffer() { | 
|  | 197 | if (acquired_buffer.IsAvailable()) | 
|  | 198 | return acquired_buffer.buffer()->buffer(); | 
|  | 199 | else | 
|  | 200 | return nullptr; | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | // Returns the surface id of the surface. | 
| Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 204 | int GetSurfaceId() const { return surface->surface_id(); } | 
|  | 205 |  | 
|  | 206 | // Returns the buffer id for the current buffer. | 
|  | 207 | int GetBufferId() const { | 
|  | 208 | if (acquired_buffer.IsAvailable()) | 
|  | 209 | return acquired_buffer.buffer()->id(); | 
|  | 210 | else | 
|  | 211 | return -1; | 
|  | 212 | } | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 213 | }; | 
|  | 214 |  | 
|  | 215 | // State when the layer is connected to a buffer. Provides the same interface | 
|  | 216 | // as SourceSurface to simplify internal use by Layer. | 
|  | 217 | struct SourceBuffer { | 
|  | 218 | std::shared_ptr<IonBuffer> buffer; | 
|  | 219 |  | 
|  | 220 | std::tuple<int, int, sp<GraphicBuffer>, pdx::LocalHandle> Acquire() { | 
|  | 221 | if (buffer) | 
|  | 222 | return std::make_tuple(buffer->width(), buffer->height(), | 
|  | 223 | buffer->buffer(), pdx::LocalHandle{}); | 
|  | 224 | else | 
|  | 225 | return std::make_tuple(0, 0, nullptr, pdx::LocalHandle{}); | 
|  | 226 | } | 
|  | 227 |  | 
|  | 228 | void Finish(pdx::LocalHandle /*fence*/) {} | 
|  | 229 |  | 
|  | 230 | IonBuffer* GetBuffer() { return buffer.get(); } | 
|  | 231 |  | 
|  | 232 | int GetSurfaceId() const { return -1; } | 
| Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 233 | int GetBufferId() const { return -1; } | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 234 | }; | 
|  | 235 |  | 
|  | 236 | // The underlying hardware composer layer is supplied buffers either from a | 
|  | 237 | // surface buffer train or from a buffer directly. | 
|  | 238 | pdx::rpc::Variant<SourceSurface, SourceBuffer> source_; | 
|  | 239 |  | 
|  | 240 | pdx::LocalHandle acquire_fence_; | 
|  | 241 | bool surface_rect_functions_applied_ = false; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 242 |  | 
|  | 243 | Layer(const Layer&) = delete; | 
|  | 244 | void operator=(const Layer&) = delete; | 
|  | 245 | }; | 
|  | 246 |  | 
|  | 247 | // HardwareComposer encapsulates the hardware composer HAL, exposing a | 
|  | 248 | // simplified API to post buffers to the display. | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 249 | // | 
|  | 250 | // HardwareComposer is accessed by both the vr flinger dispatcher thread and the | 
|  | 251 | // surface flinger main thread, in addition to internally running a separate | 
|  | 252 | // thread for compositing/EDS and posting layers to the HAL. When changing how | 
|  | 253 | // variables are used or adding new state think carefully about which threads | 
|  | 254 | // will access the state and whether it needs to be synchronized. | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 255 | class HardwareComposer { | 
|  | 256 | public: | 
|  | 257 | // Type for vsync callback. | 
|  | 258 | using VSyncCallback = std::function<void(int, int64_t, int64_t, uint32_t)>; | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 259 | using RequestDisplayCallback = std::function<void(bool)>; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 260 |  | 
|  | 261 | // Since there is no universal way to query the number of hardware layers, | 
|  | 262 | // just set it to 4 for now. | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 263 | static constexpr size_t kMaxHardwareLayers = 4; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 264 |  | 
|  | 265 | HardwareComposer(); | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 266 | HardwareComposer(Hwc2::Composer* hidl, | 
|  | 267 | RequestDisplayCallback request_display_callback); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 268 | ~HardwareComposer(); | 
|  | 269 |  | 
| Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 270 | bool Initialize(); | 
|  | 271 |  | 
|  | 272 | bool IsInitialized() const { return initialized_; } | 
|  | 273 |  | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 274 | // Start the post thread if there's work to do (i.e. visible layers). This | 
|  | 275 | // should only be called from surface flinger's main thread. | 
|  | 276 | void Enable(); | 
|  | 277 | // Pause the post thread, blocking until the post thread has signaled that | 
|  | 278 | // it's paused. This should only be called from surface flinger's main thread. | 
|  | 279 | void Disable(); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 280 |  | 
|  | 281 | // Get the HMD display metrics for the current display. | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 282 | display::Metrics GetHmdDisplayMetrics() const; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 283 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 284 | HWC::Error GetDisplayAttribute(hwc2_display_t display, hwc2_config_t config, | 
|  | 285 | hwc2_attribute_t attributes, | 
|  | 286 | int32_t* out_value) const; | 
|  | 287 | HWC::Error GetDisplayMetrics(hwc2_display_t display, hwc2_config_t config, | 
|  | 288 | HWCDisplayMetrics* out_metrics) const; | 
|  | 289 | std::string Dump(); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 290 |  | 
|  | 291 | void SetVSyncCallback(VSyncCallback callback); | 
|  | 292 |  | 
|  | 293 | // Metrics of the logical display, which is always landscape. | 
|  | 294 | int DisplayWidth() const { return display_metrics_.width; } | 
|  | 295 | int DisplayHeight() const { return display_metrics_.height; } | 
|  | 296 | HWCDisplayMetrics display_metrics() const { return display_metrics_; } | 
|  | 297 |  | 
|  | 298 | // Metrics of the native display, which depends on the specific hardware | 
|  | 299 | // implementation of the display. | 
|  | 300 | HWCDisplayMetrics native_display_metrics() const { | 
|  | 301 | return native_display_metrics_; | 
|  | 302 | } | 
|  | 303 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 304 | // Sets the display surfaces to compose the hardware layer stack. | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 305 | void SetDisplaySurfaces( | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 306 | std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 307 |  | 
| John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 308 | int OnNewGlobalBuffer(DvrGlobalBufferKey key, IonBuffer& ion_buffer); | 
|  | 309 | void OnDeletedGlobalBuffer(DvrGlobalBufferKey key); | 
|  | 310 |  | 
| Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 311 | void OnHardwareComposerRefresh(); | 
|  | 312 |  | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 313 | private: | 
|  | 314 | int32_t EnableVsync(bool enabled); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 315 |  | 
|  | 316 | class ComposerCallback : public Hwc2::IComposerCallback { | 
|  | 317 | public: | 
|  | 318 | ComposerCallback() {} | 
|  | 319 |  | 
|  | 320 | hardware::Return<void> onHotplug(Hwc2::Display /*display*/, | 
|  | 321 | Connection /*connected*/) override { | 
|  | 322 | // TODO(skiazyk): depending on how the server is implemented, we might | 
|  | 323 | // have to set it up to synchronize with receiving this event, as it can | 
|  | 324 | // potentially be a critical event for setting up state within the | 
|  | 325 | // hwc2 module. That is, we (technically) should not call any other hwc | 
|  | 326 | // methods until this method has been called after registering the | 
|  | 327 | // callbacks. | 
|  | 328 | return hardware::Void(); | 
|  | 329 | } | 
|  | 330 |  | 
|  | 331 | hardware::Return<void> onRefresh(Hwc2::Display /*display*/) override { | 
|  | 332 | return hardware::Void(); | 
|  | 333 | } | 
|  | 334 |  | 
|  | 335 | hardware::Return<void> onVsync(Hwc2::Display /*display*/, | 
|  | 336 | int64_t /*timestamp*/) override { | 
|  | 337 | return hardware::Void(); | 
|  | 338 | } | 
|  | 339 | }; | 
|  | 340 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 341 | HWC::Error Validate(hwc2_display_t display); | 
|  | 342 | HWC::Error Present(hwc2_display_t display); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 343 |  | 
|  | 344 | void SetBacklightBrightness(int brightness); | 
|  | 345 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 346 | void PostLayers(); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 347 | void PostThread(); | 
|  | 348 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 349 | // The post thread has two controlling states: | 
|  | 350 | // 1. Idle: no work to do (no visible surfaces). | 
|  | 351 | // 2. Suspended: explicitly halted (system is not in VR mode). | 
|  | 352 | // When either #1 or #2 is true then the post thread is quiescent, otherwise | 
|  | 353 | // it is active. | 
|  | 354 | using PostThreadStateType = uint32_t; | 
|  | 355 | struct PostThreadState { | 
|  | 356 | enum : PostThreadStateType { | 
|  | 357 | Active = 0, | 
|  | 358 | Idle = (1 << 0), | 
|  | 359 | Suspended = (1 << 1), | 
|  | 360 | Quit = (1 << 2), | 
|  | 361 | }; | 
|  | 362 | }; | 
|  | 363 |  | 
|  | 364 | void UpdatePostThreadState(uint32_t state, bool suspend); | 
|  | 365 |  | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 366 | // Blocks until either event_fd becomes readable, or we're interrupted by a | 
|  | 367 | // control thread. Any errors are returned as negative errno values. If we're | 
|  | 368 | // interrupted, kPostThreadInterrupted will be returned. | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 369 | int PostThreadPollInterruptible(const pdx::LocalHandle& event_fd, | 
|  | 370 | int requested_events); | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 371 |  | 
|  | 372 | // BlockUntilVSync, WaitForVSync, and SleepUntil are all blocking calls made | 
|  | 373 | // on the post thread that can be interrupted by a control thread. If | 
|  | 374 | // interrupted, these calls return kPostThreadInterrupted. | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 375 | int ReadWaitPPState(); | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 376 | int BlockUntilVSync(); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 377 | int ReadVSyncTimestamp(int64_t* timestamp); | 
|  | 378 | int WaitForVSync(int64_t* timestamp); | 
|  | 379 | int SleepUntil(int64_t wakeup_timestamp); | 
|  | 380 |  | 
|  | 381 | bool IsFramePendingInDriver() { return ReadWaitPPState() == 1; } | 
|  | 382 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 383 | // Reconfigures the layer stack if the display surfaces changed since the last | 
|  | 384 | // frame. Called only from the post thread. | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 385 | bool UpdateLayerConfig(); | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 386 |  | 
|  | 387 | // Called on the post thread when the post thread is resumed. | 
|  | 388 | void OnPostThreadResumed(); | 
|  | 389 | // Called on the post thread when the post thread is paused or quits. | 
|  | 390 | void OnPostThreadPaused(); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 391 |  | 
| John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 392 | // Map the given shared memory buffer to our broadcast ring to track updates | 
|  | 393 | // to the config parameters. | 
|  | 394 | int MapConfigBuffer(IonBuffer& ion_buffer); | 
|  | 395 | void ConfigBufferDeleted(); | 
|  | 396 | // Poll for config udpates. | 
|  | 397 | void UpdateConfigBuffer(); | 
|  | 398 |  | 
| Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 399 | bool initialized_; | 
|  | 400 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 401 | // Hardware composer HAL device from SurfaceFlinger. VrFlinger does not own | 
|  | 402 | // this pointer. | 
|  | 403 | Hwc2::Composer* hwc2_hidl_; | 
|  | 404 | RequestDisplayCallback request_display_callback_; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 405 | sp<ComposerCallback> callbacks_; | 
|  | 406 |  | 
|  | 407 | // Display metrics of the physical display. | 
|  | 408 | HWCDisplayMetrics native_display_metrics_; | 
|  | 409 | // Display metrics of the logical display, adjusted so that orientation is | 
|  | 410 | // landscape. | 
|  | 411 | HWCDisplayMetrics display_metrics_; | 
|  | 412 | // Transform required to get from native to logical display orientation. | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 413 | HWC::Transform display_transform_ = HWC::Transform::None; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 414 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 415 | // Pending surface list. Set by the display service when DirectSurfaces are | 
|  | 416 | // added, removed, or change visibility. Written by the message dispatch | 
|  | 417 | // thread and read by the post thread. | 
|  | 418 | std::vector<std::shared_ptr<DirectDisplaySurface>> pending_surfaces_; | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 419 |  | 
|  | 420 | // The surfaces displayed by the post thread. Used exclusively by the post | 
|  | 421 | // thread. | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 422 | std::vector<std::shared_ptr<DirectDisplaySurface>> display_surfaces_; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 423 |  | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 424 | // Layer array for handling buffer flow into hardware composer layers. | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 425 | std::array<Layer, kMaxHardwareLayers> layers_; | 
|  | 426 | size_t active_layer_count_ = 0; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 427 |  | 
|  | 428 | // Handler to hook vsync events outside of this class. | 
|  | 429 | VSyncCallback vsync_callback_; | 
|  | 430 |  | 
| Steven Thomas | 282a5ed | 2017-02-07 18:07:01 -0800 | [diff] [blame] | 431 | // The layer posting thread. This thread wakes up a short time before vsync to | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 432 | // hand buffers to hardware composer. | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 433 | std::thread post_thread_; | 
|  | 434 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 435 | // Post thread state machine and synchronization primitives. | 
|  | 436 | PostThreadStateType post_thread_state_{PostThreadState::Idle}; | 
|  | 437 | std::atomic<bool> post_thread_quiescent_{true}; | 
|  | 438 | bool post_thread_resumed_{false}; | 
|  | 439 | pdx::LocalHandle post_thread_event_fd_; | 
|  | 440 | std::mutex post_thread_mutex_; | 
|  | 441 | std::condition_variable post_thread_wait_; | 
|  | 442 | std::condition_variable post_thread_ready_; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 443 |  | 
|  | 444 | // Backlight LED brightness sysfs node. | 
|  | 445 | pdx::LocalHandle backlight_brightness_fd_; | 
|  | 446 |  | 
|  | 447 | // Primary display vsync event sysfs node. | 
|  | 448 | pdx::LocalHandle primary_display_vsync_event_fd_; | 
|  | 449 |  | 
|  | 450 | // Primary display wait_pingpong state sysfs node. | 
|  | 451 | pdx::LocalHandle primary_display_wait_pp_fd_; | 
|  | 452 |  | 
|  | 453 | // VSync sleep timerfd. | 
|  | 454 | pdx::LocalHandle vsync_sleep_timer_fd_; | 
|  | 455 |  | 
|  | 456 | // The timestamp of the last vsync. | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 457 | int64_t last_vsync_timestamp_ = 0; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 458 |  | 
|  | 459 | // Vsync count since display on. | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 460 | uint32_t vsync_count_ = 0; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 461 |  | 
|  | 462 | // Counter tracking the number of skipped frames. | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 463 | int frame_skip_count_ = 0; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 464 |  | 
|  | 465 | // Fd array for tracking retire fences that are returned by hwc. This allows | 
|  | 466 | // us to detect when the display driver begins queuing frames. | 
|  | 467 | std::vector<pdx::LocalHandle> retire_fence_fds_; | 
|  | 468 |  | 
| Okan Arikan | 822b710 | 2017-05-08 13:31:34 -0700 | [diff] [blame] | 469 | // If we are publishing vsync data, we will put it here. | 
|  | 470 | std::unique_ptr<CPUMappedBroadcastRing<DvrVsyncRing>> vsync_ring_; | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 471 |  | 
| John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 472 | // Broadcast ring for receiving config data from the DisplayManager. | 
| Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 473 | DvrConfigRing shared_config_ring_; | 
| John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 474 | uint32_t shared_config_ring_sequence_{0}; | 
|  | 475 | // Config buffer for reading from the post thread. | 
| Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 476 | DvrConfig post_thread_config_; | 
| John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 477 | std::mutex shared_config_mutex_; | 
|  | 478 |  | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 479 | static constexpr int kPostThreadInterrupted = 1; | 
|  | 480 |  | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 481 | static void HwcRefresh(hwc2_callback_data_t data, hwc2_display_t display); | 
|  | 482 | static void HwcVSync(hwc2_callback_data_t data, hwc2_display_t display, | 
|  | 483 | int64_t timestamp); | 
|  | 484 | static void HwcHotplug(hwc2_callback_data_t callbackData, | 
|  | 485 | hwc2_display_t display, hwc2_connection_t connected); | 
|  | 486 |  | 
|  | 487 | HardwareComposer(const HardwareComposer&) = delete; | 
|  | 488 | void operator=(const HardwareComposer&) = delete; | 
|  | 489 | }; | 
|  | 490 |  | 
|  | 491 | }  // namespace dvr | 
|  | 492 | }  // namespace android | 
|  | 493 |  | 
|  | 494 | #endif  // ANDROID_DVR_SERVICES_DISPLAYD_HARDWARE_COMPOSER_H_ |