65e3de53e0dfcade76cb557abc8063dfa5e91e2a
[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 "../../frontend/plugin_lib.h"
15
16 static const struct rearmed_cbs *cbs;
17
18 int vout_init(void)
19 {
20   return 0;
21 }
22
23 int vout_finish(void)
24 {
25   return 0;
26 }
27
28 static void check_mode_change(int force)
29 {
30   int w = gpu.screen.hres;
31   int h = gpu.screen.vres;
32   int w_out, h_out, bpp = 16;
33
34   if (gpu.state.screen_centering_type == C_BORDERLESS)
35     h = gpu.screen.h;
36   w_out = w, h_out = h;
37 #ifdef RAW_FB_DISPLAY
38   w = w_out = (gpu.status & PSX_GPU_STATUS_RGB24) ? 2048/3 : 1024;
39   h = h_out = 512;
40 #endif
41   gpu.state.enhancement_active =
42     gpu.get_enhancement_bufer != NULL && gpu.state.enhancement_enable
43     && w <= 512 && h <= 256 && !(gpu.status & PSX_GPU_STATUS_RGB24);
44
45   if (gpu.state.enhancement_active) {
46     w_out *= 2;
47     h_out *= 2;
48   }
49   if (gpu.status & PSX_GPU_STATUS_RGB24) {
50     // some asm relies on this alignment
51     w_out = (w_out + 7) & ~7;
52     bpp = 24;
53   }
54
55   gpu.state.downscale_active =
56     gpu.get_downscale_buffer != NULL && gpu.state.downscale_enable
57     && (w >= 512 || h >= 256);
58
59   if (gpu.state.downscale_active) {
60     w_out = w < 512 ? w : 320;
61     h_out = h < 256 ? h : h / 2;
62   }
63
64   // width|rgb24 change?
65   if (force || (gpu.status ^ gpu.state.status_vo_old) & ((7<<16)|(1<<21))
66       || w_out != gpu.state.w_out_old || h_out != gpu.state.h_out_old)
67   {
68     gpu.state.status_vo_old = gpu.status;
69     gpu.state.w_out_old = w_out;
70     gpu.state.h_out_old = h_out;
71
72     if (w_out != 0 && h_out != 0)
73       cbs->pl_vout_set_mode(w_out, h_out, w, h, bpp);
74   }
75 }
76
77 int vout_update(void)
78 {
79   int bpp = (gpu.status & PSX_GPU_STATUS_RGB24) ? 24 : 16;
80   uint8_t *vram = (uint8_t *)gpu.vram;
81   int src_x = gpu.screen.src_x;
82   int src_y = gpu.screen.src_y;
83   int x = gpu.screen.x;
84   int y = gpu.screen.y;
85   int w = gpu.screen.w;
86   int h = gpu.screen.h;
87   int vram_h = 512;
88   int src_x2 = 0;
89   int offset;
90
91 #ifdef RAW_FB_DISPLAY
92   w = (gpu.status & PSX_GPU_STATUS_RGB24) ? 2048/3 : 1024;
93   h = 512, x = src_x = y = src_y = 0;
94 #endif
95   if (x < 0) { w += x; src_x2 = -x; x = 0; }
96   if (y < 0) { h += y; src_y -=  y; y = 0; }
97
98   if (w <= 0 || h <= 0)
99     return 0;
100
101   check_mode_change(0);
102   if (gpu.state.enhancement_active) {
103     if (!gpu.state.enhancement_was_active)
104       return 0; // buffer not ready yet
105     vram = gpu.get_enhancement_bufer(&src_x, &src_y, &w, &h, &vram_h);
106     if (vram == NULL)
107       return 0;
108     x *= 2; y *= 2;
109     src_x2 *= 2;
110   }
111
112   if (gpu.state.downscale_active)
113     vram = (void *)gpu.get_downscale_buffer(&src_x, &src_y, &w, &h, &vram_h);
114
115   if (src_y + h > vram_h) {
116     if (src_y + h - vram_h > h / 2) {
117       // wrap
118       h -= vram_h - src_y;
119       src_y = 0;
120     }
121     else
122       // clip
123       h = vram_h - src_y;
124   }
125
126   offset = (src_y * 1024 + src_x) * 2;
127   offset += src_x2 * bpp / 8;
128
129   cbs->pl_vout_flip(vram, offset, !!(gpu.status & PSX_GPU_STATUS_RGB24),
130       x, y, w, h, gpu.state.dims_changed);
131   gpu.state.dims_changed = 0;
132   return 1;
133 }
134
135 void vout_blank(void)
136 {
137   int w = gpu.screen.hres;
138   int h = gpu.screen.vres;
139
140   check_mode_change(0);
141   if (gpu.state.enhancement_active) {
142     w *= 2;
143     h *= 2;
144   }
145   cbs->pl_vout_flip(NULL, 0, !!(gpu.status & PSX_GPU_STATUS_RGB24), 0, 0, w, h, 0);
146 }
147
148 long GPUopen(unsigned long *disp, char *cap, char *cfg)
149 {
150   gpu.frameskip.active = 0;
151   gpu.frameskip.frame_ready = 1;
152
153   cbs->pl_vout_open();
154   check_mode_change(1);
155   vout_update();
156   return 0;
157 }
158
159 long GPUclose(void)
160 {
161   cbs->pl_vout_close();
162   return 0;
163 }
164
165 void vout_set_config(const struct rearmed_cbs *cbs_)
166 {
167   cbs = cbs_;
168 }
169
170 // vim:shiftwidth=2:expandtab