blob: dee74017244f261fa496d8438dff377cf115598e [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_);
529 ret = composition->Plan(compositor_.squash_state(), &primary_planes,
530 &overlay_planes);
531 if (ret) {
532 ALOGE("Failed to plan the composition ret=%d", ret);
533 return HWC2::Error::BadConfig;
534 }
535
536 // Disable the planes we're not using
537 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
538 composition->AddPlaneDisable(*i);
539 i = primary_planes.erase(i);
540 }
541 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
542 composition->AddPlaneDisable(*i);
543 i = overlay_planes.erase(i);
544 }
545
Robert Fossa1ade4e2017-09-27 19:28:15 +0200546 AddFenceToRetireFence(composition->take_out_fence());
547
Sean Pauled45a8e2017-02-28 13:17:34 -0500548 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500549 if (ret) {
550 ALOGE("Failed to apply the frame composition ret=%d", ret);
551 return HWC2::Error::BadParameter;
552 }
553
Sean Paulac874152016-03-10 16:00:26 -0500554 // The retire fence returned here is for the last frame, so return it and
555 // promote the next retire fence
556 *retire_fence = retire_fence_.Release();
557 retire_fence_ = std::move(next_retire_fence_);
558
559 ++frame_no_;
560 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500561}
562
563HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500564 supported(__func__);
565 auto mode =
566 std::find_if(connector_->modes().begin(), connector_->modes().end(),
567 [config](DrmMode const &m) { return m.id() == config; });
568 if (mode == connector_->modes().end()) {
569 ALOGE("Could not find active mode for %d", config);
570 return HWC2::Error::BadConfig;
571 }
572
573 std::unique_ptr<DrmDisplayComposition> composition =
574 compositor_.CreateComposition();
575 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
576 int ret = composition->SetDisplayMode(*mode);
Sean Pauled45a8e2017-02-28 13:17:34 -0500577 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500578 if (ret) {
579 ALOGE("Failed to queue dpms composition on %d", ret);
580 return HWC2::Error::BadConfig;
581 }
582 if (connector_->active_mode().id() == 0)
583 connector_->set_active_mode(*mode);
584
585 // Setup the client layer's dimensions
586 hwc_rect_t display_frame = {.left = 0,
587 .top = 0,
588 .right = static_cast<int>(mode->h_display()),
589 .bottom = static_cast<int>(mode->v_display())};
590 client_layer_.SetLayerDisplayFrame(display_frame);
591 hwc_frect_t source_crop = {.left = 0.0f,
592 .top = 0.0f,
593 .right = mode->h_display() + 0.0f,
594 .bottom = mode->v_display() + 0.0f};
595 client_layer_.SetLayerSourceCrop(source_crop);
596
597 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500598}
599
600HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
601 int32_t acquire_fence,
602 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600603 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500604 supported(__func__);
605 UniqueFd uf(acquire_fence);
606
607 client_layer_.set_buffer(target);
608 client_layer_.set_acquire_fence(uf.get());
609 client_layer_.SetLayerDataspace(dataspace);
610 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500611}
612
613HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500614 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800615
616 if (mode != HAL_COLOR_MODE_NATIVE)
617 return HWC2::Error::Unsupported;
618
619 color_mode_ = mode;
620 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500621}
622
623HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500624 int32_t hint) {
625 supported(__func__);
626 // TODO: Force client composition if we get this
Sean Pauled2ec4b2016-03-10 15:35:40 -0500627 return unsupported(__func__, matrix, hint);
628}
629
630HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500631 int32_t release_fence) {
632 supported(__func__);
633 // TODO: Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500634 return unsupported(__func__, buffer, release_fence);
635}
636
Sean Paulac874152016-03-10 16:00:26 -0500637HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
638 supported(__func__);
639 uint64_t dpms_value = 0;
640 auto mode = static_cast<HWC2::PowerMode>(mode_in);
641 switch (mode) {
642 case HWC2::PowerMode::Off:
643 dpms_value = DRM_MODE_DPMS_OFF;
644 break;
645 case HWC2::PowerMode::On:
646 dpms_value = DRM_MODE_DPMS_ON;
647 break;
648 default:
649 ALOGI("Power mode %d is unsupported\n", mode);
650 return HWC2::Error::Unsupported;
651 };
652
653 std::unique_ptr<DrmDisplayComposition> composition =
654 compositor_.CreateComposition();
655 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
656 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500657 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500658 if (ret) {
659 ALOGE("Failed to apply the dpms composition ret=%d", ret);
660 return HWC2::Error::BadParameter;
661 }
662 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500663}
664
665HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500666 supported(__func__);
667 vsync_worker_.VSyncControl(enabled);
668 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500669}
670
671HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500672 uint32_t *num_requests) {
673 supported(__func__);
674 *num_types = 0;
675 *num_requests = 0;
676 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
677 DrmHwcTwo::HwcLayer &layer = l.second;
678 switch (layer.sf_type()) {
679 case HWC2::Composition::SolidColor:
680 case HWC2::Composition::Cursor:
681 case HWC2::Composition::Sideband:
682 layer.set_validated_type(HWC2::Composition::Client);
683 ++*num_types;
684 break;
John Stultzacc4dcf2018-04-26 12:05:57 -0700685 case HWC2::Composition::Device:
686 if (!compositor_.uses_GL()) {
687 layer.set_validated_type(HWC2::Composition::Client);
688 ++*num_types;
689 break;
690 }
691 /* fall through */
Sean Paulac874152016-03-10 16:00:26 -0500692 default:
693 layer.set_validated_type(layer.sf_type());
694 break;
695 }
696 }
Rob Herringee8f45b2017-06-09 15:15:55 -0500697 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500698}
699
700HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -0500701 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800702 cursor_x_ = x;
703 cursor_y_ = y;
704 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500705}
706
707HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500708 supported(__func__);
709 blending_ = static_cast<HWC2::BlendMode>(mode);
710 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500711}
712
713HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500714 int32_t acquire_fence) {
715 supported(__func__);
716 UniqueFd uf(acquire_fence);
717
718 // The buffer and acquire_fence are handled elsewhere
719 if (sf_type_ == HWC2::Composition::Client ||
720 sf_type_ == HWC2::Composition::Sideband ||
721 sf_type_ == HWC2::Composition::SolidColor)
722 return HWC2::Error::None;
723
724 set_buffer(buffer);
725 set_acquire_fence(uf.get());
726 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500727}
728
729HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Sean Paulac874152016-03-10 16:00:26 -0500730 // TODO: Punt to client composition here?
Sean Pauled2ec4b2016-03-10 15:35:40 -0500731 return unsupported(__func__, color);
732}
733
734HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -0500735 sf_type_ = static_cast<HWC2::Composition>(type);
736 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500737}
738
739HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -0500740 supported(__func__);
741 dataspace_ = static_cast<android_dataspace_t>(dataspace);
742 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500743}
744
745HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -0500746 supported(__func__);
747 display_frame_ = frame;
748 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500749}
750
751HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -0500752 supported(__func__);
753 alpha_ = alpha;
754 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500755}
756
757HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
758 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -0500759 supported(__func__);
760 // TODO: We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -0500761 return unsupported(__func__, stream);
762}
763
764HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -0500765 supported(__func__);
766 source_crop_ = crop;
767 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500768}
769
770HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -0500771 supported(__func__);
772 // TODO: We don't use surface damage, marking as unsupported
773 unsupported(__func__, damage);
774 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500775}
776
777HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -0500778 supported(__func__);
779 transform_ = static_cast<HWC2::Transform>(transform);
780 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500781}
782
783HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -0500784 supported(__func__);
785 // TODO: We don't use this information, marking as unsupported
786 unsupported(__func__, visible);
787 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500788}
789
Sean Paulac874152016-03-10 16:00:26 -0500790HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
791 supported(__func__);
792 z_order_ = order;
793 return HWC2::Error::None;
794}
795
796void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
797 supported(__func__);
798 switch (blending_) {
799 case HWC2::BlendMode::None:
800 layer->blending = DrmHwcBlending::kNone;
801 break;
802 case HWC2::BlendMode::Premultiplied:
803 layer->blending = DrmHwcBlending::kPreMult;
804 break;
805 case HWC2::BlendMode::Coverage:
806 layer->blending = DrmHwcBlending::kCoverage;
807 break;
808 default:
809 ALOGE("Unknown blending mode b=%d", blending_);
810 layer->blending = DrmHwcBlending::kNone;
811 break;
812 }
813
814 OutputFd release_fence = release_fence_output();
815
816 layer->sf_handle = buffer_;
817 layer->acquire_fence = acquire_fence_.Release();
818 layer->release_fence = std::move(release_fence);
819 layer->SetDisplayFrame(display_frame_);
Stefan Schake025d0a62018-05-04 18:03:00 +0200820 layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f);
Sean Paulac874152016-03-10 16:00:26 -0500821 layer->SetSourceCrop(source_crop_);
822 layer->SetTransform(static_cast<int32_t>(transform_));
Sean Pauled2ec4b2016-03-10 15:35:40 -0500823}
824
825// static
826int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
827 unsupported(__func__);
828 return 0;
829}
830
831// static
832void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -0500833 uint32_t *out_count,
834 int32_t * /*out_capabilities*/) {
835 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500836 *out_count = 0;
837}
838
839// static
Sean Paulac874152016-03-10 16:00:26 -0500840hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
841 struct hwc2_device * /*dev*/, int32_t descriptor) {
842 supported(__func__);
843 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500844 switch (func) {
845 // Device functions
846 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
847 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
848 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
849 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
850 int32_t*, hwc2_display_t *>);
851 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
852 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
853 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
854 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
855 case HWC2::FunctionDescriptor::Dump:
856 return ToHook<HWC2_PFN_DUMP>(
857 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
858 uint32_t *, char *>);
859 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
860 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
861 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
862 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
863 case HWC2::FunctionDescriptor::RegisterCallback:
864 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
865 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
866 &DrmHwcTwo::RegisterCallback, int32_t,
867 hwc2_callback_data_t, hwc2_function_pointer_t>);
868
869 // Display functions
870 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
871 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
872 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
873 &HwcDisplay::AcceptDisplayChanges>);
874 case HWC2::FunctionDescriptor::CreateLayer:
875 return ToHook<HWC2_PFN_CREATE_LAYER>(
876 DisplayHook<decltype(&HwcDisplay::CreateLayer),
877 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
878 case HWC2::FunctionDescriptor::DestroyLayer:
879 return ToHook<HWC2_PFN_DESTROY_LAYER>(
880 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
881 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
882 case HWC2::FunctionDescriptor::GetActiveConfig:
883 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
884 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
885 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
886 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
887 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
888 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
889 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
890 hwc2_layer_t *, int32_t *>);
891 case HWC2::FunctionDescriptor::GetClientTargetSupport:
892 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
893 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
894 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
895 int32_t, int32_t>);
896 case HWC2::FunctionDescriptor::GetColorModes:
897 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
898 DisplayHook<decltype(&HwcDisplay::GetColorModes),
899 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
900 case HWC2::FunctionDescriptor::GetDisplayAttribute:
901 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(DisplayHook<
902 decltype(&HwcDisplay::GetDisplayAttribute),
903 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t, int32_t *>);
904 case HWC2::FunctionDescriptor::GetDisplayConfigs:
905 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(DisplayHook<
906 decltype(&HwcDisplay::GetDisplayConfigs),
907 &HwcDisplay::GetDisplayConfigs, uint32_t *, hwc2_config_t *>);
908 case HWC2::FunctionDescriptor::GetDisplayName:
909 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
910 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
911 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
912 case HWC2::FunctionDescriptor::GetDisplayRequests:
913 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
914 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
915 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
916 hwc2_layer_t *, int32_t *>);
917 case HWC2::FunctionDescriptor::GetDisplayType:
918 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
919 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
920 &HwcDisplay::GetDisplayType, int32_t *>);
921 case HWC2::FunctionDescriptor::GetDozeSupport:
922 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
923 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
924 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -0500925 case HWC2::FunctionDescriptor::GetHdrCapabilities:
926 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
927 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
928 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
929 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500930 case HWC2::FunctionDescriptor::GetReleaseFences:
931 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
932 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
933 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
934 int32_t *>);
935 case HWC2::FunctionDescriptor::PresentDisplay:
936 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
937 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
938 &HwcDisplay::PresentDisplay, int32_t *>);
939 case HWC2::FunctionDescriptor::SetActiveConfig:
940 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
941 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
942 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
943 case HWC2::FunctionDescriptor::SetClientTarget:
944 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(DisplayHook<
945 decltype(&HwcDisplay::SetClientTarget), &HwcDisplay::SetClientTarget,
946 buffer_handle_t, int32_t, int32_t, hwc_region_t>);
947 case HWC2::FunctionDescriptor::SetColorMode:
948 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
949 DisplayHook<decltype(&HwcDisplay::SetColorMode),
950 &HwcDisplay::SetColorMode, int32_t>);
951 case HWC2::FunctionDescriptor::SetColorTransform:
952 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
953 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
954 &HwcDisplay::SetColorTransform, const float *, int32_t>);
955 case HWC2::FunctionDescriptor::SetOutputBuffer:
956 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
957 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
958 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
959 case HWC2::FunctionDescriptor::SetPowerMode:
960 return ToHook<HWC2_PFN_SET_POWER_MODE>(
961 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
962 &HwcDisplay::SetPowerMode, int32_t>);
963 case HWC2::FunctionDescriptor::SetVsyncEnabled:
964 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
965 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
966 &HwcDisplay::SetVsyncEnabled, int32_t>);
967 case HWC2::FunctionDescriptor::ValidateDisplay:
968 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
969 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
970 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
971
972 // Layer functions
973 case HWC2::FunctionDescriptor::SetCursorPosition:
974 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
975 LayerHook<decltype(&HwcLayer::SetCursorPosition),
976 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
977 case HWC2::FunctionDescriptor::SetLayerBlendMode:
978 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
979 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
980 &HwcLayer::SetLayerBlendMode, int32_t>);
981 case HWC2::FunctionDescriptor::SetLayerBuffer:
982 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
983 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
984 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
985 case HWC2::FunctionDescriptor::SetLayerColor:
986 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
987 LayerHook<decltype(&HwcLayer::SetLayerColor),
988 &HwcLayer::SetLayerColor, hwc_color_t>);
989 case HWC2::FunctionDescriptor::SetLayerCompositionType:
990 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
991 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
992 &HwcLayer::SetLayerCompositionType, int32_t>);
993 case HWC2::FunctionDescriptor::SetLayerDataspace:
994 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
995 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
996 &HwcLayer::SetLayerDataspace, int32_t>);
997 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
998 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
999 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1000 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1001 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1002 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1003 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1004 &HwcLayer::SetLayerPlaneAlpha, float>);
1005 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
1006 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(LayerHook<
1007 decltype(&HwcLayer::SetLayerSidebandStream),
1008 &HwcLayer::SetLayerSidebandStream, const native_handle_t *>);
1009 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1010 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1011 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1012 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1013 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1014 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1015 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1016 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1017 case HWC2::FunctionDescriptor::SetLayerTransform:
1018 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1019 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1020 &HwcLayer::SetLayerTransform, int32_t>);
1021 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1022 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1023 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1024 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1025 case HWC2::FunctionDescriptor::SetLayerZOrder:
1026 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1027 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1028 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001029 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001030 default:
1031 return NULL;
1032 }
1033}
Sean Paulac874152016-03-10 16:00:26 -05001034
1035// static
1036int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1037 struct hw_device_t **dev) {
1038 supported(__func__);
1039 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1040 ALOGE("Invalid module name- %s", name);
1041 return -EINVAL;
1042 }
1043
1044 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1045 if (!ctx) {
1046 ALOGE("Failed to allocate DrmHwcTwo");
1047 return -ENOMEM;
1048 }
1049
1050 HWC2::Error err = ctx->Init();
1051 if (err != HWC2::Error::None) {
1052 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1053 return -EINVAL;
1054 }
1055
1056 ctx->common.module = const_cast<hw_module_t *>(module);
1057 *dev = &ctx->common;
1058 ctx.release();
1059 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001060}
Sean Paulac874152016-03-10 16:00:26 -05001061}
1062
1063static struct hw_module_methods_t hwc2_module_methods = {
1064 .open = android::DrmHwcTwo::HookDevOpen,
1065};
1066
1067hw_module_t HAL_MODULE_INFO_SYM = {
1068 .tag = HARDWARE_MODULE_TAG,
1069 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1070 .id = HWC_HARDWARE_MODULE_ID,
1071 .name = "DrmHwcTwo module",
1072 .author = "The Android Open Source Project",
1073 .methods = &hwc2_module_methods,
1074 .dso = NULL,
1075 .reserved = {0},
1076};