blob: 814d8f77cdd127516c51f6e0273f735b71c5bf74 [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
Rob Herring4f6c62e2018-05-17 14:33:02 -0500563HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500564 std::vector<DrmCompositionDisplayLayersMap> layers_map;
565 layers_map.emplace_back();
566 DrmCompositionDisplayLayersMap &map = layers_map.back();
567
568 map.display = static_cast<int>(handle_);
569 map.geometry_changed = true; // TODO: Fix this
570
571 // order the layers by z-order
572 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100573 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500574 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
575 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500576 HWC2::Composition comp_type;
Alexey Firago18ec6882018-11-21 23:47:05 +0300577 if (test) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500578 comp_type = l.second.sf_type();
Alexey Firago18ec6882018-11-21 23:47:05 +0300579 if (comp_type == HWC2::Composition::Device) {
580 if (!importer_->CanImportBuffer(l.second.buffer()))
581 comp_type = HWC2::Composition::Client;
582 }
583 } else
Rob Herring4f6c62e2018-05-17 14:33:02 -0500584 comp_type = l.second.validated_type();
585
586 switch (comp_type) {
Sean Paulac874152016-03-10 16:00:26 -0500587 case HWC2::Composition::Device:
588 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
589 break;
590 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100591 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500592 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100593 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500594 break;
595 default:
596 continue;
597 }
598 }
599 if (use_client_layer)
600 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
601
Rob Herring4f6c62e2018-05-17 14:33:02 -0500602 if (z_map.empty())
603 return HWC2::Error::BadLayer;
604
Sean Paulac874152016-03-10 16:00:26 -0500605 // now that they're ordered by z, add them to the composition
606 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
607 DrmHwcLayer layer;
608 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200609 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500610 if (ret) {
611 ALOGE("Failed to import layer, ret=%d", ret);
612 return HWC2::Error::NoResources;
613 }
614 map.layers.emplace_back(std::move(layer));
615 }
Sean Paulac874152016-03-10 16:00:26 -0500616
Sean Paulf72cccd2018-08-27 13:59:08 -0400617 std::unique_ptr<DrmDisplayComposition> composition = compositor_
618 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500619 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
620
621 // TODO: Don't always assume geometry changed
622 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
623 if (ret) {
624 ALOGE("Failed to set layers in the composition ret=%d", ret);
625 return HWC2::Error::BadLayer;
626 }
627
628 std::vector<DrmPlane *> primary_planes(primary_planes_);
629 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500630 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500631 if (ret) {
632 ALOGE("Failed to plan the composition ret=%d", ret);
633 return HWC2::Error::BadConfig;
634 }
635
636 // Disable the planes we're not using
637 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
638 composition->AddPlaneDisable(*i);
639 i = primary_planes.erase(i);
640 }
641 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
642 composition->AddPlaneDisable(*i);
643 i = overlay_planes.erase(i);
644 }
645
Rob Herring4f6c62e2018-05-17 14:33:02 -0500646 if (test) {
647 ret = compositor_.TestComposition(composition.get());
648 } else {
649 AddFenceToRetireFence(composition->take_out_fence());
650 ret = compositor_.ApplyComposition(std::move(composition));
651 }
Sean Paulac874152016-03-10 16:00:26 -0500652 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700653 if (!test)
654 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500655 return HWC2::Error::BadParameter;
656 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500657 return HWC2::Error::None;
658}
659
660HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *retire_fence) {
661 supported(__func__);
662 HWC2::Error ret;
663
664 ret = CreateComposition(false);
665 if (ret == HWC2::Error::BadLayer) {
666 // Can we really have no client or device layers?
667 *retire_fence = -1;
668 return HWC2::Error::None;
669 }
670 if (ret != HWC2::Error::None)
671 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500672
Sean Paulac874152016-03-10 16:00:26 -0500673 // The retire fence returned here is for the last frame, so return it and
674 // promote the next retire fence
675 *retire_fence = retire_fence_.Release();
676 retire_fence_ = std::move(next_retire_fence_);
677
678 ++frame_no_;
679 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500680}
681
682HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500683 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400684 auto mode = std::find_if(connector_->modes().begin(),
685 connector_->modes().end(),
686 [config](DrmMode const &m) {
687 return m.id() == config;
688 });
Sean Paulac874152016-03-10 16:00:26 -0500689 if (mode == connector_->modes().end()) {
690 ALOGE("Could not find active mode for %d", config);
691 return HWC2::Error::BadConfig;
692 }
693
Sean Paulf72cccd2018-08-27 13:59:08 -0400694 std::unique_ptr<DrmDisplayComposition> composition = compositor_
695 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500696 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
697 int ret = composition->SetDisplayMode(*mode);
Sean Pauled45a8e2017-02-28 13:17:34 -0500698 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500699 if (ret) {
700 ALOGE("Failed to queue dpms composition on %d", ret);
701 return HWC2::Error::BadConfig;
702 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300703
704 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500705
706 // Setup the client layer's dimensions
707 hwc_rect_t display_frame = {.left = 0,
708 .top = 0,
709 .right = static_cast<int>(mode->h_display()),
710 .bottom = static_cast<int>(mode->v_display())};
711 client_layer_.SetLayerDisplayFrame(display_frame);
712 hwc_frect_t source_crop = {.left = 0.0f,
713 .top = 0.0f,
714 .right = mode->h_display() + 0.0f,
715 .bottom = mode->v_display() + 0.0f};
716 client_layer_.SetLayerSourceCrop(source_crop);
717
718 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500719}
720
721HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
722 int32_t acquire_fence,
723 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600724 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500725 supported(__func__);
726 UniqueFd uf(acquire_fence);
727
728 client_layer_.set_buffer(target);
729 client_layer_.set_acquire_fence(uf.get());
730 client_layer_.SetLayerDataspace(dataspace);
731 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500732}
733
734HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500735 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800736
737 if (mode != HAL_COLOR_MODE_NATIVE)
Vincent Donnefort7834a892019-10-09 15:53:56 +0100738 return HWC2::Error::BadParameter;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800739
740 color_mode_ = mode;
741 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500742}
743
744HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500745 int32_t hint) {
746 supported(__func__);
747 // TODO: Force client composition if we get this
Sean Pauled2ec4b2016-03-10 15:35:40 -0500748 return unsupported(__func__, matrix, hint);
749}
750
751HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500752 int32_t release_fence) {
753 supported(__func__);
754 // TODO: Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500755 return unsupported(__func__, buffer, release_fence);
756}
757
Sean Paulac874152016-03-10 16:00:26 -0500758HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
759 supported(__func__);
760 uint64_t dpms_value = 0;
761 auto mode = static_cast<HWC2::PowerMode>(mode_in);
762 switch (mode) {
763 case HWC2::PowerMode::Off:
764 dpms_value = DRM_MODE_DPMS_OFF;
765 break;
766 case HWC2::PowerMode::On:
767 dpms_value = DRM_MODE_DPMS_ON;
768 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100769 case HWC2::PowerMode::Doze:
770 case HWC2::PowerMode::DozeSuspend:
771 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500772 default:
773 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100774 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500775 };
776
Sean Paulf72cccd2018-08-27 13:59:08 -0400777 std::unique_ptr<DrmDisplayComposition> composition = compositor_
778 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500779 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
780 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500781 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500782 if (ret) {
783 ALOGE("Failed to apply the dpms composition ret=%d", ret);
784 return HWC2::Error::BadParameter;
785 }
786 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500787}
788
789HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500790 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300791 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500792 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500793}
794
795HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500796 uint32_t *num_requests) {
797 supported(__func__);
798 *num_types = 0;
799 *num_requests = 0;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500800 size_t avail_planes = primary_planes_.size() + overlay_planes_.size();
John Stultz76ca20e2018-07-06 10:34:42 -0700801 bool comp_failed = false;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500802
803 HWC2::Error ret;
804
805 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
806 l.second.set_validated_type(HWC2::Composition::Invalid);
807
808 ret = CreateComposition(true);
809 if (ret != HWC2::Error::None)
John Stultz76ca20e2018-07-06 10:34:42 -0700810 comp_failed = true;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500811
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100812 std::map<uint32_t, DrmHwcTwo::HwcLayer *, std::greater<int>> z_map;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500813 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
814 if (l.second.sf_type() == HWC2::Composition::Device)
815 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
816 }
817
818 /*
819 * If more layers then planes, save one plane
820 * for client composited layers
821 */
822 if (avail_planes < layers_.size())
823 avail_planes--;
824
825 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
John Stultz76ca20e2018-07-06 10:34:42 -0700826 if (comp_failed || !avail_planes--)
Rob Herring4f6c62e2018-05-17 14:33:02 -0500827 break;
Alexey Firago18ec6882018-11-21 23:47:05 +0300828 if (importer_->CanImportBuffer(l.second->buffer()))
829 l.second->set_validated_type(HWC2::Composition::Device);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500830 }
831
Sean Paulac874152016-03-10 16:00:26 -0500832 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
833 DrmHwcTwo::HwcLayer &layer = l.second;
John Stultz734ee1c2019-01-09 14:54:52 -0800834 // We can only handle layers of Device type, send everything else to SF
835 if (layer.sf_type() != HWC2::Composition::Device ||
836 layer.validated_type() != HWC2::Composition::Device) {
837 layer.set_validated_type(HWC2::Composition::Client);
838 ++*num_types;
Sean Paulac874152016-03-10 16:00:26 -0500839 }
840 }
Rob Herringee8f45b2017-06-09 15:15:55 -0500841 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500842}
843
844HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -0500845 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800846 cursor_x_ = x;
847 cursor_y_ = y;
848 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500849}
850
851HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500852 supported(__func__);
853 blending_ = static_cast<HWC2::BlendMode>(mode);
854 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500855}
856
857HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500858 int32_t acquire_fence) {
859 supported(__func__);
860 UniqueFd uf(acquire_fence);
861
862 // The buffer and acquire_fence are handled elsewhere
863 if (sf_type_ == HWC2::Composition::Client ||
864 sf_type_ == HWC2::Composition::Sideband ||
865 sf_type_ == HWC2::Composition::SolidColor)
866 return HWC2::Error::None;
867
868 set_buffer(buffer);
869 set_acquire_fence(uf.get());
870 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500871}
872
873HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Sean Paulac874152016-03-10 16:00:26 -0500874 // TODO: Punt to client composition here?
Sean Pauled2ec4b2016-03-10 15:35:40 -0500875 return unsupported(__func__, color);
876}
877
878HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -0500879 sf_type_ = static_cast<HWC2::Composition>(type);
880 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500881}
882
883HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -0500884 supported(__func__);
885 dataspace_ = static_cast<android_dataspace_t>(dataspace);
886 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500887}
888
889HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -0500890 supported(__func__);
891 display_frame_ = frame;
892 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500893}
894
895HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -0500896 supported(__func__);
897 alpha_ = alpha;
898 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500899}
900
901HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
902 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -0500903 supported(__func__);
904 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -0500905 return unsupported(__func__, stream);
906}
907
908HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -0500909 supported(__func__);
910 source_crop_ = crop;
911 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500912}
913
914HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -0500915 supported(__func__);
916 // TODO: We don't use surface damage, marking as unsupported
917 unsupported(__func__, damage);
918 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500919}
920
921HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -0500922 supported(__func__);
923 transform_ = static_cast<HWC2::Transform>(transform);
924 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500925}
926
927HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -0500928 supported(__func__);
929 // TODO: We don't use this information, marking as unsupported
930 unsupported(__func__, visible);
931 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500932}
933
Sean Paulac874152016-03-10 16:00:26 -0500934HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
935 supported(__func__);
936 z_order_ = order;
937 return HWC2::Error::None;
938}
939
940void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
941 supported(__func__);
942 switch (blending_) {
943 case HWC2::BlendMode::None:
944 layer->blending = DrmHwcBlending::kNone;
945 break;
946 case HWC2::BlendMode::Premultiplied:
947 layer->blending = DrmHwcBlending::kPreMult;
948 break;
949 case HWC2::BlendMode::Coverage:
950 layer->blending = DrmHwcBlending::kCoverage;
951 break;
952 default:
953 ALOGE("Unknown blending mode b=%d", blending_);
954 layer->blending = DrmHwcBlending::kNone;
955 break;
956 }
957
958 OutputFd release_fence = release_fence_output();
959
960 layer->sf_handle = buffer_;
961 layer->acquire_fence = acquire_fence_.Release();
962 layer->release_fence = std::move(release_fence);
963 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +0200964 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -0500965 layer->SetSourceCrop(source_crop_);
966 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -0500967}
968
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300969void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
970 auto cb = callbacks_.find(HWC2::Callback::Hotplug);
971 if (cb == callbacks_.end())
972 return;
973
974 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(cb->second.func);
975 hotplug(cb->second.data, displayid,
976 (state == DRM_MODE_CONNECTED ? HWC2_CONNECTION_CONNECTED
977 : HWC2_CONNECTION_DISCONNECTED));
978}
979
980void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
981 for (auto &conn : drmDevice->connectors()) {
982 if (conn->state() != DRM_MODE_CONNECTED)
983 continue;
984 HandleDisplayHotplug(conn->display(), conn->state());
985 }
986}
987
988void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
989 for (auto &conn : drm_->connectors()) {
990 drmModeConnection old_state = conn->state();
991 drmModeConnection cur_state = conn->UpdateModes()
992 ? DRM_MODE_UNKNOWNCONNECTION
993 : conn->state();
994
995 if (cur_state == old_state)
996 continue;
997
998 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
999 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1000 conn->id(), conn->display());
1001
1002 int display_id = conn->display();
1003 if (cur_state == DRM_MODE_CONNECTED) {
1004 auto &display = hwc2_->displays_.at(display_id);
1005 display.ChosePreferredConfig();
1006 } else {
1007 auto &display = hwc2_->displays_.at(display_id);
1008 display.ClearDisplay();
1009 }
1010
1011 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1012 }
1013}
1014
Sean Pauled2ec4b2016-03-10 15:35:40 -05001015// static
1016int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1017 unsupported(__func__);
1018 return 0;
1019}
1020
1021// static
1022void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001023 uint32_t *out_count,
1024 int32_t * /*out_capabilities*/) {
1025 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001026 *out_count = 0;
1027}
1028
1029// static
Sean Paulac874152016-03-10 16:00:26 -05001030hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1031 struct hwc2_device * /*dev*/, int32_t descriptor) {
1032 supported(__func__);
1033 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001034 switch (func) {
1035 // Device functions
1036 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1037 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1038 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1039 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001040 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001041 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1042 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1043 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1044 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1045 case HWC2::FunctionDescriptor::Dump:
1046 return ToHook<HWC2_PFN_DUMP>(
1047 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1048 uint32_t *, char *>);
1049 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1050 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1051 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1052 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1053 case HWC2::FunctionDescriptor::RegisterCallback:
1054 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1055 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1056 &DrmHwcTwo::RegisterCallback, int32_t,
1057 hwc2_callback_data_t, hwc2_function_pointer_t>);
1058
1059 // Display functions
1060 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1061 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1062 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1063 &HwcDisplay::AcceptDisplayChanges>);
1064 case HWC2::FunctionDescriptor::CreateLayer:
1065 return ToHook<HWC2_PFN_CREATE_LAYER>(
1066 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1067 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1068 case HWC2::FunctionDescriptor::DestroyLayer:
1069 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1070 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1071 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1072 case HWC2::FunctionDescriptor::GetActiveConfig:
1073 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1074 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1075 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1076 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1077 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1078 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1079 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1080 hwc2_layer_t *, int32_t *>);
1081 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1082 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1083 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1084 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1085 int32_t, int32_t>);
1086 case HWC2::FunctionDescriptor::GetColorModes:
1087 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1088 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1089 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1090 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001091 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1092 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1093 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1094 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001095 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001096 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1097 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1098 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1099 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001100 case HWC2::FunctionDescriptor::GetDisplayName:
1101 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1102 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1103 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1104 case HWC2::FunctionDescriptor::GetDisplayRequests:
1105 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1106 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1107 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1108 hwc2_layer_t *, int32_t *>);
1109 case HWC2::FunctionDescriptor::GetDisplayType:
1110 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1111 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1112 &HwcDisplay::GetDisplayType, int32_t *>);
1113 case HWC2::FunctionDescriptor::GetDozeSupport:
1114 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1115 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1116 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001117 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1118 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1119 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1120 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1121 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001122 case HWC2::FunctionDescriptor::GetReleaseFences:
1123 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1124 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1125 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1126 int32_t *>);
1127 case HWC2::FunctionDescriptor::PresentDisplay:
1128 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1129 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1130 &HwcDisplay::PresentDisplay, int32_t *>);
1131 case HWC2::FunctionDescriptor::SetActiveConfig:
1132 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1133 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1134 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1135 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001136 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1137 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1138 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1139 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001140 case HWC2::FunctionDescriptor::SetColorMode:
1141 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1142 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1143 &HwcDisplay::SetColorMode, int32_t>);
1144 case HWC2::FunctionDescriptor::SetColorTransform:
1145 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1146 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1147 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1148 case HWC2::FunctionDescriptor::SetOutputBuffer:
1149 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1150 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1151 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1152 case HWC2::FunctionDescriptor::SetPowerMode:
1153 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1154 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1155 &HwcDisplay::SetPowerMode, int32_t>);
1156 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1157 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1158 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1159 &HwcDisplay::SetVsyncEnabled, int32_t>);
1160 case HWC2::FunctionDescriptor::ValidateDisplay:
1161 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1162 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1163 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
1164
1165 // Layer functions
1166 case HWC2::FunctionDescriptor::SetCursorPosition:
1167 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1168 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1169 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1170 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1171 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1172 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1173 &HwcLayer::SetLayerBlendMode, int32_t>);
1174 case HWC2::FunctionDescriptor::SetLayerBuffer:
1175 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1176 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1177 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1178 case HWC2::FunctionDescriptor::SetLayerColor:
1179 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1180 LayerHook<decltype(&HwcLayer::SetLayerColor),
1181 &HwcLayer::SetLayerColor, hwc_color_t>);
1182 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1183 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1184 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1185 &HwcLayer::SetLayerCompositionType, int32_t>);
1186 case HWC2::FunctionDescriptor::SetLayerDataspace:
1187 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1188 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1189 &HwcLayer::SetLayerDataspace, int32_t>);
1190 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1191 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1192 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1193 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1194 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1195 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1196 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1197 &HwcLayer::SetLayerPlaneAlpha, float>);
1198 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001199 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1200 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1201 &HwcLayer::SetLayerSidebandStream,
1202 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001203 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1204 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1205 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1206 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1207 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1208 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1209 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1210 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1211 case HWC2::FunctionDescriptor::SetLayerTransform:
1212 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1213 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1214 &HwcLayer::SetLayerTransform, int32_t>);
1215 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1216 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1217 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1218 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1219 case HWC2::FunctionDescriptor::SetLayerZOrder:
1220 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1221 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1222 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001223 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001224 default:
1225 return NULL;
1226 }
1227}
Sean Paulac874152016-03-10 16:00:26 -05001228
1229// static
1230int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1231 struct hw_device_t **dev) {
1232 supported(__func__);
1233 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1234 ALOGE("Invalid module name- %s", name);
1235 return -EINVAL;
1236 }
1237
1238 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1239 if (!ctx) {
1240 ALOGE("Failed to allocate DrmHwcTwo");
1241 return -ENOMEM;
1242 }
1243
1244 HWC2::Error err = ctx->Init();
1245 if (err != HWC2::Error::None) {
1246 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1247 return -EINVAL;
1248 }
1249
1250 ctx->common.module = const_cast<hw_module_t *>(module);
1251 *dev = &ctx->common;
1252 ctx.release();
1253 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001254}
Sean Paulf72cccd2018-08-27 13:59:08 -04001255} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001256
1257static struct hw_module_methods_t hwc2_module_methods = {
1258 .open = android::DrmHwcTwo::HookDevOpen,
1259};
1260
1261hw_module_t HAL_MODULE_INFO_SYM = {
1262 .tag = HARDWARE_MODULE_TAG,
1263 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1264 .id = HWC_HARDWARE_MODULE_ID,
1265 .name = "DrmHwcTwo module",
1266 .author = "The Android Open Source Project",
1267 .methods = &hwc2_module_methods,
1268 .dso = NULL,
1269 .reserved = {0},
1270};