2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __ANDROID_HAL_CAMERA2_TESTS_STREAM_FIXTURE__
18 #define __ANDROID_HAL_CAMERA2_TESTS_STREAM_FIXTURE__
20 #include <gtest/gtest.h>
24 #include <gui/CpuConsumer.h>
25 #include <gui/Surface.h>
26 #include <utils/Condition.h>
27 #include <utils/Mutex.h>
28 #include <system/camera_metadata.h>
30 #include "CameraModuleFixture.h"
31 #include "TestExtensions.h"
33 #define ALIGN(x, mask) ( ((x) + (mask) - 1) & ~((mask) - 1) )
39 // Format specifier for picking the best format for CPU reading the given device
41 #define CAMERA_STREAM_AUTO_CPU_FORMAT (-1)
43 struct CameraStreamParams;
45 void PrintTo(const CameraStreamParams& p, ::std::ostream* os);
47 struct CameraStreamParams {
53 inline ::std::ostream& operator<<(::std::ostream& os, const CameraStreamParams &p) {
58 inline void PrintTo(const CameraStreamParams& p, ::std::ostream* os) {
60 camera_metadata_enum_snprint(
61 ANDROID_SCALER_AVAILABLE_FORMATS, p.mFormat, fmt, sizeof(fmt));
64 *os << "Format: 0x" << std::hex << p.mFormat << ", ";
65 *os << "Format name: " << fmt << ", ";
66 *os << "HeapCount: " << p.mHeapCount;
70 class CameraStreamFixture
71 : public CameraModuleFixture</*InfoQuirk*/true> {
74 CameraStreamFixture(CameraStreamParams p)
75 : CameraModuleFixture(TestSettings::DeviceId()) {
76 TEST_EXTENSION_FORKING_CONSTRUCTOR;
83 ~CameraStreamFixture() {
84 TEST_EXTENSION_FORKING_DESTRUCTOR;
92 TEST_EXTENSION_FORKING_SET_UP;
94 CameraModuleFixture::SetUp();
96 sp<CameraDeviceBase> device = mDevice;
98 /* use an arbitrary w,h */
99 if (getDeviceVersion() < CAMERA_DEVICE_API_VERSION_3_2) {
100 const int tag = ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES;
102 const CameraMetadata& staticInfo = device->info();
103 camera_metadata_ro_entry entry = staticInfo.find(tag);
104 ASSERT_NE(0u, entry.count)
105 << "Missing tag android.scaler.availableProcessedSizes";
107 ASSERT_LE(2u, entry.count);
108 /* this seems like it would always be the smallest w,h
109 but we actually make no contract that it's sorted asc */
110 mWidth = entry.data.i32[0];
111 mHeight = entry.data.i32[1];
113 buildOutputResolutions();
114 const int32_t *implDefResolutions;
115 size_t implDefResolutionsCount;
117 int format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
119 getResolutionList(format,
120 &implDefResolutions, &implDefResolutionsCount);
121 ASSERT_NE(0u, implDefResolutionsCount)
122 << "Missing implementation defined sizes";
123 mWidth = implDefResolutions[0];
124 mHeight = implDefResolutions[1];
128 TEST_EXTENSION_FORKING_TEAR_DOWN;
130 // important: shut down HAL before releasing streams
131 CameraModuleFixture::TearDown();
133 deleteOutputResolutions();
134 mNativeWindow.clear();
135 mCpuConsumer.clear();
136 mFrameListener.clear();
141 int64_t getMinFrameDurationFor(int32_t format, int32_t width, int32_t height) {
142 int64_t minFrameDuration = -1L;
143 const int tag = ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS;
144 sp<CameraDeviceBase> device = mDevice;
145 const CameraMetadata& staticInfo = device->info();
146 camera_metadata_ro_entry_t availableMinDurations = staticInfo.find(tag);
147 for (uint32_t i = 0; i < availableMinDurations.count; i += 4) {
148 if (format == availableMinDurations.data.i64[i] &&
149 width == availableMinDurations.data.i64[i + 1] &&
150 height == availableMinDurations.data.i64[i + 2]) {
151 minFrameDuration = availableMinDurations.data.i64[i + 3];
155 return minFrameDuration;
158 void buildOutputResolutions() {
159 if (getDeviceVersion() < CAMERA_DEVICE_API_VERSION_3_2) {
162 if (mOutputResolutions.isEmpty()) {
163 const int tag = ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS;
164 const CameraMetadata& staticInfo = mDevice->info();
165 camera_metadata_ro_entry_t availableStrmConfigs = staticInfo.find(tag);
166 ASSERT_EQ(0u, availableStrmConfigs.count % 4);
167 for (uint32_t i = 0; i < availableStrmConfigs.count; i += 4) {
168 int32_t format = availableStrmConfigs.data.i32[i];
169 int32_t width = availableStrmConfigs.data.i32[i + 1];
170 int32_t height = availableStrmConfigs.data.i32[i + 2];
171 int32_t inOrOut = availableStrmConfigs.data.i32[i + 3];
172 if (inOrOut == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT) {
173 int index = mOutputResolutions.indexOfKey(format);
175 index = mOutputResolutions.add(format, new Vector<int32_t>());
176 ASSERT_TRUE(index >= 0);
178 Vector<int32_t> *resolutions = mOutputResolutions.editValueAt(index);
179 resolutions->add(width);
180 resolutions->add(height);
186 void getResolutionList(int32_t format,
187 const int32_t **list,
190 ALOGV("Getting resolutions for format %x", format);
191 if (getDeviceVersion() < CAMERA_DEVICE_API_VERSION_3_2) {
194 int index = mOutputResolutions.indexOfKey(format);
195 ASSERT_TRUE(index >= 0);
196 Vector<int32_t>* resolutions = mOutputResolutions.valueAt(index);
197 *list = resolutions->array();
198 *count = resolutions->size();
201 void deleteOutputResolutions() {
202 for (uint32_t i = 0; i < mOutputResolutions.size(); i++) {
203 Vector<int32_t>* resolutions = mOutputResolutions.editValueAt(i);
206 mOutputResolutions.clear();
209 struct FrameListener : public ConsumerBase::FrameAvailableListener {
215 // CpuConsumer::FrameAvailableListener implementation
216 virtual void onFrameAvailable(const BufferItem& /* item */) {
217 ALOGV("Frame now available (start)");
219 Mutex::Autolock lock(mMutex);
223 ALOGV("Frame now available (end)");
226 status_t waitForFrame(nsecs_t timeout) {
228 Mutex::Autolock lock(mMutex);
229 while (mPendingFrames == 0) {
230 res = mCondition.waitRelative(mMutex, timeout);
231 if (res != OK) return res;
239 Condition mCondition;
243 void CreateStream() {
244 sp<CameraDeviceBase> device = mDevice;
245 CameraStreamParams p = mParam;
247 sp<IGraphicBufferProducer> producer;
248 sp<IGraphicBufferConsumer> consumer;
249 BufferQueue::createBufferQueue(&producer, &consumer);
250 mCpuConsumer = new CpuConsumer(consumer, p.mHeapCount);
251 mCpuConsumer->setName(String8("CameraStreamTest::mCpuConsumer"));
253 mNativeWindow = new Surface(producer);
255 int format = MapAutoFormat(p.mFormat);
258 device->createStream(mNativeWindow,
259 mWidth, mHeight, format,
262 ASSERT_NE(-1, mStreamId);
264 // do not make 'this' a FrameListener or the lifetime policy will clash
265 mFrameListener = new FrameListener();
266 mCpuConsumer->setFrameAvailableListener(mFrameListener);
269 void DeleteStream() {
270 ASSERT_EQ(OK, mDevice->deleteStream(mStreamId));
273 int MapAutoFormat(int format) {
274 if (format == CAMERA_STREAM_AUTO_CPU_FORMAT) {
275 if (getDeviceVersion() >= CAMERA_DEVICE_API_VERSION_3_0) {
276 format = HAL_PIXEL_FORMAT_YCbCr_420_888;
278 format = HAL_PIXEL_FORMAT_YCrCb_420_SP;
284 void DumpYuvToFile(const String8 &fileName, const CpuConsumer::LockedBuffer &img) {
285 uint8_t *dataCb, *dataCr;
287 uint32_t chromaStride;
290 switch (img.format) {
291 case HAL_PIXEL_FORMAT_YCbCr_420_888:
293 chromaStride = img.chromaStride;
294 chromaStep = img.chromaStep;
298 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
300 chromaStride = img.width;
302 dataCr = img.data + img.width * img.height;
305 case HAL_PIXEL_FORMAT_YV12:
307 chromaStride = ALIGN(img.width / 2, 16);
309 dataCr = img.data + img.stride * img.height;
310 dataCb = dataCr + chromaStride * img.height/2;
313 ALOGE("Unknown format %d, not dumping", img.format);
318 FILE *yuvFile = fopen(fileName.string(), "w");
322 for (size_t y = 0; y < img.height; ++y) {
324 reinterpret_cast<const char*>(img.data + stride * y),
325 1, img.width, yuvFile);
326 if (bytes != img.width) {
327 ALOGE("Unable to write to file %s", fileName.string());
334 uint8_t *src = dataCb;
335 for (int c = 0; c < 2; ++c) {
336 for (size_t y = 0; y < img.height / 2; ++y) {
337 uint8_t *px = src + y * chromaStride;
338 if (chromaStep != 1) {
339 for (size_t x = 0; x < img.width / 2; ++x) {
344 bytes = fwrite(reinterpret_cast<const char*>(px),
345 1, img.width / 2, yuvFile);
346 if (bytes != img.width / 2) {
347 ALOGE("Unable to write to file %s", fileName.string());
363 android::sp<FrameListener> mFrameListener;
364 android::sp<CpuConsumer> mCpuConsumer;
365 android::sp<ANativeWindow> mNativeWindow;
366 KeyedVector<int32_t, Vector<int32_t>* > mOutputResolutions;
369 CameraStreamParams mParam;