blob: 0e85b90c3b3292cf63f32edb8decdb33f893b908 [file] [log] [blame]
Dan Stozacb1fcde2013-12-03 12:37:36 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SurfaceTextureGL_test"
18//#define LOG_NDEBUG 0
19
20#include "SurfaceTextureGL.h"
21
22#include "DisconnectWaiter.h"
23#include "FillBuffer.h"
24
25namespace android {
26
27TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferNpot) {
28 const int texWidth = 64;
29 const int texHeight = 66;
30
Dan Stozaf8cebe52015-04-20 12:09:38 -070031 ASSERT_EQ(NO_ERROR, native_window_set_buffers_dimensions(mANW.get(),
32 texWidth, texHeight));
33 ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(),
34 HAL_PIXEL_FORMAT_YV12));
Dan Stozacb1fcde2013-12-03 12:37:36 -080035 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
36 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
37
38 ANativeWindowBuffer* anb;
39 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
40 &anb));
41 ASSERT_TRUE(anb != NULL);
42
43 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
44
45 // Fill the buffer with the a checkerboard pattern
46 uint8_t* img = NULL;
47 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
48 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
49 buf->unlock();
50 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
51 -1));
52
53 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
54
55 glClearColor(0.2, 0.2, 0.2, 0.2);
56 glClear(GL_COLOR_BUFFER_BIT);
57
58 glViewport(0, 0, texWidth, texHeight);
59 drawTexture();
60
61 EXPECT_TRUE(checkPixel( 0, 0, 255, 127, 255, 255, 3));
62 EXPECT_TRUE(checkPixel(63, 0, 0, 133, 0, 255, 3));
63 EXPECT_TRUE(checkPixel(63, 65, 0, 133, 0, 255, 3));
64 EXPECT_TRUE(checkPixel( 0, 65, 255, 127, 255, 255, 3));
65
66 EXPECT_TRUE(checkPixel(22, 44, 255, 127, 255, 255, 3));
67 EXPECT_TRUE(checkPixel(45, 52, 255, 127, 255, 255, 3));
68 EXPECT_TRUE(checkPixel(52, 51, 98, 255, 73, 255, 3));
69 EXPECT_TRUE(checkPixel( 7, 31, 155, 0, 118, 255, 3));
70 EXPECT_TRUE(checkPixel(31, 9, 107, 24, 87, 255, 3));
71 EXPECT_TRUE(checkPixel(29, 35, 255, 127, 255, 255, 3));
72 EXPECT_TRUE(checkPixel(36, 22, 155, 29, 0, 255, 3));
73}
74
75TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferPow2) {
76 const int texWidth = 64;
77 const int texHeight = 64;
78
Dan Stozaf8cebe52015-04-20 12:09:38 -070079 ASSERT_EQ(NO_ERROR, native_window_set_buffers_dimensions(mANW.get(),
80 texWidth, texHeight));
81 ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(),
82 HAL_PIXEL_FORMAT_YV12));
Dan Stozacb1fcde2013-12-03 12:37:36 -080083 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
84 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
85
86 ANativeWindowBuffer* anb;
87 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
88 &anb));
89 ASSERT_TRUE(anb != NULL);
90
91 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
92
93 // Fill the buffer with the a checkerboard pattern
94 uint8_t* img = NULL;
95 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
96 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
97 buf->unlock();
98 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
99 -1));
100
101 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
102
103 glClearColor(0.2, 0.2, 0.2, 0.2);
104 glClear(GL_COLOR_BUFFER_BIT);
105
106 glViewport(0, 0, texWidth, texHeight);
107 drawTexture();
108
109 EXPECT_TRUE(checkPixel( 0, 0, 0, 133, 0, 255));
110 EXPECT_TRUE(checkPixel(63, 0, 255, 127, 255, 255));
111 EXPECT_TRUE(checkPixel(63, 63, 0, 133, 0, 255));
112 EXPECT_TRUE(checkPixel( 0, 63, 255, 127, 255, 255));
113
114 EXPECT_TRUE(checkPixel(22, 19, 100, 255, 74, 255));
115 EXPECT_TRUE(checkPixel(45, 11, 100, 255, 74, 255));
116 EXPECT_TRUE(checkPixel(52, 12, 155, 0, 181, 255));
117 EXPECT_TRUE(checkPixel( 7, 32, 150, 237, 170, 255));
118 EXPECT_TRUE(checkPixel(31, 54, 0, 71, 117, 255));
119 EXPECT_TRUE(checkPixel(29, 28, 0, 133, 0, 255));
120 EXPECT_TRUE(checkPixel(36, 41, 100, 232, 255, 255));
121}
122
123TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferWithCrop) {
124 const int texWidth = 64;
125 const int texHeight = 66;
126
Dan Stozaf8cebe52015-04-20 12:09:38 -0700127 ASSERT_EQ(NO_ERROR, native_window_set_buffers_dimensions(mANW.get(),
128 texWidth, texHeight));
129 ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(),
130 HAL_PIXEL_FORMAT_YV12));
Dan Stozacb1fcde2013-12-03 12:37:36 -0800131 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
132 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
133
134 android_native_rect_t crops[] = {
135 {4, 6, 22, 36},
136 {0, 6, 22, 36},
137 {4, 0, 22, 36},
138 {4, 6, texWidth, 36},
139 {4, 6, 22, texHeight},
140 };
141
142 for (int i = 0; i < 5; i++) {
143 const android_native_rect_t& crop(crops[i]);
144 SCOPED_TRACE(String8::format("rect{ l: %d t: %d r: %d b: %d }",
145 crop.left, crop.top, crop.right, crop.bottom).string());
146
147 ASSERT_EQ(NO_ERROR, native_window_set_crop(mANW.get(), &crop));
148
149 ANativeWindowBuffer* anb;
150 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
151 &anb));
152 ASSERT_TRUE(anb != NULL);
153
154 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
155
156 uint8_t* img = NULL;
157 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
158 fillYV12BufferRect(img, texWidth, texHeight, buf->getStride(), crop);
159 buf->unlock();
160 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(),
161 buf->getNativeBuffer(), -1));
162
163 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
164
165 glClearColor(0.2, 0.2, 0.2, 0.2);
166 glClear(GL_COLOR_BUFFER_BIT);
167
168 glViewport(0, 0, 64, 64);
169 drawTexture();
170
171 EXPECT_TRUE(checkPixel( 0, 0, 82, 255, 35, 255));
172 EXPECT_TRUE(checkPixel(63, 0, 82, 255, 35, 255));
173 EXPECT_TRUE(checkPixel(63, 63, 82, 255, 35, 255));
174 EXPECT_TRUE(checkPixel( 0, 63, 82, 255, 35, 255));
175
176 EXPECT_TRUE(checkPixel(25, 14, 82, 255, 35, 255));
177 EXPECT_TRUE(checkPixel(35, 31, 82, 255, 35, 255));
178 EXPECT_TRUE(checkPixel(57, 6, 82, 255, 35, 255));
179 EXPECT_TRUE(checkPixel( 5, 42, 82, 255, 35, 255));
180 EXPECT_TRUE(checkPixel(32, 33, 82, 255, 35, 255));
181 EXPECT_TRUE(checkPixel(16, 26, 82, 255, 35, 255));
182 EXPECT_TRUE(checkPixel(46, 51, 82, 255, 35, 255));
183 }
184}
185
186// This test is intended to catch synchronization bugs between the CPU-written
187// and GPU-read buffers.
188TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BuffersRepeatedly) {
189 enum { texWidth = 16 };
190 enum { texHeight = 16 };
191 enum { numFrames = 1024 };
192
Dan Stozaf8cebe52015-04-20 12:09:38 -0700193 ASSERT_EQ(NO_ERROR, native_window_set_buffers_dimensions(mANW.get(),
194 texWidth, texHeight));
195 ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(),
196 HAL_PIXEL_FORMAT_YV12));
Dan Stozacb1fcde2013-12-03 12:37:36 -0800197 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
198 GRALLOC_USAGE_SW_WRITE_OFTEN));
199
200 struct TestPixel {
201 int x;
202 int y;
203 };
204 const TestPixel testPixels[] = {
205 { 4, 11 },
206 { 12, 14 },
207 { 7, 2 },
208 };
209 enum {numTestPixels = sizeof(testPixels) / sizeof(testPixels[0])};
210
211 class ProducerThread : public Thread {
212 public:
213 ProducerThread(const sp<ANativeWindow>& anw,
214 const TestPixel* testPixels):
215 mANW(anw),
216 mTestPixels(testPixels) {
217 }
218
219 virtual ~ProducerThread() {
220 }
221
222 virtual bool threadLoop() {
223 for (int i = 0; i < numFrames; i++) {
224 ANativeWindowBuffer* anb;
225 if (native_window_dequeue_buffer_and_wait(mANW.get(),
226 &anb) != NO_ERROR) {
227 return false;
228 }
229 if (anb == NULL) {
230 return false;
231 }
232
233 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
234
235 const int yuvTexOffsetY = 0;
236 int stride = buf->getStride();
237 int yuvTexStrideY = stride;
238 int yuvTexOffsetV = yuvTexStrideY * texHeight;
239 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
240 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * texHeight/2;
241 int yuvTexStrideU = yuvTexStrideV;
242
243 uint8_t* img = NULL;
244 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
245
246 // Gray out all the test pixels first, so we're more likely to
247 // see a failure if GL is still texturing from the buffer we
248 // just dequeued.
249 for (int j = 0; j < numTestPixels; j++) {
250 int x = mTestPixels[j].x;
251 int y = mTestPixels[j].y;
252 uint8_t value = 128;
253 img[y*stride + x] = value;
254 }
255
256 // Fill the buffer with gray.
257 for (int y = 0; y < texHeight; y++) {
258 for (int x = 0; x < texWidth; x++) {
259 img[yuvTexOffsetY + y*yuvTexStrideY + x] = 128;
260 img[yuvTexOffsetU + (y/2)*yuvTexStrideU + x/2] = 128;
261 img[yuvTexOffsetV + (y/2)*yuvTexStrideV + x/2] = 128;
262 }
263 }
264
265 // Set the test pixels to either white or black.
266 for (int j = 0; j < numTestPixels; j++) {
267 int x = mTestPixels[j].x;
268 int y = mTestPixels[j].y;
269 uint8_t value = 0;
270 if (j == (i % numTestPixels)) {
271 value = 255;
272 }
273 img[y*stride + x] = value;
274 }
275
276 buf->unlock();
277 if (mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(), -1)
278 != NO_ERROR) {
279 return false;
280 }
281 }
282 return false;
283 }
284
285 sp<ANativeWindow> mANW;
286 const TestPixel* mTestPixels;
287 };
288
289 sp<Thread> pt(new ProducerThread(mANW, testPixels));
290 pt->run();
291
292 glViewport(0, 0, texWidth, texHeight);
293
294 glClearColor(0.2, 0.2, 0.2, 0.2);
295 glClear(GL_COLOR_BUFFER_BIT);
296
297 // We wait for the first two frames up front so that the producer will be
298 // likely to dequeue the buffer that's currently being textured from.
299 mFW->waitForFrame();
300 mFW->waitForFrame();
301
302 for (int i = 0; i < numFrames; i++) {
303 SCOPED_TRACE(String8::format("frame %d", i).string());
304
305 // We must wait for each frame to come in because if we ever do an
306 // updateTexImage call that doesn't consume a newly available buffer
307 // then the producer and consumer will get out of sync, which will cause
308 // a deadlock.
309 if (i > 1) {
310 mFW->waitForFrame();
311 }
312 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
313 drawTexture();
314
315 for (int j = 0; j < numTestPixels; j++) {
316 int x = testPixels[j].x;
317 int y = testPixels[j].y;
318 uint8_t value = 0;
319 if (j == (i % numTestPixels)) {
320 // We must y-invert the texture coords
321 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 255, 255, 255, 255));
322 } else {
323 // We must y-invert the texture coords
324 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 0, 0, 0, 255));
325 }
326 }
327 }
328
329 pt->requestExitAndWait();
330}
331
332TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferNpot) {
333 const int texWidth = 64;
334 const int texHeight = 66;
335
Dan Stozaf8cebe52015-04-20 12:09:38 -0700336 ASSERT_EQ(NO_ERROR, native_window_set_buffers_dimensions(mANW.get(),
337 texWidth, texHeight));
338 ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(),
339 HAL_PIXEL_FORMAT_RGBA_8888));
Dan Stozacb1fcde2013-12-03 12:37:36 -0800340 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
341 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
342
343 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
344
345 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
346
347 glClearColor(0.2, 0.2, 0.2, 0.2);
348 glClear(GL_COLOR_BUFFER_BIT);
349
350 glViewport(0, 0, texWidth, texHeight);
351 drawTexture();
352
353 EXPECT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
354 EXPECT_TRUE(checkPixel(63, 0, 231, 231, 231, 231));
355 EXPECT_TRUE(checkPixel(63, 65, 231, 231, 231, 231));
356 EXPECT_TRUE(checkPixel( 0, 65, 35, 35, 35, 35));
357
358 EXPECT_TRUE(checkPixel(15, 10, 35, 231, 231, 231));
359 EXPECT_TRUE(checkPixel(23, 65, 231, 35, 231, 35));
360 EXPECT_TRUE(checkPixel(19, 40, 35, 231, 35, 35));
361 EXPECT_TRUE(checkPixel(38, 30, 231, 35, 35, 35));
362 EXPECT_TRUE(checkPixel(42, 54, 35, 35, 35, 231));
363 EXPECT_TRUE(checkPixel(37, 34, 35, 231, 231, 231));
364 EXPECT_TRUE(checkPixel(31, 8, 231, 35, 35, 231));
365 EXPECT_TRUE(checkPixel(37, 47, 231, 35, 231, 231));
366 EXPECT_TRUE(checkPixel(25, 38, 35, 35, 35, 35));
367 EXPECT_TRUE(checkPixel(49, 6, 35, 231, 35, 35));
368 EXPECT_TRUE(checkPixel(54, 50, 35, 231, 231, 231));
369 EXPECT_TRUE(checkPixel(27, 26, 231, 231, 231, 231));
370 EXPECT_TRUE(checkPixel(10, 6, 35, 35, 231, 231));
371 EXPECT_TRUE(checkPixel(29, 4, 35, 35, 35, 231));
372 EXPECT_TRUE(checkPixel(55, 28, 35, 35, 231, 35));
373 EXPECT_TRUE(checkPixel(58, 55, 35, 35, 231, 231));
374}
375
376TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferPow2) {
377 const int texWidth = 64;
378 const int texHeight = 64;
379
Dan Stozaf8cebe52015-04-20 12:09:38 -0700380 ASSERT_EQ(NO_ERROR, native_window_set_buffers_dimensions(mANW.get(),
381 texWidth, texHeight));
382 ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(),
383 HAL_PIXEL_FORMAT_RGBA_8888));
Dan Stozacb1fcde2013-12-03 12:37:36 -0800384 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
385 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
386
387 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
388
389 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
390
391 glClearColor(0.2, 0.2, 0.2, 0.2);
392 glClear(GL_COLOR_BUFFER_BIT);
393
394 glViewport(0, 0, texWidth, texHeight);
395 drawTexture();
396
397 EXPECT_TRUE(checkPixel( 0, 0, 231, 231, 231, 231));
398 EXPECT_TRUE(checkPixel(63, 0, 35, 35, 35, 35));
399 EXPECT_TRUE(checkPixel(63, 63, 231, 231, 231, 231));
400 EXPECT_TRUE(checkPixel( 0, 63, 35, 35, 35, 35));
401
402 EXPECT_TRUE(checkPixel(12, 46, 231, 231, 231, 35));
403 EXPECT_TRUE(checkPixel(16, 1, 231, 231, 35, 231));
404 EXPECT_TRUE(checkPixel(21, 12, 231, 35, 35, 231));
405 EXPECT_TRUE(checkPixel(26, 51, 231, 35, 231, 35));
406 EXPECT_TRUE(checkPixel( 5, 32, 35, 231, 231, 35));
407 EXPECT_TRUE(checkPixel(13, 8, 35, 231, 231, 231));
408 EXPECT_TRUE(checkPixel(46, 3, 35, 35, 231, 35));
409 EXPECT_TRUE(checkPixel(30, 33, 35, 35, 35, 35));
410 EXPECT_TRUE(checkPixel( 6, 52, 231, 231, 35, 35));
411 EXPECT_TRUE(checkPixel(55, 33, 35, 231, 35, 231));
412 EXPECT_TRUE(checkPixel(16, 29, 35, 35, 231, 231));
413 EXPECT_TRUE(checkPixel( 1, 30, 35, 35, 35, 231));
414 EXPECT_TRUE(checkPixel(41, 37, 35, 35, 231, 231));
415 EXPECT_TRUE(checkPixel(46, 29, 231, 231, 35, 35));
416 EXPECT_TRUE(checkPixel(15, 25, 35, 231, 35, 231));
417 EXPECT_TRUE(checkPixel( 3, 52, 35, 231, 35, 35));
418}
419
420// Tests if GLConsumer and BufferQueue are robust enough
421// to handle a special case where updateTexImage is called
422// in the middle of disconnect. This ordering is enforced
423// by blocking in the disconnect callback.
424TEST_F(SurfaceTextureGLTest, DisconnectStressTest) {
425
426 class ProducerThread : public Thread {
427 public:
428 ProducerThread(const sp<ANativeWindow>& anw):
429 mANW(anw) {
430 }
431
432 virtual ~ProducerThread() {
433 }
434
435 virtual bool threadLoop() {
436 ANativeWindowBuffer* anb;
437
438 native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_EGL);
439
440 for (int numFrames =0 ; numFrames < 2; numFrames ++) {
441
442 if (native_window_dequeue_buffer_and_wait(mANW.get(),
443 &anb) != NO_ERROR) {
444 return false;
445 }
446 if (anb == NULL) {
447 return false;
448 }
449 if (mANW->queueBuffer(mANW.get(), anb, -1)
450 != NO_ERROR) {
451 return false;
452 }
453 }
454
455 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_EGL);
456
457 return false;
458 }
459
460 private:
461 sp<ANativeWindow> mANW;
462 };
463
464 sp<DisconnectWaiter> dw(new DisconnectWaiter());
Dan Stoza5603a2f2014-04-07 13:41:37 -0700465 mConsumer->consumerConnect(dw, false);
Dan Stozacb1fcde2013-12-03 12:37:36 -0800466
467
468 sp<Thread> pt(new ProducerThread(mANW));
469 pt->run();
470
471 // eat a frame so GLConsumer will own an at least one slot
472 dw->waitForFrame();
473 EXPECT_EQ(OK,mST->updateTexImage());
474
475 dw->waitForFrame();
476 // Could fail here as GLConsumer thinks it still owns the slot
477 // but bufferQueue has released all slots
478 EXPECT_EQ(OK,mST->updateTexImage());
479
480 dw->finishDisconnect();
481}
482
483
484// This test ensures that the GLConsumer clears the mCurrentTexture
485// when it is disconnected and reconnected. Otherwise it will
486// attempt to release a buffer that it does not owned
487TEST_F(SurfaceTextureGLTest, DisconnectClearsCurrentTexture) {
488 ASSERT_EQ(OK, native_window_api_connect(mANW.get(),
489 NATIVE_WINDOW_API_EGL));
490
491 ANativeWindowBuffer *anb;
492
493 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
494 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
495
496 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
497 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
498
499 EXPECT_EQ(OK,mST->updateTexImage());
500 EXPECT_EQ(OK,mST->updateTexImage());
501
502 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(),
503 NATIVE_WINDOW_API_EGL));
504 ASSERT_EQ(OK, native_window_api_connect(mANW.get(),
505 NATIVE_WINDOW_API_EGL));
506
507 EXPECT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
508 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
509
510 // Will fail here if mCurrentTexture is not cleared properly
511 mFW->waitForFrame();
512 EXPECT_EQ(OK,mST->updateTexImage());
513
514 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(),
515 NATIVE_WINDOW_API_EGL));
516}
517
518TEST_F(SurfaceTextureGLTest, ScaleToWindowMode) {
519 ASSERT_EQ(OK, native_window_set_scaling_mode(mANW.get(),
520 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW));
521
522 // The producer image size
523 ASSERT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 512, 512));
524
525 // The consumer image size (16 x 9) ratio
526 mST->setDefaultBufferSize(1280, 720);
527
528 ASSERT_EQ(OK, native_window_api_connect(mANW.get(),
529 NATIVE_WINDOW_API_CPU));
530
531 ANativeWindowBuffer *anb;
532
533 android_native_rect_t odd = {23, 78, 123, 477};
534 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &odd));
535 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
536 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
537 mFW->waitForFrame();
538 EXPECT_EQ(OK, mST->updateTexImage());
539 Rect r = mST->getCurrentCrop();
540 assertRectEq(Rect(23, 78, 123, 477), r);
541
542 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(),
543 NATIVE_WINDOW_API_CPU));
544}
545
546// This test ensures the scaling mode does the right thing
547// ie NATIVE_WINDOW_SCALING_MODE_CROP should crop
548// the image such that it has the same aspect ratio as the
549// default buffer size
550TEST_F(SurfaceTextureGLTest, CroppedScalingMode) {
551 ASSERT_EQ(OK, native_window_set_scaling_mode(mANW.get(),
552 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP));
553
554 // The producer image size
555 ASSERT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 512, 512));
556
557 // The consumer image size (16 x 9) ratio
558 mST->setDefaultBufferSize(1280, 720);
559
560 native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU);
561
562 ANativeWindowBuffer *anb;
563
564 // The crop is in the shape of (320, 180) === 16 x 9
565 android_native_rect_t standard = {10, 20, 330, 200};
566 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &standard));
567 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
568 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
569 mFW->waitForFrame();
570 EXPECT_EQ(OK, mST->updateTexImage());
571 Rect r = mST->getCurrentCrop();
572 // crop should be the same as crop (same aspect ratio)
573 assertRectEq(Rect(10, 20, 330, 200), r);
574
575 // make this wider then desired aspect 239 x 100 (2.39:1)
576 android_native_rect_t wide = {20, 30, 259, 130};
577 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &wide));
578 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
579 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
580 mFW->waitForFrame();
581 EXPECT_EQ(OK, mST->updateTexImage());
582 r = mST->getCurrentCrop();
583 // crop should be the same height, but have cropped left and right borders
584 // offset is 30.6 px L+, R-
585 assertRectEq(Rect(51, 30, 228, 130), r);
586
587 // This image is taller then desired aspect 400 x 300 (4:3)
588 android_native_rect_t narrow = {0, 0, 400, 300};
589 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &narrow));
590 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
591 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
592 mFW->waitForFrame();
593 EXPECT_EQ(OK, mST->updateTexImage());
594 r = mST->getCurrentCrop();
595 // crop should be the same width, but have cropped top and bottom borders
596 // offset is 37.5 px
597 assertRectEq(Rect(0, 37, 400, 262), r);
598
599 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
600}
601
602TEST_F(SurfaceTextureGLTest, AbandonUnblocksDequeueBuffer) {
603 class ProducerThread : public Thread {
604 public:
605 ProducerThread(const sp<ANativeWindow>& anw):
606 mANW(anw),
607 mDequeueError(NO_ERROR) {
608 }
609
610 virtual ~ProducerThread() {
611 }
612
613 virtual bool threadLoop() {
614 Mutex::Autolock lock(mMutex);
615 ANativeWindowBuffer* anb;
616
617 // Frame 1
618 if (native_window_dequeue_buffer_and_wait(mANW.get(),
619 &anb) != NO_ERROR) {
620 return false;
621 }
622 if (anb == NULL) {
623 return false;
624 }
625 if (mANW->queueBuffer(mANW.get(), anb, -1)
626 != NO_ERROR) {
627 return false;
628 }
629
630 // Frame 2
631 if (native_window_dequeue_buffer_and_wait(mANW.get(),
632 &anb) != NO_ERROR) {
633 return false;
634 }
635 if (anb == NULL) {
636 return false;
637 }
638 if (mANW->queueBuffer(mANW.get(), anb, -1)
639 != NO_ERROR) {
640 return false;
641 }
642
643 // Frame 3 - error expected
644 mDequeueError = native_window_dequeue_buffer_and_wait(mANW.get(),
645 &anb);
646 return false;
647 }
648
649 status_t getDequeueError() {
650 Mutex::Autolock lock(mMutex);
651 return mDequeueError;
652 }
653
654 private:
655 sp<ANativeWindow> mANW;
656 status_t mDequeueError;
657 Mutex mMutex;
658 };
659
Dan Stozacb1fcde2013-12-03 12:37:36 -0800660 sp<Thread> pt(new ProducerThread(mANW));
661 pt->run();
662
663 mFW->waitForFrame();
664 mFW->waitForFrame();
665
666 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
667 // block waiting for a buffer to become available.
668 usleep(100000);
669
670 mST->abandon();
671
672 pt->requestExitAndWait();
673 ASSERT_EQ(NO_INIT,
674 reinterpret_cast<ProducerThread*>(pt.get())->getDequeueError());
675}
676
677TEST_F(SurfaceTextureGLTest, InvalidWidthOrHeightFails) {
678 int texHeight = 16;
679 ANativeWindowBuffer* anb;
680
681 GLint maxTextureSize;
682 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
683
684 // make sure it works with small textures
685 mST->setDefaultBufferSize(16, texHeight);
686 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
687 &anb));
688 EXPECT_EQ(16, anb->width);
689 EXPECT_EQ(texHeight, anb->height);
690 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1));
691 EXPECT_EQ(NO_ERROR, mST->updateTexImage());
692
693 // make sure it works with GL_MAX_TEXTURE_SIZE
694 mST->setDefaultBufferSize(maxTextureSize, texHeight);
695 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
696 &anb));
697 EXPECT_EQ(maxTextureSize, anb->width);
698 EXPECT_EQ(texHeight, anb->height);
699 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1));
700 EXPECT_EQ(NO_ERROR, mST->updateTexImage());
701
702 // make sure it fails with GL_MAX_TEXTURE_SIZE+1
703 mST->setDefaultBufferSize(maxTextureSize+1, texHeight);
704 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
705 &anb));
706 EXPECT_EQ(maxTextureSize+1, anb->width);
707 EXPECT_EQ(texHeight, anb->height);
708 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1));
709 ASSERT_NE(NO_ERROR, mST->updateTexImage());
710}
711
712} // namespace android