blob: 10177e9bc71844eae78b8abcb1cebb88459868c1 [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 Stratiienko863a3c22021-09-29 13:00:29 +030057 std::forward_as_tuple(&resource_manager_, drm, displ, type,
58 this));
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 Stratiienko863a3c22021-09-29 13:00:29 +0300186 std::unique_lock<std::mutex> lock(callback_lock_);
187
Roman Stratiienko23701092020-09-26 02:08:41 +0300188 switch (static_cast<HWC2::Callback>(descriptor)) {
Sean Paulac874152016-03-10 16:00:26 -0500189 case HWC2::Callback::Hotplug: {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300190 hotplug_callback_ = std::make_pair(HWC2_PFN_HOTPLUG(function), data);
191 lock.unlock();
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200192 const auto &drm_devices = resource_manager_.getDrmDevices();
193 for (const auto &device : drm_devices)
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300194 HandleInitialHotplugState(device.get());
Sean Paulac874152016-03-10 16:00:26 -0500195 break;
196 }
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200197 case HWC2::Callback::Refresh: {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300198 refresh_callback_ = std::make_pair(HWC2_PFN_REFRESH(function), data);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200199 break;
200 }
Sean Paulac874152016-03-10 16:00:26 -0500201 case HWC2::Callback::Vsync: {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300202 vsync_callback_ = std::make_pair(HWC2_PFN_VSYNC(function), data);
Sean Paulac874152016-03-10 16:00:26 -0500203 break;
204 }
Roman Stratiienko11ef8c52021-09-29 13:01:39 +0300205#if PLATFORM_SDK_VERSION > 29
206 case HWC2::Callback::Vsync_2_4: {
207 vsync_2_4_callback_ = std::make_pair(HWC2_PFN_VSYNC_2_4(function), data);
208 break;
209 }
210#endif
Sean Paulac874152016-03-10 16:00:26 -0500211 default:
212 break;
213 }
214 return HWC2::Error::None;
215}
216
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100217DrmHwcTwo::HwcDisplay::HwcDisplay(ResourceManager *resource_manager,
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200218 DrmDevice *drm, hwc2_display_t handle,
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300219 HWC2::DisplayType type, DrmHwcTwo *hwc2)
220 : hwc2_(hwc2),
221 resource_manager_(resource_manager),
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100222 drm_(drm),
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100223 handle_(handle),
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200224 type_(type),
225 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
Sean Paulac874152016-03-10 16:00:26 -0500226 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200227
228 // clang-format off
229 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
230 0.0, 1.0, 0.0, 0.0,
231 0.0, 0.0, 1.0, 0.0,
232 0.0, 0.0, 0.0, 1.0};
233 // clang-format on
Sean Paulac874152016-03-10 16:00:26 -0500234}
235
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300236void DrmHwcTwo::HwcDisplay::ClearDisplay() {
237 compositor_.ClearDisplay();
238}
239
Sean Paulac874152016-03-10 16:00:26 -0500240HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
241 supported(__func__);
242 planner_ = Planner::CreateInstance(drm_);
243 if (!planner_) {
244 ALOGE("Failed to create planner instance for composition");
245 return HWC2::Error::NoResources;
246 }
247
248 int display = static_cast<int>(handle_);
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300249 int ret = compositor_.Init(resource_manager_, display, [this] {
250 /* refresh callback */
251 const std::lock_guard<std::mutex> lock(hwc2_->callback_lock_);
252 if (hwc2_->refresh_callback_.first != nullptr &&
253 hwc2_->refresh_callback_.second != nullptr) {
254 hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_);
255 }
256 });
Sean Paulac874152016-03-10 16:00:26 -0500257 if (ret) {
258 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
259 return HWC2::Error::NoResources;
260 }
261
262 // Split up the given display planes into primary and overlay to properly
263 // interface with the composition
264 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
Jason Macnakf1af9572020-08-20 11:49:51 -0700265 property_get("vendor.hwc.drm.use_overlay_planes", use_overlay_planes_prop,
266 "1");
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200267 bool use_overlay_planes = strtol(use_overlay_planes_prop, nullptr, 10);
Sean Paulac874152016-03-10 16:00:26 -0500268 for (auto &plane : *planes) {
269 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
270 primary_planes_.push_back(plane);
271 else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY)
272 overlay_planes_.push_back(plane);
273 }
274
275 crtc_ = drm_->GetCrtcForDisplay(display);
276 if (!crtc_) {
277 ALOGE("Failed to get crtc for display %d", display);
278 return HWC2::Error::BadDisplay;
279 }
280
281 connector_ = drm_->GetConnectorForDisplay(display);
282 if (!connector_) {
283 ALOGE("Failed to get connector for display %d", display);
284 return HWC2::Error::BadDisplay;
285 }
286
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300287 ret = vsync_worker_.Init(drm_, display, [this](int64_t timestamp) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300288 const std::lock_guard<std::mutex> lock(hwc2_->callback_lock_);
Roman Stratiienko11ef8c52021-09-29 13:01:39 +0300289 /* vsync callback */
290#if PLATFORM_SDK_VERSION > 29
291 if (hwc2_->vsync_2_4_callback_.first != nullptr &&
292 hwc2_->vsync_2_4_callback_.second != nullptr) {
293 hwc2_vsync_period_t period_ns{};
294 GetDisplayVsyncPeriod(&period_ns);
295 hwc2_->vsync_2_4_callback_.first(hwc2_->vsync_2_4_callback_.second,
296 handle_, timestamp, period_ns);
297 } else
298#endif
299 if (hwc2_->vsync_callback_.first != nullptr &&
300 hwc2_->vsync_callback_.second != nullptr) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300301 hwc2_->vsync_callback_.first(hwc2_->vsync_callback_.second, handle_,
302 timestamp);
303 }
304 });
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300305 if (ret) {
306 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
307 return HWC2::Error::BadDisplay;
308 }
309
Matvii Zorinef3c7972020-08-11 15:15:44 +0300310 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
311 if (ret) {
312 ALOGE("Failed to set backend for d=%d %d\n", display, ret);
313 return HWC2::Error::BadDisplay;
314 }
315
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300316 return ChosePreferredConfig();
317}
318
319HWC2::Error DrmHwcTwo::HwcDisplay::ChosePreferredConfig() {
Sean Paulac874152016-03-10 16:00:26 -0500320 // Fetch the number of modes from the display
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200321 uint32_t num_configs = 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200322 HWC2::Error err = GetDisplayConfigs(&num_configs, nullptr);
Sean Paulac874152016-03-10 16:00:26 -0500323 if (err != HWC2::Error::None || !num_configs)
324 return err;
325
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200326 return SetActiveConfig(connector_->get_preferred_mode_id());
Sean Paulac874152016-03-10 16:00:26 -0500327}
328
Sean Pauled2ec4b2016-03-10 15:35:40 -0500329HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500330 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500331 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
332 l.second.accept_type_change();
333 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500334}
335
336HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500337 supported(__func__);
338 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
339 *layer = static_cast<hwc2_layer_t>(layer_idx_);
340 ++layer_idx_;
341 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500342}
343
344HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Sean Paulac874152016-03-10 16:00:26 -0500345 supported(__func__);
Vincent Donnefort9abec032019-10-09 15:43:43 +0100346 if (!get_layer(layer))
347 return HWC2::Error::BadLayer;
348
Sean Paulac874152016-03-10 16:00:26 -0500349 layers_.erase(layer);
350 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500351}
352
353HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
Sean Paulac874152016-03-10 16:00:26 -0500354 supported(__func__);
355 DrmMode const &mode = connector_->active_mode();
356 if (mode.id() == 0)
357 return HWC2::Error::BadConfig;
358
359 *config = mode.id();
360 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500361}
362
363HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
364 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500365 supported(__func__);
366 uint32_t num_changes = 0;
367 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
368 if (l.second.type_changed()) {
369 if (layers && num_changes < *num_elements)
370 layers[num_changes] = l.first;
371 if (types && num_changes < *num_elements)
372 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
373 ++num_changes;
374 }
375 }
376 if (!layers && !types)
377 *num_elements = num_changes;
378 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500379}
380
381HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500382 uint32_t height,
383 int32_t /*format*/,
384 int32_t dataspace) {
385 supported(__func__);
386 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
387 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
388
389 if (width < min.first || height < min.second)
390 return HWC2::Error::Unsupported;
391
392 if (width > max.first || height > max.second)
393 return HWC2::Error::Unsupported;
394
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200395 if (dataspace != HAL_DATASPACE_UNKNOWN)
Sean Paulac874152016-03-10 16:00:26 -0500396 return HWC2::Error::Unsupported;
397
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200398 // TODO(nobody): Validate format can be handled by either GL or planes
Sean Paulac874152016-03-10 16:00:26 -0500399 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500400}
401
402HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500403 int32_t *modes) {
404 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800405 if (!modes)
406 *num_modes = 1;
407
408 if (modes)
409 *modes = HAL_COLOR_MODE_NATIVE;
410
411 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500412}
413
414HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500415 int32_t attribute_in,
416 int32_t *value) {
417 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400418 auto mode = std::find_if(connector_->modes().begin(),
419 connector_->modes().end(),
420 [config](DrmMode const &m) {
421 return m.id() == config;
422 });
Sean Paulac874152016-03-10 16:00:26 -0500423 if (mode == connector_->modes().end()) {
424 ALOGE("Could not find active mode for %d", config);
425 return HWC2::Error::BadConfig;
426 }
427
428 static const int32_t kUmPerInch = 25400;
429 uint32_t mm_width = connector_->mm_width();
430 uint32_t mm_height = connector_->mm_height();
431 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
432 switch (attribute) {
433 case HWC2::Attribute::Width:
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300434 *value = static_cast<int>(mode->h_display());
Sean Paulac874152016-03-10 16:00:26 -0500435 break;
436 case HWC2::Attribute::Height:
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300437 *value = static_cast<int>(mode->v_display());
Sean Paulac874152016-03-10 16:00:26 -0500438 break;
439 case HWC2::Attribute::VsyncPeriod:
440 // in nanoseconds
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300441 *value = static_cast<int>(1E9 / mode->v_refresh());
Sean Paulac874152016-03-10 16:00:26 -0500442 break;
443 case HWC2::Attribute::DpiX:
444 // Dots per 1000 inches
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300445 *value = mm_width
446 ? static_cast<int>(mode->h_display() * kUmPerInch / mm_width)
447 : -1;
Sean Paulac874152016-03-10 16:00:26 -0500448 break;
449 case HWC2::Attribute::DpiY:
450 // Dots per 1000 inches
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300451 *value = mm_height ? static_cast<int>(mode->v_display() * kUmPerInch /
452 mm_height)
453 : -1;
Sean Paulac874152016-03-10 16:00:26 -0500454 break;
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300455#if PLATFORM_SDK_VERSION > 29
456 case HWC2::Attribute::ConfigGroup:
457 *value = 0; /* TODO: Add support for config groups */
458 break;
459#endif
Sean Paulac874152016-03-10 16:00:26 -0500460 default:
461 *value = -1;
462 return HWC2::Error::BadConfig;
463 }
464 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500465}
466
467HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
468 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500469 supported(__func__);
470 // Since this callback is normally invoked twice (once to get the count, and
471 // once to populate configs), we don't really want to read the edid
472 // redundantly. Instead, only update the modes on the first invocation. While
473 // it's possible this will result in stale modes, it'll all come out in the
474 // wash when we try to set the active config later.
475 if (!configs) {
476 int ret = connector_->UpdateModes();
477 if (ret) {
478 ALOGE("Failed to update display modes %d", ret);
479 return HWC2::Error::BadDisplay;
480 }
481 }
482
Neil Armstrongb67d0492019-06-20 09:00:21 +0000483 // Since the upper layers only look at vactive/hactive/refresh, height and
484 // width, it doesn't differentiate interlaced from progressive and other
485 // similar modes. Depending on the order of modes we return to SF, it could
486 // end up choosing a suboptimal configuration and dropping the preferred
487 // mode. To workaround this, don't offer interlaced modes to SF if there is
488 // at least one non-interlaced alternative and only offer a single WxH@R
489 // mode with at least the prefered mode from in DrmConnector::UpdateModes()
490
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200491 // TODO(nobody): Remove the following block of code until AOSP handles all
492 // modes
Neil Armstrongb67d0492019-06-20 09:00:21 +0000493 std::vector<DrmMode> sel_modes;
494
495 // Add the preferred mode first to be sure it's not dropped
496 auto mode = std::find_if(connector_->modes().begin(),
497 connector_->modes().end(), [&](DrmMode const &m) {
498 return m.id() ==
499 connector_->get_preferred_mode_id();
500 });
501 if (mode != connector_->modes().end())
502 sel_modes.push_back(*mode);
503
504 // Add the active mode if different from preferred mode
505 if (connector_->active_mode().id() != connector_->get_preferred_mode_id())
506 sel_modes.push_back(connector_->active_mode());
507
508 // Cycle over the modes and filter out "similar" modes, keeping only the
509 // first ones in the order given by DRM (from CEA ids and timings order)
Sean Paulac874152016-03-10 16:00:26 -0500510 for (const DrmMode &mode : connector_->modes()) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200511 // TODO(nobody): Remove this when 3D Attributes are in AOSP
Neil Armstrongb67d0492019-06-20 09:00:21 +0000512 if (mode.flags() & DRM_MODE_FLAG_3D_MASK)
513 continue;
514
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200515 // TODO(nobody): Remove this when the Interlaced attribute is in AOSP
Neil Armstrong4c027a72019-06-04 14:48:02 +0000516 if (mode.flags() & DRM_MODE_FLAG_INTERLACE) {
517 auto m = std::find_if(connector_->modes().begin(),
518 connector_->modes().end(),
519 [&mode](DrmMode const &m) {
520 return !(m.flags() & DRM_MODE_FLAG_INTERLACE) &&
521 m.h_display() == mode.h_display() &&
522 m.v_display() == mode.v_display();
523 });
Neil Armstrongb67d0492019-06-20 09:00:21 +0000524 if (m == connector_->modes().end())
525 sel_modes.push_back(mode);
526
527 continue;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000528 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000529
530 // Search for a similar WxH@R mode in the filtered list and drop it if
531 // another mode with the same WxH@R has already been selected
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200532 // TODO(nobody): Remove this when AOSP handles duplicates modes
Neil Armstrongb67d0492019-06-20 09:00:21 +0000533 auto m = std::find_if(sel_modes.begin(), sel_modes.end(),
534 [&mode](DrmMode const &m) {
535 return m.h_display() == mode.h_display() &&
536 m.v_display() == mode.v_display() &&
537 m.v_refresh() == mode.v_refresh();
538 });
539 if (m == sel_modes.end())
540 sel_modes.push_back(mode);
541 }
542
543 auto num_modes = static_cast<uint32_t>(sel_modes.size());
544 if (!configs) {
545 *num_configs = num_modes;
546 return HWC2::Error::None;
547 }
548
549 uint32_t idx = 0;
550 for (const DrmMode &mode : sel_modes) {
551 if (idx >= *num_configs)
552 break;
553 configs[idx++] = mode.id();
Sean Paulac874152016-03-10 16:00:26 -0500554 }
555 *num_configs = idx;
556 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500557}
558
559HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500560 supported(__func__);
561 std::ostringstream stream;
562 stream << "display-" << connector_->id();
563 std::string string = stream.str();
564 size_t length = string.length();
565 if (!name) {
566 *size = length;
567 return HWC2::Error::None;
568 }
569
570 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
571 strncpy(name, string.c_str(), *size);
572 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500573}
574
Sean Paulac874152016-03-10 16:00:26 -0500575HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
576 uint32_t *num_elements,
577 hwc2_layer_t *layers,
578 int32_t *layer_requests) {
579 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200580 // TODO(nobody): I think virtual display should request
Sean Paulac874152016-03-10 16:00:26 -0500581 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
582 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
583 *num_elements = 0;
584 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500585}
586
587HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500588 supported(__func__);
589 *type = static_cast<int32_t>(type_);
590 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500591}
592
593HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500594 supported(__func__);
595 *support = 0;
596 return HWC2::Error::None;
597}
598
599HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400600 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
601 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500602 supported(__func__);
603 *num_types = 0;
604 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500605}
606
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300607/* Find API details at:
608 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767
609 */
Sean Pauled2ec4b2016-03-10 15:35:40 -0500610HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500611 hwc2_layer_t *layers,
612 int32_t *fences) {
613 supported(__func__);
614 uint32_t num_layers = 0;
615
616 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
617 ++num_layers;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200618 if (layers == nullptr || fences == nullptr)
Sean Paulac874152016-03-10 16:00:26 -0500619 continue;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200620
621 if (num_layers > *num_elements) {
Sean Paulac874152016-03-10 16:00:26 -0500622 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
623 return HWC2::Error::None;
624 }
625
626 layers[num_layers - 1] = l.first;
Roman Stratiienko0fade372021-02-20 13:59:55 +0200627 fences[num_layers - 1] = l.second.release_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500628 }
629 *num_elements = num_layers;
630 return HWC2::Error::None;
631}
632
Roman Stratiienko0fade372021-02-20 13:59:55 +0200633void DrmHwcTwo::HwcDisplay::AddFenceToPresentFence(UniqueFd fd) {
634 if (!fd) {
Sean Paulac874152016-03-10 16:00:26 -0500635 return;
Roman Stratiienko0fade372021-02-20 13:59:55 +0200636 }
Sean Paulac874152016-03-10 16:00:26 -0500637
Roman Stratiienko0fade372021-02-20 13:59:55 +0200638 if (present_fence_) {
639 present_fence_ = UniqueFd(
640 sync_merge("dc_present", present_fence_.Get(), fd.Get()));
Sean Paulac874152016-03-10 16:00:26 -0500641 } else {
Roman Stratiienko0fade372021-02-20 13:59:55 +0200642 present_fence_ = std::move(fd);
Sean Paulac874152016-03-10 16:00:26 -0500643 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500644}
645
Rob Herring4f6c62e2018-05-17 14:33:02 -0500646HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500647 // order the layers by z-order
648 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100649 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500650 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
651 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200652 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500653 case HWC2::Composition::Device:
654 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
655 break;
656 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100657 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500658 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100659 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500660 break;
661 default:
662 continue;
663 }
664 }
665 if (use_client_layer)
666 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
667
Rob Herring4f6c62e2018-05-17 14:33:02 -0500668 if (z_map.empty())
669 return HWC2::Error::BadLayer;
670
Matvii Zorin5368b732021-01-12 10:53:08 +0200671 std::vector<DrmHwcLayer> composition_layers;
672
Sean Paulac874152016-03-10 16:00:26 -0500673 // now that they're ordered by z, add them to the composition
674 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
675 DrmHwcLayer layer;
676 l.second->PopulateDrmLayer(&layer);
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200677 int ret = layer.ImportBuffer(drm_);
Sean Paulac874152016-03-10 16:00:26 -0500678 if (ret) {
679 ALOGE("Failed to import layer, ret=%d", ret);
680 return HWC2::Error::NoResources;
681 }
Matvii Zorin5368b732021-01-12 10:53:08 +0200682 composition_layers.emplace_back(std::move(layer));
Sean Paulac874152016-03-10 16:00:26 -0500683 }
Sean Paulac874152016-03-10 16:00:26 -0500684
Matvii Zorin704ea0e2021-01-08 12:55:45 +0200685 auto composition = std::make_unique<DrmDisplayComposition>(crtc_,
686 planner_.get());
Sean Paulac874152016-03-10 16:00:26 -0500687
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200688 // TODO(nobody): Don't always assume geometry changed
Matvii Zorin5368b732021-01-12 10:53:08 +0200689 int ret = composition->SetLayers(composition_layers.data(),
690 composition_layers.size(), true);
Sean Paulac874152016-03-10 16:00:26 -0500691 if (ret) {
692 ALOGE("Failed to set layers in the composition ret=%d", ret);
693 return HWC2::Error::BadLayer;
694 }
695
696 std::vector<DrmPlane *> primary_planes(primary_planes_);
697 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500698 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500699 if (ret) {
John Stultz66763d52021-08-24 04:59:25 +0000700 ALOGV("Failed to plan the composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500701 return HWC2::Error::BadConfig;
702 }
703
704 // Disable the planes we're not using
705 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
706 composition->AddPlaneDisable(*i);
707 i = primary_planes.erase(i);
708 }
709 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
710 composition->AddPlaneDisable(*i);
711 i = overlay_planes.erase(i);
712 }
713
Rob Herring4f6c62e2018-05-17 14:33:02 -0500714 if (test) {
715 ret = compositor_.TestComposition(composition.get());
716 } else {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500717 ret = compositor_.ApplyComposition(std::move(composition));
Matteo Franchinc56eede2019-12-03 17:10:38 +0000718 AddFenceToPresentFence(compositor_.TakeOutFence());
Rob Herring4f6c62e2018-05-17 14:33:02 -0500719 }
Sean Paulac874152016-03-10 16:00:26 -0500720 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700721 if (!test)
722 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500723 return HWC2::Error::BadParameter;
724 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500725 return HWC2::Error::None;
726}
727
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300728/* Find API details at:
729 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
730 */
Matteo Franchinc56eede2019-12-03 17:10:38 +0000731HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500732 supported(__func__);
733 HWC2::Error ret;
734
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200735 ++total_stats_.total_frames_;
736
Rob Herring4f6c62e2018-05-17 14:33:02 -0500737 ret = CreateComposition(false);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200738 if (ret != HWC2::Error::None)
739 ++total_stats_.failed_kms_present_;
740
Rob Herring4f6c62e2018-05-17 14:33:02 -0500741 if (ret == HWC2::Error::BadLayer) {
742 // Can we really have no client or device layers?
Matteo Franchinc56eede2019-12-03 17:10:38 +0000743 *present_fence = -1;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500744 return HWC2::Error::None;
745 }
746 if (ret != HWC2::Error::None)
747 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500748
Matteo Franchinc56eede2019-12-03 17:10:38 +0000749 *present_fence = present_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500750
751 ++frame_no_;
752 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500753}
754
755HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500756 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400757 auto mode = std::find_if(connector_->modes().begin(),
758 connector_->modes().end(),
759 [config](DrmMode const &m) {
760 return m.id() == config;
761 });
Sean Paulac874152016-03-10 16:00:26 -0500762 if (mode == connector_->modes().end()) {
763 ALOGE("Could not find active mode for %d", config);
764 return HWC2::Error::BadConfig;
765 }
766
Matvii Zorin704ea0e2021-01-08 12:55:45 +0200767 auto composition = std::make_unique<DrmDisplayComposition>(crtc_,
768 planner_.get());
Sean Paulac874152016-03-10 16:00:26 -0500769 int ret = composition->SetDisplayMode(*mode);
Roman Stratiienko6a10c4c2021-02-15 11:25:23 +0200770 if (ret) {
771 return HWC2::Error::BadConfig;
772 }
Sean Pauled45a8e2017-02-28 13:17:34 -0500773 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500774 if (ret) {
775 ALOGE("Failed to queue dpms composition on %d", ret);
776 return HWC2::Error::BadConfig;
777 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300778
779 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500780
781 // Setup the client layer's dimensions
782 hwc_rect_t display_frame = {.left = 0,
783 .top = 0,
784 .right = static_cast<int>(mode->h_display()),
785 .bottom = static_cast<int>(mode->v_display())};
786 client_layer_.SetLayerDisplayFrame(display_frame);
Sean Paulac874152016-03-10 16:00:26 -0500787
788 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500789}
790
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300791/* Find API details at:
792 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
793 */
Sean Pauled2ec4b2016-03-10 15:35:40 -0500794HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
795 int32_t acquire_fence,
796 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600797 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500798 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500799
800 client_layer_.set_buffer(target);
John Stultz8adf5442021-07-31 00:19:50 +0000801 client_layer_.acquire_fence_ = UniqueFd(acquire_fence);
Sean Paulac874152016-03-10 16:00:26 -0500802 client_layer_.SetLayerDataspace(dataspace);
Roman Stratiienko33365c22020-10-10 23:06:36 +0300803
804 /* TODO: Do not update source_crop every call.
805 * It makes sense to do it once after every hotplug event. */
806 hwc_drm_bo bo{};
807 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
808
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200809 hwc_frect_t source_crop = {.left = 0.0F,
810 .top = 0.0F,
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300811 .right = static_cast<float>(bo.width),
812 .bottom = static_cast<float>(bo.height)};
Roman Stratiienko33365c22020-10-10 23:06:36 +0300813 client_layer_.SetLayerSourceCrop(source_crop);
814
Sean Paulac874152016-03-10 16:00:26 -0500815 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500816}
817
818HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500819 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800820
Roman Stratiienkod146d6d2020-10-04 23:56:46 +0300821 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
822 return HWC2::Error::BadParameter;
823
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800824 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +0300825 return HWC2::Error::Unsupported;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800826
827 color_mode_ = mode;
828 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500829}
830
831HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500832 int32_t hint) {
833 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200834 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
835 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
836 return HWC2::Error::BadParameter;
837
838 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
839 return HWC2::Error::BadParameter;
840
841 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
842 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
843 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
844
845 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500846}
847
848HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500849 int32_t release_fence) {
850 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200851 // TODO(nobody): Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500852 return unsupported(__func__, buffer, release_fence);
853}
854
Sean Paulac874152016-03-10 16:00:26 -0500855HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
856 supported(__func__);
857 uint64_t dpms_value = 0;
858 auto mode = static_cast<HWC2::PowerMode>(mode_in);
859 switch (mode) {
860 case HWC2::PowerMode::Off:
861 dpms_value = DRM_MODE_DPMS_OFF;
862 break;
863 case HWC2::PowerMode::On:
864 dpms_value = DRM_MODE_DPMS_ON;
865 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100866 case HWC2::PowerMode::Doze:
867 case HWC2::PowerMode::DozeSuspend:
868 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500869 default:
870 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100871 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500872 };
873
Matvii Zorin704ea0e2021-01-08 12:55:45 +0200874 auto composition = std::make_unique<DrmDisplayComposition>(crtc_,
875 planner_.get());
Sean Paulac874152016-03-10 16:00:26 -0500876 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500877 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500878 if (ret) {
879 ALOGE("Failed to apply the dpms composition ret=%d", ret);
880 return HWC2::Error::BadParameter;
881 }
882 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500883}
884
885HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500886 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300887 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500888 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500889}
890
891HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500892 uint32_t *num_requests) {
893 supported(__func__);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500894
Matvii Zorinef3c7972020-08-11 15:15:44 +0300895 return backend_->ValidateDisplay(this, num_types, num_requests);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500896}
897
Matvii Zorined90ef92021-01-29 18:32:06 +0200898std::vector<DrmHwcTwo::HwcLayer *>
899DrmHwcTwo::HwcDisplay::GetOrderLayersByZPos() {
900 std::vector<DrmHwcTwo::HwcLayer *> ordered_layers;
901 ordered_layers.reserve(layers_.size());
902
903 for (auto &[handle, layer] : layers_) {
904 ordered_layers.emplace_back(&layer);
905 }
906
907 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
908 [](const DrmHwcTwo::HwcLayer *lhs, const DrmHwcTwo::HwcLayer *rhs) {
909 return lhs->z_order() < rhs->z_order();
910 });
911
912 return ordered_layers;
913}
914
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300915#if PLATFORM_SDK_VERSION > 29
916HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
917 if (connector_->internal())
918 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
919 else if (connector_->external())
920 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
921 else
922 return HWC2::Error::BadConfig;
923
924 return HWC2::Error::None;
925}
926
927HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayVsyncPeriod(
928 hwc2_vsync_period_t *outVsyncPeriod /* ns */) {
929 supported(__func__);
930 DrmMode const &mode = connector_->active_mode();
931 if (mode.id() == 0)
932 return HWC2::Error::BadConfig;
933
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300934 *outVsyncPeriod = static_cast<hwc2_vsync_period_t>(1E9 / mode.v_refresh());
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300935 return HWC2::Error::None;
936}
937
938HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfigWithConstraints(
939 hwc2_config_t /*config*/,
940 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
941 hwc_vsync_period_change_timeline_t *outTimeline) {
942 supported(__func__);
943
944 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
945 return HWC2::Error::BadParameter;
946 }
947
948 return HWC2::Error::BadConfig;
949}
950
951HWC2::Error DrmHwcTwo::HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
952 return HWC2::Error::Unsupported;
953}
954
955HWC2::Error DrmHwcTwo::HwcDisplay::GetSupportedContentTypes(
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200956 uint32_t *outNumSupportedContentTypes,
957 const uint32_t *outSupportedContentTypes) {
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300958 if (outSupportedContentTypes == nullptr)
959 *outNumSupportedContentTypes = 0;
960
961 return HWC2::Error::None;
962}
963
964HWC2::Error DrmHwcTwo::HwcDisplay::SetContentType(int32_t contentType) {
965 supported(__func__);
966
967 if (contentType != HWC2_CONTENT_TYPE_NONE)
968 return HWC2::Error::Unsupported;
969
970 /* TODO: Map to the DRM Connector property:
971 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
972 */
973
974 return HWC2::Error::None;
975}
976#endif
977
John Stultz8c7229d2020-02-07 21:31:08 +0000978#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800979HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
980 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
981 supported(__func__);
982
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300983 auto blob = connector_->GetEdidBlob();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800984
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300985 if (!blob) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800986 ALOGE("Failed to get edid property value.");
987 return HWC2::Error::Unsupported;
988 }
989
Andrii Chepurnyi8115dbe2020-04-14 13:03:57 +0300990 if (outData) {
991 *outDataSize = std::min(*outDataSize, blob->length);
992 memcpy(outData, blob->data, *outDataSize);
993 } else {
994 *outDataSize = blob->length;
995 }
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800996 *outPort = connector_->id();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800997
998 return HWC2::Error::None;
999}
1000
1001HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
1002 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
1003 unsupported(__func__, outCapabilities);
1004
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001005 if (outNumCapabilities == nullptr) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001006 return HWC2::Error::BadParameter;
1007 }
1008
1009 *outNumCapabilities = 0;
1010
1011 return HWC2::Error::None;
1012}
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001013
1014HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayBrightnessSupport(
1015 bool *supported) {
1016 *supported = false;
1017 return HWC2::Error::None;
1018}
1019
1020HWC2::Error DrmHwcTwo::HwcDisplay::SetDisplayBrightness(
1021 float /* brightness */) {
1022 return HWC2::Error::Unsupported;
1023}
1024
John Stultz8c7229d2020-02-07 21:31:08 +00001025#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001026
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001027#if PLATFORM_SDK_VERSION > 27
1028
1029HWC2::Error DrmHwcTwo::HwcDisplay::GetRenderIntents(
1030 int32_t mode, uint32_t *outNumIntents,
1031 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1032 if (mode != HAL_COLOR_MODE_NATIVE) {
1033 return HWC2::Error::BadParameter;
1034 }
1035
1036 if (outIntents == nullptr) {
1037 *outNumIntents = 1;
1038 return HWC2::Error::None;
1039 }
1040 *outNumIntents = 1;
1041 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1042 return HWC2::Error::None;
1043}
1044
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001045HWC2::Error DrmHwcTwo::HwcDisplay::SetColorModeWithIntent(int32_t mode,
1046 int32_t intent) {
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001047 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1048 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1049 return HWC2::Error::BadParameter;
1050
1051 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
1052 return HWC2::Error::BadParameter;
1053
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001054 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001055 return HWC2::Error::Unsupported;
1056
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001057 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001058 return HWC2::Error::Unsupported;
1059
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001060 color_mode_ = mode;
1061 return HWC2::Error::None;
1062}
1063
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001064#endif /* PLATFORM_SDK_VERSION > 27 */
1065
Roman Stratiienkoaaaa4692021-09-29 12:50:10 +03001066HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t /*x*/,
1067 int32_t /*y*/) {
Sean Paulac874152016-03-10 16:00:26 -05001068 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -08001069 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001070}
1071
1072HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -05001073 supported(__func__);
Roman Stratiienko5063d532021-09-29 12:47:31 +03001074 switch (static_cast<HWC2::BlendMode>(mode)) {
1075 case HWC2::BlendMode::None:
1076 blending_ = DrmHwcBlending::kNone;
1077 break;
1078 case HWC2::BlendMode::Premultiplied:
1079 blending_ = DrmHwcBlending::kPreMult;
1080 break;
1081 case HWC2::BlendMode::Coverage:
1082 blending_ = DrmHwcBlending::kCoverage;
1083 break;
1084 default:
1085 ALOGE("Unknown blending mode b=%d", blending_);
1086 blending_ = DrmHwcBlending::kNone;
1087 break;
1088 }
Sean Paulac874152016-03-10 16:00:26 -05001089 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001090}
1091
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +03001092/* Find API details at:
1093 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=2314
1094 */
Sean Pauled2ec4b2016-03-10 15:35:40 -05001095HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001096 int32_t acquire_fence) {
1097 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -05001098
Sean Paulac874152016-03-10 16:00:26 -05001099 set_buffer(buffer);
John Stultz8adf5442021-07-31 00:19:50 +00001100 acquire_fence_ = UniqueFd(acquire_fence);
Sean Paulac874152016-03-10 16:00:26 -05001101 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001102}
1103
Roman Stratiienkoaaaa4692021-09-29 12:50:10 +03001104HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t /*color*/) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001105 // TODO(nobody): Put to client composition here?
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001106 supported(__func__);
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001107 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001108}
1109
1110HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001111 sf_type_ = static_cast<HWC2::Composition>(type);
1112 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001113}
1114
1115HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001116 supported(__func__);
Roman Stratiienko515da8f2021-09-29 12:47:21 +03001117 switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
1118 case HAL_DATASPACE_STANDARD_BT709:
1119 color_space_ = DrmHwcColorSpace::kItuRec709;
1120 break;
1121 case HAL_DATASPACE_STANDARD_BT601_625:
1122 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
1123 case HAL_DATASPACE_STANDARD_BT601_525:
1124 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
1125 color_space_ = DrmHwcColorSpace::kItuRec601;
1126 break;
1127 case HAL_DATASPACE_STANDARD_BT2020:
1128 case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
1129 color_space_ = DrmHwcColorSpace::kItuRec2020;
1130 break;
1131 default:
1132 color_space_ = DrmHwcColorSpace::kUndefined;
1133 }
1134
1135 switch (dataspace & HAL_DATASPACE_RANGE_MASK) {
1136 case HAL_DATASPACE_RANGE_FULL:
1137 sample_range_ = DrmHwcSampleRange::kFullRange;
1138 break;
1139 case HAL_DATASPACE_RANGE_LIMITED:
1140 sample_range_ = DrmHwcSampleRange::kLimitedRange;
1141 break;
1142 default:
1143 sample_range_ = DrmHwcSampleRange::kUndefined;
1144 }
Sean Paulac874152016-03-10 16:00:26 -05001145 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001146}
1147
1148HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001149 supported(__func__);
1150 display_frame_ = frame;
1151 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001152}
1153
1154HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001155 supported(__func__);
1156 alpha_ = alpha;
1157 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001158}
1159
1160HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1161 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001162 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001163 // TODO(nobody): We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001164 return unsupported(__func__, stream);
1165}
1166
1167HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001168 supported(__func__);
1169 source_crop_ = crop;
1170 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001171}
1172
1173HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001174 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001175 // TODO(nobody): We don't use surface damage, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001176 unsupported(__func__, damage);
1177 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001178}
1179
1180HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001181 supported(__func__);
Roman Stratiienko2ed4cbe2021-09-29 12:47:35 +03001182
1183 uint32_t l_transform = 0;
1184
1185 // 270* and 180* cannot be combined with flips. More specifically, they
1186 // already contain both horizontal and vertical flips, so those fields are
1187 // redundant in this case. 90* rotation can be combined with either horizontal
1188 // flip or vertical flip, so treat it differently
1189 if (transform == HWC_TRANSFORM_ROT_270) {
1190 l_transform = DrmHwcTransform::kRotate270;
1191 } else if (transform == HWC_TRANSFORM_ROT_180) {
1192 l_transform = DrmHwcTransform::kRotate180;
1193 } else {
1194 if (transform & HWC_TRANSFORM_FLIP_H)
1195 l_transform |= DrmHwcTransform::kFlipH;
1196 if (transform & HWC_TRANSFORM_FLIP_V)
1197 l_transform |= DrmHwcTransform::kFlipV;
1198 if (transform & HWC_TRANSFORM_ROT_90)
1199 l_transform |= DrmHwcTransform::kRotate90;
1200 }
1201
1202 transform_ = static_cast<DrmHwcTransform>(l_transform);
Sean Paulac874152016-03-10 16:00:26 -05001203 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001204}
1205
1206HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001207 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001208 // TODO(nobody): We don't use this information, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001209 unsupported(__func__, visible);
1210 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001211}
1212
Sean Paulac874152016-03-10 16:00:26 -05001213HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1214 supported(__func__);
1215 z_order_ = order;
1216 return HWC2::Error::None;
1217}
1218
1219void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1220 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -05001221 layer->sf_handle = buffer_;
Roman Stratiienko0fade372021-02-20 13:59:55 +02001222 // TODO(rsglobal): Avoid extra fd duplication
1223 layer->acquire_fence = UniqueFd(fcntl(acquire_fence_.Get(), F_DUPFD_CLOEXEC));
Roman Kovalivskyib8652272021-01-24 20:32:37 +02001224 layer->display_frame = display_frame_;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001225 layer->alpha = lround(65535.0F * alpha_);
Roman Stratiienko5063d532021-09-29 12:47:31 +03001226 layer->blending = blending_;
Roman Kovalivskyib8652272021-01-24 20:32:37 +02001227 layer->source_crop = source_crop_;
Roman Stratiienko2ed4cbe2021-09-29 12:47:35 +03001228 layer->transform = transform_;
Roman Stratiienko515da8f2021-09-29 12:47:21 +03001229 layer->color_space = color_space_;
1230 layer->sample_range = sample_range_;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001231}
1232
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001233void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +03001234 const std::lock_guard<std::mutex> lock(callback_lock_);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001235
Roman Stratiienko863a3c22021-09-29 13:00:29 +03001236 if (hotplug_callback_.first != nullptr &&
1237 hotplug_callback_.second != nullptr) {
1238 hotplug_callback_.first(hotplug_callback_.second, displayid,
1239 state == DRM_MODE_CONNECTED
1240 ? HWC2_CONNECTION_CONNECTED
1241 : HWC2_CONNECTION_DISCONNECTED);
1242 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001243}
1244
1245void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
Roman Stratiienkod21071f2021-03-09 21:56:50 +02001246 for (const auto &conn : drmDevice->connectors()) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001247 if (conn->state() != DRM_MODE_CONNECTED)
1248 continue;
1249 HandleDisplayHotplug(conn->display(), conn->state());
1250 }
1251}
1252
1253void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
Roman Stratiienkod21071f2021-03-09 21:56:50 +02001254 for (const auto &conn : drm_->connectors()) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001255 drmModeConnection old_state = conn->state();
1256 drmModeConnection cur_state = conn->UpdateModes()
1257 ? DRM_MODE_UNKNOWNCONNECTION
1258 : conn->state();
1259
1260 if (cur_state == old_state)
1261 continue;
1262
1263 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
1264 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1265 conn->id(), conn->display());
1266
1267 int display_id = conn->display();
1268 if (cur_state == DRM_MODE_CONNECTED) {
1269 auto &display = hwc2_->displays_.at(display_id);
1270 display.ChosePreferredConfig();
1271 } else {
1272 auto &display = hwc2_->displays_.at(display_id);
1273 display.ClearDisplay();
1274 }
1275
1276 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1277 }
1278}
1279
Sean Pauled2ec4b2016-03-10 15:35:40 -05001280// static
1281int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1282 unsupported(__func__);
1283 return 0;
1284}
1285
1286// static
1287void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001288 uint32_t *out_count,
1289 int32_t * /*out_capabilities*/) {
1290 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001291 *out_count = 0;
1292}
1293
1294// static
Sean Paulac874152016-03-10 16:00:26 -05001295hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1296 struct hwc2_device * /*dev*/, int32_t descriptor) {
1297 supported(__func__);
1298 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001299 switch (func) {
1300 // Device functions
1301 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1302 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1303 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1304 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001305 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001306 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1307 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1308 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1309 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1310 case HWC2::FunctionDescriptor::Dump:
1311 return ToHook<HWC2_PFN_DUMP>(
1312 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1313 uint32_t *, char *>);
1314 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1315 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1316 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1317 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1318 case HWC2::FunctionDescriptor::RegisterCallback:
1319 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1320 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1321 &DrmHwcTwo::RegisterCallback, int32_t,
1322 hwc2_callback_data_t, hwc2_function_pointer_t>);
1323
1324 // Display functions
1325 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1326 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1327 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1328 &HwcDisplay::AcceptDisplayChanges>);
1329 case HWC2::FunctionDescriptor::CreateLayer:
1330 return ToHook<HWC2_PFN_CREATE_LAYER>(
1331 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1332 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1333 case HWC2::FunctionDescriptor::DestroyLayer:
1334 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1335 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1336 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1337 case HWC2::FunctionDescriptor::GetActiveConfig:
1338 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1339 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1340 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1341 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1342 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1343 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1344 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1345 hwc2_layer_t *, int32_t *>);
1346 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1347 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1348 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1349 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1350 int32_t, int32_t>);
1351 case HWC2::FunctionDescriptor::GetColorModes:
1352 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1353 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1354 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1355 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001356 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1357 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1358 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1359 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001360 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001361 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1362 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1363 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1364 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001365 case HWC2::FunctionDescriptor::GetDisplayName:
1366 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1367 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1368 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1369 case HWC2::FunctionDescriptor::GetDisplayRequests:
1370 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1371 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1372 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1373 hwc2_layer_t *, int32_t *>);
1374 case HWC2::FunctionDescriptor::GetDisplayType:
1375 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1376 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1377 &HwcDisplay::GetDisplayType, int32_t *>);
1378 case HWC2::FunctionDescriptor::GetDozeSupport:
1379 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1380 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1381 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001382 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1383 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1384 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1385 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1386 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001387 case HWC2::FunctionDescriptor::GetReleaseFences:
1388 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1389 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1390 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1391 int32_t *>);
1392 case HWC2::FunctionDescriptor::PresentDisplay:
1393 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1394 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1395 &HwcDisplay::PresentDisplay, int32_t *>);
1396 case HWC2::FunctionDescriptor::SetActiveConfig:
1397 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1398 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1399 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1400 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001401 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1402 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1403 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1404 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001405 case HWC2::FunctionDescriptor::SetColorMode:
1406 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1407 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1408 &HwcDisplay::SetColorMode, int32_t>);
1409 case HWC2::FunctionDescriptor::SetColorTransform:
1410 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1411 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1412 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1413 case HWC2::FunctionDescriptor::SetOutputBuffer:
1414 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1415 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1416 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1417 case HWC2::FunctionDescriptor::SetPowerMode:
1418 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1419 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1420 &HwcDisplay::SetPowerMode, int32_t>);
1421 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1422 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1423 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1424 &HwcDisplay::SetVsyncEnabled, int32_t>);
1425 case HWC2::FunctionDescriptor::ValidateDisplay:
1426 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1427 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1428 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001429#if PLATFORM_SDK_VERSION > 27
1430 case HWC2::FunctionDescriptor::GetRenderIntents:
1431 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
1432 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
1433 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
1434 int32_t *>);
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001435 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
1436 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
1437 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
1438 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001439#endif
John Stultz8c7229d2020-02-07 21:31:08 +00001440#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001441 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1442 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1443 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1444 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1445 uint32_t *, uint8_t *>);
1446 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1447 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1448 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1449 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1450 uint32_t *>);
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001451 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
1452 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
1453 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
1454 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
1455 case HWC2::FunctionDescriptor::SetDisplayBrightness:
1456 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
1457 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
1458 &HwcDisplay::SetDisplayBrightness, float>);
John Stultz8c7229d2020-02-07 21:31:08 +00001459#endif /* PLATFORM_SDK_VERSION > 28 */
Roman Stratiienko6f5df172020-09-27 22:48:08 +03001460#if PLATFORM_SDK_VERSION > 29
1461 case HWC2::FunctionDescriptor::GetDisplayConnectionType:
1462 return ToHook<HWC2_PFN_GET_DISPLAY_CONNECTION_TYPE>(
1463 DisplayHook<decltype(&HwcDisplay::GetDisplayConnectionType),
1464 &HwcDisplay::GetDisplayConnectionType, uint32_t *>);
1465 case HWC2::FunctionDescriptor::GetDisplayVsyncPeriod:
1466 return ToHook<HWC2_PFN_GET_DISPLAY_VSYNC_PERIOD>(
1467 DisplayHook<decltype(&HwcDisplay::GetDisplayVsyncPeriod),
1468 &HwcDisplay::GetDisplayVsyncPeriod,
1469 hwc2_vsync_period_t *>);
1470 case HWC2::FunctionDescriptor::SetActiveConfigWithConstraints:
1471 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG_WITH_CONSTRAINTS>(
1472 DisplayHook<decltype(&HwcDisplay::SetActiveConfigWithConstraints),
1473 &HwcDisplay::SetActiveConfigWithConstraints,
1474 hwc2_config_t, hwc_vsync_period_change_constraints_t *,
1475 hwc_vsync_period_change_timeline_t *>);
1476 case HWC2::FunctionDescriptor::SetAutoLowLatencyMode:
1477 return ToHook<HWC2_PFN_SET_AUTO_LOW_LATENCY_MODE>(
1478 DisplayHook<decltype(&HwcDisplay::SetAutoLowLatencyMode),
1479 &HwcDisplay::SetAutoLowLatencyMode, bool>);
1480 case HWC2::FunctionDescriptor::GetSupportedContentTypes:
1481 return ToHook<HWC2_PFN_GET_SUPPORTED_CONTENT_TYPES>(
1482 DisplayHook<decltype(&HwcDisplay::GetSupportedContentTypes),
1483 &HwcDisplay::GetSupportedContentTypes, uint32_t *,
1484 uint32_t *>);
1485 case HWC2::FunctionDescriptor::SetContentType:
1486 return ToHook<HWC2_PFN_SET_CONTENT_TYPE>(
1487 DisplayHook<decltype(&HwcDisplay::SetContentType),
1488 &HwcDisplay::SetContentType, int32_t>);
1489#endif
Sean Pauled2ec4b2016-03-10 15:35:40 -05001490 // Layer functions
1491 case HWC2::FunctionDescriptor::SetCursorPosition:
1492 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1493 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1494 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1495 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1496 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1497 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1498 &HwcLayer::SetLayerBlendMode, int32_t>);
1499 case HWC2::FunctionDescriptor::SetLayerBuffer:
1500 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1501 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1502 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1503 case HWC2::FunctionDescriptor::SetLayerColor:
1504 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1505 LayerHook<decltype(&HwcLayer::SetLayerColor),
1506 &HwcLayer::SetLayerColor, hwc_color_t>);
1507 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1508 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1509 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1510 &HwcLayer::SetLayerCompositionType, int32_t>);
1511 case HWC2::FunctionDescriptor::SetLayerDataspace:
1512 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1513 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1514 &HwcLayer::SetLayerDataspace, int32_t>);
1515 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1516 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1517 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1518 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1519 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1520 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1521 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1522 &HwcLayer::SetLayerPlaneAlpha, float>);
1523 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001524 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1525 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1526 &HwcLayer::SetLayerSidebandStream,
1527 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001528 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1529 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1530 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1531 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1532 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1533 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1534 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1535 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1536 case HWC2::FunctionDescriptor::SetLayerTransform:
1537 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1538 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1539 &HwcLayer::SetLayerTransform, int32_t>);
1540 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1541 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1542 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1543 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1544 case HWC2::FunctionDescriptor::SetLayerZOrder:
1545 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1546 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1547 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001548 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001549 default:
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001550 return nullptr;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001551 }
1552}
Sean Paulac874152016-03-10 16:00:26 -05001553
1554// static
1555int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1556 struct hw_device_t **dev) {
1557 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001558 if (strcmp(name, HWC_HARDWARE_COMPOSER) != 0) {
Sean Paulac874152016-03-10 16:00:26 -05001559 ALOGE("Invalid module name- %s", name);
1560 return -EINVAL;
1561 }
1562
1563 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1564 if (!ctx) {
1565 ALOGE("Failed to allocate DrmHwcTwo");
1566 return -ENOMEM;
1567 }
1568
1569 HWC2::Error err = ctx->Init();
1570 if (err != HWC2::Error::None) {
1571 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1572 return -EINVAL;
1573 }
1574
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001575 ctx->common.module = (hw_module_t *)module;
Sean Paulac874152016-03-10 16:00:26 -05001576 *dev = &ctx->common;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001577 ctx.release(); // NOLINT(bugprone-unused-return-value)
Sean Paulac874152016-03-10 16:00:26 -05001578 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001579}
Sean Paulf72cccd2018-08-27 13:59:08 -04001580} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001581
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001582// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001583static struct hw_module_methods_t hwc2_module_methods = {
1584 .open = android::DrmHwcTwo::HookDevOpen,
1585};
1586
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001587// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001588hw_module_t HAL_MODULE_INFO_SYM = {
1589 .tag = HARDWARE_MODULE_TAG,
1590 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1591 .id = HWC_HARDWARE_MODULE_ID,
1592 .name = "DrmHwcTwo module",
1593 .author = "The Android Open Source Project",
1594 .methods = &hwc2_module_methods,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001595 .dso = nullptr,
Sean Paulac874152016-03-10 16:00:26 -05001596 .reserved = {0},
1597};