blob: dc2cd88fc456fd3d790b22887748a35028f6c6fb [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>
Sean Paulf72cccd2018-08-27 13:59:08 -040025#include <log/log.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050026
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020027#include <cinttypes>
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
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020086 auto &drm_devices = resource_manager_.getDrmDevices();
87 for (auto &device : drm_devices) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030088 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) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200106 // TODO(nobody): 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) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200111 // TODO(nobody): 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";
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200119 double ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200120
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"
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200133 << " Composition efficiency: " << ratio)
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200134 .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) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200156 auto copied_bytes = mDumpString.copy(outBuffer, *outSize);
157 *outSize = static_cast<uint32_t>(copied_bytes);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200158 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() {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200173 // TODO(nobody): 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);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200186 auto &drm_devices = resource_manager_.getDrmDevices();
187 for (auto &device : drm_devices)
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300188 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),
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200215 importer_(std::move(importer)),
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100216 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");
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200253 bool use_overlay_planes = strtol(use_overlay_planes_prop, nullptr, 10);
Sean Paulac874152016-03-10 16:00:26 -0500254 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;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200291 HWC2::Error err = GetDisplayConfigs(&num_configs, nullptr);
Sean Paulac874152016-03-10 16:00:26 -0500292 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
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200376 if (dataspace != HAL_DATASPACE_UNKNOWN)
Sean Paulac874152016-03-10 16:00:26 -0500377 return HWC2::Error::Unsupported;
378
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200379 // TODO(nobody): Validate format can be handled by either GL or planes
Sean Paulac874152016-03-10 16:00:26 -0500380 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500381}
382
383HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500384 int32_t *modes) {
385 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800386 if (!modes)
387 *num_modes = 1;
388
389 if (modes)
390 *modes = HAL_COLOR_MODE_NATIVE;
391
392 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500393}
394
395HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500396 int32_t attribute_in,
397 int32_t *value) {
398 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400399 auto mode = std::find_if(connector_->modes().begin(),
400 connector_->modes().end(),
401 [config](DrmMode const &m) {
402 return m.id() == config;
403 });
Sean Paulac874152016-03-10 16:00:26 -0500404 if (mode == connector_->modes().end()) {
405 ALOGE("Could not find active mode for %d", config);
406 return HWC2::Error::BadConfig;
407 }
408
409 static const int32_t kUmPerInch = 25400;
410 uint32_t mm_width = connector_->mm_width();
411 uint32_t mm_height = connector_->mm_height();
412 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
413 switch (attribute) {
414 case HWC2::Attribute::Width:
415 *value = mode->h_display();
416 break;
417 case HWC2::Attribute::Height:
418 *value = mode->v_display();
419 break;
420 case HWC2::Attribute::VsyncPeriod:
421 // in nanoseconds
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200422 *value = 1000.0 * 1000.0 * 1000.0 / mode->v_refresh();
Sean Paulac874152016-03-10 16:00:26 -0500423 break;
424 case HWC2::Attribute::DpiX:
425 // Dots per 1000 inches
426 *value = mm_width ? (mode->h_display() * kUmPerInch) / mm_width : -1;
427 break;
428 case HWC2::Attribute::DpiY:
429 // Dots per 1000 inches
430 *value = mm_height ? (mode->v_display() * kUmPerInch) / mm_height : -1;
431 break;
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300432#if PLATFORM_SDK_VERSION > 29
433 case HWC2::Attribute::ConfigGroup:
434 *value = 0; /* TODO: Add support for config groups */
435 break;
436#endif
Sean Paulac874152016-03-10 16:00:26 -0500437 default:
438 *value = -1;
439 return HWC2::Error::BadConfig;
440 }
441 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500442}
443
444HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
445 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500446 supported(__func__);
447 // Since this callback is normally invoked twice (once to get the count, and
448 // once to populate configs), we don't really want to read the edid
449 // redundantly. Instead, only update the modes on the first invocation. While
450 // it's possible this will result in stale modes, it'll all come out in the
451 // wash when we try to set the active config later.
452 if (!configs) {
453 int ret = connector_->UpdateModes();
454 if (ret) {
455 ALOGE("Failed to update display modes %d", ret);
456 return HWC2::Error::BadDisplay;
457 }
458 }
459
Neil Armstrongb67d0492019-06-20 09:00:21 +0000460 // Since the upper layers only look at vactive/hactive/refresh, height and
461 // width, it doesn't differentiate interlaced from progressive and other
462 // similar modes. Depending on the order of modes we return to SF, it could
463 // end up choosing a suboptimal configuration and dropping the preferred
464 // mode. To workaround this, don't offer interlaced modes to SF if there is
465 // at least one non-interlaced alternative and only offer a single WxH@R
466 // mode with at least the prefered mode from in DrmConnector::UpdateModes()
467
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200468 // TODO(nobody): Remove the following block of code until AOSP handles all
469 // modes
Neil Armstrongb67d0492019-06-20 09:00:21 +0000470 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()) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200488 // TODO(nobody): Remove this when 3D Attributes are in AOSP
Neil Armstrongb67d0492019-06-20 09:00:21 +0000489 if (mode.flags() & DRM_MODE_FLAG_3D_MASK)
490 continue;
491
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200492 // TODO(nobody): Remove this when the Interlaced attribute is in AOSP
Neil Armstrong4c027a72019-06-04 14:48:02 +0000493 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
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200509 // TODO(nobody): Remove this when AOSP handles duplicates modes
Neil Armstrongb67d0492019-06-20 09:00:21 +0000510 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__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200557 // TODO(nobody): I think virtual display should request
Sean Paulac874152016-03-10 16:00:26 -0500558 // 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;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200592 if (layers == nullptr || fences == nullptr)
Sean Paulac874152016-03-10 16:00:26 -0500593 continue;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200594
595 if (num_layers > *num_elements) {
Sean Paulac874152016-03-10 16:00:26 -0500596 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
597 return HWC2::Error::None;
598 }
599
600 layers[num_layers - 1] = l.first;
601 fences[num_layers - 1] = l.second.take_release_fence();
602 }
603 *num_elements = num_layers;
604 return HWC2::Error::None;
605}
606
Matteo Franchinc56eede2019-12-03 17:10:38 +0000607void DrmHwcTwo::HwcDisplay::AddFenceToPresentFence(int fd) {
Sean Paulac874152016-03-10 16:00:26 -0500608 if (fd < 0)
609 return;
610
Matteo Franchinc56eede2019-12-03 17:10:38 +0000611 if (present_fence_.get() >= 0) {
612 int old_fence = present_fence_.get();
613 present_fence_.Set(sync_merge("dc_present", old_fence, fd));
614 close(fd);
Sean Paulac874152016-03-10 16:00:26 -0500615 } else {
Matteo Franchinc56eede2019-12-03 17:10:38 +0000616 present_fence_.Set(fd);
Sean Paulac874152016-03-10 16:00:26 -0500617 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500618}
619
Roman Stratiienkoafb36892019-11-08 17:16:11 +0200620bool DrmHwcTwo::HwcDisplay::HardwareSupportsLayerType(
621 HWC2::Composition comp_type) {
622 return comp_type == HWC2::Composition::Device ||
623 comp_type == HWC2::Composition::Cursor;
624}
625
Rob Herring4f6c62e2018-05-17 14:33:02 -0500626HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500627 std::vector<DrmCompositionDisplayLayersMap> layers_map;
628 layers_map.emplace_back();
629 DrmCompositionDisplayLayersMap &map = layers_map.back();
630
631 map.display = static_cast<int>(handle_);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200632 map.geometry_changed = true; // TODO(nobody): Fix this
Sean Paulac874152016-03-10 16:00:26 -0500633
634 // order the layers by z-order
635 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100636 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500637 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
638 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200639 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500640 case HWC2::Composition::Device:
641 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
642 break;
643 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100644 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500645 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100646 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500647 break;
648 default:
649 continue;
650 }
651 }
652 if (use_client_layer)
653 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
654
Rob Herring4f6c62e2018-05-17 14:33:02 -0500655 if (z_map.empty())
656 return HWC2::Error::BadLayer;
657
Sean Paulac874152016-03-10 16:00:26 -0500658 // now that they're ordered by z, add them to the composition
659 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
660 DrmHwcLayer layer;
661 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200662 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500663 if (ret) {
664 ALOGE("Failed to import layer, ret=%d", ret);
665 return HWC2::Error::NoResources;
666 }
667 map.layers.emplace_back(std::move(layer));
668 }
Sean Paulac874152016-03-10 16:00:26 -0500669
Sean Paulf72cccd2018-08-27 13:59:08 -0400670 std::unique_ptr<DrmDisplayComposition> composition = compositor_
671 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500672 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
673
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200674 // TODO(nobody): Don't always assume geometry changed
Sean Paulac874152016-03-10 16:00:26 -0500675 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
676 if (ret) {
677 ALOGE("Failed to set layers in the composition ret=%d", ret);
678 return HWC2::Error::BadLayer;
679 }
680
681 std::vector<DrmPlane *> primary_planes(primary_planes_);
682 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500683 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500684 if (ret) {
685 ALOGE("Failed to plan the composition ret=%d", ret);
686 return HWC2::Error::BadConfig;
687 }
688
689 // Disable the planes we're not using
690 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
691 composition->AddPlaneDisable(*i);
692 i = primary_planes.erase(i);
693 }
694 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
695 composition->AddPlaneDisable(*i);
696 i = overlay_planes.erase(i);
697 }
698
Rob Herring4f6c62e2018-05-17 14:33:02 -0500699 if (test) {
700 ret = compositor_.TestComposition(composition.get());
701 } else {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500702 ret = compositor_.ApplyComposition(std::move(composition));
Matteo Franchinc56eede2019-12-03 17:10:38 +0000703 AddFenceToPresentFence(compositor_.TakeOutFence());
Rob Herring4f6c62e2018-05-17 14:33:02 -0500704 }
Sean Paulac874152016-03-10 16:00:26 -0500705 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700706 if (!test)
707 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500708 return HWC2::Error::BadParameter;
709 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500710 return HWC2::Error::None;
711}
712
Matteo Franchinc56eede2019-12-03 17:10:38 +0000713HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500714 supported(__func__);
715 HWC2::Error ret;
716
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200717 ++total_stats_.total_frames_;
718
Rob Herring4f6c62e2018-05-17 14:33:02 -0500719 ret = CreateComposition(false);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200720 if (ret != HWC2::Error::None)
721 ++total_stats_.failed_kms_present_;
722
Rob Herring4f6c62e2018-05-17 14:33:02 -0500723 if (ret == HWC2::Error::BadLayer) {
724 // Can we really have no client or device layers?
Matteo Franchinc56eede2019-12-03 17:10:38 +0000725 *present_fence = -1;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500726 return HWC2::Error::None;
727 }
728 if (ret != HWC2::Error::None)
729 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500730
Matteo Franchinc56eede2019-12-03 17:10:38 +0000731 *present_fence = present_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500732
733 ++frame_no_;
734 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500735}
736
737HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500738 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400739 auto mode = std::find_if(connector_->modes().begin(),
740 connector_->modes().end(),
741 [config](DrmMode const &m) {
742 return m.id() == config;
743 });
Sean Paulac874152016-03-10 16:00:26 -0500744 if (mode == connector_->modes().end()) {
745 ALOGE("Could not find active mode for %d", config);
746 return HWC2::Error::BadConfig;
747 }
748
Sean Paulf72cccd2018-08-27 13:59:08 -0400749 std::unique_ptr<DrmDisplayComposition> composition = compositor_
750 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500751 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
752 int ret = composition->SetDisplayMode(*mode);
Roman Stratiienko6a10c4c2021-02-15 11:25:23 +0200753 if (ret) {
754 return HWC2::Error::BadConfig;
755 }
Sean Pauled45a8e2017-02-28 13:17:34 -0500756 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500757 if (ret) {
758 ALOGE("Failed to queue dpms composition on %d", ret);
759 return HWC2::Error::BadConfig;
760 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300761
762 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500763
764 // Setup the client layer's dimensions
765 hwc_rect_t display_frame = {.left = 0,
766 .top = 0,
767 .right = static_cast<int>(mode->h_display()),
768 .bottom = static_cast<int>(mode->v_display())};
769 client_layer_.SetLayerDisplayFrame(display_frame);
Sean Paulac874152016-03-10 16:00:26 -0500770
771 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500772}
773
774HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
775 int32_t acquire_fence,
776 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600777 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500778 supported(__func__);
779 UniqueFd uf(acquire_fence);
780
781 client_layer_.set_buffer(target);
782 client_layer_.set_acquire_fence(uf.get());
783 client_layer_.SetLayerDataspace(dataspace);
Roman Stratiienko33365c22020-10-10 23:06:36 +0300784
785 /* TODO: Do not update source_crop every call.
786 * It makes sense to do it once after every hotplug event. */
787 hwc_drm_bo bo{};
788 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
789
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200790 hwc_frect_t source_crop = {.left = 0.0F,
791 .top = 0.0F,
792 .right = bo.width + 0.0F,
793 .bottom = bo.height + 0.0F};
Roman Stratiienko33365c22020-10-10 23:06:36 +0300794 client_layer_.SetLayerSourceCrop(source_crop);
795
Sean Paulac874152016-03-10 16:00:26 -0500796 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500797}
798
799HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500800 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800801
Roman Stratiienkod146d6d2020-10-04 23:56:46 +0300802 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
803 return HWC2::Error::BadParameter;
804
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800805 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +0300806 return HWC2::Error::Unsupported;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800807
808 color_mode_ = mode;
809 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500810}
811
812HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500813 int32_t hint) {
814 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200815 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
816 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
817 return HWC2::Error::BadParameter;
818
819 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
820 return HWC2::Error::BadParameter;
821
822 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
823 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
824 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
825
826 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500827}
828
829HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500830 int32_t release_fence) {
831 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200832 // TODO(nobody): Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500833 return unsupported(__func__, buffer, release_fence);
834}
835
Sean Paulac874152016-03-10 16:00:26 -0500836HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
837 supported(__func__);
838 uint64_t dpms_value = 0;
839 auto mode = static_cast<HWC2::PowerMode>(mode_in);
840 switch (mode) {
841 case HWC2::PowerMode::Off:
842 dpms_value = DRM_MODE_DPMS_OFF;
843 break;
844 case HWC2::PowerMode::On:
845 dpms_value = DRM_MODE_DPMS_ON;
846 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100847 case HWC2::PowerMode::Doze:
848 case HWC2::PowerMode::DozeSuspend:
849 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500850 default:
851 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100852 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500853 };
854
Sean Paulf72cccd2018-08-27 13:59:08 -0400855 std::unique_ptr<DrmDisplayComposition> composition = compositor_
856 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500857 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
858 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500859 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500860 if (ret) {
861 ALOGE("Failed to apply the dpms composition ret=%d", ret);
862 return HWC2::Error::BadParameter;
863 }
864 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500865}
866
867HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500868 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300869 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500870 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500871}
872
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200873uint32_t DrmHwcTwo::HwcDisplay::CalcPixOps(
874 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t first_z,
875 size_t size) {
876 uint32_t pixops = 0;
877 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
878 if (l.first >= first_z && l.first < first_z + size) {
879 hwc_rect_t df = l.second->display_frame();
880 pixops += (df.right - df.left) * (df.bottom - df.top);
881 }
882 }
883 return pixops;
884}
885
886void DrmHwcTwo::HwcDisplay::MarkValidated(
887 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t client_first_z,
888 size_t client_size) {
889 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
890 if (l.first >= client_first_z && l.first < client_first_z + client_size)
891 l.second->set_validated_type(HWC2::Composition::Client);
892 else
893 l.second->set_validated_type(HWC2::Composition::Device);
894 }
895}
896
Sean Pauled2ec4b2016-03-10 15:35:40 -0500897HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500898 uint32_t *num_requests) {
899 supported(__func__);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500900
Matvii Zorinef3c7972020-08-11 15:15:44 +0300901 return backend_->ValidateDisplay(this, num_types, num_requests);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500902}
903
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300904#if PLATFORM_SDK_VERSION > 29
905HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
906 if (connector_->internal())
907 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
908 else if (connector_->external())
909 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
910 else
911 return HWC2::Error::BadConfig;
912
913 return HWC2::Error::None;
914}
915
916HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayVsyncPeriod(
917 hwc2_vsync_period_t *outVsyncPeriod /* ns */) {
918 supported(__func__);
919 DrmMode const &mode = connector_->active_mode();
920 if (mode.id() == 0)
921 return HWC2::Error::BadConfig;
922
923 *outVsyncPeriod = 1E9 / mode.v_refresh();
924 return HWC2::Error::None;
925}
926
927HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfigWithConstraints(
928 hwc2_config_t /*config*/,
929 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
930 hwc_vsync_period_change_timeline_t *outTimeline) {
931 supported(__func__);
932
933 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
934 return HWC2::Error::BadParameter;
935 }
936
937 return HWC2::Error::BadConfig;
938}
939
940HWC2::Error DrmHwcTwo::HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
941 return HWC2::Error::Unsupported;
942}
943
944HWC2::Error DrmHwcTwo::HwcDisplay::GetSupportedContentTypes(
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200945 uint32_t *outNumSupportedContentTypes,
946 const uint32_t *outSupportedContentTypes) {
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300947 if (outSupportedContentTypes == nullptr)
948 *outNumSupportedContentTypes = 0;
949
950 return HWC2::Error::None;
951}
952
953HWC2::Error DrmHwcTwo::HwcDisplay::SetContentType(int32_t contentType) {
954 supported(__func__);
955
956 if (contentType != HWC2_CONTENT_TYPE_NONE)
957 return HWC2::Error::Unsupported;
958
959 /* TODO: Map to the DRM Connector property:
960 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
961 */
962
963 return HWC2::Error::None;
964}
965#endif
966
John Stultz8c7229d2020-02-07 21:31:08 +0000967#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800968HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
969 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
970 supported(__func__);
971
972 drmModePropertyBlobPtr blob;
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800973
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300974 if (connector_->GetEdidBlob(blob)) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800975 ALOGE("Failed to get edid property value.");
976 return HWC2::Error::Unsupported;
977 }
978
Andrii Chepurnyi8115dbe2020-04-14 13:03:57 +0300979 if (outData) {
980 *outDataSize = std::min(*outDataSize, blob->length);
981 memcpy(outData, blob->data, *outDataSize);
982 } else {
983 *outDataSize = blob->length;
984 }
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800985 *outPort = connector_->id();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800986
987 return HWC2::Error::None;
988}
989
990HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
991 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
992 unsupported(__func__, outCapabilities);
993
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200994 if (outNumCapabilities == nullptr) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800995 return HWC2::Error::BadParameter;
996 }
997
998 *outNumCapabilities = 0;
999
1000 return HWC2::Error::None;
1001}
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001002
1003HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayBrightnessSupport(
1004 bool *supported) {
1005 *supported = false;
1006 return HWC2::Error::None;
1007}
1008
1009HWC2::Error DrmHwcTwo::HwcDisplay::SetDisplayBrightness(
1010 float /* brightness */) {
1011 return HWC2::Error::Unsupported;
1012}
1013
John Stultz8c7229d2020-02-07 21:31:08 +00001014#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001015
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001016#if PLATFORM_SDK_VERSION > 27
1017
1018HWC2::Error DrmHwcTwo::HwcDisplay::GetRenderIntents(
1019 int32_t mode, uint32_t *outNumIntents,
1020 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1021 if (mode != HAL_COLOR_MODE_NATIVE) {
1022 return HWC2::Error::BadParameter;
1023 }
1024
1025 if (outIntents == nullptr) {
1026 *outNumIntents = 1;
1027 return HWC2::Error::None;
1028 }
1029 *outNumIntents = 1;
1030 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1031 return HWC2::Error::None;
1032}
1033
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001034HWC2::Error DrmHwcTwo::HwcDisplay::SetColorModeWithIntent(int32_t mode,
1035 int32_t intent) {
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001036 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1037 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1038 return HWC2::Error::BadParameter;
1039
1040 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
1041 return HWC2::Error::BadParameter;
1042
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001043 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001044 return HWC2::Error::Unsupported;
1045
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001046 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001047 return HWC2::Error::Unsupported;
1048
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001049 color_mode_ = mode;
1050 return HWC2::Error::None;
1051}
1052
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001053#endif /* PLATFORM_SDK_VERSION > 27 */
1054
Sean Pauled2ec4b2016-03-10 15:35:40 -05001055HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -05001056 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -08001057 cursor_x_ = x;
1058 cursor_y_ = y;
1059 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001060}
1061
1062HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -05001063 supported(__func__);
1064 blending_ = static_cast<HWC2::BlendMode>(mode);
1065 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001066}
1067
1068HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001069 int32_t acquire_fence) {
1070 supported(__func__);
1071 UniqueFd uf(acquire_fence);
1072
Sean Paulac874152016-03-10 16:00:26 -05001073 set_buffer(buffer);
1074 set_acquire_fence(uf.get());
1075 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001076}
1077
1078HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001079 // TODO(nobody): Put to client composition here?
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001080 supported(__func__);
1081 layer_color_ = color;
1082 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001083}
1084
1085HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001086 sf_type_ = static_cast<HWC2::Composition>(type);
1087 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001088}
1089
1090HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001091 supported(__func__);
1092 dataspace_ = static_cast<android_dataspace_t>(dataspace);
1093 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001094}
1095
1096HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001097 supported(__func__);
1098 display_frame_ = frame;
1099 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001100}
1101
1102HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001103 supported(__func__);
1104 alpha_ = alpha;
1105 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001106}
1107
1108HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1109 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001110 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001111 // TODO(nobody): We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001112 return unsupported(__func__, stream);
1113}
1114
1115HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001116 supported(__func__);
1117 source_crop_ = crop;
1118 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001119}
1120
1121HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001122 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001123 // TODO(nobody): We don't use surface damage, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001124 unsupported(__func__, damage);
1125 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001126}
1127
1128HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001129 supported(__func__);
1130 transform_ = static_cast<HWC2::Transform>(transform);
1131 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001132}
1133
1134HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001135 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001136 // TODO(nobody): We don't use this information, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001137 unsupported(__func__, visible);
1138 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001139}
1140
Sean Paulac874152016-03-10 16:00:26 -05001141HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1142 supported(__func__);
1143 z_order_ = order;
1144 return HWC2::Error::None;
1145}
1146
1147void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1148 supported(__func__);
1149 switch (blending_) {
1150 case HWC2::BlendMode::None:
1151 layer->blending = DrmHwcBlending::kNone;
1152 break;
1153 case HWC2::BlendMode::Premultiplied:
1154 layer->blending = DrmHwcBlending::kPreMult;
1155 break;
1156 case HWC2::BlendMode::Coverage:
1157 layer->blending = DrmHwcBlending::kCoverage;
1158 break;
1159 default:
1160 ALOGE("Unknown blending mode b=%d", blending_);
1161 layer->blending = DrmHwcBlending::kNone;
1162 break;
1163 }
1164
1165 OutputFd release_fence = release_fence_output();
1166
1167 layer->sf_handle = buffer_;
1168 layer->acquire_fence = acquire_fence_.Release();
1169 layer->release_fence = std::move(release_fence);
1170 layer->SetDisplayFrame(display_frame_);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001171 layer->alpha = lround(65535.0F * alpha_);
Sean Paulac874152016-03-10 16:00:26 -05001172 layer->SetSourceCrop(source_crop_);
1173 layer->SetTransform(static_cast<int32_t>(transform_));
Matvii Zorin8338c342020-09-08 16:12:51 +03001174 layer->dataspace = dataspace_;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001175}
1176
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001177void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
Roman Stratiienko23701092020-09-26 02:08:41 +03001178 const std::lock_guard<std::mutex> lock(hotplug_callback_lock);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001179
Roman Stratiienko23701092020-09-26 02:08:41 +03001180 if (hotplug_callback_hook_ && hotplug_callback_data_)
1181 hotplug_callback_hook_(hotplug_callback_data_, displayid,
1182 state == DRM_MODE_CONNECTED
1183 ? HWC2_CONNECTION_CONNECTED
1184 : HWC2_CONNECTION_DISCONNECTED);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001185}
1186
1187void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
1188 for (auto &conn : drmDevice->connectors()) {
1189 if (conn->state() != DRM_MODE_CONNECTED)
1190 continue;
1191 HandleDisplayHotplug(conn->display(), conn->state());
1192 }
1193}
1194
1195void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
1196 for (auto &conn : drm_->connectors()) {
1197 drmModeConnection old_state = conn->state();
1198 drmModeConnection cur_state = conn->UpdateModes()
1199 ? DRM_MODE_UNKNOWNCONNECTION
1200 : conn->state();
1201
1202 if (cur_state == old_state)
1203 continue;
1204
1205 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
1206 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1207 conn->id(), conn->display());
1208
1209 int display_id = conn->display();
1210 if (cur_state == DRM_MODE_CONNECTED) {
1211 auto &display = hwc2_->displays_.at(display_id);
1212 display.ChosePreferredConfig();
1213 } else {
1214 auto &display = hwc2_->displays_.at(display_id);
1215 display.ClearDisplay();
1216 }
1217
1218 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1219 }
1220}
1221
Sean Pauled2ec4b2016-03-10 15:35:40 -05001222// static
1223int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1224 unsupported(__func__);
1225 return 0;
1226}
1227
1228// static
1229void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001230 uint32_t *out_count,
1231 int32_t * /*out_capabilities*/) {
1232 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001233 *out_count = 0;
1234}
1235
1236// static
Sean Paulac874152016-03-10 16:00:26 -05001237hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1238 struct hwc2_device * /*dev*/, int32_t descriptor) {
1239 supported(__func__);
1240 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001241 switch (func) {
1242 // Device functions
1243 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1244 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1245 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1246 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001247 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001248 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1249 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1250 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1251 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1252 case HWC2::FunctionDescriptor::Dump:
1253 return ToHook<HWC2_PFN_DUMP>(
1254 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1255 uint32_t *, char *>);
1256 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1257 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1258 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1259 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1260 case HWC2::FunctionDescriptor::RegisterCallback:
1261 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1262 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1263 &DrmHwcTwo::RegisterCallback, int32_t,
1264 hwc2_callback_data_t, hwc2_function_pointer_t>);
1265
1266 // Display functions
1267 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1268 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1269 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1270 &HwcDisplay::AcceptDisplayChanges>);
1271 case HWC2::FunctionDescriptor::CreateLayer:
1272 return ToHook<HWC2_PFN_CREATE_LAYER>(
1273 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1274 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1275 case HWC2::FunctionDescriptor::DestroyLayer:
1276 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1277 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1278 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1279 case HWC2::FunctionDescriptor::GetActiveConfig:
1280 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1281 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1282 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1283 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1284 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1285 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1286 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1287 hwc2_layer_t *, int32_t *>);
1288 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1289 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1290 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1291 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1292 int32_t, int32_t>);
1293 case HWC2::FunctionDescriptor::GetColorModes:
1294 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1295 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1296 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1297 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001298 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1299 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1300 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1301 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001302 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001303 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1304 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1305 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1306 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001307 case HWC2::FunctionDescriptor::GetDisplayName:
1308 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1309 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1310 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1311 case HWC2::FunctionDescriptor::GetDisplayRequests:
1312 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1313 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1314 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1315 hwc2_layer_t *, int32_t *>);
1316 case HWC2::FunctionDescriptor::GetDisplayType:
1317 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1318 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1319 &HwcDisplay::GetDisplayType, int32_t *>);
1320 case HWC2::FunctionDescriptor::GetDozeSupport:
1321 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1322 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1323 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001324 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1325 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1326 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1327 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1328 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001329 case HWC2::FunctionDescriptor::GetReleaseFences:
1330 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1331 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1332 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1333 int32_t *>);
1334 case HWC2::FunctionDescriptor::PresentDisplay:
1335 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1336 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1337 &HwcDisplay::PresentDisplay, int32_t *>);
1338 case HWC2::FunctionDescriptor::SetActiveConfig:
1339 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1340 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1341 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1342 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001343 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1344 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1345 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1346 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001347 case HWC2::FunctionDescriptor::SetColorMode:
1348 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1349 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1350 &HwcDisplay::SetColorMode, int32_t>);
1351 case HWC2::FunctionDescriptor::SetColorTransform:
1352 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1353 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1354 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1355 case HWC2::FunctionDescriptor::SetOutputBuffer:
1356 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1357 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1358 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1359 case HWC2::FunctionDescriptor::SetPowerMode:
1360 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1361 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1362 &HwcDisplay::SetPowerMode, int32_t>);
1363 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1364 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1365 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1366 &HwcDisplay::SetVsyncEnabled, int32_t>);
1367 case HWC2::FunctionDescriptor::ValidateDisplay:
1368 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1369 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1370 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001371#if PLATFORM_SDK_VERSION > 27
1372 case HWC2::FunctionDescriptor::GetRenderIntents:
1373 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
1374 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
1375 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
1376 int32_t *>);
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001377 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
1378 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
1379 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
1380 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001381#endif
John Stultz8c7229d2020-02-07 21:31:08 +00001382#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001383 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1384 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1385 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1386 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1387 uint32_t *, uint8_t *>);
1388 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1389 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1390 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1391 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1392 uint32_t *>);
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001393 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
1394 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
1395 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
1396 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
1397 case HWC2::FunctionDescriptor::SetDisplayBrightness:
1398 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
1399 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
1400 &HwcDisplay::SetDisplayBrightness, float>);
John Stultz8c7229d2020-02-07 21:31:08 +00001401#endif /* PLATFORM_SDK_VERSION > 28 */
Roman Stratiienko6f5df172020-09-27 22:48:08 +03001402#if PLATFORM_SDK_VERSION > 29
1403 case HWC2::FunctionDescriptor::GetDisplayConnectionType:
1404 return ToHook<HWC2_PFN_GET_DISPLAY_CONNECTION_TYPE>(
1405 DisplayHook<decltype(&HwcDisplay::GetDisplayConnectionType),
1406 &HwcDisplay::GetDisplayConnectionType, uint32_t *>);
1407 case HWC2::FunctionDescriptor::GetDisplayVsyncPeriod:
1408 return ToHook<HWC2_PFN_GET_DISPLAY_VSYNC_PERIOD>(
1409 DisplayHook<decltype(&HwcDisplay::GetDisplayVsyncPeriod),
1410 &HwcDisplay::GetDisplayVsyncPeriod,
1411 hwc2_vsync_period_t *>);
1412 case HWC2::FunctionDescriptor::SetActiveConfigWithConstraints:
1413 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG_WITH_CONSTRAINTS>(
1414 DisplayHook<decltype(&HwcDisplay::SetActiveConfigWithConstraints),
1415 &HwcDisplay::SetActiveConfigWithConstraints,
1416 hwc2_config_t, hwc_vsync_period_change_constraints_t *,
1417 hwc_vsync_period_change_timeline_t *>);
1418 case HWC2::FunctionDescriptor::SetAutoLowLatencyMode:
1419 return ToHook<HWC2_PFN_SET_AUTO_LOW_LATENCY_MODE>(
1420 DisplayHook<decltype(&HwcDisplay::SetAutoLowLatencyMode),
1421 &HwcDisplay::SetAutoLowLatencyMode, bool>);
1422 case HWC2::FunctionDescriptor::GetSupportedContentTypes:
1423 return ToHook<HWC2_PFN_GET_SUPPORTED_CONTENT_TYPES>(
1424 DisplayHook<decltype(&HwcDisplay::GetSupportedContentTypes),
1425 &HwcDisplay::GetSupportedContentTypes, uint32_t *,
1426 uint32_t *>);
1427 case HWC2::FunctionDescriptor::SetContentType:
1428 return ToHook<HWC2_PFN_SET_CONTENT_TYPE>(
1429 DisplayHook<decltype(&HwcDisplay::SetContentType),
1430 &HwcDisplay::SetContentType, int32_t>);
1431#endif
Sean Pauled2ec4b2016-03-10 15:35:40 -05001432 // Layer functions
1433 case HWC2::FunctionDescriptor::SetCursorPosition:
1434 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1435 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1436 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1437 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1438 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1439 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1440 &HwcLayer::SetLayerBlendMode, int32_t>);
1441 case HWC2::FunctionDescriptor::SetLayerBuffer:
1442 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1443 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1444 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1445 case HWC2::FunctionDescriptor::SetLayerColor:
1446 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1447 LayerHook<decltype(&HwcLayer::SetLayerColor),
1448 &HwcLayer::SetLayerColor, hwc_color_t>);
1449 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1450 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1451 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1452 &HwcLayer::SetLayerCompositionType, int32_t>);
1453 case HWC2::FunctionDescriptor::SetLayerDataspace:
1454 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1455 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1456 &HwcLayer::SetLayerDataspace, int32_t>);
1457 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1458 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1459 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1460 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1461 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1462 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1463 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1464 &HwcLayer::SetLayerPlaneAlpha, float>);
1465 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001466 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1467 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1468 &HwcLayer::SetLayerSidebandStream,
1469 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001470 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1471 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1472 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1473 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1474 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1475 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1476 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1477 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1478 case HWC2::FunctionDescriptor::SetLayerTransform:
1479 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1480 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1481 &HwcLayer::SetLayerTransform, int32_t>);
1482 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1483 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1484 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1485 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1486 case HWC2::FunctionDescriptor::SetLayerZOrder:
1487 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1488 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1489 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001490 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001491 default:
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001492 return nullptr;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001493 }
1494}
Sean Paulac874152016-03-10 16:00:26 -05001495
1496// static
1497int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1498 struct hw_device_t **dev) {
1499 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001500 if (strcmp(name, HWC_HARDWARE_COMPOSER) != 0) {
Sean Paulac874152016-03-10 16:00:26 -05001501 ALOGE("Invalid module name- %s", name);
1502 return -EINVAL;
1503 }
1504
1505 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1506 if (!ctx) {
1507 ALOGE("Failed to allocate DrmHwcTwo");
1508 return -ENOMEM;
1509 }
1510
1511 HWC2::Error err = ctx->Init();
1512 if (err != HWC2::Error::None) {
1513 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1514 return -EINVAL;
1515 }
1516
1517 ctx->common.module = const_cast<hw_module_t *>(module);
1518 *dev = &ctx->common;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001519 ctx.release(); // NOLINT(bugprone-unused-return-value)
Sean Paulac874152016-03-10 16:00:26 -05001520 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001521}
Sean Paulf72cccd2018-08-27 13:59:08 -04001522} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001523
1524static struct hw_module_methods_t hwc2_module_methods = {
1525 .open = android::DrmHwcTwo::HookDevOpen,
1526};
1527
1528hw_module_t HAL_MODULE_INFO_SYM = {
1529 .tag = HARDWARE_MODULE_TAG,
1530 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1531 .id = HWC_HARDWARE_MODULE_ID,
1532 .name = "DrmHwcTwo module",
1533 .author = "The Android Open Source Project",
1534 .methods = &hwc2_module_methods,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001535 .dso = nullptr,
Sean Paulac874152016-03-10 16:00:26 -05001536 .reserved = {0},
1537};