1/* Written by Rudolf Cornelissen 05/2002-5/2009 */
2
3/* Note on 'missing features' in BeOS 5.0.3 and DANO:
4 * BeOS needs to define more colorspaces! It would be nice if BeOS would support the FourCC 'definitions'
5 * of colorspaces. These colorspaces are 32bit words, so it could be simply done (or is it already so?)
6 */
7
8#define MODULE_BIT 0x00000400
9
10#include "acc_std.h"
11
12/* define the supported overlay input colorspaces */
13/* It would be nice to have the YUV4:2:0 2-plane mode implemented also later on, but the Be colorspace
14 * definitions (in GraphicsDefs.h, R5.0.3 and DANO5.1d0) do not include this one... */
15static uint32 overlay_colorspaces [] = { (uint32)B_YCbCr422, (uint32)B_NO_COLOR_SPACE };
16
17uint32 OVERLAY_COUNT(const display_mode *dm)
18// This method is never used AFAIK though it *is* exported on R5.0.3 and DANO.
19// Does someone know howto invoke it?
20{
21	LOG(4,("Overlay: count called\n"));
22
23	/* check for NULL pointer */
24	if (dm == NULL)
25	{
26		LOG(4,("Overlay: No display mode specified!\n"));
27	}
28	/* apparantly overlay count should report the number of 'overlay units' on the card */
29	return 1;
30}
31
32const uint32 *OVERLAY_SUPPORTED_SPACES(const display_mode *dm)
33// This method is never used AFAIK though it *is* exported on R5.0.3 and DANO.
34// Does someone know howto invoke it?
35{
36	LOG(4,("Overlay: supported_spaces called.\n"));
37
38	/* check for NULL pointer */
39	if (dm == NULL)
40	{
41		LOG(4,("Overlay: No display mode specified!\n"));
42		return NULL;
43	}
44
45	/* assuming interlaced VGA is not supported */
46	if (dm->timing.flags & B_TIMING_INTERLACED)
47	{
48		return NULL;
49	}
50	/* return a B_NO_COLOR_SPACE terminated list */
51	return &overlay_colorspaces[0];
52}
53
54uint32 OVERLAY_SUPPORTED_FEATURES(uint32 a_color_space)
55// This method is never used AFAIK. On R5.0.3 and DANO it is not even exported!
56{
57	LOG(4,("Overlay: supported_features: color_space $%08x\n",a_color_space));
58
59	/* check what features are supported for the current overlaybitmap colorspace */
60	switch (a_color_space)
61	{
62	default:
63			return
64				( B_OVERLAY_KEYING_USES_ALPHA 	 |
65				  B_OVERLAY_COLOR_KEY 			 |
66				  B_OVERLAY_HORIZONTAL_FILTERING |
67				  B_OVERLAY_VERTICAL_FILTERING );
68	}
69}
70
71const overlay_buffer *ALLOCATE_OVERLAY_BUFFER(color_space cs, uint16 width, uint16 height)
72{
73	int offset = 0;					/* used to determine next buffer to create */
74	uint32 adress, adress2, temp32;	/* used to calculate buffer adresses */
75	uint32 oldsize = 0;				/* used to 'squeeze' new buffers between already existing ones */
76	int cnt;						/* loopcounter */
77
78	/* acquire the shared benaphore */
79	AQUIRE_BEN(si->overlay.lock)
80
81	LOG(4,("Overlay: cardRAM_start = $%08x\n",(uint32)((uint8*)si->framebuffer)));
82	LOG(4,("Overlay: cardRAM_start_DMA = $%08x\n",(uint32)((uint8*)si->framebuffer_pci)));
83	LOG(4,("Overlay: cardRAM_size = %3.3fMb\n",(si->ps.memory_size / (1024.0 * 1024.0))));
84
85	/* find first empty slot (room for another buffer?) */
86	for (offset = 0; offset < MAXBUFFERS; offset++)
87	{
88		if (si->overlay.myBuffer[offset].buffer == NULL) break;
89	}
90
91	LOG(4,("Overlay: Allocate_buffer offset = %d\n",offset));
92
93	if (offset < MAXBUFFERS)
94	/* setup new scaler input buffer */
95	{
96		switch (cs)
97		{
98			case B_YCbCr422:
99					if (si->ps.card_arch < NV10A)
100					{
101						/* check if slopspace is needed: RIVA128 and TNT need ~0x000f. */
102						si->overlay.myBuffer[offset].width = ((width + 0x000f) & ~0x000f);
103					}
104					else
105					{
106						/* check if slopspace is needed: GeForce need ~0x001f. */
107						/* fixme:
108						 * update needed for GF DVDmax support to adhere to CRTC2 constraints?? */
109						si->overlay.myBuffer[offset].width = ((width + 0x001f) & ~0x001f);
110					}
111					si->overlay.myBuffer[offset].bytes_per_row = 2 * si->overlay.myBuffer[offset].width;
112
113					/* check if the requested horizontal pitch is supported: */
114					//fixme: tune for GF and TNT...
115					if (si->overlay.myBuffer[offset].width > 4088)
116					{
117						LOG(4,("Overlay: Sorry, requested buffer pitch not supported, aborted\n"));
118
119						/* release the shared benaphore */
120						RELEASE_BEN(si->overlay.lock)
121
122						return NULL;
123					}
124					break;
125			default:
126					/* unsupported colorspace! */
127					LOG(4,("Overlay: Sorry, colorspace $%08x not supported, aborted\n",cs));
128
129					/* release the shared benaphore */
130					RELEASE_BEN(si->overlay.lock)
131
132					return NULL;
133					break;
134		}
135
136		/* checkout input picture size */
137		switch (si->ps.card_arch) {
138		case NV04A:
139			/* all DVD's are OK, but HDTV 1280x720p is not: it displays ghost images
140			 * (still find exact limit..) */
141
142			/* check if the requested buffer width is supported */
143			if (si->overlay.myBuffer[offset].width > 1024) {
144				LOG(4,("Overlay: Sorry, requested buffer width not supported, aborted\n"));
145
146				/* release the shared benaphore */
147				RELEASE_BEN(si->overlay.lock)
148				return NULL;
149			}
150			/* check if the requested buffer height is supported */
151			if (height > 1024) {
152				LOG(4,("Overlay: Sorry, requested buffer height not supported, aborted\n"));
153
154				/* release the shared benaphore */
155				RELEASE_BEN(si->overlay.lock)
156				return NULL;
157			}
158			break;
159		default:
160			/* HDTV 1920x1080p is confirmed OK on NV11 and higher cards */
161
162			/* check if the requested buffer width is supported */
163			if (si->overlay.myBuffer[offset].width > 1920) {
164				LOG(4,("Overlay: Sorry, requested buffer width not supported, aborted\n"));
165
166				/* release the shared benaphore */
167				RELEASE_BEN(si->overlay.lock)
168				return NULL;
169			}
170			/* check if the requested buffer height is supported */
171			if (height > 1080) {
172				LOG(4,("Overlay: Sorry, requested buffer height not supported, aborted\n"));
173
174				/* release the shared benaphore */
175				RELEASE_BEN(si->overlay.lock)
176				return NULL;
177			}
178			break;
179		}
180
181		/* store slopspace (in pixels) for each bitmap for use by 'overlay unit' (BES) */
182		si->overlay.myBufInfo[offset].slopspace = si->overlay.myBuffer[offset].width - width;
183
184		si->overlay.myBuffer[offset].space = cs;
185		si->overlay.myBuffer[offset].height = height;
186
187		/* we define the overlay buffers to reside 'in the back' of the cards RAM */
188		/* NOTE to app programmers:
189		 * Beware that an app using overlay needs to track workspace switches and screenprefs
190		 * changes. If such an action is detected, the app needs to reset it's pointers to the
191		 * newly created overlay bitmaps, which will be assigned by BeOS automatically after such
192		 * an event. (Also the app needs to respect the new overlay_constraints that will be applicable!)
193		 *
194		 * It is entirely possible that new bitmaps may *not* be re-setup at all, or less of them
195		 * than previously setup by the app might be re-setup. This is due to cardRAM restraints then.
196		 * This means that the app should also check for NULL pointers returned by the bitmaps,
197		 * and if this happens, it needs to fallback to single buffered overlay or even fallback to
198		 * bitmap output for the new situation. */
199
200		/* Another NOTE for app programmers:
201		 * A *positive* side-effect of assigning the first overlay buffer exactly at the end of the
202		 * cardRAM is that apps that try to write beyond the buffer's space get a segfault immediately.
203		 * This *greatly* simplifies tracking such errors!
204		 * Of course such errors may lead to strange effects in the app or driver behaviour if they are
205		 * not hunted down and removed.. */
206
207		/* calculate first free RAM adress in card:
208		 * Driver setup is as follows:
209		 * card base: 		- hardware cursor bitmap (if used),
210		 * directly above	- screen memory for both heads */
211		adress2 = (((uint32)((uint8*)si->fbc.frame_buffer)) +	/* cursor already included here */
212			(si->fbc.bytes_per_row * si->dm.virtual_height));	/* size in bytes of screen(s) */
213		LOG(4,("Overlay: first free cardRAM virtual adress $%08x\n", adress2));
214
215		/* calculate 'preliminary' buffer size including slopspace */
216		oldsize = si->overlay.myBufInfo[offset].size;
217		si->overlay.myBufInfo[offset].size =
218			si->overlay.myBuffer[offset].bytes_per_row * si->overlay.myBuffer[offset].height;
219
220		/* calculate virtual memory adress that would be needed for a new bitmap */
221		/* NOTE to app programmers:
222		 * For testing app behaviour regarding workspace switches or screen prefs changes to settings
223		 * that do not have enough cardRAM left for allocation of overlay bitmaps, you need a card with
224		 * a low amount of RAM. Or you can set in the file nv.settings for example:
225		 * memory 8 #8Mb RAM on card
226		 * and reboot (this simulates 8Mb RAM on the card).
227		 *
228		 * If you switch now to settings: 1600x1200x32bit (single head) the app needs to fallback to
229		 * bitmap output or maybe single buffered overlay output if small bitmaps are used. */
230
231		adress = (((uint32)((uint8*)si->framebuffer)) + si->ps.memory_size);
232		/* Keep some extra distance as a workaround for certain bugs (see
233		 * DriverInterface.h for an explanation). */
234		if (si->ps.card_arch < NV40A)
235			adress -= PRE_NV40_OFFSET;
236		else
237			adress -= NV40_PLUS_OFFSET;
238
239		for (cnt = 0; cnt <= offset; cnt++)
240		{
241			adress -= si->overlay.myBufInfo[cnt].size;
242		}
243
244		/* the > G200 scalers require buffers to be aligned to 16 byte pages cardRAM offset, G200 can do with
245		 * 8 byte pages cardRAM offset. Compatible settings used, has no real downside consequences here */
246
247		/* Check if we need to modify the buffers starting adress and thus the size */
248		/* calculate 'would be' cardRAM offset */
249		temp32 = (adress - ((uint32)((vuint32 *)si->framebuffer)));
250		/* check if it is aligned */
251		if (temp32 != (temp32 & 0xfffffff0))
252		{
253			/* update the (already calculated) buffersize to get it aligned */
254			si->overlay.myBufInfo[offset].size += (temp32 - (temp32 & 0xfffffff0));
255			/* update the (already calculated) adress to get it aligned */
256			adress -= (temp32 - (temp32 & 0xfffffff0));
257		}
258		LOG(4,("Overlay: new buffer needs virtual adress $%08x\n", adress));
259
260		/* First check now if buffer to be defined is 'last one' in memory (speaking backwards):
261		 * this is done to prevent a large buffer getting created in the space a small buffer
262		 * occupied earlier, if not all buffers created were deleted.
263		 * Note also that the app can delete the buffers in any order desired. */
264
265		/* NOTE to app programmers:
266		 * If you are going to delete a overlay buffer you created, you should delete them *all* and
267		 * then re-create only the new ones needed. This way you are sure not to get unused memory-
268		 * space in between your overlay buffers for instance, so cardRAM is used 'to the max'.
269		 * If you don't, you might not get a buffer at all if you are trying to set up a larger one
270		 * than before.
271		 * (Indeed: not all buffers *have* to be of the same type and size...) */
272
273		for (cnt = offset; cnt < MAXBUFFERS; cnt++)
274		{
275			if (si->overlay.myBuffer[cnt].buffer != NULL)
276			{
277				/* Check if the new buffer would fit into the space the single old one used here */
278				if (si->overlay.myBufInfo[offset].size <= oldsize)
279				{
280					/* It does, so we reset to the old size and adresses to prevent the space from shrinking
281					 * if we get here again... */
282					adress -= (oldsize - si->overlay.myBufInfo[offset].size);
283					si->overlay.myBufInfo[offset].size = oldsize;
284					LOG(4,("Overlay: 'squeezing' in buffer:\n"
285						   "Overlay: resetting it to virtual adress $%08x and size $%08x\n", adress,oldsize));
286					/* force exiting the FOR loop */
287					cnt = MAXBUFFERS;
288				}
289				else
290				{
291					/* nogo, sorry */
292					LOG(4,("Overlay: Other buffer(s) exist after this one:\n"
293						   "Overlay: not enough space to 'squeeze' this one in, aborted\n"));
294
295					/* Reset to the old size to prevent the space from 'growing' if we get here again... */
296					si->overlay.myBufInfo[offset].size = oldsize;
297
298					/* release the shared benaphore */
299					RELEASE_BEN(si->overlay.lock)
300
301					return NULL;
302				}
303			}
304		}
305
306		/* check if we have enough space to setup this new bitmap
307		 * (preventing overlap of desktop RAMspace & overlay bitmap RAMspace here) */
308		if (adress < adress2)
309		/* nope, sorry */
310		{
311			LOG(4,("Overlay: Sorry, no more space for buffers: aborted\n"));
312
313			/* release the shared benaphore */
314			RELEASE_BEN(si->overlay.lock)
315
316			return NULL;
317		}
318		/* continue buffer setup */
319		si->overlay.myBuffer[offset].buffer = (void *) adress;
320
321		/* calculate physical memory adress (for dma use) */
322		adress = (((uint32)((uint8*)si->framebuffer_pci)) + si->ps.memory_size);
323		/* Keep some extra distance as a workaround for certain bugs (see
324		 * DriverInterface.h for an explanation). */
325		if (si->ps.card_arch < NV40A)
326			adress -= PRE_NV40_OFFSET;
327		else
328			adress -= NV40_PLUS_OFFSET;
329
330		for (cnt = 0; cnt <= offset; cnt++)
331		{
332			adress -= si->overlay.myBufInfo[cnt].size;
333		}
334		/* this adress is already aligned to the scaler's requirements (via the already modified sizes) */
335		si->overlay.myBuffer[offset].buffer_dma = (void *) adress;
336
337		LOG(4,("Overlay: New buffer: addr $%08x, dma_addr $%08x, color space $%08x\n",
338			(uint32)((uint8*)si->overlay.myBuffer[offset].buffer),
339			(uint32)((uint8*)si->overlay.myBuffer[offset].buffer_dma), cs));
340		LOG(4,("Overlay: New buffer's size is $%08x\n", si->overlay.myBufInfo[offset].size));
341
342		/* release the shared benaphore */
343		RELEASE_BEN(si->overlay.lock)
344
345		return &si->overlay.myBuffer[offset];
346	}
347	else
348	/* sorry, no more room for buffers */
349	{
350		LOG(4,("Overlay: Sorry, no more space for buffers: aborted\n"));
351
352		/* release the shared benaphore */
353		RELEASE_BEN(si->overlay.lock)
354
355		return NULL;
356	}
357}
358
359status_t RELEASE_OVERLAY_BUFFER(const overlay_buffer *ob)
360/* Note that the user can delete the buffers in any order desired! */
361{
362	int offset = 0;
363
364	if (ob != NULL)
365	{
366		/* find the buffer */
367		for (offset = 0; offset < MAXBUFFERS; offset++)
368		{
369			if (si->overlay.myBuffer[offset].buffer == ob->buffer) break;
370		}
371
372		if (offset < MAXBUFFERS)
373		/* delete current buffer */
374		{
375			si->overlay.myBuffer[offset].buffer = NULL;
376			si->overlay.myBuffer[offset].buffer_dma = NULL;
377
378			LOG(4,("Overlay: Release_buffer offset = %d, buffer released\n",offset));
379
380			return B_OK;
381		}
382		else
383		{
384			/* this is no buffer of ours! */
385			LOG(4,("Overlay: Release_overlay_buffer: not ours, aborted!\n"));
386
387			return B_ERROR;
388		}
389	}
390	else
391	/* no buffer specified! */
392	{
393		LOG(4,("Overlay: Release_overlay_buffer: no buffer specified, aborted!\n"));
394
395		return B_ERROR;
396	}
397}
398
399status_t GET_OVERLAY_CONSTRAINTS
400	(const display_mode *dm, const overlay_buffer *ob, overlay_constraints *oc)
401{
402	int offset = 0;
403
404	LOG(4,("Overlay: Get_overlay_constraints called\n"));
405
406	/* check for NULL pointers */
407	if ((dm == NULL) || (ob == NULL) || (oc == NULL))
408	{
409		LOG(4,("Overlay: Get_overlay_constraints: Null pointer(s) detected!\n"));
410		return B_ERROR;
411	}
412
413	/* find the buffer */
414	for (offset = 0; offset < MAXBUFFERS; offset++)
415	{
416		if (si->overlay.myBuffer[offset].buffer == ob->buffer) break;
417	}
418
419	if (offset < MAXBUFFERS)
420	{
421		/* scaler input (values are in pixels) */
422		oc->view.h_alignment = 0;
423		oc->view.v_alignment = 0;
424
425		switch (ob->space)
426		{
427			case B_YCbCr422:
428					if (si->ps.card_arch < NV10A)
429					{
430						/* RIVA128 and TNT need 15.
431						 * Note: this has to be in sync with the slopspace setup during buffer allocation.. */
432						oc->view.width_alignment = 15;
433					}
434					else
435					{
436						/* GeForce need 31.
437						 * Note: this has to be in sync with the slopspace setup during buffer allocation.. */
438						oc->view.width_alignment = 31;
439					}
440					break;
441			default:
442					/* we should not be here, but set the worst-case value just to be safe anyway */
443					oc->view.width_alignment = 31;
444					break;
445		}
446
447		oc->view.height_alignment = 0;
448		oc->view.width.min = 1;
449		oc->view.height.min = 2; /* two fields */
450		oc->view.width.max = ob->width;
451		oc->view.height.max = ob->height;
452
453		/* scaler output restrictions */
454		oc->window.h_alignment = 0;
455		oc->window.v_alignment = 0;
456		oc->window.width_alignment = 0;
457		oc->window.height_alignment = 0;
458		oc->window.width.min = 2;
459		/* GeForce cards can output upto and including 2046 pixels in width */
460		//fixme: how about TNT?
461		if (dm->virtual_width > 2046)
462		{
463			oc->window.width.max = 2046;
464		}
465		else
466		{
467			oc->window.width.max = dm->virtual_width;
468		}
469		oc->window.height.min = 2;
470		/* GeForce cards can output upto and including 2046 pixels in height */
471		//fixme: how about TNT?
472		if (dm->virtual_height > 2046)
473		{
474			oc->window.height.max = 2046;
475		}
476		else
477		{
478			oc->window.height.max = dm->virtual_height;
479		}
480
481		/* GeForce scaling restrictions */
482		switch (si->ps.card_arch)
483		{
484		case NV04A:
485			/* Riva128-TNT2 series have an old BES engine... */
486			oc->h_scale.min = 1.0;
487			oc->v_scale.min = 1.0;
488			break;
489		case NV30A:
490		case NV40A:
491			/* GeForceFX series and up have a new BES engine... */
492			oc->h_scale.min = 0.5;
493			oc->v_scale.min = 0.5;
494			/* NV31 (confirmed GeForceFX 5600) has NV20A scaling limits!
495			 * So let it fall through... */
496			if (si->ps.card_type != NV31) break;
497		default:
498			/* the rest in between... */
499			oc->h_scale.min = 0.125;
500			oc->v_scale.min = 0.125;
501			break;
502		}
503		/* all cards have a upscaling limit of 8.0 (see official nVidia specsheets) */
504		oc->h_scale.max = 8.0;
505		oc->v_scale.max = 8.0;
506
507		return B_OK;
508	}
509	else
510	{
511		/* this is no buffer of ours! */
512		LOG(4,("Overlay: Get_overlay_constraints: buffer is not ours, aborted!\n"));
513
514		return B_ERROR;
515	}
516}
517
518overlay_token ALLOCATE_OVERLAY(void)
519{
520	uint32 tmpToken;
521	LOG(4,("Overlay: Allocate_overlay called: "));
522
523	/* come up with a token */
524	tmpToken = 0x12345678;
525
526	/* acquire the shared benaphore */
527	AQUIRE_BEN(si->overlay.lock)
528
529	/* overlay unit already in use? */
530	if (si->overlay.myToken == NULL)
531	/* overlay unit is available */
532	{
533		LOG(4,("succesfull\n"));
534
535		si->overlay.myToken = &tmpToken;
536
537		/* release the shared benaphore */
538		RELEASE_BEN(si->overlay.lock)
539
540		return si->overlay.myToken;
541	}
542	else
543	/* sorry, overlay unit is occupied */
544	{
545		LOG(4,("failed: already in use!\n"));
546
547		/* release the shared benaphore */
548		RELEASE_BEN(si->overlay.lock)
549
550		return NULL;
551	}
552}
553
554status_t RELEASE_OVERLAY(overlay_token ot)
555{
556	LOG(4,("Overlay: Release_overlay called: "));
557
558	/* is this call for real? */
559	if ((ot == NULL) || (si->overlay.myToken == NULL) || (ot != si->overlay.myToken))
560	/* nope, abort */
561	{
562		LOG(4,("failed, not in use!\n"));
563
564		return B_ERROR;
565	}
566	else
567	/* call is for real */
568	{
569
570		nv_release_bes();
571
572		LOG(4,("succesfull\n"));
573
574		si->overlay.myToken = NULL;
575		return B_OK;
576	}
577}
578
579status_t CONFIGURE_OVERLAY
580	(overlay_token ot, const overlay_buffer *ob, const overlay_window *ow, const overlay_view *ov)
581{
582	int offset = 0; /* used for buffer index */
583
584	LOG(4,("Overlay: Configure_overlay called: "));
585
586	/* Note:
587	 * When a Workspace switch, screen prefs change, or overlay app shutdown occurs, BeOS will
588	 * release all overlay buffers. The buffer currently displayed at that moment, may need some
589	 * 'hardware releasing' in the CONFIGURE_OVERLAY routine. This is why CONFIGURE_OVERLAY gets
590	 * called one more time then, with a null pointer for overlay_window and overlay_view, while
591	 * the currently displayed overlay_buffer is given.
592	 * The G200-G550 do not need to do anything on such an occasion, so we simply return if we
593	 * get called then. */
594	if ((ow == NULL) || (ov == NULL))
595	{
596		LOG(4,("output properties changed\n"));
597
598		return B_OK;
599	}
600
601	/* Note:
602	 * If during overlay use the screen prefs are changed, or the workspace has changed, it
603	 * may be that we were not able to re-allocate the requested overlay buffers (or only partly)
604	 * due to lack of cardRAM. If the app does not respond properly to this, we might end up
605	 * with a NULL pointer instead of a overlay_buffer to work with here.
606	 * Of course, we need to abort then to prevent the system from 'going down'.
607	 * The app will probably crash because it will want to write into this non-existant buffer
608	 * at some point. */
609	if (ob == NULL)
610	{
611		LOG(4,("no overlay buffer specified\n"));
612
613		return B_ERROR;
614	}
615
616	/* is this call done by the app that owns us? */
617	if ((ot == NULL) || (si->overlay.myToken == NULL) || (ot != si->overlay.myToken))
618	/* nope, abort */
619	{
620		LOG(4,("failed\n"));
621
622		return B_ERROR;
623	}
624	else
625	/* call is for real */
626	{
627		/* find the buffer's offset */
628		for (offset = 0; offset < MAXBUFFERS; offset++)
629		{
630			if (si->overlay.myBuffer[offset].buffer == ob->buffer) break;
631		}
632
633		if (offset < MAXBUFFERS)
634		{
635			LOG(4,("succesfull, switching to buffer %d\n", offset));
636
637			/* program overlay hardware */
638			nv_configure_bes(ob, ow, ov, offset);
639
640			return B_OK;
641		}
642		else
643		{
644			/* this is no buffer of ours! */
645			LOG(4,("buffer is not ours, aborted!\n"));
646
647			return B_ERROR;
648		}
649	}
650}
651