blob: a4a8187a449ea0aa7cef1ecb215680500b51e25e [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 }
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200211 case HWC2::Callback::Refresh: {
212 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
213 displays_)
214 d.second.RegisterRefreshCallback(data, function);
215 break;
216 }
Sean Paulac874152016-03-10 16:00:26 -0500217 case HWC2::Callback::Vsync: {
218 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
219 displays_)
220 d.second.RegisterVsyncCallback(data, function);
221 break;
222 }
223 default:
224 break;
225 }
226 return HWC2::Error::None;
227}
228
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100229DrmHwcTwo::HwcDisplay::HwcDisplay(ResourceManager *resource_manager,
230 DrmDevice *drm,
Sean Paulac874152016-03-10 16:00:26 -0500231 std::shared_ptr<Importer> importer,
Sean Paulac874152016-03-10 16:00:26 -0500232 hwc2_display_t handle, HWC2::DisplayType type)
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100233 : resource_manager_(resource_manager),
234 drm_(drm),
235 importer_(importer),
236 handle_(handle),
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200237 type_(type),
238 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
Sean Paulac874152016-03-10 16:00:26 -0500239 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200240
241 // clang-format off
242 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
243 0.0, 1.0, 0.0, 0.0,
244 0.0, 0.0, 1.0, 0.0,
245 0.0, 0.0, 0.0, 1.0};
246 // clang-format on
Sean Paulac874152016-03-10 16:00:26 -0500247}
248
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300249void DrmHwcTwo::HwcDisplay::ClearDisplay() {
250 compositor_.ClearDisplay();
251}
252
Sean Paulac874152016-03-10 16:00:26 -0500253HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
254 supported(__func__);
255 planner_ = Planner::CreateInstance(drm_);
256 if (!planner_) {
257 ALOGE("Failed to create planner instance for composition");
258 return HWC2::Error::NoResources;
259 }
260
261 int display = static_cast<int>(handle_);
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100262 int ret = compositor_.Init(resource_manager_, display);
Sean Paulac874152016-03-10 16:00:26 -0500263 if (ret) {
264 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
265 return HWC2::Error::NoResources;
266 }
267
268 // Split up the given display planes into primary and overlay to properly
269 // interface with the composition
270 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
271 property_get("hwc.drm.use_overlay_planes", use_overlay_planes_prop, "1");
272 bool use_overlay_planes = atoi(use_overlay_planes_prop);
273 for (auto &plane : *planes) {
274 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
275 primary_planes_.push_back(plane);
276 else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY)
277 overlay_planes_.push_back(plane);
278 }
279
280 crtc_ = drm_->GetCrtcForDisplay(display);
281 if (!crtc_) {
282 ALOGE("Failed to get crtc for display %d", display);
283 return HWC2::Error::BadDisplay;
284 }
285
286 connector_ = drm_->GetConnectorForDisplay(display);
287 if (!connector_) {
288 ALOGE("Failed to get connector for display %d", display);
289 return HWC2::Error::BadDisplay;
290 }
291
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300292 ret = vsync_worker_.Init(drm_, display);
293 if (ret) {
294 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
295 return HWC2::Error::BadDisplay;
296 }
297
298 return ChosePreferredConfig();
299}
300
301HWC2::Error DrmHwcTwo::HwcDisplay::ChosePreferredConfig() {
Sean Paulac874152016-03-10 16:00:26 -0500302 // Fetch the number of modes from the display
303 uint32_t num_configs;
304 HWC2::Error err = GetDisplayConfigs(&num_configs, NULL);
305 if (err != HWC2::Error::None || !num_configs)
306 return err;
307
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200308 return SetActiveConfig(connector_->get_preferred_mode_id());
Sean Paulac874152016-03-10 16:00:26 -0500309}
310
311HWC2::Error DrmHwcTwo::HwcDisplay::RegisterVsyncCallback(
312 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
313 supported(__func__);
314 auto callback = std::make_shared<DrmVsyncCallback>(data, func);
Adrian Salidofa37f672017-02-16 10:29:46 -0800315 vsync_worker_.RegisterCallback(std::move(callback));
Sean Paulac874152016-03-10 16:00:26 -0500316 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500317}
318
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200319void DrmHwcTwo::HwcDisplay::RegisterRefreshCallback(
320 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
321 supported(__func__);
322 auto hook = reinterpret_cast<HWC2_PFN_REFRESH>(func);
323 compositor_.SetRefreshCallback([data, hook](int display) {
324 hook(data, static_cast<hwc2_display_t>(display));
325 });
326}
327
Sean Pauled2ec4b2016-03-10 15:35:40 -0500328HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500329 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500330 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
331 l.second.accept_type_change();
332 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500333}
334
335HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500336 supported(__func__);
337 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
338 *layer = static_cast<hwc2_layer_t>(layer_idx_);
339 ++layer_idx_;
340 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500341}
342
343HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Sean Paulac874152016-03-10 16:00:26 -0500344 supported(__func__);
Vincent Donnefort9abec032019-10-09 15:43:43 +0100345 if (!get_layer(layer))
346 return HWC2::Error::BadLayer;
347
Sean Paulac874152016-03-10 16:00:26 -0500348 layers_.erase(layer);
349 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500350}
351
352HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
Sean Paulac874152016-03-10 16:00:26 -0500353 supported(__func__);
354 DrmMode const &mode = connector_->active_mode();
355 if (mode.id() == 0)
356 return HWC2::Error::BadConfig;
357
358 *config = mode.id();
359 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500360}
361
362HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
363 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500364 supported(__func__);
365 uint32_t num_changes = 0;
366 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
367 if (l.second.type_changed()) {
368 if (layers && num_changes < *num_elements)
369 layers[num_changes] = l.first;
370 if (types && num_changes < *num_elements)
371 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
372 ++num_changes;
373 }
374 }
375 if (!layers && !types)
376 *num_elements = num_changes;
377 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500378}
379
380HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500381 uint32_t height,
382 int32_t /*format*/,
383 int32_t dataspace) {
384 supported(__func__);
385 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
386 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
387
388 if (width < min.first || height < min.second)
389 return HWC2::Error::Unsupported;
390
391 if (width > max.first || height > max.second)
392 return HWC2::Error::Unsupported;
393
394 if (dataspace != HAL_DATASPACE_UNKNOWN &&
395 dataspace != HAL_DATASPACE_STANDARD_UNSPECIFIED)
396 return HWC2::Error::Unsupported;
397
398 // TODO: Validate format can be handled by either GL or planes
399 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500400}
401
402HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500403 int32_t *modes) {
404 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800405 if (!modes)
406 *num_modes = 1;
407
408 if (modes)
409 *modes = HAL_COLOR_MODE_NATIVE;
410
411 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500412}
413
414HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500415 int32_t attribute_in,
416 int32_t *value) {
417 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400418 auto mode = std::find_if(connector_->modes().begin(),
419 connector_->modes().end(),
420 [config](DrmMode const &m) {
421 return m.id() == config;
422 });
Sean Paulac874152016-03-10 16:00:26 -0500423 if (mode == connector_->modes().end()) {
424 ALOGE("Could not find active mode for %d", config);
425 return HWC2::Error::BadConfig;
426 }
427
428 static const int32_t kUmPerInch = 25400;
429 uint32_t mm_width = connector_->mm_width();
430 uint32_t mm_height = connector_->mm_height();
431 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
432 switch (attribute) {
433 case HWC2::Attribute::Width:
434 *value = mode->h_display();
435 break;
436 case HWC2::Attribute::Height:
437 *value = mode->v_display();
438 break;
439 case HWC2::Attribute::VsyncPeriod:
440 // in nanoseconds
441 *value = 1000 * 1000 * 1000 / mode->v_refresh();
442 break;
443 case HWC2::Attribute::DpiX:
444 // Dots per 1000 inches
445 *value = mm_width ? (mode->h_display() * kUmPerInch) / mm_width : -1;
446 break;
447 case HWC2::Attribute::DpiY:
448 // Dots per 1000 inches
449 *value = mm_height ? (mode->v_display() * kUmPerInch) / mm_height : -1;
450 break;
451 default:
452 *value = -1;
453 return HWC2::Error::BadConfig;
454 }
455 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500456}
457
458HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
459 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500460 supported(__func__);
461 // Since this callback is normally invoked twice (once to get the count, and
462 // once to populate configs), we don't really want to read the edid
463 // redundantly. Instead, only update the modes on the first invocation. While
464 // it's possible this will result in stale modes, it'll all come out in the
465 // wash when we try to set the active config later.
466 if (!configs) {
467 int ret = connector_->UpdateModes();
468 if (ret) {
469 ALOGE("Failed to update display modes %d", ret);
470 return HWC2::Error::BadDisplay;
471 }
472 }
473
Neil Armstrongb67d0492019-06-20 09:00:21 +0000474 // Since the upper layers only look at vactive/hactive/refresh, height and
475 // width, it doesn't differentiate interlaced from progressive and other
476 // similar modes. Depending on the order of modes we return to SF, it could
477 // end up choosing a suboptimal configuration and dropping the preferred
478 // mode. To workaround this, don't offer interlaced modes to SF if there is
479 // at least one non-interlaced alternative and only offer a single WxH@R
480 // mode with at least the prefered mode from in DrmConnector::UpdateModes()
481
482 // TODO: Remove the following block of code until AOSP handles all modes
483 std::vector<DrmMode> sel_modes;
484
485 // Add the preferred mode first to be sure it's not dropped
486 auto mode = std::find_if(connector_->modes().begin(),
487 connector_->modes().end(), [&](DrmMode const &m) {
488 return m.id() ==
489 connector_->get_preferred_mode_id();
490 });
491 if (mode != connector_->modes().end())
492 sel_modes.push_back(*mode);
493
494 // Add the active mode if different from preferred mode
495 if (connector_->active_mode().id() != connector_->get_preferred_mode_id())
496 sel_modes.push_back(connector_->active_mode());
497
498 // Cycle over the modes and filter out "similar" modes, keeping only the
499 // first ones in the order given by DRM (from CEA ids and timings order)
Sean Paulac874152016-03-10 16:00:26 -0500500 for (const DrmMode &mode : connector_->modes()) {
Neil Armstrongb67d0492019-06-20 09:00:21 +0000501 // TODO: Remove this when 3D Attributes are in AOSP
502 if (mode.flags() & DRM_MODE_FLAG_3D_MASK)
503 continue;
504
Neil Armstrong4c027a72019-06-04 14:48:02 +0000505 // TODO: Remove this when the Interlaced attribute is in AOSP
506 if (mode.flags() & DRM_MODE_FLAG_INTERLACE) {
507 auto m = std::find_if(connector_->modes().begin(),
508 connector_->modes().end(),
509 [&mode](DrmMode const &m) {
510 return !(m.flags() & DRM_MODE_FLAG_INTERLACE) &&
511 m.h_display() == mode.h_display() &&
512 m.v_display() == mode.v_display();
513 });
Neil Armstrongb67d0492019-06-20 09:00:21 +0000514 if (m == connector_->modes().end())
515 sel_modes.push_back(mode);
516
517 continue;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000518 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000519
520 // Search for a similar WxH@R mode in the filtered list and drop it if
521 // another mode with the same WxH@R has already been selected
522 // TODO: Remove this when AOSP handles duplicates modes
523 auto m = std::find_if(sel_modes.begin(), sel_modes.end(),
524 [&mode](DrmMode const &m) {
525 return m.h_display() == mode.h_display() &&
526 m.v_display() == mode.v_display() &&
527 m.v_refresh() == mode.v_refresh();
528 });
529 if (m == sel_modes.end())
530 sel_modes.push_back(mode);
531 }
532
533 auto num_modes = static_cast<uint32_t>(sel_modes.size());
534 if (!configs) {
535 *num_configs = num_modes;
536 return HWC2::Error::None;
537 }
538
539 uint32_t idx = 0;
540 for (const DrmMode &mode : sel_modes) {
541 if (idx >= *num_configs)
542 break;
543 configs[idx++] = mode.id();
Sean Paulac874152016-03-10 16:00:26 -0500544 }
545 *num_configs = idx;
546 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500547}
548
549HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500550 supported(__func__);
551 std::ostringstream stream;
552 stream << "display-" << connector_->id();
553 std::string string = stream.str();
554 size_t length = string.length();
555 if (!name) {
556 *size = length;
557 return HWC2::Error::None;
558 }
559
560 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
561 strncpy(name, string.c_str(), *size);
562 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500563}
564
Sean Paulac874152016-03-10 16:00:26 -0500565HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
566 uint32_t *num_elements,
567 hwc2_layer_t *layers,
568 int32_t *layer_requests) {
569 supported(__func__);
570 // TODO: I think virtual display should request
571 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
572 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
573 *num_elements = 0;
574 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500575}
576
577HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500578 supported(__func__);
579 *type = static_cast<int32_t>(type_);
580 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500581}
582
583HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500584 supported(__func__);
585 *support = 0;
586 return HWC2::Error::None;
587}
588
589HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400590 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
591 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500592 supported(__func__);
593 *num_types = 0;
594 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500595}
596
597HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500598 hwc2_layer_t *layers,
599 int32_t *fences) {
600 supported(__func__);
601 uint32_t num_layers = 0;
602
603 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
604 ++num_layers;
605 if (layers == NULL || fences == NULL) {
606 continue;
607 } else if (num_layers > *num_elements) {
608 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
609 return HWC2::Error::None;
610 }
611
612 layers[num_layers - 1] = l.first;
613 fences[num_layers - 1] = l.second.take_release_fence();
614 }
615 *num_elements = num_layers;
616 return HWC2::Error::None;
617}
618
Matteo Franchinc56eede2019-12-03 17:10:38 +0000619void DrmHwcTwo::HwcDisplay::AddFenceToPresentFence(int fd) {
Sean Paulac874152016-03-10 16:00:26 -0500620 if (fd < 0)
621 return;
622
Matteo Franchinc56eede2019-12-03 17:10:38 +0000623 if (present_fence_.get() >= 0) {
624 int old_fence = present_fence_.get();
625 present_fence_.Set(sync_merge("dc_present", old_fence, fd));
626 close(fd);
Sean Paulac874152016-03-10 16:00:26 -0500627 } else {
Matteo Franchinc56eede2019-12-03 17:10:38 +0000628 present_fence_.Set(fd);
Sean Paulac874152016-03-10 16:00:26 -0500629 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500630}
631
Roman Stratiienkoafb36892019-11-08 17:16:11 +0200632bool DrmHwcTwo::HwcDisplay::HardwareSupportsLayerType(
633 HWC2::Composition comp_type) {
634 return comp_type == HWC2::Composition::Device ||
635 comp_type == HWC2::Composition::Cursor;
636}
637
Rob Herring4f6c62e2018-05-17 14:33:02 -0500638HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500639 std::vector<DrmCompositionDisplayLayersMap> layers_map;
640 layers_map.emplace_back();
641 DrmCompositionDisplayLayersMap &map = layers_map.back();
642
643 map.display = static_cast<int>(handle_);
644 map.geometry_changed = true; // TODO: Fix this
645
646 // order the layers by z-order
647 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100648 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500649 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
650 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200651 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500652 case HWC2::Composition::Device:
653 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
654 break;
655 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100656 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500657 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100658 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500659 break;
660 default:
661 continue;
662 }
663 }
664 if (use_client_layer)
665 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
666
Rob Herring4f6c62e2018-05-17 14:33:02 -0500667 if (z_map.empty())
668 return HWC2::Error::BadLayer;
669
Sean Paulac874152016-03-10 16:00:26 -0500670 // now that they're ordered by z, add them to the composition
671 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
672 DrmHwcLayer layer;
673 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200674 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500675 if (ret) {
676 ALOGE("Failed to import layer, ret=%d", ret);
677 return HWC2::Error::NoResources;
678 }
679 map.layers.emplace_back(std::move(layer));
680 }
Sean Paulac874152016-03-10 16:00:26 -0500681
Sean Paulf72cccd2018-08-27 13:59:08 -0400682 std::unique_ptr<DrmDisplayComposition> composition = compositor_
683 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500684 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
685
686 // TODO: Don't always assume geometry changed
687 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
688 if (ret) {
689 ALOGE("Failed to set layers in the composition ret=%d", ret);
690 return HWC2::Error::BadLayer;
691 }
692
693 std::vector<DrmPlane *> primary_planes(primary_planes_);
694 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500695 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500696 if (ret) {
697 ALOGE("Failed to plan the composition ret=%d", ret);
698 return HWC2::Error::BadConfig;
699 }
700
701 // Disable the planes we're not using
702 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
703 composition->AddPlaneDisable(*i);
704 i = primary_planes.erase(i);
705 }
706 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
707 composition->AddPlaneDisable(*i);
708 i = overlay_planes.erase(i);
709 }
710
Rob Herring4f6c62e2018-05-17 14:33:02 -0500711 if (test) {
712 ret = compositor_.TestComposition(composition.get());
713 } else {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500714 ret = compositor_.ApplyComposition(std::move(composition));
Matteo Franchinc56eede2019-12-03 17:10:38 +0000715 AddFenceToPresentFence(compositor_.TakeOutFence());
Rob Herring4f6c62e2018-05-17 14:33:02 -0500716 }
Sean Paulac874152016-03-10 16:00:26 -0500717 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700718 if (!test)
719 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500720 return HWC2::Error::BadParameter;
721 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500722 return HWC2::Error::None;
723}
724
Matteo Franchinc56eede2019-12-03 17:10:38 +0000725HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500726 supported(__func__);
727 HWC2::Error ret;
728
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200729 ++total_stats_.total_frames_;
730
Rob Herring4f6c62e2018-05-17 14:33:02 -0500731 ret = CreateComposition(false);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200732 if (ret != HWC2::Error::None)
733 ++total_stats_.failed_kms_present_;
734
Rob Herring4f6c62e2018-05-17 14:33:02 -0500735 if (ret == HWC2::Error::BadLayer) {
736 // Can we really have no client or device layers?
Matteo Franchinc56eede2019-12-03 17:10:38 +0000737 *present_fence = -1;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500738 return HWC2::Error::None;
739 }
740 if (ret != HWC2::Error::None)
741 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500742
Matteo Franchinc56eede2019-12-03 17:10:38 +0000743 *present_fence = present_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500744
745 ++frame_no_;
746 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500747}
748
749HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500750 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400751 auto mode = std::find_if(connector_->modes().begin(),
752 connector_->modes().end(),
753 [config](DrmMode const &m) {
754 return m.id() == config;
755 });
Sean Paulac874152016-03-10 16:00:26 -0500756 if (mode == connector_->modes().end()) {
757 ALOGE("Could not find active mode for %d", config);
758 return HWC2::Error::BadConfig;
759 }
760
Sean Paulf72cccd2018-08-27 13:59:08 -0400761 std::unique_ptr<DrmDisplayComposition> composition = compositor_
762 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500763 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
764 int ret = composition->SetDisplayMode(*mode);
Sean Pauled45a8e2017-02-28 13:17:34 -0500765 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500766 if (ret) {
767 ALOGE("Failed to queue dpms composition on %d", ret);
768 return HWC2::Error::BadConfig;
769 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300770
771 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500772
773 // Setup the client layer's dimensions
774 hwc_rect_t display_frame = {.left = 0,
775 .top = 0,
776 .right = static_cast<int>(mode->h_display()),
777 .bottom = static_cast<int>(mode->v_display())};
778 client_layer_.SetLayerDisplayFrame(display_frame);
779 hwc_frect_t source_crop = {.left = 0.0f,
780 .top = 0.0f,
781 .right = mode->h_display() + 0.0f,
782 .bottom = mode->v_display() + 0.0f};
783 client_layer_.SetLayerSourceCrop(source_crop);
784
785 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500786}
787
788HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
789 int32_t acquire_fence,
790 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600791 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500792 supported(__func__);
793 UniqueFd uf(acquire_fence);
794
795 client_layer_.set_buffer(target);
796 client_layer_.set_acquire_fence(uf.get());
797 client_layer_.SetLayerDataspace(dataspace);
798 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500799}
800
801HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500802 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800803
804 if (mode != HAL_COLOR_MODE_NATIVE)
Vincent Donnefort7834a892019-10-09 15:53:56 +0100805 return HWC2::Error::BadParameter;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800806
807 color_mode_ = mode;
808 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500809}
810
811HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500812 int32_t hint) {
813 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200814 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
815 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
816 return HWC2::Error::BadParameter;
817
818 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
819 return HWC2::Error::BadParameter;
820
821 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
822 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
823 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
824
825 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500826}
827
828HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500829 int32_t release_fence) {
830 supported(__func__);
831 // TODO: Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500832 return unsupported(__func__, buffer, release_fence);
833}
834
Sean Paulac874152016-03-10 16:00:26 -0500835HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
836 supported(__func__);
837 uint64_t dpms_value = 0;
838 auto mode = static_cast<HWC2::PowerMode>(mode_in);
839 switch (mode) {
840 case HWC2::PowerMode::Off:
841 dpms_value = DRM_MODE_DPMS_OFF;
842 break;
843 case HWC2::PowerMode::On:
844 dpms_value = DRM_MODE_DPMS_ON;
845 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100846 case HWC2::PowerMode::Doze:
847 case HWC2::PowerMode::DozeSuspend:
848 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500849 default:
850 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100851 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500852 };
853
Sean Paulf72cccd2018-08-27 13:59:08 -0400854 std::unique_ptr<DrmDisplayComposition> composition = compositor_
855 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500856 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
857 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500858 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500859 if (ret) {
860 ALOGE("Failed to apply the dpms composition ret=%d", ret);
861 return HWC2::Error::BadParameter;
862 }
863 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500864}
865
866HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500867 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300868 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500869 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500870}
871
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200872uint32_t DrmHwcTwo::HwcDisplay::CalcPixOps(
873 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t first_z,
874 size_t size) {
875 uint32_t pixops = 0;
876 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
877 if (l.first >= first_z && l.first < first_z + size) {
878 hwc_rect_t df = l.second->display_frame();
879 pixops += (df.right - df.left) * (df.bottom - df.top);
880 }
881 }
882 return pixops;
883}
884
885void DrmHwcTwo::HwcDisplay::MarkValidated(
886 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t client_first_z,
887 size_t client_size) {
888 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
889 if (l.first >= client_first_z && l.first < client_first_z + client_size)
890 l.second->set_validated_type(HWC2::Composition::Client);
891 else
892 l.second->set_validated_type(HWC2::Composition::Device);
893 }
894}
895
Sean Pauled2ec4b2016-03-10 15:35:40 -0500896HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500897 uint32_t *num_requests) {
898 supported(__func__);
899 *num_types = 0;
900 *num_requests = 0;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500901 size_t avail_planes = primary_planes_.size() + overlay_planes_.size();
Rob Herring4f6c62e2018-05-17 14:33:02 -0500902
903 /*
904 * If more layers then planes, save one plane
905 * for client composited layers
906 */
907 if (avail_planes < layers_.size())
908 avail_planes--;
909
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200910 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
Roman Stratiienkof2647232019-11-21 01:58:35 +0200911 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
912 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
913
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200914 uint32_t total_pixops = CalcPixOps(z_map, 0, z_map.size()), gpu_pixops = 0;
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200915
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200916 int client_start = -1, client_size = 0;
917
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200918 if (compositor_.ShouldFlattenOnClient()) {
919 client_start = 0;
920 client_size = z_map.size();
921 MarkValidated(z_map, client_start, client_size);
922 } else {
923 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
924 if (!HardwareSupportsLayerType(l.second->sf_type()) ||
925 !importer_->CanImportBuffer(l.second->buffer()) ||
926 color_transform_hint_ != HAL_COLOR_TRANSFORM_IDENTITY ||
927 (l.second->RequireScalingOrPhasing() &&
928 resource_manager_->ForcedScalingWithGpu())) {
929 if (client_start < 0)
930 client_start = l.first;
931 client_size = (l.first - client_start) + 1;
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200932 }
933 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500934
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200935 int extra_client = (z_map.size() - client_size) - avail_planes;
936 if (extra_client > 0) {
937 int start = 0, steps;
938 if (client_size != 0) {
939 int prepend = std::min(client_start, extra_client);
940 int append = std::min(int(z_map.size() - (client_start + client_size)),
941 extra_client);
942 start = client_start - prepend;
943 client_size += extra_client;
944 steps = 1 + std::min(std::min(append, prepend),
945 int(z_map.size()) - (start + client_size));
946 } else {
947 client_size = extra_client;
948 steps = 1 + z_map.size() - extra_client;
949 }
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200950
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200951 gpu_pixops = INT_MAX;
952 for (int i = 0; i < steps; i++) {
953 uint32_t po = CalcPixOps(z_map, start + i, client_size);
954 if (po < gpu_pixops) {
955 gpu_pixops = po;
956 client_start = start + i;
957 }
958 }
959 }
960
961 MarkValidated(z_map, client_start, client_size);
962
963 if (CreateComposition(true) != HWC2::Error::None) {
964 ++total_stats_.failed_kms_validate_;
965 gpu_pixops = total_pixops;
966 client_size = z_map.size();
967 MarkValidated(z_map, 0, client_size);
968 }
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200969 }
970
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200971 *num_types = client_size;
972
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200973 total_stats_.gpu_pixops_ += gpu_pixops;
974 total_stats_.total_pixops_ += total_pixops;
Roman Stratiienkof2647232019-11-21 01:58:35 +0200975
Rob Herringee8f45b2017-06-09 15:15:55 -0500976 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500977}
978
John Stultz8c7229d2020-02-07 21:31:08 +0000979#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800980HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
981 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
982 supported(__func__);
983
984 drmModePropertyBlobPtr blob;
985 int ret;
986 uint64_t blob_id;
987
988 std::tie(ret, blob_id) = connector_->edid_property().value();
989 if (ret) {
990 ALOGE("Failed to get edid property value.");
991 return HWC2::Error::Unsupported;
992 }
993
994 blob = drmModeGetPropertyBlob(drm_->fd(), blob_id);
995
996 outData = static_cast<uint8_t *>(blob->data);
997
998 *outPort = connector_->id();
999 *outDataSize = blob->length;
1000
1001 return HWC2::Error::None;
1002}
1003
1004HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
1005 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
1006 unsupported(__func__, outCapabilities);
1007
1008 if (outNumCapabilities == NULL) {
1009 return HWC2::Error::BadParameter;
1010 }
1011
1012 *outNumCapabilities = 0;
1013
1014 return HWC2::Error::None;
1015}
John Stultz8c7229d2020-02-07 21:31:08 +00001016#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001017
Sean Pauled2ec4b2016-03-10 15:35:40 -05001018HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -05001019 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -08001020 cursor_x_ = x;
1021 cursor_y_ = y;
1022 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001023}
1024
1025HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -05001026 supported(__func__);
1027 blending_ = static_cast<HWC2::BlendMode>(mode);
1028 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001029}
1030
1031HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001032 int32_t acquire_fence) {
1033 supported(__func__);
1034 UniqueFd uf(acquire_fence);
1035
1036 // The buffer and acquire_fence are handled elsewhere
1037 if (sf_type_ == HWC2::Composition::Client ||
1038 sf_type_ == HWC2::Composition::Sideband ||
1039 sf_type_ == HWC2::Composition::SolidColor)
1040 return HWC2::Error::None;
1041
1042 set_buffer(buffer);
1043 set_acquire_fence(uf.get());
1044 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001045}
1046
1047HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001048 // TODO: Put to client composition here?
1049 supported(__func__);
1050 layer_color_ = color;
1051 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001052}
1053
1054HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001055 sf_type_ = static_cast<HWC2::Composition>(type);
1056 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001057}
1058
1059HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001060 supported(__func__);
1061 dataspace_ = static_cast<android_dataspace_t>(dataspace);
1062 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001063}
1064
1065HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001066 supported(__func__);
1067 display_frame_ = frame;
1068 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001069}
1070
1071HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001072 supported(__func__);
1073 alpha_ = alpha;
1074 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001075}
1076
1077HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1078 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001079 supported(__func__);
1080 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001081 return unsupported(__func__, stream);
1082}
1083
1084HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001085 supported(__func__);
1086 source_crop_ = crop;
1087 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001088}
1089
1090HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001091 supported(__func__);
1092 // TODO: We don't use surface damage, marking as unsupported
1093 unsupported(__func__, damage);
1094 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001095}
1096
1097HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001098 supported(__func__);
1099 transform_ = static_cast<HWC2::Transform>(transform);
1100 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001101}
1102
1103HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001104 supported(__func__);
1105 // TODO: We don't use this information, marking as unsupported
1106 unsupported(__func__, visible);
1107 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001108}
1109
Sean Paulac874152016-03-10 16:00:26 -05001110HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1111 supported(__func__);
1112 z_order_ = order;
1113 return HWC2::Error::None;
1114}
1115
1116void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1117 supported(__func__);
1118 switch (blending_) {
1119 case HWC2::BlendMode::None:
1120 layer->blending = DrmHwcBlending::kNone;
1121 break;
1122 case HWC2::BlendMode::Premultiplied:
1123 layer->blending = DrmHwcBlending::kPreMult;
1124 break;
1125 case HWC2::BlendMode::Coverage:
1126 layer->blending = DrmHwcBlending::kCoverage;
1127 break;
1128 default:
1129 ALOGE("Unknown blending mode b=%d", blending_);
1130 layer->blending = DrmHwcBlending::kNone;
1131 break;
1132 }
1133
1134 OutputFd release_fence = release_fence_output();
1135
1136 layer->sf_handle = buffer_;
1137 layer->acquire_fence = acquire_fence_.Release();
1138 layer->release_fence = std::move(release_fence);
1139 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +02001140 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -05001141 layer->SetSourceCrop(source_crop_);
1142 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -05001143}
1144
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001145void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
1146 auto cb = callbacks_.find(HWC2::Callback::Hotplug);
1147 if (cb == callbacks_.end())
1148 return;
1149
1150 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(cb->second.func);
1151 hotplug(cb->second.data, displayid,
1152 (state == DRM_MODE_CONNECTED ? HWC2_CONNECTION_CONNECTED
1153 : HWC2_CONNECTION_DISCONNECTED));
1154}
1155
1156void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
1157 for (auto &conn : drmDevice->connectors()) {
1158 if (conn->state() != DRM_MODE_CONNECTED)
1159 continue;
1160 HandleDisplayHotplug(conn->display(), conn->state());
1161 }
1162}
1163
1164void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
1165 for (auto &conn : drm_->connectors()) {
1166 drmModeConnection old_state = conn->state();
1167 drmModeConnection cur_state = conn->UpdateModes()
1168 ? DRM_MODE_UNKNOWNCONNECTION
1169 : conn->state();
1170
1171 if (cur_state == old_state)
1172 continue;
1173
1174 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
1175 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1176 conn->id(), conn->display());
1177
1178 int display_id = conn->display();
1179 if (cur_state == DRM_MODE_CONNECTED) {
1180 auto &display = hwc2_->displays_.at(display_id);
1181 display.ChosePreferredConfig();
1182 } else {
1183 auto &display = hwc2_->displays_.at(display_id);
1184 display.ClearDisplay();
1185 }
1186
1187 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1188 }
1189}
1190
Sean Pauled2ec4b2016-03-10 15:35:40 -05001191// static
1192int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1193 unsupported(__func__);
1194 return 0;
1195}
1196
1197// static
1198void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001199 uint32_t *out_count,
1200 int32_t * /*out_capabilities*/) {
1201 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001202 *out_count = 0;
1203}
1204
1205// static
Sean Paulac874152016-03-10 16:00:26 -05001206hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1207 struct hwc2_device * /*dev*/, int32_t descriptor) {
1208 supported(__func__);
1209 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001210 switch (func) {
1211 // Device functions
1212 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1213 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1214 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1215 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001216 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001217 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1218 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1219 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1220 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1221 case HWC2::FunctionDescriptor::Dump:
1222 return ToHook<HWC2_PFN_DUMP>(
1223 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1224 uint32_t *, char *>);
1225 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1226 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1227 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1228 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1229 case HWC2::FunctionDescriptor::RegisterCallback:
1230 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1231 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1232 &DrmHwcTwo::RegisterCallback, int32_t,
1233 hwc2_callback_data_t, hwc2_function_pointer_t>);
1234
1235 // Display functions
1236 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1237 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1238 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1239 &HwcDisplay::AcceptDisplayChanges>);
1240 case HWC2::FunctionDescriptor::CreateLayer:
1241 return ToHook<HWC2_PFN_CREATE_LAYER>(
1242 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1243 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1244 case HWC2::FunctionDescriptor::DestroyLayer:
1245 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1246 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1247 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1248 case HWC2::FunctionDescriptor::GetActiveConfig:
1249 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1250 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1251 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1252 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1253 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1254 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1255 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1256 hwc2_layer_t *, int32_t *>);
1257 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1258 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1259 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1260 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1261 int32_t, int32_t>);
1262 case HWC2::FunctionDescriptor::GetColorModes:
1263 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1264 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1265 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1266 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001267 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1268 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1269 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1270 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001271 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001272 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1273 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1274 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1275 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001276 case HWC2::FunctionDescriptor::GetDisplayName:
1277 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1278 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1279 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1280 case HWC2::FunctionDescriptor::GetDisplayRequests:
1281 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1282 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1283 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1284 hwc2_layer_t *, int32_t *>);
1285 case HWC2::FunctionDescriptor::GetDisplayType:
1286 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1287 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1288 &HwcDisplay::GetDisplayType, int32_t *>);
1289 case HWC2::FunctionDescriptor::GetDozeSupport:
1290 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1291 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1292 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001293 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1294 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1295 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1296 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1297 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001298 case HWC2::FunctionDescriptor::GetReleaseFences:
1299 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1300 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1301 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1302 int32_t *>);
1303 case HWC2::FunctionDescriptor::PresentDisplay:
1304 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1305 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1306 &HwcDisplay::PresentDisplay, int32_t *>);
1307 case HWC2::FunctionDescriptor::SetActiveConfig:
1308 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1309 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1310 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1311 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001312 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1313 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1314 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1315 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001316 case HWC2::FunctionDescriptor::SetColorMode:
1317 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1318 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1319 &HwcDisplay::SetColorMode, int32_t>);
1320 case HWC2::FunctionDescriptor::SetColorTransform:
1321 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1322 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1323 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1324 case HWC2::FunctionDescriptor::SetOutputBuffer:
1325 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1326 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1327 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1328 case HWC2::FunctionDescriptor::SetPowerMode:
1329 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1330 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1331 &HwcDisplay::SetPowerMode, int32_t>);
1332 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1333 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1334 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1335 &HwcDisplay::SetVsyncEnabled, int32_t>);
1336 case HWC2::FunctionDescriptor::ValidateDisplay:
1337 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1338 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1339 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
John Stultz8c7229d2020-02-07 21:31:08 +00001340#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001341 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1342 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1343 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1344 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1345 uint32_t *, uint8_t *>);
1346 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1347 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1348 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1349 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1350 uint32_t *>);
John Stultz8c7229d2020-02-07 21:31:08 +00001351#endif /* PLATFORM_SDK_VERSION > 28 */
Sean Pauled2ec4b2016-03-10 15:35:40 -05001352 // Layer functions
1353 case HWC2::FunctionDescriptor::SetCursorPosition:
1354 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1355 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1356 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1357 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1358 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1359 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1360 &HwcLayer::SetLayerBlendMode, int32_t>);
1361 case HWC2::FunctionDescriptor::SetLayerBuffer:
1362 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1363 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1364 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1365 case HWC2::FunctionDescriptor::SetLayerColor:
1366 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1367 LayerHook<decltype(&HwcLayer::SetLayerColor),
1368 &HwcLayer::SetLayerColor, hwc_color_t>);
1369 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1370 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1371 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1372 &HwcLayer::SetLayerCompositionType, int32_t>);
1373 case HWC2::FunctionDescriptor::SetLayerDataspace:
1374 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1375 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1376 &HwcLayer::SetLayerDataspace, int32_t>);
1377 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1378 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1379 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1380 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1381 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1382 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1383 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1384 &HwcLayer::SetLayerPlaneAlpha, float>);
1385 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001386 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1387 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1388 &HwcLayer::SetLayerSidebandStream,
1389 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001390 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1391 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1392 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1393 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1394 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1395 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1396 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1397 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1398 case HWC2::FunctionDescriptor::SetLayerTransform:
1399 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1400 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1401 &HwcLayer::SetLayerTransform, int32_t>);
1402 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1403 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1404 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1405 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1406 case HWC2::FunctionDescriptor::SetLayerZOrder:
1407 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1408 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1409 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001410 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001411 default:
1412 return NULL;
1413 }
1414}
Sean Paulac874152016-03-10 16:00:26 -05001415
1416// static
1417int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1418 struct hw_device_t **dev) {
1419 supported(__func__);
1420 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1421 ALOGE("Invalid module name- %s", name);
1422 return -EINVAL;
1423 }
1424
1425 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1426 if (!ctx) {
1427 ALOGE("Failed to allocate DrmHwcTwo");
1428 return -ENOMEM;
1429 }
1430
1431 HWC2::Error err = ctx->Init();
1432 if (err != HWC2::Error::None) {
1433 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1434 return -EINVAL;
1435 }
1436
1437 ctx->common.module = const_cast<hw_module_t *>(module);
1438 *dev = &ctx->common;
1439 ctx.release();
1440 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001441}
Sean Paulf72cccd2018-08-27 13:59:08 -04001442} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001443
1444static struct hw_module_methods_t hwc2_module_methods = {
1445 .open = android::DrmHwcTwo::HookDevOpen,
1446};
1447
1448hw_module_t HAL_MODULE_INFO_SYM = {
1449 .tag = HARDWARE_MODULE_TAG,
1450 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1451 .id = HWC_HARDWARE_MODULE_ID,
1452 .name = "DrmHwcTwo module",
1453 .author = "The Android Open Source Project",
1454 .methods = &hwc2_module_methods,
1455 .dso = NULL,
1456 .reserved = {0},
1457};