src/video/qnxgf/SDL_gf_render.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Jan 2011 22:44:43 -0800
changeset 5138 da10636e5eca
parent 4929 aa8888658021
child 5140 e743b9c3f6d6
permissions -rw-r--r--
Making the API simpler, scaling is always defined as linear interpolation and should be supported as much as possible on all renderers.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 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     QNX Graphics Framework SDL driver
    23     Copyright (C) 2009 Mike Gorchak
    24     (mike@malva.ua, lestat@i.com.ua)
    25 */
    26 
    27 #include "SDL_config.h"
    28 
    29 #include "../SDL_pixels_c.h"
    30 #include "../SDL_yuv_sw_c.h"
    31 
    32 #include "SDL_video.h"
    33 
    34 #include "SDL_gf_render.h"
    35 #include "SDL_qnxgf.h"
    36 
    37 static SDL_Renderer *gf_createrenderer(SDL_Window * window, Uint32 flags);
    38 static int gf_displaymodechanged(SDL_Renderer * renderer);
    39 static int gf_activaterenderer(SDL_Renderer * renderer);
    40 static int gf_createtexture(SDL_Renderer * renderer, SDL_Texture * texture);
    41 static int gf_querytexturepixels(SDL_Renderer * renderer,
    42                                  SDL_Texture * texture, void **pixels,
    43                                  int *pitch);
    44 static int gf_settexturepalette(SDL_Renderer * renderer,
    45                                 SDL_Texture * texture,
    46                                 const SDL_Color * colors, int firstcolor,
    47                                 int ncolors);
    48 static int gf_gettexturepalette(SDL_Renderer * renderer,
    49                                 SDL_Texture * texture, SDL_Color * colors,
    50                                 int firstcolor, int ncolors);
    51 static int gf_settexturecolormod(SDL_Renderer * renderer,
    52                                  SDL_Texture * texture);
    53 static int gf_settexturealphamod(SDL_Renderer * renderer,
    54                                  SDL_Texture * texture);
    55 static int gf_settextureblendmode(SDL_Renderer * renderer,
    56                                   SDL_Texture * texture);
    57 static int gf_settexturescalemode(SDL_Renderer * renderer,
    58                                   SDL_Texture * texture);
    59 static int gf_updatetexture(SDL_Renderer * renderer, SDL_Texture * texture,
    60                             const SDL_Rect * rect, const void *pixels,
    61                             int pitch);
    62 static int gf_locktexture(SDL_Renderer * renderer, SDL_Texture * texture,
    63                           const SDL_Rect * rect, int markDirty, void **pixels,
    64                           int *pitch);
    65 static void gf_unlocktexture(SDL_Renderer * renderer, SDL_Texture * texture);
    66 static void gf_dirtytexture(SDL_Renderer * renderer, SDL_Texture * texture,
    67                             int numrects, const SDL_Rect * rects);
    68 static int gf_renderpoint(SDL_Renderer * renderer, int x, int y);
    69 static int gf_renderline(SDL_Renderer * renderer, int x1, int y1, int x2,
    70                          int y2);
    71 static int gf_renderfill(SDL_Renderer * renderer, const SDL_Rect * rect);
    72 static int gf_rendercopy(SDL_Renderer * renderer, SDL_Texture * texture,
    73                          const SDL_Rect * srcrect, const SDL_Rect * dstrect);
    74 static void gf_renderpresent(SDL_Renderer * renderer);
    75 static void gf_destroytexture(SDL_Renderer * renderer, SDL_Texture * texture);
    76 static void gf_destroyrenderer(SDL_Renderer * renderer);
    77 
    78 SDL_RenderDriver gf_renderdriver = {
    79     gf_createrenderer,
    80     {
    81      "qnxgf",
    82      (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY |
    83       SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 |
    84       SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_PRESENTDISCARD |
    85       SDL_RENDERER_ACCELERATED),
    86      (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
    87       SDL_TEXTUREMODULATE_ALPHA),
    88      (SDL_BLENDMODE_NONE | SDL_BLENDMODE_MASK |
    89       SDL_BLENDMODE_BLEND | SDL_BLENDMODE_ADD | SDL_BLENDMODE_MOD),
    90      13,
    91      {
    92       SDL_PIXELFORMAT_INDEX8,
    93       SDL_PIXELFORMAT_RGB555,
    94       SDL_PIXELFORMAT_RGB565,
    95       SDL_PIXELFORMAT_RGB888,
    96       SDL_PIXELFORMAT_BGR888,
    97       SDL_PIXELFORMAT_ARGB8888,
    98       SDL_PIXELFORMAT_RGBA8888,
    99       SDL_PIXELFORMAT_ABGR8888,
   100       SDL_PIXELFORMAT_BGRA8888,
   101       SDL_PIXELFORMAT_YV12,
   102       SDL_PIXELFORMAT_YUY2,
   103       SDL_PIXELFORMAT_UYVY,
   104       SDL_PIXELFORMAT_YVYU},
   105      0,
   106      0}
   107 };
   108 
   109 static SDL_Renderer *
   110 gf_createrenderer(SDL_Window * window, Uint32 flags)
   111 {
   112     SDL_VideoDisplay *display = window->display;
   113     SDL_DisplayData *didata = (SDL_DisplayData *) display->driverdata;
   114     SDL_WindowData *wdata = (SDL_WindowData *) window->driverdata;
   115     SDL_Renderer *renderer = NULL;
   116     SDL_RenderData *rdata = NULL;
   117     uint32_t it;
   118     int32_t jt;
   119     int32_t status;
   120 
   121     /* Check if it is OpenGL ES window */
   122     if ((window->flags & SDL_WINDOW_OPENGL) == SDL_WINDOW_OPENGL) {
   123         /* No error, just no need to create 2D renderer for OpenGL ES window */
   124         return NULL;
   125     }
   126 
   127     /* Allocate new renderer structure */
   128     renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(SDL_Renderer));
   129     if (renderer == NULL) {
   130         SDL_OutOfMemory();
   131         return NULL;
   132     }
   133 
   134     /* Allocate renderer data */
   135     rdata = (SDL_RenderData *) SDL_calloc(1, sizeof(SDL_RenderData));
   136     if (rdata == NULL) {
   137         SDL_free(renderer);
   138         SDL_OutOfMemory();
   139         return NULL;
   140     }
   141 
   142     renderer->DisplayModeChanged = gf_displaymodechanged;
   143     renderer->ActivateRenderer = gf_activaterenderer;
   144     renderer->CreateTexture = gf_createtexture;
   145     renderer->QueryTexturePixels = gf_querytexturepixels;
   146     renderer->SetTexturePalette = gf_settexturepalette;
   147     renderer->GetTexturePalette = gf_gettexturepalette;
   148     renderer->SetTextureAlphaMod = gf_settexturealphamod;
   149     renderer->SetTextureColorMod = gf_settexturecolormod;
   150     renderer->SetTextureBlendMode = gf_settextureblendmode;
   151     renderer->UpdateTexture = gf_updatetexture;
   152     renderer->LockTexture = gf_locktexture;
   153     renderer->UnlockTexture = gf_unlocktexture;
   154     renderer->DirtyTexture = gf_dirtytexture;
   155     renderer->RenderPoint = gf_renderpoint;
   156     renderer->RenderLine = gf_renderline;
   157     renderer->RenderFill = gf_renderfill;
   158     renderer->RenderCopy = gf_rendercopy;
   159     renderer->RenderPresent = gf_renderpresent;
   160     renderer->DestroyTexture = gf_destroytexture;
   161     renderer->DestroyRenderer = gf_destroyrenderer;
   162     renderer->info = gf_renderdriver.info;
   163     renderer->window = window;
   164     renderer->driverdata = rdata;
   165 
   166     /* Set render acceleration flag in case it is accelerated */
   167     if ((didata->caps & SDL_GF_ACCELERATED) == SDL_GF_ACCELERATED) {
   168         renderer->info.flags = SDL_RENDERER_ACCELERATED;
   169     } else {
   170         renderer->info.flags &= ~(SDL_RENDERER_ACCELERATED);
   171     }
   172 
   173     rdata->window = window;
   174 
   175     /* Check if upper level requested synchronization on vsync signal */
   176     if ((flags & SDL_RENDERER_PRESENTVSYNC) == SDL_RENDERER_PRESENTVSYNC) {
   177         rdata->enable_vsync = SDL_TRUE;
   178     } else {
   179         rdata->enable_vsync = SDL_FALSE;
   180     }
   181 
   182     /* Check what buffer copy/flip scheme is requested */
   183     rdata->surfaces_count = 0;
   184     if ((flags & SDL_RENDERER_SINGLEBUFFER) == SDL_RENDERER_SINGLEBUFFER) {
   185         if ((flags & SDL_RENDERER_PRESENTDISCARD) ==
   186             SDL_RENDERER_PRESENTDISCARD) {
   187             renderer->info.flags |=
   188                 SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD;
   189         } else {
   190             renderer->info.flags |=
   191                 SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY;
   192         }
   193         rdata->surfaces_count = 1;
   194         rdata->surface_visible_idx = 0;
   195         rdata->surface_render_idx = 0;
   196     } else {
   197         if ((flags & SDL_RENDERER_PRESENTFLIP2) == SDL_RENDERER_PRESENTFLIP2) {
   198             renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
   199             rdata->surfaces_count = 2;
   200             rdata->surface_visible_idx = 0;
   201             rdata->surface_render_idx = 1;
   202         } else {
   203             if ((flags & SDL_RENDERER_PRESENTFLIP3) ==
   204                 SDL_RENDERER_PRESENTFLIP3) {
   205                 renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
   206                 rdata->surfaces_count = 3;
   207                 rdata->surface_visible_idx = 0;
   208                 rdata->surface_render_idx = 1;
   209             } else {
   210                 renderer->info.flags |=
   211                     SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY;
   212                 rdata->surfaces_count = 1;
   213                 rdata->surface_visible_idx = 0;
   214                 rdata->surface_render_idx = 0;
   215             }
   216         }
   217     }
   218 
   219     /* Create layer surfaces, which could be visible */
   220     for (it = 0; it < rdata->surfaces_count; it++) {
   221         /* TODO: add palette creation */
   222 
   223         /* Create displayable surfaces */
   224         status =
   225             gf_surface_create_layer(&rdata->surface[it], &didata->layer, 1, 0,
   226                                     didata->current_mode.w,
   227                                     didata->current_mode.h,
   228                                     qnxgf_sdl_to_gf_pixelformat(didata->
   229                                                                 current_mode.
   230                                                                 format), NULL,
   231                                     GF_SURFACE_CREATE_2D_ACCESSIBLE);
   232 
   233         if (status != GF_ERR_OK) {
   234             /* Free already allocated surfaces */
   235             for (jt = it - 1; jt > 0; jt--) {
   236                 gf_surface_free(rdata->surface[jt]);
   237                 rdata->surface[jt] = NULL;
   238             }
   239             SDL_free(rdata);
   240             SDL_free(renderer);
   241             if (status == GF_ERR_MEM) {
   242                 SDL_SetError("unsufficient free video memory");
   243             } else {
   244                 SDL_SetError("error during displayable surface creation");
   245             }
   246             return NULL;
   247         }
   248 
   249         /* Get detailed information about allocated surface */
   250         gf_surface_get_info(rdata->surface[it], &rdata->surface_info[it]);
   251     }
   252 
   253     return renderer;
   254 }
   255 
   256 void
   257 gf_addrenderdriver(_THIS)
   258 {
   259     uint32_t it;
   260 
   261     for (it = 0; it < _this->num_displays; it++) {
   262         SDL_AddRenderDriver(&_this->displays[it], &gf_renderdriver);
   263     }
   264 }
   265 
   266 /****************************************************************************/
   267 /* SDL render interface                                                     */
   268 /****************************************************************************/
   269 static int
   270 gf_displaymodechanged(SDL_Renderer * renderer)
   271 {
   272     SDL_RenderData *rdata = (SDL_RenderData *) renderer->driverdata;
   273 
   274     /* Remove all allocated surfaces, they are no more valid */
   275 
   276     /* TODO: Add video mode change detection and new parameters detection */
   277 
   278     return 0;
   279 }
   280 
   281 static int
   282 gf_activaterenderer(SDL_Renderer * renderer)
   283 {
   284     SDL_RenderData *rdata = (SDL_RenderData *) renderer->driverdata;
   285     SDL_VideoDisplay *display = rdata->window->display;
   286     SDL_DisplayData *didata = (SDL_DisplayData *) display->driverdata;
   287 
   288     /* Setup current surface as visible */
   289 //   gf_layer_set_surfaces(didata->layer, &rdata->surface[rdata->surface_visible_idx], 1);
   290 
   291     /* Set visible surface when hardware in idle state */
   292 //   gf_layer_update(didata->layer, GF_LAYER_UPDATE_NO_WAIT_IDLE);
   293 
   294     return 0;
   295 }
   296 
   297 static int
   298 gf_createtexture(SDL_Renderer * renderer, SDL_Texture * texture)
   299 {
   300     SDL_RenderData *renderdata = (SDL_RenderData *) renderer->driverdata;
   301     SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   302     SDL_VideoDisplay *display = window->display;
   303     SDL_TextureData *tdata = NULL;
   304 
   305     /* Allocate texture driver data */
   306     tdata = (SDL_TextureData *) SDL_calloc(1, sizeof(SDL_TextureData));
   307     if (tdata == NULL) {
   308         SDL_OutOfMemory();
   309         return -1;
   310     }
   311 
   312     /* Set texture driver data */
   313     texture->driverdata = tdata;
   314 
   315 }
   316 
   317 static int
   318 gf_querytexturepixels(SDL_Renderer * renderer, SDL_Texture * texture,
   319                       void **pixels, int *pitch)
   320 {
   321 }
   322 
   323 static int
   324 gf_settexturepalette(SDL_Renderer * renderer, SDL_Texture * texture,
   325                      const SDL_Color * colors, int firstcolor, int ncolors)
   326 {
   327 }
   328 
   329 static int
   330 gf_gettexturepalette(SDL_Renderer * renderer, SDL_Texture * texture,
   331                      SDL_Color * colors, int firstcolor, int ncolors)
   332 {
   333 }
   334 
   335 static int
   336 gf_settexturecolormod(SDL_Renderer * renderer, SDL_Texture * texture)
   337 {
   338 }
   339 
   340 static int
   341 gf_settexturealphamod(SDL_Renderer * renderer, SDL_Texture * texture)
   342 {
   343 }
   344 
   345 static int
   346 gf_settextureblendmode(SDL_Renderer * renderer, SDL_Texture * texture)
   347 {
   348 }
   349 
   350 static int
   351 gf_settexturescalemode(SDL_Renderer * renderer, SDL_Texture * texture)
   352 {
   353 }
   354 
   355 static int
   356 gf_updatetexture(SDL_Renderer * renderer, SDL_Texture * texture,
   357                  const SDL_Rect * rect, const void *pixels, int pitch)
   358 {
   359 }
   360 
   361 static int
   362 gf_locktexture(SDL_Renderer * renderer, SDL_Texture * texture,
   363                const SDL_Rect * rect, int markDirty, void **pixels,
   364                int *pitch)
   365 {
   366 }
   367 
   368 static void
   369 gf_unlocktexture(SDL_Renderer * renderer, SDL_Texture * texture)
   370 {
   371 }
   372 
   373 static void
   374 gf_dirtytexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
   375                 const SDL_Rect * rects)
   376 {
   377 }
   378 
   379 static int
   380 gf_renderpoint(SDL_Renderer * renderer, int x, int y)
   381 {
   382 }
   383 
   384 static int
   385 gf_renderline(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
   386 {
   387 }
   388 
   389 static int
   390 gf_renderfill(SDL_Renderer * renderer, const SDL_Rect * rect)
   391 {
   392 }
   393 
   394 static int
   395 gf_rendercopy(SDL_Renderer * renderer, SDL_Texture * texture,
   396               const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   397 {
   398 }
   399 
   400 static void
   401 gf_renderpresent(SDL_Renderer * renderer)
   402 {
   403 }
   404 
   405 static void
   406 gf_destroytexture(SDL_Renderer * renderer, SDL_Texture * texture)
   407 {
   408 }
   409 
   410 static void
   411 gf_destroyrenderer(SDL_Renderer * renderer)
   412 {
   413     SDL_RenderData *rdata = (SDL_RenderData *) renderer->driverdata;
   414     uint32_t it;
   415 
   416     for (it = 0; it < rdata->surfaces_count; it++) {
   417         if (rdata->surface[it] != NULL) {
   418             gf_surface_free(rdata->surface[it]);
   419         }
   420     }
   421 }
   422 
   423 /* vi: set ts=4 sw=4 expandtab: */