blob: 33ad0fbfcf5ae6ec532606303544dc7352347321 [file] [log] [blame]
Sean Pauled2ec4b2016-03-10 15:35:40 -05001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18#define LOG_TAG "hwc-drm-two"
19
Sean Paulf72cccd2018-08-27 13:59:08 -040020#include "drmhwctwo.h"
Sean Paulac874152016-03-10 16:00:26 -050021#include "drmdisplaycomposition.h"
22#include "drmhwcomposer.h"
Sean Paulac874152016-03-10 16:00:26 -050023#include "platform.h"
24#include "vsyncworker.h"
25
26#include <inttypes.h>
27#include <string>
Sean Pauled2ec4b2016-03-10 15:35:40 -050028
Sean Paulac874152016-03-10 16:00:26 -050029#include <cutils/properties.h>
30#include <hardware/hardware.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050031#include <hardware/hwcomposer2.h>
Sean Paulf72cccd2018-08-27 13:59:08 -040032#include <log/log.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050033
34namespace android {
35
Sean Paulac874152016-03-10 16:00:26 -050036class DrmVsyncCallback : public VsyncCallback {
37 public:
38 DrmVsyncCallback(hwc2_callback_data_t data, hwc2_function_pointer_t hook)
39 : data_(data), hook_(hook) {
40 }
41
42 void Callback(int display, int64_t timestamp) {
43 auto hook = reinterpret_cast<HWC2_PFN_VSYNC>(hook_);
44 hook(data_, display, timestamp);
45 }
46
47 private:
48 hwc2_callback_data_t data_;
49 hwc2_function_pointer_t hook_;
50};
51
Sean Pauled2ec4b2016-03-10 15:35:40 -050052DrmHwcTwo::DrmHwcTwo() {
Sean Paulac874152016-03-10 16:00:26 -050053 common.tag = HARDWARE_DEVICE_TAG;
54 common.version = HWC_DEVICE_API_VERSION_2_0;
Sean Pauled2ec4b2016-03-10 15:35:40 -050055 common.close = HookDevClose;
56 getCapabilities = HookDevGetCapabilities;
57 getFunction = HookDevGetFunction;
58}
59
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030060HWC2::Error DrmHwcTwo::CreateDisplay(hwc2_display_t displ,
61 HWC2::DisplayType type) {
62 DrmDevice *drm = resource_manager_.GetDrmDevice(displ);
63 std::shared_ptr<Importer> importer = resource_manager_.GetImporter(displ);
Alexandru Gheorghec5463582018-03-27 15:52:02 +010064 if (!drm || !importer) {
65 ALOGE("Failed to get a valid drmresource and importer");
Sean Paulac874152016-03-10 16:00:26 -050066 return HWC2::Error::NoResources;
67 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030068 displays_.emplace(std::piecewise_construct, std::forward_as_tuple(displ),
Sean Paulf72cccd2018-08-27 13:59:08 -040069 std::forward_as_tuple(&resource_manager_, drm, importer,
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030070 displ, type));
Sean Paulac874152016-03-10 16:00:26 -050071
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030072 DrmCrtc *crtc = drm->GetCrtcForDisplay(static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050073 if (!crtc) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030074 ALOGE("Failed to get crtc for display %d", static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050075 return HWC2::Error::BadDisplay;
76 }
Sean Paulac874152016-03-10 16:00:26 -050077 std::vector<DrmPlane *> display_planes;
Alexandru Gheorghec5463582018-03-27 15:52:02 +010078 for (auto &plane : drm->planes()) {
Sean Paulac874152016-03-10 16:00:26 -050079 if (plane->GetCrtcSupported(*crtc))
80 display_planes.push_back(plane.get());
81 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030082 displays_.at(displ).Init(&display_planes);
Sean Paulac874152016-03-10 16:00:26 -050083 return HWC2::Error::None;
84}
85
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030086HWC2::Error DrmHwcTwo::Init() {
87 int rv = resource_manager_.Init();
88 if (rv) {
89 ALOGE("Can't initialize the resource manager %d", rv);
90 return HWC2::Error::NoResources;
91 }
92
93 HWC2::Error ret = HWC2::Error::None;
94 for (int i = 0; i < resource_manager_.getDisplayCount(); i++) {
95 ret = CreateDisplay(i, HWC2::DisplayType::Physical);
96 if (ret != HWC2::Error::None) {
97 ALOGE("Failed to create display %d with error %d", i, ret);
98 return ret;
99 }
100 }
101
102 auto &drmDevices = resource_manager_.getDrmDevices();
103 for (auto &device : drmDevices) {
104 device->RegisterHotplugHandler(new DrmHotplugHandler(this, device.get()));
105 }
106 return ret;
107}
108
Sean Pauled2ec4b2016-03-10 15:35:40 -0500109template <typename... Args>
110static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
111 ALOGV("Unsupported function: %s", func);
112 return HWC2::Error::Unsupported;
113}
114
Sean Paulac874152016-03-10 16:00:26 -0500115static inline void supported(char const *func) {
116 ALOGV("Supported function: %s", func);
117}
118
Sean Pauled2ec4b2016-03-10 15:35:40 -0500119HWC2::Error DrmHwcTwo::CreateVirtualDisplay(uint32_t width, uint32_t height,
120 int32_t *format,
121 hwc2_display_t *display) {
122 // TODO: Implement virtual display
Sean Paulac874152016-03-10 16:00:26 -0500123 return unsupported(__func__, width, height, format, display);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500124}
125
126HWC2::Error DrmHwcTwo::DestroyVirtualDisplay(hwc2_display_t display) {
Sean Paulac874152016-03-10 16:00:26 -0500127 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500128 return unsupported(__func__, display);
129}
130
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200131std::string DrmHwcTwo::HwcDisplay::DumpDelta(
132 DrmHwcTwo::HwcDisplay::Stats delta) {
133 if (delta.total_pixops_ == 0)
134 return "No stats yet";
135 double Ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
136
137 return (std::stringstream()
138 << " Total frames count: " << delta.total_frames_ << "\n"
139 << " Failed to test commit frames: " << delta.failed_kms_validate_
140 << "\n"
141 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
142 << ((delta.failed_kms_present_ > 0)
143 ? " !!! Internal failure, FIX it please\n"
144 : "")
145 << " Pixel operations (free units)"
146 << " : [TOTAL: " << delta.total_pixops_
147 << " / GPU: " << delta.gpu_pixops_ << "]\n"
148 << " Composition efficiency: " << Ratio)
149 .str();
150}
151
152std::string DrmHwcTwo::HwcDisplay::Dump() {
153 auto out = (std::stringstream()
154 << "- Display on: " << connector_->name() << "\n"
155 << "Statistics since system boot:\n"
156 << DumpDelta(total_stats_) << "\n\n"
157 << "Statistics since last dumpsys request:\n"
158 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n")
159 .str();
160
161 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
162 return out;
163}
164
165void DrmHwcTwo::Dump(uint32_t *outSize, char *outBuffer) {
166 supported(__func__);
167
168 if (outBuffer != nullptr) {
169 auto copiedBytes = mDumpString.copy(outBuffer, *outSize);
170 *outSize = static_cast<uint32_t>(copiedBytes);
171 return;
172 }
173
174 std::stringstream output;
175
176 output << "-- drm_hwcomposer --\n\n";
177
178 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &dp : displays_)
179 output << dp.second.Dump();
180
181 mDumpString = output.str();
182 *outSize = static_cast<uint32_t>(mDumpString.size());
Sean Pauled2ec4b2016-03-10 15:35:40 -0500183}
184
185uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() {
Sean Paulac874152016-03-10 16:00:26 -0500186 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500187 unsupported(__func__);
188 return 0;
189}
190
191HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor,
Sean Paulac874152016-03-10 16:00:26 -0500192 hwc2_callback_data_t data,
193 hwc2_function_pointer_t function) {
194 supported(__func__);
195 auto callback = static_cast<HWC2::Callback>(descriptor);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300196
197 if (!function) {
198 callbacks_.erase(callback);
199 return HWC2::Error::None;
200 }
201
Sean Paulac874152016-03-10 16:00:26 -0500202 callbacks_.emplace(callback, HwcCallback(data, function));
203
204 switch (callback) {
205 case HWC2::Callback::Hotplug: {
206 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(function);
207 hotplug(data, HWC_DISPLAY_PRIMARY,
208 static_cast<int32_t>(HWC2::Connection::Connected));
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 }
214 case HWC2::Callback::Vsync: {
215 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
216 displays_)
217 d.second.RegisterVsyncCallback(data, function);
218 break;
219 }
220 default:
221 break;
222 }
223 return HWC2::Error::None;
224}
225
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100226DrmHwcTwo::HwcDisplay::HwcDisplay(ResourceManager *resource_manager,
227 DrmDevice *drm,
Sean Paulac874152016-03-10 16:00:26 -0500228 std::shared_ptr<Importer> importer,
Sean Paulac874152016-03-10 16:00:26 -0500229 hwc2_display_t handle, HWC2::DisplayType type)
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100230 : resource_manager_(resource_manager),
231 drm_(drm),
232 importer_(importer),
233 handle_(handle),
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200234 type_(type),
235 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
Sean Paulac874152016-03-10 16:00:26 -0500236 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200237
238 // clang-format off
239 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
240 0.0, 1.0, 0.0, 0.0,
241 0.0, 0.0, 1.0, 0.0,
242 0.0, 0.0, 0.0, 1.0};
243 // clang-format on
Sean Paulac874152016-03-10 16:00:26 -0500244}
245
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300246void DrmHwcTwo::HwcDisplay::ClearDisplay() {
247 compositor_.ClearDisplay();
248}
249
Sean Paulac874152016-03-10 16:00:26 -0500250HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
251 supported(__func__);
252 planner_ = Planner::CreateInstance(drm_);
253 if (!planner_) {
254 ALOGE("Failed to create planner instance for composition");
255 return HWC2::Error::NoResources;
256 }
257
258 int display = static_cast<int>(handle_);
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100259 int ret = compositor_.Init(resource_manager_, display);
Sean Paulac874152016-03-10 16:00:26 -0500260 if (ret) {
261 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
262 return HWC2::Error::NoResources;
263 }
264
265 // Split up the given display planes into primary and overlay to properly
266 // interface with the composition
267 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
268 property_get("hwc.drm.use_overlay_planes", use_overlay_planes_prop, "1");
269 bool use_overlay_planes = atoi(use_overlay_planes_prop);
270 for (auto &plane : *planes) {
271 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
272 primary_planes_.push_back(plane);
273 else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY)
274 overlay_planes_.push_back(plane);
275 }
276
277 crtc_ = drm_->GetCrtcForDisplay(display);
278 if (!crtc_) {
279 ALOGE("Failed to get crtc for display %d", display);
280 return HWC2::Error::BadDisplay;
281 }
282
283 connector_ = drm_->GetConnectorForDisplay(display);
284 if (!connector_) {
285 ALOGE("Failed to get connector for display %d", display);
286 return HWC2::Error::BadDisplay;
287 }
288
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300289 ret = vsync_worker_.Init(drm_, display);
290 if (ret) {
291 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
292 return HWC2::Error::BadDisplay;
293 }
294
295 return ChosePreferredConfig();
296}
297
298HWC2::Error DrmHwcTwo::HwcDisplay::ChosePreferredConfig() {
Sean Paulac874152016-03-10 16:00:26 -0500299 // Fetch the number of modes from the display
300 uint32_t num_configs;
301 HWC2::Error err = GetDisplayConfigs(&num_configs, NULL);
302 if (err != HWC2::Error::None || !num_configs)
303 return err;
304
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200305 return SetActiveConfig(connector_->get_preferred_mode_id());
Sean Paulac874152016-03-10 16:00:26 -0500306}
307
308HWC2::Error DrmHwcTwo::HwcDisplay::RegisterVsyncCallback(
309 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
310 supported(__func__);
311 auto callback = std::make_shared<DrmVsyncCallback>(data, func);
Adrian Salidofa37f672017-02-16 10:29:46 -0800312 vsync_worker_.RegisterCallback(std::move(callback));
Sean Paulac874152016-03-10 16:00:26 -0500313 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500314}
315
316HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500317 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500318 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
319 l.second.accept_type_change();
320 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500321}
322
323HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500324 supported(__func__);
325 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
326 *layer = static_cast<hwc2_layer_t>(layer_idx_);
327 ++layer_idx_;
328 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500329}
330
331HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Sean Paulac874152016-03-10 16:00:26 -0500332 supported(__func__);
Vincent Donnefort9abec032019-10-09 15:43:43 +0100333 if (!get_layer(layer))
334 return HWC2::Error::BadLayer;
335
Sean Paulac874152016-03-10 16:00:26 -0500336 layers_.erase(layer);
337 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500338}
339
340HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
Sean Paulac874152016-03-10 16:00:26 -0500341 supported(__func__);
342 DrmMode const &mode = connector_->active_mode();
343 if (mode.id() == 0)
344 return HWC2::Error::BadConfig;
345
346 *config = mode.id();
347 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500348}
349
350HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
351 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500352 supported(__func__);
353 uint32_t num_changes = 0;
354 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
355 if (l.second.type_changed()) {
356 if (layers && num_changes < *num_elements)
357 layers[num_changes] = l.first;
358 if (types && num_changes < *num_elements)
359 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
360 ++num_changes;
361 }
362 }
363 if (!layers && !types)
364 *num_elements = num_changes;
365 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500366}
367
368HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500369 uint32_t height,
370 int32_t /*format*/,
371 int32_t dataspace) {
372 supported(__func__);
373 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
374 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
375
376 if (width < min.first || height < min.second)
377 return HWC2::Error::Unsupported;
378
379 if (width > max.first || height > max.second)
380 return HWC2::Error::Unsupported;
381
382 if (dataspace != HAL_DATASPACE_UNKNOWN &&
383 dataspace != HAL_DATASPACE_STANDARD_UNSPECIFIED)
384 return HWC2::Error::Unsupported;
385
386 // TODO: Validate format can be handled by either GL or planes
387 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500388}
389
390HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500391 int32_t *modes) {
392 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800393 if (!modes)
394 *num_modes = 1;
395
396 if (modes)
397 *modes = HAL_COLOR_MODE_NATIVE;
398
399 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500400}
401
402HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500403 int32_t attribute_in,
404 int32_t *value) {
405 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400406 auto mode = std::find_if(connector_->modes().begin(),
407 connector_->modes().end(),
408 [config](DrmMode const &m) {
409 return m.id() == config;
410 });
Sean Paulac874152016-03-10 16:00:26 -0500411 if (mode == connector_->modes().end()) {
412 ALOGE("Could not find active mode for %d", config);
413 return HWC2::Error::BadConfig;
414 }
415
416 static const int32_t kUmPerInch = 25400;
417 uint32_t mm_width = connector_->mm_width();
418 uint32_t mm_height = connector_->mm_height();
419 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
420 switch (attribute) {
421 case HWC2::Attribute::Width:
422 *value = mode->h_display();
423 break;
424 case HWC2::Attribute::Height:
425 *value = mode->v_display();
426 break;
427 case HWC2::Attribute::VsyncPeriod:
428 // in nanoseconds
429 *value = 1000 * 1000 * 1000 / mode->v_refresh();
430 break;
431 case HWC2::Attribute::DpiX:
432 // Dots per 1000 inches
433 *value = mm_width ? (mode->h_display() * kUmPerInch) / mm_width : -1;
434 break;
435 case HWC2::Attribute::DpiY:
436 // Dots per 1000 inches
437 *value = mm_height ? (mode->v_display() * kUmPerInch) / mm_height : -1;
438 break;
439 default:
440 *value = -1;
441 return HWC2::Error::BadConfig;
442 }
443 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500444}
445
446HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
447 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500448 supported(__func__);
449 // Since this callback is normally invoked twice (once to get the count, and
450 // once to populate configs), we don't really want to read the edid
451 // redundantly. Instead, only update the modes on the first invocation. While
452 // it's possible this will result in stale modes, it'll all come out in the
453 // wash when we try to set the active config later.
454 if (!configs) {
455 int ret = connector_->UpdateModes();
456 if (ret) {
457 ALOGE("Failed to update display modes %d", ret);
458 return HWC2::Error::BadDisplay;
459 }
460 }
461
Neil Armstrongb67d0492019-06-20 09:00:21 +0000462 // Since the upper layers only look at vactive/hactive/refresh, height and
463 // width, it doesn't differentiate interlaced from progressive and other
464 // similar modes. Depending on the order of modes we return to SF, it could
465 // end up choosing a suboptimal configuration and dropping the preferred
466 // mode. To workaround this, don't offer interlaced modes to SF if there is
467 // at least one non-interlaced alternative and only offer a single WxH@R
468 // mode with at least the prefered mode from in DrmConnector::UpdateModes()
469
470 // TODO: Remove the following block of code until AOSP handles all modes
471 std::vector<DrmMode> sel_modes;
472
473 // Add the preferred mode first to be sure it's not dropped
474 auto mode = std::find_if(connector_->modes().begin(),
475 connector_->modes().end(), [&](DrmMode const &m) {
476 return m.id() ==
477 connector_->get_preferred_mode_id();
478 });
479 if (mode != connector_->modes().end())
480 sel_modes.push_back(*mode);
481
482 // Add the active mode if different from preferred mode
483 if (connector_->active_mode().id() != connector_->get_preferred_mode_id())
484 sel_modes.push_back(connector_->active_mode());
485
486 // Cycle over the modes and filter out "similar" modes, keeping only the
487 // first ones in the order given by DRM (from CEA ids and timings order)
Sean Paulac874152016-03-10 16:00:26 -0500488 for (const DrmMode &mode : connector_->modes()) {
Neil Armstrongb67d0492019-06-20 09:00:21 +0000489 // TODO: Remove this when 3D Attributes are in AOSP
490 if (mode.flags() & DRM_MODE_FLAG_3D_MASK)
491 continue;
492
Neil Armstrong4c027a72019-06-04 14:48:02 +0000493 // TODO: Remove this when the Interlaced attribute is in AOSP
494 if (mode.flags() & DRM_MODE_FLAG_INTERLACE) {
495 auto m = std::find_if(connector_->modes().begin(),
496 connector_->modes().end(),
497 [&mode](DrmMode const &m) {
498 return !(m.flags() & DRM_MODE_FLAG_INTERLACE) &&
499 m.h_display() == mode.h_display() &&
500 m.v_display() == mode.v_display();
501 });
Neil Armstrongb67d0492019-06-20 09:00:21 +0000502 if (m == connector_->modes().end())
503 sel_modes.push_back(mode);
504
505 continue;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000506 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000507
508 // Search for a similar WxH@R mode in the filtered list and drop it if
509 // another mode with the same WxH@R has already been selected
510 // TODO: Remove this when AOSP handles duplicates modes
511 auto m = std::find_if(sel_modes.begin(), sel_modes.end(),
512 [&mode](DrmMode const &m) {
513 return m.h_display() == mode.h_display() &&
514 m.v_display() == mode.v_display() &&
515 m.v_refresh() == mode.v_refresh();
516 });
517 if (m == sel_modes.end())
518 sel_modes.push_back(mode);
519 }
520
521 auto num_modes = static_cast<uint32_t>(sel_modes.size());
522 if (!configs) {
523 *num_configs = num_modes;
524 return HWC2::Error::None;
525 }
526
527 uint32_t idx = 0;
528 for (const DrmMode &mode : sel_modes) {
529 if (idx >= *num_configs)
530 break;
531 configs[idx++] = mode.id();
Sean Paulac874152016-03-10 16:00:26 -0500532 }
533 *num_configs = idx;
534 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500535}
536
537HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500538 supported(__func__);
539 std::ostringstream stream;
540 stream << "display-" << connector_->id();
541 std::string string = stream.str();
542 size_t length = string.length();
543 if (!name) {
544 *size = length;
545 return HWC2::Error::None;
546 }
547
548 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
549 strncpy(name, string.c_str(), *size);
550 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500551}
552
Sean Paulac874152016-03-10 16:00:26 -0500553HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
554 uint32_t *num_elements,
555 hwc2_layer_t *layers,
556 int32_t *layer_requests) {
557 supported(__func__);
558 // TODO: I think virtual display should request
559 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
560 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
561 *num_elements = 0;
562 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500563}
564
565HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500566 supported(__func__);
567 *type = static_cast<int32_t>(type_);
568 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500569}
570
571HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500572 supported(__func__);
573 *support = 0;
574 return HWC2::Error::None;
575}
576
577HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400578 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
579 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500580 supported(__func__);
581 *num_types = 0;
582 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500583}
584
585HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500586 hwc2_layer_t *layers,
587 int32_t *fences) {
588 supported(__func__);
589 uint32_t num_layers = 0;
590
591 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
592 ++num_layers;
593 if (layers == NULL || fences == NULL) {
594 continue;
595 } else if (num_layers > *num_elements) {
596 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
597 return HWC2::Error::None;
598 }
599
600 layers[num_layers - 1] = l.first;
601 fences[num_layers - 1] = l.second.take_release_fence();
602 }
603 *num_elements = num_layers;
604 return HWC2::Error::None;
605}
606
Matteo Franchinc56eede2019-12-03 17:10:38 +0000607void DrmHwcTwo::HwcDisplay::AddFenceToPresentFence(int fd) {
Sean Paulac874152016-03-10 16:00:26 -0500608 if (fd < 0)
609 return;
610
Matteo Franchinc56eede2019-12-03 17:10:38 +0000611 if (present_fence_.get() >= 0) {
612 int old_fence = present_fence_.get();
613 present_fence_.Set(sync_merge("dc_present", old_fence, fd));
614 close(fd);
Sean Paulac874152016-03-10 16:00:26 -0500615 } else {
Matteo Franchinc56eede2019-12-03 17:10:38 +0000616 present_fence_.Set(fd);
Sean Paulac874152016-03-10 16:00:26 -0500617 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500618}
619
Roman Stratiienkoafb36892019-11-08 17:16:11 +0200620bool DrmHwcTwo::HwcDisplay::HardwareSupportsLayerType(
621 HWC2::Composition comp_type) {
622 return comp_type == HWC2::Composition::Device ||
623 comp_type == HWC2::Composition::Cursor;
624}
625
Rob Herring4f6c62e2018-05-17 14:33:02 -0500626HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500627 std::vector<DrmCompositionDisplayLayersMap> layers_map;
628 layers_map.emplace_back();
629 DrmCompositionDisplayLayersMap &map = layers_map.back();
630
631 map.display = static_cast<int>(handle_);
632 map.geometry_changed = true; // TODO: Fix this
633
634 // order the layers by z-order
635 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100636 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500637 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
638 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200639 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500640 case HWC2::Composition::Device:
641 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
642 break;
643 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100644 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500645 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100646 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500647 break;
648 default:
649 continue;
650 }
651 }
652 if (use_client_layer)
653 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
654
Rob Herring4f6c62e2018-05-17 14:33:02 -0500655 if (z_map.empty())
656 return HWC2::Error::BadLayer;
657
Sean Paulac874152016-03-10 16:00:26 -0500658 // now that they're ordered by z, add them to the composition
659 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
660 DrmHwcLayer layer;
661 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200662 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500663 if (ret) {
664 ALOGE("Failed to import layer, ret=%d", ret);
665 return HWC2::Error::NoResources;
666 }
667 map.layers.emplace_back(std::move(layer));
668 }
Sean Paulac874152016-03-10 16:00:26 -0500669
Sean Paulf72cccd2018-08-27 13:59:08 -0400670 std::unique_ptr<DrmDisplayComposition> composition = compositor_
671 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500672 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
673
674 // TODO: Don't always assume geometry changed
675 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
676 if (ret) {
677 ALOGE("Failed to set layers in the composition ret=%d", ret);
678 return HWC2::Error::BadLayer;
679 }
680
681 std::vector<DrmPlane *> primary_planes(primary_planes_);
682 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500683 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500684 if (ret) {
685 ALOGE("Failed to plan the composition ret=%d", ret);
686 return HWC2::Error::BadConfig;
687 }
688
689 // Disable the planes we're not using
690 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
691 composition->AddPlaneDisable(*i);
692 i = primary_planes.erase(i);
693 }
694 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
695 composition->AddPlaneDisable(*i);
696 i = overlay_planes.erase(i);
697 }
698
Rob Herring4f6c62e2018-05-17 14:33:02 -0500699 if (test) {
700 ret = compositor_.TestComposition(composition.get());
701 } else {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500702 ret = compositor_.ApplyComposition(std::move(composition));
Matteo Franchinc56eede2019-12-03 17:10:38 +0000703 AddFenceToPresentFence(compositor_.TakeOutFence());
Rob Herring4f6c62e2018-05-17 14:33:02 -0500704 }
Sean Paulac874152016-03-10 16:00:26 -0500705 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700706 if (!test)
707 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500708 return HWC2::Error::BadParameter;
709 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500710 return HWC2::Error::None;
711}
712
Matteo Franchinc56eede2019-12-03 17:10:38 +0000713HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500714 supported(__func__);
715 HWC2::Error ret;
716
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200717 ++total_stats_.total_frames_;
718
Rob Herring4f6c62e2018-05-17 14:33:02 -0500719 ret = CreateComposition(false);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200720 if (ret != HWC2::Error::None)
721 ++total_stats_.failed_kms_present_;
722
Rob Herring4f6c62e2018-05-17 14:33:02 -0500723 if (ret == HWC2::Error::BadLayer) {
724 // Can we really have no client or device layers?
Matteo Franchinc56eede2019-12-03 17:10:38 +0000725 *present_fence = -1;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500726 return HWC2::Error::None;
727 }
728 if (ret != HWC2::Error::None)
729 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500730
Matteo Franchinc56eede2019-12-03 17:10:38 +0000731 *present_fence = present_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500732
733 ++frame_no_;
734 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500735}
736
737HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500738 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400739 auto mode = std::find_if(connector_->modes().begin(),
740 connector_->modes().end(),
741 [config](DrmMode const &m) {
742 return m.id() == config;
743 });
Sean Paulac874152016-03-10 16:00:26 -0500744 if (mode == connector_->modes().end()) {
745 ALOGE("Could not find active mode for %d", config);
746 return HWC2::Error::BadConfig;
747 }
748
Sean Paulf72cccd2018-08-27 13:59:08 -0400749 std::unique_ptr<DrmDisplayComposition> composition = compositor_
750 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500751 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
752 int ret = composition->SetDisplayMode(*mode);
Sean Pauled45a8e2017-02-28 13:17:34 -0500753 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500754 if (ret) {
755 ALOGE("Failed to queue dpms composition on %d", ret);
756 return HWC2::Error::BadConfig;
757 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300758
759 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500760
761 // Setup the client layer's dimensions
762 hwc_rect_t display_frame = {.left = 0,
763 .top = 0,
764 .right = static_cast<int>(mode->h_display()),
765 .bottom = static_cast<int>(mode->v_display())};
766 client_layer_.SetLayerDisplayFrame(display_frame);
767 hwc_frect_t source_crop = {.left = 0.0f,
768 .top = 0.0f,
769 .right = mode->h_display() + 0.0f,
770 .bottom = mode->v_display() + 0.0f};
771 client_layer_.SetLayerSourceCrop(source_crop);
772
773 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500774}
775
776HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
777 int32_t acquire_fence,
778 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600779 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500780 supported(__func__);
781 UniqueFd uf(acquire_fence);
782
783 client_layer_.set_buffer(target);
784 client_layer_.set_acquire_fence(uf.get());
785 client_layer_.SetLayerDataspace(dataspace);
786 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500787}
788
789HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500790 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800791
792 if (mode != HAL_COLOR_MODE_NATIVE)
Vincent Donnefort7834a892019-10-09 15:53:56 +0100793 return HWC2::Error::BadParameter;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800794
795 color_mode_ = mode;
796 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500797}
798
799HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500800 int32_t hint) {
801 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200802 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
803 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
804 return HWC2::Error::BadParameter;
805
806 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
807 return HWC2::Error::BadParameter;
808
809 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
810 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
811 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
812
813 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500814}
815
816HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500817 int32_t release_fence) {
818 supported(__func__);
819 // TODO: Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500820 return unsupported(__func__, buffer, release_fence);
821}
822
Sean Paulac874152016-03-10 16:00:26 -0500823HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
824 supported(__func__);
825 uint64_t dpms_value = 0;
826 auto mode = static_cast<HWC2::PowerMode>(mode_in);
827 switch (mode) {
828 case HWC2::PowerMode::Off:
829 dpms_value = DRM_MODE_DPMS_OFF;
830 break;
831 case HWC2::PowerMode::On:
832 dpms_value = DRM_MODE_DPMS_ON;
833 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100834 case HWC2::PowerMode::Doze:
835 case HWC2::PowerMode::DozeSuspend:
836 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500837 default:
838 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100839 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500840 };
841
Sean Paulf72cccd2018-08-27 13:59:08 -0400842 std::unique_ptr<DrmDisplayComposition> composition = compositor_
843 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500844 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
845 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500846 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500847 if (ret) {
848 ALOGE("Failed to apply the dpms composition ret=%d", ret);
849 return HWC2::Error::BadParameter;
850 }
851 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500852}
853
854HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500855 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300856 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500857 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500858}
859
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200860uint32_t DrmHwcTwo::HwcDisplay::CalcPixOps(
861 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t first_z,
862 size_t size) {
863 uint32_t pixops = 0;
864 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
865 if (l.first >= first_z && l.first < first_z + size) {
866 hwc_rect_t df = l.second->display_frame();
867 pixops += (df.right - df.left) * (df.bottom - df.top);
868 }
869 }
870 return pixops;
871}
872
873void DrmHwcTwo::HwcDisplay::MarkValidated(
874 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t client_first_z,
875 size_t client_size) {
876 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
877 if (l.first >= client_first_z && l.first < client_first_z + client_size)
878 l.second->set_validated_type(HWC2::Composition::Client);
879 else
880 l.second->set_validated_type(HWC2::Composition::Device);
881 }
882}
883
Sean Pauled2ec4b2016-03-10 15:35:40 -0500884HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500885 uint32_t *num_requests) {
886 supported(__func__);
887 *num_types = 0;
888 *num_requests = 0;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500889 size_t avail_planes = primary_planes_.size() + overlay_planes_.size();
Rob Herring4f6c62e2018-05-17 14:33:02 -0500890
891 /*
892 * If more layers then planes, save one plane
893 * for client composited layers
894 */
895 if (avail_planes < layers_.size())
896 avail_planes--;
897
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200898 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
Roman Stratiienkof2647232019-11-21 01:58:35 +0200899 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
900 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
901
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200902 uint32_t total_pixops = CalcPixOps(z_map, 0, z_map.size()), gpu_pixops = 0;
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200903
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200904 int client_start = -1, client_size = 0;
905
Rob Herring4f6c62e2018-05-17 14:33:02 -0500906 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200907 if (!HardwareSupportsLayerType(l.second->sf_type()) ||
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200908 !importer_->CanImportBuffer(l.second->buffer()) ||
909 color_transform_hint_ != HAL_COLOR_TRANSFORM_IDENTITY) {
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200910 if (client_start < 0)
911 client_start = l.first;
912 client_size = (l.first - client_start) + 1;
913 }
914 }
915
916 int extra_client = (z_map.size() - client_size) - avail_planes;
917 if (extra_client > 0) {
918 int start = 0, steps;
919 if (client_size != 0) {
920 int prepend = std::min(client_start, extra_client);
921 int append = std::min(int(z_map.size() - (client_start + client_size)),
922 extra_client);
923 start = client_start - prepend;
924 client_size += extra_client;
925 steps = 1 + std::min(std::min(append, prepend),
926 int(z_map.size()) - (start + client_size));
Roman Stratiienkof2647232019-11-21 01:58:35 +0200927 } else {
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200928 client_size = extra_client;
929 steps = 1 + z_map.size() - extra_client;
Roman Stratiienkof2647232019-11-21 01:58:35 +0200930 }
931
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200932 gpu_pixops = INT_MAX;
933 for (int i = 0; i < steps; i++) {
934 uint32_t po = CalcPixOps(z_map, start + i, client_size);
935 if (po < gpu_pixops) {
936 gpu_pixops = po;
937 client_start = start + i;
938 }
939 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500940 }
941
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200942 MarkValidated(z_map, client_start, client_size);
943
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200944 if (CreateComposition(true) != HWC2::Error::None) {
945 ++total_stats_.failed_kms_validate_;
946 gpu_pixops = total_pixops;
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200947 client_size = z_map.size();
948 MarkValidated(z_map, 0, client_size);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200949 }
950
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200951 *num_types = client_size;
952
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200953 total_stats_.gpu_pixops_ += gpu_pixops;
954 total_stats_.total_pixops_ += total_pixops;
Roman Stratiienkof2647232019-11-21 01:58:35 +0200955
Rob Herringee8f45b2017-06-09 15:15:55 -0500956 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500957}
958
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800959HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
960 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
961 supported(__func__);
962
963 drmModePropertyBlobPtr blob;
964 int ret;
965 uint64_t blob_id;
966
967 std::tie(ret, blob_id) = connector_->edid_property().value();
968 if (ret) {
969 ALOGE("Failed to get edid property value.");
970 return HWC2::Error::Unsupported;
971 }
972
973 blob = drmModeGetPropertyBlob(drm_->fd(), blob_id);
974
975 outData = static_cast<uint8_t *>(blob->data);
976
977 *outPort = connector_->id();
978 *outDataSize = blob->length;
979
980 return HWC2::Error::None;
981}
982
983HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
984 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
985 unsupported(__func__, outCapabilities);
986
987 if (outNumCapabilities == NULL) {
988 return HWC2::Error::BadParameter;
989 }
990
991 *outNumCapabilities = 0;
992
993 return HWC2::Error::None;
994}
995
Sean Pauled2ec4b2016-03-10 15:35:40 -0500996HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -0500997 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800998 cursor_x_ = x;
999 cursor_y_ = y;
1000 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001001}
1002
1003HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -05001004 supported(__func__);
1005 blending_ = static_cast<HWC2::BlendMode>(mode);
1006 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001007}
1008
1009HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001010 int32_t acquire_fence) {
1011 supported(__func__);
1012 UniqueFd uf(acquire_fence);
1013
1014 // The buffer and acquire_fence are handled elsewhere
1015 if (sf_type_ == HWC2::Composition::Client ||
1016 sf_type_ == HWC2::Composition::Sideband ||
1017 sf_type_ == HWC2::Composition::SolidColor)
1018 return HWC2::Error::None;
1019
1020 set_buffer(buffer);
1021 set_acquire_fence(uf.get());
1022 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001023}
1024
1025HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001026 // TODO: Put to client composition here?
1027 supported(__func__);
1028 layer_color_ = color;
1029 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001030}
1031
1032HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001033 sf_type_ = static_cast<HWC2::Composition>(type);
1034 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001035}
1036
1037HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001038 supported(__func__);
1039 dataspace_ = static_cast<android_dataspace_t>(dataspace);
1040 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001041}
1042
1043HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001044 supported(__func__);
1045 display_frame_ = frame;
1046 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001047}
1048
1049HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001050 supported(__func__);
1051 alpha_ = alpha;
1052 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001053}
1054
1055HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1056 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001057 supported(__func__);
1058 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001059 return unsupported(__func__, stream);
1060}
1061
1062HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001063 supported(__func__);
1064 source_crop_ = crop;
1065 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001066}
1067
1068HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001069 supported(__func__);
1070 // TODO: We don't use surface damage, marking as unsupported
1071 unsupported(__func__, damage);
1072 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001073}
1074
1075HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001076 supported(__func__);
1077 transform_ = static_cast<HWC2::Transform>(transform);
1078 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001079}
1080
1081HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001082 supported(__func__);
1083 // TODO: We don't use this information, marking as unsupported
1084 unsupported(__func__, visible);
1085 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001086}
1087
Sean Paulac874152016-03-10 16:00:26 -05001088HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1089 supported(__func__);
1090 z_order_ = order;
1091 return HWC2::Error::None;
1092}
1093
1094void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1095 supported(__func__);
1096 switch (blending_) {
1097 case HWC2::BlendMode::None:
1098 layer->blending = DrmHwcBlending::kNone;
1099 break;
1100 case HWC2::BlendMode::Premultiplied:
1101 layer->blending = DrmHwcBlending::kPreMult;
1102 break;
1103 case HWC2::BlendMode::Coverage:
1104 layer->blending = DrmHwcBlending::kCoverage;
1105 break;
1106 default:
1107 ALOGE("Unknown blending mode b=%d", blending_);
1108 layer->blending = DrmHwcBlending::kNone;
1109 break;
1110 }
1111
1112 OutputFd release_fence = release_fence_output();
1113
1114 layer->sf_handle = buffer_;
1115 layer->acquire_fence = acquire_fence_.Release();
1116 layer->release_fence = std::move(release_fence);
1117 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +02001118 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -05001119 layer->SetSourceCrop(source_crop_);
1120 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -05001121}
1122
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001123void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
1124 auto cb = callbacks_.find(HWC2::Callback::Hotplug);
1125 if (cb == callbacks_.end())
1126 return;
1127
1128 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(cb->second.func);
1129 hotplug(cb->second.data, displayid,
1130 (state == DRM_MODE_CONNECTED ? HWC2_CONNECTION_CONNECTED
1131 : HWC2_CONNECTION_DISCONNECTED));
1132}
1133
1134void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
1135 for (auto &conn : drmDevice->connectors()) {
1136 if (conn->state() != DRM_MODE_CONNECTED)
1137 continue;
1138 HandleDisplayHotplug(conn->display(), conn->state());
1139 }
1140}
1141
1142void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
1143 for (auto &conn : drm_->connectors()) {
1144 drmModeConnection old_state = conn->state();
1145 drmModeConnection cur_state = conn->UpdateModes()
1146 ? DRM_MODE_UNKNOWNCONNECTION
1147 : conn->state();
1148
1149 if (cur_state == old_state)
1150 continue;
1151
1152 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
1153 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1154 conn->id(), conn->display());
1155
1156 int display_id = conn->display();
1157 if (cur_state == DRM_MODE_CONNECTED) {
1158 auto &display = hwc2_->displays_.at(display_id);
1159 display.ChosePreferredConfig();
1160 } else {
1161 auto &display = hwc2_->displays_.at(display_id);
1162 display.ClearDisplay();
1163 }
1164
1165 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1166 }
1167}
1168
Sean Pauled2ec4b2016-03-10 15:35:40 -05001169// static
1170int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1171 unsupported(__func__);
1172 return 0;
1173}
1174
1175// static
1176void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001177 uint32_t *out_count,
1178 int32_t * /*out_capabilities*/) {
1179 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001180 *out_count = 0;
1181}
1182
1183// static
Sean Paulac874152016-03-10 16:00:26 -05001184hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1185 struct hwc2_device * /*dev*/, int32_t descriptor) {
1186 supported(__func__);
1187 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001188 switch (func) {
1189 // Device functions
1190 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1191 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1192 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1193 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001194 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001195 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1196 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1197 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1198 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1199 case HWC2::FunctionDescriptor::Dump:
1200 return ToHook<HWC2_PFN_DUMP>(
1201 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1202 uint32_t *, char *>);
1203 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1204 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1205 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1206 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1207 case HWC2::FunctionDescriptor::RegisterCallback:
1208 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1209 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1210 &DrmHwcTwo::RegisterCallback, int32_t,
1211 hwc2_callback_data_t, hwc2_function_pointer_t>);
1212
1213 // Display functions
1214 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1215 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1216 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1217 &HwcDisplay::AcceptDisplayChanges>);
1218 case HWC2::FunctionDescriptor::CreateLayer:
1219 return ToHook<HWC2_PFN_CREATE_LAYER>(
1220 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1221 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1222 case HWC2::FunctionDescriptor::DestroyLayer:
1223 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1224 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1225 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1226 case HWC2::FunctionDescriptor::GetActiveConfig:
1227 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1228 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1229 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1230 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1231 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1232 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1233 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1234 hwc2_layer_t *, int32_t *>);
1235 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1236 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1237 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1238 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1239 int32_t, int32_t>);
1240 case HWC2::FunctionDescriptor::GetColorModes:
1241 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1242 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1243 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1244 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001245 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1246 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1247 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1248 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001249 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001250 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1251 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1252 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1253 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001254 case HWC2::FunctionDescriptor::GetDisplayName:
1255 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1256 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1257 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1258 case HWC2::FunctionDescriptor::GetDisplayRequests:
1259 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1260 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1261 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1262 hwc2_layer_t *, int32_t *>);
1263 case HWC2::FunctionDescriptor::GetDisplayType:
1264 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1265 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1266 &HwcDisplay::GetDisplayType, int32_t *>);
1267 case HWC2::FunctionDescriptor::GetDozeSupport:
1268 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1269 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1270 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001271 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1272 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1273 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1274 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1275 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001276 case HWC2::FunctionDescriptor::GetReleaseFences:
1277 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1278 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1279 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1280 int32_t *>);
1281 case HWC2::FunctionDescriptor::PresentDisplay:
1282 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1283 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1284 &HwcDisplay::PresentDisplay, int32_t *>);
1285 case HWC2::FunctionDescriptor::SetActiveConfig:
1286 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1287 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1288 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1289 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001290 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1291 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1292 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1293 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001294 case HWC2::FunctionDescriptor::SetColorMode:
1295 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1296 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1297 &HwcDisplay::SetColorMode, int32_t>);
1298 case HWC2::FunctionDescriptor::SetColorTransform:
1299 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1300 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1301 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1302 case HWC2::FunctionDescriptor::SetOutputBuffer:
1303 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1304 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1305 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1306 case HWC2::FunctionDescriptor::SetPowerMode:
1307 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1308 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1309 &HwcDisplay::SetPowerMode, int32_t>);
1310 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1311 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1312 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1313 &HwcDisplay::SetVsyncEnabled, int32_t>);
1314 case HWC2::FunctionDescriptor::ValidateDisplay:
1315 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1316 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1317 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001318 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1319 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1320 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1321 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1322 uint32_t *, uint8_t *>);
1323 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1324 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1325 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1326 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1327 uint32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001328
1329 // Layer functions
1330 case HWC2::FunctionDescriptor::SetCursorPosition:
1331 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1332 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1333 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1334 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1335 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1336 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1337 &HwcLayer::SetLayerBlendMode, int32_t>);
1338 case HWC2::FunctionDescriptor::SetLayerBuffer:
1339 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1340 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1341 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1342 case HWC2::FunctionDescriptor::SetLayerColor:
1343 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1344 LayerHook<decltype(&HwcLayer::SetLayerColor),
1345 &HwcLayer::SetLayerColor, hwc_color_t>);
1346 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1347 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1348 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1349 &HwcLayer::SetLayerCompositionType, int32_t>);
1350 case HWC2::FunctionDescriptor::SetLayerDataspace:
1351 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1352 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1353 &HwcLayer::SetLayerDataspace, int32_t>);
1354 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1355 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1356 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1357 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1358 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1359 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1360 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1361 &HwcLayer::SetLayerPlaneAlpha, float>);
1362 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001363 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1364 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1365 &HwcLayer::SetLayerSidebandStream,
1366 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001367 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1368 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1369 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1370 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1371 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1372 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1373 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1374 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1375 case HWC2::FunctionDescriptor::SetLayerTransform:
1376 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1377 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1378 &HwcLayer::SetLayerTransform, int32_t>);
1379 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1380 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1381 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1382 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1383 case HWC2::FunctionDescriptor::SetLayerZOrder:
1384 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1385 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1386 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001387 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001388 default:
1389 return NULL;
1390 }
1391}
Sean Paulac874152016-03-10 16:00:26 -05001392
1393// static
1394int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1395 struct hw_device_t **dev) {
1396 supported(__func__);
1397 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1398 ALOGE("Invalid module name- %s", name);
1399 return -EINVAL;
1400 }
1401
1402 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1403 if (!ctx) {
1404 ALOGE("Failed to allocate DrmHwcTwo");
1405 return -ENOMEM;
1406 }
1407
1408 HWC2::Error err = ctx->Init();
1409 if (err != HWC2::Error::None) {
1410 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1411 return -EINVAL;
1412 }
1413
1414 ctx->common.module = const_cast<hw_module_t *>(module);
1415 *dev = &ctx->common;
1416 ctx.release();
1417 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001418}
Sean Paulf72cccd2018-08-27 13:59:08 -04001419} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001420
1421static struct hw_module_methods_t hwc2_module_methods = {
1422 .open = android::DrmHwcTwo::HookDevOpen,
1423};
1424
1425hw_module_t HAL_MODULE_INFO_SYM = {
1426 .tag = HARDWARE_MODULE_TAG,
1427 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1428 .id = HWC_HARDWARE_MODULE_ID,
1429 .name = "DrmHwcTwo module",
1430 .author = "The Android Open Source Project",
1431 .methods = &hwc2_module_methods,
1432 .dso = NULL,
1433 .reserved = {0},
1434};