blob: b5b460eed03381e3d1d4a151eb04aac111cc845d [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;
433 default:
434 *value = -1;
435 return HWC2::Error::BadConfig;
436 }
437 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500438}
439
440HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
441 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500442 supported(__func__);
443 // Since this callback is normally invoked twice (once to get the count, and
444 // once to populate configs), we don't really want to read the edid
445 // redundantly. Instead, only update the modes on the first invocation. While
446 // it's possible this will result in stale modes, it'll all come out in the
447 // wash when we try to set the active config later.
448 if (!configs) {
449 int ret = connector_->UpdateModes();
450 if (ret) {
451 ALOGE("Failed to update display modes %d", ret);
452 return HWC2::Error::BadDisplay;
453 }
454 }
455
Neil Armstrongb67d0492019-06-20 09:00:21 +0000456 // Since the upper layers only look at vactive/hactive/refresh, height and
457 // width, it doesn't differentiate interlaced from progressive and other
458 // similar modes. Depending on the order of modes we return to SF, it could
459 // end up choosing a suboptimal configuration and dropping the preferred
460 // mode. To workaround this, don't offer interlaced modes to SF if there is
461 // at least one non-interlaced alternative and only offer a single WxH@R
462 // mode with at least the prefered mode from in DrmConnector::UpdateModes()
463
464 // TODO: Remove the following block of code until AOSP handles all modes
465 std::vector<DrmMode> sel_modes;
466
467 // Add the preferred mode first to be sure it's not dropped
468 auto mode = std::find_if(connector_->modes().begin(),
469 connector_->modes().end(), [&](DrmMode const &m) {
470 return m.id() ==
471 connector_->get_preferred_mode_id();
472 });
473 if (mode != connector_->modes().end())
474 sel_modes.push_back(*mode);
475
476 // Add the active mode if different from preferred mode
477 if (connector_->active_mode().id() != connector_->get_preferred_mode_id())
478 sel_modes.push_back(connector_->active_mode());
479
480 // Cycle over the modes and filter out "similar" modes, keeping only the
481 // first ones in the order given by DRM (from CEA ids and timings order)
Sean Paulac874152016-03-10 16:00:26 -0500482 for (const DrmMode &mode : connector_->modes()) {
Neil Armstrongb67d0492019-06-20 09:00:21 +0000483 // TODO: Remove this when 3D Attributes are in AOSP
484 if (mode.flags() & DRM_MODE_FLAG_3D_MASK)
485 continue;
486
Neil Armstrong4c027a72019-06-04 14:48:02 +0000487 // TODO: Remove this when the Interlaced attribute is in AOSP
488 if (mode.flags() & DRM_MODE_FLAG_INTERLACE) {
489 auto m = std::find_if(connector_->modes().begin(),
490 connector_->modes().end(),
491 [&mode](DrmMode const &m) {
492 return !(m.flags() & DRM_MODE_FLAG_INTERLACE) &&
493 m.h_display() == mode.h_display() &&
494 m.v_display() == mode.v_display();
495 });
Neil Armstrongb67d0492019-06-20 09:00:21 +0000496 if (m == connector_->modes().end())
497 sel_modes.push_back(mode);
498
499 continue;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000500 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000501
502 // Search for a similar WxH@R mode in the filtered list and drop it if
503 // another mode with the same WxH@R has already been selected
504 // TODO: Remove this when AOSP handles duplicates modes
505 auto m = std::find_if(sel_modes.begin(), sel_modes.end(),
506 [&mode](DrmMode const &m) {
507 return m.h_display() == mode.h_display() &&
508 m.v_display() == mode.v_display() &&
509 m.v_refresh() == mode.v_refresh();
510 });
511 if (m == sel_modes.end())
512 sel_modes.push_back(mode);
513 }
514
515 auto num_modes = static_cast<uint32_t>(sel_modes.size());
516 if (!configs) {
517 *num_configs = num_modes;
518 return HWC2::Error::None;
519 }
520
521 uint32_t idx = 0;
522 for (const DrmMode &mode : sel_modes) {
523 if (idx >= *num_configs)
524 break;
525 configs[idx++] = mode.id();
Sean Paulac874152016-03-10 16:00:26 -0500526 }
527 *num_configs = idx;
528 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500529}
530
531HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500532 supported(__func__);
533 std::ostringstream stream;
534 stream << "display-" << connector_->id();
535 std::string string = stream.str();
536 size_t length = string.length();
537 if (!name) {
538 *size = length;
539 return HWC2::Error::None;
540 }
541
542 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
543 strncpy(name, string.c_str(), *size);
544 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500545}
546
Sean Paulac874152016-03-10 16:00:26 -0500547HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
548 uint32_t *num_elements,
549 hwc2_layer_t *layers,
550 int32_t *layer_requests) {
551 supported(__func__);
552 // TODO: I think virtual display should request
553 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
554 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
555 *num_elements = 0;
556 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500557}
558
559HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500560 supported(__func__);
561 *type = static_cast<int32_t>(type_);
562 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500563}
564
565HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500566 supported(__func__);
567 *support = 0;
568 return HWC2::Error::None;
569}
570
571HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400572 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
573 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500574 supported(__func__);
575 *num_types = 0;
576 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500577}
578
579HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500580 hwc2_layer_t *layers,
581 int32_t *fences) {
582 supported(__func__);
583 uint32_t num_layers = 0;
584
585 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
586 ++num_layers;
587 if (layers == NULL || fences == NULL) {
588 continue;
589 } else if (num_layers > *num_elements) {
590 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
591 return HWC2::Error::None;
592 }
593
594 layers[num_layers - 1] = l.first;
595 fences[num_layers - 1] = l.second.take_release_fence();
596 }
597 *num_elements = num_layers;
598 return HWC2::Error::None;
599}
600
Matteo Franchinc56eede2019-12-03 17:10:38 +0000601void DrmHwcTwo::HwcDisplay::AddFenceToPresentFence(int fd) {
Sean Paulac874152016-03-10 16:00:26 -0500602 if (fd < 0)
603 return;
604
Matteo Franchinc56eede2019-12-03 17:10:38 +0000605 if (present_fence_.get() >= 0) {
606 int old_fence = present_fence_.get();
607 present_fence_.Set(sync_merge("dc_present", old_fence, fd));
608 close(fd);
Sean Paulac874152016-03-10 16:00:26 -0500609 } else {
Matteo Franchinc56eede2019-12-03 17:10:38 +0000610 present_fence_.Set(fd);
Sean Paulac874152016-03-10 16:00:26 -0500611 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500612}
613
Roman Stratiienkoafb36892019-11-08 17:16:11 +0200614bool DrmHwcTwo::HwcDisplay::HardwareSupportsLayerType(
615 HWC2::Composition comp_type) {
616 return comp_type == HWC2::Composition::Device ||
617 comp_type == HWC2::Composition::Cursor;
618}
619
Rob Herring4f6c62e2018-05-17 14:33:02 -0500620HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500621 std::vector<DrmCompositionDisplayLayersMap> layers_map;
622 layers_map.emplace_back();
623 DrmCompositionDisplayLayersMap &map = layers_map.back();
624
625 map.display = static_cast<int>(handle_);
626 map.geometry_changed = true; // TODO: Fix this
627
628 // order the layers by z-order
629 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100630 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500631 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
632 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200633 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500634 case HWC2::Composition::Device:
635 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
636 break;
637 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100638 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500639 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100640 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500641 break;
642 default:
643 continue;
644 }
645 }
646 if (use_client_layer)
647 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
648
Rob Herring4f6c62e2018-05-17 14:33:02 -0500649 if (z_map.empty())
650 return HWC2::Error::BadLayer;
651
Sean Paulac874152016-03-10 16:00:26 -0500652 // now that they're ordered by z, add them to the composition
653 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
654 DrmHwcLayer layer;
655 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200656 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500657 if (ret) {
658 ALOGE("Failed to import layer, ret=%d", ret);
659 return HWC2::Error::NoResources;
660 }
661 map.layers.emplace_back(std::move(layer));
662 }
Sean Paulac874152016-03-10 16:00:26 -0500663
Sean Paulf72cccd2018-08-27 13:59:08 -0400664 std::unique_ptr<DrmDisplayComposition> composition = compositor_
665 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500666 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
667
668 // TODO: Don't always assume geometry changed
669 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
670 if (ret) {
671 ALOGE("Failed to set layers in the composition ret=%d", ret);
672 return HWC2::Error::BadLayer;
673 }
674
675 std::vector<DrmPlane *> primary_planes(primary_planes_);
676 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500677 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500678 if (ret) {
679 ALOGE("Failed to plan the composition ret=%d", ret);
680 return HWC2::Error::BadConfig;
681 }
682
683 // Disable the planes we're not using
684 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
685 composition->AddPlaneDisable(*i);
686 i = primary_planes.erase(i);
687 }
688 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
689 composition->AddPlaneDisable(*i);
690 i = overlay_planes.erase(i);
691 }
692
Rob Herring4f6c62e2018-05-17 14:33:02 -0500693 if (test) {
694 ret = compositor_.TestComposition(composition.get());
695 } else {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500696 ret = compositor_.ApplyComposition(std::move(composition));
Matteo Franchinc56eede2019-12-03 17:10:38 +0000697 AddFenceToPresentFence(compositor_.TakeOutFence());
Rob Herring4f6c62e2018-05-17 14:33:02 -0500698 }
Sean Paulac874152016-03-10 16:00:26 -0500699 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700700 if (!test)
701 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500702 return HWC2::Error::BadParameter;
703 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500704 return HWC2::Error::None;
705}
706
Matteo Franchinc56eede2019-12-03 17:10:38 +0000707HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500708 supported(__func__);
709 HWC2::Error ret;
710
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200711 ++total_stats_.total_frames_;
712
Rob Herring4f6c62e2018-05-17 14:33:02 -0500713 ret = CreateComposition(false);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200714 if (ret != HWC2::Error::None)
715 ++total_stats_.failed_kms_present_;
716
Rob Herring4f6c62e2018-05-17 14:33:02 -0500717 if (ret == HWC2::Error::BadLayer) {
718 // Can we really have no client or device layers?
Matteo Franchinc56eede2019-12-03 17:10:38 +0000719 *present_fence = -1;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500720 return HWC2::Error::None;
721 }
722 if (ret != HWC2::Error::None)
723 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500724
Matteo Franchinc56eede2019-12-03 17:10:38 +0000725 *present_fence = present_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500726
727 ++frame_no_;
728 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500729}
730
731HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500732 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400733 auto mode = std::find_if(connector_->modes().begin(),
734 connector_->modes().end(),
735 [config](DrmMode const &m) {
736 return m.id() == config;
737 });
Sean Paulac874152016-03-10 16:00:26 -0500738 if (mode == connector_->modes().end()) {
739 ALOGE("Could not find active mode for %d", config);
740 return HWC2::Error::BadConfig;
741 }
742
Sean Paulf72cccd2018-08-27 13:59:08 -0400743 std::unique_ptr<DrmDisplayComposition> composition = compositor_
744 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500745 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
746 int ret = composition->SetDisplayMode(*mode);
Sean Pauled45a8e2017-02-28 13:17:34 -0500747 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500748 if (ret) {
749 ALOGE("Failed to queue dpms composition on %d", ret);
750 return HWC2::Error::BadConfig;
751 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300752
753 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500754
755 // Setup the client layer's dimensions
756 hwc_rect_t display_frame = {.left = 0,
757 .top = 0,
758 .right = static_cast<int>(mode->h_display()),
759 .bottom = static_cast<int>(mode->v_display())};
760 client_layer_.SetLayerDisplayFrame(display_frame);
Sean Paulac874152016-03-10 16:00:26 -0500761
762 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500763}
764
765HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
766 int32_t acquire_fence,
767 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600768 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500769 supported(__func__);
770 UniqueFd uf(acquire_fence);
771
772 client_layer_.set_buffer(target);
773 client_layer_.set_acquire_fence(uf.get());
774 client_layer_.SetLayerDataspace(dataspace);
Roman Stratiienko33365c22020-10-10 23:06:36 +0300775
776 /* TODO: Do not update source_crop every call.
777 * It makes sense to do it once after every hotplug event. */
778 hwc_drm_bo bo{};
779 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
780
781 hwc_frect_t source_crop = {.left = 0.0f,
782 .top = 0.0f,
783 .right = bo.width + 0.0f,
784 .bottom = bo.height + 0.0f};
785 client_layer_.SetLayerSourceCrop(source_crop);
786
Sean Paulac874152016-03-10 16:00:26 -0500787 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500788}
789
790HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500791 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800792
Roman Stratiienkod146d6d2020-10-04 23:56:46 +0300793 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
794 return HWC2::Error::BadParameter;
795
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800796 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +0300797 return HWC2::Error::Unsupported;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800798
799 color_mode_ = mode;
800 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500801}
802
803HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500804 int32_t hint) {
805 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200806 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
807 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
808 return HWC2::Error::BadParameter;
809
810 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
811 return HWC2::Error::BadParameter;
812
813 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
814 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
815 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
816
817 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500818}
819
820HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500821 int32_t release_fence) {
822 supported(__func__);
823 // TODO: Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500824 return unsupported(__func__, buffer, release_fence);
825}
826
Sean Paulac874152016-03-10 16:00:26 -0500827HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
828 supported(__func__);
829 uint64_t dpms_value = 0;
830 auto mode = static_cast<HWC2::PowerMode>(mode_in);
831 switch (mode) {
832 case HWC2::PowerMode::Off:
833 dpms_value = DRM_MODE_DPMS_OFF;
834 break;
835 case HWC2::PowerMode::On:
836 dpms_value = DRM_MODE_DPMS_ON;
837 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100838 case HWC2::PowerMode::Doze:
839 case HWC2::PowerMode::DozeSuspend:
840 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500841 default:
842 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100843 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500844 };
845
Sean Paulf72cccd2018-08-27 13:59:08 -0400846 std::unique_ptr<DrmDisplayComposition> composition = compositor_
847 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500848 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
849 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500850 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500851 if (ret) {
852 ALOGE("Failed to apply the dpms composition ret=%d", ret);
853 return HWC2::Error::BadParameter;
854 }
855 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500856}
857
858HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500859 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300860 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500861 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500862}
863
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200864uint32_t DrmHwcTwo::HwcDisplay::CalcPixOps(
865 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t first_z,
866 size_t size) {
867 uint32_t pixops = 0;
868 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
869 if (l.first >= first_z && l.first < first_z + size) {
870 hwc_rect_t df = l.second->display_frame();
871 pixops += (df.right - df.left) * (df.bottom - df.top);
872 }
873 }
874 return pixops;
875}
876
877void DrmHwcTwo::HwcDisplay::MarkValidated(
878 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t client_first_z,
879 size_t client_size) {
880 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
881 if (l.first >= client_first_z && l.first < client_first_z + client_size)
882 l.second->set_validated_type(HWC2::Composition::Client);
883 else
884 l.second->set_validated_type(HWC2::Composition::Device);
885 }
886}
887
Sean Pauled2ec4b2016-03-10 15:35:40 -0500888HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500889 uint32_t *num_requests) {
890 supported(__func__);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500891
Matvii Zorinef3c7972020-08-11 15:15:44 +0300892 return backend_->ValidateDisplay(this, num_types, num_requests);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500893}
894
John Stultz8c7229d2020-02-07 21:31:08 +0000895#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800896HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
897 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
898 supported(__func__);
899
900 drmModePropertyBlobPtr blob;
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800901
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300902 if (connector_->GetEdidBlob(blob)) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800903 ALOGE("Failed to get edid property value.");
904 return HWC2::Error::Unsupported;
905 }
906
Andrii Chepurnyi8115dbe2020-04-14 13:03:57 +0300907 if (outData) {
908 *outDataSize = std::min(*outDataSize, blob->length);
909 memcpy(outData, blob->data, *outDataSize);
910 } else {
911 *outDataSize = blob->length;
912 }
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800913 *outPort = connector_->id();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800914
915 return HWC2::Error::None;
916}
917
918HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
919 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
920 unsupported(__func__, outCapabilities);
921
922 if (outNumCapabilities == NULL) {
923 return HWC2::Error::BadParameter;
924 }
925
926 *outNumCapabilities = 0;
927
928 return HWC2::Error::None;
929}
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +0300930
931HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayBrightnessSupport(
932 bool *supported) {
933 *supported = false;
934 return HWC2::Error::None;
935}
936
937HWC2::Error DrmHwcTwo::HwcDisplay::SetDisplayBrightness(
938 float /* brightness */) {
939 return HWC2::Error::Unsupported;
940}
941
John Stultz8c7229d2020-02-07 21:31:08 +0000942#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800943
Andrii Chepurnyi50d37452020-04-24 14:20:24 +0300944#if PLATFORM_SDK_VERSION > 27
945
946HWC2::Error DrmHwcTwo::HwcDisplay::GetRenderIntents(
947 int32_t mode, uint32_t *outNumIntents,
948 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
949 if (mode != HAL_COLOR_MODE_NATIVE) {
950 return HWC2::Error::BadParameter;
951 }
952
953 if (outIntents == nullptr) {
954 *outNumIntents = 1;
955 return HWC2::Error::None;
956 }
957 *outNumIntents = 1;
958 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
959 return HWC2::Error::None;
960}
961
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +0300962HWC2::Error DrmHwcTwo::HwcDisplay::SetColorModeWithIntent(int32_t mode,
963 int32_t intent) {
Roman Stratiienko27d2ed62020-09-26 23:59:19 +0300964 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
965 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
966 return HWC2::Error::BadParameter;
967
968 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
969 return HWC2::Error::BadParameter;
970
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +0300971 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +0300972 return HWC2::Error::Unsupported;
973
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +0300974 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +0300975 return HWC2::Error::Unsupported;
976
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +0300977 color_mode_ = mode;
978 return HWC2::Error::None;
979}
980
Andrii Chepurnyi50d37452020-04-24 14:20:24 +0300981#endif /* PLATFORM_SDK_VERSION > 27 */
982
Sean Pauled2ec4b2016-03-10 15:35:40 -0500983HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -0500984 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800985 cursor_x_ = x;
986 cursor_y_ = y;
987 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500988}
989
990HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500991 supported(__func__);
992 blending_ = static_cast<HWC2::BlendMode>(mode);
993 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500994}
995
996HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500997 int32_t acquire_fence) {
998 supported(__func__);
999 UniqueFd uf(acquire_fence);
1000
Sean Paulac874152016-03-10 16:00:26 -05001001 set_buffer(buffer);
1002 set_acquire_fence(uf.get());
1003 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001004}
1005
1006HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001007 // TODO: Put to client composition here?
1008 supported(__func__);
1009 layer_color_ = color;
1010 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001011}
1012
1013HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001014 sf_type_ = static_cast<HWC2::Composition>(type);
1015 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001016}
1017
1018HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001019 supported(__func__);
1020 dataspace_ = static_cast<android_dataspace_t>(dataspace);
1021 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001022}
1023
1024HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001025 supported(__func__);
1026 display_frame_ = frame;
1027 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001028}
1029
1030HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001031 supported(__func__);
1032 alpha_ = alpha;
1033 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001034}
1035
1036HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1037 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001038 supported(__func__);
1039 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001040 return unsupported(__func__, stream);
1041}
1042
1043HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001044 supported(__func__);
1045 source_crop_ = crop;
1046 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001047}
1048
1049HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001050 supported(__func__);
1051 // TODO: We don't use surface damage, marking as unsupported
1052 unsupported(__func__, damage);
1053 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001054}
1055
1056HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001057 supported(__func__);
1058 transform_ = static_cast<HWC2::Transform>(transform);
1059 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001060}
1061
1062HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001063 supported(__func__);
1064 // TODO: We don't use this information, marking as unsupported
1065 unsupported(__func__, visible);
1066 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001067}
1068
Sean Paulac874152016-03-10 16:00:26 -05001069HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1070 supported(__func__);
1071 z_order_ = order;
1072 return HWC2::Error::None;
1073}
1074
1075void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1076 supported(__func__);
1077 switch (blending_) {
1078 case HWC2::BlendMode::None:
1079 layer->blending = DrmHwcBlending::kNone;
1080 break;
1081 case HWC2::BlendMode::Premultiplied:
1082 layer->blending = DrmHwcBlending::kPreMult;
1083 break;
1084 case HWC2::BlendMode::Coverage:
1085 layer->blending = DrmHwcBlending::kCoverage;
1086 break;
1087 default:
1088 ALOGE("Unknown blending mode b=%d", blending_);
1089 layer->blending = DrmHwcBlending::kNone;
1090 break;
1091 }
1092
1093 OutputFd release_fence = release_fence_output();
1094
1095 layer->sf_handle = buffer_;
1096 layer->acquire_fence = acquire_fence_.Release();
1097 layer->release_fence = std::move(release_fence);
1098 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +02001099 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -05001100 layer->SetSourceCrop(source_crop_);
1101 layer->SetTransform(static_cast<int32_t>(transform_));
Matvii Zorin8338c342020-09-08 16:12:51 +03001102 layer->dataspace = dataspace_;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001103}
1104
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001105void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
Roman Stratiienko23701092020-09-26 02:08:41 +03001106 const std::lock_guard<std::mutex> lock(hotplug_callback_lock);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001107
Roman Stratiienko23701092020-09-26 02:08:41 +03001108 if (hotplug_callback_hook_ && hotplug_callback_data_)
1109 hotplug_callback_hook_(hotplug_callback_data_, displayid,
1110 state == DRM_MODE_CONNECTED
1111 ? HWC2_CONNECTION_CONNECTED
1112 : HWC2_CONNECTION_DISCONNECTED);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001113}
1114
1115void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
1116 for (auto &conn : drmDevice->connectors()) {
1117 if (conn->state() != DRM_MODE_CONNECTED)
1118 continue;
1119 HandleDisplayHotplug(conn->display(), conn->state());
1120 }
1121}
1122
1123void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
1124 for (auto &conn : drm_->connectors()) {
1125 drmModeConnection old_state = conn->state();
1126 drmModeConnection cur_state = conn->UpdateModes()
1127 ? DRM_MODE_UNKNOWNCONNECTION
1128 : conn->state();
1129
1130 if (cur_state == old_state)
1131 continue;
1132
1133 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
1134 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1135 conn->id(), conn->display());
1136
1137 int display_id = conn->display();
1138 if (cur_state == DRM_MODE_CONNECTED) {
1139 auto &display = hwc2_->displays_.at(display_id);
1140 display.ChosePreferredConfig();
1141 } else {
1142 auto &display = hwc2_->displays_.at(display_id);
1143 display.ClearDisplay();
1144 }
1145
1146 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1147 }
1148}
1149
Sean Pauled2ec4b2016-03-10 15:35:40 -05001150// static
1151int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1152 unsupported(__func__);
1153 return 0;
1154}
1155
1156// static
1157void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001158 uint32_t *out_count,
1159 int32_t * /*out_capabilities*/) {
1160 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001161 *out_count = 0;
1162}
1163
1164// static
Sean Paulac874152016-03-10 16:00:26 -05001165hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1166 struct hwc2_device * /*dev*/, int32_t descriptor) {
1167 supported(__func__);
1168 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001169 switch (func) {
1170 // Device functions
1171 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1172 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1173 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1174 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001175 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001176 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1177 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1178 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1179 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1180 case HWC2::FunctionDescriptor::Dump:
1181 return ToHook<HWC2_PFN_DUMP>(
1182 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1183 uint32_t *, char *>);
1184 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1185 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1186 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1187 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1188 case HWC2::FunctionDescriptor::RegisterCallback:
1189 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1190 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1191 &DrmHwcTwo::RegisterCallback, int32_t,
1192 hwc2_callback_data_t, hwc2_function_pointer_t>);
1193
1194 // Display functions
1195 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1196 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1197 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1198 &HwcDisplay::AcceptDisplayChanges>);
1199 case HWC2::FunctionDescriptor::CreateLayer:
1200 return ToHook<HWC2_PFN_CREATE_LAYER>(
1201 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1202 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1203 case HWC2::FunctionDescriptor::DestroyLayer:
1204 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1205 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1206 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1207 case HWC2::FunctionDescriptor::GetActiveConfig:
1208 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1209 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1210 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1211 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1212 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1213 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1214 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1215 hwc2_layer_t *, int32_t *>);
1216 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1217 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1218 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1219 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1220 int32_t, int32_t>);
1221 case HWC2::FunctionDescriptor::GetColorModes:
1222 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1223 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1224 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1225 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001226 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1227 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1228 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1229 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001230 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001231 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1232 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1233 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1234 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001235 case HWC2::FunctionDescriptor::GetDisplayName:
1236 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1237 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1238 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1239 case HWC2::FunctionDescriptor::GetDisplayRequests:
1240 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1241 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1242 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1243 hwc2_layer_t *, int32_t *>);
1244 case HWC2::FunctionDescriptor::GetDisplayType:
1245 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1246 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1247 &HwcDisplay::GetDisplayType, int32_t *>);
1248 case HWC2::FunctionDescriptor::GetDozeSupport:
1249 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1250 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1251 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001252 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1253 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1254 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1255 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1256 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001257 case HWC2::FunctionDescriptor::GetReleaseFences:
1258 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1259 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1260 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1261 int32_t *>);
1262 case HWC2::FunctionDescriptor::PresentDisplay:
1263 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1264 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1265 &HwcDisplay::PresentDisplay, int32_t *>);
1266 case HWC2::FunctionDescriptor::SetActiveConfig:
1267 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1268 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1269 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1270 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001271 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1272 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1273 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1274 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001275 case HWC2::FunctionDescriptor::SetColorMode:
1276 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1277 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1278 &HwcDisplay::SetColorMode, int32_t>);
1279 case HWC2::FunctionDescriptor::SetColorTransform:
1280 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1281 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1282 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1283 case HWC2::FunctionDescriptor::SetOutputBuffer:
1284 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1285 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1286 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1287 case HWC2::FunctionDescriptor::SetPowerMode:
1288 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1289 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1290 &HwcDisplay::SetPowerMode, int32_t>);
1291 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1292 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1293 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1294 &HwcDisplay::SetVsyncEnabled, int32_t>);
1295 case HWC2::FunctionDescriptor::ValidateDisplay:
1296 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1297 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1298 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001299#if PLATFORM_SDK_VERSION > 27
1300 case HWC2::FunctionDescriptor::GetRenderIntents:
1301 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
1302 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
1303 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
1304 int32_t *>);
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001305 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
1306 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
1307 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
1308 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001309#endif
John Stultz8c7229d2020-02-07 21:31:08 +00001310#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001311 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1312 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1313 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1314 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1315 uint32_t *, uint8_t *>);
1316 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1317 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1318 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1319 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1320 uint32_t *>);
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001321 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
1322 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
1323 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
1324 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
1325 case HWC2::FunctionDescriptor::SetDisplayBrightness:
1326 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
1327 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
1328 &HwcDisplay::SetDisplayBrightness, float>);
John Stultz8c7229d2020-02-07 21:31:08 +00001329#endif /* PLATFORM_SDK_VERSION > 28 */
Sean Pauled2ec4b2016-03-10 15:35:40 -05001330 // Layer functions
1331 case HWC2::FunctionDescriptor::SetCursorPosition:
1332 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1333 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1334 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1335 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1336 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1337 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1338 &HwcLayer::SetLayerBlendMode, int32_t>);
1339 case HWC2::FunctionDescriptor::SetLayerBuffer:
1340 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1341 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1342 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1343 case HWC2::FunctionDescriptor::SetLayerColor:
1344 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1345 LayerHook<decltype(&HwcLayer::SetLayerColor),
1346 &HwcLayer::SetLayerColor, hwc_color_t>);
1347 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1348 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1349 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1350 &HwcLayer::SetLayerCompositionType, int32_t>);
1351 case HWC2::FunctionDescriptor::SetLayerDataspace:
1352 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1353 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1354 &HwcLayer::SetLayerDataspace, int32_t>);
1355 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1356 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1357 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1358 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1359 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1360 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1361 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1362 &HwcLayer::SetLayerPlaneAlpha, float>);
1363 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001364 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1365 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1366 &HwcLayer::SetLayerSidebandStream,
1367 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001368 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1369 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1370 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1371 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1372 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1373 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1374 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1375 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1376 case HWC2::FunctionDescriptor::SetLayerTransform:
1377 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1378 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1379 &HwcLayer::SetLayerTransform, int32_t>);
1380 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1381 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1382 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1383 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1384 case HWC2::FunctionDescriptor::SetLayerZOrder:
1385 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1386 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1387 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001388 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001389 default:
1390 return NULL;
1391 }
1392}
Sean Paulac874152016-03-10 16:00:26 -05001393
1394// static
1395int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1396 struct hw_device_t **dev) {
1397 supported(__func__);
1398 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1399 ALOGE("Invalid module name- %s", name);
1400 return -EINVAL;
1401 }
1402
1403 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1404 if (!ctx) {
1405 ALOGE("Failed to allocate DrmHwcTwo");
1406 return -ENOMEM;
1407 }
1408
1409 HWC2::Error err = ctx->Init();
1410 if (err != HWC2::Error::None) {
1411 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1412 return -EINVAL;
1413 }
1414
1415 ctx->common.module = const_cast<hw_module_t *>(module);
1416 *dev = &ctx->common;
1417 ctx.release();
1418 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001419}
Sean Paulf72cccd2018-08-27 13:59:08 -04001420} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001421
1422static struct hw_module_methods_t hwc2_module_methods = {
1423 .open = android::DrmHwcTwo::HookDevOpen,
1424};
1425
1426hw_module_t HAL_MODULE_INFO_SYM = {
1427 .tag = HARDWARE_MODULE_TAG,
1428 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1429 .id = HWC_HARDWARE_MODULE_ID,
1430 .name = "DrmHwcTwo module",
1431 .author = "The Android Open Source Project",
1432 .methods = &hwc2_module_methods,
1433 .dso = NULL,
1434 .reserved = {0},
1435};