blob: 81bb96d22376a3e55dff489fec7f6115c830bca9 [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
Roman Stratiienko13cc3662020-08-29 21:35:39 +030020#include "DrmHwcTwo.h"
Sean Pauled2ec4b2016-03-10 15:35:40 -050021
Sean Paulac874152016-03-10 16:00:26 -050022#include <cutils/properties.h>
23#include <hardware/hardware.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050024#include <hardware/hwcomposer2.h>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030025#include <inttypes.h>
Sean Paulf72cccd2018-08-27 13:59:08 -040026#include <log/log.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050027
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030028#include <string>
29
Roman Stratiienko13cc3662020-08-29 21:35:39 +030030#include "backend/BackendManager.h"
31#include "compositor/DrmDisplayComposition.h"
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030032
Sean Pauled2ec4b2016-03-10 15:35:40 -050033namespace android {
34
35DrmHwcTwo::DrmHwcTwo() {
Sean Paulac874152016-03-10 16:00:26 -050036 common.tag = HARDWARE_DEVICE_TAG;
37 common.version = HWC_DEVICE_API_VERSION_2_0;
Sean Pauled2ec4b2016-03-10 15:35:40 -050038 common.close = HookDevClose;
39 getCapabilities = HookDevGetCapabilities;
40 getFunction = HookDevGetFunction;
41}
42
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030043HWC2::Error DrmHwcTwo::CreateDisplay(hwc2_display_t displ,
44 HWC2::DisplayType type) {
45 DrmDevice *drm = resource_manager_.GetDrmDevice(displ);
46 std::shared_ptr<Importer> importer = resource_manager_.GetImporter(displ);
Alexandru Gheorghec5463582018-03-27 15:52:02 +010047 if (!drm || !importer) {
48 ALOGE("Failed to get a valid drmresource and importer");
Sean Paulac874152016-03-10 16:00:26 -050049 return HWC2::Error::NoResources;
50 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030051 displays_.emplace(std::piecewise_construct, std::forward_as_tuple(displ),
Sean Paulf72cccd2018-08-27 13:59:08 -040052 std::forward_as_tuple(&resource_manager_, drm, importer,
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030053 displ, type));
Sean Paulac874152016-03-10 16:00:26 -050054
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030055 DrmCrtc *crtc = drm->GetCrtcForDisplay(static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050056 if (!crtc) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030057 ALOGE("Failed to get crtc for display %d", static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050058 return HWC2::Error::BadDisplay;
59 }
Sean Paulac874152016-03-10 16:00:26 -050060 std::vector<DrmPlane *> display_planes;
Alexandru Gheorghec5463582018-03-27 15:52:02 +010061 for (auto &plane : drm->planes()) {
Sean Paulac874152016-03-10 16:00:26 -050062 if (plane->GetCrtcSupported(*crtc))
63 display_planes.push_back(plane.get());
64 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030065 displays_.at(displ).Init(&display_planes);
Sean Paulac874152016-03-10 16:00:26 -050066 return HWC2::Error::None;
67}
68
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030069HWC2::Error DrmHwcTwo::Init() {
70 int rv = resource_manager_.Init();
71 if (rv) {
72 ALOGE("Can't initialize the resource manager %d", rv);
73 return HWC2::Error::NoResources;
74 }
75
76 HWC2::Error ret = HWC2::Error::None;
77 for (int i = 0; i < resource_manager_.getDisplayCount(); i++) {
78 ret = CreateDisplay(i, HWC2::DisplayType::Physical);
79 if (ret != HWC2::Error::None) {
80 ALOGE("Failed to create display %d with error %d", i, ret);
81 return ret;
82 }
83 }
84
85 auto &drmDevices = resource_manager_.getDrmDevices();
86 for (auto &device : drmDevices) {
87 device->RegisterHotplugHandler(new DrmHotplugHandler(this, device.get()));
88 }
89 return ret;
90}
91
Sean Pauled2ec4b2016-03-10 15:35:40 -050092template <typename... Args>
93static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
94 ALOGV("Unsupported function: %s", func);
95 return HWC2::Error::Unsupported;
96}
97
Sean Paulac874152016-03-10 16:00:26 -050098static inline void supported(char const *func) {
99 ALOGV("Supported function: %s", func);
100}
101
Sean Pauled2ec4b2016-03-10 15:35:40 -0500102HWC2::Error DrmHwcTwo::CreateVirtualDisplay(uint32_t width, uint32_t height,
103 int32_t *format,
104 hwc2_display_t *display) {
105 // TODO: Implement virtual display
Sean Paulac874152016-03-10 16:00:26 -0500106 return unsupported(__func__, width, height, format, display);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500107}
108
109HWC2::Error DrmHwcTwo::DestroyVirtualDisplay(hwc2_display_t display) {
Sean Paulac874152016-03-10 16:00:26 -0500110 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500111 return unsupported(__func__, display);
112}
113
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200114std::string DrmHwcTwo::HwcDisplay::DumpDelta(
115 DrmHwcTwo::HwcDisplay::Stats delta) {
116 if (delta.total_pixops_ == 0)
117 return "No stats yet";
118 double Ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
119
120 return (std::stringstream()
121 << " Total frames count: " << delta.total_frames_ << "\n"
122 << " Failed to test commit frames: " << delta.failed_kms_validate_
123 << "\n"
124 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
125 << ((delta.failed_kms_present_ > 0)
126 ? " !!! Internal failure, FIX it please\n"
127 : "")
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200128 << " Flattened frames: " << delta.frames_flattened_ << "\n"
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200129 << " Pixel operations (free units)"
130 << " : [TOTAL: " << delta.total_pixops_
131 << " / GPU: " << delta.gpu_pixops_ << "]\n"
132 << " Composition efficiency: " << Ratio)
133 .str();
134}
135
136std::string DrmHwcTwo::HwcDisplay::Dump() {
137 auto out = (std::stringstream()
138 << "- Display on: " << connector_->name() << "\n"
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200139 << " Flattening state: " << compositor_.GetFlatteningState()
140 << "\n"
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200141 << "Statistics since system boot:\n"
142 << DumpDelta(total_stats_) << "\n\n"
143 << "Statistics since last dumpsys request:\n"
144 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n")
145 .str();
146
147 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
148 return out;
149}
150
151void DrmHwcTwo::Dump(uint32_t *outSize, char *outBuffer) {
152 supported(__func__);
153
154 if (outBuffer != nullptr) {
155 auto copiedBytes = mDumpString.copy(outBuffer, *outSize);
156 *outSize = static_cast<uint32_t>(copiedBytes);
157 return;
158 }
159
160 std::stringstream output;
161
162 output << "-- drm_hwcomposer --\n\n";
163
164 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &dp : displays_)
165 output << dp.second.Dump();
166
167 mDumpString = output.str();
168 *outSize = static_cast<uint32_t>(mDumpString.size());
Sean Pauled2ec4b2016-03-10 15:35:40 -0500169}
170
171uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() {
Sean Paulac874152016-03-10 16:00:26 -0500172 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500173 unsupported(__func__);
174 return 0;
175}
176
177HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor,
Sean Paulac874152016-03-10 16:00:26 -0500178 hwc2_callback_data_t data,
179 hwc2_function_pointer_t function) {
180 supported(__func__);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300181
Roman Stratiienko23701092020-09-26 02:08:41 +0300182 switch (static_cast<HWC2::Callback>(descriptor)) {
Sean Paulac874152016-03-10 16:00:26 -0500183 case HWC2::Callback::Hotplug: {
Roman Stratiienko23701092020-09-26 02:08:41 +0300184 SetHotplugCallback(data, function);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300185 auto &drmDevices = resource_manager_.getDrmDevices();
186 for (auto &device : drmDevices)
187 HandleInitialHotplugState(device.get());
Sean Paulac874152016-03-10 16:00:26 -0500188 break;
189 }
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200190 case HWC2::Callback::Refresh: {
191 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
192 displays_)
193 d.second.RegisterRefreshCallback(data, function);
194 break;
195 }
Sean Paulac874152016-03-10 16:00:26 -0500196 case HWC2::Callback::Vsync: {
197 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
198 displays_)
199 d.second.RegisterVsyncCallback(data, function);
200 break;
201 }
202 default:
203 break;
204 }
205 return HWC2::Error::None;
206}
207
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100208DrmHwcTwo::HwcDisplay::HwcDisplay(ResourceManager *resource_manager,
209 DrmDevice *drm,
Sean Paulac874152016-03-10 16:00:26 -0500210 std::shared_ptr<Importer> importer,
Sean Paulac874152016-03-10 16:00:26 -0500211 hwc2_display_t handle, HWC2::DisplayType type)
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100212 : resource_manager_(resource_manager),
213 drm_(drm),
214 importer_(importer),
215 handle_(handle),
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200216 type_(type),
217 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
Sean Paulac874152016-03-10 16:00:26 -0500218 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200219
220 // clang-format off
221 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
222 0.0, 1.0, 0.0, 0.0,
223 0.0, 0.0, 1.0, 0.0,
224 0.0, 0.0, 0.0, 1.0};
225 // clang-format on
Sean Paulac874152016-03-10 16:00:26 -0500226}
227
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300228void DrmHwcTwo::HwcDisplay::ClearDisplay() {
229 compositor_.ClearDisplay();
230}
231
Sean Paulac874152016-03-10 16:00:26 -0500232HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
233 supported(__func__);
234 planner_ = Planner::CreateInstance(drm_);
235 if (!planner_) {
236 ALOGE("Failed to create planner instance for composition");
237 return HWC2::Error::NoResources;
238 }
239
240 int display = static_cast<int>(handle_);
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100241 int ret = compositor_.Init(resource_manager_, display);
Sean Paulac874152016-03-10 16:00:26 -0500242 if (ret) {
243 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
244 return HWC2::Error::NoResources;
245 }
246
247 // Split up the given display planes into primary and overlay to properly
248 // interface with the composition
249 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
Jason Macnakf1af9572020-08-20 11:49:51 -0700250 property_get("vendor.hwc.drm.use_overlay_planes", use_overlay_planes_prop,
251 "1");
Sean Paulac874152016-03-10 16:00:26 -0500252 bool use_overlay_planes = atoi(use_overlay_planes_prop);
253 for (auto &plane : *planes) {
254 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
255 primary_planes_.push_back(plane);
256 else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY)
257 overlay_planes_.push_back(plane);
258 }
259
260 crtc_ = drm_->GetCrtcForDisplay(display);
261 if (!crtc_) {
262 ALOGE("Failed to get crtc for display %d", display);
263 return HWC2::Error::BadDisplay;
264 }
265
266 connector_ = drm_->GetConnectorForDisplay(display);
267 if (!connector_) {
268 ALOGE("Failed to get connector for display %d", display);
269 return HWC2::Error::BadDisplay;
270 }
271
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300272 ret = vsync_worker_.Init(drm_, display);
273 if (ret) {
274 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
275 return HWC2::Error::BadDisplay;
276 }
277
Matvii Zorinef3c7972020-08-11 15:15:44 +0300278 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
279 if (ret) {
280 ALOGE("Failed to set backend for d=%d %d\n", display, ret);
281 return HWC2::Error::BadDisplay;
282 }
283
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300284 return ChosePreferredConfig();
285}
286
287HWC2::Error DrmHwcTwo::HwcDisplay::ChosePreferredConfig() {
Sean Paulac874152016-03-10 16:00:26 -0500288 // Fetch the number of modes from the display
289 uint32_t num_configs;
290 HWC2::Error err = GetDisplayConfigs(&num_configs, NULL);
291 if (err != HWC2::Error::None || !num_configs)
292 return err;
293
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200294 return SetActiveConfig(connector_->get_preferred_mode_id());
Sean Paulac874152016-03-10 16:00:26 -0500295}
296
Roman Stratiienko23701092020-09-26 02:08:41 +0300297void DrmHwcTwo::HwcDisplay::RegisterVsyncCallback(
Sean Paulac874152016-03-10 16:00:26 -0500298 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
299 supported(__func__);
Roman Stratiienko23701092020-09-26 02:08:41 +0300300 vsync_worker_.RegisterClientCallback(data, func);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500301}
302
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200303void DrmHwcTwo::HwcDisplay::RegisterRefreshCallback(
304 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
305 supported(__func__);
Roman Stratiienko23701092020-09-26 02:08:41 +0300306 compositor_.SetRefreshCallback(data, func);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200307}
308
Sean Pauled2ec4b2016-03-10 15:35:40 -0500309HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500310 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500311 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
312 l.second.accept_type_change();
313 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500314}
315
316HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500317 supported(__func__);
318 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
319 *layer = static_cast<hwc2_layer_t>(layer_idx_);
320 ++layer_idx_;
321 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500322}
323
324HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Sean Paulac874152016-03-10 16:00:26 -0500325 supported(__func__);
Vincent Donnefort9abec032019-10-09 15:43:43 +0100326 if (!get_layer(layer))
327 return HWC2::Error::BadLayer;
328
Sean Paulac874152016-03-10 16:00:26 -0500329 layers_.erase(layer);
330 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500331}
332
333HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
Sean Paulac874152016-03-10 16:00:26 -0500334 supported(__func__);
335 DrmMode const &mode = connector_->active_mode();
336 if (mode.id() == 0)
337 return HWC2::Error::BadConfig;
338
339 *config = mode.id();
340 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500341}
342
343HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
344 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500345 supported(__func__);
346 uint32_t num_changes = 0;
347 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
348 if (l.second.type_changed()) {
349 if (layers && num_changes < *num_elements)
350 layers[num_changes] = l.first;
351 if (types && num_changes < *num_elements)
352 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
353 ++num_changes;
354 }
355 }
356 if (!layers && !types)
357 *num_elements = num_changes;
358 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500359}
360
361HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500362 uint32_t height,
363 int32_t /*format*/,
364 int32_t dataspace) {
365 supported(__func__);
366 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
367 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
368
369 if (width < min.first || height < min.second)
370 return HWC2::Error::Unsupported;
371
372 if (width > max.first || height > max.second)
373 return HWC2::Error::Unsupported;
374
375 if (dataspace != HAL_DATASPACE_UNKNOWN &&
376 dataspace != HAL_DATASPACE_STANDARD_UNSPECIFIED)
377 return HWC2::Error::Unsupported;
378
379 // TODO: Validate format can be handled by either GL or planes
380 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500381}
382
383HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500384 int32_t *modes) {
385 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800386 if (!modes)
387 *num_modes = 1;
388
389 if (modes)
390 *modes = HAL_COLOR_MODE_NATIVE;
391
392 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500393}
394
395HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500396 int32_t attribute_in,
397 int32_t *value) {
398 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400399 auto mode = std::find_if(connector_->modes().begin(),
400 connector_->modes().end(),
401 [config](DrmMode const &m) {
402 return m.id() == config;
403 });
Sean Paulac874152016-03-10 16:00:26 -0500404 if (mode == connector_->modes().end()) {
405 ALOGE("Could not find active mode for %d", config);
406 return HWC2::Error::BadConfig;
407 }
408
409 static const int32_t kUmPerInch = 25400;
410 uint32_t mm_width = connector_->mm_width();
411 uint32_t mm_height = connector_->mm_height();
412 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
413 switch (attribute) {
414 case HWC2::Attribute::Width:
415 *value = mode->h_display();
416 break;
417 case HWC2::Attribute::Height:
418 *value = mode->v_display();
419 break;
420 case HWC2::Attribute::VsyncPeriod:
421 // in nanoseconds
422 *value = 1000 * 1000 * 1000 / mode->v_refresh();
423 break;
424 case HWC2::Attribute::DpiX:
425 // Dots per 1000 inches
426 *value = mm_width ? (mode->h_display() * kUmPerInch) / mm_width : -1;
427 break;
428 case HWC2::Attribute::DpiY:
429 // Dots per 1000 inches
430 *value = mm_height ? (mode->v_display() * kUmPerInch) / mm_height : -1;
431 break;
432 default:
433 *value = -1;
434 return HWC2::Error::BadConfig;
435 }
436 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500437}
438
439HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
440 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500441 supported(__func__);
442 // Since this callback is normally invoked twice (once to get the count, and
443 // once to populate configs), we don't really want to read the edid
444 // redundantly. Instead, only update the modes on the first invocation. While
445 // it's possible this will result in stale modes, it'll all come out in the
446 // wash when we try to set the active config later.
447 if (!configs) {
448 int ret = connector_->UpdateModes();
449 if (ret) {
450 ALOGE("Failed to update display modes %d", ret);
451 return HWC2::Error::BadDisplay;
452 }
453 }
454
Neil Armstrongb67d0492019-06-20 09:00:21 +0000455 // Since the upper layers only look at vactive/hactive/refresh, height and
456 // width, it doesn't differentiate interlaced from progressive and other
457 // similar modes. Depending on the order of modes we return to SF, it could
458 // end up choosing a suboptimal configuration and dropping the preferred
459 // mode. To workaround this, don't offer interlaced modes to SF if there is
460 // at least one non-interlaced alternative and only offer a single WxH@R
461 // mode with at least the prefered mode from in DrmConnector::UpdateModes()
462
463 // TODO: Remove the following block of code until AOSP handles all modes
464 std::vector<DrmMode> sel_modes;
465
466 // Add the preferred mode first to be sure it's not dropped
467 auto mode = std::find_if(connector_->modes().begin(),
468 connector_->modes().end(), [&](DrmMode const &m) {
469 return m.id() ==
470 connector_->get_preferred_mode_id();
471 });
472 if (mode != connector_->modes().end())
473 sel_modes.push_back(*mode);
474
475 // Add the active mode if different from preferred mode
476 if (connector_->active_mode().id() != connector_->get_preferred_mode_id())
477 sel_modes.push_back(connector_->active_mode());
478
479 // Cycle over the modes and filter out "similar" modes, keeping only the
480 // first ones in the order given by DRM (from CEA ids and timings order)
Sean Paulac874152016-03-10 16:00:26 -0500481 for (const DrmMode &mode : connector_->modes()) {
Neil Armstrongb67d0492019-06-20 09:00:21 +0000482 // TODO: Remove this when 3D Attributes are in AOSP
483 if (mode.flags() & DRM_MODE_FLAG_3D_MASK)
484 continue;
485
Neil Armstrong4c027a72019-06-04 14:48:02 +0000486 // TODO: Remove this when the Interlaced attribute is in AOSP
487 if (mode.flags() & DRM_MODE_FLAG_INTERLACE) {
488 auto m = std::find_if(connector_->modes().begin(),
489 connector_->modes().end(),
490 [&mode](DrmMode const &m) {
491 return !(m.flags() & DRM_MODE_FLAG_INTERLACE) &&
492 m.h_display() == mode.h_display() &&
493 m.v_display() == mode.v_display();
494 });
Neil Armstrongb67d0492019-06-20 09:00:21 +0000495 if (m == connector_->modes().end())
496 sel_modes.push_back(mode);
497
498 continue;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000499 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000500
501 // Search for a similar WxH@R mode in the filtered list and drop it if
502 // another mode with the same WxH@R has already been selected
503 // TODO: Remove this when AOSP handles duplicates modes
504 auto m = std::find_if(sel_modes.begin(), sel_modes.end(),
505 [&mode](DrmMode const &m) {
506 return m.h_display() == mode.h_display() &&
507 m.v_display() == mode.v_display() &&
508 m.v_refresh() == mode.v_refresh();
509 });
510 if (m == sel_modes.end())
511 sel_modes.push_back(mode);
512 }
513
514 auto num_modes = static_cast<uint32_t>(sel_modes.size());
515 if (!configs) {
516 *num_configs = num_modes;
517 return HWC2::Error::None;
518 }
519
520 uint32_t idx = 0;
521 for (const DrmMode &mode : sel_modes) {
522 if (idx >= *num_configs)
523 break;
524 configs[idx++] = mode.id();
Sean Paulac874152016-03-10 16:00:26 -0500525 }
526 *num_configs = idx;
527 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500528}
529
530HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500531 supported(__func__);
532 std::ostringstream stream;
533 stream << "display-" << connector_->id();
534 std::string string = stream.str();
535 size_t length = string.length();
536 if (!name) {
537 *size = length;
538 return HWC2::Error::None;
539 }
540
541 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
542 strncpy(name, string.c_str(), *size);
543 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500544}
545
Sean Paulac874152016-03-10 16:00:26 -0500546HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
547 uint32_t *num_elements,
548 hwc2_layer_t *layers,
549 int32_t *layer_requests) {
550 supported(__func__);
551 // TODO: I think virtual display should request
552 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
553 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
554 *num_elements = 0;
555 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500556}
557
558HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500559 supported(__func__);
560 *type = static_cast<int32_t>(type_);
561 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500562}
563
564HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500565 supported(__func__);
566 *support = 0;
567 return HWC2::Error::None;
568}
569
570HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400571 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
572 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500573 supported(__func__);
574 *num_types = 0;
575 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500576}
577
578HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500579 hwc2_layer_t *layers,
580 int32_t *fences) {
581 supported(__func__);
582 uint32_t num_layers = 0;
583
584 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
585 ++num_layers;
586 if (layers == NULL || fences == NULL) {
587 continue;
588 } else if (num_layers > *num_elements) {
589 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
590 return HWC2::Error::None;
591 }
592
593 layers[num_layers - 1] = l.first;
594 fences[num_layers - 1] = l.second.take_release_fence();
595 }
596 *num_elements = num_layers;
597 return HWC2::Error::None;
598}
599
Matteo Franchinc56eede2019-12-03 17:10:38 +0000600void DrmHwcTwo::HwcDisplay::AddFenceToPresentFence(int fd) {
Sean Paulac874152016-03-10 16:00:26 -0500601 if (fd < 0)
602 return;
603
Matteo Franchinc56eede2019-12-03 17:10:38 +0000604 if (present_fence_.get() >= 0) {
605 int old_fence = present_fence_.get();
606 present_fence_.Set(sync_merge("dc_present", old_fence, fd));
607 close(fd);
Sean Paulac874152016-03-10 16:00:26 -0500608 } else {
Matteo Franchinc56eede2019-12-03 17:10:38 +0000609 present_fence_.Set(fd);
Sean Paulac874152016-03-10 16:00:26 -0500610 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500611}
612
Roman Stratiienkoafb36892019-11-08 17:16:11 +0200613bool DrmHwcTwo::HwcDisplay::HardwareSupportsLayerType(
614 HWC2::Composition comp_type) {
615 return comp_type == HWC2::Composition::Device ||
616 comp_type == HWC2::Composition::Cursor;
617}
618
Rob Herring4f6c62e2018-05-17 14:33:02 -0500619HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500620 std::vector<DrmCompositionDisplayLayersMap> layers_map;
621 layers_map.emplace_back();
622 DrmCompositionDisplayLayersMap &map = layers_map.back();
623
624 map.display = static_cast<int>(handle_);
625 map.geometry_changed = true; // TODO: Fix this
626
627 // order the layers by z-order
628 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100629 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500630 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
631 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200632 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500633 case HWC2::Composition::Device:
634 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
635 break;
636 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100637 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500638 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100639 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500640 break;
641 default:
642 continue;
643 }
644 }
645 if (use_client_layer)
646 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
647
Rob Herring4f6c62e2018-05-17 14:33:02 -0500648 if (z_map.empty())
649 return HWC2::Error::BadLayer;
650
Sean Paulac874152016-03-10 16:00:26 -0500651 // now that they're ordered by z, add them to the composition
652 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
653 DrmHwcLayer layer;
654 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200655 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500656 if (ret) {
657 ALOGE("Failed to import layer, ret=%d", ret);
658 return HWC2::Error::NoResources;
659 }
660 map.layers.emplace_back(std::move(layer));
661 }
Sean Paulac874152016-03-10 16:00:26 -0500662
Sean Paulf72cccd2018-08-27 13:59:08 -0400663 std::unique_ptr<DrmDisplayComposition> composition = compositor_
664 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500665 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
666
667 // TODO: Don't always assume geometry changed
668 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
669 if (ret) {
670 ALOGE("Failed to set layers in the composition ret=%d", ret);
671 return HWC2::Error::BadLayer;
672 }
673
674 std::vector<DrmPlane *> primary_planes(primary_planes_);
675 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500676 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500677 if (ret) {
678 ALOGE("Failed to plan the composition ret=%d", ret);
679 return HWC2::Error::BadConfig;
680 }
681
682 // Disable the planes we're not using
683 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
684 composition->AddPlaneDisable(*i);
685 i = primary_planes.erase(i);
686 }
687 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
688 composition->AddPlaneDisable(*i);
689 i = overlay_planes.erase(i);
690 }
691
Rob Herring4f6c62e2018-05-17 14:33:02 -0500692 if (test) {
693 ret = compositor_.TestComposition(composition.get());
694 } else {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500695 ret = compositor_.ApplyComposition(std::move(composition));
Matteo Franchinc56eede2019-12-03 17:10:38 +0000696 AddFenceToPresentFence(compositor_.TakeOutFence());
Rob Herring4f6c62e2018-05-17 14:33:02 -0500697 }
Sean Paulac874152016-03-10 16:00:26 -0500698 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700699 if (!test)
700 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500701 return HWC2::Error::BadParameter;
702 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500703 return HWC2::Error::None;
704}
705
Matteo Franchinc56eede2019-12-03 17:10:38 +0000706HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500707 supported(__func__);
708 HWC2::Error ret;
709
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200710 ++total_stats_.total_frames_;
711
Rob Herring4f6c62e2018-05-17 14:33:02 -0500712 ret = CreateComposition(false);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200713 if (ret != HWC2::Error::None)
714 ++total_stats_.failed_kms_present_;
715
Rob Herring4f6c62e2018-05-17 14:33:02 -0500716 if (ret == HWC2::Error::BadLayer) {
717 // Can we really have no client or device layers?
Matteo Franchinc56eede2019-12-03 17:10:38 +0000718 *present_fence = -1;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500719 return HWC2::Error::None;
720 }
721 if (ret != HWC2::Error::None)
722 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500723
Matteo Franchinc56eede2019-12-03 17:10:38 +0000724 *present_fence = present_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500725
726 ++frame_no_;
727 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500728}
729
730HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500731 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400732 auto mode = std::find_if(connector_->modes().begin(),
733 connector_->modes().end(),
734 [config](DrmMode const &m) {
735 return m.id() == config;
736 });
Sean Paulac874152016-03-10 16:00:26 -0500737 if (mode == connector_->modes().end()) {
738 ALOGE("Could not find active mode for %d", config);
739 return HWC2::Error::BadConfig;
740 }
741
Sean Paulf72cccd2018-08-27 13:59:08 -0400742 std::unique_ptr<DrmDisplayComposition> composition = compositor_
743 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500744 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
745 int ret = composition->SetDisplayMode(*mode);
Sean Pauled45a8e2017-02-28 13:17:34 -0500746 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500747 if (ret) {
748 ALOGE("Failed to queue dpms composition on %d", ret);
749 return HWC2::Error::BadConfig;
750 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300751
752 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500753
754 // Setup the client layer's dimensions
755 hwc_rect_t display_frame = {.left = 0,
756 .top = 0,
757 .right = static_cast<int>(mode->h_display()),
758 .bottom = static_cast<int>(mode->v_display())};
759 client_layer_.SetLayerDisplayFrame(display_frame);
760 hwc_frect_t source_crop = {.left = 0.0f,
761 .top = 0.0f,
762 .right = mode->h_display() + 0.0f,
763 .bottom = mode->v_display() + 0.0f};
764 client_layer_.SetLayerSourceCrop(source_crop);
765
766 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500767}
768
769HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
770 int32_t acquire_fence,
771 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600772 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500773 supported(__func__);
774 UniqueFd uf(acquire_fence);
775
776 client_layer_.set_buffer(target);
777 client_layer_.set_acquire_fence(uf.get());
778 client_layer_.SetLayerDataspace(dataspace);
779 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500780}
781
782HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500783 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800784
785 if (mode != HAL_COLOR_MODE_NATIVE)
Vincent Donnefort7834a892019-10-09 15:53:56 +0100786 return HWC2::Error::BadParameter;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800787
788 color_mode_ = mode;
789 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500790}
791
792HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500793 int32_t hint) {
794 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200795 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
796 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
797 return HWC2::Error::BadParameter;
798
799 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
800 return HWC2::Error::BadParameter;
801
802 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
803 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
804 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
805
806 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500807}
808
809HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500810 int32_t release_fence) {
811 supported(__func__);
812 // TODO: Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500813 return unsupported(__func__, buffer, release_fence);
814}
815
Sean Paulac874152016-03-10 16:00:26 -0500816HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
817 supported(__func__);
818 uint64_t dpms_value = 0;
819 auto mode = static_cast<HWC2::PowerMode>(mode_in);
820 switch (mode) {
821 case HWC2::PowerMode::Off:
822 dpms_value = DRM_MODE_DPMS_OFF;
823 break;
824 case HWC2::PowerMode::On:
825 dpms_value = DRM_MODE_DPMS_ON;
826 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100827 case HWC2::PowerMode::Doze:
828 case HWC2::PowerMode::DozeSuspend:
829 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500830 default:
831 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100832 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500833 };
834
Sean Paulf72cccd2018-08-27 13:59:08 -0400835 std::unique_ptr<DrmDisplayComposition> composition = compositor_
836 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500837 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
838 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500839 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500840 if (ret) {
841 ALOGE("Failed to apply the dpms composition ret=%d", ret);
842 return HWC2::Error::BadParameter;
843 }
844 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500845}
846
847HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500848 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300849 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500850 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500851}
852
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200853uint32_t DrmHwcTwo::HwcDisplay::CalcPixOps(
854 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t first_z,
855 size_t size) {
856 uint32_t pixops = 0;
857 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
858 if (l.first >= first_z && l.first < first_z + size) {
859 hwc_rect_t df = l.second->display_frame();
860 pixops += (df.right - df.left) * (df.bottom - df.top);
861 }
862 }
863 return pixops;
864}
865
866void DrmHwcTwo::HwcDisplay::MarkValidated(
867 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t client_first_z,
868 size_t client_size) {
869 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
870 if (l.first >= client_first_z && l.first < client_first_z + client_size)
871 l.second->set_validated_type(HWC2::Composition::Client);
872 else
873 l.second->set_validated_type(HWC2::Composition::Device);
874 }
875}
876
Sean Pauled2ec4b2016-03-10 15:35:40 -0500877HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500878 uint32_t *num_requests) {
879 supported(__func__);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500880
Matvii Zorinef3c7972020-08-11 15:15:44 +0300881 return backend_->ValidateDisplay(this, num_types, num_requests);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500882}
883
John Stultz8c7229d2020-02-07 21:31:08 +0000884#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800885HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
886 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
887 supported(__func__);
888
889 drmModePropertyBlobPtr blob;
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800890
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300891 if (connector_->GetEdidBlob(blob)) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800892 ALOGE("Failed to get edid property value.");
893 return HWC2::Error::Unsupported;
894 }
895
Andrii Chepurnyi8115dbe2020-04-14 13:03:57 +0300896 if (outData) {
897 *outDataSize = std::min(*outDataSize, blob->length);
898 memcpy(outData, blob->data, *outDataSize);
899 } else {
900 *outDataSize = blob->length;
901 }
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800902 *outPort = connector_->id();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800903
904 return HWC2::Error::None;
905}
906
907HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
908 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
909 unsupported(__func__, outCapabilities);
910
911 if (outNumCapabilities == NULL) {
912 return HWC2::Error::BadParameter;
913 }
914
915 *outNumCapabilities = 0;
916
917 return HWC2::Error::None;
918}
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +0300919
920HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayBrightnessSupport(
921 bool *supported) {
922 *supported = false;
923 return HWC2::Error::None;
924}
925
926HWC2::Error DrmHwcTwo::HwcDisplay::SetDisplayBrightness(
927 float /* brightness */) {
928 return HWC2::Error::Unsupported;
929}
930
John Stultz8c7229d2020-02-07 21:31:08 +0000931#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800932
Andrii Chepurnyi50d37452020-04-24 14:20:24 +0300933#if PLATFORM_SDK_VERSION > 27
934
935HWC2::Error DrmHwcTwo::HwcDisplay::GetRenderIntents(
936 int32_t mode, uint32_t *outNumIntents,
937 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
938 if (mode != HAL_COLOR_MODE_NATIVE) {
939 return HWC2::Error::BadParameter;
940 }
941
942 if (outIntents == nullptr) {
943 *outNumIntents = 1;
944 return HWC2::Error::None;
945 }
946 *outNumIntents = 1;
947 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
948 return HWC2::Error::None;
949}
950
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +0300951HWC2::Error DrmHwcTwo::HwcDisplay::SetColorModeWithIntent(int32_t mode,
952 int32_t intent) {
953 if (mode != HAL_COLOR_MODE_NATIVE)
954 return HWC2::Error::BadParameter;
955 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
956 return HWC2::Error::BadParameter;
957 color_mode_ = mode;
958 return HWC2::Error::None;
959}
960
Andrii Chepurnyi50d37452020-04-24 14:20:24 +0300961#endif /* PLATFORM_SDK_VERSION > 27 */
962
Sean Pauled2ec4b2016-03-10 15:35:40 -0500963HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -0500964 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800965 cursor_x_ = x;
966 cursor_y_ = y;
967 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500968}
969
970HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500971 supported(__func__);
972 blending_ = static_cast<HWC2::BlendMode>(mode);
973 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500974}
975
976HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500977 int32_t acquire_fence) {
978 supported(__func__);
979 UniqueFd uf(acquire_fence);
980
Sean Paulac874152016-03-10 16:00:26 -0500981 set_buffer(buffer);
982 set_acquire_fence(uf.get());
983 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500984}
985
986HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Roman Kovalivskyibb375692019-12-11 17:48:44 +0200987 // TODO: Put to client composition here?
988 supported(__func__);
989 layer_color_ = color;
990 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500991}
992
993HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -0500994 sf_type_ = static_cast<HWC2::Composition>(type);
995 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500996}
997
998HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -0500999 supported(__func__);
1000 dataspace_ = static_cast<android_dataspace_t>(dataspace);
1001 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001002}
1003
1004HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001005 supported(__func__);
1006 display_frame_ = frame;
1007 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001008}
1009
1010HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001011 supported(__func__);
1012 alpha_ = alpha;
1013 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001014}
1015
1016HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1017 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001018 supported(__func__);
1019 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001020 return unsupported(__func__, stream);
1021}
1022
1023HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001024 supported(__func__);
1025 source_crop_ = crop;
1026 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001027}
1028
1029HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001030 supported(__func__);
1031 // TODO: We don't use surface damage, marking as unsupported
1032 unsupported(__func__, damage);
1033 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001034}
1035
1036HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001037 supported(__func__);
1038 transform_ = static_cast<HWC2::Transform>(transform);
1039 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001040}
1041
1042HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001043 supported(__func__);
1044 // TODO: We don't use this information, marking as unsupported
1045 unsupported(__func__, visible);
1046 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001047}
1048
Sean Paulac874152016-03-10 16:00:26 -05001049HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1050 supported(__func__);
1051 z_order_ = order;
1052 return HWC2::Error::None;
1053}
1054
1055void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1056 supported(__func__);
1057 switch (blending_) {
1058 case HWC2::BlendMode::None:
1059 layer->blending = DrmHwcBlending::kNone;
1060 break;
1061 case HWC2::BlendMode::Premultiplied:
1062 layer->blending = DrmHwcBlending::kPreMult;
1063 break;
1064 case HWC2::BlendMode::Coverage:
1065 layer->blending = DrmHwcBlending::kCoverage;
1066 break;
1067 default:
1068 ALOGE("Unknown blending mode b=%d", blending_);
1069 layer->blending = DrmHwcBlending::kNone;
1070 break;
1071 }
1072
1073 OutputFd release_fence = release_fence_output();
1074
1075 layer->sf_handle = buffer_;
1076 layer->acquire_fence = acquire_fence_.Release();
1077 layer->release_fence = std::move(release_fence);
1078 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +02001079 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -05001080 layer->SetSourceCrop(source_crop_);
1081 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -05001082}
1083
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001084void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
Roman Stratiienko23701092020-09-26 02:08:41 +03001085 const std::lock_guard<std::mutex> lock(hotplug_callback_lock);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001086
Roman Stratiienko23701092020-09-26 02:08:41 +03001087 if (hotplug_callback_hook_ && hotplug_callback_data_)
1088 hotplug_callback_hook_(hotplug_callback_data_, displayid,
1089 state == DRM_MODE_CONNECTED
1090 ? HWC2_CONNECTION_CONNECTED
1091 : HWC2_CONNECTION_DISCONNECTED);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001092}
1093
1094void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
1095 for (auto &conn : drmDevice->connectors()) {
1096 if (conn->state() != DRM_MODE_CONNECTED)
1097 continue;
1098 HandleDisplayHotplug(conn->display(), conn->state());
1099 }
1100}
1101
1102void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
1103 for (auto &conn : drm_->connectors()) {
1104 drmModeConnection old_state = conn->state();
1105 drmModeConnection cur_state = conn->UpdateModes()
1106 ? DRM_MODE_UNKNOWNCONNECTION
1107 : conn->state();
1108
1109 if (cur_state == old_state)
1110 continue;
1111
1112 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
1113 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1114 conn->id(), conn->display());
1115
1116 int display_id = conn->display();
1117 if (cur_state == DRM_MODE_CONNECTED) {
1118 auto &display = hwc2_->displays_.at(display_id);
1119 display.ChosePreferredConfig();
1120 } else {
1121 auto &display = hwc2_->displays_.at(display_id);
1122 display.ClearDisplay();
1123 }
1124
1125 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1126 }
1127}
1128
Sean Pauled2ec4b2016-03-10 15:35:40 -05001129// static
1130int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1131 unsupported(__func__);
1132 return 0;
1133}
1134
1135// static
1136void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001137 uint32_t *out_count,
1138 int32_t * /*out_capabilities*/) {
1139 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001140 *out_count = 0;
1141}
1142
1143// static
Sean Paulac874152016-03-10 16:00:26 -05001144hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1145 struct hwc2_device * /*dev*/, int32_t descriptor) {
1146 supported(__func__);
1147 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001148 switch (func) {
1149 // Device functions
1150 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1151 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1152 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1153 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001154 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001155 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1156 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1157 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1158 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1159 case HWC2::FunctionDescriptor::Dump:
1160 return ToHook<HWC2_PFN_DUMP>(
1161 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1162 uint32_t *, char *>);
1163 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1164 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1165 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1166 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1167 case HWC2::FunctionDescriptor::RegisterCallback:
1168 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1169 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1170 &DrmHwcTwo::RegisterCallback, int32_t,
1171 hwc2_callback_data_t, hwc2_function_pointer_t>);
1172
1173 // Display functions
1174 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1175 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1176 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1177 &HwcDisplay::AcceptDisplayChanges>);
1178 case HWC2::FunctionDescriptor::CreateLayer:
1179 return ToHook<HWC2_PFN_CREATE_LAYER>(
1180 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1181 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1182 case HWC2::FunctionDescriptor::DestroyLayer:
1183 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1184 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1185 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1186 case HWC2::FunctionDescriptor::GetActiveConfig:
1187 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1188 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1189 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1190 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1191 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1192 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1193 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1194 hwc2_layer_t *, int32_t *>);
1195 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1196 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1197 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1198 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1199 int32_t, int32_t>);
1200 case HWC2::FunctionDescriptor::GetColorModes:
1201 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1202 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1203 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1204 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001205 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1206 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1207 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1208 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001209 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001210 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1211 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1212 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1213 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001214 case HWC2::FunctionDescriptor::GetDisplayName:
1215 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1216 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1217 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1218 case HWC2::FunctionDescriptor::GetDisplayRequests:
1219 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1220 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1221 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1222 hwc2_layer_t *, int32_t *>);
1223 case HWC2::FunctionDescriptor::GetDisplayType:
1224 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1225 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1226 &HwcDisplay::GetDisplayType, int32_t *>);
1227 case HWC2::FunctionDescriptor::GetDozeSupport:
1228 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1229 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1230 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001231 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1232 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1233 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1234 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1235 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001236 case HWC2::FunctionDescriptor::GetReleaseFences:
1237 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1238 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1239 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1240 int32_t *>);
1241 case HWC2::FunctionDescriptor::PresentDisplay:
1242 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1243 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1244 &HwcDisplay::PresentDisplay, int32_t *>);
1245 case HWC2::FunctionDescriptor::SetActiveConfig:
1246 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1247 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1248 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1249 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001250 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1251 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1252 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1253 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001254 case HWC2::FunctionDescriptor::SetColorMode:
1255 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1256 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1257 &HwcDisplay::SetColorMode, int32_t>);
1258 case HWC2::FunctionDescriptor::SetColorTransform:
1259 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1260 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1261 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1262 case HWC2::FunctionDescriptor::SetOutputBuffer:
1263 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1264 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1265 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1266 case HWC2::FunctionDescriptor::SetPowerMode:
1267 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1268 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1269 &HwcDisplay::SetPowerMode, int32_t>);
1270 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1271 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1272 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1273 &HwcDisplay::SetVsyncEnabled, int32_t>);
1274 case HWC2::FunctionDescriptor::ValidateDisplay:
1275 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1276 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1277 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001278#if PLATFORM_SDK_VERSION > 27
1279 case HWC2::FunctionDescriptor::GetRenderIntents:
1280 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
1281 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
1282 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
1283 int32_t *>);
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001284 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
1285 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
1286 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
1287 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001288#endif
John Stultz8c7229d2020-02-07 21:31:08 +00001289#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001290 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1291 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1292 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1293 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1294 uint32_t *, uint8_t *>);
1295 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1296 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1297 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1298 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1299 uint32_t *>);
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001300 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
1301 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
1302 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
1303 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
1304 case HWC2::FunctionDescriptor::SetDisplayBrightness:
1305 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
1306 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
1307 &HwcDisplay::SetDisplayBrightness, float>);
John Stultz8c7229d2020-02-07 21:31:08 +00001308#endif /* PLATFORM_SDK_VERSION > 28 */
Sean Pauled2ec4b2016-03-10 15:35:40 -05001309 // Layer functions
1310 case HWC2::FunctionDescriptor::SetCursorPosition:
1311 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1312 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1313 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1314 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1315 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1316 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1317 &HwcLayer::SetLayerBlendMode, int32_t>);
1318 case HWC2::FunctionDescriptor::SetLayerBuffer:
1319 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1320 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1321 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1322 case HWC2::FunctionDescriptor::SetLayerColor:
1323 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1324 LayerHook<decltype(&HwcLayer::SetLayerColor),
1325 &HwcLayer::SetLayerColor, hwc_color_t>);
1326 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1327 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1328 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1329 &HwcLayer::SetLayerCompositionType, int32_t>);
1330 case HWC2::FunctionDescriptor::SetLayerDataspace:
1331 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1332 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1333 &HwcLayer::SetLayerDataspace, int32_t>);
1334 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1335 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1336 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1337 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1338 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1339 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1340 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1341 &HwcLayer::SetLayerPlaneAlpha, float>);
1342 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001343 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1344 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1345 &HwcLayer::SetLayerSidebandStream,
1346 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001347 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1348 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1349 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1350 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1351 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1352 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1353 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1354 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1355 case HWC2::FunctionDescriptor::SetLayerTransform:
1356 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1357 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1358 &HwcLayer::SetLayerTransform, int32_t>);
1359 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1360 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1361 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1362 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1363 case HWC2::FunctionDescriptor::SetLayerZOrder:
1364 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1365 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1366 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001367 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001368 default:
1369 return NULL;
1370 }
1371}
Sean Paulac874152016-03-10 16:00:26 -05001372
1373// static
1374int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1375 struct hw_device_t **dev) {
1376 supported(__func__);
1377 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1378 ALOGE("Invalid module name- %s", name);
1379 return -EINVAL;
1380 }
1381
1382 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1383 if (!ctx) {
1384 ALOGE("Failed to allocate DrmHwcTwo");
1385 return -ENOMEM;
1386 }
1387
1388 HWC2::Error err = ctx->Init();
1389 if (err != HWC2::Error::None) {
1390 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1391 return -EINVAL;
1392 }
1393
1394 ctx->common.module = const_cast<hw_module_t *>(module);
1395 *dev = &ctx->common;
1396 ctx.release();
1397 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001398}
Sean Paulf72cccd2018-08-27 13:59:08 -04001399} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001400
1401static struct hw_module_methods_t hwc2_module_methods = {
1402 .open = android::DrmHwcTwo::HookDevOpen,
1403};
1404
1405hw_module_t HAL_MODULE_INFO_SYM = {
1406 .tag = HARDWARE_MODULE_TAG,
1407 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1408 .id = HWC_HARDWARE_MODULE_ID,
1409 .name = "DrmHwcTwo module",
1410 .author = "The Android Open Source Project",
1411 .methods = &hwc2_module_methods,
1412 .dso = NULL,
1413 .reserved = {0},
1414};