summaryrefslogtreecommitdiffstats
path: root/src/egl/drivers/demo/demo.c
blob: e1f85e774f934f36aa9e965ce0a0e3eca6828e20 (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
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
/*
 * Sample driver: Demo
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "eglconfig.h"
#include "eglcontext.h"
#include "egldisplay.h"
#include "egldriver.h"
#include "eglglobals.h"
#include "eglmode.h"
#include "eglscreen.h"
#include "eglsurface.h"


/**
 * Demo driver-specific driver class derived from _EGLDriver
 */
typedef struct demo_driver
{
   _EGLDriver Base;  /* base class/object */
   GLuint DemoStuff;
} DemoDriver;

#define DEMO_DRIVER(D) ((DemoDriver *) (D))


/**
 * Demo driver-specific surface class derived from _EGLSurface
 */
typedef struct demo_surface
{
   _EGLSurface Base;  /* base class/object */
   GLuint DemoStuff;
} DemoSurface;


/**
 * Demo driver-specific context class derived from _EGLContext
 */
typedef struct demo_context
{
   _EGLContext Base;  /* base class/object */
   GLuint DemoStuff;
} DemoContext;



static EGLBoolean
demoInitialize(_EGLDriver *drv, EGLDisplay dpy, EGLint *major, EGLint *minor)
{
   _EGLDisplay *disp = _eglLookupDisplay(dpy);
   _EGLScreen *scrn;
   EGLint i;

   /* Create a screen */
   scrn = calloc(1, sizeof(*scrn));
   _eglAddScreen(disp, scrn);

   /* Create the screen's modes - silly example */
   _eglAddMode(scrn, 1600, 1200, 72 * 1000, "1600x1200-72");
   _eglAddMode(scrn, 1280, 1024, 72 * 1000, "1280x1024-70");
   _eglAddMode(scrn, 1280, 1024, 70 * 1000, "1280x1024-70");
   _eglAddMode(scrn, 1024,  768, 72 * 1000, "1024x768-72");

   /* Create the display's visual configs - silly example */
   for (i = 0; i < 4; i++) {
      _EGLConfig config;
      _eglInitConfig(&config, i + 1);
      _eglSetConfigAttrib(&config, EGL_RED_SIZE, 8);
      _eglSetConfigAttrib(&config, EGL_GREEN_SIZE, 8);
      _eglSetConfigAttrib(&config, EGL_BLUE_SIZE, 8);
      _eglSetConfigAttrib(&config, EGL_ALPHA_SIZE, 8);
      _eglSetConfigAttrib(&config, EGL_BUFFER_SIZE, 32);
      if (i & 1) {
         _eglSetConfigAttrib(&config, EGL_DEPTH_SIZE, 32);
      }
      if (i & 2) {
         _eglSetConfigAttrib(&config, EGL_STENCIL_SIZE, 8);
      }
      _eglSetConfigAttrib(&config, EGL_SURFACE_TYPE,
                        (EGL_WINDOW_BIT | EGL_PIXMAP_BIT | EGL_PBUFFER_BIT));
      _eglAddConfig(disp, &config);
   }

   drv->Initialized = EGL_TRUE;

   *major = 1;
   *minor = 0;

   return EGL_TRUE;
}


static EGLBoolean
demoTerminate(_EGLDriver *drv, EGLDisplay dpy)
{
   /*DemoDriver *demo = DEMO_DRIVER(dpy);*/
   free(drv);
   return EGL_TRUE;
}


static DemoContext *
LookupDemoContext(EGLContext ctx)
{
   _EGLContext *c = _eglLookupContext(ctx);
   return (DemoContext *) c;
}


static DemoSurface *
LookupDemoSurface(EGLSurface surf)
{
   _EGLSurface *s = _eglLookupSurface(surf);
   return (DemoSurface *) s;
}



static EGLContext
demoCreateContext(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, EGLContext share_list, const EGLint *attrib_list)
{
   _EGLConfig *conf;
   DemoContext *c;
   _EGLDisplay *disp = _eglLookupDisplay(dpy);
   int i;

   conf = _eglLookupConfig(drv, dpy, config);
   if (!conf) {
      _eglError(EGL_BAD_CONFIG, "eglCreateContext");
      return EGL_NO_CONTEXT;
   }

   for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
      switch (attrib_list[i]) {
         /* no attribs defined for now */
      default:
         _eglError(EGL_BAD_ATTRIBUTE, "eglCreateContext");
         return EGL_NO_CONTEXT;
      }
   }

   c = (DemoContext *) calloc(1, sizeof(DemoContext));
   if (!c)
      return EGL_NO_CONTEXT;

   _eglInitContext(&c->Base);
   c->Base.Display = disp;
   c->Base.Config = conf;
   c->Base.DrawSurface = EGL_NO_SURFACE;
   c->Base.ReadSurface = EGL_NO_SURFACE;
   c->DemoStuff = 1;
   printf("demoCreateContext\n");

   /* generate handle and insert into hash table */
   _eglSaveContext(&c->Base);
   assert(c->Base.Handle);

   return c->Base.Handle;
}


static EGLSurface
demoCreateWindowSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, NativeWindowType window, const EGLint *attrib_list)
{
   int i;
   for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
      switch (attrib_list[i]) {
         /* no attribs at this time */
      default:
         _eglError(EGL_BAD_ATTRIBUTE, "eglCreateWindowSurface");
         return EGL_NO_SURFACE;
      }
   }
   printf("eglCreateWindowSurface()\n");
   /* XXX unfinished */

   return EGL_NO_SURFACE;
}


static EGLSurface
demoCreatePixmapSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, NativePixmapType pixmap, const EGLint *attrib_list)
{
   _EGLConfig *conf;
   EGLint i;

   conf = _eglLookupConfig(drv, dpy, config);
   if (!conf) {
      _eglError(EGL_BAD_CONFIG, "eglCreatePixmapSurface");
      return EGL_NO_SURFACE;
   }

   for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
      switch (attrib_list[i]) {
         /* no attribs at this time */
      default:
         _eglError(EGL_BAD_ATTRIBUTE, "eglCreatePixmapSurface");
         return EGL_NO_SURFACE;
      }
   }

   if (conf->Attrib[EGL_SURFACE_TYPE - FIRST_ATTRIB] == 0) {
      _eglError(EGL_BAD_MATCH, "eglCreatePixmapSurface");
      return EGL_NO_SURFACE;
   }

   printf("eglCreatePixmapSurface()\n");
   return EGL_NO_SURFACE;
}


static EGLSurface
demoCreatePbufferSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, const EGLint *attrib_list)
{
   _EGLConfig *conf;
   EGLint i, width = 0, height = 0, largest, texFormat, texTarget, mipmapTex;
   DemoSurface *surf;

   conf = _eglLookupConfig(drv, dpy, config);
   if (!conf) {
      _eglError(EGL_BAD_CONFIG, "eglCreatePbufferSurface");
      return EGL_NO_SURFACE;
   }

   for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
      switch (attrib_list[i]) {
      case EGL_WIDTH:
         width = attrib_list[++i];
         break;
      case EGL_HEIGHT:
         height = attrib_list[++i];
         break;
      case EGL_LARGEST_PBUFFER:
         largest = attrib_list[++i];
         break;
      case EGL_TEXTURE_FORMAT:
         texFormat = attrib_list[++i];
         break;
      case EGL_TEXTURE_TARGET:
         texTarget = attrib_list[++i];
         break;
      case EGL_MIPMAP_TEXTURE:
         mipmapTex = attrib_list[++i];
         break;
      default:
         _eglError(EGL_BAD_ATTRIBUTE, "eglCreatePbufferSurface");
         return EGL_NO_SURFACE;
      }
   }

   if (width <= 0 || height <= 0) {
      _eglError(EGL_BAD_ATTRIBUTE, "eglCreatePbufferSurface(width or height)");
      return EGL_NO_SURFACE;
   }

   surf = (DemoSurface *) calloc(1, sizeof(DemoSurface));
   if (!surf)
      return EGL_NO_SURFACE;

   surf->Base.Config = conf;
   surf->Base.Type = EGL_PBUFFER_BIT;
   surf->Base.Width = width;
   surf->Base.Height = height;
   surf->Base.TextureFormat = texFormat;
   surf->Base.TextureTarget = texTarget;
   surf->Base.MipmapTexture = mipmapTex;
   surf->Base.MipmapLevel = 0;
   surf->Base.SwapInterval = 0;

   printf("eglCreatePbufferSurface()\n");

   /* insert into hash table */
   _eglSaveSurface(&surf->Base);
   assert(surf->Base.Handle);

   return surf->Base.Handle;
}


static EGLBoolean
demoDestroySurface(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface)
{
   DemoSurface *fs = LookupDemoSurface(surface);
   _eglRemoveSurface(&fs->Base);
   if (fs->Base.IsBound) {
      fs->Base.DeletePending = EGL_TRUE;
   }
   else {
      free(fs);
   }
   return EGL_TRUE;
}


static EGLBoolean
demoDestroyContext(_EGLDriver *drv, EGLDisplay dpy, EGLContext context)
{
   DemoContext *fc = LookupDemoContext(context);
   _eglRemoveContext(&fc->Base);
   if (fc->Base.IsBound) {
      fc->Base.DeletePending = EGL_TRUE;
   }
   else {
      free(fc);
   }
   return EGL_TRUE;
}


static EGLBoolean
demoMakeCurrent(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext context)
{
   /*DemoDriver *demo = DEMO_DRIVER(dpy);*/
   DemoSurface *readSurf = LookupDemoSurface(read);
   DemoSurface *drawSurf = LookupDemoSurface(draw);
   DemoContext *ctx = LookupDemoContext(context);
   EGLBoolean b;

   b = _eglMakeCurrent(drv, dpy, draw, read, context);
   if (!b)
      return EGL_FALSE;

   /* XXX this is where we'd do the hardware context switch */
   (void) drawSurf;
   (void) readSurf;
   (void) ctx;

   printf("eglMakeCurrent()\n");
   return EGL_TRUE;
}


/*
 * Just to silence warning
 */
extern _EGLDriver *
_eglMain(NativeDisplayType dpy);


/**
 * The bootstrap function.  Return a new DemoDriver object and
 * plug in API functions.
 */
_EGLDriver *
_eglMain(NativeDisplayType dpy)
{
   DemoDriver *demo;

   demo = (DemoDriver *) calloc(1, sizeof(DemoDriver));
   if (!demo) {
      return NULL;
   }

   /* First fill in the dispatch table with defaults */
   _eglInitDriverFallbacks(&demo->Base);
   /* then plug in our Demo-specific functions */
   demo->Base.Initialize = demoInitialize;
   demo->Base.Terminate = demoTerminate;
   demo->Base.CreateContext = demoCreateContext;
   demo->Base.MakeCurrent = demoMakeCurrent;
   demo->Base.CreateWindowSurface = demoCreateWindowSurface;
   demo->Base.CreatePixmapSurface = demoCreatePixmapSurface;
   demo->Base.CreatePbufferSurface = demoCreatePbufferSurface;
   demo->Base.DestroySurface = demoDestroySurface;
   demo->Base.DestroyContext = demoDestroyContext;

   /* enable supported extensions */
   demo->Base.MESA_screen_surface = EGL_TRUE;
   demo->Base.MESA_copy_context = EGL_TRUE;

   return &demo->Base;
}