blob: 5e07f2f97922395a81c96e5bd0cf0d0b87d2b028 [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 : "")
145 << " Pixel operations (free units)"
146 << " : [TOTAL: " << delta.total_pixops_
147 << " / GPU: " << delta.gpu_pixops_ << "]\n"
148 << " Composition efficiency: " << Ratio)
149 .str();
150}
151
152std::string DrmHwcTwo::HwcDisplay::Dump() {
153 auto out = (std::stringstream()
154 << "- Display on: " << connector_->name() << "\n"
155 << "Statistics since system boot:\n"
156 << DumpDelta(total_stats_) << "\n\n"
157 << "Statistics since last dumpsys request:\n"
158 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n")
159 .str();
160
161 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
162 return out;
163}
164
165void DrmHwcTwo::Dump(uint32_t *outSize, char *outBuffer) {
166 supported(__func__);
167
168 if (outBuffer != nullptr) {
169 auto copiedBytes = mDumpString.copy(outBuffer, *outSize);
170 *outSize = static_cast<uint32_t>(copiedBytes);
171 return;
172 }
173
174 std::stringstream output;
175
176 output << "-- drm_hwcomposer --\n\n";
177
178 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &dp : displays_)
179 output << dp.second.Dump();
180
181 mDumpString = output.str();
182 *outSize = static_cast<uint32_t>(mDumpString.size());
Sean Pauled2ec4b2016-03-10 15:35:40 -0500183}
184
185uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() {
Sean Paulac874152016-03-10 16:00:26 -0500186 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500187 unsupported(__func__);
188 return 0;
189}
190
191HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor,
Sean Paulac874152016-03-10 16:00:26 -0500192 hwc2_callback_data_t data,
193 hwc2_function_pointer_t function) {
194 supported(__func__);
195 auto callback = static_cast<HWC2::Callback>(descriptor);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300196
197 if (!function) {
198 callbacks_.erase(callback);
199 return HWC2::Error::None;
200 }
201
Sean Paulac874152016-03-10 16:00:26 -0500202 callbacks_.emplace(callback, HwcCallback(data, function));
203
204 switch (callback) {
205 case HWC2::Callback::Hotplug: {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300206 auto &drmDevices = resource_manager_.getDrmDevices();
207 for (auto &device : drmDevices)
208 HandleInitialHotplugState(device.get());
Sean Paulac874152016-03-10 16:00:26 -0500209 break;
210 }
211 case HWC2::Callback::Vsync: {
212 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
213 displays_)
214 d.second.RegisterVsyncCallback(data, function);
215 break;
216 }
217 default:
218 break;
219 }
220 return HWC2::Error::None;
221}
222
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100223DrmHwcTwo::HwcDisplay::HwcDisplay(ResourceManager *resource_manager,
224 DrmDevice *drm,
Sean Paulac874152016-03-10 16:00:26 -0500225 std::shared_ptr<Importer> importer,
Sean Paulac874152016-03-10 16:00:26 -0500226 hwc2_display_t handle, HWC2::DisplayType type)
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100227 : resource_manager_(resource_manager),
228 drm_(drm),
229 importer_(importer),
230 handle_(handle),
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200231 type_(type),
232 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
Sean Paulac874152016-03-10 16:00:26 -0500233 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200234
235 // clang-format off
236 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
237 0.0, 1.0, 0.0, 0.0,
238 0.0, 0.0, 1.0, 0.0,
239 0.0, 0.0, 0.0, 1.0};
240 // clang-format on
Sean Paulac874152016-03-10 16:00:26 -0500241}
242
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300243void DrmHwcTwo::HwcDisplay::ClearDisplay() {
244 compositor_.ClearDisplay();
245}
246
Sean Paulac874152016-03-10 16:00:26 -0500247HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
248 supported(__func__);
249 planner_ = Planner::CreateInstance(drm_);
250 if (!planner_) {
251 ALOGE("Failed to create planner instance for composition");
252 return HWC2::Error::NoResources;
253 }
254
255 int display = static_cast<int>(handle_);
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100256 int ret = compositor_.Init(resource_manager_, display);
Sean Paulac874152016-03-10 16:00:26 -0500257 if (ret) {
258 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
259 return HWC2::Error::NoResources;
260 }
261
262 // Split up the given display planes into primary and overlay to properly
263 // interface with the composition
264 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
265 property_get("hwc.drm.use_overlay_planes", use_overlay_planes_prop, "1");
266 bool use_overlay_planes = atoi(use_overlay_planes_prop);
267 for (auto &plane : *planes) {
268 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
269 primary_planes_.push_back(plane);
270 else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY)
271 overlay_planes_.push_back(plane);
272 }
273
274 crtc_ = drm_->GetCrtcForDisplay(display);
275 if (!crtc_) {
276 ALOGE("Failed to get crtc for display %d", display);
277 return HWC2::Error::BadDisplay;
278 }
279
280 connector_ = drm_->GetConnectorForDisplay(display);
281 if (!connector_) {
282 ALOGE("Failed to get connector for display %d", display);
283 return HWC2::Error::BadDisplay;
284 }
285
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300286 ret = vsync_worker_.Init(drm_, display);
287 if (ret) {
288 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
289 return HWC2::Error::BadDisplay;
290 }
291
292 return ChosePreferredConfig();
293}
294
295HWC2::Error DrmHwcTwo::HwcDisplay::ChosePreferredConfig() {
Sean Paulac874152016-03-10 16:00:26 -0500296 // Fetch the number of modes from the display
297 uint32_t num_configs;
298 HWC2::Error err = GetDisplayConfigs(&num_configs, NULL);
299 if (err != HWC2::Error::None || !num_configs)
300 return err;
301
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200302 return SetActiveConfig(connector_->get_preferred_mode_id());
Sean Paulac874152016-03-10 16:00:26 -0500303}
304
305HWC2::Error DrmHwcTwo::HwcDisplay::RegisterVsyncCallback(
306 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
307 supported(__func__);
308 auto callback = std::make_shared<DrmVsyncCallback>(data, func);
Adrian Salidofa37f672017-02-16 10:29:46 -0800309 vsync_worker_.RegisterCallback(std::move(callback));
Sean Paulac874152016-03-10 16:00:26 -0500310 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500311}
312
313HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500314 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500315 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
316 l.second.accept_type_change();
317 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500318}
319
320HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500321 supported(__func__);
322 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
323 *layer = static_cast<hwc2_layer_t>(layer_idx_);
324 ++layer_idx_;
325 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500326}
327
328HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Sean Paulac874152016-03-10 16:00:26 -0500329 supported(__func__);
Vincent Donnefort9abec032019-10-09 15:43:43 +0100330 if (!get_layer(layer))
331 return HWC2::Error::BadLayer;
332
Sean Paulac874152016-03-10 16:00:26 -0500333 layers_.erase(layer);
334 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500335}
336
337HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
Sean Paulac874152016-03-10 16:00:26 -0500338 supported(__func__);
339 DrmMode const &mode = connector_->active_mode();
340 if (mode.id() == 0)
341 return HWC2::Error::BadConfig;
342
343 *config = mode.id();
344 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500345}
346
347HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
348 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500349 supported(__func__);
350 uint32_t num_changes = 0;
351 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
352 if (l.second.type_changed()) {
353 if (layers && num_changes < *num_elements)
354 layers[num_changes] = l.first;
355 if (types && num_changes < *num_elements)
356 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
357 ++num_changes;
358 }
359 }
360 if (!layers && !types)
361 *num_elements = num_changes;
362 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500363}
364
365HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500366 uint32_t height,
367 int32_t /*format*/,
368 int32_t dataspace) {
369 supported(__func__);
370 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
371 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
372
373 if (width < min.first || height < min.second)
374 return HWC2::Error::Unsupported;
375
376 if (width > max.first || height > max.second)
377 return HWC2::Error::Unsupported;
378
379 if (dataspace != HAL_DATASPACE_UNKNOWN &&
380 dataspace != HAL_DATASPACE_STANDARD_UNSPECIFIED)
381 return HWC2::Error::Unsupported;
382
383 // TODO: Validate format can be handled by either GL or planes
384 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500385}
386
387HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500388 int32_t *modes) {
389 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800390 if (!modes)
391 *num_modes = 1;
392
393 if (modes)
394 *modes = HAL_COLOR_MODE_NATIVE;
395
396 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500397}
398
399HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500400 int32_t attribute_in,
401 int32_t *value) {
402 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400403 auto mode = std::find_if(connector_->modes().begin(),
404 connector_->modes().end(),
405 [config](DrmMode const &m) {
406 return m.id() == config;
407 });
Sean Paulac874152016-03-10 16:00:26 -0500408 if (mode == connector_->modes().end()) {
409 ALOGE("Could not find active mode for %d", config);
410 return HWC2::Error::BadConfig;
411 }
412
413 static const int32_t kUmPerInch = 25400;
414 uint32_t mm_width = connector_->mm_width();
415 uint32_t mm_height = connector_->mm_height();
416 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
417 switch (attribute) {
418 case HWC2::Attribute::Width:
419 *value = mode->h_display();
420 break;
421 case HWC2::Attribute::Height:
422 *value = mode->v_display();
423 break;
424 case HWC2::Attribute::VsyncPeriod:
425 // in nanoseconds
426 *value = 1000 * 1000 * 1000 / mode->v_refresh();
427 break;
428 case HWC2::Attribute::DpiX:
429 // Dots per 1000 inches
430 *value = mm_width ? (mode->h_display() * kUmPerInch) / mm_width : -1;
431 break;
432 case HWC2::Attribute::DpiY:
433 // Dots per 1000 inches
434 *value = mm_height ? (mode->v_display() * kUmPerInch) / mm_height : -1;
435 break;
436 default:
437 *value = -1;
438 return HWC2::Error::BadConfig;
439 }
440 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500441}
442
443HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
444 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500445 supported(__func__);
446 // Since this callback is normally invoked twice (once to get the count, and
447 // once to populate configs), we don't really want to read the edid
448 // redundantly. Instead, only update the modes on the first invocation. While
449 // it's possible this will result in stale modes, it'll all come out in the
450 // wash when we try to set the active config later.
451 if (!configs) {
452 int ret = connector_->UpdateModes();
453 if (ret) {
454 ALOGE("Failed to update display modes %d", ret);
455 return HWC2::Error::BadDisplay;
456 }
457 }
458
Neil Armstrongb67d0492019-06-20 09:00:21 +0000459 // Since the upper layers only look at vactive/hactive/refresh, height and
460 // width, it doesn't differentiate interlaced from progressive and other
461 // similar modes. Depending on the order of modes we return to SF, it could
462 // end up choosing a suboptimal configuration and dropping the preferred
463 // mode. To workaround this, don't offer interlaced modes to SF if there is
464 // at least one non-interlaced alternative and only offer a single WxH@R
465 // mode with at least the prefered mode from in DrmConnector::UpdateModes()
466
467 // TODO: Remove the following block of code until AOSP handles all modes
468 std::vector<DrmMode> sel_modes;
469
470 // Add the preferred mode first to be sure it's not dropped
471 auto mode = std::find_if(connector_->modes().begin(),
472 connector_->modes().end(), [&](DrmMode const &m) {
473 return m.id() ==
474 connector_->get_preferred_mode_id();
475 });
476 if (mode != connector_->modes().end())
477 sel_modes.push_back(*mode);
478
479 // Add the active mode if different from preferred mode
480 if (connector_->active_mode().id() != connector_->get_preferred_mode_id())
481 sel_modes.push_back(connector_->active_mode());
482
483 // Cycle over the modes and filter out "similar" modes, keeping only the
484 // first ones in the order given by DRM (from CEA ids and timings order)
Sean Paulac874152016-03-10 16:00:26 -0500485 for (const DrmMode &mode : connector_->modes()) {
Neil Armstrongb67d0492019-06-20 09:00:21 +0000486 // TODO: Remove this when 3D Attributes are in AOSP
487 if (mode.flags() & DRM_MODE_FLAG_3D_MASK)
488 continue;
489
Neil Armstrong4c027a72019-06-04 14:48:02 +0000490 // TODO: Remove this when the Interlaced attribute is in AOSP
491 if (mode.flags() & DRM_MODE_FLAG_INTERLACE) {
492 auto m = std::find_if(connector_->modes().begin(),
493 connector_->modes().end(),
494 [&mode](DrmMode const &m) {
495 return !(m.flags() & DRM_MODE_FLAG_INTERLACE) &&
496 m.h_display() == mode.h_display() &&
497 m.v_display() == mode.v_display();
498 });
Neil Armstrongb67d0492019-06-20 09:00:21 +0000499 if (m == connector_->modes().end())
500 sel_modes.push_back(mode);
501
502 continue;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000503 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000504
505 // Search for a similar WxH@R mode in the filtered list and drop it if
506 // another mode with the same WxH@R has already been selected
507 // TODO: Remove this when AOSP handles duplicates modes
508 auto m = std::find_if(sel_modes.begin(), sel_modes.end(),
509 [&mode](DrmMode const &m) {
510 return m.h_display() == mode.h_display() &&
511 m.v_display() == mode.v_display() &&
512 m.v_refresh() == mode.v_refresh();
513 });
514 if (m == sel_modes.end())
515 sel_modes.push_back(mode);
516 }
517
518 auto num_modes = static_cast<uint32_t>(sel_modes.size());
519 if (!configs) {
520 *num_configs = num_modes;
521 return HWC2::Error::None;
522 }
523
524 uint32_t idx = 0;
525 for (const DrmMode &mode : sel_modes) {
526 if (idx >= *num_configs)
527 break;
528 configs[idx++] = mode.id();
Sean Paulac874152016-03-10 16:00:26 -0500529 }
530 *num_configs = idx;
531 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500532}
533
534HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500535 supported(__func__);
536 std::ostringstream stream;
537 stream << "display-" << connector_->id();
538 std::string string = stream.str();
539 size_t length = string.length();
540 if (!name) {
541 *size = length;
542 return HWC2::Error::None;
543 }
544
545 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
546 strncpy(name, string.c_str(), *size);
547 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500548}
549
Sean Paulac874152016-03-10 16:00:26 -0500550HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
551 uint32_t *num_elements,
552 hwc2_layer_t *layers,
553 int32_t *layer_requests) {
554 supported(__func__);
555 // TODO: I think virtual display should request
556 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
557 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
558 *num_elements = 0;
559 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500560}
561
562HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500563 supported(__func__);
564 *type = static_cast<int32_t>(type_);
565 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500566}
567
568HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500569 supported(__func__);
570 *support = 0;
571 return HWC2::Error::None;
572}
573
574HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400575 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
576 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500577 supported(__func__);
578 *num_types = 0;
579 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500580}
581
582HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500583 hwc2_layer_t *layers,
584 int32_t *fences) {
585 supported(__func__);
586 uint32_t num_layers = 0;
587
588 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
589 ++num_layers;
590 if (layers == NULL || fences == NULL) {
591 continue;
592 } else if (num_layers > *num_elements) {
593 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
594 return HWC2::Error::None;
595 }
596
597 layers[num_layers - 1] = l.first;
598 fences[num_layers - 1] = l.second.take_release_fence();
599 }
600 *num_elements = num_layers;
601 return HWC2::Error::None;
602}
603
Matteo Franchinc56eede2019-12-03 17:10:38 +0000604void DrmHwcTwo::HwcDisplay::AddFenceToPresentFence(int fd) {
Sean Paulac874152016-03-10 16:00:26 -0500605 if (fd < 0)
606 return;
607
Matteo Franchinc56eede2019-12-03 17:10:38 +0000608 if (present_fence_.get() >= 0) {
609 int old_fence = present_fence_.get();
610 present_fence_.Set(sync_merge("dc_present", old_fence, fd));
611 close(fd);
Sean Paulac874152016-03-10 16:00:26 -0500612 } else {
Matteo Franchinc56eede2019-12-03 17:10:38 +0000613 present_fence_.Set(fd);
Sean Paulac874152016-03-10 16:00:26 -0500614 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500615}
616
Roman Stratiienkoafb36892019-11-08 17:16:11 +0200617bool DrmHwcTwo::HwcDisplay::HardwareSupportsLayerType(
618 HWC2::Composition comp_type) {
619 return comp_type == HWC2::Composition::Device ||
620 comp_type == HWC2::Composition::Cursor;
621}
622
Rob Herring4f6c62e2018-05-17 14:33:02 -0500623HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500624 std::vector<DrmCompositionDisplayLayersMap> layers_map;
625 layers_map.emplace_back();
626 DrmCompositionDisplayLayersMap &map = layers_map.back();
627
628 map.display = static_cast<int>(handle_);
629 map.geometry_changed = true; // TODO: Fix this
630
631 // order the layers by z-order
632 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100633 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500634 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
635 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200636 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500637 case HWC2::Composition::Device:
638 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
639 break;
640 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100641 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500642 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100643 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500644 break;
645 default:
646 continue;
647 }
648 }
649 if (use_client_layer)
650 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
651
Rob Herring4f6c62e2018-05-17 14:33:02 -0500652 if (z_map.empty())
653 return HWC2::Error::BadLayer;
654
Sean Paulac874152016-03-10 16:00:26 -0500655 // now that they're ordered by z, add them to the composition
656 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
657 DrmHwcLayer layer;
658 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200659 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500660 if (ret) {
661 ALOGE("Failed to import layer, ret=%d", ret);
662 return HWC2::Error::NoResources;
663 }
664 map.layers.emplace_back(std::move(layer));
665 }
Sean Paulac874152016-03-10 16:00:26 -0500666
Sean Paulf72cccd2018-08-27 13:59:08 -0400667 std::unique_ptr<DrmDisplayComposition> composition = compositor_
668 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500669 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
670
671 // TODO: Don't always assume geometry changed
672 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
673 if (ret) {
674 ALOGE("Failed to set layers in the composition ret=%d", ret);
675 return HWC2::Error::BadLayer;
676 }
677
678 std::vector<DrmPlane *> primary_planes(primary_planes_);
679 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500680 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500681 if (ret) {
682 ALOGE("Failed to plan the composition ret=%d", ret);
683 return HWC2::Error::BadConfig;
684 }
685
686 // Disable the planes we're not using
687 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
688 composition->AddPlaneDisable(*i);
689 i = primary_planes.erase(i);
690 }
691 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
692 composition->AddPlaneDisable(*i);
693 i = overlay_planes.erase(i);
694 }
695
Rob Herring4f6c62e2018-05-17 14:33:02 -0500696 if (test) {
697 ret = compositor_.TestComposition(composition.get());
698 } else {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500699 ret = compositor_.ApplyComposition(std::move(composition));
Matteo Franchinc56eede2019-12-03 17:10:38 +0000700 AddFenceToPresentFence(compositor_.TakeOutFence());
Rob Herring4f6c62e2018-05-17 14:33:02 -0500701 }
Sean Paulac874152016-03-10 16:00:26 -0500702 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700703 if (!test)
704 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500705 return HWC2::Error::BadParameter;
706 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500707 return HWC2::Error::None;
708}
709
Matteo Franchinc56eede2019-12-03 17:10:38 +0000710HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500711 supported(__func__);
712 HWC2::Error ret;
713
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200714 ++total_stats_.total_frames_;
715
Rob Herring4f6c62e2018-05-17 14:33:02 -0500716 ret = CreateComposition(false);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200717 if (ret != HWC2::Error::None)
718 ++total_stats_.failed_kms_present_;
719
Rob Herring4f6c62e2018-05-17 14:33:02 -0500720 if (ret == HWC2::Error::BadLayer) {
721 // Can we really have no client or device layers?
Matteo Franchinc56eede2019-12-03 17:10:38 +0000722 *present_fence = -1;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500723 return HWC2::Error::None;
724 }
725 if (ret != HWC2::Error::None)
726 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500727
Matteo Franchinc56eede2019-12-03 17:10:38 +0000728 *present_fence = present_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500729
730 ++frame_no_;
731 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500732}
733
734HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500735 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400736 auto mode = std::find_if(connector_->modes().begin(),
737 connector_->modes().end(),
738 [config](DrmMode const &m) {
739 return m.id() == config;
740 });
Sean Paulac874152016-03-10 16:00:26 -0500741 if (mode == connector_->modes().end()) {
742 ALOGE("Could not find active mode for %d", config);
743 return HWC2::Error::BadConfig;
744 }
745
Sean Paulf72cccd2018-08-27 13:59:08 -0400746 std::unique_ptr<DrmDisplayComposition> composition = compositor_
747 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500748 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
749 int ret = composition->SetDisplayMode(*mode);
Sean Pauled45a8e2017-02-28 13:17:34 -0500750 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500751 if (ret) {
752 ALOGE("Failed to queue dpms composition on %d", ret);
753 return HWC2::Error::BadConfig;
754 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300755
756 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500757
758 // Setup the client layer's dimensions
759 hwc_rect_t display_frame = {.left = 0,
760 .top = 0,
761 .right = static_cast<int>(mode->h_display()),
762 .bottom = static_cast<int>(mode->v_display())};
763 client_layer_.SetLayerDisplayFrame(display_frame);
764 hwc_frect_t source_crop = {.left = 0.0f,
765 .top = 0.0f,
766 .right = mode->h_display() + 0.0f,
767 .bottom = mode->v_display() + 0.0f};
768 client_layer_.SetLayerSourceCrop(source_crop);
769
770 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500771}
772
773HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
774 int32_t acquire_fence,
775 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600776 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500777 supported(__func__);
778 UniqueFd uf(acquire_fence);
779
780 client_layer_.set_buffer(target);
781 client_layer_.set_acquire_fence(uf.get());
782 client_layer_.SetLayerDataspace(dataspace);
783 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500784}
785
786HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500787 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800788
789 if (mode != HAL_COLOR_MODE_NATIVE)
Vincent Donnefort7834a892019-10-09 15:53:56 +0100790 return HWC2::Error::BadParameter;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800791
792 color_mode_ = mode;
793 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500794}
795
796HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500797 int32_t hint) {
798 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200799 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
800 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
801 return HWC2::Error::BadParameter;
802
803 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
804 return HWC2::Error::BadParameter;
805
806 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
807 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
808 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
809
810 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500811}
812
813HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500814 int32_t release_fence) {
815 supported(__func__);
816 // TODO: Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500817 return unsupported(__func__, buffer, release_fence);
818}
819
Sean Paulac874152016-03-10 16:00:26 -0500820HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
821 supported(__func__);
822 uint64_t dpms_value = 0;
823 auto mode = static_cast<HWC2::PowerMode>(mode_in);
824 switch (mode) {
825 case HWC2::PowerMode::Off:
826 dpms_value = DRM_MODE_DPMS_OFF;
827 break;
828 case HWC2::PowerMode::On:
829 dpms_value = DRM_MODE_DPMS_ON;
830 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100831 case HWC2::PowerMode::Doze:
832 case HWC2::PowerMode::DozeSuspend:
833 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500834 default:
835 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100836 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500837 };
838
Sean Paulf72cccd2018-08-27 13:59:08 -0400839 std::unique_ptr<DrmDisplayComposition> composition = compositor_
840 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500841 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
842 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500843 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500844 if (ret) {
845 ALOGE("Failed to apply the dpms composition ret=%d", ret);
846 return HWC2::Error::BadParameter;
847 }
848 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500849}
850
851HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500852 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300853 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500854 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500855}
856
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200857uint32_t DrmHwcTwo::HwcDisplay::CalcPixOps(
858 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t first_z,
859 size_t size) {
860 uint32_t pixops = 0;
861 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
862 if (l.first >= first_z && l.first < first_z + size) {
863 hwc_rect_t df = l.second->display_frame();
864 pixops += (df.right - df.left) * (df.bottom - df.top);
865 }
866 }
867 return pixops;
868}
869
870void DrmHwcTwo::HwcDisplay::MarkValidated(
871 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t client_first_z,
872 size_t client_size) {
873 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
874 if (l.first >= client_first_z && l.first < client_first_z + client_size)
875 l.second->set_validated_type(HWC2::Composition::Client);
876 else
877 l.second->set_validated_type(HWC2::Composition::Device);
878 }
879}
880
Sean Pauled2ec4b2016-03-10 15:35:40 -0500881HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500882 uint32_t *num_requests) {
883 supported(__func__);
884 *num_types = 0;
885 *num_requests = 0;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500886 size_t avail_planes = primary_planes_.size() + overlay_planes_.size();
Rob Herring4f6c62e2018-05-17 14:33:02 -0500887
888 /*
889 * If more layers then planes, save one plane
890 * for client composited layers
891 */
892 if (avail_planes < layers_.size())
893 avail_planes--;
894
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200895 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
Roman Stratiienkof2647232019-11-21 01:58:35 +0200896 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
897 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
898
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200899 uint32_t total_pixops = CalcPixOps(z_map, 0, z_map.size()), gpu_pixops = 0;
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200900
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200901 int client_start = -1, client_size = 0;
902
Rob Herring4f6c62e2018-05-17 14:33:02 -0500903 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200904 if (!HardwareSupportsLayerType(l.second->sf_type()) ||
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200905 !importer_->CanImportBuffer(l.second->buffer()) ||
906 color_transform_hint_ != HAL_COLOR_TRANSFORM_IDENTITY) {
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200907 if (client_start < 0)
908 client_start = l.first;
909 client_size = (l.first - client_start) + 1;
910 }
911 }
912
913 int extra_client = (z_map.size() - client_size) - avail_planes;
914 if (extra_client > 0) {
915 int start = 0, steps;
916 if (client_size != 0) {
917 int prepend = std::min(client_start, extra_client);
918 int append = std::min(int(z_map.size() - (client_start + client_size)),
919 extra_client);
920 start = client_start - prepend;
921 client_size += extra_client;
922 steps = 1 + std::min(std::min(append, prepend),
923 int(z_map.size()) - (start + client_size));
Roman Stratiienkof2647232019-11-21 01:58:35 +0200924 } else {
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200925 client_size = extra_client;
926 steps = 1 + z_map.size() - extra_client;
Roman Stratiienkof2647232019-11-21 01:58:35 +0200927 }
928
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200929 gpu_pixops = INT_MAX;
930 for (int i = 0; i < steps; i++) {
931 uint32_t po = CalcPixOps(z_map, start + i, client_size);
932 if (po < gpu_pixops) {
933 gpu_pixops = po;
934 client_start = start + i;
935 }
936 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500937 }
938
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200939 MarkValidated(z_map, client_start, client_size);
940
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200941 if (CreateComposition(true) != HWC2::Error::None) {
942 ++total_stats_.failed_kms_validate_;
943 gpu_pixops = total_pixops;
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200944 client_size = z_map.size();
945 MarkValidated(z_map, 0, client_size);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200946 }
947
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200948 *num_types = client_size;
949
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200950 total_stats_.gpu_pixops_ += gpu_pixops;
951 total_stats_.total_pixops_ += total_pixops;
Roman Stratiienkof2647232019-11-21 01:58:35 +0200952
Rob Herringee8f45b2017-06-09 15:15:55 -0500953 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500954}
955
John Stultz8c7229d2020-02-07 21:31:08 +0000956#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800957HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
958 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
959 supported(__func__);
960
961 drmModePropertyBlobPtr blob;
962 int ret;
963 uint64_t blob_id;
964
965 std::tie(ret, blob_id) = connector_->edid_property().value();
966 if (ret) {
967 ALOGE("Failed to get edid property value.");
968 return HWC2::Error::Unsupported;
969 }
970
971 blob = drmModeGetPropertyBlob(drm_->fd(), blob_id);
972
973 outData = static_cast<uint8_t *>(blob->data);
974
975 *outPort = connector_->id();
976 *outDataSize = blob->length;
977
978 return HWC2::Error::None;
979}
980
981HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
982 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
983 unsupported(__func__, outCapabilities);
984
985 if (outNumCapabilities == NULL) {
986 return HWC2::Error::BadParameter;
987 }
988
989 *outNumCapabilities = 0;
990
991 return HWC2::Error::None;
992}
John Stultz8c7229d2020-02-07 21:31:08 +0000993#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800994
Sean Pauled2ec4b2016-03-10 15:35:40 -0500995HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -0500996 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800997 cursor_x_ = x;
998 cursor_y_ = y;
999 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001000}
1001
1002HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -05001003 supported(__func__);
1004 blending_ = static_cast<HWC2::BlendMode>(mode);
1005 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001006}
1007
1008HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001009 int32_t acquire_fence) {
1010 supported(__func__);
1011 UniqueFd uf(acquire_fence);
1012
1013 // The buffer and acquire_fence are handled elsewhere
1014 if (sf_type_ == HWC2::Composition::Client ||
1015 sf_type_ == HWC2::Composition::Sideband ||
1016 sf_type_ == HWC2::Composition::SolidColor)
1017 return HWC2::Error::None;
1018
1019 set_buffer(buffer);
1020 set_acquire_fence(uf.get());
1021 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001022}
1023
1024HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001025 // TODO: Put to client composition here?
1026 supported(__func__);
1027 layer_color_ = color;
1028 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001029}
1030
1031HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001032 sf_type_ = static_cast<HWC2::Composition>(type);
1033 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001034}
1035
1036HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001037 supported(__func__);
1038 dataspace_ = static_cast<android_dataspace_t>(dataspace);
1039 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001040}
1041
1042HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001043 supported(__func__);
1044 display_frame_ = frame;
1045 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001046}
1047
1048HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001049 supported(__func__);
1050 alpha_ = alpha;
1051 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001052}
1053
1054HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1055 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001056 supported(__func__);
1057 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001058 return unsupported(__func__, stream);
1059}
1060
1061HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001062 supported(__func__);
1063 source_crop_ = crop;
1064 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001065}
1066
1067HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001068 supported(__func__);
1069 // TODO: We don't use surface damage, marking as unsupported
1070 unsupported(__func__, damage);
1071 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001072}
1073
1074HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001075 supported(__func__);
1076 transform_ = static_cast<HWC2::Transform>(transform);
1077 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001078}
1079
1080HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001081 supported(__func__);
1082 // TODO: We don't use this information, marking as unsupported
1083 unsupported(__func__, visible);
1084 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001085}
1086
Sean Paulac874152016-03-10 16:00:26 -05001087HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1088 supported(__func__);
1089 z_order_ = order;
1090 return HWC2::Error::None;
1091}
1092
1093void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1094 supported(__func__);
1095 switch (blending_) {
1096 case HWC2::BlendMode::None:
1097 layer->blending = DrmHwcBlending::kNone;
1098 break;
1099 case HWC2::BlendMode::Premultiplied:
1100 layer->blending = DrmHwcBlending::kPreMult;
1101 break;
1102 case HWC2::BlendMode::Coverage:
1103 layer->blending = DrmHwcBlending::kCoverage;
1104 break;
1105 default:
1106 ALOGE("Unknown blending mode b=%d", blending_);
1107 layer->blending = DrmHwcBlending::kNone;
1108 break;
1109 }
1110
1111 OutputFd release_fence = release_fence_output();
1112
1113 layer->sf_handle = buffer_;
1114 layer->acquire_fence = acquire_fence_.Release();
1115 layer->release_fence = std::move(release_fence);
1116 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +02001117 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -05001118 layer->SetSourceCrop(source_crop_);
1119 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -05001120}
1121
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001122void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
1123 auto cb = callbacks_.find(HWC2::Callback::Hotplug);
1124 if (cb == callbacks_.end())
1125 return;
1126
1127 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(cb->second.func);
1128 hotplug(cb->second.data, displayid,
1129 (state == DRM_MODE_CONNECTED ? HWC2_CONNECTION_CONNECTED
1130 : HWC2_CONNECTION_DISCONNECTED));
1131}
1132
1133void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
1134 for (auto &conn : drmDevice->connectors()) {
1135 if (conn->state() != DRM_MODE_CONNECTED)
1136 continue;
1137 HandleDisplayHotplug(conn->display(), conn->state());
1138 }
1139}
1140
1141void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
1142 for (auto &conn : drm_->connectors()) {
1143 drmModeConnection old_state = conn->state();
1144 drmModeConnection cur_state = conn->UpdateModes()
1145 ? DRM_MODE_UNKNOWNCONNECTION
1146 : conn->state();
1147
1148 if (cur_state == old_state)
1149 continue;
1150
1151 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
1152 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1153 conn->id(), conn->display());
1154
1155 int display_id = conn->display();
1156 if (cur_state == DRM_MODE_CONNECTED) {
1157 auto &display = hwc2_->displays_.at(display_id);
1158 display.ChosePreferredConfig();
1159 } else {
1160 auto &display = hwc2_->displays_.at(display_id);
1161 display.ClearDisplay();
1162 }
1163
1164 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1165 }
1166}
1167
Sean Pauled2ec4b2016-03-10 15:35:40 -05001168// static
1169int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1170 unsupported(__func__);
1171 return 0;
1172}
1173
1174// static
1175void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001176 uint32_t *out_count,
1177 int32_t * /*out_capabilities*/) {
1178 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001179 *out_count = 0;
1180}
1181
1182// static
Sean Paulac874152016-03-10 16:00:26 -05001183hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1184 struct hwc2_device * /*dev*/, int32_t descriptor) {
1185 supported(__func__);
1186 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001187 switch (func) {
1188 // Device functions
1189 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1190 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1191 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1192 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001193 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001194 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1195 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1196 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1197 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1198 case HWC2::FunctionDescriptor::Dump:
1199 return ToHook<HWC2_PFN_DUMP>(
1200 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1201 uint32_t *, char *>);
1202 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1203 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1204 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1205 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1206 case HWC2::FunctionDescriptor::RegisterCallback:
1207 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1208 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1209 &DrmHwcTwo::RegisterCallback, int32_t,
1210 hwc2_callback_data_t, hwc2_function_pointer_t>);
1211
1212 // Display functions
1213 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1214 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1215 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1216 &HwcDisplay::AcceptDisplayChanges>);
1217 case HWC2::FunctionDescriptor::CreateLayer:
1218 return ToHook<HWC2_PFN_CREATE_LAYER>(
1219 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1220 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1221 case HWC2::FunctionDescriptor::DestroyLayer:
1222 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1223 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1224 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1225 case HWC2::FunctionDescriptor::GetActiveConfig:
1226 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1227 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1228 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1229 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1230 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1231 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1232 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1233 hwc2_layer_t *, int32_t *>);
1234 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1235 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1236 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1237 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1238 int32_t, int32_t>);
1239 case HWC2::FunctionDescriptor::GetColorModes:
1240 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1241 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1242 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1243 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001244 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1245 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1246 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1247 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001248 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001249 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1250 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1251 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1252 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001253 case HWC2::FunctionDescriptor::GetDisplayName:
1254 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1255 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1256 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1257 case HWC2::FunctionDescriptor::GetDisplayRequests:
1258 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1259 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1260 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1261 hwc2_layer_t *, int32_t *>);
1262 case HWC2::FunctionDescriptor::GetDisplayType:
1263 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1264 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1265 &HwcDisplay::GetDisplayType, int32_t *>);
1266 case HWC2::FunctionDescriptor::GetDozeSupport:
1267 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1268 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1269 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001270 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1271 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1272 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1273 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1274 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001275 case HWC2::FunctionDescriptor::GetReleaseFences:
1276 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1277 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1278 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1279 int32_t *>);
1280 case HWC2::FunctionDescriptor::PresentDisplay:
1281 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1282 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1283 &HwcDisplay::PresentDisplay, int32_t *>);
1284 case HWC2::FunctionDescriptor::SetActiveConfig:
1285 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1286 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1287 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1288 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001289 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1290 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1291 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1292 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001293 case HWC2::FunctionDescriptor::SetColorMode:
1294 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1295 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1296 &HwcDisplay::SetColorMode, int32_t>);
1297 case HWC2::FunctionDescriptor::SetColorTransform:
1298 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1299 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1300 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1301 case HWC2::FunctionDescriptor::SetOutputBuffer:
1302 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1303 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1304 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1305 case HWC2::FunctionDescriptor::SetPowerMode:
1306 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1307 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1308 &HwcDisplay::SetPowerMode, int32_t>);
1309 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1310 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1311 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1312 &HwcDisplay::SetVsyncEnabled, int32_t>);
1313 case HWC2::FunctionDescriptor::ValidateDisplay:
1314 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1315 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1316 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
John Stultz8c7229d2020-02-07 21:31:08 +00001317#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001318 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1319 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1320 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1321 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1322 uint32_t *, uint8_t *>);
1323 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1324 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1325 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1326 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1327 uint32_t *>);
John Stultz8c7229d2020-02-07 21:31:08 +00001328#endif /* PLATFORM_SDK_VERSION > 28 */
Sean Pauled2ec4b2016-03-10 15:35:40 -05001329 // Layer functions
1330 case HWC2::FunctionDescriptor::SetCursorPosition:
1331 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1332 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1333 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1334 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1335 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1336 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1337 &HwcLayer::SetLayerBlendMode, int32_t>);
1338 case HWC2::FunctionDescriptor::SetLayerBuffer:
1339 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1340 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1341 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1342 case HWC2::FunctionDescriptor::SetLayerColor:
1343 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1344 LayerHook<decltype(&HwcLayer::SetLayerColor),
1345 &HwcLayer::SetLayerColor, hwc_color_t>);
1346 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1347 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1348 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1349 &HwcLayer::SetLayerCompositionType, int32_t>);
1350 case HWC2::FunctionDescriptor::SetLayerDataspace:
1351 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1352 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1353 &HwcLayer::SetLayerDataspace, int32_t>);
1354 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1355 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1356 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1357 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1358 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1359 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1360 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1361 &HwcLayer::SetLayerPlaneAlpha, float>);
1362 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001363 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1364 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1365 &HwcLayer::SetLayerSidebandStream,
1366 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001367 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1368 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1369 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1370 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1371 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1372 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1373 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1374 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1375 case HWC2::FunctionDescriptor::SetLayerTransform:
1376 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1377 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1378 &HwcLayer::SetLayerTransform, int32_t>);
1379 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1380 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1381 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1382 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1383 case HWC2::FunctionDescriptor::SetLayerZOrder:
1384 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1385 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1386 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001387 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001388 default:
1389 return NULL;
1390 }
1391}
Sean Paulac874152016-03-10 16:00:26 -05001392
1393// static
1394int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1395 struct hw_device_t **dev) {
1396 supported(__func__);
1397 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1398 ALOGE("Invalid module name- %s", name);
1399 return -EINVAL;
1400 }
1401
1402 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1403 if (!ctx) {
1404 ALOGE("Failed to allocate DrmHwcTwo");
1405 return -ENOMEM;
1406 }
1407
1408 HWC2::Error err = ctx->Init();
1409 if (err != HWC2::Error::None) {
1410 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1411 return -EINVAL;
1412 }
1413
1414 ctx->common.module = const_cast<hw_module_t *>(module);
1415 *dev = &ctx->common;
1416 ctx.release();
1417 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001418}
Sean Paulf72cccd2018-08-27 13:59:08 -04001419} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001420
1421static struct hw_module_methods_t hwc2_module_methods = {
1422 .open = android::DrmHwcTwo::HookDevOpen,
1423};
1424
1425hw_module_t HAL_MODULE_INFO_SYM = {
1426 .tag = HARDWARE_MODULE_TAG,
1427 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1428 .id = HWC_HARDWARE_MODULE_ID,
1429 .name = "DrmHwcTwo module",
1430 .author = "The Android Open Source Project",
1431 .methods = &hwc2_module_methods,
1432 .dso = NULL,
1433 .reserved = {0},
1434};