blob: b96eb312f652d4190f06162298d2605d41f50bfb [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
Sean Paulac874152016-03-10 16:00:26 -0500402 uint32_t idx = 0;
403 for (const DrmMode &mode : connector_->modes()) {
Neil Armstrong4c027a72019-06-04 14:48:02 +0000404 if (configs && idx >= *num_configs)
Sean Paulac874152016-03-10 16:00:26 -0500405 break;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000406 // Since the upper layers only look at vactive/hactive/refresh, it doesn't
407 // differentiate interlaced from progressive modes. Depending on the order
408 // of modes we return to SF, it could end up choosing a suboptimal
409 // configuration.
410 // To workaround this, don't offer interlaced modes to SF if there is at
411 // least one non-interlaced alternative.
412 //
413 // TODO: Remove this when the Interlaced attribute is in AOSP
414 if (mode.flags() & DRM_MODE_FLAG_INTERLACE) {
415 auto m = std::find_if(connector_->modes().begin(),
416 connector_->modes().end(),
417 [&mode](DrmMode const &m) {
418 return !(m.flags() & DRM_MODE_FLAG_INTERLACE) &&
419 m.h_display() == mode.h_display() &&
420 m.v_display() == mode.v_display();
421 });
422 if (m != connector_->modes().end())
423 continue;
424 }
425 if (configs) {
426 configs[idx++] = mode.id();
427 } else {
428 idx++;
429 }
Sean Paulac874152016-03-10 16:00:26 -0500430 }
431 *num_configs = idx;
432 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500433}
434
435HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500436 supported(__func__);
437 std::ostringstream stream;
438 stream << "display-" << connector_->id();
439 std::string string = stream.str();
440 size_t length = string.length();
441 if (!name) {
442 *size = length;
443 return HWC2::Error::None;
444 }
445
446 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
447 strncpy(name, string.c_str(), *size);
448 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500449}
450
Sean Paulac874152016-03-10 16:00:26 -0500451HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
452 uint32_t *num_elements,
453 hwc2_layer_t *layers,
454 int32_t *layer_requests) {
455 supported(__func__);
456 // TODO: I think virtual display should request
457 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
458 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
459 *num_elements = 0;
460 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500461}
462
463HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500464 supported(__func__);
465 *type = static_cast<int32_t>(type_);
466 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500467}
468
469HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500470 supported(__func__);
471 *support = 0;
472 return HWC2::Error::None;
473}
474
475HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400476 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
477 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500478 supported(__func__);
479 *num_types = 0;
480 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500481}
482
483HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500484 hwc2_layer_t *layers,
485 int32_t *fences) {
486 supported(__func__);
487 uint32_t num_layers = 0;
488
489 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
490 ++num_layers;
491 if (layers == NULL || fences == NULL) {
492 continue;
493 } else if (num_layers > *num_elements) {
494 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
495 return HWC2::Error::None;
496 }
497
498 layers[num_layers - 1] = l.first;
499 fences[num_layers - 1] = l.second.take_release_fence();
500 }
501 *num_elements = num_layers;
502 return HWC2::Error::None;
503}
504
505void DrmHwcTwo::HwcDisplay::AddFenceToRetireFence(int fd) {
506 supported(__func__);
507 if (fd < 0)
508 return;
509
510 if (next_retire_fence_.get() >= 0) {
511 int old_fence = next_retire_fence_.get();
512 next_retire_fence_.Set(sync_merge("dc_retire", old_fence, fd));
513 } else {
514 next_retire_fence_.Set(dup(fd));
515 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500516}
517
Rob Herring4f6c62e2018-05-17 14:33:02 -0500518HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500519 std::vector<DrmCompositionDisplayLayersMap> layers_map;
520 layers_map.emplace_back();
521 DrmCompositionDisplayLayersMap &map = layers_map.back();
522
523 map.display = static_cast<int>(handle_);
524 map.geometry_changed = true; // TODO: Fix this
525
526 // order the layers by z-order
527 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100528 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500529 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
530 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500531 HWC2::Composition comp_type;
Alexey Firago18ec6882018-11-21 23:47:05 +0300532 if (test) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500533 comp_type = l.second.sf_type();
Alexey Firago18ec6882018-11-21 23:47:05 +0300534 if (comp_type == HWC2::Composition::Device) {
535 if (!importer_->CanImportBuffer(l.second.buffer()))
536 comp_type = HWC2::Composition::Client;
537 }
538 } else
Rob Herring4f6c62e2018-05-17 14:33:02 -0500539 comp_type = l.second.validated_type();
540
541 switch (comp_type) {
Sean Paulac874152016-03-10 16:00:26 -0500542 case HWC2::Composition::Device:
543 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
544 break;
545 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100546 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500547 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100548 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500549 break;
550 default:
551 continue;
552 }
553 }
554 if (use_client_layer)
555 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
556
Rob Herring4f6c62e2018-05-17 14:33:02 -0500557 if (z_map.empty())
558 return HWC2::Error::BadLayer;
559
Sean Paulac874152016-03-10 16:00:26 -0500560 // now that they're ordered by z, add them to the composition
561 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
562 DrmHwcLayer layer;
563 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200564 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500565 if (ret) {
566 ALOGE("Failed to import layer, ret=%d", ret);
567 return HWC2::Error::NoResources;
568 }
569 map.layers.emplace_back(std::move(layer));
570 }
Sean Paulac874152016-03-10 16:00:26 -0500571
Sean Paulf72cccd2018-08-27 13:59:08 -0400572 std::unique_ptr<DrmDisplayComposition> composition = compositor_
573 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500574 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
575
576 // TODO: Don't always assume geometry changed
577 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
578 if (ret) {
579 ALOGE("Failed to set layers in the composition ret=%d", ret);
580 return HWC2::Error::BadLayer;
581 }
582
583 std::vector<DrmPlane *> primary_planes(primary_planes_);
584 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500585 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500586 if (ret) {
587 ALOGE("Failed to plan the composition ret=%d", ret);
588 return HWC2::Error::BadConfig;
589 }
590
591 // Disable the planes we're not using
592 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
593 composition->AddPlaneDisable(*i);
594 i = primary_planes.erase(i);
595 }
596 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
597 composition->AddPlaneDisable(*i);
598 i = overlay_planes.erase(i);
599 }
600
Rob Herring4f6c62e2018-05-17 14:33:02 -0500601 if (test) {
602 ret = compositor_.TestComposition(composition.get());
603 } else {
604 AddFenceToRetireFence(composition->take_out_fence());
605 ret = compositor_.ApplyComposition(std::move(composition));
606 }
Sean Paulac874152016-03-10 16:00:26 -0500607 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700608 if (!test)
609 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500610 return HWC2::Error::BadParameter;
611 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500612 return HWC2::Error::None;
613}
614
615HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *retire_fence) {
616 supported(__func__);
617 HWC2::Error ret;
618
619 ret = CreateComposition(false);
620 if (ret == HWC2::Error::BadLayer) {
621 // Can we really have no client or device layers?
622 *retire_fence = -1;
623 return HWC2::Error::None;
624 }
625 if (ret != HWC2::Error::None)
626 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500627
Sean Paulac874152016-03-10 16:00:26 -0500628 // The retire fence returned here is for the last frame, so return it and
629 // promote the next retire fence
630 *retire_fence = retire_fence_.Release();
631 retire_fence_ = std::move(next_retire_fence_);
632
633 ++frame_no_;
634 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500635}
636
637HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500638 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400639 auto mode = std::find_if(connector_->modes().begin(),
640 connector_->modes().end(),
641 [config](DrmMode const &m) {
642 return m.id() == config;
643 });
Sean Paulac874152016-03-10 16:00:26 -0500644 if (mode == connector_->modes().end()) {
645 ALOGE("Could not find active mode for %d", config);
646 return HWC2::Error::BadConfig;
647 }
648
Sean Paulf72cccd2018-08-27 13:59:08 -0400649 std::unique_ptr<DrmDisplayComposition> composition = compositor_
650 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500651 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
652 int ret = composition->SetDisplayMode(*mode);
Sean Pauled45a8e2017-02-28 13:17:34 -0500653 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500654 if (ret) {
655 ALOGE("Failed to queue dpms composition on %d", ret);
656 return HWC2::Error::BadConfig;
657 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300658
659 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500660
661 // Setup the client layer's dimensions
662 hwc_rect_t display_frame = {.left = 0,
663 .top = 0,
664 .right = static_cast<int>(mode->h_display()),
665 .bottom = static_cast<int>(mode->v_display())};
666 client_layer_.SetLayerDisplayFrame(display_frame);
667 hwc_frect_t source_crop = {.left = 0.0f,
668 .top = 0.0f,
669 .right = mode->h_display() + 0.0f,
670 .bottom = mode->v_display() + 0.0f};
671 client_layer_.SetLayerSourceCrop(source_crop);
672
673 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500674}
675
676HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
677 int32_t acquire_fence,
678 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600679 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500680 supported(__func__);
681 UniqueFd uf(acquire_fence);
682
683 client_layer_.set_buffer(target);
684 client_layer_.set_acquire_fence(uf.get());
685 client_layer_.SetLayerDataspace(dataspace);
686 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500687}
688
689HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500690 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800691
692 if (mode != HAL_COLOR_MODE_NATIVE)
693 return HWC2::Error::Unsupported;
694
695 color_mode_ = mode;
696 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500697}
698
699HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500700 int32_t hint) {
701 supported(__func__);
702 // TODO: Force client composition if we get this
Sean Pauled2ec4b2016-03-10 15:35:40 -0500703 return unsupported(__func__, matrix, hint);
704}
705
706HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500707 int32_t release_fence) {
708 supported(__func__);
709 // TODO: Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500710 return unsupported(__func__, buffer, release_fence);
711}
712
Sean Paulac874152016-03-10 16:00:26 -0500713HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
714 supported(__func__);
715 uint64_t dpms_value = 0;
716 auto mode = static_cast<HWC2::PowerMode>(mode_in);
717 switch (mode) {
718 case HWC2::PowerMode::Off:
719 dpms_value = DRM_MODE_DPMS_OFF;
720 break;
721 case HWC2::PowerMode::On:
722 dpms_value = DRM_MODE_DPMS_ON;
723 break;
724 default:
725 ALOGI("Power mode %d is unsupported\n", mode);
726 return HWC2::Error::Unsupported;
727 };
728
Sean Paulf72cccd2018-08-27 13:59:08 -0400729 std::unique_ptr<DrmDisplayComposition> composition = compositor_
730 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500731 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
732 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500733 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500734 if (ret) {
735 ALOGE("Failed to apply the dpms composition ret=%d", ret);
736 return HWC2::Error::BadParameter;
737 }
738 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500739}
740
741HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500742 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300743 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500744 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500745}
746
747HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500748 uint32_t *num_requests) {
749 supported(__func__);
750 *num_types = 0;
751 *num_requests = 0;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500752 size_t avail_planes = primary_planes_.size() + overlay_planes_.size();
John Stultz76ca20e2018-07-06 10:34:42 -0700753 bool comp_failed = false;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500754
755 HWC2::Error ret;
756
757 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
758 l.second.set_validated_type(HWC2::Composition::Invalid);
759
760 ret = CreateComposition(true);
761 if (ret != HWC2::Error::None)
John Stultz76ca20e2018-07-06 10:34:42 -0700762 comp_failed = true;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500763
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100764 std::map<uint32_t, DrmHwcTwo::HwcLayer *, std::greater<int>> z_map;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500765 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
766 if (l.second.sf_type() == HWC2::Composition::Device)
767 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
768 }
769
770 /*
771 * If more layers then planes, save one plane
772 * for client composited layers
773 */
774 if (avail_planes < layers_.size())
775 avail_planes--;
776
777 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
John Stultz76ca20e2018-07-06 10:34:42 -0700778 if (comp_failed || !avail_planes--)
Rob Herring4f6c62e2018-05-17 14:33:02 -0500779 break;
Alexey Firago18ec6882018-11-21 23:47:05 +0300780 if (importer_->CanImportBuffer(l.second->buffer()))
781 l.second->set_validated_type(HWC2::Composition::Device);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500782 }
783
Sean Paulac874152016-03-10 16:00:26 -0500784 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
785 DrmHwcTwo::HwcLayer &layer = l.second;
John Stultz734ee1c2019-01-09 14:54:52 -0800786 // We can only handle layers of Device type, send everything else to SF
787 if (layer.sf_type() != HWC2::Composition::Device ||
788 layer.validated_type() != HWC2::Composition::Device) {
789 layer.set_validated_type(HWC2::Composition::Client);
790 ++*num_types;
Sean Paulac874152016-03-10 16:00:26 -0500791 }
792 }
Rob Herringee8f45b2017-06-09 15:15:55 -0500793 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500794}
795
796HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -0500797 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800798 cursor_x_ = x;
799 cursor_y_ = y;
800 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500801}
802
803HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500804 supported(__func__);
805 blending_ = static_cast<HWC2::BlendMode>(mode);
806 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500807}
808
809HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500810 int32_t acquire_fence) {
811 supported(__func__);
812 UniqueFd uf(acquire_fence);
813
814 // The buffer and acquire_fence are handled elsewhere
815 if (sf_type_ == HWC2::Composition::Client ||
816 sf_type_ == HWC2::Composition::Sideband ||
817 sf_type_ == HWC2::Composition::SolidColor)
818 return HWC2::Error::None;
819
820 set_buffer(buffer);
821 set_acquire_fence(uf.get());
822 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500823}
824
825HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Sean Paulac874152016-03-10 16:00:26 -0500826 // TODO: Punt to client composition here?
Sean Pauled2ec4b2016-03-10 15:35:40 -0500827 return unsupported(__func__, color);
828}
829
830HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -0500831 sf_type_ = static_cast<HWC2::Composition>(type);
832 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500833}
834
835HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -0500836 supported(__func__);
837 dataspace_ = static_cast<android_dataspace_t>(dataspace);
838 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500839}
840
841HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -0500842 supported(__func__);
843 display_frame_ = frame;
844 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500845}
846
847HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -0500848 supported(__func__);
849 alpha_ = alpha;
850 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500851}
852
853HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
854 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -0500855 supported(__func__);
856 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -0500857 return unsupported(__func__, stream);
858}
859
860HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -0500861 supported(__func__);
862 source_crop_ = crop;
863 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500864}
865
866HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -0500867 supported(__func__);
868 // TODO: We don't use surface damage, marking as unsupported
869 unsupported(__func__, damage);
870 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500871}
872
873HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -0500874 supported(__func__);
875 transform_ = static_cast<HWC2::Transform>(transform);
876 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500877}
878
879HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -0500880 supported(__func__);
881 // TODO: We don't use this information, marking as unsupported
882 unsupported(__func__, visible);
883 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500884}
885
Sean Paulac874152016-03-10 16:00:26 -0500886HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
887 supported(__func__);
888 z_order_ = order;
889 return HWC2::Error::None;
890}
891
892void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
893 supported(__func__);
894 switch (blending_) {
895 case HWC2::BlendMode::None:
896 layer->blending = DrmHwcBlending::kNone;
897 break;
898 case HWC2::BlendMode::Premultiplied:
899 layer->blending = DrmHwcBlending::kPreMult;
900 break;
901 case HWC2::BlendMode::Coverage:
902 layer->blending = DrmHwcBlending::kCoverage;
903 break;
904 default:
905 ALOGE("Unknown blending mode b=%d", blending_);
906 layer->blending = DrmHwcBlending::kNone;
907 break;
908 }
909
910 OutputFd release_fence = release_fence_output();
911
912 layer->sf_handle = buffer_;
913 layer->acquire_fence = acquire_fence_.Release();
914 layer->release_fence = std::move(release_fence);
915 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +0200916 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -0500917 layer->SetSourceCrop(source_crop_);
918 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -0500919}
920
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300921void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
922 auto cb = callbacks_.find(HWC2::Callback::Hotplug);
923 if (cb == callbacks_.end())
924 return;
925
926 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(cb->second.func);
927 hotplug(cb->second.data, displayid,
928 (state == DRM_MODE_CONNECTED ? HWC2_CONNECTION_CONNECTED
929 : HWC2_CONNECTION_DISCONNECTED));
930}
931
932void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
933 for (auto &conn : drmDevice->connectors()) {
934 if (conn->state() != DRM_MODE_CONNECTED)
935 continue;
936 HandleDisplayHotplug(conn->display(), conn->state());
937 }
938}
939
940void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
941 for (auto &conn : drm_->connectors()) {
942 drmModeConnection old_state = conn->state();
943 drmModeConnection cur_state = conn->UpdateModes()
944 ? DRM_MODE_UNKNOWNCONNECTION
945 : conn->state();
946
947 if (cur_state == old_state)
948 continue;
949
950 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
951 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
952 conn->id(), conn->display());
953
954 int display_id = conn->display();
955 if (cur_state == DRM_MODE_CONNECTED) {
956 auto &display = hwc2_->displays_.at(display_id);
957 display.ChosePreferredConfig();
958 } else {
959 auto &display = hwc2_->displays_.at(display_id);
960 display.ClearDisplay();
961 }
962
963 hwc2_->HandleDisplayHotplug(display_id, cur_state);
964 }
965}
966
Sean Pauled2ec4b2016-03-10 15:35:40 -0500967// static
968int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
969 unsupported(__func__);
970 return 0;
971}
972
973// static
974void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -0500975 uint32_t *out_count,
976 int32_t * /*out_capabilities*/) {
977 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500978 *out_count = 0;
979}
980
981// static
Sean Paulac874152016-03-10 16:00:26 -0500982hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
983 struct hwc2_device * /*dev*/, int32_t descriptor) {
984 supported(__func__);
985 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500986 switch (func) {
987 // Device functions
988 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
989 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
990 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
991 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -0400992 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500993 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
994 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
995 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
996 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
997 case HWC2::FunctionDescriptor::Dump:
998 return ToHook<HWC2_PFN_DUMP>(
999 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1000 uint32_t *, char *>);
1001 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1002 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1003 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1004 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1005 case HWC2::FunctionDescriptor::RegisterCallback:
1006 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1007 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1008 &DrmHwcTwo::RegisterCallback, int32_t,
1009 hwc2_callback_data_t, hwc2_function_pointer_t>);
1010
1011 // Display functions
1012 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1013 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1014 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1015 &HwcDisplay::AcceptDisplayChanges>);
1016 case HWC2::FunctionDescriptor::CreateLayer:
1017 return ToHook<HWC2_PFN_CREATE_LAYER>(
1018 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1019 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1020 case HWC2::FunctionDescriptor::DestroyLayer:
1021 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1022 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1023 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1024 case HWC2::FunctionDescriptor::GetActiveConfig:
1025 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1026 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1027 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1028 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1029 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1030 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1031 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1032 hwc2_layer_t *, int32_t *>);
1033 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1034 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1035 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1036 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1037 int32_t, int32_t>);
1038 case HWC2::FunctionDescriptor::GetColorModes:
1039 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1040 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1041 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1042 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001043 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1044 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1045 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1046 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001047 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001048 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1049 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1050 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1051 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001052 case HWC2::FunctionDescriptor::GetDisplayName:
1053 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1054 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1055 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1056 case HWC2::FunctionDescriptor::GetDisplayRequests:
1057 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1058 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1059 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1060 hwc2_layer_t *, int32_t *>);
1061 case HWC2::FunctionDescriptor::GetDisplayType:
1062 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1063 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1064 &HwcDisplay::GetDisplayType, int32_t *>);
1065 case HWC2::FunctionDescriptor::GetDozeSupport:
1066 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1067 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1068 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001069 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1070 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1071 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1072 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1073 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001074 case HWC2::FunctionDescriptor::GetReleaseFences:
1075 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1076 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1077 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1078 int32_t *>);
1079 case HWC2::FunctionDescriptor::PresentDisplay:
1080 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1081 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1082 &HwcDisplay::PresentDisplay, int32_t *>);
1083 case HWC2::FunctionDescriptor::SetActiveConfig:
1084 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1085 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1086 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1087 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001088 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1089 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1090 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1091 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001092 case HWC2::FunctionDescriptor::SetColorMode:
1093 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1094 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1095 &HwcDisplay::SetColorMode, int32_t>);
1096 case HWC2::FunctionDescriptor::SetColorTransform:
1097 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1098 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1099 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1100 case HWC2::FunctionDescriptor::SetOutputBuffer:
1101 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1102 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1103 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1104 case HWC2::FunctionDescriptor::SetPowerMode:
1105 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1106 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1107 &HwcDisplay::SetPowerMode, int32_t>);
1108 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1109 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1110 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1111 &HwcDisplay::SetVsyncEnabled, int32_t>);
1112 case HWC2::FunctionDescriptor::ValidateDisplay:
1113 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1114 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1115 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
1116
1117 // Layer functions
1118 case HWC2::FunctionDescriptor::SetCursorPosition:
1119 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1120 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1121 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1122 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1123 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1124 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1125 &HwcLayer::SetLayerBlendMode, int32_t>);
1126 case HWC2::FunctionDescriptor::SetLayerBuffer:
1127 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1128 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1129 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1130 case HWC2::FunctionDescriptor::SetLayerColor:
1131 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1132 LayerHook<decltype(&HwcLayer::SetLayerColor),
1133 &HwcLayer::SetLayerColor, hwc_color_t>);
1134 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1135 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1136 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1137 &HwcLayer::SetLayerCompositionType, int32_t>);
1138 case HWC2::FunctionDescriptor::SetLayerDataspace:
1139 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1140 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1141 &HwcLayer::SetLayerDataspace, int32_t>);
1142 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1143 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1144 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1145 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1146 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1147 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1148 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1149 &HwcLayer::SetLayerPlaneAlpha, float>);
1150 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001151 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1152 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1153 &HwcLayer::SetLayerSidebandStream,
1154 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001155 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1156 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1157 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1158 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1159 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1160 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1161 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1162 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1163 case HWC2::FunctionDescriptor::SetLayerTransform:
1164 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1165 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1166 &HwcLayer::SetLayerTransform, int32_t>);
1167 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1168 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1169 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1170 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1171 case HWC2::FunctionDescriptor::SetLayerZOrder:
1172 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1173 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1174 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001175 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001176 default:
1177 return NULL;
1178 }
1179}
Sean Paulac874152016-03-10 16:00:26 -05001180
1181// static
1182int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1183 struct hw_device_t **dev) {
1184 supported(__func__);
1185 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1186 ALOGE("Invalid module name- %s", name);
1187 return -EINVAL;
1188 }
1189
1190 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1191 if (!ctx) {
1192 ALOGE("Failed to allocate DrmHwcTwo");
1193 return -ENOMEM;
1194 }
1195
1196 HWC2::Error err = ctx->Init();
1197 if (err != HWC2::Error::None) {
1198 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1199 return -EINVAL;
1200 }
1201
1202 ctx->common.module = const_cast<hw_module_t *>(module);
1203 *dev = &ctx->common;
1204 ctx.release();
1205 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001206}
Sean Paulf72cccd2018-08-27 13:59:08 -04001207} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001208
1209static struct hw_module_methods_t hwc2_module_methods = {
1210 .open = android::DrmHwcTwo::HookDevOpen,
1211};
1212
1213hw_module_t HAL_MODULE_INFO_SYM = {
1214 .tag = HARDWARE_MODULE_TAG,
1215 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1216 .id = HWC_HARDWARE_MODULE_ID,
1217 .name = "DrmHwcTwo module",
1218 .author = "The Android Open Source Project",
1219 .methods = &hwc2_module_methods,
1220 .dso = NULL,
1221 .reserved = {0},
1222};