Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [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 <gtest/gtest.h> |
| 18 | |
| 19 | #define LOG_TAG "CameraBurstTest" |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | #include <utils/Log.h> |
| 22 | |
| 23 | #include <cmath> |
| 24 | |
| 25 | #include "CameraStreamFixture.h" |
| 26 | #include "TestExtensions.h" |
| 27 | |
| 28 | #define CAMERA_FRAME_TIMEOUT 1000000000 //nsecs (1 secs) |
| 29 | #define CAMERA_HEAP_COUNT 2 //HALBUG: 1 means registerBuffers fails |
| 30 | #define CAMERA_BURST_DEBUGGING 0 |
| 31 | #define CAMERA_FRAME_BURST_COUNT 10 |
| 32 | |
| 33 | /* constants for the exposure test */ |
| 34 | #define CAMERA_EXPOSURE_DOUBLE 2 |
| 35 | #define CAMERA_EXPOSURE_DOUBLING_THRESHOLD 1.0f |
| 36 | #define CAMERA_EXPOSURE_DOUBLING_COUNT 4 |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame^] | 37 | #define CAMERA_EXPOSURE_FORMAT CAMERA_STREAM_AUTO_CPU_FORMAT |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame] | 38 | #define CAMERA_EXPOSURE_STARTING 100000 // 1/10ms, up to 51.2ms with 10 steps |
| 39 | |
| 40 | #if CAMERA_BURST_DEBUGGING |
| 41 | #define dout std::cout |
| 42 | #else |
| 43 | #define dout if (0) std::cout |
| 44 | #endif |
| 45 | |
| 46 | using namespace android; |
| 47 | using namespace android::camera2; |
| 48 | |
| 49 | namespace android { |
| 50 | namespace camera2 { |
| 51 | namespace tests { |
| 52 | |
| 53 | static CameraStreamParams STREAM_PARAMETERS = { |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame] | 54 | /*mFormat*/ CAMERA_EXPOSURE_FORMAT, |
| 55 | /*mHeapCount*/ CAMERA_HEAP_COUNT |
| 56 | }; |
| 57 | |
| 58 | class CameraBurstTest |
| 59 | : public ::testing::Test, |
| 60 | public CameraStreamFixture { |
| 61 | |
| 62 | public: |
| 63 | CameraBurstTest() : CameraStreamFixture(STREAM_PARAMETERS) { |
| 64 | TEST_EXTENSION_FORKING_CONSTRUCTOR; |
| 65 | |
| 66 | if (HasFatalFailure()) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | CreateStream(); |
| 71 | } |
| 72 | |
| 73 | ~CameraBurstTest() { |
| 74 | TEST_EXTENSION_FORKING_DESTRUCTOR; |
| 75 | |
| 76 | if (mDevice.get()) { |
| 77 | mDevice->waitUntilDrained(); |
| 78 | } |
| 79 | DeleteStream(); |
| 80 | } |
| 81 | |
| 82 | virtual void SetUp() { |
| 83 | TEST_EXTENSION_FORKING_SET_UP; |
| 84 | } |
| 85 | virtual void TearDown() { |
| 86 | TEST_EXTENSION_FORKING_TEAR_DOWN; |
| 87 | } |
| 88 | |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame^] | 89 | /* this assumes the format is YUV420sp or flexible YUV */ |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame] | 90 | long long TotalBrightness(const CpuConsumer::LockedBuffer& imgBuffer, |
| 91 | int *underexposed, |
| 92 | int *overexposed) const { |
| 93 | |
| 94 | const uint8_t* buf = imgBuffer.data; |
| 95 | size_t stride = imgBuffer.stride; |
| 96 | |
| 97 | /* iterate over the Y plane only */ |
| 98 | long long acc = 0; |
| 99 | |
| 100 | *underexposed = 0; |
| 101 | *overexposed = 0; |
| 102 | |
| 103 | for (size_t y = 0; y < imgBuffer.height; ++y) { |
| 104 | for (size_t x = 0; x < imgBuffer.width; ++x) { |
| 105 | const uint8_t p = buf[y * stride + x]; |
| 106 | |
| 107 | if (p == 0) { |
| 108 | if (underexposed) { |
| 109 | ++*underexposed; |
| 110 | } |
| 111 | continue; |
| 112 | } else if (p == 255) { |
| 113 | if (overexposed) { |
| 114 | ++*overexposed; |
| 115 | } |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | acc += p; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return acc; |
| 124 | } |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | TEST_F(CameraBurstTest, ManualExposureControl) { |
| 128 | |
| 129 | TEST_EXTENSION_FORKING_INIT; |
| 130 | |
| 131 | // Range of valid exposure times, in nanoseconds |
| 132 | int64_t minExp, maxExp; |
| 133 | { |
| 134 | camera_metadata_ro_entry exposureTimeRange = |
| 135 | GetStaticEntry(ANDROID_SENSOR_INFO_EXPOSURE_TIME_RANGE); |
| 136 | |
| 137 | ASSERT_EQ(2u, exposureTimeRange.count); |
| 138 | minExp = exposureTimeRange.data.i64[0]; |
| 139 | maxExp = exposureTimeRange.data.i64[1]; |
| 140 | } |
| 141 | |
| 142 | dout << "Min exposure is " << minExp; |
| 143 | dout << " max exposure is " << maxExp << std::endl; |
| 144 | |
| 145 | // Calculate some set of valid exposure times for each request |
| 146 | int64_t exposures[CAMERA_FRAME_BURST_COUNT]; |
| 147 | exposures[0] = CAMERA_EXPOSURE_STARTING; |
| 148 | for (int i = 1; i < CAMERA_FRAME_BURST_COUNT; ++i) { |
| 149 | exposures[i] = exposures[i-1] * CAMERA_EXPOSURE_DOUBLE; |
| 150 | } |
| 151 | // Our calculated exposure times should be in [minExp, maxExp] |
| 152 | EXPECT_LE(minExp, exposures[0]) |
| 153 | << "Minimum exposure range is too high, wanted at most " |
| 154 | << exposures[0] << "ns"; |
| 155 | EXPECT_GE(maxExp, exposures[CAMERA_FRAME_BURST_COUNT-1]) |
| 156 | << "Maximum exposure range is too low, wanted at least " |
| 157 | << exposures[CAMERA_FRAME_BURST_COUNT-1] << "ns"; |
| 158 | |
| 159 | // Create a preview request, turning off all 3A |
| 160 | CameraMetadata previewRequest; |
| 161 | ASSERT_EQ(OK, mDevice->createDefaultRequest(CAMERA2_TEMPLATE_PREVIEW, |
| 162 | &previewRequest)); |
| 163 | { |
| 164 | Vector<uint8_t> outputStreamIds; |
| 165 | outputStreamIds.push(mStreamId); |
| 166 | ASSERT_EQ(OK, previewRequest.update(ANDROID_REQUEST_OUTPUT_STREAMS, |
| 167 | outputStreamIds)); |
| 168 | |
| 169 | // Disable all 3A routines |
| 170 | uint8_t cmOff = static_cast<uint8_t>(ANDROID_CONTROL_MODE_OFF); |
| 171 | ASSERT_EQ(OK, previewRequest.update(ANDROID_CONTROL_MODE, |
| 172 | &cmOff, 1)); |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame^] | 173 | |
| 174 | int requestId = 1; |
| 175 | ASSERT_EQ(OK, previewRequest.update(ANDROID_REQUEST_ID, |
| 176 | &requestId, 1)); |
| 177 | |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame] | 178 | if (CAMERA_BURST_DEBUGGING) { |
| 179 | int frameCount = 0; |
| 180 | ASSERT_EQ(OK, previewRequest.update(ANDROID_REQUEST_FRAME_COUNT, |
| 181 | &frameCount, 1)); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | if (CAMERA_BURST_DEBUGGING) { |
| 186 | previewRequest.dump(STDOUT_FILENO); |
| 187 | } |
| 188 | |
| 189 | // Submit capture requests |
| 190 | for (int i = 0; i < CAMERA_FRAME_BURST_COUNT; ++i) { |
| 191 | CameraMetadata tmpRequest = previewRequest; |
| 192 | ASSERT_EQ(OK, tmpRequest.update(ANDROID_SENSOR_EXPOSURE_TIME, |
| 193 | &exposures[i], 1)); |
| 194 | ALOGV("Submitting capture request %d with exposure %lld", i, |
| 195 | exposures[i]); |
| 196 | dout << "Capture request " << i << " exposure is " |
| 197 | << (exposures[i]/1e6f) << std::endl; |
| 198 | ASSERT_EQ(OK, mDevice->capture(tmpRequest)); |
| 199 | } |
| 200 | |
| 201 | dout << "Buffer dimensions " << mWidth << "x" << mHeight << std::endl; |
| 202 | |
| 203 | float brightnesses[CAMERA_FRAME_BURST_COUNT]; |
| 204 | // Get each frame (metadata) and then the buffer. Calculate brightness. |
| 205 | for (int i = 0; i < CAMERA_FRAME_BURST_COUNT; ++i) { |
| 206 | ALOGV("Reading capture request %d with exposure %lld", i, exposures[i]); |
| 207 | ASSERT_EQ(OK, mDevice->waitForNextFrame(CAMERA_FRAME_TIMEOUT)); |
| 208 | ALOGV("Reading capture request-1 %d", i); |
| 209 | CameraMetadata frameMetadata; |
| 210 | ASSERT_EQ(OK, mDevice->getNextFrame(&frameMetadata)); |
| 211 | ALOGV("Reading capture request-2 %d", i); |
| 212 | |
| 213 | ASSERT_EQ(OK, mFrameListener->waitForFrame(CAMERA_FRAME_TIMEOUT)); |
| 214 | ALOGV("We got the frame now"); |
| 215 | |
| 216 | CpuConsumer::LockedBuffer imgBuffer; |
| 217 | ASSERT_EQ(OK, mCpuConsumer->lockNextBuffer(&imgBuffer)); |
| 218 | |
| 219 | int underexposed, overexposed; |
| 220 | long long brightness = TotalBrightness(imgBuffer, &underexposed, |
| 221 | &overexposed); |
| 222 | float avgBrightness = brightness * 1.0f / |
| 223 | (mWidth * mHeight - (underexposed + overexposed)); |
| 224 | ALOGV("Total brightness for frame %d was %lld (underexposed %d, " |
| 225 | "overexposed %d), avg %f", i, brightness, underexposed, |
| 226 | overexposed, avgBrightness); |
| 227 | dout << "Average brightness (frame " << i << ") was " << avgBrightness |
| 228 | << " (underexposed " << underexposed << ", overexposed " |
| 229 | << overexposed << ")" << std::endl; |
| 230 | |
| 231 | ASSERT_EQ(OK, mCpuConsumer->unlockBuffer(imgBuffer)); |
| 232 | |
| 233 | brightnesses[i] = avgBrightness; |
| 234 | } |
| 235 | |
| 236 | // Calculate max consecutive frame exposure doubling |
| 237 | float prev = brightnesses[0]; |
| 238 | int doubling_count = 1; |
| 239 | int max_doubling_count = 0; |
| 240 | for (int i = 1; i < CAMERA_FRAME_BURST_COUNT; ++i) { |
| 241 | if (fabs(brightnesses[i] - prev*CAMERA_EXPOSURE_DOUBLE) |
| 242 | <= CAMERA_EXPOSURE_DOUBLING_THRESHOLD) { |
| 243 | doubling_count++; |
| 244 | } |
| 245 | else { |
| 246 | max_doubling_count = std::max(max_doubling_count, doubling_count); |
| 247 | doubling_count = 1; |
| 248 | } |
| 249 | prev = brightnesses[i]; |
| 250 | } |
| 251 | |
| 252 | dout << "max doubling count: " << max_doubling_count << std::endl; |
| 253 | |
| 254 | EXPECT_LE(CAMERA_EXPOSURE_DOUBLING_COUNT, max_doubling_count) |
| 255 | << "average brightness should double at least " |
| 256 | << CAMERA_EXPOSURE_DOUBLING_COUNT |
| 257 | << " times over each consecutive frame as the exposure is doubled"; |
| 258 | } |
| 259 | |
| 260 | } |
| 261 | } |
| 262 | } |