blob: 5812649f5b07b5e5c504651cb2b0ebb3648a1fa4 [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,
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070090 v4l2_buffer* device_buffer) {
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070091 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;
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070098 camera3_stream_t* stream = camera_buffer->stream;
99 switch(stream->format) {
100 // TODO(b/30119452): support more YCbCr formats.
101 case HAL_PIXEL_FORMAT_YCbCr_420_888:
102 android_ycbcr yuv_data;
103 mModule->lock_ycbcr(mModule, buffer, stream->usage, 0, 0,
104 stream->width, stream->height, &yuv_data);
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700105
106 // Check if gralloc format matches v4l2 format
107 // (same padding, not interleaved, contiguous).
108 if (yuv_data.ystride == bytes_per_line &&
109 yuv_data.cstride == bytes_per_line / 2 &&
110 yuv_data.chroma_step == 1 &&
111 (reinterpret_cast<uint8_t*>(yuv_data.cb) ==
112 reinterpret_cast<uint8_t*>(yuv_data.y) +
113 (stream->height * yuv_data.ystride)) &&
114 (reinterpret_cast<uint8_t*>(yuv_data.cr) ==
115 reinterpret_cast<uint8_t*>(yuv_data.cb) +
116 (stream->height / 2 * yuv_data.cstride))) {
117 // If so, great, point to the beginning.
118 HAL_LOGV("V4L2 YUV matches gralloc YUV.");
119 data = yuv_data.y;
120 } else {
121 // If not, allocate a contiguous buffer of appropriate size
122 // (to be transformed back upon unlock).
123 HAL_LOGV("Need to transform V4L2 YUV to gralloc YUV.");
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700124 data = new uint8_t[device_buffer->length];
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700125 // Make a dynamically-allocated copy of yuv_data,
126 // since it will be needed at transform time.
127 buffer_data->transform_dest.reset(new android_ycbcr(yuv_data));
128 }
129 break;
130 case HAL_PIXEL_FORMAT_BLOB:
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700131 // Jpeg buffers are just contiguous blobs; lock length * 1.
132 mModule->lock(mModule, buffer, stream->usage, 0, 0, device_buffer->length, 1, &data);
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700133 break;
134 default:
135 return -EINVAL;
136 }
137
138 // Set up the device buffer.
139 static_assert(sizeof(unsigned long) >= sizeof(void*),
140 "void* must be able to fit in the v4l2_buffer m.userptr "
141 "field (unsigned long) for this code to work");
142 device_buffer->m.userptr = reinterpret_cast<unsigned long>(data);
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700143
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
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700261int V4L2Gralloc::unlockAllBuffers() {
262 HAL_LOG_ENTER();
263
264 bool failed = false;
265 for (auto const& entry : mBufferMap) {
266 int res = mModule->unlock(mModule, *entry.second->camera_buffer->buffer);
267 if (res) {
268 failed = true;
269 }
270 // When there is a transform to be made, the buffer returned by lock()
271 // is dynamically allocated (to hold the pre-transform data).
272 if (entry.second->transform_dest) {
273 delete [] reinterpret_cast<uint8_t*>(entry.first);
274 }
275 // The BufferData entry is always dynamically allocated in lock().
276 delete entry.second;
277 }
278
279 // If any unlock failed, return error.
280 if (failed) {
281 return -ENODEV;
282 }
283
284 return 0;
285}
286
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -0700287} // namespace default_camera_hal