xref: /aosp_15_r20/external/drm_hwcomposer/bufferinfo/legacy/BufferInfoMinigbm.cpp (revision 0a9764fe0a15e71ebbeb85e87e10990c23aab47f)
1 /*
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 
17 #define LOG_TAG "drmhwc"
18 
19 #include "BufferInfoMinigbm.h"
20 
21 #include <xf86drm.h>
22 #include <xf86drmMode.h>
23 
24 #include <cerrno>
25 #include <cstring>
26 
27 #include "utils/log.h"
28 namespace android {
29 
30 LEGACY_BUFFER_INFO_GETTER(BufferInfoMinigbm);
31 
32 constexpr int CROS_GRALLOC_DRM_GET_FORMAT = 1;
33 constexpr int CROS_GRALLOC_DRM_GET_DIMENSIONS = 2;
34 constexpr int CROS_GRALLOC_DRM_GET_BUFFER_INFO = 4;
35 constexpr int CROS_GRALLOC_DRM_GET_USAGE = 5;
36 
37 struct 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 
GetBoInfo(buffer_handle_t handle)46 auto BufferInfoMinigbm::GetBoInfo(buffer_handle_t handle)
47     -> std::optional<BufferInfo> {
48   if (handle == nullptr) {
49     return {};
50   }
51 
52   BufferInfo bi{};
53 
54   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.");
61     return {};
62   }
63 
64   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.");
70     return {};
71   }
72 
73   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.");
79     return {};
80   }
81 
82   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.");
88     return {};
89   }
90 
91   bi.width = width;
92   bi.height = height;
93 
94   bi.format = info.drm_fourcc;
95 
96   for (int i = 0; i < info.num_fds; i++) {
97     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];
101   }
102 
103   return bi;
104 }
105 
106 constexpr char cros_gralloc_module_name[] = "CrOS Gralloc";
107 
ValidateGralloc()108 int 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 
115   if (gralloc_->perform == nullptr) {
116     ALOGE(
117         "CrOS gralloc has no perform call implemented. Please upgrade your "
118         "minigbm.");
119     return -EINVAL;
120   }
121 
122   return 0;
123 }
124 
125 }  // namespace android
126