src/gb/extra/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/gb/renderers/proxy.h>
7
8#include <mgba/core/cache-set.h>
9#include <mgba/internal/gb/gb.h>
10#include <mgba/internal/gb/io.h>
11
12#define BUFFER_OAM 1
13#define BUFFER_SGB 2
14
15static void GBVideoProxyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model, bool borders);
16static void GBVideoProxyRendererDeinit(struct GBVideoRenderer* renderer);
17static uint8_t GBVideoProxyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
18static void GBVideoProxyRendererWriteSGBPacket(struct GBVideoRenderer* renderer, uint8_t* data);
19static void GBVideoProxyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address);
20static void GBVideoProxyRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam);
21static void GBVideoProxyRendererWritePalette(struct GBVideoRenderer* renderer, int address, uint16_t value);
22static void GBVideoProxyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y);
23static void GBVideoProxyRendererFinishScanline(struct GBVideoRenderer* renderer, int y);
24static void GBVideoProxyRendererFinishFrame(struct GBVideoRenderer* renderer);
25static void GBVideoProxyRendererEnableSGBBorder(struct GBVideoRenderer* renderer, bool enable);
26static void GBVideoProxyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels);
27static void GBVideoProxyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels);
28
29static bool _parsePacket(struct mVideoLogger* logger, const struct mVideoLoggerDirtyInfo* packet);
30static uint16_t* _vramBlock(struct mVideoLogger* logger, uint32_t address);
31
32void GBVideoProxyRendererCreate(struct GBVideoProxyRenderer* renderer, struct GBVideoRenderer* backend) {
33 renderer->d.init = GBVideoProxyRendererInit;
34 renderer->d.deinit = GBVideoProxyRendererDeinit;
35 renderer->d.writeVideoRegister = GBVideoProxyRendererWriteVideoRegister;
36 renderer->d.writeSGBPacket = GBVideoProxyRendererWriteSGBPacket;
37 renderer->d.writeVRAM = GBVideoProxyRendererWriteVRAM;
38 renderer->d.writeOAM = GBVideoProxyRendererWriteOAM;
39 renderer->d.writePalette = GBVideoProxyRendererWritePalette;
40 renderer->d.drawRange = GBVideoProxyRendererDrawRange;
41 renderer->d.finishScanline = GBVideoProxyRendererFinishScanline;
42 renderer->d.finishFrame = GBVideoProxyRendererFinishFrame;
43 renderer->d.enableSGBBorder = GBVideoProxyRendererEnableSGBBorder;
44 renderer->d.getPixels = GBVideoProxyRendererGetPixels;
45 renderer->d.putPixels = GBVideoProxyRendererPutPixels;
46
47 renderer->logger->context = renderer;
48 renderer->logger->parsePacket = _parsePacket;
49 renderer->logger->vramBlock = _vramBlock;
50 renderer->logger->paletteSize = 0;
51 renderer->logger->vramSize = GB_SIZE_VRAM;
52 renderer->logger->oamSize = GB_SIZE_OAM;
53
54 renderer->backend = backend;
55}
56
57static void _init(struct GBVideoProxyRenderer* proxyRenderer) {
58 mVideoLoggerRendererInit(proxyRenderer->logger);
59
60 if (proxyRenderer->logger->block) {
61 proxyRenderer->backend->vram = (uint8_t*) proxyRenderer->logger->vram;
62 proxyRenderer->backend->oam = (union GBOAM*) proxyRenderer->logger->oam;
63 proxyRenderer->backend->cache = NULL;
64 }
65}
66
67static void _reset(struct GBVideoProxyRenderer* proxyRenderer) {
68 memcpy(proxyRenderer->logger->oam, &proxyRenderer->d.oam->raw, GB_SIZE_OAM);
69 memcpy(proxyRenderer->logger->vram, proxyRenderer->d.vram, GB_SIZE_VRAM);
70
71 mVideoLoggerRendererReset(proxyRenderer->logger);
72}
73
74void GBVideoProxyRendererShim(struct GBVideo* video, struct GBVideoProxyRenderer* renderer) {
75 if ((renderer->backend && video->renderer != renderer->backend) || video->renderer == &renderer->d) {
76 return;
77 }
78 renderer->backend = video->renderer;
79 video->renderer = &renderer->d;
80 renderer->d.cache = renderer->backend->cache;
81 renderer->d.vram = video->vram;
82 renderer->d.oam = &video->oam;
83 _init(renderer);
84 _reset(renderer);
85}
86
87void GBVideoProxyRendererUnshim(struct GBVideo* video, struct GBVideoProxyRenderer* renderer) {
88 if (video->renderer != &renderer->d) {
89 return;
90 }
91 renderer->backend->cache = video->renderer->cache;
92 video->renderer = renderer->backend;
93 renderer->backend->vram = video->vram;
94 renderer->backend->oam = &video->oam;
95
96 mVideoLoggerRendererDeinit(renderer->logger);
97}
98
99void GBVideoProxyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model, bool borders) {
100 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
101
102 _init(proxyRenderer);
103
104 proxyRenderer->backend->init(proxyRenderer->backend, model, borders);
105}
106
107void GBVideoProxyRendererDeinit(struct GBVideoRenderer* renderer) {
108 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
109
110 proxyRenderer->backend->deinit(proxyRenderer->backend);
111
112 mVideoLoggerRendererDeinit(proxyRenderer->logger);
113}
114
115static bool _parsePacket(struct mVideoLogger* logger, const struct mVideoLoggerDirtyInfo* item) {
116 struct GBVideoProxyRenderer* proxyRenderer = logger->context;
117 uint8_t sgbPacket[16];
118 struct GBObj legacyBuffer[40];
119 switch (item->type) {
120 case DIRTY_REGISTER:
121 proxyRenderer->backend->writeVideoRegister(proxyRenderer->backend, item->address, item->value);
122 break;
123 case DIRTY_PALETTE:
124 if (item->address < 64) {
125 proxyRenderer->backend->writePalette(proxyRenderer->backend, item->address, item->value);
126 }
127 break;
128 case DIRTY_OAM:
129 if (item->address < GB_SIZE_OAM) {
130 ((uint8_t*) logger->oam)[item->address] = item->value;
131 proxyRenderer->backend->writeOAM(proxyRenderer->backend, item->address);
132 }
133 break;
134 case DIRTY_VRAM:
135 if (item->address <= GB_SIZE_VRAM - 0x1000) {
136 logger->readData(logger, &logger->vram[item->address >> 1], 0x1000, true);
137 proxyRenderer->backend->writeVRAM(proxyRenderer->backend, item->address);
138 }
139 break;
140 case DIRTY_SCANLINE:
141 proxyRenderer->backend->disableBG = proxyRenderer->d.disableBG;
142 proxyRenderer->backend->disableWIN = proxyRenderer->d.disableWIN;
143 proxyRenderer->backend->disableOBJ = proxyRenderer->d.disableOBJ;
144 if (item->address < GB_VIDEO_VERTICAL_PIXELS) {
145 proxyRenderer->backend->finishScanline(proxyRenderer->backend, item->address);
146 }
147 break;
148 case DIRTY_RANGE:
149 if (item->value < item->value2 && item->value2 <= GB_VIDEO_HORIZONTAL_PIXELS && item->address < GB_VIDEO_VERTICAL_PIXELS) {
150 proxyRenderer->backend->drawRange(proxyRenderer->backend, item->value, item->value2, item->address);
151 }
152 break;
153 case DIRTY_FRAME:
154 proxyRenderer->backend->finishFrame(proxyRenderer->backend);
155 break;
156 case DIRTY_BUFFER:
157 switch (item->address) {
158 case BUFFER_OAM:
159 if (item->value2 / sizeof(struct GBObj) > 40) {
160 return false;
161 }
162 logger->readData(logger, legacyBuffer, item->value2, true);
163 break;
164 case BUFFER_SGB:
165 logger->readData(logger, sgbPacket, 16, true);
166 proxyRenderer->backend->writeSGBPacket(proxyRenderer->backend, sgbPacket);
167 break;
168 }
169 break;
170 case DIRTY_FLUSH:
171 return false;
172 default:
173 return false;
174 }
175 return true;
176}
177
178static uint16_t* _vramBlock(struct mVideoLogger* logger, uint32_t address) {
179 struct GBVideoProxyRenderer* proxyRenderer = logger->context;
180 return (uint16_t*) &proxyRenderer->d.vram[address];
181}
182
183uint8_t GBVideoProxyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
184 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
185
186 mVideoLoggerRendererWriteVideoRegister(proxyRenderer->logger, address, value);
187 if (!proxyRenderer->logger->block) {
188 proxyRenderer->backend->writeVideoRegister(proxyRenderer->backend, address, value);
189 }
190 return value;
191}
192
193void GBVideoProxyRendererWriteSGBPacket(struct GBVideoRenderer* renderer, uint8_t* data) {
194 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
195 if (!proxyRenderer->logger->block) {
196 proxyRenderer->backend->writeSGBPacket(proxyRenderer->backend, data);
197 }
198 mVideoLoggerWriteBuffer(proxyRenderer->logger, BUFFER_SGB, 0, 16, data);
199}
200
201void GBVideoProxyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
202 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
203 mVideoLoggerRendererWriteVRAM(proxyRenderer->logger, address);
204 if (!proxyRenderer->logger->block) {
205 proxyRenderer->backend->writeVRAM(proxyRenderer->backend, address);
206 }
207 if (renderer->cache) {
208 mCacheSetWriteVRAM(renderer->cache, address);
209 }
210}
211
212void GBVideoProxyRendererWritePalette(struct GBVideoRenderer* renderer, int address, uint16_t value) {
213 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
214 mVideoLoggerRendererWritePalette(proxyRenderer->logger, address, value);
215 if (!proxyRenderer->logger->block) {
216 proxyRenderer->backend->writePalette(proxyRenderer->backend, address, value);
217 }
218 if (renderer->cache) {
219 mCacheSetWritePalette(renderer->cache, address, mColorFrom555(value));
220 }
221}
222
223void GBVideoProxyRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam) {
224 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
225 if (!proxyRenderer->logger->block) {
226 proxyRenderer->backend->writeOAM(proxyRenderer->backend, oam);
227 }
228 mVideoLoggerRendererWriteOAM(proxyRenderer->logger, oam, ((uint8_t*) proxyRenderer->d.oam->raw)[oam]);
229}
230
231void GBVideoProxyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y) {
232 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
233 if (!proxyRenderer->logger->block) {
234 proxyRenderer->backend->drawRange(proxyRenderer->backend, startX, endX, y);
235 }
236 mVideoLoggerRendererDrawRange(proxyRenderer->logger, startX, endX, y);
237}
238
239void GBVideoProxyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
240 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
241 if (!proxyRenderer->logger->block) {
242 proxyRenderer->backend->finishScanline(proxyRenderer->backend, y);
243 }
244 mVideoLoggerRendererDrawScanline(proxyRenderer->logger, y);
245 if (proxyRenderer->logger->block && proxyRenderer->logger->wake) {
246 proxyRenderer->logger->wake(proxyRenderer->logger, y);
247 }
248}
249
250void GBVideoProxyRendererFinishFrame(struct GBVideoRenderer* renderer) {
251 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
252 if (!proxyRenderer->logger->block) {
253 proxyRenderer->backend->finishFrame(proxyRenderer->backend);
254 }
255 mVideoLoggerRendererFinishFrame(proxyRenderer->logger);
256 mVideoLoggerRendererFlush(proxyRenderer->logger);
257}
258
259static void GBVideoProxyRendererEnableSGBBorder(struct GBVideoRenderer* renderer, bool enable) {
260 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
261 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
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->enableSGBBorder(proxyRenderer->backend, enable);
267}
268
269static void GBVideoProxyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
270 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
271 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
272 proxyRenderer->logger->lock(proxyRenderer->logger);
273 // Insert an extra item into the queue to make sure it gets flushed
274 mVideoLoggerRendererFlush(proxyRenderer->logger);
275 proxyRenderer->logger->postEvent(proxyRenderer->logger, LOGGER_EVENT_GET_PIXELS);
276 mVideoLoggerRendererFlush(proxyRenderer->logger);
277 proxyRenderer->logger->unlock(proxyRenderer->logger);
278 *pixels = proxyRenderer->logger->pixelBuffer;
279 *stride = proxyRenderer->logger->pixelStride;
280 } else {
281 proxyRenderer->backend->getPixels(proxyRenderer->backend, stride, pixels);
282 }
283}
284
285static void GBVideoProxyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
286 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
287 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
288 proxyRenderer->logger->lock(proxyRenderer->logger);
289 }
290 proxyRenderer->backend->putPixels(proxyRenderer->backend, stride, pixels);
291 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
292 proxyRenderer->logger->unlock(proxyRenderer->logger);
293 }
294}