SurfaceTextureGLToGL_test.cpp revision cb1fcdedaaf95acabeac6a2d5bff423d6ca62296
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 a single reference to buffer 1 in its
192    // mCurrentBuffer member.  All of the references in the slots should have
193    // been released.
194    EXPECT_EQ(2, 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[0]->getStrongCount());
239    EXPECT_EQ(1, buffers[1]->getStrongCount());
240
241    // Depending on how lazily the GL driver dequeues buffers, we may end up
242    // with either two or three total buffers.  If there are three, make sure
243    // the last one was properly down-ref'd.
244    if (buffers[2] != buffers[0]) {
245        EXPECT_EQ(1, buffers[2]->getStrongCount());
246    }
247}
248
249TEST_F(SurfaceTextureGLToGLTest, EglMakeCurrentBeforeConsumerDeathUnrefsBuffers) {
250    sp<GraphicBuffer> buffer;
251
252    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
253            mProducerEglSurface, mProducerEglContext));
254
255    // Produce a frame
256    glClear(GL_COLOR_BUFFER_BIT);
257    EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
258    ASSERT_EQ(EGL_SUCCESS, eglGetError());
259
260    // Destroy the EGLSurface.
261    EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
262    ASSERT_EQ(EGL_SUCCESS, eglGetError());
263    mProducerEglSurface = EGL_NO_SURFACE;
264    mSTC.clear();
265    mANW.clear();
266    mTextureRenderer.clear();
267
268    // Consume a frame
269    ASSERT_EQ(NO_ERROR, mST->updateTexImage());
270    buffer = mST->getCurrentBuffer();
271
272    // Destroy the GL texture object to release its ref
273    GLuint texID = TEX_ID;
274    glDeleteTextures(1, &texID);
275
276    // make un-current, all references to buffer should be gone
277    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE,
278            EGL_NO_SURFACE, EGL_NO_CONTEXT));
279
280    // Destroy consumer
281    mST.clear();
282
283    EXPECT_EQ(1, buffer->getStrongCount());
284}
285
286TEST_F(SurfaceTextureGLToGLTest, EglMakeCurrentAfterConsumerDeathUnrefsBuffers) {
287    sp<GraphicBuffer> buffer;
288
289    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
290            mProducerEglSurface, mProducerEglContext));
291
292    // Produce a frame
293    glClear(GL_COLOR_BUFFER_BIT);
294    EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
295    ASSERT_EQ(EGL_SUCCESS, eglGetError());
296
297    // Destroy the EGLSurface.
298    EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
299    ASSERT_EQ(EGL_SUCCESS, eglGetError());
300    mProducerEglSurface = EGL_NO_SURFACE;
301    mSTC.clear();
302    mANW.clear();
303    mTextureRenderer.clear();
304
305    // Consume a frame
306    ASSERT_EQ(NO_ERROR, mST->updateTexImage());
307    buffer = mST->getCurrentBuffer();
308
309    // Destroy the GL texture object to release its ref
310    GLuint texID = TEX_ID;
311    glDeleteTextures(1, &texID);
312
313    // Destroy consumer
314    mST.clear();
315
316    // make un-current, all references to buffer should be gone
317    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE,
318            EGL_NO_SURFACE, EGL_NO_CONTEXT));
319
320    EXPECT_EQ(1, buffer->getStrongCount());
321}
322
323TEST_F(SurfaceTextureGLToGLTest, TexturingFromUserSizedGLFilledBuffer) {
324    enum { texWidth = 64 };
325    enum { texHeight = 64 };
326
327    // This test requires 3 buffers to complete run on a single thread.
328    mST->setDefaultMaxBufferCount(3);
329
330    // Set the user buffer size.
331    native_window_set_buffers_user_dimensions(mANW.get(), texWidth, texHeight);
332
333    // Do the producer side of things
334    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
335            mProducerEglSurface, mProducerEglContext));
336    ASSERT_EQ(EGL_SUCCESS, eglGetError());
337
338    // This is needed to ensure we pick up a buffer of the correct size.
339    eglSwapBuffers(mEglDisplay, mProducerEglSurface);
340
341    glClearColor(0.6, 0.6, 0.6, 0.6);
342    glClear(GL_COLOR_BUFFER_BIT);
343
344    glEnable(GL_SCISSOR_TEST);
345    glScissor(4, 4, 1, 1);
346    glClearColor(1.0, 0.0, 0.0, 1.0);
347    glClear(GL_COLOR_BUFFER_BIT);
348
349    eglSwapBuffers(mEglDisplay, mProducerEglSurface);
350
351    // Do the consumer side of things
352    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
353            mEglContext));
354    ASSERT_EQ(EGL_SUCCESS, eglGetError());
355
356    glDisable(GL_SCISSOR_TEST);
357
358    // Skip the first frame, which was empty
359    ASSERT_EQ(NO_ERROR, mST->updateTexImage());
360    ASSERT_EQ(NO_ERROR, mST->updateTexImage());
361
362    glClearColor(0.2, 0.2, 0.2, 0.2);
363    glClear(GL_COLOR_BUFFER_BIT);
364
365    glViewport(0, 0, texWidth, texHeight);
366    drawTexture();
367
368    EXPECT_TRUE(checkPixel( 0,  0, 153, 153, 153, 153));
369    EXPECT_TRUE(checkPixel(63,  0, 153, 153, 153, 153));
370    EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
371    EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
372
373    EXPECT_TRUE(checkPixel( 4,  4, 255,   0,   0, 255));
374    EXPECT_TRUE(checkPixel( 5,  5, 153, 153, 153, 153));
375    EXPECT_TRUE(checkPixel( 3,  3, 153, 153, 153, 153));
376    EXPECT_TRUE(checkPixel(45, 52, 153, 153, 153, 153));
377    EXPECT_TRUE(checkPixel(12, 36, 153, 153, 153, 153));
378}
379
380TEST_F(SurfaceTextureGLToGLTest, TexturingFromPreRotatedUserSizedGLFilledBuffer) {
381    enum { texWidth = 64 };
382    enum { texHeight = 16 };
383
384    // This test requires 3 buffers to complete run on a single thread.
385    mST->setDefaultMaxBufferCount(3);
386
387    // Set the transform hint.
388    mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
389
390    // Set the user buffer size.
391    native_window_set_buffers_user_dimensions(mANW.get(), texWidth, texHeight);
392
393    // Do the producer side of things
394    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
395            mProducerEglSurface, mProducerEglContext));
396    ASSERT_EQ(EGL_SUCCESS, eglGetError());
397
398    // This is needed to ensure we pick up a buffer of the correct size and the
399    // new rotation hint.
400    eglSwapBuffers(mEglDisplay, mProducerEglSurface);
401
402    glClearColor(0.6, 0.6, 0.6, 0.6);
403    glClear(GL_COLOR_BUFFER_BIT);
404
405    glEnable(GL_SCISSOR_TEST);
406    glScissor(24, 4, 1, 1);
407    glClearColor(1.0, 0.0, 0.0, 1.0);
408    glClear(GL_COLOR_BUFFER_BIT);
409
410    eglSwapBuffers(mEglDisplay, mProducerEglSurface);
411
412    // Do the consumer side of things
413    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
414            mEglContext));
415    ASSERT_EQ(EGL_SUCCESS, eglGetError());
416
417    glDisable(GL_SCISSOR_TEST);
418
419    // Skip the first frame, which was empty
420    ASSERT_EQ(NO_ERROR, mST->updateTexImage());
421    ASSERT_EQ(NO_ERROR, mST->updateTexImage());
422
423    glClearColor(0.2, 0.2, 0.2, 0.2);
424    glClear(GL_COLOR_BUFFER_BIT);
425
426    glViewport(0, 0, texWidth, texHeight);
427    drawTexture();
428
429    EXPECT_TRUE(checkPixel( 0,  0, 153, 153, 153, 153));
430    EXPECT_TRUE(checkPixel(63,  0, 153, 153, 153, 153));
431    EXPECT_TRUE(checkPixel(63, 15, 153, 153, 153, 153));
432    EXPECT_TRUE(checkPixel( 0, 15, 153, 153, 153, 153));
433
434    EXPECT_TRUE(checkPixel(24,  4, 255,   0,   0, 255));
435    EXPECT_TRUE(checkPixel(25,  5, 153, 153, 153, 153));
436    EXPECT_TRUE(checkPixel(23,  3, 153, 153, 153, 153));
437    EXPECT_TRUE(checkPixel(45, 13, 153, 153, 153, 153));
438    EXPECT_TRUE(checkPixel(12,  8, 153, 153, 153, 153));
439}
440
441TEST_F(SurfaceTextureGLToGLTest, TexturingFromPreRotatedGLFilledBuffer) {
442    enum { texWidth = 64 };
443    enum { texHeight = 16 };
444
445    // This test requires 3 buffers to complete run on a single thread.
446    mST->setDefaultMaxBufferCount(3);
447
448    // Set the transform hint.
449    mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
450
451    // Set the default buffer size.
452    mST->setDefaultBufferSize(texWidth, texHeight);
453
454    // Do the producer side of things
455    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
456            mProducerEglSurface, mProducerEglContext));
457    ASSERT_EQ(EGL_SUCCESS, eglGetError());
458
459    // This is needed to ensure we pick up a buffer of the correct size and the
460    // new rotation hint.
461    eglSwapBuffers(mEglDisplay, mProducerEglSurface);
462
463    glClearColor(0.6, 0.6, 0.6, 0.6);
464    glClear(GL_COLOR_BUFFER_BIT);
465
466    glEnable(GL_SCISSOR_TEST);
467    glScissor(24, 4, 1, 1);
468    glClearColor(1.0, 0.0, 0.0, 1.0);
469    glClear(GL_COLOR_BUFFER_BIT);
470
471    eglSwapBuffers(mEglDisplay, mProducerEglSurface);
472
473    // Do the consumer side of things
474    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
475            mEglContext));
476    ASSERT_EQ(EGL_SUCCESS, eglGetError());
477
478    glDisable(GL_SCISSOR_TEST);
479
480    // Skip the first frame, which was empty
481    ASSERT_EQ(NO_ERROR, mST->updateTexImage());
482    ASSERT_EQ(NO_ERROR, mST->updateTexImage());
483
484    glClearColor(0.2, 0.2, 0.2, 0.2);
485    glClear(GL_COLOR_BUFFER_BIT);
486
487    glViewport(0, 0, texWidth, texHeight);
488    drawTexture();
489
490    EXPECT_TRUE(checkPixel( 0,  0, 153, 153, 153, 153));
491    EXPECT_TRUE(checkPixel(63,  0, 153, 153, 153, 153));
492    EXPECT_TRUE(checkPixel(63, 15, 153, 153, 153, 153));
493    EXPECT_TRUE(checkPixel( 0, 15, 153, 153, 153, 153));
494
495    EXPECT_TRUE(checkPixel(24,  4, 255,   0,   0, 255));
496    EXPECT_TRUE(checkPixel(25,  5, 153, 153, 153, 153));
497    EXPECT_TRUE(checkPixel(23,  3, 153, 153, 153, 153));
498    EXPECT_TRUE(checkPixel(45, 13, 153, 153, 153, 153));
499    EXPECT_TRUE(checkPixel(12,  8, 153, 153, 153, 153));
500}
501
502} // namespace android
503