blob: 798e013273ad85056cc037885618450008d463a6 [file] [log] [blame]
Sean Pauled2ec4b2016-03-10 15:35:40 -05001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18#define LOG_TAG "hwc-drm-two"
19
Sean Paulf72cccd2018-08-27 13:59:08 -040020#include "drmhwctwo.h"
Sean Paulac874152016-03-10 16:00:26 -050021#include "drmdisplaycomposition.h"
22#include "drmhwcomposer.h"
Sean Paulac874152016-03-10 16:00:26 -050023#include "platform.h"
24#include "vsyncworker.h"
25
26#include <inttypes.h>
27#include <string>
Sean Pauled2ec4b2016-03-10 15:35:40 -050028
Sean Paulac874152016-03-10 16:00:26 -050029#include <cutils/properties.h>
30#include <hardware/hardware.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050031#include <hardware/hwcomposer2.h>
Sean Paulf72cccd2018-08-27 13:59:08 -040032#include <log/log.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050033
34namespace android {
35
Sean Paulac874152016-03-10 16:00:26 -050036class DrmVsyncCallback : public VsyncCallback {
37 public:
38 DrmVsyncCallback(hwc2_callback_data_t data, hwc2_function_pointer_t hook)
39 : data_(data), hook_(hook) {
40 }
41
42 void Callback(int display, int64_t timestamp) {
43 auto hook = reinterpret_cast<HWC2_PFN_VSYNC>(hook_);
44 hook(data_, display, timestamp);
45 }
46
47 private:
48 hwc2_callback_data_t data_;
49 hwc2_function_pointer_t hook_;
50};
51
Sean Pauled2ec4b2016-03-10 15:35:40 -050052DrmHwcTwo::DrmHwcTwo() {
Sean Paulac874152016-03-10 16:00:26 -050053 common.tag = HARDWARE_DEVICE_TAG;
54 common.version = HWC_DEVICE_API_VERSION_2_0;
Sean Pauled2ec4b2016-03-10 15:35:40 -050055 common.close = HookDevClose;
56 getCapabilities = HookDevGetCapabilities;
57 getFunction = HookDevGetFunction;
58}
59
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030060HWC2::Error DrmHwcTwo::CreateDisplay(hwc2_display_t displ,
61 HWC2::DisplayType type) {
62 DrmDevice *drm = resource_manager_.GetDrmDevice(displ);
63 std::shared_ptr<Importer> importer = resource_manager_.GetImporter(displ);
Alexandru Gheorghec5463582018-03-27 15:52:02 +010064 if (!drm || !importer) {
65 ALOGE("Failed to get a valid drmresource and importer");
Sean Paulac874152016-03-10 16:00:26 -050066 return HWC2::Error::NoResources;
67 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030068 displays_.emplace(std::piecewise_construct, std::forward_as_tuple(displ),
Sean Paulf72cccd2018-08-27 13:59:08 -040069 std::forward_as_tuple(&resource_manager_, drm, importer,
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030070 displ, type));
Sean Paulac874152016-03-10 16:00:26 -050071
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030072 DrmCrtc *crtc = drm->GetCrtcForDisplay(static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050073 if (!crtc) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030074 ALOGE("Failed to get crtc for display %d", static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050075 return HWC2::Error::BadDisplay;
76 }
Sean Paulac874152016-03-10 16:00:26 -050077 std::vector<DrmPlane *> display_planes;
Alexandru Gheorghec5463582018-03-27 15:52:02 +010078 for (auto &plane : drm->planes()) {
Sean Paulac874152016-03-10 16:00:26 -050079 if (plane->GetCrtcSupported(*crtc))
80 display_planes.push_back(plane.get());
81 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030082 displays_.at(displ).Init(&display_planes);
Sean Paulac874152016-03-10 16:00:26 -050083 return HWC2::Error::None;
84}
85
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030086HWC2::Error DrmHwcTwo::Init() {
87 int rv = resource_manager_.Init();
88 if (rv) {
89 ALOGE("Can't initialize the resource manager %d", rv);
90 return HWC2::Error::NoResources;
91 }
92
93 HWC2::Error ret = HWC2::Error::None;
94 for (int i = 0; i < resource_manager_.getDisplayCount(); i++) {
95 ret = CreateDisplay(i, HWC2::DisplayType::Physical);
96 if (ret != HWC2::Error::None) {
97 ALOGE("Failed to create display %d with error %d", i, ret);
98 return ret;
99 }
100 }
101
102 auto &drmDevices = resource_manager_.getDrmDevices();
103 for (auto &device : drmDevices) {
104 device->RegisterHotplugHandler(new DrmHotplugHandler(this, device.get()));
105 }
106 return ret;
107}
108
Sean Pauled2ec4b2016-03-10 15:35:40 -0500109template <typename... Args>
110static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
111 ALOGV("Unsupported function: %s", func);
112 return HWC2::Error::Unsupported;
113}
114
Sean Paulac874152016-03-10 16:00:26 -0500115static inline void supported(char const *func) {
116 ALOGV("Supported function: %s", func);
117}
118
Sean Pauled2ec4b2016-03-10 15:35:40 -0500119HWC2::Error DrmHwcTwo::CreateVirtualDisplay(uint32_t width, uint32_t height,
120 int32_t *format,
121 hwc2_display_t *display) {
122 // TODO: Implement virtual display
Sean Paulac874152016-03-10 16:00:26 -0500123 return unsupported(__func__, width, height, format, display);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500124}
125
126HWC2::Error DrmHwcTwo::DestroyVirtualDisplay(hwc2_display_t display) {
Sean Paulac874152016-03-10 16:00:26 -0500127 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500128 return unsupported(__func__, display);
129}
130
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200131std::string DrmHwcTwo::HwcDisplay::DumpDelta(
132 DrmHwcTwo::HwcDisplay::Stats delta) {
133 if (delta.total_pixops_ == 0)
134 return "No stats yet";
135 double Ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
136
137 return (std::stringstream()
138 << " Total frames count: " << delta.total_frames_ << "\n"
139 << " Failed to test commit frames: " << delta.failed_kms_validate_
140 << "\n"
141 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
142 << ((delta.failed_kms_present_ > 0)
143 ? " !!! Internal failure, FIX it please\n"
144 : "")
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200145 << " Flattened frames: " << delta.frames_flattened_ << "\n"
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200146 << " Pixel operations (free units)"
147 << " : [TOTAL: " << delta.total_pixops_
148 << " / GPU: " << delta.gpu_pixops_ << "]\n"
149 << " Composition efficiency: " << Ratio)
150 .str();
151}
152
153std::string DrmHwcTwo::HwcDisplay::Dump() {
154 auto out = (std::stringstream()
155 << "- Display on: " << connector_->name() << "\n"
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200156 << " Flattening state: " << compositor_.GetFlatteningState()
157 << "\n"
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200158 << "Statistics since system boot:\n"
159 << DumpDelta(total_stats_) << "\n\n"
160 << "Statistics since last dumpsys request:\n"
161 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n")
162 .str();
163
164 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
165 return out;
166}
167
168void DrmHwcTwo::Dump(uint32_t *outSize, char *outBuffer) {
169 supported(__func__);
170
171 if (outBuffer != nullptr) {
172 auto copiedBytes = mDumpString.copy(outBuffer, *outSize);
173 *outSize = static_cast<uint32_t>(copiedBytes);
174 return;
175 }
176
177 std::stringstream output;
178
179 output << "-- drm_hwcomposer --\n\n";
180
181 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &dp : displays_)
182 output << dp.second.Dump();
183
184 mDumpString = output.str();
185 *outSize = static_cast<uint32_t>(mDumpString.size());
Sean Pauled2ec4b2016-03-10 15:35:40 -0500186}
187
188uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() {
Sean Paulac874152016-03-10 16:00:26 -0500189 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500190 unsupported(__func__);
191 return 0;
192}
193
194HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor,
Sean Paulac874152016-03-10 16:00:26 -0500195 hwc2_callback_data_t data,
196 hwc2_function_pointer_t function) {
197 supported(__func__);
198 auto callback = static_cast<HWC2::Callback>(descriptor);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300199
200 if (!function) {
201 callbacks_.erase(callback);
202 return HWC2::Error::None;
203 }
204
Sean Paulac874152016-03-10 16:00:26 -0500205 callbacks_.emplace(callback, HwcCallback(data, function));
206
207 switch (callback) {
208 case HWC2::Callback::Hotplug: {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300209 auto &drmDevices = resource_manager_.getDrmDevices();
210 for (auto &device : drmDevices)
211 HandleInitialHotplugState(device.get());
Sean Paulac874152016-03-10 16:00:26 -0500212 break;
213 }
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200214 case HWC2::Callback::Refresh: {
215 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
216 displays_)
217 d.second.RegisterRefreshCallback(data, function);
218 break;
219 }
Sean Paulac874152016-03-10 16:00:26 -0500220 case HWC2::Callback::Vsync: {
221 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
222 displays_)
223 d.second.RegisterVsyncCallback(data, function);
224 break;
225 }
226 default:
227 break;
228 }
229 return HWC2::Error::None;
230}
231
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100232DrmHwcTwo::HwcDisplay::HwcDisplay(ResourceManager *resource_manager,
233 DrmDevice *drm,
Sean Paulac874152016-03-10 16:00:26 -0500234 std::shared_ptr<Importer> importer,
Sean Paulac874152016-03-10 16:00:26 -0500235 hwc2_display_t handle, HWC2::DisplayType type)
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100236 : resource_manager_(resource_manager),
237 drm_(drm),
238 importer_(importer),
239 handle_(handle),
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200240 type_(type),
241 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
Sean Paulac874152016-03-10 16:00:26 -0500242 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200243
244 // clang-format off
245 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
246 0.0, 1.0, 0.0, 0.0,
247 0.0, 0.0, 1.0, 0.0,
248 0.0, 0.0, 0.0, 1.0};
249 // clang-format on
Sean Paulac874152016-03-10 16:00:26 -0500250}
251
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300252void DrmHwcTwo::HwcDisplay::ClearDisplay() {
253 compositor_.ClearDisplay();
254}
255
Sean Paulac874152016-03-10 16:00:26 -0500256HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
257 supported(__func__);
258 planner_ = Planner::CreateInstance(drm_);
259 if (!planner_) {
260 ALOGE("Failed to create planner instance for composition");
261 return HWC2::Error::NoResources;
262 }
263
264 int display = static_cast<int>(handle_);
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100265 int ret = compositor_.Init(resource_manager_, display);
Sean Paulac874152016-03-10 16:00:26 -0500266 if (ret) {
267 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
268 return HWC2::Error::NoResources;
269 }
270
271 // Split up the given display planes into primary and overlay to properly
272 // interface with the composition
273 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
274 property_get("hwc.drm.use_overlay_planes", use_overlay_planes_prop, "1");
275 bool use_overlay_planes = atoi(use_overlay_planes_prop);
276 for (auto &plane : *planes) {
277 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
278 primary_planes_.push_back(plane);
279 else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY)
280 overlay_planes_.push_back(plane);
281 }
282
283 crtc_ = drm_->GetCrtcForDisplay(display);
284 if (!crtc_) {
285 ALOGE("Failed to get crtc for display %d", display);
286 return HWC2::Error::BadDisplay;
287 }
288
289 connector_ = drm_->GetConnectorForDisplay(display);
290 if (!connector_) {
291 ALOGE("Failed to get connector for display %d", display);
292 return HWC2::Error::BadDisplay;
293 }
294
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300295 ret = vsync_worker_.Init(drm_, display);
296 if (ret) {
297 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
298 return HWC2::Error::BadDisplay;
299 }
300
301 return ChosePreferredConfig();
302}
303
304HWC2::Error DrmHwcTwo::HwcDisplay::ChosePreferredConfig() {
Sean Paulac874152016-03-10 16:00:26 -0500305 // Fetch the number of modes from the display
306 uint32_t num_configs;
307 HWC2::Error err = GetDisplayConfigs(&num_configs, NULL);
308 if (err != HWC2::Error::None || !num_configs)
309 return err;
310
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200311 return SetActiveConfig(connector_->get_preferred_mode_id());
Sean Paulac874152016-03-10 16:00:26 -0500312}
313
314HWC2::Error DrmHwcTwo::HwcDisplay::RegisterVsyncCallback(
315 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
316 supported(__func__);
317 auto callback = std::make_shared<DrmVsyncCallback>(data, func);
Adrian Salidofa37f672017-02-16 10:29:46 -0800318 vsync_worker_.RegisterCallback(std::move(callback));
Sean Paulac874152016-03-10 16:00:26 -0500319 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500320}
321
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200322void DrmHwcTwo::HwcDisplay::RegisterRefreshCallback(
323 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
324 supported(__func__);
325 auto hook = reinterpret_cast<HWC2_PFN_REFRESH>(func);
326 compositor_.SetRefreshCallback([data, hook](int display) {
327 hook(data, static_cast<hwc2_display_t>(display));
328 });
329}
330
Sean Pauled2ec4b2016-03-10 15:35:40 -0500331HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500332 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500333 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
334 l.second.accept_type_change();
335 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500336}
337
338HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500339 supported(__func__);
340 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
341 *layer = static_cast<hwc2_layer_t>(layer_idx_);
342 ++layer_idx_;
343 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500344}
345
346HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Sean Paulac874152016-03-10 16:00:26 -0500347 supported(__func__);
Vincent Donnefort9abec032019-10-09 15:43:43 +0100348 if (!get_layer(layer))
349 return HWC2::Error::BadLayer;
350
Sean Paulac874152016-03-10 16:00:26 -0500351 layers_.erase(layer);
352 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500353}
354
355HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
Sean Paulac874152016-03-10 16:00:26 -0500356 supported(__func__);
357 DrmMode const &mode = connector_->active_mode();
358 if (mode.id() == 0)
359 return HWC2::Error::BadConfig;
360
361 *config = mode.id();
362 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500363}
364
365HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
366 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500367 supported(__func__);
368 uint32_t num_changes = 0;
369 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
370 if (l.second.type_changed()) {
371 if (layers && num_changes < *num_elements)
372 layers[num_changes] = l.first;
373 if (types && num_changes < *num_elements)
374 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
375 ++num_changes;
376 }
377 }
378 if (!layers && !types)
379 *num_elements = num_changes;
380 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500381}
382
383HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500384 uint32_t height,
385 int32_t /*format*/,
386 int32_t dataspace) {
387 supported(__func__);
388 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
389 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
390
391 if (width < min.first || height < min.second)
392 return HWC2::Error::Unsupported;
393
394 if (width > max.first || height > max.second)
395 return HWC2::Error::Unsupported;
396
397 if (dataspace != HAL_DATASPACE_UNKNOWN &&
398 dataspace != HAL_DATASPACE_STANDARD_UNSPECIFIED)
399 return HWC2::Error::Unsupported;
400
401 // TODO: Validate format can be handled by either GL or planes
402 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500403}
404
405HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500406 int32_t *modes) {
407 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800408 if (!modes)
409 *num_modes = 1;
410
411 if (modes)
412 *modes = HAL_COLOR_MODE_NATIVE;
413
414 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500415}
416
417HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500418 int32_t attribute_in,
419 int32_t *value) {
420 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400421 auto mode = std::find_if(connector_->modes().begin(),
422 connector_->modes().end(),
423 [config](DrmMode const &m) {
424 return m.id() == config;
425 });
Sean Paulac874152016-03-10 16:00:26 -0500426 if (mode == connector_->modes().end()) {
427 ALOGE("Could not find active mode for %d", config);
428 return HWC2::Error::BadConfig;
429 }
430
431 static const int32_t kUmPerInch = 25400;
432 uint32_t mm_width = connector_->mm_width();
433 uint32_t mm_height = connector_->mm_height();
434 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
435 switch (attribute) {
436 case HWC2::Attribute::Width:
437 *value = mode->h_display();
438 break;
439 case HWC2::Attribute::Height:
440 *value = mode->v_display();
441 break;
442 case HWC2::Attribute::VsyncPeriod:
443 // in nanoseconds
444 *value = 1000 * 1000 * 1000 / mode->v_refresh();
445 break;
446 case HWC2::Attribute::DpiX:
447 // Dots per 1000 inches
448 *value = mm_width ? (mode->h_display() * kUmPerInch) / mm_width : -1;
449 break;
450 case HWC2::Attribute::DpiY:
451 // Dots per 1000 inches
452 *value = mm_height ? (mode->v_display() * kUmPerInch) / mm_height : -1;
453 break;
454 default:
455 *value = -1;
456 return HWC2::Error::BadConfig;
457 }
458 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500459}
460
461HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
462 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500463 supported(__func__);
464 // Since this callback is normally invoked twice (once to get the count, and
465 // once to populate configs), we don't really want to read the edid
466 // redundantly. Instead, only update the modes on the first invocation. While
467 // it's possible this will result in stale modes, it'll all come out in the
468 // wash when we try to set the active config later.
469 if (!configs) {
470 int ret = connector_->UpdateModes();
471 if (ret) {
472 ALOGE("Failed to update display modes %d", ret);
473 return HWC2::Error::BadDisplay;
474 }
475 }
476
Neil Armstrongb67d0492019-06-20 09:00:21 +0000477 // Since the upper layers only look at vactive/hactive/refresh, height and
478 // width, it doesn't differentiate interlaced from progressive and other
479 // similar modes. Depending on the order of modes we return to SF, it could
480 // end up choosing a suboptimal configuration and dropping the preferred
481 // mode. To workaround this, don't offer interlaced modes to SF if there is
482 // at least one non-interlaced alternative and only offer a single WxH@R
483 // mode with at least the prefered mode from in DrmConnector::UpdateModes()
484
485 // TODO: Remove the following block of code until AOSP handles all modes
486 std::vector<DrmMode> sel_modes;
487
488 // Add the preferred mode first to be sure it's not dropped
489 auto mode = std::find_if(connector_->modes().begin(),
490 connector_->modes().end(), [&](DrmMode const &m) {
491 return m.id() ==
492 connector_->get_preferred_mode_id();
493 });
494 if (mode != connector_->modes().end())
495 sel_modes.push_back(*mode);
496
497 // Add the active mode if different from preferred mode
498 if (connector_->active_mode().id() != connector_->get_preferred_mode_id())
499 sel_modes.push_back(connector_->active_mode());
500
501 // Cycle over the modes and filter out "similar" modes, keeping only the
502 // first ones in the order given by DRM (from CEA ids and timings order)
Sean Paulac874152016-03-10 16:00:26 -0500503 for (const DrmMode &mode : connector_->modes()) {
Neil Armstrongb67d0492019-06-20 09:00:21 +0000504 // TODO: Remove this when 3D Attributes are in AOSP
505 if (mode.flags() & DRM_MODE_FLAG_3D_MASK)
506 continue;
507
Neil Armstrong4c027a72019-06-04 14:48:02 +0000508 // TODO: Remove this when the Interlaced attribute is in AOSP
509 if (mode.flags() & DRM_MODE_FLAG_INTERLACE) {
510 auto m = std::find_if(connector_->modes().begin(),
511 connector_->modes().end(),
512 [&mode](DrmMode const &m) {
513 return !(m.flags() & DRM_MODE_FLAG_INTERLACE) &&
514 m.h_display() == mode.h_display() &&
515 m.v_display() == mode.v_display();
516 });
Neil Armstrongb67d0492019-06-20 09:00:21 +0000517 if (m == connector_->modes().end())
518 sel_modes.push_back(mode);
519
520 continue;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000521 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000522
523 // Search for a similar WxH@R mode in the filtered list and drop it if
524 // another mode with the same WxH@R has already been selected
525 // TODO: Remove this when AOSP handles duplicates modes
526 auto m = std::find_if(sel_modes.begin(), sel_modes.end(),
527 [&mode](DrmMode const &m) {
528 return m.h_display() == mode.h_display() &&
529 m.v_display() == mode.v_display() &&
530 m.v_refresh() == mode.v_refresh();
531 });
532 if (m == sel_modes.end())
533 sel_modes.push_back(mode);
534 }
535
536 auto num_modes = static_cast<uint32_t>(sel_modes.size());
537 if (!configs) {
538 *num_configs = num_modes;
539 return HWC2::Error::None;
540 }
541
542 uint32_t idx = 0;
543 for (const DrmMode &mode : sel_modes) {
544 if (idx >= *num_configs)
545 break;
546 configs[idx++] = mode.id();
Sean Paulac874152016-03-10 16:00:26 -0500547 }
548 *num_configs = idx;
549 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500550}
551
552HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500553 supported(__func__);
554 std::ostringstream stream;
555 stream << "display-" << connector_->id();
556 std::string string = stream.str();
557 size_t length = string.length();
558 if (!name) {
559 *size = length;
560 return HWC2::Error::None;
561 }
562
563 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
564 strncpy(name, string.c_str(), *size);
565 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500566}
567
Sean Paulac874152016-03-10 16:00:26 -0500568HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
569 uint32_t *num_elements,
570 hwc2_layer_t *layers,
571 int32_t *layer_requests) {
572 supported(__func__);
573 // TODO: I think virtual display should request
574 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
575 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
576 *num_elements = 0;
577 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500578}
579
580HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500581 supported(__func__);
582 *type = static_cast<int32_t>(type_);
583 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500584}
585
586HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500587 supported(__func__);
588 *support = 0;
589 return HWC2::Error::None;
590}
591
592HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400593 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
594 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500595 supported(__func__);
596 *num_types = 0;
597 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500598}
599
600HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500601 hwc2_layer_t *layers,
602 int32_t *fences) {
603 supported(__func__);
604 uint32_t num_layers = 0;
605
606 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
607 ++num_layers;
608 if (layers == NULL || fences == NULL) {
609 continue;
610 } else if (num_layers > *num_elements) {
611 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
612 return HWC2::Error::None;
613 }
614
615 layers[num_layers - 1] = l.first;
616 fences[num_layers - 1] = l.second.take_release_fence();
617 }
618 *num_elements = num_layers;
619 return HWC2::Error::None;
620}
621
Matteo Franchinc56eede2019-12-03 17:10:38 +0000622void DrmHwcTwo::HwcDisplay::AddFenceToPresentFence(int fd) {
Sean Paulac874152016-03-10 16:00:26 -0500623 if (fd < 0)
624 return;
625
Matteo Franchinc56eede2019-12-03 17:10:38 +0000626 if (present_fence_.get() >= 0) {
627 int old_fence = present_fence_.get();
628 present_fence_.Set(sync_merge("dc_present", old_fence, fd));
629 close(fd);
Sean Paulac874152016-03-10 16:00:26 -0500630 } else {
Matteo Franchinc56eede2019-12-03 17:10:38 +0000631 present_fence_.Set(fd);
Sean Paulac874152016-03-10 16:00:26 -0500632 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500633}
634
Roman Stratiienkoafb36892019-11-08 17:16:11 +0200635bool DrmHwcTwo::HwcDisplay::HardwareSupportsLayerType(
636 HWC2::Composition comp_type) {
637 return comp_type == HWC2::Composition::Device ||
638 comp_type == HWC2::Composition::Cursor;
639}
640
Rob Herring4f6c62e2018-05-17 14:33:02 -0500641HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500642 std::vector<DrmCompositionDisplayLayersMap> layers_map;
643 layers_map.emplace_back();
644 DrmCompositionDisplayLayersMap &map = layers_map.back();
645
646 map.display = static_cast<int>(handle_);
647 map.geometry_changed = true; // TODO: Fix this
648
649 // order the layers by z-order
650 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100651 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500652 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
653 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200654 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500655 case HWC2::Composition::Device:
656 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
657 break;
658 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100659 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500660 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100661 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500662 break;
663 default:
664 continue;
665 }
666 }
667 if (use_client_layer)
668 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
669
Rob Herring4f6c62e2018-05-17 14:33:02 -0500670 if (z_map.empty())
671 return HWC2::Error::BadLayer;
672
Sean Paulac874152016-03-10 16:00:26 -0500673 // now that they're ordered by z, add them to the composition
674 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
675 DrmHwcLayer layer;
676 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200677 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500678 if (ret) {
679 ALOGE("Failed to import layer, ret=%d", ret);
680 return HWC2::Error::NoResources;
681 }
682 map.layers.emplace_back(std::move(layer));
683 }
Sean Paulac874152016-03-10 16:00:26 -0500684
Sean Paulf72cccd2018-08-27 13:59:08 -0400685 std::unique_ptr<DrmDisplayComposition> composition = compositor_
686 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500687 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
688
689 // TODO: Don't always assume geometry changed
690 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
691 if (ret) {
692 ALOGE("Failed to set layers in the composition ret=%d", ret);
693 return HWC2::Error::BadLayer;
694 }
695
696 std::vector<DrmPlane *> primary_planes(primary_planes_);
697 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500698 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500699 if (ret) {
700 ALOGE("Failed to plan the composition ret=%d", ret);
701 return HWC2::Error::BadConfig;
702 }
703
704 // Disable the planes we're not using
705 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
706 composition->AddPlaneDisable(*i);
707 i = primary_planes.erase(i);
708 }
709 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
710 composition->AddPlaneDisable(*i);
711 i = overlay_planes.erase(i);
712 }
713
Rob Herring4f6c62e2018-05-17 14:33:02 -0500714 if (test) {
715 ret = compositor_.TestComposition(composition.get());
716 } else {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500717 ret = compositor_.ApplyComposition(std::move(composition));
Matteo Franchinc56eede2019-12-03 17:10:38 +0000718 AddFenceToPresentFence(compositor_.TakeOutFence());
Rob Herring4f6c62e2018-05-17 14:33:02 -0500719 }
Sean Paulac874152016-03-10 16:00:26 -0500720 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700721 if (!test)
722 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500723 return HWC2::Error::BadParameter;
724 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500725 return HWC2::Error::None;
726}
727
Matteo Franchinc56eede2019-12-03 17:10:38 +0000728HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500729 supported(__func__);
730 HWC2::Error ret;
731
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200732 ++total_stats_.total_frames_;
733
Rob Herring4f6c62e2018-05-17 14:33:02 -0500734 ret = CreateComposition(false);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200735 if (ret != HWC2::Error::None)
736 ++total_stats_.failed_kms_present_;
737
Rob Herring4f6c62e2018-05-17 14:33:02 -0500738 if (ret == HWC2::Error::BadLayer) {
739 // Can we really have no client or device layers?
Matteo Franchinc56eede2019-12-03 17:10:38 +0000740 *present_fence = -1;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500741 return HWC2::Error::None;
742 }
743 if (ret != HWC2::Error::None)
744 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500745
Matteo Franchinc56eede2019-12-03 17:10:38 +0000746 *present_fence = present_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500747
748 ++frame_no_;
749 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500750}
751
752HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500753 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400754 auto mode = std::find_if(connector_->modes().begin(),
755 connector_->modes().end(),
756 [config](DrmMode const &m) {
757 return m.id() == config;
758 });
Sean Paulac874152016-03-10 16:00:26 -0500759 if (mode == connector_->modes().end()) {
760 ALOGE("Could not find active mode for %d", config);
761 return HWC2::Error::BadConfig;
762 }
763
Sean Paulf72cccd2018-08-27 13:59:08 -0400764 std::unique_ptr<DrmDisplayComposition> composition = compositor_
765 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500766 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
767 int ret = composition->SetDisplayMode(*mode);
Sean Pauled45a8e2017-02-28 13:17:34 -0500768 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500769 if (ret) {
770 ALOGE("Failed to queue dpms composition on %d", ret);
771 return HWC2::Error::BadConfig;
772 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300773
774 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500775
776 // Setup the client layer's dimensions
777 hwc_rect_t display_frame = {.left = 0,
778 .top = 0,
779 .right = static_cast<int>(mode->h_display()),
780 .bottom = static_cast<int>(mode->v_display())};
781 client_layer_.SetLayerDisplayFrame(display_frame);
782 hwc_frect_t source_crop = {.left = 0.0f,
783 .top = 0.0f,
784 .right = mode->h_display() + 0.0f,
785 .bottom = mode->v_display() + 0.0f};
786 client_layer_.SetLayerSourceCrop(source_crop);
787
788 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500789}
790
791HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
792 int32_t acquire_fence,
793 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600794 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500795 supported(__func__);
796 UniqueFd uf(acquire_fence);
797
798 client_layer_.set_buffer(target);
799 client_layer_.set_acquire_fence(uf.get());
800 client_layer_.SetLayerDataspace(dataspace);
801 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500802}
803
804HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500805 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800806
807 if (mode != HAL_COLOR_MODE_NATIVE)
Vincent Donnefort7834a892019-10-09 15:53:56 +0100808 return HWC2::Error::BadParameter;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800809
810 color_mode_ = mode;
811 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500812}
813
814HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500815 int32_t hint) {
816 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200817 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
818 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
819 return HWC2::Error::BadParameter;
820
821 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
822 return HWC2::Error::BadParameter;
823
824 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
825 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
826 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
827
828 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500829}
830
831HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500832 int32_t release_fence) {
833 supported(__func__);
834 // TODO: Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500835 return unsupported(__func__, buffer, release_fence);
836}
837
Sean Paulac874152016-03-10 16:00:26 -0500838HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
839 supported(__func__);
840 uint64_t dpms_value = 0;
841 auto mode = static_cast<HWC2::PowerMode>(mode_in);
842 switch (mode) {
843 case HWC2::PowerMode::Off:
844 dpms_value = DRM_MODE_DPMS_OFF;
845 break;
846 case HWC2::PowerMode::On:
847 dpms_value = DRM_MODE_DPMS_ON;
848 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100849 case HWC2::PowerMode::Doze:
850 case HWC2::PowerMode::DozeSuspend:
851 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500852 default:
853 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100854 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500855 };
856
Sean Paulf72cccd2018-08-27 13:59:08 -0400857 std::unique_ptr<DrmDisplayComposition> composition = compositor_
858 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500859 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
860 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500861 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500862 if (ret) {
863 ALOGE("Failed to apply the dpms composition ret=%d", ret);
864 return HWC2::Error::BadParameter;
865 }
866 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500867}
868
869HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500870 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300871 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500872 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500873}
874
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200875uint32_t DrmHwcTwo::HwcDisplay::CalcPixOps(
876 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t first_z,
877 size_t size) {
878 uint32_t pixops = 0;
879 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
880 if (l.first >= first_z && l.first < first_z + size) {
881 hwc_rect_t df = l.second->display_frame();
882 pixops += (df.right - df.left) * (df.bottom - df.top);
883 }
884 }
885 return pixops;
886}
887
888void DrmHwcTwo::HwcDisplay::MarkValidated(
889 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t client_first_z,
890 size_t client_size) {
891 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
892 if (l.first >= client_first_z && l.first < client_first_z + client_size)
893 l.second->set_validated_type(HWC2::Composition::Client);
894 else
895 l.second->set_validated_type(HWC2::Composition::Device);
896 }
897}
898
Sean Pauled2ec4b2016-03-10 15:35:40 -0500899HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500900 uint32_t *num_requests) {
901 supported(__func__);
902 *num_types = 0;
903 *num_requests = 0;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500904 size_t avail_planes = primary_planes_.size() + overlay_planes_.size();
Rob Herring4f6c62e2018-05-17 14:33:02 -0500905
906 /*
907 * If more layers then planes, save one plane
908 * for client composited layers
909 */
910 if (avail_planes < layers_.size())
911 avail_planes--;
912
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200913 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
Roman Stratiienkof2647232019-11-21 01:58:35 +0200914 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
915 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
916
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200917 uint32_t total_pixops = CalcPixOps(z_map, 0, z_map.size()), gpu_pixops = 0;
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200918
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200919 int client_start = -1, client_size = 0;
920
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200921 if (compositor_.ShouldFlattenOnClient()) {
922 client_start = 0;
923 client_size = z_map.size();
924 MarkValidated(z_map, client_start, client_size);
925 } else {
926 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
927 if (!HardwareSupportsLayerType(l.second->sf_type()) ||
928 !importer_->CanImportBuffer(l.second->buffer()) ||
929 color_transform_hint_ != HAL_COLOR_TRANSFORM_IDENTITY ||
930 (l.second->RequireScalingOrPhasing() &&
931 resource_manager_->ForcedScalingWithGpu())) {
932 if (client_start < 0)
933 client_start = l.first;
934 client_size = (l.first - client_start) + 1;
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200935 }
936 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500937
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200938 int extra_client = (z_map.size() - client_size) - avail_planes;
939 if (extra_client > 0) {
940 int start = 0, steps;
941 if (client_size != 0) {
942 int prepend = std::min(client_start, extra_client);
943 int append = std::min(int(z_map.size() - (client_start + client_size)),
944 extra_client);
945 start = client_start - prepend;
946 client_size += extra_client;
947 steps = 1 + std::min(std::min(append, prepend),
948 int(z_map.size()) - (start + client_size));
949 } else {
950 client_size = extra_client;
951 steps = 1 + z_map.size() - extra_client;
952 }
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200953
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200954 gpu_pixops = INT_MAX;
955 for (int i = 0; i < steps; i++) {
956 uint32_t po = CalcPixOps(z_map, start + i, client_size);
957 if (po < gpu_pixops) {
958 gpu_pixops = po;
959 client_start = start + i;
960 }
961 }
962 }
963
964 MarkValidated(z_map, client_start, client_size);
965
966 if (CreateComposition(true) != HWC2::Error::None) {
967 ++total_stats_.failed_kms_validate_;
968 gpu_pixops = total_pixops;
969 client_size = z_map.size();
970 MarkValidated(z_map, 0, client_size);
971 }
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200972 }
973
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200974 *num_types = client_size;
975
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200976 total_stats_.frames_flattened_ = compositor_.GetFlattenedFramesCount();
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200977 total_stats_.gpu_pixops_ += gpu_pixops;
978 total_stats_.total_pixops_ += total_pixops;
Roman Stratiienkof2647232019-11-21 01:58:35 +0200979
Rob Herringee8f45b2017-06-09 15:15:55 -0500980 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500981}
982
John Stultz8c7229d2020-02-07 21:31:08 +0000983#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800984HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
985 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
986 supported(__func__);
987
988 drmModePropertyBlobPtr blob;
989 int ret;
990 uint64_t blob_id;
991
992 std::tie(ret, blob_id) = connector_->edid_property().value();
993 if (ret) {
994 ALOGE("Failed to get edid property value.");
995 return HWC2::Error::Unsupported;
996 }
997
998 blob = drmModeGetPropertyBlob(drm_->fd(), blob_id);
999
Andrii Chepurnyi8115dbe2020-04-14 13:03:57 +03001000 if (outData) {
1001 *outDataSize = std::min(*outDataSize, blob->length);
1002 memcpy(outData, blob->data, *outDataSize);
1003 } else {
1004 *outDataSize = blob->length;
1005 }
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001006 *outPort = connector_->id();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001007
1008 return HWC2::Error::None;
1009}
1010
1011HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
1012 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
1013 unsupported(__func__, outCapabilities);
1014
1015 if (outNumCapabilities == NULL) {
1016 return HWC2::Error::BadParameter;
1017 }
1018
1019 *outNumCapabilities = 0;
1020
1021 return HWC2::Error::None;
1022}
John Stultz8c7229d2020-02-07 21:31:08 +00001023#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001024
Sean Pauled2ec4b2016-03-10 15:35:40 -05001025HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -05001026 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -08001027 cursor_x_ = x;
1028 cursor_y_ = y;
1029 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001030}
1031
1032HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -05001033 supported(__func__);
1034 blending_ = static_cast<HWC2::BlendMode>(mode);
1035 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001036}
1037
1038HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001039 int32_t acquire_fence) {
1040 supported(__func__);
1041 UniqueFd uf(acquire_fence);
1042
1043 // The buffer and acquire_fence are handled elsewhere
1044 if (sf_type_ == HWC2::Composition::Client ||
1045 sf_type_ == HWC2::Composition::Sideband ||
1046 sf_type_ == HWC2::Composition::SolidColor)
1047 return HWC2::Error::None;
1048
1049 set_buffer(buffer);
1050 set_acquire_fence(uf.get());
1051 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001052}
1053
1054HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001055 // TODO: Put to client composition here?
1056 supported(__func__);
1057 layer_color_ = color;
1058 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001059}
1060
1061HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001062 sf_type_ = static_cast<HWC2::Composition>(type);
1063 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001064}
1065
1066HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001067 supported(__func__);
1068 dataspace_ = static_cast<android_dataspace_t>(dataspace);
1069 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001070}
1071
1072HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001073 supported(__func__);
1074 display_frame_ = frame;
1075 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001076}
1077
1078HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001079 supported(__func__);
1080 alpha_ = alpha;
1081 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001082}
1083
1084HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1085 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001086 supported(__func__);
1087 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001088 return unsupported(__func__, stream);
1089}
1090
1091HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001092 supported(__func__);
1093 source_crop_ = crop;
1094 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001095}
1096
1097HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001098 supported(__func__);
1099 // TODO: We don't use surface damage, marking as unsupported
1100 unsupported(__func__, damage);
1101 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001102}
1103
1104HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001105 supported(__func__);
1106 transform_ = static_cast<HWC2::Transform>(transform);
1107 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001108}
1109
1110HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001111 supported(__func__);
1112 // TODO: We don't use this information, marking as unsupported
1113 unsupported(__func__, visible);
1114 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001115}
1116
Sean Paulac874152016-03-10 16:00:26 -05001117HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1118 supported(__func__);
1119 z_order_ = order;
1120 return HWC2::Error::None;
1121}
1122
1123void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1124 supported(__func__);
1125 switch (blending_) {
1126 case HWC2::BlendMode::None:
1127 layer->blending = DrmHwcBlending::kNone;
1128 break;
1129 case HWC2::BlendMode::Premultiplied:
1130 layer->blending = DrmHwcBlending::kPreMult;
1131 break;
1132 case HWC2::BlendMode::Coverage:
1133 layer->blending = DrmHwcBlending::kCoverage;
1134 break;
1135 default:
1136 ALOGE("Unknown blending mode b=%d", blending_);
1137 layer->blending = DrmHwcBlending::kNone;
1138 break;
1139 }
1140
1141 OutputFd release_fence = release_fence_output();
1142
1143 layer->sf_handle = buffer_;
1144 layer->acquire_fence = acquire_fence_.Release();
1145 layer->release_fence = std::move(release_fence);
1146 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +02001147 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -05001148 layer->SetSourceCrop(source_crop_);
1149 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -05001150}
1151
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001152void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
1153 auto cb = callbacks_.find(HWC2::Callback::Hotplug);
1154 if (cb == callbacks_.end())
1155 return;
1156
1157 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(cb->second.func);
1158 hotplug(cb->second.data, displayid,
1159 (state == DRM_MODE_CONNECTED ? HWC2_CONNECTION_CONNECTED
1160 : HWC2_CONNECTION_DISCONNECTED));
1161}
1162
1163void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
1164 for (auto &conn : drmDevice->connectors()) {
1165 if (conn->state() != DRM_MODE_CONNECTED)
1166 continue;
1167 HandleDisplayHotplug(conn->display(), conn->state());
1168 }
1169}
1170
1171void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
1172 for (auto &conn : drm_->connectors()) {
1173 drmModeConnection old_state = conn->state();
1174 drmModeConnection cur_state = conn->UpdateModes()
1175 ? DRM_MODE_UNKNOWNCONNECTION
1176 : conn->state();
1177
1178 if (cur_state == old_state)
1179 continue;
1180
1181 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
1182 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1183 conn->id(), conn->display());
1184
1185 int display_id = conn->display();
1186 if (cur_state == DRM_MODE_CONNECTED) {
1187 auto &display = hwc2_->displays_.at(display_id);
1188 display.ChosePreferredConfig();
1189 } else {
1190 auto &display = hwc2_->displays_.at(display_id);
1191 display.ClearDisplay();
1192 }
1193
1194 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1195 }
1196}
1197
Sean Pauled2ec4b2016-03-10 15:35:40 -05001198// static
1199int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1200 unsupported(__func__);
1201 return 0;
1202}
1203
1204// static
1205void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001206 uint32_t *out_count,
1207 int32_t * /*out_capabilities*/) {
1208 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001209 *out_count = 0;
1210}
1211
1212// static
Sean Paulac874152016-03-10 16:00:26 -05001213hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1214 struct hwc2_device * /*dev*/, int32_t descriptor) {
1215 supported(__func__);
1216 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001217 switch (func) {
1218 // Device functions
1219 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1220 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1221 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1222 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001223 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001224 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1225 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1226 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1227 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1228 case HWC2::FunctionDescriptor::Dump:
1229 return ToHook<HWC2_PFN_DUMP>(
1230 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1231 uint32_t *, char *>);
1232 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1233 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1234 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1235 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1236 case HWC2::FunctionDescriptor::RegisterCallback:
1237 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1238 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1239 &DrmHwcTwo::RegisterCallback, int32_t,
1240 hwc2_callback_data_t, hwc2_function_pointer_t>);
1241
1242 // Display functions
1243 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1244 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1245 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1246 &HwcDisplay::AcceptDisplayChanges>);
1247 case HWC2::FunctionDescriptor::CreateLayer:
1248 return ToHook<HWC2_PFN_CREATE_LAYER>(
1249 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1250 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1251 case HWC2::FunctionDescriptor::DestroyLayer:
1252 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1253 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1254 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1255 case HWC2::FunctionDescriptor::GetActiveConfig:
1256 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1257 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1258 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1259 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1260 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1261 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1262 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1263 hwc2_layer_t *, int32_t *>);
1264 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1265 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1266 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1267 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1268 int32_t, int32_t>);
1269 case HWC2::FunctionDescriptor::GetColorModes:
1270 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1271 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1272 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1273 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001274 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1275 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1276 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1277 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001278 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001279 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1280 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1281 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1282 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001283 case HWC2::FunctionDescriptor::GetDisplayName:
1284 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1285 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1286 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1287 case HWC2::FunctionDescriptor::GetDisplayRequests:
1288 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1289 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1290 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1291 hwc2_layer_t *, int32_t *>);
1292 case HWC2::FunctionDescriptor::GetDisplayType:
1293 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1294 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1295 &HwcDisplay::GetDisplayType, int32_t *>);
1296 case HWC2::FunctionDescriptor::GetDozeSupport:
1297 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1298 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1299 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001300 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1301 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1302 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1303 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1304 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001305 case HWC2::FunctionDescriptor::GetReleaseFences:
1306 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1307 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1308 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1309 int32_t *>);
1310 case HWC2::FunctionDescriptor::PresentDisplay:
1311 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1312 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1313 &HwcDisplay::PresentDisplay, int32_t *>);
1314 case HWC2::FunctionDescriptor::SetActiveConfig:
1315 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1316 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1317 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1318 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001319 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1320 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1321 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1322 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001323 case HWC2::FunctionDescriptor::SetColorMode:
1324 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1325 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1326 &HwcDisplay::SetColorMode, int32_t>);
1327 case HWC2::FunctionDescriptor::SetColorTransform:
1328 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1329 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1330 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1331 case HWC2::FunctionDescriptor::SetOutputBuffer:
1332 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1333 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1334 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1335 case HWC2::FunctionDescriptor::SetPowerMode:
1336 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1337 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1338 &HwcDisplay::SetPowerMode, int32_t>);
1339 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1340 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1341 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1342 &HwcDisplay::SetVsyncEnabled, int32_t>);
1343 case HWC2::FunctionDescriptor::ValidateDisplay:
1344 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1345 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1346 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
John Stultz8c7229d2020-02-07 21:31:08 +00001347#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001348 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1349 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1350 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1351 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1352 uint32_t *, uint8_t *>);
1353 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1354 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1355 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1356 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1357 uint32_t *>);
John Stultz8c7229d2020-02-07 21:31:08 +00001358#endif /* PLATFORM_SDK_VERSION > 28 */
Sean Pauled2ec4b2016-03-10 15:35:40 -05001359 // Layer functions
1360 case HWC2::FunctionDescriptor::SetCursorPosition:
1361 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1362 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1363 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1364 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1365 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1366 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1367 &HwcLayer::SetLayerBlendMode, int32_t>);
1368 case HWC2::FunctionDescriptor::SetLayerBuffer:
1369 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1370 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1371 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1372 case HWC2::FunctionDescriptor::SetLayerColor:
1373 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1374 LayerHook<decltype(&HwcLayer::SetLayerColor),
1375 &HwcLayer::SetLayerColor, hwc_color_t>);
1376 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1377 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1378 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1379 &HwcLayer::SetLayerCompositionType, int32_t>);
1380 case HWC2::FunctionDescriptor::SetLayerDataspace:
1381 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1382 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1383 &HwcLayer::SetLayerDataspace, int32_t>);
1384 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1385 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1386 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1387 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1388 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1389 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1390 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1391 &HwcLayer::SetLayerPlaneAlpha, float>);
1392 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001393 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1394 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1395 &HwcLayer::SetLayerSidebandStream,
1396 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001397 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1398 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1399 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1400 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1401 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1402 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1403 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1404 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1405 case HWC2::FunctionDescriptor::SetLayerTransform:
1406 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1407 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1408 &HwcLayer::SetLayerTransform, int32_t>);
1409 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1410 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1411 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1412 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1413 case HWC2::FunctionDescriptor::SetLayerZOrder:
1414 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1415 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1416 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001417 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001418 default:
1419 return NULL;
1420 }
1421}
Sean Paulac874152016-03-10 16:00:26 -05001422
1423// static
1424int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1425 struct hw_device_t **dev) {
1426 supported(__func__);
1427 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1428 ALOGE("Invalid module name- %s", name);
1429 return -EINVAL;
1430 }
1431
1432 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1433 if (!ctx) {
1434 ALOGE("Failed to allocate DrmHwcTwo");
1435 return -ENOMEM;
1436 }
1437
1438 HWC2::Error err = ctx->Init();
1439 if (err != HWC2::Error::None) {
1440 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1441 return -EINVAL;
1442 }
1443
1444 ctx->common.module = const_cast<hw_module_t *>(module);
1445 *dev = &ctx->common;
1446 ctx.release();
1447 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001448}
Sean Paulf72cccd2018-08-27 13:59:08 -04001449} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001450
1451static struct hw_module_methods_t hwc2_module_methods = {
1452 .open = android::DrmHwcTwo::HookDevOpen,
1453};
1454
1455hw_module_t HAL_MODULE_INFO_SYM = {
1456 .tag = HARDWARE_MODULE_TAG,
1457 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1458 .id = HWC_HARDWARE_MODULE_ID,
1459 .name = "DrmHwcTwo module",
1460 .author = "The Android Open Source Project",
1461 .methods = &hwc2_module_methods,
1462 .dso = NULL,
1463 .reserved = {0},
1464};