• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "CpuConsumer_test"
18 //#define LOG_NDEBUG 0
19 //#define LOG_NNDEBUG 0
20 
21 #ifdef LOG_NNDEBUG
22 #define ALOGVV(...) ALOGV(__VA_ARGS__)
23 #else
24 #define ALOGVV(...) ((void)0)
25 #endif
26 
27 #include <gtest/gtest.h>
28 #include <gui/CpuConsumer.h>
29 #include <gui/Surface.h>
30 #include <ui/GraphicBuffer.h>
31 #include <utils/String8.h>
32 #include <utils/Thread.h>
33 #include <utils/Mutex.h>
34 #include <utils/Condition.h>
35 
36 #include <vector>
37 #define CPU_CONSUMER_TEST_FORMAT_RAW 0
38 #define CPU_CONSUMER_TEST_FORMAT_Y8 0
39 #define CPU_CONSUMER_TEST_FORMAT_Y16 0
40 #define CPU_CONSUMER_TEST_FORMAT_RGBA_8888 1
41 
42 namespace android {
43 
44 struct CpuConsumerTestParams {
45     uint32_t width;
46     uint32_t height;
47     int maxLockedBuffers;
48     PixelFormat format;
49 };
50 
operator <<(::std::ostream & os,const CpuConsumerTestParams & p)51 ::std::ostream& operator<<(::std::ostream& os, const CpuConsumerTestParams& p) {
52     return os << "[ (" << p.width << ", " << p.height << "), B:"
53               << p.maxLockedBuffers << ", F:0x"
54               << ::std::hex << p.format << "]";
55 }
56 
57 class CpuConsumerTest : public ::testing::TestWithParam<CpuConsumerTestParams> {
58 protected:
59 
SetUp()60     virtual void SetUp() {
61         const ::testing::TestInfo* const test_info =
62                 ::testing::UnitTest::GetInstance()->current_test_info();
63         CpuConsumerTestParams params = GetParam();
64         ALOGV("** Starting test %s (%d x %d, %d, 0x%x)",
65                 test_info->name(),
66                 params.width, params.height,
67                 params.maxLockedBuffers, params.format);
68         sp<IGraphicBufferProducer> producer;
69         sp<IGraphicBufferConsumer> consumer;
70         BufferQueue::createBufferQueue(&producer, &consumer);
71         mCC = new CpuConsumer(consumer, params.maxLockedBuffers);
72         String8 name("CpuConsumer_Under_Test");
73         mCC->setName(name);
74         mSTC = new Surface(producer);
75         mANW = mSTC;
76     }
77 
TearDown()78     virtual void TearDown() {
79         mANW.clear();
80         mSTC.clear();
81         mCC.clear();
82     }
83 
84     class FrameWaiter : public CpuConsumer::FrameAvailableListener {
85     public:
FrameWaiter()86         FrameWaiter():
87                 mPendingFrames(0) {
88         }
89 
waitForFrame()90         void waitForFrame() {
91             Mutex::Autolock lock(mMutex);
92             while (mPendingFrames == 0) {
93                 mCondition.wait(mMutex);
94             }
95             mPendingFrames--;
96         }
97 
onFrameAvailable(const BufferItem &)98         virtual void onFrameAvailable(const BufferItem&) {
99             Mutex::Autolock lock(mMutex);
100             mPendingFrames++;
101             mCondition.signal();
102         }
103 
104         int mPendingFrames;
105         Mutex mMutex;
106         Condition mCondition;
107     };
108 
109     // Note that SurfaceTexture will lose the notifications
110     // onBuffersReleased and onFrameAvailable as there is currently
111     // no way to forward the events.  This DisconnectWaiter will not let the
112     // disconnect finish until finishDisconnect() is called.  It will
113     // also block until a disconnect is called
114     class DisconnectWaiter : public BufferQueue::ConsumerListener {
115     public:
DisconnectWaiter()116         DisconnectWaiter () :
117             mWaitForDisconnect(false),
118             mPendingFrames(0) {
119         }
120 
waitForFrame()121         void waitForFrame() {
122             Mutex::Autolock lock(mMutex);
123             while (mPendingFrames == 0) {
124                 mFrameCondition.wait(mMutex);
125             }
126             mPendingFrames--;
127         }
128 
onFrameAvailable(const BufferItem &)129         virtual void onFrameAvailable(const BufferItem&) {
130             Mutex::Autolock lock(mMutex);
131             mPendingFrames++;
132             mFrameCondition.signal();
133         }
134 
onBuffersReleased()135         virtual void onBuffersReleased() {
136             Mutex::Autolock lock(mMutex);
137             while (!mWaitForDisconnect) {
138                 mDisconnectCondition.wait(mMutex);
139             }
140         }
141 
finishDisconnect()142         void finishDisconnect() {
143             Mutex::Autolock lock(mMutex);
144             mWaitForDisconnect = true;
145             mDisconnectCondition.signal();
146         }
147 
148     private:
149         Mutex mMutex;
150 
151         bool mWaitForDisconnect;
152         Condition mDisconnectCondition;
153 
154         int mPendingFrames;
155         Condition mFrameCondition;
156     };
157 
158     sp<CpuConsumer> mCC;
159     sp<Surface> mSTC;
160     sp<ANativeWindow> mANW;
161 };
162 
163 #define ASSERT_NO_ERROR(err, msg) \
164     ASSERT_EQ(NO_ERROR, err) << (msg) << strerror(-(err))
165 
checkPixel(const CpuConsumer::LockedBuffer & buf,uint32_t x,uint32_t y,uint32_t r,uint32_t g=0,uint32_t b=0)166 void checkPixel(const CpuConsumer::LockedBuffer &buf,
167         uint32_t x, uint32_t y, uint32_t r, uint32_t g=0, uint32_t b=0) {
168     // Ignores components that don't exist for given pixel
169     switch(buf.format) {
170         case HAL_PIXEL_FORMAT_RAW16: {
171             String8 msg;
172             uint16_t *bPtr = (uint16_t*)buf.data;
173             bPtr += y * buf.stride + x;
174             // GRBG Bayer mosaic; only check the matching channel
175             switch( ((y & 1) << 1) | (x & 1) ) {
176                 case 0: // G
177                 case 3: // G
178                     EXPECT_EQ(g, *bPtr);
179                     break;
180                 case 1: // R
181                     EXPECT_EQ(r, *bPtr);
182                     break;
183                 case 2: // B
184                     EXPECT_EQ(b, *bPtr);
185                     break;
186             }
187             break;
188         }
189         // ignores g,b
190         case HAL_PIXEL_FORMAT_Y8: {
191             uint8_t *bPtr = (uint8_t*)buf.data;
192             bPtr += y * buf.stride + x;
193             EXPECT_EQ(r, *bPtr) << "at x = " << x << " y = " << y;
194             break;
195         }
196         // ignores g,b
197         case HAL_PIXEL_FORMAT_Y16: {
198             // stride is in pixels, not in bytes
199             uint16_t *bPtr = ((uint16_t*)buf.data) + y * buf.stride + x;
200 
201             EXPECT_EQ(r, *bPtr) << "at x = " << x << " y = " << y;
202             break;
203         }
204         case HAL_PIXEL_FORMAT_RGBA_8888: {
205             const int bytesPerPixel = 4;
206             uint8_t *bPtr = (uint8_t*)buf.data;
207             bPtr += (y * buf.stride + x) * bytesPerPixel;
208 
209             EXPECT_EQ(r, bPtr[0]) << "at x = " << x << " y = " << y;
210             EXPECT_EQ(g, bPtr[1]) << "at x = " << x << " y = " << y;
211             EXPECT_EQ(b, bPtr[2]) << "at x = " << x << " y = " << y;
212             break;
213         }
214         default: {
215             ADD_FAILURE() << "Unknown format for check:" << buf.format;
216             break;
217         }
218     }
219 }
220 
221 // Fill a YV12 buffer with a multi-colored checkerboard pattern
222 void fillYV12Buffer(uint8_t* buf, int w, int h, int stride);
223 
224 // Fill a Y8/Y16 buffer with a multi-colored checkerboard pattern
225 template <typename T> // T == uint8_t or uint16_t
fillGreyscaleBuffer(T * buf,int w,int h,int stride,int bpp)226 void fillGreyscaleBuffer(T* buf, int w, int h, int stride, int bpp) {
227     const int blockWidth = w > 16 ? w / 16 : 1;
228     const int blockHeight = h > 16 ? h / 16 : 1;
229     const int yuvTexOffsetY = 0;
230 
231     ASSERT_TRUE(bpp == 8 || bpp == 16);
232     ASSERT_TRUE(sizeof(T)*8 == bpp);
233 
234     // stride is in pixels, not in bytes
235     int yuvTexStrideY = stride;
236     for (int x = 0; x < w; x++) {
237         for (int y = 0; y < h; y++) {
238             int parityX = (x / blockWidth) & 1;
239             int parityY = (y / blockHeight) & 1;
240             T intensity = (parityX ^ parityY) ? 63 : 191;
241             buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity;
242         }
243     }
244 }
245 
chooseColorRgba8888(int blockX,int blockY,uint8_t channel)246 inline uint8_t chooseColorRgba8888(int blockX, int blockY, uint8_t channel) {
247     const int colorVariations = 3;
248     uint8_t color = ((blockX % colorVariations) + (blockY % colorVariations))
249                         % (colorVariations) == channel ? 191: 63;
250 
251     return color;
252 }
253 
254 // Fill a RGBA8888 buffer with a multi-colored checkerboard pattern
fillRgba8888Buffer(uint8_t * buf,int w,int h,int stride)255 void fillRgba8888Buffer(uint8_t* buf, int w, int h, int stride)
256 {
257     const int blockWidth = w > 16 ? w / 16 : 1;
258     const int blockHeight = h > 16 ? h / 16 : 1;
259     const int bytesPerPixel = 4;
260 
261     // stride is in pixels, not in bytes
262     for (int x = 0; x < w; ++x) {
263         for (int y = 0; y < h; ++y) {
264             int blockX = (x / blockWidth);
265             int blockY = (y / blockHeight);
266 
267             uint8_t r = chooseColorRgba8888(blockX, blockY, 0);
268             uint8_t g = chooseColorRgba8888(blockX, blockY, 1);
269             uint8_t b = chooseColorRgba8888(blockX, blockY, 2);
270 
271             buf[(y*stride + x)*bytesPerPixel + 0] = r;
272             buf[(y*stride + x)*bytesPerPixel + 1] = g;
273             buf[(y*stride + x)*bytesPerPixel + 2] = b;
274             buf[(y*stride + x)*bytesPerPixel + 3] = 255;
275         }
276     }
277 }
278 
279 // Fill a RAW sensor buffer with a multi-colored checkerboard pattern.
280 // Assumes GRBG mosaic ordering. Result should be a grid in a 2x2 pattern
281 // of [ R, B; G, W]
fillBayerRawBuffer(uint8_t * buf,int w,int h,int stride)282 void fillBayerRawBuffer(uint8_t* buf, int w, int h, int stride) {
283     ALOGVV("fillBayerRawBuffer: %p with %d x %d, stride %d", buf, w, h ,stride);
284     // Blocks need to be even-width/height, aim for 8-wide otherwise
285     const int blockWidth = (w > 16 ? w / 8 : 2) & ~0x1;
286     const int blockHeight = (h > 16 ? h / 8 : 2) & ~0x1;
287     for (int y = 0; y < h; y+=2) {
288         uint16_t *bPtr1 = ((uint16_t*)buf) + stride*y;
289         uint16_t *bPtr2 = bPtr1 + stride;
290         for (int x = 0; x < w; x+=2) {
291             int blockX = (x / blockWidth ) & 1;
292             int blockY = (y / blockHeight) & 1;
293             unsigned short r = (blockX == blockY) ? 1000 : 200;
294             unsigned short g = blockY ? 1000: 200;
295             unsigned short b = blockX ? 1000: 200;
296             // GR row
297             *bPtr1++ = g;
298             *bPtr1++ = r;
299             // BG row
300             *bPtr2++ = b;
301             *bPtr2++ = g;
302         }
303     }
304 
305 }
306 
307 template<typename T> // uint8_t or uint16_t
checkGreyscaleBuffer(const CpuConsumer::LockedBuffer & buf)308 void checkGreyscaleBuffer(const CpuConsumer::LockedBuffer &buf) {
309     uint32_t w = buf.width;
310     uint32_t h = buf.height;
311     const int blockWidth = w > 16 ? w / 16 : 1;
312     const int blockHeight = h > 16 ? h / 16 : 1;
313     const int blockRows = h / blockHeight;
314     const int blockCols = w / blockWidth;
315 
316     // Top-left square is bright
317     checkPixel(buf, 0, 0, 191);
318     checkPixel(buf, 1, 0, 191);
319     checkPixel(buf, 0, 1, 191);
320     checkPixel(buf, 1, 1, 191);
321 
322     // One-right square is dark
323     checkPixel(buf, blockWidth,     0, 63);
324     checkPixel(buf, blockWidth + 1, 0, 63);
325     checkPixel(buf, blockWidth,     1, 63);
326     checkPixel(buf, blockWidth + 1, 1, 63);
327 
328     // One-down square is dark
329     checkPixel(buf, 0, blockHeight, 63);
330     checkPixel(buf, 1, blockHeight, 63);
331     checkPixel(buf, 0, blockHeight + 1, 63);
332     checkPixel(buf, 1, blockHeight + 1, 63);
333 
334     // One-diag square is bright
335     checkPixel(buf, blockWidth,     blockHeight, 191);
336     checkPixel(buf, blockWidth + 1, blockHeight, 191);
337     checkPixel(buf, blockWidth,     blockHeight + 1, 191);
338     checkPixel(buf, blockWidth + 1, blockHeight + 1, 191);
339 
340     // Test bottom-right pixel
341     const int maxBlockX = ((w-1 + (blockWidth-1)) / blockWidth) & 0x1;
342     const int maxBlockY = ((h-1 + (blockHeight-1)) / blockHeight) & 0x1;
343     uint32_t pixelValue = ((maxBlockX % 2) == (maxBlockY % 2)) ? 191 : 63;
344     checkPixel(buf, w-1, h-1, pixelValue);
345 }
346 
checkRgba8888Buffer(const CpuConsumer::LockedBuffer & buf)347 void checkRgba8888Buffer(const CpuConsumer::LockedBuffer &buf) {
348     uint32_t w = buf.width;
349     uint32_t h = buf.height;
350     const int blockWidth = w > 16 ? w / 16 : 1;
351     const int blockHeight = h > 16 ? h / 16 : 1;
352     const int blockRows = h / blockHeight;
353     const int blockCols = w / blockWidth;
354 
355     // Top-left square is bright red
356     checkPixel(buf, 0, 0, 191, 63, 63);
357     checkPixel(buf, 1, 0, 191, 63, 63);
358     checkPixel(buf, 0, 1, 191, 63, 63);
359     checkPixel(buf, 1, 1, 191, 63, 63);
360 
361     // One-right square is bright green
362     checkPixel(buf, blockWidth,     0, 63, 191, 63);
363     checkPixel(buf, blockWidth + 1, 0, 63, 191, 63);
364     checkPixel(buf, blockWidth,     1, 63, 191, 63);
365     checkPixel(buf, blockWidth + 1, 1, 63, 191, 63);
366 
367     // One-down square is bright green
368     checkPixel(buf, 0, blockHeight, 63, 191, 63);
369     checkPixel(buf, 1, blockHeight, 63, 191, 63);
370     checkPixel(buf, 0, blockHeight + 1, 63, 191, 63);
371     checkPixel(buf, 1, blockHeight + 1, 63, 191, 63);
372 
373     // One-diag square is bright blue
374     checkPixel(buf, blockWidth,     blockHeight, 63, 63, 191);
375     checkPixel(buf, blockWidth + 1, blockHeight, 63, 63, 191);
376     checkPixel(buf, blockWidth,     blockHeight + 1, 63, 63, 191);
377     checkPixel(buf, blockWidth + 1, blockHeight + 1, 63, 63, 191);
378 
379     // Test bottom-right pixel
380     {
381         const int maxBlockX = ((w-1) / blockWidth);
382         const int maxBlockY = ((h-1) / blockHeight);
383         uint8_t r = chooseColorRgba8888(maxBlockX, maxBlockY, 0);
384         uint8_t g = chooseColorRgba8888(maxBlockX, maxBlockY, 1);
385         uint8_t b = chooseColorRgba8888(maxBlockX, maxBlockY, 2);
386         checkPixel(buf, w-1, h-1, r, g, b);
387     }
388 }
389 
checkBayerRawBuffer(const CpuConsumer::LockedBuffer & buf)390 void checkBayerRawBuffer(const CpuConsumer::LockedBuffer &buf) {
391     uint32_t w = buf.width;
392     uint32_t h = buf.height;
393     const int blockWidth = (w > 16 ? w / 8 : 2) & ~0x1;
394     const int blockHeight = (h > 16 ? h / 8 : 2) & ~0x1;
395     const int blockRows = h / blockHeight;
396     const int blockCols = w / blockWidth;
397 
398     // Top-left square is red
399     checkPixel(buf, 0, 0, 1000, 200, 200);
400     checkPixel(buf, 1, 0, 1000, 200, 200);
401     checkPixel(buf, 0, 1, 1000, 200, 200);
402     checkPixel(buf, 1, 1, 1000, 200, 200);
403 
404     // One-right square is blue
405     checkPixel(buf, blockWidth,     0, 200, 200, 1000);
406     checkPixel(buf, blockWidth + 1, 0, 200, 200, 1000);
407     checkPixel(buf, blockWidth,     1, 200, 200, 1000);
408     checkPixel(buf, blockWidth + 1, 1, 200, 200, 1000);
409 
410     // One-down square is green
411     checkPixel(buf, 0, blockHeight, 200, 1000, 200);
412     checkPixel(buf, 1, blockHeight, 200, 1000, 200);
413     checkPixel(buf, 0, blockHeight + 1, 200, 1000, 200);
414     checkPixel(buf, 1, blockHeight + 1, 200, 1000, 200);
415 
416     // One-diag square is white
417     checkPixel(buf, blockWidth,     blockHeight, 1000, 1000, 1000);
418     checkPixel(buf, blockWidth + 1, blockHeight, 1000, 1000, 1000);
419     checkPixel(buf, blockWidth,     blockHeight + 1, 1000, 1000, 1000);
420     checkPixel(buf, blockWidth + 1, blockHeight + 1, 1000, 1000, 1000);
421 
422     // Test bottom-right pixel
423     const int maxBlockX = ((w-1) / blockWidth) & 0x1;
424     const int maxBlockY = ((w-1) / blockHeight) & 0x1;
425     unsigned short maxR = (maxBlockX == maxBlockY) ? 1000 : 200;
426     unsigned short maxG = maxBlockY ? 1000: 200;
427     unsigned short maxB = maxBlockX ? 1000: 200;
428     checkPixel(buf, w-1, h-1, maxR, maxG, maxB);
429 }
430 
checkAnyBuffer(const CpuConsumer::LockedBuffer & buf,int format)431 void checkAnyBuffer(const CpuConsumer::LockedBuffer &buf, int format) {
432     switch (format) {
433         case HAL_PIXEL_FORMAT_RAW16:
434             checkBayerRawBuffer(buf);
435             break;
436         case HAL_PIXEL_FORMAT_Y8:
437             checkGreyscaleBuffer<uint8_t>(buf);
438             break;
439         case HAL_PIXEL_FORMAT_Y16:
440             checkGreyscaleBuffer<uint16_t>(buf);
441             break;
442         case HAL_PIXEL_FORMAT_RGBA_8888:
443             checkRgba8888Buffer(buf);
444             break;
445     }
446 }
447 
448 // Configures the ANativeWindow producer-side interface based on test parameters
configureANW(const sp<ANativeWindow> & anw,const CpuConsumerTestParams & params,int maxBufferSlack)449 void configureANW(const sp<ANativeWindow>& anw,
450         const CpuConsumerTestParams& params,
451         int maxBufferSlack) {
452     status_t err;
453     err = native_window_api_connect(anw.get(), NATIVE_WINDOW_API_CPU);
454     ASSERT_NO_ERROR(err, "connect error: ");
455 
456     err = native_window_set_buffers_dimensions(anw.get(),
457             params.width, params.height);
458     ASSERT_NO_ERROR(err, "set_buffers_dimensions error: ");
459 
460     err = native_window_set_buffers_format(anw.get(), params.format);
461     ASSERT_NO_ERROR(err, "set_buffers_format error: ");
462 
463     err = native_window_set_usage(anw.get(),
464             GRALLOC_USAGE_SW_WRITE_OFTEN);
465     ASSERT_NO_ERROR(err, "set_usage error: ");
466 
467     int minUndequeuedBuffers;
468     err = anw.get()->query(anw.get(),
469             NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
470             &minUndequeuedBuffers);
471     ASSERT_NO_ERROR(err, "query error: ");
472 
473     ALOGVV("Setting buffer count to %d",
474             maxBufferSlack + 1 + minUndequeuedBuffers);
475     err = native_window_set_buffer_count(anw.get(),
476             maxBufferSlack + 1 + minUndequeuedBuffers);
477     ASSERT_NO_ERROR(err, "set_buffer_count error: ");
478 
479 }
480 
481 // Produce one frame of image data; assumes format and resolution configuration
482 // is already done.
produceOneFrame(const sp<ANativeWindow> & anw,const CpuConsumerTestParams & params,int64_t timestamp,uint32_t * stride)483 void produceOneFrame(const sp<ANativeWindow>& anw,
484         const CpuConsumerTestParams& params,
485         int64_t timestamp, uint32_t *stride) {
486     status_t err;
487     ANativeWindowBuffer* anb;
488     ALOGVV("Dequeue buffer from %p", anw.get());
489     err = native_window_dequeue_buffer_and_wait(anw.get(), &anb);
490     ASSERT_NO_ERROR(err, "dequeueBuffer error: ");
491 
492     ASSERT_TRUE(anb != NULL);
493 
494     sp<GraphicBuffer> buf(GraphicBuffer::from(anb));
495 
496     *stride = buf->getStride();
497     uint8_t* img = NULL;
498 
499     ALOGVV("Lock buffer from %p for write", anw.get());
500     err = buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
501     ASSERT_NO_ERROR(err, "lock error: ");
502 
503     switch (params.format) {
504         case HAL_PIXEL_FORMAT_YV12:
505             fillYV12Buffer(img, params.width, params.height, *stride);
506             break;
507         case HAL_PIXEL_FORMAT_RAW16:
508             fillBayerRawBuffer(img, params.width, params.height, buf->getStride());
509             break;
510         case HAL_PIXEL_FORMAT_Y8:
511             fillGreyscaleBuffer<uint8_t>(img, params.width, params.height,
512                                          buf->getStride(), /*bpp*/8);
513             break;
514         case HAL_PIXEL_FORMAT_Y16:
515             fillGreyscaleBuffer<uint16_t>((uint16_t*)img, params.width,
516                                           params.height, buf->getStride(),
517                                           /*bpp*/16);
518             break;
519         case HAL_PIXEL_FORMAT_RGBA_8888:
520             fillRgba8888Buffer(img, params.width, params.height, buf->getStride());
521             break;
522         default:
523             FAIL() << "Unknown pixel format under test!";
524             break;
525     }
526     ALOGVV("Unlock buffer from %p", anw.get());
527     err = buf->unlock();
528     ASSERT_NO_ERROR(err, "unlock error: ");
529 
530     ALOGVV("Set timestamp to %p", anw.get());
531     err = native_window_set_buffers_timestamp(anw.get(), timestamp);
532     ASSERT_NO_ERROR(err, "set_buffers_timestamp error: ");
533 
534     ALOGVV("Queue buffer to %p", anw.get());
535     err = anw->queueBuffer(anw.get(), buf->getNativeBuffer(), -1);
536     ASSERT_NO_ERROR(err, "queueBuffer error:");
537 };
538 
539 // This test is disabled because the HAL_PIXEL_FORMAT_RAW16 format is not
540 // supported on all devices.
TEST_P(CpuConsumerTest,FromCpuSingle)541 TEST_P(CpuConsumerTest, FromCpuSingle) {
542     status_t err;
543     CpuConsumerTestParams params = GetParam();
544 
545     // Set up
546 
547     ASSERT_NO_FATAL_FAILURE(configureANW(mANW, params, 1));
548 
549     // Produce
550 
551     const int64_t time = 12345678L;
552     uint32_t stride;
553     ASSERT_NO_FATAL_FAILURE(produceOneFrame(mANW, params, time,
554                     &stride));
555 
556     // Consume
557 
558     CpuConsumer::LockedBuffer b;
559     err = mCC->lockNextBuffer(&b);
560     ASSERT_NO_ERROR(err, "getNextBuffer error: ");
561 
562     ASSERT_TRUE(b.data != NULL);
563     EXPECT_EQ(params.width,  b.width);
564     EXPECT_EQ(params.height, b.height);
565     EXPECT_EQ(params.format, b.format);
566     EXPECT_EQ(stride, b.stride);
567     EXPECT_EQ(time, b.timestamp);
568 
569     checkAnyBuffer(b, GetParam().format);
570     mCC->unlockBuffer(b);
571 }
572 
573 // This test is disabled because the HAL_PIXEL_FORMAT_RAW16 format is not
574 // supported on all devices.
TEST_P(CpuConsumerTest,FromCpuManyInQueue)575 TEST_P(CpuConsumerTest, FromCpuManyInQueue) {
576     status_t err;
577     CpuConsumerTestParams params = GetParam();
578 
579     const int numInQueue = 5;
580     // Set up
581 
582     ASSERT_NO_FATAL_FAILURE(configureANW(mANW, params, numInQueue));
583 
584     // Produce
585 
586     const int64_t time[numInQueue] = { 1L, 2L, 3L, 4L, 5L};
587     uint32_t stride[numInQueue];
588 
589     for (int i = 0; i < numInQueue; i++) {
590         ALOGV("Producing frame %d", i);
591         ASSERT_NO_FATAL_FAILURE(produceOneFrame(mANW, params, time[i],
592                         &stride[i]));
593     }
594 
595     // Consume
596 
597     for (int i = 0; i < numInQueue; i++) {
598         ALOGV("Consuming frame %d", i);
599         CpuConsumer::LockedBuffer b;
600         err = mCC->lockNextBuffer(&b);
601         ASSERT_NO_ERROR(err, "getNextBuffer error: ");
602 
603         ASSERT_TRUE(b.data != NULL);
604         EXPECT_EQ(params.width,  b.width);
605         EXPECT_EQ(params.height, b.height);
606         EXPECT_EQ(params.format, b.format);
607         EXPECT_EQ(stride[i], b.stride);
608         EXPECT_EQ(time[i], b.timestamp);
609 
610         checkAnyBuffer(b, GetParam().format);
611 
612         mCC->unlockBuffer(b);
613     }
614 }
615 
616 // This test is disabled because the HAL_PIXEL_FORMAT_RAW16 format is not
617 // supported on all devices.
TEST_P(CpuConsumerTest,FromCpuLockMax)618 TEST_P(CpuConsumerTest, FromCpuLockMax) {
619     status_t err;
620     CpuConsumerTestParams params = GetParam();
621 
622     // Set up
623 
624     ASSERT_NO_FATAL_FAILURE(configureANW(mANW, params, params.maxLockedBuffers + 1));
625 
626     // Produce
627 
628     const int64_t time = 1234L;
629     uint32_t stride;
630 
631     for (int i = 0; i < params.maxLockedBuffers + 1; i++) {
632         ALOGV("Producing frame %d", i);
633         ASSERT_NO_FATAL_FAILURE(produceOneFrame(mANW, params, time,
634                         &stride));
635     }
636 
637     // Consume
638 
639     std::vector<CpuConsumer::LockedBuffer> b(params.maxLockedBuffers);
640     for (int i = 0; i < params.maxLockedBuffers; i++) {
641         ALOGV("Locking frame %d", i);
642         err = mCC->lockNextBuffer(&b[i]);
643         ASSERT_NO_ERROR(err, "getNextBuffer error: ");
644 
645         ASSERT_TRUE(b[i].data != NULL);
646         EXPECT_EQ(params.width,  b[i].width);
647         EXPECT_EQ(params.height, b[i].height);
648         EXPECT_EQ(params.format, b[i].format);
649         EXPECT_EQ(stride, b[i].stride);
650         EXPECT_EQ(time, b[i].timestamp);
651 
652         checkAnyBuffer(b[i], GetParam().format);
653     }
654 
655     ALOGV("Locking frame %d (too many)", params.maxLockedBuffers);
656     CpuConsumer::LockedBuffer bTooMuch;
657     err = mCC->lockNextBuffer(&bTooMuch);
658     ASSERT_TRUE(err == NOT_ENOUGH_DATA) << "Allowing too many locks";
659 
660     ALOGV("Unlocking frame 0");
661     err = mCC->unlockBuffer(b[0]);
662     ASSERT_NO_ERROR(err, "Could not unlock buffer 0: ");
663 
664     ALOGV("Locking frame %d (should work now)", params.maxLockedBuffers);
665     err = mCC->lockNextBuffer(&bTooMuch);
666     ASSERT_NO_ERROR(err, "Did not allow new lock after unlock");
667 
668     ASSERT_TRUE(bTooMuch.data != NULL);
669     EXPECT_EQ(params.width,  bTooMuch.width);
670     EXPECT_EQ(params.height, bTooMuch.height);
671     EXPECT_EQ(params.format, bTooMuch.format);
672     EXPECT_EQ(stride, bTooMuch.stride);
673     EXPECT_EQ(time, bTooMuch.timestamp);
674 
675     checkAnyBuffer(bTooMuch, GetParam().format);
676 
677     ALOGV("Unlocking extra buffer");
678     err = mCC->unlockBuffer(bTooMuch);
679     ASSERT_NO_ERROR(err, "Could not unlock extra buffer: ");
680 
681     ALOGV("Locking frame %d (no more available)", params.maxLockedBuffers + 1);
682     err = mCC->lockNextBuffer(&b[0]);
683     ASSERT_EQ(BAD_VALUE, err) << "Not out of buffers somehow";
684 
685     for (int i = 1; i < params.maxLockedBuffers; i++) {
686         mCC->unlockBuffer(b[i]);
687     }
688 }
689 
690 CpuConsumerTestParams y8TestSets[] = {
691     { 512,   512, 1, HAL_PIXEL_FORMAT_Y8},
692     { 512,   512, 3, HAL_PIXEL_FORMAT_Y8},
693     { 2608, 1960, 1, HAL_PIXEL_FORMAT_Y8},
694     { 2608, 1960, 3, HAL_PIXEL_FORMAT_Y8},
695     { 100,   100, 1, HAL_PIXEL_FORMAT_Y8},
696     { 100,   100, 3, HAL_PIXEL_FORMAT_Y8},
697 };
698 
699 CpuConsumerTestParams y16TestSets[] = {
700     { 512,   512, 1, HAL_PIXEL_FORMAT_Y16},
701     { 512,   512, 3, HAL_PIXEL_FORMAT_Y16},
702     { 2608, 1960, 1, HAL_PIXEL_FORMAT_Y16},
703     { 2608, 1960, 3, HAL_PIXEL_FORMAT_Y16},
704     { 100,   100, 1, HAL_PIXEL_FORMAT_Y16},
705     { 100,   100, 3, HAL_PIXEL_FORMAT_Y16},
706 };
707 
708 CpuConsumerTestParams rawTestSets[] = {
709     { 512,   512, 1, HAL_PIXEL_FORMAT_RAW16},
710     { 512,   512, 3, HAL_PIXEL_FORMAT_RAW16},
711     { 2608, 1960, 1, HAL_PIXEL_FORMAT_RAW16},
712     { 2608, 1960, 3, HAL_PIXEL_FORMAT_RAW16},
713     { 100,   100, 1, HAL_PIXEL_FORMAT_RAW16},
714     { 100,   100, 3, HAL_PIXEL_FORMAT_RAW16},
715 };
716 
717 CpuConsumerTestParams rgba8888TestSets[] = {
718     { 512,   512, 1, HAL_PIXEL_FORMAT_RGBA_8888},
719     { 512,   512, 3, HAL_PIXEL_FORMAT_RGBA_8888},
720     { 2608, 1960, 1, HAL_PIXEL_FORMAT_RGBA_8888},
721     { 2608, 1960, 3, HAL_PIXEL_FORMAT_RGBA_8888},
722     { 100,   100, 1, HAL_PIXEL_FORMAT_RGBA_8888},
723     { 100,   100, 3, HAL_PIXEL_FORMAT_RGBA_8888},
724 };
725 
726 #if CPU_CONSUMER_TEST_FORMAT_Y8
727 INSTANTIATE_TEST_CASE_P(Y8Tests,
728         CpuConsumerTest,
729         ::testing::ValuesIn(y8TestSets));
730 #endif
731 
732 #if CPU_CONSUMER_TEST_FORMAT_Y16
733 INSTANTIATE_TEST_CASE_P(Y16Tests,
734         CpuConsumerTest,
735         ::testing::ValuesIn(y16TestSets));
736 #endif
737 
738 #if CPU_CONSUMER_TEST_FORMAT_RAW
739 INSTANTIATE_TEST_CASE_P(RawTests,
740         CpuConsumerTest,
741         ::testing::ValuesIn(rawTestSets));
742 #endif
743 
744 #if CPU_CONSUMER_TEST_FORMAT_RGBA_8888
745 INSTANTIATE_TEST_CASE_P(Rgba8888Tests,
746         CpuConsumerTest,
747         ::testing::ValuesIn(rgba8888TestSets));
748 #endif
749 
750 
751 
752 } // namespace android
753