blob: 95c93de74784d8513ec70c603efc3cde7a61c2df [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__);
276 layers_.erase(layer);
277 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500278}
279
280HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
Sean Paulac874152016-03-10 16:00:26 -0500281 supported(__func__);
282 DrmMode const &mode = connector_->active_mode();
283 if (mode.id() == 0)
284 return HWC2::Error::BadConfig;
285
286 *config = mode.id();
287 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500288}
289
290HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
291 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500292 supported(__func__);
293 uint32_t num_changes = 0;
294 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
295 if (l.second.type_changed()) {
296 if (layers && num_changes < *num_elements)
297 layers[num_changes] = l.first;
298 if (types && num_changes < *num_elements)
299 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
300 ++num_changes;
301 }
302 }
303 if (!layers && !types)
304 *num_elements = num_changes;
305 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500306}
307
308HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500309 uint32_t height,
310 int32_t /*format*/,
311 int32_t dataspace) {
312 supported(__func__);
313 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
314 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
315
316 if (width < min.first || height < min.second)
317 return HWC2::Error::Unsupported;
318
319 if (width > max.first || height > max.second)
320 return HWC2::Error::Unsupported;
321
322 if (dataspace != HAL_DATASPACE_UNKNOWN &&
323 dataspace != HAL_DATASPACE_STANDARD_UNSPECIFIED)
324 return HWC2::Error::Unsupported;
325
326 // TODO: Validate format can be handled by either GL or planes
327 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500328}
329
330HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500331 int32_t *modes) {
332 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800333 if (!modes)
334 *num_modes = 1;
335
336 if (modes)
337 *modes = HAL_COLOR_MODE_NATIVE;
338
339 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500340}
341
342HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500343 int32_t attribute_in,
344 int32_t *value) {
345 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400346 auto mode = std::find_if(connector_->modes().begin(),
347 connector_->modes().end(),
348 [config](DrmMode const &m) {
349 return m.id() == config;
350 });
Sean Paulac874152016-03-10 16:00:26 -0500351 if (mode == connector_->modes().end()) {
352 ALOGE("Could not find active mode for %d", config);
353 return HWC2::Error::BadConfig;
354 }
355
356 static const int32_t kUmPerInch = 25400;
357 uint32_t mm_width = connector_->mm_width();
358 uint32_t mm_height = connector_->mm_height();
359 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
360 switch (attribute) {
361 case HWC2::Attribute::Width:
362 *value = mode->h_display();
363 break;
364 case HWC2::Attribute::Height:
365 *value = mode->v_display();
366 break;
367 case HWC2::Attribute::VsyncPeriod:
368 // in nanoseconds
369 *value = 1000 * 1000 * 1000 / mode->v_refresh();
370 break;
371 case HWC2::Attribute::DpiX:
372 // Dots per 1000 inches
373 *value = mm_width ? (mode->h_display() * kUmPerInch) / mm_width : -1;
374 break;
375 case HWC2::Attribute::DpiY:
376 // Dots per 1000 inches
377 *value = mm_height ? (mode->v_display() * kUmPerInch) / mm_height : -1;
378 break;
379 default:
380 *value = -1;
381 return HWC2::Error::BadConfig;
382 }
383 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500384}
385
386HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
387 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500388 supported(__func__);
389 // Since this callback is normally invoked twice (once to get the count, and
390 // once to populate configs), we don't really want to read the edid
391 // redundantly. Instead, only update the modes on the first invocation. While
392 // it's possible this will result in stale modes, it'll all come out in the
393 // wash when we try to set the active config later.
394 if (!configs) {
395 int ret = connector_->UpdateModes();
396 if (ret) {
397 ALOGE("Failed to update display modes %d", ret);
398 return HWC2::Error::BadDisplay;
399 }
400 }
401
Neil Armstrongb67d0492019-06-20 09:00:21 +0000402 // Since the upper layers only look at vactive/hactive/refresh, height and
403 // width, it doesn't differentiate interlaced from progressive and other
404 // similar modes. Depending on the order of modes we return to SF, it could
405 // end up choosing a suboptimal configuration and dropping the preferred
406 // mode. To workaround this, don't offer interlaced modes to SF if there is
407 // at least one non-interlaced alternative and only offer a single WxH@R
408 // mode with at least the prefered mode from in DrmConnector::UpdateModes()
409
410 // TODO: Remove the following block of code until AOSP handles all modes
411 std::vector<DrmMode> sel_modes;
412
413 // Add the preferred mode first to be sure it's not dropped
414 auto mode = std::find_if(connector_->modes().begin(),
415 connector_->modes().end(), [&](DrmMode const &m) {
416 return m.id() ==
417 connector_->get_preferred_mode_id();
418 });
419 if (mode != connector_->modes().end())
420 sel_modes.push_back(*mode);
421
422 // Add the active mode if different from preferred mode
423 if (connector_->active_mode().id() != connector_->get_preferred_mode_id())
424 sel_modes.push_back(connector_->active_mode());
425
426 // Cycle over the modes and filter out "similar" modes, keeping only the
427 // first ones in the order given by DRM (from CEA ids and timings order)
Sean Paulac874152016-03-10 16:00:26 -0500428 for (const DrmMode &mode : connector_->modes()) {
Neil Armstrongb67d0492019-06-20 09:00:21 +0000429 // TODO: Remove this when 3D Attributes are in AOSP
430 if (mode.flags() & DRM_MODE_FLAG_3D_MASK)
431 continue;
432
Neil Armstrong4c027a72019-06-04 14:48:02 +0000433 // TODO: Remove this when the Interlaced attribute is in AOSP
434 if (mode.flags() & DRM_MODE_FLAG_INTERLACE) {
435 auto m = std::find_if(connector_->modes().begin(),
436 connector_->modes().end(),
437 [&mode](DrmMode const &m) {
438 return !(m.flags() & DRM_MODE_FLAG_INTERLACE) &&
439 m.h_display() == mode.h_display() &&
440 m.v_display() == mode.v_display();
441 });
Neil Armstrongb67d0492019-06-20 09:00:21 +0000442 if (m == connector_->modes().end())
443 sel_modes.push_back(mode);
444
445 continue;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000446 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000447
448 // Search for a similar WxH@R mode in the filtered list and drop it if
449 // another mode with the same WxH@R has already been selected
450 // TODO: Remove this when AOSP handles duplicates modes
451 auto m = std::find_if(sel_modes.begin(), sel_modes.end(),
452 [&mode](DrmMode const &m) {
453 return m.h_display() == mode.h_display() &&
454 m.v_display() == mode.v_display() &&
455 m.v_refresh() == mode.v_refresh();
456 });
457 if (m == sel_modes.end())
458 sel_modes.push_back(mode);
459 }
460
461 auto num_modes = static_cast<uint32_t>(sel_modes.size());
462 if (!configs) {
463 *num_configs = num_modes;
464 return HWC2::Error::None;
465 }
466
467 uint32_t idx = 0;
468 for (const DrmMode &mode : sel_modes) {
469 if (idx >= *num_configs)
470 break;
471 configs[idx++] = mode.id();
Sean Paulac874152016-03-10 16:00:26 -0500472 }
473 *num_configs = idx;
474 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500475}
476
477HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500478 supported(__func__);
479 std::ostringstream stream;
480 stream << "display-" << connector_->id();
481 std::string string = stream.str();
482 size_t length = string.length();
483 if (!name) {
484 *size = length;
485 return HWC2::Error::None;
486 }
487
488 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
489 strncpy(name, string.c_str(), *size);
490 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500491}
492
Sean Paulac874152016-03-10 16:00:26 -0500493HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
494 uint32_t *num_elements,
495 hwc2_layer_t *layers,
496 int32_t *layer_requests) {
497 supported(__func__);
498 // TODO: I think virtual display should request
499 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
500 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
501 *num_elements = 0;
502 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500503}
504
505HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500506 supported(__func__);
507 *type = static_cast<int32_t>(type_);
508 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500509}
510
511HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500512 supported(__func__);
513 *support = 0;
514 return HWC2::Error::None;
515}
516
517HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400518 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
519 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500520 supported(__func__);
521 *num_types = 0;
522 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500523}
524
525HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500526 hwc2_layer_t *layers,
527 int32_t *fences) {
528 supported(__func__);
529 uint32_t num_layers = 0;
530
531 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
532 ++num_layers;
533 if (layers == NULL || fences == NULL) {
534 continue;
535 } else if (num_layers > *num_elements) {
536 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
537 return HWC2::Error::None;
538 }
539
540 layers[num_layers - 1] = l.first;
541 fences[num_layers - 1] = l.second.take_release_fence();
542 }
543 *num_elements = num_layers;
544 return HWC2::Error::None;
545}
546
547void DrmHwcTwo::HwcDisplay::AddFenceToRetireFence(int fd) {
548 supported(__func__);
549 if (fd < 0)
550 return;
551
552 if (next_retire_fence_.get() >= 0) {
553 int old_fence = next_retire_fence_.get();
554 next_retire_fence_.Set(sync_merge("dc_retire", old_fence, fd));
555 } else {
556 next_retire_fence_.Set(dup(fd));
557 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500558}
559
Rob Herring4f6c62e2018-05-17 14:33:02 -0500560HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500561 std::vector<DrmCompositionDisplayLayersMap> layers_map;
562 layers_map.emplace_back();
563 DrmCompositionDisplayLayersMap &map = layers_map.back();
564
565 map.display = static_cast<int>(handle_);
566 map.geometry_changed = true; // TODO: Fix this
567
568 // order the layers by z-order
569 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100570 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500571 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
572 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500573 HWC2::Composition comp_type;
Alexey Firago18ec6882018-11-21 23:47:05 +0300574 if (test) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500575 comp_type = l.second.sf_type();
Alexey Firago18ec6882018-11-21 23:47:05 +0300576 if (comp_type == HWC2::Composition::Device) {
577 if (!importer_->CanImportBuffer(l.second.buffer()))
578 comp_type = HWC2::Composition::Client;
579 }
580 } else
Rob Herring4f6c62e2018-05-17 14:33:02 -0500581 comp_type = l.second.validated_type();
582
583 switch (comp_type) {
Sean Paulac874152016-03-10 16:00:26 -0500584 case HWC2::Composition::Device:
585 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
586 break;
587 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100588 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500589 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100590 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500591 break;
592 default:
593 continue;
594 }
595 }
596 if (use_client_layer)
597 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
598
Rob Herring4f6c62e2018-05-17 14:33:02 -0500599 if (z_map.empty())
600 return HWC2::Error::BadLayer;
601
Sean Paulac874152016-03-10 16:00:26 -0500602 // now that they're ordered by z, add them to the composition
603 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
604 DrmHwcLayer layer;
605 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200606 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500607 if (ret) {
608 ALOGE("Failed to import layer, ret=%d", ret);
609 return HWC2::Error::NoResources;
610 }
611 map.layers.emplace_back(std::move(layer));
612 }
Sean Paulac874152016-03-10 16:00:26 -0500613
Sean Paulf72cccd2018-08-27 13:59:08 -0400614 std::unique_ptr<DrmDisplayComposition> composition = compositor_
615 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500616 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
617
618 // TODO: Don't always assume geometry changed
619 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
620 if (ret) {
621 ALOGE("Failed to set layers in the composition ret=%d", ret);
622 return HWC2::Error::BadLayer;
623 }
624
625 std::vector<DrmPlane *> primary_planes(primary_planes_);
626 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500627 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500628 if (ret) {
629 ALOGE("Failed to plan the composition ret=%d", ret);
630 return HWC2::Error::BadConfig;
631 }
632
633 // Disable the planes we're not using
634 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
635 composition->AddPlaneDisable(*i);
636 i = primary_planes.erase(i);
637 }
638 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
639 composition->AddPlaneDisable(*i);
640 i = overlay_planes.erase(i);
641 }
642
Rob Herring4f6c62e2018-05-17 14:33:02 -0500643 if (test) {
644 ret = compositor_.TestComposition(composition.get());
645 } else {
646 AddFenceToRetireFence(composition->take_out_fence());
647 ret = compositor_.ApplyComposition(std::move(composition));
648 }
Sean Paulac874152016-03-10 16:00:26 -0500649 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700650 if (!test)
651 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500652 return HWC2::Error::BadParameter;
653 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500654 return HWC2::Error::None;
655}
656
657HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *retire_fence) {
658 supported(__func__);
659 HWC2::Error ret;
660
661 ret = CreateComposition(false);
662 if (ret == HWC2::Error::BadLayer) {
663 // Can we really have no client or device layers?
664 *retire_fence = -1;
665 return HWC2::Error::None;
666 }
667 if (ret != HWC2::Error::None)
668 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500669
Sean Paulac874152016-03-10 16:00:26 -0500670 // The retire fence returned here is for the last frame, so return it and
671 // promote the next retire fence
672 *retire_fence = retire_fence_.Release();
673 retire_fence_ = std::move(next_retire_fence_);
674
675 ++frame_no_;
676 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500677}
678
679HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500680 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400681 auto mode = std::find_if(connector_->modes().begin(),
682 connector_->modes().end(),
683 [config](DrmMode const &m) {
684 return m.id() == config;
685 });
Sean Paulac874152016-03-10 16:00:26 -0500686 if (mode == connector_->modes().end()) {
687 ALOGE("Could not find active mode for %d", config);
688 return HWC2::Error::BadConfig;
689 }
690
Sean Paulf72cccd2018-08-27 13:59:08 -0400691 std::unique_ptr<DrmDisplayComposition> composition = compositor_
692 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500693 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
694 int ret = composition->SetDisplayMode(*mode);
Sean Pauled45a8e2017-02-28 13:17:34 -0500695 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500696 if (ret) {
697 ALOGE("Failed to queue dpms composition on %d", ret);
698 return HWC2::Error::BadConfig;
699 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300700
701 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500702
703 // Setup the client layer's dimensions
704 hwc_rect_t display_frame = {.left = 0,
705 .top = 0,
706 .right = static_cast<int>(mode->h_display()),
707 .bottom = static_cast<int>(mode->v_display())};
708 client_layer_.SetLayerDisplayFrame(display_frame);
709 hwc_frect_t source_crop = {.left = 0.0f,
710 .top = 0.0f,
711 .right = mode->h_display() + 0.0f,
712 .bottom = mode->v_display() + 0.0f};
713 client_layer_.SetLayerSourceCrop(source_crop);
714
715 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500716}
717
718HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
719 int32_t acquire_fence,
720 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600721 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500722 supported(__func__);
723 UniqueFd uf(acquire_fence);
724
725 client_layer_.set_buffer(target);
726 client_layer_.set_acquire_fence(uf.get());
727 client_layer_.SetLayerDataspace(dataspace);
728 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500729}
730
731HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500732 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800733
734 if (mode != HAL_COLOR_MODE_NATIVE)
Vincent Donnefort7834a892019-10-09 15:53:56 +0100735 return HWC2::Error::BadParameter;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800736
737 color_mode_ = mode;
738 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500739}
740
741HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500742 int32_t hint) {
743 supported(__func__);
744 // TODO: Force client composition if we get this
Sean Pauled2ec4b2016-03-10 15:35:40 -0500745 return unsupported(__func__, matrix, hint);
746}
747
748HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500749 int32_t release_fence) {
750 supported(__func__);
751 // TODO: Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500752 return unsupported(__func__, buffer, release_fence);
753}
754
Sean Paulac874152016-03-10 16:00:26 -0500755HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
756 supported(__func__);
757 uint64_t dpms_value = 0;
758 auto mode = static_cast<HWC2::PowerMode>(mode_in);
759 switch (mode) {
760 case HWC2::PowerMode::Off:
761 dpms_value = DRM_MODE_DPMS_OFF;
762 break;
763 case HWC2::PowerMode::On:
764 dpms_value = DRM_MODE_DPMS_ON;
765 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100766 case HWC2::PowerMode::Doze:
767 case HWC2::PowerMode::DozeSuspend:
768 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500769 default:
770 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100771 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500772 };
773
Sean Paulf72cccd2018-08-27 13:59:08 -0400774 std::unique_ptr<DrmDisplayComposition> composition = compositor_
775 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500776 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
777 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500778 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500779 if (ret) {
780 ALOGE("Failed to apply the dpms composition ret=%d", ret);
781 return HWC2::Error::BadParameter;
782 }
783 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500784}
785
786HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500787 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300788 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500789 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500790}
791
792HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500793 uint32_t *num_requests) {
794 supported(__func__);
795 *num_types = 0;
796 *num_requests = 0;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500797 size_t avail_planes = primary_planes_.size() + overlay_planes_.size();
John Stultz76ca20e2018-07-06 10:34:42 -0700798 bool comp_failed = false;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500799
800 HWC2::Error ret;
801
802 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
803 l.second.set_validated_type(HWC2::Composition::Invalid);
804
805 ret = CreateComposition(true);
806 if (ret != HWC2::Error::None)
John Stultz76ca20e2018-07-06 10:34:42 -0700807 comp_failed = true;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500808
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100809 std::map<uint32_t, DrmHwcTwo::HwcLayer *, std::greater<int>> z_map;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500810 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
811 if (l.second.sf_type() == HWC2::Composition::Device)
812 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
813 }
814
815 /*
816 * If more layers then planes, save one plane
817 * for client composited layers
818 */
819 if (avail_planes < layers_.size())
820 avail_planes--;
821
822 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
John Stultz76ca20e2018-07-06 10:34:42 -0700823 if (comp_failed || !avail_planes--)
Rob Herring4f6c62e2018-05-17 14:33:02 -0500824 break;
Alexey Firago18ec6882018-11-21 23:47:05 +0300825 if (importer_->CanImportBuffer(l.second->buffer()))
826 l.second->set_validated_type(HWC2::Composition::Device);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500827 }
828
Sean Paulac874152016-03-10 16:00:26 -0500829 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
830 DrmHwcTwo::HwcLayer &layer = l.second;
John Stultz734ee1c2019-01-09 14:54:52 -0800831 // We can only handle layers of Device type, send everything else to SF
832 if (layer.sf_type() != HWC2::Composition::Device ||
833 layer.validated_type() != HWC2::Composition::Device) {
834 layer.set_validated_type(HWC2::Composition::Client);
835 ++*num_types;
Sean Paulac874152016-03-10 16:00:26 -0500836 }
837 }
Rob Herringee8f45b2017-06-09 15:15:55 -0500838 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500839}
840
841HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -0500842 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800843 cursor_x_ = x;
844 cursor_y_ = y;
845 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500846}
847
848HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500849 supported(__func__);
850 blending_ = static_cast<HWC2::BlendMode>(mode);
851 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500852}
853
854HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500855 int32_t acquire_fence) {
856 supported(__func__);
857 UniqueFd uf(acquire_fence);
858
859 // The buffer and acquire_fence are handled elsewhere
860 if (sf_type_ == HWC2::Composition::Client ||
861 sf_type_ == HWC2::Composition::Sideband ||
862 sf_type_ == HWC2::Composition::SolidColor)
863 return HWC2::Error::None;
864
865 set_buffer(buffer);
866 set_acquire_fence(uf.get());
867 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500868}
869
870HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Sean Paulac874152016-03-10 16:00:26 -0500871 // TODO: Punt to client composition here?
Sean Pauled2ec4b2016-03-10 15:35:40 -0500872 return unsupported(__func__, color);
873}
874
875HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -0500876 sf_type_ = static_cast<HWC2::Composition>(type);
877 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500878}
879
880HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -0500881 supported(__func__);
882 dataspace_ = static_cast<android_dataspace_t>(dataspace);
883 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500884}
885
886HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -0500887 supported(__func__);
888 display_frame_ = frame;
889 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500890}
891
892HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -0500893 supported(__func__);
894 alpha_ = alpha;
895 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500896}
897
898HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
899 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -0500900 supported(__func__);
901 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -0500902 return unsupported(__func__, stream);
903}
904
905HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -0500906 supported(__func__);
907 source_crop_ = crop;
908 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500909}
910
911HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -0500912 supported(__func__);
913 // TODO: We don't use surface damage, marking as unsupported
914 unsupported(__func__, damage);
915 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500916}
917
918HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -0500919 supported(__func__);
920 transform_ = static_cast<HWC2::Transform>(transform);
921 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500922}
923
924HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -0500925 supported(__func__);
926 // TODO: We don't use this information, marking as unsupported
927 unsupported(__func__, visible);
928 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500929}
930
Sean Paulac874152016-03-10 16:00:26 -0500931HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
932 supported(__func__);
933 z_order_ = order;
934 return HWC2::Error::None;
935}
936
937void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
938 supported(__func__);
939 switch (blending_) {
940 case HWC2::BlendMode::None:
941 layer->blending = DrmHwcBlending::kNone;
942 break;
943 case HWC2::BlendMode::Premultiplied:
944 layer->blending = DrmHwcBlending::kPreMult;
945 break;
946 case HWC2::BlendMode::Coverage:
947 layer->blending = DrmHwcBlending::kCoverage;
948 break;
949 default:
950 ALOGE("Unknown blending mode b=%d", blending_);
951 layer->blending = DrmHwcBlending::kNone;
952 break;
953 }
954
955 OutputFd release_fence = release_fence_output();
956
957 layer->sf_handle = buffer_;
958 layer->acquire_fence = acquire_fence_.Release();
959 layer->release_fence = std::move(release_fence);
960 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +0200961 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -0500962 layer->SetSourceCrop(source_crop_);
963 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -0500964}
965
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300966void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
967 auto cb = callbacks_.find(HWC2::Callback::Hotplug);
968 if (cb == callbacks_.end())
969 return;
970
971 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(cb->second.func);
972 hotplug(cb->second.data, displayid,
973 (state == DRM_MODE_CONNECTED ? HWC2_CONNECTION_CONNECTED
974 : HWC2_CONNECTION_DISCONNECTED));
975}
976
977void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
978 for (auto &conn : drmDevice->connectors()) {
979 if (conn->state() != DRM_MODE_CONNECTED)
980 continue;
981 HandleDisplayHotplug(conn->display(), conn->state());
982 }
983}
984
985void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
986 for (auto &conn : drm_->connectors()) {
987 drmModeConnection old_state = conn->state();
988 drmModeConnection cur_state = conn->UpdateModes()
989 ? DRM_MODE_UNKNOWNCONNECTION
990 : conn->state();
991
992 if (cur_state == old_state)
993 continue;
994
995 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
996 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
997 conn->id(), conn->display());
998
999 int display_id = conn->display();
1000 if (cur_state == DRM_MODE_CONNECTED) {
1001 auto &display = hwc2_->displays_.at(display_id);
1002 display.ChosePreferredConfig();
1003 } else {
1004 auto &display = hwc2_->displays_.at(display_id);
1005 display.ClearDisplay();
1006 }
1007
1008 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1009 }
1010}
1011
Sean Pauled2ec4b2016-03-10 15:35:40 -05001012// static
1013int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1014 unsupported(__func__);
1015 return 0;
1016}
1017
1018// static
1019void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001020 uint32_t *out_count,
1021 int32_t * /*out_capabilities*/) {
1022 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001023 *out_count = 0;
1024}
1025
1026// static
Sean Paulac874152016-03-10 16:00:26 -05001027hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1028 struct hwc2_device * /*dev*/, int32_t descriptor) {
1029 supported(__func__);
1030 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001031 switch (func) {
1032 // Device functions
1033 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1034 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1035 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1036 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001037 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001038 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1039 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1040 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1041 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1042 case HWC2::FunctionDescriptor::Dump:
1043 return ToHook<HWC2_PFN_DUMP>(
1044 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1045 uint32_t *, char *>);
1046 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1047 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1048 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1049 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1050 case HWC2::FunctionDescriptor::RegisterCallback:
1051 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1052 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1053 &DrmHwcTwo::RegisterCallback, int32_t,
1054 hwc2_callback_data_t, hwc2_function_pointer_t>);
1055
1056 // Display functions
1057 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1058 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1059 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1060 &HwcDisplay::AcceptDisplayChanges>);
1061 case HWC2::FunctionDescriptor::CreateLayer:
1062 return ToHook<HWC2_PFN_CREATE_LAYER>(
1063 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1064 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1065 case HWC2::FunctionDescriptor::DestroyLayer:
1066 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1067 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1068 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1069 case HWC2::FunctionDescriptor::GetActiveConfig:
1070 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1071 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1072 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1073 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1074 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1075 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1076 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1077 hwc2_layer_t *, int32_t *>);
1078 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1079 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1080 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1081 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1082 int32_t, int32_t>);
1083 case HWC2::FunctionDescriptor::GetColorModes:
1084 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1085 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1086 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1087 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001088 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1089 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1090 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1091 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001092 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001093 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1094 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1095 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1096 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001097 case HWC2::FunctionDescriptor::GetDisplayName:
1098 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1099 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1100 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1101 case HWC2::FunctionDescriptor::GetDisplayRequests:
1102 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1103 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1104 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1105 hwc2_layer_t *, int32_t *>);
1106 case HWC2::FunctionDescriptor::GetDisplayType:
1107 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1108 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1109 &HwcDisplay::GetDisplayType, int32_t *>);
1110 case HWC2::FunctionDescriptor::GetDozeSupport:
1111 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1112 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1113 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001114 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1115 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1116 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1117 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1118 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001119 case HWC2::FunctionDescriptor::GetReleaseFences:
1120 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1121 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1122 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1123 int32_t *>);
1124 case HWC2::FunctionDescriptor::PresentDisplay:
1125 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1126 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1127 &HwcDisplay::PresentDisplay, int32_t *>);
1128 case HWC2::FunctionDescriptor::SetActiveConfig:
1129 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1130 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1131 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1132 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001133 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1134 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1135 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1136 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001137 case HWC2::FunctionDescriptor::SetColorMode:
1138 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1139 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1140 &HwcDisplay::SetColorMode, int32_t>);
1141 case HWC2::FunctionDescriptor::SetColorTransform:
1142 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1143 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1144 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1145 case HWC2::FunctionDescriptor::SetOutputBuffer:
1146 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1147 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1148 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1149 case HWC2::FunctionDescriptor::SetPowerMode:
1150 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1151 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1152 &HwcDisplay::SetPowerMode, int32_t>);
1153 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1154 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1155 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1156 &HwcDisplay::SetVsyncEnabled, int32_t>);
1157 case HWC2::FunctionDescriptor::ValidateDisplay:
1158 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1159 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1160 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
1161
1162 // Layer functions
1163 case HWC2::FunctionDescriptor::SetCursorPosition:
1164 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1165 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1166 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1167 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1168 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1169 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1170 &HwcLayer::SetLayerBlendMode, int32_t>);
1171 case HWC2::FunctionDescriptor::SetLayerBuffer:
1172 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1173 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1174 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1175 case HWC2::FunctionDescriptor::SetLayerColor:
1176 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1177 LayerHook<decltype(&HwcLayer::SetLayerColor),
1178 &HwcLayer::SetLayerColor, hwc_color_t>);
1179 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1180 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1181 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1182 &HwcLayer::SetLayerCompositionType, int32_t>);
1183 case HWC2::FunctionDescriptor::SetLayerDataspace:
1184 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1185 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1186 &HwcLayer::SetLayerDataspace, int32_t>);
1187 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1188 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1189 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1190 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1191 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1192 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1193 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1194 &HwcLayer::SetLayerPlaneAlpha, float>);
1195 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001196 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1197 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1198 &HwcLayer::SetLayerSidebandStream,
1199 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001200 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1201 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1202 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1203 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1204 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1205 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1206 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1207 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1208 case HWC2::FunctionDescriptor::SetLayerTransform:
1209 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1210 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1211 &HwcLayer::SetLayerTransform, int32_t>);
1212 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1213 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1214 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1215 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1216 case HWC2::FunctionDescriptor::SetLayerZOrder:
1217 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1218 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1219 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001220 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001221 default:
1222 return NULL;
1223 }
1224}
Sean Paulac874152016-03-10 16:00:26 -05001225
1226// static
1227int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1228 struct hw_device_t **dev) {
1229 supported(__func__);
1230 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1231 ALOGE("Invalid module name- %s", name);
1232 return -EINVAL;
1233 }
1234
1235 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1236 if (!ctx) {
1237 ALOGE("Failed to allocate DrmHwcTwo");
1238 return -ENOMEM;
1239 }
1240
1241 HWC2::Error err = ctx->Init();
1242 if (err != HWC2::Error::None) {
1243 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1244 return -EINVAL;
1245 }
1246
1247 ctx->common.module = const_cast<hw_module_t *>(module);
1248 *dev = &ctx->common;
1249 ctx.release();
1250 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001251}
Sean Paulf72cccd2018-08-27 13:59:08 -04001252} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001253
1254static struct hw_module_methods_t hwc2_module_methods = {
1255 .open = android::DrmHwcTwo::HookDevOpen,
1256};
1257
1258hw_module_t HAL_MODULE_INFO_SYM = {
1259 .tag = HARDWARE_MODULE_TAG,
1260 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1261 .id = HWC_HARDWARE_MODULE_ID,
1262 .name = "DrmHwcTwo module",
1263 .author = "The Android Open Source Project",
1264 .methods = &hwc2_module_methods,
1265 .dso = NULL,
1266 .reserved = {0},
1267};