src/video/win32/SDL_d3drender.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 12 Jul 2006 07:01:35 +0000
changeset 1898 f89e49e51e89
parent 1895 c121d94672cb
child 1900 5c6bdbf3aadf
permissions -rw-r--r--
Playing around, trying to find a fast path with GDI
     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 #include "SDL_config.h"
    23 
    24 #if SDL_VIDEO_RENDER_D3D
    25 
    26 #include "SDL_win32video.h"
    27 #include "../SDL_yuv_sw_c.h"
    28 
    29 /* Direct3D renderer implementation */
    30 
    31 static SDL_Renderer *SDL_D3D_CreateRenderer(SDL_Window * window,
    32                                             Uint32 flags);
    33 static int SDL_D3D_CreateTexture(SDL_Renderer * renderer,
    34                                  SDL_Texture * texture);
    35 static int SDL_D3D_QueryTexturePixels(SDL_Renderer * renderer,
    36                                       SDL_Texture * texture, void **pixels,
    37                                       int *pitch);
    38 static int SDL_D3D_SetTexturePalette(SDL_Renderer * renderer,
    39                                      SDL_Texture * texture,
    40                                      const SDL_Color * colors, int firstcolor,
    41                                      int ncolors);
    42 static int SDL_D3D_GetTexturePalette(SDL_Renderer * renderer,
    43                                      SDL_Texture * texture,
    44                                      SDL_Color * colors, int firstcolor,
    45                                      int ncolors);
    46 static int SDL_D3D_UpdateTexture(SDL_Renderer * renderer,
    47                                  SDL_Texture * texture, const SDL_Rect * rect,
    48                                  const void *pixels, int pitch);
    49 static int SDL_D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    50                                const SDL_Rect * rect, int markDirty,
    51                                void **pixels, int *pitch);
    52 static void SDL_D3D_UnlockTexture(SDL_Renderer * renderer,
    53                                   SDL_Texture * texture);
    54 static void SDL_D3D_DirtyTexture(SDL_Renderer * renderer,
    55                                  SDL_Texture * texture, int numrects,
    56                                  const SDL_Rect * rects);
    57 static void SDL_D3D_SelectRenderTexture(SDL_Renderer * renderer,
    58                                         SDL_Texture * texture);
    59 static int SDL_D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
    60                               Uint32 color);
    61 static int SDL_D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
    62                               const SDL_Rect * srcrect,
    63                               const SDL_Rect * dstrect, int blendMode,
    64                               int scaleMode);
    65 static int SDL_D3D_RenderReadPixels(SDL_Renderer * renderer,
    66                                     const SDL_Rect * rect, void *pixels,
    67                                     int pitch);
    68 static int SDL_D3D_RenderWritePixels(SDL_Renderer * renderer,
    69                                      const SDL_Rect * rect,
    70                                      const void *pixels, int pitch);
    71 static void SDL_D3D_RenderPresent(SDL_Renderer * renderer);
    72 static void SDL_D3D_DestroyTexture(SDL_Renderer * renderer,
    73                                    SDL_Texture * texture);
    74 static void SDL_D3D_DestroyRenderer(SDL_Renderer * renderer);
    75 
    76 
    77 SDL_RenderDriver SDL_D3D_RenderDriver = {
    78     SDL_D3D_CreateRenderer,
    79     {
    80      "d3d",
    81      (SDL_Renderer_Minimal |
    82       SDL_Renderer_SingleBuffer | SDL_Renderer_PresentCopy |
    83       SDL_Renderer_PresentFlip2 | SDL_Renderer_PresentFlip3 |
    84       SDL_Renderer_PresentDiscard | SDL_Renderer_RenderTarget),
    85      (SDL_TextureBlendMode_None |
    86       SDL_TextureBlendMode_Mask | SDL_TextureBlendMode_Blend),
    87      (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast),
    88      11,
    89      {
    90       SDL_PixelFormat_Index8,
    91       SDL_PixelFormat_RGB555,
    92       SDL_PixelFormat_RGB565,
    93       SDL_PixelFormat_RGB888,
    94       SDL_PixelFormat_BGR888,
    95       SDL_PixelFormat_ARGB8888,
    96       SDL_PixelFormat_RGBA8888,
    97       SDL_PixelFormat_ABGR8888,
    98       SDL_PixelFormat_BGRA8888,
    99       SDL_PixelFormat_YUY2,
   100       SDL_PixelFormat_UYVY},
   101      0,
   102      0}
   103 };
   104 
   105 typedef struct
   106 {
   107     IDirect3DDevice9 *device;
   108 } SDL_D3D_RenderData;
   109 
   110 typedef struct
   111 {
   112     SDL_SW_YUVTexture *yuv;
   113 } SDL_D3D_TextureData;
   114 
   115 static void
   116 UpdateYUVTextureData(SDL_Texture * texture)
   117 {
   118     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   119     SDL_Rect rect;
   120 
   121     rect.x = 0;
   122     rect.y = 0;
   123     rect.w = texture->w;
   124     rect.h = texture->h;
   125     //SDL_SW_CopyYUVToRGB(data->yuv, &rect, data->format, texture->w,
   126     //                    texture->h, data->pixels, data->pitch);
   127 }
   128 
   129 void
   130 D3D_AddRenderDriver(_THIS)
   131 {
   132     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   133 
   134     if (data->d3d) {
   135         SDL_AddRenderDriver(0, &SDL_D3D_RenderDriver);
   136     }
   137 }
   138 
   139 SDL_Renderer *
   140 SDL_D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
   141 {
   142     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   143     SDL_VideoData *videodata = (SDL_VideoData *) display->device->driverdata;
   144     SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
   145     SDL_Renderer *renderer;
   146     SDL_D3D_RenderData *data;
   147 
   148     renderer = (SDL_Renderer *) SDL_malloc(sizeof(*renderer));
   149     if (!renderer) {
   150         SDL_OutOfMemory();
   151         return NULL;
   152     }
   153     SDL_zerop(renderer);
   154 
   155     data = (SDL_D3D_RenderData *) SDL_malloc(sizeof(*data));
   156     if (!data) {
   157         SDL_D3D_DestroyRenderer(renderer);
   158         SDL_OutOfMemory();
   159         return NULL;
   160     }
   161     SDL_zerop(data);
   162 
   163     //data->device = IDirect3D9_CreateDevice(videodata->d3d,
   164 
   165     renderer->CreateTexture = SDL_D3D_CreateTexture;
   166     renderer->QueryTexturePixels = SDL_D3D_QueryTexturePixels;
   167     renderer->SetTexturePalette = SDL_D3D_SetTexturePalette;
   168     renderer->GetTexturePalette = SDL_D3D_GetTexturePalette;
   169     renderer->UpdateTexture = SDL_D3D_UpdateTexture;
   170     renderer->LockTexture = SDL_D3D_LockTexture;
   171     renderer->UnlockTexture = SDL_D3D_UnlockTexture;
   172     renderer->DirtyTexture = SDL_D3D_DirtyTexture;
   173     renderer->SelectRenderTexture = SDL_D3D_SelectRenderTexture;
   174     renderer->RenderFill = SDL_D3D_RenderFill;
   175     renderer->RenderCopy = SDL_D3D_RenderCopy;
   176     renderer->RenderReadPixels = SDL_D3D_RenderReadPixels;
   177     renderer->RenderWritePixels = SDL_D3D_RenderWritePixels;
   178     renderer->RenderPresent = SDL_D3D_RenderPresent;
   179     renderer->DestroyTexture = SDL_D3D_DestroyTexture;
   180     renderer->DestroyRenderer = SDL_D3D_DestroyRenderer;
   181     renderer->info = SDL_D3D_RenderDriver.info;
   182     renderer->window = window->id;
   183     renderer->driverdata = data;
   184 
   185     renderer->info.flags = SDL_Renderer_RenderTarget;
   186 
   187     return renderer;
   188 }
   189 
   190 static int
   191 SDL_D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   192 {
   193     SDL_D3D_RenderData *renderdata =
   194         (SDL_D3D_RenderData *) renderer->driverdata;
   195     SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   196     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   197     SDL_D3D_TextureData *data;
   198 
   199     data = (SDL_D3D_TextureData *) SDL_malloc(sizeof(*data));
   200     if (!data) {
   201         SDL_OutOfMemory();
   202         return -1;
   203     }
   204     SDL_zerop(data);
   205 
   206     texture->driverdata = data;
   207 
   208     return 0;
   209 }
   210 
   211 static int
   212 SDL_D3D_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
   213                            void **pixels, int *pitch)
   214 {
   215     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   216 
   217     if (data->yuv) {
   218         return SDL_SW_QueryYUVTexturePixels(data->yuv, pixels, pitch);
   219     } else {
   220         return 0;
   221     }
   222 }
   223 
   224 static int
   225 SDL_D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   226                           const SDL_Color * colors, int firstcolor,
   227                           int ncolors)
   228 {
   229     SDL_D3D_RenderData *renderdata =
   230         (SDL_D3D_RenderData *) renderer->driverdata;
   231     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   232 
   233     if (data->yuv) {
   234         SDL_SetError("YUV textures don't have a palette");
   235         return -1;
   236     } else {
   237         return 0;
   238     }
   239 }
   240 
   241 static int
   242 SDL_D3D_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   243                           SDL_Color * colors, int firstcolor, int ncolors)
   244 {
   245     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   246 
   247     if (data->yuv) {
   248         SDL_SetError("YUV textures don't have a palette");
   249         return -1;
   250     } else {
   251         return 0;
   252     }
   253 }
   254 
   255 static int
   256 SDL_D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   257                       const SDL_Rect * rect, const void *pixels, int pitch)
   258 {
   259     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   260 
   261     if (data->yuv) {
   262         if (SDL_SW_UpdateYUVTexture(data->yuv, rect, pixels, pitch) < 0) {
   263             return -1;
   264         }
   265         UpdateYUVTextureData(texture);
   266         return 0;
   267     } else {
   268         SDL_D3D_RenderData *renderdata =
   269             (SDL_D3D_RenderData *) renderer->driverdata;
   270 
   271         return 0;
   272     }
   273 }
   274 
   275 static int
   276 SDL_D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   277                     const SDL_Rect * rect, int markDirty, void **pixels,
   278                     int *pitch)
   279 {
   280     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   281 
   282     if (data->yuv) {
   283         return SDL_SW_LockYUVTexture(data->yuv, rect, markDirty, pixels,
   284                                      pitch);
   285     } else {
   286         return 0;
   287     }
   288 }
   289 
   290 static void
   291 SDL_D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   292 {
   293     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   294 
   295     if (data->yuv) {
   296         SDL_SW_UnlockYUVTexture(data->yuv);
   297         UpdateYUVTextureData(texture);
   298     }
   299 }
   300 
   301 static void
   302 SDL_D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   303                      int numrects, const SDL_Rect * rects)
   304 {
   305 }
   306 
   307 static void
   308 SDL_D3D_SelectRenderTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   309 {
   310     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   311 }
   312 
   313 static int
   314 SDL_D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
   315                    Uint32 color)
   316 {
   317     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   318     Uint8 r, g, b;
   319 
   320     r = (Uint8) ((color >> 16) & 0xFF);
   321     g = (Uint8) ((color >> 8) & 0xFF);
   322     b = (Uint8) (color & 0xFF);
   323 
   324     return 0;
   325 }
   326 
   327 static int
   328 SDL_D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   329                    const SDL_Rect * srcrect, const SDL_Rect * dstrect,
   330                    int blendMode, int scaleMode)
   331 {
   332     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   333     SDL_D3D_TextureData *texturedata =
   334         (SDL_D3D_TextureData *) texture->driverdata;
   335 
   336     return 0;
   337 }
   338 
   339 static int
   340 SDL_D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   341                          void *pixels, int pitch)
   342 {
   343     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   344 
   345     return 0;
   346 }
   347 
   348 static int
   349 SDL_D3D_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   350                           const void *pixels, int pitch)
   351 {
   352     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   353 
   354     return 0;
   355 }
   356 
   357 static void
   358 SDL_D3D_RenderPresent(SDL_Renderer * renderer)
   359 {
   360 }
   361 
   362 static void
   363 SDL_D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   364 {
   365     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   366 
   367     if (!data) {
   368         return;
   369     }
   370     SDL_free(data);
   371     texture->driverdata = NULL;
   372 }
   373 
   374 void
   375 SDL_D3D_DestroyRenderer(SDL_Renderer * renderer)
   376 {
   377     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   378 
   379     if (data) {
   380         SDL_free(data);
   381     }
   382     SDL_free(renderer);
   383 }
   384 
   385 #endif /* SDL_VIDEO_RENDER_D3D */
   386 
   387 /* vi: set ts=4 sw=4 expandtab: */