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