summaryrefslogtreecommitdiff
path: root/eglstream.c
blob: 71d2c5d5b7763f90a46659a4380022902e389b3b (plain)
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
/*
 * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "shared.h"
#include "create_window.h"

#define NUM_FRAMES           100
#define WINDOW_WIDTH         600
#define WINDOW_HEIGHT        600
#define STREAM_WIDTH         128
#define STREAM_HEIGHT        128

// Shared.
static EGLDisplay dpy;
static EGLStreamKHR stream;
static int frame = 0;

// Consumer.
static EGLNativeWindowType consumerWin;
static EGLContext consumerCtx;
static EGLSurface consumerSurf;
static GLuint consumerRotationMatrixUniform;

// Producer.
static EGLContext producerCtx;
static EGLSurface producerSurf;
static GLuint producerScaleUniform;

static void DoTest(void)
{
    while (frame++ < NUM_FRAMES) {

        // Producer rendering.
        {
            float scale = (float)((frame % 50) + 50) / 100.0f;

            if (!eglMakeCurrent(dpy, producerSurf, producerSurf, producerCtx))
                FATAL_ERROR("Could not make producer context current");

            glClear(GL_COLOR_BUFFER_BIT);
            glUniform1f(producerScaleUniform, scale);
            glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
            glFinish();

            if (!eglSwapBuffers(dpy, producerSurf))
                FATAL_ERROR("SwapBuffers failed");
        }

        // Consumer rendering.
        {
            static const GLushort cubeIndices[] = {
                0, 1, 2, 3, 7, 1, 5, 4, 7, 6, 2, 4, 0, 1
            };

            if (!eglMakeCurrent(dpy, consumerSurf, consumerSurf, consumerCtx))
                FATAL_ERROR("Could not make consumer context current");

            if (!eglStreamConsumerAcquireKHR(dpy, stream))
                FATAL_ERROR("Failed to acquire frame");

            GLfloat rotMat[16];
            CreateRotationMatrix(rotMat, (float)frame, 0.0f, 1.0f, 0.0f);
            glUniformMatrix4fv(consumerRotationMatrixUniform, 1, GL_FALSE, rotMat);
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glDrawElements(GL_TRIANGLE_STRIP, 14, GL_UNSIGNED_SHORT, cubeIndices);
            glFinish();

            if (!eglSwapBuffers(dpy, consumerSurf))
                FATAL_ERROR("Failed in eglSwapBuffers");
        }
    }
}

static void InitializeEgl(void)
{
    InitEGLExtFuncs();

    dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    if (dpy == EGL_NO_DISPLAY)
        FATAL_ERROR("Could not get EGL display");
    if (!eglInitialize(dpy, 0, 0))
        FATAL_ERROR("Could not initialize EGL display");
}

static void InitializeStream(void)
{
    static EGLint streamAttrs[] = {
        EGL_NONE, EGL_NONE,
        EGL_NONE
    };
    stream = eglCreateStreamKHR(dpy, streamAttrs);
    if (stream == EGL_NO_STREAM_KHR)
        FATAL_ERROR("Error creating stream");
}

static void InitializeConsumer(void)
{
    static const char vertexShaderSource[] =
        "attribute vec4 aPosition;\n"
        "attribute vec2 aTexCoord;\n"
        "varying vec2 vTexCoord;\n"
        "uniform mat4 uProjectionMat;\n"
        "uniform mat4 uRotationMat;\n"
        "void main() {\n"
        "    vec4 pos = uRotationMat * aPosition;\n"
        "    pos.z -= 5.0;\n"
        "    gl_Position = uProjectionMat * pos;\n"
        "    vTexCoord = aTexCoord;\n"
        "}\n";

    static const char fragmentShaderSource[] =
        "#extension GL_OES_EGL_image_external : enable\n"
        "precision lowp float;\n"
        "uniform samplerExternalOES uTex;\n"
        "varying vec2 vTexCoord;\n"
        "void main() {\n"
        "    gl_FragColor = texture2D(uTex, vec2(vTexCoord.x, 1.0-vTexCoord.y));\n"
        "    if (vTexCoord.x < 0.01 || vTexCoord.x > 0.99 || vTexCoord.y < 0.01 || vTexCoord.y > 0.99) {\n"
        "        gl_FragColor = vec4(0.0, 0.5, 0.0, 1.0);\n"
        "    }\n"
        "}\n";

    static const GLfloat cubeVertices[] = {
        -1.0, -1.0,  1.0, 1.0, -1.0,  1.0, -1.0,  1.0,  1.0, 1.0,  1.0,  1.0,
        -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0,  1.0, -1.0, 1.0,  1.0, -1.0,
    };

    static const GLfloat cubeTexCoords[] = {
        1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0,
        0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0,
        1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
        0.0, 1.0, 1.0, 1.0,
    };

    static EGLint configAttrs[] = {
        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
        EGL_DEPTH_SIZE, 16,
        EGL_NONE
    };

    static EGLint contextAttrs[] = {
        EGL_CONTEXT_CLIENT_VERSION, 2,
        EGL_NONE
    };

    // Create window.
    consumerWin = CreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "EGL Streams");

    // Find config.
    EGLint ncfg = 0;
    EGLConfig cfg;
    if (!eglChooseConfig(dpy, configAttrs, &cfg, 1, &ncfg))
        FATAL_ERROR("Could not find EGL config");
    if (ncfg == 0)
        FATAL_ERROR("No EGL config found");

    // Create context.
    consumerCtx = eglCreateContext(dpy, cfg, EGL_NO_CONTEXT, contextAttrs);
    if (consumerCtx == EGL_NO_CONTEXT)
        FATAL_ERROR("Could not create EGL context");

    // Create window surface.
    consumerSurf = eglCreateWindowSurface(dpy, cfg, consumerWin, 0);
    if (consumerSurf == EGL_NO_SURFACE)
        FATAL_ERROR("Could not create EGLSurface for window");

    // Make current.
    if (!eglMakeCurrent(dpy, consumerSurf, consumerSurf, consumerCtx))
        FATAL_ERROR("Could not make context current");

    // Set clear color/viewport.
    glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    glViewport(10, 10, WINDOW_WIDTH - 20, WINDOW_HEIGHT - 20);
    glEnable(GL_DEPTH_TEST);

    // Set vertex state and create program.
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, cubeVertices);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, cubeTexCoords);
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    GLuint vs = glCreateShader(GL_VERTEX_SHADER);
    GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
    LoadShader(vs, vertexShaderSource, sizeof(vertexShaderSource));
    LoadShader(fs, fragmentShaderSource, sizeof(fragmentShaderSource));
    GLuint program = glCreateProgram();
    glAttachShader(program, vs);
    glAttachShader(program, fs);
    glBindAttribLocation(program, 0, "aPosition");
    glBindAttribLocation(program, 1, "aTexCoord");
    glLinkProgram(program);
    glUseProgram(program);

    // Set the uniforms.
    GLfloat projMat[16];
    CreatePerspectiveMatrix(projMat, 45.0f, (float)WINDOW_WIDTH/(float)WINDOW_HEIGHT, 1.0f, 1000.0f);
    glUniformMatrix4fv(glGetUniformLocation(program, "uProjectionMat"), 1, GL_FALSE, projMat);
    glUniform1i(glGetUniformLocation(program, "uTex"), 0);
    consumerRotationMatrixUniform = glGetUniformLocation(program, "uRotationMat");

    // Connect a new external texture as a consumer to the stream.
    GLuint tex;
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex);
    if (!eglStreamConsumerGLTextureExternalKHR(dpy, stream))
        FATAL_ERROR("Failed to connect consumer");

    if (glGetError() != GL_NO_ERROR)
        FATAL_ERROR("GL Error while initializing consumer");
}

static void InitializeProducer(void)
{
    static const char vertexShaderSource[] =
        "attribute vec2 aPosition;\n"
        "uniform float uScale;\n"
        "void main() {\n"
        "    gl_Position = vec4(aPosition * uScale, 0.0, 1.0);\n"
        "}\n";

    static const char fragmentShaderSource[] =
        "precision lowp float;\n"
        "void main() {\n"
        "    gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
        "}\n";

    static const GLfloat triVertices[] = {
        -0.5, -0.5,
         0.5, -0.5,
         0.0,  0.5};

    static EGLint configAttrs[] = {
        EGL_SURFACE_TYPE, EGL_STREAM_BIT_KHR,
        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
        EGL_NONE
    };

    static EGLint contextAttrs[] = {
        EGL_CONTEXT_CLIENT_VERSION, 2,
        EGL_NONE
    };

    static EGLint surfaceAttrs[] = {
        EGL_WIDTH, STREAM_WIDTH,
        EGL_HEIGHT, STREAM_HEIGHT,
        EGL_NONE
    };

    // Find producer config.
    EGLint ncfg = 0;
    EGLConfig cfg;
    if (!eglChooseConfig(dpy, configAttrs, &cfg, 1, &ncfg))
        FATAL_ERROR("Could not find EGL config for producer");
    if (ncfg == 0)
        FATAL_ERROR("No EGL config found for producer");

    // Create producer context.
    producerCtx = eglCreateContext(dpy, cfg, EGL_NO_CONTEXT, contextAttrs);
    if (producerCtx == EGL_NO_CONTEXT)
        FATAL_ERROR("Could not create producer EGL context");

    // Create producer surface.
    producerSurf = eglCreateStreamProducerSurfaceKHR(dpy, cfg, stream, surfaceAttrs);
    if (producerSurf == EGL_NO_STREAM_KHR)
        FATAL_ERROR("Could not create producer surface");

    // Make producer context current.
    if (!eglMakeCurrent(dpy, producerSurf, producerSurf, producerCtx))
        FATAL_ERROR("Could not make producer context current");

    // Set the clear color/viewport.
    glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
    glViewport(0, 0, STREAM_WIDTH, STREAM_HEIGHT);

    // Set vertex state and create program.
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, triVertices);
    glEnableVertexAttribArray(0);
    GLuint vs = glCreateShader(GL_VERTEX_SHADER);
    GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
    LoadShader(vs, vertexShaderSource, sizeof(vertexShaderSource));
    LoadShader(fs, fragmentShaderSource, sizeof(fragmentShaderSource));
    GLuint program = glCreateProgram();
    glAttachShader(program, vs);
    glAttachShader(program, fs);
    glBindAttribLocation(program, 0, "aPosition");
    glLinkProgram(program);
    glUseProgram(program);
    producerScaleUniform = glGetUniformLocation(program, "uScale");

    if (glGetError() != GL_NO_ERROR)
        FATAL_ERROR("GL Error while initializing consumer");
}

static void TerminateEgl(void)
{
    eglTerminate(dpy);
}

static void TerminateStream(void)
{
    eglDestroyStreamKHR(dpy, stream);
}

static void TerminateConsumer(void)
{
    eglDestroyContext(dpy, consumerCtx);
    eglDestroySurface(dpy, consumerSurf);
    DestroyWindow(consumerWin);
}

static void TerminateProducer(void)
{
    eglDestroyContext(dpy, producerCtx);
    eglDestroySurface(dpy, producerSurf);
}

int main(int argc, char *argv[])
{
    InitializeEgl();
    InitializeStream();
    InitializeConsumer();
    InitializeProducer();
    DoTest();
    TerminateProducer();
    TerminateConsumer();
    TerminateStream();
    TerminateEgl();
    return 0;
}