1 /*
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
17 #define LOG_TAG "Camera2-Device"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 //#define LOG_NDEBUG 0
20 //#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
27
28 #include <inttypes.h>
29 #include <utils/Log.h>
30 #include <utils/Trace.h>
31 #include <utils/Timers.h>
32 #include "Camera2Device.h"
33 #include "CameraService.h"
34
35 namespace android {
36
Camera2Device(int id)37 Camera2Device::Camera2Device(int id):
38 mId(id),
39 mHal2Device(NULL)
40 {
41 ATRACE_CALL();
42 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
43 }
44
~Camera2Device()45 Camera2Device::~Camera2Device()
46 {
47 ATRACE_CALL();
48 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
49 disconnect();
50 }
51
getId() const52 int Camera2Device::getId() const {
53 return mId;
54 }
55
initialize(CameraModule * module)56 status_t Camera2Device::initialize(CameraModule *module)
57 {
58 ATRACE_CALL();
59 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
60 if (mHal2Device != NULL) {
61 ALOGE("%s: Already initialized!", __FUNCTION__);
62 return INVALID_OPERATION;
63 }
64
65 status_t res;
66 char name[10];
67 snprintf(name, sizeof(name), "%d", mId);
68
69 camera2_device_t *device;
70
71 res = module->open(name, reinterpret_cast<hw_device_t**>(&device));
72
73 if (res != OK) {
74 ALOGE("%s: Could not open camera %d: %s (%d)", __FUNCTION__,
75 mId, strerror(-res), res);
76 return res;
77 }
78
79 if (device->common.version != CAMERA_DEVICE_API_VERSION_2_0) {
80 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,
83 device->common.version);
84 device->common.close(&device->common);
85 return BAD_VALUE;
86 }
87
88 camera_info info;
89 res = module->getCameraInfo(mId, &info);
90 if (res != OK ) return res;
91
92 if (info.device_version != device->common.version) {
93 ALOGE("%s: HAL reporting mismatched camera_info version (%x)"
94 " and device version (%x).", __FUNCTION__,
95 device->common.version, info.device_version);
96 device->common.close(&device->common);
97 return BAD_VALUE;
98 }
99
100 res = mRequestQueue.setConsumerDevice(device);
101 if (res != OK) {
102 ALOGE("%s: Camera %d: Unable to connect request queue to device: %s (%d)",
103 __FUNCTION__, mId, strerror(-res), res);
104 device->common.close(&device->common);
105 return res;
106 }
107 res = mFrameQueue.setProducerDevice(device);
108 if (res != OK) {
109 ALOGE("%s: Camera %d: Unable to connect frame queue to device: %s (%d)",
110 __FUNCTION__, mId, strerror(-res), res);
111 device->common.close(&device->common);
112 return res;
113 }
114
115 res = device->ops->set_notify_callback(device, notificationCallback,
116 NULL);
117 if (res != OK) {
118 ALOGE("%s: Camera %d: Unable to initialize notification callback!",
119 __FUNCTION__, mId);
120 device->common.close(&device->common);
121 return res;
122 }
123
124 mDeviceInfo = info.static_camera_characteristics;
125 mHal2Device = device;
126 mDeviceVersion = device->common.version;
127
128 return OK;
129 }
130
disconnect()131 status_t Camera2Device::disconnect() {
132 ATRACE_CALL();
133 status_t res = OK;
134 if (mHal2Device) {
135 ALOGV("%s: Closing device for camera %d", __FUNCTION__, mId);
136
137 int inProgressCount = mHal2Device->ops->get_in_progress_count(mHal2Device);
138 if (inProgressCount > 0) {
139 ALOGW("%s: Closing camera device %d with %d requests in flight!",
140 __FUNCTION__, mId, inProgressCount);
141 }
142 mReprocessStreams.clear();
143 mStreams.clear();
144 res = mHal2Device->common.close(&mHal2Device->common);
145 if (res != OK) {
146 ALOGE("%s: Could not close camera %d: %s (%d)",
147 __FUNCTION__,
148 mId, strerror(-res), res);
149 }
150 mHal2Device = NULL;
151 ALOGV("%s: Shutdown complete", __FUNCTION__);
152 }
153 return res;
154 }
155
dump(int fd,const Vector<String16> & args)156 status_t Camera2Device::dump(int fd, const Vector<String16>& args) {
157 ATRACE_CALL();
158 String8 result;
159 int detailLevel = 0;
160 int n = args.size();
161 String16 detailOption("-d");
162 for (int i = 0; i + 1 < n; i++) {
163 if (args[i] == detailOption) {
164 String8 levelStr(args[i+1]);
165 detailLevel = atoi(levelStr.string());
166 }
167 }
168
169 result.appendFormat(" Camera2Device[%d] dump (detail level %d):\n",
170 mId, detailLevel);
171
172 if (detailLevel > 0) {
173 result = " Request queue contents:\n";
174 write(fd, result.string(), result.size());
175 mRequestQueue.dump(fd, args);
176
177 result = " Frame queue contents:\n";
178 write(fd, result.string(), result.size());
179 mFrameQueue.dump(fd, args);
180 }
181
182 result = " Active streams:\n";
183 write(fd, result.string(), result.size());
184 for (StreamList::iterator s = mStreams.begin(); s != mStreams.end(); s++) {
185 (*s)->dump(fd, args);
186 }
187
188 result = " HAL device dump:\n";
189 write(fd, result.string(), result.size());
190
191 status_t res;
192 res = mHal2Device->ops->dump(mHal2Device, fd);
193
194 return res;
195 }
196
info() const197 const CameraMetadata& Camera2Device::info() const {
198 ALOGVV("%s: E", __FUNCTION__);
199
200 return mDeviceInfo;
201 }
202
capture(CameraMetadata & request,int64_t *)203 status_t Camera2Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
204 ATRACE_CALL();
205 ALOGV("%s: E", __FUNCTION__);
206
207 mRequestQueue.enqueue(request.release());
208 return OK;
209 }
210
captureList(const List<const CameraMetadata> & requests,int64_t *)211 status_t Camera2Device::captureList(const List<const CameraMetadata> &requests,
212 int64_t* /*lastFrameNumber*/) {
213 ATRACE_CALL();
214 ALOGE("%s: Camera2Device burst capture not implemented", __FUNCTION__);
215 return INVALID_OPERATION;
216 }
217
setStreamingRequest(const CameraMetadata & request,int64_t *)218 status_t Camera2Device::setStreamingRequest(const CameraMetadata &request,
219 int64_t* /*lastFrameNumber*/) {
220 ATRACE_CALL();
221 ALOGV("%s: E", __FUNCTION__);
222 CameraMetadata streamRequest(request);
223 return mRequestQueue.setStreamSlot(streamRequest.release());
224 }
225
setStreamingRequestList(const List<const CameraMetadata> & requests,int64_t *)226 status_t Camera2Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
227 int64_t* /*lastFrameNumber*/) {
228 ATRACE_CALL();
229 ALOGE("%s, Camera2Device streaming burst not implemented", __FUNCTION__);
230 return INVALID_OPERATION;
231 }
232
clearStreamingRequest(int64_t *)233 status_t Camera2Device::clearStreamingRequest(int64_t* /*lastFrameNumber*/) {
234 ATRACE_CALL();
235 return mRequestQueue.setStreamSlot(NULL);
236 }
237
waitUntilRequestReceived(int32_t requestId,nsecs_t timeout)238 status_t Camera2Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
239 ATRACE_CALL();
240 return mRequestQueue.waitForDequeue(requestId, timeout);
241 }
242
createStream(sp<Surface> consumer,uint32_t width,uint32_t height,int format,android_dataspace,camera3_stream_rotation_t rotation,int * id)243 status_t Camera2Device::createStream(sp<Surface> consumer,
244 uint32_t width, uint32_t height, int format,
245 android_dataspace /*dataSpace*/, camera3_stream_rotation_t rotation, int *id) {
246 ATRACE_CALL();
247 status_t res;
248 ALOGV("%s: E", __FUNCTION__);
249
250 sp<StreamAdapter> stream = new StreamAdapter(mHal2Device);
251 size_t size = 0;
252 if (format == HAL_PIXEL_FORMAT_BLOB) {
253 size = getJpegBufferSize(width, height);
254 }
255 res = stream->connectToDevice(consumer, width, height, format, size);
256 if (res != OK) {
257 ALOGE("%s: Camera %d: Unable to create stream (%d x %d, format %x):"
258 "%s (%d)",
259 __FUNCTION__, mId, width, height, format, strerror(-res), res);
260 return res;
261 }
262
263 *id = stream->getId();
264
265 mStreams.push_back(stream);
266 return OK;
267 }
268
getJpegBufferSize(uint32_t width,uint32_t height) const269 ssize_t Camera2Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
270 // Always give the max jpeg buffer size regardless of the actual jpeg resolution.
271 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
272 if (jpegBufMaxSize.count == 0) {
273 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
274 return BAD_VALUE;
275 }
276
277 return jpegBufMaxSize.data.i32[0];
278 }
279
createReprocessStreamFromStream(int outputId,int * id)280 status_t Camera2Device::createReprocessStreamFromStream(int outputId, int *id) {
281 ATRACE_CALL();
282 status_t res;
283 ALOGV("%s: E", __FUNCTION__);
284
285 bool found = false;
286 StreamList::iterator streamI;
287 for (streamI = mStreams.begin();
288 streamI != mStreams.end(); streamI++) {
289 if ((*streamI)->getId() == outputId) {
290 found = true;
291 break;
292 }
293 }
294 if (!found) {
295 ALOGE("%s: Camera %d: Output stream %d doesn't exist; can't create "
296 "reprocess stream from it!", __FUNCTION__, mId, outputId);
297 return BAD_VALUE;
298 }
299
300 sp<ReprocessStreamAdapter> stream = new ReprocessStreamAdapter(mHal2Device);
301
302 res = stream->connectToDevice((*streamI));
303 if (res != OK) {
304 ALOGE("%s: Camera %d: Unable to create reprocessing stream from "\
305 "stream %d: %s (%d)", __FUNCTION__, mId, outputId,
306 strerror(-res), res);
307 return res;
308 }
309
310 *id = stream->getId();
311
312 mReprocessStreams.push_back(stream);
313 return OK;
314 }
315
316
getStreamInfo(int id,uint32_t * width,uint32_t * height,uint32_t * format,android_dataspace * dataSpace)317 status_t Camera2Device::getStreamInfo(int id,
318 uint32_t *width, uint32_t *height,
319 uint32_t *format, android_dataspace *dataSpace) {
320 ATRACE_CALL();
321 ALOGV("%s: E", __FUNCTION__);
322 bool found = false;
323 StreamList::iterator streamI;
324 for (streamI = mStreams.begin();
325 streamI != mStreams.end(); streamI++) {
326 if ((*streamI)->getId() == id) {
327 found = true;
328 break;
329 }
330 }
331 if (!found) {
332 ALOGE("%s: Camera %d: Stream %d does not exist",
333 __FUNCTION__, mId, id);
334 return BAD_VALUE;
335 }
336
337 if (width) *width = (*streamI)->getWidth();
338 if (height) *height = (*streamI)->getHeight();
339 if (format) *format = (*streamI)->getFormat();
340 if (dataSpace) *dataSpace = HAL_DATASPACE_UNKNOWN;
341
342 return OK;
343 }
344
setStreamTransform(int id,int transform)345 status_t Camera2Device::setStreamTransform(int id,
346 int transform) {
347 ATRACE_CALL();
348 ALOGV("%s: E", __FUNCTION__);
349 bool found = false;
350 StreamList::iterator streamI;
351 for (streamI = mStreams.begin();
352 streamI != mStreams.end(); streamI++) {
353 if ((*streamI)->getId() == id) {
354 found = true;
355 break;
356 }
357 }
358 if (!found) {
359 ALOGE("%s: Camera %d: Stream %d does not exist",
360 __FUNCTION__, mId, id);
361 return BAD_VALUE;
362 }
363
364 return (*streamI)->setTransform(transform);
365 }
366
deleteStream(int id)367 status_t Camera2Device::deleteStream(int id) {
368 ATRACE_CALL();
369 ALOGV("%s: E", __FUNCTION__);
370 bool found = false;
371 for (StreamList::iterator streamI = mStreams.begin();
372 streamI != mStreams.end(); streamI++) {
373 if ((*streamI)->getId() == id) {
374 status_t res = (*streamI)->release();
375 if (res != OK) {
376 ALOGE("%s: Unable to release stream %d from HAL device: "
377 "%s (%d)", __FUNCTION__, id, strerror(-res), res);
378 return res;
379 }
380 mStreams.erase(streamI);
381 found = true;
382 break;
383 }
384 }
385 if (!found) {
386 ALOGE("%s: Camera %d: Unable to find stream %d to delete",
387 __FUNCTION__, mId, id);
388 return BAD_VALUE;
389 }
390 return OK;
391 }
392
deleteReprocessStream(int id)393 status_t Camera2Device::deleteReprocessStream(int id) {
394 ATRACE_CALL();
395 ALOGV("%s: E", __FUNCTION__);
396 bool found = false;
397 for (ReprocessStreamList::iterator streamI = mReprocessStreams.begin();
398 streamI != mReprocessStreams.end(); streamI++) {
399 if ((*streamI)->getId() == id) {
400 status_t res = (*streamI)->release();
401 if (res != OK) {
402 ALOGE("%s: Unable to release reprocess stream %d from "
403 "HAL device: %s (%d)", __FUNCTION__, id,
404 strerror(-res), res);
405 return res;
406 }
407 mReprocessStreams.erase(streamI);
408 found = true;
409 break;
410 }
411 }
412 if (!found) {
413 ALOGE("%s: Camera %d: Unable to find stream %d to delete",
414 __FUNCTION__, mId, id);
415 return BAD_VALUE;
416 }
417 return OK;
418 }
419
configureStreams(bool isConstrainedHighSpeed)420 status_t Camera2Device::configureStreams(bool isConstrainedHighSpeed) {
421 ATRACE_CALL();
422 ALOGV("%s: E", __FUNCTION__);
423
424 /**
425 * HAL2 devices do not need to configure streams;
426 * streams are created on the fly.
427 */
428 ALOGW("%s: No-op for HAL2 devices", __FUNCTION__);
429
430 return OK;
431 }
432
433
createDefaultRequest(int templateId,CameraMetadata * request)434 status_t Camera2Device::createDefaultRequest(int templateId,
435 CameraMetadata *request) {
436 ATRACE_CALL();
437 status_t err;
438 ALOGV("%s: E", __FUNCTION__);
439 camera_metadata_t *rawRequest;
440 err = mHal2Device->ops->construct_default_request(
441 mHal2Device, templateId, &rawRequest);
442 request->acquire(rawRequest);
443 return err;
444 }
445
waitUntilDrained()446 status_t Camera2Device::waitUntilDrained() {
447 ATRACE_CALL();
448 static const uint32_t kSleepTime = 50000; // 50 ms
449 static const uint32_t kMaxSleepTime = 10000000; // 10 s
450 ALOGV("%s: Camera %d: Starting wait", __FUNCTION__, mId);
451 if (mRequestQueue.getBufferCount() ==
452 CAMERA2_REQUEST_QUEUE_IS_BOTTOMLESS) return INVALID_OPERATION;
453
454 // TODO: Set up notifications from HAL, instead of sleeping here
455 uint32_t totalTime = 0;
456 while (mHal2Device->ops->get_in_progress_count(mHal2Device) > 0) {
457 usleep(kSleepTime);
458 totalTime += kSleepTime;
459 if (totalTime > kMaxSleepTime) {
460 ALOGE("%s: Waited %d us, %d requests still in flight", __FUNCTION__,
461 totalTime, mHal2Device->ops->get_in_progress_count(mHal2Device));
462 return TIMED_OUT;
463 }
464 }
465 ALOGV("%s: Camera %d: HAL is idle", __FUNCTION__, mId);
466 return OK;
467 }
468
setNotifyCallback(NotificationListener * listener)469 status_t Camera2Device::setNotifyCallback(NotificationListener *listener) {
470 ATRACE_CALL();
471 status_t res;
472 res = mHal2Device->ops->set_notify_callback(mHal2Device, notificationCallback,
473 reinterpret_cast<void*>(listener) );
474 if (res != OK) {
475 ALOGE("%s: Unable to set notification callback!", __FUNCTION__);
476 }
477 return res;
478 }
479
willNotify3A()480 bool Camera2Device::willNotify3A() {
481 return true;
482 }
483
notificationCallback(int32_t msg_type,int32_t ext1,int32_t ext2,int32_t ext3,void * user)484 void Camera2Device::notificationCallback(int32_t msg_type,
485 int32_t ext1,
486 int32_t ext2,
487 int32_t ext3,
488 void *user) {
489 ATRACE_CALL();
490 NotificationListener *listener = reinterpret_cast<NotificationListener*>(user);
491 ALOGV("%s: Notification %d, arguments %d, %d, %d", __FUNCTION__, msg_type,
492 ext1, ext2, ext3);
493 if (listener != NULL) {
494 switch (msg_type) {
495 case CAMERA2_MSG_ERROR:
496 // TODO: This needs to be fixed. ext2 and ext3 need to be considered.
497 listener->notifyError(
498 ((ext1 == CAMERA2_MSG_ERROR_DEVICE)
499 || (ext1 == CAMERA2_MSG_ERROR_HARDWARE)) ?
500 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE :
501 ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE,
502 CaptureResultExtras());
503 break;
504 case CAMERA2_MSG_SHUTTER: {
505 // TODO: Only needed for camera2 API, which is unsupported
506 // by HAL2 directly.
507 // nsecs_t timestamp = (nsecs_t)ext2 | ((nsecs_t)(ext3) << 32 );
508 // listener->notifyShutter(requestId, timestamp);
509 break;
510 }
511 case CAMERA2_MSG_AUTOFOCUS:
512 listener->notifyAutoFocus(ext1, ext2);
513 break;
514 case CAMERA2_MSG_AUTOEXPOSURE:
515 listener->notifyAutoExposure(ext1, ext2);
516 break;
517 case CAMERA2_MSG_AUTOWB:
518 listener->notifyAutoWhitebalance(ext1, ext2);
519 break;
520 default:
521 ALOGE("%s: Unknown notification %d (arguments %d, %d, %d)!",
522 __FUNCTION__, msg_type, ext1, ext2, ext3);
523 }
524 }
525 }
526
waitForNextFrame(nsecs_t timeout)527 status_t Camera2Device::waitForNextFrame(nsecs_t timeout) {
528 return mFrameQueue.waitForBuffer(timeout);
529 }
530
getNextResult(CaptureResult * result)531 status_t Camera2Device::getNextResult(CaptureResult *result) {
532 ATRACE_CALL();
533 ALOGV("%s: get CaptureResult", __FUNCTION__);
534 if (result == NULL) {
535 ALOGE("%s: result pointer is NULL", __FUNCTION__);
536 return BAD_VALUE;
537 }
538 status_t res;
539 camera_metadata_t *rawFrame;
540 res = mFrameQueue.dequeue(&rawFrame);
541 if (rawFrame == NULL) {
542 return NOT_ENOUGH_DATA;
543 } else if (res == OK) {
544 result->mMetadata.acquire(rawFrame);
545 }
546
547 return res;
548 }
549
triggerAutofocus(uint32_t id)550 status_t Camera2Device::triggerAutofocus(uint32_t id) {
551 ATRACE_CALL();
552 status_t res;
553 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
554 res = mHal2Device->ops->trigger_action(mHal2Device,
555 CAMERA2_TRIGGER_AUTOFOCUS, id, 0);
556 if (res != OK) {
557 ALOGE("%s: Error triggering autofocus (id %d)",
558 __FUNCTION__, id);
559 }
560 return res;
561 }
562
triggerCancelAutofocus(uint32_t id)563 status_t Camera2Device::triggerCancelAutofocus(uint32_t id) {
564 ATRACE_CALL();
565 status_t res;
566 ALOGV("%s: Canceling autofocus, id %d", __FUNCTION__, id);
567 res = mHal2Device->ops->trigger_action(mHal2Device,
568 CAMERA2_TRIGGER_CANCEL_AUTOFOCUS, id, 0);
569 if (res != OK) {
570 ALOGE("%s: Error canceling autofocus (id %d)",
571 __FUNCTION__, id);
572 }
573 return res;
574 }
575
triggerPrecaptureMetering(uint32_t id)576 status_t Camera2Device::triggerPrecaptureMetering(uint32_t id) {
577 ATRACE_CALL();
578 status_t res;
579 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
580 res = mHal2Device->ops->trigger_action(mHal2Device,
581 CAMERA2_TRIGGER_PRECAPTURE_METERING, id, 0);
582 if (res != OK) {
583 ALOGE("%s: Error triggering precapture metering (id %d)",
584 __FUNCTION__, id);
585 }
586 return res;
587 }
588
pushReprocessBuffer(int reprocessStreamId,buffer_handle_t * buffer,wp<BufferReleasedListener> listener)589 status_t Camera2Device::pushReprocessBuffer(int reprocessStreamId,
590 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
591 ATRACE_CALL();
592 ALOGV("%s: E", __FUNCTION__);
593 bool found = false;
594 status_t res = OK;
595 for (ReprocessStreamList::iterator streamI = mReprocessStreams.begin();
596 streamI != mReprocessStreams.end(); streamI++) {
597 if ((*streamI)->getId() == reprocessStreamId) {
598 res = (*streamI)->pushIntoStream(buffer, listener);
599 if (res != OK) {
600 ALOGE("%s: Unable to push buffer to reprocess stream %d: %s (%d)",
601 __FUNCTION__, reprocessStreamId, strerror(-res), res);
602 return res;
603 }
604 found = true;
605 break;
606 }
607 }
608 if (!found) {
609 ALOGE("%s: Camera %d: Unable to find reprocess stream %d",
610 __FUNCTION__, mId, reprocessStreamId);
611 res = BAD_VALUE;
612 }
613 return res;
614 }
615
flush(int64_t *)616 status_t Camera2Device::flush(int64_t* /*lastFrameNumber*/) {
617 ATRACE_CALL();
618
619 mRequestQueue.clear();
620 return waitUntilDrained();
621 }
622
prepare(int streamId)623 status_t Camera2Device::prepare(int streamId) {
624 ATRACE_CALL();
625 ALOGE("%s: Camera %d: unimplemented", __FUNCTION__, mId);
626 return NO_INIT;
627 }
628
tearDown(int streamId)629 status_t Camera2Device::tearDown(int streamId) {
630 ATRACE_CALL();
631 ALOGE("%s: Camera %d: unimplemented", __FUNCTION__, mId);
632 return NO_INIT;
633 }
634
getDeviceVersion()635 uint32_t Camera2Device::getDeviceVersion() {
636 ATRACE_CALL();
637 return mDeviceVersion;
638 }
639
640 /**
641 * Camera2Device::MetadataQueue
642 */
643
MetadataQueue()644 Camera2Device::MetadataQueue::MetadataQueue():
645 mHal2Device(NULL),
646 mFrameCount(0),
647 mLatestRequestId(0),
648 mCount(0),
649 mStreamSlotCount(0),
650 mSignalConsumer(true)
651 {
652 ATRACE_CALL();
653 camera2_request_queue_src_ops::dequeue_request = consumer_dequeue;
654 camera2_request_queue_src_ops::request_count = consumer_buffer_count;
655 camera2_request_queue_src_ops::free_request = consumer_free;
656
657 camera2_frame_queue_dst_ops::dequeue_frame = producer_dequeue;
658 camera2_frame_queue_dst_ops::cancel_frame = producer_cancel;
659 camera2_frame_queue_dst_ops::enqueue_frame = producer_enqueue;
660 }
661
~MetadataQueue()662 Camera2Device::MetadataQueue::~MetadataQueue() {
663 ATRACE_CALL();
664 clear();
665 }
666
667 // Connect to camera2 HAL as consumer (input requests/reprocessing)
setConsumerDevice(camera2_device_t * d)668 status_t Camera2Device::MetadataQueue::setConsumerDevice(camera2_device_t *d) {
669 ATRACE_CALL();
670 status_t res;
671 res = d->ops->set_request_queue_src_ops(d,
672 this);
673 if (res != OK) return res;
674 mHal2Device = d;
675 return OK;
676 }
677
setProducerDevice(camera2_device_t * d)678 status_t Camera2Device::MetadataQueue::setProducerDevice(camera2_device_t *d) {
679 ATRACE_CALL();
680 status_t res;
681 res = d->ops->set_frame_queue_dst_ops(d,
682 this);
683 return res;
684 }
685
686 // Real interfaces
enqueue(camera_metadata_t * buf)687 status_t Camera2Device::MetadataQueue::enqueue(camera_metadata_t *buf) {
688 ATRACE_CALL();
689 ALOGVV("%s: E", __FUNCTION__);
690 Mutex::Autolock l(mMutex);
691
692 mCount++;
693 mEntries.push_back(buf);
694
695 return signalConsumerLocked();
696 }
697
getBufferCount()698 int Camera2Device::MetadataQueue::getBufferCount() {
699 ATRACE_CALL();
700 Mutex::Autolock l(mMutex);
701 if (mStreamSlotCount > 0) {
702 return CAMERA2_REQUEST_QUEUE_IS_BOTTOMLESS;
703 }
704 return mCount;
705 }
706
dequeue(camera_metadata_t ** buf,bool incrementCount)707 status_t Camera2Device::MetadataQueue::dequeue(camera_metadata_t **buf,
708 bool incrementCount)
709 {
710 ATRACE_CALL();
711 ALOGVV("%s: E", __FUNCTION__);
712 status_t res;
713 Mutex::Autolock l(mMutex);
714
715 if (mCount == 0) {
716 if (mStreamSlotCount == 0) {
717 ALOGVV("%s: Empty", __FUNCTION__);
718 *buf = NULL;
719 mSignalConsumer = true;
720 return OK;
721 }
722 ALOGVV("%s: Streaming %d frames to queue", __FUNCTION__,
723 mStreamSlotCount);
724
725 for (List<camera_metadata_t*>::iterator slotEntry = mStreamSlot.begin();
726 slotEntry != mStreamSlot.end();
727 slotEntry++ ) {
728 size_t entries = get_camera_metadata_entry_count(*slotEntry);
729 size_t dataBytes = get_camera_metadata_data_count(*slotEntry);
730
731 camera_metadata_t *copy =
732 allocate_camera_metadata(entries, dataBytes);
733 append_camera_metadata(copy, *slotEntry);
734 mEntries.push_back(copy);
735 }
736 mCount = mStreamSlotCount;
737 }
738 ALOGVV("MetadataQueue: deque (%d buffers)", mCount);
739 camera_metadata_t *b = *(mEntries.begin());
740 mEntries.erase(mEntries.begin());
741
742 if (incrementCount) {
743 ATRACE_INT("cam2_request", mFrameCount);
744 camera_metadata_entry_t frameCount;
745 res = find_camera_metadata_entry(b,
746 ANDROID_REQUEST_FRAME_COUNT,
747 &frameCount);
748 if (res != OK) {
749 ALOGE("%s: Unable to add frame count: %s (%d)",
750 __FUNCTION__, strerror(-res), res);
751 } else {
752 *frameCount.data.i32 = mFrameCount;
753 }
754 mFrameCount++;
755 }
756
757 // Check for request ID, and if present, signal waiters.
758 camera_metadata_entry_t requestId;
759 res = find_camera_metadata_entry(b,
760 ANDROID_REQUEST_ID,
761 &requestId);
762 if (res == OK) {
763 mLatestRequestId = requestId.data.i32[0];
764 mNewRequestId.signal();
765 }
766
767 *buf = b;
768 mCount--;
769
770 return OK;
771 }
772
waitForBuffer(nsecs_t timeout)773 status_t Camera2Device::MetadataQueue::waitForBuffer(nsecs_t timeout)
774 {
775 Mutex::Autolock l(mMutex);
776 status_t res;
777 while (mCount == 0) {
778 res = notEmpty.waitRelative(mMutex,timeout);
779 if (res != OK) return res;
780 }
781 return OK;
782 }
783
waitForDequeue(int32_t id,nsecs_t timeout)784 status_t Camera2Device::MetadataQueue::waitForDequeue(int32_t id,
785 nsecs_t timeout) {
786 Mutex::Autolock l(mMutex);
787 status_t res;
788 while (mLatestRequestId != id) {
789 nsecs_t startTime = systemTime();
790
791 res = mNewRequestId.waitRelative(mMutex, timeout);
792 if (res != OK) return res;
793
794 timeout -= (systemTime() - startTime);
795 }
796
797 return OK;
798 }
799
setStreamSlot(camera_metadata_t * buf)800 status_t Camera2Device::MetadataQueue::setStreamSlot(camera_metadata_t *buf)
801 {
802 ATRACE_CALL();
803 ALOGV("%s: E", __FUNCTION__);
804 Mutex::Autolock l(mMutex);
805 if (buf == NULL) {
806 freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
807 mStreamSlotCount = 0;
808 return OK;
809 }
810
811 if (mStreamSlotCount > 1) {
812 List<camera_metadata_t*>::iterator deleter = ++mStreamSlot.begin();
813 freeBuffers(++mStreamSlot.begin(), mStreamSlot.end());
814 mStreamSlotCount = 1;
815 }
816 if (mStreamSlotCount == 1) {
817 free_camera_metadata( *(mStreamSlot.begin()) );
818 *(mStreamSlot.begin()) = buf;
819 } else {
820 mStreamSlot.push_front(buf);
821 mStreamSlotCount = 1;
822 }
823 return signalConsumerLocked();
824 }
825
setStreamSlot(const List<camera_metadata_t * > & bufs)826 status_t Camera2Device::MetadataQueue::setStreamSlot(
827 const List<camera_metadata_t*> &bufs)
828 {
829 ATRACE_CALL();
830 ALOGV("%s: E", __FUNCTION__);
831 Mutex::Autolock l(mMutex);
832
833 if (mStreamSlotCount > 0) {
834 freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
835 }
836 mStreamSlotCount = 0;
837 for (List<camera_metadata_t*>::const_iterator r = bufs.begin();
838 r != bufs.end(); r++) {
839 mStreamSlot.push_back(*r);
840 mStreamSlotCount++;
841 }
842 return signalConsumerLocked();
843 }
844
clear()845 status_t Camera2Device::MetadataQueue::clear()
846 {
847 ATRACE_CALL();
848 ALOGV("%s: E", __FUNCTION__);
849
850 Mutex::Autolock l(mMutex);
851
852 // Clear streaming slot
853 freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
854 mStreamSlotCount = 0;
855
856 // Clear request queue
857 freeBuffers(mEntries.begin(), mEntries.end());
858 mCount = 0;
859 return OK;
860 }
861
dump(int fd,const Vector<String16> &)862 status_t Camera2Device::MetadataQueue::dump(int fd,
863 const Vector<String16>& /*args*/) {
864 ATRACE_CALL();
865 String8 result;
866 status_t notLocked;
867 notLocked = mMutex.tryLock();
868 if (notLocked) {
869 result.append(" (Unable to lock queue mutex)\n");
870 }
871 result.appendFormat(" Current frame number: %d\n", mFrameCount);
872 if (mStreamSlotCount == 0) {
873 result.append(" Stream slot: Empty\n");
874 write(fd, result.string(), result.size());
875 } else {
876 result.appendFormat(" Stream slot: %zu entries\n",
877 mStreamSlot.size());
878 int i = 0;
879 for (List<camera_metadata_t*>::iterator r = mStreamSlot.begin();
880 r != mStreamSlot.end(); r++) {
881 result = String8::format(" Stream slot buffer %d:\n", i);
882 write(fd, result.string(), result.size());
883 dump_indented_camera_metadata(*r, fd, 2, 10);
884 i++;
885 }
886 }
887 if (mEntries.size() == 0) {
888 result = " Main queue is empty\n";
889 write(fd, result.string(), result.size());
890 } else {
891 result = String8::format(" Main queue has %zu entries:\n",
892 mEntries.size());
893 int i = 0;
894 for (List<camera_metadata_t*>::iterator r = mEntries.begin();
895 r != mEntries.end(); r++) {
896 result = String8::format(" Queue entry %d:\n", i);
897 write(fd, result.string(), result.size());
898 dump_indented_camera_metadata(*r, fd, 2, 10);
899 i++;
900 }
901 }
902
903 if (notLocked == 0) {
904 mMutex.unlock();
905 }
906
907 return OK;
908 }
909
signalConsumerLocked()910 status_t Camera2Device::MetadataQueue::signalConsumerLocked() {
911 ATRACE_CALL();
912 status_t res = OK;
913 notEmpty.signal();
914 if (mSignalConsumer && mHal2Device != NULL) {
915 mSignalConsumer = false;
916
917 mMutex.unlock();
918 ALOGV("%s: Signaling consumer", __FUNCTION__);
919 res = mHal2Device->ops->notify_request_queue_not_empty(mHal2Device);
920 mMutex.lock();
921 }
922 return res;
923 }
924
freeBuffers(List<camera_metadata_t * >::iterator start,List<camera_metadata_t * >::iterator end)925 status_t Camera2Device::MetadataQueue::freeBuffers(
926 List<camera_metadata_t*>::iterator start,
927 List<camera_metadata_t*>::iterator end)
928 {
929 ATRACE_CALL();
930 while (start != end) {
931 free_camera_metadata(*start);
932 start = mStreamSlot.erase(start);
933 }
934 return OK;
935 }
936
getInstance(const camera2_request_queue_src_ops_t * q)937 Camera2Device::MetadataQueue* Camera2Device::MetadataQueue::getInstance(
938 const camera2_request_queue_src_ops_t *q)
939 {
940 const MetadataQueue* cmq = static_cast<const MetadataQueue*>(q);
941 return const_cast<MetadataQueue*>(cmq);
942 }
943
getInstance(const camera2_frame_queue_dst_ops_t * q)944 Camera2Device::MetadataQueue* Camera2Device::MetadataQueue::getInstance(
945 const camera2_frame_queue_dst_ops_t *q)
946 {
947 const MetadataQueue* cmq = static_cast<const MetadataQueue*>(q);
948 return const_cast<MetadataQueue*>(cmq);
949 }
950
consumer_buffer_count(const camera2_request_queue_src_ops_t * q)951 int Camera2Device::MetadataQueue::consumer_buffer_count(
952 const camera2_request_queue_src_ops_t *q)
953 {
954 MetadataQueue *queue = getInstance(q);
955 return queue->getBufferCount();
956 }
957
consumer_dequeue(const camera2_request_queue_src_ops_t * q,camera_metadata_t ** buffer)958 int Camera2Device::MetadataQueue::consumer_dequeue(
959 const camera2_request_queue_src_ops_t *q,
960 camera_metadata_t **buffer)
961 {
962 MetadataQueue *queue = getInstance(q);
963 return queue->dequeue(buffer, true);
964 }
965
consumer_free(const camera2_request_queue_src_ops_t * q,camera_metadata_t * old_buffer)966 int Camera2Device::MetadataQueue::consumer_free(
967 const camera2_request_queue_src_ops_t *q,
968 camera_metadata_t *old_buffer)
969 {
970 ATRACE_CALL();
971 MetadataQueue *queue = getInstance(q);
972 (void)queue;
973 free_camera_metadata(old_buffer);
974 return OK;
975 }
976
producer_dequeue(const camera2_frame_queue_dst_ops_t *,size_t entries,size_t bytes,camera_metadata_t ** buffer)977 int Camera2Device::MetadataQueue::producer_dequeue(
978 const camera2_frame_queue_dst_ops_t * /*q*/,
979 size_t entries, size_t bytes,
980 camera_metadata_t **buffer)
981 {
982 ATRACE_CALL();
983 camera_metadata_t *new_buffer =
984 allocate_camera_metadata(entries, bytes);
985 if (new_buffer == NULL) return NO_MEMORY;
986 *buffer = new_buffer;
987 return OK;
988 }
989
producer_cancel(const camera2_frame_queue_dst_ops_t *,camera_metadata_t * old_buffer)990 int Camera2Device::MetadataQueue::producer_cancel(
991 const camera2_frame_queue_dst_ops_t * /*q*/,
992 camera_metadata_t *old_buffer)
993 {
994 ATRACE_CALL();
995 free_camera_metadata(old_buffer);
996 return OK;
997 }
998
producer_enqueue(const camera2_frame_queue_dst_ops_t * q,camera_metadata_t * filled_buffer)999 int Camera2Device::MetadataQueue::producer_enqueue(
1000 const camera2_frame_queue_dst_ops_t *q,
1001 camera_metadata_t *filled_buffer)
1002 {
1003 MetadataQueue *queue = getInstance(q);
1004 return queue->enqueue(filled_buffer);
1005 }
1006
1007 /**
1008 * Camera2Device::StreamAdapter
1009 */
1010
1011 #ifndef container_of
1012 #define container_of(ptr, type, member) \
1013 (type *)((char*)(ptr) - offsetof(type, member))
1014 #endif
1015
StreamAdapter(camera2_device_t * d)1016 Camera2Device::StreamAdapter::StreamAdapter(camera2_device_t *d):
1017 mState(RELEASED),
1018 mHal2Device(d),
1019 mId(-1),
1020 mWidth(0), mHeight(0), mFormat(0), mSize(0), mUsage(0),
1021 mMaxProducerBuffers(0), mMaxConsumerBuffers(0),
1022 mTotalBuffers(0),
1023 mFormatRequested(0),
1024 mActiveBuffers(0),
1025 mFrameCount(0),
1026 mLastTimestamp(0)
1027 {
1028 camera2_stream_ops::dequeue_buffer = dequeue_buffer;
1029 camera2_stream_ops::enqueue_buffer = enqueue_buffer;
1030 camera2_stream_ops::cancel_buffer = cancel_buffer;
1031 camera2_stream_ops::set_crop = set_crop;
1032 }
1033
~StreamAdapter()1034 Camera2Device::StreamAdapter::~StreamAdapter() {
1035 ATRACE_CALL();
1036 if (mState != RELEASED) {
1037 release();
1038 }
1039 }
1040
connectToDevice(sp<ANativeWindow> consumer,uint32_t width,uint32_t height,int format,size_t size)1041 status_t Camera2Device::StreamAdapter::connectToDevice(
1042 sp<ANativeWindow> consumer,
1043 uint32_t width, uint32_t height, int format, size_t size) {
1044 ATRACE_CALL();
1045 status_t res;
1046 ALOGV("%s: E", __FUNCTION__);
1047
1048 if (mState != RELEASED) return INVALID_OPERATION;
1049 if (consumer == NULL) {
1050 ALOGE("%s: Null consumer passed to stream adapter", __FUNCTION__);
1051 return BAD_VALUE;
1052 }
1053
1054 ALOGV("%s: New stream parameters %d x %d, format 0x%x, size %zu",
1055 __FUNCTION__, width, height, format, size);
1056
1057 mConsumerInterface = consumer;
1058 mWidth = width;
1059 mHeight = height;
1060 mSize = (format == HAL_PIXEL_FORMAT_BLOB) ? size : 0;
1061 mFormatRequested = format;
1062
1063 // Allocate device-side stream interface
1064
1065 uint32_t id;
1066 uint32_t formatActual;
1067 uint32_t usage;
1068 uint32_t maxBuffers = 2;
1069 res = mHal2Device->ops->allocate_stream(mHal2Device,
1070 mWidth, mHeight, mFormatRequested, getStreamOps(),
1071 &id, &formatActual, &usage, &maxBuffers);
1072 if (res != OK) {
1073 ALOGE("%s: Device stream allocation failed: %s (%d)",
1074 __FUNCTION__, strerror(-res), res);
1075 return res;
1076 }
1077
1078 ALOGV("%s: Allocated stream id %d, actual format 0x%x, "
1079 "usage 0x%x, producer wants %d buffers", __FUNCTION__,
1080 id, formatActual, usage, maxBuffers);
1081
1082 mId = id;
1083 mFormat = formatActual;
1084 mUsage = usage;
1085 mMaxProducerBuffers = maxBuffers;
1086
1087 mState = ALLOCATED;
1088
1089 // Configure consumer-side ANativeWindow interface
1090 res = native_window_api_connect(mConsumerInterface.get(),
1091 NATIVE_WINDOW_API_CAMERA);
1092 if (res != OK) {
1093 ALOGE("%s: Unable to connect to native window for stream %d",
1094 __FUNCTION__, mId);
1095
1096 return res;
1097 }
1098
1099 mState = CONNECTED;
1100
1101 res = native_window_set_usage(mConsumerInterface.get(), mUsage);
1102 if (res != OK) {
1103 ALOGE("%s: Unable to configure usage %08x for stream %d",
1104 __FUNCTION__, mUsage, mId);
1105 return res;
1106 }
1107
1108 res = native_window_set_scaling_mode(mConsumerInterface.get(),
1109 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1110 if (res != OK) {
1111 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
1112 __FUNCTION__, strerror(-res), res);
1113 return res;
1114 }
1115
1116 res = setTransform(0);
1117 if (res != OK) {
1118 return res;
1119 }
1120
1121 if (mFormat == HAL_PIXEL_FORMAT_BLOB) {
1122 res = native_window_set_buffers_dimensions(mConsumerInterface.get(),
1123 mSize, 1);
1124 if (res != OK) {
1125 ALOGE("%s: Unable to configure compressed stream buffer dimensions"
1126 " %d x %d, size %zu for stream %d",
1127 __FUNCTION__, mWidth, mHeight, mSize, mId);
1128 return res;
1129 }
1130 } else {
1131 res = native_window_set_buffers_dimensions(mConsumerInterface.get(),
1132 mWidth, mHeight);
1133 if (res != OK) {
1134 ALOGE("%s: Unable to configure stream buffer dimensions"
1135 " %d x %d for stream %d",
1136 __FUNCTION__, mWidth, mHeight, mId);
1137 return res;
1138 }
1139 }
1140
1141 res = native_window_set_buffers_format(mConsumerInterface.get(), mFormat);
1142 if (res != OK) {
1143 ALOGE("%s: Unable to configure stream buffer format"
1144 " %#x for stream %d",
1145 __FUNCTION__, mFormat, mId);
1146 return res;
1147 }
1148
1149 int maxConsumerBuffers;
1150 res = mConsumerInterface->query(mConsumerInterface.get(),
1151 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
1152 if (res != OK) {
1153 ALOGE("%s: Unable to query consumer undequeued"
1154 " buffer count for stream %d", __FUNCTION__, mId);
1155 return res;
1156 }
1157 mMaxConsumerBuffers = maxConsumerBuffers;
1158
1159 ALOGV("%s: Consumer wants %d buffers", __FUNCTION__,
1160 mMaxConsumerBuffers);
1161
1162 mTotalBuffers = mMaxConsumerBuffers + mMaxProducerBuffers;
1163 mActiveBuffers = 0;
1164 mFrameCount = 0;
1165 mLastTimestamp = 0;
1166
1167 res = native_window_set_buffer_count(mConsumerInterface.get(),
1168 mTotalBuffers);
1169 if (res != OK) {
1170 ALOGE("%s: Unable to set buffer count for stream %d",
1171 __FUNCTION__, mId);
1172 return res;
1173 }
1174
1175 // Register allocated buffers with HAL device
1176 buffer_handle_t *buffers = new buffer_handle_t[mTotalBuffers];
1177 ANativeWindowBuffer **anwBuffers = new ANativeWindowBuffer*[mTotalBuffers];
1178 uint32_t bufferIdx = 0;
1179 for (; bufferIdx < mTotalBuffers; bufferIdx++) {
1180 res = native_window_dequeue_buffer_and_wait(mConsumerInterface.get(),
1181 &anwBuffers[bufferIdx]);
1182 if (res != OK) {
1183 ALOGE("%s: Unable to dequeue buffer %d for initial registration for "
1184 "stream %d", __FUNCTION__, bufferIdx, mId);
1185 goto cleanUpBuffers;
1186 }
1187
1188 buffers[bufferIdx] = anwBuffers[bufferIdx]->handle;
1189 ALOGV("%s: Buffer %p allocated", __FUNCTION__, (void*)buffers[bufferIdx]);
1190 }
1191
1192 ALOGV("%s: Registering %d buffers with camera HAL", __FUNCTION__, mTotalBuffers);
1193 res = mHal2Device->ops->register_stream_buffers(mHal2Device,
1194 mId,
1195 mTotalBuffers,
1196 buffers);
1197 if (res != OK) {
1198 ALOGE("%s: Unable to register buffers with HAL device for stream %d",
1199 __FUNCTION__, mId);
1200 } else {
1201 mState = ACTIVE;
1202 }
1203
1204 cleanUpBuffers:
1205 ALOGV("%s: Cleaning up %d buffers", __FUNCTION__, bufferIdx);
1206 for (uint32_t i = 0; i < bufferIdx; i++) {
1207 res = mConsumerInterface->cancelBuffer(mConsumerInterface.get(),
1208 anwBuffers[i], -1);
1209 if (res != OK) {
1210 ALOGE("%s: Unable to cancel buffer %d after registration",
1211 __FUNCTION__, i);
1212 }
1213 }
1214 delete[] anwBuffers;
1215 delete[] buffers;
1216
1217 return res;
1218 }
1219
release()1220 status_t Camera2Device::StreamAdapter::release() {
1221 ATRACE_CALL();
1222 status_t res;
1223 ALOGV("%s: Releasing stream %d (%d x %d, format %d)", __FUNCTION__, mId,
1224 mWidth, mHeight, mFormat);
1225 if (mState >= ALLOCATED) {
1226 res = mHal2Device->ops->release_stream(mHal2Device, mId);
1227 if (res != OK) {
1228 ALOGE("%s: Unable to release stream %d",
1229 __FUNCTION__, mId);
1230 return res;
1231 }
1232 }
1233 if (mState >= CONNECTED) {
1234 res = native_window_api_disconnect(mConsumerInterface.get(),
1235 NATIVE_WINDOW_API_CAMERA);
1236
1237 /* this is not an error. if client calling process dies,
1238 the window will also die and all calls to it will return
1239 DEAD_OBJECT, thus it's already "disconnected" */
1240 if (res == DEAD_OBJECT) {
1241 ALOGW("%s: While disconnecting stream %d from native window, the"
1242 " native window died from under us", __FUNCTION__, mId);
1243 }
1244 else if (res != OK) {
1245 ALOGE("%s: Unable to disconnect stream %d from native window (error %d %s)",
1246 __FUNCTION__, mId, res, strerror(-res));
1247 return res;
1248 }
1249 }
1250 mId = -1;
1251 mState = RELEASED;
1252 return OK;
1253 }
1254
setTransform(int transform)1255 status_t Camera2Device::StreamAdapter::setTransform(int transform) {
1256 ATRACE_CALL();
1257 status_t res;
1258 if (mState < CONNECTED) {
1259 ALOGE("%s: Cannot set transform on unconnected stream", __FUNCTION__);
1260 return INVALID_OPERATION;
1261 }
1262 res = native_window_set_buffers_transform(mConsumerInterface.get(),
1263 transform);
1264 if (res != OK) {
1265 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
1266 __FUNCTION__, transform, strerror(-res), res);
1267 }
1268 return res;
1269 }
1270
dump(int fd,const Vector<String16> &)1271 status_t Camera2Device::StreamAdapter::dump(int fd,
1272 const Vector<String16>& /*args*/) {
1273 ATRACE_CALL();
1274 String8 result = String8::format(" Stream %d: %d x %d, format 0x%x\n",
1275 mId, mWidth, mHeight, mFormat);
1276 result.appendFormat(" size %zu, usage 0x%x, requested format 0x%x\n",
1277 mSize, mUsage, mFormatRequested);
1278 result.appendFormat(" total buffers: %d, dequeued buffers: %d\n",
1279 mTotalBuffers, mActiveBuffers);
1280 result.appendFormat(" frame count: %d, last timestamp %" PRId64 "\n",
1281 mFrameCount, mLastTimestamp);
1282 write(fd, result.string(), result.size());
1283 return OK;
1284 }
1285
getStreamOps()1286 const camera2_stream_ops *Camera2Device::StreamAdapter::getStreamOps() {
1287 return static_cast<camera2_stream_ops *>(this);
1288 }
1289
toANW(const camera2_stream_ops_t * w)1290 ANativeWindow* Camera2Device::StreamAdapter::toANW(
1291 const camera2_stream_ops_t *w) {
1292 return static_cast<const StreamAdapter*>(w)->mConsumerInterface.get();
1293 }
1294
dequeue_buffer(const camera2_stream_ops_t * w,buffer_handle_t ** buffer)1295 int Camera2Device::StreamAdapter::dequeue_buffer(const camera2_stream_ops_t *w,
1296 buffer_handle_t** buffer) {
1297 ATRACE_CALL();
1298 int res;
1299 StreamAdapter* stream =
1300 const_cast<StreamAdapter*>(static_cast<const StreamAdapter*>(w));
1301 if (stream->mState != ACTIVE) {
1302 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, stream->mState);
1303 return INVALID_OPERATION;
1304 }
1305
1306 ANativeWindow *a = toANW(w);
1307 ANativeWindowBuffer* anb;
1308 res = native_window_dequeue_buffer_and_wait(a, &anb);
1309 if (res != OK) {
1310 ALOGE("Stream %d dequeue: Error from native_window: %s (%d)", stream->mId,
1311 strerror(-res), res);
1312 return res;
1313 }
1314
1315 *buffer = &(anb->handle);
1316 stream->mActiveBuffers++;
1317
1318 ALOGVV("Stream %d dequeue: Buffer %p dequeued", stream->mId, (void*)(**buffer));
1319 return res;
1320 }
1321
enqueue_buffer(const camera2_stream_ops_t * w,int64_t timestamp,buffer_handle_t * buffer)1322 int Camera2Device::StreamAdapter::enqueue_buffer(const camera2_stream_ops_t* w,
1323 int64_t timestamp,
1324 buffer_handle_t* buffer) {
1325 ATRACE_CALL();
1326 StreamAdapter *stream =
1327 const_cast<StreamAdapter*>(static_cast<const StreamAdapter*>(w));
1328 stream->mFrameCount++;
1329 ALOGVV("Stream %d enqueue: Frame %d (%p) captured at %lld ns",
1330 stream->mId, stream->mFrameCount, (void*)(*buffer), timestamp);
1331 int state = stream->mState;
1332 if (state != ACTIVE) {
1333 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
1334 return INVALID_OPERATION;
1335 }
1336 ANativeWindow *a = toANW(w);
1337 status_t err;
1338
1339 err = native_window_set_buffers_timestamp(a, timestamp);
1340 if (err != OK) {
1341 ALOGE("%s: Error setting timestamp on native window: %s (%d)",
1342 __FUNCTION__, strerror(-err), err);
1343 return err;
1344 }
1345 err = a->queueBuffer(a,
1346 container_of(buffer, ANativeWindowBuffer, handle), -1);
1347 if (err != OK) {
1348 ALOGE("%s: Error queueing buffer to native window: %s (%d)",
1349 __FUNCTION__, strerror(-err), err);
1350 return err;
1351 }
1352
1353 stream->mActiveBuffers--;
1354 stream->mLastTimestamp = timestamp;
1355 return OK;
1356 }
1357
cancel_buffer(const camera2_stream_ops_t * w,buffer_handle_t * buffer)1358 int Camera2Device::StreamAdapter::cancel_buffer(const camera2_stream_ops_t* w,
1359 buffer_handle_t* buffer) {
1360 ATRACE_CALL();
1361 StreamAdapter *stream =
1362 const_cast<StreamAdapter*>(static_cast<const StreamAdapter*>(w));
1363 ALOGVV("Stream %d cancel: Buffer %p",
1364 stream->mId, (void*)(*buffer));
1365 if (stream->mState != ACTIVE) {
1366 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, stream->mState);
1367 return INVALID_OPERATION;
1368 }
1369
1370 ANativeWindow *a = toANW(w);
1371 int err = a->cancelBuffer(a,
1372 container_of(buffer, ANativeWindowBuffer, handle), -1);
1373 if (err != OK) {
1374 ALOGE("%s: Error canceling buffer to native window: %s (%d)",
1375 __FUNCTION__, strerror(-err), err);
1376 return err;
1377 }
1378
1379 stream->mActiveBuffers--;
1380 return OK;
1381 }
1382
set_crop(const camera2_stream_ops_t * w,int left,int top,int right,int bottom)1383 int Camera2Device::StreamAdapter::set_crop(const camera2_stream_ops_t* w,
1384 int left, int top, int right, int bottom) {
1385 ATRACE_CALL();
1386 int state = static_cast<const StreamAdapter*>(w)->mState;
1387 if (state != ACTIVE) {
1388 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
1389 return INVALID_OPERATION;
1390 }
1391 ANativeWindow *a = toANW(w);
1392 android_native_rect_t crop = { left, top, right, bottom };
1393 return native_window_set_crop(a, &crop);
1394 }
1395
1396 /**
1397 * Camera2Device::ReprocessStreamAdapter
1398 */
1399
1400 #ifndef container_of
1401 #define container_of(ptr, type, member) \
1402 (type *)((char*)(ptr) - offsetof(type, member))
1403 #endif
1404
ReprocessStreamAdapter(camera2_device_t * d)1405 Camera2Device::ReprocessStreamAdapter::ReprocessStreamAdapter(camera2_device_t *d):
1406 mState(RELEASED),
1407 mHal2Device(d),
1408 mId(-1),
1409 mWidth(0), mHeight(0), mFormat(0),
1410 mActiveBuffers(0),
1411 mFrameCount(0)
1412 {
1413 ATRACE_CALL();
1414 camera2_stream_in_ops::acquire_buffer = acquire_buffer;
1415 camera2_stream_in_ops::release_buffer = release_buffer;
1416 }
1417
~ReprocessStreamAdapter()1418 Camera2Device::ReprocessStreamAdapter::~ReprocessStreamAdapter() {
1419 ATRACE_CALL();
1420 if (mState != RELEASED) {
1421 release();
1422 }
1423 }
1424
connectToDevice(const sp<StreamAdapter> & outputStream)1425 status_t Camera2Device::ReprocessStreamAdapter::connectToDevice(
1426 const sp<StreamAdapter> &outputStream) {
1427 ATRACE_CALL();
1428 status_t res;
1429 ALOGV("%s: E", __FUNCTION__);
1430
1431 if (mState != RELEASED) return INVALID_OPERATION;
1432 if (outputStream == NULL) {
1433 ALOGE("%s: Null base stream passed to reprocess stream adapter",
1434 __FUNCTION__);
1435 return BAD_VALUE;
1436 }
1437
1438 mBaseStream = outputStream;
1439 mWidth = outputStream->getWidth();
1440 mHeight = outputStream->getHeight();
1441 mFormat = outputStream->getFormat();
1442
1443 ALOGV("%s: New reprocess stream parameters %d x %d, format 0x%x",
1444 __FUNCTION__, mWidth, mHeight, mFormat);
1445
1446 // Allocate device-side stream interface
1447
1448 uint32_t id;
1449 res = mHal2Device->ops->allocate_reprocess_stream_from_stream(mHal2Device,
1450 outputStream->getId(), getStreamOps(),
1451 &id);
1452 if (res != OK) {
1453 ALOGE("%s: Device reprocess stream allocation failed: %s (%d)",
1454 __FUNCTION__, strerror(-res), res);
1455 return res;
1456 }
1457
1458 ALOGV("%s: Allocated reprocess stream id %d based on stream %d",
1459 __FUNCTION__, id, outputStream->getId());
1460
1461 mId = id;
1462
1463 mState = ACTIVE;
1464
1465 return OK;
1466 }
1467
release()1468 status_t Camera2Device::ReprocessStreamAdapter::release() {
1469 ATRACE_CALL();
1470 status_t res;
1471 ALOGV("%s: Releasing stream %d", __FUNCTION__, mId);
1472 if (mState >= ACTIVE) {
1473 res = mHal2Device->ops->release_reprocess_stream(mHal2Device, mId);
1474 if (res != OK) {
1475 ALOGE("%s: Unable to release stream %d",
1476 __FUNCTION__, mId);
1477 return res;
1478 }
1479 }
1480
1481 List<QueueEntry>::iterator s;
1482 for (s = mQueue.begin(); s != mQueue.end(); s++) {
1483 sp<BufferReleasedListener> listener = s->releaseListener.promote();
1484 if (listener != 0) listener->onBufferReleased(s->handle);
1485 }
1486 for (s = mInFlightQueue.begin(); s != mInFlightQueue.end(); s++) {
1487 sp<BufferReleasedListener> listener = s->releaseListener.promote();
1488 if (listener != 0) listener->onBufferReleased(s->handle);
1489 }
1490 mQueue.clear();
1491 mInFlightQueue.clear();
1492
1493 mState = RELEASED;
1494 return OK;
1495 }
1496
pushIntoStream(buffer_handle_t * handle,const wp<BufferReleasedListener> & releaseListener)1497 status_t Camera2Device::ReprocessStreamAdapter::pushIntoStream(
1498 buffer_handle_t *handle, const wp<BufferReleasedListener> &releaseListener) {
1499 ATRACE_CALL();
1500 // TODO: Some error checking here would be nice
1501 ALOGV("%s: Pushing buffer %p to stream", __FUNCTION__, (void*)(*handle));
1502
1503 QueueEntry entry;
1504 entry.handle = handle;
1505 entry.releaseListener = releaseListener;
1506 mQueue.push_back(entry);
1507 return OK;
1508 }
1509
dump(int fd,const Vector<String16> &)1510 status_t Camera2Device::ReprocessStreamAdapter::dump(int fd,
1511 const Vector<String16>& /*args*/) {
1512 ATRACE_CALL();
1513 String8 result =
1514 String8::format(" Reprocess stream %d: %d x %d, fmt 0x%x\n",
1515 mId, mWidth, mHeight, mFormat);
1516 result.appendFormat(" acquired buffers: %d\n",
1517 mActiveBuffers);
1518 result.appendFormat(" frame count: %d\n",
1519 mFrameCount);
1520 write(fd, result.string(), result.size());
1521 return OK;
1522 }
1523
getStreamOps()1524 const camera2_stream_in_ops *Camera2Device::ReprocessStreamAdapter::getStreamOps() {
1525 return static_cast<camera2_stream_in_ops *>(this);
1526 }
1527
acquire_buffer(const camera2_stream_in_ops_t * w,buffer_handle_t ** buffer)1528 int Camera2Device::ReprocessStreamAdapter::acquire_buffer(
1529 const camera2_stream_in_ops_t *w,
1530 buffer_handle_t** buffer) {
1531 ATRACE_CALL();
1532
1533 ReprocessStreamAdapter* stream =
1534 const_cast<ReprocessStreamAdapter*>(
1535 static_cast<const ReprocessStreamAdapter*>(w));
1536 if (stream->mState != ACTIVE) {
1537 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, stream->mState);
1538 return INVALID_OPERATION;
1539 }
1540
1541 if (stream->mQueue.empty()) {
1542 *buffer = NULL;
1543 return OK;
1544 }
1545
1546 QueueEntry &entry = *(stream->mQueue.begin());
1547
1548 *buffer = entry.handle;
1549
1550 stream->mInFlightQueue.push_back(entry);
1551 stream->mQueue.erase(stream->mQueue.begin());
1552
1553 stream->mActiveBuffers++;
1554
1555 ALOGV("Stream %d acquire: Buffer %p acquired", stream->mId,
1556 (void*)(**buffer));
1557 return OK;
1558 }
1559
release_buffer(const camera2_stream_in_ops_t * w,buffer_handle_t * buffer)1560 int Camera2Device::ReprocessStreamAdapter::release_buffer(
1561 const camera2_stream_in_ops_t* w,
1562 buffer_handle_t* buffer) {
1563 ATRACE_CALL();
1564 ReprocessStreamAdapter *stream =
1565 const_cast<ReprocessStreamAdapter*>(
1566 static_cast<const ReprocessStreamAdapter*>(w) );
1567 stream->mFrameCount++;
1568 ALOGV("Reprocess stream %d release: Frame %d (%p)",
1569 stream->mId, stream->mFrameCount, (void*)*buffer);
1570 int state = stream->mState;
1571 if (state != ACTIVE) {
1572 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
1573 return INVALID_OPERATION;
1574 }
1575 stream->mActiveBuffers--;
1576
1577 List<QueueEntry>::iterator s;
1578 for (s = stream->mInFlightQueue.begin(); s != stream->mInFlightQueue.end(); s++) {
1579 if ( s->handle == buffer ) break;
1580 }
1581 if (s == stream->mInFlightQueue.end()) {
1582 ALOGE("%s: Can't find buffer %p in in-flight list!", __FUNCTION__,
1583 buffer);
1584 return INVALID_OPERATION;
1585 }
1586
1587 sp<BufferReleasedListener> listener = s->releaseListener.promote();
1588 if (listener != 0) {
1589 listener->onBufferReleased(s->handle);
1590 } else {
1591 ALOGE("%s: Can't free buffer - missing listener", __FUNCTION__);
1592 }
1593 stream->mInFlightQueue.erase(s);
1594
1595 return OK;
1596 }
1597
1598 // camera 2 devices don't support reprocessing
createInputStream(uint32_t width,uint32_t height,int format,int * id)1599 status_t Camera2Device::createInputStream(
1600 uint32_t width, uint32_t height, int format, int *id) {
1601 ALOGE("%s: camera 2 devices don't support reprocessing", __FUNCTION__);
1602 return INVALID_OPERATION;
1603 }
1604
1605 // camera 2 devices don't support reprocessing
getInputBufferProducer(sp<IGraphicBufferProducer> * producer)1606 status_t Camera2Device::getInputBufferProducer(
1607 sp<IGraphicBufferProducer> *producer) {
1608 ALOGE("%s: camera 2 devices don't support reprocessing", __FUNCTION__);
1609 return INVALID_OPERATION;
1610 }
1611
1612 }; // namespace android
1613