blob: 968d3ce0570d8ee6fb30b25622f0c4fde1192114 [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
Roman Stratiienko0fade372021-02-20 13:59:55 +020022#include <fcntl.h>
Sean Paulac874152016-03-10 16:00:26 -050023#include <hardware/hardware.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050024#include <hardware/hwcomposer2.h>
Roman Stratiienko74774712021-02-05 16:32:47 +020025#include <sync/sync.h>
Roman Stratiienko0fade372021-02-20 13:59:55 +020026#include <unistd.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050027
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020028#include <cinttypes>
Roman Stratiienkod21071f2021-03-09 21:56:50 +020029#include <iostream>
30#include <sstream>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030031#include <string>
32
Roman Stratiienko13cc3662020-08-29 21:35:39 +030033#include "backend/BackendManager.h"
Roman Stratiienko33365c22020-10-10 23:06:36 +030034#include "bufferinfo/BufferInfoGetter.h"
Roman Stratiienko13cc3662020-08-29 21:35:39 +030035#include "compositor/DrmDisplayComposition.h"
Roman Stratiienkod21071f2021-03-09 21:56:50 +020036#include "utils/log.h"
37#include "utils/properties.h"
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030038
Sean Pauled2ec4b2016-03-10 15:35:40 -050039namespace android {
40
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020041DrmHwcTwo::DrmHwcTwo() : hwc2_device() {
Sean Paulac874152016-03-10 16:00:26 -050042 common.tag = HARDWARE_DEVICE_TAG;
43 common.version = HWC_DEVICE_API_VERSION_2_0;
Sean Pauled2ec4b2016-03-10 15:35:40 -050044 common.close = HookDevClose;
45 getCapabilities = HookDevGetCapabilities;
46 getFunction = HookDevGetFunction;
47}
48
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030049HWC2::Error DrmHwcTwo::CreateDisplay(hwc2_display_t displ,
50 HWC2::DisplayType type) {
Roman Stratiienkod26619b2021-08-04 19:55:37 +030051 DrmDevice *drm = resource_manager_.GetDrmDevice(static_cast<int>(displ));
Roman Stratiienko8666dc92021-02-09 17:49:55 +020052 if (!drm) {
53 ALOGE("Failed to get a valid drmresource");
Sean Paulac874152016-03-10 16:00:26 -050054 return HWC2::Error::NoResources;
55 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030056 displays_.emplace(std::piecewise_construct, std::forward_as_tuple(displ),
Roman Stratiienko8666dc92021-02-09 17:49:55 +020057 std::forward_as_tuple(&resource_manager_, drm, displ,
58 type));
Sean Paulac874152016-03-10 16:00:26 -050059
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030060 DrmCrtc *crtc = drm->GetCrtcForDisplay(static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050061 if (!crtc) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030062 ALOGE("Failed to get crtc for display %d", static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050063 return HWC2::Error::BadDisplay;
64 }
Roman Stratiienkod21071f2021-03-09 21:56:50 +020065 auto display_planes = std::vector<DrmPlane *>();
66 for (const auto &plane : drm->planes()) {
Sean Paulac874152016-03-10 16:00:26 -050067 if (plane->GetCrtcSupported(*crtc))
68 display_planes.push_back(plane.get());
69 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030070 displays_.at(displ).Init(&display_planes);
Sean Paulac874152016-03-10 16:00:26 -050071 return HWC2::Error::None;
72}
73
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030074HWC2::Error DrmHwcTwo::Init() {
75 int rv = resource_manager_.Init();
76 if (rv) {
77 ALOGE("Can't initialize the resource manager %d", rv);
78 return HWC2::Error::NoResources;
79 }
80
81 HWC2::Error ret = HWC2::Error::None;
82 for (int i = 0; i < resource_manager_.getDisplayCount(); i++) {
83 ret = CreateDisplay(i, HWC2::DisplayType::Physical);
84 if (ret != HWC2::Error::None) {
85 ALOGE("Failed to create display %d with error %d", i, ret);
86 return ret;
87 }
88 }
89
Roman Stratiienkod21071f2021-03-09 21:56:50 +020090 const auto &drm_devices = resource_manager_.getDrmDevices();
91 for (const auto &device : drm_devices) {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020092 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030093 device->RegisterHotplugHandler(new DrmHotplugHandler(this, device.get()));
94 }
95 return ret;
96}
97
Sean Pauled2ec4b2016-03-10 15:35:40 -050098template <typename... Args>
99static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
100 ALOGV("Unsupported function: %s", func);
101 return HWC2::Error::Unsupported;
102}
103
Sean Paulac874152016-03-10 16:00:26 -0500104static inline void supported(char const *func) {
105 ALOGV("Supported function: %s", func);
106}
107
Sean Pauled2ec4b2016-03-10 15:35:40 -0500108HWC2::Error DrmHwcTwo::CreateVirtualDisplay(uint32_t width, uint32_t height,
109 int32_t *format,
110 hwc2_display_t *display) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200111 // TODO(nobody): Implement virtual display
Sean Paulac874152016-03-10 16:00:26 -0500112 return unsupported(__func__, width, height, format, display);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500113}
114
115HWC2::Error DrmHwcTwo::DestroyVirtualDisplay(hwc2_display_t display) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200116 // TODO(nobody): Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500117 return unsupported(__func__, display);
118}
119
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200120std::string DrmHwcTwo::HwcDisplay::DumpDelta(
121 DrmHwcTwo::HwcDisplay::Stats delta) {
122 if (delta.total_pixops_ == 0)
123 return "No stats yet";
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200124 double ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200125
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200126 std::stringstream ss;
127 ss << " Total frames count: " << delta.total_frames_ << "\n"
128 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
129 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
130 << ((delta.failed_kms_present_ > 0)
131 ? " !!! Internal failure, FIX it please\n"
132 : "")
133 << " Flattened frames: " << delta.frames_flattened_ << "\n"
134 << " Pixel operations (free units)"
135 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
136 << "]\n"
137 << " Composition efficiency: " << ratio;
138
139 return ss.str();
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200140}
141
142std::string DrmHwcTwo::HwcDisplay::Dump() {
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200143 std::stringstream ss;
144 ss << "- Display on: " << connector_->name() << "\n"
145 << " Flattening state: " << compositor_.GetFlatteningState() << "\n"
146 << "Statistics since system boot:\n"
147 << DumpDelta(total_stats_) << "\n\n"
148 << "Statistics since last dumpsys request:\n"
149 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200150
151 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200152 return ss.str();
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200153}
154
155void DrmHwcTwo::Dump(uint32_t *outSize, char *outBuffer) {
156 supported(__func__);
157
158 if (outBuffer != nullptr) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200159 auto copied_bytes = mDumpString.copy(outBuffer, *outSize);
160 *outSize = static_cast<uint32_t>(copied_bytes);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200161 return;
162 }
163
164 std::stringstream output;
165
166 output << "-- drm_hwcomposer --\n\n";
167
168 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &dp : displays_)
169 output << dp.second.Dump();
170
171 mDumpString = output.str();
172 *outSize = static_cast<uint32_t>(mDumpString.size());
Sean Pauled2ec4b2016-03-10 15:35:40 -0500173}
174
175uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200176 // TODO(nobody): Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500177 unsupported(__func__);
178 return 0;
179}
180
181HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor,
Sean Paulac874152016-03-10 16:00:26 -0500182 hwc2_callback_data_t data,
183 hwc2_function_pointer_t function) {
184 supported(__func__);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300185
Roman Stratiienko23701092020-09-26 02:08:41 +0300186 switch (static_cast<HWC2::Callback>(descriptor)) {
Sean Paulac874152016-03-10 16:00:26 -0500187 case HWC2::Callback::Hotplug: {
Roman Stratiienko23701092020-09-26 02:08:41 +0300188 SetHotplugCallback(data, function);
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200189 const auto &drm_devices = resource_manager_.getDrmDevices();
190 for (const auto &device : drm_devices)
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300191 HandleInitialHotplugState(device.get());
Sean Paulac874152016-03-10 16:00:26 -0500192 break;
193 }
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200194 case HWC2::Callback::Refresh: {
195 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
196 displays_)
197 d.second.RegisterRefreshCallback(data, function);
198 break;
199 }
Sean Paulac874152016-03-10 16:00:26 -0500200 case HWC2::Callback::Vsync: {
201 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
202 displays_)
203 d.second.RegisterVsyncCallback(data, function);
204 break;
205 }
206 default:
207 break;
208 }
209 return HWC2::Error::None;
210}
211
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100212DrmHwcTwo::HwcDisplay::HwcDisplay(ResourceManager *resource_manager,
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200213 DrmDevice *drm, hwc2_display_t handle,
214 HWC2::DisplayType type)
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100215 : resource_manager_(resource_manager),
216 drm_(drm),
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100217 handle_(handle),
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200218 type_(type),
219 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
Sean Paulac874152016-03-10 16:00:26 -0500220 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200221
222 // clang-format off
223 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
224 0.0, 1.0, 0.0, 0.0,
225 0.0, 0.0, 1.0, 0.0,
226 0.0, 0.0, 0.0, 1.0};
227 // clang-format on
Sean Paulac874152016-03-10 16:00:26 -0500228}
229
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300230void DrmHwcTwo::HwcDisplay::ClearDisplay() {
231 compositor_.ClearDisplay();
232}
233
Sean Paulac874152016-03-10 16:00:26 -0500234HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
235 supported(__func__);
236 planner_ = Planner::CreateInstance(drm_);
237 if (!planner_) {
238 ALOGE("Failed to create planner instance for composition");
239 return HWC2::Error::NoResources;
240 }
241
242 int display = static_cast<int>(handle_);
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100243 int ret = compositor_.Init(resource_manager_, display);
Sean Paulac874152016-03-10 16:00:26 -0500244 if (ret) {
245 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
246 return HWC2::Error::NoResources;
247 }
248
249 // Split up the given display planes into primary and overlay to properly
250 // interface with the composition
251 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
Jason Macnakf1af9572020-08-20 11:49:51 -0700252 property_get("vendor.hwc.drm.use_overlay_planes", use_overlay_planes_prop,
253 "1");
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200254 bool use_overlay_planes = strtol(use_overlay_planes_prop, nullptr, 10);
Sean Paulac874152016-03-10 16:00:26 -0500255 for (auto &plane : *planes) {
256 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
257 primary_planes_.push_back(plane);
258 else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY)
259 overlay_planes_.push_back(plane);
260 }
261
262 crtc_ = drm_->GetCrtcForDisplay(display);
263 if (!crtc_) {
264 ALOGE("Failed to get crtc for display %d", display);
265 return HWC2::Error::BadDisplay;
266 }
267
268 connector_ = drm_->GetConnectorForDisplay(display);
269 if (!connector_) {
270 ALOGE("Failed to get connector for display %d", display);
271 return HWC2::Error::BadDisplay;
272 }
273
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300274 ret = vsync_worker_.Init(drm_, display);
275 if (ret) {
276 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
277 return HWC2::Error::BadDisplay;
278 }
279
Matvii Zorinef3c7972020-08-11 15:15:44 +0300280 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
281 if (ret) {
282 ALOGE("Failed to set backend for d=%d %d\n", display, ret);
283 return HWC2::Error::BadDisplay;
284 }
285
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300286 return ChosePreferredConfig();
287}
288
289HWC2::Error DrmHwcTwo::HwcDisplay::ChosePreferredConfig() {
Sean Paulac874152016-03-10 16:00:26 -0500290 // Fetch the number of modes from the display
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200291 uint32_t num_configs = 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200292 HWC2::Error err = GetDisplayConfigs(&num_configs, nullptr);
Sean Paulac874152016-03-10 16:00:26 -0500293 if (err != HWC2::Error::None || !num_configs)
294 return err;
295
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200296 return SetActiveConfig(connector_->get_preferred_mode_id());
Sean Paulac874152016-03-10 16:00:26 -0500297}
298
Roman Stratiienko23701092020-09-26 02:08:41 +0300299void DrmHwcTwo::HwcDisplay::RegisterVsyncCallback(
Sean Paulac874152016-03-10 16:00:26 -0500300 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
301 supported(__func__);
Roman Stratiienko23701092020-09-26 02:08:41 +0300302 vsync_worker_.RegisterClientCallback(data, func);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500303}
304
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200305void DrmHwcTwo::HwcDisplay::RegisterRefreshCallback(
306 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
307 supported(__func__);
Roman Stratiienko23701092020-09-26 02:08:41 +0300308 compositor_.SetRefreshCallback(data, func);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200309}
310
Sean Pauled2ec4b2016-03-10 15:35:40 -0500311HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500312 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500313 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
314 l.second.accept_type_change();
315 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500316}
317
318HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500319 supported(__func__);
320 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
321 *layer = static_cast<hwc2_layer_t>(layer_idx_);
322 ++layer_idx_;
323 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500324}
325
326HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Sean Paulac874152016-03-10 16:00:26 -0500327 supported(__func__);
Vincent Donnefort9abec032019-10-09 15:43:43 +0100328 if (!get_layer(layer))
329 return HWC2::Error::BadLayer;
330
Sean Paulac874152016-03-10 16:00:26 -0500331 layers_.erase(layer);
332 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500333}
334
335HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
Sean Paulac874152016-03-10 16:00:26 -0500336 supported(__func__);
337 DrmMode const &mode = connector_->active_mode();
338 if (mode.id() == 0)
339 return HWC2::Error::BadConfig;
340
341 *config = mode.id();
342 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500343}
344
345HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
346 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500347 supported(__func__);
348 uint32_t num_changes = 0;
349 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
350 if (l.second.type_changed()) {
351 if (layers && num_changes < *num_elements)
352 layers[num_changes] = l.first;
353 if (types && num_changes < *num_elements)
354 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
355 ++num_changes;
356 }
357 }
358 if (!layers && !types)
359 *num_elements = num_changes;
360 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500361}
362
363HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500364 uint32_t height,
365 int32_t /*format*/,
366 int32_t dataspace) {
367 supported(__func__);
368 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
369 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
370
371 if (width < min.first || height < min.second)
372 return HWC2::Error::Unsupported;
373
374 if (width > max.first || height > max.second)
375 return HWC2::Error::Unsupported;
376
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200377 if (dataspace != HAL_DATASPACE_UNKNOWN)
Sean Paulac874152016-03-10 16:00:26 -0500378 return HWC2::Error::Unsupported;
379
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200380 // TODO(nobody): Validate format can be handled by either GL or planes
Sean Paulac874152016-03-10 16:00:26 -0500381 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:
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300416 *value = static_cast<int>(mode->h_display());
Sean Paulac874152016-03-10 16:00:26 -0500417 break;
418 case HWC2::Attribute::Height:
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300419 *value = static_cast<int>(mode->v_display());
Sean Paulac874152016-03-10 16:00:26 -0500420 break;
421 case HWC2::Attribute::VsyncPeriod:
422 // in nanoseconds
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300423 *value = static_cast<int>(1E9 / mode->v_refresh());
Sean Paulac874152016-03-10 16:00:26 -0500424 break;
425 case HWC2::Attribute::DpiX:
426 // Dots per 1000 inches
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300427 *value = mm_width
428 ? static_cast<int>(mode->h_display() * kUmPerInch / mm_width)
429 : -1;
Sean Paulac874152016-03-10 16:00:26 -0500430 break;
431 case HWC2::Attribute::DpiY:
432 // Dots per 1000 inches
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300433 *value = mm_height ? static_cast<int>(mode->v_display() * kUmPerInch /
434 mm_height)
435 : -1;
Sean Paulac874152016-03-10 16:00:26 -0500436 break;
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300437#if PLATFORM_SDK_VERSION > 29
438 case HWC2::Attribute::ConfigGroup:
439 *value = 0; /* TODO: Add support for config groups */
440 break;
441#endif
Sean Paulac874152016-03-10 16:00:26 -0500442 default:
443 *value = -1;
444 return HWC2::Error::BadConfig;
445 }
446 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500447}
448
449HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
450 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500451 supported(__func__);
452 // Since this callback is normally invoked twice (once to get the count, and
453 // once to populate configs), we don't really want to read the edid
454 // redundantly. Instead, only update the modes on the first invocation. While
455 // it's possible this will result in stale modes, it'll all come out in the
456 // wash when we try to set the active config later.
457 if (!configs) {
458 int ret = connector_->UpdateModes();
459 if (ret) {
460 ALOGE("Failed to update display modes %d", ret);
461 return HWC2::Error::BadDisplay;
462 }
463 }
464
Neil Armstrongb67d0492019-06-20 09:00:21 +0000465 // Since the upper layers only look at vactive/hactive/refresh, height and
466 // width, it doesn't differentiate interlaced from progressive and other
467 // similar modes. Depending on the order of modes we return to SF, it could
468 // end up choosing a suboptimal configuration and dropping the preferred
469 // mode. To workaround this, don't offer interlaced modes to SF if there is
470 // at least one non-interlaced alternative and only offer a single WxH@R
471 // mode with at least the prefered mode from in DrmConnector::UpdateModes()
472
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200473 // TODO(nobody): Remove the following block of code until AOSP handles all
474 // modes
Neil Armstrongb67d0492019-06-20 09:00:21 +0000475 std::vector<DrmMode> sel_modes;
476
477 // Add the preferred mode first to be sure it's not dropped
478 auto mode = std::find_if(connector_->modes().begin(),
479 connector_->modes().end(), [&](DrmMode const &m) {
480 return m.id() ==
481 connector_->get_preferred_mode_id();
482 });
483 if (mode != connector_->modes().end())
484 sel_modes.push_back(*mode);
485
486 // Add the active mode if different from preferred mode
487 if (connector_->active_mode().id() != connector_->get_preferred_mode_id())
488 sel_modes.push_back(connector_->active_mode());
489
490 // Cycle over the modes and filter out "similar" modes, keeping only the
491 // first ones in the order given by DRM (from CEA ids and timings order)
Sean Paulac874152016-03-10 16:00:26 -0500492 for (const DrmMode &mode : connector_->modes()) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200493 // TODO(nobody): Remove this when 3D Attributes are in AOSP
Neil Armstrongb67d0492019-06-20 09:00:21 +0000494 if (mode.flags() & DRM_MODE_FLAG_3D_MASK)
495 continue;
496
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200497 // TODO(nobody): Remove this when the Interlaced attribute is in AOSP
Neil Armstrong4c027a72019-06-04 14:48:02 +0000498 if (mode.flags() & DRM_MODE_FLAG_INTERLACE) {
499 auto m = std::find_if(connector_->modes().begin(),
500 connector_->modes().end(),
501 [&mode](DrmMode const &m) {
502 return !(m.flags() & DRM_MODE_FLAG_INTERLACE) &&
503 m.h_display() == mode.h_display() &&
504 m.v_display() == mode.v_display();
505 });
Neil Armstrongb67d0492019-06-20 09:00:21 +0000506 if (m == connector_->modes().end())
507 sel_modes.push_back(mode);
508
509 continue;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000510 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000511
512 // Search for a similar WxH@R mode in the filtered list and drop it if
513 // another mode with the same WxH@R has already been selected
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200514 // TODO(nobody): Remove this when AOSP handles duplicates modes
Neil Armstrongb67d0492019-06-20 09:00:21 +0000515 auto m = std::find_if(sel_modes.begin(), sel_modes.end(),
516 [&mode](DrmMode const &m) {
517 return m.h_display() == mode.h_display() &&
518 m.v_display() == mode.v_display() &&
519 m.v_refresh() == mode.v_refresh();
520 });
521 if (m == sel_modes.end())
522 sel_modes.push_back(mode);
523 }
524
525 auto num_modes = static_cast<uint32_t>(sel_modes.size());
526 if (!configs) {
527 *num_configs = num_modes;
528 return HWC2::Error::None;
529 }
530
531 uint32_t idx = 0;
532 for (const DrmMode &mode : sel_modes) {
533 if (idx >= *num_configs)
534 break;
535 configs[idx++] = mode.id();
Sean Paulac874152016-03-10 16:00:26 -0500536 }
537 *num_configs = idx;
538 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500539}
540
541HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500542 supported(__func__);
543 std::ostringstream stream;
544 stream << "display-" << connector_->id();
545 std::string string = stream.str();
546 size_t length = string.length();
547 if (!name) {
548 *size = length;
549 return HWC2::Error::None;
550 }
551
552 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
553 strncpy(name, string.c_str(), *size);
554 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500555}
556
Sean Paulac874152016-03-10 16:00:26 -0500557HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
558 uint32_t *num_elements,
559 hwc2_layer_t *layers,
560 int32_t *layer_requests) {
561 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200562 // TODO(nobody): I think virtual display should request
Sean Paulac874152016-03-10 16:00:26 -0500563 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
564 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
565 *num_elements = 0;
566 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500567}
568
569HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500570 supported(__func__);
571 *type = static_cast<int32_t>(type_);
572 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500573}
574
575HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500576 supported(__func__);
577 *support = 0;
578 return HWC2::Error::None;
579}
580
581HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400582 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
583 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500584 supported(__func__);
585 *num_types = 0;
586 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500587}
588
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300589/* Find API details at:
590 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767
591 */
Sean Pauled2ec4b2016-03-10 15:35:40 -0500592HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500593 hwc2_layer_t *layers,
594 int32_t *fences) {
595 supported(__func__);
596 uint32_t num_layers = 0;
597
598 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
599 ++num_layers;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200600 if (layers == nullptr || fences == nullptr)
Sean Paulac874152016-03-10 16:00:26 -0500601 continue;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200602
603 if (num_layers > *num_elements) {
Sean Paulac874152016-03-10 16:00:26 -0500604 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
605 return HWC2::Error::None;
606 }
607
608 layers[num_layers - 1] = l.first;
Roman Stratiienko0fade372021-02-20 13:59:55 +0200609 fences[num_layers - 1] = l.second.release_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500610 }
611 *num_elements = num_layers;
612 return HWC2::Error::None;
613}
614
Roman Stratiienko0fade372021-02-20 13:59:55 +0200615void DrmHwcTwo::HwcDisplay::AddFenceToPresentFence(UniqueFd fd) {
616 if (!fd) {
Sean Paulac874152016-03-10 16:00:26 -0500617 return;
Roman Stratiienko0fade372021-02-20 13:59:55 +0200618 }
Sean Paulac874152016-03-10 16:00:26 -0500619
Roman Stratiienko0fade372021-02-20 13:59:55 +0200620 if (present_fence_) {
621 present_fence_ = UniqueFd(
622 sync_merge("dc_present", present_fence_.Get(), fd.Get()));
Sean Paulac874152016-03-10 16:00:26 -0500623 } else {
Roman Stratiienko0fade372021-02-20 13:59:55 +0200624 present_fence_ = std::move(fd);
Sean Paulac874152016-03-10 16:00:26 -0500625 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500626}
627
Rob Herring4f6c62e2018-05-17 14:33:02 -0500628HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500629 // order the layers by z-order
630 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100631 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500632 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
633 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200634 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500635 case HWC2::Composition::Device:
636 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
637 break;
638 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100639 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500640 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100641 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500642 break;
643 default:
644 continue;
645 }
646 }
647 if (use_client_layer)
648 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
649
Rob Herring4f6c62e2018-05-17 14:33:02 -0500650 if (z_map.empty())
651 return HWC2::Error::BadLayer;
652
Matvii Zorin5368b732021-01-12 10:53:08 +0200653 std::vector<DrmHwcLayer> composition_layers;
654
Sean Paulac874152016-03-10 16:00:26 -0500655 // now that they're ordered by z, add them to the composition
656 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
657 DrmHwcLayer layer;
658 l.second->PopulateDrmLayer(&layer);
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200659 int ret = layer.ImportBuffer(drm_);
Sean Paulac874152016-03-10 16:00:26 -0500660 if (ret) {
661 ALOGE("Failed to import layer, ret=%d", ret);
662 return HWC2::Error::NoResources;
663 }
Matvii Zorin5368b732021-01-12 10:53:08 +0200664 composition_layers.emplace_back(std::move(layer));
Sean Paulac874152016-03-10 16:00:26 -0500665 }
Sean Paulac874152016-03-10 16:00:26 -0500666
Matvii Zorin704ea0e2021-01-08 12:55:45 +0200667 auto composition = std::make_unique<DrmDisplayComposition>(crtc_,
668 planner_.get());
Sean Paulac874152016-03-10 16:00:26 -0500669
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200670 // TODO(nobody): Don't always assume geometry changed
Matvii Zorin5368b732021-01-12 10:53:08 +0200671 int ret = composition->SetLayers(composition_layers.data(),
672 composition_layers.size(), true);
Sean Paulac874152016-03-10 16:00:26 -0500673 if (ret) {
674 ALOGE("Failed to set layers in the composition ret=%d", ret);
675 return HWC2::Error::BadLayer;
676 }
677
678 std::vector<DrmPlane *> primary_planes(primary_planes_);
679 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500680 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500681 if (ret) {
John Stultz66763d52021-08-24 04:59:25 +0000682 ALOGV("Failed to plan the composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500683 return HWC2::Error::BadConfig;
684 }
685
686 // Disable the planes we're not using
687 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
688 composition->AddPlaneDisable(*i);
689 i = primary_planes.erase(i);
690 }
691 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
692 composition->AddPlaneDisable(*i);
693 i = overlay_planes.erase(i);
694 }
695
Rob Herring4f6c62e2018-05-17 14:33:02 -0500696 if (test) {
697 ret = compositor_.TestComposition(composition.get());
698 } else {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500699 ret = compositor_.ApplyComposition(std::move(composition));
Matteo Franchinc56eede2019-12-03 17:10:38 +0000700 AddFenceToPresentFence(compositor_.TakeOutFence());
Rob Herring4f6c62e2018-05-17 14:33:02 -0500701 }
Sean Paulac874152016-03-10 16:00:26 -0500702 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700703 if (!test)
704 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500705 return HWC2::Error::BadParameter;
706 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500707 return HWC2::Error::None;
708}
709
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300710/* Find API details at:
711 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
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
Matvii Zorin704ea0e2021-01-08 12:55:45 +0200749 auto composition = std::make_unique<DrmDisplayComposition>(crtc_,
750 planner_.get());
Sean Paulac874152016-03-10 16:00:26 -0500751 int ret = composition->SetDisplayMode(*mode);
Roman Stratiienko6a10c4c2021-02-15 11:25:23 +0200752 if (ret) {
753 return HWC2::Error::BadConfig;
754 }
Sean Pauled45a8e2017-02-28 13:17:34 -0500755 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500756 if (ret) {
757 ALOGE("Failed to queue dpms composition on %d", ret);
758 return HWC2::Error::BadConfig;
759 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300760
761 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500762
763 // Setup the client layer's dimensions
764 hwc_rect_t display_frame = {.left = 0,
765 .top = 0,
766 .right = static_cast<int>(mode->h_display()),
767 .bottom = static_cast<int>(mode->v_display())};
768 client_layer_.SetLayerDisplayFrame(display_frame);
Sean Paulac874152016-03-10 16:00:26 -0500769
770 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500771}
772
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300773/* Find API details at:
774 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
775 */
Sean Pauled2ec4b2016-03-10 15:35:40 -0500776HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
777 int32_t acquire_fence,
778 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600779 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500780 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500781
782 client_layer_.set_buffer(target);
John Stultz8adf5442021-07-31 00:19:50 +0000783 client_layer_.acquire_fence_ = UniqueFd(acquire_fence);
Sean Paulac874152016-03-10 16:00:26 -0500784 client_layer_.SetLayerDataspace(dataspace);
Roman Stratiienko33365c22020-10-10 23:06:36 +0300785
786 /* TODO: Do not update source_crop every call.
787 * It makes sense to do it once after every hotplug event. */
788 hwc_drm_bo bo{};
789 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
790
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200791 hwc_frect_t source_crop = {.left = 0.0F,
792 .top = 0.0F,
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300793 .right = static_cast<float>(bo.width),
794 .bottom = static_cast<float>(bo.height)};
Roman Stratiienko33365c22020-10-10 23:06:36 +0300795 client_layer_.SetLayerSourceCrop(source_crop);
796
Sean Paulac874152016-03-10 16:00:26 -0500797 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500798}
799
800HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500801 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800802
Roman Stratiienkod146d6d2020-10-04 23:56:46 +0300803 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
804 return HWC2::Error::BadParameter;
805
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800806 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +0300807 return HWC2::Error::Unsupported;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800808
809 color_mode_ = mode;
810 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500811}
812
813HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500814 int32_t hint) {
815 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200816 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
817 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
818 return HWC2::Error::BadParameter;
819
820 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
821 return HWC2::Error::BadParameter;
822
823 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
824 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
825 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
826
827 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500828}
829
830HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500831 int32_t release_fence) {
832 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200833 // TODO(nobody): Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500834 return unsupported(__func__, buffer, release_fence);
835}
836
Sean Paulac874152016-03-10 16:00:26 -0500837HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
838 supported(__func__);
839 uint64_t dpms_value = 0;
840 auto mode = static_cast<HWC2::PowerMode>(mode_in);
841 switch (mode) {
842 case HWC2::PowerMode::Off:
843 dpms_value = DRM_MODE_DPMS_OFF;
844 break;
845 case HWC2::PowerMode::On:
846 dpms_value = DRM_MODE_DPMS_ON;
847 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100848 case HWC2::PowerMode::Doze:
849 case HWC2::PowerMode::DozeSuspend:
850 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500851 default:
852 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100853 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500854 };
855
Matvii Zorin704ea0e2021-01-08 12:55:45 +0200856 auto composition = std::make_unique<DrmDisplayComposition>(crtc_,
857 planner_.get());
Sean Paulac874152016-03-10 16:00:26 -0500858 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
873HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500874 uint32_t *num_requests) {
875 supported(__func__);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500876
Matvii Zorinef3c7972020-08-11 15:15:44 +0300877 return backend_->ValidateDisplay(this, num_types, num_requests);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500878}
879
Matvii Zorined90ef92021-01-29 18:32:06 +0200880std::vector<DrmHwcTwo::HwcLayer *>
881DrmHwcTwo::HwcDisplay::GetOrderLayersByZPos() {
882 std::vector<DrmHwcTwo::HwcLayer *> ordered_layers;
883 ordered_layers.reserve(layers_.size());
884
885 for (auto &[handle, layer] : layers_) {
886 ordered_layers.emplace_back(&layer);
887 }
888
889 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
890 [](const DrmHwcTwo::HwcLayer *lhs, const DrmHwcTwo::HwcLayer *rhs) {
891 return lhs->z_order() < rhs->z_order();
892 });
893
894 return ordered_layers;
895}
896
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300897#if PLATFORM_SDK_VERSION > 29
898HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
899 if (connector_->internal())
900 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
901 else if (connector_->external())
902 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
903 else
904 return HWC2::Error::BadConfig;
905
906 return HWC2::Error::None;
907}
908
909HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayVsyncPeriod(
910 hwc2_vsync_period_t *outVsyncPeriod /* ns */) {
911 supported(__func__);
912 DrmMode const &mode = connector_->active_mode();
913 if (mode.id() == 0)
914 return HWC2::Error::BadConfig;
915
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300916 *outVsyncPeriod = static_cast<hwc2_vsync_period_t>(1E9 / mode.v_refresh());
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300917 return HWC2::Error::None;
918}
919
920HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfigWithConstraints(
921 hwc2_config_t /*config*/,
922 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
923 hwc_vsync_period_change_timeline_t *outTimeline) {
924 supported(__func__);
925
926 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
927 return HWC2::Error::BadParameter;
928 }
929
930 return HWC2::Error::BadConfig;
931}
932
933HWC2::Error DrmHwcTwo::HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
934 return HWC2::Error::Unsupported;
935}
936
937HWC2::Error DrmHwcTwo::HwcDisplay::GetSupportedContentTypes(
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200938 uint32_t *outNumSupportedContentTypes,
939 const uint32_t *outSupportedContentTypes) {
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300940 if (outSupportedContentTypes == nullptr)
941 *outNumSupportedContentTypes = 0;
942
943 return HWC2::Error::None;
944}
945
946HWC2::Error DrmHwcTwo::HwcDisplay::SetContentType(int32_t contentType) {
947 supported(__func__);
948
949 if (contentType != HWC2_CONTENT_TYPE_NONE)
950 return HWC2::Error::Unsupported;
951
952 /* TODO: Map to the DRM Connector property:
953 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
954 */
955
956 return HWC2::Error::None;
957}
958#endif
959
John Stultz8c7229d2020-02-07 21:31:08 +0000960#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800961HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
962 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
963 supported(__func__);
964
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300965 auto blob = connector_->GetEdidBlob();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800966
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300967 if (!blob) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800968 ALOGE("Failed to get edid property value.");
969 return HWC2::Error::Unsupported;
970 }
971
Andrii Chepurnyi8115dbe2020-04-14 13:03:57 +0300972 if (outData) {
973 *outDataSize = std::min(*outDataSize, blob->length);
974 memcpy(outData, blob->data, *outDataSize);
975 } else {
976 *outDataSize = blob->length;
977 }
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800978 *outPort = connector_->id();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800979
980 return HWC2::Error::None;
981}
982
983HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
984 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
985 unsupported(__func__, outCapabilities);
986
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200987 if (outNumCapabilities == nullptr) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800988 return HWC2::Error::BadParameter;
989 }
990
991 *outNumCapabilities = 0;
992
993 return HWC2::Error::None;
994}
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +0300995
996HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayBrightnessSupport(
997 bool *supported) {
998 *supported = false;
999 return HWC2::Error::None;
1000}
1001
1002HWC2::Error DrmHwcTwo::HwcDisplay::SetDisplayBrightness(
1003 float /* brightness */) {
1004 return HWC2::Error::Unsupported;
1005}
1006
John Stultz8c7229d2020-02-07 21:31:08 +00001007#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001008
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001009#if PLATFORM_SDK_VERSION > 27
1010
1011HWC2::Error DrmHwcTwo::HwcDisplay::GetRenderIntents(
1012 int32_t mode, uint32_t *outNumIntents,
1013 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1014 if (mode != HAL_COLOR_MODE_NATIVE) {
1015 return HWC2::Error::BadParameter;
1016 }
1017
1018 if (outIntents == nullptr) {
1019 *outNumIntents = 1;
1020 return HWC2::Error::None;
1021 }
1022 *outNumIntents = 1;
1023 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1024 return HWC2::Error::None;
1025}
1026
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001027HWC2::Error DrmHwcTwo::HwcDisplay::SetColorModeWithIntent(int32_t mode,
1028 int32_t intent) {
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001029 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1030 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1031 return HWC2::Error::BadParameter;
1032
1033 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
1034 return HWC2::Error::BadParameter;
1035
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001036 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001037 return HWC2::Error::Unsupported;
1038
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001039 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001040 return HWC2::Error::Unsupported;
1041
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001042 color_mode_ = mode;
1043 return HWC2::Error::None;
1044}
1045
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001046#endif /* PLATFORM_SDK_VERSION > 27 */
1047
Sean Pauled2ec4b2016-03-10 15:35:40 -05001048HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -05001049 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -08001050 cursor_x_ = x;
1051 cursor_y_ = y;
1052 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001053}
1054
1055HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -05001056 supported(__func__);
1057 blending_ = static_cast<HWC2::BlendMode>(mode);
1058 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001059}
1060
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +03001061/* Find API details at:
1062 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=2314
1063 */
Sean Pauled2ec4b2016-03-10 15:35:40 -05001064HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001065 int32_t acquire_fence) {
1066 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -05001067
Sean Paulac874152016-03-10 16:00:26 -05001068 set_buffer(buffer);
John Stultz8adf5442021-07-31 00:19:50 +00001069 acquire_fence_ = UniqueFd(acquire_fence);
Sean Paulac874152016-03-10 16:00:26 -05001070 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001071}
1072
1073HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001074 // TODO(nobody): Put to client composition here?
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001075 supported(__func__);
1076 layer_color_ = color;
1077 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001078}
1079
1080HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001081 sf_type_ = static_cast<HWC2::Composition>(type);
1082 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001083}
1084
1085HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001086 supported(__func__);
1087 dataspace_ = static_cast<android_dataspace_t>(dataspace);
1088 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001089}
1090
1091HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001092 supported(__func__);
1093 display_frame_ = frame;
1094 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001095}
1096
1097HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001098 supported(__func__);
1099 alpha_ = alpha;
1100 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001101}
1102
1103HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1104 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001105 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001106 // TODO(nobody): We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001107 return unsupported(__func__, stream);
1108}
1109
1110HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001111 supported(__func__);
1112 source_crop_ = crop;
1113 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001114}
1115
1116HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001117 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001118 // TODO(nobody): We don't use surface damage, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001119 unsupported(__func__, damage);
1120 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001121}
1122
1123HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001124 supported(__func__);
1125 transform_ = static_cast<HWC2::Transform>(transform);
1126 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001127}
1128
1129HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001130 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001131 // TODO(nobody): We don't use this information, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001132 unsupported(__func__, visible);
1133 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001134}
1135
Sean Paulac874152016-03-10 16:00:26 -05001136HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1137 supported(__func__);
1138 z_order_ = order;
1139 return HWC2::Error::None;
1140}
1141
1142void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1143 supported(__func__);
1144 switch (blending_) {
1145 case HWC2::BlendMode::None:
1146 layer->blending = DrmHwcBlending::kNone;
1147 break;
1148 case HWC2::BlendMode::Premultiplied:
1149 layer->blending = DrmHwcBlending::kPreMult;
1150 break;
1151 case HWC2::BlendMode::Coverage:
1152 layer->blending = DrmHwcBlending::kCoverage;
1153 break;
1154 default:
1155 ALOGE("Unknown blending mode b=%d", blending_);
1156 layer->blending = DrmHwcBlending::kNone;
1157 break;
1158 }
1159
Sean Paulac874152016-03-10 16:00:26 -05001160 layer->sf_handle = buffer_;
Roman Stratiienko0fade372021-02-20 13:59:55 +02001161 // TODO(rsglobal): Avoid extra fd duplication
1162 layer->acquire_fence = UniqueFd(fcntl(acquire_fence_.Get(), F_DUPFD_CLOEXEC));
Roman Kovalivskyib8652272021-01-24 20:32:37 +02001163 layer->display_frame = display_frame_;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001164 layer->alpha = lround(65535.0F * alpha_);
Roman Kovalivskyib8652272021-01-24 20:32:37 +02001165 layer->source_crop = source_crop_;
Sean Paulac874152016-03-10 16:00:26 -05001166 layer->SetTransform(static_cast<int32_t>(transform_));
Matvii Zorin8338c342020-09-08 16:12:51 +03001167 layer->dataspace = dataspace_;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001168}
1169
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001170void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
Roman Stratiienko23701092020-09-26 02:08:41 +03001171 const std::lock_guard<std::mutex> lock(hotplug_callback_lock);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001172
Roman Stratiienko23701092020-09-26 02:08:41 +03001173 if (hotplug_callback_hook_ && hotplug_callback_data_)
1174 hotplug_callback_hook_(hotplug_callback_data_, displayid,
1175 state == DRM_MODE_CONNECTED
1176 ? HWC2_CONNECTION_CONNECTED
1177 : HWC2_CONNECTION_DISCONNECTED);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001178}
1179
1180void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
Roman Stratiienkod21071f2021-03-09 21:56:50 +02001181 for (const auto &conn : drmDevice->connectors()) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001182 if (conn->state() != DRM_MODE_CONNECTED)
1183 continue;
1184 HandleDisplayHotplug(conn->display(), conn->state());
1185 }
1186}
1187
1188void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
Roman Stratiienkod21071f2021-03-09 21:56:50 +02001189 for (const auto &conn : drm_->connectors()) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001190 drmModeConnection old_state = conn->state();
1191 drmModeConnection cur_state = conn->UpdateModes()
1192 ? DRM_MODE_UNKNOWNCONNECTION
1193 : conn->state();
1194
1195 if (cur_state == old_state)
1196 continue;
1197
1198 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
1199 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1200 conn->id(), conn->display());
1201
1202 int display_id = conn->display();
1203 if (cur_state == DRM_MODE_CONNECTED) {
1204 auto &display = hwc2_->displays_.at(display_id);
1205 display.ChosePreferredConfig();
1206 } else {
1207 auto &display = hwc2_->displays_.at(display_id);
1208 display.ClearDisplay();
1209 }
1210
1211 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1212 }
1213}
1214
Sean Pauled2ec4b2016-03-10 15:35:40 -05001215// static
1216int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1217 unsupported(__func__);
1218 return 0;
1219}
1220
1221// static
1222void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001223 uint32_t *out_count,
1224 int32_t * /*out_capabilities*/) {
1225 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001226 *out_count = 0;
1227}
1228
1229// static
Sean Paulac874152016-03-10 16:00:26 -05001230hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1231 struct hwc2_device * /*dev*/, int32_t descriptor) {
1232 supported(__func__);
1233 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001234 switch (func) {
1235 // Device functions
1236 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1237 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1238 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1239 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001240 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001241 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1242 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1243 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1244 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1245 case HWC2::FunctionDescriptor::Dump:
1246 return ToHook<HWC2_PFN_DUMP>(
1247 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1248 uint32_t *, char *>);
1249 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1250 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1251 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1252 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1253 case HWC2::FunctionDescriptor::RegisterCallback:
1254 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1255 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1256 &DrmHwcTwo::RegisterCallback, int32_t,
1257 hwc2_callback_data_t, hwc2_function_pointer_t>);
1258
1259 // Display functions
1260 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1261 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1262 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1263 &HwcDisplay::AcceptDisplayChanges>);
1264 case HWC2::FunctionDescriptor::CreateLayer:
1265 return ToHook<HWC2_PFN_CREATE_LAYER>(
1266 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1267 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1268 case HWC2::FunctionDescriptor::DestroyLayer:
1269 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1270 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1271 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1272 case HWC2::FunctionDescriptor::GetActiveConfig:
1273 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1274 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1275 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1276 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1277 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1278 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1279 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1280 hwc2_layer_t *, int32_t *>);
1281 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1282 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1283 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1284 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1285 int32_t, int32_t>);
1286 case HWC2::FunctionDescriptor::GetColorModes:
1287 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1288 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1289 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1290 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001291 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1292 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1293 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1294 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001295 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001296 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1297 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1298 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1299 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001300 case HWC2::FunctionDescriptor::GetDisplayName:
1301 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1302 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1303 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1304 case HWC2::FunctionDescriptor::GetDisplayRequests:
1305 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1306 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1307 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1308 hwc2_layer_t *, int32_t *>);
1309 case HWC2::FunctionDescriptor::GetDisplayType:
1310 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1311 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1312 &HwcDisplay::GetDisplayType, int32_t *>);
1313 case HWC2::FunctionDescriptor::GetDozeSupport:
1314 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1315 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1316 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001317 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1318 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1319 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1320 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1321 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001322 case HWC2::FunctionDescriptor::GetReleaseFences:
1323 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1324 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1325 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1326 int32_t *>);
1327 case HWC2::FunctionDescriptor::PresentDisplay:
1328 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1329 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1330 &HwcDisplay::PresentDisplay, int32_t *>);
1331 case HWC2::FunctionDescriptor::SetActiveConfig:
1332 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1333 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1334 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1335 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001336 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1337 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1338 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1339 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001340 case HWC2::FunctionDescriptor::SetColorMode:
1341 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1342 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1343 &HwcDisplay::SetColorMode, int32_t>);
1344 case HWC2::FunctionDescriptor::SetColorTransform:
1345 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1346 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1347 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1348 case HWC2::FunctionDescriptor::SetOutputBuffer:
1349 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1350 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1351 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1352 case HWC2::FunctionDescriptor::SetPowerMode:
1353 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1354 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1355 &HwcDisplay::SetPowerMode, int32_t>);
1356 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1357 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1358 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1359 &HwcDisplay::SetVsyncEnabled, int32_t>);
1360 case HWC2::FunctionDescriptor::ValidateDisplay:
1361 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1362 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1363 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001364#if PLATFORM_SDK_VERSION > 27
1365 case HWC2::FunctionDescriptor::GetRenderIntents:
1366 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
1367 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
1368 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
1369 int32_t *>);
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001370 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
1371 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
1372 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
1373 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001374#endif
John Stultz8c7229d2020-02-07 21:31:08 +00001375#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001376 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1377 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1378 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1379 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1380 uint32_t *, uint8_t *>);
1381 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1382 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1383 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1384 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1385 uint32_t *>);
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001386 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
1387 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
1388 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
1389 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
1390 case HWC2::FunctionDescriptor::SetDisplayBrightness:
1391 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
1392 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
1393 &HwcDisplay::SetDisplayBrightness, float>);
John Stultz8c7229d2020-02-07 21:31:08 +00001394#endif /* PLATFORM_SDK_VERSION > 28 */
Roman Stratiienko6f5df172020-09-27 22:48:08 +03001395#if PLATFORM_SDK_VERSION > 29
1396 case HWC2::FunctionDescriptor::GetDisplayConnectionType:
1397 return ToHook<HWC2_PFN_GET_DISPLAY_CONNECTION_TYPE>(
1398 DisplayHook<decltype(&HwcDisplay::GetDisplayConnectionType),
1399 &HwcDisplay::GetDisplayConnectionType, uint32_t *>);
1400 case HWC2::FunctionDescriptor::GetDisplayVsyncPeriod:
1401 return ToHook<HWC2_PFN_GET_DISPLAY_VSYNC_PERIOD>(
1402 DisplayHook<decltype(&HwcDisplay::GetDisplayVsyncPeriod),
1403 &HwcDisplay::GetDisplayVsyncPeriod,
1404 hwc2_vsync_period_t *>);
1405 case HWC2::FunctionDescriptor::SetActiveConfigWithConstraints:
1406 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG_WITH_CONSTRAINTS>(
1407 DisplayHook<decltype(&HwcDisplay::SetActiveConfigWithConstraints),
1408 &HwcDisplay::SetActiveConfigWithConstraints,
1409 hwc2_config_t, hwc_vsync_period_change_constraints_t *,
1410 hwc_vsync_period_change_timeline_t *>);
1411 case HWC2::FunctionDescriptor::SetAutoLowLatencyMode:
1412 return ToHook<HWC2_PFN_SET_AUTO_LOW_LATENCY_MODE>(
1413 DisplayHook<decltype(&HwcDisplay::SetAutoLowLatencyMode),
1414 &HwcDisplay::SetAutoLowLatencyMode, bool>);
1415 case HWC2::FunctionDescriptor::GetSupportedContentTypes:
1416 return ToHook<HWC2_PFN_GET_SUPPORTED_CONTENT_TYPES>(
1417 DisplayHook<decltype(&HwcDisplay::GetSupportedContentTypes),
1418 &HwcDisplay::GetSupportedContentTypes, uint32_t *,
1419 uint32_t *>);
1420 case HWC2::FunctionDescriptor::SetContentType:
1421 return ToHook<HWC2_PFN_SET_CONTENT_TYPE>(
1422 DisplayHook<decltype(&HwcDisplay::SetContentType),
1423 &HwcDisplay::SetContentType, int32_t>);
1424#endif
Sean Pauled2ec4b2016-03-10 15:35:40 -05001425 // Layer functions
1426 case HWC2::FunctionDescriptor::SetCursorPosition:
1427 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1428 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1429 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1430 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1431 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1432 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1433 &HwcLayer::SetLayerBlendMode, int32_t>);
1434 case HWC2::FunctionDescriptor::SetLayerBuffer:
1435 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1436 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1437 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1438 case HWC2::FunctionDescriptor::SetLayerColor:
1439 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1440 LayerHook<decltype(&HwcLayer::SetLayerColor),
1441 &HwcLayer::SetLayerColor, hwc_color_t>);
1442 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1443 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1444 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1445 &HwcLayer::SetLayerCompositionType, int32_t>);
1446 case HWC2::FunctionDescriptor::SetLayerDataspace:
1447 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1448 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1449 &HwcLayer::SetLayerDataspace, int32_t>);
1450 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1451 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1452 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1453 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1454 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1455 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1456 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1457 &HwcLayer::SetLayerPlaneAlpha, float>);
1458 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001459 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1460 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1461 &HwcLayer::SetLayerSidebandStream,
1462 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001463 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1464 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1465 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1466 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1467 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1468 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1469 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1470 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1471 case HWC2::FunctionDescriptor::SetLayerTransform:
1472 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1473 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1474 &HwcLayer::SetLayerTransform, int32_t>);
1475 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1476 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1477 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1478 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1479 case HWC2::FunctionDescriptor::SetLayerZOrder:
1480 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1481 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1482 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001483 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001484 default:
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001485 return nullptr;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001486 }
1487}
Sean Paulac874152016-03-10 16:00:26 -05001488
1489// static
1490int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1491 struct hw_device_t **dev) {
1492 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001493 if (strcmp(name, HWC_HARDWARE_COMPOSER) != 0) {
Sean Paulac874152016-03-10 16:00:26 -05001494 ALOGE("Invalid module name- %s", name);
1495 return -EINVAL;
1496 }
1497
1498 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1499 if (!ctx) {
1500 ALOGE("Failed to allocate DrmHwcTwo");
1501 return -ENOMEM;
1502 }
1503
1504 HWC2::Error err = ctx->Init();
1505 if (err != HWC2::Error::None) {
1506 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1507 return -EINVAL;
1508 }
1509
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001510 ctx->common.module = (hw_module_t *)module;
Sean Paulac874152016-03-10 16:00:26 -05001511 *dev = &ctx->common;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001512 ctx.release(); // NOLINT(bugprone-unused-return-value)
Sean Paulac874152016-03-10 16:00:26 -05001513 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001514}
Sean Paulf72cccd2018-08-27 13:59:08 -04001515} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001516
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001517// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001518static struct hw_module_methods_t hwc2_module_methods = {
1519 .open = android::DrmHwcTwo::HookDevOpen,
1520};
1521
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001522// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001523hw_module_t HAL_MODULE_INFO_SYM = {
1524 .tag = HARDWARE_MODULE_TAG,
1525 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1526 .id = HWC_HARDWARE_MODULE_ID,
1527 .name = "DrmHwcTwo module",
1528 .author = "The Android Open Source Project",
1529 .methods = &hwc2_module_methods,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001530 .dso = nullptr,
Sean Paulac874152016-03-10 16:00:26 -05001531 .reserved = {0},
1532};