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 if (item->address < GB_VIDEO_VERTICAL_PIXELS) {
142 proxyRenderer->backend->finishScanline(proxyRenderer->backend, item->address);
143 }
144 break;
145 case DIRTY_RANGE:
146 if (item->value < item->value2 && item->value2 <= GB_VIDEO_HORIZONTAL_PIXELS && item->address < GB_VIDEO_VERTICAL_PIXELS) {
147 proxyRenderer->backend->drawRange(proxyRenderer->backend, item->value, item->value2, item->address);
148 }
149 break;
150 case DIRTY_FRAME:
151 proxyRenderer->backend->finishFrame(proxyRenderer->backend);
152 break;
153 case DIRTY_BUFFER:
154 switch (item->address) {
155 case BUFFER_OAM:
156 if (item->value2 / sizeof(struct GBObj) > 40) {
157 return false;
158 }
159 logger->readData(logger, legacyBuffer, item->value2, true);
160 break;
161 case BUFFER_SGB:
162 logger->readData(logger, sgbPacket, 16, true);
163 proxyRenderer->backend->writeSGBPacket(proxyRenderer->backend, sgbPacket);
164 break;
165 }
166 break;
167 case DIRTY_FLUSH:
168 return false;
169 default:
170 return false;
171 }
172 return true;
173}
174
175static uint16_t* _vramBlock(struct mVideoLogger* logger, uint32_t address) {
176 struct GBVideoProxyRenderer* proxyRenderer = logger->context;
177 return (uint16_t*) &proxyRenderer->d.vram[address];
178}
179
180uint8_t GBVideoProxyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
181 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
182
183 mVideoLoggerRendererWriteVideoRegister(proxyRenderer->logger, address, value);
184 if (!proxyRenderer->logger->block) {
185 proxyRenderer->backend->writeVideoRegister(proxyRenderer->backend, address, value);
186 }
187 return value;
188}
189
190void GBVideoProxyRendererWriteSGBPacket(struct GBVideoRenderer* renderer, uint8_t* data) {
191 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
192 if (!proxyRenderer->logger->block) {
193 proxyRenderer->backend->writeSGBPacket(proxyRenderer->backend, data);
194 }
195 mVideoLoggerWriteBuffer(proxyRenderer->logger, BUFFER_SGB, 0, 16, data);
196}
197
198void GBVideoProxyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
199 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
200 mVideoLoggerRendererWriteVRAM(proxyRenderer->logger, address);
201 if (!proxyRenderer->logger->block) {
202 proxyRenderer->backend->writeVRAM(proxyRenderer->backend, address);
203 }
204 if (renderer->cache) {
205 mCacheSetWriteVRAM(renderer->cache, address);
206 }
207}
208
209void GBVideoProxyRendererWritePalette(struct GBVideoRenderer* renderer, int address, uint16_t value) {
210 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
211 mVideoLoggerRendererWritePalette(proxyRenderer->logger, address, value);
212 if (!proxyRenderer->logger->block) {
213 proxyRenderer->backend->writePalette(proxyRenderer->backend, address, value);
214 }
215 if (renderer->cache) {
216 mCacheSetWritePalette(renderer->cache, address, mColorFrom555(value));
217 }
218}
219
220void GBVideoProxyRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam) {
221 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
222 if (!proxyRenderer->logger->block) {
223 proxyRenderer->backend->writeOAM(proxyRenderer->backend, oam);
224 }
225 mVideoLoggerRendererWriteOAM(proxyRenderer->logger, oam, ((uint8_t*) proxyRenderer->d.oam->raw)[oam]);
226}
227
228void GBVideoProxyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y) {
229 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
230 if (!proxyRenderer->logger->block) {
231 proxyRenderer->backend->drawRange(proxyRenderer->backend, startX, endX, y);
232 }
233 mVideoLoggerRendererDrawRange(proxyRenderer->logger, startX, endX, y);
234}
235
236void GBVideoProxyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
237 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
238 if (!proxyRenderer->logger->block) {
239 proxyRenderer->backend->finishScanline(proxyRenderer->backend, y);
240 }
241 mVideoLoggerRendererDrawScanline(proxyRenderer->logger, y);
242 if (proxyRenderer->logger->block && proxyRenderer->logger->wake) {
243 proxyRenderer->logger->wake(proxyRenderer->logger, y);
244 }
245}
246
247void GBVideoProxyRendererFinishFrame(struct GBVideoRenderer* renderer) {
248 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
249 if (!proxyRenderer->logger->block) {
250 proxyRenderer->backend->finishFrame(proxyRenderer->backend);
251 }
252 mVideoLoggerRendererFinishFrame(proxyRenderer->logger);
253 mVideoLoggerRendererFlush(proxyRenderer->logger);
254}
255
256static void GBVideoProxyRendererEnableSGBBorder(struct GBVideoRenderer* renderer, bool enable) {
257 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
258 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
259 // Insert an extra item into the queue to make sure it gets flushed
260 mVideoLoggerRendererFlush(proxyRenderer->logger);
261 proxyRenderer->logger->wait(proxyRenderer->logger);
262 }
263 proxyRenderer->backend->enableSGBBorder(proxyRenderer->backend, enable);
264}
265
266static void GBVideoProxyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
267 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
268 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
269 proxyRenderer->logger->lock(proxyRenderer->logger);
270 // Insert an extra item into the queue to make sure it gets flushed
271 mVideoLoggerRendererFlush(proxyRenderer->logger);
272 proxyRenderer->logger->postEvent(proxyRenderer->logger, LOGGER_EVENT_GET_PIXELS);
273 mVideoLoggerRendererFlush(proxyRenderer->logger);
274 proxyRenderer->logger->unlock(proxyRenderer->logger);
275 *pixels = proxyRenderer->logger->pixelBuffer;
276 *stride = proxyRenderer->logger->pixelStride;
277 } else {
278 proxyRenderer->backend->getPixels(proxyRenderer->backend, stride, pixels);
279 }
280}
281
282static void GBVideoProxyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
283 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
284 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
285 proxyRenderer->logger->lock(proxyRenderer->logger);
286 }
287 proxyRenderer->backend->putPixels(proxyRenderer->backend, stride, pixels);
288 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
289 proxyRenderer->logger->unlock(proxyRenderer->logger);
290 }
291}