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