From b6eccda3174977ced5e654b2278dd24381c713d8 Mon Sep 17 00:00:00 2001
From: notaz <notasas@gmail.com>
Date: Fri, 17 Sep 2010 23:02:04 +0000
Subject: [PATCH] pandora: handle custom size fb for menu layer

git-svn-id: file:///home/notaz/opt/svn/PicoDrive/platform@897 be3aeb3a-fb24-0410-a615-afba39da0efa
---
 common/menu.c         |  2 +-
 common/menu.h         |  2 +-
 gp2x/port_config.h    |  1 +
 linux/io.c            | 13 +++++---
 linux/port_config.h   |  1 +
 pandora/plat.c        | 75 ++++++++++++++++++++++++++-----------------
 pandora/port_config.h |  5 +--
 psp/port_config.h     |  3 ++
 win32/port_config.h   |  3 ++
 9 files changed, 66 insertions(+), 39 deletions(-)

diff --git a/common/menu.c b/common/menu.c
index 8b69d20..d11343b 100644
--- a/common/menu.c
+++ b/common/menu.c
@@ -31,7 +31,7 @@ void *g_menubg_ptr;
 
 #ifndef UIQ3
 
-#if !SCREEN_SIZE_FIXED
+#if !MSCREEN_SIZE_FIXED
 int g_menuscreen_w = MSCREEN_WIDTH;
 int g_menuscreen_h = MSCREEN_HEIGHT;
 #endif
diff --git a/common/menu.h b/common/menu.h
index 386b37e..7539948 100644
--- a/common/menu.h
+++ b/common/menu.h
@@ -188,7 +188,7 @@ extern me_bind_action emuctrl_actions[];	// platform code
 extern void *g_menubg_src_ptr;
 extern void *g_menubg_ptr;
 extern void *g_menuscreen_ptr;
-#if SCREEN_SIZE_FIXED
+#if MSCREEN_SIZE_FIXED
 #define g_menuscreen_w MSCREEN_WIDTH
 #define g_menuscreen_h MSCREEN_HEIGHT
 #else
diff --git a/gp2x/port_config.h b/gp2x/port_config.h
index 2002302..a4eef7b 100644
--- a/gp2x/port_config.h
+++ b/gp2x/port_config.h
@@ -11,6 +11,7 @@
 #define SCREEN_SIZE_FIXED 1
 #define SCREEN_WIDTH  320
 #define SCREEN_HEIGHT 240
+#define MSCREEN_SIZE_FIXED 1
 #define MSCREEN_WIDTH  SCREEN_WIDTH
 #define MSCREEN_HEIGHT SCREEN_HEIGHT
 
diff --git a/linux/io.c b/linux/io.c
index 3fc5cc2..efd476e 100644
--- a/linux/io.c
+++ b/linux/io.c
@@ -5,6 +5,7 @@
 
 #include "../common/emu.h"
 #include "../common/menu.h"
+#include "../common/plat.h"
 #include "sndout_oss.h"
 #include "version.h"
 
@@ -252,8 +253,8 @@ static void xlib_init(void)
 static void realloc_screen(void)
 {
 	int size = scr_w * scr_h * 2;
-	g_menuscreen_w = scr_w;
-	g_menuscreen_h = scr_h;
+	g_screen_width = g_menuscreen_w = scr_w;
+	g_screen_height = g_menuscreen_h = scr_h;
 	g_screen_ptr = realloc(g_screen_ptr, size);
 	g_menubg_ptr = realloc(g_menubg_ptr, size);
 	memset(g_screen_ptr, 0, size);
@@ -302,6 +303,9 @@ void plat_video_flip(void)
 	if (scr_changed) {
 		realloc_screen();
 		ximage_realloc(xlib_display, DefaultVisual(xlib_display, 0));
+
+		// propagate new ponters to renderers
+		plat_video_toggle_renderer(0, 0);
 	}
 }
 
@@ -322,9 +326,8 @@ void plat_init(void)
 	ret = vout_fbdev_init(&w, &h);
 	if (ret != 0)
 		exit(1);
-	g_menuscreen_w = w;
-	g_menuscreen_h = h;
-	g_screen_width = w;
+	g_screen_width = g_menuscreen_w = w;
+	g_screen_height = g_menuscreen_h = h;
 	g_menubg_ptr = realloc(g_menubg_ptr, w * g_screen_height * 2);
 	PicoDraw2FB = g_menubg_ptr;
 #else
diff --git a/linux/port_config.h b/linux/port_config.h
index d8fa108..71543f3 100644
--- a/linux/port_config.h
+++ b/linux/port_config.h
@@ -12,6 +12,7 @@
 #define SCREEN_SIZE_FIXED 0
 #define SCREEN_WIDTH  320
 #define SCREEN_HEIGHT 240
+#define MSCREEN_SIZE_FIXED 0
 #define MSCREEN_WIDTH  SCREEN_WIDTH
 #define MSCREEN_HEIGHT SCREEN_HEIGHT
 
diff --git a/pandora/plat.c b/pandora/plat.c
index 7ba6c57..a24e3ba 100644
--- a/pandora/plat.c
+++ b/pandora/plat.c
@@ -32,13 +32,13 @@
 static struct vout_fbdev *main_fb, *layer_fb;
 static int g_layer_x, g_layer_y;
 static int g_layer_w = 320, g_layer_h = 240;
-static int g_osd_fps_x, g_osd_y;
+static int g_osd_fps_x, g_osd_y, doing_bg_frame;
 
 static const char pnd_script_base[] = "sudo -n /usr/pandora/scripts";
 static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
-static unsigned char __attribute__((aligned(4))) temp_frame[g_menuscreen_w * g_menuscreen_h * 2];
 static unsigned char __attribute__((aligned(4))) fb_copy[g_screen_width * g_screen_height * 2];
-unsigned char *PicoDraw2FB = temp_frame;
+static void *temp_frame;
+unsigned char *PicoDraw2FB;
 const char *renderer_names[] = { NULL };
 const char *renderer_names32x[] = { NULL };
 
@@ -207,20 +207,34 @@ void plat_update_volume(int has_changed, int is_up)
 	}
 }
 
-static void make_bg(void)
+static void make_bg(int no_scale)
 {
 	unsigned short *s = (void *)fb_copy;
-	unsigned int t, *d = (unsigned int *)g_menubg_src_ptr + 80 / 2;
 	int x, y;
 
-	memset32(g_menubg_src_ptr, 0, 800 * 480 * 2 / 4);
-
-	for (y = 0; y < 240; y++, s += 320, d += 800*2/2) {
-		for (x = 0; x < 320; x++) {
-			t = s[x];
-			t |= t << 16;
-			d[x] = d[x + 800 / 2] = t;
+	memset32(g_menubg_src_ptr, 0, g_menuscreen_w * g_menuscreen_h * 2 / 4);
+
+	if (!no_scale && g_menuscreen_w >= 640 && g_menuscreen_h >= 480) {
+		unsigned int t, *d = g_menubg_src_ptr;
+		d += (g_menuscreen_h / 2 - 480 / 2) * g_menuscreen_w / 2;
+		d += (g_menuscreen_w / 2 - 640 / 2) / 2;
+		for (y = 0; y < 240; y++, s += 320, d += g_menuscreen_w*2/2) {
+			for (x = 0; x < 320; x++) {
+				t = s[x];
+				t |= t << 16;
+				d[x] = d[x + g_menuscreen_w / 2] = t;
+			}
 		}
+		return;
+	}
+
+	if (g_menuscreen_w >= 320 && g_menuscreen_h >= 240) {
+		unsigned short *d = g_menubg_src_ptr;
+		d += (g_menuscreen_h / 2 - 240 / 2) * g_menuscreen_w;
+		d += (g_menuscreen_w / 2 - 320 / 2);
+		for (y = 0; y < 240; y++, s += 320, d += g_menuscreen_w)
+			memcpy(d, s, 320*2);
+		return;
 	}
 }
 
@@ -236,14 +250,16 @@ void pemu_forced_frame(int no_scale, int do_emu)
 
 	PicoDrawSetOutFormat(PDF_RGB555, 1);
 	Pico.m.dirtyPal = 1;
+	doing_bg_frame = 1;
 	if (do_emu)
 		PicoFrame();
 	else
 		PicoFrameDrawOnly();
+	doing_bg_frame = 0;
 
 	// making a copy because enabling the layer clears it's mem
 	memcpy32((void *)fb_copy, g_screen_ptr, sizeof(fb_copy) / 4);
-	make_bg(); // FIXME: honour no_scale
+	make_bg(no_scale);
 
 	PicoOpt = po_old;
 }
@@ -273,32 +289,24 @@ static void updateSound(int len)
 
 void pemu_sound_start(void)
 {
-	int target_fps = Pico.m.pal ? 50 : 60;
-
 	PsndOut = NULL;
 
 	if (currentConfig.EmuOpt & EOPT_EN_SOUND)
 	{
-		int snd_excess_add, frame_samples;
 		int is_stereo = (PicoOpt & POPT_EN_STEREO) ? 1 : 0;
 
 		PsndRerate(Pico.m.frame_count ? 1 : 0);
 
-		frame_samples = PsndLen;
-		snd_excess_add = ((PsndRate - PsndLen * target_fps)<<16) / target_fps;
-		if (snd_excess_add != 0)
-			frame_samples++;
-
 		/*
-		 * for 44k stereo, we do 1470 samples/frame
+		 * for 44k stereo, we do 1470 samples/emu_frame
 		 * OMAP driver does power of 2 buffers, so we need at least 4K buffer.
 		 * The most we can lag is 1K samples, size of OMAP's McBSP FIFO,
 		 * with 2K sample buffer we might sometimes lag more than that,
 		 * thus causing underflows.
 		 */
-		printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
-			PsndRate, PsndLen, snd_excess_add, is_stereo, Pico.m.pal);
-		sndout_oss_start(PsndRate, frame_samples * 2, is_stereo);
+		printf("starting audio: %i len: %i stereo: %i, pal: %i\n",
+			PsndRate, PsndLen, is_stereo, Pico.m.pal);
+		sndout_oss_start(PsndRate, is_stereo, 2);
 		//sndout_oss_setvol(currentConfig.volume, currentConfig.volume);
 		PicoWriteSound = updateSound;
 		plat_update_volume(0, 0);
@@ -410,6 +418,9 @@ void emu_video_mode_change(int start_line, int line_count, int is_32cols)
 {
 	int fb_w = 320, fb_h = 240, fb_left = 0, fb_right = 0, fb_top = 0, fb_bottom = 0;
 
+	if (doing_bg_frame)
+		return;
+
 	PicoDrawSetOutFormat(PDF_RGB555, 1);
 	PicoDrawSetCallbacks(emuscan, NULL);
 
@@ -490,7 +501,7 @@ void pemu_loop_prep(void)
 	}
 
 	// make sure there is no junk left behind the layer
-	memset32(g_menuscreen_ptr, 0, 800 * 480 * 2 / 4);
+	memset32(g_menuscreen_ptr, 0, g_menuscreen_w * g_menuscreen_h * 2 / 4);
 	g_menuscreen_ptr = vout_fbdev_flip(main_fb);
 
 	// emu_video_mode_change will call pnd_setup_layer()
@@ -596,10 +607,8 @@ void plat_init(void)
 		exit(1);
 	}
 
-	if (w != g_menuscreen_w || h != g_menuscreen_h) {
-		fprintf(stderr, "%dx%d not supported on %s\n", w, h, main_fb_name);
-		goto fail0;
-	}
+	g_menuscreen_w = w;
+	g_menuscreen_h = h;
 	g_menuscreen_ptr = vout_fbdev_flip(main_fb);
 
 	w = 320; h = 240;
@@ -615,8 +624,14 @@ void plat_init(void)
 	}
 	g_screen_ptr = vout_fbdev_flip(layer_fb);
 
+	temp_frame = calloc(g_menuscreen_w * g_menuscreen_h * 2, 1);
+	if (temp_frame == NULL) {
+		fprintf(stderr, "OOM\n");
+		goto fail1;
+	}
 	g_menubg_ptr = temp_frame;
 	g_menubg_src_ptr = temp_frame;
+	PicoDraw2FB = temp_frame;
 
 	sndout_oss_init();
 	pnd_menu_init();
diff --git a/pandora/port_config.h b/pandora/port_config.h
index 50bc723..fa485ea 100644
--- a/pandora/port_config.h
+++ b/pandora/port_config.h
@@ -10,8 +10,9 @@
 #define SCREEN_SIZE_FIXED 1
 #define SCREEN_WIDTH  320
 #define SCREEN_HEIGHT 240
-#define MSCREEN_WIDTH  800
-#define MSCREEN_HEIGHT 480
+#define MSCREEN_SIZE_FIXED 0
+#define MSCREEN_WIDTH  SCREEN_WIDTH
+#define MSCREEN_HEIGHT SCREEN_HEIGHT
 
 // draw2.c
 #define START_ROW  0 // which row of tiles to start rendering at?
diff --git a/psp/port_config.h b/psp/port_config.h
index d07c9f4..e60c2ea 100644
--- a/psp/port_config.h
+++ b/psp/port_config.h
@@ -11,6 +11,9 @@
 #define SCREEN_SIZE_FIXED 1
 #define SCREEN_WIDTH  512
 #define SCREEN_HEIGHT 272
+#define MSCREEN_SIZE_FIXED 1
+#define MSCREEN_WIDTH  SCREEN_WIDTH
+#define MSCREEN_HEIGHT SCREEN_HEIGHT
 
 // draw.c
 #define USE_BGR555 1
diff --git a/win32/port_config.h b/win32/port_config.h
index d6a09cc..faae904 100644
--- a/win32/port_config.h
+++ b/win32/port_config.h
@@ -12,6 +12,9 @@
 #define SCREEN_SIZE_FIXED 0
 #define SCREEN_WIDTH  320
 #define SCREEN_HEIGHT 240
+#define MSCREEN_SIZE_FIXED 0
+#define MSCREEN_WIDTH  SCREEN_WIDTH
+#define MSCREEN_HEIGHT SCREEN_HEIGHT
 
 // draw2.c
 #define START_ROW  0 // which row of tiles to start rendering at?
-- 
2.39.5