blob: 8ffd2620a5ede162598767ac901934dca49b5267 [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
17#include "V4L2Gralloc.h"
18
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
27#include "Common.h"
28
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
49V4L2Gralloc::V4L2Gralloc(const gralloc_module_t* module)
50 : mModule(module) {
51 HAL_LOG_ENTER();
52}
53
54V4L2Gralloc::~V4L2Gralloc() {
55 HAL_LOG_ENTER();
56
57 // Unlock buffers that are still locked.
58 for (auto const& entry : mBufferMap) {
59 mModule->unlock(mModule, *entry.second->camera_buffer->buffer);
60 // Clean up dynamically allocated stuff.
61 if (entry.second->transform_dest) {
62 delete [] reinterpret_cast<uint8_t*>(entry.first);
63 }
64 delete entry.second;
65 }
66}
67
68bool V4L2Gralloc::isValid() {
69 HAL_LOG_ENTER();
70
71 // This helper only supports Gralloc v0, not Gralloc V1.
72 if (mModule->common.module_api_version > GRALLOC_MODULE_API_VERSION_0_3) {
73 HAL_LOGE("Invalid gralloc version %x. Only 0.3 (%x) "
74 "and below are supported by this HAL.",
75 mModule->common.module_api_version,
76 GRALLOC_MODULE_API_VERSION_0_3);
77 return false;
78 }
79
80 return true;
81}
82
83int V4L2Gralloc::lock(const camera3_stream_buffer_t* camera_buffer,
84 uint32_t bytes_per_line,
85 /*out*/ v4l2_buffer* device_buffer) {
86 HAL_LOG_ENTER();
87
88 // Lock the camera buffer (varies depending on if the buffer is YUV or not).
89 std::unique_ptr<BufferData> buffer_data(new BufferData {
90 camera_buffer, nullptr, bytes_per_line});
91 buffer_handle_t buffer = *camera_buffer->buffer;
92 void* data;
93 size_t size;
94 camera3_stream_t* stream = camera_buffer->stream;
95 switch(stream->format) {
96 // TODO(b/30119452): support more YCbCr formats.
97 case HAL_PIXEL_FORMAT_YCbCr_420_888:
98 android_ycbcr yuv_data;
99 mModule->lock_ycbcr(mModule, buffer, stream->usage, 0, 0,
100 stream->width, stream->height, &yuv_data);
101 // YUV_420_888 = 1.5 planes (1 Y plane, 1/4 Cb, 1/4 Cr).
102 size = bytes_per_line * stream->height * 3 / 2;
103
104 // Check if gralloc format matches v4l2 format
105 // (same padding, not interleaved, contiguous).
106 if (yuv_data.ystride == bytes_per_line &&
107 yuv_data.cstride == bytes_per_line / 2 &&
108 yuv_data.chroma_step == 1 &&
109 (reinterpret_cast<uint8_t*>(yuv_data.cb) ==
110 reinterpret_cast<uint8_t*>(yuv_data.y) +
111 (stream->height * yuv_data.ystride)) &&
112 (reinterpret_cast<uint8_t*>(yuv_data.cr) ==
113 reinterpret_cast<uint8_t*>(yuv_data.cb) +
114 (stream->height / 2 * yuv_data.cstride))) {
115 // If so, great, point to the beginning.
116 HAL_LOGV("V4L2 YUV matches gralloc YUV.");
117 data = yuv_data.y;
118 } else {
119 // If not, allocate a contiguous buffer of appropriate size
120 // (to be transformed back upon unlock).
121 HAL_LOGV("Need to transform V4L2 YUV to gralloc YUV.");
122 data = new uint8_t[size];
123 // Make a dynamically-allocated copy of yuv_data,
124 // since it will be needed at transform time.
125 buffer_data->transform_dest.reset(new android_ycbcr(yuv_data));
126 }
127 break;
128 case HAL_PIXEL_FORMAT_BLOB:
129 // Jpeg buffers are just contiguous blobs; always max_size * 1.
130 size = V4L2_MAX_JPEG_SIZE;
131 mModule->lock(mModule, buffer, stream->usage, 0, 0, size, 1, &data);
132 break;
133 default:
134 return -EINVAL;
135 }
136
137 // Set up the device buffer.
138 static_assert(sizeof(unsigned long) >= sizeof(void*),
139 "void* must be able to fit in the v4l2_buffer m.userptr "
140 "field (unsigned long) for this code to work");
141 device_buffer->m.userptr = reinterpret_cast<unsigned long>(data);
142 device_buffer->length = size;
143
144 // Note the mapping of data:buffer info for when unlock is called.
145 mBufferMap.emplace(data, buffer_data.release());
146
147 return 0;
148}
149
150int V4L2Gralloc::unlock(const v4l2_buffer* device_buffer) {
151 HAL_LOG_ENTER();
152
153 // TODO(b/30000211): support multi-planar data (video_capture_mplane).
154 if (device_buffer->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
155 return -EINVAL;
156 }
157
158 void* data = reinterpret_cast<void*>(device_buffer->m.userptr);
159
160 // Find and pop the matching entry in the map.
161 auto map_entry = mBufferMap.find(data);
162 if (map_entry == mBufferMap.end()) {
163 HAL_LOGE("No matching buffer for data at %p", data);
164 return -EINVAL;
165 }
166 std::unique_ptr<const BufferData> buffer_data(map_entry->second);
167 mBufferMap.erase(map_entry);
168
169 const camera3_stream_buffer_t* camera_buffer = buffer_data->camera_buffer;
170 const buffer_handle_t buffer = *camera_buffer->buffer;
171
172 // Check for transform.
173 if (buffer_data->transform_dest) {
174 HAL_LOGV("Transforming V4L2 YUV to gralloc YUV.");
175 // In this case data was allocated by this class, put it in a unique_ptr
176 // to ensure it gets cleaned up no matter which way this function exits.
177 std::unique_ptr<uint8_t[]> data_cleanup(reinterpret_cast<uint8_t*>(data));
178
179 uint32_t bytes_per_line = buffer_data->v4l2_bytes_per_line;
180 android_ycbcr* yuv_data = buffer_data->transform_dest.get();
181
182 // Should only occur in error situations.
183 if (device_buffer->bytesused == 0) {
184 return -EINVAL;
185 }
186
187 // Transform V4L2 to Gralloc, copying each plane to the correct place,
188 // adjusting padding, and interleaving if necessary.
189 uint32_t height = camera_buffer->stream->height;
190 // Y data first.
191 size_t y_len = bytes_per_line * height;
192 if (yuv_data->ystride == bytes_per_line) {
193 // Data should match exactly.
194 memcpy(yuv_data->y, data, y_len);
195 } else {
196 HAL_LOGV("Changing padding on Y plane from %u to %u.",
197 bytes_per_line, yuv_data->ystride);
198 // Wrong padding from V4L2.
199 copyWithPadding(reinterpret_cast<uint8_t*>(yuv_data->y),
200 reinterpret_cast<uint8_t*>(data),
201 yuv_data->ystride, bytes_per_line, height);
202 }
203 // C data.
204 // TODO(b/30119452): These calculations assume YCbCr_420_888.
205 size_t c_len = y_len / 4;
206 uint32_t c_bytes_per_line = bytes_per_line / 2;
207 // V4L2 is packed, meaning the data is stored as contiguous {y, cb, cr}.
208 uint8_t* cb_device = reinterpret_cast<uint8_t*>(data) + y_len;
209 uint8_t* cr_device = cb_device + c_len;
210 size_t step = yuv_data->chroma_step;
211 if (step == 1) {
212 // Still planar.
213 if (yuv_data->cstride == c_bytes_per_line) {
214 // Data should match exactly.
215 memcpy(yuv_data->cb, cb_device, c_len);
216 memcpy(yuv_data->cr, cr_device, c_len);
217 } else {
218 HAL_LOGV("Changing padding on C plane from %u to %u.",
219 c_bytes_per_line, yuv_data->cstride);
220 // Wrong padding from V4L2.
221 copyWithPadding(reinterpret_cast<uint8_t*>(yuv_data->cb),
222 cb_device, yuv_data->cstride,
223 c_bytes_per_line, height / 2);
224 copyWithPadding(reinterpret_cast<uint8_t*>(yuv_data->cr),
225 cr_device, yuv_data->cstride,
226 c_bytes_per_line, height / 2);
227 }
228 } else {
229 // Desire semiplanar (cb and cr interleaved).
230 HAL_LOGV("Interleaving cb and cr. Padding going from %u to %u.",
231 c_bytes_per_line, yuv_data->cstride);
232 uint32_t c_height = height / 2;
233 uint32_t c_width = camera_buffer->stream->width / 2;
234 // Zero out destination
235 uint8_t* cb_gralloc = reinterpret_cast<uint8_t*>(yuv_data->cb);
236 uint8_t* cr_gralloc = reinterpret_cast<uint8_t*>(yuv_data->cr);
237 memset(cb_gralloc, 0, c_width * c_height * step);
238
239 // Interleaving means we need to copy the cb and cr bytes one by one.
240 for (size_t line = 0; line < c_height; ++line,
241 cb_gralloc += yuv_data->cstride, cr_gralloc += yuv_data->cstride,
242 cb_device += c_bytes_per_line, cr_device += c_bytes_per_line) {
243 for (size_t i = 0; i < c_width; ++i) {
244 *(cb_gralloc + (i * step)) = *(cb_device + i);
245 *(cr_gralloc + (i * step)) = *(cr_device + i);
246 }
247 }
248 }
249 }
250
251 // Unlock.
252 int res = mModule->unlock(mModule, buffer);
253 if (res) {
254 HAL_LOGE("Failed to unlock buffer at %p", buffer);
255 return -ENODEV;
256 }
257
258 return 0;
259}
260
261} // namespace default_camera_hal