blob: cd79e7b0b1e91b7f404ad431337e943c5dd6005b [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
Sean Paulac874152016-03-10 16:00:26 -050060HWC2::Error DrmHwcTwo::Init() {
Alexandru Gheorghec5463582018-03-27 15:52:02 +010061 int ret = resource_manager_.Init();
Sean Paulac874152016-03-10 16:00:26 -050062 if (ret) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +010063 ALOGE("Can't initialize the resource manager %d", ret);
Sean Paulac874152016-03-10 16:00:26 -050064 return HWC2::Error::NoResources;
65 }
66
Alexandru Gheorghec5463582018-03-27 15:52:02 +010067 DrmDevice *drm = resource_manager_.GetDrmDevice(HWC_DISPLAY_PRIMARY);
Sean Paulf72cccd2018-08-27 13:59:08 -040068 std::shared_ptr<Importer> importer = resource_manager_.GetImporter(
69 HWC_DISPLAY_PRIMARY);
Alexandru Gheorghec5463582018-03-27 15:52:02 +010070 if (!drm || !importer) {
71 ALOGE("Failed to get a valid drmresource and importer");
Sean Paulac874152016-03-10 16:00:26 -050072 return HWC2::Error::NoResources;
73 }
74
Sean Paulf72cccd2018-08-27 13:59:08 -040075 displays_.emplace(std::piecewise_construct,
76 std::forward_as_tuple(HWC_DISPLAY_PRIMARY),
77 std::forward_as_tuple(&resource_manager_, drm, importer,
78 HWC_DISPLAY_PRIMARY,
79 HWC2::DisplayType::Physical));
Sean Paulac874152016-03-10 16:00:26 -050080
Alexandru Gheorghec5463582018-03-27 15:52:02 +010081 DrmCrtc *crtc = drm->GetCrtcForDisplay(static_cast<int>(HWC_DISPLAY_PRIMARY));
Sean Paulac874152016-03-10 16:00:26 -050082 if (!crtc) {
83 ALOGE("Failed to get crtc for display %d",
84 static_cast<int>(HWC_DISPLAY_PRIMARY));
85 return HWC2::Error::BadDisplay;
86 }
87
88 std::vector<DrmPlane *> display_planes;
Alexandru Gheorghec5463582018-03-27 15:52:02 +010089 for (auto &plane : drm->planes()) {
Sean Paulac874152016-03-10 16:00:26 -050090 if (plane->GetCrtcSupported(*crtc))
91 display_planes.push_back(plane.get());
92 }
93 displays_.at(HWC_DISPLAY_PRIMARY).Init(&display_planes);
94 return HWC2::Error::None;
95}
96
Sean Pauled2ec4b2016-03-10 15:35:40 -050097template <typename... Args>
98static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
99 ALOGV("Unsupported function: %s", func);
100 return HWC2::Error::Unsupported;
101}
102
Sean Paulac874152016-03-10 16:00:26 -0500103static inline void supported(char const *func) {
104 ALOGV("Supported function: %s", func);
105}
106
Sean Pauled2ec4b2016-03-10 15:35:40 -0500107HWC2::Error DrmHwcTwo::CreateVirtualDisplay(uint32_t width, uint32_t height,
108 int32_t *format,
109 hwc2_display_t *display) {
110 // TODO: Implement virtual display
Sean Paulac874152016-03-10 16:00:26 -0500111 return unsupported(__func__, width, height, format, display);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500112}
113
114HWC2::Error DrmHwcTwo::DestroyVirtualDisplay(hwc2_display_t display) {
Sean Paulac874152016-03-10 16:00:26 -0500115 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500116 return unsupported(__func__, display);
117}
118
119void DrmHwcTwo::Dump(uint32_t *size, char *buffer) {
Sean Paulac874152016-03-10 16:00:26 -0500120 // TODO: Implement dump
Sean Pauled2ec4b2016-03-10 15:35:40 -0500121 unsupported(__func__, size, buffer);
122}
123
124uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() {
Sean Paulac874152016-03-10 16:00:26 -0500125 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500126 unsupported(__func__);
127 return 0;
128}
129
130HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor,
Sean Paulac874152016-03-10 16:00:26 -0500131 hwc2_callback_data_t data,
132 hwc2_function_pointer_t function) {
133 supported(__func__);
134 auto callback = static_cast<HWC2::Callback>(descriptor);
135 callbacks_.emplace(callback, HwcCallback(data, function));
136
137 switch (callback) {
138 case HWC2::Callback::Hotplug: {
139 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(function);
140 hotplug(data, HWC_DISPLAY_PRIMARY,
141 static_cast<int32_t>(HWC2::Connection::Connected));
142 break;
143 }
144 case HWC2::Callback::Vsync: {
145 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
146 displays_)
147 d.second.RegisterVsyncCallback(data, function);
148 break;
149 }
150 default:
151 break;
152 }
153 return HWC2::Error::None;
154}
155
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100156DrmHwcTwo::HwcDisplay::HwcDisplay(ResourceManager *resource_manager,
157 DrmDevice *drm,
Sean Paulac874152016-03-10 16:00:26 -0500158 std::shared_ptr<Importer> importer,
Sean Paulac874152016-03-10 16:00:26 -0500159 hwc2_display_t handle, HWC2::DisplayType type)
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100160 : resource_manager_(resource_manager),
161 drm_(drm),
162 importer_(importer),
163 handle_(handle),
164 type_(type) {
Sean Paulac874152016-03-10 16:00:26 -0500165 supported(__func__);
166}
167
168HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
169 supported(__func__);
170 planner_ = Planner::CreateInstance(drm_);
171 if (!planner_) {
172 ALOGE("Failed to create planner instance for composition");
173 return HWC2::Error::NoResources;
174 }
175
176 int display = static_cast<int>(handle_);
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100177 int ret = compositor_.Init(resource_manager_, display);
Sean Paulac874152016-03-10 16:00:26 -0500178 if (ret) {
179 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
180 return HWC2::Error::NoResources;
181 }
182
183 // Split up the given display planes into primary and overlay to properly
184 // interface with the composition
185 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
186 property_get("hwc.drm.use_overlay_planes", use_overlay_planes_prop, "1");
187 bool use_overlay_planes = atoi(use_overlay_planes_prop);
188 for (auto &plane : *planes) {
189 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
190 primary_planes_.push_back(plane);
191 else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY)
192 overlay_planes_.push_back(plane);
193 }
194
195 crtc_ = drm_->GetCrtcForDisplay(display);
196 if (!crtc_) {
197 ALOGE("Failed to get crtc for display %d", display);
198 return HWC2::Error::BadDisplay;
199 }
200
201 connector_ = drm_->GetConnectorForDisplay(display);
202 if (!connector_) {
203 ALOGE("Failed to get connector for display %d", display);
204 return HWC2::Error::BadDisplay;
205 }
206
207 // Fetch the number of modes from the display
208 uint32_t num_configs;
209 HWC2::Error err = GetDisplayConfigs(&num_configs, NULL);
210 if (err != HWC2::Error::None || !num_configs)
211 return err;
212
213 // Grab the first mode, we'll choose this as the active mode
214 // TODO: Should choose the preferred mode here
215 hwc2_config_t default_config;
216 num_configs = 1;
217 err = GetDisplayConfigs(&num_configs, &default_config);
218 if (err != HWC2::Error::None)
219 return err;
220
221 ret = vsync_worker_.Init(drm_, display);
222 if (ret) {
223 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
224 return HWC2::Error::BadDisplay;
225 }
226
227 return SetActiveConfig(default_config);
228}
229
230HWC2::Error DrmHwcTwo::HwcDisplay::RegisterVsyncCallback(
231 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
232 supported(__func__);
233 auto callback = std::make_shared<DrmVsyncCallback>(data, func);
Adrian Salidofa37f672017-02-16 10:29:46 -0800234 vsync_worker_.RegisterCallback(std::move(callback));
Sean Paulac874152016-03-10 16:00:26 -0500235 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500236}
237
238HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500239 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500240 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
241 l.second.accept_type_change();
242 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500243}
244
245HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500246 supported(__func__);
247 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
248 *layer = static_cast<hwc2_layer_t>(layer_idx_);
249 ++layer_idx_;
250 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500251}
252
253HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Sean Paulac874152016-03-10 16:00:26 -0500254 supported(__func__);
255 layers_.erase(layer);
256 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500257}
258
259HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
Sean Paulac874152016-03-10 16:00:26 -0500260 supported(__func__);
261 DrmMode const &mode = connector_->active_mode();
262 if (mode.id() == 0)
263 return HWC2::Error::BadConfig;
264
265 *config = mode.id();
266 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500267}
268
269HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
270 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500271 supported(__func__);
272 uint32_t num_changes = 0;
273 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
274 if (l.second.type_changed()) {
275 if (layers && num_changes < *num_elements)
276 layers[num_changes] = l.first;
277 if (types && num_changes < *num_elements)
278 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
279 ++num_changes;
280 }
281 }
282 if (!layers && !types)
283 *num_elements = num_changes;
284 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500285}
286
287HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500288 uint32_t height,
289 int32_t /*format*/,
290 int32_t dataspace) {
291 supported(__func__);
292 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
293 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
294
295 if (width < min.first || height < min.second)
296 return HWC2::Error::Unsupported;
297
298 if (width > max.first || height > max.second)
299 return HWC2::Error::Unsupported;
300
301 if (dataspace != HAL_DATASPACE_UNKNOWN &&
302 dataspace != HAL_DATASPACE_STANDARD_UNSPECIFIED)
303 return HWC2::Error::Unsupported;
304
305 // TODO: Validate format can be handled by either GL or planes
306 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500307}
308
309HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500310 int32_t *modes) {
311 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800312 if (!modes)
313 *num_modes = 1;
314
315 if (modes)
316 *modes = HAL_COLOR_MODE_NATIVE;
317
318 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500319}
320
321HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500322 int32_t attribute_in,
323 int32_t *value) {
324 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400325 auto mode = std::find_if(connector_->modes().begin(),
326 connector_->modes().end(),
327 [config](DrmMode const &m) {
328 return m.id() == config;
329 });
Sean Paulac874152016-03-10 16:00:26 -0500330 if (mode == connector_->modes().end()) {
331 ALOGE("Could not find active mode for %d", config);
332 return HWC2::Error::BadConfig;
333 }
334
335 static const int32_t kUmPerInch = 25400;
336 uint32_t mm_width = connector_->mm_width();
337 uint32_t mm_height = connector_->mm_height();
338 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
339 switch (attribute) {
340 case HWC2::Attribute::Width:
341 *value = mode->h_display();
342 break;
343 case HWC2::Attribute::Height:
344 *value = mode->v_display();
345 break;
346 case HWC2::Attribute::VsyncPeriod:
347 // in nanoseconds
348 *value = 1000 * 1000 * 1000 / mode->v_refresh();
349 break;
350 case HWC2::Attribute::DpiX:
351 // Dots per 1000 inches
352 *value = mm_width ? (mode->h_display() * kUmPerInch) / mm_width : -1;
353 break;
354 case HWC2::Attribute::DpiY:
355 // Dots per 1000 inches
356 *value = mm_height ? (mode->v_display() * kUmPerInch) / mm_height : -1;
357 break;
358 default:
359 *value = -1;
360 return HWC2::Error::BadConfig;
361 }
362 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500363}
364
365HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
366 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500367 supported(__func__);
368 // Since this callback is normally invoked twice (once to get the count, and
369 // once to populate configs), we don't really want to read the edid
370 // redundantly. Instead, only update the modes on the first invocation. While
371 // it's possible this will result in stale modes, it'll all come out in the
372 // wash when we try to set the active config later.
373 if (!configs) {
374 int ret = connector_->UpdateModes();
375 if (ret) {
376 ALOGE("Failed to update display modes %d", ret);
377 return HWC2::Error::BadDisplay;
378 }
379 }
380
381 auto num_modes = static_cast<uint32_t>(connector_->modes().size());
382 if (!configs) {
383 *num_configs = num_modes;
384 return HWC2::Error::None;
385 }
386
387 uint32_t idx = 0;
388 for (const DrmMode &mode : connector_->modes()) {
389 if (idx >= *num_configs)
390 break;
391 configs[idx++] = mode.id();
392 }
393 *num_configs = idx;
394 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500395}
396
397HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500398 supported(__func__);
399 std::ostringstream stream;
400 stream << "display-" << connector_->id();
401 std::string string = stream.str();
402 size_t length = string.length();
403 if (!name) {
404 *size = length;
405 return HWC2::Error::None;
406 }
407
408 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
409 strncpy(name, string.c_str(), *size);
410 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500411}
412
Sean Paulac874152016-03-10 16:00:26 -0500413HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
414 uint32_t *num_elements,
415 hwc2_layer_t *layers,
416 int32_t *layer_requests) {
417 supported(__func__);
418 // TODO: I think virtual display should request
419 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
420 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
421 *num_elements = 0;
422 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500423}
424
425HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500426 supported(__func__);
427 *type = static_cast<int32_t>(type_);
428 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500429}
430
431HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500432 supported(__func__);
433 *support = 0;
434 return HWC2::Error::None;
435}
436
437HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400438 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
439 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500440 supported(__func__);
441 *num_types = 0;
442 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500443}
444
445HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500446 hwc2_layer_t *layers,
447 int32_t *fences) {
448 supported(__func__);
449 uint32_t num_layers = 0;
450
451 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
452 ++num_layers;
453 if (layers == NULL || fences == NULL) {
454 continue;
455 } else if (num_layers > *num_elements) {
456 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
457 return HWC2::Error::None;
458 }
459
460 layers[num_layers - 1] = l.first;
461 fences[num_layers - 1] = l.second.take_release_fence();
462 }
463 *num_elements = num_layers;
464 return HWC2::Error::None;
465}
466
467void DrmHwcTwo::HwcDisplay::AddFenceToRetireFence(int fd) {
468 supported(__func__);
469 if (fd < 0)
470 return;
471
472 if (next_retire_fence_.get() >= 0) {
473 int old_fence = next_retire_fence_.get();
474 next_retire_fence_.Set(sync_merge("dc_retire", old_fence, fd));
475 } else {
476 next_retire_fence_.Set(dup(fd));
477 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500478}
479
Rob Herring4f6c62e2018-05-17 14:33:02 -0500480HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500481 std::vector<DrmCompositionDisplayLayersMap> layers_map;
482 layers_map.emplace_back();
483 DrmCompositionDisplayLayersMap &map = layers_map.back();
484
485 map.display = static_cast<int>(handle_);
486 map.geometry_changed = true; // TODO: Fix this
487
488 // order the layers by z-order
489 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100490 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500491 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
492 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500493 HWC2::Composition comp_type;
Alexey Firago18ec6882018-11-21 23:47:05 +0300494 if (test) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500495 comp_type = l.second.sf_type();
Alexey Firago18ec6882018-11-21 23:47:05 +0300496 if (comp_type == HWC2::Composition::Device) {
497 if (!importer_->CanImportBuffer(l.second.buffer()))
498 comp_type = HWC2::Composition::Client;
499 }
500 } else
Rob Herring4f6c62e2018-05-17 14:33:02 -0500501 comp_type = l.second.validated_type();
502
503 switch (comp_type) {
Sean Paulac874152016-03-10 16:00:26 -0500504 case HWC2::Composition::Device:
505 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
506 break;
507 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100508 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500509 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100510 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500511 break;
512 default:
513 continue;
514 }
515 }
516 if (use_client_layer)
517 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
518
Rob Herring4f6c62e2018-05-17 14:33:02 -0500519 if (z_map.empty())
520 return HWC2::Error::BadLayer;
521
Sean Paulac874152016-03-10 16:00:26 -0500522 // now that they're ordered by z, add them to the composition
523 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
524 DrmHwcLayer layer;
525 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200526 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500527 if (ret) {
528 ALOGE("Failed to import layer, ret=%d", ret);
529 return HWC2::Error::NoResources;
530 }
531 map.layers.emplace_back(std::move(layer));
532 }
Sean Paulac874152016-03-10 16:00:26 -0500533
Sean Paulf72cccd2018-08-27 13:59:08 -0400534 std::unique_ptr<DrmDisplayComposition> composition = compositor_
535 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500536 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
537
538 // TODO: Don't always assume geometry changed
539 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
540 if (ret) {
541 ALOGE("Failed to set layers in the composition ret=%d", ret);
542 return HWC2::Error::BadLayer;
543 }
544
545 std::vector<DrmPlane *> primary_planes(primary_planes_);
546 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500547 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500548 if (ret) {
549 ALOGE("Failed to plan the composition ret=%d", ret);
550 return HWC2::Error::BadConfig;
551 }
552
553 // Disable the planes we're not using
554 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
555 composition->AddPlaneDisable(*i);
556 i = primary_planes.erase(i);
557 }
558 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
559 composition->AddPlaneDisable(*i);
560 i = overlay_planes.erase(i);
561 }
562
Rob Herring4f6c62e2018-05-17 14:33:02 -0500563 if (test) {
564 ret = compositor_.TestComposition(composition.get());
565 } else {
566 AddFenceToRetireFence(composition->take_out_fence());
567 ret = compositor_.ApplyComposition(std::move(composition));
568 }
Sean Paulac874152016-03-10 16:00:26 -0500569 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700570 if (!test)
571 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500572 return HWC2::Error::BadParameter;
573 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500574 return HWC2::Error::None;
575}
576
577HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *retire_fence) {
578 supported(__func__);
579 HWC2::Error ret;
580
581 ret = CreateComposition(false);
582 if (ret == HWC2::Error::BadLayer) {
583 // Can we really have no client or device layers?
584 *retire_fence = -1;
585 return HWC2::Error::None;
586 }
587 if (ret != HWC2::Error::None)
588 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500589
Sean Paulac874152016-03-10 16:00:26 -0500590 // The retire fence returned here is for the last frame, so return it and
591 // promote the next retire fence
592 *retire_fence = retire_fence_.Release();
593 retire_fence_ = std::move(next_retire_fence_);
594
595 ++frame_no_;
596 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500597}
598
599HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500600 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400601 auto mode = std::find_if(connector_->modes().begin(),
602 connector_->modes().end(),
603 [config](DrmMode const &m) {
604 return m.id() == config;
605 });
Sean Paulac874152016-03-10 16:00:26 -0500606 if (mode == connector_->modes().end()) {
607 ALOGE("Could not find active mode for %d", config);
608 return HWC2::Error::BadConfig;
609 }
610
Sean Paulf72cccd2018-08-27 13:59:08 -0400611 std::unique_ptr<DrmDisplayComposition> composition = compositor_
612 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500613 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
614 int ret = composition->SetDisplayMode(*mode);
Sean Pauled45a8e2017-02-28 13:17:34 -0500615 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500616 if (ret) {
617 ALOGE("Failed to queue dpms composition on %d", ret);
618 return HWC2::Error::BadConfig;
619 }
620 if (connector_->active_mode().id() == 0)
621 connector_->set_active_mode(*mode);
622
623 // Setup the client layer's dimensions
624 hwc_rect_t display_frame = {.left = 0,
625 .top = 0,
626 .right = static_cast<int>(mode->h_display()),
627 .bottom = static_cast<int>(mode->v_display())};
628 client_layer_.SetLayerDisplayFrame(display_frame);
629 hwc_frect_t source_crop = {.left = 0.0f,
630 .top = 0.0f,
631 .right = mode->h_display() + 0.0f,
632 .bottom = mode->v_display() + 0.0f};
633 client_layer_.SetLayerSourceCrop(source_crop);
634
635 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500636}
637
638HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
639 int32_t acquire_fence,
640 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600641 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500642 supported(__func__);
643 UniqueFd uf(acquire_fence);
644
645 client_layer_.set_buffer(target);
646 client_layer_.set_acquire_fence(uf.get());
647 client_layer_.SetLayerDataspace(dataspace);
648 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500649}
650
651HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500652 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800653
654 if (mode != HAL_COLOR_MODE_NATIVE)
655 return HWC2::Error::Unsupported;
656
657 color_mode_ = mode;
658 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500659}
660
661HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500662 int32_t hint) {
663 supported(__func__);
664 // TODO: Force client composition if we get this
Sean Pauled2ec4b2016-03-10 15:35:40 -0500665 return unsupported(__func__, matrix, hint);
666}
667
668HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500669 int32_t release_fence) {
670 supported(__func__);
671 // TODO: Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500672 return unsupported(__func__, buffer, release_fence);
673}
674
Sean Paulac874152016-03-10 16:00:26 -0500675HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
676 supported(__func__);
677 uint64_t dpms_value = 0;
678 auto mode = static_cast<HWC2::PowerMode>(mode_in);
679 switch (mode) {
680 case HWC2::PowerMode::Off:
681 dpms_value = DRM_MODE_DPMS_OFF;
682 break;
683 case HWC2::PowerMode::On:
684 dpms_value = DRM_MODE_DPMS_ON;
685 break;
686 default:
687 ALOGI("Power mode %d is unsupported\n", mode);
688 return HWC2::Error::Unsupported;
689 };
690
Sean Paulf72cccd2018-08-27 13:59:08 -0400691 std::unique_ptr<DrmDisplayComposition> composition = compositor_
692 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500693 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
694 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500695 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500696 if (ret) {
697 ALOGE("Failed to apply the dpms composition ret=%d", ret);
698 return HWC2::Error::BadParameter;
699 }
700 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500701}
702
703HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500704 supported(__func__);
705 vsync_worker_.VSyncControl(enabled);
706 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500707}
708
709HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500710 uint32_t *num_requests) {
711 supported(__func__);
712 *num_types = 0;
713 *num_requests = 0;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500714 size_t avail_planes = primary_planes_.size() + overlay_planes_.size();
John Stultz76ca20e2018-07-06 10:34:42 -0700715 bool comp_failed = false;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500716
717 HWC2::Error ret;
718
719 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
720 l.second.set_validated_type(HWC2::Composition::Invalid);
721
722 ret = CreateComposition(true);
723 if (ret != HWC2::Error::None)
John Stultz76ca20e2018-07-06 10:34:42 -0700724 comp_failed = true;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500725
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100726 std::map<uint32_t, DrmHwcTwo::HwcLayer *, std::greater<int>> z_map;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500727 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
728 if (l.second.sf_type() == HWC2::Composition::Device)
729 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
730 }
731
732 /*
733 * If more layers then planes, save one plane
734 * for client composited layers
735 */
736 if (avail_planes < layers_.size())
737 avail_planes--;
738
739 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
John Stultz76ca20e2018-07-06 10:34:42 -0700740 if (comp_failed || !avail_planes--)
Rob Herring4f6c62e2018-05-17 14:33:02 -0500741 break;
Alexey Firago18ec6882018-11-21 23:47:05 +0300742 if (importer_->CanImportBuffer(l.second->buffer()))
743 l.second->set_validated_type(HWC2::Composition::Device);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500744 }
745
Sean Paulac874152016-03-10 16:00:26 -0500746 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
747 DrmHwcTwo::HwcLayer &layer = l.second;
748 switch (layer.sf_type()) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500749 case HWC2::Composition::Device:
750 if (layer.validated_type() == HWC2::Composition::Device)
751 break;
752 // fall thru
Sean Paulac874152016-03-10 16:00:26 -0500753 case HWC2::Composition::SolidColor:
754 case HWC2::Composition::Cursor:
755 case HWC2::Composition::Sideband:
Rob Herringaf0d9752018-05-04 16:34:19 -0500756 default:
Sean Paulac874152016-03-10 16:00:26 -0500757 layer.set_validated_type(HWC2::Composition::Client);
758 ++*num_types;
759 break;
Sean Paulac874152016-03-10 16:00:26 -0500760 }
761 }
Rob Herringee8f45b2017-06-09 15:15:55 -0500762 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500763}
764
765HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -0500766 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800767 cursor_x_ = x;
768 cursor_y_ = y;
769 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500770}
771
772HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500773 supported(__func__);
774 blending_ = static_cast<HWC2::BlendMode>(mode);
775 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500776}
777
778HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500779 int32_t acquire_fence) {
780 supported(__func__);
781 UniqueFd uf(acquire_fence);
782
783 // The buffer and acquire_fence are handled elsewhere
784 if (sf_type_ == HWC2::Composition::Client ||
785 sf_type_ == HWC2::Composition::Sideband ||
786 sf_type_ == HWC2::Composition::SolidColor)
787 return HWC2::Error::None;
788
789 set_buffer(buffer);
790 set_acquire_fence(uf.get());
791 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500792}
793
794HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Sean Paulac874152016-03-10 16:00:26 -0500795 // TODO: Punt to client composition here?
Sean Pauled2ec4b2016-03-10 15:35:40 -0500796 return unsupported(__func__, color);
797}
798
799HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -0500800 sf_type_ = static_cast<HWC2::Composition>(type);
801 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500802}
803
804HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -0500805 supported(__func__);
806 dataspace_ = static_cast<android_dataspace_t>(dataspace);
807 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500808}
809
810HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -0500811 supported(__func__);
812 display_frame_ = frame;
813 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500814}
815
816HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -0500817 supported(__func__);
818 alpha_ = alpha;
819 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500820}
821
822HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
823 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -0500824 supported(__func__);
825 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -0500826 return unsupported(__func__, stream);
827}
828
829HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -0500830 supported(__func__);
831 source_crop_ = crop;
832 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500833}
834
835HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -0500836 supported(__func__);
837 // TODO: We don't use surface damage, marking as unsupported
838 unsupported(__func__, damage);
839 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500840}
841
842HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -0500843 supported(__func__);
844 transform_ = static_cast<HWC2::Transform>(transform);
845 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500846}
847
848HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -0500849 supported(__func__);
850 // TODO: We don't use this information, marking as unsupported
851 unsupported(__func__, visible);
852 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500853}
854
Sean Paulac874152016-03-10 16:00:26 -0500855HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
856 supported(__func__);
857 z_order_ = order;
858 return HWC2::Error::None;
859}
860
861void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
862 supported(__func__);
863 switch (blending_) {
864 case HWC2::BlendMode::None:
865 layer->blending = DrmHwcBlending::kNone;
866 break;
867 case HWC2::BlendMode::Premultiplied:
868 layer->blending = DrmHwcBlending::kPreMult;
869 break;
870 case HWC2::BlendMode::Coverage:
871 layer->blending = DrmHwcBlending::kCoverage;
872 break;
873 default:
874 ALOGE("Unknown blending mode b=%d", blending_);
875 layer->blending = DrmHwcBlending::kNone;
876 break;
877 }
878
879 OutputFd release_fence = release_fence_output();
880
881 layer->sf_handle = buffer_;
882 layer->acquire_fence = acquire_fence_.Release();
883 layer->release_fence = std::move(release_fence);
884 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +0200885 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -0500886 layer->SetSourceCrop(source_crop_);
887 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -0500888}
889
890// static
891int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
892 unsupported(__func__);
893 return 0;
894}
895
896// static
897void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -0500898 uint32_t *out_count,
899 int32_t * /*out_capabilities*/) {
900 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500901 *out_count = 0;
902}
903
904// static
Sean Paulac874152016-03-10 16:00:26 -0500905hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
906 struct hwc2_device * /*dev*/, int32_t descriptor) {
907 supported(__func__);
908 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500909 switch (func) {
910 // Device functions
911 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
912 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
913 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
914 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -0400915 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500916 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
917 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
918 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
919 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
920 case HWC2::FunctionDescriptor::Dump:
921 return ToHook<HWC2_PFN_DUMP>(
922 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
923 uint32_t *, char *>);
924 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
925 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
926 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
927 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
928 case HWC2::FunctionDescriptor::RegisterCallback:
929 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
930 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
931 &DrmHwcTwo::RegisterCallback, int32_t,
932 hwc2_callback_data_t, hwc2_function_pointer_t>);
933
934 // Display functions
935 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
936 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
937 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
938 &HwcDisplay::AcceptDisplayChanges>);
939 case HWC2::FunctionDescriptor::CreateLayer:
940 return ToHook<HWC2_PFN_CREATE_LAYER>(
941 DisplayHook<decltype(&HwcDisplay::CreateLayer),
942 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
943 case HWC2::FunctionDescriptor::DestroyLayer:
944 return ToHook<HWC2_PFN_DESTROY_LAYER>(
945 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
946 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
947 case HWC2::FunctionDescriptor::GetActiveConfig:
948 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
949 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
950 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
951 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
952 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
953 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
954 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
955 hwc2_layer_t *, int32_t *>);
956 case HWC2::FunctionDescriptor::GetClientTargetSupport:
957 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
958 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
959 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
960 int32_t, int32_t>);
961 case HWC2::FunctionDescriptor::GetColorModes:
962 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
963 DisplayHook<decltype(&HwcDisplay::GetColorModes),
964 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
965 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -0400966 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
967 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
968 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
969 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500970 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -0400971 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
972 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
973 &HwcDisplay::GetDisplayConfigs, uint32_t *,
974 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500975 case HWC2::FunctionDescriptor::GetDisplayName:
976 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
977 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
978 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
979 case HWC2::FunctionDescriptor::GetDisplayRequests:
980 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
981 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
982 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
983 hwc2_layer_t *, int32_t *>);
984 case HWC2::FunctionDescriptor::GetDisplayType:
985 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
986 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
987 &HwcDisplay::GetDisplayType, int32_t *>);
988 case HWC2::FunctionDescriptor::GetDozeSupport:
989 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
990 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
991 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -0500992 case HWC2::FunctionDescriptor::GetHdrCapabilities:
993 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
994 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
995 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
996 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500997 case HWC2::FunctionDescriptor::GetReleaseFences:
998 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
999 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1000 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1001 int32_t *>);
1002 case HWC2::FunctionDescriptor::PresentDisplay:
1003 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1004 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1005 &HwcDisplay::PresentDisplay, int32_t *>);
1006 case HWC2::FunctionDescriptor::SetActiveConfig:
1007 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1008 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1009 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1010 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001011 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1012 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1013 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1014 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001015 case HWC2::FunctionDescriptor::SetColorMode:
1016 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1017 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1018 &HwcDisplay::SetColorMode, int32_t>);
1019 case HWC2::FunctionDescriptor::SetColorTransform:
1020 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1021 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1022 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1023 case HWC2::FunctionDescriptor::SetOutputBuffer:
1024 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1025 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1026 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1027 case HWC2::FunctionDescriptor::SetPowerMode:
1028 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1029 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1030 &HwcDisplay::SetPowerMode, int32_t>);
1031 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1032 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1033 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1034 &HwcDisplay::SetVsyncEnabled, int32_t>);
1035 case HWC2::FunctionDescriptor::ValidateDisplay:
1036 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1037 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1038 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
1039
1040 // Layer functions
1041 case HWC2::FunctionDescriptor::SetCursorPosition:
1042 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1043 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1044 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1045 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1046 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1047 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1048 &HwcLayer::SetLayerBlendMode, int32_t>);
1049 case HWC2::FunctionDescriptor::SetLayerBuffer:
1050 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1051 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1052 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1053 case HWC2::FunctionDescriptor::SetLayerColor:
1054 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1055 LayerHook<decltype(&HwcLayer::SetLayerColor),
1056 &HwcLayer::SetLayerColor, hwc_color_t>);
1057 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1058 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1059 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1060 &HwcLayer::SetLayerCompositionType, int32_t>);
1061 case HWC2::FunctionDescriptor::SetLayerDataspace:
1062 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1063 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1064 &HwcLayer::SetLayerDataspace, int32_t>);
1065 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1066 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1067 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1068 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1069 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1070 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1071 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1072 &HwcLayer::SetLayerPlaneAlpha, float>);
1073 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001074 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1075 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1076 &HwcLayer::SetLayerSidebandStream,
1077 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001078 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1079 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1080 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1081 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1082 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1083 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1084 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1085 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1086 case HWC2::FunctionDescriptor::SetLayerTransform:
1087 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1088 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1089 &HwcLayer::SetLayerTransform, int32_t>);
1090 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1091 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1092 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1093 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1094 case HWC2::FunctionDescriptor::SetLayerZOrder:
1095 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1096 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1097 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001098 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001099 default:
1100 return NULL;
1101 }
1102}
Sean Paulac874152016-03-10 16:00:26 -05001103
1104// static
1105int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1106 struct hw_device_t **dev) {
1107 supported(__func__);
1108 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1109 ALOGE("Invalid module name- %s", name);
1110 return -EINVAL;
1111 }
1112
1113 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1114 if (!ctx) {
1115 ALOGE("Failed to allocate DrmHwcTwo");
1116 return -ENOMEM;
1117 }
1118
1119 HWC2::Error err = ctx->Init();
1120 if (err != HWC2::Error::None) {
1121 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1122 return -EINVAL;
1123 }
1124
1125 ctx->common.module = const_cast<hw_module_t *>(module);
1126 *dev = &ctx->common;
1127 ctx.release();
1128 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001129}
Sean Paulf72cccd2018-08-27 13:59:08 -04001130} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001131
1132static struct hw_module_methods_t hwc2_module_methods = {
1133 .open = android::DrmHwcTwo::HookDevOpen,
1134};
1135
1136hw_module_t HAL_MODULE_INFO_SYM = {
1137 .tag = HARDWARE_MODULE_TAG,
1138 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1139 .id = HWC_HARDWARE_MODULE_ID,
1140 .name = "DrmHwcTwo module",
1141 .author = "The Android Open Source Project",
1142 .methods = &hwc2_module_methods,
1143 .dso = NULL,
1144 .reserved = {0},
1145};