/[gxemul]/trunk/src/devices/dev_fb.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/src/devices/dev_fb.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 20 by dpavlin, Mon Oct 8 16:19:23 2007 UTC revision 42 by dpavlin, Mon Oct 8 16:22:32 2007 UTC
# Line 1  Line 1 
1  /*  /*
2   *  Copyright (C) 2003-2005  Anders Gavare.  All rights reserved.   *  Copyright (C) 2003-2007  Anders Gavare.  All rights reserved.
3   *   *
4   *  Redistribution and use in source and binary forms, with or without   *  Redistribution and use in source and binary forms, with or without
5   *  modification, are permitted provided that the following conditions are met:   *  modification, are permitted provided that the following conditions are met:
# Line 25  Line 25 
25   *  SUCH DAMAGE.   *  SUCH DAMAGE.
26   *     *  
27   *   *
28   *  $Id: dev_fb.c,v 1.110 2005/11/13 00:14:08 debug Exp $   *  $Id: dev_fb.c,v 1.132 2007/06/15 19:11:15 debug Exp $
29   *     *  
30   *  Generic framebuffer device.   *  COMMENT: Generic framebuffer device
31   *   *
32   *      DECstation VFB01 monochrome framebuffer, 1024x864   *      DECstation VFB01 monochrome framebuffer, 1024x864
33   *      DECstation VFB02 8-bit color framebuffer, 1024x864   *      DECstation VFB02 8-bit color framebuffer, 1024x864
34   *      DECstation Maxine, 1024x768 8-bit color   *      DECstation Maxine, 1024x768 8-bit color
  *      HPC (mips, arm, ..) framebuffer  
35   *      Playstation 2 (24-bit color)   *      Playstation 2 (24-bit color)
36   *      generic (any resolution, several bit depths possible)   *      Generic (any resolution, several bit depths possible, useful for
37     *              testmachines)
38   *   *
39   *   *
40   *  TODO:  There is still a bug when redrawing the cursor. The underlying   *  TODO:  This should actually be independent of X11, but that
  *         image is moved 1 pixel (?), or something like that.  
  *  
  *  TODO:  This should actually be independant of X11, but that  
41   *         might be too hard to do right now.   *         might be too hard to do right now.
42   *   *
43   *  TODO:  playstation 2 pixels are stored in another format, actually   *  TODO:  playstation 2 pixels are stored in another format, actually
# Line 65  Line 62 
62  #endif  #endif
63    
64    
65  #define FB_TICK_SHIFT           18  #define FB_TICK_SHIFT           19
66    
67    
68  /*  #define FB_DEBUG  */  /*  #define FB_DEBUG  */
# Line 107  void set_blackwhite_palette(struct vfb_d Line 104  void set_blackwhite_palette(struct vfb_d
104  }  }
105    
106    
107    static void set_title(struct vfb_data *d)
108    {
109            snprintf(d->title, sizeof(d->title),"GXemul: %ix%ix%i %s framebuffer",
110                d->visible_xsize, d->visible_ysize, d->bit_depth, d->name);
111            d->title[sizeof(d->title)-1] = '\0';
112    }
113    
114    
115  /*  /*
116   *  dev_fb_resize():   *  dev_fb_resize():
117   *   *
118   *  Resize a framebuffer window. (This functionality is probably a bit buggy,   *  Resize a framebuffer window. (This functionality is probably a bit buggy,
119   *  because I didn't think of including it from the start.)   *  because I didn't think of including it from the start.)
120     *
121     *  SUPER-IMPORTANT: Anyone who resizes a framebuffer by calling this function
122     *  must also clear all dyntrans address translations manually, in all cpus
123     *  which might have access to the framebuffer!
124   */   */
125  void dev_fb_resize(struct vfb_data *d, int new_xsize, int new_ysize)  void dev_fb_resize(struct vfb_data *d, int new_xsize, int new_ysize)
126  {  {
# Line 124  void dev_fb_resize(struct vfb_data *d, i Line 133  void dev_fb_resize(struct vfb_data *d, i
133                  return;                  return;
134          }          }
135    
136            if (new_xsize < 10 || new_ysize < 10) {
137                    fatal("dev_fb_resize(): size too small.\n");
138                    exit(1);
139            }
140    
141          new_bytes_per_line = new_xsize * d->bit_depth / 8;          new_bytes_per_line = new_xsize * d->bit_depth / 8;
142          size = new_ysize * new_bytes_per_line;          size = new_ysize * new_bytes_per_line;
143    
144          new_framebuffer = malloc(size);          CHECK_ALLOCATION(new_framebuffer = malloc(size));
         if (new_framebuffer == NULL) {  
                 fprintf(stderr, "dev_fb_resize(): out of memory\n");  
                 exit(1);  
         }  
145    
146          /*  Copy the old framebuffer to the new:  */          /*  Copy the old framebuffer to the new:  */
147          if (d->framebuffer != NULL) {          if (d->framebuffer != NULL) {
# Line 153  void dev_fb_resize(struct vfb_data *d, i Line 163  void dev_fb_resize(struct vfb_data *d, i
163          d->framebuffer = new_framebuffer;          d->framebuffer = new_framebuffer;
164          d->framebuffer_size = size;          d->framebuffer_size = size;
165    
166          if (new_xsize > d->x11_xsize || new_ysize > d->x11_ysize) {          if (new_xsize > d->xsize || new_ysize > d->ysize) {
167                  d->update_x1 = d->update_y1 = 0;                  d->update_x1 = d->update_y1 = 0;
168                  d->update_x2 = new_xsize - 1;                  d->update_x2 = new_xsize - 1;
169                  d->update_y2 = new_ysize - 1;                  d->update_y2 = new_ysize - 1;
170          }          }
171    
172          d->bytes_per_line = new_bytes_per_line;          d->bytes_per_line = new_bytes_per_line;
173          d->x11_xsize = d->visible_xsize = new_xsize;          d->xsize = d->visible_xsize = new_xsize;
174          d->x11_ysize = d->visible_ysize = new_ysize;          d->ysize = d->visible_ysize = new_ysize;
175    
176            d->x11_xsize = d->xsize / d->vfb_scaledown;
177            d->x11_ysize = d->ysize / d->vfb_scaledown;
178    
179            memory_device_update_data(d->memory, d, d->framebuffer);
180    
181            set_title(d);
182    
183  #ifdef WITH_X11  #ifdef WITH_X11
184          if (d->fb_window != NULL)          if (d->fb_window != NULL) {
185                  x11_fb_resize(d->fb_window, new_xsize, new_ysize);                  x11_fb_resize(d->fb_window, new_xsize, new_ysize);
186                    x11_set_standard_properties(d->fb_window, d->title);
187            }
188  #endif  #endif
189  }  }
190    
# Line 195  void dev_fb_setcursor(struct vfb_data *d Line 214  void dev_fb_setcursor(struct vfb_data *d
214          }          }
215  #endif  #endif
216    
         if (d->fb_window != NULL)  
                 console_set_framebuffer_mouse(cursor_x, cursor_y,  
                     d->fb_window->fb_number);  
   
217          /*  debug("dev_fb_setcursor(%i,%i, size %i,%i, on=%i)\n",          /*  debug("dev_fb_setcursor(%i,%i, size %i,%i, on=%i)\n",
218              cursor_x, cursor_y, cursor_xsize, cursor_ysize, on);  */              cursor_x, cursor_y, cursor_xsize, cursor_ysize, on);  */
219  }  }
# Line 211  void dev_fb_setcursor(struct vfb_data *d Line 226  void dev_fb_setcursor(struct vfb_data *d
226   *  block copy/fill.   *  block copy/fill.
227   *   *
228   *  If fillflag is non-zero, then fill_[rgb] should contain the color   *  If fillflag is non-zero, then fill_[rgb] should contain the color
229   *  with which to fill.   *  with which to fill. (In 8-bit mode, only fill_r is used.)
230   *   *
231   *  If fillflag is zero, copy mode is used, and from_[xy] should contain   *  If fillflag is zero, copy mode is used, and from_[xy] should contain
232   *  the offset on the framebuffer where we should copy from.   *  the offset on the framebuffer where we should copy from.
# Line 222  void framebuffer_blockcopyfill(struct vf Line 237  void framebuffer_blockcopyfill(struct vf
237          int fill_g, int fill_b, int x1, int y1, int x2, int y2,          int fill_g, int fill_b, int x1, int y1, int x2, int y2,
238          int from_x, int from_y)          int from_x, int from_y)
239  {  {
240          int y;          int x, y;
241          long from_ofs, dest_ofs, linelen;          long from_ofs, dest_ofs, linelen;
242    
243          if (fillflag)          if (fillflag)
# Line 245  void framebuffer_blockcopyfill(struct vf Line 260  void framebuffer_blockcopyfill(struct vf
260          if (fillflag) {          if (fillflag) {
261                  for (y=y1; y<=y2; y++) {                  for (y=y1; y<=y2; y++) {
262                          if (y>=0 && y<d->ysize) {                          if (y>=0 && y<d->ysize) {
263                                  int x;                                  unsigned char *buf =
264                                  char buf[8192 * 3];                                      d->framebuffer + dest_ofs;
265                                  if (d->bit_depth == 24)  
266                                          for (x=0; x<linelen; x+=3) {                                  if (d->bit_depth == 24) {
267                                            for (x=0; x<linelen && x <
268                                                (int) sizeof(buf); x += 3) {
269                                                  buf[x] = fill_r;                                                  buf[x] = fill_r;
270                                                  buf[x+1] = fill_g;                                                  buf[x+1] = fill_g;
271                                                  buf[x+2] = fill_b;                                                  buf[x+2] = fill_b;
272                                          }                                          }
273                                  else                                  } else if (d->bit_depth == 8) {
274                                          printf("TODO: fill for non-24-bit"                                          memset(buf, fill_r, linelen);
275                                              " modes\n");                                  } else {
276                                            fatal("Unimplemented bit-depth (%i)"
277                                  memmove(d->framebuffer + dest_ofs, buf,                                              " for fb fill\n", d->bit_depth);
278                                      linelen);                                          exit(1);
279                                    }
280                          }                          }
281    
282                          dest_ofs += d->bytes_per_line;                          dest_ofs += d->bytes_per_line;
# Line 266  void framebuffer_blockcopyfill(struct vf Line 284  void framebuffer_blockcopyfill(struct vf
284          } else {          } else {
285                  from_ofs = d->bytes_per_line * from_y +                  from_ofs = d->bytes_per_line * from_y +
286                      (d->bit_depth/8) * from_x;                      (d->bit_depth/8) * from_x;
   
287                  for (y=y1; y<=y2; y++) {                  for (y=y1; y<=y2; y++) {
288                          if (y>=0 && y<d->ysize)                          if (y >= 0 && y < d->ysize) {
289                                  memmove(d->framebuffer + dest_ofs,                                  if (from_y >= 0 && from_y < d->ysize)
290                                      d->framebuffer + from_ofs, linelen);                                          memmove(d->framebuffer + dest_ofs,
291                                                d->framebuffer + from_ofs, linelen);
292                                    else
293                                            memset(d->framebuffer + dest_ofs,
294                                                0, linelen);
295                            }
296                            from_y ++;
297                          from_ofs += d->bytes_per_line;                          from_ofs += d->bytes_per_line;
298                          dest_ofs += d->bytes_per_line;                          dest_ofs += d->bytes_per_line;
299                  }                  }
# Line 290  void framebuffer_blockcopyfill(struct vf Line 312  void framebuffer_blockcopyfill(struct vf
312    
313    
314  #ifdef WITH_X11  #ifdef WITH_X11
 #define macro_put_pixel() {     \  
         /*  Combine the color into an X11 long and display it:  */      \  
         /*  TODO:  construct color in a more portable way:  */          \  
         switch (d->fb_window->x11_screen_depth) {                       \  
         case 24:                                                        \  
                 if (d->fb_window->fb_ximage->byte_order)                \  
                         color = (b << 16) + (g << 8) + r;               \  
                 else                                                    \  
                         color = (r << 16) + (g << 8) + b;               \  
                 break;                                                  \  
         case 16:                                                        \  
                 r >>= 3; g >>= 2; b >>= 3;                              \  
                 if (d->fb_window->fb_ximage->byte_order) {              \  
                         /*  Big endian 16-bit X server:  */             \  
                         static int first = 1;                           \  
                         if (first) {                                    \  
                                 fprintf(stderr, "\n*** Please report "  \  
                                     "to the author whether 16-bit X11 " \  
                                     "colors are rendered correctly or " \  
                                     "not!\n\n");                        \  
                                 first = 0;                              \  
                         }                                               \  
                         color = (b << 11) + (g << 5) + r;               \  
                 } else {                                                \  
                         /*  Little endian (eg PC) X servers:  */        \  
                         color = (r << 11) + (g << 5) + b;               \  
                 }                                                       \  
                 break;                                                  \  
         case 15:                                                        \  
                 r >>= 3; g >>= 3; b >>= 3;                              \  
                 if (d->fb_window->fb_ximage->byte_order) {              \  
                         /*  Big endian 15-bit X server:  */             \  
                         static int first = 1;                           \  
                         if (first) {                                    \  
                                 fprintf(stderr, "\n*** Please report "  \  
                                     "to the author whether 15-bit X11 " \  
                                     "colors are rendered correctly or " \  
                                     "not!\n\n");                        \  
                                 first = 0;                              \  
                         }                                               \  
                         color = (b << 10) + (g << 5) + r;               \  
                 } else {                                                \  
                         /*  Little endian (eg PC) X servers:  */        \  
                         color = (r << 10) + (g << 5) + b;               \  
                 }                                                       \  
                 break;                                                  \  
         default:                                                        \  
                 color = d->fb_window->x11_graycolor[15 * (r + g + b)    \  
                     / (255 * 3)].pixel;                                 \  
         }                                                               \  
         if (x>=0 && x<d->x11_xsize && y>=0 && y<d->x11_ysize)           \  
                 XPutPixel(d->fb_window->fb_ximage, x, y, color);        \  
     }  
 #else  
 /*  If not WITH_X11:  */  
 #define macro_put_pixel() { }  
 #endif  
   
   
 /*  
  *  update_framebuffer():  
  *  
  *  The framebuffer memory has been updated. This function tries to make  
  *  sure that the XImage is also updated (1 or more pixels).  
  */  
 void update_framebuffer(struct vfb_data *d, int addr, int len)  
 {  
         int x, y, pixel, npixels;  
         long color_r, color_g, color_b;  
 #ifdef WITH_X11  
         long color;  
 #endif  
         int scaledown = d->vfb_scaledown;  
         int scaledownXscaledown = 1;  
   
         if (scaledown == 1) {  
                 /*  Which framebuffer pixel does addr correspond to?  */  
                 pixel = addr * 8 / d->bit_depth;  
                 y = pixel / d->xsize;  
                 x = pixel % d->xsize;  
   
                 /*  How many framebuffer pixels?  */  
                 npixels = len * 8 / d->bit_depth;  
                 if (npixels == 0)  
                         npixels = 1;  
   
                 if (d->bit_depth < 8) {  
                         for (pixel=0; pixel<npixels; pixel++) {  
                                 int fb_addr, c, r, g, b;  
                                 color_r = color_g = color_b = 0;  
   
                                 fb_addr = (y * d->xsize + x) * d->bit_depth;  
                                 /*  fb_addr is now which _bit_ in  
                                     the framebuffer  */  
   
                                 c = d->framebuffer[fb_addr >> 3];  
                                 fb_addr &= 7;  
   
                                 /*  HPC is reverse:  */  
                                 if (d->vfb_type == VFB_HPC)  
                                         fb_addr = 8 - d->bit_depth - fb_addr;  
   
                                 c = (c >> fb_addr) & ((1<<d->bit_depth) - 1);  
                                 /*  c <<= (8 - d->bit_depth);  */  
   
                                 r = d->rgb_palette[c*3 + 0];  
                                 g = d->rgb_palette[c*3 + 1];  
                                 b = d->rgb_palette[c*3 + 2];  
   
                                 macro_put_pixel();  
                                 x++;  
                         }  
                 } else if (d->bit_depth == 8) {  
                         for (pixel=0; pixel<npixels; pixel++) {  
                                 int fb_addr, c, r, g, b;  
                                 color_r = color_g = color_b = 0;  
   
                                 fb_addr = y * d->xsize + x;  
                                 /*  fb_addr is now which byte in framebuffer  */  
                                 c = d->framebuffer[fb_addr];  
                                 r = d->rgb_palette[c*3 + 0];  
                                 g = d->rgb_palette[c*3 + 1];  
                                 b = d->rgb_palette[c*3 + 2];  
   
                                 macro_put_pixel();  
                                 x++;  
                         }  
                 } else {        /*  d->bit_depth > 8  */  
                         for (pixel=0; pixel<npixels; pixel++) {  
                                 int fb_addr, r, g, b;  
                                 color_r = color_g = color_b = 0;  
   
                                 fb_addr = (y * d->xsize + x) * d->bit_depth;  
                                 /*  fb_addr is now which byte in framebuffer  */  
   
                                 /*  > 8 bits color.  */  
                                 fb_addr >>= 3;  
                                 switch (d->bit_depth) {  
                                 case 24:  
                                         r = d->framebuffer[fb_addr];  
                                         g = d->framebuffer[fb_addr + 1];  
                                         b = d->framebuffer[fb_addr + 2];  
                                         break;  
                                 /*  TODO: copy to the scaledown code below  */  
                                 case 16:  
                                         if (d->vfb_type == VFB_HPC) {  
                                                 b = d->framebuffer[fb_addr] +  
                                                     (d->framebuffer[fb_addr+1]  
                                                     << 8);  
   
                                                 if (d->color32k) {  
                                                         r = b >> 11;  
                                                         g = b >> 5;  
                                                         r = r & 31;  
                                                         g = (g & 31) * 2;  
                                                         b = b & 31;  
                                                 } else if (d->psp_15bit) {  
                                                         int tmp;  
                                                         r = (b >> 10) & 0x1f;  
                                                         g = (b >>  5) & 0x1f;  
                                                         b = b & 0x1f;  
                                                         g <<= 1;  
                                                         tmp = r; r = b; b = tmp;  
                                                 } else {  
                                                         r = (b >> 11) & 0x1f;  
                                                         g = (b >>  5) & 0x3f;  
                                                         b = b & 0x1f;  
                                                 }  
                                         } else {  
                                             r = d->framebuffer[fb_addr] >> 3;  
                                             g = (d->framebuffer[fb_addr] << 5) +  
                                               (d->framebuffer[fb_addr + 1] >>5);  
                                             b = d->framebuffer[fb_addr + 1]&31;  
                                         }  
   
                                         r *= 8;  
                                         g *= 4;  
                                         b *= 8;  
                                         break;  
                                 default:  
                                         r = g = b = random() & 255;  
                                 }  
   
                                 macro_put_pixel();  
                                 x++;  
                         }  
                 }  
   
                 return;  
         }  
   
         /*  scaledown > 1:  */  
315    
316          scaledown = d->vfb_scaledown;  #define REDRAW  redraw_fallback
317          scaledownXscaledown = scaledown * scaledown;  #include "fb_include.c"
318    #undef REDRAW
319    
320    #define FB_24
321    #define REDRAW  redraw_24
322    #include "fb_include.c"
323    #undef REDRAW
324    #undef FB_24
325    #define FB_16
326    #define REDRAW  redraw_16
327    #include "fb_include.c"
328    #undef FB_16
329    #undef REDRAW
330    #define FB_15
331    #define REDRAW  redraw_15
332    #include "fb_include.c"
333    #undef REDRAW
334    #undef FB_15
335    
336    #define FB_BO
337    #define FB_24
338    #define REDRAW  redraw_24_bo
339    #include "fb_include.c"
340    #undef REDRAW
341    #undef FB_24
342    #define FB_16
343    #define REDRAW  redraw_16_bo
344    #include "fb_include.c"
345    #undef FB_16
346    #undef REDRAW
347    #define FB_15
348    #define REDRAW  redraw_15_bo
349    #include "fb_include.c"
350    #undef REDRAW
351    #undef FB_15
352    #undef FB_BO
353    
354    #define FB_SCALEDOWN
355    
356    #define REDRAW  redraw_fallback_sd
357    #include "fb_include.c"
358    #undef REDRAW
359    
360    #define FB_24
361    #define REDRAW  redraw_24_sd
362    #include "fb_include.c"
363    #undef REDRAW
364    #undef FB_24
365    #define FB_16
366    #define REDRAW  redraw_16_sd
367    #include "fb_include.c"
368    #undef FB_16
369    #undef REDRAW
370    #define FB_15
371    #define REDRAW  redraw_15_sd
372    #include "fb_include.c"
373    #undef REDRAW
374    #undef FB_15
375    
376    #define FB_BO
377    #define FB_24
378    #define REDRAW  redraw_24_bo_sd
379    #include "fb_include.c"
380    #undef REDRAW
381    #undef FB_24
382    #define FB_16
383    #define REDRAW  redraw_16_bo_sd
384    #include "fb_include.c"
385    #undef FB_16
386    #undef REDRAW
387    #define FB_15
388    #define REDRAW  redraw_15_bo_sd
389    #include "fb_include.c"
390    #undef REDRAW
391    #undef FB_15
392    #undef FB_BO
393    
394    void (*redraw[2 * 4 * 2])(struct vfb_data *, int, int) = {
395            redraw_fallback, redraw_fallback,
396            redraw_15, redraw_15_bo,
397            redraw_16, redraw_16_bo,
398            redraw_24, redraw_24_bo,
399            redraw_fallback_sd, redraw_fallback_sd,
400            redraw_15_sd, redraw_15_bo_sd,
401            redraw_16_sd, redraw_16_bo_sd,
402            redraw_24_sd, redraw_24_bo_sd  };
403    
404          /*  Which framebuffer pixel does addr correspond to?  */  #endif  /*  WITH_X11  */
         pixel = addr * 8 / d->bit_depth;  
         y = pixel / d->xsize;  
         x = pixel % d->xsize;  
   
         /*  How many framebuffer pixels?  */  
         npixels = len * 8 / d->bit_depth;  
   
         /*  Which x11 pixel?  */  
         x /= scaledown;  
         y /= scaledown;  
   
         /*  How many x11 pixels:  */  
         npixels /= scaledown;  
         if (npixels == 0)  
                 npixels = 1;  
   
         if (d->bit_depth < 8) {  
                 for (pixel=0; pixel<npixels; pixel++) {  
                         int subx, suby, r, g, b;  
                         color_r = color_g = color_b = 0;  
                         for (suby=0; suby<scaledown; suby++)  
                             for (subx=0; subx<scaledown; subx++) {  
                                 int fb_x, fb_y, fb_addr, c;  
   
                                 fb_x = x * scaledown + subx;  
                                 fb_y = y * scaledown + suby;  
                                 fb_addr = fb_y * d->xsize + fb_x;  
                                 fb_addr = fb_addr * d->bit_depth;  
                                 /*  fb_addr is now which _bit_ in  
                                     the framebuffer  */  
   
                                 c = d->framebuffer[fb_addr >> 3];  
                                 fb_addr &= 7;  
   
                                 /*  HPC is reverse:  */  
                                 if (d->vfb_type == VFB_HPC)  
                                         fb_addr = 8 - d->bit_depth - fb_addr;  
   
                                 c = (c >> fb_addr) & ((1<<d->bit_depth) - 1);  
                                 /*  c <<= (8 - d->bit_depth);  */  
   
                                 r = d->rgb_palette[c*3 + 0];  
                                 g = d->rgb_palette[c*3 + 1];  
                                 b = d->rgb_palette[c*3 + 2];  
   
                                 color_r += r;  
                                 color_g += g;  
                                 color_b += b;  
                             }  
   
                         r = color_r / scaledownXscaledown;  
                         g = color_g / scaledownXscaledown;  
                         b = color_b / scaledownXscaledown;  
                         macro_put_pixel();  
                         x++;  
                 }  
         } else if (d->bit_depth == 8) {  
                 for (pixel=0; pixel<npixels; pixel++) {  
                         int subx, suby, r, g, b;  
                         color_r = color_g = color_b = 0;  
                         for (suby=0; suby<scaledown; suby++)  
                             for (subx=0; subx<scaledown; subx++) {  
                                 int fb_x, fb_y, fb_addr, c;  
   
                                 fb_x = x * scaledown + subx;  
                                 fb_y = y * scaledown + suby;  
                                 fb_addr = fb_y * d->xsize + fb_x;  
                                 /*  fb_addr is which _byte_ in framebuffer  */  
                                 c = d->framebuffer[fb_addr] * 3;  
                                 r = d->rgb_palette[c + 0];  
                                 g = d->rgb_palette[c + 1];  
                                 b = d->rgb_palette[c + 2];  
                                 color_r += r;  
                                 color_g += g;  
                                 color_b += b;  
                             }  
   
                         r = color_r / scaledownXscaledown;  
                         g = color_g / scaledownXscaledown;  
                         b = color_b / scaledownXscaledown;  
                         macro_put_pixel();  
                         x++;  
                 }  
         } else {  
                 /*  Generic > 8 bit bit-depth:  */  
                 for (pixel=0; pixel<npixels; pixel++) {  
                         int subx, suby, r, g, b;  
                         color_r = color_g = color_b = 0;  
                         for (suby=0; suby<scaledown; suby++)  
                             for (subx=0; subx<scaledown; subx++) {  
                                 int fb_x, fb_y, fb_addr;  
   
                                 fb_x = x * scaledown + subx;  
                                 fb_y = y * scaledown + suby;  
                                 fb_addr = fb_y * d->xsize + fb_x;  
                                 fb_addr = (fb_addr * d->bit_depth) >> 3;  
                                 /*  fb_addr is which _byte_ in framebuffer  */  
   
                                 /*  > 8 bits color.  */  
                                 switch (d->bit_depth) {  
                                 case 24:  
                                         r = d->framebuffer[fb_addr];  
                                         g = d->framebuffer[fb_addr + 1];  
                                         b = d->framebuffer[fb_addr + 2];  
                                         break;  
                                 default:  
                                         r = g = b = random() & 255;  
                                 }  
                                 color_r += r;  
                                 color_g += g;  
                                 color_b += b;  
                             }  
                         r = color_r / scaledownXscaledown;  
                         g = color_g / scaledownXscaledown;  
                         b = color_b / scaledownXscaledown;  
                         macro_put_pixel();  
                         x++;  
                 }  
         }  
 }  
405    
406    
407  /*  DEVICE_TICK(fb)
  *  dev_fb_tick():  
  *  
  */  
 void dev_fb_tick(struct cpu *cpu, void *extra)  
408  {  {
409          struct vfb_data *d = extra;          struct vfb_data *d = extra;
410  #ifdef WITH_X11  #ifdef WITH_X11
# Line 621  void dev_fb_tick(struct cpu *cpu, void * Line 412  void dev_fb_tick(struct cpu *cpu, void *
412          int need_to_redraw_cursor = 0;          int need_to_redraw_cursor = 0;
413  #endif  #endif
414    
415          if (!cpu->machine->use_x11)          if (!cpu->machine->x11_md.in_use)
416                  return;                  return;
417    
418          do {          do {
# Line 738  void dev_fb_tick(struct cpu *cpu, void * Line 529  void dev_fb_tick(struct cpu *cpu, void *
529  #endif  #endif
530    
531          if (d->update_x2 != -1) {          if (d->update_x2 != -1) {
532                  int y, addr, addr2, q = d->vfb_scaledown;  #ifdef WITH_X11
533                    int y;
534    #endif
535                    int addr, addr2, q = d->vfb_scaledown;
536    
537                  if (d->update_x1 >= d->visible_xsize)                  if (d->update_x1 >= d->visible_xsize)
538                          d->update_x1 = d->visible_xsize - 1;                          d->update_x1 = d->visible_xsize - 1;
# Line 763  void dev_fb_tick(struct cpu *cpu, void * Line 557  void dev_fb_tick(struct cpu *cpu, void *
557                  addr2 = d->update_y1 * d->bytes_per_line +                  addr2 = d->update_y1 * d->bytes_per_line +
558                      d->update_x2 * d->bit_depth / 8;                      d->update_x2 * d->bit_depth / 8;
559    
560    #ifdef WITH_X11
561                  for (y=d->update_y1; y<=d->update_y2; y+=q) {                  for (y=d->update_y1; y<=d->update_y2; y+=q) {
562                          update_framebuffer(d, addr, addr2 - addr);                          d->redraw_func(d, addr, addr2 - addr);
563                          addr  += d->bytes_per_line * q;                          addr  += d->bytes_per_line * q;
564                          addr2 += d->bytes_per_line * q;                          addr2 += d->bytes_per_line * q;
565                  }                  }
566    
 #ifdef WITH_X11  
567                  XPutImage(d->fb_window->x11_display, d->fb_window->                  XPutImage(d->fb_window->x11_display, d->fb_window->
568                      x11_fb_window, d->fb_window->x11_fb_gc, d->fb_window->                      x11_fb_window, d->fb_window->x11_fb_gc, d->fb_window->
569                      fb_ximage, d->update_x1/d->vfb_scaledown, d->update_y1/                      fb_ximage, d->update_x1/d->vfb_scaledown, d->update_y1/
# Line 798  void dev_fb_tick(struct cpu *cpu, void * Line 592  void dev_fb_tick(struct cpu *cpu, void *
592                              cursor_xsize;                              cursor_xsize;
593                          d->fb_window->OLD_cursor_ysize = d->fb_window->                          d->fb_window->OLD_cursor_ysize = d->fb_window->
594                              cursor_ysize;                              cursor_ysize;
595                            need_to_flush_x11 = 1;
596                  }                  }
597          }          }
598  #endif  #endif
# Line 809  void dev_fb_tick(struct cpu *cpu, void * Line 604  void dev_fb_tick(struct cpu *cpu, void *
604  }  }
605    
606    
607  /*  DEVICE_ACCESS(fb)
  *  dev_fb_access():  
  */  
 int dev_fb_access(struct cpu *cpu, struct memory *mem,  
         uint64_t relative_addr, unsigned char *data, size_t len,  
         int writeflag, void *extra)  
608  {  {
609          struct vfb_data *d = extra;          struct vfb_data *d = extra;
610          int i;          size_t i;
611    
612  #ifdef FB_DEBUG  #ifdef FB_DEBUG
613          if (writeflag == MEM_WRITE) { if (data[0]) {          if (writeflag == MEM_WRITE) { if (data[0]) {
# Line 846  int dev_fb_access(struct cpu *cpu, struc Line 636  int dev_fb_access(struct cpu *cpu, struc
636    
637                          /*  If all bytes are equal to what is already stored                          /*  If all bytes are equal to what is already stored
638                              in the framebuffer, then simply return:  */                              in the framebuffer, then simply return:  */
639                          if (i==len-1)                          if (i == len-1)
640                                  return 1;                                  return 1;
641                  }                  }
642          }          }
# Line 856  int dev_fb_access(struct cpu *cpu, struc Line 646  int dev_fb_access(struct cpu *cpu, struc
646           *  of which area(s) we modify, so that the display isn't updated           *  of which area(s) we modify, so that the display isn't updated
647           *  unnecessarily.           *  unnecessarily.
648           */           */
649          if (writeflag == MEM_WRITE && cpu->machine->use_x11) {          if (writeflag == MEM_WRITE && cpu->machine->x11_md.in_use) {
650                  int x, y, x2,y2;                  int x, y, x2,y2;
651    
652                  x = (relative_addr % d->bytes_per_line) * 8 / d->bit_depth;                  x = (relative_addr % d->bytes_per_line) * 8 / d->bit_depth;
# Line 905  int dev_fb_access(struct cpu *cpu, struc Line 695  int dev_fb_access(struct cpu *cpu, struc
695          if (writeflag == MEM_WRITE) {          if (writeflag == MEM_WRITE) {
696                  if (len > 8)                  if (len > 8)
697                          memcpy(d->framebuffer + relative_addr, data, len);                          memcpy(d->framebuffer + relative_addr, data, len);
698                  else                  else {
699                          for (i=0; i<len; i++)                          for (i=0; i<len; i++)
700                                  d->framebuffer[relative_addr + i] = data[i];                                  d->framebuffer[relative_addr + i] = data[i];
701                    }
702          } else {          } else {
703                  if (len > 8)                  if (len > 8)
704                          memcpy(data, d->framebuffer + relative_addr, len);                          memcpy(data, d->framebuffer + relative_addr, len);
705                  else                  else {
706                          for (i=0; i<len; i++)                          for (i=0; i<len; i++)
707                                  data[i] = d->framebuffer[relative_addr + i];                                  data[i] = d->framebuffer[relative_addr + i];
708                    }
709          }          }
710    
711          return 1;          return 1;
# Line 936  int dev_fb_access(struct cpu *cpu, struc Line 728  int dev_fb_access(struct cpu *cpu, struc
728   *   *
729   *  VFB_DEC_VFB01, _VFB02, and VFB_DEC_MAXINE are DECstation specific.   *  VFB_DEC_VFB01, _VFB02, and VFB_DEC_MAXINE are DECstation specific.
730   *   *
731   *  If type is VFB_HPC, then color encoding differs from the generic case.   *  VFB_HPC is like generic, but the color encoding is done as on HPCmips
732     *  and Dreamcast.
733   *   *
734   *  If bit_depth = -15 (note the minus sign), then a special hack is used for   *  If bit_depth = -15 (note the minus sign), then a special hack is used for
735   *  the Playstation Portable's 5-bit R, 5-bit G, 5-bit B.   *  the Playstation Portable's 5-bit R, 5-bit G, 5-bit B.
# Line 948  struct vfb_data *dev_fb_init(struct mach Line 741  struct vfb_data *dev_fb_init(struct mach
741          struct vfb_data *d;          struct vfb_data *d;
742          size_t size, nlen;          size_t size, nlen;
743          int flags;          int flags;
744          char title[400];          int reverse_start = 0;
745          char *name2;          char *name2;
746    
747          d = malloc(sizeof(struct vfb_data));          CHECK_ALLOCATION(d = malloc(sizeof(struct vfb_data)));
         if (d == NULL) {  
                 fprintf(stderr, "out of memory\n");  
                 exit(1);  
         }  
748          memset(d, 0, sizeof(struct vfb_data));          memset(d, 0, sizeof(struct vfb_data));
749    
750            if (vfb_type & VFB_REVERSE_START) {
751                    vfb_type &= ~VFB_REVERSE_START;
752                    reverse_start = 1;
753            }
754    
755            d->memory = mem;
756          d->vfb_type = vfb_type;          d->vfb_type = vfb_type;
757    
758          /*  Defaults:  */          /*  Defaults:  */
# Line 1000  struct vfb_data *dev_fb_init(struct mach Line 795  struct vfb_data *dev_fb_init(struct mach
795                  d->ysize = ysize;  d->visible_ysize = d->ysize;                  d->ysize = ysize;  d->visible_ysize = d->ysize;
796                  d->bit_depth = 24;                  d->bit_depth = 24;
797                  break;                  break;
         default:  
                 ;  
798          }          }
799    
800          if (d->bit_depth == 2 || d->bit_depth == 4)          if (d->bit_depth == 2 || d->bit_depth == 4)
# Line 1009  struct vfb_data *dev_fb_init(struct mach Line 802  struct vfb_data *dev_fb_init(struct mach
802          else if (d->bit_depth == 8 || d->bit_depth == 1)          else if (d->bit_depth == 8 || d->bit_depth == 1)
803                  set_blackwhite_palette(d, 1 << d->bit_depth);                  set_blackwhite_palette(d, 1 << d->bit_depth);
804    
805          d->vfb_scaledown = machine->x11_scaledown;          d->vfb_scaledown = machine->x11_md.scaledown;
806    
807          d->bytes_per_line = d->xsize * d->bit_depth / 8;          d->bytes_per_line = d->xsize * d->bit_depth / 8;
808          size = d->ysize * d->bytes_per_line;          size = d->ysize * d->bytes_per_line;
809    
810          d->framebuffer = malloc(size);          CHECK_ALLOCATION(d->framebuffer = malloc(size));
         if (d->framebuffer == NULL) {  
                 fprintf(stderr, "out of memory\n");  
                 exit(1);  
         }  
811    
812          /*  Clear the framebuffer (all black pixels):  */          /*  Clear the framebuffer (all black pixels):  */
813          d->framebuffer_size = size;          d->framebuffer_size = size;
814          memset(d->framebuffer, 0, size);          memset(d->framebuffer, reverse_start? 255 : 0, size);
815    
816          d->x11_xsize = d->visible_xsize / d->vfb_scaledown;          d->x11_xsize = d->visible_xsize / d->vfb_scaledown;
817          d->x11_ysize = d->visible_ysize / d->vfb_scaledown;          d->x11_ysize = d->visible_ysize / d->vfb_scaledown;
818    
819          d->update_x1 = d->update_y1 = 99999;          /*  Only "update" from the start if we need to fill with white.  */
820          d->update_x2 = d->update_y2 = -1;          /*  (The Ximage will be black from the start anyway.)  */
821            if (reverse_start) {
822                    d->update_x1 = d->update_y1 = 0;
823                    d->update_x2 = d->xsize - 1;
824                    d->update_y2 = d->ysize - 1;
825            } else {
826                    d->update_x1 = d->update_y1 = 99999;
827                    d->update_x2 = d->update_y2 = -1;
828            }
829    
830          /*  Don't set the title to include the size of the framebuffer for          CHECK_ALLOCATION(d->name = strdup(name));
831              VGA, since then the resolution might change during runtime.  */          set_title(d);
         if (strcmp(name, "VGA") == 0)  
                 snprintf(title, sizeof(title),"GXemul: %s framebuffer", name);  
         else  
                 snprintf(title, sizeof(title),"GXemul: %ix%ix%i %s framebuffer",  
                     d->visible_xsize, d->visible_ysize, d->bit_depth, name);  
         title[sizeof(title)-1] = '\0';  
832    
833  #ifdef WITH_X11  #ifdef WITH_X11
834          if (machine->use_x11)          if (machine->x11_md.in_use) {
835                    int i = 0;
836                  d->fb_window = x11_fb_init(d->x11_xsize, d->x11_ysize,                  d->fb_window = x11_fb_init(d->x11_xsize, d->x11_ysize,
837                      title, machine->x11_scaledown, machine);                      d->title, machine->x11_md.scaledown, machine);
838          else                  switch (d->fb_window->x11_screen_depth) {
839                    case 15: i = 2; break;
840                    case 16: i = 4; break;
841                    case 24: i = 6; break;
842                    }
843                    if (d->fb_window->fb_ximage->byte_order)
844                            i ++;
845                    if (d->vfb_scaledown > 1)
846                            i += 8;
847                    d->redraw_func = redraw[i];
848            } else
849  #endif  #endif
850                  d->fb_window = NULL;                  d->fb_window = NULL;
851    
852          nlen = strlen(name) + 10;          nlen = strlen(name) + 10;
853          name2 = malloc(nlen);          CHECK_ALLOCATION(name2 = malloc(nlen));
854          if (name2 == NULL) {  
                 fprintf(stderr, "out of memory in dev_fb_init()\n");  
                 exit(1);  
         }  
855          snprintf(name2, nlen, "fb [%s]", name);          snprintf(name2, nlen, "fb [%s]", name);
856    
857          flags = DM_DEFAULT;          flags = DM_DEFAULT;
# Line 1066  struct vfb_data *dev_fb_init(struct mach Line 864  struct vfb_data *dev_fb_init(struct mach
864              d, flags, d->framebuffer);              d, flags, d->framebuffer);
865    
866          machine_add_tickfunction(machine, dev_fb_tick, d, FB_TICK_SHIFT);          machine_add_tickfunction(machine, dev_fb_tick, d, FB_TICK_SHIFT);
867    
868          return d;          return d;
869  }  }
870    

Legend:
Removed from v.20  
changed lines
  Added in v.42

  ViewVC Help
Powered by ViewVC 1.1.26