0bd1ecf6207af3f07205044a2f7d3e3c60befcb1
[pcsx_rearmed.git] / plugins / gpulib / vout_pl.c
1 /*
2  * video output handling using plugin_lib
3  * (C) GraÅžvydas "notaz" Ignotas, 2011
4  *
5  * This work is licensed under the terms of any of these licenses
6  * (at your option):
7  *  - GNU GPL, version 2 or later.
8  *  - GNU LGPL, version 2.1 or later.
9  * See the COPYING file in the top-level directory.
10  */
11
12 #include <string.h>
13 #include "gpu.h"
14 #include "cspace.h"
15 #include "../../frontend/plugin_lib.h"
16
17 static const struct rearmed_cbs *cbs;
18 static void *screen_buf;
19
20 int vout_init(void)
21 {
22   return 0;
23 }
24
25 int vout_finish(void)
26 {
27   return 0;
28 }
29
30 static void check_mode_change(void)
31 {
32   static uint32_t old_status;
33   static int old_h;
34
35   // width|rgb24 change?
36   if ((gpu.status.reg ^ old_status) & ((7<<16)|(1<<21)) || gpu.screen.h != old_h)
37   {
38     old_status = gpu.status.reg;
39     old_h = gpu.screen.h;
40     screen_buf = cbs->pl_vout_set_mode(gpu.screen.hres, gpu.screen.h,
41       (gpu.status.rgb24 && !cbs->only_16bpp) ? 24 : 16);
42   }
43 }
44
45 static void blit(void)
46 {
47   int x = gpu.screen.x & ~1; // alignment needed by blitter
48   int y = gpu.screen.y;
49   int w = gpu.screen.w;
50   int h = gpu.screen.h;
51   uint16_t *vram = gpu.vram;
52   int stride = gpu.screen.hres;
53   int fb_offs, doffs;
54   uint8_t *dest;
55
56   dest = (uint8_t *)screen_buf;
57   if (dest == NULL)
58     return;
59
60   fb_offs = y * 1024 + x;
61
62   // only do centering, at least for now
63   doffs = (stride - w) / 2 & ~1;
64
65   if (gpu.status.rgb24)
66   {
67     if (cbs->only_16bpp) {
68       dest += doffs * 2;
69       for (; h-- > 0; dest += stride * 2, fb_offs += 1024)
70       {
71         fb_offs &= 1024*512-1;
72         bgr888_to_rgb565(dest, vram + fb_offs, w * 3);
73       }
74     }
75     else {
76       dest += (doffs / 8) * 24;
77       for (; h-- > 0; dest += stride * 3, fb_offs += 1024)
78       {
79         fb_offs &= 1024*512-1;
80         bgr888_to_rgb888(dest, vram + fb_offs, w * 3);
81       }
82     }
83   }
84   else
85   {
86     dest += doffs * 2;
87     for (; h-- > 0; dest += stride * 2, fb_offs += 1024)
88     {
89       fb_offs &= 1024*512-1;
90       bgr555_to_rgb565(dest, vram + fb_offs, w * 2);
91     }
92   }
93
94   screen_buf = cbs->pl_vout_flip();
95 }
96
97 void vout_update(void)
98 {
99   check_mode_change();
100   if (cbs->pl_vout_raw_flip)
101     cbs->pl_vout_raw_flip(gpu.screen.x, gpu.screen.y);
102   else
103     blit();
104 }
105
106 void vout_blank(void)
107 {
108   check_mode_change();
109   if (cbs->pl_vout_raw_flip == NULL) {
110     int bytespp = gpu.status.rgb24 ? 3 : 2;
111     memset(screen_buf, 0, gpu.screen.hres * gpu.screen.h * bytespp);
112     screen_buf = cbs->pl_vout_flip();
113   }
114 }
115
116 long GPUopen(void **unused)
117 {
118   gpu.frameskip.active = 0;
119   gpu.frameskip.frame_ready = 1;
120
121   cbs->pl_vout_open();
122   screen_buf = cbs->pl_vout_flip();
123   return 0;
124 }
125
126 long GPUclose(void)
127 {
128   cbs->pl_vout_close();
129   return 0;
130 }
131
132 void vout_set_config(const struct rearmed_cbs *cbs_)
133 {
134   cbs = cbs_;
135 }
136
137 // vim:shiftwidth=2:expandtab