blob: 605406b574e59bc6ef57fa6232838e78f466a5ef [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
1000 outData = static_cast<uint8_t *>(blob->data);
1001
1002 *outPort = connector_->id();
1003 *outDataSize = blob->length;
1004
1005 return HWC2::Error::None;
1006}
1007
1008HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
1009 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
1010 unsupported(__func__, outCapabilities);
1011
1012 if (outNumCapabilities == NULL) {
1013 return HWC2::Error::BadParameter;
1014 }
1015
1016 *outNumCapabilities = 0;
1017
1018 return HWC2::Error::None;
1019}
John Stultz8c7229d2020-02-07 21:31:08 +00001020#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001021
Sean Pauled2ec4b2016-03-10 15:35:40 -05001022HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -05001023 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -08001024 cursor_x_ = x;
1025 cursor_y_ = y;
1026 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001027}
1028
1029HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -05001030 supported(__func__);
1031 blending_ = static_cast<HWC2::BlendMode>(mode);
1032 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001033}
1034
1035HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001036 int32_t acquire_fence) {
1037 supported(__func__);
1038 UniqueFd uf(acquire_fence);
1039
1040 // The buffer and acquire_fence are handled elsewhere
1041 if (sf_type_ == HWC2::Composition::Client ||
1042 sf_type_ == HWC2::Composition::Sideband ||
1043 sf_type_ == HWC2::Composition::SolidColor)
1044 return HWC2::Error::None;
1045
1046 set_buffer(buffer);
1047 set_acquire_fence(uf.get());
1048 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001049}
1050
1051HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001052 // TODO: Put to client composition here?
1053 supported(__func__);
1054 layer_color_ = color;
1055 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001056}
1057
1058HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001059 sf_type_ = static_cast<HWC2::Composition>(type);
1060 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001061}
1062
1063HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001064 supported(__func__);
1065 dataspace_ = static_cast<android_dataspace_t>(dataspace);
1066 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001067}
1068
1069HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001070 supported(__func__);
1071 display_frame_ = frame;
1072 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001073}
1074
1075HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001076 supported(__func__);
1077 alpha_ = alpha;
1078 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001079}
1080
1081HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1082 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001083 supported(__func__);
1084 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001085 return unsupported(__func__, stream);
1086}
1087
1088HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001089 supported(__func__);
1090 source_crop_ = crop;
1091 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001092}
1093
1094HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001095 supported(__func__);
1096 // TODO: We don't use surface damage, marking as unsupported
1097 unsupported(__func__, damage);
1098 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001099}
1100
1101HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001102 supported(__func__);
1103 transform_ = static_cast<HWC2::Transform>(transform);
1104 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001105}
1106
1107HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001108 supported(__func__);
1109 // TODO: We don't use this information, marking as unsupported
1110 unsupported(__func__, visible);
1111 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001112}
1113
Sean Paulac874152016-03-10 16:00:26 -05001114HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1115 supported(__func__);
1116 z_order_ = order;
1117 return HWC2::Error::None;
1118}
1119
1120void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1121 supported(__func__);
1122 switch (blending_) {
1123 case HWC2::BlendMode::None:
1124 layer->blending = DrmHwcBlending::kNone;
1125 break;
1126 case HWC2::BlendMode::Premultiplied:
1127 layer->blending = DrmHwcBlending::kPreMult;
1128 break;
1129 case HWC2::BlendMode::Coverage:
1130 layer->blending = DrmHwcBlending::kCoverage;
1131 break;
1132 default:
1133 ALOGE("Unknown blending mode b=%d", blending_);
1134 layer->blending = DrmHwcBlending::kNone;
1135 break;
1136 }
1137
1138 OutputFd release_fence = release_fence_output();
1139
1140 layer->sf_handle = buffer_;
1141 layer->acquire_fence = acquire_fence_.Release();
1142 layer->release_fence = std::move(release_fence);
1143 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +02001144 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -05001145 layer->SetSourceCrop(source_crop_);
1146 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -05001147}
1148
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001149void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
1150 auto cb = callbacks_.find(HWC2::Callback::Hotplug);
1151 if (cb == callbacks_.end())
1152 return;
1153
1154 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(cb->second.func);
1155 hotplug(cb->second.data, displayid,
1156 (state == DRM_MODE_CONNECTED ? HWC2_CONNECTION_CONNECTED
1157 : HWC2_CONNECTION_DISCONNECTED));
1158}
1159
1160void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
1161 for (auto &conn : drmDevice->connectors()) {
1162 if (conn->state() != DRM_MODE_CONNECTED)
1163 continue;
1164 HandleDisplayHotplug(conn->display(), conn->state());
1165 }
1166}
1167
1168void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
1169 for (auto &conn : drm_->connectors()) {
1170 drmModeConnection old_state = conn->state();
1171 drmModeConnection cur_state = conn->UpdateModes()
1172 ? DRM_MODE_UNKNOWNCONNECTION
1173 : conn->state();
1174
1175 if (cur_state == old_state)
1176 continue;
1177
1178 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
1179 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1180 conn->id(), conn->display());
1181
1182 int display_id = conn->display();
1183 if (cur_state == DRM_MODE_CONNECTED) {
1184 auto &display = hwc2_->displays_.at(display_id);
1185 display.ChosePreferredConfig();
1186 } else {
1187 auto &display = hwc2_->displays_.at(display_id);
1188 display.ClearDisplay();
1189 }
1190
1191 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1192 }
1193}
1194
Sean Pauled2ec4b2016-03-10 15:35:40 -05001195// static
1196int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1197 unsupported(__func__);
1198 return 0;
1199}
1200
1201// static
1202void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001203 uint32_t *out_count,
1204 int32_t * /*out_capabilities*/) {
1205 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001206 *out_count = 0;
1207}
1208
1209// static
Sean Paulac874152016-03-10 16:00:26 -05001210hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1211 struct hwc2_device * /*dev*/, int32_t descriptor) {
1212 supported(__func__);
1213 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001214 switch (func) {
1215 // Device functions
1216 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1217 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1218 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1219 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001220 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001221 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1222 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1223 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1224 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1225 case HWC2::FunctionDescriptor::Dump:
1226 return ToHook<HWC2_PFN_DUMP>(
1227 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1228 uint32_t *, char *>);
1229 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1230 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1231 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1232 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1233 case HWC2::FunctionDescriptor::RegisterCallback:
1234 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1235 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1236 &DrmHwcTwo::RegisterCallback, int32_t,
1237 hwc2_callback_data_t, hwc2_function_pointer_t>);
1238
1239 // Display functions
1240 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1241 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1242 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1243 &HwcDisplay::AcceptDisplayChanges>);
1244 case HWC2::FunctionDescriptor::CreateLayer:
1245 return ToHook<HWC2_PFN_CREATE_LAYER>(
1246 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1247 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1248 case HWC2::FunctionDescriptor::DestroyLayer:
1249 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1250 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1251 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1252 case HWC2::FunctionDescriptor::GetActiveConfig:
1253 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1254 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1255 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1256 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1257 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1258 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1259 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1260 hwc2_layer_t *, int32_t *>);
1261 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1262 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1263 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1264 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1265 int32_t, int32_t>);
1266 case HWC2::FunctionDescriptor::GetColorModes:
1267 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1268 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1269 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1270 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001271 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1272 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1273 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1274 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001275 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001276 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1277 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1278 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1279 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001280 case HWC2::FunctionDescriptor::GetDisplayName:
1281 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1282 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1283 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1284 case HWC2::FunctionDescriptor::GetDisplayRequests:
1285 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1286 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1287 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1288 hwc2_layer_t *, int32_t *>);
1289 case HWC2::FunctionDescriptor::GetDisplayType:
1290 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1291 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1292 &HwcDisplay::GetDisplayType, int32_t *>);
1293 case HWC2::FunctionDescriptor::GetDozeSupport:
1294 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1295 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1296 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001297 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1298 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1299 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1300 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1301 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001302 case HWC2::FunctionDescriptor::GetReleaseFences:
1303 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1304 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1305 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1306 int32_t *>);
1307 case HWC2::FunctionDescriptor::PresentDisplay:
1308 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1309 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1310 &HwcDisplay::PresentDisplay, int32_t *>);
1311 case HWC2::FunctionDescriptor::SetActiveConfig:
1312 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1313 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1314 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1315 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001316 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1317 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1318 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1319 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001320 case HWC2::FunctionDescriptor::SetColorMode:
1321 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1322 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1323 &HwcDisplay::SetColorMode, int32_t>);
1324 case HWC2::FunctionDescriptor::SetColorTransform:
1325 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1326 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1327 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1328 case HWC2::FunctionDescriptor::SetOutputBuffer:
1329 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1330 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1331 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1332 case HWC2::FunctionDescriptor::SetPowerMode:
1333 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1334 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1335 &HwcDisplay::SetPowerMode, int32_t>);
1336 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1337 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1338 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1339 &HwcDisplay::SetVsyncEnabled, int32_t>);
1340 case HWC2::FunctionDescriptor::ValidateDisplay:
1341 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1342 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1343 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
John Stultz8c7229d2020-02-07 21:31:08 +00001344#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001345 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1346 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1347 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1348 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1349 uint32_t *, uint8_t *>);
1350 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1351 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1352 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1353 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1354 uint32_t *>);
John Stultz8c7229d2020-02-07 21:31:08 +00001355#endif /* PLATFORM_SDK_VERSION > 28 */
Sean Pauled2ec4b2016-03-10 15:35:40 -05001356 // Layer functions
1357 case HWC2::FunctionDescriptor::SetCursorPosition:
1358 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1359 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1360 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1361 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1362 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1363 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1364 &HwcLayer::SetLayerBlendMode, int32_t>);
1365 case HWC2::FunctionDescriptor::SetLayerBuffer:
1366 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1367 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1368 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1369 case HWC2::FunctionDescriptor::SetLayerColor:
1370 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1371 LayerHook<decltype(&HwcLayer::SetLayerColor),
1372 &HwcLayer::SetLayerColor, hwc_color_t>);
1373 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1374 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1375 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1376 &HwcLayer::SetLayerCompositionType, int32_t>);
1377 case HWC2::FunctionDescriptor::SetLayerDataspace:
1378 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1379 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1380 &HwcLayer::SetLayerDataspace, int32_t>);
1381 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1382 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1383 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1384 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1385 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1386 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1387 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1388 &HwcLayer::SetLayerPlaneAlpha, float>);
1389 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001390 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1391 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1392 &HwcLayer::SetLayerSidebandStream,
1393 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001394 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1395 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1396 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1397 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1398 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1399 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1400 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1401 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1402 case HWC2::FunctionDescriptor::SetLayerTransform:
1403 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1404 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1405 &HwcLayer::SetLayerTransform, int32_t>);
1406 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1407 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1408 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1409 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1410 case HWC2::FunctionDescriptor::SetLayerZOrder:
1411 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1412 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1413 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001414 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001415 default:
1416 return NULL;
1417 }
1418}
Sean Paulac874152016-03-10 16:00:26 -05001419
1420// static
1421int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1422 struct hw_device_t **dev) {
1423 supported(__func__);
1424 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1425 ALOGE("Invalid module name- %s", name);
1426 return -EINVAL;
1427 }
1428
1429 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1430 if (!ctx) {
1431 ALOGE("Failed to allocate DrmHwcTwo");
1432 return -ENOMEM;
1433 }
1434
1435 HWC2::Error err = ctx->Init();
1436 if (err != HWC2::Error::None) {
1437 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1438 return -EINVAL;
1439 }
1440
1441 ctx->common.module = const_cast<hw_module_t *>(module);
1442 *dev = &ctx->common;
1443 ctx.release();
1444 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001445}
Sean Paulf72cccd2018-08-27 13:59:08 -04001446} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001447
1448static struct hw_module_methods_t hwc2_module_methods = {
1449 .open = android::DrmHwcTwo::HookDevOpen,
1450};
1451
1452hw_module_t HAL_MODULE_INFO_SYM = {
1453 .tag = HARDWARE_MODULE_TAG,
1454 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1455 .id = HWC_HARDWARE_MODULE_ID,
1456 .name = "DrmHwcTwo module",
1457 .author = "The Android Open Source Project",
1458 .methods = &hwc2_module_methods,
1459 .dso = NULL,
1460 .reserved = {0},
1461};