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 */
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];
115 TEST_EXTENSION_FORKING_TEAR_DOWN;
117 // important: shut down HAL before releasing streams
118 CameraModuleFixture::TearDown();
120 mNativeWindow.clear();
121 mCpuConsumer.clear();
122 mFrameListener.clear();
126 struct FrameListener : public ConsumerBase::FrameAvailableListener {
132 // CpuConsumer::FrameAvailableListener implementation
133 virtual void onFrameAvailable(const BufferItem& /* item */) {
134 ALOGV("Frame now available (start)");
136 Mutex::Autolock lock(mMutex);
140 ALOGV("Frame now available (end)");
143 status_t waitForFrame(nsecs_t timeout) {
145 Mutex::Autolock lock(mMutex);
146 while (mPendingFrames == 0) {
147 res = mCondition.waitRelative(mMutex, timeout);
148 if (res != OK) return res;
156 Condition mCondition;
160 void CreateStream() {
161 sp<CameraDeviceBase> device = mDevice;
162 CameraStreamParams p = mParam;
164 sp<IGraphicBufferProducer> producer;
165 sp<IGraphicBufferConsumer> consumer;
166 BufferQueue::createBufferQueue(&producer, &consumer);
167 mCpuConsumer = new CpuConsumer(consumer, p.mHeapCount);
168 mCpuConsumer->setName(String8("CameraStreamTest::mCpuConsumer"));
170 mNativeWindow = new Surface(producer);
172 int format = MapAutoFormat(p.mFormat);
175 device->createStream(mNativeWindow,
176 mWidth, mHeight, format,
179 ASSERT_NE(-1, mStreamId);
181 // do not make 'this' a FrameListener or the lifetime policy will clash
182 mFrameListener = new FrameListener();
183 mCpuConsumer->setFrameAvailableListener(mFrameListener);
186 void DeleteStream() {
187 ASSERT_EQ(OK, mDevice->deleteStream(mStreamId));
190 int MapAutoFormat(int format) {
191 if (format == CAMERA_STREAM_AUTO_CPU_FORMAT) {
192 if (getDeviceVersion() >= CAMERA_DEVICE_API_VERSION_3_0) {
193 format = HAL_PIXEL_FORMAT_YCbCr_420_888;
195 format = HAL_PIXEL_FORMAT_YCrCb_420_SP;
201 void DumpYuvToFile(const String8 &fileName, const CpuConsumer::LockedBuffer &img) {
202 uint8_t *dataCb, *dataCr;
204 uint32_t chromaStride;
207 switch (img.format) {
208 case HAL_PIXEL_FORMAT_YCbCr_420_888:
210 chromaStride = img.chromaStride;
211 chromaStep = img.chromaStep;
215 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
217 chromaStride = img.width;
219 dataCr = img.data + img.width * img.height;
222 case HAL_PIXEL_FORMAT_YV12:
224 chromaStride = ALIGN(img.width / 2, 16);
226 dataCr = img.data + img.stride * img.height;
227 dataCb = dataCr + chromaStride * img.height/2;
230 ALOGE("Unknown format %d, not dumping", img.format);
235 FILE *yuvFile = fopen(fileName.string(), "w");
239 for (size_t y = 0; y < img.height; ++y) {
241 reinterpret_cast<const char*>(img.data + stride * y),
242 1, img.width, yuvFile);
243 if (bytes != img.width) {
244 ALOGE("Unable to write to file %s", fileName.string());
251 uint8_t *src = dataCb;
252 for (int c = 0; c < 2; ++c) {
253 for (size_t y = 0; y < img.height / 2; ++y) {
254 uint8_t *px = src + y * chromaStride;
255 if (chromaStep != 1) {
256 for (size_t x = 0; x < img.width / 2; ++x) {
261 bytes = fwrite(reinterpret_cast<const char*>(px),
262 1, img.width / 2, yuvFile);
263 if (bytes != img.width / 2) {
264 ALOGE("Unable to write to file %s", fileName.string());
280 android::sp<FrameListener> mFrameListener;
281 android::sp<CpuConsumer> mCpuConsumer;
282 android::sp<ANativeWindow> mNativeWindow;
286 CameraStreamParams mParam;