2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
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.
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.
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
22 #include "SDL_config.h"
24 /* This is the DirectFB implementation of YUV video overlays */
26 #include "SDL_video.h"
27 #include "SDL_DirectFB_yuv.h"
28 #include "../SDL_yuvfuncs.h"
31 /* The functions used to manipulate software video overlays */
32 static struct private_yuvhwfuncs directfb_yuvfuncs = {
33 DirectFB_LockYUVOverlay,
34 DirectFB_UnlockYUVOverlay,
35 DirectFB_DisplayYUVOverlay,
36 DirectFB_FreeYUVOverlay
39 struct private_yuvhwdata {
40 DFBDisplayLayerID layer_id;
42 IDirectFBDisplayLayer *layer;
43 IDirectFBSurface *surface;
45 /* These are just so we don't have to allocate them separately */
50 static DFBEnumerationResult
51 enum_layers_callback( DFBDisplayLayerID id,
52 DFBDisplayLayerDescription desc,
55 struct private_yuvhwdata *hwdata = (struct private_yuvhwdata *) data;
57 /* we don't want the primary */
58 if (id == DLID_PRIMARY)
61 /* take the one with a surface for video */
62 if ((desc.caps & DLCAPS_SURFACE) && (desc.type & DLTF_VIDEO))
64 hwdata->layer_id = id;
73 static DFBResult CreateYUVSurface(_THIS, struct private_yuvhwdata *hwdata,
74 int width, int height, Uint32 format)
77 IDirectFB *dfb = HIDDEN->dfb;
78 IDirectFBDisplayLayer *layer;
79 DFBDisplayLayerConfig conf;
81 ret = dfb->EnumDisplayLayers (dfb, enum_layers_callback, hwdata);
84 SetDirectFBerror("IDirectFB::EnumDisplayLayers", ret);
88 if (!hwdata->layer_id)
89 return DFB_UNSUPPORTED;
91 ret = dfb->GetDisplayLayer (dfb, hwdata->layer_id, &layer);
94 SetDirectFBerror("IDirectFB::GetDisplayLayer", ret);
98 conf.flags = DLCONF_WIDTH | DLCONF_HEIGHT | DLCONF_PIXELFORMAT;
100 conf.height = height;
104 case SDL_YV12_OVERLAY:
105 conf.pixelformat = DSPF_YV12;
107 case SDL_IYUV_OVERLAY:
108 conf.pixelformat = DSPF_I420;
110 case SDL_YUY2_OVERLAY:
111 conf.pixelformat = DSPF_YUY2;
113 case SDL_UYVY_OVERLAY:
114 conf.pixelformat = DSPF_UYVY;
117 fprintf (stderr, "SDL_DirectFB: Unsupported YUV format (0x%08x)!\n", format);
121 /* Need to set coop level or newer DirectFB versions will fail here. */
122 ret = layer->SetCooperativeLevel (layer, DLSCL_ADMINISTRATIVE);
125 SetDirectFBerror("IDirectFBDisplayLayer::SetCooperativeLevel() failed", ret);
126 layer->Release (layer);
130 ret = layer->SetConfiguration (layer, &conf);
133 SetDirectFBerror("IDirectFBDisplayLayer::SetConfiguration", ret);
134 layer->Release (layer);
138 ret = layer->GetSurface (layer, &hwdata->surface);
141 SetDirectFBerror("IDirectFBDisplayLayer::GetSurface", ret);
142 layer->Release (layer);
146 hwdata->layer = layer;
151 SDL_Overlay *DirectFB_CreateYUVOverlay(_THIS, int width, int height, Uint32 format, SDL_Surface *display)
153 SDL_Overlay *overlay;
154 struct private_yuvhwdata *hwdata;
156 /* Create the overlay structure */
157 overlay = SDL_calloc (1, sizeof(SDL_Overlay));
164 /* Fill in the basic members */
165 overlay->format = format;
169 /* Set up the YUV surface function structure */
170 overlay->hwfuncs = &directfb_yuvfuncs;
172 /* Create the pixel data and lookup tables */
173 hwdata = SDL_calloc(1, sizeof(struct private_yuvhwdata));
174 overlay->hwdata = hwdata;
178 SDL_FreeYUVOverlay (overlay);
182 if (CreateYUVSurface (this, hwdata, width, height, format))
184 SDL_FreeYUVOverlay (overlay);
188 overlay->hw_overlay = 1;
190 /* Set up the plane pointers */
191 overlay->pitches = hwdata->pitches;
192 overlay->pixels = hwdata->planes;
195 case SDL_YV12_OVERLAY:
196 case SDL_IYUV_OVERLAY:
204 /* We're all done.. */
208 int DirectFB_LockYUVOverlay(_THIS, SDL_Overlay *overlay)
213 IDirectFBSurface *surface = overlay->hwdata->surface;
215 ret = surface->Lock (surface, DSLF_READ | DSLF_WRITE, &data, &pitch);
218 SetDirectFBerror("IDirectFBSurface::Lock", ret);
222 /* Find the pitch and offset values for the overlay */
223 overlay->pitches[0] = (Uint16) pitch;
224 overlay->pixels[0] = (Uint8*) data;
226 switch (overlay->format)
228 case SDL_YV12_OVERLAY:
229 case SDL_IYUV_OVERLAY:
230 /* Add the two extra planes */
231 overlay->pitches[1] = overlay->pitches[0] / 2;
232 overlay->pitches[2] = overlay->pitches[0] / 2;
233 overlay->pixels[1] = overlay->pixels[0] + overlay->pitches[0] * overlay->h;
234 overlay->pixels[2] = overlay->pixels[1] + overlay->pitches[1] * overlay->h / 2;
237 /* Only one plane, no worries */
244 void DirectFB_UnlockYUVOverlay(_THIS, SDL_Overlay *overlay)
246 IDirectFBSurface *surface = overlay->hwdata->surface;
248 overlay->pixels[0] = overlay->pixels[1] = overlay->pixels[2] = NULL;
250 surface->Unlock (surface);
253 int DirectFB_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *src, SDL_Rect *dst)
256 DFBDisplayLayerConfig conf;
257 IDirectFBDisplayLayer *primary = HIDDEN->layer;
258 IDirectFBDisplayLayer *layer = overlay->hwdata->layer;
260 primary->GetConfiguration (primary, &conf);
262 ret = layer->SetScreenLocation (layer,
263 dst->x / (float) conf.width, dst->y / (float) conf.height,
264 dst->w / (float) conf.width, dst->h / (float) conf.height );
267 SetDirectFBerror("IDirectFBDisplayLayer::SetScreenLocation", ret);
274 void DirectFB_FreeYUVOverlay(_THIS, SDL_Overlay *overlay)
276 struct private_yuvhwdata *hwdata;
278 hwdata = overlay->hwdata;
282 hwdata->surface->Release (hwdata->surface);
285 hwdata->layer->Release (hwdata->layer);