1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "GrallocAndroid.hpp"
16 #include "Debug.hpp"
17
18 #ifdef HAVE_GRALLOC1
19 #include <sync/sync.h>
20 #endif
21 #ifdef HAVE_GRALLOC4
22 using V4Error = android::hardware::graphics::mapper::V4_0::Error;
23 using V4Mapper = android::hardware::graphics::mapper::V4_0::IMapper;
24 using android::hardware::hidl_handle;
25 #endif
26
getInstance()27 GrallocModule *GrallocModule::getInstance()
28 {
29 static GrallocModule instance;
30 return &instance;
31 }
32
GrallocModule()33 GrallocModule::GrallocModule()
34 {
35 #ifdef HAVE_GRALLOC4
36 m_gralloc4_mapper = V4Mapper::getService();
37 if (m_gralloc4_mapper != nullptr)
38 {
39 return;
40 }
41 #endif
42
43 const hw_module_t *module = nullptr;
44 hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
45
46 m_major_version = (module->module_api_version >> 8) & 0xff;
47 switch(m_major_version)
48 {
49 case 0:
50 m_module = reinterpret_cast<const gralloc_module_t*>(module);
51 break;
52 case 1:
53 #ifdef HAVE_GRALLOC1
54 gralloc1_open(module, &m_gralloc1_device);
55 m_gralloc1_lock = (GRALLOC1_PFN_LOCK)m_gralloc1_device->getFunction(m_gralloc1_device, GRALLOC1_FUNCTION_LOCK);
56 m_gralloc1_unlock = (GRALLOC1_PFN_UNLOCK)m_gralloc1_device->getFunction(m_gralloc1_device, GRALLOC1_FUNCTION_UNLOCK);
57 break;
58 #endif
59 default:
60 TRACE("unknown gralloc major version (%d)", m_major_version);
61 break;
62 }
63 }
64
import(buffer_handle_t handle,buffer_handle_t * imported_handle)65 int GrallocModule::import(buffer_handle_t handle, buffer_handle_t* imported_handle)
66 {
67 #ifdef HAVE_GRALLOC4
68 if (m_gralloc4_mapper != nullptr)
69 {
70 V4Error error;
71 auto ret = m_gralloc4_mapper->importBuffer(handle,
72 [&](const auto& tmp_err, const auto& tmp_buf) {
73 error = tmp_err;
74 if (error == V4Error::NONE)
75 {
76 *imported_handle = static_cast<buffer_handle_t>(tmp_buf);
77 }
78 });
79 return ret.isOk() && error == V4Error::NONE ? 0 : -1;
80 }
81 #endif
82
83 *imported_handle = handle;
84 return 0;
85 }
86
release(buffer_handle_t handle)87 int GrallocModule::release(buffer_handle_t handle)
88 {
89 #ifdef HAVE_GRALLOC4
90 if (m_gralloc4_mapper != nullptr)
91 {
92 native_handle_t* native_handle = const_cast<native_handle_t*>(handle);
93 return m_gralloc4_mapper->freeBuffer(native_handle).isOk() ? 0 : 1;
94 }
95 #endif
96
97 return 0;
98 }
99
lock(buffer_handle_t handle,int usage,int left,int top,int width,int height,void ** vaddr)100 int GrallocModule::lock(buffer_handle_t handle, int usage, int left, int top, int width, int height, void **vaddr)
101 {
102 #ifdef HAVE_GRALLOC4
103 if (m_gralloc4_mapper != nullptr)
104 {
105 native_handle_t* native_handle = const_cast<native_handle_t*>(handle);
106
107 V4Mapper::Rect rect;
108 rect.left = left;
109 rect.top = top;
110 rect.width = width;
111 rect.height = height;
112
113 hidl_handle empty_fence_handle;
114
115 V4Error error;
116 auto ret = m_gralloc4_mapper->lock(native_handle, usage, rect, empty_fence_handle,
117 [&](const auto& tmp_err, const auto& tmp_vaddr) {
118 error = tmp_err;
119 if (tmp_err == V4Error::NONE)
120 {
121 *vaddr = tmp_vaddr;
122 }
123 });
124 return ret.isOk() && error == V4Error::NONE ? 0 : -1;
125 }
126 #endif
127
128 switch(m_major_version)
129 {
130 case 0:
131 {
132 return m_module->lock(m_module, handle, usage, left, top, width, height, vaddr);
133 }
134 case 1:
135 #ifdef HAVE_GRALLOC1
136 {
137 gralloc1_rect_t outRect{};
138 outRect.left = left;
139 outRect.top = top;
140 outRect.width = width;
141 outRect.height = height;
142 return m_gralloc1_lock(m_gralloc1_device, handle, usage, usage, &outRect, vaddr, -1);
143 }
144 #endif
145 default:
146 {
147 TRACE("no gralloc module to lock");
148 return -1;
149 }
150 }
151 }
152
unlock(buffer_handle_t handle)153 int GrallocModule::unlock(buffer_handle_t handle)
154 {
155 #ifdef HAVE_GRALLOC4
156 if (m_gralloc4_mapper != nullptr)
157 {
158 native_handle_t* native_handle = const_cast<native_handle_t*>(handle);
159
160 V4Error error;
161 auto ret = m_gralloc4_mapper->unlock(native_handle,
162 [&](const auto& tmp_err, const auto&)
163 {
164 error = tmp_err;
165 });
166 return ret.isOk() && error == V4Error::NONE ? 0 : -1;
167 }
168 #endif
169
170 switch(m_major_version)
171 {
172 case 0:
173 {
174 return m_module->unlock(m_module, handle);
175 }
176 case 1:
177 #ifdef HAVE_GRALLOC1
178 {
179 int32_t fenceFd = -1;
180 int error = m_gralloc1_unlock(m_gralloc1_device, handle, &fenceFd);
181 if (!error)
182 {
183 sync_wait(fenceFd, -1);
184 close(fenceFd);
185 }
186 return error;
187 }
188 #endif
189 default:
190 {
191 TRACE("no gralloc module to unlock");
192 return -1;
193 }
194 }
195 }
196