blob: 921c8fcd1c62202c44bf346318733ac2e51228d0 [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 Talvala61ab9f92012-05-17 10:30:54 -070037 mDevice(NULL)
38{
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
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070050status_t Camera2Device::initialize(camera_module_t *module)
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070051{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -070052 ATRACE_CALL();
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -070053 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070054 if (mDevice != NULL) {
55 ALOGE("%s: Already initialized!", __FUNCTION__);
56 return INVALID_OPERATION;
57 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070058
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070059 status_t res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070060 char name[10];
61 snprintf(name, sizeof(name), "%d", mId);
62
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070063 camera2_device_t *device;
64
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070065 res = module->common.methods->open(&module->common, name,
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070066 reinterpret_cast<hw_device_t**>(&device));
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070067
68 if (res != OK) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070069 ALOGE("%s: Could not open camera %d: %s (%d)", __FUNCTION__,
70 mId, strerror(-res), res);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070071 return res;
72 }
73
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070074 if (device->common.version != CAMERA_DEVICE_API_VERSION_2_0) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070075 ALOGE("%s: Could not open camera %d: "
76 "Camera device is not version %x, reports %x instead",
77 __FUNCTION__, mId, CAMERA_DEVICE_API_VERSION_2_0,
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070078 device->common.version);
79 device->common.close(&device->common);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070080 return BAD_VALUE;
81 }
82
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070083 camera_info info;
84 res = module->get_camera_info(mId, &info);
85 if (res != OK ) return res;
86
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070087 if (info.device_version != device->common.version) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070088 ALOGE("%s: HAL reporting mismatched camera_info version (%x)"
89 " and device version (%x).", __FUNCTION__,
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070090 device->common.version, info.device_version);
91 device->common.close(&device->common);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070092 return BAD_VALUE;
93 }
94
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070095 res = mRequestQueue.setConsumerDevice(device);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070096 if (res != OK) {
97 ALOGE("%s: Camera %d: Unable to connect request queue to device: %s (%d)",
98 __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070099 device->common.close(&device->common);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700100 return res;
101 }
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700102 res = mFrameQueue.setProducerDevice(device);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700103 if (res != OK) {
104 ALOGE("%s: Camera %d: Unable to connect frame queue to device: %s (%d)",
105 __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700106 device->common.close(&device->common);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700107 return res;
108 }
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700109
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700110 res = device->ops->get_metadata_vendor_tag_ops(device, &mVendorTagOps);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700111 if (res != OK ) {
112 ALOGE("%s: Camera %d: Unable to retrieve tag ops from device: %s (%d)",
113 __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700114 device->common.close(&device->common);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700115 return res;
116 }
Shuzhen Wang314079e2012-08-31 10:24:22 -0700117 res = set_camera_metadata_vendor_tag_ops(mVendorTagOps);
118 if (res != OK) {
119 ALOGE("%s: Camera %d: Unable to set tag ops: %s (%d)",
120 __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700121 device->common.close(&device->common);
Shuzhen Wang314079e2012-08-31 10:24:22 -0700122 return res;
123 }
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700124 res = device->ops->set_notify_callback(device, notificationCallback,
125 NULL);
126 if (res != OK) {
127 ALOGE("%s: Camera %d: Unable to initialize notification callback!",
128 __FUNCTION__, mId);
129 device->common.close(&device->common);
130 return res;
131 }
132
133 mDeviceInfo = info.static_camera_characteristics;
134 mDevice = device;
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700135
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700136 return OK;
137}
138
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700139status_t Camera2Device::disconnect() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700140 ATRACE_CALL();
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700141 status_t res = OK;
142 if (mDevice) {
143 ALOGV("%s: Closing device for camera %d", __FUNCTION__, mId);
144
145 int inProgressCount = mDevice->ops->get_in_progress_count(mDevice);
146 if (inProgressCount > 0) {
147 ALOGW("%s: Closing camera device %d with %d requests in flight!",
148 __FUNCTION__, mId, inProgressCount);
149 }
Eino-Ville Talvalac62bb782012-09-24 13:44:07 -0700150 mReprocessStreams.clear();
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700151 mStreams.clear();
152 res = mDevice->common.close(&mDevice->common);
153 if (res != OK) {
154 ALOGE("%s: Could not close camera %d: %s (%d)",
155 __FUNCTION__,
156 mId, strerror(-res), res);
157 }
158 mDevice = NULL;
159 ALOGV("%s: Shutdown complete", __FUNCTION__);
160 }
161 return res;
162}
163
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700164status_t Camera2Device::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700165 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700166 String8 result;
Eino-Ville Talvala97197152012-08-06 14:25:19 -0700167 int detailLevel = 0;
168 int n = args.size();
169 String16 detailOption("-d");
170 for (int i = 0; i + 1 < n; i++) {
171 if (args[i] == detailOption) {
172 String8 levelStr(args[i+1]);
173 detailLevel = atoi(levelStr.string());
174 }
175 }
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700176
Eino-Ville Talvala603b12e2012-08-08 09:25:58 -0700177 result.appendFormat(" Camera2Device[%d] dump (detail level %d):\n",
178 mId, detailLevel);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700179
Eino-Ville Talvala97197152012-08-06 14:25:19 -0700180 if (detailLevel > 0) {
181 result = " Request queue contents:\n";
182 write(fd, result.string(), result.size());
183 mRequestQueue.dump(fd, args);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700184
Eino-Ville Talvala97197152012-08-06 14:25:19 -0700185 result = " Frame queue contents:\n";
186 write(fd, result.string(), result.size());
187 mFrameQueue.dump(fd, args);
188 }
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700189
190 result = " Active streams:\n";
191 write(fd, result.string(), result.size());
192 for (StreamList::iterator s = mStreams.begin(); s != mStreams.end(); s++) {
193 (*s)->dump(fd, args);
194 }
195
196 result = " HAL device dump:\n";
197 write(fd, result.string(), result.size());
198
199 status_t res;
200 res = mDevice->ops->dump(mDevice, fd);
201
202 return res;
203}
204
Igor Murashkinbd02dd12013-02-13 15:53:56 -0800205const CameraMetadata& Camera2Device::info() const {
Eino-Ville Talvala9e4c3db2012-07-20 11:07:52 -0700206 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700207
208 return mDeviceInfo;
209}
210
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700211status_t Camera2Device::capture(CameraMetadata &request) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700212 ATRACE_CALL();
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700213 ALOGV("%s: E", __FUNCTION__);
214
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700215 mRequestQueue.enqueue(request.release());
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700216 return OK;
217}
218
219
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700220status_t Camera2Device::setStreamingRequest(const CameraMetadata &request) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700221 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700222 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700223 CameraMetadata streamRequest(request);
224 return mRequestQueue.setStreamSlot(streamRequest.release());
225}
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700226
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700227status_t Camera2Device::clearStreamingRequest() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700228 ATRACE_CALL();
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700229 return mRequestQueue.setStreamSlot(NULL);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700230}
231
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700232status_t Camera2Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
233 ATRACE_CALL();
234 return mRequestQueue.waitForDequeue(requestId, timeout);
235}
236
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700237status_t Camera2Device::createStream(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700238 uint32_t width, uint32_t height, int format, size_t size, int *id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700239 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700240 status_t res;
241 ALOGV("%s: E", __FUNCTION__);
242
243 sp<StreamAdapter> stream = new StreamAdapter(mDevice);
244
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700245 res = stream->connectToDevice(consumer, width, height, format, size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700246 if (res != OK) {
247 ALOGE("%s: Camera %d: Unable to create stream (%d x %d, format %x):"
248 "%s (%d)",
249 __FUNCTION__, mId, width, height, format, strerror(-res), res);
250 return res;
251 }
252
253 *id = stream->getId();
254
255 mStreams.push_back(stream);
256 return OK;
257}
258
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700259status_t Camera2Device::createReprocessStreamFromStream(int outputId, int *id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700260 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700261 status_t res;
262 ALOGV("%s: E", __FUNCTION__);
263
264 bool found = false;
265 StreamList::iterator streamI;
266 for (streamI = mStreams.begin();
267 streamI != mStreams.end(); streamI++) {
268 if ((*streamI)->getId() == outputId) {
269 found = true;
270 break;
271 }
272 }
273 if (!found) {
274 ALOGE("%s: Camera %d: Output stream %d doesn't exist; can't create "
275 "reprocess stream from it!", __FUNCTION__, mId, outputId);
276 return BAD_VALUE;
277 }
278
279 sp<ReprocessStreamAdapter> stream = new ReprocessStreamAdapter(mDevice);
280
281 res = stream->connectToDevice((*streamI));
282 if (res != OK) {
283 ALOGE("%s: Camera %d: Unable to create reprocessing stream from "\
284 "stream %d: %s (%d)", __FUNCTION__, mId, outputId,
285 strerror(-res), res);
286 return res;
287 }
288
289 *id = stream->getId();
290
291 mReprocessStreams.push_back(stream);
292 return OK;
293}
294
295
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700296status_t Camera2Device::getStreamInfo(int id,
297 uint32_t *width, uint32_t *height, uint32_t *format) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700298 ATRACE_CALL();
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700299 ALOGV("%s: E", __FUNCTION__);
300 bool found = false;
301 StreamList::iterator streamI;
302 for (streamI = mStreams.begin();
303 streamI != mStreams.end(); streamI++) {
304 if ((*streamI)->getId() == id) {
305 found = true;
306 break;
307 }
308 }
309 if (!found) {
310 ALOGE("%s: Camera %d: Stream %d does not exist",
311 __FUNCTION__, mId, id);
312 return BAD_VALUE;
313 }
314
315 if (width) *width = (*streamI)->getWidth();
316 if (height) *height = (*streamI)->getHeight();
317 if (format) *format = (*streamI)->getFormat();
318
319 return OK;
320}
321
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700322status_t Camera2Device::setStreamTransform(int id,
323 int transform) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700324 ATRACE_CALL();
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700325 ALOGV("%s: E", __FUNCTION__);
326 bool found = false;
327 StreamList::iterator streamI;
328 for (streamI = mStreams.begin();
329 streamI != mStreams.end(); streamI++) {
330 if ((*streamI)->getId() == id) {
331 found = true;
332 break;
333 }
334 }
335 if (!found) {
336 ALOGE("%s: Camera %d: Stream %d does not exist",
337 __FUNCTION__, mId, id);
338 return BAD_VALUE;
339 }
340
341 return (*streamI)->setTransform(transform);
342}
343
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700344status_t Camera2Device::deleteStream(int id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700345 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700346 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700347 bool found = false;
348 for (StreamList::iterator streamI = mStreams.begin();
349 streamI != mStreams.end(); streamI++) {
350 if ((*streamI)->getId() == id) {
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700351 status_t res = (*streamI)->release();
Eino-Ville Talvala4ecfec32012-06-12 17:13:48 -0700352 if (res != OK) {
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700353 ALOGE("%s: Unable to release stream %d from HAL device: "
Eino-Ville Talvala4ecfec32012-06-12 17:13:48 -0700354 "%s (%d)", __FUNCTION__, id, strerror(-res), res);
355 return res;
356 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700357 mStreams.erase(streamI);
358 found = true;
359 break;
360 }
361 }
362 if (!found) {
363 ALOGE("%s: Camera %d: Unable to find stream %d to delete",
364 __FUNCTION__, mId, id);
365 return BAD_VALUE;
366 }
367 return OK;
368}
369
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700370status_t Camera2Device::deleteReprocessStream(int id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700371 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700372 ALOGV("%s: E", __FUNCTION__);
373 bool found = false;
374 for (ReprocessStreamList::iterator streamI = mReprocessStreams.begin();
375 streamI != mReprocessStreams.end(); streamI++) {
376 if ((*streamI)->getId() == id) {
377 status_t res = (*streamI)->release();
378 if (res != OK) {
379 ALOGE("%s: Unable to release reprocess stream %d from "
380 "HAL device: %s (%d)", __FUNCTION__, id,
381 strerror(-res), res);
382 return res;
383 }
384 mReprocessStreams.erase(streamI);
385 found = true;
386 break;
387 }
388 }
389 if (!found) {
390 ALOGE("%s: Camera %d: Unable to find stream %d to delete",
391 __FUNCTION__, mId, id);
392 return BAD_VALUE;
393 }
394 return OK;
395}
396
397
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700398status_t Camera2Device::createDefaultRequest(int templateId,
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700399 CameraMetadata *request) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700400 ATRACE_CALL();
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700401 status_t err;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700402 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700403 camera_metadata_t *rawRequest;
404 err = mDevice->ops->construct_default_request(
405 mDevice, templateId, &rawRequest);
406 request->acquire(rawRequest);
407 return err;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700408}
409
410status_t Camera2Device::waitUntilDrained() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700411 ATRACE_CALL();
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700412 static const uint32_t kSleepTime = 50000; // 50 ms
413 static const uint32_t kMaxSleepTime = 10000000; // 10 s
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700414 ALOGV("%s: Camera %d: Starting wait", __FUNCTION__, mId);
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700415 if (mRequestQueue.getBufferCount() ==
416 CAMERA2_REQUEST_QUEUE_IS_BOTTOMLESS) return INVALID_OPERATION;
417
418 // TODO: Set up notifications from HAL, instead of sleeping here
419 uint32_t totalTime = 0;
420 while (mDevice->ops->get_in_progress_count(mDevice) > 0) {
421 usleep(kSleepTime);
422 totalTime += kSleepTime;
423 if (totalTime > kMaxSleepTime) {
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700424 ALOGE("%s: Waited %d us, %d requests still in flight", __FUNCTION__,
425 mDevice->ops->get_in_progress_count(mDevice), totalTime);
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700426 return TIMED_OUT;
427 }
428 }
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -0700429 ALOGV("%s: Camera %d: HAL is idle", __FUNCTION__, mId);
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700430 return OK;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700431}
432
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700433status_t Camera2Device::setNotifyCallback(NotificationListener *listener) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700434 ATRACE_CALL();
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700435 status_t res;
436 res = mDevice->ops->set_notify_callback(mDevice, notificationCallback,
437 reinterpret_cast<void*>(listener) );
438 if (res != OK) {
439 ALOGE("%s: Unable to set notification callback!", __FUNCTION__);
440 }
441 return res;
442}
443
444void Camera2Device::notificationCallback(int32_t msg_type,
445 int32_t ext1,
446 int32_t ext2,
447 int32_t ext3,
448 void *user) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700449 ATRACE_CALL();
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700450 NotificationListener *listener = reinterpret_cast<NotificationListener*>(user);
451 ALOGV("%s: Notification %d, arguments %d, %d, %d", __FUNCTION__, msg_type,
452 ext1, ext2, ext3);
453 if (listener != NULL) {
454 switch (msg_type) {
455 case CAMERA2_MSG_ERROR:
456 listener->notifyError(ext1, ext2, ext3);
457 break;
458 case CAMERA2_MSG_SHUTTER: {
459 nsecs_t timestamp = (nsecs_t)ext2 | ((nsecs_t)(ext3) << 32 );
460 listener->notifyShutter(ext1, timestamp);
461 break;
462 }
463 case CAMERA2_MSG_AUTOFOCUS:
464 listener->notifyAutoFocus(ext1, ext2);
465 break;
466 case CAMERA2_MSG_AUTOEXPOSURE:
467 listener->notifyAutoExposure(ext1, ext2);
468 break;
469 case CAMERA2_MSG_AUTOWB:
470 listener->notifyAutoWhitebalance(ext1, ext2);
471 break;
472 default:
473 ALOGE("%s: Unknown notification %d (arguments %d, %d, %d)!",
474 __FUNCTION__, msg_type, ext1, ext2, ext3);
475 }
476 }
477}
478
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -0700479status_t Camera2Device::waitForNextFrame(nsecs_t timeout) {
480 return mFrameQueue.waitForBuffer(timeout);
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700481}
482
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700483status_t Camera2Device::getNextFrame(CameraMetadata *frame) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700484 ATRACE_CALL();
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700485 status_t res;
486 camera_metadata_t *rawFrame;
487 res = mFrameQueue.dequeue(&rawFrame);
488 if (rawFrame == NULL) {
489 return NOT_ENOUGH_DATA;
490 } else if (res == OK) {
491 frame->acquire(rawFrame);
492 }
493 return res;
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700494}
495
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700496status_t Camera2Device::triggerAutofocus(uint32_t id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700497 ATRACE_CALL();
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700498 status_t res;
499 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
500 res = mDevice->ops->trigger_action(mDevice,
501 CAMERA2_TRIGGER_AUTOFOCUS, id, 0);
502 if (res != OK) {
503 ALOGE("%s: Error triggering autofocus (id %d)",
504 __FUNCTION__, id);
505 }
506 return res;
507}
508
509status_t Camera2Device::triggerCancelAutofocus(uint32_t id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700510 ATRACE_CALL();
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700511 status_t res;
512 ALOGV("%s: Canceling autofocus, id %d", __FUNCTION__, id);
513 res = mDevice->ops->trigger_action(mDevice,
514 CAMERA2_TRIGGER_CANCEL_AUTOFOCUS, id, 0);
515 if (res != OK) {
516 ALOGE("%s: Error canceling autofocus (id %d)",
517 __FUNCTION__, id);
518 }
519 return res;
520}
521
522status_t Camera2Device::triggerPrecaptureMetering(uint32_t id) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700523 ATRACE_CALL();
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700524 status_t res;
525 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
526 res = mDevice->ops->trigger_action(mDevice,
527 CAMERA2_TRIGGER_PRECAPTURE_METERING, id, 0);
528 if (res != OK) {
529 ALOGE("%s: Error triggering precapture metering (id %d)",
530 __FUNCTION__, id);
531 }
532 return res;
533}
534
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700535status_t Camera2Device::pushReprocessBuffer(int reprocessStreamId,
536 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700537 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700538 ALOGV("%s: E", __FUNCTION__);
539 bool found = false;
540 status_t res = OK;
541 for (ReprocessStreamList::iterator streamI = mReprocessStreams.begin();
542 streamI != mReprocessStreams.end(); streamI++) {
543 if ((*streamI)->getId() == reprocessStreamId) {
544 res = (*streamI)->pushIntoStream(buffer, listener);
545 if (res != OK) {
546 ALOGE("%s: Unable to push buffer to reprocess stream %d: %s (%d)",
547 __FUNCTION__, reprocessStreamId, strerror(-res), res);
548 return res;
549 }
550 found = true;
551 break;
552 }
553 }
554 if (!found) {
555 ALOGE("%s: Camera %d: Unable to find reprocess stream %d",
556 __FUNCTION__, mId, reprocessStreamId);
557 res = BAD_VALUE;
558 }
559 return res;
560}
561
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700562/**
563 * Camera2Device::NotificationListener
564 */
565
566Camera2Device::NotificationListener::~NotificationListener() {
567}
568
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700569/**
570 * Camera2Device::MetadataQueue
571 */
572
573Camera2Device::MetadataQueue::MetadataQueue():
574 mDevice(NULL),
575 mFrameCount(0),
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700576 mLatestRequestId(0),
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700577 mCount(0),
578 mStreamSlotCount(0),
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -0700579 mSignalConsumer(true)
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700580{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700581 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700582 camera2_request_queue_src_ops::dequeue_request = consumer_dequeue;
583 camera2_request_queue_src_ops::request_count = consumer_buffer_count;
584 camera2_request_queue_src_ops::free_request = consumer_free;
585
586 camera2_frame_queue_dst_ops::dequeue_frame = producer_dequeue;
587 camera2_frame_queue_dst_ops::cancel_frame = producer_cancel;
588 camera2_frame_queue_dst_ops::enqueue_frame = producer_enqueue;
589}
590
591Camera2Device::MetadataQueue::~MetadataQueue() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700592 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700593 Mutex::Autolock l(mMutex);
594 freeBuffers(mEntries.begin(), mEntries.end());
595 freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
596}
597
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700598// Connect to camera2 HAL as consumer (input requests/reprocessing)
599status_t Camera2Device::MetadataQueue::setConsumerDevice(camera2_device_t *d) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700600 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700601 status_t res;
602 res = d->ops->set_request_queue_src_ops(d,
603 this);
604 if (res != OK) return res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700605 mDevice = d;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700606 return OK;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700607}
608
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700609status_t Camera2Device::MetadataQueue::setProducerDevice(camera2_device_t *d) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700610 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700611 status_t res;
612 res = d->ops->set_frame_queue_dst_ops(d,
613 this);
614 return res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700615}
616
617// Real interfaces
618status_t Camera2Device::MetadataQueue::enqueue(camera_metadata_t *buf) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700619 ATRACE_CALL();
Eino-Ville Talvala2c08dc62012-06-15 12:49:21 -0700620 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700621 Mutex::Autolock l(mMutex);
622
623 mCount++;
624 mEntries.push_back(buf);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700625
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700626 return signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700627}
628
629int Camera2Device::MetadataQueue::getBufferCount() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700630 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700631 Mutex::Autolock l(mMutex);
632 if (mStreamSlotCount > 0) {
633 return CAMERA2_REQUEST_QUEUE_IS_BOTTOMLESS;
634 }
635 return mCount;
636}
637
638status_t Camera2Device::MetadataQueue::dequeue(camera_metadata_t **buf,
639 bool incrementCount)
640{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700641 ATRACE_CALL();
Eino-Ville Talvala2c08dc62012-06-15 12:49:21 -0700642 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700643 status_t res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700644 Mutex::Autolock l(mMutex);
645
646 if (mCount == 0) {
647 if (mStreamSlotCount == 0) {
Eino-Ville Talvala2c08dc62012-06-15 12:49:21 -0700648 ALOGVV("%s: Empty", __FUNCTION__);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700649 *buf = NULL;
650 mSignalConsumer = true;
651 return OK;
652 }
Eino-Ville Talvala2c08dc62012-06-15 12:49:21 -0700653 ALOGVV("%s: Streaming %d frames to queue", __FUNCTION__,
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700654 mStreamSlotCount);
655
656 for (List<camera_metadata_t*>::iterator slotEntry = mStreamSlot.begin();
657 slotEntry != mStreamSlot.end();
658 slotEntry++ ) {
659 size_t entries = get_camera_metadata_entry_count(*slotEntry);
660 size_t dataBytes = get_camera_metadata_data_count(*slotEntry);
661
662 camera_metadata_t *copy =
663 allocate_camera_metadata(entries, dataBytes);
664 append_camera_metadata(copy, *slotEntry);
665 mEntries.push_back(copy);
666 }
667 mCount = mStreamSlotCount;
668 }
Eino-Ville Talvala2c08dc62012-06-15 12:49:21 -0700669 ALOGVV("MetadataQueue: deque (%d buffers)", mCount);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700670 camera_metadata_t *b = *(mEntries.begin());
671 mEntries.erase(mEntries.begin());
672
673 if (incrementCount) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700674 ATRACE_INT("cam2_request", mFrameCount);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700675 camera_metadata_entry_t frameCount;
676 res = find_camera_metadata_entry(b,
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700677 ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700678 &frameCount);
679 if (res != OK) {
680 ALOGE("%s: Unable to add frame count: %s (%d)",
681 __FUNCTION__, strerror(-res), res);
682 } else {
683 *frameCount.data.i32 = mFrameCount;
684 }
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700685 mFrameCount++;
686 }
687
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700688 // Check for request ID, and if present, signal waiters.
689 camera_metadata_entry_t requestId;
690 res = find_camera_metadata_entry(b,
691 ANDROID_REQUEST_ID,
692 &requestId);
693 if (res == OK) {
694 mLatestRequestId = requestId.data.i32[0];
695 mNewRequestId.signal();
696 }
697
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700698 *buf = b;
699 mCount--;
700
701 return OK;
702}
703
704status_t Camera2Device::MetadataQueue::waitForBuffer(nsecs_t timeout)
705{
706 Mutex::Autolock l(mMutex);
707 status_t res;
708 while (mCount == 0) {
709 res = notEmpty.waitRelative(mMutex,timeout);
710 if (res != OK) return res;
711 }
712 return OK;
713}
714
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700715status_t Camera2Device::MetadataQueue::waitForDequeue(int32_t id,
716 nsecs_t timeout) {
717 Mutex::Autolock l(mMutex);
718 status_t res;
719 while (mLatestRequestId != id) {
720 nsecs_t startTime = systemTime();
721
722 res = mNewRequestId.waitRelative(mMutex, timeout);
723 if (res != OK) return res;
724
725 timeout -= (systemTime() - startTime);
726 }
727
728 return OK;
729}
730
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700731status_t Camera2Device::MetadataQueue::setStreamSlot(camera_metadata_t *buf)
732{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700733 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700734 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700735 Mutex::Autolock l(mMutex);
736 if (buf == NULL) {
737 freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
738 mStreamSlotCount = 0;
739 return OK;
740 }
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700741 camera_metadata_t *buf2 = clone_camera_metadata(buf);
742 if (!buf2) {
743 ALOGE("%s: Unable to clone metadata buffer!", __FUNCTION__);
744 return NO_MEMORY;
745 }
746
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700747 if (mStreamSlotCount > 1) {
748 List<camera_metadata_t*>::iterator deleter = ++mStreamSlot.begin();
749 freeBuffers(++mStreamSlot.begin(), mStreamSlot.end());
750 mStreamSlotCount = 1;
751 }
752 if (mStreamSlotCount == 1) {
753 free_camera_metadata( *(mStreamSlot.begin()) );
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700754 *(mStreamSlot.begin()) = buf2;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700755 } else {
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700756 mStreamSlot.push_front(buf2);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700757 mStreamSlotCount = 1;
758 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700759 return signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700760}
761
762status_t Camera2Device::MetadataQueue::setStreamSlot(
763 const List<camera_metadata_t*> &bufs)
764{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700765 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700766 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700767 Mutex::Autolock l(mMutex);
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700768
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700769 if (mStreamSlotCount > 0) {
770 freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
771 }
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700772 mStreamSlotCount = 0;
773 for (List<camera_metadata_t*>::const_iterator r = bufs.begin();
774 r != bufs.end(); r++) {
775 camera_metadata_t *r2 = clone_camera_metadata(*r);
776 if (!r2) {
777 ALOGE("%s: Unable to clone metadata buffer!", __FUNCTION__);
778 return NO_MEMORY;
779 }
780 mStreamSlot.push_back(r2);
781 mStreamSlotCount++;
782 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700783 return signalConsumerLocked();
784}
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700785
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700786status_t Camera2Device::MetadataQueue::dump(int fd,
Igor Murashkinebe3f692012-10-12 16:56:11 -0700787 const Vector<String16>& /*args*/) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700788 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700789 String8 result;
790 status_t notLocked;
791 notLocked = mMutex.tryLock();
792 if (notLocked) {
793 result.append(" (Unable to lock queue mutex)\n");
794 }
795 result.appendFormat(" Current frame number: %d\n", mFrameCount);
796 if (mStreamSlotCount == 0) {
797 result.append(" Stream slot: Empty\n");
798 write(fd, result.string(), result.size());
799 } else {
800 result.appendFormat(" Stream slot: %d entries\n",
801 mStreamSlot.size());
802 int i = 0;
803 for (List<camera_metadata_t*>::iterator r = mStreamSlot.begin();
804 r != mStreamSlot.end(); r++) {
805 result = String8::format(" Stream slot buffer %d:\n", i);
806 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -0700807 dump_indented_camera_metadata(*r, fd, 2, 10);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700808 i++;
809 }
810 }
811 if (mEntries.size() == 0) {
812 result = " Main queue is empty\n";
813 write(fd, result.string(), result.size());
814 } else {
815 result = String8::format(" Main queue has %d entries:\n",
816 mEntries.size());
817 int i = 0;
818 for (List<camera_metadata_t*>::iterator r = mEntries.begin();
819 r != mEntries.end(); r++) {
820 result = String8::format(" Queue entry %d:\n", i);
821 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -0700822 dump_indented_camera_metadata(*r, fd, 2, 10);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700823 i++;
824 }
825 }
826
827 if (notLocked == 0) {
828 mMutex.unlock();
829 }
830
831 return OK;
832}
833
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700834status_t Camera2Device::MetadataQueue::signalConsumerLocked() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700835 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700836 status_t res = OK;
837 notEmpty.signal();
838 if (mSignalConsumer && mDevice != NULL) {
839 mSignalConsumer = false;
840
841 mMutex.unlock();
842 ALOGV("%s: Signaling consumer", __FUNCTION__);
843 res = mDevice->ops->notify_request_queue_not_empty(mDevice);
844 mMutex.lock();
845 }
846 return res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700847}
848
849status_t Camera2Device::MetadataQueue::freeBuffers(
850 List<camera_metadata_t*>::iterator start,
851 List<camera_metadata_t*>::iterator end)
852{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700853 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700854 while (start != end) {
855 free_camera_metadata(*start);
856 start = mStreamSlot.erase(start);
857 }
858 return OK;
859}
860
861Camera2Device::MetadataQueue* Camera2Device::MetadataQueue::getInstance(
862 const camera2_request_queue_src_ops_t *q)
863{
864 const MetadataQueue* cmq = static_cast<const MetadataQueue*>(q);
865 return const_cast<MetadataQueue*>(cmq);
866}
867
868Camera2Device::MetadataQueue* Camera2Device::MetadataQueue::getInstance(
869 const camera2_frame_queue_dst_ops_t *q)
870{
871 const MetadataQueue* cmq = static_cast<const MetadataQueue*>(q);
872 return const_cast<MetadataQueue*>(cmq);
873}
874
875int Camera2Device::MetadataQueue::consumer_buffer_count(
876 const camera2_request_queue_src_ops_t *q)
877{
878 MetadataQueue *queue = getInstance(q);
879 return queue->getBufferCount();
880}
881
882int Camera2Device::MetadataQueue::consumer_dequeue(
883 const camera2_request_queue_src_ops_t *q,
884 camera_metadata_t **buffer)
885{
886 MetadataQueue *queue = getInstance(q);
887 return queue->dequeue(buffer, true);
888}
889
890int Camera2Device::MetadataQueue::consumer_free(
891 const camera2_request_queue_src_ops_t *q,
892 camera_metadata_t *old_buffer)
893{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700894 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700895 MetadataQueue *queue = getInstance(q);
Igor Murashkinebe3f692012-10-12 16:56:11 -0700896 (void)queue;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700897 free_camera_metadata(old_buffer);
898 return OK;
899}
900
901int Camera2Device::MetadataQueue::producer_dequeue(
Igor Murashkinebe3f692012-10-12 16:56:11 -0700902 const camera2_frame_queue_dst_ops_t * /*q*/,
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700903 size_t entries, size_t bytes,
904 camera_metadata_t **buffer)
905{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700906 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700907 camera_metadata_t *new_buffer =
908 allocate_camera_metadata(entries, bytes);
909 if (new_buffer == NULL) return NO_MEMORY;
910 *buffer = new_buffer;
911 return OK;
912}
913
914int Camera2Device::MetadataQueue::producer_cancel(
Igor Murashkinebe3f692012-10-12 16:56:11 -0700915 const camera2_frame_queue_dst_ops_t * /*q*/,
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700916 camera_metadata_t *old_buffer)
917{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700918 ATRACE_CALL();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700919 free_camera_metadata(old_buffer);
920 return OK;
921}
922
923int Camera2Device::MetadataQueue::producer_enqueue(
924 const camera2_frame_queue_dst_ops_t *q,
925 camera_metadata_t *filled_buffer)
926{
927 MetadataQueue *queue = getInstance(q);
928 return queue->enqueue(filled_buffer);
929}
930
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700931/**
932 * Camera2Device::StreamAdapter
933 */
934
935#ifndef container_of
936#define container_of(ptr, type, member) \
937 (type *)((char*)(ptr) - offsetof(type, member))
938#endif
939
940Camera2Device::StreamAdapter::StreamAdapter(camera2_device_t *d):
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700941 mState(RELEASED),
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700942 mDevice(d),
943 mId(-1),
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700944 mWidth(0), mHeight(0), mFormat(0), mSize(0), mUsage(0),
945 mMaxProducerBuffers(0), mMaxConsumerBuffers(0),
946 mTotalBuffers(0),
947 mFormatRequested(0),
948 mActiveBuffers(0),
949 mFrameCount(0),
950 mLastTimestamp(0)
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700951{
952 camera2_stream_ops::dequeue_buffer = dequeue_buffer;
953 camera2_stream_ops::enqueue_buffer = enqueue_buffer;
954 camera2_stream_ops::cancel_buffer = cancel_buffer;
955 camera2_stream_ops::set_crop = set_crop;
956}
957
958Camera2Device::StreamAdapter::~StreamAdapter() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700959 ATRACE_CALL();
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700960 if (mState != RELEASED) {
961 release();
962 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700963}
964
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700965status_t Camera2Device::StreamAdapter::connectToDevice(
966 sp<ANativeWindow> consumer,
967 uint32_t width, uint32_t height, int format, size_t size) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -0700968 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700969 status_t res;
Eino-Ville Talvala9e4c3db2012-07-20 11:07:52 -0700970 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700971
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700972 if (mState != RELEASED) return INVALID_OPERATION;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700973 if (consumer == NULL) {
974 ALOGE("%s: Null consumer passed to stream adapter", __FUNCTION__);
975 return BAD_VALUE;
976 }
977
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700978 ALOGV("%s: New stream parameters %d x %d, format 0x%x, size %d",
979 __FUNCTION__, width, height, format, size);
980
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700981 mConsumerInterface = consumer;
982 mWidth = width;
983 mHeight = height;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700984 mSize = (format == HAL_PIXEL_FORMAT_BLOB) ? size : 0;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700985 mFormatRequested = format;
986
987 // Allocate device-side stream interface
988
989 uint32_t id;
990 uint32_t formatActual;
991 uint32_t usage;
992 uint32_t maxBuffers = 2;
993 res = mDevice->ops->allocate_stream(mDevice,
994 mWidth, mHeight, mFormatRequested, getStreamOps(),
995 &id, &formatActual, &usage, &maxBuffers);
996 if (res != OK) {
997 ALOGE("%s: Device stream allocation failed: %s (%d)",
998 __FUNCTION__, strerror(-res), res);
999 return res;
1000 }
1001
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001002 ALOGV("%s: Allocated stream id %d, actual format 0x%x, "
1003 "usage 0x%x, producer wants %d buffers", __FUNCTION__,
1004 id, formatActual, usage, maxBuffers);
1005
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001006 mId = id;
1007 mFormat = formatActual;
1008 mUsage = usage;
1009 mMaxProducerBuffers = maxBuffers;
1010
1011 mState = ALLOCATED;
1012
1013 // Configure consumer-side ANativeWindow interface
1014 res = native_window_api_connect(mConsumerInterface.get(),
1015 NATIVE_WINDOW_API_CAMERA);
1016 if (res != OK) {
1017 ALOGE("%s: Unable to connect to native window for stream %d",
1018 __FUNCTION__, mId);
1019
1020 return res;
1021 }
1022
1023 mState = CONNECTED;
1024
1025 res = native_window_set_usage(mConsumerInterface.get(), mUsage);
1026 if (res != OK) {
1027 ALOGE("%s: Unable to configure usage %08x for stream %d",
1028 __FUNCTION__, mUsage, mId);
1029 return res;
1030 }
1031
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001032 res = native_window_set_scaling_mode(mConsumerInterface.get(),
1033 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1034 if (res != OK) {
1035 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
1036 __FUNCTION__, strerror(-res), res);
1037 return res;
1038 }
1039
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -07001040 res = setTransform(0);
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001041 if (res != OK) {
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001042 return res;
1043 }
1044
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -07001045 if (mFormat == HAL_PIXEL_FORMAT_BLOB) {
1046 res = native_window_set_buffers_geometry(mConsumerInterface.get(),
1047 mSize, 1, mFormat);
1048 if (res != OK) {
1049 ALOGE("%s: Unable to configure compressed stream buffer geometry"
1050 " %d x %d, size %d for stream %d",
1051 __FUNCTION__, mWidth, mHeight, mSize, mId);
1052 return res;
1053 }
1054 } else {
1055 res = native_window_set_buffers_geometry(mConsumerInterface.get(),
1056 mWidth, mHeight, mFormat);
1057 if (res != OK) {
1058 ALOGE("%s: Unable to configure stream buffer geometry"
1059 " %d x %d, format 0x%x for stream %d",
1060 __FUNCTION__, mWidth, mHeight, mFormat, mId);
1061 return res;
1062 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001063 }
1064
1065 int maxConsumerBuffers;
1066 res = mConsumerInterface->query(mConsumerInterface.get(),
1067 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
1068 if (res != OK) {
1069 ALOGE("%s: Unable to query consumer undequeued"
1070 " buffer count for stream %d", __FUNCTION__, mId);
1071 return res;
1072 }
1073 mMaxConsumerBuffers = maxConsumerBuffers;
1074
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001075 ALOGV("%s: Consumer wants %d buffers", __FUNCTION__,
1076 mMaxConsumerBuffers);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001077
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001078 mTotalBuffers = mMaxConsumerBuffers + mMaxProducerBuffers;
1079 mActiveBuffers = 0;
1080 mFrameCount = 0;
1081 mLastTimestamp = 0;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001082
1083 res = native_window_set_buffer_count(mConsumerInterface.get(),
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001084 mTotalBuffers);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001085 if (res != OK) {
1086 ALOGE("%s: Unable to set buffer count for stream %d",
1087 __FUNCTION__, mId);
1088 return res;
1089 }
1090
1091 // Register allocated buffers with HAL device
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001092 buffer_handle_t *buffers = new buffer_handle_t[mTotalBuffers];
1093 ANativeWindowBuffer **anwBuffers = new ANativeWindowBuffer*[mTotalBuffers];
1094 uint32_t bufferIdx = 0;
1095 for (; bufferIdx < mTotalBuffers; bufferIdx++) {
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001096 res = native_window_dequeue_buffer_and_wait(mConsumerInterface.get(),
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001097 &anwBuffers[bufferIdx]);
1098 if (res != OK) {
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001099 ALOGE("%s: Unable to dequeue buffer %d for initial registration for "
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001100 "stream %d", __FUNCTION__, bufferIdx, mId);
1101 goto cleanUpBuffers;
1102 }
1103
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001104 buffers[bufferIdx] = anwBuffers[bufferIdx]->handle;
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001105 ALOGV("%s: Buffer %p allocated", __FUNCTION__, (void*)buffers[bufferIdx]);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001106 }
1107
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001108 ALOGV("%s: Registering %d buffers with camera HAL", __FUNCTION__, mTotalBuffers);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001109 res = mDevice->ops->register_stream_buffers(mDevice,
1110 mId,
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001111 mTotalBuffers,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001112 buffers);
1113 if (res != OK) {
1114 ALOGE("%s: Unable to register buffers with HAL device for stream %d",
1115 __FUNCTION__, mId);
1116 } else {
1117 mState = ACTIVE;
1118 }
1119
1120cleanUpBuffers:
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001121 ALOGV("%s: Cleaning up %d buffers", __FUNCTION__, bufferIdx);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001122 for (uint32_t i = 0; i < bufferIdx; i++) {
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001123 res = mConsumerInterface->cancelBuffer(mConsumerInterface.get(),
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001124 anwBuffers[i], -1);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001125 if (res != OK) {
1126 ALOGE("%s: Unable to cancel buffer %d after registration",
1127 __FUNCTION__, i);
1128 }
1129 }
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001130 delete[] anwBuffers;
1131 delete[] buffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001132
1133 return res;
1134}
1135
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001136status_t Camera2Device::StreamAdapter::release() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001137 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001138 status_t res;
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001139 ALOGV("%s: Releasing stream %d", __FUNCTION__, mId);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001140 if (mState >= ALLOCATED) {
1141 res = mDevice->ops->release_stream(mDevice, mId);
1142 if (res != OK) {
1143 ALOGE("%s: Unable to release stream %d",
1144 __FUNCTION__, mId);
1145 return res;
1146 }
1147 }
1148 if (mState >= CONNECTED) {
1149 res = native_window_api_disconnect(mConsumerInterface.get(),
1150 NATIVE_WINDOW_API_CAMERA);
Igor Murashkina1e5dcc2012-10-02 15:21:31 -07001151
1152 /* this is not an error. if client calling process dies,
1153 the window will also die and all calls to it will return
1154 DEAD_OBJECT, thus it's already "disconnected" */
1155 if (res == DEAD_OBJECT) {
1156 ALOGW("%s: While disconnecting stream %d from native window, the"
1157 " native window died from under us", __FUNCTION__, mId);
1158 }
1159 else if (res != OK) {
1160 ALOGE("%s: Unable to disconnect stream %d from native window (error %d %s)",
1161 __FUNCTION__, mId, res, strerror(-res));
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001162 return res;
1163 }
1164 }
1165 mId = -1;
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -07001166 mState = RELEASED;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001167 return OK;
1168}
1169
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -07001170status_t Camera2Device::StreamAdapter::setTransform(int transform) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001171 ATRACE_CALL();
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -07001172 status_t res;
1173 if (mState < CONNECTED) {
1174 ALOGE("%s: Cannot set transform on unconnected stream", __FUNCTION__);
1175 return INVALID_OPERATION;
1176 }
1177 res = native_window_set_buffers_transform(mConsumerInterface.get(),
1178 transform);
1179 if (res != OK) {
1180 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
1181 __FUNCTION__, transform, strerror(-res), res);
1182 }
1183 return res;
1184}
1185
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001186status_t Camera2Device::StreamAdapter::dump(int fd,
Igor Murashkinebe3f692012-10-12 16:56:11 -07001187 const Vector<String16>& /*args*/) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001188 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001189 String8 result = String8::format(" Stream %d: %d x %d, format 0x%x\n",
1190 mId, mWidth, mHeight, mFormat);
1191 result.appendFormat(" size %d, usage 0x%x, requested format 0x%x\n",
1192 mSize, mUsage, mFormatRequested);
1193 result.appendFormat(" total buffers: %d, dequeued buffers: %d\n",
1194 mTotalBuffers, mActiveBuffers);
1195 result.appendFormat(" frame count: %d, last timestamp %lld\n",
1196 mFrameCount, mLastTimestamp);
1197 write(fd, result.string(), result.size());
1198 return OK;
1199}
1200
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001201const camera2_stream_ops *Camera2Device::StreamAdapter::getStreamOps() {
1202 return static_cast<camera2_stream_ops *>(this);
1203}
1204
1205ANativeWindow* Camera2Device::StreamAdapter::toANW(
1206 const camera2_stream_ops_t *w) {
1207 return static_cast<const StreamAdapter*>(w)->mConsumerInterface.get();
1208}
1209
1210int Camera2Device::StreamAdapter::dequeue_buffer(const camera2_stream_ops_t *w,
1211 buffer_handle_t** buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001212 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001213 int res;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001214 StreamAdapter* stream =
1215 const_cast<StreamAdapter*>(static_cast<const StreamAdapter*>(w));
1216 if (stream->mState != ACTIVE) {
1217 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, stream->mState);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001218 return INVALID_OPERATION;
1219 }
1220
1221 ANativeWindow *a = toANW(w);
1222 ANativeWindowBuffer* anb;
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001223 res = native_window_dequeue_buffer_and_wait(a, &anb);
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001224 if (res != OK) {
1225 ALOGE("Stream %d dequeue: Error from native_window: %s (%d)", stream->mId,
1226 strerror(-res), res);
1227 return res;
1228 }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001229
1230 *buffer = &(anb->handle);
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001231 stream->mActiveBuffers++;
1232
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001233 ALOGVV("Stream %d dequeue: Buffer %p dequeued", stream->mId, (void*)(**buffer));
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001234 return res;
1235}
1236
1237int Camera2Device::StreamAdapter::enqueue_buffer(const camera2_stream_ops_t* w,
1238 int64_t timestamp,
1239 buffer_handle_t* buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001240 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001241 StreamAdapter *stream =
1242 const_cast<StreamAdapter*>(static_cast<const StreamAdapter*>(w));
Eino-Ville Talvala228a5382012-08-13 12:16:06 -07001243 stream->mFrameCount++;
1244 ALOGVV("Stream %d enqueue: Frame %d (%p) captured at %lld ns",
James Donga289bf62012-09-05 16:46:36 -07001245 stream->mId, stream->mFrameCount, (void*)(*buffer), timestamp);
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001246 int state = stream->mState;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001247 if (state != ACTIVE) {
1248 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
1249 return INVALID_OPERATION;
1250 }
1251 ANativeWindow *a = toANW(w);
1252 status_t err;
Eino-Ville Talvala228a5382012-08-13 12:16:06 -07001253
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001254 err = native_window_set_buffers_timestamp(a, timestamp);
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001255 if (err != OK) {
1256 ALOGE("%s: Error setting timestamp on native window: %s (%d)",
1257 __FUNCTION__, strerror(-err), err);
1258 return err;
1259 }
1260 err = a->queueBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001261 container_of(buffer, ANativeWindowBuffer, handle), -1);
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001262 if (err != OK) {
1263 ALOGE("%s: Error queueing buffer to native window: %s (%d)",
1264 __FUNCTION__, strerror(-err), err);
James Dong31d377b2012-08-09 17:43:46 -07001265 return err;
Eino-Ville Talvalabd4976a2012-06-07 10:40:25 -07001266 }
James Dong31d377b2012-08-09 17:43:46 -07001267
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001268 stream->mActiveBuffers--;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001269 stream->mLastTimestamp = timestamp;
James Dong31d377b2012-08-09 17:43:46 -07001270 return OK;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001271}
1272
1273int Camera2Device::StreamAdapter::cancel_buffer(const camera2_stream_ops_t* w,
1274 buffer_handle_t* buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001275 ATRACE_CALL();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001276 StreamAdapter *stream =
1277 const_cast<StreamAdapter*>(static_cast<const StreamAdapter*>(w));
Eino-Ville Talvala750d74b2012-08-01 09:05:04 -07001278 ALOGVV("Stream %d cancel: Buffer %p",
1279 stream->mId, (void*)(*buffer));
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -07001280 if (stream->mState != ACTIVE) {
1281 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, stream->mState);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001282 return INVALID_OPERATION;
1283 }
James Dong31d377b2012-08-09 17:43:46 -07001284
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001285 ANativeWindow *a = toANW(w);
James Dong31d377b2012-08-09 17:43:46 -07001286 int err = a->cancelBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -07001287 container_of(buffer, ANativeWindowBuffer, handle), -1);
James Dong31d377b2012-08-09 17:43:46 -07001288 if (err != OK) {
1289 ALOGE("%s: Error canceling buffer to native window: %s (%d)",
1290 __FUNCTION__, strerror(-err), err);
1291 return err;
1292 }
1293
1294 stream->mActiveBuffers--;
1295 return OK;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001296}
1297
1298int Camera2Device::StreamAdapter::set_crop(const camera2_stream_ops_t* w,
1299 int left, int top, int right, int bottom) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001300 ATRACE_CALL();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -07001301 int state = static_cast<const StreamAdapter*>(w)->mState;
1302 if (state != ACTIVE) {
1303 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
1304 return INVALID_OPERATION;
1305 }
1306 ANativeWindow *a = toANW(w);
1307 android_native_rect_t crop = { left, top, right, bottom };
1308 return native_window_set_crop(a, &crop);
1309}
1310
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001311/**
1312 * Camera2Device::ReprocessStreamAdapter
1313 */
1314
1315#ifndef container_of
1316#define container_of(ptr, type, member) \
1317 (type *)((char*)(ptr) - offsetof(type, member))
1318#endif
1319
1320Camera2Device::ReprocessStreamAdapter::ReprocessStreamAdapter(camera2_device_t *d):
1321 mState(RELEASED),
1322 mDevice(d),
1323 mId(-1),
1324 mWidth(0), mHeight(0), mFormat(0),
1325 mActiveBuffers(0),
1326 mFrameCount(0)
1327{
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001328 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001329 camera2_stream_in_ops::acquire_buffer = acquire_buffer;
1330 camera2_stream_in_ops::release_buffer = release_buffer;
1331}
1332
1333Camera2Device::ReprocessStreamAdapter::~ReprocessStreamAdapter() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001334 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001335 if (mState != RELEASED) {
1336 release();
1337 }
1338}
1339
1340status_t Camera2Device::ReprocessStreamAdapter::connectToDevice(
1341 const sp<StreamAdapter> &outputStream) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001342 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001343 status_t res;
1344 ALOGV("%s: E", __FUNCTION__);
1345
1346 if (mState != RELEASED) return INVALID_OPERATION;
1347 if (outputStream == NULL) {
1348 ALOGE("%s: Null base stream passed to reprocess stream adapter",
1349 __FUNCTION__);
1350 return BAD_VALUE;
1351 }
1352
1353 mBaseStream = outputStream;
1354 mWidth = outputStream->getWidth();
1355 mHeight = outputStream->getHeight();
1356 mFormat = outputStream->getFormat();
1357
1358 ALOGV("%s: New reprocess stream parameters %d x %d, format 0x%x",
1359 __FUNCTION__, mWidth, mHeight, mFormat);
1360
1361 // Allocate device-side stream interface
1362
1363 uint32_t id;
1364 res = mDevice->ops->allocate_reprocess_stream_from_stream(mDevice,
1365 outputStream->getId(), getStreamOps(),
1366 &id);
1367 if (res != OK) {
1368 ALOGE("%s: Device reprocess stream allocation failed: %s (%d)",
1369 __FUNCTION__, strerror(-res), res);
1370 return res;
1371 }
1372
1373 ALOGV("%s: Allocated reprocess stream id %d based on stream %d",
1374 __FUNCTION__, id, outputStream->getId());
1375
1376 mId = id;
1377
1378 mState = ACTIVE;
1379
1380 return OK;
1381}
1382
1383status_t Camera2Device::ReprocessStreamAdapter::release() {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001384 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001385 status_t res;
1386 ALOGV("%s: Releasing stream %d", __FUNCTION__, mId);
1387 if (mState >= ACTIVE) {
1388 res = mDevice->ops->release_reprocess_stream(mDevice, mId);
1389 if (res != OK) {
1390 ALOGE("%s: Unable to release stream %d",
1391 __FUNCTION__, mId);
1392 return res;
1393 }
1394 }
1395
1396 List<QueueEntry>::iterator s;
1397 for (s = mQueue.begin(); s != mQueue.end(); s++) {
1398 sp<BufferReleasedListener> listener = s->releaseListener.promote();
1399 if (listener != 0) listener->onBufferReleased(s->handle);
1400 }
1401 for (s = mInFlightQueue.begin(); s != mInFlightQueue.end(); s++) {
1402 sp<BufferReleasedListener> listener = s->releaseListener.promote();
1403 if (listener != 0) listener->onBufferReleased(s->handle);
1404 }
1405 mQueue.clear();
1406 mInFlightQueue.clear();
1407
1408 mState = RELEASED;
1409 return OK;
1410}
1411
1412status_t Camera2Device::ReprocessStreamAdapter::pushIntoStream(
1413 buffer_handle_t *handle, const wp<BufferReleasedListener> &releaseListener) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001414 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001415 // TODO: Some error checking here would be nice
1416 ALOGV("%s: Pushing buffer %p to stream", __FUNCTION__, (void*)(*handle));
1417
1418 QueueEntry entry;
1419 entry.handle = handle;
1420 entry.releaseListener = releaseListener;
1421 mQueue.push_back(entry);
1422 return OK;
1423}
1424
1425status_t Camera2Device::ReprocessStreamAdapter::dump(int fd,
Igor Murashkinebe3f692012-10-12 16:56:11 -07001426 const Vector<String16>& /*args*/) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001427 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001428 String8 result =
1429 String8::format(" Reprocess stream %d: %d x %d, fmt 0x%x\n",
1430 mId, mWidth, mHeight, mFormat);
1431 result.appendFormat(" acquired buffers: %d\n",
1432 mActiveBuffers);
1433 result.appendFormat(" frame count: %d\n",
1434 mFrameCount);
1435 write(fd, result.string(), result.size());
1436 return OK;
1437}
1438
1439const camera2_stream_in_ops *Camera2Device::ReprocessStreamAdapter::getStreamOps() {
1440 return static_cast<camera2_stream_in_ops *>(this);
1441}
1442
1443int Camera2Device::ReprocessStreamAdapter::acquire_buffer(
1444 const camera2_stream_in_ops_t *w,
1445 buffer_handle_t** buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001446 ATRACE_CALL();
Igor Murashkinebe3f692012-10-12 16:56:11 -07001447
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001448 ReprocessStreamAdapter* stream =
1449 const_cast<ReprocessStreamAdapter*>(
1450 static_cast<const ReprocessStreamAdapter*>(w));
1451 if (stream->mState != ACTIVE) {
1452 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, stream->mState);
1453 return INVALID_OPERATION;
1454 }
1455
1456 if (stream->mQueue.empty()) {
1457 *buffer = NULL;
1458 return OK;
1459 }
1460
1461 QueueEntry &entry = *(stream->mQueue.begin());
1462
1463 *buffer = entry.handle;
1464
1465 stream->mInFlightQueue.push_back(entry);
1466 stream->mQueue.erase(stream->mQueue.begin());
1467
1468 stream->mActiveBuffers++;
1469
1470 ALOGV("Stream %d acquire: Buffer %p acquired", stream->mId,
1471 (void*)(**buffer));
1472 return OK;
1473}
1474
1475int Camera2Device::ReprocessStreamAdapter::release_buffer(
1476 const camera2_stream_in_ops_t* w,
1477 buffer_handle_t* buffer) {
Eino-Ville Talvala852c3812012-09-24 09:46:53 -07001478 ATRACE_CALL();
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001479 ReprocessStreamAdapter *stream =
1480 const_cast<ReprocessStreamAdapter*>(
1481 static_cast<const ReprocessStreamAdapter*>(w) );
1482 stream->mFrameCount++;
1483 ALOGV("Reprocess stream %d release: Frame %d (%p)",
1484 stream->mId, stream->mFrameCount, (void*)*buffer);
1485 int state = stream->mState;
1486 if (state != ACTIVE) {
1487 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
1488 return INVALID_OPERATION;
1489 }
1490 stream->mActiveBuffers--;
1491
1492 List<QueueEntry>::iterator s;
1493 for (s = stream->mInFlightQueue.begin(); s != stream->mInFlightQueue.end(); s++) {
1494 if ( s->handle == buffer ) break;
1495 }
1496 if (s == stream->mInFlightQueue.end()) {
1497 ALOGE("%s: Can't find buffer %p in in-flight list!", __FUNCTION__,
1498 buffer);
1499 return INVALID_OPERATION;
1500 }
1501
1502 sp<BufferReleasedListener> listener = s->releaseListener.promote();
1503 if (listener != 0) {
1504 listener->onBufferReleased(s->handle);
1505 } else {
1506 ALOGE("%s: Can't free buffer - missing listener", __FUNCTION__);
1507 }
1508 stream->mInFlightQueue.erase(s);
1509
1510 return OK;
1511}
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -07001512
1513}; // namespace android