gpulib: implement rgb888_to_rgb565
[pcsx_rearmed.git] / plugins / gpulib / vout_pl.c
CommitLineData
56f08d83 1/*
1f219c7b 2 * video output handling using plugin_lib
56f08d83 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
aafcb4dd 12#include <string.h>
56f08d83 13#include "gpu.h"
62d7fa95 14#include "cspace.h"
56f08d83 15#include "../../frontend/plugin_lib.h"
56f08d83 16
17static const struct rearmed_cbs *cbs;
18static void *screen_buf;
19
20int vout_init(void)
21{
22 return 0;
23}
24
25int vout_finish(void)
26{
27 return 0;
28}
29
7d993ee2 30static void check_mode_change(void)
56f08d83 31{
6f2ee2be 32 static uint32_t old_status;
33 static int old_h;
7d993ee2 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;
4ea7de6a 40 screen_buf = cbs->pl_vout_set_mode(gpu.screen.hres, gpu.screen.h,
41 (gpu.status.rgb24 && !cbs->only_16bpp) ? 24 : 16);
7d993ee2 42 }
43}
44
45static void blit(void)
46{
8dd855cd 47 int x = gpu.screen.x & ~1; // alignment needed by blitter
56f08d83 48 int y = gpu.screen.y;
49 int w = gpu.screen.w;
8dd855cd 50 int h = gpu.screen.h;
5f26e402 51 uint16_t *vram = gpu.vram;
8dd855cd 52 int stride = gpu.screen.hres;
5f26e402 53 int fb_offs, doffs;
54 uint8_t *dest;
56f08d83 55
6f2ee2be 56 dest = (uint8_t *)screen_buf;
aafcb4dd 57 if (dest == NULL)
58 return;
59
60 fb_offs = y * 1024 + x;
56f08d83 61
8dd855cd 62 // only do centering, at least for now
63 doffs = (stride - w) / 2 & ~1;
64
56f08d83 65 if (gpu.status.rgb24)
66 {
4ea7de6a 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 }
56f08d83 74 }
4ea7de6a 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 }
56f08d83 82 }
56f08d83 83 }
84 else
85 {
8dd855cd 86 dest += doffs * 2;
5f26e402 87 for (; h-- > 0; dest += stride * 2, fb_offs += 1024)
56f08d83 88 {
5f26e402 89 fb_offs &= 1024*512-1;
90 bgr555_to_rgb565(dest, vram + fb_offs, w * 2);
56f08d83 91 }
92 }
93
9394ada5 94 screen_buf = cbs->pl_vout_flip();
56f08d83 95}
96
5440b88e 97void vout_update(void)
56f08d83 98{
7d993ee2 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();
56f08d83 104}
105
aafcb4dd 106void 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
096ec49b 116long GPUopen(void **unused)
56f08d83 117{
19e7cf87 118 gpu.frameskip.active = 0;
119 gpu.frameskip.frame_ready = 1;
fc84f618 120
9394ada5 121 cbs->pl_vout_open();
122 screen_buf = cbs->pl_vout_flip();
56f08d83 123 return 0;
124}
125
126long GPUclose(void)
127{
9394ada5 128 cbs->pl_vout_close();
56f08d83 129 return 0;
130}
131
5440b88e 132void vout_set_config(const struct rearmed_cbs *cbs_)
56f08d83 133{
134 cbs = cbs_;
135}
136
137// vim:shiftwidth=2:expandtab