blob: 9268cdc9c9502c67b4ca5c9146cb9506c44c529d [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)
735 return HWC2::Error::Unsupported;
736
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;
766 default:
767 ALOGI("Power mode %d is unsupported\n", mode);
768 return HWC2::Error::Unsupported;
769 };
770
Sean Paulf72cccd2018-08-27 13:59:08 -0400771 std::unique_ptr<DrmDisplayComposition> composition = compositor_
772 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500773 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
774 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500775 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500776 if (ret) {
777 ALOGE("Failed to apply the dpms composition ret=%d", ret);
778 return HWC2::Error::BadParameter;
779 }
780 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500781}
782
783HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500784 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300785 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500786 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500787}
788
789HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500790 uint32_t *num_requests) {
791 supported(__func__);
792 *num_types = 0;
793 *num_requests = 0;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500794 size_t avail_planes = primary_planes_.size() + overlay_planes_.size();
John Stultz76ca20e2018-07-06 10:34:42 -0700795 bool comp_failed = false;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500796
797 HWC2::Error ret;
798
799 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
800 l.second.set_validated_type(HWC2::Composition::Invalid);
801
802 ret = CreateComposition(true);
803 if (ret != HWC2::Error::None)
John Stultz76ca20e2018-07-06 10:34:42 -0700804 comp_failed = true;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500805
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100806 std::map<uint32_t, DrmHwcTwo::HwcLayer *, std::greater<int>> z_map;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500807 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
808 if (l.second.sf_type() == HWC2::Composition::Device)
809 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
810 }
811
812 /*
813 * If more layers then planes, save one plane
814 * for client composited layers
815 */
816 if (avail_planes < layers_.size())
817 avail_planes--;
818
819 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
John Stultz76ca20e2018-07-06 10:34:42 -0700820 if (comp_failed || !avail_planes--)
Rob Herring4f6c62e2018-05-17 14:33:02 -0500821 break;
Alexey Firago18ec6882018-11-21 23:47:05 +0300822 if (importer_->CanImportBuffer(l.second->buffer()))
823 l.second->set_validated_type(HWC2::Composition::Device);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500824 }
825
Sean Paulac874152016-03-10 16:00:26 -0500826 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
827 DrmHwcTwo::HwcLayer &layer = l.second;
John Stultz734ee1c2019-01-09 14:54:52 -0800828 // We can only handle layers of Device type, send everything else to SF
829 if (layer.sf_type() != HWC2::Composition::Device ||
830 layer.validated_type() != HWC2::Composition::Device) {
831 layer.set_validated_type(HWC2::Composition::Client);
832 ++*num_types;
Sean Paulac874152016-03-10 16:00:26 -0500833 }
834 }
Rob Herringee8f45b2017-06-09 15:15:55 -0500835 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500836}
837
838HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -0500839 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800840 cursor_x_ = x;
841 cursor_y_ = y;
842 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500843}
844
845HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500846 supported(__func__);
847 blending_ = static_cast<HWC2::BlendMode>(mode);
848 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500849}
850
851HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500852 int32_t acquire_fence) {
853 supported(__func__);
854 UniqueFd uf(acquire_fence);
855
856 // The buffer and acquire_fence are handled elsewhere
857 if (sf_type_ == HWC2::Composition::Client ||
858 sf_type_ == HWC2::Composition::Sideband ||
859 sf_type_ == HWC2::Composition::SolidColor)
860 return HWC2::Error::None;
861
862 set_buffer(buffer);
863 set_acquire_fence(uf.get());
864 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500865}
866
867HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Sean Paulac874152016-03-10 16:00:26 -0500868 // TODO: Punt to client composition here?
Sean Pauled2ec4b2016-03-10 15:35:40 -0500869 return unsupported(__func__, color);
870}
871
872HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -0500873 sf_type_ = static_cast<HWC2::Composition>(type);
874 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500875}
876
877HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -0500878 supported(__func__);
879 dataspace_ = static_cast<android_dataspace_t>(dataspace);
880 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500881}
882
883HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -0500884 supported(__func__);
885 display_frame_ = frame;
886 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500887}
888
889HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -0500890 supported(__func__);
891 alpha_ = alpha;
892 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500893}
894
895HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
896 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -0500897 supported(__func__);
898 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -0500899 return unsupported(__func__, stream);
900}
901
902HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -0500903 supported(__func__);
904 source_crop_ = crop;
905 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500906}
907
908HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -0500909 supported(__func__);
910 // TODO: We don't use surface damage, marking as unsupported
911 unsupported(__func__, damage);
912 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500913}
914
915HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -0500916 supported(__func__);
917 transform_ = static_cast<HWC2::Transform>(transform);
918 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500919}
920
921HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -0500922 supported(__func__);
923 // TODO: We don't use this information, marking as unsupported
924 unsupported(__func__, visible);
925 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500926}
927
Sean Paulac874152016-03-10 16:00:26 -0500928HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
929 supported(__func__);
930 z_order_ = order;
931 return HWC2::Error::None;
932}
933
934void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
935 supported(__func__);
936 switch (blending_) {
937 case HWC2::BlendMode::None:
938 layer->blending = DrmHwcBlending::kNone;
939 break;
940 case HWC2::BlendMode::Premultiplied:
941 layer->blending = DrmHwcBlending::kPreMult;
942 break;
943 case HWC2::BlendMode::Coverage:
944 layer->blending = DrmHwcBlending::kCoverage;
945 break;
946 default:
947 ALOGE("Unknown blending mode b=%d", blending_);
948 layer->blending = DrmHwcBlending::kNone;
949 break;
950 }
951
952 OutputFd release_fence = release_fence_output();
953
954 layer->sf_handle = buffer_;
955 layer->acquire_fence = acquire_fence_.Release();
956 layer->release_fence = std::move(release_fence);
957 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +0200958 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -0500959 layer->SetSourceCrop(source_crop_);
960 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -0500961}
962
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300963void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
964 auto cb = callbacks_.find(HWC2::Callback::Hotplug);
965 if (cb == callbacks_.end())
966 return;
967
968 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(cb->second.func);
969 hotplug(cb->second.data, displayid,
970 (state == DRM_MODE_CONNECTED ? HWC2_CONNECTION_CONNECTED
971 : HWC2_CONNECTION_DISCONNECTED));
972}
973
974void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
975 for (auto &conn : drmDevice->connectors()) {
976 if (conn->state() != DRM_MODE_CONNECTED)
977 continue;
978 HandleDisplayHotplug(conn->display(), conn->state());
979 }
980}
981
982void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
983 for (auto &conn : drm_->connectors()) {
984 drmModeConnection old_state = conn->state();
985 drmModeConnection cur_state = conn->UpdateModes()
986 ? DRM_MODE_UNKNOWNCONNECTION
987 : conn->state();
988
989 if (cur_state == old_state)
990 continue;
991
992 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
993 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
994 conn->id(), conn->display());
995
996 int display_id = conn->display();
997 if (cur_state == DRM_MODE_CONNECTED) {
998 auto &display = hwc2_->displays_.at(display_id);
999 display.ChosePreferredConfig();
1000 } else {
1001 auto &display = hwc2_->displays_.at(display_id);
1002 display.ClearDisplay();
1003 }
1004
1005 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1006 }
1007}
1008
Sean Pauled2ec4b2016-03-10 15:35:40 -05001009// static
1010int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1011 unsupported(__func__);
1012 return 0;
1013}
1014
1015// static
1016void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001017 uint32_t *out_count,
1018 int32_t * /*out_capabilities*/) {
1019 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001020 *out_count = 0;
1021}
1022
1023// static
Sean Paulac874152016-03-10 16:00:26 -05001024hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1025 struct hwc2_device * /*dev*/, int32_t descriptor) {
1026 supported(__func__);
1027 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001028 switch (func) {
1029 // Device functions
1030 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1031 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1032 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1033 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001034 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001035 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1036 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1037 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1038 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1039 case HWC2::FunctionDescriptor::Dump:
1040 return ToHook<HWC2_PFN_DUMP>(
1041 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1042 uint32_t *, char *>);
1043 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1044 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1045 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1046 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1047 case HWC2::FunctionDescriptor::RegisterCallback:
1048 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1049 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1050 &DrmHwcTwo::RegisterCallback, int32_t,
1051 hwc2_callback_data_t, hwc2_function_pointer_t>);
1052
1053 // Display functions
1054 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1055 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1056 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1057 &HwcDisplay::AcceptDisplayChanges>);
1058 case HWC2::FunctionDescriptor::CreateLayer:
1059 return ToHook<HWC2_PFN_CREATE_LAYER>(
1060 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1061 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1062 case HWC2::FunctionDescriptor::DestroyLayer:
1063 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1064 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1065 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1066 case HWC2::FunctionDescriptor::GetActiveConfig:
1067 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1068 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1069 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1070 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1071 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1072 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1073 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1074 hwc2_layer_t *, int32_t *>);
1075 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1076 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1077 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1078 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1079 int32_t, int32_t>);
1080 case HWC2::FunctionDescriptor::GetColorModes:
1081 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1082 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1083 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1084 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001085 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1086 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1087 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1088 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001089 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001090 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1091 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1092 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1093 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001094 case HWC2::FunctionDescriptor::GetDisplayName:
1095 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1096 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1097 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1098 case HWC2::FunctionDescriptor::GetDisplayRequests:
1099 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1100 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1101 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1102 hwc2_layer_t *, int32_t *>);
1103 case HWC2::FunctionDescriptor::GetDisplayType:
1104 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1105 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1106 &HwcDisplay::GetDisplayType, int32_t *>);
1107 case HWC2::FunctionDescriptor::GetDozeSupport:
1108 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1109 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1110 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001111 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1112 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1113 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1114 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1115 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001116 case HWC2::FunctionDescriptor::GetReleaseFences:
1117 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1118 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1119 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1120 int32_t *>);
1121 case HWC2::FunctionDescriptor::PresentDisplay:
1122 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1123 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1124 &HwcDisplay::PresentDisplay, int32_t *>);
1125 case HWC2::FunctionDescriptor::SetActiveConfig:
1126 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1127 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1128 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1129 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001130 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1131 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1132 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1133 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001134 case HWC2::FunctionDescriptor::SetColorMode:
1135 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1136 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1137 &HwcDisplay::SetColorMode, int32_t>);
1138 case HWC2::FunctionDescriptor::SetColorTransform:
1139 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1140 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1141 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1142 case HWC2::FunctionDescriptor::SetOutputBuffer:
1143 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1144 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1145 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1146 case HWC2::FunctionDescriptor::SetPowerMode:
1147 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1148 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1149 &HwcDisplay::SetPowerMode, int32_t>);
1150 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1151 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1152 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1153 &HwcDisplay::SetVsyncEnabled, int32_t>);
1154 case HWC2::FunctionDescriptor::ValidateDisplay:
1155 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1156 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1157 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
1158
1159 // Layer functions
1160 case HWC2::FunctionDescriptor::SetCursorPosition:
1161 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1162 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1163 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1164 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1165 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1166 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1167 &HwcLayer::SetLayerBlendMode, int32_t>);
1168 case HWC2::FunctionDescriptor::SetLayerBuffer:
1169 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1170 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1171 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1172 case HWC2::FunctionDescriptor::SetLayerColor:
1173 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1174 LayerHook<decltype(&HwcLayer::SetLayerColor),
1175 &HwcLayer::SetLayerColor, hwc_color_t>);
1176 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1177 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1178 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1179 &HwcLayer::SetLayerCompositionType, int32_t>);
1180 case HWC2::FunctionDescriptor::SetLayerDataspace:
1181 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1182 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1183 &HwcLayer::SetLayerDataspace, int32_t>);
1184 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1185 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1186 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1187 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1188 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1189 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1190 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1191 &HwcLayer::SetLayerPlaneAlpha, float>);
1192 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001193 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1194 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1195 &HwcLayer::SetLayerSidebandStream,
1196 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001197 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1198 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1199 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1200 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1201 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1202 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1203 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1204 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1205 case HWC2::FunctionDescriptor::SetLayerTransform:
1206 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1207 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1208 &HwcLayer::SetLayerTransform, int32_t>);
1209 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1210 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1211 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1212 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1213 case HWC2::FunctionDescriptor::SetLayerZOrder:
1214 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1215 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1216 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001217 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001218 default:
1219 return NULL;
1220 }
1221}
Sean Paulac874152016-03-10 16:00:26 -05001222
1223// static
1224int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1225 struct hw_device_t **dev) {
1226 supported(__func__);
1227 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1228 ALOGE("Invalid module name- %s", name);
1229 return -EINVAL;
1230 }
1231
1232 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1233 if (!ctx) {
1234 ALOGE("Failed to allocate DrmHwcTwo");
1235 return -ENOMEM;
1236 }
1237
1238 HWC2::Error err = ctx->Init();
1239 if (err != HWC2::Error::None) {
1240 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1241 return -EINVAL;
1242 }
1243
1244 ctx->common.module = const_cast<hw_module_t *>(module);
1245 *dev = &ctx->common;
1246 ctx.release();
1247 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001248}
Sean Paulf72cccd2018-08-27 13:59:08 -04001249} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001250
1251static struct hw_module_methods_t hwc2_module_methods = {
1252 .open = android::DrmHwcTwo::HookDevOpen,
1253};
1254
1255hw_module_t HAL_MODULE_INFO_SYM = {
1256 .tag = HARDWARE_MODULE_TAG,
1257 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1258 .id = HWC_HARDWARE_MODULE_ID,
1259 .name = "DrmHwcTwo module",
1260 .author = "The Android Open Source Project",
1261 .methods = &hwc2_module_methods,
1262 .dso = NULL,
1263 .reserved = {0},
1264};