1/*
2 * Copyright (C) 2011 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 "SurfaceTextureClient_test"
18//#define LOG_NDEBUG 0
19
20#include <EGL/egl.h>
21#include <GLES2/gl2.h>
22
23#include <gtest/gtest.h>
24#include <gui/GLConsumer.h>
25#include <gui/Surface.h>
26#include <gui/BufferQueue.h>
27#include <system/graphics.h>
28#include <utils/Log.h>
29#include <utils/Thread.h>
30
31EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
32#define CROP_EXT_STR "EGL_ANDROID_image_crop"
33
34namespace android {
35
36class SurfaceTextureClientTest : public ::testing::Test {
37protected:
38    SurfaceTextureClientTest():
39            mEglDisplay(EGL_NO_DISPLAY),
40            mEglSurface(EGL_NO_SURFACE),
41            mEglContext(EGL_NO_CONTEXT),
42            mEglConfig(NULL) {
43    }
44
45    virtual void SetUp() {
46        const ::testing::TestInfo* const testInfo =
47            ::testing::UnitTest::GetInstance()->current_test_info();
48        ALOGV("Begin test: %s.%s", testInfo->test_case_name(),
49                testInfo->name());
50
51        sp<IGraphicBufferProducer> producer;
52        sp<IGraphicBufferConsumer> consumer;
53        BufferQueue::createBufferQueue(&producer, &consumer);
54        mST = new GLConsumer(consumer, 123, GLConsumer::TEXTURE_EXTERNAL, true,
55                false);
56        mSTC = new Surface(producer);
57        mANW = mSTC;
58
59        // We need a valid GL context so we can test updateTexImage()
60        // This initializes EGL and create a dummy GL context with a
61        // pbuffer render target.
62        mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
63        ASSERT_EQ(EGL_SUCCESS, eglGetError());
64        ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay);
65
66        EGLint majorVersion, minorVersion;
67        EXPECT_TRUE(eglInitialize(mEglDisplay, &majorVersion, &minorVersion));
68        ASSERT_EQ(EGL_SUCCESS, eglGetError());
69
70        EGLConfig myConfig;
71        EGLint numConfigs = 0;
72        EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(),
73                &myConfig, 1, &numConfigs));
74        ASSERT_EQ(EGL_SUCCESS, eglGetError());
75
76        mEglConfig = myConfig;
77        EGLint pbufferAttribs[] = {
78            EGL_WIDTH, 16,
79            EGL_HEIGHT, 16,
80            EGL_NONE };
81        mEglSurface = eglCreatePbufferSurface(mEglDisplay, myConfig, pbufferAttribs);
82        ASSERT_EQ(EGL_SUCCESS, eglGetError());
83        ASSERT_NE(EGL_NO_SURFACE, mEglSurface);
84
85        mEglContext = eglCreateContext(mEglDisplay, myConfig, EGL_NO_CONTEXT, 0);
86        ASSERT_EQ(EGL_SUCCESS, eglGetError());
87        ASSERT_NE(EGL_NO_CONTEXT, mEglContext);
88
89        EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext));
90        ASSERT_EQ(EGL_SUCCESS, eglGetError());
91    }
92
93    virtual void TearDown() {
94        mST.clear();
95        mSTC.clear();
96        mANW.clear();
97
98        eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
99        eglDestroyContext(mEglDisplay, mEglContext);
100        eglDestroySurface(mEglDisplay, mEglSurface);
101        eglTerminate(mEglDisplay);
102
103        const ::testing::TestInfo* const testInfo =
104            ::testing::UnitTest::GetInstance()->current_test_info();
105        ALOGV("End test:   %s.%s", testInfo->test_case_name(),
106                testInfo->name());
107    }
108
109    virtual EGLint const* getConfigAttribs() {
110        static EGLint sDefaultConfigAttribs[] = {
111            EGL_SURFACE_TYPE, EGL_PBUFFER_BIT | EGL_WINDOW_BIT,
112            EGL_NONE
113        };
114
115        return sDefaultConfigAttribs;
116    }
117
118    sp<GLConsumer> mST;
119    sp<Surface> mSTC;
120    sp<ANativeWindow> mANW;
121
122    EGLDisplay mEglDisplay;
123    EGLSurface mEglSurface;
124    EGLContext mEglContext;
125    EGLConfig  mEglConfig;
126};
127
128TEST_F(SurfaceTextureClientTest, GetISurfaceTextureIsNotNull) {
129    sp<IGraphicBufferProducer> ist(mSTC->getIGraphicBufferProducer());
130    ASSERT_TRUE(ist != NULL);
131}
132
133TEST_F(SurfaceTextureClientTest, QueuesToWindowCompositorIsFalse) {
134    int result = -123;
135    int err = mANW->query(mANW.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
136            &result);
137    EXPECT_EQ(NO_ERROR, err);
138    EXPECT_EQ(0, result);
139}
140
141TEST_F(SurfaceTextureClientTest, ConcreteTypeIsSurfaceTextureClient) {
142    int result = -123;
143    int err = mANW->query(mANW.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
144    EXPECT_EQ(NO_ERROR, err);
145    EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
146}
147
148TEST_F(SurfaceTextureClientTest, EglCreateWindowSurfaceSucceeds) {
149    EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
150    ASSERT_EQ(EGL_SUCCESS, eglGetError());
151    ASSERT_NE(EGL_NO_DISPLAY, dpy);
152
153    EGLint majorVersion;
154    EGLint minorVersion;
155    EXPECT_TRUE(eglInitialize(dpy, &majorVersion, &minorVersion));
156    ASSERT_EQ(EGL_SUCCESS, eglGetError());
157
158    EGLConfig myConfig = {0};
159    EGLint numConfigs = 0;
160    EGLint configAttribs[] = {
161        EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
162        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
163        EGL_RED_SIZE, 8,
164        EGL_GREEN_SIZE, 8,
165        EGL_BLUE_SIZE, 8,
166        EGL_ALPHA_SIZE, 8,
167        EGL_DEPTH_SIZE, 16,
168        EGL_STENCIL_SIZE, 8,
169        EGL_NONE };
170    EXPECT_TRUE(eglChooseConfig(dpy, configAttribs, &myConfig, 1,
171            &numConfigs));
172    ASSERT_EQ(EGL_SUCCESS, eglGetError());
173
174    EGLSurface eglSurface = eglCreateWindowSurface(dpy, myConfig, mANW.get(),
175            NULL);
176    EXPECT_NE(EGL_NO_SURFACE, eglSurface);
177    EXPECT_EQ(EGL_SUCCESS, eglGetError());
178
179    if (eglSurface != EGL_NO_SURFACE) {
180        eglDestroySurface(dpy, eglSurface);
181    }
182
183    eglTerminate(dpy);
184}
185
186TEST_F(SurfaceTextureClientTest, EglSwapBuffersAbandonErrorIsEglBadSurface) {
187
188    EGLSurface eglSurface = eglCreateWindowSurface(mEglDisplay, mEglConfig, mANW.get(), NULL);
189    EXPECT_NE(EGL_NO_SURFACE, eglSurface);
190    EXPECT_EQ(EGL_SUCCESS, eglGetError());
191
192    EGLBoolean success = eglMakeCurrent(mEglDisplay, eglSurface, eglSurface, mEglContext);
193    EXPECT_TRUE(success);
194
195    glClear(GL_COLOR_BUFFER_BIT);
196    success = eglSwapBuffers(mEglDisplay, eglSurface);
197    EXPECT_TRUE(success);
198
199    mST->abandon();
200
201    glClear(GL_COLOR_BUFFER_BIT);
202    success = eglSwapBuffers(mEglDisplay, eglSurface);
203    EXPECT_FALSE(success);
204    EXPECT_EQ(EGL_BAD_SURFACE, eglGetError());
205
206    success = eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext);
207    ASSERT_TRUE(success);
208
209    if (eglSurface != EGL_NO_SURFACE) {
210        eglDestroySurface(mEglDisplay, eglSurface);
211    }
212}
213
214TEST_F(SurfaceTextureClientTest, BufferGeometryInvalidSizesFail) {
215    EXPECT_GT(OK, native_window_set_buffers_dimensions(mANW.get(),  0,  8));
216    EXPECT_GT(OK, native_window_set_buffers_dimensions(mANW.get(),  8,  0));
217}
218
219TEST_F(SurfaceTextureClientTest, DefaultGeometryValues) {
220    ASSERT_EQ(OK, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU));
221    ANativeWindowBuffer* buf;
222    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf));
223    EXPECT_EQ(1, buf->width);
224    EXPECT_EQ(1, buf->height);
225    EXPECT_EQ(PIXEL_FORMAT_RGBA_8888, buf->format);
226    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf, -1));
227}
228
229TEST_F(SurfaceTextureClientTest, BufferGeometryCanBeSet) {
230    ASSERT_EQ(OK, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU));
231    ANativeWindowBuffer* buf;
232    EXPECT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 16, 8));
233    EXPECT_EQ(OK, native_window_set_buffers_format(mANW.get(), PIXEL_FORMAT_RGB_565));
234    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf));
235    EXPECT_EQ(16, buf->width);
236    EXPECT_EQ(8, buf->height);
237    EXPECT_EQ(PIXEL_FORMAT_RGB_565, buf->format);
238    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf, -1));
239}
240
241TEST_F(SurfaceTextureClientTest, BufferGeometryDefaultSizeSetFormat) {
242    ASSERT_EQ(OK, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU));
243    ANativeWindowBuffer* buf;
244    EXPECT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 0, 0));
245    EXPECT_EQ(OK, native_window_set_buffers_format(mANW.get(), PIXEL_FORMAT_RGB_565));
246    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf));
247    EXPECT_EQ(1, buf->width);
248    EXPECT_EQ(1, buf->height);
249    EXPECT_EQ(PIXEL_FORMAT_RGB_565, buf->format);
250    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf, -1));
251}
252
253TEST_F(SurfaceTextureClientTest, BufferGeometrySetSizeDefaultFormat) {
254    ASSERT_EQ(OK, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU));
255    ANativeWindowBuffer* buf;
256    EXPECT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 16, 8));
257    EXPECT_EQ(OK, native_window_set_buffers_format(mANW.get(), 0));
258    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf));
259    EXPECT_EQ(16, buf->width);
260    EXPECT_EQ(8, buf->height);
261    EXPECT_EQ(PIXEL_FORMAT_RGBA_8888, buf->format);
262    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf, -1));
263}
264
265TEST_F(SurfaceTextureClientTest, BufferGeometrySizeCanBeUnset) {
266    ASSERT_EQ(OK, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU));
267    ANativeWindowBuffer* buf;
268    EXPECT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 16, 8));
269    EXPECT_EQ(OK, native_window_set_buffers_format(mANW.get(), 0));
270    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf));
271    EXPECT_EQ(16, buf->width);
272    EXPECT_EQ(8, buf->height);
273    EXPECT_EQ(PIXEL_FORMAT_RGBA_8888, buf->format);
274    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf, -1));
275    EXPECT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 0, 0));
276    EXPECT_EQ(OK, native_window_set_buffers_format(mANW.get(), 0));
277    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf));
278    EXPECT_EQ(1, buf->width);
279    EXPECT_EQ(1, buf->height);
280    EXPECT_EQ(PIXEL_FORMAT_RGBA_8888, buf->format);
281    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf, -1));
282}
283
284TEST_F(SurfaceTextureClientTest, BufferGeometrySizeCanBeChangedWithoutFormat) {
285    ASSERT_EQ(OK, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU));
286    ANativeWindowBuffer* buf;
287    EXPECT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 0, 0));
288    EXPECT_EQ(OK, native_window_set_buffers_format(mANW.get(), PIXEL_FORMAT_RGB_565));
289    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf));
290    EXPECT_EQ(1, buf->width);
291    EXPECT_EQ(1, buf->height);
292    EXPECT_EQ(PIXEL_FORMAT_RGB_565, buf->format);
293    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf, -1));
294    EXPECT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 16, 8));
295    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf));
296    EXPECT_EQ(16, buf->width);
297    EXPECT_EQ(8, buf->height);
298    EXPECT_EQ(PIXEL_FORMAT_RGB_565, buf->format);
299    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf, -1));
300}
301
302TEST_F(SurfaceTextureClientTest, SurfaceTextureSetDefaultSize) {
303    ASSERT_EQ(OK, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU));
304    sp<GLConsumer> st(mST);
305    ANativeWindowBuffer* buf;
306    EXPECT_EQ(OK, st->setDefaultBufferSize(16, 8));
307    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf));
308    EXPECT_EQ(16, buf->width);
309    EXPECT_EQ(8, buf->height);
310    EXPECT_EQ(PIXEL_FORMAT_RGBA_8888, buf->format);
311    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf, -1));
312}
313
314TEST_F(SurfaceTextureClientTest, SurfaceTextureSetDefaultSizeAfterDequeue) {
315    ANativeWindowBuffer* buf[2];
316    ASSERT_EQ(OK, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU));
317    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 4));
318    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[0]));
319    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[1]));
320    EXPECT_NE(buf[0], buf[1]);
321    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf[0], -1));
322    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf[1], -1));
323    EXPECT_EQ(OK, mST->setDefaultBufferSize(16, 8));
324    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[0]));
325    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[1]));
326    EXPECT_NE(buf[0], buf[1]);
327    EXPECT_EQ(16, buf[0]->width);
328    EXPECT_EQ(16, buf[1]->width);
329    EXPECT_EQ(8, buf[0]->height);
330    EXPECT_EQ(8, buf[1]->height);
331    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf[0], -1));
332    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf[1], -1));
333}
334
335TEST_F(SurfaceTextureClientTest, SurfaceTextureSetDefaultSizeVsGeometry) {
336    ANativeWindowBuffer* buf[2];
337    ASSERT_EQ(OK, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU));
338    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 4));
339    EXPECT_EQ(OK, mST->setDefaultBufferSize(16, 8));
340    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[0]));
341    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[1]));
342    EXPECT_NE(buf[0], buf[1]);
343    EXPECT_EQ(16, buf[0]->width);
344    EXPECT_EQ(16, buf[1]->width);
345    EXPECT_EQ(8, buf[0]->height);
346    EXPECT_EQ(8, buf[1]->height);
347    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf[0], -1));
348    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf[1], -1));
349    EXPECT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 12, 24));
350    EXPECT_EQ(OK, native_window_set_buffers_format(mANW.get(), 0));
351    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[0]));
352    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[1]));
353    EXPECT_NE(buf[0], buf[1]);
354    EXPECT_EQ(12, buf[0]->width);
355    EXPECT_EQ(12, buf[1]->width);
356    EXPECT_EQ(24, buf[0]->height);
357    EXPECT_EQ(24, buf[1]->height);
358    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf[0], -1));
359    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf[1], -1));
360}
361
362TEST_F(SurfaceTextureClientTest, SurfaceTextureTooManyUpdateTexImage) {
363    android_native_buffer_t* buf[3];
364    ASSERT_EQ(OK, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU));
365    ASSERT_EQ(OK, mANW->setSwapInterval(mANW.get(), 0));
366    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 4));
367
368    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[0]));
369    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[0], -1));
370    EXPECT_EQ(OK, mST->updateTexImage());
371    EXPECT_EQ(OK, mST->updateTexImage());
372
373    ASSERT_EQ(OK, mANW->setSwapInterval(mANW.get(), 1));
374    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 3));
375
376    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[0]));
377    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[0], -1));
378    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[1]));
379    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[1], -1));
380
381    EXPECT_EQ(OK, mST->updateTexImage());
382    EXPECT_EQ(OK, mST->updateTexImage());
383    EXPECT_EQ(OK, mST->updateTexImage());
384}
385
386TEST_F(SurfaceTextureClientTest, SurfaceTextureSyncModeSlowRetire) {
387    android_native_buffer_t* buf[3];
388    ASSERT_EQ(OK, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU));
389    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 4));
390    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[0]));
391    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[1]));
392    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[2]));
393    EXPECT_NE(buf[0], buf[1]);
394    EXPECT_NE(buf[1], buf[2]);
395    EXPECT_NE(buf[2], buf[0]);
396    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[0], -1));
397    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[1], -1));
398    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[2], -1));
399    EXPECT_EQ(OK, mST->updateTexImage());
400    EXPECT_EQ(mST->getCurrentBuffer().get(), buf[0]);
401    EXPECT_EQ(OK, mST->updateTexImage());
402    EXPECT_EQ(mST->getCurrentBuffer().get(), buf[1]);
403    EXPECT_EQ(OK, mST->updateTexImage());
404    EXPECT_EQ(mST->getCurrentBuffer().get(), buf[2]);
405}
406
407TEST_F(SurfaceTextureClientTest, SurfaceTextureSyncModeFastRetire) {
408    android_native_buffer_t* buf[3];
409    ASSERT_EQ(OK, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU));
410    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 4));
411    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[0]));
412    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[1]));
413    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[2]));
414    EXPECT_NE(buf[0], buf[1]);
415    EXPECT_NE(buf[1], buf[2]);
416    EXPECT_NE(buf[2], buf[0]);
417    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[0], -1));
418    EXPECT_EQ(OK, mST->updateTexImage());
419    EXPECT_EQ(mST->getCurrentBuffer().get(), buf[0]);
420    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[1], -1));
421    EXPECT_EQ(OK, mST->updateTexImage());
422    EXPECT_EQ(mST->getCurrentBuffer().get(), buf[1]);
423    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[2], -1));
424    EXPECT_EQ(OK, mST->updateTexImage());
425    EXPECT_EQ(mST->getCurrentBuffer().get(), buf[2]);
426}
427
428TEST_F(SurfaceTextureClientTest, SurfaceTextureSyncModeDQQR) {
429    android_native_buffer_t* buf[3];
430    ASSERT_EQ(OK, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU));
431    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 3));
432    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[0]));
433    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[0], -1));
434    EXPECT_EQ(OK, mST->updateTexImage());
435    EXPECT_EQ(mST->getCurrentBuffer().get(), buf[0]);
436
437    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[1]));
438    EXPECT_NE(buf[0], buf[1]);
439    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[1], -1));
440    EXPECT_EQ(OK, mST->updateTexImage());
441    EXPECT_EQ(mST->getCurrentBuffer().get(), buf[1]);
442
443    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[2]));
444    EXPECT_NE(buf[1], buf[2]);
445    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[2], -1));
446    EXPECT_EQ(OK, mST->updateTexImage());
447    EXPECT_EQ(mST->getCurrentBuffer().get(), buf[2]);
448}
449
450// XXX: We currently have no hardware that properly handles dequeuing the
451// buffer that is currently bound to the texture.
452TEST_F(SurfaceTextureClientTest, DISABLED_SurfaceTextureSyncModeDequeueCurrent) {
453    android_native_buffer_t* buf[3];
454    android_native_buffer_t* firstBuf;
455    ASSERT_EQ(OK, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU));
456    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 3));
457    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &firstBuf));
458    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), firstBuf, -1));
459    EXPECT_EQ(OK, mST->updateTexImage());
460    EXPECT_EQ(mST->getCurrentBuffer().get(), firstBuf);
461    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[0]));
462    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[0], -1));
463    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[1]));
464    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[1], -1));
465    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[2]));
466    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[2], -1));
467    EXPECT_NE(buf[0], buf[1]);
468    EXPECT_NE(buf[1], buf[2]);
469    EXPECT_NE(buf[2], buf[0]);
470    EXPECT_EQ(firstBuf, buf[2]);
471}
472
473TEST_F(SurfaceTextureClientTest, SurfaceTextureSyncModeMinUndequeued) {
474    android_native_buffer_t* buf[3];
475    ASSERT_EQ(OK, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU));
476    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 3));
477
478    // We should be able to dequeue all the buffers before we've queued mANWy.
479    EXPECT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[0]));
480    EXPECT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[1]));
481    EXPECT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[2]));
482
483    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf[2], -1));
484    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[1], -1));
485
486    EXPECT_EQ(OK, mST->updateTexImage());
487    EXPECT_EQ(mST->getCurrentBuffer().get(), buf[1]);
488
489    EXPECT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[2]));
490
491    // Once we've queued a buffer, however we should not be able to dequeue more
492    // than (buffer-count - MIN_UNDEQUEUED_BUFFERS), which is 2 in this case.
493    EXPECT_EQ(INVALID_OPERATION,
494            native_window_dequeue_buffer_and_wait(mANW.get(), &buf[1]));
495
496    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf[0], -1));
497    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf[2], -1));
498}
499
500TEST_F(SurfaceTextureClientTest, SetCropCropsCrop) {
501    ASSERT_EQ(OK, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU));
502    android_native_rect_t rect = {-2, -13, 40, 18};
503    native_window_set_crop(mANW.get(), &rect);
504
505    ASSERT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 4, 4));
506
507    android_native_buffer_t* buf;
508    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf));
509    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf, -1));
510    ASSERT_EQ(OK, mST->updateTexImage());
511
512    Rect crop = mST->getCurrentCrop();
513    EXPECT_EQ(0, crop.left);
514    EXPECT_EQ(0, crop.top);
515    EXPECT_EQ(4, crop.right);
516    EXPECT_EQ(4, crop.bottom);
517}
518
519// XXX: This is not expected to pass until the synchronization hacks are removed
520// from the SurfaceTexture class.
521TEST_F(SurfaceTextureClientTest, DISABLED_SurfaceTextureSyncModeWaitRetire) {
522    class MyThread : public Thread {
523        sp<GLConsumer> mST;
524        EGLContext ctx;
525        EGLSurface sur;
526        EGLDisplay dpy;
527        bool mBufferRetired;
528        Mutex mLock;
529        virtual bool threadLoop() {
530            eglMakeCurrent(dpy, sur, sur, ctx);
531            usleep(20000);
532            Mutex::Autolock _l(mLock);
533            mST->updateTexImage();
534            mBufferRetired = true;
535            eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
536            return false;
537        }
538    public:
539        explicit MyThread(const sp<GLConsumer>& mST)
540            : mST(mST), mBufferRetired(false) {
541            ctx = eglGetCurrentContext();
542            sur = eglGetCurrentSurface(EGL_DRAW);
543            dpy = eglGetCurrentDisplay();
544            eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
545        }
546        ~MyThread() {
547            eglMakeCurrent(dpy, sur, sur, ctx);
548        }
549        void bufferDequeued() {
550            Mutex::Autolock _l(mLock);
551            EXPECT_EQ(true, mBufferRetired);
552        }
553    };
554
555    android_native_buffer_t* buf[3];
556    ASSERT_EQ(OK, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU));
557    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 3));
558    // dequeue/queue/update so we have a current buffer
559    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[0]));
560    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[0], -1));
561    mST->updateTexImage();
562
563    MyThread* thread = new MyThread(mST);
564    sp<Thread> threadBase(thread);
565
566    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[0]));
567    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[0], -1));
568    thread->run("MyThread");
569    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[1]));
570    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[1], -1));
571    //ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[2]));
572    //ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[2], -1));
573    thread->bufferDequeued();
574    thread->requestExitAndWait();
575}
576
577TEST_F(SurfaceTextureClientTest, GetTransformMatrixReturnsVerticalFlip) {
578    android_native_buffer_t* buf[3];
579    float mtx[16] = {};
580    ASSERT_EQ(OK, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU));
581    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 4));
582    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[0]));
583    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[0], -1));
584    ASSERT_EQ(OK, mST->updateTexImage());
585    mST->getTransformMatrix(mtx);
586
587    EXPECT_EQ(1.f, mtx[0]);
588    EXPECT_EQ(0.f, mtx[1]);
589    EXPECT_EQ(0.f, mtx[2]);
590    EXPECT_EQ(0.f, mtx[3]);
591
592    EXPECT_EQ(0.f, mtx[4]);
593    EXPECT_EQ(-1.f, mtx[5]);
594    EXPECT_EQ(0.f, mtx[6]);
595    EXPECT_EQ(0.f, mtx[7]);
596
597    EXPECT_EQ(0.f, mtx[8]);
598    EXPECT_EQ(0.f, mtx[9]);
599    EXPECT_EQ(1.f, mtx[10]);
600    EXPECT_EQ(0.f, mtx[11]);
601
602    EXPECT_EQ(0.f, mtx[12]);
603    EXPECT_EQ(1.f, mtx[13]);
604    EXPECT_EQ(0.f, mtx[14]);
605    EXPECT_EQ(1.f, mtx[15]);
606}
607
608TEST_F(SurfaceTextureClientTest, GetTransformMatrixSucceedsAfterFreeingBuffers) {
609    android_native_buffer_t* buf[3];
610    float mtx[16] = {};
611    ASSERT_EQ(OK, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU));
612    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 4));
613    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[0]));
614    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[0], -1));
615    ASSERT_EQ(OK, mST->updateTexImage());
616    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 6)); // frees buffers
617    mST->getTransformMatrix(mtx);
618
619    EXPECT_EQ(1.f, mtx[0]);
620    EXPECT_EQ(0.f, mtx[1]);
621    EXPECT_EQ(0.f, mtx[2]);
622    EXPECT_EQ(0.f, mtx[3]);
623
624    EXPECT_EQ(0.f, mtx[4]);
625    EXPECT_EQ(-1.f, mtx[5]);
626    EXPECT_EQ(0.f, mtx[6]);
627    EXPECT_EQ(0.f, mtx[7]);
628
629    EXPECT_EQ(0.f, mtx[8]);
630    EXPECT_EQ(0.f, mtx[9]);
631    EXPECT_EQ(1.f, mtx[10]);
632    EXPECT_EQ(0.f, mtx[11]);
633
634    EXPECT_EQ(0.f, mtx[12]);
635    EXPECT_EQ(1.f, mtx[13]);
636    EXPECT_EQ(0.f, mtx[14]);
637    EXPECT_EQ(1.f, mtx[15]);
638}
639
640TEST_F(SurfaceTextureClientTest, GetTransformMatrixSucceedsAfterFreeingBuffersWithCrop) {
641    // Query to see if the image crop extension exists
642    EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
643    const char* exts = eglQueryStringImplementationANDROID(dpy, EGL_EXTENSIONS);
644    size_t cropExtLen = strlen(CROP_EXT_STR);
645    size_t extsLen = strlen(exts);
646    bool equal = !strcmp(CROP_EXT_STR, exts);
647    bool atStart = !strncmp(CROP_EXT_STR " ", exts, cropExtLen+1);
648    bool atEnd = (cropExtLen+1) < extsLen &&
649            !strcmp(" " CROP_EXT_STR, exts + extsLen - (cropExtLen+1));
650    bool inMiddle = strstr(exts, " " CROP_EXT_STR " ");
651    bool hasEglAndroidImageCrop = equal || atStart || atEnd || inMiddle;
652
653    android_native_buffer_t* buf[3];
654    float mtx[16] = {};
655    android_native_rect_t crop;
656    crop.left = 0;
657    crop.top = 0;
658    crop.right = 5;
659    crop.bottom = 5;
660
661    ASSERT_EQ(OK, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU));
662    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 4));
663    ASSERT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 8, 8));
664    ASSERT_EQ(OK, native_window_set_buffers_format(mANW.get(), 0));
665    ASSERT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &buf[0]));
666    ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &crop));
667    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[0], -1));
668    ASSERT_EQ(OK, mST->updateTexImage());
669    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 6)); // frees buffers
670    mST->getTransformMatrix(mtx);
671
672    // If the egl image crop extension is not present, this accounts for the
673    // .5 texel shrink for each edge that's included in the transform matrix
674    // to avoid texturing outside the crop region. Otherwise the crop is not
675    // included in the transform matrix.
676    EXPECT_EQ(hasEglAndroidImageCrop ? 1 : 0.5, mtx[0]);
677    EXPECT_EQ(0.f, mtx[1]);
678    EXPECT_EQ(0.f, mtx[2]);
679    EXPECT_EQ(0.f, mtx[3]);
680
681    EXPECT_EQ(0.f, mtx[4]);
682    EXPECT_EQ(hasEglAndroidImageCrop ? -1 : -0.5, mtx[5]);
683    EXPECT_EQ(0.f, mtx[6]);
684    EXPECT_EQ(0.f, mtx[7]);
685
686    EXPECT_EQ(0.f, mtx[8]);
687    EXPECT_EQ(0.f, mtx[9]);
688    EXPECT_EQ(1.f, mtx[10]);
689    EXPECT_EQ(0.f, mtx[11]);
690
691    EXPECT_EQ(hasEglAndroidImageCrop ? 0 : 0.0625f, mtx[12]);
692    EXPECT_EQ(hasEglAndroidImageCrop ? 1 : 0.5625f, mtx[13]);
693    EXPECT_EQ(0.f, mtx[14]);
694    EXPECT_EQ(1.f, mtx[15]);
695}
696
697// This test verifies that the buffer format can be queried immediately after
698// it is set.
699TEST_F(SurfaceTextureClientTest, QueryFormatAfterSettingWorks) {
700    sp<ANativeWindow> anw(mSTC);
701    int fmts[] = {
702        // RGBA_8888 should not come first, as it's the default
703        HAL_PIXEL_FORMAT_RGBX_8888,
704        HAL_PIXEL_FORMAT_RGBA_8888,
705        HAL_PIXEL_FORMAT_RGB_888,
706        HAL_PIXEL_FORMAT_RGB_565,
707        HAL_PIXEL_FORMAT_BGRA_8888,
708        HAL_PIXEL_FORMAT_YV12,
709    };
710
711    const int numFmts = (sizeof(fmts) / sizeof(fmts[0]));
712    for (int i = 0; i < numFmts; i++) {
713      int fmt = -1;
714      ASSERT_EQ(OK, native_window_set_buffers_dimensions(anw.get(), 0, 0));
715      ASSERT_EQ(OK, native_window_set_buffers_format(anw.get(), fmts[i]));
716      ASSERT_EQ(OK, anw->query(anw.get(), NATIVE_WINDOW_FORMAT, &fmt));
717      EXPECT_EQ(fmts[i], fmt);
718    }
719}
720
721class MultiSurfaceTextureClientTest : public ::testing::Test {
722
723public:
724    MultiSurfaceTextureClientTest() :
725            mEglDisplay(EGL_NO_DISPLAY),
726            mEglContext(EGL_NO_CONTEXT) {
727        for (int i = 0; i < NUM_SURFACE_TEXTURES; i++) {
728            mEglSurfaces[i] = EGL_NO_CONTEXT;
729        }
730    }
731
732protected:
733
734    enum { NUM_SURFACE_TEXTURES = 32 };
735
736    virtual void SetUp() {
737        mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
738        ASSERT_EQ(EGL_SUCCESS, eglGetError());
739        ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay);
740
741        EGLint majorVersion, minorVersion;
742        EXPECT_TRUE(eglInitialize(mEglDisplay, &majorVersion, &minorVersion));
743        ASSERT_EQ(EGL_SUCCESS, eglGetError());
744
745        EGLConfig myConfig;
746        EGLint numConfigs = 0;
747        EGLint configAttribs[] = {
748            EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
749            EGL_NONE
750        };
751        EXPECT_TRUE(eglChooseConfig(mEglDisplay, configAttribs, &myConfig, 1,
752                &numConfigs));
753        ASSERT_EQ(EGL_SUCCESS, eglGetError());
754
755        mEglContext = eglCreateContext(mEglDisplay, myConfig, EGL_NO_CONTEXT,
756                0);
757        ASSERT_EQ(EGL_SUCCESS, eglGetError());
758        ASSERT_NE(EGL_NO_CONTEXT, mEglContext);
759
760        for (int i = 0; i < NUM_SURFACE_TEXTURES; i++) {
761            sp<IGraphicBufferProducer> producer;
762            sp<IGraphicBufferConsumer> consumer;
763            BufferQueue::createBufferQueue(&producer, &consumer);
764            sp<GLConsumer> st(new GLConsumer(consumer, i,
765                    GLConsumer::TEXTURE_EXTERNAL, true, false));
766            sp<Surface> stc(new Surface(producer));
767            mEglSurfaces[i] = eglCreateWindowSurface(mEglDisplay, myConfig,
768                    static_cast<ANativeWindow*>(stc.get()), NULL);
769            ASSERT_EQ(EGL_SUCCESS, eglGetError());
770            ASSERT_NE(EGL_NO_SURFACE, mEglSurfaces[i]);
771        }
772    }
773
774    virtual void TearDown() {
775        eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
776                EGL_NO_CONTEXT);
777
778        for (int i = 0; i < NUM_SURFACE_TEXTURES; i++) {
779            if (mEglSurfaces[i] != EGL_NO_SURFACE) {
780                eglDestroySurface(mEglDisplay, mEglSurfaces[i]);
781            }
782        }
783
784        if (mEglContext != EGL_NO_CONTEXT) {
785            eglDestroyContext(mEglDisplay, mEglContext);
786        }
787
788        if (mEglDisplay != EGL_NO_DISPLAY) {
789            eglTerminate(mEglDisplay);
790        }
791    }
792
793    EGLDisplay mEglDisplay;
794    EGLSurface mEglSurfaces[NUM_SURFACE_TEXTURES];
795    EGLContext mEglContext;
796};
797
798// XXX: This test is disabled because it causes a hang on some devices.  See bug
799// 5015672.
800TEST_F(MultiSurfaceTextureClientTest, DISABLED_MakeCurrentBetweenSurfacesWorks) {
801    for (int iter = 0; iter < 8; iter++) {
802        for (int i = 0; i < NUM_SURFACE_TEXTURES; i++) {
803            eglMakeCurrent(mEglDisplay, mEglSurfaces[i], mEglSurfaces[i],
804                    mEglContext);
805            glClear(GL_COLOR_BUFFER_BIT);
806            eglSwapBuffers(mEglDisplay, mEglSurfaces[i]);
807        }
808    }
809}
810
811} // namespace android
812