blob: ab81f61d336142ee7e1fad921ab77a867f30329c [file] [log] [blame]
Sean Pauled2ec4b2016-03-10 15:35:40 -05001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18#define LOG_TAG "hwc-drm-two"
19
Sean Paulf72cccd2018-08-27 13:59:08 -040020#include "drmhwctwo.h"
Sean Paulac874152016-03-10 16:00:26 -050021#include "drmdisplaycomposition.h"
22#include "drmhwcomposer.h"
Sean Paulac874152016-03-10 16:00:26 -050023#include "platform.h"
24#include "vsyncworker.h"
25
26#include <inttypes.h>
27#include <string>
Sean Pauled2ec4b2016-03-10 15:35:40 -050028
Sean Paulac874152016-03-10 16:00:26 -050029#include <cutils/properties.h>
30#include <hardware/hardware.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050031#include <hardware/hwcomposer2.h>
Sean Paulf72cccd2018-08-27 13:59:08 -040032#include <log/log.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050033
34namespace android {
35
Sean Paulac874152016-03-10 16:00:26 -050036class DrmVsyncCallback : public VsyncCallback {
37 public:
38 DrmVsyncCallback(hwc2_callback_data_t data, hwc2_function_pointer_t hook)
39 : data_(data), hook_(hook) {
40 }
41
42 void Callback(int display, int64_t timestamp) {
43 auto hook = reinterpret_cast<HWC2_PFN_VSYNC>(hook_);
44 hook(data_, display, timestamp);
45 }
46
47 private:
48 hwc2_callback_data_t data_;
49 hwc2_function_pointer_t hook_;
50};
51
Sean Pauled2ec4b2016-03-10 15:35:40 -050052DrmHwcTwo::DrmHwcTwo() {
Sean Paulac874152016-03-10 16:00:26 -050053 common.tag = HARDWARE_DEVICE_TAG;
54 common.version = HWC_DEVICE_API_VERSION_2_0;
Sean Pauled2ec4b2016-03-10 15:35:40 -050055 common.close = HookDevClose;
56 getCapabilities = HookDevGetCapabilities;
57 getFunction = HookDevGetFunction;
58}
59
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030060HWC2::Error DrmHwcTwo::CreateDisplay(hwc2_display_t displ,
61 HWC2::DisplayType type) {
62 DrmDevice *drm = resource_manager_.GetDrmDevice(displ);
63 std::shared_ptr<Importer> importer = resource_manager_.GetImporter(displ);
Alexandru Gheorghec5463582018-03-27 15:52:02 +010064 if (!drm || !importer) {
65 ALOGE("Failed to get a valid drmresource and importer");
Sean Paulac874152016-03-10 16:00:26 -050066 return HWC2::Error::NoResources;
67 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030068 displays_.emplace(std::piecewise_construct, std::forward_as_tuple(displ),
Sean Paulf72cccd2018-08-27 13:59:08 -040069 std::forward_as_tuple(&resource_manager_, drm, importer,
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030070 displ, type));
Sean Paulac874152016-03-10 16:00:26 -050071
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030072 DrmCrtc *crtc = drm->GetCrtcForDisplay(static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050073 if (!crtc) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030074 ALOGE("Failed to get crtc for display %d", static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050075 return HWC2::Error::BadDisplay;
76 }
Sean Paulac874152016-03-10 16:00:26 -050077 std::vector<DrmPlane *> display_planes;
Alexandru Gheorghec5463582018-03-27 15:52:02 +010078 for (auto &plane : drm->planes()) {
Sean Paulac874152016-03-10 16:00:26 -050079 if (plane->GetCrtcSupported(*crtc))
80 display_planes.push_back(plane.get());
81 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030082 displays_.at(displ).Init(&display_planes);
Sean Paulac874152016-03-10 16:00:26 -050083 return HWC2::Error::None;
84}
85
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030086HWC2::Error DrmHwcTwo::Init() {
87 int rv = resource_manager_.Init();
88 if (rv) {
89 ALOGE("Can't initialize the resource manager %d", rv);
90 return HWC2::Error::NoResources;
91 }
92
93 HWC2::Error ret = HWC2::Error::None;
94 for (int i = 0; i < resource_manager_.getDisplayCount(); i++) {
95 ret = CreateDisplay(i, HWC2::DisplayType::Physical);
96 if (ret != HWC2::Error::None) {
97 ALOGE("Failed to create display %d with error %d", i, ret);
98 return ret;
99 }
100 }
101
102 auto &drmDevices = resource_manager_.getDrmDevices();
103 for (auto &device : drmDevices) {
104 device->RegisterHotplugHandler(new DrmHotplugHandler(this, device.get()));
105 }
106 return ret;
107}
108
Sean Pauled2ec4b2016-03-10 15:35:40 -0500109template <typename... Args>
110static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
111 ALOGV("Unsupported function: %s", func);
112 return HWC2::Error::Unsupported;
113}
114
Sean Paulac874152016-03-10 16:00:26 -0500115static inline void supported(char const *func) {
116 ALOGV("Supported function: %s", func);
117}
118
Sean Pauled2ec4b2016-03-10 15:35:40 -0500119HWC2::Error DrmHwcTwo::CreateVirtualDisplay(uint32_t width, uint32_t height,
120 int32_t *format,
121 hwc2_display_t *display) {
122 // TODO: Implement virtual display
Sean Paulac874152016-03-10 16:00:26 -0500123 return unsupported(__func__, width, height, format, display);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500124}
125
126HWC2::Error DrmHwcTwo::DestroyVirtualDisplay(hwc2_display_t display) {
Sean Paulac874152016-03-10 16:00:26 -0500127 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500128 return unsupported(__func__, display);
129}
130
131void DrmHwcTwo::Dump(uint32_t *size, char *buffer) {
Sean Paulac874152016-03-10 16:00:26 -0500132 // TODO: Implement dump
Sean Pauled2ec4b2016-03-10 15:35:40 -0500133 unsupported(__func__, size, buffer);
134}
135
136uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() {
Sean Paulac874152016-03-10 16:00:26 -0500137 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500138 unsupported(__func__);
139 return 0;
140}
141
142HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor,
Sean Paulac874152016-03-10 16:00:26 -0500143 hwc2_callback_data_t data,
144 hwc2_function_pointer_t function) {
145 supported(__func__);
146 auto callback = static_cast<HWC2::Callback>(descriptor);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300147
148 if (!function) {
149 callbacks_.erase(callback);
150 return HWC2::Error::None;
151 }
152
Sean Paulac874152016-03-10 16:00:26 -0500153 callbacks_.emplace(callback, HwcCallback(data, function));
154
155 switch (callback) {
156 case HWC2::Callback::Hotplug: {
157 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(function);
158 hotplug(data, HWC_DISPLAY_PRIMARY,
159 static_cast<int32_t>(HWC2::Connection::Connected));
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300160 auto &drmDevices = resource_manager_.getDrmDevices();
161 for (auto &device : drmDevices)
162 HandleInitialHotplugState(device.get());
Sean Paulac874152016-03-10 16:00:26 -0500163 break;
164 }
165 case HWC2::Callback::Vsync: {
166 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
167 displays_)
168 d.second.RegisterVsyncCallback(data, function);
169 break;
170 }
171 default:
172 break;
173 }
174 return HWC2::Error::None;
175}
176
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100177DrmHwcTwo::HwcDisplay::HwcDisplay(ResourceManager *resource_manager,
178 DrmDevice *drm,
Sean Paulac874152016-03-10 16:00:26 -0500179 std::shared_ptr<Importer> importer,
Sean Paulac874152016-03-10 16:00:26 -0500180 hwc2_display_t handle, HWC2::DisplayType type)
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100181 : resource_manager_(resource_manager),
182 drm_(drm),
183 importer_(importer),
184 handle_(handle),
185 type_(type) {
Sean Paulac874152016-03-10 16:00:26 -0500186 supported(__func__);
187}
188
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300189void DrmHwcTwo::HwcDisplay::ClearDisplay() {
190 compositor_.ClearDisplay();
191}
192
Sean Paulac874152016-03-10 16:00:26 -0500193HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
194 supported(__func__);
195 planner_ = Planner::CreateInstance(drm_);
196 if (!planner_) {
197 ALOGE("Failed to create planner instance for composition");
198 return HWC2::Error::NoResources;
199 }
200
201 int display = static_cast<int>(handle_);
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100202 int ret = compositor_.Init(resource_manager_, display);
Sean Paulac874152016-03-10 16:00:26 -0500203 if (ret) {
204 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
205 return HWC2::Error::NoResources;
206 }
207
208 // Split up the given display planes into primary and overlay to properly
209 // interface with the composition
210 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
211 property_get("hwc.drm.use_overlay_planes", use_overlay_planes_prop, "1");
212 bool use_overlay_planes = atoi(use_overlay_planes_prop);
213 for (auto &plane : *planes) {
214 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
215 primary_planes_.push_back(plane);
216 else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY)
217 overlay_planes_.push_back(plane);
218 }
219
220 crtc_ = drm_->GetCrtcForDisplay(display);
221 if (!crtc_) {
222 ALOGE("Failed to get crtc for display %d", display);
223 return HWC2::Error::BadDisplay;
224 }
225
226 connector_ = drm_->GetConnectorForDisplay(display);
227 if (!connector_) {
228 ALOGE("Failed to get connector for display %d", display);
229 return HWC2::Error::BadDisplay;
230 }
231
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300232 ret = vsync_worker_.Init(drm_, display);
233 if (ret) {
234 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
235 return HWC2::Error::BadDisplay;
236 }
237
238 return ChosePreferredConfig();
239}
240
241HWC2::Error DrmHwcTwo::HwcDisplay::ChosePreferredConfig() {
Sean Paulac874152016-03-10 16:00:26 -0500242 // Fetch the number of modes from the display
243 uint32_t num_configs;
244 HWC2::Error err = GetDisplayConfigs(&num_configs, NULL);
245 if (err != HWC2::Error::None || !num_configs)
246 return err;
247
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200248 return SetActiveConfig(connector_->get_preferred_mode_id());
Sean Paulac874152016-03-10 16:00:26 -0500249}
250
251HWC2::Error DrmHwcTwo::HwcDisplay::RegisterVsyncCallback(
252 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
253 supported(__func__);
254 auto callback = std::make_shared<DrmVsyncCallback>(data, func);
Adrian Salidofa37f672017-02-16 10:29:46 -0800255 vsync_worker_.RegisterCallback(std::move(callback));
Sean Paulac874152016-03-10 16:00:26 -0500256 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500257}
258
259HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500260 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500261 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
262 l.second.accept_type_change();
263 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500264}
265
266HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500267 supported(__func__);
268 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
269 *layer = static_cast<hwc2_layer_t>(layer_idx_);
270 ++layer_idx_;
271 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500272}
273
274HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Sean Paulac874152016-03-10 16:00:26 -0500275 supported(__func__);
Vincent Donnefort9abec032019-10-09 15:43:43 +0100276 if (!get_layer(layer))
277 return HWC2::Error::BadLayer;
278
Sean Paulac874152016-03-10 16:00:26 -0500279 layers_.erase(layer);
280 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500281}
282
283HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
Sean Paulac874152016-03-10 16:00:26 -0500284 supported(__func__);
285 DrmMode const &mode = connector_->active_mode();
286 if (mode.id() == 0)
287 return HWC2::Error::BadConfig;
288
289 *config = mode.id();
290 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500291}
292
293HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
294 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500295 supported(__func__);
296 uint32_t num_changes = 0;
297 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
298 if (l.second.type_changed()) {
299 if (layers && num_changes < *num_elements)
300 layers[num_changes] = l.first;
301 if (types && num_changes < *num_elements)
302 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
303 ++num_changes;
304 }
305 }
306 if (!layers && !types)
307 *num_elements = num_changes;
308 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500309}
310
311HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500312 uint32_t height,
313 int32_t /*format*/,
314 int32_t dataspace) {
315 supported(__func__);
316 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
317 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
318
319 if (width < min.first || height < min.second)
320 return HWC2::Error::Unsupported;
321
322 if (width > max.first || height > max.second)
323 return HWC2::Error::Unsupported;
324
325 if (dataspace != HAL_DATASPACE_UNKNOWN &&
326 dataspace != HAL_DATASPACE_STANDARD_UNSPECIFIED)
327 return HWC2::Error::Unsupported;
328
329 // TODO: Validate format can be handled by either GL or planes
330 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500331}
332
333HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500334 int32_t *modes) {
335 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800336 if (!modes)
337 *num_modes = 1;
338
339 if (modes)
340 *modes = HAL_COLOR_MODE_NATIVE;
341
342 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500343}
344
345HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500346 int32_t attribute_in,
347 int32_t *value) {
348 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400349 auto mode = std::find_if(connector_->modes().begin(),
350 connector_->modes().end(),
351 [config](DrmMode const &m) {
352 return m.id() == config;
353 });
Sean Paulac874152016-03-10 16:00:26 -0500354 if (mode == connector_->modes().end()) {
355 ALOGE("Could not find active mode for %d", config);
356 return HWC2::Error::BadConfig;
357 }
358
359 static const int32_t kUmPerInch = 25400;
360 uint32_t mm_width = connector_->mm_width();
361 uint32_t mm_height = connector_->mm_height();
362 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
363 switch (attribute) {
364 case HWC2::Attribute::Width:
365 *value = mode->h_display();
366 break;
367 case HWC2::Attribute::Height:
368 *value = mode->v_display();
369 break;
370 case HWC2::Attribute::VsyncPeriod:
371 // in nanoseconds
372 *value = 1000 * 1000 * 1000 / mode->v_refresh();
373 break;
374 case HWC2::Attribute::DpiX:
375 // Dots per 1000 inches
376 *value = mm_width ? (mode->h_display() * kUmPerInch) / mm_width : -1;
377 break;
378 case HWC2::Attribute::DpiY:
379 // Dots per 1000 inches
380 *value = mm_height ? (mode->v_display() * kUmPerInch) / mm_height : -1;
381 break;
382 default:
383 *value = -1;
384 return HWC2::Error::BadConfig;
385 }
386 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500387}
388
389HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
390 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500391 supported(__func__);
392 // Since this callback is normally invoked twice (once to get the count, and
393 // once to populate configs), we don't really want to read the edid
394 // redundantly. Instead, only update the modes on the first invocation. While
395 // it's possible this will result in stale modes, it'll all come out in the
396 // wash when we try to set the active config later.
397 if (!configs) {
398 int ret = connector_->UpdateModes();
399 if (ret) {
400 ALOGE("Failed to update display modes %d", ret);
401 return HWC2::Error::BadDisplay;
402 }
403 }
404
Neil Armstrongb67d0492019-06-20 09:00:21 +0000405 // Since the upper layers only look at vactive/hactive/refresh, height and
406 // width, it doesn't differentiate interlaced from progressive and other
407 // similar modes. Depending on the order of modes we return to SF, it could
408 // end up choosing a suboptimal configuration and dropping the preferred
409 // mode. To workaround this, don't offer interlaced modes to SF if there is
410 // at least one non-interlaced alternative and only offer a single WxH@R
411 // mode with at least the prefered mode from in DrmConnector::UpdateModes()
412
413 // TODO: Remove the following block of code until AOSP handles all modes
414 std::vector<DrmMode> sel_modes;
415
416 // Add the preferred mode first to be sure it's not dropped
417 auto mode = std::find_if(connector_->modes().begin(),
418 connector_->modes().end(), [&](DrmMode const &m) {
419 return m.id() ==
420 connector_->get_preferred_mode_id();
421 });
422 if (mode != connector_->modes().end())
423 sel_modes.push_back(*mode);
424
425 // Add the active mode if different from preferred mode
426 if (connector_->active_mode().id() != connector_->get_preferred_mode_id())
427 sel_modes.push_back(connector_->active_mode());
428
429 // Cycle over the modes and filter out "similar" modes, keeping only the
430 // first ones in the order given by DRM (from CEA ids and timings order)
Sean Paulac874152016-03-10 16:00:26 -0500431 for (const DrmMode &mode : connector_->modes()) {
Neil Armstrongb67d0492019-06-20 09:00:21 +0000432 // TODO: Remove this when 3D Attributes are in AOSP
433 if (mode.flags() & DRM_MODE_FLAG_3D_MASK)
434 continue;
435
Neil Armstrong4c027a72019-06-04 14:48:02 +0000436 // TODO: Remove this when the Interlaced attribute is in AOSP
437 if (mode.flags() & DRM_MODE_FLAG_INTERLACE) {
438 auto m = std::find_if(connector_->modes().begin(),
439 connector_->modes().end(),
440 [&mode](DrmMode const &m) {
441 return !(m.flags() & DRM_MODE_FLAG_INTERLACE) &&
442 m.h_display() == mode.h_display() &&
443 m.v_display() == mode.v_display();
444 });
Neil Armstrongb67d0492019-06-20 09:00:21 +0000445 if (m == connector_->modes().end())
446 sel_modes.push_back(mode);
447
448 continue;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000449 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000450
451 // Search for a similar WxH@R mode in the filtered list and drop it if
452 // another mode with the same WxH@R has already been selected
453 // TODO: Remove this when AOSP handles duplicates modes
454 auto m = std::find_if(sel_modes.begin(), sel_modes.end(),
455 [&mode](DrmMode const &m) {
456 return m.h_display() == mode.h_display() &&
457 m.v_display() == mode.v_display() &&
458 m.v_refresh() == mode.v_refresh();
459 });
460 if (m == sel_modes.end())
461 sel_modes.push_back(mode);
462 }
463
464 auto num_modes = static_cast<uint32_t>(sel_modes.size());
465 if (!configs) {
466 *num_configs = num_modes;
467 return HWC2::Error::None;
468 }
469
470 uint32_t idx = 0;
471 for (const DrmMode &mode : sel_modes) {
472 if (idx >= *num_configs)
473 break;
474 configs[idx++] = mode.id();
Sean Paulac874152016-03-10 16:00:26 -0500475 }
476 *num_configs = idx;
477 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500478}
479
480HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500481 supported(__func__);
482 std::ostringstream stream;
483 stream << "display-" << connector_->id();
484 std::string string = stream.str();
485 size_t length = string.length();
486 if (!name) {
487 *size = length;
488 return HWC2::Error::None;
489 }
490
491 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
492 strncpy(name, string.c_str(), *size);
493 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500494}
495
Sean Paulac874152016-03-10 16:00:26 -0500496HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
497 uint32_t *num_elements,
498 hwc2_layer_t *layers,
499 int32_t *layer_requests) {
500 supported(__func__);
501 // TODO: I think virtual display should request
502 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
503 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
504 *num_elements = 0;
505 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500506}
507
508HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500509 supported(__func__);
510 *type = static_cast<int32_t>(type_);
511 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500512}
513
514HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500515 supported(__func__);
516 *support = 0;
517 return HWC2::Error::None;
518}
519
520HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400521 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
522 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500523 supported(__func__);
524 *num_types = 0;
525 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500526}
527
528HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500529 hwc2_layer_t *layers,
530 int32_t *fences) {
531 supported(__func__);
532 uint32_t num_layers = 0;
533
534 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
535 ++num_layers;
536 if (layers == NULL || fences == NULL) {
537 continue;
538 } else if (num_layers > *num_elements) {
539 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
540 return HWC2::Error::None;
541 }
542
543 layers[num_layers - 1] = l.first;
544 fences[num_layers - 1] = l.second.take_release_fence();
545 }
546 *num_elements = num_layers;
547 return HWC2::Error::None;
548}
549
550void DrmHwcTwo::HwcDisplay::AddFenceToRetireFence(int fd) {
551 supported(__func__);
552 if (fd < 0)
553 return;
554
555 if (next_retire_fence_.get() >= 0) {
556 int old_fence = next_retire_fence_.get();
557 next_retire_fence_.Set(sync_merge("dc_retire", old_fence, fd));
558 } else {
559 next_retire_fence_.Set(dup(fd));
560 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500561}
562
Roman Stratiienkoafb36892019-11-08 17:16:11 +0200563bool DrmHwcTwo::HwcDisplay::HardwareSupportsLayerType(
564 HWC2::Composition comp_type) {
565 return comp_type == HWC2::Composition::Device ||
566 comp_type == HWC2::Composition::Cursor;
567}
568
Rob Herring4f6c62e2018-05-17 14:33:02 -0500569HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500570 std::vector<DrmCompositionDisplayLayersMap> layers_map;
571 layers_map.emplace_back();
572 DrmCompositionDisplayLayersMap &map = layers_map.back();
573
574 map.display = static_cast<int>(handle_);
575 map.geometry_changed = true; // TODO: Fix this
576
577 // order the layers by z-order
578 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100579 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500580 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
581 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200582 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500583 case HWC2::Composition::Device:
584 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
585 break;
586 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100587 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500588 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100589 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500590 break;
591 default:
592 continue;
593 }
594 }
595 if (use_client_layer)
596 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
597
Rob Herring4f6c62e2018-05-17 14:33:02 -0500598 if (z_map.empty())
599 return HWC2::Error::BadLayer;
600
Sean Paulac874152016-03-10 16:00:26 -0500601 // now that they're ordered by z, add them to the composition
602 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
603 DrmHwcLayer layer;
604 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200605 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500606 if (ret) {
607 ALOGE("Failed to import layer, ret=%d", ret);
608 return HWC2::Error::NoResources;
609 }
610 map.layers.emplace_back(std::move(layer));
611 }
Sean Paulac874152016-03-10 16:00:26 -0500612
Sean Paulf72cccd2018-08-27 13:59:08 -0400613 std::unique_ptr<DrmDisplayComposition> composition = compositor_
614 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500615 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
616
617 // TODO: Don't always assume geometry changed
618 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
619 if (ret) {
620 ALOGE("Failed to set layers in the composition ret=%d", ret);
621 return HWC2::Error::BadLayer;
622 }
623
624 std::vector<DrmPlane *> primary_planes(primary_planes_);
625 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500626 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500627 if (ret) {
628 ALOGE("Failed to plan the composition ret=%d", ret);
629 return HWC2::Error::BadConfig;
630 }
631
632 // Disable the planes we're not using
633 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
634 composition->AddPlaneDisable(*i);
635 i = primary_planes.erase(i);
636 }
637 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
638 composition->AddPlaneDisable(*i);
639 i = overlay_planes.erase(i);
640 }
641
Rob Herring4f6c62e2018-05-17 14:33:02 -0500642 if (test) {
643 ret = compositor_.TestComposition(composition.get());
644 } else {
645 AddFenceToRetireFence(composition->take_out_fence());
646 ret = compositor_.ApplyComposition(std::move(composition));
647 }
Sean Paulac874152016-03-10 16:00:26 -0500648 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700649 if (!test)
650 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500651 return HWC2::Error::BadParameter;
652 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500653 return HWC2::Error::None;
654}
655
656HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *retire_fence) {
657 supported(__func__);
658 HWC2::Error ret;
659
660 ret = CreateComposition(false);
661 if (ret == HWC2::Error::BadLayer) {
662 // Can we really have no client or device layers?
663 *retire_fence = -1;
664 return HWC2::Error::None;
665 }
666 if (ret != HWC2::Error::None)
667 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500668
Sean Paulac874152016-03-10 16:00:26 -0500669 // The retire fence returned here is for the last frame, so return it and
670 // promote the next retire fence
671 *retire_fence = retire_fence_.Release();
672 retire_fence_ = std::move(next_retire_fence_);
673
674 ++frame_no_;
675 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500676}
677
678HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500679 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400680 auto mode = std::find_if(connector_->modes().begin(),
681 connector_->modes().end(),
682 [config](DrmMode const &m) {
683 return m.id() == config;
684 });
Sean Paulac874152016-03-10 16:00:26 -0500685 if (mode == connector_->modes().end()) {
686 ALOGE("Could not find active mode for %d", config);
687 return HWC2::Error::BadConfig;
688 }
689
Sean Paulf72cccd2018-08-27 13:59:08 -0400690 std::unique_ptr<DrmDisplayComposition> composition = compositor_
691 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500692 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
693 int ret = composition->SetDisplayMode(*mode);
Sean Pauled45a8e2017-02-28 13:17:34 -0500694 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500695 if (ret) {
696 ALOGE("Failed to queue dpms composition on %d", ret);
697 return HWC2::Error::BadConfig;
698 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300699
700 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500701
702 // Setup the client layer's dimensions
703 hwc_rect_t display_frame = {.left = 0,
704 .top = 0,
705 .right = static_cast<int>(mode->h_display()),
706 .bottom = static_cast<int>(mode->v_display())};
707 client_layer_.SetLayerDisplayFrame(display_frame);
708 hwc_frect_t source_crop = {.left = 0.0f,
709 .top = 0.0f,
710 .right = mode->h_display() + 0.0f,
711 .bottom = mode->v_display() + 0.0f};
712 client_layer_.SetLayerSourceCrop(source_crop);
713
714 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500715}
716
717HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
718 int32_t acquire_fence,
719 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600720 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500721 supported(__func__);
722 UniqueFd uf(acquire_fence);
723
724 client_layer_.set_buffer(target);
725 client_layer_.set_acquire_fence(uf.get());
726 client_layer_.SetLayerDataspace(dataspace);
727 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500728}
729
730HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500731 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800732
733 if (mode != HAL_COLOR_MODE_NATIVE)
Vincent Donnefort7834a892019-10-09 15:53:56 +0100734 return HWC2::Error::BadParameter;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800735
736 color_mode_ = mode;
737 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500738}
739
740HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500741 int32_t hint) {
742 supported(__func__);
743 // TODO: Force client composition if we get this
Sean Pauled2ec4b2016-03-10 15:35:40 -0500744 return unsupported(__func__, matrix, hint);
745}
746
747HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500748 int32_t release_fence) {
749 supported(__func__);
750 // TODO: Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500751 return unsupported(__func__, buffer, release_fence);
752}
753
Sean Paulac874152016-03-10 16:00:26 -0500754HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
755 supported(__func__);
756 uint64_t dpms_value = 0;
757 auto mode = static_cast<HWC2::PowerMode>(mode_in);
758 switch (mode) {
759 case HWC2::PowerMode::Off:
760 dpms_value = DRM_MODE_DPMS_OFF;
761 break;
762 case HWC2::PowerMode::On:
763 dpms_value = DRM_MODE_DPMS_ON;
764 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100765 case HWC2::PowerMode::Doze:
766 case HWC2::PowerMode::DozeSuspend:
767 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500768 default:
769 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100770 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500771 };
772
Sean Paulf72cccd2018-08-27 13:59:08 -0400773 std::unique_ptr<DrmDisplayComposition> composition = compositor_
774 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500775 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
776 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500777 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500778 if (ret) {
779 ALOGE("Failed to apply the dpms composition ret=%d", ret);
780 return HWC2::Error::BadParameter;
781 }
782 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500783}
784
785HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500786 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300787 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500788 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500789}
790
791HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500792 uint32_t *num_requests) {
793 supported(__func__);
794 *num_types = 0;
795 *num_requests = 0;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500796 size_t avail_planes = primary_planes_.size() + overlay_planes_.size();
Rob Herring4f6c62e2018-05-17 14:33:02 -0500797
798 /*
799 * If more layers then planes, save one plane
800 * for client composited layers
801 */
802 if (avail_planes < layers_.size())
803 avail_planes--;
804
Roman Stratiienkof2647232019-11-21 01:58:35 +0200805 std::map<uint32_t, DrmHwcTwo::HwcLayer *, std::greater<int>> z_map;
806 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
807 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
808
809 bool gpu_block = false;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500810 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200811 if (gpu_block || avail_planes == 0 ||
Roman Stratiienkoafb36892019-11-08 17:16:11 +0200812 !HardwareSupportsLayerType(l.second->sf_type()) ||
Roman Stratiienkof2647232019-11-21 01:58:35 +0200813 !importer_->CanImportBuffer(l.second->buffer())) {
814 gpu_block = true;
815 ++*num_types;
816 } else {
817 avail_planes--;
818 }
819
820 l.second->set_validated_type(gpu_block ? HWC2::Composition::Client
821 : HWC2::Composition::Device);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500822 }
823
Roman Stratiienkof2647232019-11-21 01:58:35 +0200824 if (CreateComposition(true) != HWC2::Error::None)
825 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
826 l.second.set_validated_type(HWC2::Composition::Client);
827
Rob Herringee8f45b2017-06-09 15:15:55 -0500828 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500829}
830
831HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -0500832 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800833 cursor_x_ = x;
834 cursor_y_ = y;
835 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500836}
837
838HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500839 supported(__func__);
840 blending_ = static_cast<HWC2::BlendMode>(mode);
841 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500842}
843
844HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500845 int32_t acquire_fence) {
846 supported(__func__);
847 UniqueFd uf(acquire_fence);
848
849 // The buffer and acquire_fence are handled elsewhere
850 if (sf_type_ == HWC2::Composition::Client ||
851 sf_type_ == HWC2::Composition::Sideband ||
852 sf_type_ == HWC2::Composition::SolidColor)
853 return HWC2::Error::None;
854
855 set_buffer(buffer);
856 set_acquire_fence(uf.get());
857 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500858}
859
860HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Sean Paulac874152016-03-10 16:00:26 -0500861 // TODO: Punt to client composition here?
Sean Pauled2ec4b2016-03-10 15:35:40 -0500862 return unsupported(__func__, color);
863}
864
865HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -0500866 sf_type_ = static_cast<HWC2::Composition>(type);
867 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500868}
869
870HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -0500871 supported(__func__);
872 dataspace_ = static_cast<android_dataspace_t>(dataspace);
873 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500874}
875
876HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -0500877 supported(__func__);
878 display_frame_ = frame;
879 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500880}
881
882HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -0500883 supported(__func__);
884 alpha_ = alpha;
885 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500886}
887
888HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
889 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -0500890 supported(__func__);
891 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -0500892 return unsupported(__func__, stream);
893}
894
895HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -0500896 supported(__func__);
897 source_crop_ = crop;
898 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500899}
900
901HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -0500902 supported(__func__);
903 // TODO: We don't use surface damage, marking as unsupported
904 unsupported(__func__, damage);
905 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500906}
907
908HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -0500909 supported(__func__);
910 transform_ = static_cast<HWC2::Transform>(transform);
911 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500912}
913
914HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -0500915 supported(__func__);
916 // TODO: We don't use this information, marking as unsupported
917 unsupported(__func__, visible);
918 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500919}
920
Sean Paulac874152016-03-10 16:00:26 -0500921HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
922 supported(__func__);
923 z_order_ = order;
924 return HWC2::Error::None;
925}
926
927void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
928 supported(__func__);
929 switch (blending_) {
930 case HWC2::BlendMode::None:
931 layer->blending = DrmHwcBlending::kNone;
932 break;
933 case HWC2::BlendMode::Premultiplied:
934 layer->blending = DrmHwcBlending::kPreMult;
935 break;
936 case HWC2::BlendMode::Coverage:
937 layer->blending = DrmHwcBlending::kCoverage;
938 break;
939 default:
940 ALOGE("Unknown blending mode b=%d", blending_);
941 layer->blending = DrmHwcBlending::kNone;
942 break;
943 }
944
945 OutputFd release_fence = release_fence_output();
946
947 layer->sf_handle = buffer_;
948 layer->acquire_fence = acquire_fence_.Release();
949 layer->release_fence = std::move(release_fence);
950 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +0200951 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -0500952 layer->SetSourceCrop(source_crop_);
953 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -0500954}
955
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300956void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
957 auto cb = callbacks_.find(HWC2::Callback::Hotplug);
958 if (cb == callbacks_.end())
959 return;
960
961 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(cb->second.func);
962 hotplug(cb->second.data, displayid,
963 (state == DRM_MODE_CONNECTED ? HWC2_CONNECTION_CONNECTED
964 : HWC2_CONNECTION_DISCONNECTED));
965}
966
967void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
968 for (auto &conn : drmDevice->connectors()) {
969 if (conn->state() != DRM_MODE_CONNECTED)
970 continue;
971 HandleDisplayHotplug(conn->display(), conn->state());
972 }
973}
974
975void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
976 for (auto &conn : drm_->connectors()) {
977 drmModeConnection old_state = conn->state();
978 drmModeConnection cur_state = conn->UpdateModes()
979 ? DRM_MODE_UNKNOWNCONNECTION
980 : conn->state();
981
982 if (cur_state == old_state)
983 continue;
984
985 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
986 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
987 conn->id(), conn->display());
988
989 int display_id = conn->display();
990 if (cur_state == DRM_MODE_CONNECTED) {
991 auto &display = hwc2_->displays_.at(display_id);
992 display.ChosePreferredConfig();
993 } else {
994 auto &display = hwc2_->displays_.at(display_id);
995 display.ClearDisplay();
996 }
997
998 hwc2_->HandleDisplayHotplug(display_id, cur_state);
999 }
1000}
1001
Sean Pauled2ec4b2016-03-10 15:35:40 -05001002// static
1003int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1004 unsupported(__func__);
1005 return 0;
1006}
1007
1008// static
1009void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001010 uint32_t *out_count,
1011 int32_t * /*out_capabilities*/) {
1012 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001013 *out_count = 0;
1014}
1015
1016// static
Sean Paulac874152016-03-10 16:00:26 -05001017hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1018 struct hwc2_device * /*dev*/, int32_t descriptor) {
1019 supported(__func__);
1020 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001021 switch (func) {
1022 // Device functions
1023 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1024 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1025 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1026 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001027 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001028 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1029 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1030 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1031 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1032 case HWC2::FunctionDescriptor::Dump:
1033 return ToHook<HWC2_PFN_DUMP>(
1034 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1035 uint32_t *, char *>);
1036 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1037 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1038 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1039 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1040 case HWC2::FunctionDescriptor::RegisterCallback:
1041 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1042 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1043 &DrmHwcTwo::RegisterCallback, int32_t,
1044 hwc2_callback_data_t, hwc2_function_pointer_t>);
1045
1046 // Display functions
1047 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1048 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1049 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1050 &HwcDisplay::AcceptDisplayChanges>);
1051 case HWC2::FunctionDescriptor::CreateLayer:
1052 return ToHook<HWC2_PFN_CREATE_LAYER>(
1053 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1054 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1055 case HWC2::FunctionDescriptor::DestroyLayer:
1056 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1057 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1058 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1059 case HWC2::FunctionDescriptor::GetActiveConfig:
1060 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1061 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1062 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1063 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1064 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1065 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1066 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1067 hwc2_layer_t *, int32_t *>);
1068 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1069 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1070 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1071 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1072 int32_t, int32_t>);
1073 case HWC2::FunctionDescriptor::GetColorModes:
1074 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1075 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1076 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1077 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001078 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1079 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1080 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1081 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001082 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001083 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1084 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1085 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1086 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001087 case HWC2::FunctionDescriptor::GetDisplayName:
1088 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1089 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1090 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1091 case HWC2::FunctionDescriptor::GetDisplayRequests:
1092 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1093 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1094 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1095 hwc2_layer_t *, int32_t *>);
1096 case HWC2::FunctionDescriptor::GetDisplayType:
1097 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1098 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1099 &HwcDisplay::GetDisplayType, int32_t *>);
1100 case HWC2::FunctionDescriptor::GetDozeSupport:
1101 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1102 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1103 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001104 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1105 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1106 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1107 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1108 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001109 case HWC2::FunctionDescriptor::GetReleaseFences:
1110 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1111 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1112 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1113 int32_t *>);
1114 case HWC2::FunctionDescriptor::PresentDisplay:
1115 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1116 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1117 &HwcDisplay::PresentDisplay, int32_t *>);
1118 case HWC2::FunctionDescriptor::SetActiveConfig:
1119 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1120 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1121 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1122 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001123 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1124 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1125 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1126 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001127 case HWC2::FunctionDescriptor::SetColorMode:
1128 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1129 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1130 &HwcDisplay::SetColorMode, int32_t>);
1131 case HWC2::FunctionDescriptor::SetColorTransform:
1132 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1133 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1134 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1135 case HWC2::FunctionDescriptor::SetOutputBuffer:
1136 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1137 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1138 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1139 case HWC2::FunctionDescriptor::SetPowerMode:
1140 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1141 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1142 &HwcDisplay::SetPowerMode, int32_t>);
1143 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1144 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1145 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1146 &HwcDisplay::SetVsyncEnabled, int32_t>);
1147 case HWC2::FunctionDescriptor::ValidateDisplay:
1148 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1149 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1150 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
1151
1152 // Layer functions
1153 case HWC2::FunctionDescriptor::SetCursorPosition:
1154 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1155 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1156 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1157 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1158 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1159 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1160 &HwcLayer::SetLayerBlendMode, int32_t>);
1161 case HWC2::FunctionDescriptor::SetLayerBuffer:
1162 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1163 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1164 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1165 case HWC2::FunctionDescriptor::SetLayerColor:
1166 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1167 LayerHook<decltype(&HwcLayer::SetLayerColor),
1168 &HwcLayer::SetLayerColor, hwc_color_t>);
1169 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1170 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1171 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1172 &HwcLayer::SetLayerCompositionType, int32_t>);
1173 case HWC2::FunctionDescriptor::SetLayerDataspace:
1174 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1175 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1176 &HwcLayer::SetLayerDataspace, int32_t>);
1177 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1178 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1179 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1180 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1181 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1182 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1183 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1184 &HwcLayer::SetLayerPlaneAlpha, float>);
1185 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001186 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1187 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1188 &HwcLayer::SetLayerSidebandStream,
1189 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001190 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1191 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1192 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1193 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1194 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1195 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1196 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1197 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1198 case HWC2::FunctionDescriptor::SetLayerTransform:
1199 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1200 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1201 &HwcLayer::SetLayerTransform, int32_t>);
1202 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1203 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1204 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1205 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1206 case HWC2::FunctionDescriptor::SetLayerZOrder:
1207 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1208 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1209 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001210 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001211 default:
1212 return NULL;
1213 }
1214}
Sean Paulac874152016-03-10 16:00:26 -05001215
1216// static
1217int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1218 struct hw_device_t **dev) {
1219 supported(__func__);
1220 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1221 ALOGE("Invalid module name- %s", name);
1222 return -EINVAL;
1223 }
1224
1225 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1226 if (!ctx) {
1227 ALOGE("Failed to allocate DrmHwcTwo");
1228 return -ENOMEM;
1229 }
1230
1231 HWC2::Error err = ctx->Init();
1232 if (err != HWC2::Error::None) {
1233 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1234 return -EINVAL;
1235 }
1236
1237 ctx->common.module = const_cast<hw_module_t *>(module);
1238 *dev = &ctx->common;
1239 ctx.release();
1240 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001241}
Sean Paulf72cccd2018-08-27 13:59:08 -04001242} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001243
1244static struct hw_module_methods_t hwc2_module_methods = {
1245 .open = android::DrmHwcTwo::HookDevOpen,
1246};
1247
1248hw_module_t HAL_MODULE_INFO_SYM = {
1249 .tag = HARDWARE_MODULE_TAG,
1250 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1251 .id = HWC_HARDWARE_MODULE_ID,
1252 .name = "DrmHwcTwo module",
1253 .author = "The Android Open Source Project",
1254 .methods = &hwc2_module_methods,
1255 .dso = NULL,
1256 .reserved = {0},
1257};