blob: a847c35586d842a4f3df0839c515977af60620ef [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"
Sean Paulac874152016-03-10 16:00:26 -050021#include "drmdisplaycomposition.h"
22#include "drmhwcomposer.h"
Sean Paulac874152016-03-10 16:00:26 -050023#include "platform.h"
24#include "vsyncworker.h"
25
26#include <inttypes.h>
27#include <string>
Sean Pauled2ec4b2016-03-10 15:35:40 -050028
Sean Paulac874152016-03-10 16:00:26 -050029#include <cutils/properties.h>
30#include <hardware/hardware.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050031#include <hardware/hwcomposer2.h>
Sean Paulf72cccd2018-08-27 13:59:08 -040032#include <log/log.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050033
34namespace android {
35
Sean Paulac874152016-03-10 16:00:26 -050036class DrmVsyncCallback : public VsyncCallback {
37 public:
38 DrmVsyncCallback(hwc2_callback_data_t data, hwc2_function_pointer_t hook)
39 : data_(data), hook_(hook) {
40 }
41
42 void Callback(int display, int64_t timestamp) {
43 auto hook = reinterpret_cast<HWC2_PFN_VSYNC>(hook_);
44 hook(data_, display, timestamp);
45 }
46
47 private:
48 hwc2_callback_data_t data_;
49 hwc2_function_pointer_t hook_;
50};
51
Sean Pauled2ec4b2016-03-10 15:35:40 -050052DrmHwcTwo::DrmHwcTwo() {
Sean Paulac874152016-03-10 16:00:26 -050053 common.tag = HARDWARE_DEVICE_TAG;
54 common.version = HWC_DEVICE_API_VERSION_2_0;
Sean Pauled2ec4b2016-03-10 15:35:40 -050055 common.close = HookDevClose;
56 getCapabilities = HookDevGetCapabilities;
57 getFunction = HookDevGetFunction;
58}
59
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030060HWC2::Error DrmHwcTwo::CreateDisplay(hwc2_display_t displ,
61 HWC2::DisplayType type) {
62 DrmDevice *drm = resource_manager_.GetDrmDevice(displ);
63 std::shared_ptr<Importer> importer = resource_manager_.GetImporter(displ);
Alexandru Gheorghec5463582018-03-27 15:52:02 +010064 if (!drm || !importer) {
65 ALOGE("Failed to get a valid drmresource and importer");
Sean Paulac874152016-03-10 16:00:26 -050066 return HWC2::Error::NoResources;
67 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030068 displays_.emplace(std::piecewise_construct, std::forward_as_tuple(displ),
Sean Paulf72cccd2018-08-27 13:59:08 -040069 std::forward_as_tuple(&resource_manager_, drm, importer,
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030070 displ, type));
Sean Paulac874152016-03-10 16:00:26 -050071
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030072 DrmCrtc *crtc = drm->GetCrtcForDisplay(static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050073 if (!crtc) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030074 ALOGE("Failed to get crtc for display %d", static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050075 return HWC2::Error::BadDisplay;
76 }
Sean Paulac874152016-03-10 16:00:26 -050077 std::vector<DrmPlane *> display_planes;
Alexandru Gheorghec5463582018-03-27 15:52:02 +010078 for (auto &plane : drm->planes()) {
Sean Paulac874152016-03-10 16:00:26 -050079 if (plane->GetCrtcSupported(*crtc))
80 display_planes.push_back(plane.get());
81 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030082 displays_.at(displ).Init(&display_planes);
Sean Paulac874152016-03-10 16:00:26 -050083 return HWC2::Error::None;
84}
85
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030086HWC2::Error DrmHwcTwo::Init() {
87 int rv = resource_manager_.Init();
88 if (rv) {
89 ALOGE("Can't initialize the resource manager %d", rv);
90 return HWC2::Error::NoResources;
91 }
92
93 HWC2::Error ret = HWC2::Error::None;
94 for (int i = 0; i < resource_manager_.getDisplayCount(); i++) {
95 ret = CreateDisplay(i, HWC2::DisplayType::Physical);
96 if (ret != HWC2::Error::None) {
97 ALOGE("Failed to create display %d with error %d", i, ret);
98 return ret;
99 }
100 }
101
102 auto &drmDevices = resource_manager_.getDrmDevices();
103 for (auto &device : drmDevices) {
104 device->RegisterHotplugHandler(new DrmHotplugHandler(this, device.get()));
105 }
106 return ret;
107}
108
Sean Pauled2ec4b2016-03-10 15:35:40 -0500109template <typename... Args>
110static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
111 ALOGV("Unsupported function: %s", func);
112 return HWC2::Error::Unsupported;
113}
114
Sean Paulac874152016-03-10 16:00:26 -0500115static inline void supported(char const *func) {
116 ALOGV("Supported function: %s", func);
117}
118
Sean Pauled2ec4b2016-03-10 15:35:40 -0500119HWC2::Error DrmHwcTwo::CreateVirtualDisplay(uint32_t width, uint32_t height,
120 int32_t *format,
121 hwc2_display_t *display) {
122 // TODO: Implement virtual display
Sean Paulac874152016-03-10 16:00:26 -0500123 return unsupported(__func__, width, height, format, display);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500124}
125
126HWC2::Error DrmHwcTwo::DestroyVirtualDisplay(hwc2_display_t display) {
Sean Paulac874152016-03-10 16:00:26 -0500127 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500128 return unsupported(__func__, display);
129}
130
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200131std::string DrmHwcTwo::HwcDisplay::DumpDelta(
132 DrmHwcTwo::HwcDisplay::Stats delta) {
133 if (delta.total_pixops_ == 0)
134 return "No stats yet";
135 double Ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
136
137 return (std::stringstream()
138 << " Total frames count: " << delta.total_frames_ << "\n"
139 << " Failed to test commit frames: " << delta.failed_kms_validate_
140 << "\n"
141 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
142 << ((delta.failed_kms_present_ > 0)
143 ? " !!! Internal failure, FIX it please\n"
144 : "")
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200145 << " Flattened frames: " << delta.frames_flattened_ << "\n"
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200146 << " Pixel operations (free units)"
147 << " : [TOTAL: " << delta.total_pixops_
148 << " / GPU: " << delta.gpu_pixops_ << "]\n"
149 << " Composition efficiency: " << Ratio)
150 .str();
151}
152
153std::string DrmHwcTwo::HwcDisplay::Dump() {
154 auto out = (std::stringstream()
155 << "- Display on: " << connector_->name() << "\n"
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200156 << " Flattening state: " << compositor_.GetFlatteningState()
157 << "\n"
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200158 << "Statistics since system boot:\n"
159 << DumpDelta(total_stats_) << "\n\n"
160 << "Statistics since last dumpsys request:\n"
161 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n")
162 .str();
163
164 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
165 return out;
166}
167
168void DrmHwcTwo::Dump(uint32_t *outSize, char *outBuffer) {
169 supported(__func__);
170
171 if (outBuffer != nullptr) {
172 auto copiedBytes = mDumpString.copy(outBuffer, *outSize);
173 *outSize = static_cast<uint32_t>(copiedBytes);
174 return;
175 }
176
177 std::stringstream output;
178
179 output << "-- drm_hwcomposer --\n\n";
180
181 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &dp : displays_)
182 output << dp.second.Dump();
183
184 mDumpString = output.str();
185 *outSize = static_cast<uint32_t>(mDumpString.size());
Sean Pauled2ec4b2016-03-10 15:35:40 -0500186}
187
188uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() {
Sean Paulac874152016-03-10 16:00:26 -0500189 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500190 unsupported(__func__);
191 return 0;
192}
193
194HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor,
Sean Paulac874152016-03-10 16:00:26 -0500195 hwc2_callback_data_t data,
196 hwc2_function_pointer_t function) {
197 supported(__func__);
198 auto callback = static_cast<HWC2::Callback>(descriptor);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300199
200 if (!function) {
201 callbacks_.erase(callback);
202 return HWC2::Error::None;
203 }
204
Sean Paulac874152016-03-10 16:00:26 -0500205 callbacks_.emplace(callback, HwcCallback(data, function));
206
207 switch (callback) {
208 case HWC2::Callback::Hotplug: {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300209 auto &drmDevices = resource_manager_.getDrmDevices();
210 for (auto &device : drmDevices)
211 HandleInitialHotplugState(device.get());
Sean Paulac874152016-03-10 16:00:26 -0500212 break;
213 }
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200214 case HWC2::Callback::Refresh: {
215 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
216 displays_)
217 d.second.RegisterRefreshCallback(data, function);
218 break;
219 }
Sean Paulac874152016-03-10 16:00:26 -0500220 case HWC2::Callback::Vsync: {
221 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
222 displays_)
223 d.second.RegisterVsyncCallback(data, function);
224 break;
225 }
226 default:
227 break;
228 }
229 return HWC2::Error::None;
230}
231
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100232DrmHwcTwo::HwcDisplay::HwcDisplay(ResourceManager *resource_manager,
233 DrmDevice *drm,
Sean Paulac874152016-03-10 16:00:26 -0500234 std::shared_ptr<Importer> importer,
Sean Paulac874152016-03-10 16:00:26 -0500235 hwc2_display_t handle, HWC2::DisplayType type)
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100236 : resource_manager_(resource_manager),
237 drm_(drm),
238 importer_(importer),
239 handle_(handle),
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200240 type_(type),
241 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
Sean Paulac874152016-03-10 16:00:26 -0500242 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200243
244 // clang-format off
245 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
246 0.0, 1.0, 0.0, 0.0,
247 0.0, 0.0, 1.0, 0.0,
248 0.0, 0.0, 0.0, 1.0};
249 // clang-format on
Sean Paulac874152016-03-10 16:00:26 -0500250}
251
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300252void DrmHwcTwo::HwcDisplay::ClearDisplay() {
253 compositor_.ClearDisplay();
254}
255
Sean Paulac874152016-03-10 16:00:26 -0500256HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
257 supported(__func__);
258 planner_ = Planner::CreateInstance(drm_);
259 if (!planner_) {
260 ALOGE("Failed to create planner instance for composition");
261 return HWC2::Error::NoResources;
262 }
263
264 int display = static_cast<int>(handle_);
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100265 int ret = compositor_.Init(resource_manager_, display);
Sean Paulac874152016-03-10 16:00:26 -0500266 if (ret) {
267 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
268 return HWC2::Error::NoResources;
269 }
270
271 // Split up the given display planes into primary and overlay to properly
272 // interface with the composition
273 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
Jason Macnakf1af9572020-08-20 11:49:51 -0700274 property_get("vendor.hwc.drm.use_overlay_planes", use_overlay_planes_prop,
275 "1");
Sean Paulac874152016-03-10 16:00:26 -0500276 bool use_overlay_planes = atoi(use_overlay_planes_prop);
277 for (auto &plane : *planes) {
278 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
279 primary_planes_.push_back(plane);
280 else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY)
281 overlay_planes_.push_back(plane);
282 }
283
284 crtc_ = drm_->GetCrtcForDisplay(display);
285 if (!crtc_) {
286 ALOGE("Failed to get crtc for display %d", display);
287 return HWC2::Error::BadDisplay;
288 }
289
290 connector_ = drm_->GetConnectorForDisplay(display);
291 if (!connector_) {
292 ALOGE("Failed to get connector for display %d", display);
293 return HWC2::Error::BadDisplay;
294 }
295
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300296 ret = vsync_worker_.Init(drm_, display);
297 if (ret) {
298 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
299 return HWC2::Error::BadDisplay;
300 }
301
302 return ChosePreferredConfig();
303}
304
305HWC2::Error DrmHwcTwo::HwcDisplay::ChosePreferredConfig() {
Sean Paulac874152016-03-10 16:00:26 -0500306 // Fetch the number of modes from the display
307 uint32_t num_configs;
308 HWC2::Error err = GetDisplayConfigs(&num_configs, NULL);
309 if (err != HWC2::Error::None || !num_configs)
310 return err;
311
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200312 return SetActiveConfig(connector_->get_preferred_mode_id());
Sean Paulac874152016-03-10 16:00:26 -0500313}
314
315HWC2::Error DrmHwcTwo::HwcDisplay::RegisterVsyncCallback(
316 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
317 supported(__func__);
318 auto callback = std::make_shared<DrmVsyncCallback>(data, func);
Adrian Salidofa37f672017-02-16 10:29:46 -0800319 vsync_worker_.RegisterCallback(std::move(callback));
Sean Paulac874152016-03-10 16:00:26 -0500320 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500321}
322
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200323void DrmHwcTwo::HwcDisplay::RegisterRefreshCallback(
324 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
325 supported(__func__);
326 auto hook = reinterpret_cast<HWC2_PFN_REFRESH>(func);
327 compositor_.SetRefreshCallback([data, hook](int display) {
328 hook(data, static_cast<hwc2_display_t>(display));
329 });
330}
331
Sean Pauled2ec4b2016-03-10 15:35:40 -0500332HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500333 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500334 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
335 l.second.accept_type_change();
336 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500337}
338
339HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500340 supported(__func__);
341 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
342 *layer = static_cast<hwc2_layer_t>(layer_idx_);
343 ++layer_idx_;
344 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500345}
346
347HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Sean Paulac874152016-03-10 16:00:26 -0500348 supported(__func__);
Vincent Donnefort9abec032019-10-09 15:43:43 +0100349 if (!get_layer(layer))
350 return HWC2::Error::BadLayer;
351
Sean Paulac874152016-03-10 16:00:26 -0500352 layers_.erase(layer);
353 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500354}
355
356HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
Sean Paulac874152016-03-10 16:00:26 -0500357 supported(__func__);
358 DrmMode const &mode = connector_->active_mode();
359 if (mode.id() == 0)
360 return HWC2::Error::BadConfig;
361
362 *config = mode.id();
363 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500364}
365
366HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
367 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500368 supported(__func__);
369 uint32_t num_changes = 0;
370 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
371 if (l.second.type_changed()) {
372 if (layers && num_changes < *num_elements)
373 layers[num_changes] = l.first;
374 if (types && num_changes < *num_elements)
375 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
376 ++num_changes;
377 }
378 }
379 if (!layers && !types)
380 *num_elements = num_changes;
381 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500382}
383
384HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500385 uint32_t height,
386 int32_t /*format*/,
387 int32_t dataspace) {
388 supported(__func__);
389 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
390 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
391
392 if (width < min.first || height < min.second)
393 return HWC2::Error::Unsupported;
394
395 if (width > max.first || height > max.second)
396 return HWC2::Error::Unsupported;
397
398 if (dataspace != HAL_DATASPACE_UNKNOWN &&
399 dataspace != HAL_DATASPACE_STANDARD_UNSPECIFIED)
400 return HWC2::Error::Unsupported;
401
402 // TODO: Validate format can be handled by either GL or planes
403 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500404}
405
406HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500407 int32_t *modes) {
408 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800409 if (!modes)
410 *num_modes = 1;
411
412 if (modes)
413 *modes = HAL_COLOR_MODE_NATIVE;
414
415 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500416}
417
418HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500419 int32_t attribute_in,
420 int32_t *value) {
421 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400422 auto mode = std::find_if(connector_->modes().begin(),
423 connector_->modes().end(),
424 [config](DrmMode const &m) {
425 return m.id() == config;
426 });
Sean Paulac874152016-03-10 16:00:26 -0500427 if (mode == connector_->modes().end()) {
428 ALOGE("Could not find active mode for %d", config);
429 return HWC2::Error::BadConfig;
430 }
431
432 static const int32_t kUmPerInch = 25400;
433 uint32_t mm_width = connector_->mm_width();
434 uint32_t mm_height = connector_->mm_height();
435 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
436 switch (attribute) {
437 case HWC2::Attribute::Width:
438 *value = mode->h_display();
439 break;
440 case HWC2::Attribute::Height:
441 *value = mode->v_display();
442 break;
443 case HWC2::Attribute::VsyncPeriod:
444 // in nanoseconds
445 *value = 1000 * 1000 * 1000 / mode->v_refresh();
446 break;
447 case HWC2::Attribute::DpiX:
448 // Dots per 1000 inches
449 *value = mm_width ? (mode->h_display() * kUmPerInch) / mm_width : -1;
450 break;
451 case HWC2::Attribute::DpiY:
452 // Dots per 1000 inches
453 *value = mm_height ? (mode->v_display() * kUmPerInch) / mm_height : -1;
454 break;
455 default:
456 *value = -1;
457 return HWC2::Error::BadConfig;
458 }
459 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500460}
461
462HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
463 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500464 supported(__func__);
465 // Since this callback is normally invoked twice (once to get the count, and
466 // once to populate configs), we don't really want to read the edid
467 // redundantly. Instead, only update the modes on the first invocation. While
468 // it's possible this will result in stale modes, it'll all come out in the
469 // wash when we try to set the active config later.
470 if (!configs) {
471 int ret = connector_->UpdateModes();
472 if (ret) {
473 ALOGE("Failed to update display modes %d", ret);
474 return HWC2::Error::BadDisplay;
475 }
476 }
477
Neil Armstrongb67d0492019-06-20 09:00:21 +0000478 // Since the upper layers only look at vactive/hactive/refresh, height and
479 // width, it doesn't differentiate interlaced from progressive and other
480 // similar modes. Depending on the order of modes we return to SF, it could
481 // end up choosing a suboptimal configuration and dropping the preferred
482 // mode. To workaround this, don't offer interlaced modes to SF if there is
483 // at least one non-interlaced alternative and only offer a single WxH@R
484 // mode with at least the prefered mode from in DrmConnector::UpdateModes()
485
486 // TODO: Remove the following block of code until AOSP handles all modes
487 std::vector<DrmMode> sel_modes;
488
489 // Add the preferred mode first to be sure it's not dropped
490 auto mode = std::find_if(connector_->modes().begin(),
491 connector_->modes().end(), [&](DrmMode const &m) {
492 return m.id() ==
493 connector_->get_preferred_mode_id();
494 });
495 if (mode != connector_->modes().end())
496 sel_modes.push_back(*mode);
497
498 // Add the active mode if different from preferred mode
499 if (connector_->active_mode().id() != connector_->get_preferred_mode_id())
500 sel_modes.push_back(connector_->active_mode());
501
502 // Cycle over the modes and filter out "similar" modes, keeping only the
503 // first ones in the order given by DRM (from CEA ids and timings order)
Sean Paulac874152016-03-10 16:00:26 -0500504 for (const DrmMode &mode : connector_->modes()) {
Neil Armstrongb67d0492019-06-20 09:00:21 +0000505 // TODO: Remove this when 3D Attributes are in AOSP
506 if (mode.flags() & DRM_MODE_FLAG_3D_MASK)
507 continue;
508
Neil Armstrong4c027a72019-06-04 14:48:02 +0000509 // TODO: Remove this when the Interlaced attribute is in AOSP
510 if (mode.flags() & DRM_MODE_FLAG_INTERLACE) {
511 auto m = std::find_if(connector_->modes().begin(),
512 connector_->modes().end(),
513 [&mode](DrmMode const &m) {
514 return !(m.flags() & DRM_MODE_FLAG_INTERLACE) &&
515 m.h_display() == mode.h_display() &&
516 m.v_display() == mode.v_display();
517 });
Neil Armstrongb67d0492019-06-20 09:00:21 +0000518 if (m == connector_->modes().end())
519 sel_modes.push_back(mode);
520
521 continue;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000522 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000523
524 // Search for a similar WxH@R mode in the filtered list and drop it if
525 // another mode with the same WxH@R has already been selected
526 // TODO: Remove this when AOSP handles duplicates modes
527 auto m = std::find_if(sel_modes.begin(), sel_modes.end(),
528 [&mode](DrmMode const &m) {
529 return m.h_display() == mode.h_display() &&
530 m.v_display() == mode.v_display() &&
531 m.v_refresh() == mode.v_refresh();
532 });
533 if (m == sel_modes.end())
534 sel_modes.push_back(mode);
535 }
536
537 auto num_modes = static_cast<uint32_t>(sel_modes.size());
538 if (!configs) {
539 *num_configs = num_modes;
540 return HWC2::Error::None;
541 }
542
543 uint32_t idx = 0;
544 for (const DrmMode &mode : sel_modes) {
545 if (idx >= *num_configs)
546 break;
547 configs[idx++] = mode.id();
Sean Paulac874152016-03-10 16:00:26 -0500548 }
549 *num_configs = idx;
550 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500551}
552
553HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500554 supported(__func__);
555 std::ostringstream stream;
556 stream << "display-" << connector_->id();
557 std::string string = stream.str();
558 size_t length = string.length();
559 if (!name) {
560 *size = length;
561 return HWC2::Error::None;
562 }
563
564 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
565 strncpy(name, string.c_str(), *size);
566 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500567}
568
Sean Paulac874152016-03-10 16:00:26 -0500569HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
570 uint32_t *num_elements,
571 hwc2_layer_t *layers,
572 int32_t *layer_requests) {
573 supported(__func__);
574 // TODO: I think virtual display should request
575 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
576 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
577 *num_elements = 0;
578 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500579}
580
581HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500582 supported(__func__);
583 *type = static_cast<int32_t>(type_);
584 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500585}
586
587HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500588 supported(__func__);
589 *support = 0;
590 return HWC2::Error::None;
591}
592
593HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400594 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
595 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500596 supported(__func__);
597 *num_types = 0;
598 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500599}
600
601HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500602 hwc2_layer_t *layers,
603 int32_t *fences) {
604 supported(__func__);
605 uint32_t num_layers = 0;
606
607 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
608 ++num_layers;
609 if (layers == NULL || fences == NULL) {
610 continue;
611 } else if (num_layers > *num_elements) {
612 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
613 return HWC2::Error::None;
614 }
615
616 layers[num_layers - 1] = l.first;
617 fences[num_layers - 1] = l.second.take_release_fence();
618 }
619 *num_elements = num_layers;
620 return HWC2::Error::None;
621}
622
Matteo Franchinc56eede2019-12-03 17:10:38 +0000623void DrmHwcTwo::HwcDisplay::AddFenceToPresentFence(int fd) {
Sean Paulac874152016-03-10 16:00:26 -0500624 if (fd < 0)
625 return;
626
Matteo Franchinc56eede2019-12-03 17:10:38 +0000627 if (present_fence_.get() >= 0) {
628 int old_fence = present_fence_.get();
629 present_fence_.Set(sync_merge("dc_present", old_fence, fd));
630 close(fd);
Sean Paulac874152016-03-10 16:00:26 -0500631 } else {
Matteo Franchinc56eede2019-12-03 17:10:38 +0000632 present_fence_.Set(fd);
Sean Paulac874152016-03-10 16:00:26 -0500633 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500634}
635
Roman Stratiienkoafb36892019-11-08 17:16:11 +0200636bool DrmHwcTwo::HwcDisplay::HardwareSupportsLayerType(
637 HWC2::Composition comp_type) {
638 return comp_type == HWC2::Composition::Device ||
639 comp_type == HWC2::Composition::Cursor;
640}
641
Rob Herring4f6c62e2018-05-17 14:33:02 -0500642HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500643 std::vector<DrmCompositionDisplayLayersMap> layers_map;
644 layers_map.emplace_back();
645 DrmCompositionDisplayLayersMap &map = layers_map.back();
646
647 map.display = static_cast<int>(handle_);
648 map.geometry_changed = true; // TODO: Fix this
649
650 // order the layers by z-order
651 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100652 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500653 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
654 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200655 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500656 case HWC2::Composition::Device:
657 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
658 break;
659 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100660 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500661 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100662 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500663 break;
664 default:
665 continue;
666 }
667 }
668 if (use_client_layer)
669 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
670
Rob Herring4f6c62e2018-05-17 14:33:02 -0500671 if (z_map.empty())
672 return HWC2::Error::BadLayer;
673
Sean Paulac874152016-03-10 16:00:26 -0500674 // now that they're ordered by z, add them to the composition
675 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
676 DrmHwcLayer layer;
677 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200678 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500679 if (ret) {
680 ALOGE("Failed to import layer, ret=%d", ret);
681 return HWC2::Error::NoResources;
682 }
683 map.layers.emplace_back(std::move(layer));
684 }
Sean Paulac874152016-03-10 16:00:26 -0500685
Sean Paulf72cccd2018-08-27 13:59:08 -0400686 std::unique_ptr<DrmDisplayComposition> composition = compositor_
687 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500688 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
689
690 // TODO: Don't always assume geometry changed
691 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
692 if (ret) {
693 ALOGE("Failed to set layers in the composition ret=%d", ret);
694 return HWC2::Error::BadLayer;
695 }
696
697 std::vector<DrmPlane *> primary_planes(primary_planes_);
698 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500699 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500700 if (ret) {
701 ALOGE("Failed to plan the composition ret=%d", ret);
702 return HWC2::Error::BadConfig;
703 }
704
705 // Disable the planes we're not using
706 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
707 composition->AddPlaneDisable(*i);
708 i = primary_planes.erase(i);
709 }
710 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
711 composition->AddPlaneDisable(*i);
712 i = overlay_planes.erase(i);
713 }
714
Rob Herring4f6c62e2018-05-17 14:33:02 -0500715 if (test) {
716 ret = compositor_.TestComposition(composition.get());
717 } else {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500718 ret = compositor_.ApplyComposition(std::move(composition));
Matteo Franchinc56eede2019-12-03 17:10:38 +0000719 AddFenceToPresentFence(compositor_.TakeOutFence());
Rob Herring4f6c62e2018-05-17 14:33:02 -0500720 }
Sean Paulac874152016-03-10 16:00:26 -0500721 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700722 if (!test)
723 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500724 return HWC2::Error::BadParameter;
725 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500726 return HWC2::Error::None;
727}
728
Matteo Franchinc56eede2019-12-03 17:10:38 +0000729HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500730 supported(__func__);
731 HWC2::Error ret;
732
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200733 ++total_stats_.total_frames_;
734
Rob Herring4f6c62e2018-05-17 14:33:02 -0500735 ret = CreateComposition(false);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200736 if (ret != HWC2::Error::None)
737 ++total_stats_.failed_kms_present_;
738
Rob Herring4f6c62e2018-05-17 14:33:02 -0500739 if (ret == HWC2::Error::BadLayer) {
740 // Can we really have no client or device layers?
Matteo Franchinc56eede2019-12-03 17:10:38 +0000741 *present_fence = -1;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500742 return HWC2::Error::None;
743 }
744 if (ret != HWC2::Error::None)
745 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500746
Matteo Franchinc56eede2019-12-03 17:10:38 +0000747 *present_fence = present_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500748
749 ++frame_no_;
750 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500751}
752
753HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500754 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400755 auto mode = std::find_if(connector_->modes().begin(),
756 connector_->modes().end(),
757 [config](DrmMode const &m) {
758 return m.id() == config;
759 });
Sean Paulac874152016-03-10 16:00:26 -0500760 if (mode == connector_->modes().end()) {
761 ALOGE("Could not find active mode for %d", config);
762 return HWC2::Error::BadConfig;
763 }
764
Sean Paulf72cccd2018-08-27 13:59:08 -0400765 std::unique_ptr<DrmDisplayComposition> composition = compositor_
766 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500767 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
768 int ret = composition->SetDisplayMode(*mode);
Sean Pauled45a8e2017-02-28 13:17:34 -0500769 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500770 if (ret) {
771 ALOGE("Failed to queue dpms composition on %d", ret);
772 return HWC2::Error::BadConfig;
773 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300774
775 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500776
777 // Setup the client layer's dimensions
778 hwc_rect_t display_frame = {.left = 0,
779 .top = 0,
780 .right = static_cast<int>(mode->h_display()),
781 .bottom = static_cast<int>(mode->v_display())};
782 client_layer_.SetLayerDisplayFrame(display_frame);
783 hwc_frect_t source_crop = {.left = 0.0f,
784 .top = 0.0f,
785 .right = mode->h_display() + 0.0f,
786 .bottom = mode->v_display() + 0.0f};
787 client_layer_.SetLayerSourceCrop(source_crop);
788
789 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500790}
791
792HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
793 int32_t acquire_fence,
794 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600795 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500796 supported(__func__);
797 UniqueFd uf(acquire_fence);
798
799 client_layer_.set_buffer(target);
800 client_layer_.set_acquire_fence(uf.get());
801 client_layer_.SetLayerDataspace(dataspace);
802 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500803}
804
805HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500806 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800807
808 if (mode != HAL_COLOR_MODE_NATIVE)
Vincent Donnefort7834a892019-10-09 15:53:56 +0100809 return HWC2::Error::BadParameter;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800810
811 color_mode_ = mode;
812 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500813}
814
815HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500816 int32_t hint) {
817 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200818 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
819 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
820 return HWC2::Error::BadParameter;
821
822 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
823 return HWC2::Error::BadParameter;
824
825 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
826 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
827 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
828
829 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500830}
831
832HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500833 int32_t release_fence) {
834 supported(__func__);
835 // TODO: Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500836 return unsupported(__func__, buffer, release_fence);
837}
838
Sean Paulac874152016-03-10 16:00:26 -0500839HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
840 supported(__func__);
841 uint64_t dpms_value = 0;
842 auto mode = static_cast<HWC2::PowerMode>(mode_in);
843 switch (mode) {
844 case HWC2::PowerMode::Off:
845 dpms_value = DRM_MODE_DPMS_OFF;
846 break;
847 case HWC2::PowerMode::On:
848 dpms_value = DRM_MODE_DPMS_ON;
849 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100850 case HWC2::PowerMode::Doze:
851 case HWC2::PowerMode::DozeSuspend:
852 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500853 default:
854 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100855 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500856 };
857
Sean Paulf72cccd2018-08-27 13:59:08 -0400858 std::unique_ptr<DrmDisplayComposition> composition = compositor_
859 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500860 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
861 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500862 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500863 if (ret) {
864 ALOGE("Failed to apply the dpms composition ret=%d", ret);
865 return HWC2::Error::BadParameter;
866 }
867 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500868}
869
870HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500871 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300872 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500873 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500874}
875
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200876uint32_t DrmHwcTwo::HwcDisplay::CalcPixOps(
877 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t first_z,
878 size_t size) {
879 uint32_t pixops = 0;
880 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
881 if (l.first >= first_z && l.first < first_z + size) {
882 hwc_rect_t df = l.second->display_frame();
883 pixops += (df.right - df.left) * (df.bottom - df.top);
884 }
885 }
886 return pixops;
887}
888
889void DrmHwcTwo::HwcDisplay::MarkValidated(
890 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t client_first_z,
891 size_t client_size) {
892 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
893 if (l.first >= client_first_z && l.first < client_first_z + client_size)
894 l.second->set_validated_type(HWC2::Composition::Client);
895 else
896 l.second->set_validated_type(HWC2::Composition::Device);
897 }
898}
899
Sean Pauled2ec4b2016-03-10 15:35:40 -0500900HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500901 uint32_t *num_requests) {
902 supported(__func__);
903 *num_types = 0;
904 *num_requests = 0;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500905 size_t avail_planes = primary_planes_.size() + overlay_planes_.size();
Rob Herring4f6c62e2018-05-17 14:33:02 -0500906
907 /*
908 * If more layers then planes, save one plane
909 * for client composited layers
910 */
911 if (avail_planes < layers_.size())
912 avail_planes--;
913
Liviu Dudaueb012292020-06-15 17:08:33 +0100914 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map, z_map_tmp;
915 uint32_t z_index = 0;
916 // First create a map of layers and z_order values
Roman Stratiienkof2647232019-11-21 01:58:35 +0200917 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
Liviu Dudaueb012292020-06-15 17:08:33 +0100918 z_map_tmp.emplace(std::make_pair(l.second.z_order(), &l.second));
919 // normalise the map so that the lowest z_order layer has key 0
920 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map_tmp)
921 z_map.emplace(std::make_pair(z_index++, l.second));
Roman Stratiienkof2647232019-11-21 01:58:35 +0200922
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200923 uint32_t total_pixops = CalcPixOps(z_map, 0, z_map.size()), gpu_pixops = 0;
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200924
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200925 int client_start = -1, client_size = 0;
926
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200927 if (compositor_.ShouldFlattenOnClient()) {
928 client_start = 0;
929 client_size = z_map.size();
930 MarkValidated(z_map, client_start, client_size);
931 } else {
932 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
933 if (!HardwareSupportsLayerType(l.second->sf_type()) ||
934 !importer_->CanImportBuffer(l.second->buffer()) ||
935 color_transform_hint_ != HAL_COLOR_TRANSFORM_IDENTITY ||
936 (l.second->RequireScalingOrPhasing() &&
937 resource_manager_->ForcedScalingWithGpu())) {
938 if (client_start < 0)
939 client_start = l.first;
940 client_size = (l.first - client_start) + 1;
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200941 }
942 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500943
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200944 int extra_client = (z_map.size() - client_size) - avail_planes;
945 if (extra_client > 0) {
946 int start = 0, steps;
947 if (client_size != 0) {
948 int prepend = std::min(client_start, extra_client);
949 int append = std::min(int(z_map.size() - (client_start + client_size)),
950 extra_client);
951 start = client_start - prepend;
952 client_size += extra_client;
953 steps = 1 + std::min(std::min(append, prepend),
954 int(z_map.size()) - (start + client_size));
955 } else {
956 client_size = extra_client;
957 steps = 1 + z_map.size() - extra_client;
958 }
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200959
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200960 gpu_pixops = INT_MAX;
961 for (int i = 0; i < steps; i++) {
962 uint32_t po = CalcPixOps(z_map, start + i, client_size);
963 if (po < gpu_pixops) {
964 gpu_pixops = po;
965 client_start = start + i;
966 }
967 }
968 }
969
970 MarkValidated(z_map, client_start, client_size);
971
Matvii Zorinfdcdeab2020-04-06 19:03:03 +0300972 bool testing_needed = !(client_start == 0 && client_size == z_map.size());
973
974 if (testing_needed && CreateComposition(true) != HWC2::Error::None) {
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200975 ++total_stats_.failed_kms_validate_;
976 gpu_pixops = total_pixops;
977 client_size = z_map.size();
978 MarkValidated(z_map, 0, client_size);
979 }
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200980 }
981
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200982 *num_types = client_size;
983
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200984 total_stats_.frames_flattened_ = compositor_.GetFlattenedFramesCount();
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200985 total_stats_.gpu_pixops_ += gpu_pixops;
986 total_stats_.total_pixops_ += total_pixops;
Roman Stratiienkof2647232019-11-21 01:58:35 +0200987
Rob Herringee8f45b2017-06-09 15:15:55 -0500988 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500989}
990
John Stultz8c7229d2020-02-07 21:31:08 +0000991#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800992HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
993 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
994 supported(__func__);
995
996 drmModePropertyBlobPtr blob;
997 int ret;
998 uint64_t blob_id;
999
1000 std::tie(ret, blob_id) = connector_->edid_property().value();
1001 if (ret) {
1002 ALOGE("Failed to get edid property value.");
1003 return HWC2::Error::Unsupported;
1004 }
1005
1006 blob = drmModeGetPropertyBlob(drm_->fd(), blob_id);
1007
Andrii Chepurnyi8115dbe2020-04-14 13:03:57 +03001008 if (outData) {
1009 *outDataSize = std::min(*outDataSize, blob->length);
1010 memcpy(outData, blob->data, *outDataSize);
1011 } else {
1012 *outDataSize = blob->length;
1013 }
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001014 *outPort = connector_->id();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001015
1016 return HWC2::Error::None;
1017}
1018
1019HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
1020 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
1021 unsupported(__func__, outCapabilities);
1022
1023 if (outNumCapabilities == NULL) {
1024 return HWC2::Error::BadParameter;
1025 }
1026
1027 *outNumCapabilities = 0;
1028
1029 return HWC2::Error::None;
1030}
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001031
1032HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayBrightnessSupport(
1033 bool *supported) {
1034 *supported = false;
1035 return HWC2::Error::None;
1036}
1037
1038HWC2::Error DrmHwcTwo::HwcDisplay::SetDisplayBrightness(
1039 float /* brightness */) {
1040 return HWC2::Error::Unsupported;
1041}
1042
John Stultz8c7229d2020-02-07 21:31:08 +00001043#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001044
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001045#if PLATFORM_SDK_VERSION > 27
1046
1047HWC2::Error DrmHwcTwo::HwcDisplay::GetRenderIntents(
1048 int32_t mode, uint32_t *outNumIntents,
1049 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1050 if (mode != HAL_COLOR_MODE_NATIVE) {
1051 return HWC2::Error::BadParameter;
1052 }
1053
1054 if (outIntents == nullptr) {
1055 *outNumIntents = 1;
1056 return HWC2::Error::None;
1057 }
1058 *outNumIntents = 1;
1059 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1060 return HWC2::Error::None;
1061}
1062
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001063HWC2::Error DrmHwcTwo::HwcDisplay::SetColorModeWithIntent(int32_t mode,
1064 int32_t intent) {
1065 if (mode != HAL_COLOR_MODE_NATIVE)
1066 return HWC2::Error::BadParameter;
1067 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1068 return HWC2::Error::BadParameter;
1069 color_mode_ = mode;
1070 return HWC2::Error::None;
1071}
1072
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001073#endif /* PLATFORM_SDK_VERSION > 27 */
1074
Sean Pauled2ec4b2016-03-10 15:35:40 -05001075HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -05001076 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -08001077 cursor_x_ = x;
1078 cursor_y_ = y;
1079 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001080}
1081
1082HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -05001083 supported(__func__);
1084 blending_ = static_cast<HWC2::BlendMode>(mode);
1085 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001086}
1087
1088HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001089 int32_t acquire_fence) {
1090 supported(__func__);
1091 UniqueFd uf(acquire_fence);
1092
Sean Paulac874152016-03-10 16:00:26 -05001093 set_buffer(buffer);
1094 set_acquire_fence(uf.get());
1095 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001096}
1097
1098HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001099 // TODO: Put to client composition here?
1100 supported(__func__);
1101 layer_color_ = color;
1102 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001103}
1104
1105HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001106 sf_type_ = static_cast<HWC2::Composition>(type);
1107 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001108}
1109
1110HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001111 supported(__func__);
1112 dataspace_ = static_cast<android_dataspace_t>(dataspace);
1113 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001114}
1115
1116HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001117 supported(__func__);
1118 display_frame_ = frame;
1119 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001120}
1121
1122HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001123 supported(__func__);
1124 alpha_ = alpha;
1125 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001126}
1127
1128HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1129 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001130 supported(__func__);
1131 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001132 return unsupported(__func__, stream);
1133}
1134
1135HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001136 supported(__func__);
1137 source_crop_ = crop;
1138 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001139}
1140
1141HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001142 supported(__func__);
1143 // TODO: We don't use surface damage, marking as unsupported
1144 unsupported(__func__, damage);
1145 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001146}
1147
1148HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001149 supported(__func__);
1150 transform_ = static_cast<HWC2::Transform>(transform);
1151 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001152}
1153
1154HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001155 supported(__func__);
1156 // TODO: We don't use this information, marking as unsupported
1157 unsupported(__func__, visible);
1158 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001159}
1160
Sean Paulac874152016-03-10 16:00:26 -05001161HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1162 supported(__func__);
1163 z_order_ = order;
1164 return HWC2::Error::None;
1165}
1166
1167void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1168 supported(__func__);
1169 switch (blending_) {
1170 case HWC2::BlendMode::None:
1171 layer->blending = DrmHwcBlending::kNone;
1172 break;
1173 case HWC2::BlendMode::Premultiplied:
1174 layer->blending = DrmHwcBlending::kPreMult;
1175 break;
1176 case HWC2::BlendMode::Coverage:
1177 layer->blending = DrmHwcBlending::kCoverage;
1178 break;
1179 default:
1180 ALOGE("Unknown blending mode b=%d", blending_);
1181 layer->blending = DrmHwcBlending::kNone;
1182 break;
1183 }
1184
1185 OutputFd release_fence = release_fence_output();
1186
1187 layer->sf_handle = buffer_;
1188 layer->acquire_fence = acquire_fence_.Release();
1189 layer->release_fence = std::move(release_fence);
1190 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +02001191 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -05001192 layer->SetSourceCrop(source_crop_);
1193 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -05001194}
1195
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001196void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
1197 auto cb = callbacks_.find(HWC2::Callback::Hotplug);
1198 if (cb == callbacks_.end())
1199 return;
1200
1201 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(cb->second.func);
1202 hotplug(cb->second.data, displayid,
1203 (state == DRM_MODE_CONNECTED ? HWC2_CONNECTION_CONNECTED
1204 : HWC2_CONNECTION_DISCONNECTED));
1205}
1206
1207void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
1208 for (auto &conn : drmDevice->connectors()) {
1209 if (conn->state() != DRM_MODE_CONNECTED)
1210 continue;
1211 HandleDisplayHotplug(conn->display(), conn->state());
1212 }
1213}
1214
1215void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
1216 for (auto &conn : drm_->connectors()) {
1217 drmModeConnection old_state = conn->state();
1218 drmModeConnection cur_state = conn->UpdateModes()
1219 ? DRM_MODE_UNKNOWNCONNECTION
1220 : conn->state();
1221
1222 if (cur_state == old_state)
1223 continue;
1224
1225 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
1226 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1227 conn->id(), conn->display());
1228
1229 int display_id = conn->display();
1230 if (cur_state == DRM_MODE_CONNECTED) {
1231 auto &display = hwc2_->displays_.at(display_id);
1232 display.ChosePreferredConfig();
1233 } else {
1234 auto &display = hwc2_->displays_.at(display_id);
1235 display.ClearDisplay();
1236 }
1237
1238 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1239 }
1240}
1241
Sean Pauled2ec4b2016-03-10 15:35:40 -05001242// static
1243int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1244 unsupported(__func__);
1245 return 0;
1246}
1247
1248// static
1249void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001250 uint32_t *out_count,
1251 int32_t * /*out_capabilities*/) {
1252 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001253 *out_count = 0;
1254}
1255
1256// static
Sean Paulac874152016-03-10 16:00:26 -05001257hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1258 struct hwc2_device * /*dev*/, int32_t descriptor) {
1259 supported(__func__);
1260 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001261 switch (func) {
1262 // Device functions
1263 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1264 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1265 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1266 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001267 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001268 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1269 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1270 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1271 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1272 case HWC2::FunctionDescriptor::Dump:
1273 return ToHook<HWC2_PFN_DUMP>(
1274 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1275 uint32_t *, char *>);
1276 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1277 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1278 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1279 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1280 case HWC2::FunctionDescriptor::RegisterCallback:
1281 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1282 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1283 &DrmHwcTwo::RegisterCallback, int32_t,
1284 hwc2_callback_data_t, hwc2_function_pointer_t>);
1285
1286 // Display functions
1287 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1288 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1289 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1290 &HwcDisplay::AcceptDisplayChanges>);
1291 case HWC2::FunctionDescriptor::CreateLayer:
1292 return ToHook<HWC2_PFN_CREATE_LAYER>(
1293 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1294 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1295 case HWC2::FunctionDescriptor::DestroyLayer:
1296 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1297 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1298 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1299 case HWC2::FunctionDescriptor::GetActiveConfig:
1300 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1301 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1302 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1303 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1304 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1305 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1306 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1307 hwc2_layer_t *, int32_t *>);
1308 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1309 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1310 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1311 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1312 int32_t, int32_t>);
1313 case HWC2::FunctionDescriptor::GetColorModes:
1314 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1315 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1316 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1317 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001318 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1319 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1320 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1321 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001322 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001323 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1324 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1325 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1326 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001327 case HWC2::FunctionDescriptor::GetDisplayName:
1328 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1329 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1330 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1331 case HWC2::FunctionDescriptor::GetDisplayRequests:
1332 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1333 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1334 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1335 hwc2_layer_t *, int32_t *>);
1336 case HWC2::FunctionDescriptor::GetDisplayType:
1337 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1338 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1339 &HwcDisplay::GetDisplayType, int32_t *>);
1340 case HWC2::FunctionDescriptor::GetDozeSupport:
1341 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1342 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1343 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001344 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1345 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1346 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1347 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1348 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001349 case HWC2::FunctionDescriptor::GetReleaseFences:
1350 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1351 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1352 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1353 int32_t *>);
1354 case HWC2::FunctionDescriptor::PresentDisplay:
1355 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1356 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1357 &HwcDisplay::PresentDisplay, int32_t *>);
1358 case HWC2::FunctionDescriptor::SetActiveConfig:
1359 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1360 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1361 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1362 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001363 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1364 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1365 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1366 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001367 case HWC2::FunctionDescriptor::SetColorMode:
1368 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1369 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1370 &HwcDisplay::SetColorMode, int32_t>);
1371 case HWC2::FunctionDescriptor::SetColorTransform:
1372 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1373 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1374 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1375 case HWC2::FunctionDescriptor::SetOutputBuffer:
1376 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1377 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1378 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1379 case HWC2::FunctionDescriptor::SetPowerMode:
1380 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1381 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1382 &HwcDisplay::SetPowerMode, int32_t>);
1383 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1384 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1385 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1386 &HwcDisplay::SetVsyncEnabled, int32_t>);
1387 case HWC2::FunctionDescriptor::ValidateDisplay:
1388 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1389 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1390 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001391#if PLATFORM_SDK_VERSION > 27
1392 case HWC2::FunctionDescriptor::GetRenderIntents:
1393 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
1394 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
1395 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
1396 int32_t *>);
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001397 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
1398 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
1399 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
1400 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001401#endif
John Stultz8c7229d2020-02-07 21:31:08 +00001402#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001403 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1404 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1405 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1406 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1407 uint32_t *, uint8_t *>);
1408 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1409 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1410 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1411 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1412 uint32_t *>);
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001413 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
1414 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
1415 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
1416 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
1417 case HWC2::FunctionDescriptor::SetDisplayBrightness:
1418 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
1419 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
1420 &HwcDisplay::SetDisplayBrightness, float>);
John Stultz8c7229d2020-02-07 21:31:08 +00001421#endif /* PLATFORM_SDK_VERSION > 28 */
Sean Pauled2ec4b2016-03-10 15:35:40 -05001422 // Layer functions
1423 case HWC2::FunctionDescriptor::SetCursorPosition:
1424 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1425 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1426 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1427 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1428 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1429 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1430 &HwcLayer::SetLayerBlendMode, int32_t>);
1431 case HWC2::FunctionDescriptor::SetLayerBuffer:
1432 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1433 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1434 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1435 case HWC2::FunctionDescriptor::SetLayerColor:
1436 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1437 LayerHook<decltype(&HwcLayer::SetLayerColor),
1438 &HwcLayer::SetLayerColor, hwc_color_t>);
1439 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1440 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1441 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1442 &HwcLayer::SetLayerCompositionType, int32_t>);
1443 case HWC2::FunctionDescriptor::SetLayerDataspace:
1444 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1445 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1446 &HwcLayer::SetLayerDataspace, int32_t>);
1447 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1448 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1449 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1450 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1451 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1452 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1453 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1454 &HwcLayer::SetLayerPlaneAlpha, float>);
1455 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001456 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1457 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1458 &HwcLayer::SetLayerSidebandStream,
1459 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001460 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1461 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1462 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1463 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1464 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1465 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1466 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1467 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1468 case HWC2::FunctionDescriptor::SetLayerTransform:
1469 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1470 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1471 &HwcLayer::SetLayerTransform, int32_t>);
1472 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1473 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1474 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1475 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1476 case HWC2::FunctionDescriptor::SetLayerZOrder:
1477 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1478 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1479 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001480 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001481 default:
1482 return NULL;
1483 }
1484}
Sean Paulac874152016-03-10 16:00:26 -05001485
1486// static
1487int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1488 struct hw_device_t **dev) {
1489 supported(__func__);
1490 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1491 ALOGE("Invalid module name- %s", name);
1492 return -EINVAL;
1493 }
1494
1495 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1496 if (!ctx) {
1497 ALOGE("Failed to allocate DrmHwcTwo");
1498 return -ENOMEM;
1499 }
1500
1501 HWC2::Error err = ctx->Init();
1502 if (err != HWC2::Error::None) {
1503 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1504 return -EINVAL;
1505 }
1506
1507 ctx->common.module = const_cast<hw_module_t *>(module);
1508 *dev = &ctx->common;
1509 ctx.release();
1510 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001511}
Sean Paulf72cccd2018-08-27 13:59:08 -04001512} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001513
1514static struct hw_module_methods_t hwc2_module_methods = {
1515 .open = android::DrmHwcTwo::HookDevOpen,
1516};
1517
1518hw_module_t HAL_MODULE_INFO_SYM = {
1519 .tag = HARDWARE_MODULE_TAG,
1520 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1521 .id = HWC_HARDWARE_MODULE_ID,
1522 .name = "DrmHwcTwo module",
1523 .author = "The Android Open Source Project",
1524 .methods = &hwc2_module_methods,
1525 .dso = NULL,
1526 .reserved = {0},
1527};