-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathegl_wrapper.cc
445 lines (340 loc) · 11.5 KB
/
egl_wrapper.cc
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "egl_wrapper.h"
#include "base/logging.h"
typedef NativeDisplayType NativeDisplay;
typedef intptr_t NativeWindow;
typedef void * NativePixmap;
typedef struct fbdev_window
{
unsigned short width;
unsigned short height;
} fbdev_window;
//rgba8888
/*
static EGLint g_configAttribs[] = {
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_NONE,
};
*/
static EGLint g_configAttribs[] = {
EGL_RED_SIZE, 5,
EGL_GREEN_SIZE, 6,
EGL_BLUE_SIZE, 5,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_NONE,
};
static EGLDisplay g_EglDisplay = NULL;
static EGLContext g_EglContext = NULL;
static EGLSurface g_EglSurface = NULL;
static NativeDisplayType g_NativeDisplay= NULL;
static NativeWindowType g_NativeWindow;
static int g_WindowWidth=0;
static int g_WindowHeigth=0;
NativeDisplay ozone_egl_nativeCreateDisplay(void)
{
return (NativeDisplay)EGL_DEFAULT_DISPLAY;
}
void ozone_egl_nativeDestroyDisplay(NativeDisplay display)
{
return;
}
NativeWindow ozone_egl_nativeCreateWindow(const char *title, int width, int height, EGLint visualId)
{
fbdev_window *fbwin =(fbdev_window *) malloc( sizeof(fbdev_window));
if (NULL == fbwin)
{
return 0;
}
fbwin->width = width;
fbwin->height = height;
return (NativeWindow) fbwin;
}
static void ozone_egl_nativeDestroyWindow(NativeWindowType window)
{
if(window !=0 )
{
free((fbdev_window*) window);
}
}
EGLint ozone_egl_setup(EGLint x, EGLint y, EGLint width, EGLint height )
{
EGLConfig configs[10];
EGLint matchingConfigs;
EGLint err;
EGLint ctxAttribs[] =
{
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
g_NativeDisplay = (NativeDisplayType)ozone_egl_nativeCreateDisplay();
if (g_NativeDisplay < 0)
{
LOG(ERROR) << "ozone_egl_nativeCreateDisplay failed!\n";
return -1;
}
g_NativeWindow = (NativeWindowType)ozone_egl_nativeCreateWindow("egl window", width, height, 0);
g_WindowWidth = width;
g_WindowHeigth = height;
eglBindAPI(EGL_OPENGL_ES_API);
/* Get EGLDisplay */
g_EglDisplay = eglGetDisplay(g_NativeDisplay);
if (g_EglDisplay == EGL_NO_DISPLAY)
{
LOG(ERROR) << "eglGetDisplay returned EGL_NO_DISPLAY";
return OZONE_EGL_FAILURE;
}
if (!eglInitialize(g_EglDisplay, NULL, NULL))
{
LOG(ERROR) << "eglInitialize failed.";
return OZONE_EGL_FAILURE;
}
if (!eglChooseConfig(g_EglDisplay, g_configAttribs, &configs[0],
sizeof(configs)/sizeof(configs[0]), &matchingConfigs))
{
LOG(ERROR) << "eglChooseConfig failed.";
return OZONE_EGL_FAILURE;
}
if (matchingConfigs < 1)
{
LOG(ERROR) << "No matching configs found";
return OZONE_EGL_FAILURE;
}
g_EglSurface = eglCreateWindowSurface(g_EglDisplay, configs[0], 0, NULL);
if (g_EglSurface == NULL)
{
LOG(ERROR) << "g_EglSurface == EGL_NO_SURFACE eglGeterror = " << eglGetError();
return OZONE_EGL_FAILURE;
}
g_EglContext = eglCreateContext(g_EglDisplay, configs[0], NULL, ctxAttribs);
if (g_EglContext == EGL_NO_CONTEXT)
{
LOG(ERROR) << "Failed to get EGL Context";
return OZONE_EGL_FAILURE;
}
eglMakeCurrent(g_EglDisplay, g_EglSurface, g_EglSurface, g_EglContext);
if (EGL_SUCCESS != (err = eglGetError()))
{
LOG(ERROR) << "Failed eglMakeCurrent. eglGetError = 0x%x\n" << err;
return OZONE_EGL_FAILURE;
}
return OZONE_EGL_SUCCESS;
}
int ozone_egl_destroy()
{
int s32Loop = 0;
/** clean double buffer **/
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
for (s32Loop = 0; s32Loop < 2; s32Loop++)
{
glClear(GL_COLOR_BUFFER_BIT);
ozone_egl_swap();
}
eglMakeCurrent(g_EglDisplay, NULL, NULL, NULL);
if (g_EglContext)
{
eglDestroyContext(g_EglDisplay, g_EglContext);
}
if (g_EglSurface)
{
eglDestroySurface(g_EglDisplay, g_EglSurface);
}
eglTerminate(g_EglDisplay);
ozone_egl_nativeDestroyWindow(g_NativeWindow);
ozone_egl_nativeDestroyDisplay(g_NativeDisplay);
return OZONE_EGL_SUCCESS;
}
int ozone_egl_swap()
{
eglSwapBuffers(g_EglDisplay, g_EglSurface);
return OZONE_EGL_SUCCESS;
}
NativeDisplayType ozone_egl_getNativedisp()
{
return g_NativeDisplay;
}
EGLint * ozone_egl_getConfigAttribs()
{
return g_configAttribs;
}
EGLDisplay ozone_egl_getdisp()
{
return g_EglDisplay;
}
EGLSurface ozone_egl_getsurface()
{
return g_EglSurface;
}
void ozone_egl_makecurrent()
{
eglMakeCurrent(g_EglDisplay, g_EglSurface, g_EglSurface, g_EglContext);
}
GLuint ozone_egl_loadShader ( GLenum type, const char *shaderSrc )
{
GLuint shader;
GLint compiled;
// Create the shader object
shader = glCreateShader ( type );
if ( shader == 0 )
return 0;
// Load the shader source
glShaderSource ( shader, 1, &shaderSrc, NULL );
// Compile the shader
glCompileShader ( shader );
// Check the compile status
glGetShaderiv ( shader, GL_COMPILE_STATUS, &compiled );
if ( !compiled )
{
GLint infoLen = 0;
glGetShaderiv ( shader, GL_INFO_LOG_LENGTH, &infoLen );
if ( infoLen > 1 )
{
char* infoLog = new char[infoLen];
glGetShaderInfoLog ( shader, infoLen, NULL, infoLog );
printf ( "Error compiling shader:%s\n", infoLog );
delete infoLog;
}
glDeleteShader ( shader );
return 0;
}
return shader;
}
GLuint ozone_egl_loadProgram ( const char *vertShaderSrc, const char *fragShaderSrc )
{
GLuint vertexShader;
GLuint fragmentShader;
GLuint programObject;
GLint linked;
// Load the vertex/fragment shaders
vertexShader = ozone_egl_loadShader ( GL_VERTEX_SHADER, vertShaderSrc );
if ( vertexShader == 0 )
return 0;
fragmentShader = ozone_egl_loadShader ( GL_FRAGMENT_SHADER, fragShaderSrc );
if ( fragmentShader == 0 )
{
glDeleteShader( vertexShader );
return 0;
}
// Create the program object
programObject = glCreateProgram ( );
if ( programObject == 0 )
return 0;
glAttachShader ( programObject, vertexShader );
glAttachShader ( programObject, fragmentShader );
// Link the program
glLinkProgram ( programObject );
// Check the link status
glGetProgramiv ( programObject, GL_LINK_STATUS, &linked );
if ( !linked )
{
GLint infoLen = 0;
glGetProgramiv ( programObject, GL_INFO_LOG_LENGTH, &infoLen );
if ( infoLen > 1 )
{
char* infoLog = new char[infoLen];
glGetProgramInfoLog ( programObject, infoLen, NULL, infoLog );
printf ( "Error linking program:%s\n", infoLog );
delete infoLog;
}
glDeleteProgram ( programObject );
return 0;
}
// Free up no longer needed shader resources
glDeleteShader ( vertexShader );
glDeleteShader ( fragmentShader );
return programObject;
}
int ozone_egl_textureInit (ozone_egl_UserData * userData )
{
GLbyte vShaderStr[] =
"attribute vec4 a_position; \n"
"attribute vec2 a_texCoord; \n"
"varying vec2 v_texCoord; \n"
"void main() \n"
"{ \n"
" gl_Position = a_position; \n"
" v_texCoord = a_texCoord; \n"
"} \n";
GLbyte fShaderStr[] =
"precision mediump float; \n"
"varying vec2 v_texCoord; \n"
"uniform sampler2D s_texture; \n"
"void main() \n"
"{ \n"
" gl_FragColor = texture2D( s_texture, v_texCoord );\n"
"} \n";
// Load the shaders and get a linked program object
userData->programObject = ozone_egl_loadProgram ( (const char *)vShaderStr, (const char*)fShaderStr );
// Get the attribute locations
userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" );
userData->texCoordLoc = glGetAttribLocation ( userData->programObject, "a_texCoord" );
// Get the sampler location
userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_texture" );
// Load the texture
glGenTextures ( 1, &(userData->textureId) );
glBindTexture ( GL_TEXTURE_2D, userData->textureId );
printf("-----glTexImage2D %d %d %d\n",userData->colorType, userData->width,userData->height);
glTexImage2D ( GL_TEXTURE_2D, 0, userData->colorType, userData->width, userData->height, 0, userData->colorType, GL_UNSIGNED_BYTE, NULL );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
return GL_TRUE;
}
void ozone_egl_textureDraw ( ozone_egl_UserData *userData)
{
GLfloat vVertices[] = { -0.96f, 0.96f, 0.0f, // Position 0
0.0f, 0.0f, // TexCoord 0
-0.96f, -0.96f, 0.0f, // Position 1
0.0f, 1.0f, // TexCoord 1
0.96f, -0.96f, 0.0f, // Position 2
1.0f, 1.0f, // TexCoord 2
0.96f, 0.96f, 0.0f, // Position 3
1.0f, 0.0f // TexCoord 3
};
GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, userData->width, userData->height, userData->colorType, GL_UNSIGNED_BYTE, userData->data);
// Set the viewport
glViewport ( 0, 0, g_WindowWidth, g_WindowHeigth );
// Clear the color buffer
glClear ( GL_COLOR_BUFFER_BIT );
// Use the program object
glUseProgram ( userData->programObject );
// Load the vertex position
glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT,
GL_FALSE, 5 * sizeof(GLfloat), vVertices );
// Load the texture coordinate
glVertexAttribPointer ( userData->texCoordLoc, 2, GL_FLOAT,
GL_FALSE, 5 * sizeof(GLfloat), &vVertices[3] );
glEnableVertexAttribArray ( userData->positionLoc );
glEnableVertexAttribArray ( userData->texCoordLoc );
// Bind the texture
glActiveTexture ( GL_TEXTURE0 );
glBindTexture ( GL_TEXTURE_2D, userData->textureId );
// Set the sampler texture unit to 0
glUniform1i ( userData->samplerLoc, 0 );
glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
}
void ozone_egl_textureShutDown ( ozone_egl_UserData *userData )
{
// Delete texture object
glDeleteTextures ( 1, &(userData->textureId) );
// Delete program object
glDeleteProgram ( userData->programObject );
}