blob: cbe6df78471d7cda3fb8b7461b6594a68647c6fb [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() {
61 int ret = drm_.Init();
62 if (ret) {
63 ALOGE("Can't initialize drm object %d", ret);
64 return HWC2::Error::NoResources;
65 }
66
67 importer_.reset(Importer::CreateInstance(&drm_));
68 if (!importer_) {
69 ALOGE("Failed to create importer instance");
70 return HWC2::Error::NoResources;
71 }
72
Sean Paulac874152016-03-10 16:00:26 -050073 displays_.emplace(std::piecewise_construct,
74 std::forward_as_tuple(HWC_DISPLAY_PRIMARY),
Andrii Chepurnyi1d224e82018-05-17 18:34:01 +030075 std::forward_as_tuple(&drm_, importer_, HWC_DISPLAY_PRIMARY,
Sean Paulac874152016-03-10 16:00:26 -050076 HWC2::DisplayType::Physical));
77
78 DrmCrtc *crtc = drm_.GetCrtcForDisplay(static_cast<int>(HWC_DISPLAY_PRIMARY));
79 if (!crtc) {
80 ALOGE("Failed to get crtc for display %d",
81 static_cast<int>(HWC_DISPLAY_PRIMARY));
82 return HWC2::Error::BadDisplay;
83 }
84
85 std::vector<DrmPlane *> display_planes;
86 for (auto &plane : drm_.planes()) {
87 if (plane->GetCrtcSupported(*crtc))
88 display_planes.push_back(plane.get());
89 }
90 displays_.at(HWC_DISPLAY_PRIMARY).Init(&display_planes);
91 return HWC2::Error::None;
92}
93
Sean Pauled2ec4b2016-03-10 15:35:40 -050094template <typename... Args>
95static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
96 ALOGV("Unsupported function: %s", func);
97 return HWC2::Error::Unsupported;
98}
99
Sean Paulac874152016-03-10 16:00:26 -0500100static inline void supported(char const *func) {
101 ALOGV("Supported function: %s", func);
102}
103
Sean Pauled2ec4b2016-03-10 15:35:40 -0500104HWC2::Error DrmHwcTwo::CreateVirtualDisplay(uint32_t width, uint32_t height,
105 int32_t *format,
106 hwc2_display_t *display) {
107 // TODO: Implement virtual display
Sean Paulac874152016-03-10 16:00:26 -0500108 return unsupported(__func__, width, height, format, display);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500109}
110
111HWC2::Error DrmHwcTwo::DestroyVirtualDisplay(hwc2_display_t display) {
Sean Paulac874152016-03-10 16:00:26 -0500112 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500113 return unsupported(__func__, display);
114}
115
116void DrmHwcTwo::Dump(uint32_t *size, char *buffer) {
Sean Paulac874152016-03-10 16:00:26 -0500117 // TODO: Implement dump
Sean Pauled2ec4b2016-03-10 15:35:40 -0500118 unsupported(__func__, size, buffer);
119}
120
121uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() {
Sean Paulac874152016-03-10 16:00:26 -0500122 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500123 unsupported(__func__);
124 return 0;
125}
126
127HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor,
Sean Paulac874152016-03-10 16:00:26 -0500128 hwc2_callback_data_t data,
129 hwc2_function_pointer_t function) {
130 supported(__func__);
131 auto callback = static_cast<HWC2::Callback>(descriptor);
132 callbacks_.emplace(callback, HwcCallback(data, function));
133
134 switch (callback) {
135 case HWC2::Callback::Hotplug: {
136 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(function);
137 hotplug(data, HWC_DISPLAY_PRIMARY,
138 static_cast<int32_t>(HWC2::Connection::Connected));
139 break;
140 }
141 case HWC2::Callback::Vsync: {
142 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
143 displays_)
144 d.second.RegisterVsyncCallback(data, function);
145 break;
146 }
147 default:
148 break;
149 }
150 return HWC2::Error::None;
151}
152
153DrmHwcTwo::HwcDisplay::HwcDisplay(DrmResources *drm,
154 std::shared_ptr<Importer> importer,
Sean Paulac874152016-03-10 16:00:26 -0500155 hwc2_display_t handle, HWC2::DisplayType type)
Andrii Chepurnyi1d224e82018-05-17 18:34:01 +0300156 : drm_(drm), importer_(importer), handle_(handle), type_(type) {
Sean Paulac874152016-03-10 16:00:26 -0500157 supported(__func__);
158}
159
160HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
161 supported(__func__);
162 planner_ = Planner::CreateInstance(drm_);
163 if (!planner_) {
164 ALOGE("Failed to create planner instance for composition");
165 return HWC2::Error::NoResources;
166 }
167
168 int display = static_cast<int>(handle_);
169 int ret = compositor_.Init(drm_, display);
170 if (ret) {
171 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
172 return HWC2::Error::NoResources;
173 }
174
175 // Split up the given display planes into primary and overlay to properly
176 // interface with the composition
177 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
178 property_get("hwc.drm.use_overlay_planes", use_overlay_planes_prop, "1");
179 bool use_overlay_planes = atoi(use_overlay_planes_prop);
180 for (auto &plane : *planes) {
181 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
182 primary_planes_.push_back(plane);
183 else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY)
184 overlay_planes_.push_back(plane);
185 }
186
187 crtc_ = drm_->GetCrtcForDisplay(display);
188 if (!crtc_) {
189 ALOGE("Failed to get crtc for display %d", display);
190 return HWC2::Error::BadDisplay;
191 }
192
193 connector_ = drm_->GetConnectorForDisplay(display);
194 if (!connector_) {
195 ALOGE("Failed to get connector for display %d", display);
196 return HWC2::Error::BadDisplay;
197 }
198
199 // Fetch the number of modes from the display
200 uint32_t num_configs;
201 HWC2::Error err = GetDisplayConfigs(&num_configs, NULL);
202 if (err != HWC2::Error::None || !num_configs)
203 return err;
204
205 // Grab the first mode, we'll choose this as the active mode
206 // TODO: Should choose the preferred mode here
207 hwc2_config_t default_config;
208 num_configs = 1;
209 err = GetDisplayConfigs(&num_configs, &default_config);
210 if (err != HWC2::Error::None)
211 return err;
212
213 ret = vsync_worker_.Init(drm_, display);
214 if (ret) {
215 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
216 return HWC2::Error::BadDisplay;
217 }
218
219 return SetActiveConfig(default_config);
220}
221
222HWC2::Error DrmHwcTwo::HwcDisplay::RegisterVsyncCallback(
223 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
224 supported(__func__);
225 auto callback = std::make_shared<DrmVsyncCallback>(data, func);
Adrian Salidofa37f672017-02-16 10:29:46 -0800226 vsync_worker_.RegisterCallback(std::move(callback));
Sean Paulac874152016-03-10 16:00:26 -0500227 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500228}
229
230HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500231 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500232 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
233 l.second.accept_type_change();
234 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500235}
236
237HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500238 supported(__func__);
239 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
240 *layer = static_cast<hwc2_layer_t>(layer_idx_);
241 ++layer_idx_;
242 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500243}
244
245HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Sean Paulac874152016-03-10 16:00:26 -0500246 supported(__func__);
247 layers_.erase(layer);
248 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500249}
250
251HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
Sean Paulac874152016-03-10 16:00:26 -0500252 supported(__func__);
253 DrmMode const &mode = connector_->active_mode();
254 if (mode.id() == 0)
255 return HWC2::Error::BadConfig;
256
257 *config = mode.id();
258 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500259}
260
261HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
262 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500263 supported(__func__);
264 uint32_t num_changes = 0;
265 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
266 if (l.second.type_changed()) {
267 if (layers && num_changes < *num_elements)
268 layers[num_changes] = l.first;
269 if (types && num_changes < *num_elements)
270 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
271 ++num_changes;
272 }
273 }
274 if (!layers && !types)
275 *num_elements = num_changes;
276 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500277}
278
279HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500280 uint32_t height,
281 int32_t /*format*/,
282 int32_t dataspace) {
283 supported(__func__);
284 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
285 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
286
287 if (width < min.first || height < min.second)
288 return HWC2::Error::Unsupported;
289
290 if (width > max.first || height > max.second)
291 return HWC2::Error::Unsupported;
292
293 if (dataspace != HAL_DATASPACE_UNKNOWN &&
294 dataspace != HAL_DATASPACE_STANDARD_UNSPECIFIED)
295 return HWC2::Error::Unsupported;
296
297 // TODO: Validate format can be handled by either GL or planes
298 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500299}
300
301HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500302 int32_t *modes) {
303 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800304 if (!modes)
305 *num_modes = 1;
306
307 if (modes)
308 *modes = HAL_COLOR_MODE_NATIVE;
309
310 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500311}
312
313HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500314 int32_t attribute_in,
315 int32_t *value) {
316 supported(__func__);
317 auto mode =
318 std::find_if(connector_->modes().begin(), connector_->modes().end(),
319 [config](DrmMode const &m) { return m.id() == config; });
320 if (mode == connector_->modes().end()) {
321 ALOGE("Could not find active mode for %d", config);
322 return HWC2::Error::BadConfig;
323 }
324
325 static const int32_t kUmPerInch = 25400;
326 uint32_t mm_width = connector_->mm_width();
327 uint32_t mm_height = connector_->mm_height();
328 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
329 switch (attribute) {
330 case HWC2::Attribute::Width:
331 *value = mode->h_display();
332 break;
333 case HWC2::Attribute::Height:
334 *value = mode->v_display();
335 break;
336 case HWC2::Attribute::VsyncPeriod:
337 // in nanoseconds
338 *value = 1000 * 1000 * 1000 / mode->v_refresh();
339 break;
340 case HWC2::Attribute::DpiX:
341 // Dots per 1000 inches
342 *value = mm_width ? (mode->h_display() * kUmPerInch) / mm_width : -1;
343 break;
344 case HWC2::Attribute::DpiY:
345 // Dots per 1000 inches
346 *value = mm_height ? (mode->v_display() * kUmPerInch) / mm_height : -1;
347 break;
348 default:
349 *value = -1;
350 return HWC2::Error::BadConfig;
351 }
352 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500353}
354
355HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
356 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500357 supported(__func__);
358 // Since this callback is normally invoked twice (once to get the count, and
359 // once to populate configs), we don't really want to read the edid
360 // redundantly. Instead, only update the modes on the first invocation. While
361 // it's possible this will result in stale modes, it'll all come out in the
362 // wash when we try to set the active config later.
363 if (!configs) {
364 int ret = connector_->UpdateModes();
365 if (ret) {
366 ALOGE("Failed to update display modes %d", ret);
367 return HWC2::Error::BadDisplay;
368 }
369 }
370
371 auto num_modes = static_cast<uint32_t>(connector_->modes().size());
372 if (!configs) {
373 *num_configs = num_modes;
374 return HWC2::Error::None;
375 }
376
377 uint32_t idx = 0;
378 for (const DrmMode &mode : connector_->modes()) {
379 if (idx >= *num_configs)
380 break;
381 configs[idx++] = mode.id();
382 }
383 *num_configs = idx;
384 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500385}
386
387HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500388 supported(__func__);
389 std::ostringstream stream;
390 stream << "display-" << connector_->id();
391 std::string string = stream.str();
392 size_t length = string.length();
393 if (!name) {
394 *size = length;
395 return HWC2::Error::None;
396 }
397
398 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
399 strncpy(name, string.c_str(), *size);
400 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500401}
402
Sean Paulac874152016-03-10 16:00:26 -0500403HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
404 uint32_t *num_elements,
405 hwc2_layer_t *layers,
406 int32_t *layer_requests) {
407 supported(__func__);
408 // TODO: I think virtual display should request
409 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
410 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
411 *num_elements = 0;
412 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500413}
414
415HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500416 supported(__func__);
417 *type = static_cast<int32_t>(type_);
418 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500419}
420
421HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500422 supported(__func__);
423 *support = 0;
424 return HWC2::Error::None;
425}
426
427HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
428 uint32_t *num_types, int32_t */*types*/, float */*max_luminance*/,
429 float */*max_average_luminance*/, float */*min_luminance*/) {
430 supported(__func__);
431 *num_types = 0;
432 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500433}
434
435HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500436 hwc2_layer_t *layers,
437 int32_t *fences) {
438 supported(__func__);
439 uint32_t num_layers = 0;
440
441 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
442 ++num_layers;
443 if (layers == NULL || fences == NULL) {
444 continue;
445 } else if (num_layers > *num_elements) {
446 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
447 return HWC2::Error::None;
448 }
449
450 layers[num_layers - 1] = l.first;
451 fences[num_layers - 1] = l.second.take_release_fence();
452 }
453 *num_elements = num_layers;
454 return HWC2::Error::None;
455}
456
457void DrmHwcTwo::HwcDisplay::AddFenceToRetireFence(int fd) {
458 supported(__func__);
459 if (fd < 0)
460 return;
461
462 if (next_retire_fence_.get() >= 0) {
463 int old_fence = next_retire_fence_.get();
464 next_retire_fence_.Set(sync_merge("dc_retire", old_fence, fd));
465 } else {
466 next_retire_fence_.Set(dup(fd));
467 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500468}
469
Rob Herring4f6c62e2018-05-17 14:33:02 -0500470HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500471 std::vector<DrmCompositionDisplayLayersMap> layers_map;
472 layers_map.emplace_back();
473 DrmCompositionDisplayLayersMap &map = layers_map.back();
474
475 map.display = static_cast<int>(handle_);
476 map.geometry_changed = true; // TODO: Fix this
477
478 // order the layers by z-order
479 bool use_client_layer = false;
480 uint32_t client_z_order = 0;
481 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
482 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500483 HWC2::Composition comp_type;
484 if (test)
485 comp_type = l.second.sf_type();
486 else
487 comp_type = l.second.validated_type();
488
489 switch (comp_type) {
Sean Paulac874152016-03-10 16:00:26 -0500490 case HWC2::Composition::Device:
491 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
492 break;
493 case HWC2::Composition::Client:
494 // Place it at the z_order of the highest client layer
495 use_client_layer = true;
496 client_z_order = std::max(client_z_order, l.second.z_order());
497 break;
498 default:
499 continue;
500 }
501 }
502 if (use_client_layer)
503 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
504
Rob Herring4f6c62e2018-05-17 14:33:02 -0500505 if (z_map.empty())
506 return HWC2::Error::BadLayer;
507
Sean Paulac874152016-03-10 16:00:26 -0500508 // now that they're ordered by z, add them to the composition
509 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
510 DrmHwcLayer layer;
511 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200512 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500513 if (ret) {
514 ALOGE("Failed to import layer, ret=%d", ret);
515 return HWC2::Error::NoResources;
516 }
517 map.layers.emplace_back(std::move(layer));
518 }
Sean Paulac874152016-03-10 16:00:26 -0500519
520 std::unique_ptr<DrmDisplayComposition> composition =
521 compositor_.CreateComposition();
522 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
523
524 // TODO: Don't always assume geometry changed
525 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
526 if (ret) {
527 ALOGE("Failed to set layers in the composition ret=%d", ret);
528 return HWC2::Error::BadLayer;
529 }
530
531 std::vector<DrmPlane *> primary_planes(primary_planes_);
532 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500533 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500534 if (ret) {
535 ALOGE("Failed to plan the composition ret=%d", ret);
536 return HWC2::Error::BadConfig;
537 }
538
539 // Disable the planes we're not using
540 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
541 composition->AddPlaneDisable(*i);
542 i = primary_planes.erase(i);
543 }
544 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
545 composition->AddPlaneDisable(*i);
546 i = overlay_planes.erase(i);
547 }
548
Rob Herring4f6c62e2018-05-17 14:33:02 -0500549 if (test) {
550 ret = compositor_.TestComposition(composition.get());
551 } else {
552 AddFenceToRetireFence(composition->take_out_fence());
553 ret = compositor_.ApplyComposition(std::move(composition));
554 }
Sean Paulac874152016-03-10 16:00:26 -0500555 if (ret) {
556 ALOGE("Failed to apply the frame composition ret=%d", ret);
557 return HWC2::Error::BadParameter;
558 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500559 return HWC2::Error::None;
560}
561
562HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *retire_fence) {
563 supported(__func__);
564 HWC2::Error ret;
565
566 ret = CreateComposition(false);
567 if (ret == HWC2::Error::BadLayer) {
568 // Can we really have no client or device layers?
569 *retire_fence = -1;
570 return HWC2::Error::None;
571 }
572 if (ret != HWC2::Error::None)
573 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500574
Sean Paulac874152016-03-10 16:00:26 -0500575 // The retire fence returned here is for the last frame, so return it and
576 // promote the next retire fence
577 *retire_fence = retire_fence_.Release();
578 retire_fence_ = std::move(next_retire_fence_);
579
580 ++frame_no_;
581 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500582}
583
584HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500585 supported(__func__);
586 auto mode =
587 std::find_if(connector_->modes().begin(), connector_->modes().end(),
588 [config](DrmMode const &m) { return m.id() == config; });
589 if (mode == connector_->modes().end()) {
590 ALOGE("Could not find active mode for %d", config);
591 return HWC2::Error::BadConfig;
592 }
593
594 std::unique_ptr<DrmDisplayComposition> composition =
595 compositor_.CreateComposition();
596 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
597 int ret = composition->SetDisplayMode(*mode);
Sean Pauled45a8e2017-02-28 13:17:34 -0500598 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500599 if (ret) {
600 ALOGE("Failed to queue dpms composition on %d", ret);
601 return HWC2::Error::BadConfig;
602 }
603 if (connector_->active_mode().id() == 0)
604 connector_->set_active_mode(*mode);
605
606 // Setup the client layer's dimensions
607 hwc_rect_t display_frame = {.left = 0,
608 .top = 0,
609 .right = static_cast<int>(mode->h_display()),
610 .bottom = static_cast<int>(mode->v_display())};
611 client_layer_.SetLayerDisplayFrame(display_frame);
612 hwc_frect_t source_crop = {.left = 0.0f,
613 .top = 0.0f,
614 .right = mode->h_display() + 0.0f,
615 .bottom = mode->v_display() + 0.0f};
616 client_layer_.SetLayerSourceCrop(source_crop);
617
618 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500619}
620
621HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
622 int32_t acquire_fence,
623 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600624 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500625 supported(__func__);
626 UniqueFd uf(acquire_fence);
627
628 client_layer_.set_buffer(target);
629 client_layer_.set_acquire_fence(uf.get());
630 client_layer_.SetLayerDataspace(dataspace);
631 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500632}
633
634HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500635 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800636
637 if (mode != HAL_COLOR_MODE_NATIVE)
638 return HWC2::Error::Unsupported;
639
640 color_mode_ = mode;
641 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500642}
643
644HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500645 int32_t hint) {
646 supported(__func__);
647 // TODO: Force client composition if we get this
Sean Pauled2ec4b2016-03-10 15:35:40 -0500648 return unsupported(__func__, matrix, hint);
649}
650
651HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500652 int32_t release_fence) {
653 supported(__func__);
654 // TODO: Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500655 return unsupported(__func__, buffer, release_fence);
656}
657
Sean Paulac874152016-03-10 16:00:26 -0500658HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
659 supported(__func__);
660 uint64_t dpms_value = 0;
661 auto mode = static_cast<HWC2::PowerMode>(mode_in);
662 switch (mode) {
663 case HWC2::PowerMode::Off:
664 dpms_value = DRM_MODE_DPMS_OFF;
665 break;
666 case HWC2::PowerMode::On:
667 dpms_value = DRM_MODE_DPMS_ON;
668 break;
669 default:
670 ALOGI("Power mode %d is unsupported\n", mode);
671 return HWC2::Error::Unsupported;
672 };
673
674 std::unique_ptr<DrmDisplayComposition> composition =
675 compositor_.CreateComposition();
676 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
677 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500678 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500679 if (ret) {
680 ALOGE("Failed to apply the dpms composition ret=%d", ret);
681 return HWC2::Error::BadParameter;
682 }
683 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500684}
685
686HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500687 supported(__func__);
688 vsync_worker_.VSyncControl(enabled);
689 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500690}
691
692HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500693 uint32_t *num_requests) {
694 supported(__func__);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500695 size_t plane_count = 0;
Sean Paulac874152016-03-10 16:00:26 -0500696 *num_types = 0;
697 *num_requests = 0;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500698 size_t avail_planes = primary_planes_.size() + overlay_planes_.size();
699
700 HWC2::Error ret;
701
702 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
703 l.second.set_validated_type(HWC2::Composition::Invalid);
704
705 ret = CreateComposition(true);
706 if (ret != HWC2::Error::None)
707 // Assume the test failed due to overlay planes
708 avail_planes = 1;
709
710 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
711 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
712 if (l.second.sf_type() == HWC2::Composition::Device)
713 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
714 }
715
716 /*
717 * If more layers then planes, save one plane
718 * for client composited layers
719 */
720 if (avail_planes < layers_.size())
721 avail_planes--;
722
723 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
724 if (!avail_planes--)
725 break;
726 l.second->set_validated_type(HWC2::Composition::Device);
727 }
728
Sean Paulac874152016-03-10 16:00:26 -0500729 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
730 DrmHwcTwo::HwcLayer &layer = l.second;
731 switch (layer.sf_type()) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500732 case HWC2::Composition::Device:
733 if (layer.validated_type() == HWC2::Composition::Device)
734 break;
735 // fall thru
Sean Paulac874152016-03-10 16:00:26 -0500736 case HWC2::Composition::SolidColor:
737 case HWC2::Composition::Cursor:
738 case HWC2::Composition::Sideband:
Rob Herringaf0d9752018-05-04 16:34:19 -0500739 default:
Sean Paulac874152016-03-10 16:00:26 -0500740 layer.set_validated_type(HWC2::Composition::Client);
741 ++*num_types;
742 break;
Sean Paulac874152016-03-10 16:00:26 -0500743 }
744 }
Rob Herringee8f45b2017-06-09 15:15:55 -0500745 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500746}
747
748HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -0500749 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800750 cursor_x_ = x;
751 cursor_y_ = y;
752 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500753}
754
755HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500756 supported(__func__);
757 blending_ = static_cast<HWC2::BlendMode>(mode);
758 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500759}
760
761HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500762 int32_t acquire_fence) {
763 supported(__func__);
764 UniqueFd uf(acquire_fence);
765
766 // The buffer and acquire_fence are handled elsewhere
767 if (sf_type_ == HWC2::Composition::Client ||
768 sf_type_ == HWC2::Composition::Sideband ||
769 sf_type_ == HWC2::Composition::SolidColor)
770 return HWC2::Error::None;
771
772 set_buffer(buffer);
773 set_acquire_fence(uf.get());
774 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500775}
776
777HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Sean Paulac874152016-03-10 16:00:26 -0500778 // TODO: Punt to client composition here?
Sean Pauled2ec4b2016-03-10 15:35:40 -0500779 return unsupported(__func__, color);
780}
781
782HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -0500783 sf_type_ = static_cast<HWC2::Composition>(type);
784 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500785}
786
787HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -0500788 supported(__func__);
789 dataspace_ = static_cast<android_dataspace_t>(dataspace);
790 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500791}
792
793HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -0500794 supported(__func__);
795 display_frame_ = frame;
796 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500797}
798
799HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -0500800 supported(__func__);
801 alpha_ = alpha;
802 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500803}
804
805HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
806 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -0500807 supported(__func__);
808 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -0500809 return unsupported(__func__, stream);
810}
811
812HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -0500813 supported(__func__);
814 source_crop_ = crop;
815 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500816}
817
818HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -0500819 supported(__func__);
820 // TODO: We don't use surface damage, marking as unsupported
821 unsupported(__func__, damage);
822 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500823}
824
825HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -0500826 supported(__func__);
827 transform_ = static_cast<HWC2::Transform>(transform);
828 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500829}
830
831HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -0500832 supported(__func__);
833 // TODO: We don't use this information, marking as unsupported
834 unsupported(__func__, visible);
835 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500836}
837
Sean Paulac874152016-03-10 16:00:26 -0500838HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
839 supported(__func__);
840 z_order_ = order;
841 return HWC2::Error::None;
842}
843
844void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
845 supported(__func__);
846 switch (blending_) {
847 case HWC2::BlendMode::None:
848 layer->blending = DrmHwcBlending::kNone;
849 break;
850 case HWC2::BlendMode::Premultiplied:
851 layer->blending = DrmHwcBlending::kPreMult;
852 break;
853 case HWC2::BlendMode::Coverage:
854 layer->blending = DrmHwcBlending::kCoverage;
855 break;
856 default:
857 ALOGE("Unknown blending mode b=%d", blending_);
858 layer->blending = DrmHwcBlending::kNone;
859 break;
860 }
861
862 OutputFd release_fence = release_fence_output();
863
864 layer->sf_handle = buffer_;
865 layer->acquire_fence = acquire_fence_.Release();
866 layer->release_fence = std::move(release_fence);
867 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +0200868 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -0500869 layer->SetSourceCrop(source_crop_);
870 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -0500871}
872
873// static
874int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
875 unsupported(__func__);
876 return 0;
877}
878
879// static
880void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -0500881 uint32_t *out_count,
882 int32_t * /*out_capabilities*/) {
883 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500884 *out_count = 0;
885}
886
887// static
Sean Paulac874152016-03-10 16:00:26 -0500888hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
889 struct hwc2_device * /*dev*/, int32_t descriptor) {
890 supported(__func__);
891 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500892 switch (func) {
893 // Device functions
894 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
895 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
896 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
897 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
898 int32_t*, hwc2_display_t *>);
899 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
900 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
901 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
902 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
903 case HWC2::FunctionDescriptor::Dump:
904 return ToHook<HWC2_PFN_DUMP>(
905 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
906 uint32_t *, char *>);
907 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
908 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
909 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
910 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
911 case HWC2::FunctionDescriptor::RegisterCallback:
912 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
913 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
914 &DrmHwcTwo::RegisterCallback, int32_t,
915 hwc2_callback_data_t, hwc2_function_pointer_t>);
916
917 // Display functions
918 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
919 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
920 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
921 &HwcDisplay::AcceptDisplayChanges>);
922 case HWC2::FunctionDescriptor::CreateLayer:
923 return ToHook<HWC2_PFN_CREATE_LAYER>(
924 DisplayHook<decltype(&HwcDisplay::CreateLayer),
925 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
926 case HWC2::FunctionDescriptor::DestroyLayer:
927 return ToHook<HWC2_PFN_DESTROY_LAYER>(
928 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
929 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
930 case HWC2::FunctionDescriptor::GetActiveConfig:
931 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
932 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
933 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
934 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
935 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
936 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
937 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
938 hwc2_layer_t *, int32_t *>);
939 case HWC2::FunctionDescriptor::GetClientTargetSupport:
940 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
941 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
942 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
943 int32_t, int32_t>);
944 case HWC2::FunctionDescriptor::GetColorModes:
945 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
946 DisplayHook<decltype(&HwcDisplay::GetColorModes),
947 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
948 case HWC2::FunctionDescriptor::GetDisplayAttribute:
949 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(DisplayHook<
950 decltype(&HwcDisplay::GetDisplayAttribute),
951 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t, int32_t *>);
952 case HWC2::FunctionDescriptor::GetDisplayConfigs:
953 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(DisplayHook<
954 decltype(&HwcDisplay::GetDisplayConfigs),
955 &HwcDisplay::GetDisplayConfigs, uint32_t *, hwc2_config_t *>);
956 case HWC2::FunctionDescriptor::GetDisplayName:
957 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
958 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
959 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
960 case HWC2::FunctionDescriptor::GetDisplayRequests:
961 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
962 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
963 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
964 hwc2_layer_t *, int32_t *>);
965 case HWC2::FunctionDescriptor::GetDisplayType:
966 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
967 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
968 &HwcDisplay::GetDisplayType, int32_t *>);
969 case HWC2::FunctionDescriptor::GetDozeSupport:
970 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
971 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
972 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -0500973 case HWC2::FunctionDescriptor::GetHdrCapabilities:
974 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
975 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
976 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
977 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500978 case HWC2::FunctionDescriptor::GetReleaseFences:
979 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
980 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
981 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
982 int32_t *>);
983 case HWC2::FunctionDescriptor::PresentDisplay:
984 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
985 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
986 &HwcDisplay::PresentDisplay, int32_t *>);
987 case HWC2::FunctionDescriptor::SetActiveConfig:
988 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
989 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
990 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
991 case HWC2::FunctionDescriptor::SetClientTarget:
992 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(DisplayHook<
993 decltype(&HwcDisplay::SetClientTarget), &HwcDisplay::SetClientTarget,
994 buffer_handle_t, int32_t, int32_t, hwc_region_t>);
995 case HWC2::FunctionDescriptor::SetColorMode:
996 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
997 DisplayHook<decltype(&HwcDisplay::SetColorMode),
998 &HwcDisplay::SetColorMode, int32_t>);
999 case HWC2::FunctionDescriptor::SetColorTransform:
1000 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1001 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1002 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1003 case HWC2::FunctionDescriptor::SetOutputBuffer:
1004 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1005 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1006 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1007 case HWC2::FunctionDescriptor::SetPowerMode:
1008 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1009 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1010 &HwcDisplay::SetPowerMode, int32_t>);
1011 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1012 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1013 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1014 &HwcDisplay::SetVsyncEnabled, int32_t>);
1015 case HWC2::FunctionDescriptor::ValidateDisplay:
1016 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1017 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1018 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
1019
1020 // Layer functions
1021 case HWC2::FunctionDescriptor::SetCursorPosition:
1022 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1023 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1024 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1025 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1026 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1027 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1028 &HwcLayer::SetLayerBlendMode, int32_t>);
1029 case HWC2::FunctionDescriptor::SetLayerBuffer:
1030 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1031 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1032 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1033 case HWC2::FunctionDescriptor::SetLayerColor:
1034 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1035 LayerHook<decltype(&HwcLayer::SetLayerColor),
1036 &HwcLayer::SetLayerColor, hwc_color_t>);
1037 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1038 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1039 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1040 &HwcLayer::SetLayerCompositionType, int32_t>);
1041 case HWC2::FunctionDescriptor::SetLayerDataspace:
1042 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1043 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1044 &HwcLayer::SetLayerDataspace, int32_t>);
1045 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1046 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1047 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1048 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1049 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1050 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1051 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1052 &HwcLayer::SetLayerPlaneAlpha, float>);
1053 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
1054 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(LayerHook<
1055 decltype(&HwcLayer::SetLayerSidebandStream),
1056 &HwcLayer::SetLayerSidebandStream, const native_handle_t *>);
1057 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1058 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1059 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1060 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1061 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1062 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1063 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1064 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1065 case HWC2::FunctionDescriptor::SetLayerTransform:
1066 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1067 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1068 &HwcLayer::SetLayerTransform, int32_t>);
1069 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1070 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1071 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1072 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1073 case HWC2::FunctionDescriptor::SetLayerZOrder:
1074 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1075 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1076 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001077 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001078 default:
1079 return NULL;
1080 }
1081}
Sean Paulac874152016-03-10 16:00:26 -05001082
1083// static
1084int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1085 struct hw_device_t **dev) {
1086 supported(__func__);
1087 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1088 ALOGE("Invalid module name- %s", name);
1089 return -EINVAL;
1090 }
1091
1092 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1093 if (!ctx) {
1094 ALOGE("Failed to allocate DrmHwcTwo");
1095 return -ENOMEM;
1096 }
1097
1098 HWC2::Error err = ctx->Init();
1099 if (err != HWC2::Error::None) {
1100 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1101 return -EINVAL;
1102 }
1103
1104 ctx->common.module = const_cast<hw_module_t *>(module);
1105 *dev = &ctx->common;
1106 ctx.release();
1107 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001108}
Sean Paulac874152016-03-10 16:00:26 -05001109}
1110
1111static struct hw_module_methods_t hwc2_module_methods = {
1112 .open = android::DrmHwcTwo::HookDevOpen,
1113};
1114
1115hw_module_t HAL_MODULE_INFO_SYM = {
1116 .tag = HARDWARE_MODULE_TAG,
1117 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1118 .id = HWC_HARDWARE_MODULE_ID,
1119 .name = "DrmHwcTwo module",
1120 .author = "The Android Open Source Project",
1121 .methods = &hwc2_module_methods,
1122 .dso = NULL,
1123 .reserved = {0},
1124};