blob: db5f351c8572a81f1fe580e30045c07fab5282f2 [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"
Roman Stratiienko33365c22020-10-10 23:06:36 +030031#include "bufferinfo/BufferInfoGetter.h"
Roman Stratiienko13cc3662020-08-29 21:35:39 +030032#include "compositor/DrmDisplayComposition.h"
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030033
Sean Pauled2ec4b2016-03-10 15:35:40 -050034namespace android {
35
36DrmHwcTwo::DrmHwcTwo() {
Sean Paulac874152016-03-10 16:00:26 -050037 common.tag = HARDWARE_DEVICE_TAG;
38 common.version = HWC_DEVICE_API_VERSION_2_0;
Sean Pauled2ec4b2016-03-10 15:35:40 -050039 common.close = HookDevClose;
40 getCapabilities = HookDevGetCapabilities;
41 getFunction = HookDevGetFunction;
42}
43
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030044HWC2::Error DrmHwcTwo::CreateDisplay(hwc2_display_t displ,
45 HWC2::DisplayType type) {
46 DrmDevice *drm = resource_manager_.GetDrmDevice(displ);
47 std::shared_ptr<Importer> importer = resource_manager_.GetImporter(displ);
Alexandru Gheorghec5463582018-03-27 15:52:02 +010048 if (!drm || !importer) {
49 ALOGE("Failed to get a valid drmresource and importer");
Sean Paulac874152016-03-10 16:00:26 -050050 return HWC2::Error::NoResources;
51 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030052 displays_.emplace(std::piecewise_construct, std::forward_as_tuple(displ),
Sean Paulf72cccd2018-08-27 13:59:08 -040053 std::forward_as_tuple(&resource_manager_, drm, importer,
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030054 displ, type));
Sean Paulac874152016-03-10 16:00:26 -050055
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030056 DrmCrtc *crtc = drm->GetCrtcForDisplay(static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050057 if (!crtc) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030058 ALOGE("Failed to get crtc for display %d", static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050059 return HWC2::Error::BadDisplay;
60 }
Sean Paulac874152016-03-10 16:00:26 -050061 std::vector<DrmPlane *> display_planes;
Alexandru Gheorghec5463582018-03-27 15:52:02 +010062 for (auto &plane : drm->planes()) {
Sean Paulac874152016-03-10 16:00:26 -050063 if (plane->GetCrtcSupported(*crtc))
64 display_planes.push_back(plane.get());
65 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030066 displays_.at(displ).Init(&display_planes);
Sean Paulac874152016-03-10 16:00:26 -050067 return HWC2::Error::None;
68}
69
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030070HWC2::Error DrmHwcTwo::Init() {
71 int rv = resource_manager_.Init();
72 if (rv) {
73 ALOGE("Can't initialize the resource manager %d", rv);
74 return HWC2::Error::NoResources;
75 }
76
77 HWC2::Error ret = HWC2::Error::None;
78 for (int i = 0; i < resource_manager_.getDisplayCount(); i++) {
79 ret = CreateDisplay(i, HWC2::DisplayType::Physical);
80 if (ret != HWC2::Error::None) {
81 ALOGE("Failed to create display %d with error %d", i, ret);
82 return ret;
83 }
84 }
85
86 auto &drmDevices = resource_manager_.getDrmDevices();
87 for (auto &device : drmDevices) {
88 device->RegisterHotplugHandler(new DrmHotplugHandler(this, device.get()));
89 }
90 return ret;
91}
92
Sean Pauled2ec4b2016-03-10 15:35:40 -050093template <typename... Args>
94static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
95 ALOGV("Unsupported function: %s", func);
96 return HWC2::Error::Unsupported;
97}
98
Sean Paulac874152016-03-10 16:00:26 -050099static inline void supported(char const *func) {
100 ALOGV("Supported function: %s", func);
101}
102
Sean Pauled2ec4b2016-03-10 15:35:40 -0500103HWC2::Error DrmHwcTwo::CreateVirtualDisplay(uint32_t width, uint32_t height,
104 int32_t *format,
105 hwc2_display_t *display) {
106 // TODO: Implement virtual display
Sean Paulac874152016-03-10 16:00:26 -0500107 return unsupported(__func__, width, height, format, display);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500108}
109
110HWC2::Error DrmHwcTwo::DestroyVirtualDisplay(hwc2_display_t display) {
Sean Paulac874152016-03-10 16:00:26 -0500111 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500112 return unsupported(__func__, display);
113}
114
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200115std::string DrmHwcTwo::HwcDisplay::DumpDelta(
116 DrmHwcTwo::HwcDisplay::Stats delta) {
117 if (delta.total_pixops_ == 0)
118 return "No stats yet";
119 double Ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
120
121 return (std::stringstream()
122 << " Total frames count: " << delta.total_frames_ << "\n"
123 << " Failed to test commit frames: " << delta.failed_kms_validate_
124 << "\n"
125 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
126 << ((delta.failed_kms_present_ > 0)
127 ? " !!! Internal failure, FIX it please\n"
128 : "")
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200129 << " Flattened frames: " << delta.frames_flattened_ << "\n"
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200130 << " Pixel operations (free units)"
131 << " : [TOTAL: " << delta.total_pixops_
132 << " / GPU: " << delta.gpu_pixops_ << "]\n"
133 << " Composition efficiency: " << Ratio)
134 .str();
135}
136
137std::string DrmHwcTwo::HwcDisplay::Dump() {
138 auto out = (std::stringstream()
139 << "- Display on: " << connector_->name() << "\n"
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200140 << " Flattening state: " << compositor_.GetFlatteningState()
141 << "\n"
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200142 << "Statistics since system boot:\n"
143 << DumpDelta(total_stats_) << "\n\n"
144 << "Statistics since last dumpsys request:\n"
145 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n")
146 .str();
147
148 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
149 return out;
150}
151
152void DrmHwcTwo::Dump(uint32_t *outSize, char *outBuffer) {
153 supported(__func__);
154
155 if (outBuffer != nullptr) {
156 auto copiedBytes = mDumpString.copy(outBuffer, *outSize);
157 *outSize = static_cast<uint32_t>(copiedBytes);
158 return;
159 }
160
161 std::stringstream output;
162
163 output << "-- drm_hwcomposer --\n\n";
164
165 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &dp : displays_)
166 output << dp.second.Dump();
167
168 mDumpString = output.str();
169 *outSize = static_cast<uint32_t>(mDumpString.size());
Sean Pauled2ec4b2016-03-10 15:35:40 -0500170}
171
172uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() {
Sean Paulac874152016-03-10 16:00:26 -0500173 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500174 unsupported(__func__);
175 return 0;
176}
177
178HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor,
Sean Paulac874152016-03-10 16:00:26 -0500179 hwc2_callback_data_t data,
180 hwc2_function_pointer_t function) {
181 supported(__func__);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300182
Roman Stratiienko23701092020-09-26 02:08:41 +0300183 switch (static_cast<HWC2::Callback>(descriptor)) {
Sean Paulac874152016-03-10 16:00:26 -0500184 case HWC2::Callback::Hotplug: {
Roman Stratiienko23701092020-09-26 02:08:41 +0300185 SetHotplugCallback(data, function);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300186 auto &drmDevices = resource_manager_.getDrmDevices();
187 for (auto &device : drmDevices)
188 HandleInitialHotplugState(device.get());
Sean Paulac874152016-03-10 16:00:26 -0500189 break;
190 }
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200191 case HWC2::Callback::Refresh: {
192 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
193 displays_)
194 d.second.RegisterRefreshCallback(data, function);
195 break;
196 }
Sean Paulac874152016-03-10 16:00:26 -0500197 case HWC2::Callback::Vsync: {
198 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
199 displays_)
200 d.second.RegisterVsyncCallback(data, function);
201 break;
202 }
203 default:
204 break;
205 }
206 return HWC2::Error::None;
207}
208
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100209DrmHwcTwo::HwcDisplay::HwcDisplay(ResourceManager *resource_manager,
210 DrmDevice *drm,
Sean Paulac874152016-03-10 16:00:26 -0500211 std::shared_ptr<Importer> importer,
Sean Paulac874152016-03-10 16:00:26 -0500212 hwc2_display_t handle, HWC2::DisplayType type)
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100213 : resource_manager_(resource_manager),
214 drm_(drm),
215 importer_(importer),
216 handle_(handle),
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200217 type_(type),
218 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
Sean Paulac874152016-03-10 16:00:26 -0500219 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200220
221 // clang-format off
222 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
223 0.0, 1.0, 0.0, 0.0,
224 0.0, 0.0, 1.0, 0.0,
225 0.0, 0.0, 0.0, 1.0};
226 // clang-format on
Sean Paulac874152016-03-10 16:00:26 -0500227}
228
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300229void DrmHwcTwo::HwcDisplay::ClearDisplay() {
230 compositor_.ClearDisplay();
231}
232
Sean Paulac874152016-03-10 16:00:26 -0500233HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
234 supported(__func__);
235 planner_ = Planner::CreateInstance(drm_);
236 if (!planner_) {
237 ALOGE("Failed to create planner instance for composition");
238 return HWC2::Error::NoResources;
239 }
240
241 int display = static_cast<int>(handle_);
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100242 int ret = compositor_.Init(resource_manager_, display);
Sean Paulac874152016-03-10 16:00:26 -0500243 if (ret) {
244 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
245 return HWC2::Error::NoResources;
246 }
247
248 // Split up the given display planes into primary and overlay to properly
249 // interface with the composition
250 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
Jason Macnakf1af9572020-08-20 11:49:51 -0700251 property_get("vendor.hwc.drm.use_overlay_planes", use_overlay_planes_prop,
252 "1");
Sean Paulac874152016-03-10 16:00:26 -0500253 bool use_overlay_planes = atoi(use_overlay_planes_prop);
254 for (auto &plane : *planes) {
255 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
256 primary_planes_.push_back(plane);
257 else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY)
258 overlay_planes_.push_back(plane);
259 }
260
261 crtc_ = drm_->GetCrtcForDisplay(display);
262 if (!crtc_) {
263 ALOGE("Failed to get crtc for display %d", display);
264 return HWC2::Error::BadDisplay;
265 }
266
267 connector_ = drm_->GetConnectorForDisplay(display);
268 if (!connector_) {
269 ALOGE("Failed to get connector for display %d", display);
270 return HWC2::Error::BadDisplay;
271 }
272
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300273 ret = vsync_worker_.Init(drm_, display);
274 if (ret) {
275 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
276 return HWC2::Error::BadDisplay;
277 }
278
Matvii Zorinef3c7972020-08-11 15:15:44 +0300279 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
280 if (ret) {
281 ALOGE("Failed to set backend for d=%d %d\n", display, ret);
282 return HWC2::Error::BadDisplay;
283 }
284
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300285 return ChosePreferredConfig();
286}
287
288HWC2::Error DrmHwcTwo::HwcDisplay::ChosePreferredConfig() {
Sean Paulac874152016-03-10 16:00:26 -0500289 // Fetch the number of modes from the display
290 uint32_t num_configs;
291 HWC2::Error err = GetDisplayConfigs(&num_configs, NULL);
292 if (err != HWC2::Error::None || !num_configs)
293 return err;
294
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200295 return SetActiveConfig(connector_->get_preferred_mode_id());
Sean Paulac874152016-03-10 16:00:26 -0500296}
297
Roman Stratiienko23701092020-09-26 02:08:41 +0300298void DrmHwcTwo::HwcDisplay::RegisterVsyncCallback(
Sean Paulac874152016-03-10 16:00:26 -0500299 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
300 supported(__func__);
Roman Stratiienko23701092020-09-26 02:08:41 +0300301 vsync_worker_.RegisterClientCallback(data, func);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500302}
303
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200304void DrmHwcTwo::HwcDisplay::RegisterRefreshCallback(
305 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
306 supported(__func__);
Roman Stratiienko23701092020-09-26 02:08:41 +0300307 compositor_.SetRefreshCallback(data, func);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200308}
309
Sean Pauled2ec4b2016-03-10 15:35:40 -0500310HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500311 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500312 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
313 l.second.accept_type_change();
314 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500315}
316
317HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500318 supported(__func__);
319 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
320 *layer = static_cast<hwc2_layer_t>(layer_idx_);
321 ++layer_idx_;
322 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500323}
324
325HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Sean Paulac874152016-03-10 16:00:26 -0500326 supported(__func__);
Vincent Donnefort9abec032019-10-09 15:43:43 +0100327 if (!get_layer(layer))
328 return HWC2::Error::BadLayer;
329
Sean Paulac874152016-03-10 16:00:26 -0500330 layers_.erase(layer);
331 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500332}
333
334HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
Sean Paulac874152016-03-10 16:00:26 -0500335 supported(__func__);
336 DrmMode const &mode = connector_->active_mode();
337 if (mode.id() == 0)
338 return HWC2::Error::BadConfig;
339
340 *config = mode.id();
341 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500342}
343
344HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
345 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500346 supported(__func__);
347 uint32_t num_changes = 0;
348 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
349 if (l.second.type_changed()) {
350 if (layers && num_changes < *num_elements)
351 layers[num_changes] = l.first;
352 if (types && num_changes < *num_elements)
353 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
354 ++num_changes;
355 }
356 }
357 if (!layers && !types)
358 *num_elements = num_changes;
359 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500360}
361
362HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500363 uint32_t height,
364 int32_t /*format*/,
365 int32_t dataspace) {
366 supported(__func__);
367 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
368 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
369
370 if (width < min.first || height < min.second)
371 return HWC2::Error::Unsupported;
372
373 if (width > max.first || height > max.second)
374 return HWC2::Error::Unsupported;
375
376 if (dataspace != HAL_DATASPACE_UNKNOWN &&
377 dataspace != HAL_DATASPACE_STANDARD_UNSPECIFIED)
378 return HWC2::Error::Unsupported;
379
380 // TODO: Validate format can be handled by either GL or planes
381 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500382}
383
384HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500385 int32_t *modes) {
386 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800387 if (!modes)
388 *num_modes = 1;
389
390 if (modes)
391 *modes = HAL_COLOR_MODE_NATIVE;
392
393 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500394}
395
396HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500397 int32_t attribute_in,
398 int32_t *value) {
399 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400400 auto mode = std::find_if(connector_->modes().begin(),
401 connector_->modes().end(),
402 [config](DrmMode const &m) {
403 return m.id() == config;
404 });
Sean Paulac874152016-03-10 16:00:26 -0500405 if (mode == connector_->modes().end()) {
406 ALOGE("Could not find active mode for %d", config);
407 return HWC2::Error::BadConfig;
408 }
409
410 static const int32_t kUmPerInch = 25400;
411 uint32_t mm_width = connector_->mm_width();
412 uint32_t mm_height = connector_->mm_height();
413 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
414 switch (attribute) {
415 case HWC2::Attribute::Width:
416 *value = mode->h_display();
417 break;
418 case HWC2::Attribute::Height:
419 *value = mode->v_display();
420 break;
421 case HWC2::Attribute::VsyncPeriod:
422 // in nanoseconds
423 *value = 1000 * 1000 * 1000 / mode->v_refresh();
424 break;
425 case HWC2::Attribute::DpiX:
426 // Dots per 1000 inches
427 *value = mm_width ? (mode->h_display() * kUmPerInch) / mm_width : -1;
428 break;
429 case HWC2::Attribute::DpiY:
430 // Dots per 1000 inches
431 *value = mm_height ? (mode->v_display() * kUmPerInch) / mm_height : -1;
432 break;
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300433#if PLATFORM_SDK_VERSION > 29
434 case HWC2::Attribute::ConfigGroup:
435 *value = 0; /* TODO: Add support for config groups */
436 break;
437#endif
Sean Paulac874152016-03-10 16:00:26 -0500438 default:
439 *value = -1;
440 return HWC2::Error::BadConfig;
441 }
442 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500443}
444
445HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
446 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500447 supported(__func__);
448 // Since this callback is normally invoked twice (once to get the count, and
449 // once to populate configs), we don't really want to read the edid
450 // redundantly. Instead, only update the modes on the first invocation. While
451 // it's possible this will result in stale modes, it'll all come out in the
452 // wash when we try to set the active config later.
453 if (!configs) {
454 int ret = connector_->UpdateModes();
455 if (ret) {
456 ALOGE("Failed to update display modes %d", ret);
457 return HWC2::Error::BadDisplay;
458 }
459 }
460
Neil Armstrongb67d0492019-06-20 09:00:21 +0000461 // Since the upper layers only look at vactive/hactive/refresh, height and
462 // width, it doesn't differentiate interlaced from progressive and other
463 // similar modes. Depending on the order of modes we return to SF, it could
464 // end up choosing a suboptimal configuration and dropping the preferred
465 // mode. To workaround this, don't offer interlaced modes to SF if there is
466 // at least one non-interlaced alternative and only offer a single WxH@R
467 // mode with at least the prefered mode from in DrmConnector::UpdateModes()
468
469 // TODO: Remove the following block of code until AOSP handles all modes
470 std::vector<DrmMode> sel_modes;
471
472 // Add the preferred mode first to be sure it's not dropped
473 auto mode = std::find_if(connector_->modes().begin(),
474 connector_->modes().end(), [&](DrmMode const &m) {
475 return m.id() ==
476 connector_->get_preferred_mode_id();
477 });
478 if (mode != connector_->modes().end())
479 sel_modes.push_back(*mode);
480
481 // Add the active mode if different from preferred mode
482 if (connector_->active_mode().id() != connector_->get_preferred_mode_id())
483 sel_modes.push_back(connector_->active_mode());
484
485 // Cycle over the modes and filter out "similar" modes, keeping only the
486 // first ones in the order given by DRM (from CEA ids and timings order)
Sean Paulac874152016-03-10 16:00:26 -0500487 for (const DrmMode &mode : connector_->modes()) {
Neil Armstrongb67d0492019-06-20 09:00:21 +0000488 // TODO: Remove this when 3D Attributes are in AOSP
489 if (mode.flags() & DRM_MODE_FLAG_3D_MASK)
490 continue;
491
Neil Armstrong4c027a72019-06-04 14:48:02 +0000492 // TODO: Remove this when the Interlaced attribute is in AOSP
493 if (mode.flags() & DRM_MODE_FLAG_INTERLACE) {
494 auto m = std::find_if(connector_->modes().begin(),
495 connector_->modes().end(),
496 [&mode](DrmMode const &m) {
497 return !(m.flags() & DRM_MODE_FLAG_INTERLACE) &&
498 m.h_display() == mode.h_display() &&
499 m.v_display() == mode.v_display();
500 });
Neil Armstrongb67d0492019-06-20 09:00:21 +0000501 if (m == connector_->modes().end())
502 sel_modes.push_back(mode);
503
504 continue;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000505 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000506
507 // Search for a similar WxH@R mode in the filtered list and drop it if
508 // another mode with the same WxH@R has already been selected
509 // TODO: Remove this when AOSP handles duplicates modes
510 auto m = std::find_if(sel_modes.begin(), sel_modes.end(),
511 [&mode](DrmMode const &m) {
512 return m.h_display() == mode.h_display() &&
513 m.v_display() == mode.v_display() &&
514 m.v_refresh() == mode.v_refresh();
515 });
516 if (m == sel_modes.end())
517 sel_modes.push_back(mode);
518 }
519
520 auto num_modes = static_cast<uint32_t>(sel_modes.size());
521 if (!configs) {
522 *num_configs = num_modes;
523 return HWC2::Error::None;
524 }
525
526 uint32_t idx = 0;
527 for (const DrmMode &mode : sel_modes) {
528 if (idx >= *num_configs)
529 break;
530 configs[idx++] = mode.id();
Sean Paulac874152016-03-10 16:00:26 -0500531 }
532 *num_configs = idx;
533 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500534}
535
536HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500537 supported(__func__);
538 std::ostringstream stream;
539 stream << "display-" << connector_->id();
540 std::string string = stream.str();
541 size_t length = string.length();
542 if (!name) {
543 *size = length;
544 return HWC2::Error::None;
545 }
546
547 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
548 strncpy(name, string.c_str(), *size);
549 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500550}
551
Sean Paulac874152016-03-10 16:00:26 -0500552HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
553 uint32_t *num_elements,
554 hwc2_layer_t *layers,
555 int32_t *layer_requests) {
556 supported(__func__);
557 // TODO: I think virtual display should request
558 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
559 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
560 *num_elements = 0;
561 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500562}
563
564HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500565 supported(__func__);
566 *type = static_cast<int32_t>(type_);
567 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500568}
569
570HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500571 supported(__func__);
572 *support = 0;
573 return HWC2::Error::None;
574}
575
576HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400577 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
578 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500579 supported(__func__);
580 *num_types = 0;
581 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500582}
583
584HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500585 hwc2_layer_t *layers,
586 int32_t *fences) {
587 supported(__func__);
588 uint32_t num_layers = 0;
589
590 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
591 ++num_layers;
592 if (layers == NULL || fences == NULL) {
593 continue;
594 } else if (num_layers > *num_elements) {
595 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
596 return HWC2::Error::None;
597 }
598
599 layers[num_layers - 1] = l.first;
600 fences[num_layers - 1] = l.second.take_release_fence();
601 }
602 *num_elements = num_layers;
603 return HWC2::Error::None;
604}
605
Matteo Franchinc56eede2019-12-03 17:10:38 +0000606void DrmHwcTwo::HwcDisplay::AddFenceToPresentFence(int fd) {
Sean Paulac874152016-03-10 16:00:26 -0500607 if (fd < 0)
608 return;
609
Matteo Franchinc56eede2019-12-03 17:10:38 +0000610 if (present_fence_.get() >= 0) {
611 int old_fence = present_fence_.get();
612 present_fence_.Set(sync_merge("dc_present", old_fence, fd));
613 close(fd);
Sean Paulac874152016-03-10 16:00:26 -0500614 } else {
Matteo Franchinc56eede2019-12-03 17:10:38 +0000615 present_fence_.Set(fd);
Sean Paulac874152016-03-10 16:00:26 -0500616 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500617}
618
Roman Stratiienkoafb36892019-11-08 17:16:11 +0200619bool DrmHwcTwo::HwcDisplay::HardwareSupportsLayerType(
620 HWC2::Composition comp_type) {
621 return comp_type == HWC2::Composition::Device ||
622 comp_type == HWC2::Composition::Cursor;
623}
624
Rob Herring4f6c62e2018-05-17 14:33:02 -0500625HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500626 std::vector<DrmCompositionDisplayLayersMap> layers_map;
627 layers_map.emplace_back();
628 DrmCompositionDisplayLayersMap &map = layers_map.back();
629
630 map.display = static_cast<int>(handle_);
631 map.geometry_changed = true; // TODO: Fix this
632
633 // order the layers by z-order
634 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100635 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500636 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
637 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200638 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500639 case HWC2::Composition::Device:
640 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
641 break;
642 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100643 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500644 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100645 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500646 break;
647 default:
648 continue;
649 }
650 }
651 if (use_client_layer)
652 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
653
Rob Herring4f6c62e2018-05-17 14:33:02 -0500654 if (z_map.empty())
655 return HWC2::Error::BadLayer;
656
Sean Paulac874152016-03-10 16:00:26 -0500657 // now that they're ordered by z, add them to the composition
658 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
659 DrmHwcLayer layer;
660 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200661 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500662 if (ret) {
663 ALOGE("Failed to import layer, ret=%d", ret);
664 return HWC2::Error::NoResources;
665 }
666 map.layers.emplace_back(std::move(layer));
667 }
Sean Paulac874152016-03-10 16:00:26 -0500668
Sean Paulf72cccd2018-08-27 13:59:08 -0400669 std::unique_ptr<DrmDisplayComposition> composition = compositor_
670 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500671 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
672
673 // TODO: Don't always assume geometry changed
674 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
675 if (ret) {
676 ALOGE("Failed to set layers in the composition ret=%d", ret);
677 return HWC2::Error::BadLayer;
678 }
679
680 std::vector<DrmPlane *> primary_planes(primary_planes_);
681 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500682 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500683 if (ret) {
684 ALOGE("Failed to plan the composition ret=%d", ret);
685 return HWC2::Error::BadConfig;
686 }
687
688 // Disable the planes we're not using
689 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
690 composition->AddPlaneDisable(*i);
691 i = primary_planes.erase(i);
692 }
693 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
694 composition->AddPlaneDisable(*i);
695 i = overlay_planes.erase(i);
696 }
697
Rob Herring4f6c62e2018-05-17 14:33:02 -0500698 if (test) {
699 ret = compositor_.TestComposition(composition.get());
700 } else {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500701 ret = compositor_.ApplyComposition(std::move(composition));
Matteo Franchinc56eede2019-12-03 17:10:38 +0000702 AddFenceToPresentFence(compositor_.TakeOutFence());
Rob Herring4f6c62e2018-05-17 14:33:02 -0500703 }
Sean Paulac874152016-03-10 16:00:26 -0500704 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700705 if (!test)
706 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500707 return HWC2::Error::BadParameter;
708 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500709 return HWC2::Error::None;
710}
711
Matteo Franchinc56eede2019-12-03 17:10:38 +0000712HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500713 supported(__func__);
714 HWC2::Error ret;
715
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200716 ++total_stats_.total_frames_;
717
Rob Herring4f6c62e2018-05-17 14:33:02 -0500718 ret = CreateComposition(false);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200719 if (ret != HWC2::Error::None)
720 ++total_stats_.failed_kms_present_;
721
Rob Herring4f6c62e2018-05-17 14:33:02 -0500722 if (ret == HWC2::Error::BadLayer) {
723 // Can we really have no client or device layers?
Matteo Franchinc56eede2019-12-03 17:10:38 +0000724 *present_fence = -1;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500725 return HWC2::Error::None;
726 }
727 if (ret != HWC2::Error::None)
728 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500729
Matteo Franchinc56eede2019-12-03 17:10:38 +0000730 *present_fence = present_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500731
732 ++frame_no_;
733 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500734}
735
736HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500737 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400738 auto mode = std::find_if(connector_->modes().begin(),
739 connector_->modes().end(),
740 [config](DrmMode const &m) {
741 return m.id() == config;
742 });
Sean Paulac874152016-03-10 16:00:26 -0500743 if (mode == connector_->modes().end()) {
744 ALOGE("Could not find active mode for %d", config);
745 return HWC2::Error::BadConfig;
746 }
747
Sean Paulf72cccd2018-08-27 13:59:08 -0400748 std::unique_ptr<DrmDisplayComposition> composition = compositor_
749 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500750 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
751 int ret = composition->SetDisplayMode(*mode);
Sean Pauled45a8e2017-02-28 13:17:34 -0500752 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500753 if (ret) {
754 ALOGE("Failed to queue dpms composition on %d", ret);
755 return HWC2::Error::BadConfig;
756 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300757
758 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500759
760 // Setup the client layer's dimensions
761 hwc_rect_t display_frame = {.left = 0,
762 .top = 0,
763 .right = static_cast<int>(mode->h_display()),
764 .bottom = static_cast<int>(mode->v_display())};
765 client_layer_.SetLayerDisplayFrame(display_frame);
Sean Paulac874152016-03-10 16:00:26 -0500766
767 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500768}
769
770HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
771 int32_t acquire_fence,
772 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600773 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500774 supported(__func__);
775 UniqueFd uf(acquire_fence);
776
777 client_layer_.set_buffer(target);
778 client_layer_.set_acquire_fence(uf.get());
779 client_layer_.SetLayerDataspace(dataspace);
Roman Stratiienko33365c22020-10-10 23:06:36 +0300780
781 /* TODO: Do not update source_crop every call.
782 * It makes sense to do it once after every hotplug event. */
783 hwc_drm_bo bo{};
784 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
785
786 hwc_frect_t source_crop = {.left = 0.0f,
787 .top = 0.0f,
788 .right = bo.width + 0.0f,
789 .bottom = bo.height + 0.0f};
790 client_layer_.SetLayerSourceCrop(source_crop);
791
Sean Paulac874152016-03-10 16:00:26 -0500792 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500793}
794
795HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500796 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800797
Roman Stratiienkod146d6d2020-10-04 23:56:46 +0300798 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
799 return HWC2::Error::BadParameter;
800
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800801 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +0300802 return HWC2::Error::Unsupported;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800803
804 color_mode_ = mode;
805 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500806}
807
808HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500809 int32_t hint) {
810 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200811 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
812 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
813 return HWC2::Error::BadParameter;
814
815 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
816 return HWC2::Error::BadParameter;
817
818 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
819 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
820 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
821
822 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500823}
824
825HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500826 int32_t release_fence) {
827 supported(__func__);
828 // TODO: Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500829 return unsupported(__func__, buffer, release_fence);
830}
831
Sean Paulac874152016-03-10 16:00:26 -0500832HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
833 supported(__func__);
834 uint64_t dpms_value = 0;
835 auto mode = static_cast<HWC2::PowerMode>(mode_in);
836 switch (mode) {
837 case HWC2::PowerMode::Off:
838 dpms_value = DRM_MODE_DPMS_OFF;
839 break;
840 case HWC2::PowerMode::On:
841 dpms_value = DRM_MODE_DPMS_ON;
842 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100843 case HWC2::PowerMode::Doze:
844 case HWC2::PowerMode::DozeSuspend:
845 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500846 default:
847 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100848 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500849 };
850
Sean Paulf72cccd2018-08-27 13:59:08 -0400851 std::unique_ptr<DrmDisplayComposition> composition = compositor_
852 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500853 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
854 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500855 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500856 if (ret) {
857 ALOGE("Failed to apply the dpms composition ret=%d", ret);
858 return HWC2::Error::BadParameter;
859 }
860 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500861}
862
863HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500864 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300865 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500866 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500867}
868
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200869uint32_t DrmHwcTwo::HwcDisplay::CalcPixOps(
870 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t first_z,
871 size_t size) {
872 uint32_t pixops = 0;
873 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
874 if (l.first >= first_z && l.first < first_z + size) {
875 hwc_rect_t df = l.second->display_frame();
876 pixops += (df.right - df.left) * (df.bottom - df.top);
877 }
878 }
879 return pixops;
880}
881
882void DrmHwcTwo::HwcDisplay::MarkValidated(
883 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t client_first_z,
884 size_t client_size) {
885 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
886 if (l.first >= client_first_z && l.first < client_first_z + client_size)
887 l.second->set_validated_type(HWC2::Composition::Client);
888 else
889 l.second->set_validated_type(HWC2::Composition::Device);
890 }
891}
892
Sean Pauled2ec4b2016-03-10 15:35:40 -0500893HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500894 uint32_t *num_requests) {
895 supported(__func__);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500896
Matvii Zorinef3c7972020-08-11 15:15:44 +0300897 return backend_->ValidateDisplay(this, num_types, num_requests);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500898}
899
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300900#if PLATFORM_SDK_VERSION > 29
901HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
902 if (connector_->internal())
903 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
904 else if (connector_->external())
905 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
906 else
907 return HWC2::Error::BadConfig;
908
909 return HWC2::Error::None;
910}
911
912HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayVsyncPeriod(
913 hwc2_vsync_period_t *outVsyncPeriod /* ns */) {
914 supported(__func__);
915 DrmMode const &mode = connector_->active_mode();
916 if (mode.id() == 0)
917 return HWC2::Error::BadConfig;
918
919 *outVsyncPeriod = 1E9 / mode.v_refresh();
920 return HWC2::Error::None;
921}
922
923HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfigWithConstraints(
924 hwc2_config_t /*config*/,
925 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
926 hwc_vsync_period_change_timeline_t *outTimeline) {
927 supported(__func__);
928
929 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
930 return HWC2::Error::BadParameter;
931 }
932
933 return HWC2::Error::BadConfig;
934}
935
936HWC2::Error DrmHwcTwo::HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
937 return HWC2::Error::Unsupported;
938}
939
940HWC2::Error DrmHwcTwo::HwcDisplay::GetSupportedContentTypes(
941 uint32_t *outNumSupportedContentTypes, uint32_t *outSupportedContentTypes) {
942 if (outSupportedContentTypes == nullptr)
943 *outNumSupportedContentTypes = 0;
944
945 return HWC2::Error::None;
946}
947
948HWC2::Error DrmHwcTwo::HwcDisplay::SetContentType(int32_t contentType) {
949 supported(__func__);
950
951 if (contentType != HWC2_CONTENT_TYPE_NONE)
952 return HWC2::Error::Unsupported;
953
954 /* TODO: Map to the DRM Connector property:
955 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
956 */
957
958 return HWC2::Error::None;
959}
960#endif
961
John Stultz8c7229d2020-02-07 21:31:08 +0000962#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800963HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
964 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
965 supported(__func__);
966
967 drmModePropertyBlobPtr blob;
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800968
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300969 if (connector_->GetEdidBlob(blob)) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800970 ALOGE("Failed to get edid property value.");
971 return HWC2::Error::Unsupported;
972 }
973
Andrii Chepurnyi8115dbe2020-04-14 13:03:57 +0300974 if (outData) {
975 *outDataSize = std::min(*outDataSize, blob->length);
976 memcpy(outData, blob->data, *outDataSize);
977 } else {
978 *outDataSize = blob->length;
979 }
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800980 *outPort = connector_->id();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800981
982 return HWC2::Error::None;
983}
984
985HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
986 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
987 unsupported(__func__, outCapabilities);
988
989 if (outNumCapabilities == NULL) {
990 return HWC2::Error::BadParameter;
991 }
992
993 *outNumCapabilities = 0;
994
995 return HWC2::Error::None;
996}
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +0300997
998HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayBrightnessSupport(
999 bool *supported) {
1000 *supported = false;
1001 return HWC2::Error::None;
1002}
1003
1004HWC2::Error DrmHwcTwo::HwcDisplay::SetDisplayBrightness(
1005 float /* brightness */) {
1006 return HWC2::Error::Unsupported;
1007}
1008
John Stultz8c7229d2020-02-07 21:31:08 +00001009#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001010
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001011#if PLATFORM_SDK_VERSION > 27
1012
1013HWC2::Error DrmHwcTwo::HwcDisplay::GetRenderIntents(
1014 int32_t mode, uint32_t *outNumIntents,
1015 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1016 if (mode != HAL_COLOR_MODE_NATIVE) {
1017 return HWC2::Error::BadParameter;
1018 }
1019
1020 if (outIntents == nullptr) {
1021 *outNumIntents = 1;
1022 return HWC2::Error::None;
1023 }
1024 *outNumIntents = 1;
1025 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1026 return HWC2::Error::None;
1027}
1028
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001029HWC2::Error DrmHwcTwo::HwcDisplay::SetColorModeWithIntent(int32_t mode,
1030 int32_t intent) {
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001031 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1032 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1033 return HWC2::Error::BadParameter;
1034
1035 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
1036 return HWC2::Error::BadParameter;
1037
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001038 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001039 return HWC2::Error::Unsupported;
1040
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001041 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001042 return HWC2::Error::Unsupported;
1043
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001044 color_mode_ = mode;
1045 return HWC2::Error::None;
1046}
1047
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001048#endif /* PLATFORM_SDK_VERSION > 27 */
1049
Sean Pauled2ec4b2016-03-10 15:35:40 -05001050HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -05001051 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -08001052 cursor_x_ = x;
1053 cursor_y_ = y;
1054 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001055}
1056
1057HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -05001058 supported(__func__);
1059 blending_ = static_cast<HWC2::BlendMode>(mode);
1060 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001061}
1062
1063HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001064 int32_t acquire_fence) {
1065 supported(__func__);
1066 UniqueFd uf(acquire_fence);
1067
Sean Paulac874152016-03-10 16:00:26 -05001068 set_buffer(buffer);
1069 set_acquire_fence(uf.get());
1070 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001071}
1072
1073HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001074 // TODO: Put to client composition here?
1075 supported(__func__);
1076 layer_color_ = color;
1077 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001078}
1079
1080HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001081 sf_type_ = static_cast<HWC2::Composition>(type);
1082 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001083}
1084
1085HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001086 supported(__func__);
1087 dataspace_ = static_cast<android_dataspace_t>(dataspace);
1088 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001089}
1090
1091HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001092 supported(__func__);
1093 display_frame_ = frame;
1094 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001095}
1096
1097HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001098 supported(__func__);
1099 alpha_ = alpha;
1100 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001101}
1102
1103HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1104 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001105 supported(__func__);
1106 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001107 return unsupported(__func__, stream);
1108}
1109
1110HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001111 supported(__func__);
1112 source_crop_ = crop;
1113 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001114}
1115
1116HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001117 supported(__func__);
1118 // TODO: We don't use surface damage, marking as unsupported
1119 unsupported(__func__, damage);
1120 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001121}
1122
1123HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001124 supported(__func__);
1125 transform_ = static_cast<HWC2::Transform>(transform);
1126 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001127}
1128
1129HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001130 supported(__func__);
1131 // TODO: We don't use this information, marking as unsupported
1132 unsupported(__func__, visible);
1133 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001134}
1135
Sean Paulac874152016-03-10 16:00:26 -05001136HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1137 supported(__func__);
1138 z_order_ = order;
1139 return HWC2::Error::None;
1140}
1141
1142void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1143 supported(__func__);
1144 switch (blending_) {
1145 case HWC2::BlendMode::None:
1146 layer->blending = DrmHwcBlending::kNone;
1147 break;
1148 case HWC2::BlendMode::Premultiplied:
1149 layer->blending = DrmHwcBlending::kPreMult;
1150 break;
1151 case HWC2::BlendMode::Coverage:
1152 layer->blending = DrmHwcBlending::kCoverage;
1153 break;
1154 default:
1155 ALOGE("Unknown blending mode b=%d", blending_);
1156 layer->blending = DrmHwcBlending::kNone;
1157 break;
1158 }
1159
1160 OutputFd release_fence = release_fence_output();
1161
1162 layer->sf_handle = buffer_;
1163 layer->acquire_fence = acquire_fence_.Release();
1164 layer->release_fence = std::move(release_fence);
1165 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +02001166 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -05001167 layer->SetSourceCrop(source_crop_);
1168 layer->SetTransform(static_cast<int32_t>(transform_));
Matvii Zorin8338c342020-09-08 16:12:51 +03001169 layer->dataspace = dataspace_;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001170}
1171
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001172void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
Roman Stratiienko23701092020-09-26 02:08:41 +03001173 const std::lock_guard<std::mutex> lock(hotplug_callback_lock);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001174
Roman Stratiienko23701092020-09-26 02:08:41 +03001175 if (hotplug_callback_hook_ && hotplug_callback_data_)
1176 hotplug_callback_hook_(hotplug_callback_data_, displayid,
1177 state == DRM_MODE_CONNECTED
1178 ? HWC2_CONNECTION_CONNECTED
1179 : HWC2_CONNECTION_DISCONNECTED);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001180}
1181
1182void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
1183 for (auto &conn : drmDevice->connectors()) {
1184 if (conn->state() != DRM_MODE_CONNECTED)
1185 continue;
1186 HandleDisplayHotplug(conn->display(), conn->state());
1187 }
1188}
1189
1190void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
1191 for (auto &conn : drm_->connectors()) {
1192 drmModeConnection old_state = conn->state();
1193 drmModeConnection cur_state = conn->UpdateModes()
1194 ? DRM_MODE_UNKNOWNCONNECTION
1195 : conn->state();
1196
1197 if (cur_state == old_state)
1198 continue;
1199
1200 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
1201 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1202 conn->id(), conn->display());
1203
1204 int display_id = conn->display();
1205 if (cur_state == DRM_MODE_CONNECTED) {
1206 auto &display = hwc2_->displays_.at(display_id);
1207 display.ChosePreferredConfig();
1208 } else {
1209 auto &display = hwc2_->displays_.at(display_id);
1210 display.ClearDisplay();
1211 }
1212
1213 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1214 }
1215}
1216
Sean Pauled2ec4b2016-03-10 15:35:40 -05001217// static
1218int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1219 unsupported(__func__);
1220 return 0;
1221}
1222
1223// static
1224void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001225 uint32_t *out_count,
1226 int32_t * /*out_capabilities*/) {
1227 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001228 *out_count = 0;
1229}
1230
1231// static
Sean Paulac874152016-03-10 16:00:26 -05001232hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1233 struct hwc2_device * /*dev*/, int32_t descriptor) {
1234 supported(__func__);
1235 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001236 switch (func) {
1237 // Device functions
1238 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1239 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1240 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1241 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001242 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001243 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1244 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1245 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1246 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1247 case HWC2::FunctionDescriptor::Dump:
1248 return ToHook<HWC2_PFN_DUMP>(
1249 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1250 uint32_t *, char *>);
1251 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1252 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1253 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1254 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1255 case HWC2::FunctionDescriptor::RegisterCallback:
1256 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1257 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1258 &DrmHwcTwo::RegisterCallback, int32_t,
1259 hwc2_callback_data_t, hwc2_function_pointer_t>);
1260
1261 // Display functions
1262 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1263 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1264 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1265 &HwcDisplay::AcceptDisplayChanges>);
1266 case HWC2::FunctionDescriptor::CreateLayer:
1267 return ToHook<HWC2_PFN_CREATE_LAYER>(
1268 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1269 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1270 case HWC2::FunctionDescriptor::DestroyLayer:
1271 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1272 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1273 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1274 case HWC2::FunctionDescriptor::GetActiveConfig:
1275 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1276 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1277 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1278 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1279 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1280 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1281 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1282 hwc2_layer_t *, int32_t *>);
1283 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1284 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1285 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1286 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1287 int32_t, int32_t>);
1288 case HWC2::FunctionDescriptor::GetColorModes:
1289 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1290 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1291 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1292 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001293 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1294 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1295 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1296 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001297 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001298 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1299 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1300 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1301 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001302 case HWC2::FunctionDescriptor::GetDisplayName:
1303 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1304 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1305 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1306 case HWC2::FunctionDescriptor::GetDisplayRequests:
1307 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1308 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1309 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1310 hwc2_layer_t *, int32_t *>);
1311 case HWC2::FunctionDescriptor::GetDisplayType:
1312 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1313 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1314 &HwcDisplay::GetDisplayType, int32_t *>);
1315 case HWC2::FunctionDescriptor::GetDozeSupport:
1316 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1317 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1318 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001319 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1320 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1321 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1322 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1323 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001324 case HWC2::FunctionDescriptor::GetReleaseFences:
1325 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1326 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1327 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1328 int32_t *>);
1329 case HWC2::FunctionDescriptor::PresentDisplay:
1330 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1331 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1332 &HwcDisplay::PresentDisplay, int32_t *>);
1333 case HWC2::FunctionDescriptor::SetActiveConfig:
1334 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1335 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1336 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1337 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001338 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1339 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1340 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1341 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001342 case HWC2::FunctionDescriptor::SetColorMode:
1343 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1344 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1345 &HwcDisplay::SetColorMode, int32_t>);
1346 case HWC2::FunctionDescriptor::SetColorTransform:
1347 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1348 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1349 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1350 case HWC2::FunctionDescriptor::SetOutputBuffer:
1351 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1352 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1353 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1354 case HWC2::FunctionDescriptor::SetPowerMode:
1355 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1356 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1357 &HwcDisplay::SetPowerMode, int32_t>);
1358 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1359 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1360 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1361 &HwcDisplay::SetVsyncEnabled, int32_t>);
1362 case HWC2::FunctionDescriptor::ValidateDisplay:
1363 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1364 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1365 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001366#if PLATFORM_SDK_VERSION > 27
1367 case HWC2::FunctionDescriptor::GetRenderIntents:
1368 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
1369 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
1370 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
1371 int32_t *>);
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001372 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
1373 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
1374 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
1375 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001376#endif
John Stultz8c7229d2020-02-07 21:31:08 +00001377#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001378 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1379 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1380 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1381 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1382 uint32_t *, uint8_t *>);
1383 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1384 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1385 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1386 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1387 uint32_t *>);
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001388 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
1389 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
1390 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
1391 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
1392 case HWC2::FunctionDescriptor::SetDisplayBrightness:
1393 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
1394 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
1395 &HwcDisplay::SetDisplayBrightness, float>);
John Stultz8c7229d2020-02-07 21:31:08 +00001396#endif /* PLATFORM_SDK_VERSION > 28 */
Roman Stratiienko6f5df172020-09-27 22:48:08 +03001397#if PLATFORM_SDK_VERSION > 29
1398 case HWC2::FunctionDescriptor::GetDisplayConnectionType:
1399 return ToHook<HWC2_PFN_GET_DISPLAY_CONNECTION_TYPE>(
1400 DisplayHook<decltype(&HwcDisplay::GetDisplayConnectionType),
1401 &HwcDisplay::GetDisplayConnectionType, uint32_t *>);
1402 case HWC2::FunctionDescriptor::GetDisplayVsyncPeriod:
1403 return ToHook<HWC2_PFN_GET_DISPLAY_VSYNC_PERIOD>(
1404 DisplayHook<decltype(&HwcDisplay::GetDisplayVsyncPeriod),
1405 &HwcDisplay::GetDisplayVsyncPeriod,
1406 hwc2_vsync_period_t *>);
1407 case HWC2::FunctionDescriptor::SetActiveConfigWithConstraints:
1408 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG_WITH_CONSTRAINTS>(
1409 DisplayHook<decltype(&HwcDisplay::SetActiveConfigWithConstraints),
1410 &HwcDisplay::SetActiveConfigWithConstraints,
1411 hwc2_config_t, hwc_vsync_period_change_constraints_t *,
1412 hwc_vsync_period_change_timeline_t *>);
1413 case HWC2::FunctionDescriptor::SetAutoLowLatencyMode:
1414 return ToHook<HWC2_PFN_SET_AUTO_LOW_LATENCY_MODE>(
1415 DisplayHook<decltype(&HwcDisplay::SetAutoLowLatencyMode),
1416 &HwcDisplay::SetAutoLowLatencyMode, bool>);
1417 case HWC2::FunctionDescriptor::GetSupportedContentTypes:
1418 return ToHook<HWC2_PFN_GET_SUPPORTED_CONTENT_TYPES>(
1419 DisplayHook<decltype(&HwcDisplay::GetSupportedContentTypes),
1420 &HwcDisplay::GetSupportedContentTypes, uint32_t *,
1421 uint32_t *>);
1422 case HWC2::FunctionDescriptor::SetContentType:
1423 return ToHook<HWC2_PFN_SET_CONTENT_TYPE>(
1424 DisplayHook<decltype(&HwcDisplay::SetContentType),
1425 &HwcDisplay::SetContentType, int32_t>);
1426#endif
Sean Pauled2ec4b2016-03-10 15:35:40 -05001427 // Layer functions
1428 case HWC2::FunctionDescriptor::SetCursorPosition:
1429 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1430 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1431 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1432 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1433 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1434 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1435 &HwcLayer::SetLayerBlendMode, int32_t>);
1436 case HWC2::FunctionDescriptor::SetLayerBuffer:
1437 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1438 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1439 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1440 case HWC2::FunctionDescriptor::SetLayerColor:
1441 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1442 LayerHook<decltype(&HwcLayer::SetLayerColor),
1443 &HwcLayer::SetLayerColor, hwc_color_t>);
1444 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1445 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1446 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1447 &HwcLayer::SetLayerCompositionType, int32_t>);
1448 case HWC2::FunctionDescriptor::SetLayerDataspace:
1449 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1450 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1451 &HwcLayer::SetLayerDataspace, int32_t>);
1452 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1453 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1454 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1455 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1456 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1457 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1458 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1459 &HwcLayer::SetLayerPlaneAlpha, float>);
1460 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001461 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1462 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1463 &HwcLayer::SetLayerSidebandStream,
1464 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001465 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1466 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1467 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1468 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1469 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1470 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1471 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1472 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1473 case HWC2::FunctionDescriptor::SetLayerTransform:
1474 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1475 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1476 &HwcLayer::SetLayerTransform, int32_t>);
1477 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1478 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1479 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1480 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1481 case HWC2::FunctionDescriptor::SetLayerZOrder:
1482 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1483 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1484 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001485 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001486 default:
1487 return NULL;
1488 }
1489}
Sean Paulac874152016-03-10 16:00:26 -05001490
1491// static
1492int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1493 struct hw_device_t **dev) {
1494 supported(__func__);
1495 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1496 ALOGE("Invalid module name- %s", name);
1497 return -EINVAL;
1498 }
1499
1500 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1501 if (!ctx) {
1502 ALOGE("Failed to allocate DrmHwcTwo");
1503 return -ENOMEM;
1504 }
1505
1506 HWC2::Error err = ctx->Init();
1507 if (err != HWC2::Error::None) {
1508 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1509 return -EINVAL;
1510 }
1511
1512 ctx->common.module = const_cast<hw_module_t *>(module);
1513 *dev = &ctx->common;
1514 ctx.release();
1515 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001516}
Sean Paulf72cccd2018-08-27 13:59:08 -04001517} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001518
1519static struct hw_module_methods_t hwc2_module_methods = {
1520 .open = android::DrmHwcTwo::HookDevOpen,
1521};
1522
1523hw_module_t HAL_MODULE_INFO_SYM = {
1524 .tag = HARDWARE_MODULE_TAG,
1525 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1526 .id = HWC_HARDWARE_MODULE_ID,
1527 .name = "DrmHwcTwo module",
1528 .author = "The Android Open Source Project",
1529 .methods = &hwc2_module_methods,
1530 .dso = NULL,
1531 .reserved = {0},
1532};