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