blob: dc97c47f52045eec296d255334f64fb5e6288077 [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"
33
34namespace android {
35
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070036Camera2Device::Camera2Device(int id):
37 mId(id),
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080038 mHal2Device(NULL)
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070039{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -070040 ATRACE_CALL();
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -070041 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070042}
43
44Camera2Device::~Camera2Device()
45{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -070046 ATRACE_CALL();
Igor Murashkin8dcdb952012-10-02 16:05:11 -070047 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070048 disconnect();
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070049}
50
Igor Murashkin71381052013-03-04 14:53:08 -080051int Camera2Device::getId() const {
52 return mId;
53}
54
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070055status_t Camera2Device::initialize(camera_module_t *module)
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070056{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -070057 ATRACE_CALL();
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -070058 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080059 if (mHal2Device != NULL) {
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070060 ALOGE("%s: Already initialized!", __FUNCTION__);
61 return INVALID_OPERATION;
62 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070063
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070064 status_t res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070065 char name[10];
66 snprintf(name, sizeof(name), "%d", mId);
67
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070068 camera2_device_t *device;
69
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070070 res = module->common.methods->open(&module->common, name,
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070071 reinterpret_cast<hw_device_t**>(&device));
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070072
73 if (res != OK) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070074 ALOGE("%s: Could not open camera %d: %s (%d)", __FUNCTION__,
75 mId, strerror(-res), res);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070076 return res;
77 }
78
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070079 if (device->common.version != CAMERA_DEVICE_API_VERSION_2_0) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070080 ALOGE("%s: Could not open camera %d: "
81 "Camera device is not version %x, reports %x instead",
82 __FUNCTION__, mId, CAMERA_DEVICE_API_VERSION_2_0,
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070083 device->common.version);
84 device->common.close(&device->common);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070085 return BAD_VALUE;
86 }
87
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070088 camera_info info;
89 res = module->get_camera_info(mId, &info);
90 if (res != OK ) return res;
91
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070092 if (info.device_version != device->common.version) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070093 ALOGE("%s: HAL reporting mismatched camera_info version (%x)"
94 " and device version (%x).", __FUNCTION__,
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070095 device->common.version, info.device_version);
96 device->common.close(&device->common);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070097 return BAD_VALUE;
98 }
99
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700100 res = mRequestQueue.setConsumerDevice(device);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700101 if (res != OK) {
102 ALOGE("%s: Camera %d: Unable to connect request queue to device: %s (%d)",
103 __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700104 device->common.close(&device->common);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700105 return res;
106 }
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700107 res = mFrameQueue.setProducerDevice(device);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700108 if (res != OK) {
109 ALOGE("%s: Camera %d: Unable to connect frame queue to device: %s (%d)",
110 __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700111 device->common.close(&device->common);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700112 return res;
113 }
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700114
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700115 res = device->ops->get_metadata_vendor_tag_ops(device, &mVendorTagOps);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700116 if (res != OK ) {
117 ALOGE("%s: Camera %d: Unable to retrieve tag ops from device: %s (%d)",
118 __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700119 device->common.close(&device->common);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700120 return res;
121 }
Shuzhen Wang314079e2012-08-31 10:24:22 -0700122 res = set_camera_metadata_vendor_tag_ops(mVendorTagOps);
123 if (res != OK) {
124 ALOGE("%s: Camera %d: Unable to set tag ops: %s (%d)",
125 __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700126 device->common.close(&device->common);
Shuzhen Wang314079e2012-08-31 10:24:22 -0700127 return res;
128 }
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700129 res = device->ops->set_notify_callback(device, notificationCallback,
130 NULL);
131 if (res != OK) {
132 ALOGE("%s: Camera %d: Unable to initialize notification callback!",
133 __FUNCTION__, mId);
134 device->common.close(&device->common);
135 return res;
136 }
137
138 mDeviceInfo = info.static_camera_characteristics;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800139 mHal2Device = device;
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700140
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700141 return OK;
142}
143
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700144status_t Camera2Device::disconnect() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700145 ATRACE_CALL();
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700146 status_t res = OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800147 if (mHal2Device) {
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700148 ALOGV("%s: Closing device for camera %d", __FUNCTION__, mId);
149
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800150 int inProgressCount = mHal2Device->ops->get_in_progress_count(mHal2Device);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700151 if (inProgressCount > 0) {
152 ALOGW("%s: Closing camera device %d with %d requests in flight!",
153 __FUNCTION__, mId, inProgressCount);
154 }
Eino-Ville Talvalac62bb782012-09-24 13:44:07 -0700155 mReprocessStreams.clear();
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700156 mStreams.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800157 res = mHal2Device->common.close(&mHal2Device->common);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700158 if (res != OK) {
159 ALOGE("%s: Could not close camera %d: %s (%d)",
160 __FUNCTION__,
161 mId, strerror(-res), res);
162 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800163 mHal2Device = NULL;
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700164 ALOGV("%s: Shutdown complete", __FUNCTION__);
165 }
166 return res;
167}
168
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700169status_t Camera2Device::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700170 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700171 String8 result;
Eino-Ville Talvala97197152012-08-06 14:25:19 -0700172 int detailLevel = 0;
173 int n = args.size();
174 String16 detailOption("-d");
175 for (int i = 0; i + 1 < n; i++) {
176 if (args[i] == detailOption) {
177 String8 levelStr(args[i+1]);
178 detailLevel = atoi(levelStr.string());
179 }
180 }
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700181
Eino-Ville Talvala603b12e2012-08-08 09:25:58 -0700182 result.appendFormat(" Camera2Device[%d] dump (detail level %d):\n",
183 mId, detailLevel);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700184
Eino-Ville Talvala97197152012-08-06 14:25:19 -0700185 if (detailLevel > 0) {
186 result = " Request queue contents:\n";
187 write(fd, result.string(), result.size());
188 mRequestQueue.dump(fd, args);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700189
Eino-Ville Talvala97197152012-08-06 14:25:19 -0700190 result = " Frame queue contents:\n";
191 write(fd, result.string(), result.size());
192 mFrameQueue.dump(fd, args);
193 }
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700194
195 result = " Active streams:\n";
196 write(fd, result.string(), result.size());
197 for (StreamList::iterator s = mStreams.begin(); s != mStreams.end(); s++) {
198 (*s)->dump(fd, args);
199 }
200
201 result = " HAL device dump:\n";
202 write(fd, result.string(), result.size());
203
204 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800205 res = mHal2Device->ops->dump(mHal2Device, fd);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700206
207 return res;
208}
209
Igor Murashkinbd02dd12013-02-13 15:53:56 -0800210const CameraMetadata& Camera2Device::info() const {
Eino-Ville Talvala9e4c3db2012-07-20 11:07:52 -0700211 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700212
213 return mDeviceInfo;
214}
215
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700216status_t Camera2Device::capture(CameraMetadata &request) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700217 ATRACE_CALL();
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700218 ALOGV("%s: E", __FUNCTION__);
219
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700220 mRequestQueue.enqueue(request.release());
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700221 return OK;
222}
223
224
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700225status_t Camera2Device::setStreamingRequest(const CameraMetadata &request) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700226 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700227 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700228 CameraMetadata streamRequest(request);
229 return mRequestQueue.setStreamSlot(streamRequest.release());
230}
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700231
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700232status_t Camera2Device::clearStreamingRequest() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700233 ATRACE_CALL();
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700234 return mRequestQueue.setStreamSlot(NULL);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700235}
236
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700237status_t Camera2Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
238 ATRACE_CALL();
239 return mRequestQueue.waitForDequeue(requestId, timeout);
240}
241
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700242status_t Camera2Device::createStream(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700243 uint32_t width, uint32_t height, int format, size_t size, int *id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700244 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700245 status_t res;
246 ALOGV("%s: E", __FUNCTION__);
247
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800248 sp<StreamAdapter> stream = new StreamAdapter(mHal2Device);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700249
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700250 res = stream->connectToDevice(consumer, width, height, format, size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700251 if (res != OK) {
252 ALOGE("%s: Camera %d: Unable to create stream (%d x %d, format %x):"
253 "%s (%d)",
254 __FUNCTION__, mId, width, height, format, strerror(-res), res);
255 return res;
256 }
257
258 *id = stream->getId();
259
260 mStreams.push_back(stream);
261 return OK;
262}
263
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700264status_t Camera2Device::createReprocessStreamFromStream(int outputId, int *id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700265 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700266 status_t res;
267 ALOGV("%s: E", __FUNCTION__);
268
269 bool found = false;
270 StreamList::iterator streamI;
271 for (streamI = mStreams.begin();
272 streamI != mStreams.end(); streamI++) {
273 if ((*streamI)->getId() == outputId) {
274 found = true;
275 break;
276 }
277 }
278 if (!found) {
279 ALOGE("%s: Camera %d: Output stream %d doesn't exist; can't create "
280 "reprocess stream from it!", __FUNCTION__, mId, outputId);
281 return BAD_VALUE;
282 }
283
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800284 sp<ReprocessStreamAdapter> stream = new ReprocessStreamAdapter(mHal2Device);
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700285
286 res = stream->connectToDevice((*streamI));
287 if (res != OK) {
288 ALOGE("%s: Camera %d: Unable to create reprocessing stream from "\
289 "stream %d: %s (%d)", __FUNCTION__, mId, outputId,
290 strerror(-res), res);
291 return res;
292 }
293
294 *id = stream->getId();
295
296 mReprocessStreams.push_back(stream);
297 return OK;
298}
299
300
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700301status_t Camera2Device::getStreamInfo(int id,
302 uint32_t *width, uint32_t *height, uint32_t *format) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700303 ATRACE_CALL();
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700304 ALOGV("%s: E", __FUNCTION__);
305 bool found = false;
306 StreamList::iterator streamI;
307 for (streamI = mStreams.begin();
308 streamI != mStreams.end(); streamI++) {
309 if ((*streamI)->getId() == id) {
310 found = true;
311 break;
312 }
313 }
314 if (!found) {
315 ALOGE("%s: Camera %d: Stream %d does not exist",
316 __FUNCTION__, mId, id);
317 return BAD_VALUE;
318 }
319
320 if (width) *width = (*streamI)->getWidth();
321 if (height) *height = (*streamI)->getHeight();
322 if (format) *format = (*streamI)->getFormat();
323
324 return OK;
325}
326
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700327status_t Camera2Device::setStreamTransform(int id,
328 int transform) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700329 ATRACE_CALL();
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700330 ALOGV("%s: E", __FUNCTION__);
331 bool found = false;
332 StreamList::iterator streamI;
333 for (streamI = mStreams.begin();
334 streamI != mStreams.end(); streamI++) {
335 if ((*streamI)->getId() == id) {
336 found = true;
337 break;
338 }
339 }
340 if (!found) {
341 ALOGE("%s: Camera %d: Stream %d does not exist",
342 __FUNCTION__, mId, id);
343 return BAD_VALUE;
344 }
345
346 return (*streamI)->setTransform(transform);
347}
348
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700349status_t Camera2Device::deleteStream(int id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700350 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700351 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700352 bool found = false;
353 for (StreamList::iterator streamI = mStreams.begin();
354 streamI != mStreams.end(); streamI++) {
355 if ((*streamI)->getId() == id) {
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700356 status_t res = (*streamI)->release();
Eino-Ville Talvala4ecfec32012-06-12 17:13:48 -0700357 if (res != OK) {
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700358 ALOGE("%s: Unable to release stream %d from HAL device: "
Eino-Ville Talvala4ecfec32012-06-12 17:13:48 -0700359 "%s (%d)", __FUNCTION__, id, strerror(-res), res);
360 return res;
361 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700362 mStreams.erase(streamI);
363 found = true;
364 break;
365 }
366 }
367 if (!found) {
368 ALOGE("%s: Camera %d: Unable to find stream %d to delete",
369 __FUNCTION__, mId, id);
370 return BAD_VALUE;
371 }
372 return OK;
373}
374
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700375status_t Camera2Device::deleteReprocessStream(int id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700376 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700377 ALOGV("%s: E", __FUNCTION__);
378 bool found = false;
379 for (ReprocessStreamList::iterator streamI = mReprocessStreams.begin();
380 streamI != mReprocessStreams.end(); streamI++) {
381 if ((*streamI)->getId() == id) {
382 status_t res = (*streamI)->release();
383 if (res != OK) {
384 ALOGE("%s: Unable to release reprocess stream %d from "
385 "HAL device: %s (%d)", __FUNCTION__, id,
386 strerror(-res), res);
387 return res;
388 }
389 mReprocessStreams.erase(streamI);
390 found = true;
391 break;
392 }
393 }
394 if (!found) {
395 ALOGE("%s: Camera %d: Unable to find stream %d to delete",
396 __FUNCTION__, mId, id);
397 return BAD_VALUE;
398 }
399 return OK;
400}
401
402
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700403status_t Camera2Device::createDefaultRequest(int templateId,
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700404 CameraMetadata *request) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700405 ATRACE_CALL();
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700406 status_t err;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700407 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700408 camera_metadata_t *rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800409 err = mHal2Device->ops->construct_default_request(
410 mHal2Device, templateId, &rawRequest);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700411 request->acquire(rawRequest);
412 return err;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700413}
414
415status_t Camera2Device::waitUntilDrained() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700416 ATRACE_CALL();
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700417 static const uint32_t kSleepTime = 50000; // 50 ms
418 static const uint32_t kMaxSleepTime = 10000000; // 10 s
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700419 ALOGV("%s: Camera %d: Starting wait", __FUNCTION__, mId);
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700420 if (mRequestQueue.getBufferCount() ==
421 CAMERA2_REQUEST_QUEUE_IS_BOTTOMLESS) return INVALID_OPERATION;
422
423 // TODO: Set up notifications from HAL, instead of sleeping here
424 uint32_t totalTime = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800425 while (mHal2Device->ops->get_in_progress_count(mHal2Device) > 0) {
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700426 usleep(kSleepTime);
427 totalTime += kSleepTime;
428 if (totalTime > kMaxSleepTime) {
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700429 ALOGE("%s: Waited %d us, %d requests still in flight", __FUNCTION__,
Alex Rayf0eeb532013-03-17 03:23:18 -0700430 totalTime, mHal2Device->ops->get_in_progress_count(mHal2Device));
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700431 return TIMED_OUT;
432 }
433 }
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700434 ALOGV("%s: Camera %d: HAL is idle", __FUNCTION__, mId);
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700435 return OK;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700436}
437
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700438status_t Camera2Device::setNotifyCallback(NotificationListener *listener) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700439 ATRACE_CALL();
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700440 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800441 res = mHal2Device->ops->set_notify_callback(mHal2Device, notificationCallback,
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700442 reinterpret_cast<void*>(listener) );
443 if (res != OK) {
444 ALOGE("%s: Unable to set notification callback!", __FUNCTION__);
445 }
446 return res;
447}
448
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -0700449bool Camera2Device::willNotify3A() {
450 return true;
451}
452
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700453void Camera2Device::notificationCallback(int32_t msg_type,
454 int32_t ext1,
455 int32_t ext2,
456 int32_t ext3,
457 void *user) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700458 ATRACE_CALL();
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700459 NotificationListener *listener = reinterpret_cast<NotificationListener*>(user);
460 ALOGV("%s: Notification %d, arguments %d, %d, %d", __FUNCTION__, msg_type,
461 ext1, ext2, ext3);
462 if (listener != NULL) {
463 switch (msg_type) {
464 case CAMERA2_MSG_ERROR:
465 listener->notifyError(ext1, ext2, ext3);
466 break;
467 case CAMERA2_MSG_SHUTTER: {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700468 // TODO: Only needed for camera2 API, which is unsupported
469 // by HAL2 directly.
470 // nsecs_t timestamp = (nsecs_t)ext2 | ((nsecs_t)(ext3) << 32 );
471 // listener->notifyShutter(requestId, timestamp);
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700472 break;
473 }
474 case CAMERA2_MSG_AUTOFOCUS:
475 listener->notifyAutoFocus(ext1, ext2);
476 break;
477 case CAMERA2_MSG_AUTOEXPOSURE:
478 listener->notifyAutoExposure(ext1, ext2);
479 break;
480 case CAMERA2_MSG_AUTOWB:
481 listener->notifyAutoWhitebalance(ext1, ext2);
482 break;
483 default:
484 ALOGE("%s: Unknown notification %d (arguments %d, %d, %d)!",
485 __FUNCTION__, msg_type, ext1, ext2, ext3);
486 }
487 }
488}
489
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -0700490status_t Camera2Device::waitForNextFrame(nsecs_t timeout) {
491 return mFrameQueue.waitForBuffer(timeout);
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700492}
493
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700494status_t Camera2Device::getNextFrame(CameraMetadata *frame) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700495 ATRACE_CALL();
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700496 status_t res;
497 camera_metadata_t *rawFrame;
498 res = mFrameQueue.dequeue(&rawFrame);
499 if (rawFrame == NULL) {
500 return NOT_ENOUGH_DATA;
501 } else if (res == OK) {
502 frame->acquire(rawFrame);
503 }
504 return res;
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700505}
506
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700507status_t Camera2Device::triggerAutofocus(uint32_t id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700508 ATRACE_CALL();
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700509 status_t res;
510 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800511 res = mHal2Device->ops->trigger_action(mHal2Device,
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700512 CAMERA2_TRIGGER_AUTOFOCUS, id, 0);
513 if (res != OK) {
514 ALOGE("%s: Error triggering autofocus (id %d)",
515 __FUNCTION__, id);
516 }
517 return res;
518}
519
520status_t Camera2Device::triggerCancelAutofocus(uint32_t id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700521 ATRACE_CALL();
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700522 status_t res;
523 ALOGV("%s: Canceling autofocus, id %d", __FUNCTION__, id);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800524 res = mHal2Device->ops->trigger_action(mHal2Device,
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700525 CAMERA2_TRIGGER_CANCEL_AUTOFOCUS, id, 0);
526 if (res != OK) {
527 ALOGE("%s: Error canceling autofocus (id %d)",
528 __FUNCTION__, id);
529 }
530 return res;
531}
532
533status_t Camera2Device::triggerPrecaptureMetering(uint32_t id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700534 ATRACE_CALL();
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700535 status_t res;
536 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800537 res = mHal2Device->ops->trigger_action(mHal2Device,
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700538 CAMERA2_TRIGGER_PRECAPTURE_METERING, id, 0);
539 if (res != OK) {
540 ALOGE("%s: Error triggering precapture metering (id %d)",
541 __FUNCTION__, id);
542 }
543 return res;
544}
545
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700546status_t Camera2Device::pushReprocessBuffer(int reprocessStreamId,
547 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700548 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700549 ALOGV("%s: E", __FUNCTION__);
550 bool found = false;
551 status_t res = OK;
552 for (ReprocessStreamList::iterator streamI = mReprocessStreams.begin();
553 streamI != mReprocessStreams.end(); streamI++) {
554 if ((*streamI)->getId() == reprocessStreamId) {
555 res = (*streamI)->pushIntoStream(buffer, listener);
556 if (res != OK) {
557 ALOGE("%s: Unable to push buffer to reprocess stream %d: %s (%d)",
558 __FUNCTION__, reprocessStreamId, strerror(-res), res);
559 return res;
560 }
561 found = true;
562 break;
563 }
564 }
565 if (!found) {
566 ALOGE("%s: Camera %d: Unable to find reprocess stream %d",
567 __FUNCTION__, mId, reprocessStreamId);
568 res = BAD_VALUE;
569 }
570 return res;
571}
572
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700573status_t Camera2Device::flush() {
574 ATRACE_CALL();
575
576 mRequestQueue.clear();
577 return waitUntilDrained();
578}
579
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700580/**
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700581 * Camera2Device::MetadataQueue
582 */
583
584Camera2Device::MetadataQueue::MetadataQueue():
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800585 mHal2Device(NULL),
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700586 mFrameCount(0),
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700587 mLatestRequestId(0),
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700588 mCount(0),
589 mStreamSlotCount(0),
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -0700590 mSignalConsumer(true)
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700591{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700592 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700593 camera2_request_queue_src_ops::dequeue_request = consumer_dequeue;
594 camera2_request_queue_src_ops::request_count = consumer_buffer_count;
595 camera2_request_queue_src_ops::free_request = consumer_free;
596
597 camera2_frame_queue_dst_ops::dequeue_frame = producer_dequeue;
598 camera2_frame_queue_dst_ops::cancel_frame = producer_cancel;
599 camera2_frame_queue_dst_ops::enqueue_frame = producer_enqueue;
600}
601
602Camera2Device::MetadataQueue::~MetadataQueue() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700603 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700604 clear();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700605}
606
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700607// Connect to camera2 HAL as consumer (input requests/reprocessing)
608status_t Camera2Device::MetadataQueue::setConsumerDevice(camera2_device_t *d) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700609 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700610 status_t res;
611 res = d->ops->set_request_queue_src_ops(d,
612 this);
613 if (res != OK) return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800614 mHal2Device = d;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700615 return OK;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700616}
617
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700618status_t Camera2Device::MetadataQueue::setProducerDevice(camera2_device_t *d) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700619 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700620 status_t res;
621 res = d->ops->set_frame_queue_dst_ops(d,
622 this);
623 return res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700624}
625
626// Real interfaces
627status_t Camera2Device::MetadataQueue::enqueue(camera_metadata_t *buf) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700628 ATRACE_CALL();
Eino-Ville Talvala2c08dc62012-06-15 12:49:21 -0700629 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700630 Mutex::Autolock l(mMutex);
631
632 mCount++;
633 mEntries.push_back(buf);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700634
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700635 return signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700636}
637
638int Camera2Device::MetadataQueue::getBufferCount() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700639 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700640 Mutex::Autolock l(mMutex);
641 if (mStreamSlotCount > 0) {
642 return CAMERA2_REQUEST_QUEUE_IS_BOTTOMLESS;
643 }
644 return mCount;
645}
646
647status_t Camera2Device::MetadataQueue::dequeue(camera_metadata_t **buf,
648 bool incrementCount)
649{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700650 ATRACE_CALL();
Eino-Ville Talvala2c08dc62012-06-15 12:49:21 -0700651 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700652 status_t res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700653 Mutex::Autolock l(mMutex);
654
655 if (mCount == 0) {
656 if (mStreamSlotCount == 0) {
Eino-Ville Talvala2c08dc62012-06-15 12:49:21 -0700657 ALOGVV("%s: Empty", __FUNCTION__);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700658 *buf = NULL;
659 mSignalConsumer = true;
660 return OK;
661 }
Eino-Ville Talvala2c08dc62012-06-15 12:49:21 -0700662 ALOGVV("%s: Streaming %d frames to queue", __FUNCTION__,
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700663 mStreamSlotCount);
664
665 for (List<camera_metadata_t*>::iterator slotEntry = mStreamSlot.begin();
666 slotEntry != mStreamSlot.end();
667 slotEntry++ ) {
668 size_t entries = get_camera_metadata_entry_count(*slotEntry);
669 size_t dataBytes = get_camera_metadata_data_count(*slotEntry);
670
671 camera_metadata_t *copy =
672 allocate_camera_metadata(entries, dataBytes);
673 append_camera_metadata(copy, *slotEntry);
674 mEntries.push_back(copy);
675 }
676 mCount = mStreamSlotCount;
677 }
Eino-Ville Talvala2c08dc62012-06-15 12:49:21 -0700678 ALOGVV("MetadataQueue: deque (%d buffers)", mCount);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700679 camera_metadata_t *b = *(mEntries.begin());
680 mEntries.erase(mEntries.begin());
681
682 if (incrementCount) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700683 ATRACE_INT("cam2_request", mFrameCount);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700684 camera_metadata_entry_t frameCount;
685 res = find_camera_metadata_entry(b,
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700686 ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700687 &frameCount);
688 if (res != OK) {
689 ALOGE("%s: Unable to add frame count: %s (%d)",
690 __FUNCTION__, strerror(-res), res);
691 } else {
692 *frameCount.data.i32 = mFrameCount;
693 }
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700694 mFrameCount++;
695 }
696
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700697 // Check for request ID, and if present, signal waiters.
698 camera_metadata_entry_t requestId;
699 res = find_camera_metadata_entry(b,
700 ANDROID_REQUEST_ID,
701 &requestId);
702 if (res == OK) {
703 mLatestRequestId = requestId.data.i32[0];
704 mNewRequestId.signal();
705 }
706
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700707 *buf = b;
708 mCount--;
709
710 return OK;
711}
712
713status_t Camera2Device::MetadataQueue::waitForBuffer(nsecs_t timeout)
714{
715 Mutex::Autolock l(mMutex);
716 status_t res;
717 while (mCount == 0) {
718 res = notEmpty.waitRelative(mMutex,timeout);
719 if (res != OK) return res;
720 }
721 return OK;
722}
723
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700724status_t Camera2Device::MetadataQueue::waitForDequeue(int32_t id,
725 nsecs_t timeout) {
726 Mutex::Autolock l(mMutex);
727 status_t res;
728 while (mLatestRequestId != id) {
729 nsecs_t startTime = systemTime();
730
731 res = mNewRequestId.waitRelative(mMutex, timeout);
732 if (res != OK) return res;
733
734 timeout -= (systemTime() - startTime);
735 }
736
737 return OK;
738}
739
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700740status_t Camera2Device::MetadataQueue::setStreamSlot(camera_metadata_t *buf)
741{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700742 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700743 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700744 Mutex::Autolock l(mMutex);
745 if (buf == NULL) {
746 freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
747 mStreamSlotCount = 0;
748 return OK;
749 }
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700750 camera_metadata_t *buf2 = clone_camera_metadata(buf);
751 if (!buf2) {
752 ALOGE("%s: Unable to clone metadata buffer!", __FUNCTION__);
753 return NO_MEMORY;
754 }
755
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700756 if (mStreamSlotCount > 1) {
757 List<camera_metadata_t*>::iterator deleter = ++mStreamSlot.begin();
758 freeBuffers(++mStreamSlot.begin(), mStreamSlot.end());
759 mStreamSlotCount = 1;
760 }
761 if (mStreamSlotCount == 1) {
762 free_camera_metadata( *(mStreamSlot.begin()) );
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700763 *(mStreamSlot.begin()) = buf2;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700764 } else {
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700765 mStreamSlot.push_front(buf2);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700766 mStreamSlotCount = 1;
767 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700768 return signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700769}
770
771status_t Camera2Device::MetadataQueue::setStreamSlot(
772 const List<camera_metadata_t*> &bufs)
773{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700774 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700775 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700776 Mutex::Autolock l(mMutex);
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700777
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700778 if (mStreamSlotCount > 0) {
779 freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
780 }
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700781 mStreamSlotCount = 0;
782 for (List<camera_metadata_t*>::const_iterator r = bufs.begin();
783 r != bufs.end(); r++) {
784 camera_metadata_t *r2 = clone_camera_metadata(*r);
785 if (!r2) {
786 ALOGE("%s: Unable to clone metadata buffer!", __FUNCTION__);
787 return NO_MEMORY;
788 }
789 mStreamSlot.push_back(r2);
790 mStreamSlotCount++;
791 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700792 return signalConsumerLocked();
793}
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700794
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700795status_t Camera2Device::MetadataQueue::clear()
796{
797 ATRACE_CALL();
798 ALOGV("%s: E", __FUNCTION__);
799
800 Mutex::Autolock l(mMutex);
801
802 // Clear streaming slot
803 freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
804 mStreamSlotCount = 0;
805
806 // Clear request queue
807 freeBuffers(mEntries.begin(), mEntries.end());
808 mCount = 0;
809 return OK;
810}
811
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700812status_t Camera2Device::MetadataQueue::dump(int fd,
Igor Murashkinebe3f692012-10-12 16:56:11 -0700813 const Vector<String16>& /*args*/) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700814 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700815 String8 result;
816 status_t notLocked;
817 notLocked = mMutex.tryLock();
818 if (notLocked) {
819 result.append(" (Unable to lock queue mutex)\n");
820 }
821 result.appendFormat(" Current frame number: %d\n", mFrameCount);
822 if (mStreamSlotCount == 0) {
823 result.append(" Stream slot: Empty\n");
824 write(fd, result.string(), result.size());
825 } else {
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000826 result.appendFormat(" Stream slot: %zu entries\n",
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700827 mStreamSlot.size());
828 int i = 0;
829 for (List<camera_metadata_t*>::iterator r = mStreamSlot.begin();
830 r != mStreamSlot.end(); r++) {
831 result = String8::format(" Stream slot buffer %d:\n", i);
832 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -0700833 dump_indented_camera_metadata(*r, fd, 2, 10);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700834 i++;
835 }
836 }
837 if (mEntries.size() == 0) {
838 result = " Main queue is empty\n";
839 write(fd, result.string(), result.size());
840 } else {
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000841 result = String8::format(" Main queue has %zu entries:\n",
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700842 mEntries.size());
843 int i = 0;
844 for (List<camera_metadata_t*>::iterator r = mEntries.begin();
845 r != mEntries.end(); r++) {
846 result = String8::format(" Queue entry %d:\n", i);
847 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -0700848 dump_indented_camera_metadata(*r, fd, 2, 10);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700849 i++;
850 }
851 }
852
853 if (notLocked == 0) {
854 mMutex.unlock();
855 }
856
857 return OK;
858}
859
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700860status_t Camera2Device::MetadataQueue::signalConsumerLocked() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700861 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700862 status_t res = OK;
863 notEmpty.signal();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800864 if (mSignalConsumer && mHal2Device != NULL) {
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700865 mSignalConsumer = false;
866
867 mMutex.unlock();
868 ALOGV("%s: Signaling consumer", __FUNCTION__);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800869 res = mHal2Device->ops->notify_request_queue_not_empty(mHal2Device);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700870 mMutex.lock();
871 }
872 return res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700873}
874
875status_t Camera2Device::MetadataQueue::freeBuffers(
876 List<camera_metadata_t*>::iterator start,
877 List<camera_metadata_t*>::iterator end)
878{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700879 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700880 while (start != end) {
881 free_camera_metadata(*start);
882 start = mStreamSlot.erase(start);
883 }
884 return OK;
885}
886
887Camera2Device::MetadataQueue* Camera2Device::MetadataQueue::getInstance(
888 const camera2_request_queue_src_ops_t *q)
889{
890 const MetadataQueue* cmq = static_cast<const MetadataQueue*>(q);
891 return const_cast<MetadataQueue*>(cmq);
892}
893
894Camera2Device::MetadataQueue* Camera2Device::MetadataQueue::getInstance(
895 const camera2_frame_queue_dst_ops_t *q)
896{
897 const MetadataQueue* cmq = static_cast<const MetadataQueue*>(q);
898 return const_cast<MetadataQueue*>(cmq);
899}
900
901int Camera2Device::MetadataQueue::consumer_buffer_count(
902 const camera2_request_queue_src_ops_t *q)
903{
904 MetadataQueue *queue = getInstance(q);
905 return queue->getBufferCount();
906}
907
908int Camera2Device::MetadataQueue::consumer_dequeue(
909 const camera2_request_queue_src_ops_t *q,
910 camera_metadata_t **buffer)
911{
912 MetadataQueue *queue = getInstance(q);
913 return queue->dequeue(buffer, true);
914}
915
916int Camera2Device::MetadataQueue::consumer_free(
917 const camera2_request_queue_src_ops_t *q,
918 camera_metadata_t *old_buffer)
919{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700920 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700921 MetadataQueue *queue = getInstance(q);
Igor Murashkinebe3f692012-10-12 16:56:11 -0700922 (void)queue;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700923 free_camera_metadata(old_buffer);
924 return OK;
925}
926
927int Camera2Device::MetadataQueue::producer_dequeue(
Igor Murashkinebe3f692012-10-12 16:56:11 -0700928 const camera2_frame_queue_dst_ops_t * /*q*/,
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700929 size_t entries, size_t bytes,
930 camera_metadata_t **buffer)
931{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700932 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700933 camera_metadata_t *new_buffer =
934 allocate_camera_metadata(entries, bytes);
935 if (new_buffer == NULL) return NO_MEMORY;
936 *buffer = new_buffer;
937 return OK;
938}
939
940int Camera2Device::MetadataQueue::producer_cancel(
Igor Murashkinebe3f692012-10-12 16:56:11 -0700941 const camera2_frame_queue_dst_ops_t * /*q*/,
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700942 camera_metadata_t *old_buffer)
943{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700944 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700945 free_camera_metadata(old_buffer);
946 return OK;
947}
948
949int Camera2Device::MetadataQueue::producer_enqueue(
950 const camera2_frame_queue_dst_ops_t *q,
951 camera_metadata_t *filled_buffer)
952{
953 MetadataQueue *queue = getInstance(q);
954 return queue->enqueue(filled_buffer);
955}
956
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700957/**
958 * Camera2Device::StreamAdapter
959 */
960
961#ifndef container_of
962#define container_of(ptr, type, member) \
963 (type *)((char*)(ptr) - offsetof(type, member))
964#endif
965
966Camera2Device::StreamAdapter::StreamAdapter(camera2_device_t *d):
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700967 mState(RELEASED),
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800968 mHal2Device(d),
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700969 mId(-1),
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700970 mWidth(0), mHeight(0), mFormat(0), mSize(0), mUsage(0),
971 mMaxProducerBuffers(0), mMaxConsumerBuffers(0),
972 mTotalBuffers(0),
973 mFormatRequested(0),
974 mActiveBuffers(0),
975 mFrameCount(0),
976 mLastTimestamp(0)
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700977{
978 camera2_stream_ops::dequeue_buffer = dequeue_buffer;
979 camera2_stream_ops::enqueue_buffer = enqueue_buffer;
980 camera2_stream_ops::cancel_buffer = cancel_buffer;
981 camera2_stream_ops::set_crop = set_crop;
982}
983
984Camera2Device::StreamAdapter::~StreamAdapter() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700985 ATRACE_CALL();
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700986 if (mState != RELEASED) {
987 release();
988 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700989}
990
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700991status_t Camera2Device::StreamAdapter::connectToDevice(
992 sp<ANativeWindow> consumer,
993 uint32_t width, uint32_t height, int format, size_t size) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700994 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700995 status_t res;
Eino-Ville Talvala9e4c3db2012-07-20 11:07:52 -0700996 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700997
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700998 if (mState != RELEASED) return INVALID_OPERATION;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700999 if (consumer == NULL) {
1000 ALOGE("%s: Null consumer passed to stream adapter", __FUNCTION__);
1001 return BAD_VALUE;
1002 }
1003
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001004 ALOGV("%s: New stream parameters %d x %d, format 0x%x, size %d",
1005 __FUNCTION__, width, height, format, size);
1006
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001007 mConsumerInterface = consumer;
1008 mWidth = width;
1009 mHeight = height;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -07001010 mSize = (format == HAL_PIXEL_FORMAT_BLOB) ? size : 0;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001011 mFormatRequested = format;
1012
1013 // Allocate device-side stream interface
1014
1015 uint32_t id;
1016 uint32_t formatActual;
1017 uint32_t usage;
1018 uint32_t maxBuffers = 2;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001019 res = mHal2Device->ops->allocate_stream(mHal2Device,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001020 mWidth, mHeight, mFormatRequested, getStreamOps(),
1021 &id, &formatActual, &usage, &maxBuffers);
1022 if (res != OK) {
1023 ALOGE("%s: Device stream allocation failed: %s (%d)",
1024 __FUNCTION__, strerror(-res), res);
1025 return res;
1026 }
1027
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001028 ALOGV("%s: Allocated stream id %d, actual format 0x%x, "
1029 "usage 0x%x, producer wants %d buffers", __FUNCTION__,
1030 id, formatActual, usage, maxBuffers);
1031
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001032 mId = id;
1033 mFormat = formatActual;
1034 mUsage = usage;
1035 mMaxProducerBuffers = maxBuffers;
1036
1037 mState = ALLOCATED;
1038
1039 // Configure consumer-side ANativeWindow interface
1040 res = native_window_api_connect(mConsumerInterface.get(),
1041 NATIVE_WINDOW_API_CAMERA);
1042 if (res != OK) {
1043 ALOGE("%s: Unable to connect to native window for stream %d",
1044 __FUNCTION__, mId);
1045
1046 return res;
1047 }
1048
1049 mState = CONNECTED;
1050
1051 res = native_window_set_usage(mConsumerInterface.get(), mUsage);
1052 if (res != OK) {
1053 ALOGE("%s: Unable to configure usage %08x for stream %d",
1054 __FUNCTION__, mUsage, mId);
1055 return res;
1056 }
1057
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001058 res = native_window_set_scaling_mode(mConsumerInterface.get(),
1059 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1060 if (res != OK) {
1061 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
1062 __FUNCTION__, strerror(-res), res);
1063 return res;
1064 }
1065
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -07001066 res = setTransform(0);
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001067 if (res != OK) {
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001068 return res;
1069 }
1070
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -07001071 if (mFormat == HAL_PIXEL_FORMAT_BLOB) {
1072 res = native_window_set_buffers_geometry(mConsumerInterface.get(),
1073 mSize, 1, mFormat);
1074 if (res != OK) {
1075 ALOGE("%s: Unable to configure compressed stream buffer geometry"
1076 " %d x %d, size %d for stream %d",
1077 __FUNCTION__, mWidth, mHeight, mSize, mId);
1078 return res;
1079 }
1080 } else {
1081 res = native_window_set_buffers_geometry(mConsumerInterface.get(),
1082 mWidth, mHeight, mFormat);
1083 if (res != OK) {
1084 ALOGE("%s: Unable to configure stream buffer geometry"
1085 " %d x %d, format 0x%x for stream %d",
1086 __FUNCTION__, mWidth, mHeight, mFormat, mId);
1087 return res;
1088 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001089 }
1090
1091 int maxConsumerBuffers;
1092 res = mConsumerInterface->query(mConsumerInterface.get(),
1093 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
1094 if (res != OK) {
1095 ALOGE("%s: Unable to query consumer undequeued"
1096 " buffer count for stream %d", __FUNCTION__, mId);
1097 return res;
1098 }
1099 mMaxConsumerBuffers = maxConsumerBuffers;
1100
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001101 ALOGV("%s: Consumer wants %d buffers", __FUNCTION__,
1102 mMaxConsumerBuffers);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001103
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001104 mTotalBuffers = mMaxConsumerBuffers + mMaxProducerBuffers;
1105 mActiveBuffers = 0;
1106 mFrameCount = 0;
1107 mLastTimestamp = 0;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001108
1109 res = native_window_set_buffer_count(mConsumerInterface.get(),
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001110 mTotalBuffers);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001111 if (res != OK) {
1112 ALOGE("%s: Unable to set buffer count for stream %d",
1113 __FUNCTION__, mId);
1114 return res;
1115 }
1116
1117 // Register allocated buffers with HAL device
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001118 buffer_handle_t *buffers = new buffer_handle_t[mTotalBuffers];
1119 ANativeWindowBuffer **anwBuffers = new ANativeWindowBuffer*[mTotalBuffers];
1120 uint32_t bufferIdx = 0;
1121 for (; bufferIdx < mTotalBuffers; bufferIdx++) {
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001122 res = native_window_dequeue_buffer_and_wait(mConsumerInterface.get(),
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001123 &anwBuffers[bufferIdx]);
1124 if (res != OK) {
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001125 ALOGE("%s: Unable to dequeue buffer %d for initial registration for "
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001126 "stream %d", __FUNCTION__, bufferIdx, mId);
1127 goto cleanUpBuffers;
1128 }
1129
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001130 buffers[bufferIdx] = anwBuffers[bufferIdx]->handle;
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001131 ALOGV("%s: Buffer %p allocated", __FUNCTION__, (void*)buffers[bufferIdx]);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001132 }
1133
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001134 ALOGV("%s: Registering %d buffers with camera HAL", __FUNCTION__, mTotalBuffers);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001135 res = mHal2Device->ops->register_stream_buffers(mHal2Device,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001136 mId,
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001137 mTotalBuffers,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001138 buffers);
1139 if (res != OK) {
1140 ALOGE("%s: Unable to register buffers with HAL device for stream %d",
1141 __FUNCTION__, mId);
1142 } else {
1143 mState = ACTIVE;
1144 }
1145
1146cleanUpBuffers:
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001147 ALOGV("%s: Cleaning up %d buffers", __FUNCTION__, bufferIdx);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001148 for (uint32_t i = 0; i < bufferIdx; i++) {
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001149 res = mConsumerInterface->cancelBuffer(mConsumerInterface.get(),
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001150 anwBuffers[i], -1);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001151 if (res != OK) {
1152 ALOGE("%s: Unable to cancel buffer %d after registration",
1153 __FUNCTION__, i);
1154 }
1155 }
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001156 delete[] anwBuffers;
1157 delete[] buffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001158
1159 return res;
1160}
1161
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001162status_t Camera2Device::StreamAdapter::release() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001163 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001164 status_t res;
Eino-Ville Talvala02f84572013-04-23 15:16:57 -07001165 ALOGV("%s: Releasing stream %d (%d x %d, format %d)", __FUNCTION__, mId,
1166 mWidth, mHeight, mFormat);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001167 if (mState >= ALLOCATED) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001168 res = mHal2Device->ops->release_stream(mHal2Device, mId);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001169 if (res != OK) {
1170 ALOGE("%s: Unable to release stream %d",
1171 __FUNCTION__, mId);
1172 return res;
1173 }
1174 }
1175 if (mState >= CONNECTED) {
1176 res = native_window_api_disconnect(mConsumerInterface.get(),
1177 NATIVE_WINDOW_API_CAMERA);
Igor Murashkina1e5dcc2012-10-02 15:21:31 -07001178
1179 /* this is not an error. if client calling process dies,
1180 the window will also die and all calls to it will return
1181 DEAD_OBJECT, thus it's already "disconnected" */
1182 if (res == DEAD_OBJECT) {
1183 ALOGW("%s: While disconnecting stream %d from native window, the"
1184 " native window died from under us", __FUNCTION__, mId);
1185 }
1186 else if (res != OK) {
1187 ALOGE("%s: Unable to disconnect stream %d from native window (error %d %s)",
1188 __FUNCTION__, mId, res, strerror(-res));
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001189 return res;
1190 }
1191 }
1192 mId = -1;
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001193 mState = RELEASED;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001194 return OK;
1195}
1196
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -07001197status_t Camera2Device::StreamAdapter::setTransform(int transform) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001198 ATRACE_CALL();
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -07001199 status_t res;
1200 if (mState < CONNECTED) {
1201 ALOGE("%s: Cannot set transform on unconnected stream", __FUNCTION__);
1202 return INVALID_OPERATION;
1203 }
1204 res = native_window_set_buffers_transform(mConsumerInterface.get(),
1205 transform);
1206 if (res != OK) {
1207 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
1208 __FUNCTION__, transform, strerror(-res), res);
1209 }
1210 return res;
1211}
1212
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001213status_t Camera2Device::StreamAdapter::dump(int fd,
Igor Murashkinebe3f692012-10-12 16:56:11 -07001214 const Vector<String16>& /*args*/) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001215 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001216 String8 result = String8::format(" Stream %d: %d x %d, format 0x%x\n",
1217 mId, mWidth, mHeight, mFormat);
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001218 result.appendFormat(" size %zu, usage 0x%x, requested format 0x%x\n",
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001219 mSize, mUsage, mFormatRequested);
1220 result.appendFormat(" total buffers: %d, dequeued buffers: %d\n",
1221 mTotalBuffers, mActiveBuffers);
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001222 result.appendFormat(" frame count: %d, last timestamp %" PRId64 "\n",
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001223 mFrameCount, mLastTimestamp);
1224 write(fd, result.string(), result.size());
1225 return OK;
1226}
1227
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001228const camera2_stream_ops *Camera2Device::StreamAdapter::getStreamOps() {
1229 return static_cast<camera2_stream_ops *>(this);
1230}
1231
1232ANativeWindow* Camera2Device::StreamAdapter::toANW(
1233 const camera2_stream_ops_t *w) {
1234 return static_cast<const StreamAdapter*>(w)->mConsumerInterface.get();
1235}
1236
1237int Camera2Device::StreamAdapter::dequeue_buffer(const camera2_stream_ops_t *w,
1238 buffer_handle_t** buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001239 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001240 int res;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001241 StreamAdapter* stream =
1242 const_cast<StreamAdapter*>(static_cast<const StreamAdapter*>(w));
1243 if (stream->mState != ACTIVE) {
1244 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, stream->mState);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001245 return INVALID_OPERATION;
1246 }
1247
1248 ANativeWindow *a = toANW(w);
1249 ANativeWindowBuffer* anb;
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001250 res = native_window_dequeue_buffer_and_wait(a, &anb);
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001251 if (res != OK) {
1252 ALOGE("Stream %d dequeue: Error from native_window: %s (%d)", stream->mId,
1253 strerror(-res), res);
1254 return res;
1255 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001256
1257 *buffer = &(anb->handle);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001258 stream->mActiveBuffers++;
1259
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001260 ALOGVV("Stream %d dequeue: Buffer %p dequeued", stream->mId, (void*)(**buffer));
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001261 return res;
1262}
1263
1264int Camera2Device::StreamAdapter::enqueue_buffer(const camera2_stream_ops_t* w,
1265 int64_t timestamp,
1266 buffer_handle_t* buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001267 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001268 StreamAdapter *stream =
1269 const_cast<StreamAdapter*>(static_cast<const StreamAdapter*>(w));
Eino-Ville Talvala228a5382012-08-13 12:16:06 -07001270 stream->mFrameCount++;
1271 ALOGVV("Stream %d enqueue: Frame %d (%p) captured at %lld ns",
James Donga289bf62012-09-05 16:46:36 -07001272 stream->mId, stream->mFrameCount, (void*)(*buffer), timestamp);
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001273 int state = stream->mState;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001274 if (state != ACTIVE) {
1275 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
1276 return INVALID_OPERATION;
1277 }
1278 ANativeWindow *a = toANW(w);
1279 status_t err;
Eino-Ville Talvala228a5382012-08-13 12:16:06 -07001280
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001281 err = native_window_set_buffers_timestamp(a, timestamp);
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001282 if (err != OK) {
1283 ALOGE("%s: Error setting timestamp on native window: %s (%d)",
1284 __FUNCTION__, strerror(-err), err);
1285 return err;
1286 }
1287 err = a->queueBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001288 container_of(buffer, ANativeWindowBuffer, handle), -1);
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001289 if (err != OK) {
1290 ALOGE("%s: Error queueing buffer to native window: %s (%d)",
1291 __FUNCTION__, strerror(-err), err);
James Dong31d377b2012-08-09 17:43:46 -07001292 return err;
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001293 }
James Dong31d377b2012-08-09 17:43:46 -07001294
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001295 stream->mActiveBuffers--;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001296 stream->mLastTimestamp = timestamp;
James Dong31d377b2012-08-09 17:43:46 -07001297 return OK;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001298}
1299
1300int Camera2Device::StreamAdapter::cancel_buffer(const camera2_stream_ops_t* w,
1301 buffer_handle_t* buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001302 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001303 StreamAdapter *stream =
1304 const_cast<StreamAdapter*>(static_cast<const StreamAdapter*>(w));
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001305 ALOGVV("Stream %d cancel: Buffer %p",
1306 stream->mId, (void*)(*buffer));
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001307 if (stream->mState != ACTIVE) {
1308 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, stream->mState);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001309 return INVALID_OPERATION;
1310 }
James Dong31d377b2012-08-09 17:43:46 -07001311
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001312 ANativeWindow *a = toANW(w);
James Dong31d377b2012-08-09 17:43:46 -07001313 int err = a->cancelBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001314 container_of(buffer, ANativeWindowBuffer, handle), -1);
James Dong31d377b2012-08-09 17:43:46 -07001315 if (err != OK) {
1316 ALOGE("%s: Error canceling buffer to native window: %s (%d)",
1317 __FUNCTION__, strerror(-err), err);
1318 return err;
1319 }
1320
1321 stream->mActiveBuffers--;
1322 return OK;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001323}
1324
1325int Camera2Device::StreamAdapter::set_crop(const camera2_stream_ops_t* w,
1326 int left, int top, int right, int bottom) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001327 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001328 int state = static_cast<const StreamAdapter*>(w)->mState;
1329 if (state != ACTIVE) {
1330 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
1331 return INVALID_OPERATION;
1332 }
1333 ANativeWindow *a = toANW(w);
1334 android_native_rect_t crop = { left, top, right, bottom };
1335 return native_window_set_crop(a, &crop);
1336}
1337
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001338/**
1339 * Camera2Device::ReprocessStreamAdapter
1340 */
1341
1342#ifndef container_of
1343#define container_of(ptr, type, member) \
1344 (type *)((char*)(ptr) - offsetof(type, member))
1345#endif
1346
1347Camera2Device::ReprocessStreamAdapter::ReprocessStreamAdapter(camera2_device_t *d):
1348 mState(RELEASED),
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001349 mHal2Device(d),
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001350 mId(-1),
1351 mWidth(0), mHeight(0), mFormat(0),
1352 mActiveBuffers(0),
1353 mFrameCount(0)
1354{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001355 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001356 camera2_stream_in_ops::acquire_buffer = acquire_buffer;
1357 camera2_stream_in_ops::release_buffer = release_buffer;
1358}
1359
1360Camera2Device::ReprocessStreamAdapter::~ReprocessStreamAdapter() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001361 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001362 if (mState != RELEASED) {
1363 release();
1364 }
1365}
1366
1367status_t Camera2Device::ReprocessStreamAdapter::connectToDevice(
1368 const sp<StreamAdapter> &outputStream) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001369 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001370 status_t res;
1371 ALOGV("%s: E", __FUNCTION__);
1372
1373 if (mState != RELEASED) return INVALID_OPERATION;
1374 if (outputStream == NULL) {
1375 ALOGE("%s: Null base stream passed to reprocess stream adapter",
1376 __FUNCTION__);
1377 return BAD_VALUE;
1378 }
1379
1380 mBaseStream = outputStream;
1381 mWidth = outputStream->getWidth();
1382 mHeight = outputStream->getHeight();
1383 mFormat = outputStream->getFormat();
1384
1385 ALOGV("%s: New reprocess stream parameters %d x %d, format 0x%x",
1386 __FUNCTION__, mWidth, mHeight, mFormat);
1387
1388 // Allocate device-side stream interface
1389
1390 uint32_t id;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001391 res = mHal2Device->ops->allocate_reprocess_stream_from_stream(mHal2Device,
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001392 outputStream->getId(), getStreamOps(),
1393 &id);
1394 if (res != OK) {
1395 ALOGE("%s: Device reprocess stream allocation failed: %s (%d)",
1396 __FUNCTION__, strerror(-res), res);
1397 return res;
1398 }
1399
1400 ALOGV("%s: Allocated reprocess stream id %d based on stream %d",
1401 __FUNCTION__, id, outputStream->getId());
1402
1403 mId = id;
1404
1405 mState = ACTIVE;
1406
1407 return OK;
1408}
1409
1410status_t Camera2Device::ReprocessStreamAdapter::release() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001411 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001412 status_t res;
1413 ALOGV("%s: Releasing stream %d", __FUNCTION__, mId);
1414 if (mState >= ACTIVE) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001415 res = mHal2Device->ops->release_reprocess_stream(mHal2Device, mId);
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001416 if (res != OK) {
1417 ALOGE("%s: Unable to release stream %d",
1418 __FUNCTION__, mId);
1419 return res;
1420 }
1421 }
1422
1423 List<QueueEntry>::iterator s;
1424 for (s = mQueue.begin(); s != mQueue.end(); s++) {
1425 sp<BufferReleasedListener> listener = s->releaseListener.promote();
1426 if (listener != 0) listener->onBufferReleased(s->handle);
1427 }
1428 for (s = mInFlightQueue.begin(); s != mInFlightQueue.end(); s++) {
1429 sp<BufferReleasedListener> listener = s->releaseListener.promote();
1430 if (listener != 0) listener->onBufferReleased(s->handle);
1431 }
1432 mQueue.clear();
1433 mInFlightQueue.clear();
1434
1435 mState = RELEASED;
1436 return OK;
1437}
1438
1439status_t Camera2Device::ReprocessStreamAdapter::pushIntoStream(
1440 buffer_handle_t *handle, const wp<BufferReleasedListener> &releaseListener) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001441 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001442 // TODO: Some error checking here would be nice
1443 ALOGV("%s: Pushing buffer %p to stream", __FUNCTION__, (void*)(*handle));
1444
1445 QueueEntry entry;
1446 entry.handle = handle;
1447 entry.releaseListener = releaseListener;
1448 mQueue.push_back(entry);
1449 return OK;
1450}
1451
1452status_t Camera2Device::ReprocessStreamAdapter::dump(int fd,
Igor Murashkinebe3f692012-10-12 16:56:11 -07001453 const Vector<String16>& /*args*/) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001454 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001455 String8 result =
1456 String8::format(" Reprocess stream %d: %d x %d, fmt 0x%x\n",
1457 mId, mWidth, mHeight, mFormat);
1458 result.appendFormat(" acquired buffers: %d\n",
1459 mActiveBuffers);
1460 result.appendFormat(" frame count: %d\n",
1461 mFrameCount);
1462 write(fd, result.string(), result.size());
1463 return OK;
1464}
1465
1466const camera2_stream_in_ops *Camera2Device::ReprocessStreamAdapter::getStreamOps() {
1467 return static_cast<camera2_stream_in_ops *>(this);
1468}
1469
1470int Camera2Device::ReprocessStreamAdapter::acquire_buffer(
1471 const camera2_stream_in_ops_t *w,
1472 buffer_handle_t** buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001473 ATRACE_CALL();
Igor Murashkinebe3f692012-10-12 16:56:11 -07001474
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001475 ReprocessStreamAdapter* stream =
1476 const_cast<ReprocessStreamAdapter*>(
1477 static_cast<const ReprocessStreamAdapter*>(w));
1478 if (stream->mState != ACTIVE) {
1479 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, stream->mState);
1480 return INVALID_OPERATION;
1481 }
1482
1483 if (stream->mQueue.empty()) {
1484 *buffer = NULL;
1485 return OK;
1486 }
1487
1488 QueueEntry &entry = *(stream->mQueue.begin());
1489
1490 *buffer = entry.handle;
1491
1492 stream->mInFlightQueue.push_back(entry);
1493 stream->mQueue.erase(stream->mQueue.begin());
1494
1495 stream->mActiveBuffers++;
1496
1497 ALOGV("Stream %d acquire: Buffer %p acquired", stream->mId,
1498 (void*)(**buffer));
1499 return OK;
1500}
1501
1502int Camera2Device::ReprocessStreamAdapter::release_buffer(
1503 const camera2_stream_in_ops_t* w,
1504 buffer_handle_t* buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001505 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001506 ReprocessStreamAdapter *stream =
1507 const_cast<ReprocessStreamAdapter*>(
1508 static_cast<const ReprocessStreamAdapter*>(w) );
1509 stream->mFrameCount++;
1510 ALOGV("Reprocess stream %d release: Frame %d (%p)",
1511 stream->mId, stream->mFrameCount, (void*)*buffer);
1512 int state = stream->mState;
1513 if (state != ACTIVE) {
1514 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
1515 return INVALID_OPERATION;
1516 }
1517 stream->mActiveBuffers--;
1518
1519 List<QueueEntry>::iterator s;
1520 for (s = stream->mInFlightQueue.begin(); s != stream->mInFlightQueue.end(); s++) {
1521 if ( s->handle == buffer ) break;
1522 }
1523 if (s == stream->mInFlightQueue.end()) {
1524 ALOGE("%s: Can't find buffer %p in in-flight list!", __FUNCTION__,
1525 buffer);
1526 return INVALID_OPERATION;
1527 }
1528
1529 sp<BufferReleasedListener> listener = s->releaseListener.promote();
1530 if (listener != 0) {
1531 listener->onBufferReleased(s->handle);
1532 } else {
1533 ALOGE("%s: Can't free buffer - missing listener", __FUNCTION__);
1534 }
1535 stream->mInFlightQueue.erase(s);
1536
1537 return OK;
1538}
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -07001539
1540}; // namespace android