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