blob: 3a2df69d2480e96dc1fda895069c9a4b13ab4359 [file] [log] [blame]
Igor Murashkine302ee32012-11-05 11:14:49 -08001/*
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
Igor Murashkineab33fc2012-11-06 17:02:54 -080017#ifndef __ANDROID_HAL_CAMERA2_TESTS_MODULE_FIXTURE__
18#define __ANDROID_HAL_CAMERA2_TESTS_MODULE_FIXTURE__
19
Igor Murashkine302ee32012-11-05 11:14:49 -080020#include <gtest/gtest.h>
21
22#include "hardware/hardware.h"
23#include "hardware/camera2.h"
24
Yin-Chia Yeh7eb3d912015-02-02 16:20:46 -080025#include <common/CameraModule.h>
Eino-Ville Talvala48bb03f2013-07-25 17:09:14 -070026#include <device2/Camera2Device.h>
27#include <device3/Camera3Device.h>
28
Igor Murashkine302ee32012-11-05 11:14:49 -080029#include "camera2_utils.h"
Igor Murashkineab33fc2012-11-06 17:02:54 -080030#include "TestExtensions.h"
Igor Murashkine302ee32012-11-05 11:14:49 -080031
32namespace android {
33namespace camera2 {
34namespace tests {
35
36template <bool InfoQuirk = false>
37struct CameraModuleFixture {
38
39 CameraModuleFixture(int CameraID = -1) {
Igor Murashkineab33fc2012-11-06 17:02:54 -080040 TEST_EXTENSION_FORKING_CONSTRUCTOR;
41
Igor Murashkine302ee32012-11-05 11:14:49 -080042 mCameraID = CameraID;
Igor Murashkine302ee32012-11-05 11:14:49 -080043 }
44
45 ~CameraModuleFixture() {
Igor Murashkineab33fc2012-11-06 17:02:54 -080046 TEST_EXTENSION_FORKING_DESTRUCTOR;
Igor Murashkine302ee32012-11-05 11:14:49 -080047 }
48
Igor Murashkin02f3ac02012-12-14 16:33:46 -080049 camera_metadata_ro_entry GetStaticEntry(uint32_t tag) const {
50 const CameraMetadata& staticInfo = mDevice->info();
51 camera_metadata_ro_entry entry = staticInfo.find(tag);
52 return entry;
53 }
54
Igor Murashkine302ee32012-11-05 11:14:49 -080055 void SetUp() {
Igor Murashkineab33fc2012-11-06 17:02:54 -080056 TEST_EXTENSION_FORKING_SET_UP;
57
Yin-Chia Yeh7eb3d912015-02-02 16:20:46 -080058 camera_module_t *rawModule;
Igor Murashkine302ee32012-11-05 11:14:49 -080059 ASSERT_LE(0, hw_get_module(CAMERA_HARDWARE_MODULE_ID,
Yin-Chia Yeh7eb3d912015-02-02 16:20:46 -080060 (const hw_module_t **)&rawModule)) << "Could not load camera module";
61 ASSERT_NE((void*)0, rawModule);
62 mModule = new CameraModule(rawModule);
Igor Murashkine302ee32012-11-05 11:14:49 -080063
Yin-Chia Yeh7eb3d912015-02-02 16:20:46 -080064 mNumberOfCameras = mModule->getNumberOfCameras();
Igor Murashkine302ee32012-11-05 11:14:49 -080065 ASSERT_LE(0, mNumberOfCameras);
66
Igor Murashkinfb40d5d2013-03-26 18:07:31 -070067 ASSERT_LE(
Yin-Chia Yehc2537652015-03-24 10:48:08 -070068 CAMERA_MODULE_API_VERSION_2_0, mModule->getModuleApiVersion())
Igor Murashkine302ee32012-11-05 11:14:49 -080069 << "Wrong module API version";
70
71 /* For using this fixture in other tests only */
72 SetUpMixin();
73 }
74
75 void TearDown() {
Igor Murashkineab33fc2012-11-06 17:02:54 -080076 TEST_EXTENSION_FORKING_TEAR_DOWN;
77
Yin-Chia Yeh7eb3d912015-02-02 16:20:46 -080078 delete mModule;
Igor Murashkine302ee32012-11-05 11:14:49 -080079 TearDownMixin();
80
81 /* important: device must be destructed before closing module,
82 since it calls back into HAL */
83 mDevice.clear();
84
Igor Murashkineab33fc2012-11-06 17:02:54 -080085 if (!TEST_EXTENSION_FORKING_ENABLED) {
Yin-Chia Yehc2537652015-03-24 10:48:08 -070086 ASSERT_EQ(0, HWModuleHelpers::closeModule(mModule->getDso()))
Igor Murashkineab33fc2012-11-06 17:02:54 -080087 << "Failed to close camera HAL module";
88 }
Igor Murashkine302ee32012-11-05 11:14:49 -080089 }
90
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070091 void CreateCamera(int cameraID, /*out*/ sp<CameraDeviceBase> *device) {
92 struct camera_info info;
Yin-Chia Yeh7eb3d912015-02-02 16:20:46 -080093 ASSERT_EQ(OK, mModule->getCameraInfo(cameraID, &info));
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070094
95 ASSERT_GE((int)info.device_version, CAMERA_DEVICE_API_VERSION_2_0) <<
96 "Device version too old for camera " << cameraID << ". Version: " <<
97 info.device_version;
98 switch(info.device_version) {
99 case CAMERA_DEVICE_API_VERSION_2_0:
100 case CAMERA_DEVICE_API_VERSION_2_1:
101 *device = new Camera2Device(cameraID);
102 break;
103 case CAMERA_DEVICE_API_VERSION_3_0:
Zhijun Heb5ac45f2014-08-07 16:25:41 -0700104 case CAMERA_DEVICE_API_VERSION_3_1:
105 case CAMERA_DEVICE_API_VERSION_3_2:
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700106 *device = new Camera3Device(cameraID);
107 break;
108 default:
109 device->clear();
110 FAIL() << "Device version unknown for camera " << cameraID << ". Version: " <<
111 info.device_version;
112 }
113
114 }
115
116 int getDeviceVersion() {
117 return getDeviceVersion(mCameraID);
118 }
119
120 int getDeviceVersion(int cameraId, status_t* status = NULL) {
121 camera_info info;
122 status_t res;
Yin-Chia Yeh7eb3d912015-02-02 16:20:46 -0800123 res = mModule->getCameraInfo(cameraId, &info);
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700124 if (status != NULL) *status = res;
125
126 return info.device_version;
127 }
128
Igor Murashkin7acb21a2012-12-18 13:38:40 -0800129private:
130
Igor Murashkine302ee32012-11-05 11:14:49 -0800131 void SetUpMixin() {
132 /* For using this fixture in other tests only */
133 if (mCameraID != -1) {
134 EXPECT_LE(0, mCameraID);
135 EXPECT_LT(mCameraID, mNumberOfCameras);
136
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700137 /* HALBUG (Exynos5); crashes if we skip calling get_camera_info
138 before initializing. Need info anyway now. */
Igor Murashkine302ee32012-11-05 11:14:49 -0800139
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700140 CreateCamera(mCameraID, &mDevice);
141
142 ASSERT_TRUE(mDevice != NULL) << "Failed to open device " << mCameraID;
Igor Murashkine302ee32012-11-05 11:14:49 -0800143 ASSERT_EQ(OK, mDevice->initialize(mModule))
144 << "Failed to initialize device " << mCameraID;
145 }
146 }
147
148 void TearDownMixin() {
149
150 }
151
152protected:
153 int mNumberOfCameras;
Yin-Chia Yeh7eb3d912015-02-02 16:20:46 -0800154 CameraModule *mModule;
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700155 sp<CameraDeviceBase> mDevice;
Igor Murashkine302ee32012-11-05 11:14:49 -0800156
157private:
158 int mCameraID;
159};
160
161
162}
163}
164}
165
Igor Murashkineab33fc2012-11-06 17:02:54 -0800166#endif