blob: 8caadd693e36ebb471b02d12b8c6bb7deabe3113 [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
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070056status_t Camera2Device::initialize(camera_module_t *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
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070071 res = CameraService::filterOpenErrorCode(module->common.methods->open(
72 &module->common, name, reinterpret_cast<hw_device_t**>(&device)));
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070073
74 if (res != OK) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070075 ALOGE("%s: Could not open camera %d: %s (%d)", __FUNCTION__,
76 mId, strerror(-res), res);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070077 return res;
78 }
79
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070080 if (device->common.version != CAMERA_DEVICE_API_VERSION_2_0) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070081 ALOGE("%s: Could not open camera %d: "
82 "Camera device is not version %x, reports %x instead",
83 __FUNCTION__, mId, CAMERA_DEVICE_API_VERSION_2_0,
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070084 device->common.version);
85 device->common.close(&device->common);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070086 return BAD_VALUE;
87 }
88
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070089 camera_info info;
90 res = module->get_camera_info(mId, &info);
91 if (res != OK ) return res;
92
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070093 if (info.device_version != device->common.version) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070094 ALOGE("%s: HAL reporting mismatched camera_info version (%x)"
95 " and device version (%x).", __FUNCTION__,
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070096 device->common.version, info.device_version);
97 device->common.close(&device->common);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070098 return BAD_VALUE;
99 }
100
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700101 res = mRequestQueue.setConsumerDevice(device);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700102 if (res != OK) {
103 ALOGE("%s: Camera %d: Unable to connect request queue to device: %s (%d)",
104 __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700105 device->common.close(&device->common);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700106 return res;
107 }
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700108 res = mFrameQueue.setProducerDevice(device);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700109 if (res != OK) {
110 ALOGE("%s: Camera %d: Unable to connect frame queue to device: %s (%d)",
111 __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700112 device->common.close(&device->common);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700113 return res;
114 }
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700115
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700116 res = device->ops->set_notify_callback(device, notificationCallback,
117 NULL);
118 if (res != OK) {
119 ALOGE("%s: Camera %d: Unable to initialize notification callback!",
120 __FUNCTION__, mId);
121 device->common.close(&device->common);
122 return res;
123 }
124
125 mDeviceInfo = info.static_camera_characteristics;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800126 mHal2Device = device;
Zhijun He204e3292014-07-14 17:09:23 -0700127 mDeviceVersion = device->common.version;
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700128
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700129 return OK;
130}
131
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700132status_t Camera2Device::disconnect() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700133 ATRACE_CALL();
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700134 status_t res = OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800135 if (mHal2Device) {
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700136 ALOGV("%s: Closing device for camera %d", __FUNCTION__, mId);
137
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800138 int inProgressCount = mHal2Device->ops->get_in_progress_count(mHal2Device);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700139 if (inProgressCount > 0) {
140 ALOGW("%s: Closing camera device %d with %d requests in flight!",
141 __FUNCTION__, mId, inProgressCount);
142 }
Eino-Ville Talvalac62bb782012-09-24 13:44:07 -0700143 mReprocessStreams.clear();
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700144 mStreams.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800145 res = mHal2Device->common.close(&mHal2Device->common);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700146 if (res != OK) {
147 ALOGE("%s: Could not close camera %d: %s (%d)",
148 __FUNCTION__,
149 mId, strerror(-res), res);
150 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800151 mHal2Device = NULL;
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700152 ALOGV("%s: Shutdown complete", __FUNCTION__);
153 }
154 return res;
155}
156
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700157status_t Camera2Device::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700158 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700159 String8 result;
Eino-Ville Talvala97197152012-08-06 14:25:19 -0700160 int detailLevel = 0;
161 int n = args.size();
162 String16 detailOption("-d");
163 for (int i = 0; i + 1 < n; i++) {
164 if (args[i] == detailOption) {
165 String8 levelStr(args[i+1]);
166 detailLevel = atoi(levelStr.string());
167 }
168 }
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700169
Eino-Ville Talvala603b12e2012-08-08 09:25:58 -0700170 result.appendFormat(" Camera2Device[%d] dump (detail level %d):\n",
171 mId, detailLevel);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700172
Eino-Ville Talvala97197152012-08-06 14:25:19 -0700173 if (detailLevel > 0) {
174 result = " Request queue contents:\n";
175 write(fd, result.string(), result.size());
176 mRequestQueue.dump(fd, args);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700177
Eino-Ville Talvala97197152012-08-06 14:25:19 -0700178 result = " Frame queue contents:\n";
179 write(fd, result.string(), result.size());
180 mFrameQueue.dump(fd, args);
181 }
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700182
183 result = " Active streams:\n";
184 write(fd, result.string(), result.size());
185 for (StreamList::iterator s = mStreams.begin(); s != mStreams.end(); s++) {
186 (*s)->dump(fd, args);
187 }
188
189 result = " HAL device dump:\n";
190 write(fd, result.string(), result.size());
191
192 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800193 res = mHal2Device->ops->dump(mHal2Device, fd);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700194
195 return res;
196}
197
Igor Murashkinbd02dd12013-02-13 15:53:56 -0800198const CameraMetadata& Camera2Device::info() const {
Eino-Ville Talvala9e4c3db2012-07-20 11:07:52 -0700199 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700200
201 return mDeviceInfo;
202}
203
Jianing Weicb0652e2014-03-12 18:29:36 -0700204status_t Camera2Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700205 ATRACE_CALL();
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700206 ALOGV("%s: E", __FUNCTION__);
207
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700208 mRequestQueue.enqueue(request.release());
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700209 return OK;
210}
211
Jianing Weicb0652e2014-03-12 18:29:36 -0700212status_t Camera2Device::captureList(const List<const CameraMetadata> &requests,
213 int64_t* /*lastFrameNumber*/) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700214 ATRACE_CALL();
215 ALOGE("%s: Camera2Device burst capture not implemented", __FUNCTION__);
216 return INVALID_OPERATION;
217}
218
Jianing Weicb0652e2014-03-12 18:29:36 -0700219status_t Camera2Device::setStreamingRequest(const CameraMetadata &request,
220 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700221 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700222 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700223 CameraMetadata streamRequest(request);
224 return mRequestQueue.setStreamSlot(streamRequest.release());
225}
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700226
Jianing Weicb0652e2014-03-12 18:29:36 -0700227status_t Camera2Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
228 int64_t* /*lastFrameNumber*/) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700229 ATRACE_CALL();
230 ALOGE("%s, Camera2Device streaming burst not implemented", __FUNCTION__);
231 return INVALID_OPERATION;
232}
233
Jianing Weicb0652e2014-03-12 18:29:36 -0700234status_t Camera2Device::clearStreamingRequest(int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700235 ATRACE_CALL();
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700236 return mRequestQueue.setStreamSlot(NULL);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700237}
238
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700239status_t Camera2Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
240 ATRACE_CALL();
241 return mRequestQueue.waitForDequeue(requestId, timeout);
242}
243
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700244status_t Camera2Device::createStream(sp<ANativeWindow> consumer,
Zhijun He28c9b6f2014-08-08 12:00:47 -0700245 uint32_t width, uint32_t height, int format, 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 camera_metadata_t *buf2 = clone_camera_metadata(buf);
797 if (!buf2) {
798 ALOGE("%s: Unable to clone metadata buffer!", __FUNCTION__);
799 return NO_MEMORY;
800 }
801
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700802 if (mStreamSlotCount > 1) {
803 List<camera_metadata_t*>::iterator deleter = ++mStreamSlot.begin();
804 freeBuffers(++mStreamSlot.begin(), mStreamSlot.end());
805 mStreamSlotCount = 1;
806 }
807 if (mStreamSlotCount == 1) {
808 free_camera_metadata( *(mStreamSlot.begin()) );
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700809 *(mStreamSlot.begin()) = buf2;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700810 } else {
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700811 mStreamSlot.push_front(buf2);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700812 mStreamSlotCount = 1;
813 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700814 return signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700815}
816
817status_t Camera2Device::MetadataQueue::setStreamSlot(
818 const List<camera_metadata_t*> &bufs)
819{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700820 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700821 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700822 Mutex::Autolock l(mMutex);
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700823
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700824 if (mStreamSlotCount > 0) {
825 freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
826 }
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700827 mStreamSlotCount = 0;
828 for (List<camera_metadata_t*>::const_iterator r = bufs.begin();
829 r != bufs.end(); r++) {
830 camera_metadata_t *r2 = clone_camera_metadata(*r);
831 if (!r2) {
832 ALOGE("%s: Unable to clone metadata buffer!", __FUNCTION__);
833 return NO_MEMORY;
834 }
835 mStreamSlot.push_back(r2);
836 mStreamSlotCount++;
837 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700838 return signalConsumerLocked();
839}
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700840
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700841status_t Camera2Device::MetadataQueue::clear()
842{
843 ATRACE_CALL();
844 ALOGV("%s: E", __FUNCTION__);
845
846 Mutex::Autolock l(mMutex);
847
848 // Clear streaming slot
849 freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
850 mStreamSlotCount = 0;
851
852 // Clear request queue
853 freeBuffers(mEntries.begin(), mEntries.end());
854 mCount = 0;
855 return OK;
856}
857
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700858status_t Camera2Device::MetadataQueue::dump(int fd,
Igor Murashkinebe3f692012-10-12 16:56:11 -0700859 const Vector<String16>& /*args*/) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700860 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700861 String8 result;
862 status_t notLocked;
863 notLocked = mMutex.tryLock();
864 if (notLocked) {
865 result.append(" (Unable to lock queue mutex)\n");
866 }
867 result.appendFormat(" Current frame number: %d\n", mFrameCount);
868 if (mStreamSlotCount == 0) {
869 result.append(" Stream slot: Empty\n");
870 write(fd, result.string(), result.size());
871 } else {
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000872 result.appendFormat(" Stream slot: %zu entries\n",
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700873 mStreamSlot.size());
874 int i = 0;
875 for (List<camera_metadata_t*>::iterator r = mStreamSlot.begin();
876 r != mStreamSlot.end(); r++) {
877 result = String8::format(" Stream slot buffer %d:\n", i);
878 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -0700879 dump_indented_camera_metadata(*r, fd, 2, 10);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700880 i++;
881 }
882 }
883 if (mEntries.size() == 0) {
884 result = " Main queue is empty\n";
885 write(fd, result.string(), result.size());
886 } else {
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000887 result = String8::format(" Main queue has %zu entries:\n",
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700888 mEntries.size());
889 int i = 0;
890 for (List<camera_metadata_t*>::iterator r = mEntries.begin();
891 r != mEntries.end(); r++) {
892 result = String8::format(" Queue entry %d:\n", i);
893 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -0700894 dump_indented_camera_metadata(*r, fd, 2, 10);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700895 i++;
896 }
897 }
898
899 if (notLocked == 0) {
900 mMutex.unlock();
901 }
902
903 return OK;
904}
905
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700906status_t Camera2Device::MetadataQueue::signalConsumerLocked() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700907 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700908 status_t res = OK;
909 notEmpty.signal();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800910 if (mSignalConsumer && mHal2Device != NULL) {
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700911 mSignalConsumer = false;
912
913 mMutex.unlock();
914 ALOGV("%s: Signaling consumer", __FUNCTION__);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800915 res = mHal2Device->ops->notify_request_queue_not_empty(mHal2Device);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700916 mMutex.lock();
917 }
918 return res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700919}
920
921status_t Camera2Device::MetadataQueue::freeBuffers(
922 List<camera_metadata_t*>::iterator start,
923 List<camera_metadata_t*>::iterator end)
924{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700925 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700926 while (start != end) {
927 free_camera_metadata(*start);
928 start = mStreamSlot.erase(start);
929 }
930 return OK;
931}
932
933Camera2Device::MetadataQueue* Camera2Device::MetadataQueue::getInstance(
934 const camera2_request_queue_src_ops_t *q)
935{
936 const MetadataQueue* cmq = static_cast<const MetadataQueue*>(q);
937 return const_cast<MetadataQueue*>(cmq);
938}
939
940Camera2Device::MetadataQueue* Camera2Device::MetadataQueue::getInstance(
941 const camera2_frame_queue_dst_ops_t *q)
942{
943 const MetadataQueue* cmq = static_cast<const MetadataQueue*>(q);
944 return const_cast<MetadataQueue*>(cmq);
945}
946
947int Camera2Device::MetadataQueue::consumer_buffer_count(
948 const camera2_request_queue_src_ops_t *q)
949{
950 MetadataQueue *queue = getInstance(q);
951 return queue->getBufferCount();
952}
953
954int Camera2Device::MetadataQueue::consumer_dequeue(
955 const camera2_request_queue_src_ops_t *q,
956 camera_metadata_t **buffer)
957{
958 MetadataQueue *queue = getInstance(q);
959 return queue->dequeue(buffer, true);
960}
961
962int Camera2Device::MetadataQueue::consumer_free(
963 const camera2_request_queue_src_ops_t *q,
964 camera_metadata_t *old_buffer)
965{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700966 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700967 MetadataQueue *queue = getInstance(q);
Igor Murashkinebe3f692012-10-12 16:56:11 -0700968 (void)queue;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700969 free_camera_metadata(old_buffer);
970 return OK;
971}
972
973int Camera2Device::MetadataQueue::producer_dequeue(
Igor Murashkinebe3f692012-10-12 16:56:11 -0700974 const camera2_frame_queue_dst_ops_t * /*q*/,
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700975 size_t entries, size_t bytes,
976 camera_metadata_t **buffer)
977{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700978 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700979 camera_metadata_t *new_buffer =
980 allocate_camera_metadata(entries, bytes);
981 if (new_buffer == NULL) return NO_MEMORY;
982 *buffer = new_buffer;
983 return OK;
984}
985
986int Camera2Device::MetadataQueue::producer_cancel(
Igor Murashkinebe3f692012-10-12 16:56:11 -0700987 const camera2_frame_queue_dst_ops_t * /*q*/,
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700988 camera_metadata_t *old_buffer)
989{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700990 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700991 free_camera_metadata(old_buffer);
992 return OK;
993}
994
995int Camera2Device::MetadataQueue::producer_enqueue(
996 const camera2_frame_queue_dst_ops_t *q,
997 camera_metadata_t *filled_buffer)
998{
999 MetadataQueue *queue = getInstance(q);
1000 return queue->enqueue(filled_buffer);
1001}
1002
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001003/**
1004 * Camera2Device::StreamAdapter
1005 */
1006
1007#ifndef container_of
1008#define container_of(ptr, type, member) \
1009 (type *)((char*)(ptr) - offsetof(type, member))
1010#endif
1011
1012Camera2Device::StreamAdapter::StreamAdapter(camera2_device_t *d):
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001013 mState(RELEASED),
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001014 mHal2Device(d),
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001015 mId(-1),
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001016 mWidth(0), mHeight(0), mFormat(0), mSize(0), mUsage(0),
1017 mMaxProducerBuffers(0), mMaxConsumerBuffers(0),
1018 mTotalBuffers(0),
1019 mFormatRequested(0),
1020 mActiveBuffers(0),
1021 mFrameCount(0),
1022 mLastTimestamp(0)
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001023{
1024 camera2_stream_ops::dequeue_buffer = dequeue_buffer;
1025 camera2_stream_ops::enqueue_buffer = enqueue_buffer;
1026 camera2_stream_ops::cancel_buffer = cancel_buffer;
1027 camera2_stream_ops::set_crop = set_crop;
1028}
1029
1030Camera2Device::StreamAdapter::~StreamAdapter() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001031 ATRACE_CALL();
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001032 if (mState != RELEASED) {
1033 release();
1034 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001035}
1036
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -07001037status_t Camera2Device::StreamAdapter::connectToDevice(
1038 sp<ANativeWindow> consumer,
1039 uint32_t width, uint32_t height, int format, size_t size) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001040 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001041 status_t res;
Eino-Ville Talvala9e4c3db2012-07-20 11:07:52 -07001042 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001043
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001044 if (mState != RELEASED) return INVALID_OPERATION;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001045 if (consumer == NULL) {
1046 ALOGE("%s: Null consumer passed to stream adapter", __FUNCTION__);
1047 return BAD_VALUE;
1048 }
1049
Colin Crosse5729fa2014-03-21 15:04:25 -07001050 ALOGV("%s: New stream parameters %d x %d, format 0x%x, size %zu",
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001051 __FUNCTION__, width, height, format, size);
1052
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001053 mConsumerInterface = consumer;
1054 mWidth = width;
1055 mHeight = height;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -07001056 mSize = (format == HAL_PIXEL_FORMAT_BLOB) ? size : 0;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001057 mFormatRequested = format;
1058
1059 // Allocate device-side stream interface
1060
1061 uint32_t id;
1062 uint32_t formatActual;
1063 uint32_t usage;
1064 uint32_t maxBuffers = 2;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001065 res = mHal2Device->ops->allocate_stream(mHal2Device,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001066 mWidth, mHeight, mFormatRequested, getStreamOps(),
1067 &id, &formatActual, &usage, &maxBuffers);
1068 if (res != OK) {
1069 ALOGE("%s: Device stream allocation failed: %s (%d)",
1070 __FUNCTION__, strerror(-res), res);
1071 return res;
1072 }
1073
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001074 ALOGV("%s: Allocated stream id %d, actual format 0x%x, "
1075 "usage 0x%x, producer wants %d buffers", __FUNCTION__,
1076 id, formatActual, usage, maxBuffers);
1077
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001078 mId = id;
1079 mFormat = formatActual;
1080 mUsage = usage;
1081 mMaxProducerBuffers = maxBuffers;
1082
1083 mState = ALLOCATED;
1084
1085 // Configure consumer-side ANativeWindow interface
1086 res = native_window_api_connect(mConsumerInterface.get(),
1087 NATIVE_WINDOW_API_CAMERA);
1088 if (res != OK) {
1089 ALOGE("%s: Unable to connect to native window for stream %d",
1090 __FUNCTION__, mId);
1091
1092 return res;
1093 }
1094
1095 mState = CONNECTED;
1096
1097 res = native_window_set_usage(mConsumerInterface.get(), mUsage);
1098 if (res != OK) {
1099 ALOGE("%s: Unable to configure usage %08x for stream %d",
1100 __FUNCTION__, mUsage, mId);
1101 return res;
1102 }
1103
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001104 res = native_window_set_scaling_mode(mConsumerInterface.get(),
1105 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1106 if (res != OK) {
1107 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
1108 __FUNCTION__, strerror(-res), res);
1109 return res;
1110 }
1111
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -07001112 res = setTransform(0);
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001113 if (res != OK) {
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001114 return res;
1115 }
1116
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -07001117 if (mFormat == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -07001118 res = native_window_set_buffers_dimensions(mConsumerInterface.get(),
1119 mSize, 1);
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -07001120 if (res != OK) {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -07001121 ALOGE("%s: Unable to configure compressed stream buffer dimensions"
Colin Crosse5729fa2014-03-21 15:04:25 -07001122 " %d x %d, size %zu for stream %d",
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -07001123 __FUNCTION__, mWidth, mHeight, mSize, mId);
1124 return res;
1125 }
1126 } else {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -07001127 res = native_window_set_buffers_dimensions(mConsumerInterface.get(),
1128 mWidth, mHeight);
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -07001129 if (res != OK) {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -07001130 ALOGE("%s: Unable to configure stream buffer dimensions"
1131 " %d x %d for stream %d",
1132 __FUNCTION__, mWidth, mHeight, mId);
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -07001133 return res;
1134 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001135 }
1136
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -07001137 res = native_window_set_buffers_format(mConsumerInterface.get(), mFormat);
1138 if (res != OK) {
1139 ALOGE("%s: Unable to configure stream buffer format"
1140 " %#x for stream %d",
1141 __FUNCTION__, mFormat, mId);
1142 return res;
1143 }
1144
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001145 int maxConsumerBuffers;
1146 res = mConsumerInterface->query(mConsumerInterface.get(),
1147 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
1148 if (res != OK) {
1149 ALOGE("%s: Unable to query consumer undequeued"
1150 " buffer count for stream %d", __FUNCTION__, mId);
1151 return res;
1152 }
1153 mMaxConsumerBuffers = maxConsumerBuffers;
1154
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001155 ALOGV("%s: Consumer wants %d buffers", __FUNCTION__,
1156 mMaxConsumerBuffers);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001157
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001158 mTotalBuffers = mMaxConsumerBuffers + mMaxProducerBuffers;
1159 mActiveBuffers = 0;
1160 mFrameCount = 0;
1161 mLastTimestamp = 0;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001162
1163 res = native_window_set_buffer_count(mConsumerInterface.get(),
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001164 mTotalBuffers);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001165 if (res != OK) {
1166 ALOGE("%s: Unable to set buffer count for stream %d",
1167 __FUNCTION__, mId);
1168 return res;
1169 }
1170
1171 // Register allocated buffers with HAL device
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001172 buffer_handle_t *buffers = new buffer_handle_t[mTotalBuffers];
1173 ANativeWindowBuffer **anwBuffers = new ANativeWindowBuffer*[mTotalBuffers];
1174 uint32_t bufferIdx = 0;
1175 for (; bufferIdx < mTotalBuffers; bufferIdx++) {
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001176 res = native_window_dequeue_buffer_and_wait(mConsumerInterface.get(),
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001177 &anwBuffers[bufferIdx]);
1178 if (res != OK) {
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001179 ALOGE("%s: Unable to dequeue buffer %d for initial registration for "
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001180 "stream %d", __FUNCTION__, bufferIdx, mId);
1181 goto cleanUpBuffers;
1182 }
1183
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001184 buffers[bufferIdx] = anwBuffers[bufferIdx]->handle;
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001185 ALOGV("%s: Buffer %p allocated", __FUNCTION__, (void*)buffers[bufferIdx]);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001186 }
1187
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001188 ALOGV("%s: Registering %d buffers with camera HAL", __FUNCTION__, mTotalBuffers);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001189 res = mHal2Device->ops->register_stream_buffers(mHal2Device,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001190 mId,
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001191 mTotalBuffers,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001192 buffers);
1193 if (res != OK) {
1194 ALOGE("%s: Unable to register buffers with HAL device for stream %d",
1195 __FUNCTION__, mId);
1196 } else {
1197 mState = ACTIVE;
1198 }
1199
1200cleanUpBuffers:
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001201 ALOGV("%s: Cleaning up %d buffers", __FUNCTION__, bufferIdx);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001202 for (uint32_t i = 0; i < bufferIdx; i++) {
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001203 res = mConsumerInterface->cancelBuffer(mConsumerInterface.get(),
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001204 anwBuffers[i], -1);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001205 if (res != OK) {
1206 ALOGE("%s: Unable to cancel buffer %d after registration",
1207 __FUNCTION__, i);
1208 }
1209 }
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001210 delete[] anwBuffers;
1211 delete[] buffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001212
1213 return res;
1214}
1215
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001216status_t Camera2Device::StreamAdapter::release() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001217 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001218 status_t res;
Eino-Ville Talvala02f84572013-04-23 15:16:57 -07001219 ALOGV("%s: Releasing stream %d (%d x %d, format %d)", __FUNCTION__, mId,
1220 mWidth, mHeight, mFormat);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001221 if (mState >= ALLOCATED) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001222 res = mHal2Device->ops->release_stream(mHal2Device, mId);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001223 if (res != OK) {
1224 ALOGE("%s: Unable to release stream %d",
1225 __FUNCTION__, mId);
1226 return res;
1227 }
1228 }
1229 if (mState >= CONNECTED) {
1230 res = native_window_api_disconnect(mConsumerInterface.get(),
1231 NATIVE_WINDOW_API_CAMERA);
Igor Murashkina1e5dcc2012-10-02 15:21:31 -07001232
1233 /* this is not an error. if client calling process dies,
1234 the window will also die and all calls to it will return
1235 DEAD_OBJECT, thus it's already "disconnected" */
1236 if (res == DEAD_OBJECT) {
1237 ALOGW("%s: While disconnecting stream %d from native window, the"
1238 " native window died from under us", __FUNCTION__, mId);
1239 }
1240 else if (res != OK) {
1241 ALOGE("%s: Unable to disconnect stream %d from native window (error %d %s)",
1242 __FUNCTION__, mId, res, strerror(-res));
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001243 return res;
1244 }
1245 }
1246 mId = -1;
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001247 mState = RELEASED;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001248 return OK;
1249}
1250
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -07001251status_t Camera2Device::StreamAdapter::setTransform(int transform) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001252 ATRACE_CALL();
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -07001253 status_t res;
1254 if (mState < CONNECTED) {
1255 ALOGE("%s: Cannot set transform on unconnected stream", __FUNCTION__);
1256 return INVALID_OPERATION;
1257 }
1258 res = native_window_set_buffers_transform(mConsumerInterface.get(),
1259 transform);
1260 if (res != OK) {
1261 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
1262 __FUNCTION__, transform, strerror(-res), res);
1263 }
1264 return res;
1265}
1266
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001267status_t Camera2Device::StreamAdapter::dump(int fd,
Igor Murashkinebe3f692012-10-12 16:56:11 -07001268 const Vector<String16>& /*args*/) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001269 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001270 String8 result = String8::format(" Stream %d: %d x %d, format 0x%x\n",
1271 mId, mWidth, mHeight, mFormat);
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001272 result.appendFormat(" size %zu, usage 0x%x, requested format 0x%x\n",
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001273 mSize, mUsage, mFormatRequested);
1274 result.appendFormat(" total buffers: %d, dequeued buffers: %d\n",
1275 mTotalBuffers, mActiveBuffers);
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001276 result.appendFormat(" frame count: %d, last timestamp %" PRId64 "\n",
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001277 mFrameCount, mLastTimestamp);
1278 write(fd, result.string(), result.size());
1279 return OK;
1280}
1281
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001282const camera2_stream_ops *Camera2Device::StreamAdapter::getStreamOps() {
1283 return static_cast<camera2_stream_ops *>(this);
1284}
1285
1286ANativeWindow* Camera2Device::StreamAdapter::toANW(
1287 const camera2_stream_ops_t *w) {
1288 return static_cast<const StreamAdapter*>(w)->mConsumerInterface.get();
1289}
1290
1291int Camera2Device::StreamAdapter::dequeue_buffer(const camera2_stream_ops_t *w,
1292 buffer_handle_t** buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001293 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001294 int res;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001295 StreamAdapter* stream =
1296 const_cast<StreamAdapter*>(static_cast<const StreamAdapter*>(w));
1297 if (stream->mState != ACTIVE) {
1298 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, stream->mState);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001299 return INVALID_OPERATION;
1300 }
1301
1302 ANativeWindow *a = toANW(w);
1303 ANativeWindowBuffer* anb;
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001304 res = native_window_dequeue_buffer_and_wait(a, &anb);
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001305 if (res != OK) {
1306 ALOGE("Stream %d dequeue: Error from native_window: %s (%d)", stream->mId,
1307 strerror(-res), res);
1308 return res;
1309 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001310
1311 *buffer = &(anb->handle);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001312 stream->mActiveBuffers++;
1313
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001314 ALOGVV("Stream %d dequeue: Buffer %p dequeued", stream->mId, (void*)(**buffer));
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001315 return res;
1316}
1317
1318int Camera2Device::StreamAdapter::enqueue_buffer(const camera2_stream_ops_t* w,
1319 int64_t timestamp,
1320 buffer_handle_t* buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001321 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001322 StreamAdapter *stream =
1323 const_cast<StreamAdapter*>(static_cast<const StreamAdapter*>(w));
Eino-Ville Talvala228a5382012-08-13 12:16:06 -07001324 stream->mFrameCount++;
1325 ALOGVV("Stream %d enqueue: Frame %d (%p) captured at %lld ns",
James Donga289bf62012-09-05 16:46:36 -07001326 stream->mId, stream->mFrameCount, (void*)(*buffer), timestamp);
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001327 int state = stream->mState;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001328 if (state != ACTIVE) {
1329 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
1330 return INVALID_OPERATION;
1331 }
1332 ANativeWindow *a = toANW(w);
1333 status_t err;
Eino-Ville Talvala228a5382012-08-13 12:16:06 -07001334
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001335 err = native_window_set_buffers_timestamp(a, timestamp);
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001336 if (err != OK) {
1337 ALOGE("%s: Error setting timestamp on native window: %s (%d)",
1338 __FUNCTION__, strerror(-err), err);
1339 return err;
1340 }
1341 err = a->queueBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001342 container_of(buffer, ANativeWindowBuffer, handle), -1);
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001343 if (err != OK) {
1344 ALOGE("%s: Error queueing buffer to native window: %s (%d)",
1345 __FUNCTION__, strerror(-err), err);
James Dong31d377b2012-08-09 17:43:46 -07001346 return err;
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001347 }
James Dong31d377b2012-08-09 17:43:46 -07001348
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001349 stream->mActiveBuffers--;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001350 stream->mLastTimestamp = timestamp;
James Dong31d377b2012-08-09 17:43:46 -07001351 return OK;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001352}
1353
1354int Camera2Device::StreamAdapter::cancel_buffer(const camera2_stream_ops_t* w,
1355 buffer_handle_t* buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001356 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001357 StreamAdapter *stream =
1358 const_cast<StreamAdapter*>(static_cast<const StreamAdapter*>(w));
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001359 ALOGVV("Stream %d cancel: Buffer %p",
1360 stream->mId, (void*)(*buffer));
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001361 if (stream->mState != ACTIVE) {
1362 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, stream->mState);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001363 return INVALID_OPERATION;
1364 }
James Dong31d377b2012-08-09 17:43:46 -07001365
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001366 ANativeWindow *a = toANW(w);
James Dong31d377b2012-08-09 17:43:46 -07001367 int err = a->cancelBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001368 container_of(buffer, ANativeWindowBuffer, handle), -1);
James Dong31d377b2012-08-09 17:43:46 -07001369 if (err != OK) {
1370 ALOGE("%s: Error canceling buffer to native window: %s (%d)",
1371 __FUNCTION__, strerror(-err), err);
1372 return err;
1373 }
1374
1375 stream->mActiveBuffers--;
1376 return OK;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001377}
1378
1379int Camera2Device::StreamAdapter::set_crop(const camera2_stream_ops_t* w,
1380 int left, int top, int right, int bottom) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001381 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001382 int state = static_cast<const StreamAdapter*>(w)->mState;
1383 if (state != ACTIVE) {
1384 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
1385 return INVALID_OPERATION;
1386 }
1387 ANativeWindow *a = toANW(w);
1388 android_native_rect_t crop = { left, top, right, bottom };
1389 return native_window_set_crop(a, &crop);
1390}
1391
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001392/**
1393 * Camera2Device::ReprocessStreamAdapter
1394 */
1395
1396#ifndef container_of
1397#define container_of(ptr, type, member) \
1398 (type *)((char*)(ptr) - offsetof(type, member))
1399#endif
1400
1401Camera2Device::ReprocessStreamAdapter::ReprocessStreamAdapter(camera2_device_t *d):
1402 mState(RELEASED),
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001403 mHal2Device(d),
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001404 mId(-1),
1405 mWidth(0), mHeight(0), mFormat(0),
1406 mActiveBuffers(0),
1407 mFrameCount(0)
1408{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001409 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001410 camera2_stream_in_ops::acquire_buffer = acquire_buffer;
1411 camera2_stream_in_ops::release_buffer = release_buffer;
1412}
1413
1414Camera2Device::ReprocessStreamAdapter::~ReprocessStreamAdapter() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001415 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001416 if (mState != RELEASED) {
1417 release();
1418 }
1419}
1420
1421status_t Camera2Device::ReprocessStreamAdapter::connectToDevice(
1422 const sp<StreamAdapter> &outputStream) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001423 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001424 status_t res;
1425 ALOGV("%s: E", __FUNCTION__);
1426
1427 if (mState != RELEASED) return INVALID_OPERATION;
1428 if (outputStream == NULL) {
1429 ALOGE("%s: Null base stream passed to reprocess stream adapter",
1430 __FUNCTION__);
1431 return BAD_VALUE;
1432 }
1433
1434 mBaseStream = outputStream;
1435 mWidth = outputStream->getWidth();
1436 mHeight = outputStream->getHeight();
1437 mFormat = outputStream->getFormat();
1438
1439 ALOGV("%s: New reprocess stream parameters %d x %d, format 0x%x",
1440 __FUNCTION__, mWidth, mHeight, mFormat);
1441
1442 // Allocate device-side stream interface
1443
1444 uint32_t id;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001445 res = mHal2Device->ops->allocate_reprocess_stream_from_stream(mHal2Device,
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001446 outputStream->getId(), getStreamOps(),
1447 &id);
1448 if (res != OK) {
1449 ALOGE("%s: Device reprocess stream allocation failed: %s (%d)",
1450 __FUNCTION__, strerror(-res), res);
1451 return res;
1452 }
1453
1454 ALOGV("%s: Allocated reprocess stream id %d based on stream %d",
1455 __FUNCTION__, id, outputStream->getId());
1456
1457 mId = id;
1458
1459 mState = ACTIVE;
1460
1461 return OK;
1462}
1463
1464status_t Camera2Device::ReprocessStreamAdapter::release() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001465 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001466 status_t res;
1467 ALOGV("%s: Releasing stream %d", __FUNCTION__, mId);
1468 if (mState >= ACTIVE) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001469 res = mHal2Device->ops->release_reprocess_stream(mHal2Device, mId);
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001470 if (res != OK) {
1471 ALOGE("%s: Unable to release stream %d",
1472 __FUNCTION__, mId);
1473 return res;
1474 }
1475 }
1476
1477 List<QueueEntry>::iterator s;
1478 for (s = mQueue.begin(); s != mQueue.end(); s++) {
1479 sp<BufferReleasedListener> listener = s->releaseListener.promote();
1480 if (listener != 0) listener->onBufferReleased(s->handle);
1481 }
1482 for (s = mInFlightQueue.begin(); s != mInFlightQueue.end(); s++) {
1483 sp<BufferReleasedListener> listener = s->releaseListener.promote();
1484 if (listener != 0) listener->onBufferReleased(s->handle);
1485 }
1486 mQueue.clear();
1487 mInFlightQueue.clear();
1488
1489 mState = RELEASED;
1490 return OK;
1491}
1492
1493status_t Camera2Device::ReprocessStreamAdapter::pushIntoStream(
1494 buffer_handle_t *handle, const wp<BufferReleasedListener> &releaseListener) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001495 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001496 // TODO: Some error checking here would be nice
1497 ALOGV("%s: Pushing buffer %p to stream", __FUNCTION__, (void*)(*handle));
1498
1499 QueueEntry entry;
1500 entry.handle = handle;
1501 entry.releaseListener = releaseListener;
1502 mQueue.push_back(entry);
1503 return OK;
1504}
1505
1506status_t Camera2Device::ReprocessStreamAdapter::dump(int fd,
Igor Murashkinebe3f692012-10-12 16:56:11 -07001507 const Vector<String16>& /*args*/) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001508 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001509 String8 result =
1510 String8::format(" Reprocess stream %d: %d x %d, fmt 0x%x\n",
1511 mId, mWidth, mHeight, mFormat);
1512 result.appendFormat(" acquired buffers: %d\n",
1513 mActiveBuffers);
1514 result.appendFormat(" frame count: %d\n",
1515 mFrameCount);
1516 write(fd, result.string(), result.size());
1517 return OK;
1518}
1519
1520const camera2_stream_in_ops *Camera2Device::ReprocessStreamAdapter::getStreamOps() {
1521 return static_cast<camera2_stream_in_ops *>(this);
1522}
1523
1524int Camera2Device::ReprocessStreamAdapter::acquire_buffer(
1525 const camera2_stream_in_ops_t *w,
1526 buffer_handle_t** buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001527 ATRACE_CALL();
Igor Murashkinebe3f692012-10-12 16:56:11 -07001528
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001529 ReprocessStreamAdapter* stream =
1530 const_cast<ReprocessStreamAdapter*>(
1531 static_cast<const ReprocessStreamAdapter*>(w));
1532 if (stream->mState != ACTIVE) {
1533 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, stream->mState);
1534 return INVALID_OPERATION;
1535 }
1536
1537 if (stream->mQueue.empty()) {
1538 *buffer = NULL;
1539 return OK;
1540 }
1541
1542 QueueEntry &entry = *(stream->mQueue.begin());
1543
1544 *buffer = entry.handle;
1545
1546 stream->mInFlightQueue.push_back(entry);
1547 stream->mQueue.erase(stream->mQueue.begin());
1548
1549 stream->mActiveBuffers++;
1550
1551 ALOGV("Stream %d acquire: Buffer %p acquired", stream->mId,
1552 (void*)(**buffer));
1553 return OK;
1554}
1555
1556int Camera2Device::ReprocessStreamAdapter::release_buffer(
1557 const camera2_stream_in_ops_t* w,
1558 buffer_handle_t* buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001559 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001560 ReprocessStreamAdapter *stream =
1561 const_cast<ReprocessStreamAdapter*>(
1562 static_cast<const ReprocessStreamAdapter*>(w) );
1563 stream->mFrameCount++;
1564 ALOGV("Reprocess stream %d release: Frame %d (%p)",
1565 stream->mId, stream->mFrameCount, (void*)*buffer);
1566 int state = stream->mState;
1567 if (state != ACTIVE) {
1568 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
1569 return INVALID_OPERATION;
1570 }
1571 stream->mActiveBuffers--;
1572
1573 List<QueueEntry>::iterator s;
1574 for (s = stream->mInFlightQueue.begin(); s != stream->mInFlightQueue.end(); s++) {
1575 if ( s->handle == buffer ) break;
1576 }
1577 if (s == stream->mInFlightQueue.end()) {
1578 ALOGE("%s: Can't find buffer %p in in-flight list!", __FUNCTION__,
1579 buffer);
1580 return INVALID_OPERATION;
1581 }
1582
1583 sp<BufferReleasedListener> listener = s->releaseListener.promote();
1584 if (listener != 0) {
1585 listener->onBufferReleased(s->handle);
1586 } else {
1587 ALOGE("%s: Can't free buffer - missing listener", __FUNCTION__);
1588 }
1589 stream->mInFlightQueue.erase(s);
1590
1591 return OK;
1592}
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -07001593
1594}; // namespace android