OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [MW_0_8_9PRE7/] [mw/] [src/] [drivers/] [scr_fbsd.c] - Blame information for rev 674

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 673 markom
/*
2
 * Copyright (C) 2000 Andrew K. Milton -- akm@theinternet.com.au
3
 *
4
 * Basically I stole stuff from all the other drivers, a bit here
5
 * a bit there. Don't look in here for a "good" example of a driver.
6
 *
7
 * FreeBSD VGL screen driver for Microwindows
8
 */
9
 
10
#include <stdio.h>
11
#include <machine/console.h>
12
#include <vgl.h>
13
#include <signal.h>
14
#include <osreldate.h>
15
 
16
/*#include "def.h" */
17
/*#include "hardware.h" */
18
/*##include "title_gz.h" */
19
 
20
#include "device.h"
21
#include "fb.h"
22
#include "genmem.h"
23
#include "genfont.h"
24
 
25
#ifndef SCREEN_WIDTH
26
#define SCREEN_WIDTH 800
27
#endif
28
 
29
#ifndef SCREEN_HEIGHT
30
#define SCREEN_HEIGHT 600
31
#endif
32
 
33
#ifndef SCREEN_DEPTH
34
#define SCREEN_DEPTH 8
35
#endif
36
 
37
 
38
#ifndef MWPIXEL_FORMAT
39
#define MWPIXEL_FORMAT MWPF_PALETTE
40
#endif
41
 
42
#if ALPHABLEND
43
/*
44
 * Alpha lookup tables for 256 color palette systems
45
 * A 5 bit alpha value is used to keep tables smaller.
46
 *
47
 * Two tables are created.  The first, alpha_to_rgb contains 15 bit RGB
48
 * values for each alpha value for each color: 32*256 short words.
49
 * RGB values can then be blended.  The second, rgb_to_palindex contains
50
 * the closest color (palette index) for each of the 5-bit
51
 * R, G, and B values: 32*32*32 bytes.
52
 */
53
static unsigned short *alpha_to_rgb = NULL;
54
static unsigned char  *rgb_to_palindex = NULL;
55
static void init_alpha_lookup(void);
56
#endif
57
 
58
static SCREENDEVICE savebits;   /* permanent offscreen drawing buffer*/
59
 
60
static PSD  FBSD_open(PSD psd);
61
static void FBSD_close(PSD psd);
62
static void FBSD_getscreeninfo(PSD psd,PMWSCREENINFO psi);
63
static void FBSD_setpalette(PSD psd,int first,int count,MWPALENTRY *pal);
64
static void FBSD_drawpixel(PSD psd,MWCOORD x, MWCOORD y, MWPIXELVAL c);
65
static MWPIXELVAL FBSD_readpixel(PSD psd,MWCOORD x, MWCOORD y);
66
static void FBSD_drawhline(PSD psd,MWCOORD x1, MWCOORD x2, MWCOORD y, MWPIXELVAL c);
67
static void FBSD_drawvline(PSD psd,MWCOORD x, MWCOORD y1, MWCOORD y2, MWPIXELVAL c);
68
static void FBSD_fillrect(PSD psd,MWCOORD x1,MWCOORD y1,MWCOORD x2,MWCOORD y2,MWPIXELVAL c);
69
 
70
static void FBSD_preselect(PSD psd);
71
static void FBSD_blit(PSD dstpsd,MWCOORD destx,MWCOORD desty,
72
                      MWCOORD w,MWCOORD h,
73
                      PSD srcpsd,MWCOORD srcx,MWCOORD srcy, long op);
74
 
75
static void FBSD_blit2(PSD dstpsd,MWCOORD destx,MWCOORD desty,
76
                      MWCOORD w,MWCOORD h,
77
                      PSD srcpsd,MWCOORD srcx,MWCOORD srcy, long op);
78
 
79
 
80
SCREENDEVICE    scrdev = {
81
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL,
82
        FBSD_open,
83
        FBSD_close,
84
        FBSD_getscreeninfo,
85
        FBSD_setpalette,
86
        FBSD_drawpixel,
87
        FBSD_readpixel,
88
        FBSD_drawhline,
89
        FBSD_drawvline,
90
        FBSD_fillrect,
91
        gen_fonts,
92
        FBSD_blit2,
93
        FBSD_preselect,
94
        NULL,                   /* DrawArea*/
95
        NULL,                   /* SetIOPermissions*/
96
        gen_allocatememgc,
97
        fb_mapmemgc,
98
        gen_freememgc
99
};
100
 
101
void FBSD_handle_event(void)
102
{
103
    VGLCheckSwitch();
104
}
105
 
106
static void FBSD_close(PSD psd)
107
{
108
    VGLEnd();
109
}
110
 
111
static PSD FBSD_open(PSD psd)
112
{
113
    PSUBDRIVER subdriver;
114
    int size, linelen;
115
 
116
    if (geteuid() != 0)
117
    {
118
        fprintf(stderr, "The current graphics console architecture ");
119
        fprintf(stderr, "only permits super-user to access it, ");
120
        fprintf(stderr, "therefore you either have to obtain such ");
121
        fprintf(stderr, "permissions or ask your sysadmin to put ");
122
        fprintf(stderr, "set-user-id on");
123
        exit(1);
124
    }
125
 
126
 
127
 
128
    if (VGLInit(SW_VESA_CG800x600) != 0)
129
    {
130
        fprintf(stderr, "WARNING! Could not initialise VESA mode. ");
131
        fprintf(stderr, "Trying to fallback to the VGA 640x480 mode\n");
132
        perror("microwin");
133
 
134
        if (VGLInit(SW_CG640x480) != 0)
135
        {
136
            fprintf(stderr, "WARNING! Could not initialise VGA mode ");
137
            fprintf(stderr, "either. Please check your kernel.\n");
138
            perror("microwin");
139
            return NULL;
140
 
141
        }
142
    }
143
 
144
    psd -> xres    = psd->xvirtres = VGLDisplay->Xsize;
145
    psd -> yres    = psd->yvirtres = VGLDisplay->Ysize;
146
    psd -> linelen = VGLDisplay->Xsize;
147
    psd -> planes  = 1;
148
    psd -> pixtype = MWPIXEL_FORMAT;
149
    psd -> bpp = 8;
150
 
151
/*     switch(psd->pixtype) { */
152
/*     case MWPF_PALETTE: */
153
/*          psd->bpp = SCREEN_DEPTH; */
154
/*          break; */
155
/*     case MWPF_TRUECOLOR0888: */
156
/*     default: */
157
/*          psd->bpp = 32; */
158
/*          break; */
159
/*     case MWPF_TRUECOLOR888: */
160
/*          psd->bpp = 24; */
161
/*          break; */
162
/*     case MWPF_TRUECOLOR565: */
163
/*          psd->bpp = 16; */
164
/*          break; */
165
/*     case MWPF_TRUECOLOR332: */
166
/*          psd->bpp = 8; */
167
/*          break; */
168
/*     } */
169
 
170
 
171
    /*    psd->ncolors = psd->bpp >= 24? (1 << 24): (1 << psd->bpp); */
172
    psd->ncolors = 256;
173
    psd->size = 0;
174
    psd->addr = NULL;
175
    psd->flags = PSF_SCREEN|PSF_HAVEBLIT;
176
 
177
    savebits=*psd;
178
    savebits.flags=PSF_MEMORY | PSF_HAVEBLIT;
179
    /* select a fb subdriver matching our planes and bpp */
180
    subdriver = select_fb_subdriver(&savebits);
181
    if (!subdriver)
182
    {
183
        fprintf(stderr,"Subdriver allocation failed!\n");
184
        return NULL;
185
    }
186
    /* calc size and linelen of savebits alloc*/
187
    GdCalcMemGCAlloc(&savebits, savebits.xvirtres, savebits.yvirtres,
188
                     0, 0, &size, &linelen);
189
 
190
    savebits.linelen = linelen;
191
    savebits.size = size;
192
    if ((savebits.addr = malloc(size)) == NULL)
193
    {
194
        fprintf(stderr,"Malloc for %d Failed!\n",size);
195
        return NULL;
196
    }
197
 
198
    set_subdriver(&savebits, subdriver, TRUE);
199
    return psd;
200
}
201
 
202
static void FBSD_getscreeninfo(PSD psd, PMWSCREENINFO psi)
203
{
204
    psi->rows = psd->yvirtres;
205
    psi->cols = psd->xvirtres;
206
    psi->planes = psd->planes;
207
    psi->bpp = psd->bpp;
208
    psi->ncolors = psd->ncolors;
209
    psi->pixtype = psd->pixtype;
210
    psi->fonts = NUMBER_FONTS;
211
    if(psd->yvirtres > 480)
212
    {
213
        /* SVGA 800x600*/
214
        psi->xdpcm = 33;        /* assumes screen width of 24 cm*/
215
        psi->ydpcm = 33;        /* assumes screen height of 18 cm*/
216
    }
217
    else if(psd->yvirtres > 350)
218
    {
219
        /* VGA 640x480*/
220
        psi->xdpcm = 27;        /* assumes screen width of 24 cm*/
221
        psi->ydpcm = 27;        /* assumes screen height of 18 cm*/
222
    }
223
    else
224
    {
225
        /* EGA 640x350*/
226
        psi->xdpcm = 27;        /* assumes screen width of 24 cm*/
227
        psi->ydpcm = 19;        /* assumes screen height of 18 cm*/
228
    }
229
}
230
 
231
static void FBSD_setpalette(PSD psd, int first, int count,
232
                            MWPALENTRY *pal)
233
{
234
    while(first < 256 && count-- > 0)
235
    {
236
        VGLSetPaletteIndex(first++, pal->r>>2, pal->g>>2, pal->b>>2);
237
        ++pal;
238
    }
239
 
240
}
241
 
242
static void FBSD_drawpixel(PSD psd, MWCOORD x, MWCOORD y, MWPIXELVAL c)
243
{
244
    VGLSetXY(VGLDisplay, x, y, (unsigned char)c);
245
    savebits.DrawPixel(&savebits, x, y, c);
246
}
247
 
248
static MWPIXELVAL FBSD_readpixel(PSD psd, MWCOORD x, MWCOORD y)
249
{
250
    return savebits.ReadPixel(&savebits,x,y);
251
/*    return(VGLGetXY(VGLDisplay, x, y)); */
252
}
253
 
254
static void FBSD_drawhline(PSD psd, MWCOORD x1, MWCOORD x2,
255
                           MWCOORD y, MWPIXELVAL c)
256
{
257
    VGLLine(VGLDisplay, x1, y, x2, y, c);
258
    savebits.DrawHorzLine(&savebits,x1,x2,y,c);
259
}
260
 
261
static void FBSD_drawvline(PSD psd, MWCOORD x, MWCOORD y1,
262
                           MWCOORD y2, MWPIXELVAL c)
263
{
264
    VGLLine(VGLDisplay, x, y1, x, y2, (unsigned char)c);
265
    savebits.DrawVertLine(&savebits,x, y1, y2, c);
266
}
267
 
268
static void FBSD_fillrect(PSD psd,MWCOORD x1, MWCOORD y1, MWCOORD x2, MWCOORD y2, MWPIXELVAL c)
269
{
270
    VGLFilledBox(VGLDisplay,x1, y1, x2, y2, (unsigned char)c);
271
    savebits.FillRect(&savebits,x1, y1, x2, y2, c);
272
}
273
 
274
static void FBSD_preselect(PSD psd)
275
{
276
    VGLCheckSwitch();
277
}
278
 
279
/*
280
 * Really Really stupid blit
281
 */
282
static void FBSD_blit(PSD dstpsd,MWCOORD destx,MWCOORD desty,
283
                      MWCOORD w,MWCOORD h,
284
                      PSD srcpsd,MWCOORD srcx,MWCOORD srcy, long op)
285
{
286
 
287
    int x, y;
288
 
289
    if (dstpsd == srcpsd)
290
    {
291
        if(dstpsd->flags & PSF_SCREEN)
292
        {
293
            VGLBitmapCopy(VGLDisplay, srcx, srcy, VGLDisplay,
294
                          destx, desty,  w, h);
295
            savebits.Blit(&savebits, destx, desty, w, h,
296
                          &savebits, srcx, srcy, op);
297
        }
298
        else
299
        {
300
            /* memory to memory blit, use offscreen blitter*/
301
            dstpsd->Blit(dstpsd, destx, desty, w, h, srcpsd, srcx, srcy, op);
302
        }
303
    }
304
    else if (dstpsd->flags & PSF_SCREEN)
305
    {
306
        VGLBitmap *bitmap;
307
 
308
        bitmap=VGLBitmapCreate(MEMBUF , w, h, NULL);
309
        VGLBitmapAllocateBits(bitmap);
310
        for (y = 0; y < h; y++)
311
        {
312
            for (x = 0; x < w; x++)
313
            {
314
                MWPIXELVAL c = srcpsd->ReadPixel(srcpsd,srcx+x,srcy+y);
315
                VGLSetXY(bitmap, x, y, c);
316
                /* update screen savebits*/
317
                savebits.DrawPixel(&savebits, destx+x, desty+y, c);
318
            }
319
        }
320
        VGLBitmapCopy(bitmap, srcx, srcy, VGLDisplay,
321
                      destx, desty,  w, h);
322
        VGLBitmapDestroy(bitmap);
323
    }
324
    else if (srcpsd->flags & PSF_SCREEN)
325
    {
326
        for (y = 0; y < h; y++)
327
        {
328
            for (x = 0; x < w; x++)
329
            {
330
                MWPIXELVAL c = srcpsd->ReadPixel(srcpsd,srcx+x,srcy+y);
331
                dstpsd->DrawPixel(dstpsd, destx+x, desty+y, c);
332
            }
333
        }
334
    }
335
}
336
 
337
/* srccopy bitblt*/
338
static void
339
FBSD_blit2(PSD dstpsd, MWCOORD dstx, MWCOORD dsty, MWCOORD w, MWCOORD h,
340
           PSD srcpsd, MWCOORD srcx, MWCOORD srcy, long op)
341
{
342
#if ALPHABLEND
343
 
344
    unsigned int srcalpha, dstalpha;
345
 
346
    if((op & MWROP_EXTENSION) != MWROP_BLENDCONSTANT)
347
        goto stdblit;
348
    srcalpha = op & 0xff;
349
 
350
    /* FIXME create lookup table after palette is stabilized...*/
351
    if(!rgb_to_palindex || !alpha_to_rgb)
352
    {
353
        init_alpha_lookup();
354
        if(!rgb_to_palindex || !alpha_to_rgb)
355
            goto stdblit;
356
    }
357
 
358
    /* Create 5 bit alpha value index for 256 color indexing*/
359
 
360
    /* destination alpha is (1 - source) alpha*/
361
    dstalpha = ((srcalpha>>3) ^ 31) << 8;
362
    srcalpha = (srcalpha>>3) << 8;
363
 
364
    while(--h >= 0)
365
    {
366
        int     i;
367
        for(i=0; i<w; ++i)
368
        {
369
            int c;
370
            /* Get source RGB555 value for source alpha value*/
371
            unsigned short s = alpha_to_rgb[srcalpha +
372
                                           srcpsd->ReadPixel(
373
                                               srcpsd,srcx+i,srcy+h)];
374
 
375
            /* Get destination RGB555 value for dest alpha value*/
376
            unsigned short d = alpha_to_rgb[dstalpha +
377
                                           dstpsd->ReadPixel(
378
                                               srcpsd,dstx+i,dsty+h)];
379
 
380
            /* Add RGB values together and get closest palette index to it*/
381
            VGLSetXY(VGLDisplay, dstx+i, dsty+h,
382
                     c=rgb_to_palindex[s + d]);
383
            savebits.DrawPixel(&savebits, dstx+i, dsty+h, c);
384
        }
385
    }
386
    return;
387
 stdblit:
388
#endif
389
    FBSD_blit(dstpsd, dstx, dsty, w, h, srcpsd, srcx, srcy, op);
390
}
391
 
392
 
393
#if ALPHABLEND
394
static void init_alpha_lookup(void)
395
{
396
    int i, a;
397
    int r, g, b;
398
    extern MWPALENTRY gr_palette[256];
399
 
400
    if(!alpha_to_rgb)
401
        alpha_to_rgb = (unsigned short *)malloc(
402
            sizeof(unsigned short)*32*256);
403
    if(!rgb_to_palindex)
404
        rgb_to_palindex = (unsigned char *)malloc(
405
            sizeof(unsigned char)*32*32*32);
406
    if(!rgb_to_palindex || !alpha_to_rgb)
407
        return;
408
 
409
    /*
410
     * Precompute alpha to rgb lookup by premultiplying
411
     * each palette rgb value by each possible alpha
412
     * and storing it as RGB555.
413
     */
414
    for(i=0; i<256; ++i) {
415
        MWPALENTRY *p = &gr_palette[i];
416
        for(a=0; a<32; ++a) {
417
            alpha_to_rgb[(a<<8)+i] =
418
                (((p->r * a / 31)>>3) << 10) |
419
                (((p->g * a / 31)>>3) << 5) |
420
                ((p->b * a / 31)>>3);
421
        }
422
    }
423
 
424
    /*
425
     * Precompute RGB555 to palette index table by
426
     * finding the nearest palette index for all RGB555 colors.
427
     */
428
    for(r=0; r<32; ++r) {
429
        for(g=0; g<32; ++g)
430
            for(b=0; b<32; ++b)
431
                rgb_to_palindex[ (r<<10)|(g<<5)|b] =
432
                    GdFindNearestColor(gr_palette, 256,
433
                                       MWRGB(r<<3, g<<3, b<<3));
434
    }
435
}
436
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.