• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <inttypes.h>
25 #include <log/log.h>
26 #include <ui/GraphicBufferMapper.h>
27 #include <xf86drm.h>
28 #include <xf86drmMode.h>
29 
30 using android::hardware::graphics::common::V1_1::BufferUsage;
31 
32 namespace android {
33 
CreateInstance()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))
GetFds(buffer_handle_t handle,hwc_drm_bo_t * bo)49 BufferInfoMapperMetadata::GetFds(buffer_handle_t handle, hwc_drm_bo_t *bo) {
50   int num_fds = handle->numFds;
51 
52   if (num_fds >= 1 && num_fds <= 2) {
53     if (IsDrmFormatRgb(bo->format)) {
54       bo->prime_fds[0] = handle->data[0];
55     } else {
56       bo->prime_fds[0] = bo->prime_fds[1] = bo->prime_fds[2] = handle->data[0];
57     }
58     if (bo->prime_fds[0] <= 0) {
59       ALOGE("Encountered invalid fd %d", bo->prime_fds[0]);
60       return android::BAD_VALUE;
61     }
62 
63   } else if (num_fds >= 3) {
64     bo->prime_fds[0] = handle->data[0];
65     bo->prime_fds[1] = handle->data[1];
66     bo->prime_fds[2] = handle->data[2];
67     for (int i = 0; i < 3; i++) {
68       if (bo->prime_fds[i] <= 0) {
69         ALOGE("Encountered invalid fd %d", bo->prime_fds[i]);
70         return android::BAD_VALUE;
71       }
72     }
73   }
74   return 0;
75 }
76 
ConvertBoInfo(buffer_handle_t handle,hwc_drm_bo_t * bo)77 int BufferInfoMapperMetadata::ConvertBoInfo(buffer_handle_t handle,
78                                             hwc_drm_bo_t *bo) {
79   GraphicBufferMapper &mapper = GraphicBufferMapper::getInstance();
80   if (!handle)
81     return -EINVAL;
82 
83   uint64_t usage = 0;
84   int err = mapper.getUsage(handle, &usage);
85   if (err) {
86     ALOGE("Failed to get usage err=%d", err);
87     return err;
88   }
89   bo->usage = static_cast<uint32_t>(usage);
90 
91   ui::PixelFormat hal_format;
92   err = mapper.getPixelFormatRequested(handle, &hal_format);
93   if (err) {
94     ALOGE("Failed to get HAL Pixel Format err=%d", err);
95     return err;
96   }
97   bo->hal_format = static_cast<uint32_t>(hal_format);
98 
99   err = mapper.getPixelFormatFourCC(handle, &bo->format);
100   if (err) {
101     ALOGE("Failed to get FourCC format err=%d", err);
102     return err;
103   }
104 
105   err = mapper.getPixelFormatModifier(handle, &bo->modifiers[0]);
106   if (err) {
107     ALOGE("Failed to get DRM Modifier err=%d", err);
108     return err;
109   }
110   bo->with_modifiers = true;
111 
112   uint64_t width = 0;
113   err = mapper.getWidth(handle, &width);
114   if (err) {
115     ALOGE("Failed to get Width err=%d", err);
116     return err;
117   }
118   bo->width = static_cast<uint32_t>(width);
119 
120   uint64_t height = 0;
121   err = mapper.getHeight(handle, &height);
122   if (err) {
123     ALOGE("Failed to get Height err=%d", err);
124     return err;
125   }
126   bo->height = static_cast<uint32_t>(height);
127 
128   std::vector<ui::PlaneLayout> layouts;
129   err = mapper.getPlaneLayouts(handle, &layouts);
130   if (err) {
131     ALOGE("Failed to get Plane Layouts err=%d", err);
132     return err;
133   }
134 
135   for (uint32_t i = 0; i < layouts.size(); i++) {
136     bo->modifiers[i] = bo->modifiers[0];
137     bo->pitches[i] = layouts[i].strideInBytes;
138     bo->offsets[i] = layouts[i].offsetInBytes;
139   }
140 
141   return GetFds(handle, bo);
142 }
143 
144 }  // namespace android
145 
146 #endif
147