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 void fillYV12BufferRect(uint8_t* buf, int w, int h, int stride,
448         const android_native_rect_t& rect);
449 
450 void fillRGBA8Buffer(uint8_t* buf, int w, int h, int stride);
451 
452 void fillRGBA8BufferSolid(uint8_t* buf, int w, int h, int stride, uint8_t r,
453         uint8_t g, uint8_t b, uint8_t a);
454 
455 // Configures the ANativeWindow producer-side interface based on test parameters
configureANW(const sp<ANativeWindow> & anw,const CpuConsumerTestParams & params,int maxBufferSlack)456 void configureANW(const sp<ANativeWindow>& anw,
457         const CpuConsumerTestParams& params,
458         int maxBufferSlack) {
459     status_t err;
460     err = native_window_set_buffers_dimensions(anw.get(),
461             params.width, params.height);
462     ASSERT_NO_ERROR(err, "set_buffers_dimensions error: ");
463 
464     err = native_window_set_buffers_format(anw.get(), params.format);
465     ASSERT_NO_ERROR(err, "set_buffers_format error: ");
466 
467     err = native_window_set_usage(anw.get(),
468             GRALLOC_USAGE_SW_WRITE_OFTEN);
469     ASSERT_NO_ERROR(err, "set_usage error: ");
470 
471     int minUndequeuedBuffers;
472     err = anw.get()->query(anw.get(),
473             NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
474             &minUndequeuedBuffers);
475     ASSERT_NO_ERROR(err, "query error: ");
476 
477     ALOGVV("Setting buffer count to %d",
478             maxBufferSlack + 1 + minUndequeuedBuffers);
479     err = native_window_set_buffer_count(anw.get(),
480             maxBufferSlack + 1 + minUndequeuedBuffers);
481     ASSERT_NO_ERROR(err, "set_buffer_count error: ");
482 
483 }
484 
485 // Produce one frame of image data; assumes format and resolution configuration
486 // is already done.
produceOneFrame(const sp<ANativeWindow> & anw,const CpuConsumerTestParams & params,int64_t timestamp,uint32_t * stride)487 void produceOneFrame(const sp<ANativeWindow>& anw,
488         const CpuConsumerTestParams& params,
489         int64_t timestamp, uint32_t *stride) {
490     status_t err;
491     ANativeWindowBuffer* anb;
492     ALOGVV("Dequeue buffer from %p", anw.get());
493     err = native_window_dequeue_buffer_and_wait(anw.get(), &anb);
494     ASSERT_NO_ERROR(err, "dequeueBuffer error: ");
495 
496     ASSERT_TRUE(anb != NULL);
497 
498     sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
499 
500     *stride = buf->getStride();
501     uint8_t* img = NULL;
502 
503     ALOGVV("Lock buffer from %p for write", anw.get());
504     err = buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
505     ASSERT_NO_ERROR(err, "lock error: ");
506 
507     switch (params.format) {
508         case HAL_PIXEL_FORMAT_YV12:
509             fillYV12Buffer(img, params.width, params.height, *stride);
510             break;
511         case HAL_PIXEL_FORMAT_RAW16:
512             fillBayerRawBuffer(img, params.width, params.height, buf->getStride());
513             break;
514         case HAL_PIXEL_FORMAT_Y8:
515             fillGreyscaleBuffer<uint8_t>(img, params.width, params.height,
516                                          buf->getStride(), /*bpp*/8);
517             break;
518         case HAL_PIXEL_FORMAT_Y16:
519             fillGreyscaleBuffer<uint16_t>((uint16_t*)img, params.width,
520                                           params.height, buf->getStride(),
521                                           /*bpp*/16);
522             break;
523         case HAL_PIXEL_FORMAT_RGBA_8888:
524             fillRgba8888Buffer(img, params.width, params.height, buf->getStride());
525             break;
526         default:
527             FAIL() << "Unknown pixel format under test!";
528             break;
529     }
530     ALOGVV("Unlock buffer from %p", anw.get());
531     err = buf->unlock();
532     ASSERT_NO_ERROR(err, "unlock error: ");
533 
534     ALOGVV("Set timestamp to %p", anw.get());
535     err = native_window_set_buffers_timestamp(anw.get(), timestamp);
536     ASSERT_NO_ERROR(err, "set_buffers_timestamp error: ");
537 
538     ALOGVV("Queue buffer to %p", anw.get());
539     err = anw->queueBuffer(anw.get(), buf->getNativeBuffer(), -1);
540     ASSERT_NO_ERROR(err, "queueBuffer error:");
541 };
542 
543 // This test is disabled because the HAL_PIXEL_FORMAT_RAW16 format is not
544 // supported on all devices.
TEST_P(CpuConsumerTest,FromCpuSingle)545 TEST_P(CpuConsumerTest, FromCpuSingle) {
546     status_t err;
547     CpuConsumerTestParams params = GetParam();
548 
549     // Set up
550 
551     ASSERT_NO_FATAL_FAILURE(configureANW(mANW, params, 1));
552 
553     // Produce
554 
555     const int64_t time = 12345678L;
556     uint32_t stride;
557     ASSERT_NO_FATAL_FAILURE(produceOneFrame(mANW, params, time,
558                     &stride));
559 
560     // Consume
561 
562     CpuConsumer::LockedBuffer b;
563     err = mCC->lockNextBuffer(&b);
564     ASSERT_NO_ERROR(err, "getNextBuffer error: ");
565 
566     ASSERT_TRUE(b.data != NULL);
567     EXPECT_EQ(params.width,  b.width);
568     EXPECT_EQ(params.height, b.height);
569     EXPECT_EQ(params.format, b.format);
570     EXPECT_EQ(stride, b.stride);
571     EXPECT_EQ(time, b.timestamp);
572 
573     checkAnyBuffer(b, GetParam().format);
574     mCC->unlockBuffer(b);
575 }
576 
577 // This test is disabled because the HAL_PIXEL_FORMAT_RAW16 format is not
578 // supported on all devices.
TEST_P(CpuConsumerTest,FromCpuManyInQueue)579 TEST_P(CpuConsumerTest, FromCpuManyInQueue) {
580     status_t err;
581     CpuConsumerTestParams params = GetParam();
582 
583     const int numInQueue = 5;
584     // Set up
585 
586     ASSERT_NO_FATAL_FAILURE(configureANW(mANW, params, numInQueue));
587 
588     // Produce
589 
590     const int64_t time[numInQueue] = { 1L, 2L, 3L, 4L, 5L};
591     uint32_t stride[numInQueue];
592 
593     for (int i = 0; i < numInQueue; i++) {
594         ALOGV("Producing frame %d", i);
595         ASSERT_NO_FATAL_FAILURE(produceOneFrame(mANW, params, time[i],
596                         &stride[i]));
597     }
598 
599     // Consume
600 
601     for (int i = 0; i < numInQueue; i++) {
602         ALOGV("Consuming frame %d", i);
603         CpuConsumer::LockedBuffer b;
604         err = mCC->lockNextBuffer(&b);
605         ASSERT_NO_ERROR(err, "getNextBuffer error: ");
606 
607         ASSERT_TRUE(b.data != NULL);
608         EXPECT_EQ(params.width,  b.width);
609         EXPECT_EQ(params.height, b.height);
610         EXPECT_EQ(params.format, b.format);
611         EXPECT_EQ(stride[i], b.stride);
612         EXPECT_EQ(time[i], b.timestamp);
613 
614         checkAnyBuffer(b, GetParam().format);
615 
616         mCC->unlockBuffer(b);
617     }
618 }
619 
620 // This test is disabled because the HAL_PIXEL_FORMAT_RAW16 format is not
621 // supported on all devices.
TEST_P(CpuConsumerTest,FromCpuLockMax)622 TEST_P(CpuConsumerTest, FromCpuLockMax) {
623     status_t err;
624     CpuConsumerTestParams params = GetParam();
625 
626     // Set up
627 
628     ASSERT_NO_FATAL_FAILURE(configureANW(mANW, params, params.maxLockedBuffers + 1));
629 
630     // Produce
631 
632     const int64_t time = 1234L;
633     uint32_t stride;
634 
635     for (int i = 0; i < params.maxLockedBuffers + 1; i++) {
636         ALOGV("Producing frame %d", i);
637         ASSERT_NO_FATAL_FAILURE(produceOneFrame(mANW, params, time,
638                         &stride));
639     }
640 
641     // Consume
642 
643     CpuConsumer::LockedBuffer *b = new CpuConsumer::LockedBuffer[params.maxLockedBuffers];
644     for (int i = 0; i < params.maxLockedBuffers; i++) {
645         ALOGV("Locking frame %d", i);
646         err = mCC->lockNextBuffer(&b[i]);
647         ASSERT_NO_ERROR(err, "getNextBuffer error: ");
648 
649         ASSERT_TRUE(b[i].data != NULL);
650         EXPECT_EQ(params.width,  b[i].width);
651         EXPECT_EQ(params.height, b[i].height);
652         EXPECT_EQ(params.format, b[i].format);
653         EXPECT_EQ(stride, b[i].stride);
654         EXPECT_EQ(time, b[i].timestamp);
655 
656         checkAnyBuffer(b[i], GetParam().format);
657     }
658 
659     ALOGV("Locking frame %d (too many)", params.maxLockedBuffers);
660     CpuConsumer::LockedBuffer bTooMuch;
661     err = mCC->lockNextBuffer(&bTooMuch);
662     ASSERT_TRUE(err == NOT_ENOUGH_DATA) << "Allowing too many locks";
663 
664     ALOGV("Unlocking frame 0");
665     err = mCC->unlockBuffer(b[0]);
666     ASSERT_NO_ERROR(err, "Could not unlock buffer 0: ");
667 
668     ALOGV("Locking frame %d (should work now)", params.maxLockedBuffers);
669     err = mCC->lockNextBuffer(&bTooMuch);
670     ASSERT_NO_ERROR(err, "Did not allow new lock after unlock");
671 
672     ASSERT_TRUE(bTooMuch.data != NULL);
673     EXPECT_EQ(params.width,  bTooMuch.width);
674     EXPECT_EQ(params.height, bTooMuch.height);
675     EXPECT_EQ(params.format, bTooMuch.format);
676     EXPECT_EQ(stride, bTooMuch.stride);
677     EXPECT_EQ(time, bTooMuch.timestamp);
678 
679     checkAnyBuffer(bTooMuch, GetParam().format);
680 
681     ALOGV("Unlocking extra buffer");
682     err = mCC->unlockBuffer(bTooMuch);
683     ASSERT_NO_ERROR(err, "Could not unlock extra buffer: ");
684 
685     ALOGV("Locking frame %d (no more available)", params.maxLockedBuffers + 1);
686     err = mCC->lockNextBuffer(&b[0]);
687     ASSERT_EQ(BAD_VALUE, err) << "Not out of buffers somehow";
688 
689     for (int i = 1; i < params.maxLockedBuffers; i++) {
690         mCC->unlockBuffer(b[i]);
691     }
692 
693     delete[] b;
694 
695 }
696 
697 CpuConsumerTestParams y8TestSets[] = {
698     { 512,   512, 1, HAL_PIXEL_FORMAT_Y8},
699     { 512,   512, 3, HAL_PIXEL_FORMAT_Y8},
700     { 2608, 1960, 1, HAL_PIXEL_FORMAT_Y8},
701     { 2608, 1960, 3, HAL_PIXEL_FORMAT_Y8},
702     { 100,   100, 1, HAL_PIXEL_FORMAT_Y8},
703     { 100,   100, 3, HAL_PIXEL_FORMAT_Y8},
704 };
705 
706 CpuConsumerTestParams y16TestSets[] = {
707     { 512,   512, 1, HAL_PIXEL_FORMAT_Y16},
708     { 512,   512, 3, HAL_PIXEL_FORMAT_Y16},
709     { 2608, 1960, 1, HAL_PIXEL_FORMAT_Y16},
710     { 2608, 1960, 3, HAL_PIXEL_FORMAT_Y16},
711     { 100,   100, 1, HAL_PIXEL_FORMAT_Y16},
712     { 100,   100, 3, HAL_PIXEL_FORMAT_Y16},
713 };
714 
715 CpuConsumerTestParams rawTestSets[] = {
716     { 512,   512, 1, HAL_PIXEL_FORMAT_RAW16},
717     { 512,   512, 3, HAL_PIXEL_FORMAT_RAW16},
718     { 2608, 1960, 1, HAL_PIXEL_FORMAT_RAW16},
719     { 2608, 1960, 3, HAL_PIXEL_FORMAT_RAW16},
720     { 100,   100, 1, HAL_PIXEL_FORMAT_RAW16},
721     { 100,   100, 3, HAL_PIXEL_FORMAT_RAW16},
722 };
723 
724 CpuConsumerTestParams rgba8888TestSets[] = {
725     { 512,   512, 1, HAL_PIXEL_FORMAT_RGBA_8888},
726     { 512,   512, 3, HAL_PIXEL_FORMAT_RGBA_8888},
727     { 2608, 1960, 1, HAL_PIXEL_FORMAT_RGBA_8888},
728     { 2608, 1960, 3, HAL_PIXEL_FORMAT_RGBA_8888},
729     { 100,   100, 1, HAL_PIXEL_FORMAT_RGBA_8888},
730     { 100,   100, 3, HAL_PIXEL_FORMAT_RGBA_8888},
731 };
732 
733 #if CPU_CONSUMER_TEST_FORMAT_Y8
734 INSTANTIATE_TEST_CASE_P(Y8Tests,
735         CpuConsumerTest,
736         ::testing::ValuesIn(y8TestSets));
737 #endif
738 
739 #if CPU_CONSUMER_TEST_FORMAT_Y16
740 INSTANTIATE_TEST_CASE_P(Y16Tests,
741         CpuConsumerTest,
742         ::testing::ValuesIn(y16TestSets));
743 #endif
744 
745 #if CPU_CONSUMER_TEST_FORMAT_RAW
746 INSTANTIATE_TEST_CASE_P(RawTests,
747         CpuConsumerTest,
748         ::testing::ValuesIn(rawTestSets));
749 #endif
750 
751 #if CPU_CONSUMER_TEST_FORMAT_RGBA_8888
752 INSTANTIATE_TEST_CASE_P(Rgba8888Tests,
753         CpuConsumerTest,
754         ::testing::ValuesIn(rgba8888TestSets));
755 #endif
756 
757 
758 
759 } // namespace android
760