1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
|
//
// Copyright 2016 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// EGLStreamTest:
// Tests pertaining to egl::Stream.
//
#include <gtest/gtest.h>
#include <vector>
#include "media/yuvtest.inl"
#include "OSWindow.h"
#include "test_utils/ANGLETest.h"
using namespace angle;
namespace
{
class EGLStreamTest : public ANGLETest
{
protected:
EGLStreamTest()
{
setWindowWidth(128);
setWindowHeight(128);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
setConfigDepthBits(24);
}
};
// Tests validation of the stream API
TEST_P(EGLStreamTest, StreamValidationTest)
{
EGLWindow *window = getEGLWindow();
EGLDisplay display = window->getDisplay();
const char *extensionsString = eglQueryString(display, EGL_EXTENSIONS);
if (strstr(extensionsString, "EGL_KHR_stream") == nullptr)
{
std::cout << "Stream extension not supported" << std::endl;
return;
}
const EGLint streamAttributesBad[] = {
EGL_STREAM_STATE_KHR,
0,
EGL_NONE,
EGL_PRODUCER_FRAME_KHR,
0,
EGL_NONE,
EGL_CONSUMER_FRAME_KHR,
0,
EGL_NONE,
EGL_CONSUMER_LATENCY_USEC_KHR,
-1,
EGL_NONE,
EGL_RED_SIZE,
EGL_DONT_CARE,
EGL_NONE,
};
// Validate create stream attributes
EGLStreamKHR stream = eglCreateStreamKHR(display, &streamAttributesBad[0]);
ASSERT_EGL_ERROR(EGL_BAD_ACCESS);
ASSERT_EQ(EGL_NO_STREAM_KHR, stream);
stream = eglCreateStreamKHR(display, &streamAttributesBad[3]);
ASSERT_EGL_ERROR(EGL_BAD_ACCESS);
ASSERT_EQ(EGL_NO_STREAM_KHR, stream);
stream = eglCreateStreamKHR(display, &streamAttributesBad[6]);
ASSERT_EGL_ERROR(EGL_BAD_ACCESS);
ASSERT_EQ(EGL_NO_STREAM_KHR, stream);
stream = eglCreateStreamKHR(display, &streamAttributesBad[9]);
ASSERT_EGL_ERROR(EGL_BAD_PARAMETER);
ASSERT_EQ(EGL_NO_STREAM_KHR, stream);
stream = eglCreateStreamKHR(display, &streamAttributesBad[12]);
ASSERT_EGL_ERROR(EGL_BAD_ATTRIBUTE);
ASSERT_EQ(EGL_NO_STREAM_KHR, stream);
const EGLint streamAttributes[] = {
EGL_CONSUMER_LATENCY_USEC_KHR, 0, EGL_NONE,
};
stream = eglCreateStreamKHR(EGL_NO_DISPLAY, streamAttributes);
ASSERT_EGL_ERROR(EGL_BAD_DISPLAY);
ASSERT_EQ(EGL_NO_STREAM_KHR, stream);
// Create an actual stream
stream = eglCreateStreamKHR(display, streamAttributes);
ASSERT_EGL_SUCCESS();
ASSERT_NE(EGL_NO_STREAM_KHR, stream);
// Assert it is in the created state
EGLint state;
eglQueryStreamKHR(display, stream, EGL_STREAM_STATE_KHR, &state);
ASSERT_EGL_SUCCESS();
ASSERT_EQ(EGL_STREAM_STATE_CREATED_KHR, state);
// Test getting and setting the latency
EGLint latency = 10;
eglStreamAttribKHR(display, stream, EGL_CONSUMER_LATENCY_USEC_KHR, latency);
ASSERT_EGL_SUCCESS();
eglQueryStreamKHR(display, stream, EGL_CONSUMER_LATENCY_USEC_KHR, &latency);
ASSERT_EGL_SUCCESS();
ASSERT_EQ(10, latency);
eglStreamAttribKHR(display, stream, EGL_CONSUMER_LATENCY_USEC_KHR, -1);
ASSERT_EGL_ERROR(EGL_BAD_PARAMETER);
ASSERT_EQ(10, latency);
// Test the 64-bit queries
EGLuint64KHR value;
eglQueryStreamu64KHR(display, stream, EGL_CONSUMER_FRAME_KHR, &value);
ASSERT_EGL_SUCCESS();
eglQueryStreamu64KHR(display, stream, EGL_PRODUCER_FRAME_KHR, &value);
ASSERT_EGL_SUCCESS();
// Destroy the stream
eglDestroyStreamKHR(display, stream);
ASSERT_EGL_SUCCESS();
}
// Tests validation of stream consumer gltexture API
TEST_P(EGLStreamTest, StreamConsumerGLTextureValidationTest)
{
EGLWindow *window = getEGLWindow();
EGLDisplay display = window->getDisplay();
const char *extensionsString = eglQueryString(display, EGL_EXTENSIONS);
if (strstr(extensionsString, "EGL_KHR_stream_consumer_gltexture") == nullptr)
{
std::cout << "Stream consumer gltexture extension not supported" << std::endl;
return;
}
const EGLint streamAttributes[] = {
EGL_CONSUMER_LATENCY_USEC_KHR, 0, EGL_CONSUMER_ACQUIRE_TIMEOUT_USEC_KHR, 0, EGL_NONE,
};
EGLStreamKHR stream = eglCreateStreamKHR(display, streamAttributes);
ASSERT_EGL_SUCCESS();
EGLBoolean result = eglStreamConsumerGLTextureExternalKHR(display, stream);
ASSERT_EGL_FALSE(result);
ASSERT_EGL_ERROR(EGL_BAD_ACCESS);
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex);
result = eglStreamConsumerGLTextureExternalKHR(display, stream);
ASSERT_EGL_TRUE(result);
ASSERT_EGL_SUCCESS();
EGLint state;
eglQueryStreamKHR(display, stream, EGL_STREAM_STATE_KHR, &state);
ASSERT_EGL_SUCCESS();
ASSERT_EQ(EGL_STREAM_STATE_CONNECTING_KHR, state);
eglDestroyStreamKHR(display, stream);
ASSERT_EGL_SUCCESS();
}
// Tests validation of stream consumer gltexture yuv API
TEST_P(EGLStreamTest, StreamConsumerGLTextureYUVValidationTest)
{
EGLWindow *window = getEGLWindow();
EGLDisplay display = window->getDisplay();
const char *extensionsString = eglQueryString(display, EGL_EXTENSIONS);
if (strstr(extensionsString, "EGL_NV_stream_consumer_gltexture_yuv") == nullptr)
{
std::cout << "Stream consumer gltexture yuv extension not supported" << std::endl;
return;
}
const EGLint streamAttributes[] = {
EGL_CONSUMER_LATENCY_USEC_KHR, 0, EGL_CONSUMER_ACQUIRE_TIMEOUT_USEC_KHR, 0, EGL_NONE,
};
EGLStreamKHR stream = eglCreateStreamKHR(display, streamAttributes);
ASSERT_EGL_SUCCESS();
EGLAttrib consumerAttributesBad[] = {
EGL_COLOR_BUFFER_TYPE,
EGL_YUV_BUFFER_EXT, // 0
EGL_YUV_NUMBER_OF_PLANES_EXT,
0,
EGL_NONE,
EGL_COLOR_BUFFER_TYPE,
EGL_YUV_BUFFER_EXT, // 5
EGL_YUV_NUMBER_OF_PLANES_EXT,
1,
EGL_NONE,
EGL_COLOR_BUFFER_TYPE,
EGL_YUV_BUFFER_EXT, // 10
EGL_YUV_NUMBER_OF_PLANES_EXT,
1,
EGL_YUV_PLANE0_TEXTURE_UNIT_NV,
9999,
EGL_NONE,
EGL_COLOR_BUFFER_TYPE,
EGL_YUV_BUFFER_EXT, // 17
EGL_YUV_NUMBER_OF_PLANES_EXT,
1,
EGL_YUV_PLANE0_TEXTURE_UNIT_NV,
0,
EGL_YUV_PLANE1_TEXTURE_UNIT_NV,
1,
EGL_NONE,
EGL_COLOR_BUFFER_TYPE,
EGL_YUV_BUFFER_EXT, // 26
EGL_YUV_NUMBER_OF_PLANES_EXT,
2,
EGL_YUV_PLANE0_TEXTURE_UNIT_NV,
0,
EGL_YUV_PLANE1_TEXTURE_UNIT_NV,
0,
EGL_NONE,
};
EGLAttrib consumerAttributes[] = {
EGL_COLOR_BUFFER_TYPE,
EGL_YUV_BUFFER_EXT,
EGL_YUV_NUMBER_OF_PLANES_EXT,
2,
EGL_YUV_PLANE0_TEXTURE_UNIT_NV,
0,
EGL_YUV_PLANE1_TEXTURE_UNIT_NV,
1,
EGL_NONE,
};
EGLBoolean result =
eglStreamConsumerGLTextureExternalAttribsNV(display, stream, &consumerAttributesBad[0]);
ASSERT_EGL_FALSE(result);
ASSERT_EGL_ERROR(EGL_BAD_MATCH);
result =
eglStreamConsumerGLTextureExternalAttribsNV(display, stream, &consumerAttributesBad[5]);
ASSERT_EGL_FALSE(result);
ASSERT_EGL_ERROR(EGL_BAD_MATCH);
result =
eglStreamConsumerGLTextureExternalAttribsNV(display, stream, &consumerAttributesBad[10]);
ASSERT_EGL_FALSE(result);
ASSERT_EGL_ERROR(EGL_BAD_ACCESS);
result =
eglStreamConsumerGLTextureExternalAttribsNV(display, stream, &consumerAttributesBad[17]);
ASSERT_EGL_FALSE(result);
ASSERT_EGL_ERROR(EGL_BAD_MATCH);
result = eglStreamConsumerGLTextureExternalAttribsNV(display, stream, consumerAttributes);
ASSERT_EGL_FALSE(result);
ASSERT_EGL_ERROR(EGL_BAD_ACCESS);
GLuint tex[2];
glGenTextures(2, tex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex[0]);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex[1]);
result =
eglStreamConsumerGLTextureExternalAttribsNV(display, stream, &consumerAttributesBad[26]);
ASSERT_EGL_FALSE(result);
ASSERT_EGL_ERROR(EGL_BAD_ACCESS);
result = eglStreamConsumerGLTextureExternalAttribsNV(display, stream, consumerAttributes);
ASSERT_EGL_TRUE(result);
ASSERT_EGL_SUCCESS();
EGLint state;
eglQueryStreamKHR(display, stream, EGL_STREAM_STATE_KHR, &state);
ASSERT_EGL_SUCCESS();
ASSERT_EQ(EGL_STREAM_STATE_CONNECTING_KHR, state);
eglDestroyStreamKHR(display, stream);
ASSERT_EGL_SUCCESS();
}
// Tests that deleting a texture invalidates the associated stream
TEST_P(EGLStreamTest, StreamConsumerGLTextureYUVDeletionTest)
{
EGLWindow *window = getEGLWindow();
EGLDisplay display = window->getDisplay();
const char *extensionsString = eglQueryString(display, EGL_EXTENSIONS);
if (strstr(extensionsString, "EGL_ANGLE_stream_producer_d3d_texture_nv12") == nullptr)
{
std::cout << "Stream producer d3d nv12 texture not supported" << std::endl;
return;
}
const EGLint streamAttributes[] = {
EGL_CONSUMER_LATENCY_USEC_KHR, 0, EGL_CONSUMER_ACQUIRE_TIMEOUT_USEC_KHR, 0, EGL_NONE,
};
EGLStreamKHR stream = eglCreateStreamKHR(display, streamAttributes);
ASSERT_EGL_SUCCESS();
EGLAttrib consumerAttributes[] = {
EGL_COLOR_BUFFER_TYPE,
EGL_YUV_BUFFER_EXT,
EGL_YUV_NUMBER_OF_PLANES_EXT,
2,
EGL_YUV_PLANE0_TEXTURE_UNIT_NV,
0,
EGL_YUV_PLANE1_TEXTURE_UNIT_NV,
1,
EGL_NONE,
};
GLuint tex[2];
glGenTextures(2, tex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex[0]);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex[1]);
EGLBoolean result =
eglStreamConsumerGLTextureExternalAttribsNV(display, stream, consumerAttributes);
ASSERT_EGL_TRUE(result);
ASSERT_EGL_SUCCESS();
EGLAttrib producerAttributes[] = {
EGL_NONE,
};
result = eglCreateStreamProducerD3DTextureNV12ANGLE(display, stream, producerAttributes);
ASSERT_EGL_TRUE(result);
ASSERT_EGL_SUCCESS();
EGLint state;
eglQueryStreamKHR(display, stream, EGL_STREAM_STATE_KHR, &state);
ASSERT_EGL_SUCCESS();
ASSERT_EQ(EGL_STREAM_STATE_EMPTY_KHR, state);
// Delete the first texture, which should be enough to invalidate the stream
glDeleteTextures(1, tex);
eglQueryStreamKHR(display, stream, EGL_STREAM_STATE_KHR, &state);
ASSERT_EGL_SUCCESS();
ASSERT_EQ(EGL_STREAM_STATE_DISCONNECTED_KHR, state);
eglDestroyStreamKHR(display, stream);
ASSERT_EGL_SUCCESS();
}
// End2end test for rendering an NV12 texture. Renders a YUV quad, reads back the RGB values, and
// ensures they are correct
TEST_P(EGLStreamTest, StreamProducerTextureNV12End2End)
{
EGLWindow *window = getEGLWindow();
EGLDisplay display = window->getDisplay();
if (!eglDisplayExtensionEnabled(display, "EGL_ANGLE_stream_producer_d3d_texture_nv12"))
{
std::cout << "Stream producer d3d nv12 texture not supported" << std::endl;
return;
}
bool useESSL3Shaders =
getClientMajorVersion() >= 3 && extensionEnabled("GL_OES_EGL_image_external_essl3");
// yuv to rgb conversion shader using Microsoft's given conversion formulas
std::string yuvVS, yuvPS;
if (useESSL3Shaders)
{
yuvVS =
"#version 300 es\n"
"in highp vec4 position;\n"
"out vec2 texcoord;\n"
"void main(void)\n"
"{\n"
" gl_Position = position;\n"
" texcoord = (position.xy * 0.5) + 0.5;\n"
" texcoord.y = 1.0 - texcoord.y;\n"
"}\n";
yuvPS =
"#version 300 es\n"
"#extension GL_OES_EGL_image_external_essl3 : require\n"
"#extension GL_NV_EGL_stream_consumer_external : require\n"
"precision highp float;\n"
"in vec2 texcoord;\n"
"out vec4 color;\n"
"uniform samplerExternalOES y;\n"
"uniform samplerExternalOES uv\n;"
"void main(void)\n"
"{\n"
" float c = texture(y, texcoord).r - (16.0 / 256.0);\n"
" float d = texture(uv, texcoord).r - 0.5;\n"
" float e = texture(uv, texcoord).g - 0.5;\n"
" float r = 1.164383 * c + 1.596027 * e;\n"
" float g = 1.164383 * c - 0.391762 * d - 0.812968 * e;\n"
" float b = 1.164383 * c + 2.017232 * d;\n"
" color = vec4(r, g, b, 1.0);\n"
"}\n";
}
else
{
yuvVS =
"attribute highp vec4 position;\n"
"varying vec2 texcoord;\n"
"void main(void)\n"
"{\n"
" gl_Position = position;\n"
" texcoord = (position.xy * 0.5) + 0.5;\n"
" texcoord.y = 1.0 - texcoord.y;\n"
"}\n";
yuvPS =
"#extension GL_NV_EGL_stream_consumer_external : require\n"
"precision highp float;\n"
"varying vec2 texcoord;\n"
"uniform samplerExternalOES y;\n"
"uniform samplerExternalOES uv\n;"
"void main(void)\n"
"{\n"
" float c = texture2D(y, texcoord).r - (16.0 / 256.0);\n"
" float d = texture2D(uv, texcoord).r - 0.5;\n"
" float e = texture2D(uv, texcoord).g - 0.5;\n"
" float r = 1.164383 * c + 1.596027 * e;\n"
" float g = 1.164383 * c - 0.391762 * d - 0.812968 * e;\n"
" float b = 1.164383 * c + 2.017232 * d;\n"
" gl_FragColor = vec4(r, g, b, 1.0);\n"
"}\n";
}
GLuint program = CompileProgram(yuvVS, yuvPS);
ASSERT_NE(0u, program);
GLuint yUniform = glGetUniformLocation(program, "y");
GLuint uvUniform = glGetUniformLocation(program, "uv");
// Fetch the D3D11 device
EGLDeviceEXT eglDevice;
eglQueryDisplayAttribEXT(display, EGL_DEVICE_EXT, (EGLAttrib *)&eglDevice);
ID3D11Device *device;
eglQueryDeviceAttribEXT(eglDevice, EGL_D3D11_DEVICE_ANGLE, (EGLAttrib *)&device);
// Create the NV12 D3D11 texture
HRESULT res;
D3D11_TEXTURE2D_DESC desc;
desc.Width = yuvtest_width;
desc.Height = yuvtest_height;
desc.Format = DXGI_FORMAT_NV12;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = 0;
desc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA subres;
subres.pSysMem = yuvtest_data;
subres.SysMemPitch = yuvtest_width;
subres.SysMemSlicePitch = yuvtest_width * yuvtest_height * 3 / 2;
ID3D11Texture2D *texture = nullptr;
res = device->CreateTexture2D(&desc, &subres, &texture);
// Create the stream
const EGLint streamAttributes[] = {
EGL_CONSUMER_LATENCY_USEC_KHR, 0, EGL_CONSUMER_ACQUIRE_TIMEOUT_USEC_KHR, 0, EGL_NONE,
};
EGLStreamKHR stream = eglCreateStreamKHR(display, streamAttributes);
ASSERT_EGL_SUCCESS();
EGLAttrib consumerAttributes[] = {
EGL_COLOR_BUFFER_TYPE,
EGL_YUV_BUFFER_EXT,
EGL_YUV_NUMBER_OF_PLANES_EXT,
2,
EGL_YUV_PLANE0_TEXTURE_UNIT_NV,
0,
EGL_YUV_PLANE1_TEXTURE_UNIT_NV,
1,
EGL_NONE,
};
GLuint tex[2];
glGenTextures(2, tex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex[0]);
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
ASSERT_GL_NO_ERROR();
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex[1]);
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
ASSERT_GL_NO_ERROR();
EGLBoolean result =
eglStreamConsumerGLTextureExternalAttribsNV(display, stream, consumerAttributes);
ASSERT_EGL_TRUE(result);
ASSERT_EGL_SUCCESS();
EGLAttrib producerAttributes[] = {
EGL_NONE,
};
result = eglCreateStreamProducerD3DTextureNV12ANGLE(display, stream, producerAttributes);
ASSERT_EGL_TRUE(result);
ASSERT_EGL_SUCCESS();
// Insert the frame
EGLAttrib frameAttributes[] = {
EGL_D3D_TEXTURE_SUBRESOURCE_ID_ANGLE, 0, EGL_NONE,
};
result = eglStreamPostD3DTextureNV12ANGLE(display, stream, (void *)texture, frameAttributes);
ASSERT_EGL_TRUE(result);
ASSERT_EGL_SUCCESS();
EGLint state;
eglQueryStreamKHR(display, stream, EGL_STREAM_STATE_KHR, &state);
ASSERT_EGL_SUCCESS();
ASSERT_EQ(EGL_STREAM_STATE_NEW_FRAME_AVAILABLE_KHR, state);
eglStreamConsumerAcquireKHR(display, stream);
ASSERT_EGL_SUCCESS();
glUseProgram(program);
glUniform1i(yUniform, 0);
glUniform1i(uvUniform, 1);
drawQuad(program, "position", 0.0f);
ASSERT_GL_NO_ERROR();
eglStreamConsumerReleaseKHR(display, stream);
ASSERT_EGL_SUCCESS();
eglSwapBuffers(display, window->getSurface());
SafeRelease(texture);
}
ANGLE_INSTANTIATE_TEST(EGLStreamTest,
ES2_D3D9(),
ES2_D3D11(),
ES3_D3D11(),
ES2_OPENGL(),
ES3_OPENGL());
} // anonymous namespace
|