blob: ffc45ef70cecf3a89559592a55c8de31e1e875ae [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
Sean Paulf72cccd2018-08-27 13:59:08 -040020#include "drmhwctwo.h"
Matvii Zorinef3c7972020-08-11 15:15:44 +030021#include "backendmanager.h"
Sean Paulac874152016-03-10 16:00:26 -050022#include "drmdisplaycomposition.h"
23#include "drmhwcomposer.h"
Sean Paulac874152016-03-10 16:00:26 -050024#include "platform.h"
25#include "vsyncworker.h"
26
27#include <inttypes.h>
28#include <string>
Sean Pauled2ec4b2016-03-10 15:35:40 -050029
Sean Paulac874152016-03-10 16:00:26 -050030#include <cutils/properties.h>
31#include <hardware/hardware.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050032#include <hardware/hwcomposer2.h>
Sean Paulf72cccd2018-08-27 13:59:08 -040033#include <log/log.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050034
35namespace android {
36
Sean Paulac874152016-03-10 16:00:26 -050037class DrmVsyncCallback : public VsyncCallback {
38 public:
39 DrmVsyncCallback(hwc2_callback_data_t data, hwc2_function_pointer_t hook)
40 : data_(data), hook_(hook) {
41 }
42
43 void Callback(int display, int64_t timestamp) {
44 auto hook = reinterpret_cast<HWC2_PFN_VSYNC>(hook_);
45 hook(data_, display, timestamp);
46 }
47
48 private:
49 hwc2_callback_data_t data_;
50 hwc2_function_pointer_t hook_;
51};
52
Sean Pauled2ec4b2016-03-10 15:35:40 -050053DrmHwcTwo::DrmHwcTwo() {
Sean Paulac874152016-03-10 16:00:26 -050054 common.tag = HARDWARE_DEVICE_TAG;
55 common.version = HWC_DEVICE_API_VERSION_2_0;
Sean Pauled2ec4b2016-03-10 15:35:40 -050056 common.close = HookDevClose;
57 getCapabilities = HookDevGetCapabilities;
58 getFunction = HookDevGetFunction;
59}
60
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030061HWC2::Error DrmHwcTwo::CreateDisplay(hwc2_display_t displ,
62 HWC2::DisplayType type) {
63 DrmDevice *drm = resource_manager_.GetDrmDevice(displ);
64 std::shared_ptr<Importer> importer = resource_manager_.GetImporter(displ);
Alexandru Gheorghec5463582018-03-27 15:52:02 +010065 if (!drm || !importer) {
66 ALOGE("Failed to get a valid drmresource and importer");
Sean Paulac874152016-03-10 16:00:26 -050067 return HWC2::Error::NoResources;
68 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030069 displays_.emplace(std::piecewise_construct, std::forward_as_tuple(displ),
Sean Paulf72cccd2018-08-27 13:59:08 -040070 std::forward_as_tuple(&resource_manager_, drm, importer,
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030071 displ, type));
Sean Paulac874152016-03-10 16:00:26 -050072
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030073 DrmCrtc *crtc = drm->GetCrtcForDisplay(static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050074 if (!crtc) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030075 ALOGE("Failed to get crtc for display %d", static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050076 return HWC2::Error::BadDisplay;
77 }
Sean Paulac874152016-03-10 16:00:26 -050078 std::vector<DrmPlane *> display_planes;
Alexandru Gheorghec5463582018-03-27 15:52:02 +010079 for (auto &plane : drm->planes()) {
Sean Paulac874152016-03-10 16:00:26 -050080 if (plane->GetCrtcSupported(*crtc))
81 display_planes.push_back(plane.get());
82 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030083 displays_.at(displ).Init(&display_planes);
Sean Paulac874152016-03-10 16:00:26 -050084 return HWC2::Error::None;
85}
86
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030087HWC2::Error DrmHwcTwo::Init() {
88 int rv = resource_manager_.Init();
89 if (rv) {
90 ALOGE("Can't initialize the resource manager %d", rv);
91 return HWC2::Error::NoResources;
92 }
93
94 HWC2::Error ret = HWC2::Error::None;
95 for (int i = 0; i < resource_manager_.getDisplayCount(); i++) {
96 ret = CreateDisplay(i, HWC2::DisplayType::Physical);
97 if (ret != HWC2::Error::None) {
98 ALOGE("Failed to create display %d with error %d", i, ret);
99 return ret;
100 }
101 }
102
103 auto &drmDevices = resource_manager_.getDrmDevices();
104 for (auto &device : drmDevices) {
105 device->RegisterHotplugHandler(new DrmHotplugHandler(this, device.get()));
106 }
107 return ret;
108}
109
Sean Pauled2ec4b2016-03-10 15:35:40 -0500110template <typename... Args>
111static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
112 ALOGV("Unsupported function: %s", func);
113 return HWC2::Error::Unsupported;
114}
115
Sean Paulac874152016-03-10 16:00:26 -0500116static inline void supported(char const *func) {
117 ALOGV("Supported function: %s", func);
118}
119
Sean Pauled2ec4b2016-03-10 15:35:40 -0500120HWC2::Error DrmHwcTwo::CreateVirtualDisplay(uint32_t width, uint32_t height,
121 int32_t *format,
122 hwc2_display_t *display) {
123 // TODO: Implement virtual display
Sean Paulac874152016-03-10 16:00:26 -0500124 return unsupported(__func__, width, height, format, display);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500125}
126
127HWC2::Error DrmHwcTwo::DestroyVirtualDisplay(hwc2_display_t display) {
Sean Paulac874152016-03-10 16:00:26 -0500128 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500129 return unsupported(__func__, display);
130}
131
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200132std::string DrmHwcTwo::HwcDisplay::DumpDelta(
133 DrmHwcTwo::HwcDisplay::Stats delta) {
134 if (delta.total_pixops_ == 0)
135 return "No stats yet";
136 double Ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
137
138 return (std::stringstream()
139 << " Total frames count: " << delta.total_frames_ << "\n"
140 << " Failed to test commit frames: " << delta.failed_kms_validate_
141 << "\n"
142 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
143 << ((delta.failed_kms_present_ > 0)
144 ? " !!! Internal failure, FIX it please\n"
145 : "")
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200146 << " Flattened frames: " << delta.frames_flattened_ << "\n"
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200147 << " Pixel operations (free units)"
148 << " : [TOTAL: " << delta.total_pixops_
149 << " / GPU: " << delta.gpu_pixops_ << "]\n"
150 << " Composition efficiency: " << Ratio)
151 .str();
152}
153
154std::string DrmHwcTwo::HwcDisplay::Dump() {
155 auto out = (std::stringstream()
156 << "- Display on: " << connector_->name() << "\n"
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200157 << " Flattening state: " << compositor_.GetFlatteningState()
158 << "\n"
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200159 << "Statistics since system boot:\n"
160 << DumpDelta(total_stats_) << "\n\n"
161 << "Statistics since last dumpsys request:\n"
162 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n")
163 .str();
164
165 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
166 return out;
167}
168
169void DrmHwcTwo::Dump(uint32_t *outSize, char *outBuffer) {
170 supported(__func__);
171
172 if (outBuffer != nullptr) {
173 auto copiedBytes = mDumpString.copy(outBuffer, *outSize);
174 *outSize = static_cast<uint32_t>(copiedBytes);
175 return;
176 }
177
178 std::stringstream output;
179
180 output << "-- drm_hwcomposer --\n\n";
181
182 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &dp : displays_)
183 output << dp.second.Dump();
184
185 mDumpString = output.str();
186 *outSize = static_cast<uint32_t>(mDumpString.size());
Sean Pauled2ec4b2016-03-10 15:35:40 -0500187}
188
189uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() {
Sean Paulac874152016-03-10 16:00:26 -0500190 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500191 unsupported(__func__);
192 return 0;
193}
194
195HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor,
Sean Paulac874152016-03-10 16:00:26 -0500196 hwc2_callback_data_t data,
197 hwc2_function_pointer_t function) {
198 supported(__func__);
199 auto callback = static_cast<HWC2::Callback>(descriptor);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300200
201 if (!function) {
202 callbacks_.erase(callback);
203 return HWC2::Error::None;
204 }
205
Sean Paulac874152016-03-10 16:00:26 -0500206 callbacks_.emplace(callback, HwcCallback(data, function));
207
208 switch (callback) {
209 case HWC2::Callback::Hotplug: {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300210 auto &drmDevices = resource_manager_.getDrmDevices();
211 for (auto &device : drmDevices)
212 HandleInitialHotplugState(device.get());
Sean Paulac874152016-03-10 16:00:26 -0500213 break;
214 }
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200215 case HWC2::Callback::Refresh: {
216 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
217 displays_)
218 d.second.RegisterRefreshCallback(data, function);
219 break;
220 }
Sean Paulac874152016-03-10 16:00:26 -0500221 case HWC2::Callback::Vsync: {
222 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
223 displays_)
224 d.second.RegisterVsyncCallback(data, function);
225 break;
226 }
227 default:
228 break;
229 }
230 return HWC2::Error::None;
231}
232
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100233DrmHwcTwo::HwcDisplay::HwcDisplay(ResourceManager *resource_manager,
234 DrmDevice *drm,
Sean Paulac874152016-03-10 16:00:26 -0500235 std::shared_ptr<Importer> importer,
Sean Paulac874152016-03-10 16:00:26 -0500236 hwc2_display_t handle, HWC2::DisplayType type)
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100237 : resource_manager_(resource_manager),
238 drm_(drm),
239 importer_(importer),
240 handle_(handle),
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200241 type_(type),
242 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
Sean Paulac874152016-03-10 16:00:26 -0500243 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200244
245 // clang-format off
246 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
247 0.0, 1.0, 0.0, 0.0,
248 0.0, 0.0, 1.0, 0.0,
249 0.0, 0.0, 0.0, 1.0};
250 // clang-format on
Sean Paulac874152016-03-10 16:00:26 -0500251}
252
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300253void DrmHwcTwo::HwcDisplay::ClearDisplay() {
254 compositor_.ClearDisplay();
255}
256
Sean Paulac874152016-03-10 16:00:26 -0500257HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
258 supported(__func__);
259 planner_ = Planner::CreateInstance(drm_);
260 if (!planner_) {
261 ALOGE("Failed to create planner instance for composition");
262 return HWC2::Error::NoResources;
263 }
264
265 int display = static_cast<int>(handle_);
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100266 int ret = compositor_.Init(resource_manager_, display);
Sean Paulac874152016-03-10 16:00:26 -0500267 if (ret) {
268 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
269 return HWC2::Error::NoResources;
270 }
271
272 // Split up the given display planes into primary and overlay to properly
273 // interface with the composition
274 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
Jason Macnakf1af9572020-08-20 11:49:51 -0700275 property_get("vendor.hwc.drm.use_overlay_planes", use_overlay_planes_prop,
276 "1");
Sean Paulac874152016-03-10 16:00:26 -0500277 bool use_overlay_planes = atoi(use_overlay_planes_prop);
278 for (auto &plane : *planes) {
279 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
280 primary_planes_.push_back(plane);
281 else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY)
282 overlay_planes_.push_back(plane);
283 }
284
285 crtc_ = drm_->GetCrtcForDisplay(display);
286 if (!crtc_) {
287 ALOGE("Failed to get crtc for display %d", display);
288 return HWC2::Error::BadDisplay;
289 }
290
291 connector_ = drm_->GetConnectorForDisplay(display);
292 if (!connector_) {
293 ALOGE("Failed to get connector for display %d", display);
294 return HWC2::Error::BadDisplay;
295 }
296
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300297 ret = vsync_worker_.Init(drm_, display);
298 if (ret) {
299 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
300 return HWC2::Error::BadDisplay;
301 }
302
Matvii Zorinef3c7972020-08-11 15:15:44 +0300303 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
304 if (ret) {
305 ALOGE("Failed to set backend for d=%d %d\n", display, ret);
306 return HWC2::Error::BadDisplay;
307 }
308
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300309 return ChosePreferredConfig();
310}
311
312HWC2::Error DrmHwcTwo::HwcDisplay::ChosePreferredConfig() {
Sean Paulac874152016-03-10 16:00:26 -0500313 // Fetch the number of modes from the display
314 uint32_t num_configs;
315 HWC2::Error err = GetDisplayConfigs(&num_configs, NULL);
316 if (err != HWC2::Error::None || !num_configs)
317 return err;
318
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200319 return SetActiveConfig(connector_->get_preferred_mode_id());
Sean Paulac874152016-03-10 16:00:26 -0500320}
321
322HWC2::Error DrmHwcTwo::HwcDisplay::RegisterVsyncCallback(
323 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
324 supported(__func__);
325 auto callback = std::make_shared<DrmVsyncCallback>(data, func);
Adrian Salidofa37f672017-02-16 10:29:46 -0800326 vsync_worker_.RegisterCallback(std::move(callback));
Sean Paulac874152016-03-10 16:00:26 -0500327 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500328}
329
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200330void DrmHwcTwo::HwcDisplay::RegisterRefreshCallback(
331 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
332 supported(__func__);
333 auto hook = reinterpret_cast<HWC2_PFN_REFRESH>(func);
334 compositor_.SetRefreshCallback([data, hook](int display) {
335 hook(data, static_cast<hwc2_display_t>(display));
336 });
337}
338
Sean Pauled2ec4b2016-03-10 15:35:40 -0500339HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500340 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500341 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
342 l.second.accept_type_change();
343 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500344}
345
346HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500347 supported(__func__);
348 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
349 *layer = static_cast<hwc2_layer_t>(layer_idx_);
350 ++layer_idx_;
351 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500352}
353
354HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Sean Paulac874152016-03-10 16:00:26 -0500355 supported(__func__);
Vincent Donnefort9abec032019-10-09 15:43:43 +0100356 if (!get_layer(layer))
357 return HWC2::Error::BadLayer;
358
Sean Paulac874152016-03-10 16:00:26 -0500359 layers_.erase(layer);
360 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500361}
362
363HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
Sean Paulac874152016-03-10 16:00:26 -0500364 supported(__func__);
365 DrmMode const &mode = connector_->active_mode();
366 if (mode.id() == 0)
367 return HWC2::Error::BadConfig;
368
369 *config = mode.id();
370 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500371}
372
373HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
374 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500375 supported(__func__);
376 uint32_t num_changes = 0;
377 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
378 if (l.second.type_changed()) {
379 if (layers && num_changes < *num_elements)
380 layers[num_changes] = l.first;
381 if (types && num_changes < *num_elements)
382 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
383 ++num_changes;
384 }
385 }
386 if (!layers && !types)
387 *num_elements = num_changes;
388 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500389}
390
391HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500392 uint32_t height,
393 int32_t /*format*/,
394 int32_t dataspace) {
395 supported(__func__);
396 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
397 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
398
399 if (width < min.first || height < min.second)
400 return HWC2::Error::Unsupported;
401
402 if (width > max.first || height > max.second)
403 return HWC2::Error::Unsupported;
404
405 if (dataspace != HAL_DATASPACE_UNKNOWN &&
406 dataspace != HAL_DATASPACE_STANDARD_UNSPECIFIED)
407 return HWC2::Error::Unsupported;
408
409 // TODO: Validate format can be handled by either GL or planes
410 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500411}
412
413HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500414 int32_t *modes) {
415 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800416 if (!modes)
417 *num_modes = 1;
418
419 if (modes)
420 *modes = HAL_COLOR_MODE_NATIVE;
421
422 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500423}
424
425HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500426 int32_t attribute_in,
427 int32_t *value) {
428 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400429 auto mode = std::find_if(connector_->modes().begin(),
430 connector_->modes().end(),
431 [config](DrmMode const &m) {
432 return m.id() == config;
433 });
Sean Paulac874152016-03-10 16:00:26 -0500434 if (mode == connector_->modes().end()) {
435 ALOGE("Could not find active mode for %d", config);
436 return HWC2::Error::BadConfig;
437 }
438
439 static const int32_t kUmPerInch = 25400;
440 uint32_t mm_width = connector_->mm_width();
441 uint32_t mm_height = connector_->mm_height();
442 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
443 switch (attribute) {
444 case HWC2::Attribute::Width:
445 *value = mode->h_display();
446 break;
447 case HWC2::Attribute::Height:
448 *value = mode->v_display();
449 break;
450 case HWC2::Attribute::VsyncPeriod:
451 // in nanoseconds
452 *value = 1000 * 1000 * 1000 / mode->v_refresh();
453 break;
454 case HWC2::Attribute::DpiX:
455 // Dots per 1000 inches
456 *value = mm_width ? (mode->h_display() * kUmPerInch) / mm_width : -1;
457 break;
458 case HWC2::Attribute::DpiY:
459 // Dots per 1000 inches
460 *value = mm_height ? (mode->v_display() * kUmPerInch) / mm_height : -1;
461 break;
462 default:
463 *value = -1;
464 return HWC2::Error::BadConfig;
465 }
466 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500467}
468
469HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
470 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500471 supported(__func__);
472 // Since this callback is normally invoked twice (once to get the count, and
473 // once to populate configs), we don't really want to read the edid
474 // redundantly. Instead, only update the modes on the first invocation. While
475 // it's possible this will result in stale modes, it'll all come out in the
476 // wash when we try to set the active config later.
477 if (!configs) {
478 int ret = connector_->UpdateModes();
479 if (ret) {
480 ALOGE("Failed to update display modes %d", ret);
481 return HWC2::Error::BadDisplay;
482 }
483 }
484
Neil Armstrongb67d0492019-06-20 09:00:21 +0000485 // Since the upper layers only look at vactive/hactive/refresh, height and
486 // width, it doesn't differentiate interlaced from progressive and other
487 // similar modes. Depending on the order of modes we return to SF, it could
488 // end up choosing a suboptimal configuration and dropping the preferred
489 // mode. To workaround this, don't offer interlaced modes to SF if there is
490 // at least one non-interlaced alternative and only offer a single WxH@R
491 // mode with at least the prefered mode from in DrmConnector::UpdateModes()
492
493 // TODO: Remove the following block of code until AOSP handles all modes
494 std::vector<DrmMode> sel_modes;
495
496 // Add the preferred mode first to be sure it's not dropped
497 auto mode = std::find_if(connector_->modes().begin(),
498 connector_->modes().end(), [&](DrmMode const &m) {
499 return m.id() ==
500 connector_->get_preferred_mode_id();
501 });
502 if (mode != connector_->modes().end())
503 sel_modes.push_back(*mode);
504
505 // Add the active mode if different from preferred mode
506 if (connector_->active_mode().id() != connector_->get_preferred_mode_id())
507 sel_modes.push_back(connector_->active_mode());
508
509 // Cycle over the modes and filter out "similar" modes, keeping only the
510 // first ones in the order given by DRM (from CEA ids and timings order)
Sean Paulac874152016-03-10 16:00:26 -0500511 for (const DrmMode &mode : connector_->modes()) {
Neil Armstrongb67d0492019-06-20 09:00:21 +0000512 // TODO: Remove this when 3D Attributes are in AOSP
513 if (mode.flags() & DRM_MODE_FLAG_3D_MASK)
514 continue;
515
Neil Armstrong4c027a72019-06-04 14:48:02 +0000516 // TODO: Remove this when the Interlaced attribute is in AOSP
517 if (mode.flags() & DRM_MODE_FLAG_INTERLACE) {
518 auto m = std::find_if(connector_->modes().begin(),
519 connector_->modes().end(),
520 [&mode](DrmMode const &m) {
521 return !(m.flags() & DRM_MODE_FLAG_INTERLACE) &&
522 m.h_display() == mode.h_display() &&
523 m.v_display() == mode.v_display();
524 });
Neil Armstrongb67d0492019-06-20 09:00:21 +0000525 if (m == connector_->modes().end())
526 sel_modes.push_back(mode);
527
528 continue;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000529 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000530
531 // Search for a similar WxH@R mode in the filtered list and drop it if
532 // another mode with the same WxH@R has already been selected
533 // TODO: Remove this when AOSP handles duplicates modes
534 auto m = std::find_if(sel_modes.begin(), sel_modes.end(),
535 [&mode](DrmMode const &m) {
536 return m.h_display() == mode.h_display() &&
537 m.v_display() == mode.v_display() &&
538 m.v_refresh() == mode.v_refresh();
539 });
540 if (m == sel_modes.end())
541 sel_modes.push_back(mode);
542 }
543
544 auto num_modes = static_cast<uint32_t>(sel_modes.size());
545 if (!configs) {
546 *num_configs = num_modes;
547 return HWC2::Error::None;
548 }
549
550 uint32_t idx = 0;
551 for (const DrmMode &mode : sel_modes) {
552 if (idx >= *num_configs)
553 break;
554 configs[idx++] = mode.id();
Sean Paulac874152016-03-10 16:00:26 -0500555 }
556 *num_configs = idx;
557 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500558}
559
560HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500561 supported(__func__);
562 std::ostringstream stream;
563 stream << "display-" << connector_->id();
564 std::string string = stream.str();
565 size_t length = string.length();
566 if (!name) {
567 *size = length;
568 return HWC2::Error::None;
569 }
570
571 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
572 strncpy(name, string.c_str(), *size);
573 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500574}
575
Sean Paulac874152016-03-10 16:00:26 -0500576HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
577 uint32_t *num_elements,
578 hwc2_layer_t *layers,
579 int32_t *layer_requests) {
580 supported(__func__);
581 // TODO: I think virtual display should request
582 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
583 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
584 *num_elements = 0;
585 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500586}
587
588HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500589 supported(__func__);
590 *type = static_cast<int32_t>(type_);
591 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500592}
593
594HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500595 supported(__func__);
596 *support = 0;
597 return HWC2::Error::None;
598}
599
600HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400601 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
602 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500603 supported(__func__);
604 *num_types = 0;
605 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500606}
607
608HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500609 hwc2_layer_t *layers,
610 int32_t *fences) {
611 supported(__func__);
612 uint32_t num_layers = 0;
613
614 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
615 ++num_layers;
616 if (layers == NULL || fences == NULL) {
617 continue;
618 } else if (num_layers > *num_elements) {
619 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
620 return HWC2::Error::None;
621 }
622
623 layers[num_layers - 1] = l.first;
624 fences[num_layers - 1] = l.second.take_release_fence();
625 }
626 *num_elements = num_layers;
627 return HWC2::Error::None;
628}
629
Matteo Franchinc56eede2019-12-03 17:10:38 +0000630void DrmHwcTwo::HwcDisplay::AddFenceToPresentFence(int fd) {
Sean Paulac874152016-03-10 16:00:26 -0500631 if (fd < 0)
632 return;
633
Matteo Franchinc56eede2019-12-03 17:10:38 +0000634 if (present_fence_.get() >= 0) {
635 int old_fence = present_fence_.get();
636 present_fence_.Set(sync_merge("dc_present", old_fence, fd));
637 close(fd);
Sean Paulac874152016-03-10 16:00:26 -0500638 } else {
Matteo Franchinc56eede2019-12-03 17:10:38 +0000639 present_fence_.Set(fd);
Sean Paulac874152016-03-10 16:00:26 -0500640 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500641}
642
Roman Stratiienkoafb36892019-11-08 17:16:11 +0200643bool DrmHwcTwo::HwcDisplay::HardwareSupportsLayerType(
644 HWC2::Composition comp_type) {
645 return comp_type == HWC2::Composition::Device ||
646 comp_type == HWC2::Composition::Cursor;
647}
648
Rob Herring4f6c62e2018-05-17 14:33:02 -0500649HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500650 std::vector<DrmCompositionDisplayLayersMap> layers_map;
651 layers_map.emplace_back();
652 DrmCompositionDisplayLayersMap &map = layers_map.back();
653
654 map.display = static_cast<int>(handle_);
655 map.geometry_changed = true; // TODO: Fix this
656
657 // order the layers by z-order
658 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100659 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500660 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
661 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200662 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500663 case HWC2::Composition::Device:
664 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
665 break;
666 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100667 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500668 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100669 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500670 break;
671 default:
672 continue;
673 }
674 }
675 if (use_client_layer)
676 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
677
Rob Herring4f6c62e2018-05-17 14:33:02 -0500678 if (z_map.empty())
679 return HWC2::Error::BadLayer;
680
Sean Paulac874152016-03-10 16:00:26 -0500681 // now that they're ordered by z, add them to the composition
682 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
683 DrmHwcLayer layer;
684 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200685 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500686 if (ret) {
687 ALOGE("Failed to import layer, ret=%d", ret);
688 return HWC2::Error::NoResources;
689 }
690 map.layers.emplace_back(std::move(layer));
691 }
Sean Paulac874152016-03-10 16:00:26 -0500692
Sean Paulf72cccd2018-08-27 13:59:08 -0400693 std::unique_ptr<DrmDisplayComposition> composition = compositor_
694 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500695 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
696
697 // TODO: Don't always assume geometry changed
698 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
699 if (ret) {
700 ALOGE("Failed to set layers in the composition ret=%d", ret);
701 return HWC2::Error::BadLayer;
702 }
703
704 std::vector<DrmPlane *> primary_planes(primary_planes_);
705 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500706 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500707 if (ret) {
708 ALOGE("Failed to plan the composition ret=%d", ret);
709 return HWC2::Error::BadConfig;
710 }
711
712 // Disable the planes we're not using
713 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
714 composition->AddPlaneDisable(*i);
715 i = primary_planes.erase(i);
716 }
717 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
718 composition->AddPlaneDisable(*i);
719 i = overlay_planes.erase(i);
720 }
721
Rob Herring4f6c62e2018-05-17 14:33:02 -0500722 if (test) {
723 ret = compositor_.TestComposition(composition.get());
724 } else {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500725 ret = compositor_.ApplyComposition(std::move(composition));
Matteo Franchinc56eede2019-12-03 17:10:38 +0000726 AddFenceToPresentFence(compositor_.TakeOutFence());
Rob Herring4f6c62e2018-05-17 14:33:02 -0500727 }
Sean Paulac874152016-03-10 16:00:26 -0500728 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700729 if (!test)
730 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500731 return HWC2::Error::BadParameter;
732 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500733 return HWC2::Error::None;
734}
735
Matteo Franchinc56eede2019-12-03 17:10:38 +0000736HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500737 supported(__func__);
738 HWC2::Error ret;
739
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200740 ++total_stats_.total_frames_;
741
Rob Herring4f6c62e2018-05-17 14:33:02 -0500742 ret = CreateComposition(false);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200743 if (ret != HWC2::Error::None)
744 ++total_stats_.failed_kms_present_;
745
Rob Herring4f6c62e2018-05-17 14:33:02 -0500746 if (ret == HWC2::Error::BadLayer) {
747 // Can we really have no client or device layers?
Matteo Franchinc56eede2019-12-03 17:10:38 +0000748 *present_fence = -1;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500749 return HWC2::Error::None;
750 }
751 if (ret != HWC2::Error::None)
752 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500753
Matteo Franchinc56eede2019-12-03 17:10:38 +0000754 *present_fence = present_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500755
756 ++frame_no_;
757 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500758}
759
760HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500761 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400762 auto mode = std::find_if(connector_->modes().begin(),
763 connector_->modes().end(),
764 [config](DrmMode const &m) {
765 return m.id() == config;
766 });
Sean Paulac874152016-03-10 16:00:26 -0500767 if (mode == connector_->modes().end()) {
768 ALOGE("Could not find active mode for %d", config);
769 return HWC2::Error::BadConfig;
770 }
771
Sean Paulf72cccd2018-08-27 13:59:08 -0400772 std::unique_ptr<DrmDisplayComposition> composition = compositor_
773 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500774 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
775 int ret = composition->SetDisplayMode(*mode);
Sean Pauled45a8e2017-02-28 13:17:34 -0500776 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500777 if (ret) {
778 ALOGE("Failed to queue dpms composition on %d", ret);
779 return HWC2::Error::BadConfig;
780 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300781
782 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500783
784 // Setup the client layer's dimensions
785 hwc_rect_t display_frame = {.left = 0,
786 .top = 0,
787 .right = static_cast<int>(mode->h_display()),
788 .bottom = static_cast<int>(mode->v_display())};
789 client_layer_.SetLayerDisplayFrame(display_frame);
790 hwc_frect_t source_crop = {.left = 0.0f,
791 .top = 0.0f,
792 .right = mode->h_display() + 0.0f,
793 .bottom = mode->v_display() + 0.0f};
794 client_layer_.SetLayerSourceCrop(source_crop);
795
796 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500797}
798
799HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
800 int32_t acquire_fence,
801 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600802 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500803 supported(__func__);
804 UniqueFd uf(acquire_fence);
805
806 client_layer_.set_buffer(target);
807 client_layer_.set_acquire_fence(uf.get());
808 client_layer_.SetLayerDataspace(dataspace);
809 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500810}
811
812HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500813 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800814
815 if (mode != HAL_COLOR_MODE_NATIVE)
Vincent Donnefort7834a892019-10-09 15:53:56 +0100816 return HWC2::Error::BadParameter;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800817
818 color_mode_ = mode;
819 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500820}
821
822HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500823 int32_t hint) {
824 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200825 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
826 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
827 return HWC2::Error::BadParameter;
828
829 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
830 return HWC2::Error::BadParameter;
831
832 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
833 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
834 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
835
836 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500837}
838
839HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500840 int32_t release_fence) {
841 supported(__func__);
842 // TODO: Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500843 return unsupported(__func__, buffer, release_fence);
844}
845
Sean Paulac874152016-03-10 16:00:26 -0500846HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
847 supported(__func__);
848 uint64_t dpms_value = 0;
849 auto mode = static_cast<HWC2::PowerMode>(mode_in);
850 switch (mode) {
851 case HWC2::PowerMode::Off:
852 dpms_value = DRM_MODE_DPMS_OFF;
853 break;
854 case HWC2::PowerMode::On:
855 dpms_value = DRM_MODE_DPMS_ON;
856 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100857 case HWC2::PowerMode::Doze:
858 case HWC2::PowerMode::DozeSuspend:
859 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500860 default:
861 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100862 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500863 };
864
Sean Paulf72cccd2018-08-27 13:59:08 -0400865 std::unique_ptr<DrmDisplayComposition> composition = compositor_
866 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500867 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
868 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500869 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500870 if (ret) {
871 ALOGE("Failed to apply the dpms composition ret=%d", ret);
872 return HWC2::Error::BadParameter;
873 }
874 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500875}
876
877HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500878 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300879 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500880 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500881}
882
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200883uint32_t DrmHwcTwo::HwcDisplay::CalcPixOps(
884 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t first_z,
885 size_t size) {
886 uint32_t pixops = 0;
887 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
888 if (l.first >= first_z && l.first < first_z + size) {
889 hwc_rect_t df = l.second->display_frame();
890 pixops += (df.right - df.left) * (df.bottom - df.top);
891 }
892 }
893 return pixops;
894}
895
896void DrmHwcTwo::HwcDisplay::MarkValidated(
897 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t client_first_z,
898 size_t client_size) {
899 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
900 if (l.first >= client_first_z && l.first < client_first_z + client_size)
901 l.second->set_validated_type(HWC2::Composition::Client);
902 else
903 l.second->set_validated_type(HWC2::Composition::Device);
904 }
905}
906
Sean Pauled2ec4b2016-03-10 15:35:40 -0500907HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500908 uint32_t *num_requests) {
909 supported(__func__);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500910
Matvii Zorinef3c7972020-08-11 15:15:44 +0300911 return backend_->ValidateDisplay(this, num_types, num_requests);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500912}
913
John Stultz8c7229d2020-02-07 21:31:08 +0000914#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800915HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
916 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
917 supported(__func__);
918
919 drmModePropertyBlobPtr blob;
920 int ret;
921 uint64_t blob_id;
922
923 std::tie(ret, blob_id) = connector_->edid_property().value();
924 if (ret) {
925 ALOGE("Failed to get edid property value.");
926 return HWC2::Error::Unsupported;
927 }
928
929 blob = drmModeGetPropertyBlob(drm_->fd(), blob_id);
930
Andrii Chepurnyi8115dbe2020-04-14 13:03:57 +0300931 if (outData) {
932 *outDataSize = std::min(*outDataSize, blob->length);
933 memcpy(outData, blob->data, *outDataSize);
934 } else {
935 *outDataSize = blob->length;
936 }
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800937 *outPort = connector_->id();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800938
939 return HWC2::Error::None;
940}
941
942HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
943 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
944 unsupported(__func__, outCapabilities);
945
946 if (outNumCapabilities == NULL) {
947 return HWC2::Error::BadParameter;
948 }
949
950 *outNumCapabilities = 0;
951
952 return HWC2::Error::None;
953}
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +0300954
955HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayBrightnessSupport(
956 bool *supported) {
957 *supported = false;
958 return HWC2::Error::None;
959}
960
961HWC2::Error DrmHwcTwo::HwcDisplay::SetDisplayBrightness(
962 float /* brightness */) {
963 return HWC2::Error::Unsupported;
964}
965
John Stultz8c7229d2020-02-07 21:31:08 +0000966#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800967
Andrii Chepurnyi50d37452020-04-24 14:20:24 +0300968#if PLATFORM_SDK_VERSION > 27
969
970HWC2::Error DrmHwcTwo::HwcDisplay::GetRenderIntents(
971 int32_t mode, uint32_t *outNumIntents,
972 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
973 if (mode != HAL_COLOR_MODE_NATIVE) {
974 return HWC2::Error::BadParameter;
975 }
976
977 if (outIntents == nullptr) {
978 *outNumIntents = 1;
979 return HWC2::Error::None;
980 }
981 *outNumIntents = 1;
982 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
983 return HWC2::Error::None;
984}
985
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +0300986HWC2::Error DrmHwcTwo::HwcDisplay::SetColorModeWithIntent(int32_t mode,
987 int32_t intent) {
988 if (mode != HAL_COLOR_MODE_NATIVE)
989 return HWC2::Error::BadParameter;
990 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
991 return HWC2::Error::BadParameter;
992 color_mode_ = mode;
993 return HWC2::Error::None;
994}
995
Andrii Chepurnyi50d37452020-04-24 14:20:24 +0300996#endif /* PLATFORM_SDK_VERSION > 27 */
997
Sean Pauled2ec4b2016-03-10 15:35:40 -0500998HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -0500999 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -08001000 cursor_x_ = x;
1001 cursor_y_ = y;
1002 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001003}
1004
1005HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -05001006 supported(__func__);
1007 blending_ = static_cast<HWC2::BlendMode>(mode);
1008 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001009}
1010
1011HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001012 int32_t acquire_fence) {
1013 supported(__func__);
1014 UniqueFd uf(acquire_fence);
1015
Sean Paulac874152016-03-10 16:00:26 -05001016 set_buffer(buffer);
1017 set_acquire_fence(uf.get());
1018 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001019}
1020
1021HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001022 // TODO: Put to client composition here?
1023 supported(__func__);
1024 layer_color_ = color;
1025 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001026}
1027
1028HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001029 sf_type_ = static_cast<HWC2::Composition>(type);
1030 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001031}
1032
1033HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001034 supported(__func__);
1035 dataspace_ = static_cast<android_dataspace_t>(dataspace);
1036 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001037}
1038
1039HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001040 supported(__func__);
1041 display_frame_ = frame;
1042 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001043}
1044
1045HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001046 supported(__func__);
1047 alpha_ = alpha;
1048 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001049}
1050
1051HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1052 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001053 supported(__func__);
1054 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001055 return unsupported(__func__, stream);
1056}
1057
1058HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001059 supported(__func__);
1060 source_crop_ = crop;
1061 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001062}
1063
1064HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001065 supported(__func__);
1066 // TODO: We don't use surface damage, marking as unsupported
1067 unsupported(__func__, damage);
1068 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001069}
1070
1071HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001072 supported(__func__);
1073 transform_ = static_cast<HWC2::Transform>(transform);
1074 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001075}
1076
1077HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001078 supported(__func__);
1079 // TODO: We don't use this information, marking as unsupported
1080 unsupported(__func__, visible);
1081 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001082}
1083
Sean Paulac874152016-03-10 16:00:26 -05001084HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1085 supported(__func__);
1086 z_order_ = order;
1087 return HWC2::Error::None;
1088}
1089
1090void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1091 supported(__func__);
1092 switch (blending_) {
1093 case HWC2::BlendMode::None:
1094 layer->blending = DrmHwcBlending::kNone;
1095 break;
1096 case HWC2::BlendMode::Premultiplied:
1097 layer->blending = DrmHwcBlending::kPreMult;
1098 break;
1099 case HWC2::BlendMode::Coverage:
1100 layer->blending = DrmHwcBlending::kCoverage;
1101 break;
1102 default:
1103 ALOGE("Unknown blending mode b=%d", blending_);
1104 layer->blending = DrmHwcBlending::kNone;
1105 break;
1106 }
1107
1108 OutputFd release_fence = release_fence_output();
1109
1110 layer->sf_handle = buffer_;
1111 layer->acquire_fence = acquire_fence_.Release();
1112 layer->release_fence = std::move(release_fence);
1113 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +02001114 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -05001115 layer->SetSourceCrop(source_crop_);
1116 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -05001117}
1118
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001119void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
1120 auto cb = callbacks_.find(HWC2::Callback::Hotplug);
1121 if (cb == callbacks_.end())
1122 return;
1123
1124 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(cb->second.func);
1125 hotplug(cb->second.data, displayid,
1126 (state == DRM_MODE_CONNECTED ? HWC2_CONNECTION_CONNECTED
1127 : HWC2_CONNECTION_DISCONNECTED));
1128}
1129
1130void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
1131 for (auto &conn : drmDevice->connectors()) {
1132 if (conn->state() != DRM_MODE_CONNECTED)
1133 continue;
1134 HandleDisplayHotplug(conn->display(), conn->state());
1135 }
1136}
1137
1138void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
1139 for (auto &conn : drm_->connectors()) {
1140 drmModeConnection old_state = conn->state();
1141 drmModeConnection cur_state = conn->UpdateModes()
1142 ? DRM_MODE_UNKNOWNCONNECTION
1143 : conn->state();
1144
1145 if (cur_state == old_state)
1146 continue;
1147
1148 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
1149 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1150 conn->id(), conn->display());
1151
1152 int display_id = conn->display();
1153 if (cur_state == DRM_MODE_CONNECTED) {
1154 auto &display = hwc2_->displays_.at(display_id);
1155 display.ChosePreferredConfig();
1156 } else {
1157 auto &display = hwc2_->displays_.at(display_id);
1158 display.ClearDisplay();
1159 }
1160
1161 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1162 }
1163}
1164
Sean Pauled2ec4b2016-03-10 15:35:40 -05001165// static
1166int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1167 unsupported(__func__);
1168 return 0;
1169}
1170
1171// static
1172void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001173 uint32_t *out_count,
1174 int32_t * /*out_capabilities*/) {
1175 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001176 *out_count = 0;
1177}
1178
1179// static
Sean Paulac874152016-03-10 16:00:26 -05001180hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1181 struct hwc2_device * /*dev*/, int32_t descriptor) {
1182 supported(__func__);
1183 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001184 switch (func) {
1185 // Device functions
1186 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1187 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1188 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1189 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001190 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001191 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1192 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1193 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1194 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1195 case HWC2::FunctionDescriptor::Dump:
1196 return ToHook<HWC2_PFN_DUMP>(
1197 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1198 uint32_t *, char *>);
1199 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1200 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1201 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1202 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1203 case HWC2::FunctionDescriptor::RegisterCallback:
1204 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1205 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1206 &DrmHwcTwo::RegisterCallback, int32_t,
1207 hwc2_callback_data_t, hwc2_function_pointer_t>);
1208
1209 // Display functions
1210 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1211 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1212 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1213 &HwcDisplay::AcceptDisplayChanges>);
1214 case HWC2::FunctionDescriptor::CreateLayer:
1215 return ToHook<HWC2_PFN_CREATE_LAYER>(
1216 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1217 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1218 case HWC2::FunctionDescriptor::DestroyLayer:
1219 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1220 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1221 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1222 case HWC2::FunctionDescriptor::GetActiveConfig:
1223 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1224 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1225 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1226 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1227 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1228 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1229 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1230 hwc2_layer_t *, int32_t *>);
1231 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1232 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1233 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1234 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1235 int32_t, int32_t>);
1236 case HWC2::FunctionDescriptor::GetColorModes:
1237 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1238 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1239 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1240 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001241 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1242 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1243 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1244 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001245 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001246 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1247 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1248 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1249 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001250 case HWC2::FunctionDescriptor::GetDisplayName:
1251 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1252 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1253 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1254 case HWC2::FunctionDescriptor::GetDisplayRequests:
1255 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1256 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1257 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1258 hwc2_layer_t *, int32_t *>);
1259 case HWC2::FunctionDescriptor::GetDisplayType:
1260 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1261 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1262 &HwcDisplay::GetDisplayType, int32_t *>);
1263 case HWC2::FunctionDescriptor::GetDozeSupport:
1264 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1265 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1266 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001267 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1268 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1269 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1270 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1271 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001272 case HWC2::FunctionDescriptor::GetReleaseFences:
1273 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1274 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1275 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1276 int32_t *>);
1277 case HWC2::FunctionDescriptor::PresentDisplay:
1278 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1279 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1280 &HwcDisplay::PresentDisplay, int32_t *>);
1281 case HWC2::FunctionDescriptor::SetActiveConfig:
1282 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1283 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1284 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1285 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001286 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1287 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1288 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1289 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001290 case HWC2::FunctionDescriptor::SetColorMode:
1291 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1292 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1293 &HwcDisplay::SetColorMode, int32_t>);
1294 case HWC2::FunctionDescriptor::SetColorTransform:
1295 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1296 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1297 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1298 case HWC2::FunctionDescriptor::SetOutputBuffer:
1299 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1300 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1301 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1302 case HWC2::FunctionDescriptor::SetPowerMode:
1303 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1304 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1305 &HwcDisplay::SetPowerMode, int32_t>);
1306 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1307 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1308 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1309 &HwcDisplay::SetVsyncEnabled, int32_t>);
1310 case HWC2::FunctionDescriptor::ValidateDisplay:
1311 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1312 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1313 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001314#if PLATFORM_SDK_VERSION > 27
1315 case HWC2::FunctionDescriptor::GetRenderIntents:
1316 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
1317 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
1318 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
1319 int32_t *>);
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001320 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
1321 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
1322 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
1323 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001324#endif
John Stultz8c7229d2020-02-07 21:31:08 +00001325#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001326 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1327 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1328 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1329 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1330 uint32_t *, uint8_t *>);
1331 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1332 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1333 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1334 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1335 uint32_t *>);
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001336 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
1337 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
1338 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
1339 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
1340 case HWC2::FunctionDescriptor::SetDisplayBrightness:
1341 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
1342 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
1343 &HwcDisplay::SetDisplayBrightness, float>);
John Stultz8c7229d2020-02-07 21:31:08 +00001344#endif /* PLATFORM_SDK_VERSION > 28 */
Sean Pauled2ec4b2016-03-10 15:35:40 -05001345 // Layer functions
1346 case HWC2::FunctionDescriptor::SetCursorPosition:
1347 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1348 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1349 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1350 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1351 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1352 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1353 &HwcLayer::SetLayerBlendMode, int32_t>);
1354 case HWC2::FunctionDescriptor::SetLayerBuffer:
1355 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1356 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1357 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1358 case HWC2::FunctionDescriptor::SetLayerColor:
1359 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1360 LayerHook<decltype(&HwcLayer::SetLayerColor),
1361 &HwcLayer::SetLayerColor, hwc_color_t>);
1362 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1363 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1364 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1365 &HwcLayer::SetLayerCompositionType, int32_t>);
1366 case HWC2::FunctionDescriptor::SetLayerDataspace:
1367 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1368 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1369 &HwcLayer::SetLayerDataspace, int32_t>);
1370 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1371 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1372 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1373 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1374 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1375 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1376 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1377 &HwcLayer::SetLayerPlaneAlpha, float>);
1378 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001379 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1380 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1381 &HwcLayer::SetLayerSidebandStream,
1382 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001383 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1384 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1385 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1386 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1387 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1388 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1389 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1390 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1391 case HWC2::FunctionDescriptor::SetLayerTransform:
1392 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1393 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1394 &HwcLayer::SetLayerTransform, int32_t>);
1395 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1396 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1397 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1398 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1399 case HWC2::FunctionDescriptor::SetLayerZOrder:
1400 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1401 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1402 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001403 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001404 default:
1405 return NULL;
1406 }
1407}
Sean Paulac874152016-03-10 16:00:26 -05001408
1409// static
1410int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1411 struct hw_device_t **dev) {
1412 supported(__func__);
1413 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1414 ALOGE("Invalid module name- %s", name);
1415 return -EINVAL;
1416 }
1417
1418 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1419 if (!ctx) {
1420 ALOGE("Failed to allocate DrmHwcTwo");
1421 return -ENOMEM;
1422 }
1423
1424 HWC2::Error err = ctx->Init();
1425 if (err != HWC2::Error::None) {
1426 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1427 return -EINVAL;
1428 }
1429
1430 ctx->common.module = const_cast<hw_module_t *>(module);
1431 *dev = &ctx->common;
1432 ctx.release();
1433 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001434}
Sean Paulf72cccd2018-08-27 13:59:08 -04001435} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001436
1437static struct hw_module_methods_t hwc2_module_methods = {
1438 .open = android::DrmHwcTwo::HookDevOpen,
1439};
1440
1441hw_module_t HAL_MODULE_INFO_SYM = {
1442 .tag = HARDWARE_MODULE_TAG,
1443 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1444 .id = HWC_HARDWARE_MODULE_ID,
1445 .name = "DrmHwcTwo module",
1446 .author = "The Android Open Source Project",
1447 .methods = &hwc2_module_methods,
1448 .dso = NULL,
1449 .reserved = {0},
1450};