blob: 413d9c34cf7b2d464cd09600c44845eabd38864e [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) {
51 DrmDevice *drm = resource_manager_.GetDrmDevice(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:
416 *value = mode->h_display();
417 break;
418 case HWC2::Attribute::Height:
419 *value = mode->v_display();
420 break;
421 case HWC2::Attribute::VsyncPeriod:
422 // in nanoseconds
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200423 *value = 1000.0 * 1000.0 * 1000.0 / mode->v_refresh();
Sean Paulac874152016-03-10 16:00:26 -0500424 break;
425 case HWC2::Attribute::DpiX:
426 // Dots per 1000 inches
427 *value = mm_width ? (mode->h_display() * kUmPerInch) / mm_width : -1;
428 break;
429 case HWC2::Attribute::DpiY:
430 // Dots per 1000 inches
431 *value = mm_height ? (mode->v_display() * kUmPerInch) / mm_height : -1;
432 break;
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300433#if PLATFORM_SDK_VERSION > 29
434 case HWC2::Attribute::ConfigGroup:
435 *value = 0; /* TODO: Add support for config groups */
436 break;
437#endif
Sean Paulac874152016-03-10 16:00:26 -0500438 default:
439 *value = -1;
440 return HWC2::Error::BadConfig;
441 }
442 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500443}
444
445HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
446 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500447 supported(__func__);
448 // Since this callback is normally invoked twice (once to get the count, and
449 // once to populate configs), we don't really want to read the edid
450 // redundantly. Instead, only update the modes on the first invocation. While
451 // it's possible this will result in stale modes, it'll all come out in the
452 // wash when we try to set the active config later.
453 if (!configs) {
454 int ret = connector_->UpdateModes();
455 if (ret) {
456 ALOGE("Failed to update display modes %d", ret);
457 return HWC2::Error::BadDisplay;
458 }
459 }
460
Neil Armstrongb67d0492019-06-20 09:00:21 +0000461 // Since the upper layers only look at vactive/hactive/refresh, height and
462 // width, it doesn't differentiate interlaced from progressive and other
463 // similar modes. Depending on the order of modes we return to SF, it could
464 // end up choosing a suboptimal configuration and dropping the preferred
465 // mode. To workaround this, don't offer interlaced modes to SF if there is
466 // at least one non-interlaced alternative and only offer a single WxH@R
467 // mode with at least the prefered mode from in DrmConnector::UpdateModes()
468
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200469 // TODO(nobody): Remove the following block of code until AOSP handles all
470 // modes
Neil Armstrongb67d0492019-06-20 09:00:21 +0000471 std::vector<DrmMode> sel_modes;
472
473 // Add the preferred mode first to be sure it's not dropped
474 auto mode = std::find_if(connector_->modes().begin(),
475 connector_->modes().end(), [&](DrmMode const &m) {
476 return m.id() ==
477 connector_->get_preferred_mode_id();
478 });
479 if (mode != connector_->modes().end())
480 sel_modes.push_back(*mode);
481
482 // Add the active mode if different from preferred mode
483 if (connector_->active_mode().id() != connector_->get_preferred_mode_id())
484 sel_modes.push_back(connector_->active_mode());
485
486 // Cycle over the modes and filter out "similar" modes, keeping only the
487 // first ones in the order given by DRM (from CEA ids and timings order)
Sean Paulac874152016-03-10 16:00:26 -0500488 for (const DrmMode &mode : connector_->modes()) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200489 // TODO(nobody): Remove this when 3D Attributes are in AOSP
Neil Armstrongb67d0492019-06-20 09:00:21 +0000490 if (mode.flags() & DRM_MODE_FLAG_3D_MASK)
491 continue;
492
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200493 // TODO(nobody): Remove this when the Interlaced attribute is in AOSP
Neil Armstrong4c027a72019-06-04 14:48:02 +0000494 if (mode.flags() & DRM_MODE_FLAG_INTERLACE) {
495 auto m = std::find_if(connector_->modes().begin(),
496 connector_->modes().end(),
497 [&mode](DrmMode const &m) {
498 return !(m.flags() & DRM_MODE_FLAG_INTERLACE) &&
499 m.h_display() == mode.h_display() &&
500 m.v_display() == mode.v_display();
501 });
Neil Armstrongb67d0492019-06-20 09:00:21 +0000502 if (m == connector_->modes().end())
503 sel_modes.push_back(mode);
504
505 continue;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000506 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000507
508 // Search for a similar WxH@R mode in the filtered list and drop it if
509 // another mode with the same WxH@R has already been selected
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200510 // TODO(nobody): Remove this when AOSP handles duplicates modes
Neil Armstrongb67d0492019-06-20 09:00:21 +0000511 auto m = std::find_if(sel_modes.begin(), sel_modes.end(),
512 [&mode](DrmMode const &m) {
513 return m.h_display() == mode.h_display() &&
514 m.v_display() == mode.v_display() &&
515 m.v_refresh() == mode.v_refresh();
516 });
517 if (m == sel_modes.end())
518 sel_modes.push_back(mode);
519 }
520
521 auto num_modes = static_cast<uint32_t>(sel_modes.size());
522 if (!configs) {
523 *num_configs = num_modes;
524 return HWC2::Error::None;
525 }
526
527 uint32_t idx = 0;
528 for (const DrmMode &mode : sel_modes) {
529 if (idx >= *num_configs)
530 break;
531 configs[idx++] = mode.id();
Sean Paulac874152016-03-10 16:00:26 -0500532 }
533 *num_configs = idx;
534 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500535}
536
537HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500538 supported(__func__);
539 std::ostringstream stream;
540 stream << "display-" << connector_->id();
541 std::string string = stream.str();
542 size_t length = string.length();
543 if (!name) {
544 *size = length;
545 return HWC2::Error::None;
546 }
547
548 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
549 strncpy(name, string.c_str(), *size);
550 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500551}
552
Sean Paulac874152016-03-10 16:00:26 -0500553HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
554 uint32_t *num_elements,
555 hwc2_layer_t *layers,
556 int32_t *layer_requests) {
557 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200558 // TODO(nobody): I think virtual display should request
Sean Paulac874152016-03-10 16:00:26 -0500559 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
560 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
561 *num_elements = 0;
562 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500563}
564
565HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500566 supported(__func__);
567 *type = static_cast<int32_t>(type_);
568 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500569}
570
571HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500572 supported(__func__);
573 *support = 0;
574 return HWC2::Error::None;
575}
576
577HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400578 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
579 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500580 supported(__func__);
581 *num_types = 0;
582 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500583}
584
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300585/* Find API details at:
586 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767
587 */
Sean Pauled2ec4b2016-03-10 15:35:40 -0500588HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500589 hwc2_layer_t *layers,
590 int32_t *fences) {
591 supported(__func__);
592 uint32_t num_layers = 0;
593
594 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
595 ++num_layers;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200596 if (layers == nullptr || fences == nullptr)
Sean Paulac874152016-03-10 16:00:26 -0500597 continue;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200598
599 if (num_layers > *num_elements) {
Sean Paulac874152016-03-10 16:00:26 -0500600 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
601 return HWC2::Error::None;
602 }
603
604 layers[num_layers - 1] = l.first;
Roman Stratiienko0fade372021-02-20 13:59:55 +0200605 fences[num_layers - 1] = l.second.release_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500606 }
607 *num_elements = num_layers;
608 return HWC2::Error::None;
609}
610
Roman Stratiienko0fade372021-02-20 13:59:55 +0200611void DrmHwcTwo::HwcDisplay::AddFenceToPresentFence(UniqueFd fd) {
612 if (!fd) {
Sean Paulac874152016-03-10 16:00:26 -0500613 return;
Roman Stratiienko0fade372021-02-20 13:59:55 +0200614 }
Sean Paulac874152016-03-10 16:00:26 -0500615
Roman Stratiienko0fade372021-02-20 13:59:55 +0200616 if (present_fence_) {
617 present_fence_ = UniqueFd(
618 sync_merge("dc_present", present_fence_.Get(), fd.Get()));
Sean Paulac874152016-03-10 16:00:26 -0500619 } else {
Roman Stratiienko0fade372021-02-20 13:59:55 +0200620 present_fence_ = std::move(fd);
Sean Paulac874152016-03-10 16:00:26 -0500621 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500622}
623
Rob Herring4f6c62e2018-05-17 14:33:02 -0500624HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500625 // order the layers by z-order
626 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100627 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500628 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
629 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200630 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500631 case HWC2::Composition::Device:
632 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
633 break;
634 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100635 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500636 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100637 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500638 break;
639 default:
640 continue;
641 }
642 }
643 if (use_client_layer)
644 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
645
Rob Herring4f6c62e2018-05-17 14:33:02 -0500646 if (z_map.empty())
647 return HWC2::Error::BadLayer;
648
Matvii Zorin5368b732021-01-12 10:53:08 +0200649 std::vector<DrmHwcLayer> composition_layers;
650
Sean Paulac874152016-03-10 16:00:26 -0500651 // now that they're ordered by z, add them to the composition
652 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
653 DrmHwcLayer layer;
654 l.second->PopulateDrmLayer(&layer);
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200655 int ret = layer.ImportBuffer(drm_);
Sean Paulac874152016-03-10 16:00:26 -0500656 if (ret) {
657 ALOGE("Failed to import layer, ret=%d", ret);
658 return HWC2::Error::NoResources;
659 }
Matvii Zorin5368b732021-01-12 10:53:08 +0200660 composition_layers.emplace_back(std::move(layer));
Sean Paulac874152016-03-10 16:00:26 -0500661 }
Sean Paulac874152016-03-10 16:00:26 -0500662
Matvii Zorin704ea0e2021-01-08 12:55:45 +0200663 auto composition = std::make_unique<DrmDisplayComposition>(crtc_,
664 planner_.get());
Sean Paulac874152016-03-10 16:00:26 -0500665
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200666 // TODO(nobody): Don't always assume geometry changed
Matvii Zorin5368b732021-01-12 10:53:08 +0200667 int ret = composition->SetLayers(composition_layers.data(),
668 composition_layers.size(), true);
Sean Paulac874152016-03-10 16:00:26 -0500669 if (ret) {
670 ALOGE("Failed to set layers in the composition ret=%d", ret);
671 return HWC2::Error::BadLayer;
672 }
673
674 std::vector<DrmPlane *> primary_planes(primary_planes_);
675 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500676 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500677 if (ret) {
678 ALOGE("Failed to plan the composition ret=%d", ret);
679 return HWC2::Error::BadConfig;
680 }
681
682 // Disable the planes we're not using
683 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
684 composition->AddPlaneDisable(*i);
685 i = primary_planes.erase(i);
686 }
687 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
688 composition->AddPlaneDisable(*i);
689 i = overlay_planes.erase(i);
690 }
691
Rob Herring4f6c62e2018-05-17 14:33:02 -0500692 if (test) {
693 ret = compositor_.TestComposition(composition.get());
694 } else {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500695 ret = compositor_.ApplyComposition(std::move(composition));
Matteo Franchinc56eede2019-12-03 17:10:38 +0000696 AddFenceToPresentFence(compositor_.TakeOutFence());
Rob Herring4f6c62e2018-05-17 14:33:02 -0500697 }
Sean Paulac874152016-03-10 16:00:26 -0500698 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700699 if (!test)
700 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500701 return HWC2::Error::BadParameter;
702 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500703 return HWC2::Error::None;
704}
705
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300706/* Find API details at:
707 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
708 */
Matteo Franchinc56eede2019-12-03 17:10:38 +0000709HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500710 supported(__func__);
711 HWC2::Error ret;
712
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200713 ++total_stats_.total_frames_;
714
Rob Herring4f6c62e2018-05-17 14:33:02 -0500715 ret = CreateComposition(false);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200716 if (ret != HWC2::Error::None)
717 ++total_stats_.failed_kms_present_;
718
Rob Herring4f6c62e2018-05-17 14:33:02 -0500719 if (ret == HWC2::Error::BadLayer) {
720 // Can we really have no client or device layers?
Matteo Franchinc56eede2019-12-03 17:10:38 +0000721 *present_fence = -1;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500722 return HWC2::Error::None;
723 }
724 if (ret != HWC2::Error::None)
725 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500726
Matteo Franchinc56eede2019-12-03 17:10:38 +0000727 *present_fence = present_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500728
729 ++frame_no_;
730 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500731}
732
733HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500734 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400735 auto mode = std::find_if(connector_->modes().begin(),
736 connector_->modes().end(),
737 [config](DrmMode const &m) {
738 return m.id() == config;
739 });
Sean Paulac874152016-03-10 16:00:26 -0500740 if (mode == connector_->modes().end()) {
741 ALOGE("Could not find active mode for %d", config);
742 return HWC2::Error::BadConfig;
743 }
744
Matvii Zorin704ea0e2021-01-08 12:55:45 +0200745 auto composition = std::make_unique<DrmDisplayComposition>(crtc_,
746 planner_.get());
Sean Paulac874152016-03-10 16:00:26 -0500747 int ret = composition->SetDisplayMode(*mode);
Roman Stratiienko6a10c4c2021-02-15 11:25:23 +0200748 if (ret) {
749 return HWC2::Error::BadConfig;
750 }
Sean Pauled45a8e2017-02-28 13:17:34 -0500751 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500752 if (ret) {
753 ALOGE("Failed to queue dpms composition on %d", ret);
754 return HWC2::Error::BadConfig;
755 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300756
757 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500758
759 // Setup the client layer's dimensions
760 hwc_rect_t display_frame = {.left = 0,
761 .top = 0,
762 .right = static_cast<int>(mode->h_display()),
763 .bottom = static_cast<int>(mode->v_display())};
764 client_layer_.SetLayerDisplayFrame(display_frame);
Sean Paulac874152016-03-10 16:00:26 -0500765
766 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500767}
768
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300769/* Find API details at:
770 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
771 */
Sean Pauled2ec4b2016-03-10 15:35:40 -0500772HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
773 int32_t acquire_fence,
774 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600775 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500776 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500777
778 client_layer_.set_buffer(target);
Roman Stratiienko0fade372021-02-20 13:59:55 +0200779 client_layer_.acquire_fence_ = UniqueFd(
780 fcntl(acquire_fence, F_DUPFD_CLOEXEC));
Sean Paulac874152016-03-10 16:00:26 -0500781 client_layer_.SetLayerDataspace(dataspace);
Roman Stratiienko33365c22020-10-10 23:06:36 +0300782
783 /* TODO: Do not update source_crop every call.
784 * It makes sense to do it once after every hotplug event. */
785 hwc_drm_bo bo{};
786 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
787
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200788 hwc_frect_t source_crop = {.left = 0.0F,
789 .top = 0.0F,
790 .right = bo.width + 0.0F,
791 .bottom = bo.height + 0.0F};
Roman Stratiienko33365c22020-10-10 23:06:36 +0300792 client_layer_.SetLayerSourceCrop(source_crop);
793
Sean Paulac874152016-03-10 16:00:26 -0500794 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500795}
796
797HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500798 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800799
Roman Stratiienkod146d6d2020-10-04 23:56:46 +0300800 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
801 return HWC2::Error::BadParameter;
802
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800803 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +0300804 return HWC2::Error::Unsupported;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800805
806 color_mode_ = mode;
807 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500808}
809
810HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500811 int32_t hint) {
812 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200813 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
814 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
815 return HWC2::Error::BadParameter;
816
817 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
818 return HWC2::Error::BadParameter;
819
820 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
821 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
822 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
823
824 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500825}
826
827HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500828 int32_t release_fence) {
829 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200830 // TODO(nobody): Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500831 return unsupported(__func__, buffer, release_fence);
832}
833
Sean Paulac874152016-03-10 16:00:26 -0500834HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
835 supported(__func__);
836 uint64_t dpms_value = 0;
837 auto mode = static_cast<HWC2::PowerMode>(mode_in);
838 switch (mode) {
839 case HWC2::PowerMode::Off:
840 dpms_value = DRM_MODE_DPMS_OFF;
841 break;
842 case HWC2::PowerMode::On:
843 dpms_value = DRM_MODE_DPMS_ON;
844 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100845 case HWC2::PowerMode::Doze:
846 case HWC2::PowerMode::DozeSuspend:
847 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500848 default:
849 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100850 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500851 };
852
Matvii Zorin704ea0e2021-01-08 12:55:45 +0200853 auto composition = std::make_unique<DrmDisplayComposition>(crtc_,
854 planner_.get());
Sean Paulac874152016-03-10 16:00:26 -0500855 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500856 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500857 if (ret) {
858 ALOGE("Failed to apply the dpms composition ret=%d", ret);
859 return HWC2::Error::BadParameter;
860 }
861 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500862}
863
864HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500865 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300866 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500867 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500868}
869
870HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500871 uint32_t *num_requests) {
872 supported(__func__);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500873
Matvii Zorinef3c7972020-08-11 15:15:44 +0300874 return backend_->ValidateDisplay(this, num_types, num_requests);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500875}
876
Matvii Zorined90ef92021-01-29 18:32:06 +0200877std::vector<DrmHwcTwo::HwcLayer *>
878DrmHwcTwo::HwcDisplay::GetOrderLayersByZPos() {
879 std::vector<DrmHwcTwo::HwcLayer *> ordered_layers;
880 ordered_layers.reserve(layers_.size());
881
882 for (auto &[handle, layer] : layers_) {
883 ordered_layers.emplace_back(&layer);
884 }
885
886 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
887 [](const DrmHwcTwo::HwcLayer *lhs, const DrmHwcTwo::HwcLayer *rhs) {
888 return lhs->z_order() < rhs->z_order();
889 });
890
891 return ordered_layers;
892}
893
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300894#if PLATFORM_SDK_VERSION > 29
895HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
896 if (connector_->internal())
897 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
898 else if (connector_->external())
899 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
900 else
901 return HWC2::Error::BadConfig;
902
903 return HWC2::Error::None;
904}
905
906HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayVsyncPeriod(
907 hwc2_vsync_period_t *outVsyncPeriod /* ns */) {
908 supported(__func__);
909 DrmMode const &mode = connector_->active_mode();
910 if (mode.id() == 0)
911 return HWC2::Error::BadConfig;
912
913 *outVsyncPeriod = 1E9 / mode.v_refresh();
914 return HWC2::Error::None;
915}
916
917HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfigWithConstraints(
918 hwc2_config_t /*config*/,
919 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
920 hwc_vsync_period_change_timeline_t *outTimeline) {
921 supported(__func__);
922
923 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
924 return HWC2::Error::BadParameter;
925 }
926
927 return HWC2::Error::BadConfig;
928}
929
930HWC2::Error DrmHwcTwo::HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
931 return HWC2::Error::Unsupported;
932}
933
934HWC2::Error DrmHwcTwo::HwcDisplay::GetSupportedContentTypes(
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200935 uint32_t *outNumSupportedContentTypes,
936 const uint32_t *outSupportedContentTypes) {
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300937 if (outSupportedContentTypes == nullptr)
938 *outNumSupportedContentTypes = 0;
939
940 return HWC2::Error::None;
941}
942
943HWC2::Error DrmHwcTwo::HwcDisplay::SetContentType(int32_t contentType) {
944 supported(__func__);
945
946 if (contentType != HWC2_CONTENT_TYPE_NONE)
947 return HWC2::Error::Unsupported;
948
949 /* TODO: Map to the DRM Connector property:
950 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
951 */
952
953 return HWC2::Error::None;
954}
955#endif
956
John Stultz8c7229d2020-02-07 21:31:08 +0000957#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800958HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
959 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
960 supported(__func__);
961
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200962 drmModePropertyBlobPtr blob = nullptr;
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800963
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300964 if (connector_->GetEdidBlob(blob)) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800965 ALOGE("Failed to get edid property value.");
966 return HWC2::Error::Unsupported;
967 }
968
Andrii Chepurnyi8115dbe2020-04-14 13:03:57 +0300969 if (outData) {
970 *outDataSize = std::min(*outDataSize, blob->length);
971 memcpy(outData, blob->data, *outDataSize);
972 } else {
973 *outDataSize = blob->length;
974 }
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800975 *outPort = connector_->id();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800976
977 return HWC2::Error::None;
978}
979
980HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
981 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
982 unsupported(__func__, outCapabilities);
983
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200984 if (outNumCapabilities == nullptr) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800985 return HWC2::Error::BadParameter;
986 }
987
988 *outNumCapabilities = 0;
989
990 return HWC2::Error::None;
991}
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +0300992
993HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayBrightnessSupport(
994 bool *supported) {
995 *supported = false;
996 return HWC2::Error::None;
997}
998
999HWC2::Error DrmHwcTwo::HwcDisplay::SetDisplayBrightness(
1000 float /* brightness */) {
1001 return HWC2::Error::Unsupported;
1002}
1003
John Stultz8c7229d2020-02-07 21:31:08 +00001004#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001005
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001006#if PLATFORM_SDK_VERSION > 27
1007
1008HWC2::Error DrmHwcTwo::HwcDisplay::GetRenderIntents(
1009 int32_t mode, uint32_t *outNumIntents,
1010 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1011 if (mode != HAL_COLOR_MODE_NATIVE) {
1012 return HWC2::Error::BadParameter;
1013 }
1014
1015 if (outIntents == nullptr) {
1016 *outNumIntents = 1;
1017 return HWC2::Error::None;
1018 }
1019 *outNumIntents = 1;
1020 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1021 return HWC2::Error::None;
1022}
1023
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001024HWC2::Error DrmHwcTwo::HwcDisplay::SetColorModeWithIntent(int32_t mode,
1025 int32_t intent) {
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001026 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1027 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1028 return HWC2::Error::BadParameter;
1029
1030 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
1031 return HWC2::Error::BadParameter;
1032
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001033 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001034 return HWC2::Error::Unsupported;
1035
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001036 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001037 return HWC2::Error::Unsupported;
1038
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001039 color_mode_ = mode;
1040 return HWC2::Error::None;
1041}
1042
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001043#endif /* PLATFORM_SDK_VERSION > 27 */
1044
Sean Pauled2ec4b2016-03-10 15:35:40 -05001045HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -05001046 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -08001047 cursor_x_ = x;
1048 cursor_y_ = y;
1049 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001050}
1051
1052HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -05001053 supported(__func__);
1054 blending_ = static_cast<HWC2::BlendMode>(mode);
1055 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001056}
1057
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +03001058/* Find API details at:
1059 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=2314
1060 */
Sean Pauled2ec4b2016-03-10 15:35:40 -05001061HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001062 int32_t acquire_fence) {
1063 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -05001064
Sean Paulac874152016-03-10 16:00:26 -05001065 set_buffer(buffer);
Roman Stratiienko0fade372021-02-20 13:59:55 +02001066 acquire_fence_ = UniqueFd(fcntl(acquire_fence, F_DUPFD_CLOEXEC));
Sean Paulac874152016-03-10 16:00:26 -05001067 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001068}
1069
1070HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001071 // TODO(nobody): Put to client composition here?
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001072 supported(__func__);
1073 layer_color_ = color;
1074 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001075}
1076
1077HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001078 sf_type_ = static_cast<HWC2::Composition>(type);
1079 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001080}
1081
1082HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001083 supported(__func__);
1084 dataspace_ = static_cast<android_dataspace_t>(dataspace);
1085 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001086}
1087
1088HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001089 supported(__func__);
1090 display_frame_ = frame;
1091 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001092}
1093
1094HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001095 supported(__func__);
1096 alpha_ = alpha;
1097 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001098}
1099
1100HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1101 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001102 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001103 // TODO(nobody): We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001104 return unsupported(__func__, stream);
1105}
1106
1107HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001108 supported(__func__);
1109 source_crop_ = crop;
1110 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001111}
1112
1113HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001114 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001115 // TODO(nobody): We don't use surface damage, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001116 unsupported(__func__, damage);
1117 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001118}
1119
1120HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001121 supported(__func__);
1122 transform_ = static_cast<HWC2::Transform>(transform);
1123 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001124}
1125
1126HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001127 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001128 // TODO(nobody): We don't use this information, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001129 unsupported(__func__, visible);
1130 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001131}
1132
Sean Paulac874152016-03-10 16:00:26 -05001133HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1134 supported(__func__);
1135 z_order_ = order;
1136 return HWC2::Error::None;
1137}
1138
1139void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1140 supported(__func__);
1141 switch (blending_) {
1142 case HWC2::BlendMode::None:
1143 layer->blending = DrmHwcBlending::kNone;
1144 break;
1145 case HWC2::BlendMode::Premultiplied:
1146 layer->blending = DrmHwcBlending::kPreMult;
1147 break;
1148 case HWC2::BlendMode::Coverage:
1149 layer->blending = DrmHwcBlending::kCoverage;
1150 break;
1151 default:
1152 ALOGE("Unknown blending mode b=%d", blending_);
1153 layer->blending = DrmHwcBlending::kNone;
1154 break;
1155 }
1156
Sean Paulac874152016-03-10 16:00:26 -05001157 layer->sf_handle = buffer_;
Roman Stratiienko0fade372021-02-20 13:59:55 +02001158 // TODO(rsglobal): Avoid extra fd duplication
1159 layer->acquire_fence = UniqueFd(fcntl(acquire_fence_.Get(), F_DUPFD_CLOEXEC));
Roman Kovalivskyib8652272021-01-24 20:32:37 +02001160 layer->display_frame = display_frame_;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001161 layer->alpha = lround(65535.0F * alpha_);
Roman Kovalivskyib8652272021-01-24 20:32:37 +02001162 layer->source_crop = source_crop_;
Sean Paulac874152016-03-10 16:00:26 -05001163 layer->SetTransform(static_cast<int32_t>(transform_));
Matvii Zorin8338c342020-09-08 16:12:51 +03001164 layer->dataspace = dataspace_;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001165}
1166
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001167void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
Roman Stratiienko23701092020-09-26 02:08:41 +03001168 const std::lock_guard<std::mutex> lock(hotplug_callback_lock);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001169
Roman Stratiienko23701092020-09-26 02:08:41 +03001170 if (hotplug_callback_hook_ && hotplug_callback_data_)
1171 hotplug_callback_hook_(hotplug_callback_data_, displayid,
1172 state == DRM_MODE_CONNECTED
1173 ? HWC2_CONNECTION_CONNECTED
1174 : HWC2_CONNECTION_DISCONNECTED);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001175}
1176
1177void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
Roman Stratiienkod21071f2021-03-09 21:56:50 +02001178 for (const auto &conn : drmDevice->connectors()) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001179 if (conn->state() != DRM_MODE_CONNECTED)
1180 continue;
1181 HandleDisplayHotplug(conn->display(), conn->state());
1182 }
1183}
1184
1185void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
Roman Stratiienkod21071f2021-03-09 21:56:50 +02001186 for (const auto &conn : drm_->connectors()) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001187 drmModeConnection old_state = conn->state();
1188 drmModeConnection cur_state = conn->UpdateModes()
1189 ? DRM_MODE_UNKNOWNCONNECTION
1190 : conn->state();
1191
1192 if (cur_state == old_state)
1193 continue;
1194
1195 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
1196 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1197 conn->id(), conn->display());
1198
1199 int display_id = conn->display();
1200 if (cur_state == DRM_MODE_CONNECTED) {
1201 auto &display = hwc2_->displays_.at(display_id);
1202 display.ChosePreferredConfig();
1203 } else {
1204 auto &display = hwc2_->displays_.at(display_id);
1205 display.ClearDisplay();
1206 }
1207
1208 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1209 }
1210}
1211
Sean Pauled2ec4b2016-03-10 15:35:40 -05001212// static
1213int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1214 unsupported(__func__);
1215 return 0;
1216}
1217
1218// static
1219void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001220 uint32_t *out_count,
1221 int32_t * /*out_capabilities*/) {
1222 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001223 *out_count = 0;
1224}
1225
1226// static
Sean Paulac874152016-03-10 16:00:26 -05001227hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1228 struct hwc2_device * /*dev*/, int32_t descriptor) {
1229 supported(__func__);
1230 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001231 switch (func) {
1232 // Device functions
1233 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1234 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1235 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1236 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001237 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001238 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1239 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1240 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1241 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1242 case HWC2::FunctionDescriptor::Dump:
1243 return ToHook<HWC2_PFN_DUMP>(
1244 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1245 uint32_t *, char *>);
1246 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1247 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1248 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1249 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1250 case HWC2::FunctionDescriptor::RegisterCallback:
1251 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1252 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1253 &DrmHwcTwo::RegisterCallback, int32_t,
1254 hwc2_callback_data_t, hwc2_function_pointer_t>);
1255
1256 // Display functions
1257 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1258 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1259 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1260 &HwcDisplay::AcceptDisplayChanges>);
1261 case HWC2::FunctionDescriptor::CreateLayer:
1262 return ToHook<HWC2_PFN_CREATE_LAYER>(
1263 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1264 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1265 case HWC2::FunctionDescriptor::DestroyLayer:
1266 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1267 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1268 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1269 case HWC2::FunctionDescriptor::GetActiveConfig:
1270 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1271 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1272 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1273 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1274 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1275 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1276 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1277 hwc2_layer_t *, int32_t *>);
1278 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1279 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1280 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1281 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1282 int32_t, int32_t>);
1283 case HWC2::FunctionDescriptor::GetColorModes:
1284 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1285 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1286 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1287 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001288 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1289 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1290 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1291 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001292 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001293 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1294 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1295 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1296 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001297 case HWC2::FunctionDescriptor::GetDisplayName:
1298 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1299 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1300 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1301 case HWC2::FunctionDescriptor::GetDisplayRequests:
1302 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1303 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1304 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1305 hwc2_layer_t *, int32_t *>);
1306 case HWC2::FunctionDescriptor::GetDisplayType:
1307 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1308 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1309 &HwcDisplay::GetDisplayType, int32_t *>);
1310 case HWC2::FunctionDescriptor::GetDozeSupport:
1311 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1312 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1313 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001314 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1315 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1316 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1317 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1318 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001319 case HWC2::FunctionDescriptor::GetReleaseFences:
1320 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1321 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1322 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1323 int32_t *>);
1324 case HWC2::FunctionDescriptor::PresentDisplay:
1325 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1326 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1327 &HwcDisplay::PresentDisplay, int32_t *>);
1328 case HWC2::FunctionDescriptor::SetActiveConfig:
1329 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1330 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1331 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1332 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001333 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1334 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1335 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1336 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001337 case HWC2::FunctionDescriptor::SetColorMode:
1338 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1339 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1340 &HwcDisplay::SetColorMode, int32_t>);
1341 case HWC2::FunctionDescriptor::SetColorTransform:
1342 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1343 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1344 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1345 case HWC2::FunctionDescriptor::SetOutputBuffer:
1346 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1347 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1348 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1349 case HWC2::FunctionDescriptor::SetPowerMode:
1350 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1351 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1352 &HwcDisplay::SetPowerMode, int32_t>);
1353 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1354 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1355 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1356 &HwcDisplay::SetVsyncEnabled, int32_t>);
1357 case HWC2::FunctionDescriptor::ValidateDisplay:
1358 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1359 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1360 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001361#if PLATFORM_SDK_VERSION > 27
1362 case HWC2::FunctionDescriptor::GetRenderIntents:
1363 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
1364 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
1365 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
1366 int32_t *>);
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001367 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
1368 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
1369 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
1370 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001371#endif
John Stultz8c7229d2020-02-07 21:31:08 +00001372#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001373 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1374 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1375 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1376 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1377 uint32_t *, uint8_t *>);
1378 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1379 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1380 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1381 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1382 uint32_t *>);
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001383 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
1384 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
1385 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
1386 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
1387 case HWC2::FunctionDescriptor::SetDisplayBrightness:
1388 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
1389 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
1390 &HwcDisplay::SetDisplayBrightness, float>);
John Stultz8c7229d2020-02-07 21:31:08 +00001391#endif /* PLATFORM_SDK_VERSION > 28 */
Roman Stratiienko6f5df172020-09-27 22:48:08 +03001392#if PLATFORM_SDK_VERSION > 29
1393 case HWC2::FunctionDescriptor::GetDisplayConnectionType:
1394 return ToHook<HWC2_PFN_GET_DISPLAY_CONNECTION_TYPE>(
1395 DisplayHook<decltype(&HwcDisplay::GetDisplayConnectionType),
1396 &HwcDisplay::GetDisplayConnectionType, uint32_t *>);
1397 case HWC2::FunctionDescriptor::GetDisplayVsyncPeriod:
1398 return ToHook<HWC2_PFN_GET_DISPLAY_VSYNC_PERIOD>(
1399 DisplayHook<decltype(&HwcDisplay::GetDisplayVsyncPeriod),
1400 &HwcDisplay::GetDisplayVsyncPeriod,
1401 hwc2_vsync_period_t *>);
1402 case HWC2::FunctionDescriptor::SetActiveConfigWithConstraints:
1403 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG_WITH_CONSTRAINTS>(
1404 DisplayHook<decltype(&HwcDisplay::SetActiveConfigWithConstraints),
1405 &HwcDisplay::SetActiveConfigWithConstraints,
1406 hwc2_config_t, hwc_vsync_period_change_constraints_t *,
1407 hwc_vsync_period_change_timeline_t *>);
1408 case HWC2::FunctionDescriptor::SetAutoLowLatencyMode:
1409 return ToHook<HWC2_PFN_SET_AUTO_LOW_LATENCY_MODE>(
1410 DisplayHook<decltype(&HwcDisplay::SetAutoLowLatencyMode),
1411 &HwcDisplay::SetAutoLowLatencyMode, bool>);
1412 case HWC2::FunctionDescriptor::GetSupportedContentTypes:
1413 return ToHook<HWC2_PFN_GET_SUPPORTED_CONTENT_TYPES>(
1414 DisplayHook<decltype(&HwcDisplay::GetSupportedContentTypes),
1415 &HwcDisplay::GetSupportedContentTypes, uint32_t *,
1416 uint32_t *>);
1417 case HWC2::FunctionDescriptor::SetContentType:
1418 return ToHook<HWC2_PFN_SET_CONTENT_TYPE>(
1419 DisplayHook<decltype(&HwcDisplay::SetContentType),
1420 &HwcDisplay::SetContentType, int32_t>);
1421#endif
Sean Pauled2ec4b2016-03-10 15:35:40 -05001422 // Layer functions
1423 case HWC2::FunctionDescriptor::SetCursorPosition:
1424 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1425 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1426 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1427 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1428 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1429 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1430 &HwcLayer::SetLayerBlendMode, int32_t>);
1431 case HWC2::FunctionDescriptor::SetLayerBuffer:
1432 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1433 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1434 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1435 case HWC2::FunctionDescriptor::SetLayerColor:
1436 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1437 LayerHook<decltype(&HwcLayer::SetLayerColor),
1438 &HwcLayer::SetLayerColor, hwc_color_t>);
1439 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1440 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1441 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1442 &HwcLayer::SetLayerCompositionType, int32_t>);
1443 case HWC2::FunctionDescriptor::SetLayerDataspace:
1444 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1445 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1446 &HwcLayer::SetLayerDataspace, int32_t>);
1447 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1448 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1449 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1450 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1451 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1452 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1453 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1454 &HwcLayer::SetLayerPlaneAlpha, float>);
1455 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001456 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1457 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1458 &HwcLayer::SetLayerSidebandStream,
1459 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001460 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1461 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1462 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1463 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1464 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1465 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1466 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1467 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1468 case HWC2::FunctionDescriptor::SetLayerTransform:
1469 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1470 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1471 &HwcLayer::SetLayerTransform, int32_t>);
1472 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1473 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1474 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1475 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1476 case HWC2::FunctionDescriptor::SetLayerZOrder:
1477 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1478 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1479 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001480 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001481 default:
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001482 return nullptr;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001483 }
1484}
Sean Paulac874152016-03-10 16:00:26 -05001485
1486// static
1487int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1488 struct hw_device_t **dev) {
1489 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001490 if (strcmp(name, HWC_HARDWARE_COMPOSER) != 0) {
Sean Paulac874152016-03-10 16:00:26 -05001491 ALOGE("Invalid module name- %s", name);
1492 return -EINVAL;
1493 }
1494
1495 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1496 if (!ctx) {
1497 ALOGE("Failed to allocate DrmHwcTwo");
1498 return -ENOMEM;
1499 }
1500
1501 HWC2::Error err = ctx->Init();
1502 if (err != HWC2::Error::None) {
1503 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1504 return -EINVAL;
1505 }
1506
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001507 ctx->common.module = (hw_module_t *)module;
Sean Paulac874152016-03-10 16:00:26 -05001508 *dev = &ctx->common;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001509 ctx.release(); // NOLINT(bugprone-unused-return-value)
Sean Paulac874152016-03-10 16:00:26 -05001510 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001511}
Sean Paulf72cccd2018-08-27 13:59:08 -04001512} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001513
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001514// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001515static struct hw_module_methods_t hwc2_module_methods = {
1516 .open = android::DrmHwcTwo::HookDevOpen,
1517};
1518
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001519// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001520hw_module_t HAL_MODULE_INFO_SYM = {
1521 .tag = HARDWARE_MODULE_TAG,
1522 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1523 .id = HWC_HARDWARE_MODULE_ID,
1524 .name = "DrmHwcTwo module",
1525 .author = "The Android Open Source Project",
1526 .methods = &hwc2_module_methods,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001527 .dso = nullptr,
Sean Paulac874152016-03-10 16:00:26 -05001528 .reserved = {0},
1529};