Subversion Repositories shark

Rev

Rev 1315 | Rev 1464 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1129 giacomo 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Giacomo Guidi       <giacomo@gandalf.sssup.it>
10
 *
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 */
18
 
1315 giacomo 19
#include <drivers/glib.h>
1129 giacomo 20
#include <drivers/keyb.h>
21
 
22
#include <GL/osmesa.h>
23
#include <GL/glut.h>
24
 
25
#include <math.h>
26
#include <stdlib.h>
27
#include <assert.h>
28
#include <kernel/log.h>
29
#include <kernel/kern.h>
30
 
31
#ifndef M_PI
32
#define M_PI 3.14159265
33
#endif
34
 
35
#define WIDTH 640
1321 giacomo 36
#define HEIGHT 430
1129 giacomo 37
#define BYTES_PP 2 //BytesPerPixel
38
 
39
OSMesaContext ctx;
40
 
41
static GLuint TexObj[2];
42
static GLfloat Angle = 0.0f;
43
static GLboolean UseObj = GL_FALSE;
44
 
45
#if defined(GL_VERSION_1_1) || defined(GL_VERSION_1_2)
46
#  define TEXTURE_OBJECT 1
47
#elif defined(GL_EXT_texture_object)
48
#  define TEXTURE_OBJECT 1
49
#  define glBindTexture(A,B)     glBindTextureEXT(A,B)
50
#  define glGenTextures(A,B)     glGenTexturesEXT(A,B)
51
#  define glDeleteTextures(A,B)  glDeleteTexturesEXT(A,B)
52
#endif
53
 
54
unsigned char *rgb_565_buf = NULL; //RGB 16 bpp Buffer
55
unsigned char *video_buf = NULL; //Video Buffer
56
 
57
unsigned long int VMEMLONG = WIDTH * HEIGHT * BYTES_PP / 4; // Used by copy_videomem_16to16
58
unsigned long int RGB565MEM = WIDTH * HEIGHT * BYTES_PP; // Total video mem
59
 
1321 giacomo 60
unsigned long int PERIOD_REFRESH = 30000;
61
unsigned long int PERIOD_DISEGNA = 30000;
1129 giacomo 62
 
63
unsigned long int WCET_REFRESH, WCET_DISEGNA;
64
 
65
TASK refesh(void);
66
TASK disegna(void);
67
 
68
PID refresh_PID, disegna_PID;
69
 
70
static void draw( void )
71
{
72
   glDepthFunc(GL_EQUAL);
73
   /*   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );*/
74
   glClear( GL_COLOR_BUFFER_BIT );
75
 
76
   glColor3f( 1.0, 1.0, 1.0 );
77
 
78
   /* draw first polygon */
79
   glPushMatrix();
80
   glTranslatef( -1.0, 0.0, 0.0 );
81
   glRotatef( Angle, 0.0, 0.0, 1.0 );
82
   if (UseObj) {
83
#ifdef TEXTURE_OBJECT
84
      glBindTexture( GL_TEXTURE_2D, TexObj[0] );
85
#endif
86
   }
87
   else {
88
      glCallList( TexObj[0] );
89
   }
90
   glBegin( GL_POLYGON );
91
   glTexCoord2f( 0.0, 0.0 );   glVertex2f( -1.0, -1.0 );
92
   glTexCoord2f( 1.0, 0.0 );   glVertex2f(  1.0, -1.0 );
93
   glTexCoord2f( 1.0, 1.0 );   glVertex2f(  1.0,  1.0 );
94
   glTexCoord2f( 0.0, 1.0 );   glVertex2f( -1.0,  1.0 );
95
   glEnd();
96
   glPopMatrix();
97
 
98
   /* draw second polygon */
99
   glPushMatrix();
100
   glTranslatef( 1.0, 0.0, 0.0 );
101
   glRotatef( Angle-90.0, 0.0, 1.0, 0.0 );
102
   if (UseObj) {
103
#ifdef TEXTURE_OBJECT
104
      glBindTexture( GL_TEXTURE_2D, TexObj[1] );
105
#endif
106
   }
107
   else {
108
      glCallList( TexObj[1] );
109
   }
110
   glBegin( GL_POLYGON );
111
   glTexCoord2f( 0.0, 0.0 );   glVertex2f( -1.0, -1.0 );
112
   glTexCoord2f( 1.0, 0.0 );   glVertex2f(  1.0, -1.0 );
113
   glTexCoord2f( 1.0, 1.0 );   glVertex2f(  1.0,  1.0 );
114
   glTexCoord2f( 0.0, 1.0 );   glVertex2f( -1.0,  1.0 );
115
   glEnd();
116
   glPopMatrix();
117
 
118
}
119
 
120
 
121
static void gl_init()
122
{
123
 
124
   static int twidth=8, theight=8;
125
   static GLubyte tex1[] = {
126
     0, 0, 0, 1, 1, 1, 0, 0,
1321 giacomo 127
     0, 0, 1, 0, 0, 0, 0, 0,
128
     0, 0, 1, 0, 0, 0, 0, 0,
129
     0, 0, 1, 0, 0, 0, 0, 0,
130
     0, 0, 1, 0, 0, 0, 0, 0,
131
     0, 0, 1, 0, 0, 0, 0, 0,
132
     0, 0, 1, 0, 0, 0, 0, 0,
133
     0, 0, 0, 1, 1, 1, 0, 0 };
1129 giacomo 134
 
135
   static GLubyte tex2[] = {
1321 giacomo 136
     0, 0, 0, 2, 2, 2, 0, 0,
137
     0, 0, 2, 0, 0, 0, 0, 0,
138
     0, 0, 2, 0, 0, 0, 0, 0,
139
     0, 0, 0, 2, 0, 0, 0, 0,
140
     0, 0, 0, 0, 2, 0, 0, 0,
1129 giacomo 141
     0, 0, 0, 0, 0, 2, 0, 0,
1321 giacomo 142
     0, 0, 0, 0, 0, 2, 0, 0,
143
     0, 0, 2, 2, 2, 0, 0, 0 };
1129 giacomo 144
 
145
   GLubyte tex[64][3];
146
   GLint i, j;
147
 
148
    //Create the OSMesa Context
149
   ctx = OSMesaCreateContext(OSMESA_RGB_565, NULL);
150
 
151
   //Make Current Context
152
   OSMesaMakeCurrent(ctx, rgb_565_buf, GL_UNSIGNED_SHORT_5_6_5, WIDTH, HEIGHT);
153
 
154
   UseObj = GL_TRUE;
155
 
156
   glDisable( GL_DITHER );
157
 
158
   /* Setup texturing */
159
   glEnable( GL_TEXTURE_2D );
160
   glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
161
   glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
162
 
163
   /* generate texture object IDs */
164
   if (UseObj) {
165
#ifdef TEXTURE_OBJECT
166
      glGenTextures( 2, TexObj );
167
#endif
168
   }
169
   else {
170
      TexObj[0] = glGenLists(2);
171
      TexObj[1] = TexObj[0]+1;
172
   }
173
 
174
   /* setup first texture object */
175
   if (UseObj) {
176
#ifdef TEXTURE_OBJECT
177
      glBindTexture( GL_TEXTURE_2D, TexObj[0] );
178
      assert(glIsTexture(TexObj[0]));
179
#endif
180
   }
181
   else {
182
      glNewList( TexObj[0], GL_COMPILE );
183
   }
184
   /* red on white */
185
   for (i=0;i<theight;i++) {
186
      for (j=0;j<twidth;j++) {
187
         int p = i*twidth+j;
188
         if (tex1[(theight-i-1)*twidth+j]) {
189
            tex[p][0] = 255;   tex[p][1] = 0;     tex[p][2] = 0;
190
         }
191
         else {
192
            tex[p][0] = 255;   tex[p][1] = 255;   tex[p][2] = 255;
193
         }
194
      }
195
   }
196
 
197
   glTexImage2D( GL_TEXTURE_2D, 0, 3, twidth, theight, 0,
198
                 GL_RGB, GL_UNSIGNED_BYTE, tex );
199
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
200
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
201
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
202
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
203
   if (!UseObj) {
204
      glEndList();
205
   }
206
   /* end of texture object */
207
 
208
   /* setup second texture object */
209
   if (UseObj) {
210
#ifdef TEXTURE_OBJECT
211
      glBindTexture( GL_TEXTURE_2D, TexObj[1] );
212
      assert(glIsTexture(TexObj[1]));
213
#endif
214
      assert(!glIsTexture(TexObj[1] + 999));
215
   }
216
   else {
217
      glNewList( TexObj[1], GL_COMPILE );
218
   }
219
   /* green on blue */
220
   for (i=0;i<theight;i++) {
221
      for (j=0;j<twidth;j++) {
222
         int p = i*twidth+j;
223
         if (tex2[(theight-i-1)*twidth+j]) {
224
            tex[p][0] = 0;     tex[p][1] = 255;   tex[p][2] = 0;
225
         }
226
         else {
227
            tex[p][0] = 0;     tex[p][1] = 0;     tex[p][2] = 255;
228
         }
229
      }
230
   }
231
   glTexImage2D( GL_TEXTURE_2D, 0, 3, twidth, theight, 0,
232
                 GL_RGB, GL_UNSIGNED_BYTE, tex );
233
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
234
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
235
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
236
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
237
   if (!UseObj) {
238
      glEndList();
239
   }
240
   /* end texture object */
241
 
242
   glViewport(0, 0, (GLint)WIDTH, (GLint)HEIGHT);
243
   glMatrixMode(GL_PROJECTION);
244
   glLoadIdentity();
245
   /*   glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
246
   glFrustum( -2.0, 2.0, 2.0, -2.0, 6.0, 20.0 );
247
   glMatrixMode(GL_MODELVIEW);
248
   glLoadIdentity();
249
   glTranslatef( 0.0, 0.0, -8.0 );
250
 
251
}
252
 
1315 giacomo 253
static int screen()
1129 giacomo 254
{
255
 
1315 giacomo 256
  extern DWORD flbaddr;
257
 
258
  /* graphic card Initialization */
259
  if (grx_init() < 1) {
260
     sys_abort(1);
1129 giacomo 261
  }
1315 giacomo 262
 
263
  if (grx_open(640, 480, 16) < 0) {
264
     cprintf("GRX Err\n");
265
     sys_abort(1);
1129 giacomo 266
  }
1315 giacomo 267
 
268
  video_buf = (unsigned char *)flbaddr;
1129 giacomo 269
 
270
  return 0;
271
 
272
}
273
 
274
void program_end(void *arg)
275
{
276
 
277
  OSMesaDestroyContext(ctx);
278
  free(rgb_565_buf);
279
 
1315 giacomo 280
  grx_close();
1129 giacomo 281
 
282
  sys_end();
283
 
284
}
285
 
286
void program_key_end(KEY_EVT *k)
287
{
288
 
289
  sys_end();
290
 
291
}
292
 
293
TASK refresh(void)
294
{
295
 
296
  while(1) {
297
 
1321 giacomo 298
    memcpy((video_buf+40*WIDTH*2), rgb_565_buf, RGB565MEM);
1129 giacomo 299
    task_endcycle();
300
 
301
  }
302
 
303
  sys_end();
304
 
305
}
306
 
307
 
308
TASK disegna(void)
309
{
310
 
311
  char text[100];
312
  TIME disegna_TIME, refresh_TIME;
313
 
314
  while(1) {
315
 
316
    jet_gettable(refresh_PID, &refresh_TIME, 1);
317
    jet_gettable(disegna_PID, &disegna_TIME, 1);
318
 
319
    Angle += 2.0;
320
 
321
    draw();
322
 
323
    sprintf(text,"Hard Task Refresh PER:%6d us EX:%6d us",(int)PERIOD_REFRESH,(int)refresh_TIME);
324
    grx_text(text,10,5,rgb16(0,0,255),0);    
325
    sprintf(text,"Hard Task Draw    PER:%6d us EX:%6d us",(int)PERIOD_DISEGNA,(int)disegna_TIME);
326
    grx_text(text,10,15,rgb16(0,0,255),0);
327
 
328
    task_endcycle();
329
 
330
  }
331
 
332
  sys_end();
333
 
334
}
335
 
336
int main (int argc, char *argv[])
337
{
338
 
339
    HARD_TASK_MODEL ht_refresh, ht_disegna;
340
 
341
    sys_atrunlevel(program_end,NULL, RUNLEVEL_BEFORE_EXIT);
342
 
343
    clear();
344
 
345
    WCET_REFRESH =((long int) PERIOD_REFRESH * (0.45));
346
    WCET_DISEGNA =((long int) PERIOD_DISEGNA * (0.45));
347
 
348
    hard_task_default_model(ht_refresh);
349
    hard_task_def_wcet(ht_refresh,WCET_REFRESH);
350
    hard_task_def_mit(ht_refresh,PERIOD_REFRESH);
351
    hard_task_def_usemath(ht_refresh);
352
    hard_task_def_group(ht_refresh,1);
353
    hard_task_def_ctrl_jet(ht_refresh);
354
 
355
    refresh_PID = task_create("refresh", refresh, &ht_refresh, NULL);
356
    if (refresh_PID == -1) {
357
      sys_end();
358
      exit(4);
359
    }
360
 
361
    hard_task_default_model(ht_disegna);
362
    hard_task_def_mit(ht_disegna,PERIOD_DISEGNA);
363
    hard_task_def_wcet(ht_disegna,WCET_DISEGNA);
364
    hard_task_def_group(ht_disegna,1);
365
    hard_task_def_ctrl_jet(ht_disegna);
366
    hard_task_def_usemath(ht_disegna);
1156 giacomo 367
    hard_task_def_stack(ht_disegna,30000);
368
 
1129 giacomo 369
    disegna_PID = task_create("disegna", disegna, &ht_disegna, NULL);
370
    if (disegna_PID == -1) {
371
      sys_end();
372
      exit(4);
373
    }
374
 
375
    {
376
      KEY_EVT k;
377
      k.flag = ALTL_BIT;
378
      k.scan = KEY_C;
379
      k.ascii = 'c';
380
      keyb_hook(k,program_key_end);
381
    }
1155 giacomo 382
 
383
    rgb_565_buf = malloc(RGB565MEM);
1129 giacomo 384
 
1155 giacomo 385
    gl_init();
386
 
1315 giacomo 387
    if (screen()) {
1129 giacomo 388
        printk(KERN_INFO "Graphical initialization failed !!\n");
389
        sys_end();
390
    }
391
 
392
    memset(rgb_565_buf, 0, RGB565MEM);
393
 
394
    group_activate(1);
395
 
396
    return 0;
397
 
398
}