SurfaceTextureGLToGL_test.cpp revision d8ead0ccd4b700e43a78efe9b140d4a069dc343d
1/*
2 * Copyright 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SurfaceTextureGLToGL_test"
18//#define LOG_NDEBUG 0
19
20#include "SurfaceTextureGLToGL.h"
21
22namespace android {
23
24TEST_F(SurfaceTextureGLToGLTest, TransformHintGetsRespected) {
25    const uint32_t texWidth = 32;
26    const uint32_t texHeight = 64;
27
28    mST->setDefaultBufferSize(texWidth, texHeight);
29    mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
30
31    // This test requires 3 buffers to avoid deadlock because we're
32    // both producer and consumer, and only using one thread.
33    mST->setDefaultMaxBufferCount(3);
34
35    // Do the producer side of things
36    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
37            mProducerEglSurface, mProducerEglContext));
38    ASSERT_EQ(EGL_SUCCESS, eglGetError());
39
40    // Start a buffer with our chosen size and transform hint moving
41    // through the system.
42    glClear(GL_COLOR_BUFFER_BIT);  // give the driver something to do
43    eglSwapBuffers(mEglDisplay, mProducerEglSurface);
44    mST->updateTexImage();  // consume it
45    // Swap again.
46    glClear(GL_COLOR_BUFFER_BIT);
47    eglSwapBuffers(mEglDisplay, mProducerEglSurface);
48    mST->updateTexImage();
49
50    // The current buffer should either show the effects of the transform
51    // hint (in the form of an inverse transform), or show that the
52    // transform hint has been ignored.
53    sp<GraphicBuffer> buf = mST->getCurrentBuffer();
54    if (mST->getCurrentTransform() == NATIVE_WINDOW_TRANSFORM_ROT_270) {
55        ASSERT_EQ(texWidth, buf->getHeight());
56        ASSERT_EQ(texHeight, buf->getWidth());
57    } else {
58        ASSERT_EQ(texWidth, buf->getWidth());
59        ASSERT_EQ(texHeight, buf->getHeight());
60    }
61
62    // Reset the transform hint and confirm that it takes.
63    mST->setTransformHint(0);
64    glClear(GL_COLOR_BUFFER_BIT);
65    eglSwapBuffers(mEglDisplay, mProducerEglSurface);
66    mST->updateTexImage();
67    glClear(GL_COLOR_BUFFER_BIT);
68    eglSwapBuffers(mEglDisplay, mProducerEglSurface);
69    mST->updateTexImage();
70
71    buf = mST->getCurrentBuffer();
72    ASSERT_EQ((uint32_t) 0, mST->getCurrentTransform());
73    ASSERT_EQ(texWidth, buf->getWidth());
74    ASSERT_EQ(texHeight, buf->getHeight());
75}
76
77TEST_F(SurfaceTextureGLToGLTest, TexturingFromGLFilledRGBABufferPow2) {
78    const int texWidth = 64;
79    const int texHeight = 64;
80
81    mST->setDefaultBufferSize(texWidth, texHeight);
82
83    // This test requires 3 buffers to complete run on a single thread.
84    mST->setDefaultMaxBufferCount(3);
85
86    // Do the producer side of things
87    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
88            mProducerEglSurface, mProducerEglContext));
89    ASSERT_EQ(EGL_SUCCESS, eglGetError());
90
91    // This is needed to ensure we pick up a buffer of the correct size.
92    eglSwapBuffers(mEglDisplay, mProducerEglSurface);
93
94    glClearColor(0.6, 0.6, 0.6, 0.6);
95    glClear(GL_COLOR_BUFFER_BIT);
96
97    glEnable(GL_SCISSOR_TEST);
98    glScissor(4, 4, 4, 4);
99    glClearColor(1.0, 0.0, 0.0, 1.0);
100    glClear(GL_COLOR_BUFFER_BIT);
101
102    glScissor(24, 48, 4, 4);
103    glClearColor(0.0, 1.0, 0.0, 1.0);
104    glClear(GL_COLOR_BUFFER_BIT);
105
106    glScissor(37, 17, 4, 4);
107    glClearColor(0.0, 0.0, 1.0, 1.0);
108    glClear(GL_COLOR_BUFFER_BIT);
109
110    eglSwapBuffers(mEglDisplay, mProducerEglSurface);
111
112    // Do the consumer side of things
113    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
114            mEglContext));
115    ASSERT_EQ(EGL_SUCCESS, eglGetError());
116
117    glDisable(GL_SCISSOR_TEST);
118
119    // Skip the first frame, which was empty
120    ASSERT_EQ(NO_ERROR, mST->updateTexImage());
121    ASSERT_EQ(NO_ERROR, mST->updateTexImage());
122
123    glClearColor(0.2, 0.2, 0.2, 0.2);
124    glClear(GL_COLOR_BUFFER_BIT);
125
126    glViewport(0, 0, texWidth, texHeight);
127    drawTexture();
128
129    EXPECT_TRUE(checkPixel( 0,  0, 153, 153, 153, 153));
130    EXPECT_TRUE(checkPixel(63,  0, 153, 153, 153, 153));
131    EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
132    EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
133
134    EXPECT_TRUE(checkPixel( 4,  7, 255,   0,   0, 255));
135    EXPECT_TRUE(checkPixel(25, 51,   0, 255,   0, 255));
136    EXPECT_TRUE(checkPixel(40, 19,   0,   0, 255, 255));
137    EXPECT_TRUE(checkPixel(29, 51, 153, 153, 153, 153));
138    EXPECT_TRUE(checkPixel( 5, 32, 153, 153, 153, 153));
139    EXPECT_TRUE(checkPixel(13,  8, 153, 153, 153, 153));
140    EXPECT_TRUE(checkPixel(46,  3, 153, 153, 153, 153));
141    EXPECT_TRUE(checkPixel(30, 33, 153, 153, 153, 153));
142    EXPECT_TRUE(checkPixel( 6, 52, 153, 153, 153, 153));
143    EXPECT_TRUE(checkPixel(55, 33, 153, 153, 153, 153));
144    EXPECT_TRUE(checkPixel(16, 29, 153, 153, 153, 153));
145    EXPECT_TRUE(checkPixel( 1, 30, 153, 153, 153, 153));
146    EXPECT_TRUE(checkPixel(41, 37, 153, 153, 153, 153));
147    EXPECT_TRUE(checkPixel(46, 29, 153, 153, 153, 153));
148    EXPECT_TRUE(checkPixel(15, 25, 153, 153, 153, 153));
149    EXPECT_TRUE(checkPixel( 3, 52, 153, 153, 153, 153));
150}
151
152TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceUnrefsBuffers) {
153    sp<GraphicBuffer> buffers[2];
154
155    // This test requires async mode to run on a single thread.
156    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
157            mProducerEglSurface, mProducerEglContext));
158    ASSERT_EQ(EGL_SUCCESS, eglGetError());
159    EXPECT_TRUE(eglSwapInterval(mEglDisplay, 0));
160    ASSERT_EQ(EGL_SUCCESS, eglGetError());
161
162    for (int i = 0; i < 2; i++) {
163        // Produce a frame
164        EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
165                mProducerEglSurface, mProducerEglContext));
166        ASSERT_EQ(EGL_SUCCESS, eglGetError());
167        glClear(GL_COLOR_BUFFER_BIT);
168        eglSwapBuffers(mEglDisplay, mProducerEglSurface);
169
170        // Consume a frame
171        EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
172                mEglContext));
173        ASSERT_EQ(EGL_SUCCESS, eglGetError());
174        mFW->waitForFrame();
175        ASSERT_EQ(NO_ERROR, mST->updateTexImage());
176        buffers[i] = mST->getCurrentBuffer();
177    }
178
179    // Destroy the GL texture object to release its ref on buffers[2].
180    GLuint texID = TEX_ID;
181    glDeleteTextures(1, &texID);
182
183    // Destroy the EGLSurface
184    EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
185    ASSERT_EQ(EGL_SUCCESS, eglGetError());
186    mProducerEglSurface = EGL_NO_SURFACE;
187
188    // This test should have the only reference to buffer 0.
189    EXPECT_EQ(1, buffers[0]->getStrongCount());
190
191    // The GLConsumer should hold one reference to buffer 1 in its
192    // mCurrentTextureImage member and another reference in mEglSlots. The third
193    // reference is in this test.
194    EXPECT_EQ(3, buffers[1]->getStrongCount());
195}
196
197TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceAfterAbandonUnrefsBuffers) {
198    sp<GraphicBuffer> buffers[3];
199
200    // This test requires async mode to run on a single thread.
201    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
202            mProducerEglSurface, mProducerEglContext));
203    ASSERT_EQ(EGL_SUCCESS, eglGetError());
204    EXPECT_TRUE(eglSwapInterval(mEglDisplay, 0));
205    ASSERT_EQ(EGL_SUCCESS, eglGetError());
206
207    for (int i = 0; i < 3; i++) {
208        // Produce a frame
209        EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
210                mProducerEglSurface, mProducerEglContext));
211        ASSERT_EQ(EGL_SUCCESS, eglGetError());
212        glClear(GL_COLOR_BUFFER_BIT);
213        EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
214        ASSERT_EQ(EGL_SUCCESS, eglGetError());
215
216        // Consume a frame
217        EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
218                mEglContext));
219        ASSERT_EQ(EGL_SUCCESS, eglGetError());
220        mFW->waitForFrame();
221        ASSERT_EQ(NO_ERROR, mST->updateTexImage());
222        buffers[i] = mST->getCurrentBuffer();
223    }
224
225    // Abandon the GLConsumer, releasing the ref that the GLConsumer has
226    // on buffers[2].
227    mST->abandon();
228
229    // Destroy the GL texture object to release its ref on buffers[2].
230    GLuint texID = TEX_ID;
231    glDeleteTextures(1, &texID);
232
233    // Destroy the EGLSurface.
234    EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
235    ASSERT_EQ(EGL_SUCCESS, eglGetError());
236    mProducerEglSurface = EGL_NO_SURFACE;
237
238    EXPECT_EQ(1, buffers[1]->getStrongCount());
239
240    // Depending on how lazily the GL driver dequeues buffers, we may end up
241    // with either two or three total buffers.  If there are three, each entry
242    // of the buffers array will be unique and there should only be one
243    // reference (the one in this test). If there are two the first and last
244    // element in the array will be equal meaning that buffer representing both
245    // 0 and 2 will have two references (one for 0 and one for 2).
246    if (buffers[2] != buffers[0]) {
247        EXPECT_EQ(1, buffers[0]->getStrongCount());
248        EXPECT_EQ(1, buffers[2]->getStrongCount());
249    } else {
250        EXPECT_EQ(2, buffers[0]->getStrongCount());
251    }
252}
253
254TEST_F(SurfaceTextureGLToGLTest, EglMakeCurrentBeforeConsumerDeathUnrefsBuffers) {
255    sp<GraphicBuffer> buffer;
256
257    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
258            mProducerEglSurface, mProducerEglContext));
259
260    // Produce a frame
261    glClear(GL_COLOR_BUFFER_BIT);
262    EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
263    ASSERT_EQ(EGL_SUCCESS, eglGetError());
264
265    // Destroy the EGLSurface.
266    EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
267    ASSERT_EQ(EGL_SUCCESS, eglGetError());
268    mProducerEglSurface = EGL_NO_SURFACE;
269    mSTC.clear();
270    mANW.clear();
271    mTextureRenderer.clear();
272
273    // Consume a frame
274    ASSERT_EQ(NO_ERROR, mST->updateTexImage());
275    buffer = mST->getCurrentBuffer();
276
277    // Destroy the GL texture object to release its ref
278    GLuint texID = TEX_ID;
279    glDeleteTextures(1, &texID);
280
281    // make un-current, all references to buffer should be gone
282    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE,
283            EGL_NO_SURFACE, EGL_NO_CONTEXT));
284
285    // Destroy consumer
286    mST.clear();
287
288    EXPECT_EQ(1, buffer->getStrongCount());
289}
290
291TEST_F(SurfaceTextureGLToGLTest, EglMakeCurrentAfterConsumerDeathUnrefsBuffers) {
292    sp<GraphicBuffer> buffer;
293
294    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
295            mProducerEglSurface, mProducerEglContext));
296
297    // Produce a frame
298    glClear(GL_COLOR_BUFFER_BIT);
299    EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
300    ASSERT_EQ(EGL_SUCCESS, eglGetError());
301
302    // Destroy the EGLSurface.
303    EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
304    ASSERT_EQ(EGL_SUCCESS, eglGetError());
305    mProducerEglSurface = EGL_NO_SURFACE;
306    mSTC.clear();
307    mANW.clear();
308    mTextureRenderer.clear();
309
310    // Consume a frame
311    ASSERT_EQ(NO_ERROR, mST->updateTexImage());
312    buffer = mST->getCurrentBuffer();
313
314    // Destroy the GL texture object to release its ref
315    GLuint texID = TEX_ID;
316    glDeleteTextures(1, &texID);
317
318    // Destroy consumer
319    mST.clear();
320
321    // make un-current, all references to buffer should be gone
322    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE,
323            EGL_NO_SURFACE, EGL_NO_CONTEXT));
324
325    EXPECT_EQ(1, buffer->getStrongCount());
326}
327
328TEST_F(SurfaceTextureGLToGLTest, TexturingFromUserSizedGLFilledBuffer) {
329    enum { texWidth = 64 };
330    enum { texHeight = 64 };
331
332    // This test requires 3 buffers to complete run on a single thread.
333    mST->setDefaultMaxBufferCount(3);
334
335    // Set the user buffer size.
336    native_window_set_buffers_user_dimensions(mANW.get(), texWidth, texHeight);
337
338    // Do the producer side of things
339    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
340            mProducerEglSurface, mProducerEglContext));
341    ASSERT_EQ(EGL_SUCCESS, eglGetError());
342
343    // This is needed to ensure we pick up a buffer of the correct size.
344    eglSwapBuffers(mEglDisplay, mProducerEglSurface);
345
346    glClearColor(0.6, 0.6, 0.6, 0.6);
347    glClear(GL_COLOR_BUFFER_BIT);
348
349    glEnable(GL_SCISSOR_TEST);
350    glScissor(4, 4, 1, 1);
351    glClearColor(1.0, 0.0, 0.0, 1.0);
352    glClear(GL_COLOR_BUFFER_BIT);
353
354    eglSwapBuffers(mEglDisplay, mProducerEglSurface);
355
356    // Do the consumer side of things
357    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
358            mEglContext));
359    ASSERT_EQ(EGL_SUCCESS, eglGetError());
360
361    glDisable(GL_SCISSOR_TEST);
362
363    // Skip the first frame, which was empty
364    ASSERT_EQ(NO_ERROR, mST->updateTexImage());
365    ASSERT_EQ(NO_ERROR, mST->updateTexImage());
366
367    glClearColor(0.2, 0.2, 0.2, 0.2);
368    glClear(GL_COLOR_BUFFER_BIT);
369
370    glViewport(0, 0, texWidth, texHeight);
371    drawTexture();
372
373    EXPECT_TRUE(checkPixel( 0,  0, 153, 153, 153, 153));
374    EXPECT_TRUE(checkPixel(63,  0, 153, 153, 153, 153));
375    EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
376    EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
377
378    EXPECT_TRUE(checkPixel( 4,  4, 255,   0,   0, 255));
379    EXPECT_TRUE(checkPixel( 5,  5, 153, 153, 153, 153));
380    EXPECT_TRUE(checkPixel( 3,  3, 153, 153, 153, 153));
381    EXPECT_TRUE(checkPixel(45, 52, 153, 153, 153, 153));
382    EXPECT_TRUE(checkPixel(12, 36, 153, 153, 153, 153));
383}
384
385TEST_F(SurfaceTextureGLToGLTest, TexturingFromPreRotatedUserSizedGLFilledBuffer) {
386    enum { texWidth = 64 };
387    enum { texHeight = 16 };
388
389    // This test requires 3 buffers to complete run on a single thread.
390    mST->setDefaultMaxBufferCount(3);
391
392    // Set the transform hint.
393    mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
394
395    // Set the user buffer size.
396    native_window_set_buffers_user_dimensions(mANW.get(), texWidth, texHeight);
397
398    // Do the producer side of things
399    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
400            mProducerEglSurface, mProducerEglContext));
401    ASSERT_EQ(EGL_SUCCESS, eglGetError());
402
403    // This is needed to ensure we pick up a buffer of the correct size and the
404    // new rotation hint.
405    eglSwapBuffers(mEglDisplay, mProducerEglSurface);
406
407    glClearColor(0.6, 0.6, 0.6, 0.6);
408    glClear(GL_COLOR_BUFFER_BIT);
409
410    glEnable(GL_SCISSOR_TEST);
411    glScissor(24, 4, 1, 1);
412    glClearColor(1.0, 0.0, 0.0, 1.0);
413    glClear(GL_COLOR_BUFFER_BIT);
414
415    eglSwapBuffers(mEglDisplay, mProducerEglSurface);
416
417    // Do the consumer side of things
418    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
419            mEglContext));
420    ASSERT_EQ(EGL_SUCCESS, eglGetError());
421
422    glDisable(GL_SCISSOR_TEST);
423
424    // Skip the first frame, which was empty
425    ASSERT_EQ(NO_ERROR, mST->updateTexImage());
426    ASSERT_EQ(NO_ERROR, mST->updateTexImage());
427
428    glClearColor(0.2, 0.2, 0.2, 0.2);
429    glClear(GL_COLOR_BUFFER_BIT);
430
431    glViewport(0, 0, texWidth, texHeight);
432    drawTexture();
433
434    EXPECT_TRUE(checkPixel( 0,  0, 153, 153, 153, 153));
435    EXPECT_TRUE(checkPixel(63,  0, 153, 153, 153, 153));
436    EXPECT_TRUE(checkPixel(63, 15, 153, 153, 153, 153));
437    EXPECT_TRUE(checkPixel( 0, 15, 153, 153, 153, 153));
438
439    EXPECT_TRUE(checkPixel(24,  4, 255,   0,   0, 255));
440    EXPECT_TRUE(checkPixel(25,  5, 153, 153, 153, 153));
441    EXPECT_TRUE(checkPixel(23,  3, 153, 153, 153, 153));
442    EXPECT_TRUE(checkPixel(45, 13, 153, 153, 153, 153));
443    EXPECT_TRUE(checkPixel(12,  8, 153, 153, 153, 153));
444}
445
446TEST_F(SurfaceTextureGLToGLTest, TexturingFromPreRotatedGLFilledBuffer) {
447    enum { texWidth = 64 };
448    enum { texHeight = 16 };
449
450    // This test requires 3 buffers to complete run on a single thread.
451    mST->setDefaultMaxBufferCount(3);
452
453    // Set the transform hint.
454    mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
455
456    // Set the default buffer size.
457    mST->setDefaultBufferSize(texWidth, texHeight);
458
459    // Do the producer side of things
460    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
461            mProducerEglSurface, mProducerEglContext));
462    ASSERT_EQ(EGL_SUCCESS, eglGetError());
463
464    // This is needed to ensure we pick up a buffer of the correct size and the
465    // new rotation hint.
466    eglSwapBuffers(mEglDisplay, mProducerEglSurface);
467
468    glClearColor(0.6, 0.6, 0.6, 0.6);
469    glClear(GL_COLOR_BUFFER_BIT);
470
471    glEnable(GL_SCISSOR_TEST);
472    glScissor(24, 4, 1, 1);
473    glClearColor(1.0, 0.0, 0.0, 1.0);
474    glClear(GL_COLOR_BUFFER_BIT);
475
476    eglSwapBuffers(mEglDisplay, mProducerEglSurface);
477
478    // Do the consumer side of things
479    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
480            mEglContext));
481    ASSERT_EQ(EGL_SUCCESS, eglGetError());
482
483    glDisable(GL_SCISSOR_TEST);
484
485    // Skip the first frame, which was empty
486    ASSERT_EQ(NO_ERROR, mST->updateTexImage());
487    ASSERT_EQ(NO_ERROR, mST->updateTexImage());
488
489    glClearColor(0.2, 0.2, 0.2, 0.2);
490    glClear(GL_COLOR_BUFFER_BIT);
491
492    glViewport(0, 0, texWidth, texHeight);
493    drawTexture();
494
495    EXPECT_TRUE(checkPixel( 0,  0, 153, 153, 153, 153));
496    EXPECT_TRUE(checkPixel(63,  0, 153, 153, 153, 153));
497    EXPECT_TRUE(checkPixel(63, 15, 153, 153, 153, 153));
498    EXPECT_TRUE(checkPixel( 0, 15, 153, 153, 153, 153));
499
500    EXPECT_TRUE(checkPixel(24,  4, 255,   0,   0, 255));
501    EXPECT_TRUE(checkPixel(25,  5, 153, 153, 153, 153));
502    EXPECT_TRUE(checkPixel(23,  3, 153, 153, 153, 153));
503    EXPECT_TRUE(checkPixel(45, 13, 153, 153, 153, 153));
504    EXPECT_TRUE(checkPixel(12,  8, 153, 153, 153, 153));
505}
506
507} // namespace android
508