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