blob: c801f2ed83c6b943c213152a359419a6de5e27c3 [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;
494 if (test)
495 comp_type = l.second.sf_type();
496 else
497 comp_type = l.second.validated_type();
498
499 switch (comp_type) {
Sean Paulac874152016-03-10 16:00:26 -0500500 case HWC2::Composition::Device:
501 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
502 break;
503 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100504 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500505 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100506 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500507 break;
508 default:
509 continue;
510 }
511 }
512 if (use_client_layer)
513 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
514
Rob Herring4f6c62e2018-05-17 14:33:02 -0500515 if (z_map.empty())
516 return HWC2::Error::BadLayer;
517
Sean Paulac874152016-03-10 16:00:26 -0500518 // now that they're ordered by z, add them to the composition
519 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
520 DrmHwcLayer layer;
521 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200522 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500523 if (ret) {
524 ALOGE("Failed to import layer, ret=%d", ret);
525 return HWC2::Error::NoResources;
526 }
527 map.layers.emplace_back(std::move(layer));
528 }
Sean Paulac874152016-03-10 16:00:26 -0500529
Sean Paulf72cccd2018-08-27 13:59:08 -0400530 std::unique_ptr<DrmDisplayComposition> composition = compositor_
531 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500532 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
533
534 // TODO: Don't always assume geometry changed
535 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
536 if (ret) {
537 ALOGE("Failed to set layers in the composition ret=%d", ret);
538 return HWC2::Error::BadLayer;
539 }
540
541 std::vector<DrmPlane *> primary_planes(primary_planes_);
542 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500543 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500544 if (ret) {
545 ALOGE("Failed to plan the composition ret=%d", ret);
546 return HWC2::Error::BadConfig;
547 }
548
549 // Disable the planes we're not using
550 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
551 composition->AddPlaneDisable(*i);
552 i = primary_planes.erase(i);
553 }
554 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
555 composition->AddPlaneDisable(*i);
556 i = overlay_planes.erase(i);
557 }
558
Rob Herring4f6c62e2018-05-17 14:33:02 -0500559 if (test) {
560 ret = compositor_.TestComposition(composition.get());
561 } else {
562 AddFenceToRetireFence(composition->take_out_fence());
563 ret = compositor_.ApplyComposition(std::move(composition));
564 }
Sean Paulac874152016-03-10 16:00:26 -0500565 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700566 if (!test)
567 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500568 return HWC2::Error::BadParameter;
569 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500570 return HWC2::Error::None;
571}
572
573HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *retire_fence) {
574 supported(__func__);
575 HWC2::Error ret;
576
577 ret = CreateComposition(false);
578 if (ret == HWC2::Error::BadLayer) {
579 // Can we really have no client or device layers?
580 *retire_fence = -1;
581 return HWC2::Error::None;
582 }
583 if (ret != HWC2::Error::None)
584 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500585
Sean Paulac874152016-03-10 16:00:26 -0500586 // The retire fence returned here is for the last frame, so return it and
587 // promote the next retire fence
588 *retire_fence = retire_fence_.Release();
589 retire_fence_ = std::move(next_retire_fence_);
590
591 ++frame_no_;
592 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500593}
594
595HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500596 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400597 auto mode = std::find_if(connector_->modes().begin(),
598 connector_->modes().end(),
599 [config](DrmMode const &m) {
600 return m.id() == config;
601 });
Sean Paulac874152016-03-10 16:00:26 -0500602 if (mode == connector_->modes().end()) {
603 ALOGE("Could not find active mode for %d", config);
604 return HWC2::Error::BadConfig;
605 }
606
Sean Paulf72cccd2018-08-27 13:59:08 -0400607 std::unique_ptr<DrmDisplayComposition> composition = compositor_
608 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500609 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
610 int ret = composition->SetDisplayMode(*mode);
Sean Pauled45a8e2017-02-28 13:17:34 -0500611 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500612 if (ret) {
613 ALOGE("Failed to queue dpms composition on %d", ret);
614 return HWC2::Error::BadConfig;
615 }
616 if (connector_->active_mode().id() == 0)
617 connector_->set_active_mode(*mode);
618
619 // Setup the client layer's dimensions
620 hwc_rect_t display_frame = {.left = 0,
621 .top = 0,
622 .right = static_cast<int>(mode->h_display()),
623 .bottom = static_cast<int>(mode->v_display())};
624 client_layer_.SetLayerDisplayFrame(display_frame);
625 hwc_frect_t source_crop = {.left = 0.0f,
626 .top = 0.0f,
627 .right = mode->h_display() + 0.0f,
628 .bottom = mode->v_display() + 0.0f};
629 client_layer_.SetLayerSourceCrop(source_crop);
630
631 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500632}
633
634HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
635 int32_t acquire_fence,
636 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600637 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500638 supported(__func__);
639 UniqueFd uf(acquire_fence);
640
641 client_layer_.set_buffer(target);
642 client_layer_.set_acquire_fence(uf.get());
643 client_layer_.SetLayerDataspace(dataspace);
644 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500645}
646
647HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500648 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800649
650 if (mode != HAL_COLOR_MODE_NATIVE)
651 return HWC2::Error::Unsupported;
652
653 color_mode_ = mode;
654 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500655}
656
657HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500658 int32_t hint) {
659 supported(__func__);
660 // TODO: Force client composition if we get this
Sean Pauled2ec4b2016-03-10 15:35:40 -0500661 return unsupported(__func__, matrix, hint);
662}
663
664HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500665 int32_t release_fence) {
666 supported(__func__);
667 // TODO: Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500668 return unsupported(__func__, buffer, release_fence);
669}
670
Sean Paulac874152016-03-10 16:00:26 -0500671HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
672 supported(__func__);
673 uint64_t dpms_value = 0;
674 auto mode = static_cast<HWC2::PowerMode>(mode_in);
675 switch (mode) {
676 case HWC2::PowerMode::Off:
677 dpms_value = DRM_MODE_DPMS_OFF;
678 break;
679 case HWC2::PowerMode::On:
680 dpms_value = DRM_MODE_DPMS_ON;
681 break;
682 default:
683 ALOGI("Power mode %d is unsupported\n", mode);
684 return HWC2::Error::Unsupported;
685 };
686
Sean Paulf72cccd2018-08-27 13:59:08 -0400687 std::unique_ptr<DrmDisplayComposition> composition = compositor_
688 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500689 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
690 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500691 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500692 if (ret) {
693 ALOGE("Failed to apply the dpms composition ret=%d", ret);
694 return HWC2::Error::BadParameter;
695 }
696 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500697}
698
699HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500700 supported(__func__);
701 vsync_worker_.VSyncControl(enabled);
702 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500703}
704
705HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500706 uint32_t *num_requests) {
707 supported(__func__);
708 *num_types = 0;
709 *num_requests = 0;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500710 size_t avail_planes = primary_planes_.size() + overlay_planes_.size();
John Stultz76ca20e2018-07-06 10:34:42 -0700711 bool comp_failed = false;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500712
713 HWC2::Error ret;
714
715 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
716 l.second.set_validated_type(HWC2::Composition::Invalid);
717
718 ret = CreateComposition(true);
719 if (ret != HWC2::Error::None)
John Stultz76ca20e2018-07-06 10:34:42 -0700720 comp_failed = true;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500721
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100722 std::map<uint32_t, DrmHwcTwo::HwcLayer *, std::greater<int>> z_map;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500723 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
724 if (l.second.sf_type() == HWC2::Composition::Device)
725 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
726 }
727
728 /*
729 * If more layers then planes, save one plane
730 * for client composited layers
731 */
732 if (avail_planes < layers_.size())
733 avail_planes--;
734
735 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
John Stultz76ca20e2018-07-06 10:34:42 -0700736 if (comp_failed || !avail_planes--)
Rob Herring4f6c62e2018-05-17 14:33:02 -0500737 break;
738 l.second->set_validated_type(HWC2::Composition::Device);
739 }
740
Sean Paulac874152016-03-10 16:00:26 -0500741 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
742 DrmHwcTwo::HwcLayer &layer = l.second;
743 switch (layer.sf_type()) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500744 case HWC2::Composition::Device:
745 if (layer.validated_type() == HWC2::Composition::Device)
746 break;
747 // fall thru
Sean Paulac874152016-03-10 16:00:26 -0500748 case HWC2::Composition::SolidColor:
749 case HWC2::Composition::Cursor:
750 case HWC2::Composition::Sideband:
Rob Herringaf0d9752018-05-04 16:34:19 -0500751 default:
Sean Paulac874152016-03-10 16:00:26 -0500752 layer.set_validated_type(HWC2::Composition::Client);
753 ++*num_types;
754 break;
Sean Paulac874152016-03-10 16:00:26 -0500755 }
756 }
Rob Herringee8f45b2017-06-09 15:15:55 -0500757 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500758}
759
760HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -0500761 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800762 cursor_x_ = x;
763 cursor_y_ = y;
764 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500765}
766
767HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500768 supported(__func__);
769 blending_ = static_cast<HWC2::BlendMode>(mode);
770 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500771}
772
773HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500774 int32_t acquire_fence) {
775 supported(__func__);
776 UniqueFd uf(acquire_fence);
777
778 // The buffer and acquire_fence are handled elsewhere
779 if (sf_type_ == HWC2::Composition::Client ||
780 sf_type_ == HWC2::Composition::Sideband ||
781 sf_type_ == HWC2::Composition::SolidColor)
782 return HWC2::Error::None;
783
784 set_buffer(buffer);
785 set_acquire_fence(uf.get());
786 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500787}
788
789HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Sean Paulac874152016-03-10 16:00:26 -0500790 // TODO: Punt to client composition here?
Sean Pauled2ec4b2016-03-10 15:35:40 -0500791 return unsupported(__func__, color);
792}
793
794HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -0500795 sf_type_ = static_cast<HWC2::Composition>(type);
796 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500797}
798
799HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -0500800 supported(__func__);
801 dataspace_ = static_cast<android_dataspace_t>(dataspace);
802 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500803}
804
805HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -0500806 supported(__func__);
807 display_frame_ = frame;
808 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500809}
810
811HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -0500812 supported(__func__);
813 alpha_ = alpha;
814 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500815}
816
817HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
818 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -0500819 supported(__func__);
820 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -0500821 return unsupported(__func__, stream);
822}
823
824HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -0500825 supported(__func__);
826 source_crop_ = crop;
827 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500828}
829
830HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -0500831 supported(__func__);
832 // TODO: We don't use surface damage, marking as unsupported
833 unsupported(__func__, damage);
834 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500835}
836
837HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -0500838 supported(__func__);
839 transform_ = static_cast<HWC2::Transform>(transform);
840 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500841}
842
843HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -0500844 supported(__func__);
845 // TODO: We don't use this information, marking as unsupported
846 unsupported(__func__, visible);
847 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500848}
849
Sean Paulac874152016-03-10 16:00:26 -0500850HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
851 supported(__func__);
852 z_order_ = order;
853 return HWC2::Error::None;
854}
855
856void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
857 supported(__func__);
858 switch (blending_) {
859 case HWC2::BlendMode::None:
860 layer->blending = DrmHwcBlending::kNone;
861 break;
862 case HWC2::BlendMode::Premultiplied:
863 layer->blending = DrmHwcBlending::kPreMult;
864 break;
865 case HWC2::BlendMode::Coverage:
866 layer->blending = DrmHwcBlending::kCoverage;
867 break;
868 default:
869 ALOGE("Unknown blending mode b=%d", blending_);
870 layer->blending = DrmHwcBlending::kNone;
871 break;
872 }
873
874 OutputFd release_fence = release_fence_output();
875
876 layer->sf_handle = buffer_;
877 layer->acquire_fence = acquire_fence_.Release();
878 layer->release_fence = std::move(release_fence);
879 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +0200880 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -0500881 layer->SetSourceCrop(source_crop_);
882 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -0500883}
884
885// static
886int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
887 unsupported(__func__);
888 return 0;
889}
890
891// static
892void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -0500893 uint32_t *out_count,
894 int32_t * /*out_capabilities*/) {
895 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500896 *out_count = 0;
897}
898
899// static
Sean Paulac874152016-03-10 16:00:26 -0500900hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
901 struct hwc2_device * /*dev*/, int32_t descriptor) {
902 supported(__func__);
903 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500904 switch (func) {
905 // Device functions
906 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
907 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
908 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
909 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -0400910 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500911 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
912 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
913 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
914 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
915 case HWC2::FunctionDescriptor::Dump:
916 return ToHook<HWC2_PFN_DUMP>(
917 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
918 uint32_t *, char *>);
919 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
920 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
921 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
922 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
923 case HWC2::FunctionDescriptor::RegisterCallback:
924 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
925 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
926 &DrmHwcTwo::RegisterCallback, int32_t,
927 hwc2_callback_data_t, hwc2_function_pointer_t>);
928
929 // Display functions
930 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
931 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
932 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
933 &HwcDisplay::AcceptDisplayChanges>);
934 case HWC2::FunctionDescriptor::CreateLayer:
935 return ToHook<HWC2_PFN_CREATE_LAYER>(
936 DisplayHook<decltype(&HwcDisplay::CreateLayer),
937 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
938 case HWC2::FunctionDescriptor::DestroyLayer:
939 return ToHook<HWC2_PFN_DESTROY_LAYER>(
940 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
941 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
942 case HWC2::FunctionDescriptor::GetActiveConfig:
943 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
944 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
945 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
946 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
947 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
948 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
949 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
950 hwc2_layer_t *, int32_t *>);
951 case HWC2::FunctionDescriptor::GetClientTargetSupport:
952 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
953 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
954 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
955 int32_t, int32_t>);
956 case HWC2::FunctionDescriptor::GetColorModes:
957 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
958 DisplayHook<decltype(&HwcDisplay::GetColorModes),
959 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
960 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -0400961 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
962 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
963 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
964 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500965 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -0400966 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
967 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
968 &HwcDisplay::GetDisplayConfigs, uint32_t *,
969 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500970 case HWC2::FunctionDescriptor::GetDisplayName:
971 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
972 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
973 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
974 case HWC2::FunctionDescriptor::GetDisplayRequests:
975 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
976 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
977 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
978 hwc2_layer_t *, int32_t *>);
979 case HWC2::FunctionDescriptor::GetDisplayType:
980 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
981 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
982 &HwcDisplay::GetDisplayType, int32_t *>);
983 case HWC2::FunctionDescriptor::GetDozeSupport:
984 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
985 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
986 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -0500987 case HWC2::FunctionDescriptor::GetHdrCapabilities:
988 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
989 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
990 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
991 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500992 case HWC2::FunctionDescriptor::GetReleaseFences:
993 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
994 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
995 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
996 int32_t *>);
997 case HWC2::FunctionDescriptor::PresentDisplay:
998 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
999 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1000 &HwcDisplay::PresentDisplay, int32_t *>);
1001 case HWC2::FunctionDescriptor::SetActiveConfig:
1002 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1003 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1004 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1005 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001006 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1007 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1008 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1009 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001010 case HWC2::FunctionDescriptor::SetColorMode:
1011 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1012 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1013 &HwcDisplay::SetColorMode, int32_t>);
1014 case HWC2::FunctionDescriptor::SetColorTransform:
1015 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1016 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1017 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1018 case HWC2::FunctionDescriptor::SetOutputBuffer:
1019 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1020 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1021 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1022 case HWC2::FunctionDescriptor::SetPowerMode:
1023 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1024 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1025 &HwcDisplay::SetPowerMode, int32_t>);
1026 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1027 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1028 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1029 &HwcDisplay::SetVsyncEnabled, int32_t>);
1030 case HWC2::FunctionDescriptor::ValidateDisplay:
1031 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1032 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1033 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
1034
1035 // Layer functions
1036 case HWC2::FunctionDescriptor::SetCursorPosition:
1037 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1038 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1039 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1040 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1041 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1042 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1043 &HwcLayer::SetLayerBlendMode, int32_t>);
1044 case HWC2::FunctionDescriptor::SetLayerBuffer:
1045 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1046 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1047 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1048 case HWC2::FunctionDescriptor::SetLayerColor:
1049 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1050 LayerHook<decltype(&HwcLayer::SetLayerColor),
1051 &HwcLayer::SetLayerColor, hwc_color_t>);
1052 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1053 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1054 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1055 &HwcLayer::SetLayerCompositionType, int32_t>);
1056 case HWC2::FunctionDescriptor::SetLayerDataspace:
1057 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1058 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1059 &HwcLayer::SetLayerDataspace, int32_t>);
1060 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1061 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1062 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1063 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1064 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1065 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1066 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1067 &HwcLayer::SetLayerPlaneAlpha, float>);
1068 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001069 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1070 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1071 &HwcLayer::SetLayerSidebandStream,
1072 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001073 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1074 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1075 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1076 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1077 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1078 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1079 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1080 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1081 case HWC2::FunctionDescriptor::SetLayerTransform:
1082 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1083 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1084 &HwcLayer::SetLayerTransform, int32_t>);
1085 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1086 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1087 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1088 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1089 case HWC2::FunctionDescriptor::SetLayerZOrder:
1090 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1091 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1092 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001093 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001094 default:
1095 return NULL;
1096 }
1097}
Sean Paulac874152016-03-10 16:00:26 -05001098
1099// static
1100int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1101 struct hw_device_t **dev) {
1102 supported(__func__);
1103 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1104 ALOGE("Invalid module name- %s", name);
1105 return -EINVAL;
1106 }
1107
1108 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1109 if (!ctx) {
1110 ALOGE("Failed to allocate DrmHwcTwo");
1111 return -ENOMEM;
1112 }
1113
1114 HWC2::Error err = ctx->Init();
1115 if (err != HWC2::Error::None) {
1116 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1117 return -EINVAL;
1118 }
1119
1120 ctx->common.module = const_cast<hw_module_t *>(module);
1121 *dev = &ctx->common;
1122 ctx.release();
1123 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001124}
Sean Paulf72cccd2018-08-27 13:59:08 -04001125} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001126
1127static struct hw_module_methods_t hwc2_module_methods = {
1128 .open = android::DrmHwcTwo::HookDevOpen,
1129};
1130
1131hw_module_t HAL_MODULE_INFO_SYM = {
1132 .tag = HARDWARE_MODULE_TAG,
1133 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1134 .id = HWC_HARDWARE_MODULE_ID,
1135 .name = "DrmHwcTwo module",
1136 .author = "The Android Open Source Project",
1137 .methods = &hwc2_module_methods,
1138 .dso = NULL,
1139 .reserved = {0},
1140};