SurfaceTextureClient_test.cpp revision 7a5b22c4e33928f81c2e8a3d85050c35bd44b1e0
1134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis/*
2134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis * Copyright (C) 2011 The Android Open Source Project
3134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis *
4134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis * Licensed under the Apache License, Version 2.0 (the "License");
5134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis * you may not use this file except in compliance with the License.
6134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis * You may obtain a copy of the License at
7134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis *
8134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis *      http://www.apache.org/licenses/LICENSE-2.0
9134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis *
10134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis * Unless required by applicable law or agreed to in writing, software
11134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis * distributed under the License is distributed on an "AS IS" BASIS,
12134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis * See the License for the specific language governing permissions and
14134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis * limitations under the License.
15134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis */
16134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis
175c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis#include <EGL/egl.h>
18134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis#include <gtest/gtest.h>
195c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis#include <gui/SurfaceTextureClient.h>
207a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian#include <utils/threads.h>
21134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis
22134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennisnamespace android {
23134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis
24134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennisclass SurfaceTextureClientTest : public ::testing::Test {
25134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennisprotected:
267a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    SurfaceTextureClientTest():
277a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            mEglDisplay(EGL_NO_DISPLAY),
287a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            mEglSurface(EGL_NO_SURFACE),
297a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            mEglContext(EGL_NO_CONTEXT) {
307a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    }
317a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
32134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    virtual void SetUp() {
33134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis        mST = new SurfaceTexture(123);
34134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis        mSTC = new SurfaceTextureClient(mST);
357a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
367a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        // We need a valid GL context so we can test updateTexImage()
377a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        // This initializes EGL and create a dummy GL context with a
387a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        // pbuffer render target.
397a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
407a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        ASSERT_EQ(EGL_SUCCESS, eglGetError());
417a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay);
427a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
437a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        EGLint majorVersion, minorVersion;
447a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        EXPECT_TRUE(eglInitialize(mEglDisplay, &majorVersion, &minorVersion));
457a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        ASSERT_EQ(EGL_SUCCESS, eglGetError());
467a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
477a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        EGLConfig myConfig;
487a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        EGLint numConfigs = 0;
497a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(),
507a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian                &myConfig, 1, &numConfigs));
517a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        ASSERT_EQ(EGL_SUCCESS, eglGetError());
527a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
537a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        EGLint pbufferAttribs[] = {
547a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            EGL_WIDTH, 16,
557a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            EGL_HEIGHT, 16,
567a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            EGL_NONE };
577a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        mEglSurface = eglCreatePbufferSurface(mEglDisplay, myConfig, pbufferAttribs);
587a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        ASSERT_EQ(EGL_SUCCESS, eglGetError());
597a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        ASSERT_NE(EGL_NO_SURFACE, mEglSurface);
607a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
617a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        mEglContext = eglCreateContext(mEglDisplay, myConfig, EGL_NO_CONTEXT, 0);
627a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        ASSERT_EQ(EGL_SUCCESS, eglGetError());
637a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        ASSERT_NE(EGL_NO_CONTEXT, mEglContext);
647a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
657a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext));
667a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        ASSERT_EQ(EGL_SUCCESS, eglGetError());
67134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    }
68134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis
69134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    virtual void TearDown() {
70134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis        mST.clear();
71134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis        mSTC.clear();
727a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
737a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        eglDestroyContext(mEglDisplay, mEglContext);
747a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        eglDestroySurface(mEglDisplay, mEglSurface);
757a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        eglTerminate(mEglDisplay);
767a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    }
777a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
787a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    virtual EGLint const* getConfigAttribs() {
797a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        static EGLint sDefaultConfigAttribs[] = {
807a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
817a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            EGL_NONE
827a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        };
837a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
847a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        return sDefaultConfigAttribs;
85134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    }
86134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis
87134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    sp<SurfaceTexture> mST;
88134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    sp<SurfaceTextureClient> mSTC;
897a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EGLDisplay mEglDisplay;
907a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EGLSurface mEglSurface;
917a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EGLContext mEglContext;
92134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis};
93134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis
94bae774eb20bebd409441b93386b51bccda75f546Jamie GennisTEST_F(SurfaceTextureClientTest, GetISurfaceTextureIsNotNull) {
95bae774eb20bebd409441b93386b51bccda75f546Jamie Gennis    sp<ISurfaceTexture> ist(mSTC->getISurfaceTexture());
96bae774eb20bebd409441b93386b51bccda75f546Jamie Gennis    ASSERT_TRUE(ist != NULL);
97bae774eb20bebd409441b93386b51bccda75f546Jamie Gennis}
98bae774eb20bebd409441b93386b51bccda75f546Jamie Gennis
99134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie GennisTEST_F(SurfaceTextureClientTest, QueuesToWindowCompositorIsFalse) {
100134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    sp<ANativeWindow> anw(mSTC);
101134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    int result = -123;
102134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
103134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis            &result);
104134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    EXPECT_EQ(NO_ERROR, err);
105134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    EXPECT_EQ(0, result);
106134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis}
107134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis
108391bbe2246a7547dbf3460c231b3c5ba691d4eb1Jamie GennisTEST_F(SurfaceTextureClientTest, ConcreteTypeIsSurfaceTextureClient) {
109391bbe2246a7547dbf3460c231b3c5ba691d4eb1Jamie Gennis    sp<ANativeWindow> anw(mSTC);
110391bbe2246a7547dbf3460c231b3c5ba691d4eb1Jamie Gennis    int result = -123;
111391bbe2246a7547dbf3460c231b3c5ba691d4eb1Jamie Gennis    int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
112391bbe2246a7547dbf3460c231b3c5ba691d4eb1Jamie Gennis    EXPECT_EQ(NO_ERROR, err);
113391bbe2246a7547dbf3460c231b3c5ba691d4eb1Jamie Gennis    EXPECT_EQ(NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT, result);
114391bbe2246a7547dbf3460c231b3c5ba691d4eb1Jamie Gennis}
115391bbe2246a7547dbf3460c231b3c5ba691d4eb1Jamie Gennis
1165c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie GennisTEST_F(SurfaceTextureClientTest, ANativeWindowLockFails) {
1175c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis    sp<ANativeWindow> anw(mSTC);
1185c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis    ANativeWindow_Buffer buf;
1195c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis    ASSERT_EQ(BAD_VALUE, ANativeWindow_lock(anw.get(), &buf, NULL));
1205c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis}
1215c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis
1221b528fb9d818044973abf656c9d2d3c1192bcfdcJamie GennisTEST_F(SurfaceTextureClientTest, EglCreateWindowSurfaceSucceeds) {
1235c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis    sp<ANativeWindow> anw(mSTC);
1245c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis
1255c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis    EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1265c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis    ASSERT_EQ(EGL_SUCCESS, eglGetError());
1275c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis    ASSERT_NE(EGL_NO_DISPLAY, dpy);
1285c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis
1295c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis    EGLint majorVersion;
1305c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis    EGLint minorVersion;
1315c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis    EXPECT_TRUE(eglInitialize(dpy, &majorVersion, &minorVersion));
1325c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis    ASSERT_EQ(EGL_SUCCESS, eglGetError());
1335c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis
1345c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis    EGLConfig myConfig = {0};
1355c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis    EGLint numConfigs = 0;
1365c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis    EGLint configAttribs[] = {
1375c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis        EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1385c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1395c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis        EGL_RED_SIZE, 8,
1405c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis        EGL_GREEN_SIZE, 8,
1415c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis        EGL_BLUE_SIZE, 8,
1425c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis        EGL_ALPHA_SIZE, 8,
1435c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis        EGL_DEPTH_SIZE, 16,
1445c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis        EGL_STENCIL_SIZE, 8,
1455c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis        EGL_NONE };
1465c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis    EXPECT_TRUE(eglChooseConfig(dpy, configAttribs, &myConfig, 1,
1475c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis            &numConfigs));
1485c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis    ASSERT_EQ(EGL_SUCCESS, eglGetError());
1495c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis
1505c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis    EGLSurface eglSurface = eglCreateWindowSurface(dpy, myConfig, anw.get(),
1515c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis            NULL);
152eafabcdc1639fb96062d9e3c39b0ae27b0238ae1Mathias Agopian    EXPECT_NE(EGL_NO_SURFACE, eglSurface);
153eafabcdc1639fb96062d9e3c39b0ae27b0238ae1Mathias Agopian    EXPECT_EQ(EGL_SUCCESS, eglGetError());
1545c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis
1555c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis    eglTerminate(dpy);
1565c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis}
1575c0c93a8c49b4053744efb8953b915fa7f0923a5Jamie Gennis
158a5c75c01620179ce00812354778a29a80d76e71fMathias AgopianTEST_F(SurfaceTextureClientTest, BufferGeometryInvalidSizesFail) {
159a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    sp<ANativeWindow> anw(mSTC);
160a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian
161a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_GT(OK, native_window_set_buffers_geometry(anw.get(), -1,  0,  0));
162a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_GT(OK, native_window_set_buffers_geometry(anw.get(),  0, -1,  0));
163a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_GT(OK, native_window_set_buffers_geometry(anw.get(),  0,  0, -1));
164a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_GT(OK, native_window_set_buffers_geometry(anw.get(), -1, -1,  0));
165a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_GT(OK, native_window_set_buffers_geometry(anw.get(),  0,  8,  0));
166a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_GT(OK, native_window_set_buffers_geometry(anw.get(),  8,  0,  0));
167a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian}
168a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian
169a5c75c01620179ce00812354778a29a80d76e71fMathias AgopianTEST_F(SurfaceTextureClientTest, DefaultGeometryValues) {
170a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    sp<ANativeWindow> anw(mSTC);
171697526bc9e44ce61c88614f98387ae8bbf0a187eIliyan Malchev    ANativeWindowBuffer* buf;
172a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf));
173a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(1, buf->width);
174a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(1, buf->height);
175a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(PIXEL_FORMAT_RGBA_8888, buf->format);
176a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->cancelBuffer(anw.get(), buf));
177a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian}
178a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian
179a5c75c01620179ce00812354778a29a80d76e71fMathias AgopianTEST_F(SurfaceTextureClientTest, BufferGeometryCanBeSet) {
180a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    sp<ANativeWindow> anw(mSTC);
181697526bc9e44ce61c88614f98387ae8bbf0a187eIliyan Malchev    ANativeWindowBuffer* buf;
182a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(OK, native_window_set_buffers_geometry(anw.get(), 16, 8, PIXEL_FORMAT_RGB_565));
183a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf));
184a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(16, buf->width);
185a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(8, buf->height);
186a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(PIXEL_FORMAT_RGB_565, buf->format);
187a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->cancelBuffer(anw.get(), buf));
188a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian}
189a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian
190a5c75c01620179ce00812354778a29a80d76e71fMathias AgopianTEST_F(SurfaceTextureClientTest, BufferGeometryDefaultSizeSetFormat) {
191a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    sp<ANativeWindow> anw(mSTC);
192697526bc9e44ce61c88614f98387ae8bbf0a187eIliyan Malchev    ANativeWindowBuffer* buf;
193a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(OK, native_window_set_buffers_geometry(anw.get(), 0, 0, PIXEL_FORMAT_RGB_565));
194a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf));
195a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(1, buf->width);
196a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(1, buf->height);
197a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(PIXEL_FORMAT_RGB_565, buf->format);
198a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->cancelBuffer(anw.get(), buf));
199a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian}
200a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian
201a5c75c01620179ce00812354778a29a80d76e71fMathias AgopianTEST_F(SurfaceTextureClientTest, BufferGeometrySetSizeDefaultFormat) {
202a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    sp<ANativeWindow> anw(mSTC);
203697526bc9e44ce61c88614f98387ae8bbf0a187eIliyan Malchev    ANativeWindowBuffer* buf;
204a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(OK, native_window_set_buffers_geometry(anw.get(), 16, 8, 0));
205a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf));
206a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(16, buf->width);
207a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(8, buf->height);
208a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(PIXEL_FORMAT_RGBA_8888, buf->format);
209a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->cancelBuffer(anw.get(), buf));
210a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian}
211a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian
212a5c75c01620179ce00812354778a29a80d76e71fMathias AgopianTEST_F(SurfaceTextureClientTest, BufferGeometrySizeCanBeUnset) {
213a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    sp<ANativeWindow> anw(mSTC);
214697526bc9e44ce61c88614f98387ae8bbf0a187eIliyan Malchev    ANativeWindowBuffer* buf;
215a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(OK, native_window_set_buffers_geometry(anw.get(), 16, 8, 0));
216a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf));
217a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(16, buf->width);
218a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(8, buf->height);
219a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(PIXEL_FORMAT_RGBA_8888, buf->format);
220a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->cancelBuffer(anw.get(), buf));
221a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(OK, native_window_set_buffers_geometry(anw.get(), 0, 0, 0));
222a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf));
223a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(1, buf->width);
224a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(1, buf->height);
225a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(PIXEL_FORMAT_RGBA_8888, buf->format);
226a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->cancelBuffer(anw.get(), buf));
227a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian}
228a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian
229a5c75c01620179ce00812354778a29a80d76e71fMathias AgopianTEST_F(SurfaceTextureClientTest, BufferGeometrySizeCanBeChangedWithoutFormat) {
230a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    sp<ANativeWindow> anw(mSTC);
231697526bc9e44ce61c88614f98387ae8bbf0a187eIliyan Malchev    ANativeWindowBuffer* buf;
232a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(OK, native_window_set_buffers_geometry(anw.get(), 0, 0, PIXEL_FORMAT_RGB_565));
233a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf));
234a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(1, buf->width);
235a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(1, buf->height);
236a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(PIXEL_FORMAT_RGB_565, buf->format);
237a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->cancelBuffer(anw.get(), buf));
238a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(OK, native_window_set_buffers_geometry(anw.get(), 16, 8, 0));
239a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf));
240a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(16, buf->width);
241a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(8, buf->height);
242a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(PIXEL_FORMAT_RGB_565, buf->format);
243a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->cancelBuffer(anw.get(), buf));
244a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian}
245a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian
246a5c75c01620179ce00812354778a29a80d76e71fMathias AgopianTEST_F(SurfaceTextureClientTest, SurfaceTextureSetDefaultSize) {
247a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    sp<ANativeWindow> anw(mSTC);
248a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    sp<SurfaceTexture> st(mST);
249697526bc9e44ce61c88614f98387ae8bbf0a187eIliyan Malchev    ANativeWindowBuffer* buf;
250a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(OK, st->setDefaultBufferSize(16, 8));
251a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf));
252a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(16, buf->width);
253a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(8, buf->height);
254a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(PIXEL_FORMAT_RGBA_8888, buf->format);
255a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->cancelBuffer(anw.get(), buf));
256a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian}
257a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian
258a5c75c01620179ce00812354778a29a80d76e71fMathias AgopianTEST_F(SurfaceTextureClientTest, SurfaceTextureSetDefaultSizeAfterDequeue) {
259a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    sp<ANativeWindow> anw(mSTC);
260a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    sp<SurfaceTexture> st(mST);
261697526bc9e44ce61c88614f98387ae8bbf0a187eIliyan Malchev    ANativeWindowBuffer* buf[2];
262a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[0]));
263a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[1]));
264a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_NE(buf[0], buf[1]);
265a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->cancelBuffer(anw.get(), buf[0]));
266a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->cancelBuffer(anw.get(), buf[1]));
267a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(OK, st->setDefaultBufferSize(16, 8));
268a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[0]));
269a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[1]));
270a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_NE(buf[0], buf[1]);
271a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(16, buf[0]->width);
272a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(16, buf[1]->width);
273a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(8, buf[0]->height);
274a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(8, buf[1]->height);
275a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->cancelBuffer(anw.get(), buf[0]));
276a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->cancelBuffer(anw.get(), buf[1]));
277a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian}
278a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian
279a5c75c01620179ce00812354778a29a80d76e71fMathias AgopianTEST_F(SurfaceTextureClientTest, SurfaceTextureSetDefaultSizeVsGeometry) {
280a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    sp<ANativeWindow> anw(mSTC);
281a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    sp<SurfaceTexture> st(mST);
282697526bc9e44ce61c88614f98387ae8bbf0a187eIliyan Malchev    ANativeWindowBuffer* buf[2];
283a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(OK, st->setDefaultBufferSize(16, 8));
284a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[0]));
285a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[1]));
286a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_NE(buf[0], buf[1]);
287a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(16, buf[0]->width);
288a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(16, buf[1]->width);
289a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(8, buf[0]->height);
290a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(8, buf[1]->height);
291a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->cancelBuffer(anw.get(), buf[0]));
292a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->cancelBuffer(anw.get(), buf[1]));
293a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(OK, native_window_set_buffers_geometry(anw.get(), 12, 24, 0));
294a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[0]));
295a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[1]));
296a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_NE(buf[0], buf[1]);
297a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(12, buf[0]->width);
298a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(12, buf[1]->width);
299a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(24, buf[0]->height);
300a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    EXPECT_EQ(24, buf[1]->height);
301a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->cancelBuffer(anw.get(), buf[0]));
302a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    ASSERT_EQ(OK, anw->cancelBuffer(anw.get(), buf[1]));
303a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian}
304a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian
3057a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias AgopianTEST_F(SurfaceTextureClientTest, SurfaceTextureTooManyUpdateTexImage) {
3067a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    sp<ANativeWindow> anw(mSTC);
3077a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    sp<SurfaceTexture> st(mST);
3087a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    android_native_buffer_t* buf[3];
3097a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, st->setSynchronousMode(false));
3107a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, native_window_set_buffer_count(anw.get(), 3));
3117a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
3127a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[0]));
3137a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->queueBuffer(anw.get(), buf[0]));
3147a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(OK, st->updateTexImage());
3157a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(OK, st->updateTexImage());
3167a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
3177a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, st->setSynchronousMode(true));
3187a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, native_window_set_buffer_count(anw.get(), 2));
3197a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
3207a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[0]));
3217a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->queueBuffer(anw.get(), buf[0]));
3227a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[1]));
3237a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->queueBuffer(anw.get(), buf[1]));
3247a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
3257a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(OK, st->updateTexImage());
3267a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(OK, st->updateTexImage());
3277a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(OK, st->updateTexImage());
3287a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian}
3297a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
3307a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias AgopianTEST_F(SurfaceTextureClientTest, SurfaceTextureSyncModeSlowRetire) {
3317a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    sp<ANativeWindow> anw(mSTC);
3327a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    sp<SurfaceTexture> st(mST);
3337a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    android_native_buffer_t* buf[3];
3347a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, st->setSynchronousMode(true));
3357a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, native_window_set_buffer_count(anw.get(), 4));
3367a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[0]));
3377a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[1]));
3387a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[2]));
3397a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_NE(buf[0], buf[1]);
3407a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_NE(buf[1], buf[2]);
3417a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_NE(buf[2], buf[0]);
3427a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->queueBuffer(anw.get(), buf[0]));
3437a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->queueBuffer(anw.get(), buf[1]));
3447a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->queueBuffer(anw.get(), buf[2]));
3457a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(OK, st->updateTexImage());
3467a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(st->getCurrentBuffer().get(), buf[0]);
3477a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(OK, st->updateTexImage());
3487a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(st->getCurrentBuffer().get(), buf[1]);
3497a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(OK, st->updateTexImage());
3507a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(st->getCurrentBuffer().get(), buf[2]);
3517a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian}
3527a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
3537a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias AgopianTEST_F(SurfaceTextureClientTest, SurfaceTextureSyncModeFastRetire) {
3547a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    sp<ANativeWindow> anw(mSTC);
3557a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    sp<SurfaceTexture> st(mST);
3567a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    android_native_buffer_t* buf[3];
3577a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, st->setSynchronousMode(true));
3587a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, native_window_set_buffer_count(anw.get(), 4));
3597a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[0]));
3607a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[1]));
3617a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[2]));
3627a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_NE(buf[0], buf[1]);
3637a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_NE(buf[1], buf[2]);
3647a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_NE(buf[2], buf[0]);
3657a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->queueBuffer(anw.get(), buf[0]));
3667a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(OK, st->updateTexImage());
3677a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(st->getCurrentBuffer().get(), buf[0]);
3687a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->queueBuffer(anw.get(), buf[1]));
3697a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(OK, st->updateTexImage());
3707a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(st->getCurrentBuffer().get(), buf[1]);
3717a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->queueBuffer(anw.get(), buf[2]));
3727a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(OK, st->updateTexImage());
3737a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(st->getCurrentBuffer().get(), buf[2]);
3747a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian}
3757a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
3767a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias AgopianTEST_F(SurfaceTextureClientTest, SurfaceTextureSyncModeDQQR) {
3777a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    sp<ANativeWindow> anw(mSTC);
3787a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    sp<SurfaceTexture> st(mST);
3797a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    android_native_buffer_t* buf[3];
3807a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, st->setSynchronousMode(true));
3817a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, native_window_set_buffer_count(anw.get(), 3));
3827a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
3837a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[0]));
3847a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->queueBuffer(anw.get(), buf[0]));
3857a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(OK, st->updateTexImage());
3867a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(st->getCurrentBuffer().get(), buf[0]);
3877a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
3887a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[1]));
3897a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_NE(buf[0], buf[1]);
3907a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->queueBuffer(anw.get(), buf[1]));
3917a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(OK, st->updateTexImage());
3927a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(st->getCurrentBuffer().get(), buf[1]);
3937a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
3947a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[2]));
3957a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_NE(buf[1], buf[2]);
3967a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->queueBuffer(anw.get(), buf[2]));
3977a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(OK, st->updateTexImage());
3987a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(st->getCurrentBuffer().get(), buf[2]);
3997a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian}
4007a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
4017a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias AgopianTEST_F(SurfaceTextureClientTest, SurfaceTextureSyncModeDequeueCurrent) {
4027a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    sp<ANativeWindow> anw(mSTC);
4037a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    sp<SurfaceTexture> st(mST);
4047a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    android_native_buffer_t* buf[3];
4057a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    android_native_buffer_t* firstBuf;
4067a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, st->setSynchronousMode(true));
4077a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, native_window_set_buffer_count(anw.get(), 3));
4087a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &firstBuf));
4097a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->queueBuffer(anw.get(), firstBuf));
4107a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(OK, st->updateTexImage());
4117a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(st->getCurrentBuffer().get(), firstBuf);
4127a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[0]));
4137a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->queueBuffer(anw.get(), buf[0]));
4147a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[1]));
4157a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->queueBuffer(anw.get(), buf[1]));
4167a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[2]));
4177a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->queueBuffer(anw.get(), buf[2]));
4187a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_NE(buf[0], buf[1]);
4197a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_NE(buf[1], buf[2]);
4207a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_NE(buf[2], buf[0]);
4217a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(firstBuf, buf[2]);
4227a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian}
4237a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
4247a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias AgopianTEST_F(SurfaceTextureClientTest, SurfaceTextureSyncModeTwoBuffers) {
4257a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    sp<ANativeWindow> anw(mSTC);
4267a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    sp<SurfaceTexture> st(mST);
4277a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, st->setSynchronousMode(true));
4287a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(OK, native_window_set_buffer_count(anw.get(), 3));
4297a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(OK, native_window_set_buffer_count(anw.get(), 2));
4307a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_NE(OK, native_window_set_buffer_count(anw.get(), 1));
4317a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian}
4327a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
4337a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias AgopianTEST_F(SurfaceTextureClientTest, SurfaceTextureSyncModeMinUndequeued) {
4347a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    sp<ANativeWindow> anw(mSTC);
4357a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    sp<SurfaceTexture> st(mST);
4367a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    android_native_buffer_t* buf[3];
4377a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, st->setSynchronousMode(true));
4387a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, native_window_set_buffer_count(anw.get(), 3));
4397a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[0]));
4407a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[1]));
4417a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(-EBUSY, anw->dequeueBuffer(anw.get(), &buf[2]));
4427a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
4437a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->queueBuffer(anw.get(), buf[1]));
4447a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
4457a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(OK, st->updateTexImage());
4467a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(st->getCurrentBuffer().get(), buf[1]);
4477a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
4487a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    EXPECT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[2]));
4497a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
4507a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->cancelBuffer(anw.get(), buf[0]));
4517a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->cancelBuffer(anw.get(), buf[2]));
4527a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian}
4537a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
4547a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias AgopianTEST_F(SurfaceTextureClientTest, SurfaceTextureSyncModeWaitRetire) {
4557a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    sp<ANativeWindow> anw(mSTC);
4567a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    sp<SurfaceTexture> st(mST);
4577a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
4587a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    class MyThread : public Thread {
4597a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        sp<SurfaceTexture> st;
4607a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        EGLContext ctx;
4617a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        EGLSurface sur;
4627a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        EGLDisplay dpy;
4637a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        bool mBufferRetired;
4647a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        Mutex mLock;
4657a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        virtual bool threadLoop() {
4667a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            eglMakeCurrent(dpy, sur, sur, ctx);
4677a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            usleep(20000);
4687a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            Mutex::Autolock _l(mLock);
4697a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            st->updateTexImage();
4707a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            mBufferRetired = true;
4717a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
4727a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            return false;
4737a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        }
4747a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    public:
4757a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        MyThread(const sp<SurfaceTexture>& st)
4767a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            : st(st), mBufferRetired(false) {
4777a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            ctx = eglGetCurrentContext();
4787a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            sur = eglGetCurrentSurface(EGL_DRAW);
4797a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            dpy = eglGetCurrentDisplay();
4807a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
4817a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        }
4827a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        ~MyThread() {
4837a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            eglMakeCurrent(dpy, sur, sur, ctx);
4847a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        }
4857a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        void bufferDequeued() {
4867a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            Mutex::Autolock _l(mLock);
4877a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian            EXPECT_EQ(true, mBufferRetired);
4887a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian        }
4897a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    };
4907a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
4917a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    android_native_buffer_t* buf[3];
4927a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, st->setSynchronousMode(true));
4937a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, native_window_set_buffer_count(anw.get(), 2));
4947a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    // dequeue/queue/update so we have a current buffer
4957a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[0]));
4967a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->queueBuffer(anw.get(), buf[0]));
4977a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    st->updateTexImage();
4987a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
4997a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    MyThread* thread = new MyThread(st);
5007a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    sp<Thread> threadBase(thread);
5017a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
5027a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[0]));
5037a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->queueBuffer(anw.get(), buf[0]));
5047a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[1]));
5057a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->queueBuffer(anw.get(), buf[1]));
5067a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    thread->run();
5077a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[2]));
5087a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    ASSERT_EQ(OK, anw->queueBuffer(anw.get(), buf[2]));
5097a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    thread->bufferDequeued();
5107a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian    thread->requestExitAndWait();
5117a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian}
5127a5b22c4e33928f81c2e8a3d85050c35bd44b1e0Mathias Agopian
513134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis}
514