blob: c419758547023b44deac065c35d5e7af9c052acb [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_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200576 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500577 case HWC2::Composition::Device:
578 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
579 break;
580 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100581 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500582 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100583 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500584 break;
585 default:
586 continue;
587 }
588 }
589 if (use_client_layer)
590 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
591
Rob Herring4f6c62e2018-05-17 14:33:02 -0500592 if (z_map.empty())
593 return HWC2::Error::BadLayer;
594
Sean Paulac874152016-03-10 16:00:26 -0500595 // now that they're ordered by z, add them to the composition
596 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
597 DrmHwcLayer layer;
598 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200599 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500600 if (ret) {
601 ALOGE("Failed to import layer, ret=%d", ret);
602 return HWC2::Error::NoResources;
603 }
604 map.layers.emplace_back(std::move(layer));
605 }
Sean Paulac874152016-03-10 16:00:26 -0500606
Sean Paulf72cccd2018-08-27 13:59:08 -0400607 std::unique_ptr<DrmDisplayComposition> composition = compositor_
608 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500609 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
610
611 // TODO: Don't always assume geometry changed
612 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
613 if (ret) {
614 ALOGE("Failed to set layers in the composition ret=%d", ret);
615 return HWC2::Error::BadLayer;
616 }
617
618 std::vector<DrmPlane *> primary_planes(primary_planes_);
619 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500620 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500621 if (ret) {
622 ALOGE("Failed to plan the composition ret=%d", ret);
623 return HWC2::Error::BadConfig;
624 }
625
626 // Disable the planes we're not using
627 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
628 composition->AddPlaneDisable(*i);
629 i = primary_planes.erase(i);
630 }
631 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
632 composition->AddPlaneDisable(*i);
633 i = overlay_planes.erase(i);
634 }
635
Rob Herring4f6c62e2018-05-17 14:33:02 -0500636 if (test) {
637 ret = compositor_.TestComposition(composition.get());
638 } else {
639 AddFenceToRetireFence(composition->take_out_fence());
640 ret = compositor_.ApplyComposition(std::move(composition));
641 }
Sean Paulac874152016-03-10 16:00:26 -0500642 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700643 if (!test)
644 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500645 return HWC2::Error::BadParameter;
646 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500647 return HWC2::Error::None;
648}
649
650HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *retire_fence) {
651 supported(__func__);
652 HWC2::Error ret;
653
654 ret = CreateComposition(false);
655 if (ret == HWC2::Error::BadLayer) {
656 // Can we really have no client or device layers?
657 *retire_fence = -1;
658 return HWC2::Error::None;
659 }
660 if (ret != HWC2::Error::None)
661 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500662
Sean Paulac874152016-03-10 16:00:26 -0500663 // The retire fence returned here is for the last frame, so return it and
664 // promote the next retire fence
665 *retire_fence = retire_fence_.Release();
666 retire_fence_ = std::move(next_retire_fence_);
667
668 ++frame_no_;
669 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500670}
671
672HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500673 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400674 auto mode = std::find_if(connector_->modes().begin(),
675 connector_->modes().end(),
676 [config](DrmMode const &m) {
677 return m.id() == config;
678 });
Sean Paulac874152016-03-10 16:00:26 -0500679 if (mode == connector_->modes().end()) {
680 ALOGE("Could not find active mode for %d", config);
681 return HWC2::Error::BadConfig;
682 }
683
Sean Paulf72cccd2018-08-27 13:59:08 -0400684 std::unique_ptr<DrmDisplayComposition> composition = compositor_
685 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500686 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
687 int ret = composition->SetDisplayMode(*mode);
Sean Pauled45a8e2017-02-28 13:17:34 -0500688 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500689 if (ret) {
690 ALOGE("Failed to queue dpms composition on %d", ret);
691 return HWC2::Error::BadConfig;
692 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300693
694 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500695
696 // Setup the client layer's dimensions
697 hwc_rect_t display_frame = {.left = 0,
698 .top = 0,
699 .right = static_cast<int>(mode->h_display()),
700 .bottom = static_cast<int>(mode->v_display())};
701 client_layer_.SetLayerDisplayFrame(display_frame);
702 hwc_frect_t source_crop = {.left = 0.0f,
703 .top = 0.0f,
704 .right = mode->h_display() + 0.0f,
705 .bottom = mode->v_display() + 0.0f};
706 client_layer_.SetLayerSourceCrop(source_crop);
707
708 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500709}
710
711HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
712 int32_t acquire_fence,
713 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600714 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500715 supported(__func__);
716 UniqueFd uf(acquire_fence);
717
718 client_layer_.set_buffer(target);
719 client_layer_.set_acquire_fence(uf.get());
720 client_layer_.SetLayerDataspace(dataspace);
721 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500722}
723
724HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500725 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800726
727 if (mode != HAL_COLOR_MODE_NATIVE)
Vincent Donnefort7834a892019-10-09 15:53:56 +0100728 return HWC2::Error::BadParameter;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800729
730 color_mode_ = mode;
731 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500732}
733
734HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500735 int32_t hint) {
736 supported(__func__);
737 // TODO: Force client composition if we get this
Sean Pauled2ec4b2016-03-10 15:35:40 -0500738 return unsupported(__func__, matrix, hint);
739}
740
741HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500742 int32_t release_fence) {
743 supported(__func__);
744 // TODO: Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500745 return unsupported(__func__, buffer, release_fence);
746}
747
Sean Paulac874152016-03-10 16:00:26 -0500748HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
749 supported(__func__);
750 uint64_t dpms_value = 0;
751 auto mode = static_cast<HWC2::PowerMode>(mode_in);
752 switch (mode) {
753 case HWC2::PowerMode::Off:
754 dpms_value = DRM_MODE_DPMS_OFF;
755 break;
756 case HWC2::PowerMode::On:
757 dpms_value = DRM_MODE_DPMS_ON;
758 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100759 case HWC2::PowerMode::Doze:
760 case HWC2::PowerMode::DozeSuspend:
761 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500762 default:
763 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100764 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500765 };
766
Sean Paulf72cccd2018-08-27 13:59:08 -0400767 std::unique_ptr<DrmDisplayComposition> composition = compositor_
768 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500769 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
770 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500771 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500772 if (ret) {
773 ALOGE("Failed to apply the dpms composition ret=%d", ret);
774 return HWC2::Error::BadParameter;
775 }
776 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500777}
778
779HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500780 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300781 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500782 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500783}
784
785HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500786 uint32_t *num_requests) {
787 supported(__func__);
788 *num_types = 0;
789 *num_requests = 0;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500790 size_t avail_planes = primary_planes_.size() + overlay_planes_.size();
Rob Herring4f6c62e2018-05-17 14:33:02 -0500791
792 /*
793 * If more layers then planes, save one plane
794 * for client composited layers
795 */
796 if (avail_planes < layers_.size())
797 avail_planes--;
798
Roman Stratiienkof2647232019-11-21 01:58:35 +0200799 std::map<uint32_t, DrmHwcTwo::HwcLayer *, std::greater<int>> z_map;
800 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
801 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
802
803 bool gpu_block = false;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500804 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200805 if (gpu_block || avail_planes == 0 ||
806 l.second->sf_type() != HWC2::Composition::Device ||
807 !importer_->CanImportBuffer(l.second->buffer())) {
808 gpu_block = true;
809 ++*num_types;
810 } else {
811 avail_planes--;
812 }
813
814 l.second->set_validated_type(gpu_block ? HWC2::Composition::Client
815 : HWC2::Composition::Device);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500816 }
817
Roman Stratiienkof2647232019-11-21 01:58:35 +0200818 if (CreateComposition(true) != HWC2::Error::None)
819 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
820 l.second.set_validated_type(HWC2::Composition::Client);
821
Rob Herringee8f45b2017-06-09 15:15:55 -0500822 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500823}
824
825HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -0500826 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800827 cursor_x_ = x;
828 cursor_y_ = y;
829 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500830}
831
832HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500833 supported(__func__);
834 blending_ = static_cast<HWC2::BlendMode>(mode);
835 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500836}
837
838HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500839 int32_t acquire_fence) {
840 supported(__func__);
841 UniqueFd uf(acquire_fence);
842
843 // The buffer and acquire_fence are handled elsewhere
844 if (sf_type_ == HWC2::Composition::Client ||
845 sf_type_ == HWC2::Composition::Sideband ||
846 sf_type_ == HWC2::Composition::SolidColor)
847 return HWC2::Error::None;
848
849 set_buffer(buffer);
850 set_acquire_fence(uf.get());
851 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500852}
853
854HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Sean Paulac874152016-03-10 16:00:26 -0500855 // TODO: Punt to client composition here?
Sean Pauled2ec4b2016-03-10 15:35:40 -0500856 return unsupported(__func__, color);
857}
858
859HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -0500860 sf_type_ = static_cast<HWC2::Composition>(type);
861 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500862}
863
864HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -0500865 supported(__func__);
866 dataspace_ = static_cast<android_dataspace_t>(dataspace);
867 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500868}
869
870HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -0500871 supported(__func__);
872 display_frame_ = frame;
873 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500874}
875
876HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -0500877 supported(__func__);
878 alpha_ = alpha;
879 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500880}
881
882HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
883 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -0500884 supported(__func__);
885 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -0500886 return unsupported(__func__, stream);
887}
888
889HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -0500890 supported(__func__);
891 source_crop_ = crop;
892 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500893}
894
895HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -0500896 supported(__func__);
897 // TODO: We don't use surface damage, marking as unsupported
898 unsupported(__func__, damage);
899 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500900}
901
902HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -0500903 supported(__func__);
904 transform_ = static_cast<HWC2::Transform>(transform);
905 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500906}
907
908HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -0500909 supported(__func__);
910 // TODO: We don't use this information, marking as unsupported
911 unsupported(__func__, visible);
912 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500913}
914
Sean Paulac874152016-03-10 16:00:26 -0500915HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
916 supported(__func__);
917 z_order_ = order;
918 return HWC2::Error::None;
919}
920
921void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
922 supported(__func__);
923 switch (blending_) {
924 case HWC2::BlendMode::None:
925 layer->blending = DrmHwcBlending::kNone;
926 break;
927 case HWC2::BlendMode::Premultiplied:
928 layer->blending = DrmHwcBlending::kPreMult;
929 break;
930 case HWC2::BlendMode::Coverage:
931 layer->blending = DrmHwcBlending::kCoverage;
932 break;
933 default:
934 ALOGE("Unknown blending mode b=%d", blending_);
935 layer->blending = DrmHwcBlending::kNone;
936 break;
937 }
938
939 OutputFd release_fence = release_fence_output();
940
941 layer->sf_handle = buffer_;
942 layer->acquire_fence = acquire_fence_.Release();
943 layer->release_fence = std::move(release_fence);
944 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +0200945 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -0500946 layer->SetSourceCrop(source_crop_);
947 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -0500948}
949
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300950void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
951 auto cb = callbacks_.find(HWC2::Callback::Hotplug);
952 if (cb == callbacks_.end())
953 return;
954
955 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(cb->second.func);
956 hotplug(cb->second.data, displayid,
957 (state == DRM_MODE_CONNECTED ? HWC2_CONNECTION_CONNECTED
958 : HWC2_CONNECTION_DISCONNECTED));
959}
960
961void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
962 for (auto &conn : drmDevice->connectors()) {
963 if (conn->state() != DRM_MODE_CONNECTED)
964 continue;
965 HandleDisplayHotplug(conn->display(), conn->state());
966 }
967}
968
969void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
970 for (auto &conn : drm_->connectors()) {
971 drmModeConnection old_state = conn->state();
972 drmModeConnection cur_state = conn->UpdateModes()
973 ? DRM_MODE_UNKNOWNCONNECTION
974 : conn->state();
975
976 if (cur_state == old_state)
977 continue;
978
979 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
980 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
981 conn->id(), conn->display());
982
983 int display_id = conn->display();
984 if (cur_state == DRM_MODE_CONNECTED) {
985 auto &display = hwc2_->displays_.at(display_id);
986 display.ChosePreferredConfig();
987 } else {
988 auto &display = hwc2_->displays_.at(display_id);
989 display.ClearDisplay();
990 }
991
992 hwc2_->HandleDisplayHotplug(display_id, cur_state);
993 }
994}
995
Sean Pauled2ec4b2016-03-10 15:35:40 -0500996// static
997int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
998 unsupported(__func__);
999 return 0;
1000}
1001
1002// static
1003void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001004 uint32_t *out_count,
1005 int32_t * /*out_capabilities*/) {
1006 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001007 *out_count = 0;
1008}
1009
1010// static
Sean Paulac874152016-03-10 16:00:26 -05001011hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1012 struct hwc2_device * /*dev*/, int32_t descriptor) {
1013 supported(__func__);
1014 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001015 switch (func) {
1016 // Device functions
1017 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1018 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1019 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1020 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001021 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001022 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1023 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1024 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1025 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1026 case HWC2::FunctionDescriptor::Dump:
1027 return ToHook<HWC2_PFN_DUMP>(
1028 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1029 uint32_t *, char *>);
1030 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1031 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1032 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1033 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1034 case HWC2::FunctionDescriptor::RegisterCallback:
1035 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1036 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1037 &DrmHwcTwo::RegisterCallback, int32_t,
1038 hwc2_callback_data_t, hwc2_function_pointer_t>);
1039
1040 // Display functions
1041 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1042 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1043 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1044 &HwcDisplay::AcceptDisplayChanges>);
1045 case HWC2::FunctionDescriptor::CreateLayer:
1046 return ToHook<HWC2_PFN_CREATE_LAYER>(
1047 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1048 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1049 case HWC2::FunctionDescriptor::DestroyLayer:
1050 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1051 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1052 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1053 case HWC2::FunctionDescriptor::GetActiveConfig:
1054 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1055 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1056 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1057 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1058 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1059 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1060 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1061 hwc2_layer_t *, int32_t *>);
1062 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1063 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1064 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1065 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1066 int32_t, int32_t>);
1067 case HWC2::FunctionDescriptor::GetColorModes:
1068 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1069 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1070 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1071 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001072 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1073 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1074 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1075 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001076 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001077 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1078 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1079 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1080 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001081 case HWC2::FunctionDescriptor::GetDisplayName:
1082 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1083 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1084 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1085 case HWC2::FunctionDescriptor::GetDisplayRequests:
1086 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1087 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1088 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1089 hwc2_layer_t *, int32_t *>);
1090 case HWC2::FunctionDescriptor::GetDisplayType:
1091 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1092 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1093 &HwcDisplay::GetDisplayType, int32_t *>);
1094 case HWC2::FunctionDescriptor::GetDozeSupport:
1095 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1096 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1097 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001098 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1099 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1100 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1101 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1102 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001103 case HWC2::FunctionDescriptor::GetReleaseFences:
1104 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1105 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1106 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1107 int32_t *>);
1108 case HWC2::FunctionDescriptor::PresentDisplay:
1109 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1110 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1111 &HwcDisplay::PresentDisplay, int32_t *>);
1112 case HWC2::FunctionDescriptor::SetActiveConfig:
1113 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1114 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1115 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1116 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001117 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1118 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1119 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1120 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001121 case HWC2::FunctionDescriptor::SetColorMode:
1122 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1123 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1124 &HwcDisplay::SetColorMode, int32_t>);
1125 case HWC2::FunctionDescriptor::SetColorTransform:
1126 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1127 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1128 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1129 case HWC2::FunctionDescriptor::SetOutputBuffer:
1130 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1131 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1132 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1133 case HWC2::FunctionDescriptor::SetPowerMode:
1134 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1135 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1136 &HwcDisplay::SetPowerMode, int32_t>);
1137 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1138 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1139 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1140 &HwcDisplay::SetVsyncEnabled, int32_t>);
1141 case HWC2::FunctionDescriptor::ValidateDisplay:
1142 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1143 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1144 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
1145
1146 // Layer functions
1147 case HWC2::FunctionDescriptor::SetCursorPosition:
1148 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1149 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1150 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1151 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1152 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1153 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1154 &HwcLayer::SetLayerBlendMode, int32_t>);
1155 case HWC2::FunctionDescriptor::SetLayerBuffer:
1156 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1157 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1158 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1159 case HWC2::FunctionDescriptor::SetLayerColor:
1160 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1161 LayerHook<decltype(&HwcLayer::SetLayerColor),
1162 &HwcLayer::SetLayerColor, hwc_color_t>);
1163 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1164 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1165 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1166 &HwcLayer::SetLayerCompositionType, int32_t>);
1167 case HWC2::FunctionDescriptor::SetLayerDataspace:
1168 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1169 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1170 &HwcLayer::SetLayerDataspace, int32_t>);
1171 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1172 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1173 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1174 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1175 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1176 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1177 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1178 &HwcLayer::SetLayerPlaneAlpha, float>);
1179 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001180 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1181 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1182 &HwcLayer::SetLayerSidebandStream,
1183 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001184 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1185 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1186 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1187 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1188 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1189 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1190 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1191 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1192 case HWC2::FunctionDescriptor::SetLayerTransform:
1193 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1194 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1195 &HwcLayer::SetLayerTransform, int32_t>);
1196 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1197 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1198 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1199 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1200 case HWC2::FunctionDescriptor::SetLayerZOrder:
1201 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1202 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1203 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001204 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001205 default:
1206 return NULL;
1207 }
1208}
Sean Paulac874152016-03-10 16:00:26 -05001209
1210// static
1211int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1212 struct hw_device_t **dev) {
1213 supported(__func__);
1214 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1215 ALOGE("Invalid module name- %s", name);
1216 return -EINVAL;
1217 }
1218
1219 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1220 if (!ctx) {
1221 ALOGE("Failed to allocate DrmHwcTwo");
1222 return -ENOMEM;
1223 }
1224
1225 HWC2::Error err = ctx->Init();
1226 if (err != HWC2::Error::None) {
1227 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1228 return -EINVAL;
1229 }
1230
1231 ctx->common.module = const_cast<hw_module_t *>(module);
1232 *dev = &ctx->common;
1233 ctx.release();
1234 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001235}
Sean Paulf72cccd2018-08-27 13:59:08 -04001236} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001237
1238static struct hw_module_methods_t hwc2_module_methods = {
1239 .open = android::DrmHwcTwo::HookDevOpen,
1240};
1241
1242hw_module_t HAL_MODULE_INFO_SYM = {
1243 .tag = HARDWARE_MODULE_TAG,
1244 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1245 .id = HWC_HARDWARE_MODULE_ID,
1246 .name = "DrmHwcTwo module",
1247 .author = "The Android Open Source Project",
1248 .methods = &hwc2_module_methods,
1249 .dso = NULL,
1250 .reserved = {0},
1251};