src/gba/renderers/proxy.c (view raw)
1/* Copyright (c) 2013-2017 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include <mgba/internal/gba/renderers/proxy.h>
7
8#include <mgba/core/tile-cache.h>
9#include <mgba/internal/gba/gba.h>
10#include <mgba/internal/gba/io.h>
11
12static void GBAVideoProxyRendererInit(struct GBAVideoRenderer* renderer);
13static void GBAVideoProxyRendererReset(struct GBAVideoRenderer* renderer);
14static void GBAVideoProxyRendererDeinit(struct GBAVideoRenderer* renderer);
15static uint16_t GBAVideoProxyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
16static void GBAVideoProxyRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address);
17static void GBAVideoProxyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
18static void GBAVideoProxyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam);
19static void GBAVideoProxyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y);
20static void GBAVideoProxyRendererFinishFrame(struct GBAVideoRenderer* renderer);
21static void GBAVideoProxyRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels);
22static void GBAVideoProxyRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels);
23
24static bool _parsePacket(struct mVideoLogger* logger, const struct mVideoLoggerDirtyInfo* packet);
25static uint16_t* _vramBlock(struct mVideoLogger* logger, uint32_t address);
26
27void GBAVideoProxyRendererCreate(struct GBAVideoProxyRenderer* renderer, struct GBAVideoRenderer* backend) {
28 renderer->d.init = GBAVideoProxyRendererInit;
29 renderer->d.reset = GBAVideoProxyRendererReset;
30 renderer->d.deinit = GBAVideoProxyRendererDeinit;
31 renderer->d.writeVideoRegister = GBAVideoProxyRendererWriteVideoRegister;
32 renderer->d.writeVRAM = GBAVideoProxyRendererWriteVRAM;
33 renderer->d.writeOAM = GBAVideoProxyRendererWriteOAM;
34 renderer->d.writePalette = GBAVideoProxyRendererWritePalette;
35 renderer->d.drawScanline = GBAVideoProxyRendererDrawScanline;
36 renderer->d.finishFrame = GBAVideoProxyRendererFinishFrame;
37 renderer->d.getPixels = GBAVideoProxyRendererGetPixels;
38 renderer->d.putPixels = GBAVideoProxyRendererPutPixels;
39
40 renderer->d.disableBG[0] = false;
41 renderer->d.disableBG[1] = false;
42 renderer->d.disableBG[2] = false;
43 renderer->d.disableBG[3] = false;
44 renderer->d.disableOBJ = false;
45
46 renderer->logger->context = renderer;
47 renderer->logger->parsePacket = _parsePacket;
48 renderer->logger->vramBlock = _vramBlock;
49 renderer->logger->paletteSize = SIZE_PALETTE_RAM;
50 renderer->logger->vramSize = SIZE_VRAM;
51 renderer->logger->oamSize = SIZE_OAM;
52
53 renderer->backend = backend;
54}
55
56static void _init(struct GBAVideoProxyRenderer* proxyRenderer) {
57 mVideoLoggerRendererInit(proxyRenderer->logger);
58
59 if (proxyRenderer->logger->block) {
60 proxyRenderer->backend->palette = proxyRenderer->logger->palette;
61 proxyRenderer->backend->vram = proxyRenderer->logger->vram;
62 proxyRenderer->backend->oam = (union GBAOAM*) proxyRenderer->logger->oam;
63 proxyRenderer->backend->cache = NULL;
64 }
65}
66
67static void _reset(struct GBAVideoProxyRenderer* proxyRenderer) {
68 memcpy(proxyRenderer->logger->oam, &proxyRenderer->d.oam->raw, SIZE_OAM);
69 memcpy(proxyRenderer->logger->palette, proxyRenderer->d.palette, SIZE_PALETTE_RAM);
70 memcpy(proxyRenderer->logger->vram, proxyRenderer->d.vram, SIZE_VRAM);
71
72 mVideoLoggerRendererReset(proxyRenderer->logger);
73}
74
75void GBAVideoProxyRendererShim(struct GBAVideo* video, struct GBAVideoProxyRenderer* renderer) {
76 if ((renderer->backend && video->renderer != renderer->backend) || video->renderer == &renderer->d) {
77 return;
78 }
79 renderer->backend = video->renderer;
80 video->renderer = &renderer->d;
81 renderer->d.cache = renderer->backend->cache;
82 renderer->d.palette = video->palette;
83 renderer->d.vram = video->vram;
84 renderer->d.oam = &video->oam;
85 _init(renderer);
86 _reset(renderer);
87}
88
89void GBAVideoProxyRendererUnshim(struct GBAVideo* video, struct GBAVideoProxyRenderer* renderer) {
90 if (video->renderer != &renderer->d) {
91 return;
92 }
93 renderer->backend->cache = video->renderer->cache;
94 video->renderer = renderer->backend;
95 renderer->backend->palette = video->palette;
96 renderer->backend->vram = video->vram;
97 renderer->backend->oam = &video->oam;
98
99 mVideoLoggerRendererDeinit(renderer->logger);
100}
101
102void GBAVideoProxyRendererInit(struct GBAVideoRenderer* renderer) {
103 struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
104
105 _init(proxyRenderer);
106
107 proxyRenderer->backend->init(proxyRenderer->backend);
108}
109
110void GBAVideoProxyRendererReset(struct GBAVideoRenderer* renderer) {
111 struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
112
113 _reset(proxyRenderer);
114
115 proxyRenderer->backend->reset(proxyRenderer->backend);
116}
117
118void GBAVideoProxyRendererDeinit(struct GBAVideoRenderer* renderer) {
119 struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
120
121 proxyRenderer->backend->deinit(proxyRenderer->backend);
122
123 mVideoLoggerRendererDeinit(proxyRenderer->logger);
124}
125
126static bool _parsePacket(struct mVideoLogger* logger, const struct mVideoLoggerDirtyInfo* item) {
127 struct GBAVideoProxyRenderer* proxyRenderer = logger->context;
128 switch (item->type) {
129 case DIRTY_REGISTER:
130 proxyRenderer->backend->writeVideoRegister(proxyRenderer->backend, item->address, item->value);
131 break;
132 case DIRTY_PALETTE:
133 logger->palette[item->address >> 1] = item->value;
134 proxyRenderer->backend->writePalette(proxyRenderer->backend, item->address, item->value);
135 break;
136 case DIRTY_OAM:
137 logger->oam[item->address] = item->value;
138 proxyRenderer->backend->writeOAM(proxyRenderer->backend, item->address);
139 break;
140 case DIRTY_VRAM:
141 logger->readData(logger, &logger->vram[item->address >> 1], 0x1000, true);
142 proxyRenderer->backend->writeVRAM(proxyRenderer->backend, item->address);
143 break;
144 case DIRTY_SCANLINE:
145 proxyRenderer->backend->drawScanline(proxyRenderer->backend, item->address);
146 break;
147 case DIRTY_FLUSH:
148 return false;
149 default:
150 return false;
151 }
152 return true;
153}
154
155static uint16_t* _vramBlock(struct mVideoLogger* logger, uint32_t address) {
156 struct GBAVideoProxyRenderer* proxyRenderer = logger->context;
157 return &proxyRenderer->d.vram[address >> 1];
158}
159
160uint16_t GBAVideoProxyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
161 struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
162 switch (address) {
163 case REG_BG0CNT:
164 case REG_BG1CNT:
165 case REG_BG2CNT:
166 case REG_BG3CNT:
167 value &= 0xFFCF;
168 break;
169 case REG_BG0HOFS:
170 case REG_BG0VOFS:
171 case REG_BG1HOFS:
172 case REG_BG1VOFS:
173 case REG_BG2HOFS:
174 case REG_BG2VOFS:
175 case REG_BG3HOFS:
176 case REG_BG3VOFS:
177 value &= 0x01FF;
178 break;
179 }
180 if (address > REG_BLDY) {
181 return value;
182 }
183
184 mVideoLoggerRendererWriteVideoRegister(proxyRenderer->logger, address, value);
185 if (!proxyRenderer->logger->block) {
186 proxyRenderer->backend->writeVideoRegister(proxyRenderer->backend, address, value);
187 }
188 return value;
189}
190
191void GBAVideoProxyRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
192 struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
193 mVideoLoggerRendererWriteVRAM(proxyRenderer->logger, address);
194 if (!proxyRenderer->logger->block) {
195 proxyRenderer->backend->writeVRAM(proxyRenderer->backend, address);
196 }
197 if (renderer->cache) {
198 mTileCacheWriteVRAM(renderer->cache, address);
199 }
200}
201
202void GBAVideoProxyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
203 struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
204 mVideoLoggerRendererWritePalette(proxyRenderer->logger, address, value);
205 if (!proxyRenderer->logger->block) {
206 proxyRenderer->backend->writePalette(proxyRenderer->backend, address, value);
207 }
208 if (renderer->cache) {
209 mTileCacheWritePalette(renderer->cache, address);
210 }
211}
212
213void GBAVideoProxyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
214 struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
215 if (!proxyRenderer->logger->block) {
216 proxyRenderer->backend->writeOAM(proxyRenderer->backend, oam);
217 }
218 mVideoLoggerRendererWriteOAM(proxyRenderer->logger, oam, proxyRenderer->d.oam->raw[oam]);
219}
220
221void GBAVideoProxyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
222 struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
223 if (!proxyRenderer->logger->block) {
224 proxyRenderer->backend->drawScanline(proxyRenderer->backend, y);
225 }
226 mVideoLoggerRendererDrawScanline(proxyRenderer->logger, y);
227 if (proxyRenderer->logger->block && proxyRenderer->logger->wake) {
228 proxyRenderer->logger->wake(proxyRenderer->logger, y);
229 }
230}
231
232void GBAVideoProxyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
233 struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
234 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
235 proxyRenderer->logger->lock(proxyRenderer->logger);
236 proxyRenderer->logger->wait(proxyRenderer->logger);
237 }
238 mVideoLoggerRendererFlush(proxyRenderer->logger);
239 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
240 proxyRenderer->logger->unlock(proxyRenderer->logger);
241 }
242}
243
244static void GBAVideoProxyRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
245 struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
246 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
247 proxyRenderer->logger->lock(proxyRenderer->logger);
248 // Insert an extra item into the queue to make sure it gets flushed
249 mVideoLoggerRendererFlush(proxyRenderer->logger);
250 proxyRenderer->logger->wait(proxyRenderer->logger);
251 }
252 proxyRenderer->backend->getPixels(proxyRenderer->backend, stride, pixels);
253 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
254 proxyRenderer->logger->unlock(proxyRenderer->logger);
255 }
256}
257
258static void GBAVideoProxyRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
259 struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
260 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
261 proxyRenderer->logger->lock(proxyRenderer->logger);
262 // Insert an extra item into the queue to make sure it gets flushed
263 mVideoLoggerRendererFlush(proxyRenderer->logger);
264 proxyRenderer->logger->wait(proxyRenderer->logger);
265 }
266 proxyRenderer->backend->putPixels(proxyRenderer->backend, stride, pixels);
267 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
268 proxyRenderer->logger->unlock(proxyRenderer->logger);
269 }
270}