1 /*
2 * Copyright (c) 2018, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)
31 #define DEBUG 0
32 #include "QtiMapper.h"
33 #include <cutils/trace.h>
34 #include <qdMetaData.h>
35 #include <sync/sync.h>
36 #include "gr_utils.h"
37
38 namespace vendor {
39 namespace qti {
40 namespace hardware {
41 namespace display {
42 namespace mapper {
43 namespace V1_0 {
44 namespace implementation {
45
46 using gralloc::BufferInfo;
47
QtiMapper()48 QtiMapper::QtiMapper() {
49 buf_mgr_ = BufferManager::GetInstance();
50 ALOGD_IF(DEBUG, "Created QtiMapper instance");
51 }
52
ValidDescriptor(const BufferDescriptorInfo_2_1 & bd)53 bool QtiMapper::ValidDescriptor(const BufferDescriptorInfo_2_1 &bd) {
54 if (bd.width == 0 || bd.height == 0 || (static_cast<int32_t>(bd.format) <= 0) ||
55 bd.layerCount <= 0) {
56 return false;
57 }
58
59 return true;
60 }
61
CreateDescriptor(const BufferDescriptorInfo_2_1 & descriptor_info,IMapperBufferDescriptor * descriptor)62 Error QtiMapper::CreateDescriptor(const BufferDescriptorInfo_2_1& descriptor_info,
63 IMapperBufferDescriptor *descriptor) {
64 ALOGD_IF(DEBUG,
65 "BufferDescriptorInfo: wxh: %dx%d usage: 0x%" PRIu64 " format: %d layer_count: %d",
66 descriptor_info.width, descriptor_info.height, descriptor_info.usage,
67 static_cast<uint32_t>(descriptor_info.format), descriptor_info.layerCount);
68
69 if (ValidDescriptor(descriptor_info)) {
70 auto vec = gralloc::BufferDescriptor::Encode(descriptor_info);
71 *descriptor = vec;
72 return Error::NONE;
73 } else {
74 return Error::BAD_VALUE;
75 }
76 }
77
78 // Methods from ::android::hardware::graphics::mapper::V2_0::IMapper follow.
createDescriptor(const BufferDescriptorInfo_2_0 & descriptor_info,createDescriptor_cb hidl_cb)79 Return<void> QtiMapper::createDescriptor(const BufferDescriptorInfo_2_0 &descriptor_info,
80 createDescriptor_cb hidl_cb) {
81 IMapperBufferDescriptor descriptor;
82 auto info_2_1 = BufferDescriptorInfo_2_1 {
83 descriptor_info.width,
84 descriptor_info.height,
85 descriptor_info.layerCount,
86 static_cast<PixelFormat>(descriptor_info.format),
87 descriptor_info.usage,
88 };
89 auto err = CreateDescriptor(info_2_1, &descriptor);
90 hidl_cb(err, descriptor);
91 return Void();
92 }
93
importBuffer(const hidl_handle & raw_handle,importBuffer_cb hidl_cb)94 Return<void> QtiMapper::importBuffer(const hidl_handle &raw_handle, importBuffer_cb hidl_cb) {
95 if (!raw_handle.getNativeHandle()) {
96 ALOGE("%s: Unable to import handle", __FUNCTION__);
97 hidl_cb(Error::BAD_BUFFER, nullptr);
98 return Void();
99 }
100
101 native_handle_t *buffer_handle = native_handle_clone(raw_handle.getNativeHandle());
102 if (!buffer_handle) {
103 ALOGE("%s: Unable to clone handle", __FUNCTION__);
104 hidl_cb(Error::NO_RESOURCES, nullptr);
105 return Void();
106 }
107
108 auto error = buf_mgr_->RetainBuffer(PRIV_HANDLE_CONST(buffer_handle));
109 if (error != Error::NONE) {
110 ALOGE("%s: Unable to retain handle: %p", __FUNCTION__, buffer_handle);
111 native_handle_close(buffer_handle);
112 native_handle_delete(buffer_handle);
113
114 hidl_cb(error, nullptr);
115 return Void();
116 }
117 ALOGD_IF(DEBUG, "Imported handle: %p id: %" PRIu64, buffer_handle,
118 PRIV_HANDLE_CONST(buffer_handle)->id);
119 hidl_cb(Error::NONE, buffer_handle);
120 return Void();
121 }
122
freeBuffer(void * buffer)123 Return<Error> QtiMapper::freeBuffer(void *buffer) {
124 if (!buffer) {
125 return Error::BAD_BUFFER;
126 }
127 return buf_mgr_->ReleaseBuffer(PRIV_HANDLE_CONST(buffer));
128 }
129
GetFenceFd(const hidl_handle & fence_handle,int * outFenceFd)130 bool QtiMapper::GetFenceFd(const hidl_handle &fence_handle, int *outFenceFd) {
131 auto handle = fence_handle.getNativeHandle();
132 if (handle && handle->numFds > 1) {
133 ALOGE("invalid fence handle with %d fds", handle->numFds);
134 return false;
135 }
136
137 *outFenceFd = (handle && handle->numFds == 1) ? handle->data[0] : -1;
138 return true;
139 }
140
WaitFenceFd(int fence_fd)141 void QtiMapper::WaitFenceFd(int fence_fd) {
142 if (fence_fd < 0) {
143 return;
144 }
145
146 const int timeout = 3000;
147 ATRACE_BEGIN("fence wait");
148 const int error = sync_wait(fence_fd, timeout);
149 ATRACE_END();
150 if (error < 0) {
151 ALOGE("QtiMapper: lock fence %d didn't signal in %u ms - error: %s", fence_fd, timeout,
152 strerror(errno));
153 }
154 }
155
LockBuffer(void * buffer,uint64_t usage,const hidl_handle & acquire_fence)156 Error QtiMapper::LockBuffer(void *buffer, uint64_t usage, const hidl_handle &acquire_fence) {
157 if (!buffer) {
158 return Error::BAD_BUFFER;
159 }
160
161 int fence_fd;
162 if (!GetFenceFd(acquire_fence, &fence_fd)) {
163 return Error::BAD_VALUE;
164 }
165
166 if (fence_fd > 0) {
167 WaitFenceFd(fence_fd);
168 }
169
170 auto hnd = PRIV_HANDLE_CONST(buffer);
171
172 return buf_mgr_->LockBuffer(hnd, usage);
173 }
174
lock(void * buffer,uint64_t cpu_usage,const IMapper::Rect &,const hidl_handle & acquire_fence,lock_cb hidl_cb)175 Return<void> QtiMapper::lock(void *buffer, uint64_t cpu_usage,
176 const IMapper::Rect & /*access_region*/,
177 const hidl_handle &acquire_fence, lock_cb hidl_cb) {
178 auto err = LockBuffer(buffer, cpu_usage, acquire_fence);
179 if (err != Error::NONE) {
180 hidl_cb(err, nullptr);
181 return Void();
182 }
183
184 auto hnd = PRIV_HANDLE_CONST(buffer);
185 auto *out_data = reinterpret_cast<void *>(hnd->base);
186 hidl_cb(Error::NONE, out_data);
187 return Void();
188 }
189
lockYCbCr(void * buffer,uint64_t cpu_usage,const IMapper::Rect &,const hidl_handle & acquire_fence,lockYCbCr_cb hidl_cb)190 Return<void> QtiMapper::lockYCbCr(void *buffer, uint64_t cpu_usage,
191 const IMapper::Rect & /*access_region*/,
192 const hidl_handle &acquire_fence, lockYCbCr_cb hidl_cb) {
193 YCbCrLayout layout = {};
194 auto err = LockBuffer(buffer, cpu_usage, acquire_fence);
195 if (err != Error::NONE) {
196 hidl_cb(err, layout);
197 return Void();
198 }
199
200 auto hnd = PRIV_HANDLE_CONST(buffer);
201 android_ycbcr yuv_plane_info[2];
202 if (gralloc::GetYUVPlaneInfo(hnd, yuv_plane_info) != 0) {
203 hidl_cb(Error::BAD_VALUE, layout);
204 }
205 layout.y = yuv_plane_info[0].y;
206 layout.cr = yuv_plane_info[0].cr;
207 layout.cb = yuv_plane_info[0].cb;
208 layout.yStride = static_cast<uint32_t>(yuv_plane_info[0].ystride);
209 layout.cStride = static_cast<uint32_t>(yuv_plane_info[0].cstride);
210 layout.chromaStep = static_cast<uint32_t>(yuv_plane_info[0].chroma_step);
211 hidl_cb(Error::NONE, layout);
212 return Void();
213 }
214
unlock(void * buffer,unlock_cb hidl_cb)215 Return<void> QtiMapper::unlock(void *buffer, unlock_cb hidl_cb) {
216 auto err = Error::BAD_BUFFER;
217 if (buffer != nullptr) {
218 err = buf_mgr_->UnlockBuffer(PRIV_HANDLE_CONST(buffer));
219 }
220 // We don't have a release fence
221 hidl_cb(err, hidl_handle(nullptr));
222 return Void();
223 }
224
validateBufferSize(void * buffer,const BufferDescriptorInfo_2_1 & descriptor_info,uint32_t)225 Return<Error> QtiMapper::validateBufferSize(void* buffer,
226 const BufferDescriptorInfo_2_1& descriptor_info,
227 uint32_t /*stride*/) {
228 auto err = Error::BAD_BUFFER;
229 auto hnd = static_cast<private_handle_t *>(buffer);
230 if (buffer != nullptr && private_handle_t::validate(hnd) == 0) {
231 if (buf_mgr_->IsBufferImported(hnd) != Error::NONE) {
232 return Error::BAD_BUFFER;
233 }
234 auto info = gralloc::BufferInfo(descriptor_info.width, descriptor_info.height,
235 static_cast<uint32_t>(descriptor_info.format),
236 static_cast<uint64_t>(descriptor_info.usage));
237 info.layer_count = descriptor_info.layerCount;
238 err = buf_mgr_->ValidateBufferSize(hnd, info);
239 }
240 return err;
241 }
242
getTransportSize(void * buffer,IMapper_2_1::getTransportSize_cb hidl_cb)243 Return<void> QtiMapper::getTransportSize(void *buffer,
244 IMapper_2_1::getTransportSize_cb hidl_cb) {
245 auto err = Error::BAD_BUFFER;
246 auto hnd = static_cast<private_handle_t *>(buffer);
247 uint32_t num_fds = 0, num_ints = 0;
248 if (buffer != nullptr && private_handle_t::validate(hnd) == 0 &&
249 buf_mgr_->IsBufferImported(hnd) == Error::NONE) {
250 num_fds = 2;
251 // TODO(user): reduce to transported values;
252 num_ints = static_cast<uint32_t >(hnd->numInts);
253 err = Error::NONE;
254 }
255 ALOGD_IF(DEBUG, "GetTransportSize: num fds: %d num ints: %d IsBufferImported:%d err:%d",
256 num_fds, num_ints, buf_mgr_->IsBufferImported(hnd), err);
257 hidl_cb(err, num_fds, num_ints);
258 return Void();
259 }
260
createDescriptor_2_1(const BufferDescriptorInfo_2_1 & descriptor_info,IMapper_2_1::createDescriptor_2_1_cb hidl_cb)261 Return<void> QtiMapper::createDescriptor_2_1(const BufferDescriptorInfo_2_1& descriptor_info,
262 IMapper_2_1::createDescriptor_2_1_cb hidl_cb) {
263 IMapperBufferDescriptor descriptor;
264 auto err = CreateDescriptor(descriptor_info, &descriptor);
265 hidl_cb(err, descriptor);
266 return Void();
267 }
268
269 #ifdef ENABLE_QTI_MAPPER_EXTENSION
getMapSecureBufferFlag(void * buffer,getMapSecureBufferFlag_cb hidl_cb)270 Return<void> QtiMapper::getMapSecureBufferFlag(void *buffer, getMapSecureBufferFlag_cb hidl_cb) {
271 auto err = Error::BAD_BUFFER;
272 auto hnd = static_cast<private_handle_t *>(buffer);
273 int *map_secure_buffer = 0;
274 if (buffer != nullptr && private_handle_t::validate(hnd) == 0) {
275 if (getMetaData(hnd, GET_MAP_SECURE_BUFFER, map_secure_buffer) != 0) {
276 *map_secure_buffer = 0;
277 } else {
278 err = Error::NONE;
279 }
280 }
281 hidl_cb(err, *map_secure_buffer != 0);
282 return Void();
283 }
284
getInterlacedFlag(void * buffer,getInterlacedFlag_cb hidl_cb)285 Return<void> QtiMapper::getInterlacedFlag(void *buffer, getInterlacedFlag_cb hidl_cb) {
286 auto err = Error::BAD_BUFFER;
287 auto hnd = static_cast<private_handle_t *>(buffer);
288 int *interlaced_flag = nullptr;
289 if (buffer != nullptr && private_handle_t::validate(hnd) == 0) {
290 if (getMetaData(hnd, GET_PP_PARAM_INTERLACED, interlaced_flag) != 0) {
291 *interlaced_flag = 0;
292 } else {
293 err = Error::NONE;
294 }
295 }
296 hidl_cb(err, *interlaced_flag != 0);
297 return Void();
298 }
299
getCustomDimensions(void * buffer,getCustomDimensions_cb hidl_cb)300 Return<void> QtiMapper::getCustomDimensions(void *buffer, getCustomDimensions_cb hidl_cb) {
301 auto err = Error::BAD_BUFFER;
302 auto hnd = static_cast<private_handle_t *>(buffer);
303 int stride = 0;
304 int height = 0;
305 if (buffer != nullptr && private_handle_t::validate(hnd) == 0) {
306 stride = hnd->width;
307 height = hnd->height;
308 gralloc::GetCustomDimensions(hnd, &stride, &height);
309 err = Error::NONE;
310 }
311 hidl_cb(err, stride, height);
312 return Void();
313 }
314
getRgbDataAddress(void * buffer,getRgbDataAddress_cb hidl_cb)315 Return<void> QtiMapper::getRgbDataAddress(void *buffer, getRgbDataAddress_cb hidl_cb) {
316 auto err = Error::BAD_BUFFER;
317 auto hnd = static_cast<private_handle_t *>(buffer);
318 void *rgb_data = nullptr;
319 if (buffer != nullptr && private_handle_t::validate(hnd) == 0) {
320 if (gralloc::GetRgbDataAddress(hnd, &rgb_data) == 0) {
321 err = Error::NONE;
322 }
323 }
324 hidl_cb(err, rgb_data);
325 return Void();
326 }
327
calculateBufferAttributes(int32_t width,int32_t height,int32_t format,uint64_t usage,calculateBufferAttributes_cb hidl_cb)328 Return<void> QtiMapper::calculateBufferAttributes(int32_t width, int32_t height, int32_t format,
329 uint64_t usage,
330 calculateBufferAttributes_cb hidl_cb) {
331 unsigned int alignedw, alignedh;
332 BufferInfo info(width, height, format, usage);
333 gralloc::GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
334 bool ubwc_enabled = gralloc::IsUBwcEnabled(format, usage);
335 hidl_cb(Error::NONE, alignedw, alignedh, ubwc_enabled);
336 return Void();
337 }
338
getColorSpace(void * buffer,getColorSpace_cb hidl_cb)339 Return<void> QtiMapper::getColorSpace(void *buffer, getColorSpace_cb hidl_cb) {
340 auto err = Error::BAD_BUFFER;
341 auto hnd = static_cast<private_handle_t *>(buffer);
342 int color_space = 0;
343 if (buffer != nullptr && private_handle_t::validate(hnd) == 0) {
344 gralloc::GetColorSpaceFromMetadata(hnd, &color_space);
345 err = Error::NONE;
346 }
347 hidl_cb(err, color_space);
348 return Void();
349 }
350
getYuvPlaneInfo(void * buffer,getYuvPlaneInfo_cb hidl_cb)351 Return<void> QtiMapper::getYuvPlaneInfo(void *buffer, getYuvPlaneInfo_cb hidl_cb) {
352 auto err = Error::BAD_BUFFER;
353 auto hnd = static_cast<private_handle_t *>(buffer);
354 hidl_vec<YCbCrLayout> layout;
355 layout.resize(2);
356 android_ycbcr yuv_plane_info[2];
357 if (buffer != nullptr && private_handle_t::validate(hnd) == 0) {
358 if (gralloc::GetYUVPlaneInfo(hnd, yuv_plane_info) == 0) {
359 err = Error::NONE;
360 for (int i=0; i < 2; i++) {
361 layout[i].y = yuv_plane_info[i].y;
362 layout[i].cr = yuv_plane_info[i].cr;
363 layout[i].cb = yuv_plane_info[i].cb;
364 layout[i].yStride = static_cast<uint32_t>(yuv_plane_info[i].ystride);
365 layout[i].cStride = static_cast<uint32_t>(yuv_plane_info[i].cstride);
366 layout[i].chromaStep = static_cast<uint32_t>(yuv_plane_info[i].chroma_step);
367 }
368 }
369 }
370 hidl_cb(err, layout);
371 return Void();
372 }
373
setSingleBufferMode(void * buffer,bool enable)374 Return<Error> QtiMapper::setSingleBufferMode(void *buffer, bool enable) {
375 auto err = Error::BAD_BUFFER;
376 auto hnd = static_cast<private_handle_t *>(buffer);
377 if (buffer != nullptr && private_handle_t::validate(hnd) == 0) {
378 if (setMetaData(hnd, SET_SINGLE_BUFFER_MODE, &enable) != 0) {
379 err = Error::UNSUPPORTED;
380 } else {
381 err = Error::NONE;
382 }
383 }
384 return err;
385 }
386 #endif
387
388 // Methods from ::android::hidl::base::V1_0::IBase follow.
389
390 // When we are in passthrough mode, this method is used
391 // by hidl to obtain the SP HAL object
HIDL_FETCH_IMapper(const char *)392 IMapper_2_1 *HIDL_FETCH_IMapper(const char * /* name */) {
393 ALOGD_IF(DEBUG, "Fetching IMapper from QtiMapper");
394 auto mapper = new QtiMapper();
395 return static_cast<IMapper_2_1 *>(mapper);
396 }
397
398 } // namespace implementation
399 } // namespace V1_0
400 } // namespace mapper
401 } // namespace display
402 } // namespace hardware
403 } // namespace qti
404 } // namespace vendor
405