blob: 878986bf90293bf289607d2d896c8e771673dc4d [file] [log] [blame]
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -07001/*
2 * Copyright (C) 2012 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
Eino-Ville Talvala852c3812012-09-24 09:46:53 -070017#define LOG_TAG "Camera2-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070019//#define LOG_NDEBUG 0
Eino-Ville Talvala2c08dc62012-06-15 12:49:21 -070020//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070027
Kévin PETIT377b2ec2014-02-03 12:35:36 +000028#include <inttypes.h>
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070029#include <utils/Log.h>
Eino-Ville Talvala852c3812012-09-24 09:46:53 -070030#include <utils/Trace.h>
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -070031#include <utils/Timers.h>
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070032#include "Camera2Device.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070033#include "CameraService.h"
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070034
35namespace android {
36
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070037Camera2Device::Camera2Device(int id):
38 mId(id),
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080039 mHal2Device(NULL)
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070040{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -070041 ATRACE_CALL();
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -070042 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070043}
44
45Camera2Device::~Camera2Device()
46{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -070047 ATRACE_CALL();
Igor Murashkin8dcdb952012-10-02 16:05:11 -070048 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070049 disconnect();
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070050}
51
Igor Murashkin71381052013-03-04 14:53:08 -080052int Camera2Device::getId() const {
53 return mId;
54}
55
Yin-Chia Yehe074a932015-01-30 10:29:02 -080056status_t Camera2Device::initialize(CameraModule *module)
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070057{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -070058 ATRACE_CALL();
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -070059 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080060 if (mHal2Device != NULL) {
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070061 ALOGE("%s: Already initialized!", __FUNCTION__);
62 return INVALID_OPERATION;
63 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070064
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070065 status_t res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070066 char name[10];
67 snprintf(name, sizeof(name), "%d", mId);
68
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070069 camera2_device_t *device;
70
Chien-Yu Chend231fd62015-02-25 16:04:22 -080071 res = module->open(name, reinterpret_cast<hw_device_t**>(&device));
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070072
73 if (res != OK) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070074 ALOGE("%s: Could not open camera %d: %s (%d)", __FUNCTION__,
75 mId, strerror(-res), res);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070076 return res;
77 }
78
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070079 if (device->common.version != CAMERA_DEVICE_API_VERSION_2_0) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070080 ALOGE("%s: Could not open camera %d: "
81 "Camera device is not version %x, reports %x instead",
82 __FUNCTION__, mId, CAMERA_DEVICE_API_VERSION_2_0,
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070083 device->common.version);
84 device->common.close(&device->common);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070085 return BAD_VALUE;
86 }
87
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070088 camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -080089 res = module->getCameraInfo(mId, &info);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070090 if (res != OK ) return res;
91
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070092 if (info.device_version != device->common.version) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070093 ALOGE("%s: HAL reporting mismatched camera_info version (%x)"
94 " and device version (%x).", __FUNCTION__,
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070095 device->common.version, info.device_version);
96 device->common.close(&device->common);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070097 return BAD_VALUE;
98 }
99
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700100 res = mRequestQueue.setConsumerDevice(device);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700101 if (res != OK) {
102 ALOGE("%s: Camera %d: Unable to connect request queue to device: %s (%d)",
103 __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700104 device->common.close(&device->common);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700105 return res;
106 }
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700107 res = mFrameQueue.setProducerDevice(device);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700108 if (res != OK) {
109 ALOGE("%s: Camera %d: Unable to connect frame queue to device: %s (%d)",
110 __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700111 device->common.close(&device->common);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700112 return res;
113 }
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700114
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700115 res = device->ops->set_notify_callback(device, notificationCallback,
116 NULL);
117 if (res != OK) {
118 ALOGE("%s: Camera %d: Unable to initialize notification callback!",
119 __FUNCTION__, mId);
120 device->common.close(&device->common);
121 return res;
122 }
123
124 mDeviceInfo = info.static_camera_characteristics;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800125 mHal2Device = device;
Zhijun He204e3292014-07-14 17:09:23 -0700126 mDeviceVersion = device->common.version;
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700127
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700128 return OK;
129}
130
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700131status_t Camera2Device::disconnect() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700132 ATRACE_CALL();
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700133 status_t res = OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800134 if (mHal2Device) {
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700135 ALOGV("%s: Closing device for camera %d", __FUNCTION__, mId);
136
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800137 int inProgressCount = mHal2Device->ops->get_in_progress_count(mHal2Device);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700138 if (inProgressCount > 0) {
139 ALOGW("%s: Closing camera device %d with %d requests in flight!",
140 __FUNCTION__, mId, inProgressCount);
141 }
Eino-Ville Talvalac62bb782012-09-24 13:44:07 -0700142 mReprocessStreams.clear();
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700143 mStreams.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800144 res = mHal2Device->common.close(&mHal2Device->common);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700145 if (res != OK) {
146 ALOGE("%s: Could not close camera %d: %s (%d)",
147 __FUNCTION__,
148 mId, strerror(-res), res);
149 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800150 mHal2Device = NULL;
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700151 ALOGV("%s: Shutdown complete", __FUNCTION__);
152 }
153 return res;
154}
155
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700156status_t Camera2Device::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700157 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700158 String8 result;
Eino-Ville Talvala97197152012-08-06 14:25:19 -0700159 int detailLevel = 0;
160 int n = args.size();
161 String16 detailOption("-d");
162 for (int i = 0; i + 1 < n; i++) {
163 if (args[i] == detailOption) {
164 String8 levelStr(args[i+1]);
165 detailLevel = atoi(levelStr.string());
166 }
167 }
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700168
Eino-Ville Talvala603b12e2012-08-08 09:25:58 -0700169 result.appendFormat(" Camera2Device[%d] dump (detail level %d):\n",
170 mId, detailLevel);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700171
Eino-Ville Talvala97197152012-08-06 14:25:19 -0700172 if (detailLevel > 0) {
173 result = " Request queue contents:\n";
174 write(fd, result.string(), result.size());
175 mRequestQueue.dump(fd, args);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700176
Eino-Ville Talvala97197152012-08-06 14:25:19 -0700177 result = " Frame queue contents:\n";
178 write(fd, result.string(), result.size());
179 mFrameQueue.dump(fd, args);
180 }
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700181
182 result = " Active streams:\n";
183 write(fd, result.string(), result.size());
184 for (StreamList::iterator s = mStreams.begin(); s != mStreams.end(); s++) {
185 (*s)->dump(fd, args);
186 }
187
188 result = " HAL device dump:\n";
189 write(fd, result.string(), result.size());
190
191 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800192 res = mHal2Device->ops->dump(mHal2Device, fd);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700193
194 return res;
195}
196
Igor Murashkinbd02dd12013-02-13 15:53:56 -0800197const CameraMetadata& Camera2Device::info() const {
Eino-Ville Talvala9e4c3db2012-07-20 11:07:52 -0700198 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700199
200 return mDeviceInfo;
201}
202
Jianing Weicb0652e2014-03-12 18:29:36 -0700203status_t Camera2Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700204 ATRACE_CALL();
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700205 ALOGV("%s: E", __FUNCTION__);
206
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700207 mRequestQueue.enqueue(request.release());
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700208 return OK;
209}
210
Jianing Weicb0652e2014-03-12 18:29:36 -0700211status_t Camera2Device::captureList(const List<const CameraMetadata> &requests,
212 int64_t* /*lastFrameNumber*/) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700213 ATRACE_CALL();
214 ALOGE("%s: Camera2Device burst capture not implemented", __FUNCTION__);
215 return INVALID_OPERATION;
216}
217
Jianing Weicb0652e2014-03-12 18:29:36 -0700218status_t Camera2Device::setStreamingRequest(const CameraMetadata &request,
219 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700220 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700221 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700222 CameraMetadata streamRequest(request);
223 return mRequestQueue.setStreamSlot(streamRequest.release());
224}
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700225
Jianing Weicb0652e2014-03-12 18:29:36 -0700226status_t Camera2Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
227 int64_t* /*lastFrameNumber*/) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700228 ATRACE_CALL();
229 ALOGE("%s, Camera2Device streaming burst not implemented", __FUNCTION__);
230 return INVALID_OPERATION;
231}
232
Jianing Weicb0652e2014-03-12 18:29:36 -0700233status_t Camera2Device::clearStreamingRequest(int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700234 ATRACE_CALL();
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700235 return mRequestQueue.setStreamSlot(NULL);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700236}
237
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700238status_t Camera2Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
239 ATRACE_CALL();
240 return mRequestQueue.waitForDequeue(requestId, timeout);
241}
242
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700243status_t Camera2Device::createStream(sp<ANativeWindow> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800244 uint32_t width, uint32_t height, int format,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700245 android_dataspace /*dataSpace*/, camera3_stream_rotation_t rotation, int *id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700246 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700247 status_t res;
248 ALOGV("%s: E", __FUNCTION__);
249
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800250 sp<StreamAdapter> stream = new StreamAdapter(mHal2Device);
Zhijun He28c9b6f2014-08-08 12:00:47 -0700251 size_t size = 0;
252 if (format == HAL_PIXEL_FORMAT_BLOB) {
253 size = getJpegBufferSize(width, height);
254 }
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700255 res = stream->connectToDevice(consumer, width, height, format, size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700256 if (res != OK) {
257 ALOGE("%s: Camera %d: Unable to create stream (%d x %d, format %x):"
258 "%s (%d)",
259 __FUNCTION__, mId, width, height, format, strerror(-res), res);
260 return res;
261 }
262
263 *id = stream->getId();
264
265 mStreams.push_back(stream);
266 return OK;
267}
268
Zhijun He28c9b6f2014-08-08 12:00:47 -0700269ssize_t Camera2Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
270 // Always give the max jpeg buffer size regardless of the actual jpeg resolution.
271 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
272 if (jpegBufMaxSize.count == 0) {
273 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
274 return BAD_VALUE;
275 }
276
277 return jpegBufMaxSize.data.i32[0];
278}
279
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700280status_t Camera2Device::createReprocessStreamFromStream(int outputId, int *id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700281 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700282 status_t res;
283 ALOGV("%s: E", __FUNCTION__);
284
285 bool found = false;
286 StreamList::iterator streamI;
287 for (streamI = mStreams.begin();
288 streamI != mStreams.end(); streamI++) {
289 if ((*streamI)->getId() == outputId) {
290 found = true;
291 break;
292 }
293 }
294 if (!found) {
295 ALOGE("%s: Camera %d: Output stream %d doesn't exist; can't create "
296 "reprocess stream from it!", __FUNCTION__, mId, outputId);
297 return BAD_VALUE;
298 }
299
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800300 sp<ReprocessStreamAdapter> stream = new ReprocessStreamAdapter(mHal2Device);
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700301
302 res = stream->connectToDevice((*streamI));
303 if (res != OK) {
304 ALOGE("%s: Camera %d: Unable to create reprocessing stream from "\
305 "stream %d: %s (%d)", __FUNCTION__, mId, outputId,
306 strerror(-res), res);
307 return res;
308 }
309
310 *id = stream->getId();
311
312 mReprocessStreams.push_back(stream);
313 return OK;
314}
315
316
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700317status_t Camera2Device::getStreamInfo(int id,
318 uint32_t *width, uint32_t *height, uint32_t *format) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700319 ATRACE_CALL();
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700320 ALOGV("%s: E", __FUNCTION__);
321 bool found = false;
322 StreamList::iterator streamI;
323 for (streamI = mStreams.begin();
324 streamI != mStreams.end(); streamI++) {
325 if ((*streamI)->getId() == id) {
326 found = true;
327 break;
328 }
329 }
330 if (!found) {
331 ALOGE("%s: Camera %d: Stream %d does not exist",
332 __FUNCTION__, mId, id);
333 return BAD_VALUE;
334 }
335
336 if (width) *width = (*streamI)->getWidth();
337 if (height) *height = (*streamI)->getHeight();
338 if (format) *format = (*streamI)->getFormat();
339
340 return OK;
341}
342
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700343status_t Camera2Device::setStreamTransform(int id,
344 int transform) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700345 ATRACE_CALL();
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700346 ALOGV("%s: E", __FUNCTION__);
347 bool found = false;
348 StreamList::iterator streamI;
349 for (streamI = mStreams.begin();
350 streamI != mStreams.end(); streamI++) {
351 if ((*streamI)->getId() == id) {
352 found = true;
353 break;
354 }
355 }
356 if (!found) {
357 ALOGE("%s: Camera %d: Stream %d does not exist",
358 __FUNCTION__, mId, id);
359 return BAD_VALUE;
360 }
361
362 return (*streamI)->setTransform(transform);
363}
364
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700365status_t Camera2Device::deleteStream(int id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700366 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700367 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700368 bool found = false;
369 for (StreamList::iterator streamI = mStreams.begin();
370 streamI != mStreams.end(); streamI++) {
371 if ((*streamI)->getId() == id) {
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700372 status_t res = (*streamI)->release();
Eino-Ville Talvala4ecfec32012-06-12 17:13:48 -0700373 if (res != OK) {
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700374 ALOGE("%s: Unable to release stream %d from HAL device: "
Eino-Ville Talvala4ecfec32012-06-12 17:13:48 -0700375 "%s (%d)", __FUNCTION__, id, strerror(-res), res);
376 return res;
377 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700378 mStreams.erase(streamI);
379 found = true;
380 break;
381 }
382 }
383 if (!found) {
384 ALOGE("%s: Camera %d: Unable to find stream %d to delete",
385 __FUNCTION__, mId, id);
386 return BAD_VALUE;
387 }
388 return OK;
389}
390
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700391status_t Camera2Device::deleteReprocessStream(int id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700392 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700393 ALOGV("%s: E", __FUNCTION__);
394 bool found = false;
395 for (ReprocessStreamList::iterator streamI = mReprocessStreams.begin();
396 streamI != mReprocessStreams.end(); streamI++) {
397 if ((*streamI)->getId() == id) {
398 status_t res = (*streamI)->release();
399 if (res != OK) {
400 ALOGE("%s: Unable to release reprocess stream %d from "
401 "HAL device: %s (%d)", __FUNCTION__, id,
402 strerror(-res), res);
403 return res;
404 }
405 mReprocessStreams.erase(streamI);
406 found = true;
407 break;
408 }
409 }
410 if (!found) {
411 ALOGE("%s: Camera %d: Unable to find stream %d to delete",
412 __FUNCTION__, mId, id);
413 return BAD_VALUE;
414 }
415 return OK;
416}
417
Igor Murashkine2d167e2014-08-19 16:19:59 -0700418status_t Camera2Device::configureStreams() {
419 ATRACE_CALL();
420 ALOGV("%s: E", __FUNCTION__);
421
422 /**
423 * HAL2 devices do not need to configure streams;
424 * streams are created on the fly.
425 */
426 ALOGW("%s: No-op for HAL2 devices", __FUNCTION__);
427
428 return OK;
429}
430
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700431
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700432status_t Camera2Device::createDefaultRequest(int templateId,
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700433 CameraMetadata *request) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700434 ATRACE_CALL();
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700435 status_t err;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700436 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700437 camera_metadata_t *rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800438 err = mHal2Device->ops->construct_default_request(
439 mHal2Device, templateId, &rawRequest);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700440 request->acquire(rawRequest);
441 return err;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700442}
443
444status_t Camera2Device::waitUntilDrained() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700445 ATRACE_CALL();
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700446 static const uint32_t kSleepTime = 50000; // 50 ms
447 static const uint32_t kMaxSleepTime = 10000000; // 10 s
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700448 ALOGV("%s: Camera %d: Starting wait", __FUNCTION__, mId);
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700449 if (mRequestQueue.getBufferCount() ==
450 CAMERA2_REQUEST_QUEUE_IS_BOTTOMLESS) return INVALID_OPERATION;
451
452 // TODO: Set up notifications from HAL, instead of sleeping here
453 uint32_t totalTime = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800454 while (mHal2Device->ops->get_in_progress_count(mHal2Device) > 0) {
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700455 usleep(kSleepTime);
456 totalTime += kSleepTime;
457 if (totalTime > kMaxSleepTime) {
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700458 ALOGE("%s: Waited %d us, %d requests still in flight", __FUNCTION__,
Alex Rayf0eeb532013-03-17 03:23:18 -0700459 totalTime, mHal2Device->ops->get_in_progress_count(mHal2Device));
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700460 return TIMED_OUT;
461 }
462 }
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700463 ALOGV("%s: Camera %d: HAL is idle", __FUNCTION__, mId);
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700464 return OK;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700465}
466
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700467status_t Camera2Device::setNotifyCallback(NotificationListener *listener) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700468 ATRACE_CALL();
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700469 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800470 res = mHal2Device->ops->set_notify_callback(mHal2Device, notificationCallback,
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700471 reinterpret_cast<void*>(listener) );
472 if (res != OK) {
473 ALOGE("%s: Unable to set notification callback!", __FUNCTION__);
474 }
475 return res;
476}
477
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -0700478bool Camera2Device::willNotify3A() {
479 return true;
480}
481
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700482void Camera2Device::notificationCallback(int32_t msg_type,
483 int32_t ext1,
484 int32_t ext2,
485 int32_t ext3,
486 void *user) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700487 ATRACE_CALL();
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700488 NotificationListener *listener = reinterpret_cast<NotificationListener*>(user);
489 ALOGV("%s: Notification %d, arguments %d, %d, %d", __FUNCTION__, msg_type,
490 ext1, ext2, ext3);
491 if (listener != NULL) {
492 switch (msg_type) {
493 case CAMERA2_MSG_ERROR:
Jianing Weicb0652e2014-03-12 18:29:36 -0700494 // TODO: This needs to be fixed. ext2 and ext3 need to be considered.
495 listener->notifyError(
496 ((ext1 == CAMERA2_MSG_ERROR_DEVICE)
497 || (ext1 == CAMERA2_MSG_ERROR_HARDWARE)) ?
498 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE :
499 ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE,
500 CaptureResultExtras());
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700501 break;
502 case CAMERA2_MSG_SHUTTER: {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700503 // TODO: Only needed for camera2 API, which is unsupported
504 // by HAL2 directly.
505 // nsecs_t timestamp = (nsecs_t)ext2 | ((nsecs_t)(ext3) << 32 );
506 // listener->notifyShutter(requestId, timestamp);
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700507 break;
508 }
509 case CAMERA2_MSG_AUTOFOCUS:
510 listener->notifyAutoFocus(ext1, ext2);
511 break;
512 case CAMERA2_MSG_AUTOEXPOSURE:
513 listener->notifyAutoExposure(ext1, ext2);
514 break;
515 case CAMERA2_MSG_AUTOWB:
516 listener->notifyAutoWhitebalance(ext1, ext2);
517 break;
518 default:
519 ALOGE("%s: Unknown notification %d (arguments %d, %d, %d)!",
520 __FUNCTION__, msg_type, ext1, ext2, ext3);
521 }
522 }
523}
524
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -0700525status_t Camera2Device::waitForNextFrame(nsecs_t timeout) {
526 return mFrameQueue.waitForBuffer(timeout);
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700527}
528
Jianing Weicb0652e2014-03-12 18:29:36 -0700529status_t Camera2Device::getNextResult(CaptureResult *result) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700530 ATRACE_CALL();
Jianing Weicb0652e2014-03-12 18:29:36 -0700531 ALOGV("%s: get CaptureResult", __FUNCTION__);
532 if (result == NULL) {
533 ALOGE("%s: result pointer is NULL", __FUNCTION__);
534 return BAD_VALUE;
535 }
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700536 status_t res;
537 camera_metadata_t *rawFrame;
538 res = mFrameQueue.dequeue(&rawFrame);
Jianing Weicb0652e2014-03-12 18:29:36 -0700539 if (rawFrame == NULL) {
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700540 return NOT_ENOUGH_DATA;
541 } else if (res == OK) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700542 result->mMetadata.acquire(rawFrame);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700543 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700544
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700545 return res;
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700546}
547
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700548status_t Camera2Device::triggerAutofocus(uint32_t id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700549 ATRACE_CALL();
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700550 status_t res;
551 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800552 res = mHal2Device->ops->trigger_action(mHal2Device,
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700553 CAMERA2_TRIGGER_AUTOFOCUS, id, 0);
554 if (res != OK) {
555 ALOGE("%s: Error triggering autofocus (id %d)",
556 __FUNCTION__, id);
557 }
558 return res;
559}
560
561status_t Camera2Device::triggerCancelAutofocus(uint32_t id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700562 ATRACE_CALL();
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700563 status_t res;
564 ALOGV("%s: Canceling autofocus, id %d", __FUNCTION__, id);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800565 res = mHal2Device->ops->trigger_action(mHal2Device,
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700566 CAMERA2_TRIGGER_CANCEL_AUTOFOCUS, id, 0);
567 if (res != OK) {
568 ALOGE("%s: Error canceling autofocus (id %d)",
569 __FUNCTION__, id);
570 }
571 return res;
572}
573
574status_t Camera2Device::triggerPrecaptureMetering(uint32_t id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700575 ATRACE_CALL();
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700576 status_t res;
577 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800578 res = mHal2Device->ops->trigger_action(mHal2Device,
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700579 CAMERA2_TRIGGER_PRECAPTURE_METERING, id, 0);
580 if (res != OK) {
581 ALOGE("%s: Error triggering precapture metering (id %d)",
582 __FUNCTION__, id);
583 }
584 return res;
585}
586
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700587status_t Camera2Device::pushReprocessBuffer(int reprocessStreamId,
588 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700589 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700590 ALOGV("%s: E", __FUNCTION__);
591 bool found = false;
592 status_t res = OK;
593 for (ReprocessStreamList::iterator streamI = mReprocessStreams.begin();
594 streamI != mReprocessStreams.end(); streamI++) {
595 if ((*streamI)->getId() == reprocessStreamId) {
596 res = (*streamI)->pushIntoStream(buffer, listener);
597 if (res != OK) {
598 ALOGE("%s: Unable to push buffer to reprocess stream %d: %s (%d)",
599 __FUNCTION__, reprocessStreamId, strerror(-res), res);
600 return res;
601 }
602 found = true;
603 break;
604 }
605 }
606 if (!found) {
607 ALOGE("%s: Camera %d: Unable to find reprocess stream %d",
608 __FUNCTION__, mId, reprocessStreamId);
609 res = BAD_VALUE;
610 }
611 return res;
612}
613
Jianing Weicb0652e2014-03-12 18:29:36 -0700614status_t Camera2Device::flush(int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700615 ATRACE_CALL();
616
617 mRequestQueue.clear();
618 return waitUntilDrained();
619}
620
Zhijun He204e3292014-07-14 17:09:23 -0700621uint32_t Camera2Device::getDeviceVersion() {
622 ATRACE_CALL();
623 return mDeviceVersion;
624}
625
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700626/**
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700627 * Camera2Device::MetadataQueue
628 */
629
630Camera2Device::MetadataQueue::MetadataQueue():
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800631 mHal2Device(NULL),
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700632 mFrameCount(0),
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700633 mLatestRequestId(0),
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700634 mCount(0),
635 mStreamSlotCount(0),
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -0700636 mSignalConsumer(true)
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700637{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700638 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700639 camera2_request_queue_src_ops::dequeue_request = consumer_dequeue;
640 camera2_request_queue_src_ops::request_count = consumer_buffer_count;
641 camera2_request_queue_src_ops::free_request = consumer_free;
642
643 camera2_frame_queue_dst_ops::dequeue_frame = producer_dequeue;
644 camera2_frame_queue_dst_ops::cancel_frame = producer_cancel;
645 camera2_frame_queue_dst_ops::enqueue_frame = producer_enqueue;
646}
647
648Camera2Device::MetadataQueue::~MetadataQueue() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700649 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700650 clear();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700651}
652
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700653// Connect to camera2 HAL as consumer (input requests/reprocessing)
654status_t Camera2Device::MetadataQueue::setConsumerDevice(camera2_device_t *d) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700655 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700656 status_t res;
657 res = d->ops->set_request_queue_src_ops(d,
658 this);
659 if (res != OK) return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800660 mHal2Device = d;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700661 return OK;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700662}
663
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700664status_t Camera2Device::MetadataQueue::setProducerDevice(camera2_device_t *d) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700665 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700666 status_t res;
667 res = d->ops->set_frame_queue_dst_ops(d,
668 this);
669 return res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700670}
671
672// Real interfaces
673status_t Camera2Device::MetadataQueue::enqueue(camera_metadata_t *buf) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700674 ATRACE_CALL();
Eino-Ville Talvala2c08dc62012-06-15 12:49:21 -0700675 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700676 Mutex::Autolock l(mMutex);
677
678 mCount++;
679 mEntries.push_back(buf);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700680
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700681 return signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700682}
683
684int Camera2Device::MetadataQueue::getBufferCount() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700685 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700686 Mutex::Autolock l(mMutex);
687 if (mStreamSlotCount > 0) {
688 return CAMERA2_REQUEST_QUEUE_IS_BOTTOMLESS;
689 }
690 return mCount;
691}
692
693status_t Camera2Device::MetadataQueue::dequeue(camera_metadata_t **buf,
694 bool incrementCount)
695{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700696 ATRACE_CALL();
Eino-Ville Talvala2c08dc62012-06-15 12:49:21 -0700697 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700698 status_t res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700699 Mutex::Autolock l(mMutex);
700
701 if (mCount == 0) {
702 if (mStreamSlotCount == 0) {
Eino-Ville Talvala2c08dc62012-06-15 12:49:21 -0700703 ALOGVV("%s: Empty", __FUNCTION__);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700704 *buf = NULL;
705 mSignalConsumer = true;
706 return OK;
707 }
Eino-Ville Talvala2c08dc62012-06-15 12:49:21 -0700708 ALOGVV("%s: Streaming %d frames to queue", __FUNCTION__,
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700709 mStreamSlotCount);
710
711 for (List<camera_metadata_t*>::iterator slotEntry = mStreamSlot.begin();
712 slotEntry != mStreamSlot.end();
713 slotEntry++ ) {
714 size_t entries = get_camera_metadata_entry_count(*slotEntry);
715 size_t dataBytes = get_camera_metadata_data_count(*slotEntry);
716
717 camera_metadata_t *copy =
718 allocate_camera_metadata(entries, dataBytes);
719 append_camera_metadata(copy, *slotEntry);
720 mEntries.push_back(copy);
721 }
722 mCount = mStreamSlotCount;
723 }
Eino-Ville Talvala2c08dc62012-06-15 12:49:21 -0700724 ALOGVV("MetadataQueue: deque (%d buffers)", mCount);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700725 camera_metadata_t *b = *(mEntries.begin());
726 mEntries.erase(mEntries.begin());
727
728 if (incrementCount) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700729 ATRACE_INT("cam2_request", mFrameCount);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700730 camera_metadata_entry_t frameCount;
731 res = find_camera_metadata_entry(b,
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700732 ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700733 &frameCount);
734 if (res != OK) {
735 ALOGE("%s: Unable to add frame count: %s (%d)",
736 __FUNCTION__, strerror(-res), res);
737 } else {
738 *frameCount.data.i32 = mFrameCount;
739 }
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700740 mFrameCount++;
741 }
742
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700743 // Check for request ID, and if present, signal waiters.
744 camera_metadata_entry_t requestId;
745 res = find_camera_metadata_entry(b,
746 ANDROID_REQUEST_ID,
747 &requestId);
748 if (res == OK) {
749 mLatestRequestId = requestId.data.i32[0];
750 mNewRequestId.signal();
751 }
752
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700753 *buf = b;
754 mCount--;
755
756 return OK;
757}
758
759status_t Camera2Device::MetadataQueue::waitForBuffer(nsecs_t timeout)
760{
761 Mutex::Autolock l(mMutex);
762 status_t res;
763 while (mCount == 0) {
764 res = notEmpty.waitRelative(mMutex,timeout);
765 if (res != OK) return res;
766 }
767 return OK;
768}
769
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700770status_t Camera2Device::MetadataQueue::waitForDequeue(int32_t id,
771 nsecs_t timeout) {
772 Mutex::Autolock l(mMutex);
773 status_t res;
774 while (mLatestRequestId != id) {
775 nsecs_t startTime = systemTime();
776
777 res = mNewRequestId.waitRelative(mMutex, timeout);
778 if (res != OK) return res;
779
780 timeout -= (systemTime() - startTime);
781 }
782
783 return OK;
784}
785
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700786status_t Camera2Device::MetadataQueue::setStreamSlot(camera_metadata_t *buf)
787{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700788 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700789 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700790 Mutex::Autolock l(mMutex);
791 if (buf == NULL) {
792 freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
793 mStreamSlotCount = 0;
794 return OK;
795 }
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700796
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700797 if (mStreamSlotCount > 1) {
798 List<camera_metadata_t*>::iterator deleter = ++mStreamSlot.begin();
799 freeBuffers(++mStreamSlot.begin(), mStreamSlot.end());
800 mStreamSlotCount = 1;
801 }
802 if (mStreamSlotCount == 1) {
803 free_camera_metadata( *(mStreamSlot.begin()) );
Chien-Yu Chen80de5dc2014-11-07 17:45:00 -0800804 *(mStreamSlot.begin()) = buf;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700805 } else {
Chien-Yu Chen80de5dc2014-11-07 17:45:00 -0800806 mStreamSlot.push_front(buf);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700807 mStreamSlotCount = 1;
808 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700809 return signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700810}
811
812status_t Camera2Device::MetadataQueue::setStreamSlot(
813 const List<camera_metadata_t*> &bufs)
814{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700815 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700816 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700817 Mutex::Autolock l(mMutex);
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700818
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700819 if (mStreamSlotCount > 0) {
820 freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
821 }
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700822 mStreamSlotCount = 0;
823 for (List<camera_metadata_t*>::const_iterator r = bufs.begin();
824 r != bufs.end(); r++) {
Chien-Yu Chen80de5dc2014-11-07 17:45:00 -0800825 mStreamSlot.push_back(*r);
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700826 mStreamSlotCount++;
827 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700828 return signalConsumerLocked();
829}
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700830
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700831status_t Camera2Device::MetadataQueue::clear()
832{
833 ATRACE_CALL();
834 ALOGV("%s: E", __FUNCTION__);
835
836 Mutex::Autolock l(mMutex);
837
838 // Clear streaming slot
839 freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
840 mStreamSlotCount = 0;
841
842 // Clear request queue
843 freeBuffers(mEntries.begin(), mEntries.end());
844 mCount = 0;
845 return OK;
846}
847
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700848status_t Camera2Device::MetadataQueue::dump(int fd,
Igor Murashkinebe3f692012-10-12 16:56:11 -0700849 const Vector<String16>& /*args*/) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700850 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700851 String8 result;
852 status_t notLocked;
853 notLocked = mMutex.tryLock();
854 if (notLocked) {
855 result.append(" (Unable to lock queue mutex)\n");
856 }
857 result.appendFormat(" Current frame number: %d\n", mFrameCount);
858 if (mStreamSlotCount == 0) {
859 result.append(" Stream slot: Empty\n");
860 write(fd, result.string(), result.size());
861 } else {
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000862 result.appendFormat(" Stream slot: %zu entries\n",
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700863 mStreamSlot.size());
864 int i = 0;
865 for (List<camera_metadata_t*>::iterator r = mStreamSlot.begin();
866 r != mStreamSlot.end(); r++) {
867 result = String8::format(" Stream slot buffer %d:\n", i);
868 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -0700869 dump_indented_camera_metadata(*r, fd, 2, 10);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700870 i++;
871 }
872 }
873 if (mEntries.size() == 0) {
874 result = " Main queue is empty\n";
875 write(fd, result.string(), result.size());
876 } else {
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000877 result = String8::format(" Main queue has %zu entries:\n",
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700878 mEntries.size());
879 int i = 0;
880 for (List<camera_metadata_t*>::iterator r = mEntries.begin();
881 r != mEntries.end(); r++) {
882 result = String8::format(" Queue entry %d:\n", i);
883 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -0700884 dump_indented_camera_metadata(*r, fd, 2, 10);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700885 i++;
886 }
887 }
888
889 if (notLocked == 0) {
890 mMutex.unlock();
891 }
892
893 return OK;
894}
895
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700896status_t Camera2Device::MetadataQueue::signalConsumerLocked() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700897 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700898 status_t res = OK;
899 notEmpty.signal();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800900 if (mSignalConsumer && mHal2Device != NULL) {
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700901 mSignalConsumer = false;
902
903 mMutex.unlock();
904 ALOGV("%s: Signaling consumer", __FUNCTION__);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800905 res = mHal2Device->ops->notify_request_queue_not_empty(mHal2Device);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700906 mMutex.lock();
907 }
908 return res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700909}
910
911status_t Camera2Device::MetadataQueue::freeBuffers(
912 List<camera_metadata_t*>::iterator start,
913 List<camera_metadata_t*>::iterator end)
914{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700915 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700916 while (start != end) {
917 free_camera_metadata(*start);
918 start = mStreamSlot.erase(start);
919 }
920 return OK;
921}
922
923Camera2Device::MetadataQueue* Camera2Device::MetadataQueue::getInstance(
924 const camera2_request_queue_src_ops_t *q)
925{
926 const MetadataQueue* cmq = static_cast<const MetadataQueue*>(q);
927 return const_cast<MetadataQueue*>(cmq);
928}
929
930Camera2Device::MetadataQueue* Camera2Device::MetadataQueue::getInstance(
931 const camera2_frame_queue_dst_ops_t *q)
932{
933 const MetadataQueue* cmq = static_cast<const MetadataQueue*>(q);
934 return const_cast<MetadataQueue*>(cmq);
935}
936
937int Camera2Device::MetadataQueue::consumer_buffer_count(
938 const camera2_request_queue_src_ops_t *q)
939{
940 MetadataQueue *queue = getInstance(q);
941 return queue->getBufferCount();
942}
943
944int Camera2Device::MetadataQueue::consumer_dequeue(
945 const camera2_request_queue_src_ops_t *q,
946 camera_metadata_t **buffer)
947{
948 MetadataQueue *queue = getInstance(q);
949 return queue->dequeue(buffer, true);
950}
951
952int Camera2Device::MetadataQueue::consumer_free(
953 const camera2_request_queue_src_ops_t *q,
954 camera_metadata_t *old_buffer)
955{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700956 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700957 MetadataQueue *queue = getInstance(q);
Igor Murashkinebe3f692012-10-12 16:56:11 -0700958 (void)queue;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700959 free_camera_metadata(old_buffer);
960 return OK;
961}
962
963int Camera2Device::MetadataQueue::producer_dequeue(
Igor Murashkinebe3f692012-10-12 16:56:11 -0700964 const camera2_frame_queue_dst_ops_t * /*q*/,
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700965 size_t entries, size_t bytes,
966 camera_metadata_t **buffer)
967{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700968 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700969 camera_metadata_t *new_buffer =
970 allocate_camera_metadata(entries, bytes);
971 if (new_buffer == NULL) return NO_MEMORY;
972 *buffer = new_buffer;
973 return OK;
974}
975
976int Camera2Device::MetadataQueue::producer_cancel(
Igor Murashkinebe3f692012-10-12 16:56:11 -0700977 const camera2_frame_queue_dst_ops_t * /*q*/,
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700978 camera_metadata_t *old_buffer)
979{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700980 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700981 free_camera_metadata(old_buffer);
982 return OK;
983}
984
985int Camera2Device::MetadataQueue::producer_enqueue(
986 const camera2_frame_queue_dst_ops_t *q,
987 camera_metadata_t *filled_buffer)
988{
989 MetadataQueue *queue = getInstance(q);
990 return queue->enqueue(filled_buffer);
991}
992
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700993/**
994 * Camera2Device::StreamAdapter
995 */
996
997#ifndef container_of
998#define container_of(ptr, type, member) \
999 (type *)((char*)(ptr) - offsetof(type, member))
1000#endif
1001
1002Camera2Device::StreamAdapter::StreamAdapter(camera2_device_t *d):
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001003 mState(RELEASED),
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001004 mHal2Device(d),
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001005 mId(-1),
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001006 mWidth(0), mHeight(0), mFormat(0), mSize(0), mUsage(0),
1007 mMaxProducerBuffers(0), mMaxConsumerBuffers(0),
1008 mTotalBuffers(0),
1009 mFormatRequested(0),
1010 mActiveBuffers(0),
1011 mFrameCount(0),
1012 mLastTimestamp(0)
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001013{
1014 camera2_stream_ops::dequeue_buffer = dequeue_buffer;
1015 camera2_stream_ops::enqueue_buffer = enqueue_buffer;
1016 camera2_stream_ops::cancel_buffer = cancel_buffer;
1017 camera2_stream_ops::set_crop = set_crop;
1018}
1019
1020Camera2Device::StreamAdapter::~StreamAdapter() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001021 ATRACE_CALL();
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001022 if (mState != RELEASED) {
1023 release();
1024 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001025}
1026
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -07001027status_t Camera2Device::StreamAdapter::connectToDevice(
1028 sp<ANativeWindow> consumer,
1029 uint32_t width, uint32_t height, int format, size_t size) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001030 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001031 status_t res;
Eino-Ville Talvala9e4c3db2012-07-20 11:07:52 -07001032 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001033
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001034 if (mState != RELEASED) return INVALID_OPERATION;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001035 if (consumer == NULL) {
1036 ALOGE("%s: Null consumer passed to stream adapter", __FUNCTION__);
1037 return BAD_VALUE;
1038 }
1039
Colin Crosse5729fa2014-03-21 15:04:25 -07001040 ALOGV("%s: New stream parameters %d x %d, format 0x%x, size %zu",
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001041 __FUNCTION__, width, height, format, size);
1042
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001043 mConsumerInterface = consumer;
1044 mWidth = width;
1045 mHeight = height;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -07001046 mSize = (format == HAL_PIXEL_FORMAT_BLOB) ? size : 0;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001047 mFormatRequested = format;
1048
1049 // Allocate device-side stream interface
1050
1051 uint32_t id;
1052 uint32_t formatActual;
1053 uint32_t usage;
1054 uint32_t maxBuffers = 2;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001055 res = mHal2Device->ops->allocate_stream(mHal2Device,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001056 mWidth, mHeight, mFormatRequested, getStreamOps(),
1057 &id, &formatActual, &usage, &maxBuffers);
1058 if (res != OK) {
1059 ALOGE("%s: Device stream allocation failed: %s (%d)",
1060 __FUNCTION__, strerror(-res), res);
1061 return res;
1062 }
1063
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001064 ALOGV("%s: Allocated stream id %d, actual format 0x%x, "
1065 "usage 0x%x, producer wants %d buffers", __FUNCTION__,
1066 id, formatActual, usage, maxBuffers);
1067
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001068 mId = id;
1069 mFormat = formatActual;
1070 mUsage = usage;
1071 mMaxProducerBuffers = maxBuffers;
1072
1073 mState = ALLOCATED;
1074
1075 // Configure consumer-side ANativeWindow interface
1076 res = native_window_api_connect(mConsumerInterface.get(),
1077 NATIVE_WINDOW_API_CAMERA);
1078 if (res != OK) {
1079 ALOGE("%s: Unable to connect to native window for stream %d",
1080 __FUNCTION__, mId);
1081
1082 return res;
1083 }
1084
1085 mState = CONNECTED;
1086
1087 res = native_window_set_usage(mConsumerInterface.get(), mUsage);
1088 if (res != OK) {
1089 ALOGE("%s: Unable to configure usage %08x for stream %d",
1090 __FUNCTION__, mUsage, mId);
1091 return res;
1092 }
1093
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001094 res = native_window_set_scaling_mode(mConsumerInterface.get(),
1095 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1096 if (res != OK) {
1097 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
1098 __FUNCTION__, strerror(-res), res);
1099 return res;
1100 }
1101
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -07001102 res = setTransform(0);
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001103 if (res != OK) {
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001104 return res;
1105 }
1106
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -07001107 if (mFormat == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -07001108 res = native_window_set_buffers_dimensions(mConsumerInterface.get(),
1109 mSize, 1);
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -07001110 if (res != OK) {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -07001111 ALOGE("%s: Unable to configure compressed stream buffer dimensions"
Colin Crosse5729fa2014-03-21 15:04:25 -07001112 " %d x %d, size %zu for stream %d",
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -07001113 __FUNCTION__, mWidth, mHeight, mSize, mId);
1114 return res;
1115 }
1116 } else {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -07001117 res = native_window_set_buffers_dimensions(mConsumerInterface.get(),
1118 mWidth, mHeight);
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -07001119 if (res != OK) {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -07001120 ALOGE("%s: Unable to configure stream buffer dimensions"
1121 " %d x %d for stream %d",
1122 __FUNCTION__, mWidth, mHeight, mId);
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -07001123 return res;
1124 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001125 }
1126
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -07001127 res = native_window_set_buffers_format(mConsumerInterface.get(), mFormat);
1128 if (res != OK) {
1129 ALOGE("%s: Unable to configure stream buffer format"
1130 " %#x for stream %d",
1131 __FUNCTION__, mFormat, mId);
1132 return res;
1133 }
1134
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001135 int maxConsumerBuffers;
1136 res = mConsumerInterface->query(mConsumerInterface.get(),
1137 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
1138 if (res != OK) {
1139 ALOGE("%s: Unable to query consumer undequeued"
1140 " buffer count for stream %d", __FUNCTION__, mId);
1141 return res;
1142 }
1143 mMaxConsumerBuffers = maxConsumerBuffers;
1144
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001145 ALOGV("%s: Consumer wants %d buffers", __FUNCTION__,
1146 mMaxConsumerBuffers);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001147
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001148 mTotalBuffers = mMaxConsumerBuffers + mMaxProducerBuffers;
1149 mActiveBuffers = 0;
1150 mFrameCount = 0;
1151 mLastTimestamp = 0;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001152
1153 res = native_window_set_buffer_count(mConsumerInterface.get(),
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001154 mTotalBuffers);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001155 if (res != OK) {
1156 ALOGE("%s: Unable to set buffer count for stream %d",
1157 __FUNCTION__, mId);
1158 return res;
1159 }
1160
1161 // Register allocated buffers with HAL device
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001162 buffer_handle_t *buffers = new buffer_handle_t[mTotalBuffers];
1163 ANativeWindowBuffer **anwBuffers = new ANativeWindowBuffer*[mTotalBuffers];
1164 uint32_t bufferIdx = 0;
1165 for (; bufferIdx < mTotalBuffers; bufferIdx++) {
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001166 res = native_window_dequeue_buffer_and_wait(mConsumerInterface.get(),
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001167 &anwBuffers[bufferIdx]);
1168 if (res != OK) {
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001169 ALOGE("%s: Unable to dequeue buffer %d for initial registration for "
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001170 "stream %d", __FUNCTION__, bufferIdx, mId);
1171 goto cleanUpBuffers;
1172 }
1173
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001174 buffers[bufferIdx] = anwBuffers[bufferIdx]->handle;
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001175 ALOGV("%s: Buffer %p allocated", __FUNCTION__, (void*)buffers[bufferIdx]);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001176 }
1177
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001178 ALOGV("%s: Registering %d buffers with camera HAL", __FUNCTION__, mTotalBuffers);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001179 res = mHal2Device->ops->register_stream_buffers(mHal2Device,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001180 mId,
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001181 mTotalBuffers,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001182 buffers);
1183 if (res != OK) {
1184 ALOGE("%s: Unable to register buffers with HAL device for stream %d",
1185 __FUNCTION__, mId);
1186 } else {
1187 mState = ACTIVE;
1188 }
1189
1190cleanUpBuffers:
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001191 ALOGV("%s: Cleaning up %d buffers", __FUNCTION__, bufferIdx);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001192 for (uint32_t i = 0; i < bufferIdx; i++) {
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001193 res = mConsumerInterface->cancelBuffer(mConsumerInterface.get(),
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001194 anwBuffers[i], -1);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001195 if (res != OK) {
1196 ALOGE("%s: Unable to cancel buffer %d after registration",
1197 __FUNCTION__, i);
1198 }
1199 }
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001200 delete[] anwBuffers;
1201 delete[] buffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001202
1203 return res;
1204}
1205
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001206status_t Camera2Device::StreamAdapter::release() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001207 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001208 status_t res;
Eino-Ville Talvala02f84572013-04-23 15:16:57 -07001209 ALOGV("%s: Releasing stream %d (%d x %d, format %d)", __FUNCTION__, mId,
1210 mWidth, mHeight, mFormat);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001211 if (mState >= ALLOCATED) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001212 res = mHal2Device->ops->release_stream(mHal2Device, mId);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001213 if (res != OK) {
1214 ALOGE("%s: Unable to release stream %d",
1215 __FUNCTION__, mId);
1216 return res;
1217 }
1218 }
1219 if (mState >= CONNECTED) {
1220 res = native_window_api_disconnect(mConsumerInterface.get(),
1221 NATIVE_WINDOW_API_CAMERA);
Igor Murashkina1e5dcc2012-10-02 15:21:31 -07001222
1223 /* this is not an error. if client calling process dies,
1224 the window will also die and all calls to it will return
1225 DEAD_OBJECT, thus it's already "disconnected" */
1226 if (res == DEAD_OBJECT) {
1227 ALOGW("%s: While disconnecting stream %d from native window, the"
1228 " native window died from under us", __FUNCTION__, mId);
1229 }
1230 else if (res != OK) {
1231 ALOGE("%s: Unable to disconnect stream %d from native window (error %d %s)",
1232 __FUNCTION__, mId, res, strerror(-res));
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001233 return res;
1234 }
1235 }
1236 mId = -1;
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001237 mState = RELEASED;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001238 return OK;
1239}
1240
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -07001241status_t Camera2Device::StreamAdapter::setTransform(int transform) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001242 ATRACE_CALL();
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -07001243 status_t res;
1244 if (mState < CONNECTED) {
1245 ALOGE("%s: Cannot set transform on unconnected stream", __FUNCTION__);
1246 return INVALID_OPERATION;
1247 }
1248 res = native_window_set_buffers_transform(mConsumerInterface.get(),
1249 transform);
1250 if (res != OK) {
1251 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
1252 __FUNCTION__, transform, strerror(-res), res);
1253 }
1254 return res;
1255}
1256
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001257status_t Camera2Device::StreamAdapter::dump(int fd,
Igor Murashkinebe3f692012-10-12 16:56:11 -07001258 const Vector<String16>& /*args*/) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001259 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001260 String8 result = String8::format(" Stream %d: %d x %d, format 0x%x\n",
1261 mId, mWidth, mHeight, mFormat);
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001262 result.appendFormat(" size %zu, usage 0x%x, requested format 0x%x\n",
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001263 mSize, mUsage, mFormatRequested);
1264 result.appendFormat(" total buffers: %d, dequeued buffers: %d\n",
1265 mTotalBuffers, mActiveBuffers);
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001266 result.appendFormat(" frame count: %d, last timestamp %" PRId64 "\n",
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001267 mFrameCount, mLastTimestamp);
1268 write(fd, result.string(), result.size());
1269 return OK;
1270}
1271
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001272const camera2_stream_ops *Camera2Device::StreamAdapter::getStreamOps() {
1273 return static_cast<camera2_stream_ops *>(this);
1274}
1275
1276ANativeWindow* Camera2Device::StreamAdapter::toANW(
1277 const camera2_stream_ops_t *w) {
1278 return static_cast<const StreamAdapter*>(w)->mConsumerInterface.get();
1279}
1280
1281int Camera2Device::StreamAdapter::dequeue_buffer(const camera2_stream_ops_t *w,
1282 buffer_handle_t** buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001283 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001284 int res;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001285 StreamAdapter* stream =
1286 const_cast<StreamAdapter*>(static_cast<const StreamAdapter*>(w));
1287 if (stream->mState != ACTIVE) {
1288 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, stream->mState);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001289 return INVALID_OPERATION;
1290 }
1291
1292 ANativeWindow *a = toANW(w);
1293 ANativeWindowBuffer* anb;
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001294 res = native_window_dequeue_buffer_and_wait(a, &anb);
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001295 if (res != OK) {
1296 ALOGE("Stream %d dequeue: Error from native_window: %s (%d)", stream->mId,
1297 strerror(-res), res);
1298 return res;
1299 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001300
1301 *buffer = &(anb->handle);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001302 stream->mActiveBuffers++;
1303
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001304 ALOGVV("Stream %d dequeue: Buffer %p dequeued", stream->mId, (void*)(**buffer));
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001305 return res;
1306}
1307
1308int Camera2Device::StreamAdapter::enqueue_buffer(const camera2_stream_ops_t* w,
1309 int64_t timestamp,
1310 buffer_handle_t* buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001311 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001312 StreamAdapter *stream =
1313 const_cast<StreamAdapter*>(static_cast<const StreamAdapter*>(w));
Eino-Ville Talvala228a5382012-08-13 12:16:06 -07001314 stream->mFrameCount++;
1315 ALOGVV("Stream %d enqueue: Frame %d (%p) captured at %lld ns",
James Donga289bf62012-09-05 16:46:36 -07001316 stream->mId, stream->mFrameCount, (void*)(*buffer), timestamp);
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001317 int state = stream->mState;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001318 if (state != ACTIVE) {
1319 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
1320 return INVALID_OPERATION;
1321 }
1322 ANativeWindow *a = toANW(w);
1323 status_t err;
Eino-Ville Talvala228a5382012-08-13 12:16:06 -07001324
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001325 err = native_window_set_buffers_timestamp(a, timestamp);
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001326 if (err != OK) {
1327 ALOGE("%s: Error setting timestamp on native window: %s (%d)",
1328 __FUNCTION__, strerror(-err), err);
1329 return err;
1330 }
1331 err = a->queueBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001332 container_of(buffer, ANativeWindowBuffer, handle), -1);
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001333 if (err != OK) {
1334 ALOGE("%s: Error queueing buffer to native window: %s (%d)",
1335 __FUNCTION__, strerror(-err), err);
James Dong31d377b2012-08-09 17:43:46 -07001336 return err;
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001337 }
James Dong31d377b2012-08-09 17:43:46 -07001338
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001339 stream->mActiveBuffers--;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001340 stream->mLastTimestamp = timestamp;
James Dong31d377b2012-08-09 17:43:46 -07001341 return OK;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001342}
1343
1344int Camera2Device::StreamAdapter::cancel_buffer(const camera2_stream_ops_t* w,
1345 buffer_handle_t* buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001346 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001347 StreamAdapter *stream =
1348 const_cast<StreamAdapter*>(static_cast<const StreamAdapter*>(w));
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001349 ALOGVV("Stream %d cancel: Buffer %p",
1350 stream->mId, (void*)(*buffer));
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001351 if (stream->mState != ACTIVE) {
1352 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, stream->mState);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001353 return INVALID_OPERATION;
1354 }
James Dong31d377b2012-08-09 17:43:46 -07001355
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001356 ANativeWindow *a = toANW(w);
James Dong31d377b2012-08-09 17:43:46 -07001357 int err = a->cancelBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001358 container_of(buffer, ANativeWindowBuffer, handle), -1);
James Dong31d377b2012-08-09 17:43:46 -07001359 if (err != OK) {
1360 ALOGE("%s: Error canceling buffer to native window: %s (%d)",
1361 __FUNCTION__, strerror(-err), err);
1362 return err;
1363 }
1364
1365 stream->mActiveBuffers--;
1366 return OK;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001367}
1368
1369int Camera2Device::StreamAdapter::set_crop(const camera2_stream_ops_t* w,
1370 int left, int top, int right, int bottom) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001371 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001372 int state = static_cast<const StreamAdapter*>(w)->mState;
1373 if (state != ACTIVE) {
1374 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
1375 return INVALID_OPERATION;
1376 }
1377 ANativeWindow *a = toANW(w);
1378 android_native_rect_t crop = { left, top, right, bottom };
1379 return native_window_set_crop(a, &crop);
1380}
1381
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001382/**
1383 * Camera2Device::ReprocessStreamAdapter
1384 */
1385
1386#ifndef container_of
1387#define container_of(ptr, type, member) \
1388 (type *)((char*)(ptr) - offsetof(type, member))
1389#endif
1390
1391Camera2Device::ReprocessStreamAdapter::ReprocessStreamAdapter(camera2_device_t *d):
1392 mState(RELEASED),
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001393 mHal2Device(d),
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001394 mId(-1),
1395 mWidth(0), mHeight(0), mFormat(0),
1396 mActiveBuffers(0),
1397 mFrameCount(0)
1398{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001399 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001400 camera2_stream_in_ops::acquire_buffer = acquire_buffer;
1401 camera2_stream_in_ops::release_buffer = release_buffer;
1402}
1403
1404Camera2Device::ReprocessStreamAdapter::~ReprocessStreamAdapter() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001405 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001406 if (mState != RELEASED) {
1407 release();
1408 }
1409}
1410
1411status_t Camera2Device::ReprocessStreamAdapter::connectToDevice(
1412 const sp<StreamAdapter> &outputStream) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001413 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001414 status_t res;
1415 ALOGV("%s: E", __FUNCTION__);
1416
1417 if (mState != RELEASED) return INVALID_OPERATION;
1418 if (outputStream == NULL) {
1419 ALOGE("%s: Null base stream passed to reprocess stream adapter",
1420 __FUNCTION__);
1421 return BAD_VALUE;
1422 }
1423
1424 mBaseStream = outputStream;
1425 mWidth = outputStream->getWidth();
1426 mHeight = outputStream->getHeight();
1427 mFormat = outputStream->getFormat();
1428
1429 ALOGV("%s: New reprocess stream parameters %d x %d, format 0x%x",
1430 __FUNCTION__, mWidth, mHeight, mFormat);
1431
1432 // Allocate device-side stream interface
1433
1434 uint32_t id;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001435 res = mHal2Device->ops->allocate_reprocess_stream_from_stream(mHal2Device,
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001436 outputStream->getId(), getStreamOps(),
1437 &id);
1438 if (res != OK) {
1439 ALOGE("%s: Device reprocess stream allocation failed: %s (%d)",
1440 __FUNCTION__, strerror(-res), res);
1441 return res;
1442 }
1443
1444 ALOGV("%s: Allocated reprocess stream id %d based on stream %d",
1445 __FUNCTION__, id, outputStream->getId());
1446
1447 mId = id;
1448
1449 mState = ACTIVE;
1450
1451 return OK;
1452}
1453
1454status_t Camera2Device::ReprocessStreamAdapter::release() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001455 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001456 status_t res;
1457 ALOGV("%s: Releasing stream %d", __FUNCTION__, mId);
1458 if (mState >= ACTIVE) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001459 res = mHal2Device->ops->release_reprocess_stream(mHal2Device, mId);
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001460 if (res != OK) {
1461 ALOGE("%s: Unable to release stream %d",
1462 __FUNCTION__, mId);
1463 return res;
1464 }
1465 }
1466
1467 List<QueueEntry>::iterator s;
1468 for (s = mQueue.begin(); s != mQueue.end(); s++) {
1469 sp<BufferReleasedListener> listener = s->releaseListener.promote();
1470 if (listener != 0) listener->onBufferReleased(s->handle);
1471 }
1472 for (s = mInFlightQueue.begin(); s != mInFlightQueue.end(); s++) {
1473 sp<BufferReleasedListener> listener = s->releaseListener.promote();
1474 if (listener != 0) listener->onBufferReleased(s->handle);
1475 }
1476 mQueue.clear();
1477 mInFlightQueue.clear();
1478
1479 mState = RELEASED;
1480 return OK;
1481}
1482
1483status_t Camera2Device::ReprocessStreamAdapter::pushIntoStream(
1484 buffer_handle_t *handle, const wp<BufferReleasedListener> &releaseListener) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001485 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001486 // TODO: Some error checking here would be nice
1487 ALOGV("%s: Pushing buffer %p to stream", __FUNCTION__, (void*)(*handle));
1488
1489 QueueEntry entry;
1490 entry.handle = handle;
1491 entry.releaseListener = releaseListener;
1492 mQueue.push_back(entry);
1493 return OK;
1494}
1495
1496status_t Camera2Device::ReprocessStreamAdapter::dump(int fd,
Igor Murashkinebe3f692012-10-12 16:56:11 -07001497 const Vector<String16>& /*args*/) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001498 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001499 String8 result =
1500 String8::format(" Reprocess stream %d: %d x %d, fmt 0x%x\n",
1501 mId, mWidth, mHeight, mFormat);
1502 result.appendFormat(" acquired buffers: %d\n",
1503 mActiveBuffers);
1504 result.appendFormat(" frame count: %d\n",
1505 mFrameCount);
1506 write(fd, result.string(), result.size());
1507 return OK;
1508}
1509
1510const camera2_stream_in_ops *Camera2Device::ReprocessStreamAdapter::getStreamOps() {
1511 return static_cast<camera2_stream_in_ops *>(this);
1512}
1513
1514int Camera2Device::ReprocessStreamAdapter::acquire_buffer(
1515 const camera2_stream_in_ops_t *w,
1516 buffer_handle_t** buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001517 ATRACE_CALL();
Igor Murashkinebe3f692012-10-12 16:56:11 -07001518
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001519 ReprocessStreamAdapter* stream =
1520 const_cast<ReprocessStreamAdapter*>(
1521 static_cast<const ReprocessStreamAdapter*>(w));
1522 if (stream->mState != ACTIVE) {
1523 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, stream->mState);
1524 return INVALID_OPERATION;
1525 }
1526
1527 if (stream->mQueue.empty()) {
1528 *buffer = NULL;
1529 return OK;
1530 }
1531
1532 QueueEntry &entry = *(stream->mQueue.begin());
1533
1534 *buffer = entry.handle;
1535
1536 stream->mInFlightQueue.push_back(entry);
1537 stream->mQueue.erase(stream->mQueue.begin());
1538
1539 stream->mActiveBuffers++;
1540
1541 ALOGV("Stream %d acquire: Buffer %p acquired", stream->mId,
1542 (void*)(**buffer));
1543 return OK;
1544}
1545
1546int Camera2Device::ReprocessStreamAdapter::release_buffer(
1547 const camera2_stream_in_ops_t* w,
1548 buffer_handle_t* buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001549 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001550 ReprocessStreamAdapter *stream =
1551 const_cast<ReprocessStreamAdapter*>(
1552 static_cast<const ReprocessStreamAdapter*>(w) );
1553 stream->mFrameCount++;
1554 ALOGV("Reprocess stream %d release: Frame %d (%p)",
1555 stream->mId, stream->mFrameCount, (void*)*buffer);
1556 int state = stream->mState;
1557 if (state != ACTIVE) {
1558 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
1559 return INVALID_OPERATION;
1560 }
1561 stream->mActiveBuffers--;
1562
1563 List<QueueEntry>::iterator s;
1564 for (s = stream->mInFlightQueue.begin(); s != stream->mInFlightQueue.end(); s++) {
1565 if ( s->handle == buffer ) break;
1566 }
1567 if (s == stream->mInFlightQueue.end()) {
1568 ALOGE("%s: Can't find buffer %p in in-flight list!", __FUNCTION__,
1569 buffer);
1570 return INVALID_OPERATION;
1571 }
1572
1573 sp<BufferReleasedListener> listener = s->releaseListener.promote();
1574 if (listener != 0) {
1575 listener->onBufferReleased(s->handle);
1576 } else {
1577 ALOGE("%s: Can't free buffer - missing listener", __FUNCTION__);
1578 }
1579 stream->mInFlightQueue.erase(s);
1580
1581 return OK;
1582}
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -07001583
1584}; // namespace android