src/video/nds/SDL_ndsrender.c
author Darren Alton <dalton@stevens.edu>
Thu, 10 Jul 2008 23:35:01 +0000
branchgsoc2008_nds
changeset 2678 3895761db26a
parent 2677 6386764eb222
child 2679 bc3e3e889f6d
permissions -rw-r--r--
Removed old touchscreen system, starting to implement mouse-based touchscreen. Also, more work on a non-software-rendering video driver
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #include <stdio.h>
    24 #include <stdlib.h>
    25 #include <nds.h>
    26 
    27 #include "SDL_config.h"
    28 
    29 #include "SDL_video.h"
    30 #include "../SDL_sysvideo.h"
    31 #include "../SDL_yuv_sw_c.h"
    32 #include "../SDL_renderer_sw.h"
    33 
    34 
    35 /* SDL surface based renderer implementation */
    36 
    37 static SDL_Renderer *NDS_CreateRenderer(SDL_Window * window, Uint32 flags);
    38 static int NDS_ActivateRenderer(SDL_Renderer * renderer);
    39 static int NDS_DisplayModeChanged(SDL_Renderer * renderer);
    40 static int NDS_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    41 static int NDS_QueryTexturePixels(SDL_Renderer * renderer,
    42                                  SDL_Texture * texture, void **pixels,
    43                                  int *pitch);
    44 static int NDS_SetTexturePalette(SDL_Renderer * renderer,
    45                                 SDL_Texture * texture,
    46                                 const SDL_Color * colors, int firstcolor,
    47                                 int ncolors);
    48 static int NDS_GetTexturePalette(SDL_Renderer * renderer,
    49                                 SDL_Texture * texture, SDL_Color * colors,
    50                                 int firstcolor, int ncolors);
    51 static int NDS_SetTextureColorMod(SDL_Renderer * renderer,
    52                                  SDL_Texture * texture);
    53 static int NDS_SetTextureAlphaMod(SDL_Renderer * renderer,
    54                                  SDL_Texture * texture);
    55 static int NDS_SetTextureBlendMode(SDL_Renderer * renderer,
    56                                   SDL_Texture * texture);
    57 static int NDS_SetTextureScaleMode(SDL_Renderer * renderer,
    58                                   SDL_Texture * texture);
    59 static int NDS_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    60                             const SDL_Rect * rect, const void *pixels,
    61                             int pitch);
    62 static int NDS_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    63                           const SDL_Rect * rect, int markDirty, void **pixels,
    64                           int *pitch);
    65 static void NDS_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    66 static void NDS_DirtyTexture(SDL_Renderer * renderer,
    67                             SDL_Texture * texture, int numrects,
    68                             const SDL_Rect * rects);
    69 static int NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g,
    70                               Uint8 b, Uint8 a, const SDL_Rect * rect);
    71 static int NDS_RenderCopy(SDL_Renderer * renderer,
    72                               SDL_Texture * texture,
    73                               const SDL_Rect * srcrect,
    74                               const SDL_Rect * dstrect);
    75 static void NDS_RenderPresent(SDL_Renderer * renderer);
    76 static void NDS_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    77 static void NDS_DestroyRenderer(SDL_Renderer * renderer);
    78 
    79 
    80 SDL_RenderDriver NDS_RenderDriver = {
    81     NDS_CreateRenderer,
    82     {"nds", SDL_RENDERER_SINGLEBUFFER}
    83 /*SDL_RENDERER_ values
    84 SINGLEBUFFER   Render directly to the window, if possible
    85 PRESENTCOPY    Present uses a copy from back buffer to the front buffer
    86 PRESENTFLIP2   Present uses a flip, swapping back buffer and front buffer
    87 PRESENTFLIP3   Present uses a flip, rotating two back buf.s and a front buf.
    88 PRESENTDISCARD Present leaves the contents of the backbuffer undefined
    89 PRESENTVSYNC   Present is synchronized with the refresh rate
    90 ACCELERATED    The renderer uses hardware acceleration
    91 */
    92 };
    93 
    94 typedef struct
    95 {
    96     int current_screen;
    97     u16* fb;
    98 } NDS_RenderData;
    99 
   100 typedef struct
   101 {
   102     enum { NDSTX_BG, NDSTX_SPR } type;
   103     struct { int w, h, pitch, bpp; } dim;
   104     u16 *vram;
   105 } NDS_TextureData;
   106 
   107 
   108 /* this is mainly hackish testing/debugging stuff to get cleaned up soon
   109    anything named sdlds_blah shouldn't make it into the stable version
   110  */
   111 
   112 u16
   113 sdlds_rgb2bgr(u16 c)
   114 {
   115 /* hack to get the proper colors until I actually get BGR555 to work right */
   116     u16 Rmask = 0x7C00, Bmask = 0x001F, GAmask = 0x83E0, r, b;
   117     r = (c & Rmask) >> 10;
   118     b = (c & Bmask) << 10;
   119     return (c & GAmask) | r | b;
   120 }
   121 
   122 void
   123 sdlds_surf2vram(SDL_Surface * s)
   124 {
   125     if (s->w == 256) {
   126         u16 tmpbuf[0x20000];
   127         int i;
   128 
   129         dmaCopy((u8 *) (s->pixels) + 156 * sizeof(u16),
   130                 tmpbuf, 256 * 192 * sizeof(u16));
   131     /* hack to fix the pixel format until I figure out why BGR doesn't work */
   132         for (i = 0; i < 256 * 192; ++i) {
   133             tmpbuf[i] = sdlds_rgb2bgr(tmpbuf[i]);
   134         }
   135         dmaCopy(tmpbuf, VRAM_A, 256 * 192 * sizeof(u16));
   136     }
   137 }
   138 
   139 void
   140 sdlds_print_pixfmt_info(SDL_PixelFormat * f)
   141 {
   142     if (!f)
   143         return;
   144     printf("bpp: %d\nRGBA: %x %x %x %x\n",
   145            f->BitsPerPixel, f->Rmask, f->Gmask, f->Bmask, f->Amask);
   146 }
   147 
   148 void
   149 sdlds_print_surface_info(SDL_Surface * s)
   150 {
   151     if (!s)
   152         return;
   153     printf("flags: %x\nsize: %dx%d, pitch: %d\nlocked: %d, refcount: %d\n",
   154            s->flags, s->w, s->h, s->pitch, s->locked, s->refcount);
   155     sdlds_print_pixfmt_info(s->format);
   156 }
   157 
   158 /* again the above shouldn't make it into the stable version */
   159 
   160 SDL_Renderer *
   161 NDS_CreateRenderer(SDL_Window * window, Uint32 flags)
   162 {
   163     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   164     SDL_DisplayMode *displayMode = &display->current_mode;
   165     SDL_Renderer *renderer;
   166     NDS_RenderData *data;
   167     int i, n;
   168     int bpp = 15;
   169     Uint32 Rmask, Gmask, Bmask, Amask;
   170 /*  Uint32 Rmask = 0x7C00, Gmask = 0x03E0, Bmask = 0x001F, Amask = 0x8000;
   171     Uint32 Rmask = 0x001F, Gmask = 0x03E0, Bmask = 0x7C00, Amask = 0x8000;
   172 */
   173 
   174     /* hard coded this to BGR555 for now */
   175     if (!SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_BGR555, &bpp,
   176                                     &Rmask, &Gmask, &Bmask, &Amask)) {
   177         SDL_SetError("Unknown display format");
   178         return NULL;
   179     }
   180 
   181     renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
   182     if (!renderer) {
   183         SDL_OutOfMemory();
   184         return NULL;
   185     }
   186 
   187     data = (NDS_RenderData *) SDL_malloc(sizeof(*data));
   188     if (!data) {
   189         NDS_DestroyRenderer(renderer);
   190         SDL_OutOfMemory();
   191         return NULL;
   192     }
   193     SDL_zerop(data);
   194 
   195     renderer->RenderFill = NDS_RenderFill;
   196     renderer->RenderCopy = NDS_RenderCopy;
   197     renderer->RenderPresent = NDS_RenderPresent;
   198     renderer->DestroyRenderer = NDS_DestroyRenderer;
   199     renderer->info.name = NDS_RenderDriver.info.name;
   200     renderer->info.flags = 0;
   201     renderer->window = window->id;
   202     renderer->driverdata = data;
   203     Setup_SoftwareRenderer(renderer); /* TODO: well, "TODON'T" is more like it */
   204 
   205     if (flags & SDL_RENDERER_PRESENTFLIP2) {
   206         renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
   207         n = 2;
   208     } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
   209         renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
   210         n = 3;
   211     } else {
   212         renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
   213         n = 1;
   214     }
   215     /*
   216     for (i = 0; i < n; ++i) {
   217         data->screens[i] =
   218             SDL_CreateRGBSurface(0, 256, 192, bpp, Rmask, Gmask, Bmask,
   219                                  Amask);
   220         if (!data->screens[i]) {
   221             NDS_DestroyRenderer(renderer);
   222             return NULL;
   223         }
   224         SDL_SetSurfacePalette(data->screens[i], display->palette);
   225         sdlds_print_surface_info(data->screens[i]);
   226     }*/
   227 
   228     data->fb = (u16*)0x06020000;
   229 
   230     return renderer;
   231 }
   232 
   233 static int
   234 NDS_ActivateRenderer(SDL_Renderer * renderer)
   235 {
   236     NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
   237     /* stub. TODO: figure out what needs to be done, if anything. */
   238     return 0;
   239 }
   240 
   241 static int
   242 NDS_DisplayModeChanged(SDL_Renderer * renderer)
   243 {
   244     NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
   245     /* stub. TODO: figure out what needs to be done */
   246     return 0;
   247 }
   248 
   249 static int
   250 NDS_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   251 {
   252     NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
   253 
   254     if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   255         SDL_SetError("Unsupported texture format");
   256         return -1;
   257     } else {
   258         int bpp;
   259         Uint32 Rmask, Gmask, Bmask, Amask;
   260 
   261         if (!SDL_PixelFormatEnumToMasks
   262             (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
   263             SDL_SetError("Unknown texture format");
   264             return -1;
   265         }
   266         /* TODO: appropriate checks for ABGR1555 */
   267         texture->driverdata = SDL_calloc(1, sizeof(NDS_TextureData));
   268         /* TODO: conditional statements on w/h to place it as bg/sprite */
   269     }
   270 
   271     if (!texture->driverdata) {
   272         return -1;
   273     }
   274     return 0;
   275 }
   276 
   277 static int
   278 NDS_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
   279                       void **pixels, int *pitch)
   280 {
   281     if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   282         SDL_SetError("Unsupported texture format");
   283         return -1;
   284     } else {
   285         NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
   286 
   287         *pixels = txdat->vram;
   288         *pitch = txdat->dim.pitch;
   289         return 0;
   290     }
   291 }
   292 
   293 static int
   294 NDS_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   295                      const SDL_Color * colors, int firstcolor, int ncolors)
   296 {
   297     if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   298         SDL_SetError("YUV textures don't have a palette");
   299         return -1;
   300     } else {
   301         NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
   302         /* TODO: mess with 8-bit modes and/or 16-color palette modes */
   303         return 0;
   304     }
   305 }
   306 
   307 static int
   308 NDS_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   309                      SDL_Color * colors, int firstcolor, int ncolors)
   310 {
   311     if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   312         SDL_SetError("YUV textures don't have a palette");
   313         return -1;
   314     } else {
   315         NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
   316         /* TODO: mess with 8-bit modes and/or 16-color palette modes */
   317         return 0;
   318     }
   319 }
   320 
   321 static int
   322 NDS_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
   323 {
   324     /* stub. TODO: figure out what needs to be done, if anything */
   325     return 0;
   326 }
   327 
   328 static int
   329 NDS_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
   330 {
   331     /* stub. TODO: figure out what needs to be done, if anything */
   332     return 0;
   333 }
   334 
   335 static int
   336 NDS_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
   337 {
   338     /* stub. TODO: figure out what needs to be done, if anything */
   339     return 0;
   340 }
   341 
   342 static int
   343 NDS_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
   344 {
   345     /* stub. TODO: figure out what needs to be done.
   346        (NDS hardware scaling is nearest neighbor.) */
   347     return 0;
   348 }
   349 
   350 static int
   351 NDS_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   352                  const SDL_Rect * rect, const void *pixels, int pitch)
   353 {
   354     if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   355         SDL_SetError("Unsupported texture format");
   356         return -1;
   357     } else {
   358         NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
   359         Uint8 *src, *dst;
   360         int row;
   361         size_t length;
   362         /* IMPORTANT! copy the new pixels into the sprite or bg. */
   363         src = (Uint8 *) pixels;
   364         dst =
   365             (Uint8 *) txdat->vram + rect->y * txdat->dim.pitch +
   366             rect->x * (txdat->dim.bpp/8);
   367         length = rect->w * (txdat->dim.bpp/8);
   368         for (row = 0; row < rect->h; ++row) {
   369             SDL_memcpy(dst, src, length);
   370             src += pitch;
   371             dst += surface->pitch;
   372         }
   373         return 0;
   374     }
   375 }
   376 
   377 static int
   378 NDS_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   379                const SDL_Rect * rect, int markDirty, void **pixels,
   380                int *pitch)
   381 {
   382     if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   383         SDL_SetError("Unsupported texture format");
   384         return -1;
   385     } else {
   386         NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
   387 
   388         *pixels = (void *) ((u8 *)txdat->vram + rect->y * txdat->dim.pitch
   389                             + rect->x * (txdat->dim.bpp/8));
   390         *pitch = txdat->dim.pitch;
   391         return 0;
   392     }
   393 }
   394 
   395 static void
   396 NDS_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   397 {
   398     if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   399         SDL_SetError("Unsupported texture format");
   400     }
   401 }
   402 
   403 static void
   404 NDS_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   405                 int numrects, const SDL_Rect * rects)
   406 { /* stub */
   407 }
   408 
   409 static int
   410 NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
   411                    Uint8 a, const SDL_Rect * rect)
   412 {
   413     NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
   414     SDL_Rect real_rect = *rect;
   415     u16 color;
   416     int i, j;
   417 
   418     color = RGB15(r>>3,g>>3,b>>3);
   419     for (i = real_rect.x; i < real_rect.x+real_rect.w; ++i) {
   420         for (j = real_rect.y; j < real_rect.y+real_rect.h; ++j) {
   421             data->fb[(j + real_rect.y) * 256 + i + real_rect.x] = 
   422                 0x8000 | color;
   423         }
   424     }
   425     return 0;
   426 }
   427 
   428 static int
   429 NDS_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   430                    const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   431 {
   432     NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
   433     SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   434     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   435 
   436 #if 0
   437     if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   438         SDL_Surface *target = data->screens[data->current_screen];
   439         void *pixels =
   440             (Uint8 *) target->pixels + dstrect->y * target->pitch +
   441             dstrect->x * target->format->BytesPerPixel;
   442         return SDL_SW_CopyYUVToRGB((SDL_SW_YUVTexture *) texture->driverdata,
   443                                    srcrect, display->current_mode.format,
   444                                    dstrect->w, dstrect->h, pixels,
   445                                    target->pitch);
   446     } else {
   447         SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
   448         SDL_Surface *target = data->screens[data->current_screen];
   449         SDL_Rect real_srcrect = *srcrect;
   450         SDL_Rect real_dstrect = *dstrect;
   451         /*sdlds_print_surface_info(surface);
   452            sdlds_print_surface_info(target); */
   453         sdlds_surf2vram(surface);
   454         return SDL_LowerBlit(surface, &real_srcrect, target, &real_dstrect);
   455     }
   456 #endif
   457     /* copy it directly to vram */
   458     SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
   459     sdlds_surf2vram(surface);
   460     /*
   461     int sx = srcrect->x, sy = srcrect->y, sw = srcrect->w, sh = srcrect->h;
   462     int dx = dstrect->x, dy = dstrect->y, dw = dstrect->w, dh = dstrect->h;
   463     int si, sj, di, dj;
   464     for (sj = 0, dj = 0; sj < sh && dj < dh; ++sj, ++dj) {
   465         for (si = 0, di = 0; si < sw && di < dw; ++si, ++di) {
   466             data->fb[(dj + dy) * 256 + di + dx] = 0x8000 |
   467                 ((u16 *) surface->pixels)[(sj + sy) * (surface->w) + si +
   468                                              sx];
   469         }
   470     }*/
   471     return 0;
   472 }
   473 
   474 
   475 static void
   476 NDS_RenderPresent(SDL_Renderer * renderer)
   477 {
   478     NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
   479     /* Send the data to the display TODO */
   480 
   481     /* Update the flipping chain, if any */
   482     if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
   483         data->current_screen = (data->current_screen + 1) % 2;
   484     } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {
   485         data->current_screen = (data->current_screen + 1) % 3;
   486     }
   487 }
   488 
   489 static void
   490 NDS_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   491 {
   492     if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   493         SDL_SetError("Unsupported texture format");
   494     } else {
   495         /* TODO: free anything allocated for texture */
   496         /*SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
   497         SDL_FreeSurface(surface);*/
   498     }
   499 }
   500 
   501 static void
   502 NDS_DestroyRenderer(SDL_Renderer * renderer)
   503 {
   504     NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
   505     /*SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   506     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);*/
   507     int i;
   508 
   509     if (data) {
   510         for (i = 0; i < SDL_arraysize(data->texture); ++i) {
   511             if (data->texture[i]) {
   512                 DestroyTexture(data->renderer, data->texture[i]);
   513             }
   514         }
   515         if (data->surface.format) {
   516             SDL_SetSurfacePalette(&data->surface, NULL);
   517             SDL_FreeFormat(data->surface.format);
   518         }
   519         if (display->palette) {
   520             SDL_DelPaletteWatch(display->palette, DisplayPaletteChanged,
   521                                 data);
   522         }
   523         SDL_FreeDirtyRects(&data->dirty);
   524         SDL_free(data);
   525     }
   526     SDL_free(renderer);
   527 }
   528 
   529 /* vi: set ts=4 sw=4 expandtab: */