blob: 3c224842ce74bd8fb859a3f28b61bb3b43a575c2 [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 Paulac874152016-03-10 16:00:26 -050020#include "drmdisplaycomposition.h"
21#include "drmhwcomposer.h"
Sean Pauled2ec4b2016-03-10 15:35:40 -050022#include "drmhwctwo.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
John Stultz9057a6f2018-04-26 12:05:55 -070029#include <log/log.h>
Sean Paulac874152016-03-10 16:00:26 -050030#include <cutils/properties.h>
31#include <hardware/hardware.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050032#include <hardware/hwcomposer2.h>
33
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
Sean Paulac874152016-03-10 16:00:26 -050060HWC2::Error DrmHwcTwo::Init() {
61 int ret = drm_.Init();
62 if (ret) {
63 ALOGE("Can't initialize drm object %d", ret);
64 return HWC2::Error::NoResources;
65 }
66
67 importer_.reset(Importer::CreateInstance(&drm_));
68 if (!importer_) {
69 ALOGE("Failed to create importer instance");
70 return HWC2::Error::NoResources;
71 }
72
Sean Paulac874152016-03-10 16:00:26 -050073 displays_.emplace(std::piecewise_construct,
74 std::forward_as_tuple(HWC_DISPLAY_PRIMARY),
Andrii Chepurnyi1d224e82018-05-17 18:34:01 +030075 std::forward_as_tuple(&drm_, importer_, HWC_DISPLAY_PRIMARY,
Sean Paulac874152016-03-10 16:00:26 -050076 HWC2::DisplayType::Physical));
77
78 DrmCrtc *crtc = drm_.GetCrtcForDisplay(static_cast<int>(HWC_DISPLAY_PRIMARY));
79 if (!crtc) {
80 ALOGE("Failed to get crtc for display %d",
81 static_cast<int>(HWC_DISPLAY_PRIMARY));
82 return HWC2::Error::BadDisplay;
83 }
84
85 std::vector<DrmPlane *> display_planes;
86 for (auto &plane : drm_.planes()) {
87 if (plane->GetCrtcSupported(*crtc))
88 display_planes.push_back(plane.get());
89 }
90 displays_.at(HWC_DISPLAY_PRIMARY).Init(&display_planes);
91 return HWC2::Error::None;
92}
93
Sean Pauled2ec4b2016-03-10 15:35:40 -050094template <typename... Args>
95static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
96 ALOGV("Unsupported function: %s", func);
97 return HWC2::Error::Unsupported;
98}
99
Sean Paulac874152016-03-10 16:00:26 -0500100static inline void supported(char const *func) {
101 ALOGV("Supported function: %s", func);
102}
103
Sean Pauled2ec4b2016-03-10 15:35:40 -0500104HWC2::Error DrmHwcTwo::CreateVirtualDisplay(uint32_t width, uint32_t height,
105 int32_t *format,
106 hwc2_display_t *display) {
107 // TODO: Implement virtual display
Sean Paulac874152016-03-10 16:00:26 -0500108 return unsupported(__func__, width, height, format, display);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500109}
110
111HWC2::Error DrmHwcTwo::DestroyVirtualDisplay(hwc2_display_t display) {
Sean Paulac874152016-03-10 16:00:26 -0500112 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500113 return unsupported(__func__, display);
114}
115
116void DrmHwcTwo::Dump(uint32_t *size, char *buffer) {
Sean Paulac874152016-03-10 16:00:26 -0500117 // TODO: Implement dump
Sean Pauled2ec4b2016-03-10 15:35:40 -0500118 unsupported(__func__, size, buffer);
119}
120
121uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() {
Sean Paulac874152016-03-10 16:00:26 -0500122 // TODO: Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500123 unsupported(__func__);
124 return 0;
125}
126
127HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor,
Sean Paulac874152016-03-10 16:00:26 -0500128 hwc2_callback_data_t data,
129 hwc2_function_pointer_t function) {
130 supported(__func__);
131 auto callback = static_cast<HWC2::Callback>(descriptor);
132 callbacks_.emplace(callback, HwcCallback(data, function));
133
134 switch (callback) {
135 case HWC2::Callback::Hotplug: {
136 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(function);
137 hotplug(data, HWC_DISPLAY_PRIMARY,
138 static_cast<int32_t>(HWC2::Connection::Connected));
139 break;
140 }
141 case HWC2::Callback::Vsync: {
142 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
143 displays_)
144 d.second.RegisterVsyncCallback(data, function);
145 break;
146 }
147 default:
148 break;
149 }
150 return HWC2::Error::None;
151}
152
153DrmHwcTwo::HwcDisplay::HwcDisplay(DrmResources *drm,
154 std::shared_ptr<Importer> importer,
Sean Paulac874152016-03-10 16:00:26 -0500155 hwc2_display_t handle, HWC2::DisplayType type)
Andrii Chepurnyi1d224e82018-05-17 18:34:01 +0300156 : drm_(drm), importer_(importer), handle_(handle), type_(type) {
Sean Paulac874152016-03-10 16:00:26 -0500157 supported(__func__);
158}
159
160HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
161 supported(__func__);
162 planner_ = Planner::CreateInstance(drm_);
163 if (!planner_) {
164 ALOGE("Failed to create planner instance for composition");
165 return HWC2::Error::NoResources;
166 }
167
168 int display = static_cast<int>(handle_);
169 int ret = compositor_.Init(drm_, display);
170 if (ret) {
171 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
172 return HWC2::Error::NoResources;
173 }
174
175 // Split up the given display planes into primary and overlay to properly
176 // interface with the composition
177 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
178 property_get("hwc.drm.use_overlay_planes", use_overlay_planes_prop, "1");
179 bool use_overlay_planes = atoi(use_overlay_planes_prop);
180 for (auto &plane : *planes) {
181 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
182 primary_planes_.push_back(plane);
183 else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY)
184 overlay_planes_.push_back(plane);
185 }
186
187 crtc_ = drm_->GetCrtcForDisplay(display);
188 if (!crtc_) {
189 ALOGE("Failed to get crtc for display %d", display);
190 return HWC2::Error::BadDisplay;
191 }
192
193 connector_ = drm_->GetConnectorForDisplay(display);
194 if (!connector_) {
195 ALOGE("Failed to get connector for display %d", display);
196 return HWC2::Error::BadDisplay;
197 }
198
199 // Fetch the number of modes from the display
200 uint32_t num_configs;
201 HWC2::Error err = GetDisplayConfigs(&num_configs, NULL);
202 if (err != HWC2::Error::None || !num_configs)
203 return err;
204
205 // Grab the first mode, we'll choose this as the active mode
206 // TODO: Should choose the preferred mode here
207 hwc2_config_t default_config;
208 num_configs = 1;
209 err = GetDisplayConfigs(&num_configs, &default_config);
210 if (err != HWC2::Error::None)
211 return err;
212
213 ret = vsync_worker_.Init(drm_, display);
214 if (ret) {
215 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
216 return HWC2::Error::BadDisplay;
217 }
218
219 return SetActiveConfig(default_config);
220}
221
222HWC2::Error DrmHwcTwo::HwcDisplay::RegisterVsyncCallback(
223 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
224 supported(__func__);
225 auto callback = std::make_shared<DrmVsyncCallback>(data, func);
Adrian Salidofa37f672017-02-16 10:29:46 -0800226 vsync_worker_.RegisterCallback(std::move(callback));
Sean Paulac874152016-03-10 16:00:26 -0500227 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500228}
229
230HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500231 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500232 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
233 l.second.accept_type_change();
234 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500235}
236
237HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500238 supported(__func__);
239 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
240 *layer = static_cast<hwc2_layer_t>(layer_idx_);
241 ++layer_idx_;
242 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500243}
244
245HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Sean Paulac874152016-03-10 16:00:26 -0500246 supported(__func__);
247 layers_.erase(layer);
248 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500249}
250
251HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
Sean Paulac874152016-03-10 16:00:26 -0500252 supported(__func__);
253 DrmMode const &mode = connector_->active_mode();
254 if (mode.id() == 0)
255 return HWC2::Error::BadConfig;
256
257 *config = mode.id();
258 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500259}
260
261HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
262 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500263 supported(__func__);
264 uint32_t num_changes = 0;
265 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
266 if (l.second.type_changed()) {
267 if (layers && num_changes < *num_elements)
268 layers[num_changes] = l.first;
269 if (types && num_changes < *num_elements)
270 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
271 ++num_changes;
272 }
273 }
274 if (!layers && !types)
275 *num_elements = num_changes;
276 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500277}
278
279HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500280 uint32_t height,
281 int32_t /*format*/,
282 int32_t dataspace) {
283 supported(__func__);
284 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
285 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
286
287 if (width < min.first || height < min.second)
288 return HWC2::Error::Unsupported;
289
290 if (width > max.first || height > max.second)
291 return HWC2::Error::Unsupported;
292
293 if (dataspace != HAL_DATASPACE_UNKNOWN &&
294 dataspace != HAL_DATASPACE_STANDARD_UNSPECIFIED)
295 return HWC2::Error::Unsupported;
296
297 // TODO: Validate format can be handled by either GL or planes
298 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500299}
300
301HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500302 int32_t *modes) {
303 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800304 if (!modes)
305 *num_modes = 1;
306
307 if (modes)
308 *modes = HAL_COLOR_MODE_NATIVE;
309
310 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500311}
312
313HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500314 int32_t attribute_in,
315 int32_t *value) {
316 supported(__func__);
317 auto mode =
318 std::find_if(connector_->modes().begin(), connector_->modes().end(),
319 [config](DrmMode const &m) { return m.id() == config; });
320 if (mode == connector_->modes().end()) {
321 ALOGE("Could not find active mode for %d", config);
322 return HWC2::Error::BadConfig;
323 }
324
325 static const int32_t kUmPerInch = 25400;
326 uint32_t mm_width = connector_->mm_width();
327 uint32_t mm_height = connector_->mm_height();
328 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
329 switch (attribute) {
330 case HWC2::Attribute::Width:
331 *value = mode->h_display();
332 break;
333 case HWC2::Attribute::Height:
334 *value = mode->v_display();
335 break;
336 case HWC2::Attribute::VsyncPeriod:
337 // in nanoseconds
338 *value = 1000 * 1000 * 1000 / mode->v_refresh();
339 break;
340 case HWC2::Attribute::DpiX:
341 // Dots per 1000 inches
342 *value = mm_width ? (mode->h_display() * kUmPerInch) / mm_width : -1;
343 break;
344 case HWC2::Attribute::DpiY:
345 // Dots per 1000 inches
346 *value = mm_height ? (mode->v_display() * kUmPerInch) / mm_height : -1;
347 break;
348 default:
349 *value = -1;
350 return HWC2::Error::BadConfig;
351 }
352 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500353}
354
355HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
356 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500357 supported(__func__);
358 // Since this callback is normally invoked twice (once to get the count, and
359 // once to populate configs), we don't really want to read the edid
360 // redundantly. Instead, only update the modes on the first invocation. While
361 // it's possible this will result in stale modes, it'll all come out in the
362 // wash when we try to set the active config later.
363 if (!configs) {
364 int ret = connector_->UpdateModes();
365 if (ret) {
366 ALOGE("Failed to update display modes %d", ret);
367 return HWC2::Error::BadDisplay;
368 }
369 }
370
371 auto num_modes = static_cast<uint32_t>(connector_->modes().size());
372 if (!configs) {
373 *num_configs = num_modes;
374 return HWC2::Error::None;
375 }
376
377 uint32_t idx = 0;
378 for (const DrmMode &mode : connector_->modes()) {
379 if (idx >= *num_configs)
380 break;
381 configs[idx++] = mode.id();
382 }
383 *num_configs = idx;
384 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500385}
386
387HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500388 supported(__func__);
389 std::ostringstream stream;
390 stream << "display-" << connector_->id();
391 std::string string = stream.str();
392 size_t length = string.length();
393 if (!name) {
394 *size = length;
395 return HWC2::Error::None;
396 }
397
398 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
399 strncpy(name, string.c_str(), *size);
400 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500401}
402
Sean Paulac874152016-03-10 16:00:26 -0500403HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
404 uint32_t *num_elements,
405 hwc2_layer_t *layers,
406 int32_t *layer_requests) {
407 supported(__func__);
408 // TODO: I think virtual display should request
409 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
410 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
411 *num_elements = 0;
412 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500413}
414
415HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500416 supported(__func__);
417 *type = static_cast<int32_t>(type_);
418 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500419}
420
421HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500422 supported(__func__);
423 *support = 0;
424 return HWC2::Error::None;
425}
426
427HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
428 uint32_t *num_types, int32_t */*types*/, float */*max_luminance*/,
429 float */*max_average_luminance*/, float */*min_luminance*/) {
430 supported(__func__);
431 *num_types = 0;
432 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500433}
434
435HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500436 hwc2_layer_t *layers,
437 int32_t *fences) {
438 supported(__func__);
439 uint32_t num_layers = 0;
440
441 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
442 ++num_layers;
443 if (layers == NULL || fences == NULL) {
444 continue;
445 } else if (num_layers > *num_elements) {
446 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
447 return HWC2::Error::None;
448 }
449
450 layers[num_layers - 1] = l.first;
451 fences[num_layers - 1] = l.second.take_release_fence();
452 }
453 *num_elements = num_layers;
454 return HWC2::Error::None;
455}
456
457void DrmHwcTwo::HwcDisplay::AddFenceToRetireFence(int fd) {
458 supported(__func__);
459 if (fd < 0)
460 return;
461
462 if (next_retire_fence_.get() >= 0) {
463 int old_fence = next_retire_fence_.get();
464 next_retire_fence_.Set(sync_merge("dc_retire", old_fence, fd));
465 } else {
466 next_retire_fence_.Set(dup(fd));
467 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500468}
469
470HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *retire_fence) {
Sean Paulac874152016-03-10 16:00:26 -0500471 supported(__func__);
472 std::vector<DrmCompositionDisplayLayersMap> layers_map;
473 layers_map.emplace_back();
474 DrmCompositionDisplayLayersMap &map = layers_map.back();
475
476 map.display = static_cast<int>(handle_);
477 map.geometry_changed = true; // TODO: Fix this
478
479 // order the layers by z-order
480 bool use_client_layer = false;
481 uint32_t client_z_order = 0;
482 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
483 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
484 switch (l.second.validated_type()) {
485 case HWC2::Composition::Device:
486 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
487 break;
488 case HWC2::Composition::Client:
489 // Place it at the z_order of the highest client layer
490 use_client_layer = true;
491 client_z_order = std::max(client_z_order, l.second.z_order());
492 break;
493 default:
494 continue;
495 }
496 }
497 if (use_client_layer)
498 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
499
500 // now that they're ordered by z, add them to the composition
501 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
502 DrmHwcLayer layer;
503 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200504 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500505 if (ret) {
506 ALOGE("Failed to import layer, ret=%d", ret);
507 return HWC2::Error::NoResources;
508 }
509 map.layers.emplace_back(std::move(layer));
510 }
511 if (map.layers.empty()) {
512 *retire_fence = -1;
513 return HWC2::Error::None;
514 }
515
516 std::unique_ptr<DrmDisplayComposition> composition =
517 compositor_.CreateComposition();
518 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
519
520 // TODO: Don't always assume geometry changed
521 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
522 if (ret) {
523 ALOGE("Failed to set layers in the composition ret=%d", ret);
524 return HWC2::Error::BadLayer;
525 }
526
527 std::vector<DrmPlane *> primary_planes(primary_planes_);
528 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500529 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500530 if (ret) {
531 ALOGE("Failed to plan the composition ret=%d", ret);
532 return HWC2::Error::BadConfig;
533 }
534
535 // Disable the planes we're not using
536 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
537 composition->AddPlaneDisable(*i);
538 i = primary_planes.erase(i);
539 }
540 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
541 composition->AddPlaneDisable(*i);
542 i = overlay_planes.erase(i);
543 }
544
Robert Fossa1ade4e2017-09-27 19:28:15 +0200545 AddFenceToRetireFence(composition->take_out_fence());
546
Sean Pauled45a8e2017-02-28 13:17:34 -0500547 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500548 if (ret) {
549 ALOGE("Failed to apply the frame composition ret=%d", ret);
550 return HWC2::Error::BadParameter;
551 }
552
Sean Paulac874152016-03-10 16:00:26 -0500553 // The retire fence returned here is for the last frame, so return it and
554 // promote the next retire fence
555 *retire_fence = retire_fence_.Release();
556 retire_fence_ = std::move(next_retire_fence_);
557
558 ++frame_no_;
559 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500560}
561
562HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500563 supported(__func__);
564 auto mode =
565 std::find_if(connector_->modes().begin(), connector_->modes().end(),
566 [config](DrmMode const &m) { return m.id() == config; });
567 if (mode == connector_->modes().end()) {
568 ALOGE("Could not find active mode for %d", config);
569 return HWC2::Error::BadConfig;
570 }
571
572 std::unique_ptr<DrmDisplayComposition> composition =
573 compositor_.CreateComposition();
574 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
575 int ret = composition->SetDisplayMode(*mode);
Sean Pauled45a8e2017-02-28 13:17:34 -0500576 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500577 if (ret) {
578 ALOGE("Failed to queue dpms composition on %d", ret);
579 return HWC2::Error::BadConfig;
580 }
581 if (connector_->active_mode().id() == 0)
582 connector_->set_active_mode(*mode);
583
584 // Setup the client layer's dimensions
585 hwc_rect_t display_frame = {.left = 0,
586 .top = 0,
587 .right = static_cast<int>(mode->h_display()),
588 .bottom = static_cast<int>(mode->v_display())};
589 client_layer_.SetLayerDisplayFrame(display_frame);
590 hwc_frect_t source_crop = {.left = 0.0f,
591 .top = 0.0f,
592 .right = mode->h_display() + 0.0f,
593 .bottom = mode->v_display() + 0.0f};
594 client_layer_.SetLayerSourceCrop(source_crop);
595
596 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500597}
598
599HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
600 int32_t acquire_fence,
601 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600602 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500603 supported(__func__);
604 UniqueFd uf(acquire_fence);
605
606 client_layer_.set_buffer(target);
607 client_layer_.set_acquire_fence(uf.get());
608 client_layer_.SetLayerDataspace(dataspace);
609 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500610}
611
612HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500613 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800614
615 if (mode != HAL_COLOR_MODE_NATIVE)
616 return HWC2::Error::Unsupported;
617
618 color_mode_ = mode;
619 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500620}
621
622HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500623 int32_t hint) {
624 supported(__func__);
625 // TODO: Force client composition if we get this
Sean Pauled2ec4b2016-03-10 15:35:40 -0500626 return unsupported(__func__, matrix, hint);
627}
628
629HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500630 int32_t release_fence) {
631 supported(__func__);
632 // TODO: Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500633 return unsupported(__func__, buffer, release_fence);
634}
635
Sean Paulac874152016-03-10 16:00:26 -0500636HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
637 supported(__func__);
638 uint64_t dpms_value = 0;
639 auto mode = static_cast<HWC2::PowerMode>(mode_in);
640 switch (mode) {
641 case HWC2::PowerMode::Off:
642 dpms_value = DRM_MODE_DPMS_OFF;
643 break;
644 case HWC2::PowerMode::On:
645 dpms_value = DRM_MODE_DPMS_ON;
646 break;
647 default:
648 ALOGI("Power mode %d is unsupported\n", mode);
649 return HWC2::Error::Unsupported;
650 };
651
652 std::unique_ptr<DrmDisplayComposition> composition =
653 compositor_.CreateComposition();
654 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
655 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500656 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500657 if (ret) {
658 ALOGE("Failed to apply the dpms composition ret=%d", ret);
659 return HWC2::Error::BadParameter;
660 }
661 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500662}
663
664HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500665 supported(__func__);
666 vsync_worker_.VSyncControl(enabled);
667 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500668}
669
670HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500671 uint32_t *num_requests) {
672 supported(__func__);
673 *num_types = 0;
674 *num_requests = 0;
675 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
676 DrmHwcTwo::HwcLayer &layer = l.second;
677 switch (layer.sf_type()) {
678 case HWC2::Composition::SolidColor:
679 case HWC2::Composition::Cursor:
680 case HWC2::Composition::Sideband:
Rob Herringaf0d9752018-05-04 16:34:19 -0500681 default:
Sean Paulac874152016-03-10 16:00:26 -0500682 layer.set_validated_type(HWC2::Composition::Client);
683 ++*num_types;
684 break;
Sean Paulac874152016-03-10 16:00:26 -0500685 }
686 }
Rob Herringee8f45b2017-06-09 15:15:55 -0500687 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500688}
689
690HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -0500691 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800692 cursor_x_ = x;
693 cursor_y_ = y;
694 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500695}
696
697HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500698 supported(__func__);
699 blending_ = static_cast<HWC2::BlendMode>(mode);
700 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500701}
702
703HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500704 int32_t acquire_fence) {
705 supported(__func__);
706 UniqueFd uf(acquire_fence);
707
708 // The buffer and acquire_fence are handled elsewhere
709 if (sf_type_ == HWC2::Composition::Client ||
710 sf_type_ == HWC2::Composition::Sideband ||
711 sf_type_ == HWC2::Composition::SolidColor)
712 return HWC2::Error::None;
713
714 set_buffer(buffer);
715 set_acquire_fence(uf.get());
716 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500717}
718
719HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Sean Paulac874152016-03-10 16:00:26 -0500720 // TODO: Punt to client composition here?
Sean Pauled2ec4b2016-03-10 15:35:40 -0500721 return unsupported(__func__, color);
722}
723
724HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -0500725 sf_type_ = static_cast<HWC2::Composition>(type);
726 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500727}
728
729HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -0500730 supported(__func__);
731 dataspace_ = static_cast<android_dataspace_t>(dataspace);
732 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500733}
734
735HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -0500736 supported(__func__);
737 display_frame_ = frame;
738 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500739}
740
741HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -0500742 supported(__func__);
743 alpha_ = alpha;
744 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500745}
746
747HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
748 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -0500749 supported(__func__);
750 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -0500751 return unsupported(__func__, stream);
752}
753
754HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -0500755 supported(__func__);
756 source_crop_ = crop;
757 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500758}
759
760HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -0500761 supported(__func__);
762 // TODO: We don't use surface damage, marking as unsupported
763 unsupported(__func__, damage);
764 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500765}
766
767HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -0500768 supported(__func__);
769 transform_ = static_cast<HWC2::Transform>(transform);
770 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500771}
772
773HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -0500774 supported(__func__);
775 // TODO: We don't use this information, marking as unsupported
776 unsupported(__func__, visible);
777 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500778}
779
Sean Paulac874152016-03-10 16:00:26 -0500780HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
781 supported(__func__);
782 z_order_ = order;
783 return HWC2::Error::None;
784}
785
786void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
787 supported(__func__);
788 switch (blending_) {
789 case HWC2::BlendMode::None:
790 layer->blending = DrmHwcBlending::kNone;
791 break;
792 case HWC2::BlendMode::Premultiplied:
793 layer->blending = DrmHwcBlending::kPreMult;
794 break;
795 case HWC2::BlendMode::Coverage:
796 layer->blending = DrmHwcBlending::kCoverage;
797 break;
798 default:
799 ALOGE("Unknown blending mode b=%d", blending_);
800 layer->blending = DrmHwcBlending::kNone;
801 break;
802 }
803
804 OutputFd release_fence = release_fence_output();
805
806 layer->sf_handle = buffer_;
807 layer->acquire_fence = acquire_fence_.Release();
808 layer->release_fence = std::move(release_fence);
809 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +0200810 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -0500811 layer->SetSourceCrop(source_crop_);
812 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -0500813}
814
815// static
816int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
817 unsupported(__func__);
818 return 0;
819}
820
821// static
822void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -0500823 uint32_t *out_count,
824 int32_t * /*out_capabilities*/) {
825 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500826 *out_count = 0;
827}
828
829// static
Sean Paulac874152016-03-10 16:00:26 -0500830hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
831 struct hwc2_device * /*dev*/, int32_t descriptor) {
832 supported(__func__);
833 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500834 switch (func) {
835 // Device functions
836 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
837 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
838 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
839 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
840 int32_t*, hwc2_display_t *>);
841 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
842 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
843 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
844 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
845 case HWC2::FunctionDescriptor::Dump:
846 return ToHook<HWC2_PFN_DUMP>(
847 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
848 uint32_t *, char *>);
849 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
850 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
851 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
852 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
853 case HWC2::FunctionDescriptor::RegisterCallback:
854 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
855 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
856 &DrmHwcTwo::RegisterCallback, int32_t,
857 hwc2_callback_data_t, hwc2_function_pointer_t>);
858
859 // Display functions
860 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
861 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
862 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
863 &HwcDisplay::AcceptDisplayChanges>);
864 case HWC2::FunctionDescriptor::CreateLayer:
865 return ToHook<HWC2_PFN_CREATE_LAYER>(
866 DisplayHook<decltype(&HwcDisplay::CreateLayer),
867 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
868 case HWC2::FunctionDescriptor::DestroyLayer:
869 return ToHook<HWC2_PFN_DESTROY_LAYER>(
870 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
871 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
872 case HWC2::FunctionDescriptor::GetActiveConfig:
873 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
874 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
875 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
876 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
877 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
878 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
879 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
880 hwc2_layer_t *, int32_t *>);
881 case HWC2::FunctionDescriptor::GetClientTargetSupport:
882 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
883 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
884 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
885 int32_t, int32_t>);
886 case HWC2::FunctionDescriptor::GetColorModes:
887 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
888 DisplayHook<decltype(&HwcDisplay::GetColorModes),
889 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
890 case HWC2::FunctionDescriptor::GetDisplayAttribute:
891 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(DisplayHook<
892 decltype(&HwcDisplay::GetDisplayAttribute),
893 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t, int32_t *>);
894 case HWC2::FunctionDescriptor::GetDisplayConfigs:
895 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(DisplayHook<
896 decltype(&HwcDisplay::GetDisplayConfigs),
897 &HwcDisplay::GetDisplayConfigs, uint32_t *, hwc2_config_t *>);
898 case HWC2::FunctionDescriptor::GetDisplayName:
899 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
900 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
901 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
902 case HWC2::FunctionDescriptor::GetDisplayRequests:
903 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
904 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
905 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
906 hwc2_layer_t *, int32_t *>);
907 case HWC2::FunctionDescriptor::GetDisplayType:
908 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
909 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
910 &HwcDisplay::GetDisplayType, int32_t *>);
911 case HWC2::FunctionDescriptor::GetDozeSupport:
912 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
913 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
914 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -0500915 case HWC2::FunctionDescriptor::GetHdrCapabilities:
916 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
917 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
918 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
919 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500920 case HWC2::FunctionDescriptor::GetReleaseFences:
921 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
922 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
923 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
924 int32_t *>);
925 case HWC2::FunctionDescriptor::PresentDisplay:
926 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
927 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
928 &HwcDisplay::PresentDisplay, int32_t *>);
929 case HWC2::FunctionDescriptor::SetActiveConfig:
930 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
931 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
932 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
933 case HWC2::FunctionDescriptor::SetClientTarget:
934 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(DisplayHook<
935 decltype(&HwcDisplay::SetClientTarget), &HwcDisplay::SetClientTarget,
936 buffer_handle_t, int32_t, int32_t, hwc_region_t>);
937 case HWC2::FunctionDescriptor::SetColorMode:
938 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
939 DisplayHook<decltype(&HwcDisplay::SetColorMode),
940 &HwcDisplay::SetColorMode, int32_t>);
941 case HWC2::FunctionDescriptor::SetColorTransform:
942 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
943 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
944 &HwcDisplay::SetColorTransform, const float *, int32_t>);
945 case HWC2::FunctionDescriptor::SetOutputBuffer:
946 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
947 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
948 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
949 case HWC2::FunctionDescriptor::SetPowerMode:
950 return ToHook<HWC2_PFN_SET_POWER_MODE>(
951 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
952 &HwcDisplay::SetPowerMode, int32_t>);
953 case HWC2::FunctionDescriptor::SetVsyncEnabled:
954 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
955 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
956 &HwcDisplay::SetVsyncEnabled, int32_t>);
957 case HWC2::FunctionDescriptor::ValidateDisplay:
958 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
959 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
960 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
961
962 // Layer functions
963 case HWC2::FunctionDescriptor::SetCursorPosition:
964 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
965 LayerHook<decltype(&HwcLayer::SetCursorPosition),
966 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
967 case HWC2::FunctionDescriptor::SetLayerBlendMode:
968 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
969 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
970 &HwcLayer::SetLayerBlendMode, int32_t>);
971 case HWC2::FunctionDescriptor::SetLayerBuffer:
972 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
973 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
974 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
975 case HWC2::FunctionDescriptor::SetLayerColor:
976 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
977 LayerHook<decltype(&HwcLayer::SetLayerColor),
978 &HwcLayer::SetLayerColor, hwc_color_t>);
979 case HWC2::FunctionDescriptor::SetLayerCompositionType:
980 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
981 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
982 &HwcLayer::SetLayerCompositionType, int32_t>);
983 case HWC2::FunctionDescriptor::SetLayerDataspace:
984 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
985 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
986 &HwcLayer::SetLayerDataspace, int32_t>);
987 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
988 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
989 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
990 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
991 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
992 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
993 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
994 &HwcLayer::SetLayerPlaneAlpha, float>);
995 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
996 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(LayerHook<
997 decltype(&HwcLayer::SetLayerSidebandStream),
998 &HwcLayer::SetLayerSidebandStream, const native_handle_t *>);
999 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1000 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1001 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1002 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1003 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1004 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1005 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1006 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1007 case HWC2::FunctionDescriptor::SetLayerTransform:
1008 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1009 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1010 &HwcLayer::SetLayerTransform, int32_t>);
1011 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1012 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1013 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1014 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1015 case HWC2::FunctionDescriptor::SetLayerZOrder:
1016 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1017 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1018 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001019 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001020 default:
1021 return NULL;
1022 }
1023}
Sean Paulac874152016-03-10 16:00:26 -05001024
1025// static
1026int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1027 struct hw_device_t **dev) {
1028 supported(__func__);
1029 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1030 ALOGE("Invalid module name- %s", name);
1031 return -EINVAL;
1032 }
1033
1034 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1035 if (!ctx) {
1036 ALOGE("Failed to allocate DrmHwcTwo");
1037 return -ENOMEM;
1038 }
1039
1040 HWC2::Error err = ctx->Init();
1041 if (err != HWC2::Error::None) {
1042 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1043 return -EINVAL;
1044 }
1045
1046 ctx->common.module = const_cast<hw_module_t *>(module);
1047 *dev = &ctx->common;
1048 ctx.release();
1049 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001050}
Sean Paulac874152016-03-10 16:00:26 -05001051}
1052
1053static struct hw_module_methods_t hwc2_module_methods = {
1054 .open = android::DrmHwcTwo::HookDevOpen,
1055};
1056
1057hw_module_t HAL_MODULE_INFO_SYM = {
1058 .tag = HARDWARE_MODULE_TAG,
1059 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1060 .id = HWC_HARDWARE_MODULE_ID,
1061 .name = "DrmHwcTwo module",
1062 .author = "The Android Open Source Project",
1063 .methods = &hwc2_module_methods,
1064 .dso = NULL,
1065 .reserved = {0},
1066};