Zvikomborero VIncent Zvikaramba | 9377791 | 2016-07-24 00:51:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014, The CyanogenMod Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /** |
| 18 | * @file CameraWrapper.cpp |
| 19 | * |
| 20 | * This file wraps a vendor camera module. |
| 21 | * |
| 22 | */ |
| 23 | |
Zvikomborero VIncent Zvikaramba | 9d51580 | 2016-08-18 13:51:48 -0400 | [diff] [blame] | 24 | #define LOG_NDEBUG 0 |
Zvikomborero VIncent Zvikaramba | 9377791 | 2016-07-24 00:51:04 -0400 | [diff] [blame] | 25 | |
| 26 | #define LOG_TAG "CameraWrapper" |
| 27 | #include <cutils/log.h> |
| 28 | |
| 29 | #include <utils/threads.h> |
| 30 | #include <utils/String8.h> |
| 31 | #include <hardware/hardware.h> |
| 32 | #include <hardware/camera.h> |
| 33 | #include <camera/Camera.h> |
| 34 | #include <camera/CameraParameters.h> |
| 35 | |
| 36 | static android::Mutex gCameraWrapperLock; |
| 37 | static camera_module_t *gVendorModule = 0; |
| 38 | |
| 39 | static char **fixed_set_params = NULL; |
| 40 | |
| 41 | static int camera_device_open(const hw_module_t *module, const char *name, |
| 42 | hw_device_t **device); |
| 43 | static int camera_get_number_of_cameras(void); |
| 44 | static int camera_get_camera_info(int camera_id, struct camera_info *info); |
| 45 | static int camera_send_command(struct camera_device *device, int32_t cmd, |
| 46 | int32_t arg1, int32_t arg2); |
| 47 | |
| 48 | static struct hw_module_methods_t camera_module_methods = { |
Zvikomborero VIncent Zvikaramba | 9d51580 | 2016-08-18 13:51:48 -0400 | [diff] [blame] | 49 | .open = camera_device_open |
Zvikomborero VIncent Zvikaramba | 9377791 | 2016-07-24 00:51:04 -0400 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | camera_module_t HAL_MODULE_INFO_SYM = { |
| 53 | .common = { |
| 54 | .tag = HARDWARE_MODULE_TAG, |
| 55 | .module_api_version = CAMERA_MODULE_API_VERSION_1_0, |
| 56 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
| 57 | .id = CAMERA_HARDWARE_MODULE_ID, |
| 58 | .name = "Samsung MSM8916 Camera Wrapper", |
| 59 | .author = "The CyanogenMod Project", |
| 60 | .methods = &camera_module_methods, |
| 61 | .dso = NULL, /* remove compilation warnings */ |
| 62 | .reserved = {0}, /* remove compilation warnings */ |
| 63 | }, |
Zvikomborero VIncent Zvikaramba | 9377791 | 2016-07-24 00:51:04 -0400 | [diff] [blame] | 64 | .get_number_of_cameras = camera_get_number_of_cameras, |
| 65 | .get_camera_info = camera_get_camera_info, |
| 66 | .set_callbacks = NULL, /* remove compilation warnings */ |
| 67 | .get_vendor_tag_ops = NULL, /* remove compilation warnings */ |
| 68 | .open_legacy = NULL, /* remove compilation warnings */ |
| 69 | .reserved = {0}, /* remove compilation warnings */ |
| 70 | }; |
| 71 | |
| 72 | typedef struct wrapper_camera_device { |
| 73 | camera_device_t base; |
| 74 | int id; |
| 75 | camera_device_t *vendor; |
| 76 | } wrapper_camera_device_t; |
| 77 | |
| 78 | #define VENDOR_CALL(device, func, ...) ({ \ |
| 79 | wrapper_camera_device_t *__wrapper_dev = (wrapper_camera_device_t*) device; \ |
| 80 | __wrapper_dev->vendor->ops->func(__wrapper_dev->vendor, ##__VA_ARGS__); \ |
| 81 | }) |
| 82 | |
| 83 | #define CAMERA_ID(device) (((wrapper_camera_device_t *)(device))->id) |
| 84 | |
| 85 | static int check_vendor_module() |
| 86 | { |
| 87 | int rv = 0; |
| 88 | ALOGV("%s", __FUNCTION__); |
| 89 | |
| 90 | if (gVendorModule) |
| 91 | return 0; |
| 92 | |
| 93 | rv = hw_get_module_by_class("camera", "vendor", |
| 94 | (const hw_module_t**)&gVendorModule); |
| 95 | if (rv) |
| 96 | ALOGE("failed to open vendor camera module"); |
| 97 | return rv; |
| 98 | } |
| 99 | |
Zvikomborero VIncent Zvikaramba | 9377791 | 2016-07-24 00:51:04 -0400 | [diff] [blame] | 100 | #define KEY_VIDEO_HFR_VALUES "video-hfr-values" |
| 101 | |
Zvikomborero VIncent Zvikaramba | 9d51580 | 2016-08-18 13:51:48 -0400 | [diff] [blame] | 102 | const static char * iso_values[] = {"auto,ISO_HJR,ISO100,ISO200,ISO400,ISO800"}; |
Zvikomborero VIncent Zvikaramba | 9377791 | 2016-07-24 00:51:04 -0400 | [diff] [blame] | 103 | |
| 104 | static char *camera_fixup_getparams(int id, const char *settings) |
| 105 | { |
| 106 | android::CameraParameters params; |
| 107 | params.unflatten(android::String8(settings)); |
| 108 | |
| 109 | #if !LOG_NDEBUG |
| 110 | ALOGV("%s: original parameters:", __FUNCTION__); |
| 111 | params.dump(); |
| 112 | #endif |
| 113 | |
| 114 | // fix params here |
Zvikomborero VIncent Zvikaramba | 9d51580 | 2016-08-18 13:51:48 -0400 | [diff] [blame] | 115 | params.set(android::CameraParameters::KEY_SUPPORTED_ISO_MODES, iso_values[id]); |
Zvikomborero VIncent Zvikaramba | 9377791 | 2016-07-24 00:51:04 -0400 | [diff] [blame] | 116 | params.set(android::CameraParameters::KEY_EXPOSURE_COMPENSATION_STEP, "0.5"); |
Zvikomborero VIncent Zvikaramba | 9d51580 | 2016-08-18 13:51:48 -0400 | [diff] [blame] | 117 | params.set(android::CameraParameters::KEY_MIN_EXPOSURE_COMPENSATION, "-2"); |
| 118 | params.set(android::CameraParameters::KEY_MAX_EXPOSURE_COMPENSATION, "2"); |
| 119 | |
Zvikomborero VIncent Zvikaramba | 9377791 | 2016-07-24 00:51:04 -0400 | [diff] [blame] | 120 | /* If the vendor has HFR values but doesn't also expose that |
| 121 | * this can be turned off, fixup the params to tell the Camera |
| 122 | * that it really is okay to turn it off. |
| 123 | */ |
| 124 | const char *hfrValues = params.get(KEY_VIDEO_HFR_VALUES); |
| 125 | if (hfrValues && *hfrValues && ! strstr(hfrValues, "off")) { |
| 126 | char tmp[strlen(hfrValues) + 4 + 1]; |
| 127 | sprintf(tmp, "%s,off", hfrValues); |
| 128 | params.set(KEY_VIDEO_HFR_VALUES, tmp); |
| 129 | } |
Zvikomborero VIncent Zvikaramba | 9d51580 | 2016-08-18 13:51:48 -0400 | [diff] [blame] | 130 | |
| 131 | params.set("whitebalance-values", "auto,incandescent,fluorescent,daylight,cloudy-daylight"); |
| 132 | params.set("effect-values", "none,mono,negative,sepia"); |
Zvikomborero VIncent Zvikaramba | 9377791 | 2016-07-24 00:51:04 -0400 | [diff] [blame] | 133 | |
| 134 | android::String8 strParams = params.flatten(); |
| 135 | char *ret = strdup(strParams.string()); |
| 136 | |
| 137 | #if !LOG_NDEBUG |
| 138 | ALOGV("%s: fixed parameters:", __FUNCTION__); |
| 139 | params.dump(); |
| 140 | #endif |
| 141 | |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | static char *camera_fixup_setparams(struct camera_device *device, const char *settings) |
| 146 | { |
| 147 | int id = CAMERA_ID(device); |
| 148 | android::CameraParameters params; |
| 149 | params.unflatten(android::String8(settings)); |
| 150 | |
| 151 | #if !LOG_NDEBUG |
| 152 | ALOGV("%s: original parameters:", __FUNCTION__); |
| 153 | params.dump(); |
| 154 | #endif |
| 155 | |
Zvikomborero VIncent Zvikaramba | 9d51580 | 2016-08-18 13:51:48 -0400 | [diff] [blame] | 156 | // No need to fix-up ISO_HJR, it is the same for userspace and the camera lib |
| 157 | if (params.get("iso")) { |
| 158 | const char *isoMode = params.get(android::CameraParameters::KEY_ISO_MODE); |
| 159 | if (strcmp(isoMode, "ISO100") == 0) |
| 160 | params.set(android::CameraParameters::KEY_ISO_MODE, "100"); |
| 161 | else if (strcmp(isoMode, "ISO200") == 0) |
| 162 | params.set(android::CameraParameters::KEY_ISO_MODE, "200"); |
| 163 | else if (strcmp(isoMode, "ISO400") == 0) |
| 164 | params.set(android::CameraParameters::KEY_ISO_MODE, "400"); |
| 165 | else if (strcmp(isoMode, "ISO800") == 0) |
| 166 | params.set(android::CameraParameters::KEY_ISO_MODE, "800"); |
| 167 | } |
| 168 | |
| 169 | // fix params here |
| 170 | int video_width, video_height; |
| 171 | params.getPreviewSize(&video_width, &video_height); |
| 172 | if(video_width*video_height <= 960*540){ |
Zvikomborero VIncent Zvikaramba | 9377791 | 2016-07-24 00:51:04 -0400 | [diff] [blame] | 173 | params.set("preview-format", "yuv420p"); |
| 174 | } |
Zvikomborero VIncent Zvikaramba | 9d51580 | 2016-08-18 13:51:48 -0400 | [diff] [blame] | 175 | |
| 176 | params.set("scene-detect", "off"); |
| 177 | |
| 178 | bool isVideo = false; |
| 179 | if (params.get(android::CameraParameters::KEY_RECORDING_HINT)) |
| 180 | isVideo = !strcmp(params.get(android::CameraParameters::KEY_RECORDING_HINT), "true"); |
Zvikomborero VIncent Zvikaramba | 9377791 | 2016-07-24 00:51:04 -0400 | [diff] [blame] | 181 | |
Zvikomborero VIncent Zvikaramba | 9d51580 | 2016-08-18 13:51:48 -0400 | [diff] [blame] | 182 | if (id == 0) { |
| 183 | int camMode; |
| 184 | if (params.get(android::CameraParameters::KEY_SAMSUNG_CAMERA_MODE)) { |
| 185 | camMode = params.getInt(android::CameraParameters::KEY_SAMSUNG_CAMERA_MODE); |
| 186 | } else { |
| 187 | camMode = -1; |
| 188 | } |
Zvikomborero VIncent Zvikaramba | 9377791 | 2016-07-24 00:51:04 -0400 | [diff] [blame] | 189 | |
Zvikomborero VIncent Zvikaramba | 9d51580 | 2016-08-18 13:51:48 -0400 | [diff] [blame] | 190 | if (camMode == -1) { |
| 191 | params.set(android::CameraParameters::KEY_SAMSUNG_CAMERA_MODE, "1"); |
| 192 | } else { |
| 193 | params.set(android::CameraParameters::KEY_SAMSUNG_CAMERA_MODE, isVideo ? "1" : "0"); |
| 194 | } |
| 195 | } |
| 196 | |
Zvikomborero VIncent Zvikaramba | 9377791 | 2016-07-24 00:51:04 -0400 | [diff] [blame] | 197 | android::String8 strParams = params.flatten(); |
| 198 | |
| 199 | if (fixed_set_params[id]) |
| 200 | free(fixed_set_params[id]); |
| 201 | fixed_set_params[id] = strdup(strParams.string()); |
| 202 | char *ret = fixed_set_params[id]; |
| 203 | |
| 204 | #if !LOG_NDEBUG |
| 205 | ALOGV("%s: fixed parameters:", __FUNCTION__); |
| 206 | params.dump(); |
| 207 | #endif |
| 208 | |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | /******************************************************************* |
| 213 | * implementation of camera_device_ops functions |
| 214 | *******************************************************************/ |
| 215 | |
| 216 | static int camera_set_preview_window(struct camera_device *device, |
| 217 | struct preview_stream_ops *window) |
| 218 | { |
| 219 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 220 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 221 | |
| 222 | if (!device) |
| 223 | return -EINVAL; |
| 224 | |
| 225 | return VENDOR_CALL(device, set_preview_window, window); |
| 226 | } |
| 227 | |
| 228 | static void camera_set_callbacks(struct camera_device *device, |
| 229 | camera_notify_callback notify_cb, |
| 230 | camera_data_callback data_cb, |
| 231 | camera_data_timestamp_callback data_cb_timestamp, |
| 232 | camera_request_memory get_memory, |
| 233 | void *user) |
| 234 | { |
| 235 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 236 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 237 | |
| 238 | if (!device) |
| 239 | return; |
| 240 | |
| 241 | VENDOR_CALL(device, set_callbacks, notify_cb, data_cb, data_cb_timestamp, |
| 242 | get_memory, user); |
| 243 | } |
| 244 | |
| 245 | static void camera_enable_msg_type(struct camera_device *device, |
| 246 | int32_t msg_type) |
| 247 | { |
| 248 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 249 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 250 | |
| 251 | if (!device) |
| 252 | return; |
| 253 | |
| 254 | VENDOR_CALL(device, enable_msg_type, msg_type); |
| 255 | } |
| 256 | |
| 257 | static void camera_disable_msg_type(struct camera_device *device, |
| 258 | int32_t msg_type) |
| 259 | { |
| 260 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 261 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 262 | |
| 263 | if (!device) |
| 264 | return; |
| 265 | |
| 266 | VENDOR_CALL(device, disable_msg_type, msg_type); |
| 267 | } |
| 268 | |
| 269 | static int camera_msg_type_enabled(struct camera_device *device, |
| 270 | int32_t msg_type) |
| 271 | { |
| 272 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 273 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 274 | |
| 275 | if (!device) |
| 276 | return 0; |
| 277 | |
| 278 | return VENDOR_CALL(device, msg_type_enabled, msg_type); |
| 279 | } |
| 280 | |
| 281 | static int camera_start_preview(struct camera_device *device) |
| 282 | { |
| 283 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 284 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 285 | |
| 286 | if (!device) |
| 287 | return -EINVAL; |
| 288 | |
| 289 | return VENDOR_CALL(device, start_preview); |
| 290 | } |
| 291 | |
| 292 | static void camera_stop_preview(struct camera_device *device) |
| 293 | { |
| 294 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 295 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 296 | |
| 297 | if (!device) |
| 298 | return; |
| 299 | |
| 300 | VENDOR_CALL(device, stop_preview); |
| 301 | } |
| 302 | |
| 303 | static int camera_preview_enabled(struct camera_device *device) |
| 304 | { |
| 305 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 306 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 307 | |
| 308 | if (!device) |
| 309 | return -EINVAL; |
| 310 | |
| 311 | return VENDOR_CALL(device, preview_enabled); |
| 312 | } |
| 313 | |
| 314 | static int camera_store_meta_data_in_buffers(struct camera_device *device, |
| 315 | int enable) |
| 316 | { |
| 317 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 318 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 319 | |
| 320 | if (!device) |
| 321 | return -EINVAL; |
| 322 | |
| 323 | return VENDOR_CALL(device, store_meta_data_in_buffers, enable); |
| 324 | } |
| 325 | |
| 326 | static int camera_start_recording(struct camera_device *device) |
| 327 | { |
| 328 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 329 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 330 | |
| 331 | if (!device) |
| 332 | return EINVAL; |
| 333 | |
| 334 | return VENDOR_CALL(device, start_recording); |
| 335 | } |
| 336 | |
| 337 | static void camera_stop_recording(struct camera_device *device) |
| 338 | { |
| 339 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 340 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 341 | |
| 342 | if (!device) |
| 343 | return; |
| 344 | |
| 345 | VENDOR_CALL(device, stop_recording); |
| 346 | } |
| 347 | |
| 348 | static int camera_recording_enabled(struct camera_device *device) |
| 349 | { |
| 350 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 351 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 352 | |
| 353 | if (!device) |
| 354 | return -EINVAL; |
| 355 | |
| 356 | return VENDOR_CALL(device, recording_enabled); |
| 357 | } |
| 358 | |
| 359 | static void camera_release_recording_frame(struct camera_device *device, |
| 360 | const void *opaque) |
| 361 | { |
| 362 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 363 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 364 | |
| 365 | if (!device) |
| 366 | return; |
| 367 | |
| 368 | VENDOR_CALL(device, release_recording_frame, opaque); |
| 369 | } |
| 370 | |
| 371 | static int camera_auto_focus(struct camera_device *device) |
| 372 | { |
| 373 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 374 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 375 | |
| 376 | if (!device) |
| 377 | return -EINVAL; |
| 378 | |
| 379 | return VENDOR_CALL(device, auto_focus); |
| 380 | } |
| 381 | |
| 382 | static int camera_cancel_auto_focus(struct camera_device *device) |
| 383 | { |
| 384 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 385 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 386 | |
| 387 | if (!device) |
| 388 | return -EINVAL; |
| 389 | |
| 390 | return VENDOR_CALL(device, cancel_auto_focus); |
| 391 | } |
| 392 | |
| 393 | static int camera_take_picture(struct camera_device *device) |
| 394 | { |
| 395 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 396 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 397 | |
| 398 | if (!device) |
| 399 | return -EINVAL; |
| 400 | |
| 401 | return VENDOR_CALL(device, take_picture); |
| 402 | } |
| 403 | |
| 404 | static int camera_cancel_picture(struct camera_device *device) |
| 405 | { |
| 406 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 407 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 408 | |
| 409 | if (!device) |
| 410 | return -EINVAL; |
| 411 | |
| 412 | return VENDOR_CALL(device, cancel_picture); |
| 413 | } |
| 414 | |
| 415 | static int camera_set_parameters(struct camera_device *device, |
| 416 | const char *params) |
| 417 | { |
| 418 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 419 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 420 | |
| 421 | if (!device) |
| 422 | return -EINVAL; |
| 423 | |
| 424 | char *tmp = NULL; |
| 425 | tmp = camera_fixup_setparams(device, params); |
| 426 | |
| 427 | int ret = VENDOR_CALL(device, set_parameters, tmp); |
| 428 | return ret; |
| 429 | } |
| 430 | |
| 431 | static char *camera_get_parameters(struct camera_device *device) |
| 432 | { |
| 433 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 434 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 435 | |
| 436 | if (!device) |
| 437 | return NULL; |
| 438 | |
| 439 | char *params = VENDOR_CALL(device, get_parameters); |
| 440 | |
| 441 | char *tmp = camera_fixup_getparams(CAMERA_ID(device), params); |
| 442 | VENDOR_CALL(device, put_parameters, params); |
| 443 | params = tmp; |
| 444 | |
| 445 | return params; |
| 446 | } |
| 447 | |
| 448 | static void camera_put_parameters(struct camera_device *device, char *params) |
| 449 | { |
| 450 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 451 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 452 | |
| 453 | if (params) |
| 454 | free(params); |
| 455 | } |
| 456 | |
| 457 | static int camera_send_command(struct camera_device *device, |
| 458 | int32_t cmd, int32_t arg1, int32_t arg2) |
| 459 | { |
| 460 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 461 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 462 | |
| 463 | if (!device) |
| 464 | return -EINVAL; |
| 465 | |
| 466 | return VENDOR_CALL(device, send_command, cmd, arg1, arg2); |
| 467 | } |
| 468 | |
| 469 | static void camera_release(struct camera_device *device) |
| 470 | { |
| 471 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 472 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 473 | |
| 474 | if (!device) |
| 475 | return; |
| 476 | |
| 477 | VENDOR_CALL(device, release); |
| 478 | } |
| 479 | |
| 480 | static int camera_dump(struct camera_device *device, int fd) |
| 481 | { |
| 482 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, |
| 483 | (uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); |
| 484 | |
| 485 | if (!device) |
| 486 | return -EINVAL; |
| 487 | |
| 488 | return VENDOR_CALL(device, dump, fd); |
| 489 | } |
| 490 | |
| 491 | extern "C" void heaptracker_free_leaked_memory(void); |
| 492 | |
| 493 | static int camera_device_close(hw_device_t *device) |
| 494 | { |
| 495 | int ret = 0; |
| 496 | wrapper_camera_device_t *wrapper_dev = NULL; |
| 497 | |
| 498 | ALOGV("%s", __FUNCTION__); |
| 499 | |
| 500 | android::Mutex::Autolock lock(gCameraWrapperLock); |
| 501 | |
| 502 | if (!device) { |
| 503 | ret = -EINVAL; |
| 504 | goto done; |
| 505 | } |
| 506 | |
| 507 | for (int i = 0; i < camera_get_number_of_cameras(); i++) { |
| 508 | if (fixed_set_params[i]) |
| 509 | free(fixed_set_params[i]); |
| 510 | } |
| 511 | |
| 512 | wrapper_dev = (wrapper_camera_device_t*) device; |
| 513 | |
| 514 | wrapper_dev->vendor->common.close((hw_device_t*)wrapper_dev->vendor); |
| 515 | if (wrapper_dev->base.ops) |
| 516 | free(wrapper_dev->base.ops); |
| 517 | free(wrapper_dev); |
| 518 | done: |
| 519 | #ifdef HEAPTRACKER |
| 520 | heaptracker_free_leaked_memory(); |
| 521 | #endif |
| 522 | return ret; |
| 523 | } |
| 524 | |
| 525 | /******************************************************************* |
| 526 | * implementation of camera_module functions |
| 527 | *******************************************************************/ |
| 528 | |
| 529 | /* open device handle to one of the cameras |
| 530 | * |
| 531 | * assume camera service will keep singleton of each camera |
| 532 | * so this function will always only be called once per camera instance |
| 533 | */ |
| 534 | |
| 535 | static int camera_device_open(const hw_module_t *module, const char *name, |
| 536 | hw_device_t **device) |
| 537 | { |
| 538 | int rv = 0; |
| 539 | int num_cameras = 0; |
| 540 | int cameraid; |
| 541 | wrapper_camera_device_t *camera_device = NULL; |
| 542 | camera_device_ops_t *camera_ops = NULL; |
| 543 | |
| 544 | android::Mutex::Autolock lock(gCameraWrapperLock); |
| 545 | |
| 546 | ALOGV("%s", __FUNCTION__); |
| 547 | |
| 548 | if (name != NULL) { |
| 549 | if (check_vendor_module()) |
| 550 | return -EINVAL; |
| 551 | |
| 552 | cameraid = atoi(name); |
| 553 | num_cameras = gVendorModule->get_number_of_cameras(); |
| 554 | |
| 555 | fixed_set_params = (char **) malloc(sizeof(char *) * num_cameras); |
| 556 | if (!fixed_set_params) { |
| 557 | ALOGE("parameter memory allocation fail"); |
| 558 | rv = -ENOMEM; |
| 559 | goto fail; |
| 560 | } |
| 561 | memset(fixed_set_params, 0, sizeof(char *) * num_cameras); |
| 562 | |
| 563 | if (cameraid > num_cameras) { |
| 564 | ALOGE("camera service provided cameraid out of bounds, " |
| 565 | "cameraid = %d, num supported = %d", |
| 566 | cameraid, num_cameras); |
| 567 | rv = -EINVAL; |
| 568 | goto fail; |
| 569 | } |
| 570 | |
| 571 | camera_device = (wrapper_camera_device_t*)malloc(sizeof(*camera_device)); |
| 572 | if (!camera_device) { |
| 573 | ALOGE("camera_device allocation fail"); |
| 574 | rv = -ENOMEM; |
| 575 | goto fail; |
| 576 | } |
| 577 | memset(camera_device, 0, sizeof(*camera_device)); |
| 578 | camera_device->id = cameraid; |
| 579 | |
| 580 | rv = gVendorModule->common.methods->open( |
| 581 | (const hw_module_t*)gVendorModule, name, |
| 582 | (hw_device_t**)&(camera_device->vendor)); |
| 583 | if (rv) { |
| 584 | ALOGE("vendor camera open fail"); |
| 585 | goto fail; |
| 586 | } |
| 587 | ALOGV("%s: got vendor camera device 0x%08X", |
| 588 | __FUNCTION__, (uintptr_t)(camera_device->vendor)); |
| 589 | |
| 590 | camera_ops = (camera_device_ops_t*)malloc(sizeof(*camera_ops)); |
| 591 | if (!camera_ops) { |
| 592 | ALOGE("camera_ops allocation fail"); |
| 593 | rv = -ENOMEM; |
| 594 | goto fail; |
| 595 | } |
| 596 | |
| 597 | memset(camera_ops, 0, sizeof(*camera_ops)); |
| 598 | |
| 599 | camera_device->base.common.tag = HARDWARE_DEVICE_TAG; |
| 600 | camera_device->base.common.version = HARDWARE_DEVICE_API_VERSION(1, 0); |
| 601 | camera_device->base.common.module = (hw_module_t *)(module); |
| 602 | camera_device->base.common.close = camera_device_close; |
| 603 | camera_device->base.ops = camera_ops; |
| 604 | |
| 605 | camera_ops->set_preview_window = camera_set_preview_window; |
| 606 | camera_ops->set_callbacks = camera_set_callbacks; |
| 607 | camera_ops->enable_msg_type = camera_enable_msg_type; |
| 608 | camera_ops->disable_msg_type = camera_disable_msg_type; |
| 609 | camera_ops->msg_type_enabled = camera_msg_type_enabled; |
| 610 | camera_ops->start_preview = camera_start_preview; |
| 611 | camera_ops->stop_preview = camera_stop_preview; |
| 612 | camera_ops->preview_enabled = camera_preview_enabled; |
| 613 | camera_ops->store_meta_data_in_buffers = camera_store_meta_data_in_buffers; |
| 614 | camera_ops->start_recording = camera_start_recording; |
| 615 | camera_ops->stop_recording = camera_stop_recording; |
| 616 | camera_ops->recording_enabled = camera_recording_enabled; |
| 617 | camera_ops->release_recording_frame = camera_release_recording_frame; |
| 618 | camera_ops->auto_focus = camera_auto_focus; |
| 619 | camera_ops->cancel_auto_focus = camera_cancel_auto_focus; |
| 620 | camera_ops->take_picture = camera_take_picture; |
| 621 | camera_ops->cancel_picture = camera_cancel_picture; |
| 622 | camera_ops->set_parameters = camera_set_parameters; |
| 623 | camera_ops->get_parameters = camera_get_parameters; |
| 624 | camera_ops->put_parameters = camera_put_parameters; |
| 625 | camera_ops->send_command = camera_send_command; |
| 626 | camera_ops->release = camera_release; |
| 627 | camera_ops->dump = camera_dump; |
| 628 | |
| 629 | *device = &camera_device->base.common; |
| 630 | } |
| 631 | |
| 632 | return rv; |
| 633 | |
| 634 | fail: |
| 635 | if (camera_device) { |
| 636 | free(camera_device); |
| 637 | camera_device = NULL; |
| 638 | } |
| 639 | if (camera_ops) { |
| 640 | free(camera_ops); |
| 641 | camera_ops = NULL; |
| 642 | } |
| 643 | *device = NULL; |
| 644 | return rv; |
| 645 | } |
| 646 | |
| 647 | static int camera_get_number_of_cameras(void) |
| 648 | { |
| 649 | ALOGV("%s", __FUNCTION__); |
| 650 | if (check_vendor_module()) |
| 651 | return 0; |
| 652 | return gVendorModule->get_number_of_cameras(); |
| 653 | } |
| 654 | |
| 655 | static int camera_get_camera_info(int camera_id, struct camera_info *info) |
| 656 | { |
| 657 | ALOGV("%s", __FUNCTION__); |
| 658 | if (check_vendor_module()) |
| 659 | return 0; |
| 660 | return gVendorModule->get_camera_info(camera_id, info); |
| 661 | } |