SurfaceTextureClient_test.cpp revision 8b69defa2fe2cf6a34b00e322a557d8a4dddea41
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#include <EGL/egl.h>
18#include <gtest/gtest.h>
19#include <gui/SurfaceTextureClient.h>
20#include <utils/threads.h>
21
22namespace android {
23
24class SurfaceTextureClientTest : public ::testing::Test {
25protected:
26    SurfaceTextureClientTest():
27            mEglDisplay(EGL_NO_DISPLAY),
28            mEglSurface(EGL_NO_SURFACE),
29            mEglContext(EGL_NO_CONTEXT) {
30    }
31
32    virtual void SetUp() {
33        mST = new SurfaceTexture(123);
34        mSTC = new SurfaceTextureClient(mST);
35        mANW = mSTC;
36
37        // We need a valid GL context so we can test updateTexImage()
38        // This initializes EGL and create a dummy GL context with a
39        // pbuffer render target.
40        mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
41        ASSERT_EQ(EGL_SUCCESS, eglGetError());
42        ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay);
43
44        EGLint majorVersion, minorVersion;
45        EXPECT_TRUE(eglInitialize(mEglDisplay, &majorVersion, &minorVersion));
46        ASSERT_EQ(EGL_SUCCESS, eglGetError());
47
48        EGLConfig myConfig;
49        EGLint numConfigs = 0;
50        EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(),
51                &myConfig, 1, &numConfigs));
52        ASSERT_EQ(EGL_SUCCESS, eglGetError());
53
54        EGLint pbufferAttribs[] = {
55            EGL_WIDTH, 16,
56            EGL_HEIGHT, 16,
57            EGL_NONE };
58        mEglSurface = eglCreatePbufferSurface(mEglDisplay, myConfig, pbufferAttribs);
59        ASSERT_EQ(EGL_SUCCESS, eglGetError());
60        ASSERT_NE(EGL_NO_SURFACE, mEglSurface);
61
62        mEglContext = eglCreateContext(mEglDisplay, myConfig, EGL_NO_CONTEXT, 0);
63        ASSERT_EQ(EGL_SUCCESS, eglGetError());
64        ASSERT_NE(EGL_NO_CONTEXT, mEglContext);
65
66        EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext));
67        ASSERT_EQ(EGL_SUCCESS, eglGetError());
68    }
69
70    virtual void TearDown() {
71        mST.clear();
72        mSTC.clear();
73        mANW.clear();
74
75        eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
76        eglDestroyContext(mEglDisplay, mEglContext);
77        eglDestroySurface(mEglDisplay, mEglSurface);
78        eglTerminate(mEglDisplay);
79    }
80
81    virtual EGLint const* getConfigAttribs() {
82        static EGLint sDefaultConfigAttribs[] = {
83            EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
84            EGL_NONE
85        };
86
87        return sDefaultConfigAttribs;
88    }
89
90    sp<SurfaceTexture> mST;
91    sp<SurfaceTextureClient> mSTC;
92    sp<ANativeWindow> mANW;
93
94    EGLDisplay mEglDisplay;
95    EGLSurface mEglSurface;
96    EGLContext mEglContext;
97};
98
99TEST_F(SurfaceTextureClientTest, GetISurfaceTextureIsNotNull) {
100    sp<ISurfaceTexture> ist(mSTC->getISurfaceTexture());
101    ASSERT_TRUE(ist != NULL);
102}
103
104TEST_F(SurfaceTextureClientTest, QueuesToWindowCompositorIsFalse) {
105    int result = -123;
106    int err = mANW->query(mANW.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
107            &result);
108    EXPECT_EQ(NO_ERROR, err);
109    EXPECT_EQ(0, result);
110}
111
112TEST_F(SurfaceTextureClientTest, ConcreteTypeIsSurfaceTextureClient) {
113    int result = -123;
114    int err = mANW->query(mANW.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
115    EXPECT_EQ(NO_ERROR, err);
116    EXPECT_EQ(NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT, result);
117}
118
119TEST_F(SurfaceTextureClientTest, ANativeWindowLockFails) {
120    ANativeWindow_Buffer buf;
121    ASSERT_EQ(BAD_VALUE, ANativeWindow_lock(mANW.get(), &buf, NULL));
122}
123
124TEST_F(SurfaceTextureClientTest, EglCreateWindowSurfaceSucceeds) {
125    EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
126    ASSERT_EQ(EGL_SUCCESS, eglGetError());
127    ASSERT_NE(EGL_NO_DISPLAY, dpy);
128
129    EGLint majorVersion;
130    EGLint minorVersion;
131    EXPECT_TRUE(eglInitialize(dpy, &majorVersion, &minorVersion));
132    ASSERT_EQ(EGL_SUCCESS, eglGetError());
133
134    EGLConfig myConfig = {0};
135    EGLint numConfigs = 0;
136    EGLint configAttribs[] = {
137        EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
138        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
139        EGL_RED_SIZE, 8,
140        EGL_GREEN_SIZE, 8,
141        EGL_BLUE_SIZE, 8,
142        EGL_ALPHA_SIZE, 8,
143        EGL_DEPTH_SIZE, 16,
144        EGL_STENCIL_SIZE, 8,
145        EGL_NONE };
146    EXPECT_TRUE(eglChooseConfig(dpy, configAttribs, &myConfig, 1,
147            &numConfigs));
148    ASSERT_EQ(EGL_SUCCESS, eglGetError());
149
150    EGLSurface eglSurface = eglCreateWindowSurface(dpy, myConfig, mANW.get(),
151            NULL);
152    EXPECT_NE(EGL_NO_SURFACE, eglSurface);
153    EXPECT_EQ(EGL_SUCCESS, eglGetError());
154
155    eglTerminate(dpy);
156}
157
158TEST_F(SurfaceTextureClientTest, BufferGeometryInvalidSizesFail) {
159    EXPECT_GT(OK, native_window_set_buffers_geometry(mANW.get(), -1,  0,  0));
160    EXPECT_GT(OK, native_window_set_buffers_geometry(mANW.get(),  0, -1,  0));
161    EXPECT_GT(OK, native_window_set_buffers_geometry(mANW.get(),  0,  0, -1));
162    EXPECT_GT(OK, native_window_set_buffers_geometry(mANW.get(), -1, -1,  0));
163    EXPECT_GT(OK, native_window_set_buffers_geometry(mANW.get(),  0,  8,  0));
164    EXPECT_GT(OK, native_window_set_buffers_geometry(mANW.get(),  8,  0,  0));
165}
166
167TEST_F(SurfaceTextureClientTest, DefaultGeometryValues) {
168    ANativeWindowBuffer* buf;
169    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf));
170    EXPECT_EQ(1, buf->width);
171    EXPECT_EQ(1, buf->height);
172    EXPECT_EQ(PIXEL_FORMAT_RGBA_8888, buf->format);
173    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf));
174}
175
176TEST_F(SurfaceTextureClientTest, BufferGeometryCanBeSet) {
177    ANativeWindowBuffer* buf;
178    EXPECT_EQ(OK, native_window_set_buffers_geometry(mANW.get(), 16, 8, PIXEL_FORMAT_RGB_565));
179    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf));
180    EXPECT_EQ(16, buf->width);
181    EXPECT_EQ(8, buf->height);
182    EXPECT_EQ(PIXEL_FORMAT_RGB_565, buf->format);
183    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf));
184}
185
186TEST_F(SurfaceTextureClientTest, BufferGeometryDefaultSizeSetFormat) {
187    ANativeWindowBuffer* buf;
188    EXPECT_EQ(OK, native_window_set_buffers_geometry(mANW.get(), 0, 0, PIXEL_FORMAT_RGB_565));
189    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf));
190    EXPECT_EQ(1, buf->width);
191    EXPECT_EQ(1, buf->height);
192    EXPECT_EQ(PIXEL_FORMAT_RGB_565, buf->format);
193    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf));
194}
195
196TEST_F(SurfaceTextureClientTest, BufferGeometrySetSizeDefaultFormat) {
197    ANativeWindowBuffer* buf;
198    EXPECT_EQ(OK, native_window_set_buffers_geometry(mANW.get(), 16, 8, 0));
199    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf));
200    EXPECT_EQ(16, buf->width);
201    EXPECT_EQ(8, buf->height);
202    EXPECT_EQ(PIXEL_FORMAT_RGBA_8888, buf->format);
203    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf));
204}
205
206TEST_F(SurfaceTextureClientTest, BufferGeometrySizeCanBeUnset) {
207    ANativeWindowBuffer* buf;
208    EXPECT_EQ(OK, native_window_set_buffers_geometry(mANW.get(), 16, 8, 0));
209    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf));
210    EXPECT_EQ(16, buf->width);
211    EXPECT_EQ(8, buf->height);
212    EXPECT_EQ(PIXEL_FORMAT_RGBA_8888, buf->format);
213    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf));
214    EXPECT_EQ(OK, native_window_set_buffers_geometry(mANW.get(), 0, 0, 0));
215    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf));
216    EXPECT_EQ(1, buf->width);
217    EXPECT_EQ(1, buf->height);
218    EXPECT_EQ(PIXEL_FORMAT_RGBA_8888, buf->format);
219    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf));
220}
221
222TEST_F(SurfaceTextureClientTest, BufferGeometrySizeCanBeChangedWithoutFormat) {
223    ANativeWindowBuffer* buf;
224    EXPECT_EQ(OK, native_window_set_buffers_geometry(mANW.get(), 0, 0, PIXEL_FORMAT_RGB_565));
225    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf));
226    EXPECT_EQ(1, buf->width);
227    EXPECT_EQ(1, buf->height);
228    EXPECT_EQ(PIXEL_FORMAT_RGB_565, buf->format);
229    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf));
230    EXPECT_EQ(OK, native_window_set_buffers_geometry(mANW.get(), 16, 8, 0));
231    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf));
232    EXPECT_EQ(16, buf->width);
233    EXPECT_EQ(8, buf->height);
234    EXPECT_EQ(PIXEL_FORMAT_RGB_565, buf->format);
235    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf));
236}
237
238TEST_F(SurfaceTextureClientTest, SurfaceTextureSetDefaultSize) {
239    sp<SurfaceTexture> st(mST);
240    ANativeWindowBuffer* buf;
241    EXPECT_EQ(OK, st->setDefaultBufferSize(16, 8));
242    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf));
243    EXPECT_EQ(16, buf->width);
244    EXPECT_EQ(8, buf->height);
245    EXPECT_EQ(PIXEL_FORMAT_RGBA_8888, buf->format);
246    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf));
247}
248
249TEST_F(SurfaceTextureClientTest, SurfaceTextureSetDefaultSizeAfterDequeue) {
250    ANativeWindowBuffer* buf[2];
251    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 4));
252    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[0]));
253    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[1]));
254    EXPECT_NE(buf[0], buf[1]);
255    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf[0]));
256    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf[1]));
257    EXPECT_EQ(OK, mST->setDefaultBufferSize(16, 8));
258    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[0]));
259    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[1]));
260    EXPECT_NE(buf[0], buf[1]);
261    EXPECT_EQ(16, buf[0]->width);
262    EXPECT_EQ(16, buf[1]->width);
263    EXPECT_EQ(8, buf[0]->height);
264    EXPECT_EQ(8, buf[1]->height);
265    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf[0]));
266    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf[1]));
267}
268
269TEST_F(SurfaceTextureClientTest, SurfaceTextureSetDefaultSizeVsGeometry) {
270    ANativeWindowBuffer* buf[2];
271    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 4));
272    EXPECT_EQ(OK, mST->setDefaultBufferSize(16, 8));
273    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[0]));
274    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[1]));
275    EXPECT_NE(buf[0], buf[1]);
276    EXPECT_EQ(16, buf[0]->width);
277    EXPECT_EQ(16, buf[1]->width);
278    EXPECT_EQ(8, buf[0]->height);
279    EXPECT_EQ(8, buf[1]->height);
280    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf[0]));
281    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf[1]));
282    EXPECT_EQ(OK, native_window_set_buffers_geometry(mANW.get(), 12, 24, 0));
283    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[0]));
284    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[1]));
285    EXPECT_NE(buf[0], buf[1]);
286    EXPECT_EQ(12, buf[0]->width);
287    EXPECT_EQ(12, buf[1]->width);
288    EXPECT_EQ(24, buf[0]->height);
289    EXPECT_EQ(24, buf[1]->height);
290    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf[0]));
291    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf[1]));
292}
293
294TEST_F(SurfaceTextureClientTest, SurfaceTextureTooManyUpdateTexImage) {
295    android_native_buffer_t* buf[3];
296    ASSERT_EQ(OK, mST->setSynchronousMode(false));
297    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 4));
298
299    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[0]));
300    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[0]));
301    EXPECT_EQ(OK, mST->updateTexImage());
302    EXPECT_EQ(OK, mST->updateTexImage());
303
304    ASSERT_EQ(OK, mST->setSynchronousMode(true));
305    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 3));
306
307    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[0]));
308    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[0]));
309    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[1]));
310    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[1]));
311
312    EXPECT_EQ(OK, mST->updateTexImage());
313    EXPECT_EQ(OK, mST->updateTexImage());
314    EXPECT_EQ(OK, mST->updateTexImage());
315}
316
317TEST_F(SurfaceTextureClientTest, SurfaceTextureSyncModeSlowRetire) {
318    android_native_buffer_t* buf[3];
319    ASSERT_EQ(OK, mST->setSynchronousMode(true));
320    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 4));
321    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[0]));
322    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[1]));
323    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[2]));
324    EXPECT_NE(buf[0], buf[1]);
325    EXPECT_NE(buf[1], buf[2]);
326    EXPECT_NE(buf[2], buf[0]);
327    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[0]));
328    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[1]));
329    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[2]));
330    EXPECT_EQ(OK, mST->updateTexImage());
331    EXPECT_EQ(mST->getCurrentBuffer().get(), buf[0]);
332    EXPECT_EQ(OK, mST->updateTexImage());
333    EXPECT_EQ(mST->getCurrentBuffer().get(), buf[1]);
334    EXPECT_EQ(OK, mST->updateTexImage());
335    EXPECT_EQ(mST->getCurrentBuffer().get(), buf[2]);
336}
337
338TEST_F(SurfaceTextureClientTest, SurfaceTextureSyncModeFastRetire) {
339    android_native_buffer_t* buf[3];
340    ASSERT_EQ(OK, mST->setSynchronousMode(true));
341    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 4));
342    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[0]));
343    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[1]));
344    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[2]));
345    EXPECT_NE(buf[0], buf[1]);
346    EXPECT_NE(buf[1], buf[2]);
347    EXPECT_NE(buf[2], buf[0]);
348    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[0]));
349    EXPECT_EQ(OK, mST->updateTexImage());
350    EXPECT_EQ(mST->getCurrentBuffer().get(), buf[0]);
351    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[1]));
352    EXPECT_EQ(OK, mST->updateTexImage());
353    EXPECT_EQ(mST->getCurrentBuffer().get(), buf[1]);
354    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[2]));
355    EXPECT_EQ(OK, mST->updateTexImage());
356    EXPECT_EQ(mST->getCurrentBuffer().get(), buf[2]);
357}
358
359TEST_F(SurfaceTextureClientTest, SurfaceTextureSyncModeDQQR) {
360    android_native_buffer_t* buf[3];
361    ASSERT_EQ(OK, mST->setSynchronousMode(true));
362    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 3));
363
364    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[0]));
365    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[0]));
366    EXPECT_EQ(OK, mST->updateTexImage());
367    EXPECT_EQ(mST->getCurrentBuffer().get(), buf[0]);
368
369    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[1]));
370    EXPECT_NE(buf[0], buf[1]);
371    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[1]));
372    EXPECT_EQ(OK, mST->updateTexImage());
373    EXPECT_EQ(mST->getCurrentBuffer().get(), buf[1]);
374
375    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[2]));
376    EXPECT_NE(buf[1], buf[2]);
377    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[2]));
378    EXPECT_EQ(OK, mST->updateTexImage());
379    EXPECT_EQ(mST->getCurrentBuffer().get(), buf[2]);
380}
381
382// XXX: We currently have no hardware that properly handles dequeuing the
383// buffer that is currently bound to the texture.
384TEST_F(SurfaceTextureClientTest, DISABLED_SurfaceTextureSyncModeDequeueCurrent) {
385    android_native_buffer_t* buf[3];
386    android_native_buffer_t* firstBuf;
387    ASSERT_EQ(OK, mST->setSynchronousMode(true));
388    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 3));
389    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &firstBuf));
390    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), firstBuf));
391    EXPECT_EQ(OK, mST->updateTexImage());
392    EXPECT_EQ(mST->getCurrentBuffer().get(), firstBuf);
393    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[0]));
394    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[0]));
395    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[1]));
396    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[1]));
397    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[2]));
398    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[2]));
399    EXPECT_NE(buf[0], buf[1]);
400    EXPECT_NE(buf[1], buf[2]);
401    EXPECT_NE(buf[2], buf[0]);
402    EXPECT_EQ(firstBuf, buf[2]);
403}
404
405TEST_F(SurfaceTextureClientTest, SurfaceTextureSyncModeMinUndequeued) {
406    android_native_buffer_t* buf[3];
407    ASSERT_EQ(OK, mST->setSynchronousMode(true));
408    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 3));
409
410    // We should be able to dequeue all the buffers before we've queued mANWy.
411    EXPECT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[0]));
412    EXPECT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[1]));
413    EXPECT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[2]));
414
415    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf[2]));
416    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[1]));
417
418    EXPECT_EQ(OK, mST->updateTexImage());
419    EXPECT_EQ(mST->getCurrentBuffer().get(), buf[1]);
420
421    EXPECT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[2]));
422
423    // Once we've queued a buffer, however we should not be able to dequeue more
424    // than (buffer-count - MIN_UNDEQUEUED_BUFFERS), which is 2 in this case.
425    EXPECT_EQ(-EBUSY, mANW->dequeueBuffer(mANW.get(), &buf[1]));
426
427    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf[0]));
428    ASSERT_EQ(OK, mANW->cancelBuffer(mANW.get(), buf[2]));
429}
430
431// XXX: This is not expected to pass until the synchronization hacks are removed
432// from the SurfaceTexture class.
433TEST_F(SurfaceTextureClientTest, DISABLED_SurfaceTextureSyncModeWaitRetire) {
434    class MyThread : public Thread {
435        sp<SurfaceTexture> mST;
436        EGLContext ctx;
437        EGLSurface sur;
438        EGLDisplay dpy;
439        bool mBufferRetired;
440        Mutex mLock;
441        virtual bool threadLoop() {
442            eglMakeCurrent(dpy, sur, sur, ctx);
443            usleep(20000);
444            Mutex::Autolock _l(mLock);
445            mST->updateTexImage();
446            mBufferRetired = true;
447            eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
448            return false;
449        }
450    public:
451        MyThread(const sp<SurfaceTexture>& mST)
452            : mST(mST), mBufferRetired(false) {
453            ctx = eglGetCurrentContext();
454            sur = eglGetCurrentSurface(EGL_DRAW);
455            dpy = eglGetCurrentDisplay();
456            eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
457        }
458        ~MyThread() {
459            eglMakeCurrent(dpy, sur, sur, ctx);
460        }
461        void bufferDequeued() {
462            Mutex::Autolock _l(mLock);
463            EXPECT_EQ(true, mBufferRetired);
464        }
465    };
466
467    android_native_buffer_t* buf[3];
468    ASSERT_EQ(OK, mST->setSynchronousMode(true));
469    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 3));
470    // dequeue/queue/update so we have a current buffer
471    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[0]));
472    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[0]));
473    mST->updateTexImage();
474
475    MyThread* thread = new MyThread(mST);
476    sp<Thread> threadBase(thread);
477
478    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[0]));
479    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[0]));
480    thread->run();
481    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[1]));
482    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[1]));
483    //ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[2]));
484    //ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[2]));
485    thread->bufferDequeued();
486    thread->requestExitAndWait();
487}
488
489TEST_F(SurfaceTextureClientTest, GetTransformMatrixReturnsVerticalFlip) {
490    android_native_buffer_t* buf[3];
491    float mtx[16] = {};
492    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 4));
493    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[0]));
494    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[0]));
495    ASSERT_EQ(OK, mST->updateTexImage());
496    mST->getTransformMatrix(mtx);
497
498    EXPECT_EQ(1.f, mtx[0]);
499    EXPECT_EQ(0.f, mtx[1]);
500    EXPECT_EQ(0.f, mtx[2]);
501    EXPECT_EQ(0.f, mtx[3]);
502
503    EXPECT_EQ(0.f, mtx[4]);
504    EXPECT_EQ(-1.f, mtx[5]);
505    EXPECT_EQ(0.f, mtx[6]);
506    EXPECT_EQ(0.f, mtx[7]);
507
508    EXPECT_EQ(0.f, mtx[8]);
509    EXPECT_EQ(0.f, mtx[9]);
510    EXPECT_EQ(1.f, mtx[10]);
511    EXPECT_EQ(0.f, mtx[11]);
512
513    EXPECT_EQ(0.f, mtx[12]);
514    EXPECT_EQ(1.f, mtx[13]);
515    EXPECT_EQ(0.f, mtx[14]);
516    EXPECT_EQ(1.f, mtx[15]);
517}
518
519TEST_F(SurfaceTextureClientTest, GetTransformMatrixSucceedsAfterFreeingBuffers) {
520    android_native_buffer_t* buf[3];
521    float mtx[16] = {};
522    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 4));
523    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[0]));
524    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[0]));
525    ASSERT_EQ(OK, mST->updateTexImage());
526    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 6)); // frees buffers
527    mST->getTransformMatrix(mtx);
528
529    EXPECT_EQ(1.f, mtx[0]);
530    EXPECT_EQ(0.f, mtx[1]);
531    EXPECT_EQ(0.f, mtx[2]);
532    EXPECT_EQ(0.f, mtx[3]);
533
534    EXPECT_EQ(0.f, mtx[4]);
535    EXPECT_EQ(-1.f, mtx[5]);
536    EXPECT_EQ(0.f, mtx[6]);
537    EXPECT_EQ(0.f, mtx[7]);
538
539    EXPECT_EQ(0.f, mtx[8]);
540    EXPECT_EQ(0.f, mtx[9]);
541    EXPECT_EQ(1.f, mtx[10]);
542    EXPECT_EQ(0.f, mtx[11]);
543
544    EXPECT_EQ(0.f, mtx[12]);
545    EXPECT_EQ(1.f, mtx[13]);
546    EXPECT_EQ(0.f, mtx[14]);
547    EXPECT_EQ(1.f, mtx[15]);
548}
549
550TEST_F(SurfaceTextureClientTest, GetTransformMatrixSucceedsAfterFreeingBuffersWithCrop) {
551    android_native_buffer_t* buf[3];
552    float mtx[16] = {};
553    android_native_rect_t crop;
554    crop.left = 0;
555    crop.top = 0;
556    crop.right = 5;
557    crop.bottom = 5;
558
559    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 4));
560    ASSERT_EQ(OK, native_window_set_buffers_geometry(mANW.get(), 8, 8, 0));
561    ASSERT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &buf[0]));
562    ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &crop));
563    ASSERT_EQ(OK, mANW->queueBuffer(mANW.get(), buf[0]));
564    ASSERT_EQ(OK, mST->updateTexImage());
565    ASSERT_EQ(OK, native_window_set_buffer_count(mANW.get(), 6)); // frees buffers
566    mST->getTransformMatrix(mtx);
567
568    // This accounts for the 1 texel shrink for each edge that's included in the
569    // transform matrix to avoid texturing outside the crop region.
570    EXPECT_EQ(.5f, mtx[0]);
571    EXPECT_EQ(0.f, mtx[1]);
572    EXPECT_EQ(0.f, mtx[2]);
573    EXPECT_EQ(0.f, mtx[3]);
574
575    EXPECT_EQ(0.f, mtx[4]);
576    EXPECT_EQ(-.5f, mtx[5]);
577    EXPECT_EQ(0.f, mtx[6]);
578    EXPECT_EQ(0.f, mtx[7]);
579
580    EXPECT_EQ(0.f, mtx[8]);
581    EXPECT_EQ(0.f, mtx[9]);
582    EXPECT_EQ(1.f, mtx[10]);
583    EXPECT_EQ(0.f, mtx[11]);
584
585    EXPECT_EQ(0.f, mtx[12]);
586    EXPECT_EQ(.5f, mtx[13]);
587    EXPECT_EQ(0.f, mtx[14]);
588    EXPECT_EQ(1.f, mtx[15]);
589}
590
591// This test verifies that the buffer format can be queried immediately after
592// it is set.
593TEST_F(SurfaceTextureClientTest, DISABLED_QueryFormatAfterSettingWorks) {
594    sp<ANativeWindow> anw(mSTC);
595    int fmts[] = {
596        // RGBA_8888 should not come first, as it's the default
597        HAL_PIXEL_FORMAT_RGBX_8888,
598        HAL_PIXEL_FORMAT_RGBA_8888,
599        HAL_PIXEL_FORMAT_RGB_888,
600        HAL_PIXEL_FORMAT_RGB_565,
601        HAL_PIXEL_FORMAT_BGRA_8888,
602        HAL_PIXEL_FORMAT_RGBA_5551,
603        HAL_PIXEL_FORMAT_RGBA_4444,
604        HAL_PIXEL_FORMAT_YV12,
605    };
606
607    const int numFmts = (sizeof(fmts) / sizeof(fmts[0]));
608    for (int i = 0; i < numFmts; i++) {
609      int fmt = -1;
610      ASSERT_EQ(OK, native_window_set_buffers_geometry(anw.get(), 0, 0, fmts[i]));
611      ASSERT_EQ(OK, anw->query(anw.get(), NATIVE_WINDOW_FORMAT, &fmt));
612      EXPECT_EQ(fmts[i], fmt);
613    }
614}
615
616} // namespace android
617