1 /*
2  * Copyright 2013 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 "SurfaceTextureFBO_test"
18 //#define LOG_NDEBUG 0
19 
20 #include "SurfaceTextureFBO.h"
21 
22 namespace android {
23 
24 // This test is intended to verify that proper synchronization is done when
25 // rendering into an FBO.
TEST_F(SurfaceTextureFBOTest,BlitFromCpuFilledBufferToFbo)26 TEST_F(SurfaceTextureFBOTest, BlitFromCpuFilledBufferToFbo) {
27     const int texWidth = 64;
28     const int texHeight = 64;
29 
30     ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
31             texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
32     ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
33             GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
34 
35     android_native_buffer_t* anb;
36     ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
37             &anb));
38     ASSERT_TRUE(anb != NULL);
39 
40     sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
41 
42     // Fill the buffer with green
43     uint8_t* img = NULL;
44     buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
45     fillRGBA8BufferSolid(img, texWidth, texHeight, buf->getStride(), 0, 255,
46             0, 255);
47     buf->unlock();
48     ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
49             -1));
50 
51     ASSERT_EQ(NO_ERROR, mST->updateTexImage());
52 
53     glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
54     drawTexture();
55     glBindFramebuffer(GL_FRAMEBUFFER, 0);
56 
57     for (int i = 0; i < 4; i++) {
58         SCOPED_TRACE(String8::format("frame %d", i).string());
59 
60         ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
61                 &anb));
62         ASSERT_TRUE(anb != NULL);
63 
64         buf = new GraphicBuffer(anb, false);
65 
66         // Fill the buffer with red
67         ASSERT_EQ(NO_ERROR, buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN,
68                 (void**)(&img)));
69         fillRGBA8BufferSolid(img, texWidth, texHeight, buf->getStride(), 255, 0,
70                 0, 255);
71         ASSERT_EQ(NO_ERROR, buf->unlock());
72         ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(),
73                 buf->getNativeBuffer(), -1));
74 
75         ASSERT_EQ(NO_ERROR, mST->updateTexImage());
76 
77         drawTexture();
78 
79         EXPECT_TRUE(checkPixel( 24, 39, 255, 0, 0, 255));
80     }
81 
82     glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
83 
84     EXPECT_TRUE(checkPixel( 24, 39, 0, 255, 0, 255));
85 }
86 
87 } // namespace android
88