forked from DirectFB/directfb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfbtest_eglblit.c
427 lines (309 loc) · 10.9 KB
/
dfbtest_eglblit.c
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
/*
* Copyright (C) 1999-2001 Brian Paul 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
* BRIAN PAUL 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.
*/
#define DFBEGL_ENABLE_MANGLE
#include <dfbegl.h>
#include <directfb.h>
#define GL_GLEXT_PROTOTYPES
#define EGL_EGLEXT_PROTOTYPES
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <GLES2/gl2.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
static const char vertex_shader[] =
"attribute mediump vec2 position;\n"
"\n"
"void main(void)\n"
"{\n"
" gl_Position = vec4(position.x, position.y, 0.0, 1.0);\n"
"}";
static const char fragment_shader[] =
"void main(void)\n"
"{\n"
" gl_FragColor = vec4( 0.0, 1.0, 0.0, 1.0 );\n"
"}";
static void
shader_init(void)
{
GLuint v, f, program;
const char *p;
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
program = glCreateProgram();
/* Compile the vertex shader */
p = vertex_shader;
v = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(v, 1, &p, NULL);
glCompileShader(v);
GLint log_length, char_count;
char *log;
GLint status;
glGetShaderiv(v, GL_COMPILE_STATUS, &status);
if (status) {
glAttachShader(program, v);
glDeleteShader(v); // mark for deletion on detach
}
else {
glGetShaderiv(v, GL_INFO_LOG_LENGTH, &log_length);
log = malloc(log_length);
glGetShaderInfoLog(v, log_length, &char_count, log);
fprintf(stderr,"%s: vertex shader compilation failure:\n%s\n", __FUNCTION__, log);
free(log);
}
/* Compile the fragment shader */
p = fragment_shader;
f = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(f, 1, &p, NULL);
glCompileShader(f);
glGetShaderiv(f, GL_COMPILE_STATUS, &status);
if (status) {
glAttachShader(program, f);
glDeleteShader(f); // mark for deletion on detach
}
else {
glGetShaderiv(f, GL_INFO_LOG_LENGTH, &log_length);
log = malloc(log_length);
glGetShaderInfoLog(f, log_length, &char_count, log);
fprintf(stderr,"%s: fragment shader compilation failure:\n%s\n", __FUNCTION__, log);
free(log);
}
/* Create and link the shader program */
glBindAttribLocation(program, 0, "position");
glLinkProgram(program);
glValidateProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (status) {
// Don't need the shader objects anymore.
GLuint shaders[2];
GLsizei shader_count;
glGetAttachedShaders(program, 2, &shader_count, shaders);
glDetachShader(program, shaders[0]);
glDetachShader(program, shaders[1]);
}
else {
// Report errors. Shader objects detached when program is deleted.
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_length);
log = malloc(log_length);
glGetProgramInfoLog(program, log_length, &char_count, log);
fprintf(stderr,"%s: shader program link failure:\n%s\n", __FUNCTION__, log);
free(log);
}
int l1 = glGetAttribLocation(program, "position");
printf("location: %d\n",l1);
/* Enable the shaders */
glUseProgram(program);
}
typedef struct {
IDirectFB *dfb;
IDirectFBSurface *primary;
IDirectFBEventBuffer *events;
DFBDimension size;
IDirectFBSurface *offscreen;
} Test;
static DFBResult
Initialize( Test *test,
int *argc,
char ***argv )
{
DFBResult ret;
DFBSurfaceDescription dsc;
/*
* Initialize DirectFB options
*/
ret = DirectFBInit( argc, argv );
if (ret) {
D_DERROR( ret, "DirectFBInit() failed!\n" );
return ret;
}
/*
* Create the super interface
*/
ret = DirectFBCreate( &test->dfb );
if (ret) {
D_DERROR( ret, "DirectFBCreate() failed!\n" );
return ret;
}
/*
* Create an event buffer for all devices with these caps
*/
ret = test->dfb->CreateInputEventBuffer( test->dfb, DICAPS_KEYS | DICAPS_AXES, DFB_FALSE, &test->events );
if (ret) {
D_DERROR( ret, "IDirectFB::CreateInputEventBuffer( DICAPS_KEYS | DICAPS_AXES ) failed!\n" );
return ret;
}
/*
* Try to set our cooperative level to DFSCL_FULLSCREEN for exclusive access to the primary layer
*/
test->dfb->SetCooperativeLevel( test->dfb, DFSCL_FULLSCREEN );
/*
* Create the primary surface
*/
dsc.flags = DSDESC_CAPS | DSDESC_PIXELFORMAT;
dsc.caps = DSCAPS_PRIMARY | DSCAPS_FLIPPING;
dsc.pixelformat = DSPF_ARGB;
ret = test->dfb->CreateSurface( test->dfb, &dsc, &test->primary );
if (ret) {
D_DERROR( ret, "IDirectFB::CreateSurface( DSCAPS_PRIMARY | DSCAPS_FLIPPING ) failed!\n" );
return ret;
}
/*
* Get the size of the surface, clear and show it
*/
test->primary->GetSize( test->primary, &test->size.w, &test->size.h );
/*
* Create the primary surface
*/
dsc.flags = DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT | DSDESC_CAPS;
dsc.width = test->size.w;
dsc.height = test->size.h;
dsc.pixelformat = DSPF_ARGB;
dsc.caps = DSCAPS_DOUBLE;
ret = test->dfb->CreateSurface( test->dfb, &dsc, &test->offscreen );
if (ret) {
D_DERROR( ret, "IDirectFB::CreateSurface( %dx%d ) failed!\n", test->size.w, test->size.h );
return ret;
}
return DFB_OK;
}
static void
Shutdown( Test *test )
{
if (test->offscreen)
test->offscreen->Release( test->offscreen );
if (test->primary)
test->primary->Release( test->primary );
if (test->events)
test->events->Release( test->events );
if (test->dfb)
test->dfb->Release( test->dfb );
}
static EGLDisplay display;
static EGLConfig configs[2];
static EGLContext context;
static EGLSurface surface;
static DFBResult
InitGL( Test *test )
{
EGLint major, minor, nconfigs;
EGLint attribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, 1,
EGL_GREEN_SIZE, 1,
EGL_BLUE_SIZE, 1,
EGL_ALPHA_SIZE, 1,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
};
EGLint context_attrs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE
};
EGLint surface_attrs[] = {
EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE
};
EGLNativeDisplayType disp = EGL_DEFAULT_DISPLAY;
#define EGL_CHECK(cmd) \
/*fprintf(stderr, "CALLING %s...\n", #cmd);*/ \
if (cmd) { \
fprintf(stderr, "!!! %s failed\n", #cmd); \
goto quit; \
}
// get display
EGL_CHECK((display = eglGetDisplay(disp)) == EGL_NO_DISPLAY)
// init
EGL_CHECK(!eglInitialize(display, &major, &minor))
// get configs
EGL_CHECK(!eglGetConfigs(display, configs, 2, &nconfigs))
// choose config
EGL_CHECK(!eglChooseConfig(display, attribs, configs, 2, &nconfigs))
// create a surface
EGL_CHECK((surface = eglCreateWindowSurface(display, configs[0], test->offscreen, surface_attrs)) == EGL_NO_SURFACE)
EGL_CHECK(eglBindAPI(EGL_OPENGL_ES_API) != EGL_TRUE)
// create context
EGL_CHECK((context = eglCreateContext(display, configs[0], EGL_NO_CONTEXT, context_attrs)) == EGL_NO_CONTEXT)
EGL_CHECK(eglMakeCurrent(display, surface, surface, context) != EGL_TRUE)
eglSwapInterval( display, 1 );
/* Setup the viewport */
glViewport( 0, 0, (GLint) test->size.w, (GLint) test->size.h );
return DFB_OK;
quit:
return DFB_FAILURE;
}
/**********************************************************************************************************************/
/**********************************************************************************************************************/
int
main( int argc, char *argv[] )
{
DFBResult ret;
bool quit = false;
Test test;
memset( &test, 0, sizeof(test) );
ret = Initialize( &test, &argc, &argv );
if (ret)
goto error;
ret = InitGL( &test );
if (ret)
goto error;
shader_init();
/*
* Main Loop
*/
while (!quit) {
DFBInputEvent evt;
const static GLfloat v[6] = { -1.0, -1.0, 1.0, 0.0, -1.0, 1.0 };
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glDisable( GL_CULL_FACE );
glDisable( GL_DEPTH_TEST );
glEnableVertexAttribArray( 0 );
glVertexAttribPointer( 0, 2, GL_FLOAT, GL_TRUE, 0, v );
glDrawArrays( GL_TRIANGLE_FAN, 0, 3 );
eglSwapBuffers( display, surface );
// behaves like eglCopyBuffers( display, surface, test.primary );
test.primary->Blit( test.primary, test.offscreen, NULL, 0, 0 );
test.primary->Flip( test.primary, NULL, DSFLIP_NONE );
/*
* Process events
*/
test.events->WaitForEvent( test.events );
while (test.events->GetEvent( test.events, DFB_EVENT(&evt) ) == DFB_OK) {
switch (evt.type) {
case DIET_KEYPRESS:
switch (evt.key_symbol) {
case DIKS_ESCAPE:
quit = true;
break;
default:
;
}
break;
default:
;
}
}
}
error:
Shutdown( &test );
return ret;
}