1 /*
2 * Copyright (C) 2021 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 #include <inttypes.h>
17 #include <android-base/parseint.h>
18 #include <android-base/properties.h>
19 #include <android-base/strings.h>
20 #include <log/log.h>
21 #include <gralloc_cb_bp.h>
22 #include <cb_handle_30.h>
23 #include <xf86drm.h>
24
25 #include <C2AllocatorGralloc.h>
26
27 #include "cros_gralloc_handle.h"
28 #include "virtgpu_drm.h"
29
isMinigbmFromProperty()30 static bool isMinigbmFromProperty() {
31 static constexpr const auto kGrallocProp = "ro.hardware.gralloc";
32
33 const auto grallocProp = android::base::GetProperty(kGrallocProp, "");
34 ALOGD("%s:codecs: minigbm query prop value is: %s", __FUNCTION__, grallocProp.c_str());
35
36 if (grallocProp == "minigbm") {
37 ALOGD("%s:codecs: Using minigbm, in minigbm mode.\n", __FUNCTION__);
38 return true;
39 } else {
40 ALOGD("%s:codecs: Is not using minigbm, in goldfish mode.\n", __FUNCTION__);
41 return false;
42 }
43 }
44
45 class ColorBufferUtilsGlobalState {
46 public:
ColorBufferUtilsGlobalState()47 ColorBufferUtilsGlobalState() {
48 m_isMinigbm = isMinigbmFromProperty();
49
50 if (m_isMinigbm) {
51 static constexpr int kRendernodeMinor = 128;
52 m_rendernodeFd = drmOpenRender(kRendernodeMinor);
53 }
54 }
55
getColorBufferHandle(native_handle_t const * handle)56 uint32_t getColorBufferHandle(native_handle_t const* handle) {
57 if (m_isMinigbm) {
58 struct drm_virtgpu_resource_info info;
59 if (!getResInfo(handle, &info)) {
60 ALOGE("%s: Error gtting color buffer handle (minigbm case)", __func__);
61 return -1;
62 }
63 return info.res_handle;
64 } else {
65 return cb_handle_t::from(handle)->hostHandle;
66 }
67 }
68
69 private:
70
getResInfo(native_handle_t const * handle,struct drm_virtgpu_resource_info * info)71 bool getResInfo(native_handle_t const* handle,
72 struct drm_virtgpu_resource_info* info) {
73 memset(info, 0x0, sizeof(*info));
74 if (m_rendernodeFd < 0) {
75 ALOGE("%s: Error, rendernode fd missing\n", __func__);
76 return false;
77 }
78
79 struct drm_gem_close gem_close;
80 memset(&gem_close, 0x0, sizeof(gem_close));
81
82 cros_gralloc_handle const* cros_handle =
83 reinterpret_cast<cros_gralloc_handle const*>(handle);
84
85 uint32_t prime_handle;
86 int ret = drmPrimeFDToHandle(m_rendernodeFd, cros_handle->fds[0], &prime_handle);
87 if (ret) {
88 ALOGE("%s: DRM_IOCTL_PRIME_FD_TO_HANDLE failed: %s (errno %d)\n",
89 __func__, strerror(errno), errno);
90 return false;
91 }
92
93 info->bo_handle = prime_handle;
94 gem_close.handle = prime_handle;
95
96 ret = drmIoctl(m_rendernodeFd, DRM_IOCTL_VIRTGPU_RESOURCE_INFO, info);
97 if (ret) {
98 ALOGE("%s: DRM_IOCTL_VIRTGPU_RESOURCE_INFO failed: %s (errno %d)\n",
99 __func__, strerror(errno), errno);
100 drmIoctl(m_rendernodeFd, DRM_IOCTL_GEM_CLOSE, &gem_close);
101 return false;
102 }
103
104 drmIoctl(m_rendernodeFd, DRM_IOCTL_GEM_CLOSE, &gem_close);
105 return true;
106 }
107
108 bool m_isMinigbm;
109 int m_rendernodeFd = -1; // to be closed when this process dies
110 };
111
getGlobals()112 static ColorBufferUtilsGlobalState* getGlobals() {
113 static ColorBufferUtilsGlobalState* globals = new ColorBufferUtilsGlobalState;
114 return globals;
115 }
116
getColorBufferHandle(native_handle_t const * handle)117 uint32_t getColorBufferHandle(native_handle_t const* handle) {
118 return getGlobals()->getColorBufferHandle(handle);
119 }
120
getClientUsage(const std::shared_ptr<C2BlockPool> & pool)121 uint64_t getClientUsage(const std::shared_ptr<C2BlockPool> &pool) {
122 std::shared_ptr<C2GraphicBlock> myOutBlock;
123 const C2MemoryUsage usage = {0, 0};
124 const uint32_t format = HAL_PIXEL_FORMAT_YCBCR_420_888;
125 pool->fetchGraphicBlock(2, 2, format, usage, &myOutBlock);
126 auto myc2Handle = myOutBlock->handle();
127 native_handle_t *mygrallocHandle =
128 android::UnwrapNativeCodec2GrallocHandle(myc2Handle);
129 cb_handle_30_t* mycb = (cb_handle_30_t*)(mygrallocHandle);
130 ALOGV("%s %s %d: client usage 0x%x", __FILE__, __func__, __LINE__, mycb->usage);
131 return mycb->usage;
132 }
133
134