• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "hwc-bufferinfo-minigbm"
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 
ConvertBoInfo(buffer_handle_t handle,hwc_drm_bo_t * bo)46 int BufferInfoMinigbm::ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) {
47   if (handle == nullptr) {
48     return -EINVAL;
49   }
50 
51   uint32_t width{};
52   uint32_t height{};
53   if (gralloc_->perform(gralloc_, CROS_GRALLOC_DRM_GET_DIMENSIONS, handle,
54                         &width, &height) != 0) {
55     ALOGE(
56         "CROS_GRALLOC_DRM_GET_DIMENSIONS operation has failed. "
57         "Please ensure you are using the latest minigbm.");
58     return -EINVAL;
59   }
60 
61   int32_t droid_format{};
62   if (gralloc_->perform(gralloc_, CROS_GRALLOC_DRM_GET_FORMAT, handle,
63                         &droid_format) != 0) {
64     ALOGE(
65         "CROS_GRALLOC_DRM_GET_FORMAT operation has failed. "
66         "Please ensure you are using the latest minigbm.");
67     return -EINVAL;
68   }
69 
70   uint32_t usage{};
71   if (gralloc_->perform(gralloc_, CROS_GRALLOC_DRM_GET_USAGE, handle, &usage) !=
72       0) {
73     ALOGE(
74         "CROS_GRALLOC_DRM_GET_USAGE operation has failed. "
75         "Please ensure you are using the latest minigbm.");
76     return -EINVAL;
77   }
78 
79   struct cros_gralloc0_buffer_info info {};
80   if (gralloc_->perform(gralloc_, CROS_GRALLOC_DRM_GET_BUFFER_INFO, handle,
81                         &info) != 0) {
82     ALOGE(
83         "CROS_GRALLOC_DRM_GET_BUFFER_INFO operation has failed. "
84         "Please ensure you are using the latest minigbm.");
85     return -EINVAL;
86   }
87 
88   bo->width = width;
89   bo->height = height;
90 
91   bo->hal_format = droid_format;
92 
93   bo->format = info.drm_fourcc;
94   bo->usage = usage;
95 
96   for (int i = 0; i < info.num_fds; i++) {
97     bo->modifiers[i] = info.modifier;
98     bo->prime_fds[i] = info.fds[i];
99     bo->pitches[i] = info.stride[i];
100     bo->offsets[i] = info.offset[i];
101   }
102 
103   return 0;
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