PageRenderTime 130ms CodeModel.GetById 27ms app.highlight 93ms RepoModel.GetById 1ms app.codeStats 1ms

/project/jni/sdl-1.3/src/video/directfb/SDL_DirectFB_render.c

https://github.com/aichunyu/FFPlayer
C | 1263 lines | 999 code | 184 blank | 80 comment | 93 complexity | e6479fc750c8dc20f282ba5b3909c5e1 MD5 | raw file
   1/*
   2  Simple DirectMedia Layer
   3  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
   4
   5  This software is provided 'as-is', without any express or implied
   6  warranty.  In no event will the authors be held liable for any damages
   7  arising from the use of this software.
   8
   9  Permission is granted to anyone to use this software for any purpose,
  10  including commercial applications, and to alter it and redistribute it
  11  freely, subject to the following restrictions:
  12
  13  1. The origin of this software must not be misrepresented; you must not
  14     claim that you wrote the original software. If you use this software
  15     in a product, an acknowledgment in the product documentation would be
  16     appreciated but is not required.
  17  2. Altered source versions must be plainly marked as such, and must not be
  18     misrepresented as being the original software.
  19  3. This notice may not be removed or altered from any source distribution.
  20*/
  21#include "SDL_config.h"
  22
  23#if SDL_VIDEO_DRIVER_DIRECTFB
  24//#include "SDL_DirectFB_video.h"
  25#include "SDL_DirectFB_window.h"
  26#include "SDL_DirectFB_modes.h"
  27
  28#include "SDL_syswm.h"
  29#include "SDL_DirectFB_shape.h"
  30
  31#include "../SDL_sysvideo.h"
  32#include "../../render/SDL_sysrender.h"
  33//#include "../SDL_rect_c.h"
  34//#include "../SDL_yuv_sw_c.h"
  35
  36#ifndef DFB_VERSION_ATLEAST
  37
  38#define DFB_VERSIONNUM(X, Y, Z)						\
  39	((X)*1000 + (Y)*100 + (Z))
  40
  41#define DFB_COMPILEDVERSION \
  42	DFB_VERSIONNUM(DIRECTFB_MAJOR_VERSION, DIRECTFB_MINOR_VERSION, DIRECTFB_MICRO_VERSION)
  43
  44#define DFB_VERSION_ATLEAST(X, Y, Z) \
  45	(DFB_COMPILEDVERSION >= DFB_VERSIONNUM(X, Y, Z))
  46
  47#define SDL_DFB_CHECK(x)	x
  48
  49#endif
  50
  51/* the following is not yet tested ... */
  52#define USE_DISPLAY_PALETTE			(0)
  53
  54
  55#define SDL_DFB_RENDERERDATA(rend) DirectFB_RenderData *renddata = ((rend) ? (DirectFB_RenderData *) (rend)->driverdata : NULL)
  56
  57
  58/* GDI renderer implementation */
  59
  60static SDL_Renderer *DirectFB_CreateRenderer(SDL_Window * window,
  61                                             Uint32 flags);
  62static void DirectFB_ActivateRenderer(SDL_Renderer * renderer);
  63static int DirectFB_CreateTexture(SDL_Renderer * renderer,
  64                                  SDL_Texture * texture);
  65static int DirectFB_QueryTexturePixels(SDL_Renderer * renderer,
  66                                       SDL_Texture * texture,
  67                                       void **pixels, int *pitch);
  68static int DirectFB_SetTexturePalette(SDL_Renderer * renderer,
  69                                      SDL_Texture * texture,
  70                                      const SDL_Color * colors,
  71                                      int firstcolor, int ncolors);
  72static int DirectFB_GetTexturePalette(SDL_Renderer * renderer,
  73                                      SDL_Texture * texture,
  74                                      SDL_Color * colors,
  75                                      int firstcolor, int ncolors);
  76static int DirectFB_SetTextureAlphaMod(SDL_Renderer * renderer,
  77                                       SDL_Texture * texture);
  78static int DirectFB_SetTextureColorMod(SDL_Renderer * renderer,
  79                                       SDL_Texture * texture);
  80static int DirectFB_SetTextureBlendMode(SDL_Renderer * renderer,
  81                                        SDL_Texture * texture);
  82static int DirectFB_SetTextureScaleMode(SDL_Renderer * renderer,
  83                                        SDL_Texture * texture);
  84static int DirectFB_UpdateTexture(SDL_Renderer * renderer,
  85                                  SDL_Texture * texture,
  86                                  const SDL_Rect * rect,
  87                                  const void *pixels, int pitch);
  88static int DirectFB_LockTexture(SDL_Renderer * renderer,
  89                                SDL_Texture * texture,
  90                                const SDL_Rect * rect,
  91                                void **pixels, int *pitch);
  92static void DirectFB_UnlockTexture(SDL_Renderer * renderer,
  93                                   SDL_Texture * texture);
  94static void DirectFB_DirtyTexture(SDL_Renderer * renderer,
  95                                  SDL_Texture * texture, int numrects,
  96                                  const SDL_Rect * rects);
  97static int DirectFB_SetDrawBlendMode(SDL_Renderer * renderer);
  98static int DirectFB_RenderDrawPoints(SDL_Renderer * renderer,
  99                                const SDL_Point * points, int count);
 100static int DirectFB_RenderDrawLines(SDL_Renderer * renderer,
 101                               const SDL_Point * points, int count);
 102static int DirectFB_RenderDrawRects(SDL_Renderer * renderer,
 103		const SDL_Rect ** rects, int count);
 104static int DirectFB_RenderFillRects(SDL_Renderer * renderer,
 105		const SDL_Rect * rects, int count);
 106static int DirectFB_RenderCopy(SDL_Renderer * renderer,
 107                               SDL_Texture * texture,
 108                               const SDL_Rect * srcrect,
 109                               const SDL_Rect * dstrect);
 110static void DirectFB_RenderPresent(SDL_Renderer * renderer);
 111static void DirectFB_DestroyTexture(SDL_Renderer * renderer,
 112                                    SDL_Texture * texture);
 113static void DirectFB_DestroyRenderer(SDL_Renderer * renderer);
 114static int DirectFB_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
 115                     Uint32 format, void * pixels, int pitch);
 116static int DirectFB_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
 117                      Uint32 format, const void * pixels, int pitch);
 118static int DirectFB_UpdateViewport(SDL_Renderer * renderer);
 119
 120static int PrepareDraw(SDL_Renderer * renderer);
 121
 122
 123#define SDL_DFB_WINDOWSURFACE(win)  IDirectFBSurface *destsurf = ((DFB_WindowData *) ((win)->driverdata))->surface;
 124
 125SDL_RenderDriver DirectFB_RenderDriver = {
 126    DirectFB_CreateRenderer,
 127    {
 128     "directfb",
 129     (SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
 130     /* (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
 131      SDL_TEXTUREMODULATE_ALPHA),
 132      (SDL_BLENDMODE_NONE | SDL_BLENDMODE_MASK | SDL_BLENDMODE_BLEND |
 133      SDL_BLENDMODE_ADD | SDL_BLENDMODE_MOD),
 134     (SDL_SCALEMODE_NONE | SDL_SCALEMODE_FAST |
 135      SDL_SCALEMODE_SLOW | SDL_SCALEMODE_BEST),*/
 136     0,
 137     {
 138    		 /* formats filled in later */
 139     },
 140     0,
 141     0}
 142};
 143
 144typedef struct
 145{
 146    SDL_Window *window;
 147    DFBSurfaceFlipFlags flipflags;
 148    int size_changed;
 149    int lastBlendMode;
 150    DFBSurfaceBlittingFlags blitFlags;
 151    DFBSurfaceDrawingFlags drawFlags;
 152} DirectFB_RenderData;
 153
 154typedef struct
 155{
 156    IDirectFBSurface *surface;
 157    Uint32 format;
 158    void *pixels;
 159    int pitch;
 160    IDirectFBPalette *palette;
 161    int isDirty;
 162
 163    SDL_VideoDisplay *display;      /* only for yuv textures */
 164
 165#if (DFB_VERSION_ATLEAST(1,2,0))
 166    DFBSurfaceRenderOptions render_options;
 167#endif
 168} DirectFB_TextureData;
 169
 170static __inline__ void
 171SDLtoDFBRect(const SDL_Rect * sr, DFBRectangle * dr)
 172{
 173    dr->x = sr->x;
 174    dr->y = sr->y;
 175    dr->h = sr->h;
 176    dr->w = sr->w;
 177}
 178
 179
 180static int
 181TextureHasAlpha(DirectFB_TextureData * data)
 182{
 183    /* Drawing primitive ? */
 184    if (!data)
 185        return 0;
 186        
 187    return (DFB_PIXELFORMAT_HAS_ALPHA(DirectFB_SDLToDFBPixelFormat(data->format)) ? 1 : 0);
 188#if 0
 189    switch (data->format) {
 190    case SDL_PIXELFORMAT_INDEX4LSB:
 191    case SDL_PIXELFORMAT_INDEX4MSB:
 192    case SDL_PIXELFORMAT_ARGB4444:
 193    case SDL_PIXELFORMAT_ARGB1555:
 194    case SDL_PIXELFORMAT_ARGB8888:
 195    case SDL_PIXELFORMAT_RGBA8888:
 196    case SDL_PIXELFORMAT_ABGR8888:
 197    case SDL_PIXELFORMAT_BGRA8888:
 198    case SDL_PIXELFORMAT_ARGB2101010:
 199       return 1;
 200    default:
 201        return 0;
 202    }
 203#endif
 204}
 205
 206static inline IDirectFBSurface *get_dfb_surface(SDL_Window *window)
 207{
 208	SDL_SysWMinfo wm_info;
 209	SDL_VERSION(&wm_info.version);
 210	SDL_GetWindowWMInfo(window, &wm_info);
 211
 212	return wm_info.info.dfb.surface;
 213}
 214
 215static inline IDirectFBWindow *get_dfb_window(SDL_Window *window)
 216{
 217	SDL_SysWMinfo wm_info;
 218	SDL_VERSION(&wm_info.version);
 219	SDL_GetWindowWMInfo(window, &wm_info);
 220
 221	return wm_info.info.dfb.window;
 222}
 223
 224static void
 225SetBlendMode(DirectFB_RenderData * data, int blendMode,
 226             DirectFB_TextureData * source)
 227{
 228	IDirectFBSurface *destsurf = get_dfb_surface(data->window);
 229
 230    //FIXME: check for format change
 231    if (1 || data->lastBlendMode != blendMode) {
 232        switch (blendMode) {
 233        case SDL_BLENDMODE_NONE:
 234                                           /**< No blending */
 235            data->blitFlags = DSBLIT_NOFX;
 236            data->drawFlags = DSDRAW_NOFX;
 237            SDL_DFB_CHECK(destsurf->SetSrcBlendFunction(destsurf, DSBF_ONE));
 238            SDL_DFB_CHECK(destsurf->SetDstBlendFunction(destsurf, DSBF_ZERO));
 239            break;
 240#if 0
 241        case SDL_BLENDMODE_MASK:
 242            data->blitFlags =  DSBLIT_BLEND_ALPHACHANNEL;
 243            data->drawFlags = DSDRAW_BLEND;
 244            SDL_DFB_CHECK(destsurf->SetSrcBlendFunction(destsurf, DSBF_SRCALPHA));
 245            SDL_DFB_CHECK(destsurf->SetDstBlendFunction(destsurf, DSBF_INVSRCALPHA));
 246            break;
 247#endif
 248        case SDL_BLENDMODE_BLEND:
 249            data->blitFlags = DSBLIT_BLEND_ALPHACHANNEL;
 250            data->drawFlags = DSDRAW_BLEND;
 251            SDL_DFB_CHECK(destsurf->SetSrcBlendFunction(destsurf, DSBF_SRCALPHA));
 252            SDL_DFB_CHECK(destsurf->SetDstBlendFunction(destsurf, DSBF_INVSRCALPHA));
 253            break;
 254        case SDL_BLENDMODE_ADD:
 255            data->blitFlags = DSBLIT_BLEND_ALPHACHANNEL;
 256            data->drawFlags = DSDRAW_BLEND;
 257            // FIXME: SRCALPHA kills performance on radeon ...
 258            // It will be cheaper to copy the surface to
 259            // a temporay surface and premultiply 
 260            if (source && TextureHasAlpha(source))
 261                SDL_DFB_CHECK(destsurf->SetSrcBlendFunction(destsurf, DSBF_SRCALPHA));
 262            else
 263                SDL_DFB_CHECK(destsurf->SetSrcBlendFunction(destsurf, DSBF_ONE));
 264            SDL_DFB_CHECK(destsurf->SetDstBlendFunction(destsurf, DSBF_ONE));
 265            break;
 266        case SDL_BLENDMODE_MOD:
 267            data->blitFlags = DSBLIT_BLEND_ALPHACHANNEL;
 268            data->drawFlags = DSDRAW_BLEND;
 269            //SDL_DFB_CHECK(destsurf->SetSrcBlendFunction(destsurf, DSBF_DESTCOLOR));
 270            //SDL_DFB_CHECK(destsurf->SetDstBlendFunction(destsurf, DSBF_ZERO));
 271            //data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
 272            SDL_DFB_CHECK(destsurf->SetSrcBlendFunction(destsurf, DSBF_ZERO));
 273            SDL_DFB_CHECK(destsurf->SetDstBlendFunction(destsurf, DSBF_SRCCOLOR));
 274
 275            break;
 276        }
 277        data->lastBlendMode = blendMode;
 278    }
 279}
 280
 281static int
 282DisplayPaletteChanged(void *userdata, SDL_Palette * palette)
 283{
 284#if USE_DISPLAY_PALETTE
 285    DirectFB_RenderData *data = (DirectFB_RenderData *) userdata;
 286    SDL_DFB_WINDOWSURFACE(data->window);
 287    IDirectFBPalette *surfpal;
 288
 289    int i;
 290    int ncolors;
 291    DFBColor entries[256];
 292
 293    SDL_DFB_CHECKERR(destsurf->GetPalette(destsurf, &surfpal));
 294
 295    /* FIXME: number of colors */
 296    ncolors = (palette->ncolors < 256 ? palette->ncolors : 256);
 297
 298    for (i = 0; i < ncolors; ++i) {
 299        entries[i].r = palette->colors[i].r;
 300        entries[i].g = palette->colors[i].g;
 301        entries[i].b = palette->colors[i].b;
 302        entries[i].a = palette->colors[i].unused;
 303    }
 304    SDL_DFB_CHECKERR(surfpal->SetEntries(surfpal, entries, ncolors, 0));
 305    return 0;
 306  error:
 307#else
 308    SDL_Unsupported();
 309#endif
 310    return -1;
 311}
 312
 313static void
 314DirectFB_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
 315{
 316    SDL_DFB_RENDERERDATA(renderer);
 317
 318    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
 319        /* Rebind the context to the window area and update matrices */
 320        //SDL_CurrentContext = NULL;
 321        //data->updateSize = SDL_TRUE;
 322        renddata->size_changed = SDL_TRUE;
 323   }
 324}
 325
 326int
 327DirectFB_RenderClear(SDL_Renderer * renderer)
 328{
 329    DirectFB_RenderData *data = (DirectFB_RenderData *) renderer->driverdata;
 330	IDirectFBSurface *destsurf = get_dfb_surface(data->window);
 331
 332    DirectFB_ActivateRenderer(renderer);
 333
 334    PrepareDraw(renderer);
 335
 336    destsurf->Clear(destsurf, renderer->r, renderer->g, renderer->b, renderer->a);
 337
 338
 339    return 0;
 340}
 341
 342SDL_Renderer *
 343DirectFB_CreateRenderer(SDL_Window * window, Uint32 flags)
 344{
 345	IDirectFBSurface *winsurf = get_dfb_surface(window);
 346    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
 347    SDL_Renderer *renderer = NULL;
 348    DirectFB_RenderData *data = NULL;
 349    DFBSurfaceCapabilities scaps;
 350    //char *p;
 351
 352    SDL_DFB_ALLOC_CLEAR(renderer, sizeof(*renderer));
 353    SDL_DFB_ALLOC_CLEAR(data, sizeof(*data));
 354
 355    renderer->WindowEvent = DirectFB_WindowEvent;
 356    renderer->CreateTexture = DirectFB_CreateTexture;
 357    renderer->SetTextureAlphaMod = DirectFB_SetTextureAlphaMod;
 358    renderer->SetTextureColorMod = DirectFB_SetTextureColorMod;
 359    renderer->SetTextureBlendMode = DirectFB_SetTextureBlendMode;
 360    renderer->UpdateTexture = DirectFB_UpdateTexture;
 361    renderer->LockTexture = DirectFB_LockTexture;
 362    renderer->RenderClear = DirectFB_RenderClear;
 363    renderer->UnlockTexture = DirectFB_UnlockTexture;
 364    renderer->RenderDrawPoints = DirectFB_RenderDrawPoints;
 365    renderer->RenderDrawLines = DirectFB_RenderDrawLines;
 366    /* SetDrawColor - no needed */
 367    renderer->RenderFillRects = DirectFB_RenderFillRects;
 368
 369    /* RenderDrawEllipse - no reference implementation yet */
 370    /* RenderFillEllipse - no reference implementation yet */
 371    renderer->RenderCopy = DirectFB_RenderCopy;
 372    renderer->RenderPresent = DirectFB_RenderPresent;
 373    
 374    /* FIXME: Yet to be tested */
 375    renderer->RenderReadPixels = DirectFB_RenderReadPixels;
 376    //renderer->RenderWritePixels = DirectFB_RenderWritePixels;
 377    
 378    renderer->DestroyTexture = DirectFB_DestroyTexture;
 379    renderer->DestroyRenderer = DirectFB_DestroyRenderer;
 380    renderer->UpdateViewport = DirectFB_UpdateViewport;
 381
 382#if 0
 383    renderer->QueryTexturePixels = DirectFB_QueryTexturePixels;
 384    renderer->SetTexturePalette = DirectFB_SetTexturePalette;
 385    renderer->GetTexturePalette = DirectFB_GetTexturePalette;
 386    renderer->SetTextureScaleMode = DirectFB_SetTextureScaleMode;
 387    renderer->DirtyTexture = DirectFB_DirtyTexture;
 388    renderer->SetDrawBlendMode = DirectFB_SetDrawBlendMode;
 389    renderer->RenderDrawRects = DirectFB_RenderDrawRects;
 390#endif
 391
 392    renderer->info = DirectFB_RenderDriver.info;
 393    renderer->window = window;      /* SDL window */
 394    renderer->driverdata = data;
 395
 396    renderer->info.flags =
 397        SDL_RENDERER_ACCELERATED;
 398
 399    data->window = window;
 400
 401    data->flipflags = DSFLIP_PIPELINE | DSFLIP_BLIT;
 402
 403    if (flags & SDL_RENDERER_PRESENTVSYNC) {
 404        data->flipflags |= DSFLIP_WAITFORSYNC | DSFLIP_ONSYNC;
 405        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
 406    } else
 407        data->flipflags |= DSFLIP_ONSYNC;
 408
 409    SDL_DFB_CHECKERR(winsurf->GetCapabilities(winsurf, &scaps));
 410
 411#if 0
 412    if (scaps & DSCAPS_DOUBLE)
 413        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
 414    else if (scaps & DSCAPS_TRIPLE)
 415        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
 416    else
 417        renderer->info.flags |= SDL_RENDERER_SINGLEBUFFER;
 418#endif
 419
 420    DirectFB_SetSupportedPixelFormats(&renderer->info);
 421
 422#if 0
 423    /* Set up a palette watch on the display palette */
 424    if (display-> palette) {
 425        SDL_AddPaletteWatch(display->palette, DisplayPaletteChanged, data);
 426    }
 427#endif
 428
 429    return renderer;
 430
 431  error:
 432    SDL_DFB_FREE(renderer);
 433    SDL_DFB_FREE(data);
 434    return NULL;
 435}
 436
 437static void
 438DirectFB_ActivateRenderer(SDL_Renderer * renderer)
 439{
 440
 441    SDL_DFB_RENDERERDATA(renderer);
 442    SDL_Window *window = renderer->window;
 443    SDL_DFB_WINDOWDATA(window);
 444
 445    if (renddata->size_changed /*|| windata->wm_needs_redraw*/) {
 446        //DirectFB_AdjustWindowSurface(window);
 447    	renddata->size_changed = SDL_FALSE;
 448    }
 449}
 450
 451
 452static int
 453DirectFB_AcquireVidLayer(SDL_Renderer * renderer, SDL_Texture * texture)
 454{
 455    //SDL_DFB_RENDERERDATA(renderer);
 456    SDL_Window *window = renderer->window;
 457    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
 458    SDL_DFB_DEVICEDATA(display->device);
 459    DFB_DisplayData *dispdata = (DFB_DisplayData *) display->driverdata;
 460    DirectFB_TextureData *data = texture->driverdata;
 461    DFBDisplayLayerConfig layconf;
 462    DFBResult ret;
 463
 464    if (devdata->use_yuv_direct && (dispdata->vidID >= 0)
 465        && (!dispdata->vidIDinuse)
 466        && SDL_ISPIXELFORMAT_FOURCC(data->format)) {
 467        layconf.flags =
 468            DLCONF_WIDTH | DLCONF_HEIGHT | DLCONF_PIXELFORMAT |
 469            DLCONF_SURFACE_CAPS;
 470        layconf.width = texture->w;
 471        layconf.height = texture->h;
 472        layconf.pixelformat = DirectFB_SDLToDFBPixelFormat(data->format);
 473        layconf.surface_caps = DSCAPS_VIDEOONLY | DSCAPS_DOUBLE;
 474
 475        SDL_DFB_CHECKERR(devdata->dfb->GetDisplayLayer(devdata->dfb,
 476                                                       dispdata->vidID,
 477                                                       &dispdata->vidlayer));
 478        SDL_DFB_CHECKERR(dispdata->
 479                         vidlayer->SetCooperativeLevel(dispdata->vidlayer,
 480                                                       DLSCL_EXCLUSIVE));
 481
 482        if (devdata->use_yuv_underlays) {
 483            ret = dispdata->vidlayer->SetLevel(dispdata->vidlayer, -1);
 484            if (ret != DFB_OK)
 485                SDL_DFB_DEBUG("Underlay Setlevel not supported\n");
 486        }
 487        SDL_DFB_CHECKERR(dispdata->
 488                         vidlayer->SetConfiguration(dispdata->vidlayer,
 489                                                    &layconf));
 490        SDL_DFB_CHECKERR(dispdata->
 491                         vidlayer->GetSurface(dispdata->vidlayer,
 492                                              &data->surface));
 493        dispdata->vidIDinuse = 1;
 494        data->display = display;
 495        return 0;
 496    }
 497    return 1;
 498  error:
 499    if (dispdata->vidlayer) {
 500        SDL_DFB_RELEASE(data->surface);
 501        SDL_DFB_CHECKERR(dispdata->
 502                         vidlayer->SetCooperativeLevel(dispdata->vidlayer,
 503                                                       DLSCL_ADMINISTRATIVE));
 504        SDL_DFB_RELEASE(dispdata->vidlayer);
 505    }
 506    return 1;
 507}
 508
 509static int
 510DirectFB_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
 511{
 512    SDL_Window *window = renderer->window;
 513    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
 514    SDL_DFB_DEVICEDATA(display->device);
 515    DirectFB_TextureData *data;
 516    DFBSurfaceDescription dsc;
 517    DFBSurfacePixelFormat pixelformat;
 518
 519    DirectFB_ActivateRenderer(renderer);
 520
 521    SDL_DFB_ALLOC_CLEAR(data, sizeof(*data));
 522    texture->driverdata = data;
 523
 524    /* find the right pixelformat */
 525    pixelformat = DirectFB_SDLToDFBPixelFormat(texture->format);
 526    if (pixelformat == DSPF_UNKNOWN) {
 527        SDL_SetError("Unknown pixel format %d\n", data->format);
 528        goto error;
 529    }
 530
 531    data->format = texture->format;
 532    data->pitch = texture->w * DFB_BYTES_PER_PIXEL(pixelformat);
 533
 534    if (DirectFB_AcquireVidLayer(renderer, texture) != 0) {
 535        /* fill surface description */
 536        dsc.flags =
 537            DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT | DSDESC_CAPS;
 538        dsc.width = texture->w;
 539        dsc.height = texture->h;
 540        if(texture->format == SDL_PIXELFORMAT_YV12 ||
 541           texture->format == SDL_PIXELFORMAT_IYUV) {
 542           /* dfb has problems with odd sizes -make them even internally */
 543           dsc.width += (dsc.width % 2);
 544           dsc.height += (dsc.height % 2);
 545        }
 546        /* <1.2 Never use DSCAPS_VIDEOONLY here. It kills performance
 547         * No DSCAPS_SYSTEMONLY either - let dfb decide
 548         * 1.2: DSCAPS_SYSTEMONLY boosts performance by factor ~8
 549         * Depends on other settings as well. Let dfb decide.
 550         */
 551        dsc.caps = DSCAPS_PREMULTIPLIED;
 552#if 0
 553        if (texture->access == SDL_TEXTUREACCESS_STREAMING)
 554            dsc.caps |= DSCAPS_SYSTEMONLY;
 555        else
 556            dsc.caps |= DSCAPS_VIDEOONLY;
 557#endif
 558
 559        dsc.pixelformat = pixelformat;
 560        data->pixels = NULL;
 561
 562        /* Create the surface */
 563        SDL_DFB_CHECKERR(devdata->dfb->CreateSurface(devdata->dfb, &dsc,
 564                                                     &data->surface));
 565        if (SDL_ISPIXELFORMAT_INDEXED(data->format)
 566            && !SDL_ISPIXELFORMAT_FOURCC(data->format)) {
 567#if 1
 568            SDL_DFB_CHECKERR(data->surface->GetPalette(data->surface, &data->palette));
 569#else
 570            /* DFB has issues with blitting LUT8 surfaces.
 571			 * Creating a new palette does not help.
 572			 */
 573        	DFBPaletteDescription pal_desc;
 574        	pal_desc.flags = DPDESC_SIZE; // | DPDESC_ENTRIES
 575        	pal_desc.size = 256;
 576        	SDL_DFB_CHECKERR(devdata->dfb->CreatePalette(devdata->dfb, &pal_desc,&data->palette));
 577        	SDL_DFB_CHECKERR(data->surface->SetPalette(data->surface, data->palette));
 578#endif
 579        }
 580
 581    }
 582#if (DFB_VERSION_ATLEAST(1,2,0))
 583    data->render_options = DSRO_NONE;
 584#endif
 585    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
 586        /* 3 plane YUVs return 1 bpp, but we need more space for other planes */
 587        if(texture->format == SDL_PIXELFORMAT_YV12 ||
 588           texture->format == SDL_PIXELFORMAT_IYUV) {
 589            SDL_DFB_ALLOC_CLEAR(data->pixels, (texture->h * data->pitch  + ((texture->h + texture->h % 2) * (data->pitch + data->pitch % 2) * 2) / 4));
 590        } else {
 591            SDL_DFB_ALLOC_CLEAR(data->pixels, texture->h * data->pitch);
 592        }
 593    }
 594
 595    return 0;
 596
 597  error:
 598    SDL_DFB_RELEASE(data->palette);
 599    SDL_DFB_RELEASE(data->surface);
 600    SDL_DFB_FREE(texture->driverdata);
 601    return -1;
 602}
 603
 604static int
 605DirectFB_QueryTexturePixels(SDL_Renderer * renderer,
 606                            SDL_Texture * texture, void **pixels, int *pitch)
 607{
 608    DirectFB_TextureData *texturedata =
 609        (DirectFB_TextureData *) texture->driverdata;
 610
 611    if (texturedata->display) {
 612        return -1;
 613    } else {
 614        *pixels = texturedata->pixels;
 615        *pitch = texturedata->pitch;
 616    }
 617    return 0;
 618}
 619
 620static int
 621DirectFB_SetTexturePalette(SDL_Renderer * renderer,
 622                           SDL_Texture * texture,
 623                           const SDL_Color * colors, int firstcolor,
 624                           int ncolors)
 625{
 626    DirectFB_TextureData *data = (DirectFB_TextureData *) texture->driverdata;
 627    if (SDL_ISPIXELFORMAT_INDEXED(data->format)
 628        && !SDL_ISPIXELFORMAT_FOURCC(data->format)) {
 629        DFBColor entries[256];
 630        int i;
 631
 632        if (ncolors > 256)
 633        	ncolors = 256;
 634
 635        for (i = 0; i < ncolors; ++i) {
 636            entries[i].r = colors[i].r;
 637            entries[i].g = colors[i].g;
 638            entries[i].b = colors[i].b;
 639            entries[i].a = 0xff;
 640        }
 641        SDL_DFB_CHECKERR(data->
 642                         palette->SetEntries(data->palette, entries, ncolors, firstcolor));
 643        return 0;
 644    } else {
 645        SDL_SetError("YUV textures don't have a palette");
 646        return -1;
 647    }
 648  error:
 649    return -1;
 650}
 651
 652static int
 653DirectFB_GetTexturePalette(SDL_Renderer * renderer,
 654                           SDL_Texture * texture, SDL_Color * colors,
 655                           int firstcolor, int ncolors)
 656{
 657    DirectFB_TextureData *data = (DirectFB_TextureData *) texture->driverdata;
 658
 659    if (SDL_ISPIXELFORMAT_INDEXED(data->format)
 660        && !SDL_ISPIXELFORMAT_FOURCC(data->format)) {
 661        DFBColor entries[256];
 662        int i;
 663
 664        SDL_DFB_CHECKERR(data->
 665                         palette->GetEntries(data->palette, entries, ncolors,
 666                                             firstcolor));
 667
 668        for (i = 0; i < ncolors; ++i) {
 669            colors[i].r = entries[i].r;
 670            colors[i].g = entries[i].g;
 671            colors[i].b = entries[i].b;
 672            colors->unused = SDL_ALPHA_OPAQUE;
 673        }
 674        return 0;
 675    } else {
 676        SDL_SetError("YUV textures don't have a palette");
 677        return -1;
 678    }
 679  error:
 680    return -1;
 681}
 682
 683static int
 684DirectFB_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
 685{
 686    return 0;
 687}
 688
 689static int
 690DirectFB_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
 691{
 692    return 0;
 693}
 694
 695static int
 696DirectFB_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
 697{
 698    switch (texture->blendMode) {
 699    case SDL_BLENDMODE_NONE:
 700    //case SDL_BLENDMODE_MASK:
 701    case SDL_BLENDMODE_BLEND:
 702    case SDL_BLENDMODE_ADD:
 703    case SDL_BLENDMODE_MOD:
 704        return 0;
 705    default:
 706        SDL_Unsupported();
 707        texture->blendMode = SDL_BLENDMODE_NONE;
 708        return -1;
 709    }
 710}
 711
 712static int
 713DirectFB_SetDrawBlendMode(SDL_Renderer * renderer)
 714{
 715    switch (renderer->blendMode) {
 716    case SDL_BLENDMODE_NONE:
 717    //case SDL_BLENDMODE_MASK:
 718    case SDL_BLENDMODE_BLEND:
 719    case SDL_BLENDMODE_ADD:
 720    case SDL_BLENDMODE_MOD:
 721        return 0;
 722    default:
 723        SDL_Unsupported();
 724        renderer->blendMode = SDL_BLENDMODE_NONE;
 725        return -1;
 726    }
 727}
 728
 729#if 0
 730static int
 731DirectFB_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
 732{
 733#if (DFB_VERSION_ATLEAST(1,2,0))
 734
 735    DirectFB_TextureData *data = (DirectFB_TextureData *) texture->driverdata;
 736
 737    switch (texture->scaleMode) {
 738    case SDL_SCALEMODE_NONE:
 739    case SDL_SCALEMODE_FAST:
 740        data->render_options = DSRO_NONE;
 741        break;
 742    case SDL_SCALEMODE_SLOW:
 743        data->render_options = DSRO_SMOOTH_UPSCALE | DSRO_SMOOTH_DOWNSCALE;
 744        break;
 745    case SDL_SCALEMODE_BEST:
 746        data->render_options =
 747            DSRO_SMOOTH_UPSCALE | DSRO_SMOOTH_DOWNSCALE | DSRO_ANTIALIAS;
 748        break;
 749    default:
 750        SDL_Unsupported();
 751        data->render_options = DSRO_NONE;
 752        texture->scaleMode = SDL_SCALEMODE_NONE;
 753        return -1;
 754    }
 755#endif
 756    return 0;
 757}
 758#endif
 759
 760static int
 761DirectFB_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
 762                       const SDL_Rect * rect, const void *pixels, int pitch)
 763{
 764    DirectFB_TextureData *data = (DirectFB_TextureData *) texture->driverdata;
 765    Uint8 *dpixels;
 766    int dpitch;
 767    Uint8 *src, *dst;
 768    int row;
 769    size_t length;
 770    int bpp = DFB_BYTES_PER_PIXEL(DirectFB_SDLToDFBPixelFormat(texture->format));
 771    // FIXME: SDL_BYTESPERPIXEL(texture->format) broken for yuv yv12 3 planes
 772
 773    DirectFB_ActivateRenderer(renderer);
 774
 775    if ((texture->format == SDL_PIXELFORMAT_YV12) ||
 776        (texture->format == SDL_PIXELFORMAT_IYUV)) {
 777        bpp = 1;
 778    }
 779    
 780    SDL_DFB_CHECKERR(data->surface->Lock(data->surface,
 781                                         DSLF_WRITE | DSLF_READ,
 782                                         ((void **) &dpixels), &dpitch));
 783    src = (Uint8 *) pixels;
 784    dst = (Uint8 *) dpixels + rect->y * dpitch + rect->x * bpp;
 785    length = rect->w * bpp;
 786    for (row = 0; row < rect->h; ++row) {
 787        SDL_memcpy(dst, src, length);
 788        src += pitch;
 789        dst += dpitch;
 790    }
 791    /* copy other planes for 3 plane formats */
 792    if ((texture->format == SDL_PIXELFORMAT_YV12) ||
 793        (texture->format == SDL_PIXELFORMAT_IYUV)) {
 794        src = (Uint8 *) pixels + texture->h * pitch;
 795        dst = (Uint8 *) dpixels + texture->h * dpitch + rect->y * dpitch / 4 + rect->x * bpp / 2;
 796        for (row = 0; row < rect->h / 2 + (rect->h & 1); ++row) {
 797            SDL_memcpy(dst, src, length / 2);
 798            src += pitch / 2;
 799            dst += dpitch / 2;
 800        }
 801        src = (Uint8 *) pixels + texture->h * pitch + texture->h * pitch / 4;
 802        dst = (Uint8 *) dpixels + texture->h * dpitch + texture->h * dpitch / 4 + rect->y * dpitch / 4 + rect->x * bpp / 2;
 803        for (row = 0; row < rect->h / 2 + (rect->h & 1); ++row) {
 804            SDL_memcpy(dst, src, length / 2);
 805            src += pitch / 2;
 806            dst += dpitch / 2;
 807        }
 808    }
 809    SDL_DFB_CHECKERR(data->surface->Unlock(data->surface));
 810    data->isDirty = 0;
 811    return 0;
 812  error:
 813    return 1;
 814
 815}
 816
 817static int
 818DirectFB_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
 819                     const SDL_Rect * rect, void **pixels, int *pitch)
 820{
 821    DirectFB_TextureData *texturedata =
 822        (DirectFB_TextureData *) texture->driverdata;
 823
 824    DirectFB_ActivateRenderer(renderer);
 825
 826#if 0
 827    if (markDirty) {
 828        SDL_AddDirtyRect(&texturedata->dirty, rect);
 829    }
 830#endif
 831
 832    if (texturedata->display) {
 833        void *fdata;
 834        int fpitch;
 835
 836        SDL_DFB_CHECKERR(texturedata->surface->Lock(texturedata->surface,
 837                                                    DSLF_WRITE | DSLF_READ,
 838                                                    &fdata, &fpitch));
 839        *pitch = fpitch;
 840        *pixels = fdata;
 841    } else {
 842        *pixels =
 843            (void *) ((Uint8 *) texturedata->pixels +
 844                      rect->y * texturedata->pitch +
 845                      rect->x * DFB_BYTES_PER_PIXEL(DirectFB_SDLToDFBPixelFormat(texture->format)));
 846        *pitch = texturedata->pitch;
 847        texturedata->isDirty = 1;
 848    }
 849    return 0;
 850
 851  error:
 852    return -1;
 853}
 854
 855static void
 856DirectFB_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
 857{
 858    DirectFB_TextureData *texturedata =
 859        (DirectFB_TextureData *) texture->driverdata;
 860
 861    DirectFB_ActivateRenderer(renderer);
 862
 863    if (texturedata->display) {
 864        SDL_DFB_CHECK(texturedata->surface->Unlock(texturedata->surface));
 865        texturedata->pixels = NULL;
 866    }
 867}
 868
 869#if 0
 870static void
 871DirectFB_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
 872                      int numrects, const SDL_Rect * rects)
 873{
 874    DirectFB_TextureData *data = (DirectFB_TextureData *) texture->driverdata;
 875    int i;
 876
 877    for (i = 0; i < numrects; ++i) {
 878        SDL_AddDirtyRect(&data->dirty, &rects[i]);
 879    }
 880}
 881#endif
 882
 883static int
 884PrepareDraw(SDL_Renderer * renderer)
 885{
 886    DirectFB_RenderData *data = (DirectFB_RenderData *) renderer->driverdata;
 887	IDirectFBSurface *destsurf = get_dfb_surface(data->window);
 888
 889    Uint8 r, g, b, a;
 890
 891    r = renderer->r;
 892    g = renderer->g;
 893    b = renderer->b;
 894    a = renderer->a;
 895
 896    SetBlendMode(data, renderer->blendMode, NULL);
 897    SDL_DFB_CHECKERR(destsurf->SetDrawingFlags(destsurf, data->drawFlags));
 898
 899    switch (renderer->blendMode) {
 900    case SDL_BLENDMODE_NONE:
 901    //case SDL_BLENDMODE_MASK:
 902    case SDL_BLENDMODE_BLEND:
 903        break;
 904    case SDL_BLENDMODE_ADD:
 905    case SDL_BLENDMODE_MOD:
 906        r = ((int) r * (int) a) / 255;
 907        g = ((int) g * (int) a) / 255;
 908        b = ((int) b * (int) a) / 255;
 909        a = 255;
 910        break;
 911    }
 912
 913    SDL_DFB_CHECKERR(destsurf->SetColor(destsurf, r, g, b, a));
 914    return 0;
 915  error:
 916    return -1;
 917}
 918
 919static int DirectFB_RenderDrawPoints(SDL_Renderer * renderer,
 920                                const SDL_Point * points, int count)
 921{
 922    DirectFB_RenderData *data = (DirectFB_RenderData *) renderer->driverdata;
 923	IDirectFBSurface *destsurf = get_dfb_surface(data->window);
 924    int i;
 925
 926    DirectFB_ActivateRenderer(renderer);
 927
 928    PrepareDraw(renderer);
 929    for (i=0; i < count; i++)
 930    	SDL_DFB_CHECKERR(destsurf->DrawLine(destsurf, points[i].x, points[i].y, points[i].x, points[i].y));
 931    return 0;
 932  error:
 933    return -1;
 934}
 935
 936static int DirectFB_RenderDrawLines(SDL_Renderer * renderer,
 937                               const SDL_Point * points, int count)
 938{
 939    DirectFB_RenderData *data = (DirectFB_RenderData *) renderer->driverdata;
 940	IDirectFBSurface *destsurf = get_dfb_surface(data->window);
 941    int i;
 942
 943    DirectFB_ActivateRenderer(renderer);
 944
 945    PrepareDraw(renderer);
 946    /* Use antialiasing when available */
 947#if (DFB_VERSION_ATLEAST(1,2,0))
 948    SDL_DFB_CHECKERR(destsurf->SetRenderOptions(destsurf, DSRO_ANTIALIAS));
 949#endif
 950
 951    for (i=0; i < count - 1; i++)
 952    	SDL_DFB_CHECKERR(destsurf->DrawLine(destsurf, points[i].x, points[i].y, points[i+1].x, points[i+1].y));
 953
 954    return 0;
 955  error:
 956    return -1;
 957}
 958
 959static int
 960DirectFB_RenderDrawRects(SDL_Renderer * renderer, const SDL_Rect ** rects, int count)
 961{
 962    DirectFB_RenderData *data = (DirectFB_RenderData *) renderer->driverdata;
 963	IDirectFBSurface *destsurf = get_dfb_surface(data->window);
 964    int i;
 965
 966    DirectFB_ActivateRenderer(renderer);
 967
 968    PrepareDraw(renderer);
 969
 970    for (i=0; i<count; i++)
 971    	SDL_DFB_CHECKERR(destsurf->DrawRectangle(destsurf, rects[i]->x, rects[i]->y,
 972    			rects[i]->w, rects[i]->h));
 973
 974    return 0;
 975  error:
 976    return -1;
 977}
 978
 979static int
 980DirectFB_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect * rects, int count)
 981{
 982    DirectFB_RenderData *data = (DirectFB_RenderData *) renderer->driverdata;
 983	IDirectFBSurface *destsurf = get_dfb_surface(data->window);
 984    int i;
 985
 986    DirectFB_ActivateRenderer(renderer);
 987
 988    PrepareDraw(renderer);
 989
 990    for (i=0; i<count; i++)
 991    	SDL_DFB_CHECKERR(destsurf->FillRectangle(destsurf, rects[i].x, rects[i].y,
 992    			rects[i].w, rects[i].h));
 993
 994    return 0;
 995  error:
 996    return -1;
 997}
 998
 999static int
1000DirectFB_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
1001                    const SDL_Rect * srcrect, const SDL_Rect * dstrect)
1002{
1003    DirectFB_RenderData *data = (DirectFB_RenderData *) renderer->driverdata;
1004	IDirectFBSurface *destsurf = get_dfb_surface(data->window);
1005    DirectFB_TextureData *texturedata =
1006        (DirectFB_TextureData *) texture->driverdata;
1007    Uint8 alpha, r, g, b;
1008
1009    DirectFB_ActivateRenderer(renderer);
1010
1011    if (texturedata->display) {
1012        int px, py;
1013        SDL_Window *window = renderer->window;
1014    	IDirectFBWindow *dfbwin = get_dfb_window(window);
1015        SDL_DFB_WINDOWDATA(window);
1016        SDL_VideoDisplay *display = texturedata->display;
1017        DFB_DisplayData *dispdata = (DFB_DisplayData *) display->driverdata;
1018
1019        SDL_DFB_CHECKERR(dispdata->
1020                         vidlayer->SetSourceRectangle(dispdata->vidlayer,
1021                                                      srcrect->x, srcrect->y,
1022                                                      srcrect->w,
1023                                                      srcrect->h));
1024        dfbwin->GetPosition(dfbwin, &px, &py);
1025        px += windata->client.x;
1026        py += windata->client.y;
1027        SDL_DFB_CHECKERR(dispdata->
1028                         vidlayer->SetScreenRectangle(dispdata->vidlayer,
1029                                                      px + dstrect->x,
1030                                                      py + dstrect->y,
1031                                                      dstrect->w,
1032                                                      dstrect->h));
1033    } else {
1034        DFBRectangle sr, dr;
1035        DFBSurfaceBlittingFlags flags = 0;
1036
1037#if 0
1038        if (texturedata->dirty.list) {
1039            SDL_DirtyRect *dirty;
1040            void *pixels;
1041            int bpp = DFB_BYTES_PER_PIXEL(DirectFB_SDLToDFBPixelFormat(texture->format));
1042            int pitch = texturedata->pitch;
1043
1044            for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
1045                SDL_Rect *rect = &dirty->rect;
1046                pixels =
1047                    (void *) ((Uint8 *) texturedata->pixels +
1048                              rect->y * pitch + rect->x * bpp);
1049                DirectFB_UpdateTexture(renderer, texture, rect,
1050                                       pixels,
1051                                       texturedata->pitch);
1052            }
1053            SDL_ClearDirtyRects(&texturedata->dirty);
1054        }
1055#endif
1056        if (texturedata->isDirty)
1057        {
1058            SDL_Rect rect;
1059
1060            rect.x = 0;
1061            rect.y = 0;
1062            rect.w = texture->w;
1063            rect.h = texture->h;
1064
1065            DirectFB_UpdateTexture(renderer, texture, &rect, texturedata->pixels, texturedata->pitch);
1066        }
1067
1068        SDLtoDFBRect(srcrect, &sr);
1069        SDLtoDFBRect(dstrect, &dr);
1070
1071        alpha = r = g = b = 0xff;
1072		if (texture->modMode & SDL_TEXTUREMODULATE_ALPHA){
1073			alpha = texture->a;
1074        	flags |= DSBLIT_BLEND_COLORALPHA;
1075		}
1076
1077        if (texture->modMode & SDL_TEXTUREMODULATE_COLOR) {
1078            r = texture->r;
1079            g = texture->g;
1080            b = texture->b;
1081            flags |= DSBLIT_COLORIZE;
1082        }
1083        SDL_DFB_CHECKERR(destsurf->
1084                         SetColor(destsurf, r, g, b, alpha));
1085
1086        // ???? flags |= DSBLIT_SRC_PREMULTCOLOR;
1087
1088        SetBlendMode(data, texture->blendMode, texturedata);
1089
1090        SDL_DFB_CHECKERR(destsurf->SetBlittingFlags(destsurf,
1091                                                    data->blitFlags | flags));
1092
1093#if (DFB_VERSION_ATLEAST(1,2,0))
1094        SDL_DFB_CHECKERR(destsurf->SetRenderOptions(destsurf,
1095                                                    texturedata->
1096                                                    render_options));
1097#endif
1098
1099        if (srcrect->w == dstrect->w && srcrect->h == dstrect->h) {
1100            SDL_DFB_CHECKERR(destsurf->Blit(destsurf,
1101                                            texturedata->surface,
1102                                            &sr, dr.x, dr.y));
1103        } else {
1104            SDL_DFB_CHECKERR(destsurf->StretchBlit(destsurf,
1105                                                   texturedata->surface,
1106                                                   &sr, &dr));
1107        }
1108    }
1109    return 0;
1110  error:
1111    return -1;
1112}
1113
1114static void
1115DirectFB_RenderPresent(SDL_Renderer * renderer)
1116{
1117    DirectFB_RenderData *data = (DirectFB_RenderData *) renderer->driverdata;
1118    SDL_Window *window = renderer->window;
1119    SDL_DFB_WINDOWDATA(window);
1120    SDL_ShapeData *shape_data = (window->shaper ? window->shaper->driverdata : NULL);
1121
1122    DirectFB_ActivateRenderer(renderer);
1123
1124    if (shape_data && shape_data->surface) {
1125        /* saturate the window surface alpha channel */
1126        SDL_DFB_CHECK(windata->window_surface->SetSrcBlendFunction(windata->window_surface, DSBF_ONE));
1127        SDL_DFB_CHECK(windata->window_surface->SetDstBlendFunction(windata->window_surface, DSBF_ONE));
1128        SDL_DFB_CHECK(windata->window_surface->SetDrawingFlags(windata->window_surface, DSDRAW_BLEND));
1129        SDL_DFB_CHECK(windata->window_surface->SetColor(windata->window_surface, 0, 0, 0, 0xff));
1130        SDL_DFB_CHECK(windata->window_surface->FillRectangle(windata->window_surface, 0,0, windata->size.w, windata->size.h));
1131
1132        /* blit the mask */
1133        SDL_DFB_CHECK(windata->surface->SetSrcBlendFunction(windata->surface, DSBF_DESTCOLOR));
1134        SDL_DFB_CHECK(windata->surface->SetDstBlendFunction(windata->surface, DSBF_ZERO));
1135        SDL_DFB_CHECK(windata->surface->SetBlittingFlags(windata->surface, DSBLIT_BLEND_ALPHACHANNEL));
1136#if (DFB_VERSION_ATLEAST(1,2,0))
1137        SDL_DFB_CHECK(windata->surface->SetRenderOptions(windata->surface, DSRO_NONE));
1138#endif
1139        SDL_DFB_CHECK(windata->surface->Blit(windata->surface, shape_data->surface, NULL, 0, 0));
1140    }
1141
1142    /* Send the data to the display */
1143    SDL_DFB_CHECK(windata->window_surface->Flip(windata->window_surface, NULL,
1144                                                data->flipflags));
1145}
1146
1147static void
1148DirectFB_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
1149{
1150    DirectFB_TextureData *data = (DirectFB_TextureData *) texture->driverdata;
1151
1152    DirectFB_ActivateRenderer(renderer);
1153
1154    if (!data) {
1155        return;
1156    }
1157    //SDL_FreeDirtyRects(&data->dirty);
1158    SDL_DFB_RELEASE(data->palette);
1159    SDL_DFB_RELEASE(data->surface);
1160    if (data->display) {
1161        DFB_DisplayData *dispdata =
1162            (DFB_DisplayData *) data->display->driverdata;
1163        dispdata->vidIDinuse = 0;
1164        /* FIXME: Shouldn't we reset the cooperative level */
1165        SDL_DFB_CHECK(dispdata->vidlayer->SetCooperativeLevel(dispdata->vidlayer,
1166                                                DLSCL_ADMINISTRATIVE));
1167        SDL_DFB_RELEASE(dispdata->vidlayer);
1168    }
1169    SDL_DFB_FREE(data->pixels);
1170    SDL_free(data);
1171    texture->driverdata = NULL;
1172}
1173
1174static void
1175DirectFB_DestroyRenderer(SDL_Renderer * renderer)
1176{
1177    DirectFB_RenderData *data = (DirectFB_RenderData *) renderer->driverdata;
1178    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(data->window);
1179
1180#if 0
1181    if (display->palette) {
1182        SDL_DelPaletteWatch(display->palette, DisplayPaletteChanged, data);
1183    }
1184#endif
1185
1186    if (data) {
1187        SDL_free(data);
1188    }
1189    SDL_free(renderer);
1190}
1191
1192static int
1193DirectFB_UpdateViewport(SDL_Renderer * renderer)
1194{
1195  	IDirectFBSurface *winsurf = get_dfb_surface(renderer->window);
1196  	DFBRegion dreg;
1197
1198  	dreg.x1 = renderer->viewport.x;
1199  	dreg.y1 = renderer->viewport.y;
1200  	dreg.x2 = dreg.x1 + renderer->viewport.w - 1;
1201  	dreg.y2 = dreg.y1 + renderer->viewport.h - 1;
1202
1203  	winsurf->SetClip(winsurf, &dreg);
1204    return 0;
1205}
1206
1207static int
1208DirectFB_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
1209                     Uint32 format, void * pixels, int pitch)
1210{
1211    Uint32 sdl_format;
1212   	void * laypixels;
1213   	int laypitch;
1214  	DFBSurfacePixelFormat dfb_format;
1215  	IDirectFBSurface *winsurf = get_dfb_surface(renderer->window);
1216
1217    DirectFB_ActivateRenderer(renderer);
1218
1219    winsurf->GetPixelFormat(winsurf, &dfb_format);
1220    sdl_format = DirectFB_DFBToSDLPixelFormat(dfb_format);
1221    winsurf->Lock(winsurf, DSLF_READ, (void **) &laypixels, &laypitch);
1222    
1223    laypixels += (rect->y * laypitch + rect->x * SDL_BYTESPERPIXEL(sdl_format) );
1224    SDL_ConvertPixels(rect->w, rect->h,
1225                      sdl_format, laypixels, laypitch,
1226                      format, pixels, pitch);
1227
1228    winsurf->Unlock(winsurf);
1229    
1230    return 0;
1231}
1232
1233#if 0
1234static int
1235DirectFB_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
1236                      Uint32 format, const void * pixels, int pitch)
1237{
1238    SDL_Window *window = renderer->window;
1239    SDL_DFB_WINDOWDATA(window);
1240    Uint32 sdl_format;
1241   	void * laypixels;
1242   	int laypitch;
1243  	DFBSurfacePixelFormat dfb_format;
1244
1245    SDL_DFB_CHECK(windata->surface->GetPixelFormat(windata->surface, &dfb_format));
1246    sdl_format = DirectFB_DFBToSDLPixelFormat(dfb_format);
1247
1248    SDL_DFB_CHECK(windata->surface->Lock(windata->surface, DSLF_WRITE, (void **) &laypixels, &laypitch));
1249      
1250    laypixels += (rect->y * laypitch + rect->x * SDL_BYTESPERPIXEL(sdl_format) );
1251    SDL_ConvertPixels(rect->w, rect->h,
1252                      format, pixels, pitch,
1253                      sdl_format, laypixels, laypitch);
1254
1255    SDL_DFB_CHECK(windata->surface->Unlock(windata->surface));
1256
1257    return 0;
1258}
1259#endif
1260
1261#endif /* SDL_VIDEO_DRIVER_DIRECTFB */
1262
1263/* vi: set ts=4 sw=4 expandtab: */