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 QNX Realtime Platform version of SDL YUV video overlays */
31 #include "SDL_video.h"
32 #include "SDL_phyuv_c.h"
33 #include "../SDL_yuvfuncs.h"
35 #define OVERLAY_STATE_UNINIT 0
36 #define OVERLAY_STATE_ACTIVE 1
38 /* The functions are used to manipulate software video overlays */
39 static struct private_yuvhwfuncs ph_yuvfuncs =
47 int grab_ptrs2(PgVideoChannel_t* channel, FRAMEDATA* Frame0, FRAMEDATA* Frame1)
51 /* Buffers have moved; re-obtain the pointers */
52 Frame0->Y = (unsigned char *)PdGetOffscreenContextPtr(channel->yplane1);
53 Frame1->Y = (unsigned char *)PdGetOffscreenContextPtr(channel->yplane2);
54 Frame0->U = (unsigned char *)PdGetOffscreenContextPtr(channel->vplane1);
55 Frame1->U = (unsigned char *)PdGetOffscreenContextPtr(channel->vplane2);
56 Frame0->V = (unsigned char *)PdGetOffscreenContextPtr(channel->uplane1);
57 Frame1->V = (unsigned char *)PdGetOffscreenContextPtr(channel->uplane2);
71 SDL_Overlay* ph_CreateYUVOverlay(_THIS, int width, int height, Uint32 format, SDL_Surface* display)
74 struct private_yuvhwdata* hwdata;
81 /* Create the overlay structure */
82 overlay = SDL_calloc(1, sizeof(SDL_Overlay));
90 /* Fill in the basic members */
91 overlay->format = format;
94 overlay->hwdata = NULL;
96 /* Set up the YUV surface function structure */
97 overlay->hwfuncs = &ph_yuvfuncs;
99 /* Create the pixel data and lookup tables */
100 hwdata = SDL_calloc(1, sizeof(struct private_yuvhwdata));
105 SDL_FreeYUVOverlay(overlay);
109 overlay->hwdata = hwdata;
112 if (overlay->hwdata->channel == NULL)
114 if ((overlay->hwdata->channel = PgCreateVideoChannel(Pg_VIDEO_CHANNEL_SCALER, 0)) == NULL)
116 SDL_SetError("ph_CreateYUVOverlay(): Create channel failed: %s\n", strerror(errno));
117 SDL_FreeYUVOverlay(overlay);
123 overlay->hwdata->forcedredraw=0;
125 PtGetAbsPosition(window, &pos.x, &pos.y);
126 overlay->hwdata->CurrentWindowPos.x = pos.x;
127 overlay->hwdata->CurrentWindowPos.y = pos.y;
128 overlay->hwdata->CurrentViewPort.pos.x = 0;
129 overlay->hwdata->CurrentViewPort.pos.y = 0;
130 overlay->hwdata->CurrentViewPort.size.w = width;
131 overlay->hwdata->CurrentViewPort.size.h = height;
132 overlay->hwdata->State = OVERLAY_STATE_UNINIT;
133 overlay->hwdata->FrameData0 = (FRAMEDATA *) SDL_calloc(1, sizeof(FRAMEDATA));
134 overlay->hwdata->FrameData1 = (FRAMEDATA *) SDL_calloc(1, sizeof(FRAMEDATA));
139 overlay->hwdata->ischromakey=0;
142 SDL_memset(&overlay->hwdata->caps, 0x00, sizeof(PgScalerCaps_t));
143 overlay->hwdata->caps.size = sizeof(PgScalerCaps_t);
144 rtncode = PgGetScalerCapabilities(overlay->hwdata->channel, i, &overlay->hwdata->caps);
147 if (overlay->hwdata->caps.format==format)
149 if ((overlay->hwdata->caps.flags & Pg_SCALER_CAP_DST_CHROMA_KEY) == Pg_SCALER_CAP_DST_CHROMA_KEY)
151 overlay->hwdata->ischromakey=1;
167 SDL_SetError("No available video ports for requested format\n");
168 SDL_FreeYUVOverlay(overlay);
172 overlay->hwdata->format = format;
173 overlay->hwdata->props.format = format;
174 overlay->hwdata->props.size = sizeof(PgScalerProps_t);
175 overlay->hwdata->props.src_dim.w = width;
176 overlay->hwdata->props.src_dim.h = height;
178 /* overlay->hwdata->chromakey = PgGetOverlayChromaColor(); */
179 overlay->hwdata->chromakey = PgRGB(12, 6, 12); /* very dark pink color */
180 overlay->hwdata->props.color_key = overlay->hwdata->chromakey;
182 PhAreaToRect(&overlay->hwdata->CurrentViewPort, &overlay->hwdata->props.viewport);
184 overlay->hwdata->props.flags = Pg_SCALER_PROP_DOUBLE_BUFFER;
186 if ((overlay->hwdata->ischromakey)&&(overlay->hwdata->chromakey))
188 overlay->hwdata->props.flags |= Pg_SCALER_PROP_CHROMA_ENABLE;
189 overlay->hwdata->props.flags |= Pg_SCALER_PROP_CHROMA_SPECIFY_KEY_MASK;
193 overlay->hwdata->props.flags &= ~Pg_SCALER_PROP_CHROMA_ENABLE;
196 rtncode = PgConfigScalerChannel(overlay->hwdata->channel, &overlay->hwdata->props);
200 case -1: SDL_SetError("PgConfigScalerChannel failed\n");
201 SDL_FreeYUVOverlay(overlay);
209 planes = grab_ptrs2(overlay->hwdata->channel, overlay->hwdata->FrameData0, overlay->hwdata->FrameData1);
211 if(overlay->hwdata->channel->yplane1 != NULL)
212 overlay->hwdata->YStride = overlay->hwdata->channel->yplane1->pitch;
213 if(overlay->hwdata->channel->vplane1 != NULL)
214 overlay->hwdata->UStride = overlay->hwdata->channel->vplane1->pitch;
215 if(overlay->hwdata->channel->uplane1 != NULL)
216 overlay->hwdata->VStride = overlay->hwdata->channel->uplane1->pitch;
218 /* check for the validness of all planes */
219 if ((overlay->hwdata->channel->yplane1 == NULL) &&
220 (overlay->hwdata->channel->uplane1 == NULL) &&
221 (overlay->hwdata->channel->vplane1 == NULL))
223 SDL_FreeYUVOverlay(overlay);
224 SDL_SetError("PgConfigScaler() returns all planes equal NULL\n");
228 overlay->hwdata->current = PgNextVideoFrame(overlay->hwdata->channel);
230 if (overlay->hwdata->current==0)
232 overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData0;
236 overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData1;
239 overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData0;
242 overlay->hwdata->locked = 1;
245 /* Find the pitch and offset values for the overlay */
246 overlay->planes = planes;
247 overlay->pitches = SDL_calloc(overlay->planes, sizeof(Uint16));
248 overlay->pixels = SDL_calloc(overlay->planes, sizeof(Uint8*));
249 if (!overlay->pitches || !overlay->pixels)
252 SDL_FreeYUVOverlay(overlay);
256 if (overlay->planes > 0)
258 overlay->pitches[0] = overlay->hwdata->channel->yplane1->pitch;
259 overlay->pixels[0] = overlay->hwdata->CurrentFrameData->Y;
261 if (overlay->planes > 1)
263 overlay->pitches[1] = overlay->hwdata->channel->vplane1->pitch;
264 overlay->pixels[1] = overlay->hwdata->CurrentFrameData->U;
266 if (overlay->planes > 2)
268 overlay->pitches[2] = overlay->hwdata->channel->uplane1->pitch;
269 overlay->pixels[2] = overlay->hwdata->CurrentFrameData->V;
272 overlay->hwdata->State = OVERLAY_STATE_ACTIVE;
273 overlay->hwdata->scaler_on = 0;
274 overlay->hw_overlay = 1;
276 current_overlay=overlay;
281 int ph_LockYUVOverlay(_THIS, SDL_Overlay* overlay)
288 overlay->hwdata->locked = 1;
290 /* overlay->hwdata->current = PgNextVideoFrame(overlay->hwdata->channel);
291 if (overlay->hwdata->current == -1)
293 SDL_SetError("ph_LockYUVOverlay: PgNextFrame() failed, bailing out\n");
294 SDL_FreeYUVOverlay(overlay);
298 if (overlay->hwdata->current == 0)
300 overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData0;
304 overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData1;
307 if (overlay->planes > 0)
309 overlay->pitches[0] = overlay->hwdata->channel->yplane1->pitch;
310 overlay->pixels[0] = overlay->hwdata->CurrentFrameData->Y;
312 if (overlay->planes > 1)
314 overlay->pitches[1] = overlay->hwdata->channel->uplane1->pitch;
315 overlay->pixels[1] = overlay->hwdata->CurrentFrameData->U;
317 if (overlay->planes > 2)
319 overlay->pitches[2] = overlay->hwdata->channel->vplane1->pitch;
320 overlay->pixels[2] = overlay->hwdata->CurrentFrameData->V;
327 void ph_UnlockYUVOverlay(_THIS, SDL_Overlay* overlay)
334 overlay->hwdata->locked = 0;
337 int ph_DisplayYUVOverlay(_THIS, SDL_Overlay* overlay, SDL_Rect* src, SDL_Rect* dst)
342 PhRect_t windowextent;
345 if ((overlay == NULL) || (overlay->hwdata==NULL))
350 if (overlay->hwdata->State == OVERLAY_STATE_UNINIT)
355 PtGetAbsPosition(window, &pos.x, &pos.y);
356 if ((pos.x!=overlay->hwdata->CurrentWindowPos.x) ||
357 (pos.y!=overlay->hwdata->CurrentWindowPos.y))
360 overlay->hwdata->CurrentWindowPos.x=pos.x;
361 overlay->hwdata->CurrentWindowPos.y=pos.y;
364 /* If CurrentViewPort position/size has been changed, then move/resize the viewport */
365 if ((overlay->hwdata->CurrentViewPort.pos.x != dst->x) ||
366 (overlay->hwdata->CurrentViewPort.pos.y != dst->y) ||
367 (overlay->hwdata->CurrentViewPort.size.w != dst->w) ||
368 (overlay->hwdata->CurrentViewPort.size.h != dst->h) ||
369 (overlay->hwdata->scaler_on==0) || (winchanged==1) ||
370 (overlay->hwdata->forcedredraw==1))
373 if (overlay->hwdata->ischromakey==1)
375 /* restore screen behind the overlay/chroma color. */
376 backrect.x=overlay->hwdata->CurrentViewPort.pos.x;
377 backrect.y=overlay->hwdata->CurrentViewPort.pos.y;
378 backrect.w=overlay->hwdata->CurrentViewPort.size.w;
379 backrect.h=overlay->hwdata->CurrentViewPort.size.h;
380 this->UpdateRects(this, 1, &backrect);
382 /* Draw the new rectangle of the chroma color at the viewport position */
383 PgSetFillColor(overlay->hwdata->chromakey);
384 PgDrawIRect(dst->x, dst->y, dst->x+dst->w-1, dst->y+dst->h-1, Pg_DRAW_FILL);
388 overlay->hwdata->props.flags |= Pg_SCALER_PROP_SCALER_ENABLE;
389 overlay->hwdata->scaler_on = 1;
391 PhWindowQueryVisible(Ph_QUERY_CONSOLE, 0, PtWidgetRid(window), &windowextent);
392 overlay->hwdata->CurrentViewPort.pos.x = pos.x-windowextent.ul.x+dst->x;
393 overlay->hwdata->CurrentViewPort.pos.y = pos.y-windowextent.ul.y+dst->y;
394 overlay->hwdata->CurrentViewPort.size.w = dst->w;
395 overlay->hwdata->CurrentViewPort.size.h = dst->h;
396 PhAreaToRect(&overlay->hwdata->CurrentViewPort, &overlay->hwdata->props.viewport);
397 overlay->hwdata->CurrentViewPort.pos.x = dst->x;
398 overlay->hwdata->CurrentViewPort.pos.y = dst->y;
400 rtncode = PgConfigScalerChannel(overlay->hwdata->channel, &(overlay->hwdata->props));
405 SDL_SetError("PgConfigScalerChannel() function failed\n");
406 SDL_FreeYUVOverlay(overlay);
409 grab_ptrs2(overlay->hwdata->channel, overlay->hwdata->FrameData0, overlay->hwdata->FrameData1);
419 if (overlay->hwdata->locked==0)
421 overlay->hwdata->current = PgNextVideoFrame(overlay->hwdata->channel);
422 if (overlay->hwdata->current == -1)
424 SDL_SetError("ph_LockYUVOverlay: PgNextFrame() failed, bailing out\n");
425 SDL_FreeYUVOverlay(overlay);
429 if (overlay->hwdata->current == 0)
431 overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData0;
435 overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData1;
438 if (overlay->planes > 0)
440 overlay->pitches[0] = overlay->hwdata->channel->yplane1->pitch;
441 overlay->pixels[0] = overlay->hwdata->CurrentFrameData->Y;
443 if (overlay->planes > 1)
445 overlay->pitches[1] = overlay->hwdata->channel->uplane1->pitch;
446 overlay->pixels[1] = overlay->hwdata->CurrentFrameData->U;
448 if (overlay->planes > 2)
450 overlay->pitches[2] = overlay->hwdata->channel->vplane1->pitch;
451 overlay->pixels[2] = overlay->hwdata->CurrentFrameData->V;
459 void ph_FreeYUVOverlay(_THIS, SDL_Overlay *overlay)
468 if (overlay->hwdata == NULL)
473 current_overlay=NULL;
475 /* restore screen behind the overlay/chroma color. */
476 backrect.x=overlay->hwdata->CurrentViewPort.pos.x;
477 backrect.y=overlay->hwdata->CurrentViewPort.pos.y;
478 backrect.w=overlay->hwdata->CurrentViewPort.size.w;
479 backrect.h=overlay->hwdata->CurrentViewPort.size.h;
480 this->UpdateRects(this, 1, &backrect);
482 /* it is need for some buggy drivers, that can't hide overlay before */
483 /* freeing buffer, so we got trash on the srceen */
484 overlay->hwdata->props.flags &= ~Pg_SCALER_PROP_SCALER_ENABLE;
485 PgConfigScalerChannel(overlay->hwdata->channel, &(overlay->hwdata->props));
487 overlay->hwdata->scaler_on = 0;
488 overlay->hwdata->State = OVERLAY_STATE_UNINIT;
490 if (overlay->hwdata->channel != NULL)
492 PgDestroyVideoChannel(overlay->hwdata->channel);
493 overlay->hwdata->channel = NULL;
497 overlay->hwdata->CurrentFrameData = NULL;
499 SDL_free(overlay->hwdata->FrameData0);
500 SDL_free(overlay->hwdata->FrameData1);
501 overlay->hwdata->FrameData0 = NULL;
502 overlay->hwdata->FrameData1 = NULL;
503 SDL_free(overlay->hwdata);