blob: bb708c9e721c2c3c40d494405f276edb4787cb83 [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-Cohen3eece6f2016-07-21 11:08:24 -070028
29namespace v4l2_camera_hal {
30
31// Copy |height| lines from |src| to |dest|,
32// where |src| and |dest| may have different line lengths.
33void copyWithPadding(uint8_t* dest, const uint8_t* src, size_t dest_stride,
34 size_t src_stride, size_t height) {
35 size_t copy_stride = dest_stride;
36 if (copy_stride > src_stride) {
37 // Adding padding, not reducing. 0 out the extra memory.
38 memset(dest, 0, src_stride * height);
39 copy_stride = src_stride;
40 }
41 uint8_t* dest_line_start = dest;
42 const uint8_t* src_line_start = src;
43 for (size_t row = 0; row < height; ++row,
44 dest_line_start += dest_stride, src_line_start += src_stride) {
45 memcpy(dest_line_start, src_line_start, copy_stride);
46 }
47}
48
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070049V4L2Gralloc* V4L2Gralloc::NewV4L2Gralloc() {
50 HAL_LOG_ENTER();
51
52 // Initialize and check the gralloc module.
53 const hw_module_t* module = nullptr;
54 int res = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
55 if (res || !module) {
56 HAL_LOGE("Couldn't get gralloc module.");
57 return nullptr;
58 }
59 const gralloc_module_t* gralloc =
60 reinterpret_cast<const gralloc_module_t*>(module);
61
62 // This class only supports Gralloc v0, not Gralloc V1.
63 if (gralloc->common.module_api_version > GRALLOC_MODULE_API_VERSION_0_3) {
64 HAL_LOGE("Invalid gralloc version %x. Only 0.3 (%x) "
65 "and below are supported by this HAL.",
66 gralloc->common.module_api_version,
67 GRALLOC_MODULE_API_VERSION_0_3);
68 return nullptr;
69 }
70
71 return new V4L2Gralloc(gralloc);
72}
73
74// Private. As checked by above factory, module will be non-null
75// and a supported version.
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070076V4L2Gralloc::V4L2Gralloc(const gralloc_module_t* module)
77 : mModule(module) {
78 HAL_LOG_ENTER();
79}
80
81V4L2Gralloc::~V4L2Gralloc() {
82 HAL_LOG_ENTER();
83
84 // Unlock buffers that are still locked.
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070085 unlockAllBuffers();
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070086}
87
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070088int V4L2Gralloc::lock(const camera3_stream_buffer_t* camera_buffer,
89 uint32_t bytes_per_line,
90 /*out*/ v4l2_buffer* device_buffer) {
91 HAL_LOG_ENTER();
92
93 // Lock the camera buffer (varies depending on if the buffer is YUV or not).
94 std::unique_ptr<BufferData> buffer_data(new BufferData {
95 camera_buffer, nullptr, bytes_per_line});
96 buffer_handle_t buffer = *camera_buffer->buffer;
97 void* data;
98 size_t size;
99 camera3_stream_t* stream = camera_buffer->stream;
100 switch(stream->format) {
101 // TODO(b/30119452): support more YCbCr formats.
102 case HAL_PIXEL_FORMAT_YCbCr_420_888:
103 android_ycbcr yuv_data;
104 mModule->lock_ycbcr(mModule, buffer, stream->usage, 0, 0,
105 stream->width, stream->height, &yuv_data);
106 // YUV_420_888 = 1.5 planes (1 Y plane, 1/4 Cb, 1/4 Cr).
107 size = bytes_per_line * stream->height * 3 / 2;
108
109 // Check if gralloc format matches v4l2 format
110 // (same padding, not interleaved, contiguous).
111 if (yuv_data.ystride == bytes_per_line &&
112 yuv_data.cstride == bytes_per_line / 2 &&
113 yuv_data.chroma_step == 1 &&
114 (reinterpret_cast<uint8_t*>(yuv_data.cb) ==
115 reinterpret_cast<uint8_t*>(yuv_data.y) +
116 (stream->height * yuv_data.ystride)) &&
117 (reinterpret_cast<uint8_t*>(yuv_data.cr) ==
118 reinterpret_cast<uint8_t*>(yuv_data.cb) +
119 (stream->height / 2 * yuv_data.cstride))) {
120 // If so, great, point to the beginning.
121 HAL_LOGV("V4L2 YUV matches gralloc YUV.");
122 data = yuv_data.y;
123 } else {
124 // If not, allocate a contiguous buffer of appropriate size
125 // (to be transformed back upon unlock).
126 HAL_LOGV("Need to transform V4L2 YUV to gralloc YUV.");
127 data = new uint8_t[size];
128 // Make a dynamically-allocated copy of yuv_data,
129 // since it will be needed at transform time.
130 buffer_data->transform_dest.reset(new android_ycbcr(yuv_data));
131 }
132 break;
133 case HAL_PIXEL_FORMAT_BLOB:
134 // Jpeg buffers are just contiguous blobs; always max_size * 1.
135 size = V4L2_MAX_JPEG_SIZE;
136 mModule->lock(mModule, buffer, stream->usage, 0, 0, size, 1, &data);
137 break;
138 default:
139 return -EINVAL;
140 }
141
142 // Set up the device buffer.
143 static_assert(sizeof(unsigned long) >= sizeof(void*),
144 "void* must be able to fit in the v4l2_buffer m.userptr "
145 "field (unsigned long) for this code to work");
146 device_buffer->m.userptr = reinterpret_cast<unsigned long>(data);
147 device_buffer->length = size;
148
149 // Note the mapping of data:buffer info for when unlock is called.
150 mBufferMap.emplace(data, buffer_data.release());
151
152 return 0;
153}
154
155int V4L2Gralloc::unlock(const v4l2_buffer* device_buffer) {
156 HAL_LOG_ENTER();
157
158 // TODO(b/30000211): support multi-planar data (video_capture_mplane).
159 if (device_buffer->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
160 return -EINVAL;
161 }
162
163 void* data = reinterpret_cast<void*>(device_buffer->m.userptr);
164
165 // Find and pop the matching entry in the map.
166 auto map_entry = mBufferMap.find(data);
167 if (map_entry == mBufferMap.end()) {
168 HAL_LOGE("No matching buffer for data at %p", data);
169 return -EINVAL;
170 }
171 std::unique_ptr<const BufferData> buffer_data(map_entry->second);
172 mBufferMap.erase(map_entry);
173
174 const camera3_stream_buffer_t* camera_buffer = buffer_data->camera_buffer;
175 const buffer_handle_t buffer = *camera_buffer->buffer;
176
177 // Check for transform.
178 if (buffer_data->transform_dest) {
179 HAL_LOGV("Transforming V4L2 YUV to gralloc YUV.");
180 // In this case data was allocated by this class, put it in a unique_ptr
181 // to ensure it gets cleaned up no matter which way this function exits.
182 std::unique_ptr<uint8_t[]> data_cleanup(reinterpret_cast<uint8_t*>(data));
183
184 uint32_t bytes_per_line = buffer_data->v4l2_bytes_per_line;
185 android_ycbcr* yuv_data = buffer_data->transform_dest.get();
186
187 // Should only occur in error situations.
188 if (device_buffer->bytesused == 0) {
189 return -EINVAL;
190 }
191
192 // Transform V4L2 to Gralloc, copying each plane to the correct place,
193 // adjusting padding, and interleaving if necessary.
194 uint32_t height = camera_buffer->stream->height;
195 // Y data first.
196 size_t y_len = bytes_per_line * height;
197 if (yuv_data->ystride == bytes_per_line) {
198 // Data should match exactly.
199 memcpy(yuv_data->y, data, y_len);
200 } else {
201 HAL_LOGV("Changing padding on Y plane from %u to %u.",
202 bytes_per_line, yuv_data->ystride);
203 // Wrong padding from V4L2.
204 copyWithPadding(reinterpret_cast<uint8_t*>(yuv_data->y),
205 reinterpret_cast<uint8_t*>(data),
206 yuv_data->ystride, bytes_per_line, height);
207 }
208 // C data.
209 // TODO(b/30119452): These calculations assume YCbCr_420_888.
210 size_t c_len = y_len / 4;
211 uint32_t c_bytes_per_line = bytes_per_line / 2;
212 // V4L2 is packed, meaning the data is stored as contiguous {y, cb, cr}.
213 uint8_t* cb_device = reinterpret_cast<uint8_t*>(data) + y_len;
214 uint8_t* cr_device = cb_device + c_len;
215 size_t step = yuv_data->chroma_step;
216 if (step == 1) {
217 // Still planar.
218 if (yuv_data->cstride == c_bytes_per_line) {
219 // Data should match exactly.
220 memcpy(yuv_data->cb, cb_device, c_len);
221 memcpy(yuv_data->cr, cr_device, c_len);
222 } else {
223 HAL_LOGV("Changing padding on C plane from %u to %u.",
224 c_bytes_per_line, yuv_data->cstride);
225 // Wrong padding from V4L2.
226 copyWithPadding(reinterpret_cast<uint8_t*>(yuv_data->cb),
227 cb_device, yuv_data->cstride,
228 c_bytes_per_line, height / 2);
229 copyWithPadding(reinterpret_cast<uint8_t*>(yuv_data->cr),
230 cr_device, yuv_data->cstride,
231 c_bytes_per_line, height / 2);
232 }
233 } else {
234 // Desire semiplanar (cb and cr interleaved).
235 HAL_LOGV("Interleaving cb and cr. Padding going from %u to %u.",
236 c_bytes_per_line, yuv_data->cstride);
237 uint32_t c_height = height / 2;
238 uint32_t c_width = camera_buffer->stream->width / 2;
239 // Zero out destination
240 uint8_t* cb_gralloc = reinterpret_cast<uint8_t*>(yuv_data->cb);
241 uint8_t* cr_gralloc = reinterpret_cast<uint8_t*>(yuv_data->cr);
242 memset(cb_gralloc, 0, c_width * c_height * step);
243
244 // Interleaving means we need to copy the cb and cr bytes one by one.
245 for (size_t line = 0; line < c_height; ++line,
246 cb_gralloc += yuv_data->cstride, cr_gralloc += yuv_data->cstride,
247 cb_device += c_bytes_per_line, cr_device += c_bytes_per_line) {
248 for (size_t i = 0; i < c_width; ++i) {
249 *(cb_gralloc + (i * step)) = *(cb_device + i);
250 *(cr_gralloc + (i * step)) = *(cr_device + i);
251 }
252 }
253 }
254 }
255
256 // Unlock.
257 int res = mModule->unlock(mModule, buffer);
258 if (res) {
259 HAL_LOGE("Failed to unlock buffer at %p", buffer);
260 return -ENODEV;
261 }
262
263 return 0;
264}
265
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700266int V4L2Gralloc::unlockAllBuffers() {
267 HAL_LOG_ENTER();
268
269 bool failed = false;
270 for (auto const& entry : mBufferMap) {
271 int res = mModule->unlock(mModule, *entry.second->camera_buffer->buffer);
272 if (res) {
273 failed = true;
274 }
275 // When there is a transform to be made, the buffer returned by lock()
276 // is dynamically allocated (to hold the pre-transform data).
277 if (entry.second->transform_dest) {
278 delete [] reinterpret_cast<uint8_t*>(entry.first);
279 }
280 // The BufferData entry is always dynamically allocated in lock().
281 delete entry.second;
282 }
283
284 // If any unlock failed, return error.
285 if (failed) {
286 return -ENODEV;
287 }
288
289 return 0;
290}
291
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -0700292} // namespace default_camera_hal