47c28f3e73fe445ed521c653707203e45702a053
[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   int w = gpu.screen.hres;
35   int h = gpu.screen.h;
36
37   gpu.state.enhancement_active =
38     gpu.enhancement_bufer != NULL && gpu.state.enhancement_enable
39     && w <= 512 && h <= 256 && !gpu.status.rgb24;
40
41   if (gpu.state.enhancement_active) {
42     w *= 2;
43     h *= 2;
44   }
45
46   // width|rgb24 change?
47   if ((gpu.status.reg ^ old_status) & ((7<<16)|(1<<21)) || h != old_h)
48   {
49     old_status = gpu.status.reg;
50     old_h = h;
51
52     screen_buf = cbs->pl_vout_set_mode(w, h,
53       (gpu.status.rgb24 && !cbs->only_16bpp) ? 24 : 16);
54   }
55 }
56
57 static void blit(void)
58 {
59   int x = gpu.screen.x & ~1; // alignment needed by blitter
60   int y = gpu.screen.y;
61   int w = gpu.screen.w;
62   int h = gpu.screen.h;
63   uint16_t *vram = gpu.vram;
64   int stride = gpu.screen.hres;
65   int vram_stride = 1024;
66   int vram_mask = 1024 * 512 - 1;
67   int fb_offs, doffs;
68   uint8_t *dest;
69
70   dest = (uint8_t *)screen_buf;
71   if (dest == NULL)
72     return;
73
74   if (gpu.state.enhancement_active) {
75     vram = gpu.enhancement_bufer;
76     x *= 2;
77     y *= 2;
78     w *= 2;
79     h *= 2;
80     stride *= 2;
81     vram_mask = 1024 * 1024 - 1;
82   }
83   fb_offs = y * vram_stride + x;
84
85   // only do centering, at least for now
86   doffs = (stride - w) / 2 & ~1;
87
88   if (gpu.status.rgb24)
89   {
90     if (cbs->only_16bpp) {
91       dest += doffs * 2;
92       for (; h-- > 0; dest += stride * 2, fb_offs += vram_stride)
93       {
94         fb_offs &= vram_mask;
95         bgr888_to_rgb565(dest, vram + fb_offs, w * 3);
96       }
97     }
98     else {
99       dest += (doffs / 8) * 24;
100       for (; h-- > 0; dest += stride * 3, fb_offs += vram_stride)
101       {
102         fb_offs &= vram_mask;
103         bgr888_to_rgb888(dest, vram + fb_offs, w * 3);
104       }
105     }
106   }
107   else
108   {
109     dest += doffs * 2;
110     for (; h-- > 0; dest += stride * 2, fb_offs += vram_stride)
111     {
112       fb_offs &= vram_mask;
113       bgr555_to_rgb565(dest, vram + fb_offs, w * 2);
114     }
115   }
116
117   screen_buf = cbs->pl_vout_flip();
118 }
119
120 void vout_update(void)
121 {
122   check_mode_change();
123   if (cbs->pl_vout_raw_flip)
124     cbs->pl_vout_raw_flip(gpu.screen.x, gpu.screen.y);
125   else
126     blit();
127 }
128
129 void vout_blank(void)
130 {
131   check_mode_change();
132   if (cbs->pl_vout_raw_flip == NULL) {
133     int bytespp = gpu.status.rgb24 ? 3 : 2;
134     memset(screen_buf, 0, gpu.screen.hres * gpu.screen.h * bytespp);
135     screen_buf = cbs->pl_vout_flip();
136   }
137 }
138
139 long GPUopen(void **unused)
140 {
141   gpu.frameskip.active = 0;
142   gpu.frameskip.frame_ready = 1;
143
144   cbs->pl_vout_open();
145   screen_buf = cbs->pl_vout_flip();
146   return 0;
147 }
148
149 long GPUclose(void)
150 {
151   cbs->pl_vout_close();
152   return 0;
153 }
154
155 void vout_set_config(const struct rearmed_cbs *cbs_)
156 {
157   cbs = cbs_;
158 }
159
160 // vim:shiftwidth=2:expandtab