+ hud_print(pl_vout_buf, pl_vout_w, border + 2, h - HUD_HEIGHT, hud_msg);
+}
+
+static void print_fps(int h, int border)
+{
+ hud_printf(pl_vout_buf, pl_vout_w, border + 2, h - HUD_HEIGHT,
+ "%2d %4.1f", pl_rearmed_cbs.flips_per_sec,
+ pl_rearmed_cbs.vsps_cur);
+}
+
+static void print_cpu_usage(int x, int h)
+{
+ hud_printf(pl_vout_buf, pl_vout_w, x - 28,
+ h - HUD_HEIGHT, "%3d", pl_rearmed_cbs.cpu_usage);
+}
+
+// draw 192x8 status of 24 sound channels
+static __attribute__((noinline)) void draw_active_chans(int vout_w, int vout_h)
+{
+ extern void spu_get_debug_info(int *chans_out, int *run_chans,
+ int *fmod_chans_out, int *noise_chans_out); // hack
+ int live_chans, run_chans, fmod_chans, noise_chans;
+
+ static const unsigned short colors[2] = { 0x1fe3, 0x0700 };
+ unsigned short *dest = (unsigned short *)pl_vout_buf +
+ vout_w * (vout_h - HUD_HEIGHT) + vout_w / 2 - 192/2;
+ unsigned short *d, p;
+ int c, x, y;
+
+ if (dest == NULL || pl_vout_bpp != 16)
+ return;
+
+ spu_get_debug_info(&live_chans, &run_chans, &fmod_chans, &noise_chans);
+
+ for (c = 0; c < 24; c++) {
+ d = dest + c * 8;
+ p = !(live_chans & (1<<c)) ? (run_chans & (1<<c) ? 0x01c0 : 0) :
+ (fmod_chans & (1<<c)) ? 0xf000 :
+ (noise_chans & (1<<c)) ? 0x001f :
+ colors[c & 1];
+ for (y = 0; y < 8; y++, d += vout_w)
+ for (x = 0; x < 8; x++)
+ d[x] = p;
+ }
+}
+
+static void print_hud(int x, int w, int h)
+{
+ if (h < 192)
+ return;
+
+ if (h > pl_vout_h)
+ h = pl_vout_h;
+
+ if (g_opts & OPT_SHOWSPU)
+ draw_active_chans(w, h);
+
+ if (hud_msg[0] != 0)
+ print_msg(h, x);
+ else if (g_opts & OPT_SHOWFPS)
+ print_fps(h, x);
+
+ if (g_opts & OPT_SHOWCPU)
+ print_cpu_usage(x + w, h);
+}
+
+/* update scaler target size according to user settings */
+static void update_layer_size(int w, int h)
+{
+ float mult;
+ int imult;
+
+ switch (g_scaler) {
+ case SCALE_1_1:
+ g_layer_w = w; g_layer_h = h;
+ break;
+
+ case SCALE_2_2:
+ g_layer_w = w; g_layer_h = h;
+ if (w * 2 <= g_menuscreen_w)
+ g_layer_w = w * 2;
+ if (h * 2 <= g_menuscreen_h)
+ g_layer_h = h * 2;
+ break;
+
+ case SCALE_4_3v2:
+ if (h > g_menuscreen_h || (240 < h && h <= 360))
+ goto fractional_4_3;
+
+ // 4:3 that prefers integer scaling
+ imult = g_menuscreen_h / h;
+ g_layer_w = w * imult;
+ g_layer_h = h * imult;
+ mult = (float)g_layer_w / (float)g_layer_h;
+ if (mult < 1.25f || mult > 1.666f)
+ g_layer_w = 4.0f/3.0f * (float)g_layer_h;
+ printf(" -> %dx%d %.1f\n", g_layer_w, g_layer_h, mult);
+ break;
+
+ fractional_4_3:
+ case SCALE_4_3:
+ mult = 240.0f / (float)h * 4.0f / 3.0f;
+ if (h > 256)
+ mult *= 2.0f;
+ g_layer_w = mult * (float)g_menuscreen_h;
+ g_layer_h = g_menuscreen_h;
+ printf(" -> %dx%d %.1f\n", g_layer_w, g_layer_h, mult);
+ break;
+
+ case SCALE_FULLSCREEN:
+ g_layer_w = g_menuscreen_w;
+ g_layer_h = g_menuscreen_h;
+ break;
+
+ default:
+ break;
+ }
+
+ g_layer_x = g_menuscreen_w / 2 - g_layer_w / 2;
+ g_layer_y = g_menuscreen_h / 2 - g_layer_h / 2;
+ if (g_layer_x < 0) g_layer_x = 0;
+ if (g_layer_y < 0) g_layer_y = 0;
+ if (g_layer_w > g_menuscreen_w) g_layer_w = g_menuscreen_w;
+ if (g_layer_h > g_menuscreen_h) g_layer_h = g_menuscreen_h;
+}
+
+// XXX: this is platform specific really
+static inline int resolution_ok(int w, int h)
+{
+ return w <= 1024 && h <= 512;
+}
+
+static void pl_vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
+{
+ int vout_w, vout_h, vout_bpp;
+ int buf_yoffset = 0;
+
+ // special h handling, Wipeout likes to change it by 1-6
+ static int vsync_cnt_ms_prev;
+ if ((unsigned int)(vsync_cnt - vsync_cnt_ms_prev) < 5*60)
+ h = (h + 7) & ~7;
+ vsync_cnt_ms_prev = vsync_cnt;
+
+ psx_w = raw_w;
+ psx_h = raw_h;
+ psx_bpp = bpp;
+ vout_w = w;
+ vout_h = h;
+ vout_bpp = bpp;
+ if (pl_rearmed_cbs.only_16bpp)
+ vout_bpp = 16;
+
+ assert(vout_h >= 192);
+
+ pl_vout_scale_w = pl_vout_scale_h = 1;
+#ifdef __ARM_NEON__
+ if (soft_filter) {
+ if (resolution_ok(w * 2, h * 2) && bpp == 16) {
+ pl_vout_scale_w = 2;
+ pl_vout_scale_h = 2;
+ }
+ else {
+ // filter unavailable
+ hud_msg[0] = 0;
+ }
+ }
+ else if (scanlines != 0 && scanline_level != 100 && bpp == 16) {
+ if (h <= 256)
+ pl_vout_scale_h = 2;
+ }
+#endif
+ vout_w *= pl_vout_scale_w;
+ vout_h *= pl_vout_scale_h;
+
+ update_layer_size(vout_w, vout_h);
+
+ pl_vout_buf = plat_gvideo_set_mode(&vout_w, &vout_h, &vout_bpp);
+ if (pl_vout_buf == NULL && pl_plat_blit == NULL)
+ fprintf(stderr, "failed to set mode %dx%d@%d\n",
+ vout_w, vout_h, vout_bpp);
+ else {
+ pl_vout_w = vout_w;
+ pl_vout_h = vout_h;
+ pl_vout_bpp = vout_bpp;
+ pl_vout_yoffset = buf_yoffset;
+ }
+ if (pl_vout_buf != NULL)
+ pl_vout_buf = (char *)pl_vout_buf
+ + pl_vout_yoffset * pl_vout_w * pl_vout_bpp / 8;
+
+ menu_notify_mode_change(pl_vout_w, pl_vout_h, pl_vout_bpp);
+}
+
+static void pl_vout_flip(const void *vram, int stride, int bgr24,
+ int x, int y, int w, int h, int dims_changed)
+{
+ static int clear_counter;
+ unsigned char *dest = pl_vout_buf;
+ const unsigned short *src = vram;
+ int dstride = pl_vout_w, h1 = h;
+ int h_full = pl_vout_h - pl_vout_yoffset;
+ int xoffs = 0, doffs;
+
+ pcnt_start(PCNT_BLIT);
+
+ if (vram == NULL) {
+ // blanking
+ if (pl_plat_clear)
+ pl_plat_clear();
+ else
+ memset(pl_vout_buf, 0,
+ dstride * h_full * pl_vout_bpp / 8);
+ goto out_hud;
+ }
+
+ assert(x + w <= pl_vout_w);
+ assert(y + h <= pl_vout_h);
+
+ // offset
+ xoffs = x * pl_vout_scale_w;
+ doffs = xoffs + y * dstride;
+
+ if (dims_changed)
+ clear_counter = 2;
+
+ if (clear_counter > 0) {
+ if (pl_plat_clear)
+ pl_plat_clear();
+ else
+ memset(pl_vout_buf, 0,
+ dstride * h_full * pl_vout_bpp / 8);
+ clear_counter--;
+ }
+
+ if (pl_plat_blit)
+ {
+ pl_plat_blit(doffs, src, w, h, stride, bgr24);
+ goto out_hud;
+ }
+
+ if (dest == NULL)
+ goto out;
+
+ dest += doffs * 2;
+
+ if (bgr24)
+ {
+ if (pl_rearmed_cbs.only_16bpp) {
+ for (; h1-- > 0; dest += dstride * 2, src += stride)
+ {
+ bgr888_to_rgb565(dest, src, w * 3);
+ }
+ }
+ else {
+ dest -= doffs * 2;
+ dest += (doffs / 8) * 24;
+
+ for (; h1-- > 0; dest += dstride * 3, src += stride)
+ {
+ bgr888_to_rgb888(dest, src, w * 3);
+ }
+ }
+ }
+#ifdef __ARM_NEON__
+ else if (soft_filter == SOFT_FILTER_SCALE2X && pl_vout_scale_w == 2)
+ {
+ neon_scale2x_16_16(src, (void *)dest, w,
+ stride * 2, dstride * 2, h);
+ }
+ else if (soft_filter == SOFT_FILTER_EAGLE2X && pl_vout_scale_w == 2)
+ {
+ neon_eagle2x_16_16(src, (void *)dest, w,
+ stride * 2, dstride * 2, h);
+ }
+ else if (scanlines != 0 && scanline_level != 100)
+ {
+ int l = scanline_level * 2048 / 100;
+ int stride_0 = pl_vout_scale_h >= 2 ? 0 : stride;
+
+ h1 *= pl_vout_scale_h;
+ for (; h1 >= 2; h1 -= 2)
+ {
+ bgr555_to_rgb565(dest, src, w * 2);
+ dest += dstride * 2, src += stride_0;
+
+ bgr555_to_rgb565_b(dest, src, w * 2, l);
+ dest += dstride * 2, src += stride;
+ }
+ }
+#endif
+ else
+ {
+ for (; h1-- > 0; dest += dstride * 2, src += stride)
+ {
+ bgr555_to_rgb565(dest, src, w * 2);
+ }
+ }
+
+out_hud:
+ print_hud(xoffs, w * pl_vout_scale_w, (y + h) * pl_vout_scale_h);
+
+out:
+ pcnt_end(PCNT_BLIT);
+
+ // let's flip now
+ pl_vout_buf = plat_gvideo_flip();
+ if (pl_vout_buf != NULL)
+ pl_vout_buf = (char *)pl_vout_buf
+ + pl_vout_yoffset * pl_vout_w * pl_vout_bpp / 8;
+
+ pl_rearmed_cbs.flip_cnt++;
+}
+
+static int pl_vout_open(void)
+{
+ struct timeval now;
+
+ // force mode update on pl_vout_set_mode() call from gpulib/vout_pl
+ pl_vout_buf = NULL;
+
+ plat_gvideo_open(is_pal);
+
+ gettimeofday(&now, 0);
+ vsync_usec_time = now.tv_usec;
+ while (vsync_usec_time >= frame_interval)
+ vsync_usec_time -= frame_interval;
+