MultiTextureConsumer_test.cpp revision cb1fcdedaaf95acabeac6a2d5bff423d6ca62296
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 "MultiTextureConsumer_test"
18//#define LOG_NDEBUG 0
19
20#include "GLTest.h"
21
22#include <gui/GLConsumer.h>
23#include <gui/Surface.h>
24
25#include <android/native_window.h>
26
27#include <GLES/glext.h>
28
29namespace android {
30
31class MultiTextureConsumerTest : public GLTest {
32protected:
33    enum { TEX_ID = 123 };
34
35    virtual void SetUp() {
36        GLTest::SetUp();
37        sp<BufferQueue> bq = new BufferQueue();
38        mGlConsumer = new GLConsumer(bq, TEX_ID);
39        mSurface = new Surface(bq);
40        mANW = mSurface.get();
41
42    }
43    virtual void TearDown() {
44        GLTest::TearDown();
45    }
46    virtual EGLint const* getContextAttribs() {
47        return NULL;
48    }
49    virtual EGLint const* getConfigAttribs() {
50        static EGLint sDefaultConfigAttribs[] = {
51            EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
52            EGL_RED_SIZE, 8,
53            EGL_GREEN_SIZE, 8,
54            EGL_BLUE_SIZE, 8,
55            EGL_ALPHA_SIZE, 8,
56            EGL_NONE };
57
58        return sDefaultConfigAttribs;
59    }
60    sp<GLConsumer> mGlConsumer;
61    sp<Surface> mSurface;
62    ANativeWindow* mANW;
63};
64
65TEST_F(MultiTextureConsumerTest, EGLImageTargetWorks) {
66    ANativeWindow_Buffer buffer;
67
68    ASSERT_EQ(native_window_set_usage(mANW, GRALLOC_USAGE_SW_WRITE_OFTEN), NO_ERROR);
69    ASSERT_EQ(native_window_set_buffers_format(mANW, HAL_PIXEL_FORMAT_RGBA_8888), NO_ERROR);
70
71    glShadeModel(GL_FLAT);
72    glDisable(GL_DITHER);
73    glDisable(GL_CULL_FACE);
74    glViewport(0, 0, getSurfaceWidth(), getSurfaceHeight());
75    glOrthof(0, getSurfaceWidth(), 0, getSurfaceHeight(), 0, 1);
76    glEnableClientState(GL_VERTEX_ARRAY);
77    glColor4f(1, 1, 1, 1);
78
79    glBindTexture(GL_TEXTURE_EXTERNAL_OES, TEX_ID);
80    glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
81    glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
82    glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
83    glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
84
85    uint32_t texel = 0x80808080;
86    glBindTexture(GL_TEXTURE_2D, TEX_ID+1);
87    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, &texel);
88    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
89    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
90    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
91    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
92
93    glActiveTexture(GL_TEXTURE1);
94    glBindTexture(GL_TEXTURE_2D, TEX_ID+1);
95    glEnable(GL_TEXTURE_2D);
96    glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
97
98    glActiveTexture(GL_TEXTURE0);
99    glBindTexture(GL_TEXTURE_EXTERNAL_OES, TEX_ID);
100    glEnable(GL_TEXTURE_EXTERNAL_OES);
101    glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
102
103    glClear(GL_COLOR_BUFFER_BIT);
104    for (int i=0 ; i<8 ; i++) {
105        mSurface->lock(&buffer, NULL);
106        memset(buffer.bits, (i&7) * 0x20, buffer.stride * buffer.height * 4);
107        mSurface->unlockAndPost();
108
109        mGlConsumer->updateTexImage();
110
111        GLfloat vertices[][2] = { {i*16.0f, 0}, {(i+1)*16.0f, 0}, {(i+1)*16.0f, 16.0f}, {i*16.0f, 16.0f} };
112        glVertexPointer(2, GL_FLOAT, 0, vertices);
113        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
114
115        ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
116    }
117
118    for (int i=0 ; i<8 ; i++) {
119        EXPECT_TRUE(checkPixel(i*16 + 8,  8, i*16, i*16, i*16, i*16, 0));
120    }
121}
122
123} // namespace android
124