blob: 9ada1288048e901661d2ab320df3cc309d7faec6 [file] [log] [blame]
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -07001/*
2 * Copyright (C) 2016 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
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070017#include "v4l2_gralloc.h"
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070018
19#include <linux/videodev2.h>
20
21#include <cstdlib>
22
23#include <hardware/camera3.h>
24#include <hardware/gralloc.h>
25#include <system/graphics.h>
26
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070027#include "common.h"
Ari Hausman-Cohen04570102016-09-02 15:50:21 -070028#include "stream_format.h"
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070029
30namespace v4l2_camera_hal {
31
32// Copy |height| lines from |src| to |dest|,
33// where |src| and |dest| may have different line lengths.
Ari Hausman-Cohenfbac1742016-09-19 14:03:13 -070034void copyWithPadding(uint8_t* dest,
35 const uint8_t* src,
36 size_t dest_stride,
37 size_t src_stride,
38 size_t height) {
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070039 size_t copy_stride = dest_stride;
40 if (copy_stride > src_stride) {
41 // Adding padding, not reducing. 0 out the extra memory.
42 memset(dest, 0, src_stride * height);
43 copy_stride = src_stride;
44 }
45 uint8_t* dest_line_start = dest;
46 const uint8_t* src_line_start = src;
Ari Hausman-Cohenfbac1742016-09-19 14:03:13 -070047 for (size_t row = 0; row < height;
48 ++row, dest_line_start += dest_stride, src_line_start += src_stride) {
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070049 memcpy(dest_line_start, src_line_start, copy_stride);
50 }
51}
52
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070053V4L2Gralloc* V4L2Gralloc::NewV4L2Gralloc() {
54 HAL_LOG_ENTER();
55
56 // Initialize and check the gralloc module.
57 const hw_module_t* module = nullptr;
58 int res = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
59 if (res || !module) {
60 HAL_LOGE("Couldn't get gralloc module.");
61 return nullptr;
62 }
63 const gralloc_module_t* gralloc =
64 reinterpret_cast<const gralloc_module_t*>(module);
65
66 // This class only supports Gralloc v0, not Gralloc V1.
67 if (gralloc->common.module_api_version > GRALLOC_MODULE_API_VERSION_0_3) {
Ari Hausman-Cohenfbac1742016-09-19 14:03:13 -070068 HAL_LOGE(
69 "Invalid gralloc version %x. Only 0.3 (%x) "
70 "and below are supported by this HAL.",
71 gralloc->common.module_api_version,
72 GRALLOC_MODULE_API_VERSION_0_3);
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070073 return nullptr;
74 }
75
76 return new V4L2Gralloc(gralloc);
77}
78
79// Private. As checked by above factory, module will be non-null
80// and a supported version.
Ari Hausman-Cohenfbac1742016-09-19 14:03:13 -070081V4L2Gralloc::V4L2Gralloc(const gralloc_module_t* module) : mModule(module) {
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070082 HAL_LOG_ENTER();
83}
84
85V4L2Gralloc::~V4L2Gralloc() {
86 HAL_LOG_ENTER();
87
88 // Unlock buffers that are still locked.
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070089 unlockAllBuffers();
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070090}
91
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070092int V4L2Gralloc::lock(const camera3_stream_buffer_t* camera_buffer,
93 uint32_t bytes_per_line,
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070094 v4l2_buffer* device_buffer) {
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070095 HAL_LOG_ENTER();
96
97 // Lock the camera buffer (varies depending on if the buffer is YUV or not).
Ari Hausman-Cohenfbac1742016-09-19 14:03:13 -070098 std::unique_ptr<BufferData> buffer_data(
99 new BufferData{camera_buffer, nullptr, bytes_per_line});
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700100 buffer_handle_t buffer = *camera_buffer->buffer;
101 void* data;
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700102 camera3_stream_t* stream = camera_buffer->stream;
Ari Hausman-Cohenfbac1742016-09-19 14:03:13 -0700103 int ret = 0;
104 switch (StreamFormat::HalToV4L2PixelFormat(stream->format)) {
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700105 // TODO(b/30119452): support more YCbCr formats.
Ari Hausman-Cohen04570102016-09-02 15:50:21 -0700106 case V4L2_PIX_FMT_YUV420:
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700107 android_ycbcr yuv_data;
Ari Hausman-Cohenfbac1742016-09-19 14:03:13 -0700108 ret = mModule->lock_ycbcr(mModule,
109 buffer,
110 stream->usage,
111 0,
112 0,
113 stream->width,
114 stream->height,
115 &yuv_data);
116 if (ret) {
117 HAL_LOGE("Failed to lock ycbcr buffer: %d", ret);
118 return ret;
119 }
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700120
121 // Check if gralloc format matches v4l2 format
122 // (same padding, not interleaved, contiguous).
123 if (yuv_data.ystride == bytes_per_line &&
Ari Hausman-Cohenfbac1742016-09-19 14:03:13 -0700124 yuv_data.cstride == bytes_per_line / 2 && yuv_data.chroma_step == 1 &&
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700125 (reinterpret_cast<uint8_t*>(yuv_data.cb) ==
126 reinterpret_cast<uint8_t*>(yuv_data.y) +
Ari Hausman-Cohenfbac1742016-09-19 14:03:13 -0700127 (stream->height * yuv_data.ystride)) &&
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700128 (reinterpret_cast<uint8_t*>(yuv_data.cr) ==
129 reinterpret_cast<uint8_t*>(yuv_data.cb) +
Ari Hausman-Cohenfbac1742016-09-19 14:03:13 -0700130 (stream->height / 2 * yuv_data.cstride))) {
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700131 // If so, great, point to the beginning.
132 HAL_LOGV("V4L2 YUV matches gralloc YUV.");
133 data = yuv_data.y;
134 } else {
135 // If not, allocate a contiguous buffer of appropriate size
136 // (to be transformed back upon unlock).
137 HAL_LOGV("Need to transform V4L2 YUV to gralloc YUV.");
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700138 data = new uint8_t[device_buffer->length];
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700139 // Make a dynamically-allocated copy of yuv_data,
140 // since it will be needed at transform time.
141 buffer_data->transform_dest.reset(new android_ycbcr(yuv_data));
142 }
143 break;
Ari Hausman-Cohen04570102016-09-02 15:50:21 -0700144 case V4L2_PIX_FMT_JPEG:
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700145 // Jpeg buffers are just contiguous blobs; lock length * 1.
Ari Hausman-Cohenfbac1742016-09-19 14:03:13 -0700146 ret = mModule->lock(mModule,
147 buffer,
148 stream->usage,
149 0,
150 0,
151 device_buffer->length,
152 1,
153 &data);
154 if (ret) {
155 HAL_LOGE("Failed to lock jpeg buffer: %d", ret);
156 return ret;
157 }
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700158 break;
Ari Hausman-Cohen04570102016-09-02 15:50:21 -0700159 case V4L2_PIX_FMT_BGR32: // Fall-through.
160 case V4L2_PIX_FMT_RGB32:
Ari Hausman-Cohenfbac1742016-09-19 14:03:13 -0700161 // RGB formats have nice agreed upon representation. Unless using android
162 // flex formats.
163 ret = mModule->lock(mModule,
164 buffer,
165 stream->usage,
166 0,
167 0,
168 stream->width,
169 stream->height,
170 &data);
171 if (ret) {
172 HAL_LOGE("Failed to lock RGB buffer: %d", ret);
173 return ret;
174 }
Ari Hausman-Cohen04570102016-09-02 15:50:21 -0700175 break;
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700176 default:
177 return -EINVAL;
178 }
179
Ari Hausman-Cohenfbac1742016-09-19 14:03:13 -0700180 if (!data) {
181 ALOGE("Gralloc lock returned null ptr");
182 return -ENODEV;
183 }
184
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700185 // Set up the device buffer.
186 static_assert(sizeof(unsigned long) >= sizeof(void*),
187 "void* must be able to fit in the v4l2_buffer m.userptr "
188 "field (unsigned long) for this code to work");
189 device_buffer->m.userptr = reinterpret_cast<unsigned long>(data);
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700190
191 // Note the mapping of data:buffer info for when unlock is called.
192 mBufferMap.emplace(data, buffer_data.release());
193
194 return 0;
195}
196
197int V4L2Gralloc::unlock(const v4l2_buffer* device_buffer) {
198 HAL_LOG_ENTER();
199
200 // TODO(b/30000211): support multi-planar data (video_capture_mplane).
201 if (device_buffer->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
202 return -EINVAL;
203 }
204
205 void* data = reinterpret_cast<void*>(device_buffer->m.userptr);
206
207 // Find and pop the matching entry in the map.
208 auto map_entry = mBufferMap.find(data);
209 if (map_entry == mBufferMap.end()) {
210 HAL_LOGE("No matching buffer for data at %p", data);
211 return -EINVAL;
212 }
213 std::unique_ptr<const BufferData> buffer_data(map_entry->second);
214 mBufferMap.erase(map_entry);
215
216 const camera3_stream_buffer_t* camera_buffer = buffer_data->camera_buffer;
217 const buffer_handle_t buffer = *camera_buffer->buffer;
218
219 // Check for transform.
220 if (buffer_data->transform_dest) {
221 HAL_LOGV("Transforming V4L2 YUV to gralloc YUV.");
222 // In this case data was allocated by this class, put it in a unique_ptr
223 // to ensure it gets cleaned up no matter which way this function exits.
224 std::unique_ptr<uint8_t[]> data_cleanup(reinterpret_cast<uint8_t*>(data));
225
226 uint32_t bytes_per_line = buffer_data->v4l2_bytes_per_line;
227 android_ycbcr* yuv_data = buffer_data->transform_dest.get();
228
229 // Should only occur in error situations.
230 if (device_buffer->bytesused == 0) {
231 return -EINVAL;
232 }
233
234 // Transform V4L2 to Gralloc, copying each plane to the correct place,
235 // adjusting padding, and interleaving if necessary.
236 uint32_t height = camera_buffer->stream->height;
237 // Y data first.
238 size_t y_len = bytes_per_line * height;
239 if (yuv_data->ystride == bytes_per_line) {
240 // Data should match exactly.
241 memcpy(yuv_data->y, data, y_len);
242 } else {
243 HAL_LOGV("Changing padding on Y plane from %u to %u.",
Ari Hausman-Cohenfbac1742016-09-19 14:03:13 -0700244 bytes_per_line,
245 yuv_data->ystride);
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700246 // Wrong padding from V4L2.
247 copyWithPadding(reinterpret_cast<uint8_t*>(yuv_data->y),
248 reinterpret_cast<uint8_t*>(data),
Ari Hausman-Cohenfbac1742016-09-19 14:03:13 -0700249 yuv_data->ystride,
250 bytes_per_line,
251 height);
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700252 }
253 // C data.
254 // TODO(b/30119452): These calculations assume YCbCr_420_888.
255 size_t c_len = y_len / 4;
256 uint32_t c_bytes_per_line = bytes_per_line / 2;
257 // V4L2 is packed, meaning the data is stored as contiguous {y, cb, cr}.
258 uint8_t* cb_device = reinterpret_cast<uint8_t*>(data) + y_len;
259 uint8_t* cr_device = cb_device + c_len;
260 size_t step = yuv_data->chroma_step;
261 if (step == 1) {
262 // Still planar.
263 if (yuv_data->cstride == c_bytes_per_line) {
264 // Data should match exactly.
265 memcpy(yuv_data->cb, cb_device, c_len);
266 memcpy(yuv_data->cr, cr_device, c_len);
267 } else {
268 HAL_LOGV("Changing padding on C plane from %u to %u.",
Ari Hausman-Cohenfbac1742016-09-19 14:03:13 -0700269 c_bytes_per_line,
270 yuv_data->cstride);
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700271 // Wrong padding from V4L2.
272 copyWithPadding(reinterpret_cast<uint8_t*>(yuv_data->cb),
Ari Hausman-Cohenfbac1742016-09-19 14:03:13 -0700273 cb_device,
274 yuv_data->cstride,
275 c_bytes_per_line,
276 height / 2);
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700277 copyWithPadding(reinterpret_cast<uint8_t*>(yuv_data->cr),
Ari Hausman-Cohenfbac1742016-09-19 14:03:13 -0700278 cr_device,
279 yuv_data->cstride,
280 c_bytes_per_line,
281 height / 2);
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700282 }
283 } else {
284 // Desire semiplanar (cb and cr interleaved).
285 HAL_LOGV("Interleaving cb and cr. Padding going from %u to %u.",
Ari Hausman-Cohenfbac1742016-09-19 14:03:13 -0700286 c_bytes_per_line,
287 yuv_data->cstride);
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700288 uint32_t c_height = height / 2;
289 uint32_t c_width = camera_buffer->stream->width / 2;
290 // Zero out destination
291 uint8_t* cb_gralloc = reinterpret_cast<uint8_t*>(yuv_data->cb);
292 uint8_t* cr_gralloc = reinterpret_cast<uint8_t*>(yuv_data->cr);
293 memset(cb_gralloc, 0, c_width * c_height * step);
294
295 // Interleaving means we need to copy the cb and cr bytes one by one.
296 for (size_t line = 0; line < c_height; ++line,
Ari Hausman-Cohenfbac1742016-09-19 14:03:13 -0700297 cb_gralloc += yuv_data->cstride,
298 cr_gralloc += yuv_data->cstride,
299 cb_device += c_bytes_per_line,
300 cr_device += c_bytes_per_line) {
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700301 for (size_t i = 0; i < c_width; ++i) {
302 *(cb_gralloc + (i * step)) = *(cb_device + i);
303 *(cr_gralloc + (i * step)) = *(cr_device + i);
304 }
305 }
306 }
307 }
308
309 // Unlock.
310 int res = mModule->unlock(mModule, buffer);
311 if (res) {
312 HAL_LOGE("Failed to unlock buffer at %p", buffer);
313 return -ENODEV;
314 }
315
316 return 0;
317}
318
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700319int V4L2Gralloc::unlockAllBuffers() {
320 HAL_LOG_ENTER();
321
322 bool failed = false;
323 for (auto const& entry : mBufferMap) {
324 int res = mModule->unlock(mModule, *entry.second->camera_buffer->buffer);
325 if (res) {
326 failed = true;
327 }
328 // When there is a transform to be made, the buffer returned by lock()
329 // is dynamically allocated (to hold the pre-transform data).
330 if (entry.second->transform_dest) {
Ari Hausman-Cohenfbac1742016-09-19 14:03:13 -0700331 delete[] reinterpret_cast<uint8_t*>(entry.first);
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700332 }
333 // The BufferData entry is always dynamically allocated in lock().
334 delete entry.second;
335 }
336
337 // If any unlock failed, return error.
338 if (failed) {
339 return -ENODEV;
340 }
341
342 return 0;
343}
344
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -0700345} // namespace default_camera_hal