OSDN Git Service

drm_hwcomposer: allow to force mode by a property (v5)
[android-x86/external-drm_hwcomposer.git] / bufferinfo / BufferInfoMapperMetadata.cpp
1 /*
2  * Copyright (C) 2020 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 #if PLATFORM_SDK_VERSION >= 30
18
19 #define LOG_TAG "hwc-bufferinfo-mappermetadata"
20
21 #include "BufferInfoMapperMetadata.h"
22
23 #include <drm/drm_fourcc.h>
24 #include <ui/GraphicBufferMapper.h>
25 #include <xf86drm.h>
26 #include <xf86drmMode.h>
27
28 #include <cinttypes>
29
30 #include "utils/log.h"
31
32 namespace android {
33
34 BufferInfoGetter *BufferInfoMapperMetadata::CreateInstance() {
35   if (GraphicBufferMapper::getInstance().getMapperVersion() <
36       GraphicBufferMapper::GRALLOC_4)
37     return nullptr;
38
39   return new BufferInfoMapperMetadata();
40 }
41
42 /* The implementation below makes assumptions on the order and number of file
43  * descriptors that Gralloc places in the native_handle_t and as such it very
44  * likely needs to be adapted to match the particular Gralloc implementation
45  * used in the system. For this reason it is been declared as a weak symbol,
46  * so that it can be overridden.
47  */
48 int __attribute__((weak))
49 BufferInfoMapperMetadata::GetFds(buffer_handle_t handle, hwc_drm_bo_t *bo) {
50   int fd_index = 0;
51
52   if (handle->numFds <= 0) {
53     ALOGE("Handle has no fds");
54     return android::BAD_VALUE;
55   }
56
57   for (int i = 0; i < kHwcDrmBoMaxPlanes; i++) {
58     /* If no size, we're out of usable planes */
59     if (bo->sizes[i] <= 0) {
60       if (i == 0) {
61         ALOGE("Bad handle metadata");
62         return android::BAD_VALUE;
63       }
64       break;
65     }
66
67     /*
68      * If the offset is zero, its multi-buffer
69      * so move to the next fd
70      */
71     if (i != 0 && bo->offsets[i] == 0) {
72       fd_index++;
73       if (fd_index >= handle->numFds) {
74         ALOGE("Handle has no more fds");
75         return android::BAD_VALUE;
76       }
77     }
78
79     bo->prime_fds[i] = handle->data[fd_index];
80     if (bo->prime_fds[i] <= 0) {
81       ALOGE("Invalid prime fd");
82       return android::BAD_VALUE;
83     }
84   }
85
86   return 0;
87 }
88
89 int BufferInfoMapperMetadata::ConvertBoInfo(buffer_handle_t handle,
90                                             hwc_drm_bo_t *bo) {
91   GraphicBufferMapper &mapper = GraphicBufferMapper::getInstance();
92   if (handle == nullptr)
93     return -EINVAL;
94
95   uint64_t usage = 0;
96   int err = mapper.getUsage(handle, &usage);
97   if (err != 0) {
98     ALOGE("Failed to get usage err=%d", err);
99     return err;
100   }
101   bo->usage = static_cast<uint32_t>(usage);
102
103   ui::PixelFormat hal_format;
104   err = mapper.getPixelFormatRequested(handle, &hal_format);
105   if (err != 0) {
106     ALOGE("Failed to get HAL Pixel Format err=%d", err);
107     return err;
108   }
109   bo->hal_format = static_cast<uint32_t>(hal_format);
110
111   err = mapper.getPixelFormatFourCC(handle, &bo->format);
112   if (err != 0) {
113     ALOGE("Failed to get FourCC format err=%d", err);
114     return err;
115   }
116
117   err = mapper.getPixelFormatModifier(handle, &bo->modifiers[0]);
118   if (err != 0) {
119     ALOGE("Failed to get DRM Modifier err=%d", err);
120     return err;
121   }
122
123   uint64_t width = 0;
124   err = mapper.getWidth(handle, &width);
125   if (err != 0) {
126     ALOGE("Failed to get Width err=%d", err);
127     return err;
128   }
129   bo->width = static_cast<uint32_t>(width);
130
131   uint64_t height = 0;
132   err = mapper.getHeight(handle, &height);
133   if (err != 0) {
134     ALOGE("Failed to get Height err=%d", err);
135     return err;
136   }
137   bo->height = static_cast<uint32_t>(height);
138
139   std::vector<ui::PlaneLayout> layouts;
140   err = mapper.getPlaneLayouts(handle, &layouts);
141   if (err != 0) {
142     ALOGE("Failed to get Plane Layouts err=%d", err);
143     return err;
144   }
145
146   for (uint32_t i = 0; i < layouts.size(); i++) {
147     bo->modifiers[i] = bo->modifiers[0];
148     bo->pitches[i] = layouts[i].strideInBytes;
149     bo->offsets[i] = layouts[i].offsetInBytes;
150     bo->sizes[i] = layouts[i].totalSizeInBytes;
151   }
152
153   return GetFds(handle, bo);
154 }
155
156 }  // namespace android
157
158 #endif