summaryrefslogtreecommitdiff
path: root/Source/DirectFB/systems/sdl/sdlgfx.c
blob: 811d545e862e93b905a89b00490b7e7972531329 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
   (c) Copyright 2001-2009  The world wide DirectFB Open Source Community (directfb.org)
   (c) Copyright 2000-2004  Convergence (integrated media) GmbH

   All rights reserved.

   Written by Denis Oliver Kropp <dok@directfb.org>,
              Andreas Hundt <andi@fischlustig.de>,
              Sven Neumann <neo@directfb.org>,
              Ville Syrjälä <syrjala@sci.fi> and
              Claudio Ciccani <klan@users.sf.net>.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include <config.h>

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#include <directfb.h>

#include <SDL.h>

#include <core/coredefs.h>
#include <core/coretypes.h>

#include <core/state.h>
#include <core/gfxcard.h>
#include <core/windows.h>
#include <core/layers.h>
#include <core/screens.h>
#include <core/surface.h>

#include <gfx/convert.h>

#include <core/graphics_driver.h>

DFB_GRAPHICS_DRIVER( sdlgraphics )

#include "sdl.h"

/* FIXME: support for destination color keying */

#define SDL_DRAWING_FLAGS \
               (DSDRAW_NOFX)

#define SDL_DRAWING_FUNCTIONS \
               (DFXL_FILLRECTANGLE)

#define SDL_BLITTING_FLAGS \
               (DSBLIT_SRC_COLORKEY)

#define SDL_BLITTING_FUNCTIONS \
               (DFXL_BLIT)

D_DEBUG_DOMAIN( SDL_GFX, "SDL/Graphics", "SDL Graphics" );

typedef struct {
} SDLDriverData;

typedef struct {
     SDL_Surface *dest;
     SDL_Surface *source;

     u32          color;

     bool         color_valid;
     bool         key_valid;
} SDLDeviceData;


static DFBResult sdlEngineSync( void *drv, void *dev )
{
     return DFB_OK;
}

static void sdlCheckState( void *drv, void *dev,
                           CardState *state, DFBAccelerationMask accel )
{
     /* check destination format first */
     switch (state->destination->config.format) {
          case DSPF_RGB16:
          case DSPF_RGB32:
               break;
          default:
               return;
     }

     if (DFB_DRAWING_FUNCTION( accel )) {
          /* if there are no other drawing flags than the supported */
          if (state->drawingflags & ~SDL_DRAWING_FLAGS)
               return;

          state->accel |= SDL_DRAWING_FUNCTIONS;
     }
     else {
          /* if there are no other blitting flags than the supported
             and the source and destination formats are the same */
          if (state->blittingflags & ~SDL_BLITTING_FLAGS)
               return;

          /* check source format */
          switch (state->source->config.format) {
               case DSPF_RGB16:
               case DSPF_RGB32:
                    break;
               default:
                    return;
          }

          state->accel |= SDL_BLITTING_FUNCTIONS;
     }
}

static void sdlSetState( void *drv, void *dev, GraphicsDeviceFuncs *funcs,
                         CardState *state, DFBAccelerationMask accel )
{
     SDLDeviceData *sdev = (SDLDeviceData*) dev;

     sdev->dest   = state->dst.handle;
     sdev->source = state->src.handle;

     if (state->mod_hw & (SMF_SOURCE | SMF_BLITTING_FLAGS | SMF_SRC_COLORKEY))
          sdev->key_valid = false;

     if (state->mod_hw & (SMF_DESTINATION | SMF_COLOR))
          sdev->color_valid = false;

     switch (accel) {
          case DFXL_FILLRECTANGLE:
               if (!sdev->color_valid) {
                    switch (state->destination->config.format) {
                         case DSPF_RGB16:
                         case DSPF_RGB32:
                              sdev->color = dfb_color_to_pixel( state->destination->config.format,
                                                                state->color.r,
                                                                state->color.g,
                                                                state->color.b );
                              break;

                         default:
                              D_BUG( "unexpected format" );
                    }

                    sdev->color_valid = true;
               }

               state->set |= SDL_DRAWING_FUNCTIONS;
               break;

          case DFXL_BLIT:
               if (!sdev->key_valid) {
                    SDL_SetColorKey( sdev->source,
                                     (state->blittingflags &
                                      DSBLIT_SRC_COLORKEY) ? SDL_SRCCOLORKEY : 0,
                                     state->src_colorkey | 0xff000000 );

                    sdev->key_valid = true;
               }

               state->set |= SDL_BLITTING_FUNCTIONS;
               break;

          default:
               D_BUG("unexpected acceleration" );
               break;
     }

     state->mod_hw = 0;
}

static bool sdlFillRectangle( void *drv, void *dev, DFBRectangle *rect )
{
     SDLDeviceData *sdev = (SDLDeviceData*) dev;
     SDL_Rect       dr;

     dr.x = rect->x;
     dr.y = rect->y;
     dr.w = rect->w;
     dr.h = rect->h;

     return SDL_FillRect( sdev->dest, &dr, sdev->color ) == 0;
}

static bool sdlBlit( void *drv, void *dev, DFBRectangle *rect, int dx, int dy )
{
     SDLDeviceData *sdev = (SDLDeviceData*) dev;
     SDL_Rect       sr, dr;

     D_DEBUG_AT( SDL_GFX, "%s()\n", __FUNCTION__ );

     sr.x = rect->x;
     sr.y = rect->y;
     sr.w = rect->w;
     sr.h = rect->h;

     dr.x = dx;
     dr.y = dy;
     dr.w = rect->w;
     dr.h = rect->h;

     return SDL_BlitSurface( sdev->source, &sr, sdev->dest, &dr ) == 0;
}


/* exported symbols */

static int
driver_probe( CoreGraphicsDevice *device )
{
     return dfb_system_type() == CORE_SDL;
}

static void
driver_get_info( CoreGraphicsDevice *device,
                 GraphicsDriverInfo *info )
{
     /* fill driver info structure */
     snprintf( info->name,
               DFB_GRAPHICS_DRIVER_INFO_NAME_LENGTH,
               "SDL Graphics Driver" );

     snprintf( info->vendor,
               DFB_GRAPHICS_DRIVER_INFO_VENDOR_LENGTH,
               "directfb.org" );

     info->version.major = 0;
     info->version.minor = 1;

     info->driver_data_size = sizeof (SDLDriverData);
     info->device_data_size = sizeof (SDLDeviceData);
}

static DFBResult
driver_init_driver( CoreGraphicsDevice  *device,
                    GraphicsDeviceFuncs *funcs,
                    void                *driver_data,
                    void                *device_data,
                    CoreDFB             *core )
{
     /* fill acceleration function table */
     funcs->EngineSync    = sdlEngineSync;
     funcs->CheckState    = sdlCheckState;
     funcs->SetState      = sdlSetState;

     funcs->FillRectangle = sdlFillRectangle;
     funcs->Blit          = sdlBlit;

     return DFB_OK;
}

static DFBResult
driver_init_device( CoreGraphicsDevice *device,
                    GraphicsDeviceInfo *device_info,
                    void               *driver_data,
                    void               *device_data )
{
     /* fill device info */
     snprintf( device_info->name,
               DFB_GRAPHICS_DEVICE_INFO_NAME_LENGTH, "Graphics" );

     snprintf( device_info->vendor,
               DFB_GRAPHICS_DEVICE_INFO_VENDOR_LENGTH, "SDL" );


     device_info->caps.flags    = CCF_READSYSMEM;
     device_info->caps.accel    = SDL_DRAWING_FUNCTIONS |
                                  SDL_BLITTING_FUNCTIONS;
     device_info->caps.drawing  = SDL_DRAWING_FLAGS;
     device_info->caps.blitting = SDL_BLITTING_FLAGS;

     return DFB_OK;
}

static void
driver_close_device( CoreGraphicsDevice *device,
                     void               *driver_data,
                     void               *device_data )
{
}

static void
driver_close_driver( CoreGraphicsDevice *device,
                     void               *driver_data )
{
}