xref: /haiku/src/add-ons/media/media-add-ons/videowindow/VideoView.cpp (revision 72ac59d172768aff9504550e1e17521ff9016288)
180634794SMarcus Overhagen /*
218c2aaaeSMarcus Overhagen  * Copyright (C) 2006 Marcus Overhagen <marcus@overhagen.de>. All rights reserved.
380634794SMarcus Overhagen  *
418c2aaaeSMarcus Overhagen  * Distributed under the terms of the MIT License.
580634794SMarcus Overhagen  */
680634794SMarcus Overhagen #include "VideoNode.h"
7*72ac59d1SMaurice Kalinowski #include "VideoView.h"
8*72ac59d1SMaurice Kalinowski #include "debug.h"
9*72ac59d1SMaurice Kalinowski 
10*72ac59d1SMaurice Kalinowski #include <Bitmap.h>
11*72ac59d1SMaurice Kalinowski #include <Locker.h>
12*72ac59d1SMaurice Kalinowski #include <MediaRoster.h>
13*72ac59d1SMaurice Kalinowski #include <Message.h>
14*72ac59d1SMaurice Kalinowski 
1580634794SMarcus Overhagen 
1680634794SMarcus Overhagen #include <stdio.h>
17*72ac59d1SMaurice Kalinowski #include <stdlib.h>
1880634794SMarcus Overhagen #include <string.h>
1980634794SMarcus Overhagen 
20*72ac59d1SMaurice Kalinowski 
VideoView(BRect frame,const char * name,uint32 resizeMask,uint32 flags,VideoNode * node)21*72ac59d1SMaurice Kalinowski VideoView::VideoView(BRect frame, const char *name, uint32 resizeMask, uint32 flags, VideoNode *node)
2280634794SMarcus Overhagen  :	BView(frame, name, resizeMask, flags)
23*72ac59d1SMaurice Kalinowski  ,	fVideoNode(node)
2480634794SMarcus Overhagen  ,	fOverlayActive(false)
2580634794SMarcus Overhagen {
2680634794SMarcus Overhagen 	SetViewColor(B_TRANSPARENT_COLOR);
2780634794SMarcus Overhagen }
2880634794SMarcus Overhagen 
2980634794SMarcus Overhagen 
~VideoView()3080634794SMarcus Overhagen VideoView::~VideoView()
3180634794SMarcus Overhagen {
3280634794SMarcus Overhagen }
3380634794SMarcus Overhagen 
3480634794SMarcus Overhagen 
3580634794SMarcus Overhagen void
AttachedToWindow()3680634794SMarcus Overhagen VideoView::AttachedToWindow()
3780634794SMarcus Overhagen {
3880634794SMarcus Overhagen }
3980634794SMarcus Overhagen 
4080634794SMarcus Overhagen 
4180634794SMarcus Overhagen VideoNode *
Node()4280634794SMarcus Overhagen VideoView::Node()
4380634794SMarcus Overhagen {
4480634794SMarcus Overhagen 	return fVideoNode;
4580634794SMarcus Overhagen }
4680634794SMarcus Overhagen 
4780634794SMarcus Overhagen 
4880634794SMarcus Overhagen void
OverlayLockAcquire()4980634794SMarcus Overhagen VideoView::OverlayLockAcquire()
5080634794SMarcus Overhagen {
51*72ac59d1SMaurice Kalinowski 	CALLED();
5280634794SMarcus Overhagen }
5380634794SMarcus Overhagen 
5480634794SMarcus Overhagen 
5580634794SMarcus Overhagen void
OverlayLockRelease()5680634794SMarcus Overhagen VideoView::OverlayLockRelease()
5780634794SMarcus Overhagen {
58*72ac59d1SMaurice Kalinowski 	CALLED();
5980634794SMarcus Overhagen 	// overlaybitmap->UnlockBits
6080634794SMarcus Overhagen }
6180634794SMarcus Overhagen 
6280634794SMarcus Overhagen 
6380634794SMarcus Overhagen void
OverlayScreenshotPrepare()6480634794SMarcus Overhagen VideoView::OverlayScreenshotPrepare()
6580634794SMarcus Overhagen {
66*72ac59d1SMaurice Kalinowski 	CALLED();
6780634794SMarcus Overhagen /*
6880634794SMarcus Overhagen 	fVideoNode->LockBitmap();
6980634794SMarcus Overhagen 	if (fOverlayActive) {
7080634794SMarcus Overhagen 		BBitmap *bmp = fVideoNode->Bitmap();
7180634794SMarcus Overhagen 		if (bmp) {
7280634794SMarcus Overhagen //			Window()->UpdateIfNeeded();
7380634794SMarcus Overhagen //			Sync();
7480634794SMarcus Overhagen 			BBitmap *tmp = new BBitmap(bmp->Bounds(), 0, B_RGB32);
7580634794SMarcus Overhagen //			ConvertBitmap(tmp, bmp);
7680634794SMarcus Overhagen 			ClearViewOverlay();
7780634794SMarcus Overhagen 			DrawBitmap(tmp, Bounds());
7880634794SMarcus Overhagen 			delete tmp;
7980634794SMarcus Overhagen //			Sync();
8080634794SMarcus Overhagen 		}
8180634794SMarcus Overhagen 	}
8280634794SMarcus Overhagen 	fVideoNode->UnlockBitmap();
8380634794SMarcus Overhagen */
8480634794SMarcus Overhagen }
8580634794SMarcus Overhagen 
8680634794SMarcus Overhagen 
8780634794SMarcus Overhagen void
OverlayScreenshotCleanup()8880634794SMarcus Overhagen VideoView::OverlayScreenshotCleanup()
8980634794SMarcus Overhagen {
90*72ac59d1SMaurice Kalinowski 	CALLED();
9180634794SMarcus Overhagen /*
9280634794SMarcus Overhagen 	snooze(50000); // give app server some time to take the screenshot
9380634794SMarcus Overhagen 	fVideoNode->LockBitmap();
9480634794SMarcus Overhagen 	if (fOverlayActive) {
9580634794SMarcus Overhagen 		BBitmap *bmp = fVideoNode->Bitmap();
9680634794SMarcus Overhagen 		if (bmp) {
9780634794SMarcus Overhagen 			DrawBitmap(bmp, Bounds());
9880634794SMarcus Overhagen 			SetViewOverlay(bmp, bmp->Bounds(), Bounds(), &fOverlayKeyColor,
9980634794SMarcus Overhagen 				B_FOLLOW_ALL, B_OVERLAY_FILTER_HORIZONTAL | B_OVERLAY_FILTER_VERTICAL);
10080634794SMarcus Overhagen 			Invalidate();
10180634794SMarcus Overhagen 		}
10280634794SMarcus Overhagen 	}
10380634794SMarcus Overhagen 	fVideoNode->UnlockBitmap();
10480634794SMarcus Overhagen */
10580634794SMarcus Overhagen }
10680634794SMarcus Overhagen 
10780634794SMarcus Overhagen 
10880634794SMarcus Overhagen void
RemoveVideoDisplay()10980634794SMarcus Overhagen VideoView::RemoveVideoDisplay()
11080634794SMarcus Overhagen {
111*72ac59d1SMaurice Kalinowski 	CALLED();
11280634794SMarcus Overhagen 
11380634794SMarcus Overhagen 	if (fOverlayActive) {
11480634794SMarcus Overhagen 		ClearViewOverlay();
11580634794SMarcus Overhagen 		fOverlayActive = false;
11680634794SMarcus Overhagen 	}
11780634794SMarcus Overhagen 	Invalidate();
11880634794SMarcus Overhagen }
11980634794SMarcus Overhagen 
12080634794SMarcus Overhagen 
12180634794SMarcus Overhagen void
RemoveOverlay()12280634794SMarcus Overhagen VideoView::RemoveOverlay()
12380634794SMarcus Overhagen {
124*72ac59d1SMaurice Kalinowski 	CALLED();
12580634794SMarcus Overhagen 	if (LockLooperWithTimeout(50000) == B_OK) {
12680634794SMarcus Overhagen 		ClearViewOverlay();
12780634794SMarcus Overhagen 		fOverlayActive = false;
12880634794SMarcus Overhagen 		UnlockLooper();
12980634794SMarcus Overhagen 	}
13080634794SMarcus Overhagen }
13180634794SMarcus Overhagen 
13280634794SMarcus Overhagen 
13380634794SMarcus Overhagen void
Draw(BRect updateRect)13480634794SMarcus Overhagen VideoView::Draw(BRect updateRect)
13580634794SMarcus Overhagen {
13680634794SMarcus Overhagen 	if (fOverlayActive) {
13780634794SMarcus Overhagen 		SetHighColor(fOverlayKeyColor);
13880634794SMarcus Overhagen 		FillRect(updateRect);
13980634794SMarcus Overhagen 	} else {
14080634794SMarcus Overhagen 		fVideoNode->LockBitmap();
14180634794SMarcus Overhagen 		BBitmap *bmp = fVideoNode->Bitmap();
142*72ac59d1SMaurice Kalinowski 		if (!bmp) {
143*72ac59d1SMaurice Kalinowski 			SetHighColor(0, 0, 0, 0);
144*72ac59d1SMaurice Kalinowski 			FillRect(updateRect);
145*72ac59d1SMaurice Kalinowski 		} else
14680634794SMarcus Overhagen 			DrawBitmap(bmp, Bounds());
14780634794SMarcus Overhagen 		fVideoNode->UnlockBitmap();
14880634794SMarcus Overhagen 	}
14980634794SMarcus Overhagen }
15080634794SMarcus Overhagen 
15180634794SMarcus Overhagen 
15280634794SMarcus Overhagen void
DrawFrame()15380634794SMarcus Overhagen VideoView::DrawFrame()
15480634794SMarcus Overhagen {
155*72ac59d1SMaurice Kalinowski 	//CALLED();
15680634794SMarcus Overhagen 
15780634794SMarcus Overhagen 	bool want_overlay = fVideoNode->IsOverlayActive();
15880634794SMarcus Overhagen 
15980634794SMarcus Overhagen 	if (!want_overlay && fOverlayActive) {
16080634794SMarcus Overhagen 		if (LockLooperWithTimeout(50000) == B_OK) {
16180634794SMarcus Overhagen 			ClearViewOverlay();
16280634794SMarcus Overhagen 			UnlockLooper();
16380634794SMarcus Overhagen 			fOverlayActive = false;
16480634794SMarcus Overhagen 		} else {
165*72ac59d1SMaurice Kalinowski 			fprintf(stderr, "VideoView::DrawFrame: cannot ClearViewOverlay, as LockLooperWithTimeout failed\n");
16680634794SMarcus Overhagen 		}
16780634794SMarcus Overhagen 	}
168*72ac59d1SMaurice Kalinowski 
16980634794SMarcus Overhagen 	if (want_overlay && !fOverlayActive) {
17080634794SMarcus Overhagen 		fVideoNode->LockBitmap();
17180634794SMarcus Overhagen 		BBitmap *bmp = fVideoNode->Bitmap();
17280634794SMarcus Overhagen 		if (bmp && LockLooperWithTimeout(50000) == B_OK) {
17380634794SMarcus Overhagen 			SetViewOverlay(bmp, bmp->Bounds(), Bounds(), &fOverlayKeyColor,
17480634794SMarcus Overhagen 				B_FOLLOW_ALL, B_OVERLAY_FILTER_HORIZONTAL | B_OVERLAY_FILTER_VERTICAL);
17580634794SMarcus Overhagen 			fOverlayActive = true;
17680634794SMarcus Overhagen 
17780634794SMarcus Overhagen 			Invalidate();
17880634794SMarcus Overhagen 			UnlockLooper();
17980634794SMarcus Overhagen 		}
18080634794SMarcus Overhagen 		fVideoNode->UnlockBitmap();
18180634794SMarcus Overhagen 	}
18280634794SMarcus Overhagen 	if (!fOverlayActive) {
18380634794SMarcus Overhagen 		if (LockLooperWithTimeout(50000) != B_OK)
18480634794SMarcus Overhagen 			return;
18580634794SMarcus Overhagen 		Invalidate();
18680634794SMarcus Overhagen 		UnlockLooper();
18780634794SMarcus Overhagen 	}
18880634794SMarcus Overhagen }
18980634794SMarcus Overhagen 
19080634794SMarcus Overhagen 
19180634794SMarcus Overhagen void
MessageReceived(BMessage * msg)19280634794SMarcus Overhagen VideoView::MessageReceived(BMessage *msg)
19380634794SMarcus Overhagen {
19480634794SMarcus Overhagen 	switch (msg->what) {
19580634794SMarcus Overhagen 
19680634794SMarcus Overhagen 		default:
19780634794SMarcus Overhagen 			BView::MessageReceived(msg);
19880634794SMarcus Overhagen 	}
19980634794SMarcus Overhagen }
20080634794SMarcus Overhagen 
20180634794SMarcus Overhagen 
20280634794SMarcus Overhagen bool
IsOverlaySupported()20380634794SMarcus Overhagen VideoView::IsOverlaySupported()
20480634794SMarcus Overhagen {
20580634794SMarcus Overhagen 	struct colorcombo {
20680634794SMarcus Overhagen 		color_space colspace;
20780634794SMarcus Overhagen 		const char *name;
20880634794SMarcus Overhagen 	} colspace[] = {
20980634794SMarcus Overhagen 		{ B_RGB32,		"B_RGB32"},
21080634794SMarcus Overhagen 		{ B_RGBA32,		"B_RGBA32"},
21180634794SMarcus Overhagen 		{ B_RGB24,		"B_RGB24"},
21280634794SMarcus Overhagen 		{ B_RGB16,		"B_RGB16"},
21380634794SMarcus Overhagen 		{ B_RGB15,		"B_RGB15"},
21480634794SMarcus Overhagen 		{ B_RGBA15,		"B_RGBA15"},
21580634794SMarcus Overhagen 		{ B_RGB32_BIG,	"B_RGB32_BIG"},
21680634794SMarcus Overhagen 		{ B_RGBA32_BIG,	"B_RGBA32_BIG "},
21780634794SMarcus Overhagen 		{ B_RGB24_BIG,	"B_RGB24_BIG "},
21880634794SMarcus Overhagen 		{ B_RGB16_BIG,	"B_RGB16_BIG "},
21980634794SMarcus Overhagen 		{ B_RGB15_BIG,	"B_RGB15_BIG "},
22080634794SMarcus Overhagen 		{ B_RGBA15_BIG, "B_RGBA15_BIG "},
22180634794SMarcus Overhagen 		{ B_YCbCr422,	"B_YCbCr422"},
22280634794SMarcus Overhagen 		{ B_YCbCr411,	"B_YCbCr411"},
22380634794SMarcus Overhagen 		{ B_YCbCr444,	"B_YCbCr444"},
22480634794SMarcus Overhagen 		{ B_YCbCr420,	"B_YCbCr420"},
22580634794SMarcus Overhagen 		{ B_YUV422,		"B_YUV422"},
22680634794SMarcus Overhagen 		{ B_YUV411,		"B_YUV411"},
22780634794SMarcus Overhagen 		{ B_YUV444,		"B_YUV444"},
22880634794SMarcus Overhagen 		{ B_YUV420,		"B_YUV420"},
22980634794SMarcus Overhagen 		{ B_NO_COLOR_SPACE, NULL}
23080634794SMarcus Overhagen 	};
23180634794SMarcus Overhagen 
23280634794SMarcus Overhagen 	bool supported = false;
23380634794SMarcus Overhagen 	for (int i = 0; colspace[i].name; i++) {
23480634794SMarcus Overhagen 		BBitmap *test = new BBitmap(BRect(0,0,320,240),	B_BITMAP_WILL_OVERLAY | B_BITMAP_RESERVE_OVERLAY_CHANNEL, colspace[i].colspace);
23580634794SMarcus Overhagen 		if (test->InitCheck() == B_OK) {
23680634794SMarcus Overhagen 			printf("Display supports %s (0x%08x) overlay\n", colspace[i].name, colspace[i].colspace);
23780634794SMarcus Overhagen 			supported = true;
23880634794SMarcus Overhagen 		}
23980634794SMarcus Overhagen 		delete test;
24080634794SMarcus Overhagen //		if (supported)
24180634794SMarcus Overhagen //			break;
24280634794SMarcus Overhagen 	}
24380634794SMarcus Overhagen 	return supported;
24480634794SMarcus Overhagen }
245