blob: f506bfc6a276948cf442a186ef2aa766b97f2eff [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
248 // Grab the first mode, we'll choose this as the active mode
249 // TODO: Should choose the preferred mode here
250 hwc2_config_t default_config;
251 num_configs = 1;
252 err = GetDisplayConfigs(&num_configs, &default_config);
253 if (err != HWC2::Error::None)
254 return err;
Sean Paulac874152016-03-10 16:00:26 -0500255 return SetActiveConfig(default_config);
256}
257
258HWC2::Error DrmHwcTwo::HwcDisplay::RegisterVsyncCallback(
259 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
260 supported(__func__);
261 auto callback = std::make_shared<DrmVsyncCallback>(data, func);
Adrian Salidofa37f672017-02-16 10:29:46 -0800262 vsync_worker_.RegisterCallback(std::move(callback));
Sean Paulac874152016-03-10 16:00:26 -0500263 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500264}
265
266HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500267 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500268 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
269 l.second.accept_type_change();
270 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500271}
272
273HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500274 supported(__func__);
275 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
276 *layer = static_cast<hwc2_layer_t>(layer_idx_);
277 ++layer_idx_;
278 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500279}
280
281HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Sean Paulac874152016-03-10 16:00:26 -0500282 supported(__func__);
283 layers_.erase(layer);
284 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500285}
286
287HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
Sean Paulac874152016-03-10 16:00:26 -0500288 supported(__func__);
289 DrmMode const &mode = connector_->active_mode();
290 if (mode.id() == 0)
291 return HWC2::Error::BadConfig;
292
293 *config = mode.id();
294 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500295}
296
297HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
298 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500299 supported(__func__);
300 uint32_t num_changes = 0;
301 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
302 if (l.second.type_changed()) {
303 if (layers && num_changes < *num_elements)
304 layers[num_changes] = l.first;
305 if (types && num_changes < *num_elements)
306 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
307 ++num_changes;
308 }
309 }
310 if (!layers && !types)
311 *num_elements = num_changes;
312 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500313}
314
315HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500316 uint32_t height,
317 int32_t /*format*/,
318 int32_t dataspace) {
319 supported(__func__);
320 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
321 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
322
323 if (width < min.first || height < min.second)
324 return HWC2::Error::Unsupported;
325
326 if (width > max.first || height > max.second)
327 return HWC2::Error::Unsupported;
328
329 if (dataspace != HAL_DATASPACE_UNKNOWN &&
330 dataspace != HAL_DATASPACE_STANDARD_UNSPECIFIED)
331 return HWC2::Error::Unsupported;
332
333 // TODO: Validate format can be handled by either GL or planes
334 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500335}
336
337HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500338 int32_t *modes) {
339 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800340 if (!modes)
341 *num_modes = 1;
342
343 if (modes)
344 *modes = HAL_COLOR_MODE_NATIVE;
345
346 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500347}
348
349HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500350 int32_t attribute_in,
351 int32_t *value) {
352 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400353 auto mode = std::find_if(connector_->modes().begin(),
354 connector_->modes().end(),
355 [config](DrmMode const &m) {
356 return m.id() == config;
357 });
Sean Paulac874152016-03-10 16:00:26 -0500358 if (mode == connector_->modes().end()) {
359 ALOGE("Could not find active mode for %d", config);
360 return HWC2::Error::BadConfig;
361 }
362
363 static const int32_t kUmPerInch = 25400;
364 uint32_t mm_width = connector_->mm_width();
365 uint32_t mm_height = connector_->mm_height();
366 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
367 switch (attribute) {
368 case HWC2::Attribute::Width:
369 *value = mode->h_display();
370 break;
371 case HWC2::Attribute::Height:
372 *value = mode->v_display();
373 break;
374 case HWC2::Attribute::VsyncPeriod:
375 // in nanoseconds
376 *value = 1000 * 1000 * 1000 / mode->v_refresh();
377 break;
378 case HWC2::Attribute::DpiX:
379 // Dots per 1000 inches
380 *value = mm_width ? (mode->h_display() * kUmPerInch) / mm_width : -1;
381 break;
382 case HWC2::Attribute::DpiY:
383 // Dots per 1000 inches
384 *value = mm_height ? (mode->v_display() * kUmPerInch) / mm_height : -1;
385 break;
386 default:
387 *value = -1;
388 return HWC2::Error::BadConfig;
389 }
390 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500391}
392
393HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
394 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500395 supported(__func__);
396 // Since this callback is normally invoked twice (once to get the count, and
397 // once to populate configs), we don't really want to read the edid
398 // redundantly. Instead, only update the modes on the first invocation. While
399 // it's possible this will result in stale modes, it'll all come out in the
400 // wash when we try to set the active config later.
401 if (!configs) {
402 int ret = connector_->UpdateModes();
403 if (ret) {
404 ALOGE("Failed to update display modes %d", ret);
405 return HWC2::Error::BadDisplay;
406 }
407 }
408
409 auto num_modes = static_cast<uint32_t>(connector_->modes().size());
410 if (!configs) {
411 *num_configs = num_modes;
412 return HWC2::Error::None;
413 }
414
415 uint32_t idx = 0;
416 for (const DrmMode &mode : connector_->modes()) {
417 if (idx >= *num_configs)
418 break;
419 configs[idx++] = mode.id();
420 }
421 *num_configs = idx;
422 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500423}
424
425HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500426 supported(__func__);
427 std::ostringstream stream;
428 stream << "display-" << connector_->id();
429 std::string string = stream.str();
430 size_t length = string.length();
431 if (!name) {
432 *size = length;
433 return HWC2::Error::None;
434 }
435
436 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
437 strncpy(name, string.c_str(), *size);
438 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500439}
440
Sean Paulac874152016-03-10 16:00:26 -0500441HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
442 uint32_t *num_elements,
443 hwc2_layer_t *layers,
444 int32_t *layer_requests) {
445 supported(__func__);
446 // TODO: I think virtual display should request
447 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
448 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
449 *num_elements = 0;
450 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500451}
452
453HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500454 supported(__func__);
455 *type = static_cast<int32_t>(type_);
456 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500457}
458
459HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500460 supported(__func__);
461 *support = 0;
462 return HWC2::Error::None;
463}
464
465HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400466 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
467 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500468 supported(__func__);
469 *num_types = 0;
470 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500471}
472
473HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500474 hwc2_layer_t *layers,
475 int32_t *fences) {
476 supported(__func__);
477 uint32_t num_layers = 0;
478
479 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
480 ++num_layers;
481 if (layers == NULL || fences == NULL) {
482 continue;
483 } else if (num_layers > *num_elements) {
484 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
485 return HWC2::Error::None;
486 }
487
488 layers[num_layers - 1] = l.first;
489 fences[num_layers - 1] = l.second.take_release_fence();
490 }
491 *num_elements = num_layers;
492 return HWC2::Error::None;
493}
494
495void DrmHwcTwo::HwcDisplay::AddFenceToRetireFence(int fd) {
496 supported(__func__);
497 if (fd < 0)
498 return;
499
500 if (next_retire_fence_.get() >= 0) {
501 int old_fence = next_retire_fence_.get();
502 next_retire_fence_.Set(sync_merge("dc_retire", old_fence, fd));
503 } else {
504 next_retire_fence_.Set(dup(fd));
505 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500506}
507
Rob Herring4f6c62e2018-05-17 14:33:02 -0500508HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500509 std::vector<DrmCompositionDisplayLayersMap> layers_map;
510 layers_map.emplace_back();
511 DrmCompositionDisplayLayersMap &map = layers_map.back();
512
513 map.display = static_cast<int>(handle_);
514 map.geometry_changed = true; // TODO: Fix this
515
516 // order the layers by z-order
517 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100518 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500519 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
520 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500521 HWC2::Composition comp_type;
Alexey Firago18ec6882018-11-21 23:47:05 +0300522 if (test) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500523 comp_type = l.second.sf_type();
Alexey Firago18ec6882018-11-21 23:47:05 +0300524 if (comp_type == HWC2::Composition::Device) {
525 if (!importer_->CanImportBuffer(l.second.buffer()))
526 comp_type = HWC2::Composition::Client;
527 }
528 } else
Rob Herring4f6c62e2018-05-17 14:33:02 -0500529 comp_type = l.second.validated_type();
530
531 switch (comp_type) {
Sean Paulac874152016-03-10 16:00:26 -0500532 case HWC2::Composition::Device:
533 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
534 break;
535 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100536 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500537 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100538 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500539 break;
540 default:
541 continue;
542 }
543 }
544 if (use_client_layer)
545 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
546
Rob Herring4f6c62e2018-05-17 14:33:02 -0500547 if (z_map.empty())
548 return HWC2::Error::BadLayer;
549
Sean Paulac874152016-03-10 16:00:26 -0500550 // now that they're ordered by z, add them to the composition
551 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
552 DrmHwcLayer layer;
553 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200554 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500555 if (ret) {
556 ALOGE("Failed to import layer, ret=%d", ret);
557 return HWC2::Error::NoResources;
558 }
559 map.layers.emplace_back(std::move(layer));
560 }
Sean Paulac874152016-03-10 16:00:26 -0500561
Sean Paulf72cccd2018-08-27 13:59:08 -0400562 std::unique_ptr<DrmDisplayComposition> composition = compositor_
563 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500564 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
565
566 // TODO: Don't always assume geometry changed
567 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
568 if (ret) {
569 ALOGE("Failed to set layers in the composition ret=%d", ret);
570 return HWC2::Error::BadLayer;
571 }
572
573 std::vector<DrmPlane *> primary_planes(primary_planes_);
574 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500575 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500576 if (ret) {
577 ALOGE("Failed to plan the composition ret=%d", ret);
578 return HWC2::Error::BadConfig;
579 }
580
581 // Disable the planes we're not using
582 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
583 composition->AddPlaneDisable(*i);
584 i = primary_planes.erase(i);
585 }
586 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
587 composition->AddPlaneDisable(*i);
588 i = overlay_planes.erase(i);
589 }
590
Rob Herring4f6c62e2018-05-17 14:33:02 -0500591 if (test) {
592 ret = compositor_.TestComposition(composition.get());
593 } else {
594 AddFenceToRetireFence(composition->take_out_fence());
595 ret = compositor_.ApplyComposition(std::move(composition));
596 }
Sean Paulac874152016-03-10 16:00:26 -0500597 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700598 if (!test)
599 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500600 return HWC2::Error::BadParameter;
601 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500602 return HWC2::Error::None;
603}
604
605HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *retire_fence) {
606 supported(__func__);
607 HWC2::Error ret;
608
609 ret = CreateComposition(false);
610 if (ret == HWC2::Error::BadLayer) {
611 // Can we really have no client or device layers?
612 *retire_fence = -1;
613 return HWC2::Error::None;
614 }
615 if (ret != HWC2::Error::None)
616 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500617
Sean Paulac874152016-03-10 16:00:26 -0500618 // The retire fence returned here is for the last frame, so return it and
619 // promote the next retire fence
620 *retire_fence = retire_fence_.Release();
621 retire_fence_ = std::move(next_retire_fence_);
622
623 ++frame_no_;
624 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500625}
626
627HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500628 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400629 auto mode = std::find_if(connector_->modes().begin(),
630 connector_->modes().end(),
631 [config](DrmMode const &m) {
632 return m.id() == config;
633 });
Sean Paulac874152016-03-10 16:00:26 -0500634 if (mode == connector_->modes().end()) {
635 ALOGE("Could not find active mode for %d", config);
636 return HWC2::Error::BadConfig;
637 }
638
Sean Paulf72cccd2018-08-27 13:59:08 -0400639 std::unique_ptr<DrmDisplayComposition> composition = compositor_
640 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500641 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
642 int ret = composition->SetDisplayMode(*mode);
Sean Pauled45a8e2017-02-28 13:17:34 -0500643 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500644 if (ret) {
645 ALOGE("Failed to queue dpms composition on %d", ret);
646 return HWC2::Error::BadConfig;
647 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300648
649 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500650
651 // Setup the client layer's dimensions
652 hwc_rect_t display_frame = {.left = 0,
653 .top = 0,
654 .right = static_cast<int>(mode->h_display()),
655 .bottom = static_cast<int>(mode->v_display())};
656 client_layer_.SetLayerDisplayFrame(display_frame);
657 hwc_frect_t source_crop = {.left = 0.0f,
658 .top = 0.0f,
659 .right = mode->h_display() + 0.0f,
660 .bottom = mode->v_display() + 0.0f};
661 client_layer_.SetLayerSourceCrop(source_crop);
662
663 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500664}
665
666HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
667 int32_t acquire_fence,
668 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600669 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500670 supported(__func__);
671 UniqueFd uf(acquire_fence);
672
673 client_layer_.set_buffer(target);
674 client_layer_.set_acquire_fence(uf.get());
675 client_layer_.SetLayerDataspace(dataspace);
676 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500677}
678
679HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500680 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800681
682 if (mode != HAL_COLOR_MODE_NATIVE)
683 return HWC2::Error::Unsupported;
684
685 color_mode_ = mode;
686 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500687}
688
689HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500690 int32_t hint) {
691 supported(__func__);
692 // TODO: Force client composition if we get this
Sean Pauled2ec4b2016-03-10 15:35:40 -0500693 return unsupported(__func__, matrix, hint);
694}
695
696HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500697 int32_t release_fence) {
698 supported(__func__);
699 // TODO: Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500700 return unsupported(__func__, buffer, release_fence);
701}
702
Sean Paulac874152016-03-10 16:00:26 -0500703HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
704 supported(__func__);
705 uint64_t dpms_value = 0;
706 auto mode = static_cast<HWC2::PowerMode>(mode_in);
707 switch (mode) {
708 case HWC2::PowerMode::Off:
709 dpms_value = DRM_MODE_DPMS_OFF;
710 break;
711 case HWC2::PowerMode::On:
712 dpms_value = DRM_MODE_DPMS_ON;
713 break;
714 default:
715 ALOGI("Power mode %d is unsupported\n", mode);
716 return HWC2::Error::Unsupported;
717 };
718
Sean Paulf72cccd2018-08-27 13:59:08 -0400719 std::unique_ptr<DrmDisplayComposition> composition = compositor_
720 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500721 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
722 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500723 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500724 if (ret) {
725 ALOGE("Failed to apply the dpms composition ret=%d", ret);
726 return HWC2::Error::BadParameter;
727 }
728 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500729}
730
731HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500732 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300733 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500734 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500735}
736
737HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500738 uint32_t *num_requests) {
739 supported(__func__);
740 *num_types = 0;
741 *num_requests = 0;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500742 size_t avail_planes = primary_planes_.size() + overlay_planes_.size();
John Stultz76ca20e2018-07-06 10:34:42 -0700743 bool comp_failed = false;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500744
745 HWC2::Error ret;
746
747 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
748 l.second.set_validated_type(HWC2::Composition::Invalid);
749
750 ret = CreateComposition(true);
751 if (ret != HWC2::Error::None)
John Stultz76ca20e2018-07-06 10:34:42 -0700752 comp_failed = true;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500753
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100754 std::map<uint32_t, DrmHwcTwo::HwcLayer *, std::greater<int>> z_map;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500755 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
756 if (l.second.sf_type() == HWC2::Composition::Device)
757 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
758 }
759
760 /*
761 * If more layers then planes, save one plane
762 * for client composited layers
763 */
764 if (avail_planes < layers_.size())
765 avail_planes--;
766
767 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
John Stultz76ca20e2018-07-06 10:34:42 -0700768 if (comp_failed || !avail_planes--)
Rob Herring4f6c62e2018-05-17 14:33:02 -0500769 break;
Alexey Firago18ec6882018-11-21 23:47:05 +0300770 if (importer_->CanImportBuffer(l.second->buffer()))
771 l.second->set_validated_type(HWC2::Composition::Device);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500772 }
773
Sean Paulac874152016-03-10 16:00:26 -0500774 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
775 DrmHwcTwo::HwcLayer &layer = l.second;
John Stultz734ee1c2019-01-09 14:54:52 -0800776 // We can only handle layers of Device type, send everything else to SF
777 if (layer.sf_type() != HWC2::Composition::Device ||
778 layer.validated_type() != HWC2::Composition::Device) {
779 layer.set_validated_type(HWC2::Composition::Client);
780 ++*num_types;
Sean Paulac874152016-03-10 16:00:26 -0500781 }
782 }
Rob Herringee8f45b2017-06-09 15:15:55 -0500783 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500784}
785
786HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -0500787 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800788 cursor_x_ = x;
789 cursor_y_ = y;
790 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500791}
792
793HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500794 supported(__func__);
795 blending_ = static_cast<HWC2::BlendMode>(mode);
796 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500797}
798
799HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500800 int32_t acquire_fence) {
801 supported(__func__);
802 UniqueFd uf(acquire_fence);
803
804 // The buffer and acquire_fence are handled elsewhere
805 if (sf_type_ == HWC2::Composition::Client ||
806 sf_type_ == HWC2::Composition::Sideband ||
807 sf_type_ == HWC2::Composition::SolidColor)
808 return HWC2::Error::None;
809
810 set_buffer(buffer);
811 set_acquire_fence(uf.get());
812 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500813}
814
815HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Sean Paulac874152016-03-10 16:00:26 -0500816 // TODO: Punt to client composition here?
Sean Pauled2ec4b2016-03-10 15:35:40 -0500817 return unsupported(__func__, color);
818}
819
820HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -0500821 sf_type_ = static_cast<HWC2::Composition>(type);
822 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500823}
824
825HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -0500826 supported(__func__);
827 dataspace_ = static_cast<android_dataspace_t>(dataspace);
828 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500829}
830
831HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -0500832 supported(__func__);
833 display_frame_ = frame;
834 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500835}
836
837HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -0500838 supported(__func__);
839 alpha_ = alpha;
840 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500841}
842
843HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
844 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -0500845 supported(__func__);
846 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -0500847 return unsupported(__func__, stream);
848}
849
850HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -0500851 supported(__func__);
852 source_crop_ = crop;
853 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500854}
855
856HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -0500857 supported(__func__);
858 // TODO: We don't use surface damage, marking as unsupported
859 unsupported(__func__, damage);
860 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500861}
862
863HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -0500864 supported(__func__);
865 transform_ = static_cast<HWC2::Transform>(transform);
866 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500867}
868
869HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -0500870 supported(__func__);
871 // TODO: We don't use this information, marking as unsupported
872 unsupported(__func__, visible);
873 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500874}
875
Sean Paulac874152016-03-10 16:00:26 -0500876HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
877 supported(__func__);
878 z_order_ = order;
879 return HWC2::Error::None;
880}
881
882void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
883 supported(__func__);
884 switch (blending_) {
885 case HWC2::BlendMode::None:
886 layer->blending = DrmHwcBlending::kNone;
887 break;
888 case HWC2::BlendMode::Premultiplied:
889 layer->blending = DrmHwcBlending::kPreMult;
890 break;
891 case HWC2::BlendMode::Coverage:
892 layer->blending = DrmHwcBlending::kCoverage;
893 break;
894 default:
895 ALOGE("Unknown blending mode b=%d", blending_);
896 layer->blending = DrmHwcBlending::kNone;
897 break;
898 }
899
900 OutputFd release_fence = release_fence_output();
901
902 layer->sf_handle = buffer_;
903 layer->acquire_fence = acquire_fence_.Release();
904 layer->release_fence = std::move(release_fence);
905 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +0200906 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -0500907 layer->SetSourceCrop(source_crop_);
908 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -0500909}
910
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300911void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
912 auto cb = callbacks_.find(HWC2::Callback::Hotplug);
913 if (cb == callbacks_.end())
914 return;
915
916 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(cb->second.func);
917 hotplug(cb->second.data, displayid,
918 (state == DRM_MODE_CONNECTED ? HWC2_CONNECTION_CONNECTED
919 : HWC2_CONNECTION_DISCONNECTED));
920}
921
922void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
923 for (auto &conn : drmDevice->connectors()) {
924 if (conn->state() != DRM_MODE_CONNECTED)
925 continue;
926 HandleDisplayHotplug(conn->display(), conn->state());
927 }
928}
929
930void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
931 for (auto &conn : drm_->connectors()) {
932 drmModeConnection old_state = conn->state();
933 drmModeConnection cur_state = conn->UpdateModes()
934 ? DRM_MODE_UNKNOWNCONNECTION
935 : conn->state();
936
937 if (cur_state == old_state)
938 continue;
939
940 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
941 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
942 conn->id(), conn->display());
943
944 int display_id = conn->display();
945 if (cur_state == DRM_MODE_CONNECTED) {
946 auto &display = hwc2_->displays_.at(display_id);
947 display.ChosePreferredConfig();
948 } else {
949 auto &display = hwc2_->displays_.at(display_id);
950 display.ClearDisplay();
951 }
952
953 hwc2_->HandleDisplayHotplug(display_id, cur_state);
954 }
955}
956
Sean Pauled2ec4b2016-03-10 15:35:40 -0500957// static
958int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
959 unsupported(__func__);
960 return 0;
961}
962
963// static
964void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -0500965 uint32_t *out_count,
966 int32_t * /*out_capabilities*/) {
967 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500968 *out_count = 0;
969}
970
971// static
Sean Paulac874152016-03-10 16:00:26 -0500972hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
973 struct hwc2_device * /*dev*/, int32_t descriptor) {
974 supported(__func__);
975 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500976 switch (func) {
977 // Device functions
978 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
979 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
980 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
981 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -0400982 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500983 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
984 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
985 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
986 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
987 case HWC2::FunctionDescriptor::Dump:
988 return ToHook<HWC2_PFN_DUMP>(
989 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
990 uint32_t *, char *>);
991 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
992 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
993 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
994 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
995 case HWC2::FunctionDescriptor::RegisterCallback:
996 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
997 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
998 &DrmHwcTwo::RegisterCallback, int32_t,
999 hwc2_callback_data_t, hwc2_function_pointer_t>);
1000
1001 // Display functions
1002 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1003 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1004 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1005 &HwcDisplay::AcceptDisplayChanges>);
1006 case HWC2::FunctionDescriptor::CreateLayer:
1007 return ToHook<HWC2_PFN_CREATE_LAYER>(
1008 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1009 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1010 case HWC2::FunctionDescriptor::DestroyLayer:
1011 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1012 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1013 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1014 case HWC2::FunctionDescriptor::GetActiveConfig:
1015 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1016 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1017 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1018 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1019 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1020 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1021 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1022 hwc2_layer_t *, int32_t *>);
1023 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1024 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1025 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1026 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1027 int32_t, int32_t>);
1028 case HWC2::FunctionDescriptor::GetColorModes:
1029 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1030 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1031 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1032 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001033 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1034 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1035 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1036 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001037 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001038 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1039 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1040 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1041 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001042 case HWC2::FunctionDescriptor::GetDisplayName:
1043 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1044 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1045 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1046 case HWC2::FunctionDescriptor::GetDisplayRequests:
1047 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1048 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1049 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1050 hwc2_layer_t *, int32_t *>);
1051 case HWC2::FunctionDescriptor::GetDisplayType:
1052 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1053 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1054 &HwcDisplay::GetDisplayType, int32_t *>);
1055 case HWC2::FunctionDescriptor::GetDozeSupport:
1056 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1057 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1058 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001059 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1060 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1061 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1062 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1063 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001064 case HWC2::FunctionDescriptor::GetReleaseFences:
1065 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1066 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1067 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1068 int32_t *>);
1069 case HWC2::FunctionDescriptor::PresentDisplay:
1070 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1071 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1072 &HwcDisplay::PresentDisplay, int32_t *>);
1073 case HWC2::FunctionDescriptor::SetActiveConfig:
1074 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1075 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1076 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1077 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001078 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1079 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1080 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1081 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001082 case HWC2::FunctionDescriptor::SetColorMode:
1083 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1084 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1085 &HwcDisplay::SetColorMode, int32_t>);
1086 case HWC2::FunctionDescriptor::SetColorTransform:
1087 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1088 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1089 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1090 case HWC2::FunctionDescriptor::SetOutputBuffer:
1091 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1092 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1093 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1094 case HWC2::FunctionDescriptor::SetPowerMode:
1095 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1096 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1097 &HwcDisplay::SetPowerMode, int32_t>);
1098 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1099 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1100 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1101 &HwcDisplay::SetVsyncEnabled, int32_t>);
1102 case HWC2::FunctionDescriptor::ValidateDisplay:
1103 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1104 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1105 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
1106
1107 // Layer functions
1108 case HWC2::FunctionDescriptor::SetCursorPosition:
1109 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1110 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1111 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1112 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1113 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1114 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1115 &HwcLayer::SetLayerBlendMode, int32_t>);
1116 case HWC2::FunctionDescriptor::SetLayerBuffer:
1117 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1118 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1119 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1120 case HWC2::FunctionDescriptor::SetLayerColor:
1121 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1122 LayerHook<decltype(&HwcLayer::SetLayerColor),
1123 &HwcLayer::SetLayerColor, hwc_color_t>);
1124 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1125 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1126 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1127 &HwcLayer::SetLayerCompositionType, int32_t>);
1128 case HWC2::FunctionDescriptor::SetLayerDataspace:
1129 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1130 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1131 &HwcLayer::SetLayerDataspace, int32_t>);
1132 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1133 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1134 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1135 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1136 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1137 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1138 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1139 &HwcLayer::SetLayerPlaneAlpha, float>);
1140 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001141 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1142 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1143 &HwcLayer::SetLayerSidebandStream,
1144 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001145 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1146 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1147 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1148 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1149 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1150 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1151 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1152 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1153 case HWC2::FunctionDescriptor::SetLayerTransform:
1154 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1155 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1156 &HwcLayer::SetLayerTransform, int32_t>);
1157 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1158 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1159 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1160 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1161 case HWC2::FunctionDescriptor::SetLayerZOrder:
1162 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1163 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1164 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001165 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001166 default:
1167 return NULL;
1168 }
1169}
Sean Paulac874152016-03-10 16:00:26 -05001170
1171// static
1172int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1173 struct hw_device_t **dev) {
1174 supported(__func__);
1175 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1176 ALOGE("Invalid module name- %s", name);
1177 return -EINVAL;
1178 }
1179
1180 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1181 if (!ctx) {
1182 ALOGE("Failed to allocate DrmHwcTwo");
1183 return -ENOMEM;
1184 }
1185
1186 HWC2::Error err = ctx->Init();
1187 if (err != HWC2::Error::None) {
1188 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1189 return -EINVAL;
1190 }
1191
1192 ctx->common.module = const_cast<hw_module_t *>(module);
1193 *dev = &ctx->common;
1194 ctx.release();
1195 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001196}
Sean Paulf72cccd2018-08-27 13:59:08 -04001197} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001198
1199static struct hw_module_methods_t hwc2_module_methods = {
1200 .open = android::DrmHwcTwo::HookDevOpen,
1201};
1202
1203hw_module_t HAL_MODULE_INFO_SYM = {
1204 .tag = HARDWARE_MODULE_TAG,
1205 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1206 .id = HWC_HARDWARE_MODULE_ID,
1207 .name = "DrmHwcTwo module",
1208 .author = "The Android Open Source Project",
1209 .methods = &hwc2_module_methods,
1210 .dso = NULL,
1211 .reserved = {0},
1212};