blob: c5a9e98be65a17f906a85bdcabd0a4d8ea16ee0c [file] [log] [blame]
Alistair Strachan71edaca2018-05-02 17:01:49 -07001/*
2 * Copyright (C) 2018 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
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030017#define LOG_TAG "hwc-bufferinfo-minigbm"
Alistair Strachan71edaca2018-05-02 17:01:49 -070018
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030019#include "BufferInfoMinigbm.h"
Alistair Strachan71edaca2018-05-02 17:01:49 -070020
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030021#include <xf86drm.h>
22#include <xf86drmMode.h>
Alistair Strachan71edaca2018-05-02 17:01:49 -070023
Roman Stratiienko1e053b42021-10-25 22:54:20 +030024#include <cerrno>
Roman Stratiienko39d8f7e2021-11-30 17:06:44 +020025#include <cstring>
Roman Stratiienko1e053b42021-10-25 22:54:20 +030026
Roman Stratiienkod21071f2021-03-09 21:56:50 +020027#include "utils/log.h"
Alistair Strachan71edaca2018-05-02 17:01:49 -070028namespace android {
29
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030030LEGACY_BUFFER_INFO_GETTER(BufferInfoMinigbm);
Alistair Strachan71edaca2018-05-02 17:01:49 -070031
Roman Stratiienko5621f5f2021-11-30 17:48:16 +020032constexpr int CROS_GRALLOC_DRM_GET_FORMAT = 1;
33constexpr int CROS_GRALLOC_DRM_GET_DIMENSIONS = 2;
34constexpr int CROS_GRALLOC_DRM_GET_BUFFER_INFO = 4;
35constexpr int CROS_GRALLOC_DRM_GET_USAGE = 5;
36
37struct cros_gralloc0_buffer_info {
38 uint32_t drm_fourcc;
39 int num_fds;
40 int fds[4];
41 uint64_t modifier;
42 int offset[4];
43 int stride[4];
44};
45
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +020046auto BufferInfoMinigbm::GetBoInfo(buffer_handle_t handle)
47 -> std::optional<BufferInfo> {
Roman Stratiienko5621f5f2021-11-30 17:48:16 +020048 if (handle == nullptr) {
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +020049 return {};
Roman Stratiienko5621f5f2021-11-30 17:48:16 +020050 }
Alistair Strachan71edaca2018-05-02 17:01:49 -070051
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +020052 BufferInfo bi{};
53
Roman Stratiienko5621f5f2021-11-30 17:48:16 +020054 uint32_t width{};
55 uint32_t height{};
56 if (gralloc_->perform(gralloc_, CROS_GRALLOC_DRM_GET_DIMENSIONS, handle,
57 &width, &height) != 0) {
58 ALOGE(
59 "CROS_GRALLOC_DRM_GET_DIMENSIONS operation has failed. "
60 "Please ensure you are using the latest minigbm.");
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +020061 return {};
Roman Stratiienko5621f5f2021-11-30 17:48:16 +020062 }
Roman Stratiienko6e6a93a2020-12-13 17:41:04 +020063
Roman Stratiienko5621f5f2021-11-30 17:48:16 +020064 int32_t droid_format{};
65 if (gralloc_->perform(gralloc_, CROS_GRALLOC_DRM_GET_FORMAT, handle,
66 &droid_format) != 0) {
67 ALOGE(
68 "CROS_GRALLOC_DRM_GET_FORMAT operation has failed. "
69 "Please ensure you are using the latest minigbm.");
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +020070 return {};
Roman Stratiienko5621f5f2021-11-30 17:48:16 +020071 }
Roman Stratiienko6e6a93a2020-12-13 17:41:04 +020072
Roman Stratiienko5621f5f2021-11-30 17:48:16 +020073 uint32_t usage{};
74 if (gralloc_->perform(gralloc_, CROS_GRALLOC_DRM_GET_USAGE, handle, &usage) !=
75 0) {
76 ALOGE(
77 "CROS_GRALLOC_DRM_GET_USAGE operation has failed. "
78 "Please ensure you are using the latest minigbm.");
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +020079 return {};
Roman Stratiienko5621f5f2021-11-30 17:48:16 +020080 }
Roman Stratiienko6e6a93a2020-12-13 17:41:04 +020081
Roman Stratiienko5621f5f2021-11-30 17:48:16 +020082 struct cros_gralloc0_buffer_info info {};
83 if (gralloc_->perform(gralloc_, CROS_GRALLOC_DRM_GET_BUFFER_INFO, handle,
84 &info) != 0) {
85 ALOGE(
86 "CROS_GRALLOC_DRM_GET_BUFFER_INFO operation has failed. "
87 "Please ensure you are using the latest minigbm.");
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +020088 return {};
Roman Stratiienko5621f5f2021-11-30 17:48:16 +020089 }
90
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +020091 bi.width = width;
92 bi.height = height;
Roman Stratiienko5621f5f2021-11-30 17:48:16 +020093
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +020094 bi.format = info.drm_fourcc;
Roman Stratiienko5621f5f2021-11-30 17:48:16 +020095
96 for (int i = 0; i < info.num_fds; i++) {
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +020097 bi.modifiers[i] = info.modifier;
98 bi.prime_fds[i] = info.fds[i];
99 bi.pitches[i] = info.stride[i];
100 bi.offsets[i] = info.offset[i];
Roman Stratiienko6e6a93a2020-12-13 17:41:04 +0200101 }
Alistair Strachan71edaca2018-05-02 17:01:49 -0700102
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200103 return bi;
Alistair Strachan71edaca2018-05-02 17:01:49 -0700104}
105
Roman Stratiienko39d8f7e2021-11-30 17:06:44 +0200106constexpr char cros_gralloc_module_name[] = "CrOS Gralloc";
107
108int BufferInfoMinigbm::ValidateGralloc() {
109 if (strcmp(gralloc_->common.name, cros_gralloc_module_name) != 0) {
110 ALOGE("Gralloc name isn't valid: Expected: \"%s\", Actual: \"%s\"",
111 cros_gralloc_module_name, gralloc_->common.name);
112 return -EINVAL;
113 }
114
Roman Stratiienko5621f5f2021-11-30 17:48:16 +0200115 if (gralloc_->perform == nullptr) {
116 ALOGE(
117 "CrOS gralloc has no perform call implemented. Please upgrade your "
118 "minigbm.");
119 return -EINVAL;
120 }
121
Roman Stratiienko39d8f7e2021-11-30 17:06:44 +0200122 return 0;
123}
124
Sean Paulf72cccd2018-08-27 13:59:08 -0400125} // namespace android