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