blob: a6b48e22c1afbbe764fda9cb52e704a35ff46ee4 [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 Paulac874152016-03-10 16:00:26 -050020#include "drmdisplaycomposition.h"
21#include "drmhwcomposer.h"
Sean Pauled2ec4b2016-03-10 15:35:40 -050022#include "drmhwctwo.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
John Stultz9057a6f2018-04-26 12:05:55 -070029#include <log/log.h>
Sean Paulac874152016-03-10 16:00:26 -050030#include <cutils/properties.h>
31#include <hardware/hardware.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050032#include <hardware/hwcomposer2.h>
33
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);
68 std::shared_ptr<Importer> importer =
69 resource_manager_.GetImporter(HWC_DISPLAY_PRIMARY);
70 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
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +010075 displays_.emplace(
76 std::piecewise_construct, std::forward_as_tuple(HWC_DISPLAY_PRIMARY),
77 std::forward_as_tuple(&resource_manager_, drm, importer,
78 HWC_DISPLAY_PRIMARY, HWC2::DisplayType::Physical));
Sean Paulac874152016-03-10 16:00:26 -050079
Alexandru Gheorghec5463582018-03-27 15:52:02 +010080 DrmCrtc *crtc = drm->GetCrtcForDisplay(static_cast<int>(HWC_DISPLAY_PRIMARY));
Sean Paulac874152016-03-10 16:00:26 -050081 if (!crtc) {
82 ALOGE("Failed to get crtc for display %d",
83 static_cast<int>(HWC_DISPLAY_PRIMARY));
84 return HWC2::Error::BadDisplay;
85 }
86
87 std::vector<DrmPlane *> display_planes;
Alexandru Gheorghec5463582018-03-27 15:52:02 +010088 for (auto &plane : drm->planes()) {
Sean Paulac874152016-03-10 16:00:26 -050089 if (plane->GetCrtcSupported(*crtc))
90 display_planes.push_back(plane.get());
91 }
92 displays_.at(HWC_DISPLAY_PRIMARY).Init(&display_planes);
93 return HWC2::Error::None;
94}
95
Sean Pauled2ec4b2016-03-10 15:35:40 -050096template <typename... Args>
97static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
98 ALOGV("Unsupported function: %s", func);
99 return HWC2::Error::Unsupported;
100}
101
Sean Paulac874152016-03-10 16:00:26 -0500102static inline void supported(char const *func) {
103 ALOGV("Supported function: %s", func);
104}
105
Sean Pauled2ec4b2016-03-10 15:35:40 -0500106HWC2::Error DrmHwcTwo::CreateVirtualDisplay(uint32_t width, uint32_t height,
107 int32_t *format,
108 hwc2_display_t *display) {
109 // TODO: Implement virtual display
Sean Paulac874152016-03-10 16:00:26 -0500110 return unsupported(__func__, width, height, format, display);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500111}
112
113HWC2::Error DrmHwcTwo::DestroyVirtualDisplay(hwc2_display_t display) {
Sean Paulac874152016-03-10 16:00:26 -0500114 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500115 return unsupported(__func__, display);
116}
117
118void DrmHwcTwo::Dump(uint32_t *size, char *buffer) {
Sean Paulac874152016-03-10 16:00:26 -0500119 // TODO: Implement dump
Sean Pauled2ec4b2016-03-10 15:35:40 -0500120 unsupported(__func__, size, buffer);
121}
122
123uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() {
Sean Paulac874152016-03-10 16:00:26 -0500124 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500125 unsupported(__func__);
126 return 0;
127}
128
129HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor,
Sean Paulac874152016-03-10 16:00:26 -0500130 hwc2_callback_data_t data,
131 hwc2_function_pointer_t function) {
132 supported(__func__);
133 auto callback = static_cast<HWC2::Callback>(descriptor);
134 callbacks_.emplace(callback, HwcCallback(data, function));
135
136 switch (callback) {
137 case HWC2::Callback::Hotplug: {
138 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(function);
139 hotplug(data, HWC_DISPLAY_PRIMARY,
140 static_cast<int32_t>(HWC2::Connection::Connected));
141 break;
142 }
143 case HWC2::Callback::Vsync: {
144 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
145 displays_)
146 d.second.RegisterVsyncCallback(data, function);
147 break;
148 }
149 default:
150 break;
151 }
152 return HWC2::Error::None;
153}
154
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100155DrmHwcTwo::HwcDisplay::HwcDisplay(ResourceManager *resource_manager,
156 DrmDevice *drm,
Sean Paulac874152016-03-10 16:00:26 -0500157 std::shared_ptr<Importer> importer,
Sean Paulac874152016-03-10 16:00:26 -0500158 hwc2_display_t handle, HWC2::DisplayType type)
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100159 : resource_manager_(resource_manager),
160 drm_(drm),
161 importer_(importer),
162 handle_(handle),
163 type_(type) {
Sean Paulac874152016-03-10 16:00:26 -0500164 supported(__func__);
165}
166
167HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
168 supported(__func__);
169 planner_ = Planner::CreateInstance(drm_);
170 if (!planner_) {
171 ALOGE("Failed to create planner instance for composition");
172 return HWC2::Error::NoResources;
173 }
174
175 int display = static_cast<int>(handle_);
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100176 int ret = compositor_.Init(resource_manager_, drm_, display);
Sean Paulac874152016-03-10 16:00:26 -0500177 if (ret) {
178 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
179 return HWC2::Error::NoResources;
180 }
181
182 // Split up the given display planes into primary and overlay to properly
183 // interface with the composition
184 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
185 property_get("hwc.drm.use_overlay_planes", use_overlay_planes_prop, "1");
186 bool use_overlay_planes = atoi(use_overlay_planes_prop);
187 for (auto &plane : *planes) {
188 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
189 primary_planes_.push_back(plane);
190 else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY)
191 overlay_planes_.push_back(plane);
192 }
193
194 crtc_ = drm_->GetCrtcForDisplay(display);
195 if (!crtc_) {
196 ALOGE("Failed to get crtc for display %d", display);
197 return HWC2::Error::BadDisplay;
198 }
199
200 connector_ = drm_->GetConnectorForDisplay(display);
201 if (!connector_) {
202 ALOGE("Failed to get connector for display %d", display);
203 return HWC2::Error::BadDisplay;
204 }
205
206 // Fetch the number of modes from the display
207 uint32_t num_configs;
208 HWC2::Error err = GetDisplayConfigs(&num_configs, NULL);
209 if (err != HWC2::Error::None || !num_configs)
210 return err;
211
212 // Grab the first mode, we'll choose this as the active mode
213 // TODO: Should choose the preferred mode here
214 hwc2_config_t default_config;
215 num_configs = 1;
216 err = GetDisplayConfigs(&num_configs, &default_config);
217 if (err != HWC2::Error::None)
218 return err;
219
220 ret = vsync_worker_.Init(drm_, display);
221 if (ret) {
222 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
223 return HWC2::Error::BadDisplay;
224 }
225
226 return SetActiveConfig(default_config);
227}
228
229HWC2::Error DrmHwcTwo::HwcDisplay::RegisterVsyncCallback(
230 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
231 supported(__func__);
232 auto callback = std::make_shared<DrmVsyncCallback>(data, func);
Adrian Salidofa37f672017-02-16 10:29:46 -0800233 vsync_worker_.RegisterCallback(std::move(callback));
Sean Paulac874152016-03-10 16:00:26 -0500234 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500235}
236
237HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500238 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500239 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
240 l.second.accept_type_change();
241 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500242}
243
244HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500245 supported(__func__);
246 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
247 *layer = static_cast<hwc2_layer_t>(layer_idx_);
248 ++layer_idx_;
249 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500250}
251
252HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Sean Paulac874152016-03-10 16:00:26 -0500253 supported(__func__);
254 layers_.erase(layer);
255 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500256}
257
258HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
Sean Paulac874152016-03-10 16:00:26 -0500259 supported(__func__);
260 DrmMode const &mode = connector_->active_mode();
261 if (mode.id() == 0)
262 return HWC2::Error::BadConfig;
263
264 *config = mode.id();
265 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500266}
267
268HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
269 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500270 supported(__func__);
271 uint32_t num_changes = 0;
272 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
273 if (l.second.type_changed()) {
274 if (layers && num_changes < *num_elements)
275 layers[num_changes] = l.first;
276 if (types && num_changes < *num_elements)
277 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
278 ++num_changes;
279 }
280 }
281 if (!layers && !types)
282 *num_elements = num_changes;
283 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500284}
285
286HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500287 uint32_t height,
288 int32_t /*format*/,
289 int32_t dataspace) {
290 supported(__func__);
291 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
292 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
293
294 if (width < min.first || height < min.second)
295 return HWC2::Error::Unsupported;
296
297 if (width > max.first || height > max.second)
298 return HWC2::Error::Unsupported;
299
300 if (dataspace != HAL_DATASPACE_UNKNOWN &&
301 dataspace != HAL_DATASPACE_STANDARD_UNSPECIFIED)
302 return HWC2::Error::Unsupported;
303
304 // TODO: Validate format can be handled by either GL or planes
305 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500306}
307
308HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500309 int32_t *modes) {
310 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800311 if (!modes)
312 *num_modes = 1;
313
314 if (modes)
315 *modes = HAL_COLOR_MODE_NATIVE;
316
317 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500318}
319
320HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500321 int32_t attribute_in,
322 int32_t *value) {
323 supported(__func__);
324 auto mode =
325 std::find_if(connector_->modes().begin(), connector_->modes().end(),
326 [config](DrmMode const &m) { return m.id() == config; });
327 if (mode == connector_->modes().end()) {
328 ALOGE("Could not find active mode for %d", config);
329 return HWC2::Error::BadConfig;
330 }
331
332 static const int32_t kUmPerInch = 25400;
333 uint32_t mm_width = connector_->mm_width();
334 uint32_t mm_height = connector_->mm_height();
335 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
336 switch (attribute) {
337 case HWC2::Attribute::Width:
338 *value = mode->h_display();
339 break;
340 case HWC2::Attribute::Height:
341 *value = mode->v_display();
342 break;
343 case HWC2::Attribute::VsyncPeriod:
344 // in nanoseconds
345 *value = 1000 * 1000 * 1000 / mode->v_refresh();
346 break;
347 case HWC2::Attribute::DpiX:
348 // Dots per 1000 inches
349 *value = mm_width ? (mode->h_display() * kUmPerInch) / mm_width : -1;
350 break;
351 case HWC2::Attribute::DpiY:
352 // Dots per 1000 inches
353 *value = mm_height ? (mode->v_display() * kUmPerInch) / mm_height : -1;
354 break;
355 default:
356 *value = -1;
357 return HWC2::Error::BadConfig;
358 }
359 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500360}
361
362HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
363 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500364 supported(__func__);
365 // Since this callback is normally invoked twice (once to get the count, and
366 // once to populate configs), we don't really want to read the edid
367 // redundantly. Instead, only update the modes on the first invocation. While
368 // it's possible this will result in stale modes, it'll all come out in the
369 // wash when we try to set the active config later.
370 if (!configs) {
371 int ret = connector_->UpdateModes();
372 if (ret) {
373 ALOGE("Failed to update display modes %d", ret);
374 return HWC2::Error::BadDisplay;
375 }
376 }
377
378 auto num_modes = static_cast<uint32_t>(connector_->modes().size());
379 if (!configs) {
380 *num_configs = num_modes;
381 return HWC2::Error::None;
382 }
383
384 uint32_t idx = 0;
385 for (const DrmMode &mode : connector_->modes()) {
386 if (idx >= *num_configs)
387 break;
388 configs[idx++] = mode.id();
389 }
390 *num_configs = idx;
391 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500392}
393
394HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500395 supported(__func__);
396 std::ostringstream stream;
397 stream << "display-" << connector_->id();
398 std::string string = stream.str();
399 size_t length = string.length();
400 if (!name) {
401 *size = length;
402 return HWC2::Error::None;
403 }
404
405 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
406 strncpy(name, string.c_str(), *size);
407 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500408}
409
Sean Paulac874152016-03-10 16:00:26 -0500410HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
411 uint32_t *num_elements,
412 hwc2_layer_t *layers,
413 int32_t *layer_requests) {
414 supported(__func__);
415 // TODO: I think virtual display should request
416 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
417 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
418 *num_elements = 0;
419 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500420}
421
422HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500423 supported(__func__);
424 *type = static_cast<int32_t>(type_);
425 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500426}
427
428HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500429 supported(__func__);
430 *support = 0;
431 return HWC2::Error::None;
432}
433
434HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
435 uint32_t *num_types, int32_t */*types*/, float */*max_luminance*/,
436 float */*max_average_luminance*/, float */*min_luminance*/) {
437 supported(__func__);
438 *num_types = 0;
439 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500440}
441
442HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500443 hwc2_layer_t *layers,
444 int32_t *fences) {
445 supported(__func__);
446 uint32_t num_layers = 0;
447
448 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
449 ++num_layers;
450 if (layers == NULL || fences == NULL) {
451 continue;
452 } else if (num_layers > *num_elements) {
453 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
454 return HWC2::Error::None;
455 }
456
457 layers[num_layers - 1] = l.first;
458 fences[num_layers - 1] = l.second.take_release_fence();
459 }
460 *num_elements = num_layers;
461 return HWC2::Error::None;
462}
463
464void DrmHwcTwo::HwcDisplay::AddFenceToRetireFence(int fd) {
465 supported(__func__);
466 if (fd < 0)
467 return;
468
469 if (next_retire_fence_.get() >= 0) {
470 int old_fence = next_retire_fence_.get();
471 next_retire_fence_.Set(sync_merge("dc_retire", old_fence, fd));
472 } else {
473 next_retire_fence_.Set(dup(fd));
474 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500475}
476
Rob Herring4f6c62e2018-05-17 14:33:02 -0500477HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500478 std::vector<DrmCompositionDisplayLayersMap> layers_map;
479 layers_map.emplace_back();
480 DrmCompositionDisplayLayersMap &map = layers_map.back();
481
482 map.display = static_cast<int>(handle_);
483 map.geometry_changed = true; // TODO: Fix this
484
485 // order the layers by z-order
486 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100487 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500488 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
489 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500490 HWC2::Composition comp_type;
491 if (test)
492 comp_type = l.second.sf_type();
493 else
494 comp_type = l.second.validated_type();
495
496 switch (comp_type) {
Sean Paulac874152016-03-10 16:00:26 -0500497 case HWC2::Composition::Device:
498 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
499 break;
500 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100501 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500502 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100503 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500504 break;
505 default:
506 continue;
507 }
508 }
509 if (use_client_layer)
510 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
511
Rob Herring4f6c62e2018-05-17 14:33:02 -0500512 if (z_map.empty())
513 return HWC2::Error::BadLayer;
514
Sean Paulac874152016-03-10 16:00:26 -0500515 // now that they're ordered by z, add them to the composition
516 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
517 DrmHwcLayer layer;
518 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200519 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500520 if (ret) {
521 ALOGE("Failed to import layer, ret=%d", ret);
522 return HWC2::Error::NoResources;
523 }
524 map.layers.emplace_back(std::move(layer));
525 }
Sean Paulac874152016-03-10 16:00:26 -0500526
527 std::unique_ptr<DrmDisplayComposition> composition =
528 compositor_.CreateComposition();
529 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
530
531 // TODO: Don't always assume geometry changed
532 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
533 if (ret) {
534 ALOGE("Failed to set layers in the composition ret=%d", ret);
535 return HWC2::Error::BadLayer;
536 }
537
538 std::vector<DrmPlane *> primary_planes(primary_planes_);
539 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500540 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500541 if (ret) {
542 ALOGE("Failed to plan the composition ret=%d", ret);
543 return HWC2::Error::BadConfig;
544 }
545
546 // Disable the planes we're not using
547 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
548 composition->AddPlaneDisable(*i);
549 i = primary_planes.erase(i);
550 }
551 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
552 composition->AddPlaneDisable(*i);
553 i = overlay_planes.erase(i);
554 }
555
Rob Herring4f6c62e2018-05-17 14:33:02 -0500556 if (test) {
557 ret = compositor_.TestComposition(composition.get());
558 } else {
559 AddFenceToRetireFence(composition->take_out_fence());
560 ret = compositor_.ApplyComposition(std::move(composition));
561 }
Sean Paulac874152016-03-10 16:00:26 -0500562 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700563 if (!test)
564 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500565 return HWC2::Error::BadParameter;
566 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500567 return HWC2::Error::None;
568}
569
570HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *retire_fence) {
571 supported(__func__);
572 HWC2::Error ret;
573
574 ret = CreateComposition(false);
575 if (ret == HWC2::Error::BadLayer) {
576 // Can we really have no client or device layers?
577 *retire_fence = -1;
578 return HWC2::Error::None;
579 }
580 if (ret != HWC2::Error::None)
581 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500582
Sean Paulac874152016-03-10 16:00:26 -0500583 // The retire fence returned here is for the last frame, so return it and
584 // promote the next retire fence
585 *retire_fence = retire_fence_.Release();
586 retire_fence_ = std::move(next_retire_fence_);
587
588 ++frame_no_;
589 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500590}
591
592HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500593 supported(__func__);
594 auto mode =
595 std::find_if(connector_->modes().begin(), connector_->modes().end(),
596 [config](DrmMode const &m) { return m.id() == config; });
597 if (mode == connector_->modes().end()) {
598 ALOGE("Could not find active mode for %d", config);
599 return HWC2::Error::BadConfig;
600 }
601
602 std::unique_ptr<DrmDisplayComposition> composition =
603 compositor_.CreateComposition();
604 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
605 int ret = composition->SetDisplayMode(*mode);
Sean Pauled45a8e2017-02-28 13:17:34 -0500606 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500607 if (ret) {
608 ALOGE("Failed to queue dpms composition on %d", ret);
609 return HWC2::Error::BadConfig;
610 }
611 if (connector_->active_mode().id() == 0)
612 connector_->set_active_mode(*mode);
613
614 // Setup the client layer's dimensions
615 hwc_rect_t display_frame = {.left = 0,
616 .top = 0,
617 .right = static_cast<int>(mode->h_display()),
618 .bottom = static_cast<int>(mode->v_display())};
619 client_layer_.SetLayerDisplayFrame(display_frame);
620 hwc_frect_t source_crop = {.left = 0.0f,
621 .top = 0.0f,
622 .right = mode->h_display() + 0.0f,
623 .bottom = mode->v_display() + 0.0f};
624 client_layer_.SetLayerSourceCrop(source_crop);
625
626 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500627}
628
629HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
630 int32_t acquire_fence,
631 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600632 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500633 supported(__func__);
634 UniqueFd uf(acquire_fence);
635
636 client_layer_.set_buffer(target);
637 client_layer_.set_acquire_fence(uf.get());
638 client_layer_.SetLayerDataspace(dataspace);
639 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500640}
641
642HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500643 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800644
645 if (mode != HAL_COLOR_MODE_NATIVE)
646 return HWC2::Error::Unsupported;
647
648 color_mode_ = mode;
649 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500650}
651
652HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500653 int32_t hint) {
654 supported(__func__);
655 // TODO: Force client composition if we get this
Sean Pauled2ec4b2016-03-10 15:35:40 -0500656 return unsupported(__func__, matrix, hint);
657}
658
659HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500660 int32_t release_fence) {
661 supported(__func__);
662 // TODO: Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500663 return unsupported(__func__, buffer, release_fence);
664}
665
Sean Paulac874152016-03-10 16:00:26 -0500666HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
667 supported(__func__);
668 uint64_t dpms_value = 0;
669 auto mode = static_cast<HWC2::PowerMode>(mode_in);
670 switch (mode) {
671 case HWC2::PowerMode::Off:
672 dpms_value = DRM_MODE_DPMS_OFF;
673 break;
674 case HWC2::PowerMode::On:
675 dpms_value = DRM_MODE_DPMS_ON;
676 break;
677 default:
678 ALOGI("Power mode %d is unsupported\n", mode);
679 return HWC2::Error::Unsupported;
680 };
681
682 std::unique_ptr<DrmDisplayComposition> composition =
683 compositor_.CreateComposition();
684 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
685 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500686 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500687 if (ret) {
688 ALOGE("Failed to apply the dpms composition ret=%d", ret);
689 return HWC2::Error::BadParameter;
690 }
691 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500692}
693
694HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500695 supported(__func__);
696 vsync_worker_.VSyncControl(enabled);
697 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500698}
699
700HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500701 uint32_t *num_requests) {
702 supported(__func__);
703 *num_types = 0;
704 *num_requests = 0;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500705 size_t avail_planes = primary_planes_.size() + overlay_planes_.size();
John Stultz76ca20e2018-07-06 10:34:42 -0700706 bool comp_failed = false;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500707
708 HWC2::Error ret;
709
710 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
711 l.second.set_validated_type(HWC2::Composition::Invalid);
712
713 ret = CreateComposition(true);
714 if (ret != HWC2::Error::None)
John Stultz76ca20e2018-07-06 10:34:42 -0700715 comp_failed = true;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500716
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100717 std::map<uint32_t, DrmHwcTwo::HwcLayer *, std::greater<int>> z_map;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500718 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
719 if (l.second.sf_type() == HWC2::Composition::Device)
720 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
721 }
722
723 /*
724 * If more layers then planes, save one plane
725 * for client composited layers
726 */
727 if (avail_planes < layers_.size())
728 avail_planes--;
729
730 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
John Stultz76ca20e2018-07-06 10:34:42 -0700731 if (comp_failed || !avail_planes--)
Rob Herring4f6c62e2018-05-17 14:33:02 -0500732 break;
733 l.second->set_validated_type(HWC2::Composition::Device);
734 }
735
Sean Paulac874152016-03-10 16:00:26 -0500736 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
737 DrmHwcTwo::HwcLayer &layer = l.second;
738 switch (layer.sf_type()) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500739 case HWC2::Composition::Device:
740 if (layer.validated_type() == HWC2::Composition::Device)
741 break;
742 // fall thru
Sean Paulac874152016-03-10 16:00:26 -0500743 case HWC2::Composition::SolidColor:
744 case HWC2::Composition::Cursor:
745 case HWC2::Composition::Sideband:
Rob Herringaf0d9752018-05-04 16:34:19 -0500746 default:
Sean Paulac874152016-03-10 16:00:26 -0500747 layer.set_validated_type(HWC2::Composition::Client);
748 ++*num_types;
749 break;
Sean Paulac874152016-03-10 16:00:26 -0500750 }
751 }
Rob Herringee8f45b2017-06-09 15:15:55 -0500752 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500753}
754
755HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -0500756 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800757 cursor_x_ = x;
758 cursor_y_ = y;
759 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500760}
761
762HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500763 supported(__func__);
764 blending_ = static_cast<HWC2::BlendMode>(mode);
765 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500766}
767
768HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500769 int32_t acquire_fence) {
770 supported(__func__);
771 UniqueFd uf(acquire_fence);
772
773 // The buffer and acquire_fence are handled elsewhere
774 if (sf_type_ == HWC2::Composition::Client ||
775 sf_type_ == HWC2::Composition::Sideband ||
776 sf_type_ == HWC2::Composition::SolidColor)
777 return HWC2::Error::None;
778
779 set_buffer(buffer);
780 set_acquire_fence(uf.get());
781 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500782}
783
784HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Sean Paulac874152016-03-10 16:00:26 -0500785 // TODO: Punt to client composition here?
Sean Pauled2ec4b2016-03-10 15:35:40 -0500786 return unsupported(__func__, color);
787}
788
789HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -0500790 sf_type_ = static_cast<HWC2::Composition>(type);
791 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500792}
793
794HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -0500795 supported(__func__);
796 dataspace_ = static_cast<android_dataspace_t>(dataspace);
797 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500798}
799
800HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -0500801 supported(__func__);
802 display_frame_ = frame;
803 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500804}
805
806HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -0500807 supported(__func__);
808 alpha_ = alpha;
809 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500810}
811
812HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
813 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -0500814 supported(__func__);
815 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -0500816 return unsupported(__func__, stream);
817}
818
819HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -0500820 supported(__func__);
821 source_crop_ = crop;
822 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500823}
824
825HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -0500826 supported(__func__);
827 // TODO: We don't use surface damage, marking as unsupported
828 unsupported(__func__, damage);
829 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500830}
831
832HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -0500833 supported(__func__);
834 transform_ = static_cast<HWC2::Transform>(transform);
835 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500836}
837
838HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -0500839 supported(__func__);
840 // TODO: We don't use this information, marking as unsupported
841 unsupported(__func__, visible);
842 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500843}
844
Sean Paulac874152016-03-10 16:00:26 -0500845HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
846 supported(__func__);
847 z_order_ = order;
848 return HWC2::Error::None;
849}
850
851void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
852 supported(__func__);
853 switch (blending_) {
854 case HWC2::BlendMode::None:
855 layer->blending = DrmHwcBlending::kNone;
856 break;
857 case HWC2::BlendMode::Premultiplied:
858 layer->blending = DrmHwcBlending::kPreMult;
859 break;
860 case HWC2::BlendMode::Coverage:
861 layer->blending = DrmHwcBlending::kCoverage;
862 break;
863 default:
864 ALOGE("Unknown blending mode b=%d", blending_);
865 layer->blending = DrmHwcBlending::kNone;
866 break;
867 }
868
869 OutputFd release_fence = release_fence_output();
870
871 layer->sf_handle = buffer_;
872 layer->acquire_fence = acquire_fence_.Release();
873 layer->release_fence = std::move(release_fence);
874 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +0200875 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -0500876 layer->SetSourceCrop(source_crop_);
877 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -0500878}
879
880// static
881int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
882 unsupported(__func__);
883 return 0;
884}
885
886// static
887void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -0500888 uint32_t *out_count,
889 int32_t * /*out_capabilities*/) {
890 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500891 *out_count = 0;
892}
893
894// static
Sean Paulac874152016-03-10 16:00:26 -0500895hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
896 struct hwc2_device * /*dev*/, int32_t descriptor) {
897 supported(__func__);
898 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500899 switch (func) {
900 // Device functions
901 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
902 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
903 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
904 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
905 int32_t*, hwc2_display_t *>);
906 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
907 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
908 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
909 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
910 case HWC2::FunctionDescriptor::Dump:
911 return ToHook<HWC2_PFN_DUMP>(
912 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
913 uint32_t *, char *>);
914 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
915 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
916 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
917 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
918 case HWC2::FunctionDescriptor::RegisterCallback:
919 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
920 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
921 &DrmHwcTwo::RegisterCallback, int32_t,
922 hwc2_callback_data_t, hwc2_function_pointer_t>);
923
924 // Display functions
925 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
926 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
927 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
928 &HwcDisplay::AcceptDisplayChanges>);
929 case HWC2::FunctionDescriptor::CreateLayer:
930 return ToHook<HWC2_PFN_CREATE_LAYER>(
931 DisplayHook<decltype(&HwcDisplay::CreateLayer),
932 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
933 case HWC2::FunctionDescriptor::DestroyLayer:
934 return ToHook<HWC2_PFN_DESTROY_LAYER>(
935 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
936 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
937 case HWC2::FunctionDescriptor::GetActiveConfig:
938 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
939 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
940 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
941 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
942 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
943 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
944 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
945 hwc2_layer_t *, int32_t *>);
946 case HWC2::FunctionDescriptor::GetClientTargetSupport:
947 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
948 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
949 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
950 int32_t, int32_t>);
951 case HWC2::FunctionDescriptor::GetColorModes:
952 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
953 DisplayHook<decltype(&HwcDisplay::GetColorModes),
954 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
955 case HWC2::FunctionDescriptor::GetDisplayAttribute:
956 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(DisplayHook<
957 decltype(&HwcDisplay::GetDisplayAttribute),
958 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t, int32_t *>);
959 case HWC2::FunctionDescriptor::GetDisplayConfigs:
960 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(DisplayHook<
961 decltype(&HwcDisplay::GetDisplayConfigs),
962 &HwcDisplay::GetDisplayConfigs, uint32_t *, hwc2_config_t *>);
963 case HWC2::FunctionDescriptor::GetDisplayName:
964 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
965 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
966 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
967 case HWC2::FunctionDescriptor::GetDisplayRequests:
968 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
969 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
970 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
971 hwc2_layer_t *, int32_t *>);
972 case HWC2::FunctionDescriptor::GetDisplayType:
973 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
974 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
975 &HwcDisplay::GetDisplayType, int32_t *>);
976 case HWC2::FunctionDescriptor::GetDozeSupport:
977 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
978 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
979 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -0500980 case HWC2::FunctionDescriptor::GetHdrCapabilities:
981 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
982 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
983 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
984 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500985 case HWC2::FunctionDescriptor::GetReleaseFences:
986 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
987 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
988 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
989 int32_t *>);
990 case HWC2::FunctionDescriptor::PresentDisplay:
991 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
992 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
993 &HwcDisplay::PresentDisplay, int32_t *>);
994 case HWC2::FunctionDescriptor::SetActiveConfig:
995 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
996 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
997 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
998 case HWC2::FunctionDescriptor::SetClientTarget:
999 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(DisplayHook<
1000 decltype(&HwcDisplay::SetClientTarget), &HwcDisplay::SetClientTarget,
1001 buffer_handle_t, int32_t, int32_t, hwc_region_t>);
1002 case HWC2::FunctionDescriptor::SetColorMode:
1003 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1004 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1005 &HwcDisplay::SetColorMode, int32_t>);
1006 case HWC2::FunctionDescriptor::SetColorTransform:
1007 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1008 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1009 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1010 case HWC2::FunctionDescriptor::SetOutputBuffer:
1011 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1012 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1013 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1014 case HWC2::FunctionDescriptor::SetPowerMode:
1015 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1016 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1017 &HwcDisplay::SetPowerMode, int32_t>);
1018 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1019 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1020 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1021 &HwcDisplay::SetVsyncEnabled, int32_t>);
1022 case HWC2::FunctionDescriptor::ValidateDisplay:
1023 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1024 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1025 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
1026
1027 // Layer functions
1028 case HWC2::FunctionDescriptor::SetCursorPosition:
1029 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1030 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1031 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1032 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1033 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1034 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1035 &HwcLayer::SetLayerBlendMode, int32_t>);
1036 case HWC2::FunctionDescriptor::SetLayerBuffer:
1037 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1038 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1039 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1040 case HWC2::FunctionDescriptor::SetLayerColor:
1041 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1042 LayerHook<decltype(&HwcLayer::SetLayerColor),
1043 &HwcLayer::SetLayerColor, hwc_color_t>);
1044 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1045 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1046 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1047 &HwcLayer::SetLayerCompositionType, int32_t>);
1048 case HWC2::FunctionDescriptor::SetLayerDataspace:
1049 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1050 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1051 &HwcLayer::SetLayerDataspace, int32_t>);
1052 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1053 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1054 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1055 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1056 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1057 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1058 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1059 &HwcLayer::SetLayerPlaneAlpha, float>);
1060 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
1061 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(LayerHook<
1062 decltype(&HwcLayer::SetLayerSidebandStream),
1063 &HwcLayer::SetLayerSidebandStream, const native_handle_t *>);
1064 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1065 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1066 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1067 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1068 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1069 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1070 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1071 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1072 case HWC2::FunctionDescriptor::SetLayerTransform:
1073 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1074 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1075 &HwcLayer::SetLayerTransform, int32_t>);
1076 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1077 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1078 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1079 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1080 case HWC2::FunctionDescriptor::SetLayerZOrder:
1081 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1082 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1083 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001084 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001085 default:
1086 return NULL;
1087 }
1088}
Sean Paulac874152016-03-10 16:00:26 -05001089
1090// static
1091int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1092 struct hw_device_t **dev) {
1093 supported(__func__);
1094 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1095 ALOGE("Invalid module name- %s", name);
1096 return -EINVAL;
1097 }
1098
1099 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1100 if (!ctx) {
1101 ALOGE("Failed to allocate DrmHwcTwo");
1102 return -ENOMEM;
1103 }
1104
1105 HWC2::Error err = ctx->Init();
1106 if (err != HWC2::Error::None) {
1107 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1108 return -EINVAL;
1109 }
1110
1111 ctx->common.module = const_cast<hw_module_t *>(module);
1112 *dev = &ctx->common;
1113 ctx.release();
1114 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001115}
Sean Paulac874152016-03-10 16:00:26 -05001116}
1117
1118static struct hw_module_methods_t hwc2_module_methods = {
1119 .open = android::DrmHwcTwo::HookDevOpen,
1120};
1121
1122hw_module_t HAL_MODULE_INFO_SYM = {
1123 .tag = HARDWARE_MODULE_TAG,
1124 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1125 .id = HWC_HARDWARE_MODULE_ID,
1126 .name = "DrmHwcTwo module",
1127 .author = "The Android Open Source Project",
1128 .methods = &hwc2_module_methods,
1129 .dso = NULL,
1130 .reserved = {0},
1131};