blob: 710d0e9f27e5e958ca3b80a1bd5b78b7d6dea644 [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
28#include <utils/Log.h>
Eino-Ville Talvala852c3812012-09-24 09:46:53 -070029#include <utils/Trace.h>
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -070030#include <utils/Timers.h>
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070031#include "Camera2Device.h"
32
33namespace android {
34
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070035Camera2Device::Camera2Device(int id):
36 mId(id),
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080037 mHal2Device(NULL)
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070038{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -070039 ATRACE_CALL();
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -070040 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070041}
42
43Camera2Device::~Camera2Device()
44{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -070045 ATRACE_CALL();
Igor Murashkin8dcdb952012-10-02 16:05:11 -070046 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070047 disconnect();
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070048}
49
Igor Murashkin71381052013-03-04 14:53:08 -080050int Camera2Device::getId() const {
51 return mId;
52}
53
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070054status_t Camera2Device::initialize(camera_module_t *module)
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070055{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -070056 ATRACE_CALL();
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -070057 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058 if (mHal2Device != NULL) {
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070059 ALOGE("%s: Already initialized!", __FUNCTION__);
60 return INVALID_OPERATION;
61 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070062
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070063 status_t res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070064 char name[10];
65 snprintf(name, sizeof(name), "%d", mId);
66
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070067 camera2_device_t *device;
68
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070069 res = module->common.methods->open(&module->common, name,
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070070 reinterpret_cast<hw_device_t**>(&device));
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070071
72 if (res != OK) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070073 ALOGE("%s: Could not open camera %d: %s (%d)", __FUNCTION__,
74 mId, strerror(-res), res);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070075 return res;
76 }
77
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070078 if (device->common.version != CAMERA_DEVICE_API_VERSION_2_0) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070079 ALOGE("%s: Could not open camera %d: "
80 "Camera device is not version %x, reports %x instead",
81 __FUNCTION__, mId, CAMERA_DEVICE_API_VERSION_2_0,
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070082 device->common.version);
83 device->common.close(&device->common);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070084 return BAD_VALUE;
85 }
86
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070087 camera_info info;
88 res = module->get_camera_info(mId, &info);
89 if (res != OK ) return res;
90
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070091 if (info.device_version != device->common.version) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070092 ALOGE("%s: HAL reporting mismatched camera_info version (%x)"
93 " and device version (%x).", __FUNCTION__,
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070094 device->common.version, info.device_version);
95 device->common.close(&device->common);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070096 return BAD_VALUE;
97 }
98
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070099 res = mRequestQueue.setConsumerDevice(device);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700100 if (res != OK) {
101 ALOGE("%s: Camera %d: Unable to connect request queue to device: %s (%d)",
102 __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700103 device->common.close(&device->common);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700104 return res;
105 }
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700106 res = mFrameQueue.setProducerDevice(device);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700107 if (res != OK) {
108 ALOGE("%s: Camera %d: Unable to connect frame queue to device: %s (%d)",
109 __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700110 device->common.close(&device->common);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700111 return res;
112 }
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700113
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700114 res = device->ops->get_metadata_vendor_tag_ops(device, &mVendorTagOps);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700115 if (res != OK ) {
116 ALOGE("%s: Camera %d: Unable to retrieve tag ops from device: %s (%d)",
117 __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700118 device->common.close(&device->common);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700119 return res;
120 }
Shuzhen Wang314079e2012-08-31 10:24:22 -0700121 res = set_camera_metadata_vendor_tag_ops(mVendorTagOps);
122 if (res != OK) {
123 ALOGE("%s: Camera %d: Unable to set tag ops: %s (%d)",
124 __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700125 device->common.close(&device->common);
Shuzhen Wang314079e2012-08-31 10:24:22 -0700126 return res;
127 }
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700128 res = device->ops->set_notify_callback(device, notificationCallback,
129 NULL);
130 if (res != OK) {
131 ALOGE("%s: Camera %d: Unable to initialize notification callback!",
132 __FUNCTION__, mId);
133 device->common.close(&device->common);
134 return res;
135 }
136
137 mDeviceInfo = info.static_camera_characteristics;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800138 mHal2Device = device;
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700139
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700140 return OK;
141}
142
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700143status_t Camera2Device::disconnect() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700144 ATRACE_CALL();
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700145 status_t res = OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800146 if (mHal2Device) {
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700147 ALOGV("%s: Closing device for camera %d", __FUNCTION__, mId);
148
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800149 int inProgressCount = mHal2Device->ops->get_in_progress_count(mHal2Device);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700150 if (inProgressCount > 0) {
151 ALOGW("%s: Closing camera device %d with %d requests in flight!",
152 __FUNCTION__, mId, inProgressCount);
153 }
Eino-Ville Talvalac62bb782012-09-24 13:44:07 -0700154 mReprocessStreams.clear();
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700155 mStreams.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800156 res = mHal2Device->common.close(&mHal2Device->common);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700157 if (res != OK) {
158 ALOGE("%s: Could not close camera %d: %s (%d)",
159 __FUNCTION__,
160 mId, strerror(-res), res);
161 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800162 mHal2Device = NULL;
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700163 ALOGV("%s: Shutdown complete", __FUNCTION__);
164 }
165 return res;
166}
167
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700168status_t Camera2Device::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700169 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700170 String8 result;
Eino-Ville Talvala97197152012-08-06 14:25:19 -0700171 int detailLevel = 0;
172 int n = args.size();
173 String16 detailOption("-d");
174 for (int i = 0; i + 1 < n; i++) {
175 if (args[i] == detailOption) {
176 String8 levelStr(args[i+1]);
177 detailLevel = atoi(levelStr.string());
178 }
179 }
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700180
Eino-Ville Talvala603b12e2012-08-08 09:25:58 -0700181 result.appendFormat(" Camera2Device[%d] dump (detail level %d):\n",
182 mId, detailLevel);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700183
Eino-Ville Talvala97197152012-08-06 14:25:19 -0700184 if (detailLevel > 0) {
185 result = " Request queue contents:\n";
186 write(fd, result.string(), result.size());
187 mRequestQueue.dump(fd, args);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700188
Eino-Ville Talvala97197152012-08-06 14:25:19 -0700189 result = " Frame queue contents:\n";
190 write(fd, result.string(), result.size());
191 mFrameQueue.dump(fd, args);
192 }
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700193
194 result = " Active streams:\n";
195 write(fd, result.string(), result.size());
196 for (StreamList::iterator s = mStreams.begin(); s != mStreams.end(); s++) {
197 (*s)->dump(fd, args);
198 }
199
200 result = " HAL device dump:\n";
201 write(fd, result.string(), result.size());
202
203 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800204 res = mHal2Device->ops->dump(mHal2Device, fd);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700205
206 return res;
207}
208
Igor Murashkinbd02dd12013-02-13 15:53:56 -0800209const CameraMetadata& Camera2Device::info() const {
Eino-Ville Talvala9e4c3db2012-07-20 11:07:52 -0700210 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700211
212 return mDeviceInfo;
213}
214
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700215status_t Camera2Device::capture(CameraMetadata &request) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700216 ATRACE_CALL();
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700217 ALOGV("%s: E", __FUNCTION__);
218
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700219 mRequestQueue.enqueue(request.release());
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700220 return OK;
221}
222
223
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700224status_t Camera2Device::setStreamingRequest(const CameraMetadata &request) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700225 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700226 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700227 CameraMetadata streamRequest(request);
228 return mRequestQueue.setStreamSlot(streamRequest.release());
229}
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700230
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700231status_t Camera2Device::clearStreamingRequest() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700232 ATRACE_CALL();
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700233 return mRequestQueue.setStreamSlot(NULL);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700234}
235
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700236status_t Camera2Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
237 ATRACE_CALL();
238 return mRequestQueue.waitForDequeue(requestId, timeout);
239}
240
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700241status_t Camera2Device::createStream(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700242 uint32_t width, uint32_t height, int format, size_t size, int *id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700243 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700244 status_t res;
245 ALOGV("%s: E", __FUNCTION__);
246
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800247 sp<StreamAdapter> stream = new StreamAdapter(mHal2Device);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700248
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700249 res = stream->connectToDevice(consumer, width, height, format, size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700250 if (res != OK) {
251 ALOGE("%s: Camera %d: Unable to create stream (%d x %d, format %x):"
252 "%s (%d)",
253 __FUNCTION__, mId, width, height, format, strerror(-res), res);
254 return res;
255 }
256
257 *id = stream->getId();
258
259 mStreams.push_back(stream);
260 return OK;
261}
262
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700263status_t Camera2Device::createReprocessStreamFromStream(int outputId, int *id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700264 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700265 status_t res;
266 ALOGV("%s: E", __FUNCTION__);
267
268 bool found = false;
269 StreamList::iterator streamI;
270 for (streamI = mStreams.begin();
271 streamI != mStreams.end(); streamI++) {
272 if ((*streamI)->getId() == outputId) {
273 found = true;
274 break;
275 }
276 }
277 if (!found) {
278 ALOGE("%s: Camera %d: Output stream %d doesn't exist; can't create "
279 "reprocess stream from it!", __FUNCTION__, mId, outputId);
280 return BAD_VALUE;
281 }
282
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800283 sp<ReprocessStreamAdapter> stream = new ReprocessStreamAdapter(mHal2Device);
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700284
285 res = stream->connectToDevice((*streamI));
286 if (res != OK) {
287 ALOGE("%s: Camera %d: Unable to create reprocessing stream from "\
288 "stream %d: %s (%d)", __FUNCTION__, mId, outputId,
289 strerror(-res), res);
290 return res;
291 }
292
293 *id = stream->getId();
294
295 mReprocessStreams.push_back(stream);
296 return OK;
297}
298
299
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700300status_t Camera2Device::getStreamInfo(int id,
301 uint32_t *width, uint32_t *height, uint32_t *format) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700302 ATRACE_CALL();
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700303 ALOGV("%s: E", __FUNCTION__);
304 bool found = false;
305 StreamList::iterator streamI;
306 for (streamI = mStreams.begin();
307 streamI != mStreams.end(); streamI++) {
308 if ((*streamI)->getId() == id) {
309 found = true;
310 break;
311 }
312 }
313 if (!found) {
314 ALOGE("%s: Camera %d: Stream %d does not exist",
315 __FUNCTION__, mId, id);
316 return BAD_VALUE;
317 }
318
319 if (width) *width = (*streamI)->getWidth();
320 if (height) *height = (*streamI)->getHeight();
321 if (format) *format = (*streamI)->getFormat();
322
323 return OK;
324}
325
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700326status_t Camera2Device::setStreamTransform(int id,
327 int transform) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700328 ATRACE_CALL();
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700329 ALOGV("%s: E", __FUNCTION__);
330 bool found = false;
331 StreamList::iterator streamI;
332 for (streamI = mStreams.begin();
333 streamI != mStreams.end(); streamI++) {
334 if ((*streamI)->getId() == id) {
335 found = true;
336 break;
337 }
338 }
339 if (!found) {
340 ALOGE("%s: Camera %d: Stream %d does not exist",
341 __FUNCTION__, mId, id);
342 return BAD_VALUE;
343 }
344
345 return (*streamI)->setTransform(transform);
346}
347
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700348status_t Camera2Device::deleteStream(int id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700349 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700350 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700351 bool found = false;
352 for (StreamList::iterator streamI = mStreams.begin();
353 streamI != mStreams.end(); streamI++) {
354 if ((*streamI)->getId() == id) {
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700355 status_t res = (*streamI)->release();
Eino-Ville Talvala4ecfec32012-06-12 17:13:48 -0700356 if (res != OK) {
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700357 ALOGE("%s: Unable to release stream %d from HAL device: "
Eino-Ville Talvala4ecfec32012-06-12 17:13:48 -0700358 "%s (%d)", __FUNCTION__, id, strerror(-res), res);
359 return res;
360 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700361 mStreams.erase(streamI);
362 found = true;
363 break;
364 }
365 }
366 if (!found) {
367 ALOGE("%s: Camera %d: Unable to find stream %d to delete",
368 __FUNCTION__, mId, id);
369 return BAD_VALUE;
370 }
371 return OK;
372}
373
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700374status_t Camera2Device::deleteReprocessStream(int id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700375 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700376 ALOGV("%s: E", __FUNCTION__);
377 bool found = false;
378 for (ReprocessStreamList::iterator streamI = mReprocessStreams.begin();
379 streamI != mReprocessStreams.end(); streamI++) {
380 if ((*streamI)->getId() == id) {
381 status_t res = (*streamI)->release();
382 if (res != OK) {
383 ALOGE("%s: Unable to release reprocess stream %d from "
384 "HAL device: %s (%d)", __FUNCTION__, id,
385 strerror(-res), res);
386 return res;
387 }
388 mReprocessStreams.erase(streamI);
389 found = true;
390 break;
391 }
392 }
393 if (!found) {
394 ALOGE("%s: Camera %d: Unable to find stream %d to delete",
395 __FUNCTION__, mId, id);
396 return BAD_VALUE;
397 }
398 return OK;
399}
400
401
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700402status_t Camera2Device::createDefaultRequest(int templateId,
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700403 CameraMetadata *request) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700404 ATRACE_CALL();
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700405 status_t err;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700406 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700407 camera_metadata_t *rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800408 err = mHal2Device->ops->construct_default_request(
409 mHal2Device, templateId, &rawRequest);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700410 request->acquire(rawRequest);
411 return err;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700412}
413
414status_t Camera2Device::waitUntilDrained() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700415 ATRACE_CALL();
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700416 static const uint32_t kSleepTime = 50000; // 50 ms
417 static const uint32_t kMaxSleepTime = 10000000; // 10 s
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700418 ALOGV("%s: Camera %d: Starting wait", __FUNCTION__, mId);
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700419 if (mRequestQueue.getBufferCount() ==
420 CAMERA2_REQUEST_QUEUE_IS_BOTTOMLESS) return INVALID_OPERATION;
421
422 // TODO: Set up notifications from HAL, instead of sleeping here
423 uint32_t totalTime = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800424 while (mHal2Device->ops->get_in_progress_count(mHal2Device) > 0) {
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700425 usleep(kSleepTime);
426 totalTime += kSleepTime;
427 if (totalTime > kMaxSleepTime) {
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700428 ALOGE("%s: Waited %d us, %d requests still in flight", __FUNCTION__,
Alex Rayf0eeb532013-03-17 03:23:18 -0700429 totalTime, mHal2Device->ops->get_in_progress_count(mHal2Device));
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700430 return TIMED_OUT;
431 }
432 }
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700433 ALOGV("%s: Camera %d: HAL is idle", __FUNCTION__, mId);
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700434 return OK;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700435}
436
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700437status_t Camera2Device::setNotifyCallback(NotificationListener *listener) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700438 ATRACE_CALL();
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700439 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800440 res = mHal2Device->ops->set_notify_callback(mHal2Device, notificationCallback,
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700441 reinterpret_cast<void*>(listener) );
442 if (res != OK) {
443 ALOGE("%s: Unable to set notification callback!", __FUNCTION__);
444 }
445 return res;
446}
447
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -0700448bool Camera2Device::willNotify3A() {
449 return true;
450}
451
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700452void Camera2Device::notificationCallback(int32_t msg_type,
453 int32_t ext1,
454 int32_t ext2,
455 int32_t ext3,
456 void *user) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700457 ATRACE_CALL();
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700458 NotificationListener *listener = reinterpret_cast<NotificationListener*>(user);
459 ALOGV("%s: Notification %d, arguments %d, %d, %d", __FUNCTION__, msg_type,
460 ext1, ext2, ext3);
461 if (listener != NULL) {
462 switch (msg_type) {
463 case CAMERA2_MSG_ERROR:
464 listener->notifyError(ext1, ext2, ext3);
465 break;
466 case CAMERA2_MSG_SHUTTER: {
467 nsecs_t timestamp = (nsecs_t)ext2 | ((nsecs_t)(ext3) << 32 );
468 listener->notifyShutter(ext1, timestamp);
469 break;
470 }
471 case CAMERA2_MSG_AUTOFOCUS:
472 listener->notifyAutoFocus(ext1, ext2);
473 break;
474 case CAMERA2_MSG_AUTOEXPOSURE:
475 listener->notifyAutoExposure(ext1, ext2);
476 break;
477 case CAMERA2_MSG_AUTOWB:
478 listener->notifyAutoWhitebalance(ext1, ext2);
479 break;
480 default:
481 ALOGE("%s: Unknown notification %d (arguments %d, %d, %d)!",
482 __FUNCTION__, msg_type, ext1, ext2, ext3);
483 }
484 }
485}
486
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -0700487status_t Camera2Device::waitForNextFrame(nsecs_t timeout) {
488 return mFrameQueue.waitForBuffer(timeout);
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700489}
490
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700491status_t Camera2Device::getNextFrame(CameraMetadata *frame) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700492 ATRACE_CALL();
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700493 status_t res;
494 camera_metadata_t *rawFrame;
495 res = mFrameQueue.dequeue(&rawFrame);
496 if (rawFrame == NULL) {
497 return NOT_ENOUGH_DATA;
498 } else if (res == OK) {
499 frame->acquire(rawFrame);
500 }
501 return res;
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700502}
503
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700504status_t Camera2Device::triggerAutofocus(uint32_t id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700505 ATRACE_CALL();
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700506 status_t res;
507 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800508 res = mHal2Device->ops->trigger_action(mHal2Device,
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700509 CAMERA2_TRIGGER_AUTOFOCUS, id, 0);
510 if (res != OK) {
511 ALOGE("%s: Error triggering autofocus (id %d)",
512 __FUNCTION__, id);
513 }
514 return res;
515}
516
517status_t Camera2Device::triggerCancelAutofocus(uint32_t id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700518 ATRACE_CALL();
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700519 status_t res;
520 ALOGV("%s: Canceling autofocus, id %d", __FUNCTION__, id);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800521 res = mHal2Device->ops->trigger_action(mHal2Device,
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700522 CAMERA2_TRIGGER_CANCEL_AUTOFOCUS, id, 0);
523 if (res != OK) {
524 ALOGE("%s: Error canceling autofocus (id %d)",
525 __FUNCTION__, id);
526 }
527 return res;
528}
529
530status_t Camera2Device::triggerPrecaptureMetering(uint32_t id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700531 ATRACE_CALL();
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700532 status_t res;
533 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800534 res = mHal2Device->ops->trigger_action(mHal2Device,
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700535 CAMERA2_TRIGGER_PRECAPTURE_METERING, id, 0);
536 if (res != OK) {
537 ALOGE("%s: Error triggering precapture metering (id %d)",
538 __FUNCTION__, id);
539 }
540 return res;
541}
542
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700543status_t Camera2Device::pushReprocessBuffer(int reprocessStreamId,
544 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700545 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700546 ALOGV("%s: E", __FUNCTION__);
547 bool found = false;
548 status_t res = OK;
549 for (ReprocessStreamList::iterator streamI = mReprocessStreams.begin();
550 streamI != mReprocessStreams.end(); streamI++) {
551 if ((*streamI)->getId() == reprocessStreamId) {
552 res = (*streamI)->pushIntoStream(buffer, listener);
553 if (res != OK) {
554 ALOGE("%s: Unable to push buffer to reprocess stream %d: %s (%d)",
555 __FUNCTION__, reprocessStreamId, strerror(-res), res);
556 return res;
557 }
558 found = true;
559 break;
560 }
561 }
562 if (!found) {
563 ALOGE("%s: Camera %d: Unable to find reprocess stream %d",
564 __FUNCTION__, mId, reprocessStreamId);
565 res = BAD_VALUE;
566 }
567 return res;
568}
569
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700570/**
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700571 * Camera2Device::MetadataQueue
572 */
573
574Camera2Device::MetadataQueue::MetadataQueue():
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800575 mHal2Device(NULL),
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700576 mFrameCount(0),
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700577 mLatestRequestId(0),
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700578 mCount(0),
579 mStreamSlotCount(0),
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -0700580 mSignalConsumer(true)
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700581{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700582 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700583 camera2_request_queue_src_ops::dequeue_request = consumer_dequeue;
584 camera2_request_queue_src_ops::request_count = consumer_buffer_count;
585 camera2_request_queue_src_ops::free_request = consumer_free;
586
587 camera2_frame_queue_dst_ops::dequeue_frame = producer_dequeue;
588 camera2_frame_queue_dst_ops::cancel_frame = producer_cancel;
589 camera2_frame_queue_dst_ops::enqueue_frame = producer_enqueue;
590}
591
592Camera2Device::MetadataQueue::~MetadataQueue() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700593 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700594 Mutex::Autolock l(mMutex);
595 freeBuffers(mEntries.begin(), mEntries.end());
596 freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
597}
598
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700599// Connect to camera2 HAL as consumer (input requests/reprocessing)
600status_t Camera2Device::MetadataQueue::setConsumerDevice(camera2_device_t *d) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700601 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700602 status_t res;
603 res = d->ops->set_request_queue_src_ops(d,
604 this);
605 if (res != OK) return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800606 mHal2Device = d;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700607 return OK;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700608}
609
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700610status_t Camera2Device::MetadataQueue::setProducerDevice(camera2_device_t *d) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700611 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700612 status_t res;
613 res = d->ops->set_frame_queue_dst_ops(d,
614 this);
615 return res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700616}
617
618// Real interfaces
619status_t Camera2Device::MetadataQueue::enqueue(camera_metadata_t *buf) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700620 ATRACE_CALL();
Eino-Ville Talvala2c08dc62012-06-15 12:49:21 -0700621 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700622 Mutex::Autolock l(mMutex);
623
624 mCount++;
625 mEntries.push_back(buf);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700626
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700627 return signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700628}
629
630int Camera2Device::MetadataQueue::getBufferCount() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700631 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700632 Mutex::Autolock l(mMutex);
633 if (mStreamSlotCount > 0) {
634 return CAMERA2_REQUEST_QUEUE_IS_BOTTOMLESS;
635 }
636 return mCount;
637}
638
639status_t Camera2Device::MetadataQueue::dequeue(camera_metadata_t **buf,
640 bool incrementCount)
641{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700642 ATRACE_CALL();
Eino-Ville Talvala2c08dc62012-06-15 12:49:21 -0700643 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700644 status_t res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700645 Mutex::Autolock l(mMutex);
646
647 if (mCount == 0) {
648 if (mStreamSlotCount == 0) {
Eino-Ville Talvala2c08dc62012-06-15 12:49:21 -0700649 ALOGVV("%s: Empty", __FUNCTION__);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700650 *buf = NULL;
651 mSignalConsumer = true;
652 return OK;
653 }
Eino-Ville Talvala2c08dc62012-06-15 12:49:21 -0700654 ALOGVV("%s: Streaming %d frames to queue", __FUNCTION__,
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700655 mStreamSlotCount);
656
657 for (List<camera_metadata_t*>::iterator slotEntry = mStreamSlot.begin();
658 slotEntry != mStreamSlot.end();
659 slotEntry++ ) {
660 size_t entries = get_camera_metadata_entry_count(*slotEntry);
661 size_t dataBytes = get_camera_metadata_data_count(*slotEntry);
662
663 camera_metadata_t *copy =
664 allocate_camera_metadata(entries, dataBytes);
665 append_camera_metadata(copy, *slotEntry);
666 mEntries.push_back(copy);
667 }
668 mCount = mStreamSlotCount;
669 }
Eino-Ville Talvala2c08dc62012-06-15 12:49:21 -0700670 ALOGVV("MetadataQueue: deque (%d buffers)", mCount);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700671 camera_metadata_t *b = *(mEntries.begin());
672 mEntries.erase(mEntries.begin());
673
674 if (incrementCount) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700675 ATRACE_INT("cam2_request", mFrameCount);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700676 camera_metadata_entry_t frameCount;
677 res = find_camera_metadata_entry(b,
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700678 ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700679 &frameCount);
680 if (res != OK) {
681 ALOGE("%s: Unable to add frame count: %s (%d)",
682 __FUNCTION__, strerror(-res), res);
683 } else {
684 *frameCount.data.i32 = mFrameCount;
685 }
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700686 mFrameCount++;
687 }
688
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700689 // Check for request ID, and if present, signal waiters.
690 camera_metadata_entry_t requestId;
691 res = find_camera_metadata_entry(b,
692 ANDROID_REQUEST_ID,
693 &requestId);
694 if (res == OK) {
695 mLatestRequestId = requestId.data.i32[0];
696 mNewRequestId.signal();
697 }
698
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700699 *buf = b;
700 mCount--;
701
702 return OK;
703}
704
705status_t Camera2Device::MetadataQueue::waitForBuffer(nsecs_t timeout)
706{
707 Mutex::Autolock l(mMutex);
708 status_t res;
709 while (mCount == 0) {
710 res = notEmpty.waitRelative(mMutex,timeout);
711 if (res != OK) return res;
712 }
713 return OK;
714}
715
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700716status_t Camera2Device::MetadataQueue::waitForDequeue(int32_t id,
717 nsecs_t timeout) {
718 Mutex::Autolock l(mMutex);
719 status_t res;
720 while (mLatestRequestId != id) {
721 nsecs_t startTime = systemTime();
722
723 res = mNewRequestId.waitRelative(mMutex, timeout);
724 if (res != OK) return res;
725
726 timeout -= (systemTime() - startTime);
727 }
728
729 return OK;
730}
731
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700732status_t Camera2Device::MetadataQueue::setStreamSlot(camera_metadata_t *buf)
733{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700734 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700735 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700736 Mutex::Autolock l(mMutex);
737 if (buf == NULL) {
738 freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
739 mStreamSlotCount = 0;
740 return OK;
741 }
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700742 camera_metadata_t *buf2 = clone_camera_metadata(buf);
743 if (!buf2) {
744 ALOGE("%s: Unable to clone metadata buffer!", __FUNCTION__);
745 return NO_MEMORY;
746 }
747
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700748 if (mStreamSlotCount > 1) {
749 List<camera_metadata_t*>::iterator deleter = ++mStreamSlot.begin();
750 freeBuffers(++mStreamSlot.begin(), mStreamSlot.end());
751 mStreamSlotCount = 1;
752 }
753 if (mStreamSlotCount == 1) {
754 free_camera_metadata( *(mStreamSlot.begin()) );
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700755 *(mStreamSlot.begin()) = buf2;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700756 } else {
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700757 mStreamSlot.push_front(buf2);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700758 mStreamSlotCount = 1;
759 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700760 return signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700761}
762
763status_t Camera2Device::MetadataQueue::setStreamSlot(
764 const List<camera_metadata_t*> &bufs)
765{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700766 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700767 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700768 Mutex::Autolock l(mMutex);
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700769
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700770 if (mStreamSlotCount > 0) {
771 freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
772 }
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700773 mStreamSlotCount = 0;
774 for (List<camera_metadata_t*>::const_iterator r = bufs.begin();
775 r != bufs.end(); r++) {
776 camera_metadata_t *r2 = clone_camera_metadata(*r);
777 if (!r2) {
778 ALOGE("%s: Unable to clone metadata buffer!", __FUNCTION__);
779 return NO_MEMORY;
780 }
781 mStreamSlot.push_back(r2);
782 mStreamSlotCount++;
783 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700784 return signalConsumerLocked();
785}
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700786
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700787status_t Camera2Device::MetadataQueue::dump(int fd,
Igor Murashkinebe3f692012-10-12 16:56:11 -0700788 const Vector<String16>& /*args*/) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700789 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700790 String8 result;
791 status_t notLocked;
792 notLocked = mMutex.tryLock();
793 if (notLocked) {
794 result.append(" (Unable to lock queue mutex)\n");
795 }
796 result.appendFormat(" Current frame number: %d\n", mFrameCount);
797 if (mStreamSlotCount == 0) {
798 result.append(" Stream slot: Empty\n");
799 write(fd, result.string(), result.size());
800 } else {
801 result.appendFormat(" Stream slot: %d entries\n",
802 mStreamSlot.size());
803 int i = 0;
804 for (List<camera_metadata_t*>::iterator r = mStreamSlot.begin();
805 r != mStreamSlot.end(); r++) {
806 result = String8::format(" Stream slot buffer %d:\n", i);
807 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -0700808 dump_indented_camera_metadata(*r, fd, 2, 10);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700809 i++;
810 }
811 }
812 if (mEntries.size() == 0) {
813 result = " Main queue is empty\n";
814 write(fd, result.string(), result.size());
815 } else {
816 result = String8::format(" Main queue has %d entries:\n",
817 mEntries.size());
818 int i = 0;
819 for (List<camera_metadata_t*>::iterator r = mEntries.begin();
820 r != mEntries.end(); r++) {
821 result = String8::format(" Queue entry %d:\n", i);
822 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -0700823 dump_indented_camera_metadata(*r, fd, 2, 10);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700824 i++;
825 }
826 }
827
828 if (notLocked == 0) {
829 mMutex.unlock();
830 }
831
832 return OK;
833}
834
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700835status_t Camera2Device::MetadataQueue::signalConsumerLocked() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700836 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700837 status_t res = OK;
838 notEmpty.signal();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800839 if (mSignalConsumer && mHal2Device != NULL) {
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700840 mSignalConsumer = false;
841
842 mMutex.unlock();
843 ALOGV("%s: Signaling consumer", __FUNCTION__);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800844 res = mHal2Device->ops->notify_request_queue_not_empty(mHal2Device);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700845 mMutex.lock();
846 }
847 return res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700848}
849
850status_t Camera2Device::MetadataQueue::freeBuffers(
851 List<camera_metadata_t*>::iterator start,
852 List<camera_metadata_t*>::iterator end)
853{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700854 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700855 while (start != end) {
856 free_camera_metadata(*start);
857 start = mStreamSlot.erase(start);
858 }
859 return OK;
860}
861
862Camera2Device::MetadataQueue* Camera2Device::MetadataQueue::getInstance(
863 const camera2_request_queue_src_ops_t *q)
864{
865 const MetadataQueue* cmq = static_cast<const MetadataQueue*>(q);
866 return const_cast<MetadataQueue*>(cmq);
867}
868
869Camera2Device::MetadataQueue* Camera2Device::MetadataQueue::getInstance(
870 const camera2_frame_queue_dst_ops_t *q)
871{
872 const MetadataQueue* cmq = static_cast<const MetadataQueue*>(q);
873 return const_cast<MetadataQueue*>(cmq);
874}
875
876int Camera2Device::MetadataQueue::consumer_buffer_count(
877 const camera2_request_queue_src_ops_t *q)
878{
879 MetadataQueue *queue = getInstance(q);
880 return queue->getBufferCount();
881}
882
883int Camera2Device::MetadataQueue::consumer_dequeue(
884 const camera2_request_queue_src_ops_t *q,
885 camera_metadata_t **buffer)
886{
887 MetadataQueue *queue = getInstance(q);
888 return queue->dequeue(buffer, true);
889}
890
891int Camera2Device::MetadataQueue::consumer_free(
892 const camera2_request_queue_src_ops_t *q,
893 camera_metadata_t *old_buffer)
894{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700895 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700896 MetadataQueue *queue = getInstance(q);
Igor Murashkinebe3f692012-10-12 16:56:11 -0700897 (void)queue;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700898 free_camera_metadata(old_buffer);
899 return OK;
900}
901
902int Camera2Device::MetadataQueue::producer_dequeue(
Igor Murashkinebe3f692012-10-12 16:56:11 -0700903 const camera2_frame_queue_dst_ops_t * /*q*/,
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700904 size_t entries, size_t bytes,
905 camera_metadata_t **buffer)
906{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700907 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700908 camera_metadata_t *new_buffer =
909 allocate_camera_metadata(entries, bytes);
910 if (new_buffer == NULL) return NO_MEMORY;
911 *buffer = new_buffer;
912 return OK;
913}
914
915int Camera2Device::MetadataQueue::producer_cancel(
Igor Murashkinebe3f692012-10-12 16:56:11 -0700916 const camera2_frame_queue_dst_ops_t * /*q*/,
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700917 camera_metadata_t *old_buffer)
918{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700919 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700920 free_camera_metadata(old_buffer);
921 return OK;
922}
923
924int Camera2Device::MetadataQueue::producer_enqueue(
925 const camera2_frame_queue_dst_ops_t *q,
926 camera_metadata_t *filled_buffer)
927{
928 MetadataQueue *queue = getInstance(q);
929 return queue->enqueue(filled_buffer);
930}
931
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700932/**
933 * Camera2Device::StreamAdapter
934 */
935
936#ifndef container_of
937#define container_of(ptr, type, member) \
938 (type *)((char*)(ptr) - offsetof(type, member))
939#endif
940
941Camera2Device::StreamAdapter::StreamAdapter(camera2_device_t *d):
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700942 mState(RELEASED),
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800943 mHal2Device(d),
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700944 mId(-1),
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700945 mWidth(0), mHeight(0), mFormat(0), mSize(0), mUsage(0),
946 mMaxProducerBuffers(0), mMaxConsumerBuffers(0),
947 mTotalBuffers(0),
948 mFormatRequested(0),
949 mActiveBuffers(0),
950 mFrameCount(0),
951 mLastTimestamp(0)
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700952{
953 camera2_stream_ops::dequeue_buffer = dequeue_buffer;
954 camera2_stream_ops::enqueue_buffer = enqueue_buffer;
955 camera2_stream_ops::cancel_buffer = cancel_buffer;
956 camera2_stream_ops::set_crop = set_crop;
957}
958
959Camera2Device::StreamAdapter::~StreamAdapter() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700960 ATRACE_CALL();
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700961 if (mState != RELEASED) {
962 release();
963 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700964}
965
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700966status_t Camera2Device::StreamAdapter::connectToDevice(
967 sp<ANativeWindow> consumer,
968 uint32_t width, uint32_t height, int format, size_t size) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700969 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700970 status_t res;
Eino-Ville Talvala9e4c3db2012-07-20 11:07:52 -0700971 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700972
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700973 if (mState != RELEASED) return INVALID_OPERATION;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700974 if (consumer == NULL) {
975 ALOGE("%s: Null consumer passed to stream adapter", __FUNCTION__);
976 return BAD_VALUE;
977 }
978
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700979 ALOGV("%s: New stream parameters %d x %d, format 0x%x, size %d",
980 __FUNCTION__, width, height, format, size);
981
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700982 mConsumerInterface = consumer;
983 mWidth = width;
984 mHeight = height;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700985 mSize = (format == HAL_PIXEL_FORMAT_BLOB) ? size : 0;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700986 mFormatRequested = format;
987
988 // Allocate device-side stream interface
989
990 uint32_t id;
991 uint32_t formatActual;
992 uint32_t usage;
993 uint32_t maxBuffers = 2;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800994 res = mHal2Device->ops->allocate_stream(mHal2Device,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700995 mWidth, mHeight, mFormatRequested, getStreamOps(),
996 &id, &formatActual, &usage, &maxBuffers);
997 if (res != OK) {
998 ALOGE("%s: Device stream allocation failed: %s (%d)",
999 __FUNCTION__, strerror(-res), res);
1000 return res;
1001 }
1002
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001003 ALOGV("%s: Allocated stream id %d, actual format 0x%x, "
1004 "usage 0x%x, producer wants %d buffers", __FUNCTION__,
1005 id, formatActual, usage, maxBuffers);
1006
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001007 mId = id;
1008 mFormat = formatActual;
1009 mUsage = usage;
1010 mMaxProducerBuffers = maxBuffers;
1011
1012 mState = ALLOCATED;
1013
1014 // Configure consumer-side ANativeWindow interface
1015 res = native_window_api_connect(mConsumerInterface.get(),
1016 NATIVE_WINDOW_API_CAMERA);
1017 if (res != OK) {
1018 ALOGE("%s: Unable to connect to native window for stream %d",
1019 __FUNCTION__, mId);
1020
1021 return res;
1022 }
1023
1024 mState = CONNECTED;
1025
1026 res = native_window_set_usage(mConsumerInterface.get(), mUsage);
1027 if (res != OK) {
1028 ALOGE("%s: Unable to configure usage %08x for stream %d",
1029 __FUNCTION__, mUsage, mId);
1030 return res;
1031 }
1032
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001033 res = native_window_set_scaling_mode(mConsumerInterface.get(),
1034 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1035 if (res != OK) {
1036 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
1037 __FUNCTION__, strerror(-res), res);
1038 return res;
1039 }
1040
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -07001041 res = setTransform(0);
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001042 if (res != OK) {
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001043 return res;
1044 }
1045
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -07001046 if (mFormat == HAL_PIXEL_FORMAT_BLOB) {
1047 res = native_window_set_buffers_geometry(mConsumerInterface.get(),
1048 mSize, 1, mFormat);
1049 if (res != OK) {
1050 ALOGE("%s: Unable to configure compressed stream buffer geometry"
1051 " %d x %d, size %d for stream %d",
1052 __FUNCTION__, mWidth, mHeight, mSize, mId);
1053 return res;
1054 }
1055 } else {
1056 res = native_window_set_buffers_geometry(mConsumerInterface.get(),
1057 mWidth, mHeight, mFormat);
1058 if (res != OK) {
1059 ALOGE("%s: Unable to configure stream buffer geometry"
1060 " %d x %d, format 0x%x for stream %d",
1061 __FUNCTION__, mWidth, mHeight, mFormat, mId);
1062 return res;
1063 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001064 }
1065
1066 int maxConsumerBuffers;
1067 res = mConsumerInterface->query(mConsumerInterface.get(),
1068 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
1069 if (res != OK) {
1070 ALOGE("%s: Unable to query consumer undequeued"
1071 " buffer count for stream %d", __FUNCTION__, mId);
1072 return res;
1073 }
1074 mMaxConsumerBuffers = maxConsumerBuffers;
1075
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001076 ALOGV("%s: Consumer wants %d buffers", __FUNCTION__,
1077 mMaxConsumerBuffers);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001078
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001079 mTotalBuffers = mMaxConsumerBuffers + mMaxProducerBuffers;
1080 mActiveBuffers = 0;
1081 mFrameCount = 0;
1082 mLastTimestamp = 0;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001083
1084 res = native_window_set_buffer_count(mConsumerInterface.get(),
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001085 mTotalBuffers);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001086 if (res != OK) {
1087 ALOGE("%s: Unable to set buffer count for stream %d",
1088 __FUNCTION__, mId);
1089 return res;
1090 }
1091
1092 // Register allocated buffers with HAL device
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001093 buffer_handle_t *buffers = new buffer_handle_t[mTotalBuffers];
1094 ANativeWindowBuffer **anwBuffers = new ANativeWindowBuffer*[mTotalBuffers];
1095 uint32_t bufferIdx = 0;
1096 for (; bufferIdx < mTotalBuffers; bufferIdx++) {
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001097 res = native_window_dequeue_buffer_and_wait(mConsumerInterface.get(),
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001098 &anwBuffers[bufferIdx]);
1099 if (res != OK) {
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001100 ALOGE("%s: Unable to dequeue buffer %d for initial registration for "
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001101 "stream %d", __FUNCTION__, bufferIdx, mId);
1102 goto cleanUpBuffers;
1103 }
1104
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001105 buffers[bufferIdx] = anwBuffers[bufferIdx]->handle;
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001106 ALOGV("%s: Buffer %p allocated", __FUNCTION__, (void*)buffers[bufferIdx]);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001107 }
1108
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001109 ALOGV("%s: Registering %d buffers with camera HAL", __FUNCTION__, mTotalBuffers);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001110 res = mHal2Device->ops->register_stream_buffers(mHal2Device,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001111 mId,
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001112 mTotalBuffers,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001113 buffers);
1114 if (res != OK) {
1115 ALOGE("%s: Unable to register buffers with HAL device for stream %d",
1116 __FUNCTION__, mId);
1117 } else {
1118 mState = ACTIVE;
1119 }
1120
1121cleanUpBuffers:
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001122 ALOGV("%s: Cleaning up %d buffers", __FUNCTION__, bufferIdx);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001123 for (uint32_t i = 0; i < bufferIdx; i++) {
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001124 res = mConsumerInterface->cancelBuffer(mConsumerInterface.get(),
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001125 anwBuffers[i], -1);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001126 if (res != OK) {
1127 ALOGE("%s: Unable to cancel buffer %d after registration",
1128 __FUNCTION__, i);
1129 }
1130 }
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001131 delete[] anwBuffers;
1132 delete[] buffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001133
1134 return res;
1135}
1136
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001137status_t Camera2Device::StreamAdapter::release() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001138 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001139 status_t res;
Eino-Ville Talvala02f84572013-04-23 15:16:57 -07001140 ALOGV("%s: Releasing stream %d (%d x %d, format %d)", __FUNCTION__, mId,
1141 mWidth, mHeight, mFormat);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001142 if (mState >= ALLOCATED) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001143 res = mHal2Device->ops->release_stream(mHal2Device, mId);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001144 if (res != OK) {
1145 ALOGE("%s: Unable to release stream %d",
1146 __FUNCTION__, mId);
1147 return res;
1148 }
1149 }
1150 if (mState >= CONNECTED) {
1151 res = native_window_api_disconnect(mConsumerInterface.get(),
1152 NATIVE_WINDOW_API_CAMERA);
Igor Murashkina1e5dcc2012-10-02 15:21:31 -07001153
1154 /* this is not an error. if client calling process dies,
1155 the window will also die and all calls to it will return
1156 DEAD_OBJECT, thus it's already "disconnected" */
1157 if (res == DEAD_OBJECT) {
1158 ALOGW("%s: While disconnecting stream %d from native window, the"
1159 " native window died from under us", __FUNCTION__, mId);
1160 }
1161 else if (res != OK) {
1162 ALOGE("%s: Unable to disconnect stream %d from native window (error %d %s)",
1163 __FUNCTION__, mId, res, strerror(-res));
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001164 return res;
1165 }
1166 }
1167 mId = -1;
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001168 mState = RELEASED;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001169 return OK;
1170}
1171
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -07001172status_t Camera2Device::StreamAdapter::setTransform(int transform) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001173 ATRACE_CALL();
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -07001174 status_t res;
1175 if (mState < CONNECTED) {
1176 ALOGE("%s: Cannot set transform on unconnected stream", __FUNCTION__);
1177 return INVALID_OPERATION;
1178 }
1179 res = native_window_set_buffers_transform(mConsumerInterface.get(),
1180 transform);
1181 if (res != OK) {
1182 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
1183 __FUNCTION__, transform, strerror(-res), res);
1184 }
1185 return res;
1186}
1187
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001188status_t Camera2Device::StreamAdapter::dump(int fd,
Igor Murashkinebe3f692012-10-12 16:56:11 -07001189 const Vector<String16>& /*args*/) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001190 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001191 String8 result = String8::format(" Stream %d: %d x %d, format 0x%x\n",
1192 mId, mWidth, mHeight, mFormat);
1193 result.appendFormat(" size %d, usage 0x%x, requested format 0x%x\n",
1194 mSize, mUsage, mFormatRequested);
1195 result.appendFormat(" total buffers: %d, dequeued buffers: %d\n",
1196 mTotalBuffers, mActiveBuffers);
1197 result.appendFormat(" frame count: %d, last timestamp %lld\n",
1198 mFrameCount, mLastTimestamp);
1199 write(fd, result.string(), result.size());
1200 return OK;
1201}
1202
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001203const camera2_stream_ops *Camera2Device::StreamAdapter::getStreamOps() {
1204 return static_cast<camera2_stream_ops *>(this);
1205}
1206
1207ANativeWindow* Camera2Device::StreamAdapter::toANW(
1208 const camera2_stream_ops_t *w) {
1209 return static_cast<const StreamAdapter*>(w)->mConsumerInterface.get();
1210}
1211
1212int Camera2Device::StreamAdapter::dequeue_buffer(const camera2_stream_ops_t *w,
1213 buffer_handle_t** buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001214 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001215 int res;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001216 StreamAdapter* stream =
1217 const_cast<StreamAdapter*>(static_cast<const StreamAdapter*>(w));
1218 if (stream->mState != ACTIVE) {
1219 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, stream->mState);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001220 return INVALID_OPERATION;
1221 }
1222
1223 ANativeWindow *a = toANW(w);
1224 ANativeWindowBuffer* anb;
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001225 res = native_window_dequeue_buffer_and_wait(a, &anb);
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001226 if (res != OK) {
1227 ALOGE("Stream %d dequeue: Error from native_window: %s (%d)", stream->mId,
1228 strerror(-res), res);
1229 return res;
1230 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001231
1232 *buffer = &(anb->handle);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001233 stream->mActiveBuffers++;
1234
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001235 ALOGVV("Stream %d dequeue: Buffer %p dequeued", stream->mId, (void*)(**buffer));
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001236 return res;
1237}
1238
1239int Camera2Device::StreamAdapter::enqueue_buffer(const camera2_stream_ops_t* w,
1240 int64_t timestamp,
1241 buffer_handle_t* buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001242 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001243 StreamAdapter *stream =
1244 const_cast<StreamAdapter*>(static_cast<const StreamAdapter*>(w));
Eino-Ville Talvala228a5382012-08-13 12:16:06 -07001245 stream->mFrameCount++;
1246 ALOGVV("Stream %d enqueue: Frame %d (%p) captured at %lld ns",
James Donga289bf62012-09-05 16:46:36 -07001247 stream->mId, stream->mFrameCount, (void*)(*buffer), timestamp);
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001248 int state = stream->mState;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001249 if (state != ACTIVE) {
1250 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
1251 return INVALID_OPERATION;
1252 }
1253 ANativeWindow *a = toANW(w);
1254 status_t err;
Eino-Ville Talvala228a5382012-08-13 12:16:06 -07001255
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001256 err = native_window_set_buffers_timestamp(a, timestamp);
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001257 if (err != OK) {
1258 ALOGE("%s: Error setting timestamp on native window: %s (%d)",
1259 __FUNCTION__, strerror(-err), err);
1260 return err;
1261 }
1262 err = a->queueBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001263 container_of(buffer, ANativeWindowBuffer, handle), -1);
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001264 if (err != OK) {
1265 ALOGE("%s: Error queueing buffer to native window: %s (%d)",
1266 __FUNCTION__, strerror(-err), err);
James Dong31d377b2012-08-09 17:43:46 -07001267 return err;
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001268 }
James Dong31d377b2012-08-09 17:43:46 -07001269
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001270 stream->mActiveBuffers--;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001271 stream->mLastTimestamp = timestamp;
James Dong31d377b2012-08-09 17:43:46 -07001272 return OK;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001273}
1274
1275int Camera2Device::StreamAdapter::cancel_buffer(const camera2_stream_ops_t* w,
1276 buffer_handle_t* buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001277 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001278 StreamAdapter *stream =
1279 const_cast<StreamAdapter*>(static_cast<const StreamAdapter*>(w));
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001280 ALOGVV("Stream %d cancel: Buffer %p",
1281 stream->mId, (void*)(*buffer));
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001282 if (stream->mState != ACTIVE) {
1283 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, stream->mState);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001284 return INVALID_OPERATION;
1285 }
James Dong31d377b2012-08-09 17:43:46 -07001286
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001287 ANativeWindow *a = toANW(w);
James Dong31d377b2012-08-09 17:43:46 -07001288 int err = a->cancelBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001289 container_of(buffer, ANativeWindowBuffer, handle), -1);
James Dong31d377b2012-08-09 17:43:46 -07001290 if (err != OK) {
1291 ALOGE("%s: Error canceling buffer to native window: %s (%d)",
1292 __FUNCTION__, strerror(-err), err);
1293 return err;
1294 }
1295
1296 stream->mActiveBuffers--;
1297 return OK;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001298}
1299
1300int Camera2Device::StreamAdapter::set_crop(const camera2_stream_ops_t* w,
1301 int left, int top, int right, int bottom) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001302 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001303 int state = static_cast<const StreamAdapter*>(w)->mState;
1304 if (state != ACTIVE) {
1305 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
1306 return INVALID_OPERATION;
1307 }
1308 ANativeWindow *a = toANW(w);
1309 android_native_rect_t crop = { left, top, right, bottom };
1310 return native_window_set_crop(a, &crop);
1311}
1312
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001313/**
1314 * Camera2Device::ReprocessStreamAdapter
1315 */
1316
1317#ifndef container_of
1318#define container_of(ptr, type, member) \
1319 (type *)((char*)(ptr) - offsetof(type, member))
1320#endif
1321
1322Camera2Device::ReprocessStreamAdapter::ReprocessStreamAdapter(camera2_device_t *d):
1323 mState(RELEASED),
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001324 mHal2Device(d),
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001325 mId(-1),
1326 mWidth(0), mHeight(0), mFormat(0),
1327 mActiveBuffers(0),
1328 mFrameCount(0)
1329{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001330 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001331 camera2_stream_in_ops::acquire_buffer = acquire_buffer;
1332 camera2_stream_in_ops::release_buffer = release_buffer;
1333}
1334
1335Camera2Device::ReprocessStreamAdapter::~ReprocessStreamAdapter() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001336 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001337 if (mState != RELEASED) {
1338 release();
1339 }
1340}
1341
1342status_t Camera2Device::ReprocessStreamAdapter::connectToDevice(
1343 const sp<StreamAdapter> &outputStream) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001344 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001345 status_t res;
1346 ALOGV("%s: E", __FUNCTION__);
1347
1348 if (mState != RELEASED) return INVALID_OPERATION;
1349 if (outputStream == NULL) {
1350 ALOGE("%s: Null base stream passed to reprocess stream adapter",
1351 __FUNCTION__);
1352 return BAD_VALUE;
1353 }
1354
1355 mBaseStream = outputStream;
1356 mWidth = outputStream->getWidth();
1357 mHeight = outputStream->getHeight();
1358 mFormat = outputStream->getFormat();
1359
1360 ALOGV("%s: New reprocess stream parameters %d x %d, format 0x%x",
1361 __FUNCTION__, mWidth, mHeight, mFormat);
1362
1363 // Allocate device-side stream interface
1364
1365 uint32_t id;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001366 res = mHal2Device->ops->allocate_reprocess_stream_from_stream(mHal2Device,
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001367 outputStream->getId(), getStreamOps(),
1368 &id);
1369 if (res != OK) {
1370 ALOGE("%s: Device reprocess stream allocation failed: %s (%d)",
1371 __FUNCTION__, strerror(-res), res);
1372 return res;
1373 }
1374
1375 ALOGV("%s: Allocated reprocess stream id %d based on stream %d",
1376 __FUNCTION__, id, outputStream->getId());
1377
1378 mId = id;
1379
1380 mState = ACTIVE;
1381
1382 return OK;
1383}
1384
1385status_t Camera2Device::ReprocessStreamAdapter::release() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001386 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001387 status_t res;
1388 ALOGV("%s: Releasing stream %d", __FUNCTION__, mId);
1389 if (mState >= ACTIVE) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001390 res = mHal2Device->ops->release_reprocess_stream(mHal2Device, mId);
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001391 if (res != OK) {
1392 ALOGE("%s: Unable to release stream %d",
1393 __FUNCTION__, mId);
1394 return res;
1395 }
1396 }
1397
1398 List<QueueEntry>::iterator s;
1399 for (s = mQueue.begin(); s != mQueue.end(); s++) {
1400 sp<BufferReleasedListener> listener = s->releaseListener.promote();
1401 if (listener != 0) listener->onBufferReleased(s->handle);
1402 }
1403 for (s = mInFlightQueue.begin(); s != mInFlightQueue.end(); s++) {
1404 sp<BufferReleasedListener> listener = s->releaseListener.promote();
1405 if (listener != 0) listener->onBufferReleased(s->handle);
1406 }
1407 mQueue.clear();
1408 mInFlightQueue.clear();
1409
1410 mState = RELEASED;
1411 return OK;
1412}
1413
1414status_t Camera2Device::ReprocessStreamAdapter::pushIntoStream(
1415 buffer_handle_t *handle, const wp<BufferReleasedListener> &releaseListener) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001416 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001417 // TODO: Some error checking here would be nice
1418 ALOGV("%s: Pushing buffer %p to stream", __FUNCTION__, (void*)(*handle));
1419
1420 QueueEntry entry;
1421 entry.handle = handle;
1422 entry.releaseListener = releaseListener;
1423 mQueue.push_back(entry);
1424 return OK;
1425}
1426
1427status_t Camera2Device::ReprocessStreamAdapter::dump(int fd,
Igor Murashkinebe3f692012-10-12 16:56:11 -07001428 const Vector<String16>& /*args*/) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001429 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001430 String8 result =
1431 String8::format(" Reprocess stream %d: %d x %d, fmt 0x%x\n",
1432 mId, mWidth, mHeight, mFormat);
1433 result.appendFormat(" acquired buffers: %d\n",
1434 mActiveBuffers);
1435 result.appendFormat(" frame count: %d\n",
1436 mFrameCount);
1437 write(fd, result.string(), result.size());
1438 return OK;
1439}
1440
1441const camera2_stream_in_ops *Camera2Device::ReprocessStreamAdapter::getStreamOps() {
1442 return static_cast<camera2_stream_in_ops *>(this);
1443}
1444
1445int Camera2Device::ReprocessStreamAdapter::acquire_buffer(
1446 const camera2_stream_in_ops_t *w,
1447 buffer_handle_t** buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001448 ATRACE_CALL();
Igor Murashkinebe3f692012-10-12 16:56:11 -07001449
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001450 ReprocessStreamAdapter* stream =
1451 const_cast<ReprocessStreamAdapter*>(
1452 static_cast<const ReprocessStreamAdapter*>(w));
1453 if (stream->mState != ACTIVE) {
1454 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, stream->mState);
1455 return INVALID_OPERATION;
1456 }
1457
1458 if (stream->mQueue.empty()) {
1459 *buffer = NULL;
1460 return OK;
1461 }
1462
1463 QueueEntry &entry = *(stream->mQueue.begin());
1464
1465 *buffer = entry.handle;
1466
1467 stream->mInFlightQueue.push_back(entry);
1468 stream->mQueue.erase(stream->mQueue.begin());
1469
1470 stream->mActiveBuffers++;
1471
1472 ALOGV("Stream %d acquire: Buffer %p acquired", stream->mId,
1473 (void*)(**buffer));
1474 return OK;
1475}
1476
1477int Camera2Device::ReprocessStreamAdapter::release_buffer(
1478 const camera2_stream_in_ops_t* w,
1479 buffer_handle_t* buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001480 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001481 ReprocessStreamAdapter *stream =
1482 const_cast<ReprocessStreamAdapter*>(
1483 static_cast<const ReprocessStreamAdapter*>(w) );
1484 stream->mFrameCount++;
1485 ALOGV("Reprocess stream %d release: Frame %d (%p)",
1486 stream->mId, stream->mFrameCount, (void*)*buffer);
1487 int state = stream->mState;
1488 if (state != ACTIVE) {
1489 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
1490 return INVALID_OPERATION;
1491 }
1492 stream->mActiveBuffers--;
1493
1494 List<QueueEntry>::iterator s;
1495 for (s = stream->mInFlightQueue.begin(); s != stream->mInFlightQueue.end(); s++) {
1496 if ( s->handle == buffer ) break;
1497 }
1498 if (s == stream->mInFlightQueue.end()) {
1499 ALOGE("%s: Can't find buffer %p in in-flight list!", __FUNCTION__,
1500 buffer);
1501 return INVALID_OPERATION;
1502 }
1503
1504 sp<BufferReleasedListener> listener = s->releaseListener.promote();
1505 if (listener != 0) {
1506 listener->onBufferReleased(s->handle);
1507 } else {
1508 ALOGE("%s: Can't free buffer - missing listener", __FUNCTION__);
1509 }
1510 stream->mInFlightQueue.erase(s);
1511
1512 return OK;
1513}
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -07001514
1515}; // namespace android