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