blob: e2c943a340d5ceaf933bfb3cf2a690ba7a0bd416 [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()) ||
Roman Stratiienko65f2ba82019-12-20 17:04:01 +0200906 color_transform_hint_ != HAL_COLOR_TRANSFORM_IDENTITY ||
907 (l.second->RequireScalingOrPhasing() &&
908 resource_manager_->ForcedScalingWithGpu())) {
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200909 if (client_start < 0)
910 client_start = l.first;
911 client_size = (l.first - client_start) + 1;
912 }
913 }
914
915 int extra_client = (z_map.size() - client_size) - avail_planes;
916 if (extra_client > 0) {
917 int start = 0, steps;
918 if (client_size != 0) {
919 int prepend = std::min(client_start, extra_client);
920 int append = std::min(int(z_map.size() - (client_start + client_size)),
921 extra_client);
922 start = client_start - prepend;
923 client_size += extra_client;
924 steps = 1 + std::min(std::min(append, prepend),
925 int(z_map.size()) - (start + client_size));
Roman Stratiienkof2647232019-11-21 01:58:35 +0200926 } else {
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200927 client_size = extra_client;
928 steps = 1 + z_map.size() - extra_client;
Roman Stratiienkof2647232019-11-21 01:58:35 +0200929 }
930
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200931 gpu_pixops = INT_MAX;
932 for (int i = 0; i < steps; i++) {
933 uint32_t po = CalcPixOps(z_map, start + i, client_size);
934 if (po < gpu_pixops) {
935 gpu_pixops = po;
936 client_start = start + i;
937 }
938 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500939 }
940
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200941 MarkValidated(z_map, client_start, client_size);
942
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200943 if (CreateComposition(true) != HWC2::Error::None) {
944 ++total_stats_.failed_kms_validate_;
945 gpu_pixops = total_pixops;
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200946 client_size = z_map.size();
947 MarkValidated(z_map, 0, client_size);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200948 }
949
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200950 *num_types = client_size;
951
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200952 total_stats_.gpu_pixops_ += gpu_pixops;
953 total_stats_.total_pixops_ += total_pixops;
Roman Stratiienkof2647232019-11-21 01:58:35 +0200954
Rob Herringee8f45b2017-06-09 15:15:55 -0500955 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500956}
957
John Stultz8c7229d2020-02-07 21:31:08 +0000958#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800959HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
960 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
961 supported(__func__);
962
963 drmModePropertyBlobPtr blob;
964 int ret;
965 uint64_t blob_id;
966
967 std::tie(ret, blob_id) = connector_->edid_property().value();
968 if (ret) {
969 ALOGE("Failed to get edid property value.");
970 return HWC2::Error::Unsupported;
971 }
972
973 blob = drmModeGetPropertyBlob(drm_->fd(), blob_id);
974
975 outData = static_cast<uint8_t *>(blob->data);
976
977 *outPort = connector_->id();
978 *outDataSize = blob->length;
979
980 return HWC2::Error::None;
981}
982
983HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
984 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
985 unsupported(__func__, outCapabilities);
986
987 if (outNumCapabilities == NULL) {
988 return HWC2::Error::BadParameter;
989 }
990
991 *outNumCapabilities = 0;
992
993 return HWC2::Error::None;
994}
John Stultz8c7229d2020-02-07 21:31:08 +0000995#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800996
Sean Pauled2ec4b2016-03-10 15:35:40 -0500997HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -0500998 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800999 cursor_x_ = x;
1000 cursor_y_ = y;
1001 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001002}
1003
1004HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -05001005 supported(__func__);
1006 blending_ = static_cast<HWC2::BlendMode>(mode);
1007 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001008}
1009
1010HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001011 int32_t acquire_fence) {
1012 supported(__func__);
1013 UniqueFd uf(acquire_fence);
1014
1015 // The buffer and acquire_fence are handled elsewhere
1016 if (sf_type_ == HWC2::Composition::Client ||
1017 sf_type_ == HWC2::Composition::Sideband ||
1018 sf_type_ == HWC2::Composition::SolidColor)
1019 return HWC2::Error::None;
1020
1021 set_buffer(buffer);
1022 set_acquire_fence(uf.get());
1023 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001024}
1025
1026HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001027 // TODO: Put to client composition here?
1028 supported(__func__);
1029 layer_color_ = color;
1030 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001031}
1032
1033HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001034 sf_type_ = static_cast<HWC2::Composition>(type);
1035 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001036}
1037
1038HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001039 supported(__func__);
1040 dataspace_ = static_cast<android_dataspace_t>(dataspace);
1041 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001042}
1043
1044HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001045 supported(__func__);
1046 display_frame_ = frame;
1047 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001048}
1049
1050HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001051 supported(__func__);
1052 alpha_ = alpha;
1053 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001054}
1055
1056HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1057 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001058 supported(__func__);
1059 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001060 return unsupported(__func__, stream);
1061}
1062
1063HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001064 supported(__func__);
1065 source_crop_ = crop;
1066 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001067}
1068
1069HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001070 supported(__func__);
1071 // TODO: We don't use surface damage, marking as unsupported
1072 unsupported(__func__, damage);
1073 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001074}
1075
1076HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001077 supported(__func__);
1078 transform_ = static_cast<HWC2::Transform>(transform);
1079 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001080}
1081
1082HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001083 supported(__func__);
1084 // TODO: We don't use this information, marking as unsupported
1085 unsupported(__func__, visible);
1086 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001087}
1088
Sean Paulac874152016-03-10 16:00:26 -05001089HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1090 supported(__func__);
1091 z_order_ = order;
1092 return HWC2::Error::None;
1093}
1094
1095void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1096 supported(__func__);
1097 switch (blending_) {
1098 case HWC2::BlendMode::None:
1099 layer->blending = DrmHwcBlending::kNone;
1100 break;
1101 case HWC2::BlendMode::Premultiplied:
1102 layer->blending = DrmHwcBlending::kPreMult;
1103 break;
1104 case HWC2::BlendMode::Coverage:
1105 layer->blending = DrmHwcBlending::kCoverage;
1106 break;
1107 default:
1108 ALOGE("Unknown blending mode b=%d", blending_);
1109 layer->blending = DrmHwcBlending::kNone;
1110 break;
1111 }
1112
1113 OutputFd release_fence = release_fence_output();
1114
1115 layer->sf_handle = buffer_;
1116 layer->acquire_fence = acquire_fence_.Release();
1117 layer->release_fence = std::move(release_fence);
1118 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +02001119 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -05001120 layer->SetSourceCrop(source_crop_);
1121 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -05001122}
1123
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001124void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
1125 auto cb = callbacks_.find(HWC2::Callback::Hotplug);
1126 if (cb == callbacks_.end())
1127 return;
1128
1129 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(cb->second.func);
1130 hotplug(cb->second.data, displayid,
1131 (state == DRM_MODE_CONNECTED ? HWC2_CONNECTION_CONNECTED
1132 : HWC2_CONNECTION_DISCONNECTED));
1133}
1134
1135void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
1136 for (auto &conn : drmDevice->connectors()) {
1137 if (conn->state() != DRM_MODE_CONNECTED)
1138 continue;
1139 HandleDisplayHotplug(conn->display(), conn->state());
1140 }
1141}
1142
1143void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
1144 for (auto &conn : drm_->connectors()) {
1145 drmModeConnection old_state = conn->state();
1146 drmModeConnection cur_state = conn->UpdateModes()
1147 ? DRM_MODE_UNKNOWNCONNECTION
1148 : conn->state();
1149
1150 if (cur_state == old_state)
1151 continue;
1152
1153 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
1154 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1155 conn->id(), conn->display());
1156
1157 int display_id = conn->display();
1158 if (cur_state == DRM_MODE_CONNECTED) {
1159 auto &display = hwc2_->displays_.at(display_id);
1160 display.ChosePreferredConfig();
1161 } else {
1162 auto &display = hwc2_->displays_.at(display_id);
1163 display.ClearDisplay();
1164 }
1165
1166 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1167 }
1168}
1169
Sean Pauled2ec4b2016-03-10 15:35:40 -05001170// static
1171int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1172 unsupported(__func__);
1173 return 0;
1174}
1175
1176// static
1177void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001178 uint32_t *out_count,
1179 int32_t * /*out_capabilities*/) {
1180 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001181 *out_count = 0;
1182}
1183
1184// static
Sean Paulac874152016-03-10 16:00:26 -05001185hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1186 struct hwc2_device * /*dev*/, int32_t descriptor) {
1187 supported(__func__);
1188 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001189 switch (func) {
1190 // Device functions
1191 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1192 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1193 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1194 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001195 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001196 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1197 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1198 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1199 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1200 case HWC2::FunctionDescriptor::Dump:
1201 return ToHook<HWC2_PFN_DUMP>(
1202 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1203 uint32_t *, char *>);
1204 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1205 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1206 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1207 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1208 case HWC2::FunctionDescriptor::RegisterCallback:
1209 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1210 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1211 &DrmHwcTwo::RegisterCallback, int32_t,
1212 hwc2_callback_data_t, hwc2_function_pointer_t>);
1213
1214 // Display functions
1215 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1216 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1217 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1218 &HwcDisplay::AcceptDisplayChanges>);
1219 case HWC2::FunctionDescriptor::CreateLayer:
1220 return ToHook<HWC2_PFN_CREATE_LAYER>(
1221 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1222 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1223 case HWC2::FunctionDescriptor::DestroyLayer:
1224 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1225 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1226 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1227 case HWC2::FunctionDescriptor::GetActiveConfig:
1228 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1229 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1230 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1231 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1232 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1233 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1234 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1235 hwc2_layer_t *, int32_t *>);
1236 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1237 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1238 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1239 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1240 int32_t, int32_t>);
1241 case HWC2::FunctionDescriptor::GetColorModes:
1242 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1243 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1244 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1245 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001246 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1247 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1248 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1249 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001250 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001251 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1252 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1253 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1254 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001255 case HWC2::FunctionDescriptor::GetDisplayName:
1256 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1257 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1258 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1259 case HWC2::FunctionDescriptor::GetDisplayRequests:
1260 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1261 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1262 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1263 hwc2_layer_t *, int32_t *>);
1264 case HWC2::FunctionDescriptor::GetDisplayType:
1265 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1266 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1267 &HwcDisplay::GetDisplayType, int32_t *>);
1268 case HWC2::FunctionDescriptor::GetDozeSupport:
1269 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1270 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1271 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001272 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1273 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1274 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1275 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1276 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001277 case HWC2::FunctionDescriptor::GetReleaseFences:
1278 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1279 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1280 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1281 int32_t *>);
1282 case HWC2::FunctionDescriptor::PresentDisplay:
1283 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1284 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1285 &HwcDisplay::PresentDisplay, int32_t *>);
1286 case HWC2::FunctionDescriptor::SetActiveConfig:
1287 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1288 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1289 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1290 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001291 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1292 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1293 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1294 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001295 case HWC2::FunctionDescriptor::SetColorMode:
1296 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1297 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1298 &HwcDisplay::SetColorMode, int32_t>);
1299 case HWC2::FunctionDescriptor::SetColorTransform:
1300 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1301 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1302 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1303 case HWC2::FunctionDescriptor::SetOutputBuffer:
1304 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1305 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1306 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1307 case HWC2::FunctionDescriptor::SetPowerMode:
1308 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1309 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1310 &HwcDisplay::SetPowerMode, int32_t>);
1311 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1312 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1313 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1314 &HwcDisplay::SetVsyncEnabled, int32_t>);
1315 case HWC2::FunctionDescriptor::ValidateDisplay:
1316 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1317 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1318 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
John Stultz8c7229d2020-02-07 21:31:08 +00001319#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001320 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1321 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1322 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1323 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1324 uint32_t *, uint8_t *>);
1325 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1326 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1327 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1328 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1329 uint32_t *>);
John Stultz8c7229d2020-02-07 21:31:08 +00001330#endif /* PLATFORM_SDK_VERSION > 28 */
Sean Pauled2ec4b2016-03-10 15:35:40 -05001331 // Layer functions
1332 case HWC2::FunctionDescriptor::SetCursorPosition:
1333 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1334 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1335 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1336 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1337 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1338 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1339 &HwcLayer::SetLayerBlendMode, int32_t>);
1340 case HWC2::FunctionDescriptor::SetLayerBuffer:
1341 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1342 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1343 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1344 case HWC2::FunctionDescriptor::SetLayerColor:
1345 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1346 LayerHook<decltype(&HwcLayer::SetLayerColor),
1347 &HwcLayer::SetLayerColor, hwc_color_t>);
1348 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1349 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1350 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1351 &HwcLayer::SetLayerCompositionType, int32_t>);
1352 case HWC2::FunctionDescriptor::SetLayerDataspace:
1353 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1354 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1355 &HwcLayer::SetLayerDataspace, int32_t>);
1356 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1357 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1358 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1359 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1360 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1361 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1362 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1363 &HwcLayer::SetLayerPlaneAlpha, float>);
1364 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001365 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1366 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1367 &HwcLayer::SetLayerSidebandStream,
1368 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001369 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1370 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1371 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1372 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1373 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1374 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1375 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1376 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1377 case HWC2::FunctionDescriptor::SetLayerTransform:
1378 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1379 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1380 &HwcLayer::SetLayerTransform, int32_t>);
1381 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1382 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1383 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1384 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1385 case HWC2::FunctionDescriptor::SetLayerZOrder:
1386 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1387 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1388 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001389 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001390 default:
1391 return NULL;
1392 }
1393}
Sean Paulac874152016-03-10 16:00:26 -05001394
1395// static
1396int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1397 struct hw_device_t **dev) {
1398 supported(__func__);
1399 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1400 ALOGE("Invalid module name- %s", name);
1401 return -EINVAL;
1402 }
1403
1404 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1405 if (!ctx) {
1406 ALOGE("Failed to allocate DrmHwcTwo");
1407 return -ENOMEM;
1408 }
1409
1410 HWC2::Error err = ctx->Init();
1411 if (err != HWC2::Error::None) {
1412 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1413 return -EINVAL;
1414 }
1415
1416 ctx->common.module = const_cast<hw_module_t *>(module);
1417 *dev = &ctx->common;
1418 ctx.release();
1419 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001420}
Sean Paulf72cccd2018-08-27 13:59:08 -04001421} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001422
1423static struct hw_module_methods_t hwc2_module_methods = {
1424 .open = android::DrmHwcTwo::HookDevOpen,
1425};
1426
1427hw_module_t HAL_MODULE_INFO_SYM = {
1428 .tag = HARDWARE_MODULE_TAG,
1429 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1430 .id = HWC_HARDWARE_MODULE_ID,
1431 .name = "DrmHwcTwo module",
1432 .author = "The Android Open Source Project",
1433 .methods = &hwc2_module_methods,
1434 .dso = NULL,
1435 .reserved = {0},
1436};