19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This is the BeOS version of SDL YUV video overlays */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_video.h"
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_sysyuv.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_yuvfuncs.h"
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern "C" {
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The functions used to manipulate software video overlays */
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic struct private_yuvhwfuncs be_yuvfuncs =
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    BE_LockYUVOverlay,
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    BE_UnlockYUVOverlay,
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    BE_DisplayYUVOverlay,
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    BE_FreeYUVOverlay
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallBBitmap * BE_GetOverlayBitmap(BRect bounds, color_space cs) {
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BBitmap *bbitmap;
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bbitmap = new BBitmap(bounds,B_BITMAP_WILL_OVERLAY,cs);
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!bbitmap || bbitmap->InitCheck() != B_OK) {
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		delete bbitmap;
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay_restrictions r;
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bbitmap->GetOverlayRestrictions(&r);
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	uint32 width = bounds.IntegerWidth() + 1;
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	uint32 height = bounds.IntegerHeight() + 1;
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	uint32 width_padding = 0;
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	uint32 height_padding = 0;
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ((r.source.horizontal_alignment != 0) ||
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    (r.source.vertical_alignment != 0)) {
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		delete bbitmap;
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (r.source.width_alignment != 0) {
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		uint32 aligned_width = r.source.width_alignment + 1;
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (width % aligned_width > 0) {
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			width_padding = aligned_width - width % aligned_width;
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (r.source.height_alignment != 0) {
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		uint32 aligned_height = r.source.height_alignment + 1;
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (height % aligned_height > 0) {
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			fprintf(stderr,"GetOverlayBitmap failed height alignment\n");
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			fprintf(stderr,"- height = %lu, aligned_height = %lu\n",height,aligned_height);
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			delete bbitmap;
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return 0;
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ((r.source.min_width > width) ||
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    (r.source.min_height > height) ||
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    (r.source.max_width < width) ||
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    (r.source.max_height < height)) {
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fprintf(stderr,"GetOverlayBitmap failed bounds tests\n");
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    delete bbitmap;
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    return 0;
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ((width_padding != 0) || (height_padding != 0)) {
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		delete bbitmap;
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bounds.Set(bounds.left,bounds.top,bounds.right+width_padding,bounds.bottom+height_padding);
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bbitmap = new BBitmap(bounds,B_BITMAP_WILL_OVERLAY,cs);
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (!bbitmap || bbitmap->InitCheck() != B_OK) {
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			fprintf(stderr,"GetOverlayBitmap failed late\n");
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			delete bbitmap;
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return 0;
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return bbitmap;
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// See <GraphicsDefs.h> [btw: Cb=U, Cr=V]
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// See also http://www.fourcc.org/indexyuv.htm
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallcolor_space convert_color_space(Uint32 format) {
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (format) {
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case SDL_YV12_OVERLAY:
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return B_YUV9;
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case SDL_IYUV_OVERLAY:
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return B_YUV12;
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case SDL_YUY2_OVERLAY:
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return B_YCbCr422;
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case SDL_UYVY_OVERLAY:
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return B_YUV422;
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case SDL_YVYU_OVERLAY: // not supported on beos?
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return B_NO_COLOR_SPACE;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	default:
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return B_NO_COLOR_SPACE;
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// See SDL_video.h
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint count_planes(Uint32 format) {
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (format) {
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case SDL_YV12_OVERLAY:
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case SDL_IYUV_OVERLAY:
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 3;
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case SDL_YUY2_OVERLAY:
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case SDL_UYVY_OVERLAY:
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case SDL_YVYU_OVERLAY:
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 1;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	default:
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Overlay *BE_CreateYUVOverlay(_THIS, int width, int height, Uint32 format, SDL_Surface *display) {
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Overlay* overlay;
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct private_yuvhwdata* hwdata;
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BBitmap *bbitmap;
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int planes;
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BRect bounds;
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	color_space cs;
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* find the appropriate BeOS colorspace descriptor */
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cs = convert_color_space(format);
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (cs == B_NO_COLOR_SPACE)
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return NULL;
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* count planes */
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	planes = count_planes(format);
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (planes == 0)
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return NULL;
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* TODO: figure out planar modes, if anyone cares */
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (planes == 3)
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return NULL;
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Create the overlay structure */
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    overlay = (SDL_Overlay*)SDL_calloc(1, sizeof(SDL_Overlay));
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (overlay == NULL)
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_OutOfMemory();
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return NULL;
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Fill in the basic members */
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    overlay->format = format;
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    overlay->w = width;
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    overlay->h = height;
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    overlay->hwdata = NULL;
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Set up the YUV surface function structure */
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    overlay->hwfuncs = &be_yuvfuncs;
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Create the pixel data and lookup tables */
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    hwdata = (struct private_yuvhwdata*)SDL_calloc(1, sizeof(struct private_yuvhwdata));
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (hwdata == NULL)
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_OutOfMemory();
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_FreeYUVOverlay(overlay);
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return NULL;
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    overlay->hwdata = hwdata;
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->hwdata->display = display;
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->hwdata->bview = NULL;
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->hwdata->bbitmap = NULL;
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->hwdata->locked = 0;
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create the BBitmap framebuffer */
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bounds.top = 0;	bounds.left = 0;
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bounds.right = width-1;
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bounds.bottom = height-1;
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BView * bview = new BView(bounds,"overlay",B_FOLLOW_NONE,B_WILL_DRAW);
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!bview) {
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_FreeYUVOverlay(overlay);
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return NULL;
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->hwdata->bview = bview;
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->hwdata->first_display = true;
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bview->Hide();
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bbitmap = BE_GetOverlayBitmap(bounds,cs);
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!bbitmap) {
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		overlay->hwdata->bbitmap = NULL;
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeYUVOverlay(overlay);
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return NULL;
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->hwdata->bbitmap = bbitmap;
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->planes = planes;
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->pitches = (Uint16*)SDL_calloc(overlay->planes, sizeof(Uint16));
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->pixels  = (Uint8**)SDL_calloc(overlay->planes, sizeof(Uint8*));
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!overlay->pitches || !overlay->pixels)
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_OutOfMemory();
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_FreeYUVOverlay(overlay);
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return(NULL);
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   	overlay->pitches[0] = bbitmap->BytesPerRow();
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   	overlay->pixels[0]  = (Uint8 *)bbitmap->Bits();
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->hw_overlay = 1;
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (SDL_Win->LockWithTimeout(1000000) != B_OK) {
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_FreeYUVOverlay(overlay);
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return(NULL);
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BView * view = SDL_Win->View();
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    view->AddChild(bview);
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    rgb_color key;
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    bview->SetViewOverlay(bbitmap,bounds,bview->Bounds(),&key,B_FOLLOW_ALL,
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         B_OVERLAY_FILTER_HORIZONTAL|B_OVERLAY_FILTER_VERTICAL);
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    bview->SetViewColor(key);
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    bview->Flush();
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Win->Unlock();
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	current_overlay=overlay;
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return overlay;
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint BE_LockYUVOverlay(_THIS, SDL_Overlay* overlay)
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (overlay == NULL)
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    overlay->hwdata->locked = 1;
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid BE_UnlockYUVOverlay(_THIS, SDL_Overlay* overlay)
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (overlay == NULL)
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         return;
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    overlay->hwdata->locked = 0;
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint BE_DisplayYUVOverlay(_THIS, SDL_Overlay* overlay, SDL_Rect* src, SDL_Rect *dst)
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ((overlay == NULL) || (overlay->hwdata==NULL)
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        || (overlay->hwdata->bview==NULL) || (SDL_Win->View() == NULL))
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -1;
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (SDL_Win->LockWithTimeout(50000) != B_OK) {
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    BView * bview = overlay->hwdata->bview;
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (SDL_Win->IsFullScreen()) {
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    	int left,top;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    	SDL_Win->GetXYOffset(left,top);
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    bview->MoveTo(left+dst->x,top+dst->y);
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else {
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    bview->MoveTo(dst->x,dst->y);
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    bview->ResizeTo(dst->w,dst->h);
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    bview->Flush();
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (overlay->hwdata->first_display) {
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bview->Show();
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		overlay->hwdata->first_display = false;
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_Win->Unlock();
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid BE_FreeYUVOverlay(_THIS, SDL_Overlay *overlay)
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (overlay == NULL)
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return;
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (overlay->hwdata == NULL)
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return;
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    current_overlay=NULL;
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	delete overlay->hwdata->bbitmap;
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(overlay->hwdata);
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}; // extern "C"
315