blob: cf4ec1141358aa5301f50672229075cf6224696b [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;
John Stultz734ee1c2019-01-09 14:54:52 -0800748 // We can only handle layers of Device type, send everything else to SF
749 if (layer.sf_type() != HWC2::Composition::Device ||
750 layer.validated_type() != HWC2::Composition::Device) {
751 layer.set_validated_type(HWC2::Composition::Client);
752 ++*num_types;
Sean Paulac874152016-03-10 16:00:26 -0500753 }
754 }
Rob Herringee8f45b2017-06-09 15:15:55 -0500755 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500756}
757
758HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -0500759 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800760 cursor_x_ = x;
761 cursor_y_ = y;
762 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500763}
764
765HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500766 supported(__func__);
767 blending_ = static_cast<HWC2::BlendMode>(mode);
768 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500769}
770
771HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500772 int32_t acquire_fence) {
773 supported(__func__);
774 UniqueFd uf(acquire_fence);
775
776 // The buffer and acquire_fence are handled elsewhere
777 if (sf_type_ == HWC2::Composition::Client ||
778 sf_type_ == HWC2::Composition::Sideband ||
779 sf_type_ == HWC2::Composition::SolidColor)
780 return HWC2::Error::None;
781
782 set_buffer(buffer);
783 set_acquire_fence(uf.get());
784 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500785}
786
787HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Sean Paulac874152016-03-10 16:00:26 -0500788 // TODO: Punt to client composition here?
Sean Pauled2ec4b2016-03-10 15:35:40 -0500789 return unsupported(__func__, color);
790}
791
792HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -0500793 sf_type_ = static_cast<HWC2::Composition>(type);
794 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500795}
796
797HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -0500798 supported(__func__);
799 dataspace_ = static_cast<android_dataspace_t>(dataspace);
800 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500801}
802
803HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -0500804 supported(__func__);
805 display_frame_ = frame;
806 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500807}
808
809HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -0500810 supported(__func__);
811 alpha_ = alpha;
812 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500813}
814
815HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
816 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -0500817 supported(__func__);
818 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -0500819 return unsupported(__func__, stream);
820}
821
822HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -0500823 supported(__func__);
824 source_crop_ = crop;
825 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500826}
827
828HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -0500829 supported(__func__);
830 // TODO: We don't use surface damage, marking as unsupported
831 unsupported(__func__, damage);
832 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500833}
834
835HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -0500836 supported(__func__);
837 transform_ = static_cast<HWC2::Transform>(transform);
838 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500839}
840
841HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -0500842 supported(__func__);
843 // TODO: We don't use this information, marking as unsupported
844 unsupported(__func__, visible);
845 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500846}
847
Sean Paulac874152016-03-10 16:00:26 -0500848HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
849 supported(__func__);
850 z_order_ = order;
851 return HWC2::Error::None;
852}
853
854void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
855 supported(__func__);
856 switch (blending_) {
857 case HWC2::BlendMode::None:
858 layer->blending = DrmHwcBlending::kNone;
859 break;
860 case HWC2::BlendMode::Premultiplied:
861 layer->blending = DrmHwcBlending::kPreMult;
862 break;
863 case HWC2::BlendMode::Coverage:
864 layer->blending = DrmHwcBlending::kCoverage;
865 break;
866 default:
867 ALOGE("Unknown blending mode b=%d", blending_);
868 layer->blending = DrmHwcBlending::kNone;
869 break;
870 }
871
872 OutputFd release_fence = release_fence_output();
873
874 layer->sf_handle = buffer_;
875 layer->acquire_fence = acquire_fence_.Release();
876 layer->release_fence = std::move(release_fence);
877 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +0200878 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -0500879 layer->SetSourceCrop(source_crop_);
880 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -0500881}
882
883// static
884int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
885 unsupported(__func__);
886 return 0;
887}
888
889// static
890void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -0500891 uint32_t *out_count,
892 int32_t * /*out_capabilities*/) {
893 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500894 *out_count = 0;
895}
896
897// static
Sean Paulac874152016-03-10 16:00:26 -0500898hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
899 struct hwc2_device * /*dev*/, int32_t descriptor) {
900 supported(__func__);
901 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500902 switch (func) {
903 // Device functions
904 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
905 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
906 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
907 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -0400908 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500909 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
910 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
911 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
912 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
913 case HWC2::FunctionDescriptor::Dump:
914 return ToHook<HWC2_PFN_DUMP>(
915 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
916 uint32_t *, char *>);
917 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
918 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
919 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
920 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
921 case HWC2::FunctionDescriptor::RegisterCallback:
922 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
923 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
924 &DrmHwcTwo::RegisterCallback, int32_t,
925 hwc2_callback_data_t, hwc2_function_pointer_t>);
926
927 // Display functions
928 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
929 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
930 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
931 &HwcDisplay::AcceptDisplayChanges>);
932 case HWC2::FunctionDescriptor::CreateLayer:
933 return ToHook<HWC2_PFN_CREATE_LAYER>(
934 DisplayHook<decltype(&HwcDisplay::CreateLayer),
935 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
936 case HWC2::FunctionDescriptor::DestroyLayer:
937 return ToHook<HWC2_PFN_DESTROY_LAYER>(
938 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
939 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
940 case HWC2::FunctionDescriptor::GetActiveConfig:
941 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
942 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
943 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
944 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
945 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
946 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
947 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
948 hwc2_layer_t *, int32_t *>);
949 case HWC2::FunctionDescriptor::GetClientTargetSupport:
950 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
951 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
952 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
953 int32_t, int32_t>);
954 case HWC2::FunctionDescriptor::GetColorModes:
955 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
956 DisplayHook<decltype(&HwcDisplay::GetColorModes),
957 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
958 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -0400959 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
960 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
961 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
962 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500963 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -0400964 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
965 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
966 &HwcDisplay::GetDisplayConfigs, uint32_t *,
967 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500968 case HWC2::FunctionDescriptor::GetDisplayName:
969 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
970 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
971 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
972 case HWC2::FunctionDescriptor::GetDisplayRequests:
973 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
974 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
975 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
976 hwc2_layer_t *, int32_t *>);
977 case HWC2::FunctionDescriptor::GetDisplayType:
978 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
979 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
980 &HwcDisplay::GetDisplayType, int32_t *>);
981 case HWC2::FunctionDescriptor::GetDozeSupport:
982 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
983 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
984 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -0500985 case HWC2::FunctionDescriptor::GetHdrCapabilities:
986 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
987 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
988 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
989 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500990 case HWC2::FunctionDescriptor::GetReleaseFences:
991 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
992 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
993 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
994 int32_t *>);
995 case HWC2::FunctionDescriptor::PresentDisplay:
996 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
997 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
998 &HwcDisplay::PresentDisplay, int32_t *>);
999 case HWC2::FunctionDescriptor::SetActiveConfig:
1000 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1001 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1002 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1003 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001004 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1005 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1006 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1007 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001008 case HWC2::FunctionDescriptor::SetColorMode:
1009 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1010 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1011 &HwcDisplay::SetColorMode, int32_t>);
1012 case HWC2::FunctionDescriptor::SetColorTransform:
1013 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1014 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1015 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1016 case HWC2::FunctionDescriptor::SetOutputBuffer:
1017 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1018 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1019 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1020 case HWC2::FunctionDescriptor::SetPowerMode:
1021 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1022 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1023 &HwcDisplay::SetPowerMode, int32_t>);
1024 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1025 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1026 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1027 &HwcDisplay::SetVsyncEnabled, int32_t>);
1028 case HWC2::FunctionDescriptor::ValidateDisplay:
1029 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1030 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1031 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
1032
1033 // Layer functions
1034 case HWC2::FunctionDescriptor::SetCursorPosition:
1035 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1036 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1037 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1038 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1039 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1040 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1041 &HwcLayer::SetLayerBlendMode, int32_t>);
1042 case HWC2::FunctionDescriptor::SetLayerBuffer:
1043 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1044 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1045 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1046 case HWC2::FunctionDescriptor::SetLayerColor:
1047 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1048 LayerHook<decltype(&HwcLayer::SetLayerColor),
1049 &HwcLayer::SetLayerColor, hwc_color_t>);
1050 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1051 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1052 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1053 &HwcLayer::SetLayerCompositionType, int32_t>);
1054 case HWC2::FunctionDescriptor::SetLayerDataspace:
1055 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1056 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1057 &HwcLayer::SetLayerDataspace, int32_t>);
1058 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1059 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1060 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1061 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1062 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1063 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1064 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1065 &HwcLayer::SetLayerPlaneAlpha, float>);
1066 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001067 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1068 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1069 &HwcLayer::SetLayerSidebandStream,
1070 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001071 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1072 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1073 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1074 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1075 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1076 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1077 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1078 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1079 case HWC2::FunctionDescriptor::SetLayerTransform:
1080 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1081 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1082 &HwcLayer::SetLayerTransform, int32_t>);
1083 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1084 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1085 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1086 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1087 case HWC2::FunctionDescriptor::SetLayerZOrder:
1088 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1089 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1090 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001091 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001092 default:
1093 return NULL;
1094 }
1095}
Sean Paulac874152016-03-10 16:00:26 -05001096
1097// static
1098int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1099 struct hw_device_t **dev) {
1100 supported(__func__);
1101 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1102 ALOGE("Invalid module name- %s", name);
1103 return -EINVAL;
1104 }
1105
1106 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1107 if (!ctx) {
1108 ALOGE("Failed to allocate DrmHwcTwo");
1109 return -ENOMEM;
1110 }
1111
1112 HWC2::Error err = ctx->Init();
1113 if (err != HWC2::Error::None) {
1114 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1115 return -EINVAL;
1116 }
1117
1118 ctx->common.module = const_cast<hw_module_t *>(module);
1119 *dev = &ctx->common;
1120 ctx.release();
1121 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001122}
Sean Paulf72cccd2018-08-27 13:59:08 -04001123} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001124
1125static struct hw_module_methods_t hwc2_module_methods = {
1126 .open = android::DrmHwcTwo::HookDevOpen,
1127};
1128
1129hw_module_t HAL_MODULE_INFO_SYM = {
1130 .tag = HARDWARE_MODULE_TAG,
1131 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1132 .id = HWC_HARDWARE_MODULE_ID,
1133 .name = "DrmHwcTwo module",
1134 .author = "The Android Open Source Project",
1135 .methods = &hwc2_module_methods,
1136 .dso = NULL,
1137 .reserved = {0},
1138};