Eino-Ville Talvala | fed0c02 | 2012-03-22 13:11:05 -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 | |
| 17 | #include <system/camera_metadata.h> |
| 18 | #include <hardware/camera2.h> |
| 19 | #include <gtest/gtest.h> |
| 20 | #include <iostream> |
| 21 | |
| 22 | class Camera2Test: public testing::Test { |
| 23 | public: |
| 24 | static void SetUpTestCase() { |
| 25 | int res; |
| 26 | |
| 27 | hw_module_t *module = NULL; |
| 28 | res = hw_get_module(CAMERA_HARDWARE_MODULE_ID, |
| 29 | (const hw_module_t **)&module); |
| 30 | |
| 31 | ASSERT_EQ(0, res) |
| 32 | << "Failure opening camera hardware module: " << res; |
| 33 | ASSERT_TRUE(NULL != module) |
| 34 | << "No camera module was set by hw_get_module"; |
| 35 | |
| 36 | std::cout << " Camera module name: " << module->name << std::endl; |
| 37 | std::cout << " Camera module author: " << module->author << std::endl; |
| 38 | std::cout << " Camera module API version: 0x" << std::hex |
| 39 | << module->module_api_version << std::endl; |
| 40 | std::cout << " Camera module HAL API version: 0x" << std::hex |
| 41 | << module->hal_api_version << std::endl; |
| 42 | |
| 43 | int16_t version2_0 = CAMERA_MODULE_API_VERSION_2_0; |
| 44 | ASSERT_EQ(version2_0, module->module_api_version) |
| 45 | << "Camera module version is 0x" |
| 46 | << std::hex << module->module_api_version |
| 47 | << ", not 2.0. (0x" |
| 48 | << std::hex << CAMERA_MODULE_API_VERSION_2_0 << ")"; |
| 49 | |
| 50 | sCameraModule = reinterpret_cast<camera_module_t*>(module); |
| 51 | |
| 52 | sNumCameras = sCameraModule->get_number_of_cameras(); |
| 53 | ASSERT_LT(0, sNumCameras) << "No camera devices available!"; |
| 54 | |
| 55 | std::cout << " Camera device count: " << sNumCameras << std::endl; |
| 56 | sCameraSupportsHal2 = new bool[sNumCameras]; |
| 57 | |
| 58 | for (int i = 0; i < sNumCameras; i++) { |
| 59 | camera_info info; |
| 60 | res = sCameraModule->get_camera_info(i, &info); |
| 61 | ASSERT_EQ(0, res) |
| 62 | << "Failure getting camera info for camera " << i; |
| 63 | std::cout << " Camera device: " << std::dec |
| 64 | << i << std::endl;; |
| 65 | std::cout << " Facing: " << std::dec |
| 66 | << info.facing << std::endl; |
| 67 | std::cout << " Orientation: " << std::dec |
| 68 | << info.orientation << std::endl; |
| 69 | std::cout << " Version: 0x" << std::hex << |
| 70 | info.device_version << std::endl; |
| 71 | if (info.device_version >= CAMERA_DEVICE_API_VERSION_2_0) { |
| 72 | sCameraSupportsHal2[i] = true; |
| 73 | ASSERT_TRUE(NULL != info.static_camera_characteristics); |
| 74 | std::cout << " Static camera metadata:" << std::endl; |
| 75 | dump_camera_metadata(info.static_camera_characteristics, 0, 1); |
| 76 | } else { |
| 77 | sCameraSupportsHal2[i] = false; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static const camera_module_t *getCameraModule() { |
| 83 | return sCameraModule; |
| 84 | } |
| 85 | |
| 86 | static const camera2_device_t *openCameraDevice(int id) { |
| 87 | if (NULL == sCameraSupportsHal2) return NULL; |
| 88 | if (id >= sNumCameras) return NULL; |
| 89 | if (!sCameraSupportsHal2[id]) return NULL; |
| 90 | |
| 91 | hw_device_t *device = NULL; |
| 92 | const camera_module_t *cam_module = getCameraModule(); |
| 93 | char camId[10]; |
| 94 | int res; |
| 95 | |
| 96 | snprintf(camId, 10, "%d", id); |
| 97 | res = cam_module->common.methods->open( |
| 98 | (const hw_module_t*)cam_module, |
| 99 | camId, |
| 100 | &device); |
| 101 | if (res < 0 || cam_module == NULL) { |
| 102 | return NULL; |
| 103 | } |
| 104 | camera2_device_t *cam_device = |
| 105 | reinterpret_cast<camera2_device_t*>(device); |
| 106 | return cam_device; |
| 107 | } |
| 108 | |
| 109 | private: |
| 110 | |
| 111 | static camera_module_t *sCameraModule; |
| 112 | static int sNumCameras; |
| 113 | static bool *sCameraSupportsHal2; |
| 114 | }; |
| 115 | |
| 116 | camera_module_t *Camera2Test::sCameraModule = NULL; |
| 117 | int Camera2Test::sNumCameras = 0; |
| 118 | bool *Camera2Test::sCameraSupportsHal2 = NULL; |
| 119 | |
| 120 | |
| 121 | TEST_F(Camera2Test, Basic) { |
| 122 | ASSERT_TRUE(NULL != getCameraModule()); |
| 123 | } |