blob: ae9d5da233eddbf92e4a9890295a06b9cfaaaf6e [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
Liviu Dudaueb012292020-06-15 17:08:33 +0100913 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map, z_map_tmp;
914 uint32_t z_index = 0;
915 // First create a map of layers and z_order values
Roman Stratiienkof2647232019-11-21 01:58:35 +0200916 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
Liviu Dudaueb012292020-06-15 17:08:33 +0100917 z_map_tmp.emplace(std::make_pair(l.second.z_order(), &l.second));
918 // normalise the map so that the lowest z_order layer has key 0
919 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map_tmp)
920 z_map.emplace(std::make_pair(z_index++, l.second));
Roman Stratiienkof2647232019-11-21 01:58:35 +0200921
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200922 uint32_t total_pixops = CalcPixOps(z_map, 0, z_map.size()), gpu_pixops = 0;
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200923
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200924 int client_start = -1, client_size = 0;
925
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200926 if (compositor_.ShouldFlattenOnClient()) {
927 client_start = 0;
928 client_size = z_map.size();
929 MarkValidated(z_map, client_start, client_size);
930 } else {
931 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
932 if (!HardwareSupportsLayerType(l.second->sf_type()) ||
933 !importer_->CanImportBuffer(l.second->buffer()) ||
934 color_transform_hint_ != HAL_COLOR_TRANSFORM_IDENTITY ||
935 (l.second->RequireScalingOrPhasing() &&
936 resource_manager_->ForcedScalingWithGpu())) {
937 if (client_start < 0)
938 client_start = l.first;
939 client_size = (l.first - client_start) + 1;
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200940 }
941 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500942
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200943 int extra_client = (z_map.size() - client_size) - avail_planes;
944 if (extra_client > 0) {
945 int start = 0, steps;
946 if (client_size != 0) {
947 int prepend = std::min(client_start, extra_client);
948 int append = std::min(int(z_map.size() - (client_start + client_size)),
949 extra_client);
950 start = client_start - prepend;
951 client_size += extra_client;
952 steps = 1 + std::min(std::min(append, prepend),
953 int(z_map.size()) - (start + client_size));
954 } else {
955 client_size = extra_client;
956 steps = 1 + z_map.size() - extra_client;
957 }
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200958
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200959 gpu_pixops = INT_MAX;
960 for (int i = 0; i < steps; i++) {
961 uint32_t po = CalcPixOps(z_map, start + i, client_size);
962 if (po < gpu_pixops) {
963 gpu_pixops = po;
964 client_start = start + i;
965 }
966 }
967 }
968
969 MarkValidated(z_map, client_start, client_size);
970
Matvii Zorinfdcdeab2020-04-06 19:03:03 +0300971 bool testing_needed = !(client_start == 0 && client_size == z_map.size());
972
973 if (testing_needed && CreateComposition(true) != HWC2::Error::None) {
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200974 ++total_stats_.failed_kms_validate_;
975 gpu_pixops = total_pixops;
976 client_size = z_map.size();
977 MarkValidated(z_map, 0, client_size);
978 }
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200979 }
980
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200981 *num_types = client_size;
982
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200983 total_stats_.frames_flattened_ = compositor_.GetFlattenedFramesCount();
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200984 total_stats_.gpu_pixops_ += gpu_pixops;
985 total_stats_.total_pixops_ += total_pixops;
Roman Stratiienkof2647232019-11-21 01:58:35 +0200986
Rob Herringee8f45b2017-06-09 15:15:55 -0500987 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500988}
989
John Stultz8c7229d2020-02-07 21:31:08 +0000990#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800991HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
992 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
993 supported(__func__);
994
995 drmModePropertyBlobPtr blob;
996 int ret;
997 uint64_t blob_id;
998
999 std::tie(ret, blob_id) = connector_->edid_property().value();
1000 if (ret) {
1001 ALOGE("Failed to get edid property value.");
1002 return HWC2::Error::Unsupported;
1003 }
1004
1005 blob = drmModeGetPropertyBlob(drm_->fd(), blob_id);
1006
Andrii Chepurnyi8115dbe2020-04-14 13:03:57 +03001007 if (outData) {
1008 *outDataSize = std::min(*outDataSize, blob->length);
1009 memcpy(outData, blob->data, *outDataSize);
1010 } else {
1011 *outDataSize = blob->length;
1012 }
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001013 *outPort = connector_->id();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001014
1015 return HWC2::Error::None;
1016}
1017
1018HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
1019 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
1020 unsupported(__func__, outCapabilities);
1021
1022 if (outNumCapabilities == NULL) {
1023 return HWC2::Error::BadParameter;
1024 }
1025
1026 *outNumCapabilities = 0;
1027
1028 return HWC2::Error::None;
1029}
John Stultz8c7229d2020-02-07 21:31:08 +00001030#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001031
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001032#if PLATFORM_SDK_VERSION > 27
1033
1034HWC2::Error DrmHwcTwo::HwcDisplay::GetRenderIntents(
1035 int32_t mode, uint32_t *outNumIntents,
1036 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1037 if (mode != HAL_COLOR_MODE_NATIVE) {
1038 return HWC2::Error::BadParameter;
1039 }
1040
1041 if (outIntents == nullptr) {
1042 *outNumIntents = 1;
1043 return HWC2::Error::None;
1044 }
1045 *outNumIntents = 1;
1046 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1047 return HWC2::Error::None;
1048}
1049
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001050HWC2::Error DrmHwcTwo::HwcDisplay::SetColorModeWithIntent(int32_t mode,
1051 int32_t intent) {
1052 if (mode != HAL_COLOR_MODE_NATIVE)
1053 return HWC2::Error::BadParameter;
1054 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1055 return HWC2::Error::BadParameter;
1056 color_mode_ = mode;
1057 return HWC2::Error::None;
1058}
1059
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001060#endif /* PLATFORM_SDK_VERSION > 27 */
1061
Sean Pauled2ec4b2016-03-10 15:35:40 -05001062HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -05001063 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -08001064 cursor_x_ = x;
1065 cursor_y_ = y;
1066 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001067}
1068
1069HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -05001070 supported(__func__);
1071 blending_ = static_cast<HWC2::BlendMode>(mode);
1072 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001073}
1074
1075HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001076 int32_t acquire_fence) {
1077 supported(__func__);
1078 UniqueFd uf(acquire_fence);
1079
Sean Paulac874152016-03-10 16:00:26 -05001080 set_buffer(buffer);
1081 set_acquire_fence(uf.get());
1082 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001083}
1084
1085HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001086 // TODO: Put to client composition here?
1087 supported(__func__);
1088 layer_color_ = color;
1089 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001090}
1091
1092HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001093 sf_type_ = static_cast<HWC2::Composition>(type);
1094 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001095}
1096
1097HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001098 supported(__func__);
1099 dataspace_ = static_cast<android_dataspace_t>(dataspace);
1100 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001101}
1102
1103HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001104 supported(__func__);
1105 display_frame_ = frame;
1106 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001107}
1108
1109HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001110 supported(__func__);
1111 alpha_ = alpha;
1112 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001113}
1114
1115HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1116 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001117 supported(__func__);
1118 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001119 return unsupported(__func__, stream);
1120}
1121
1122HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001123 supported(__func__);
1124 source_crop_ = crop;
1125 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001126}
1127
1128HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001129 supported(__func__);
1130 // TODO: We don't use surface damage, marking as unsupported
1131 unsupported(__func__, damage);
1132 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001133}
1134
1135HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001136 supported(__func__);
1137 transform_ = static_cast<HWC2::Transform>(transform);
1138 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001139}
1140
1141HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001142 supported(__func__);
1143 // TODO: We don't use this information, marking as unsupported
1144 unsupported(__func__, visible);
1145 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001146}
1147
Sean Paulac874152016-03-10 16:00:26 -05001148HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1149 supported(__func__);
1150 z_order_ = order;
1151 return HWC2::Error::None;
1152}
1153
1154void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1155 supported(__func__);
1156 switch (blending_) {
1157 case HWC2::BlendMode::None:
1158 layer->blending = DrmHwcBlending::kNone;
1159 break;
1160 case HWC2::BlendMode::Premultiplied:
1161 layer->blending = DrmHwcBlending::kPreMult;
1162 break;
1163 case HWC2::BlendMode::Coverage:
1164 layer->blending = DrmHwcBlending::kCoverage;
1165 break;
1166 default:
1167 ALOGE("Unknown blending mode b=%d", blending_);
1168 layer->blending = DrmHwcBlending::kNone;
1169 break;
1170 }
1171
1172 OutputFd release_fence = release_fence_output();
1173
1174 layer->sf_handle = buffer_;
1175 layer->acquire_fence = acquire_fence_.Release();
1176 layer->release_fence = std::move(release_fence);
1177 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +02001178 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -05001179 layer->SetSourceCrop(source_crop_);
1180 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -05001181}
1182
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001183void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
1184 auto cb = callbacks_.find(HWC2::Callback::Hotplug);
1185 if (cb == callbacks_.end())
1186 return;
1187
1188 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(cb->second.func);
1189 hotplug(cb->second.data, displayid,
1190 (state == DRM_MODE_CONNECTED ? HWC2_CONNECTION_CONNECTED
1191 : HWC2_CONNECTION_DISCONNECTED));
1192}
1193
1194void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
1195 for (auto &conn : drmDevice->connectors()) {
1196 if (conn->state() != DRM_MODE_CONNECTED)
1197 continue;
1198 HandleDisplayHotplug(conn->display(), conn->state());
1199 }
1200}
1201
1202void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
1203 for (auto &conn : drm_->connectors()) {
1204 drmModeConnection old_state = conn->state();
1205 drmModeConnection cur_state = conn->UpdateModes()
1206 ? DRM_MODE_UNKNOWNCONNECTION
1207 : conn->state();
1208
1209 if (cur_state == old_state)
1210 continue;
1211
1212 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
1213 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1214 conn->id(), conn->display());
1215
1216 int display_id = conn->display();
1217 if (cur_state == DRM_MODE_CONNECTED) {
1218 auto &display = hwc2_->displays_.at(display_id);
1219 display.ChosePreferredConfig();
1220 } else {
1221 auto &display = hwc2_->displays_.at(display_id);
1222 display.ClearDisplay();
1223 }
1224
1225 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1226 }
1227}
1228
Sean Pauled2ec4b2016-03-10 15:35:40 -05001229// static
1230int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1231 unsupported(__func__);
1232 return 0;
1233}
1234
1235// static
1236void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001237 uint32_t *out_count,
1238 int32_t * /*out_capabilities*/) {
1239 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001240 *out_count = 0;
1241}
1242
1243// static
Sean Paulac874152016-03-10 16:00:26 -05001244hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1245 struct hwc2_device * /*dev*/, int32_t descriptor) {
1246 supported(__func__);
1247 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001248 switch (func) {
1249 // Device functions
1250 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1251 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1252 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1253 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001254 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001255 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1256 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1257 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1258 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1259 case HWC2::FunctionDescriptor::Dump:
1260 return ToHook<HWC2_PFN_DUMP>(
1261 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1262 uint32_t *, char *>);
1263 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1264 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1265 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1266 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1267 case HWC2::FunctionDescriptor::RegisterCallback:
1268 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1269 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1270 &DrmHwcTwo::RegisterCallback, int32_t,
1271 hwc2_callback_data_t, hwc2_function_pointer_t>);
1272
1273 // Display functions
1274 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1275 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1276 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1277 &HwcDisplay::AcceptDisplayChanges>);
1278 case HWC2::FunctionDescriptor::CreateLayer:
1279 return ToHook<HWC2_PFN_CREATE_LAYER>(
1280 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1281 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1282 case HWC2::FunctionDescriptor::DestroyLayer:
1283 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1284 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1285 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1286 case HWC2::FunctionDescriptor::GetActiveConfig:
1287 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1288 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1289 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1290 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1291 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1292 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1293 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1294 hwc2_layer_t *, int32_t *>);
1295 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1296 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1297 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1298 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1299 int32_t, int32_t>);
1300 case HWC2::FunctionDescriptor::GetColorModes:
1301 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1302 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1303 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1304 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001305 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1306 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1307 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1308 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001309 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001310 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1311 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1312 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1313 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001314 case HWC2::FunctionDescriptor::GetDisplayName:
1315 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1316 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1317 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1318 case HWC2::FunctionDescriptor::GetDisplayRequests:
1319 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1320 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1321 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1322 hwc2_layer_t *, int32_t *>);
1323 case HWC2::FunctionDescriptor::GetDisplayType:
1324 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1325 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1326 &HwcDisplay::GetDisplayType, int32_t *>);
1327 case HWC2::FunctionDescriptor::GetDozeSupport:
1328 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1329 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1330 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001331 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1332 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1333 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1334 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1335 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001336 case HWC2::FunctionDescriptor::GetReleaseFences:
1337 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1338 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1339 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1340 int32_t *>);
1341 case HWC2::FunctionDescriptor::PresentDisplay:
1342 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1343 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1344 &HwcDisplay::PresentDisplay, int32_t *>);
1345 case HWC2::FunctionDescriptor::SetActiveConfig:
1346 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1347 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1348 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1349 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001350 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1351 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1352 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1353 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001354 case HWC2::FunctionDescriptor::SetColorMode:
1355 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1356 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1357 &HwcDisplay::SetColorMode, int32_t>);
1358 case HWC2::FunctionDescriptor::SetColorTransform:
1359 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1360 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1361 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1362 case HWC2::FunctionDescriptor::SetOutputBuffer:
1363 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1364 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1365 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1366 case HWC2::FunctionDescriptor::SetPowerMode:
1367 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1368 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1369 &HwcDisplay::SetPowerMode, int32_t>);
1370 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1371 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1372 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1373 &HwcDisplay::SetVsyncEnabled, int32_t>);
1374 case HWC2::FunctionDescriptor::ValidateDisplay:
1375 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1376 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1377 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001378#if PLATFORM_SDK_VERSION > 27
1379 case HWC2::FunctionDescriptor::GetRenderIntents:
1380 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
1381 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
1382 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
1383 int32_t *>);
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001384 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
1385 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
1386 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
1387 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001388#endif
John Stultz8c7229d2020-02-07 21:31:08 +00001389#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001390 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1391 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1392 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1393 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1394 uint32_t *, uint8_t *>);
1395 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1396 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1397 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1398 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1399 uint32_t *>);
John Stultz8c7229d2020-02-07 21:31:08 +00001400#endif /* PLATFORM_SDK_VERSION > 28 */
Sean Pauled2ec4b2016-03-10 15:35:40 -05001401 // Layer functions
1402 case HWC2::FunctionDescriptor::SetCursorPosition:
1403 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1404 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1405 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1406 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1407 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1408 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1409 &HwcLayer::SetLayerBlendMode, int32_t>);
1410 case HWC2::FunctionDescriptor::SetLayerBuffer:
1411 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1412 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1413 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1414 case HWC2::FunctionDescriptor::SetLayerColor:
1415 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1416 LayerHook<decltype(&HwcLayer::SetLayerColor),
1417 &HwcLayer::SetLayerColor, hwc_color_t>);
1418 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1419 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1420 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1421 &HwcLayer::SetLayerCompositionType, int32_t>);
1422 case HWC2::FunctionDescriptor::SetLayerDataspace:
1423 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1424 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1425 &HwcLayer::SetLayerDataspace, int32_t>);
1426 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1427 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1428 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1429 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1430 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1431 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1432 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1433 &HwcLayer::SetLayerPlaneAlpha, float>);
1434 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001435 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1436 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1437 &HwcLayer::SetLayerSidebandStream,
1438 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001439 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1440 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1441 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1442 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1443 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1444 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1445 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1446 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1447 case HWC2::FunctionDescriptor::SetLayerTransform:
1448 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1449 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1450 &HwcLayer::SetLayerTransform, int32_t>);
1451 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1452 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1453 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1454 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1455 case HWC2::FunctionDescriptor::SetLayerZOrder:
1456 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1457 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1458 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001459 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001460 default:
1461 return NULL;
1462 }
1463}
Sean Paulac874152016-03-10 16:00:26 -05001464
1465// static
1466int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1467 struct hw_device_t **dev) {
1468 supported(__func__);
1469 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1470 ALOGE("Invalid module name- %s", name);
1471 return -EINVAL;
1472 }
1473
1474 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1475 if (!ctx) {
1476 ALOGE("Failed to allocate DrmHwcTwo");
1477 return -ENOMEM;
1478 }
1479
1480 HWC2::Error err = ctx->Init();
1481 if (err != HWC2::Error::None) {
1482 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1483 return -EINVAL;
1484 }
1485
1486 ctx->common.module = const_cast<hw_module_t *>(module);
1487 *dev = &ctx->common;
1488 ctx.release();
1489 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001490}
Sean Paulf72cccd2018-08-27 13:59:08 -04001491} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001492
1493static struct hw_module_methods_t hwc2_module_methods = {
1494 .open = android::DrmHwcTwo::HookDevOpen,
1495};
1496
1497hw_module_t HAL_MODULE_INFO_SYM = {
1498 .tag = HARDWARE_MODULE_TAG,
1499 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1500 .id = HWC_HARDWARE_MODULE_ID,
1501 .name = "DrmHwcTwo module",
1502 .author = "The Android Open Source Project",
1503 .methods = &hwc2_module_methods,
1504 .dso = NULL,
1505 .reserved = {0},
1506};