src/gb/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/gb/renderers/proxy.h>
7
8#include <mgba/core/tile-cache.h>
9#include <mgba/internal/gb/gb.h>
10#include <mgba/internal/gb/io.h>
11
12#define BUFFER_OAM 1
13
14static void GBVideoProxyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model);
15static void GBVideoProxyRendererDeinit(struct GBVideoRenderer* renderer);
16static uint8_t GBVideoProxyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
17static void GBVideoProxyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address);
18static void GBVideoProxyRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam);
19static void GBVideoProxyRendererWritePalette(struct GBVideoRenderer* renderer, int address, uint16_t value);
20static void GBVideoProxyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax);
21static void GBVideoProxyRendererFinishScanline(struct GBVideoRenderer* renderer, int y);
22static void GBVideoProxyRendererFinishFrame(struct GBVideoRenderer* renderer);
23static void GBVideoProxyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels);
24static void GBVideoProxyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels);
25
26static bool _parsePacket(struct mVideoLogger* logger, const struct mVideoLoggerDirtyInfo* packet);
27static uint16_t* _vramBlock(struct mVideoLogger* logger, uint32_t address);
28
29void GBVideoProxyRendererCreate(struct GBVideoProxyRenderer* renderer, struct GBVideoRenderer* backend) {
30 renderer->d.init = GBVideoProxyRendererInit;
31 renderer->d.deinit = GBVideoProxyRendererDeinit;
32 renderer->d.writeVideoRegister = GBVideoProxyRendererWriteVideoRegister;
33 renderer->d.writeVRAM = GBVideoProxyRendererWriteVRAM;
34 renderer->d.writeOAM = GBVideoProxyRendererWriteOAM;
35 renderer->d.writePalette = GBVideoProxyRendererWritePalette;
36 renderer->d.drawRange = GBVideoProxyRendererDrawRange;
37 renderer->d.finishScanline = GBVideoProxyRendererFinishScanline;
38 renderer->d.finishFrame = GBVideoProxyRendererFinishFrame;
39 renderer->d.getPixels = GBVideoProxyRendererGetPixels;
40 renderer->d.putPixels = GBVideoProxyRendererPutPixels;
41
42 renderer->logger->context = renderer;
43 renderer->logger->parsePacket = _parsePacket;
44 renderer->logger->vramBlock = _vramBlock;
45 renderer->logger->paletteSize = 0;
46 renderer->logger->vramSize = GB_SIZE_VRAM;
47 renderer->logger->oamSize = GB_SIZE_OAM;
48
49 renderer->backend = backend;
50}
51
52static void _init(struct GBVideoProxyRenderer* proxyRenderer) {
53 mVideoLoggerRendererInit(proxyRenderer->logger);
54
55 if (proxyRenderer->logger->block) {
56 proxyRenderer->backend->vram = (uint8_t*) proxyRenderer->logger->vram;
57 proxyRenderer->backend->oam = (union GBOAM*) proxyRenderer->logger->oam;
58 proxyRenderer->backend->cache = NULL;
59 }
60}
61
62static void _reset(struct GBVideoProxyRenderer* proxyRenderer, enum GBModel model) {
63 memcpy(proxyRenderer->logger->oam, &proxyRenderer->d.oam->raw, GB_SIZE_OAM);
64 memcpy(proxyRenderer->logger->vram, proxyRenderer->d.vram, GB_SIZE_VRAM);
65
66 proxyRenderer->oamMax = 0;
67
68 proxyRenderer->backend->deinit(proxyRenderer->backend);
69 proxyRenderer->backend->init(proxyRenderer->backend, model);
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, video->p->model);
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) {
100 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
101
102 _init(proxyRenderer);
103
104 proxyRenderer->backend->init(proxyRenderer->backend, model);
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 switch (item->type) {
118 case DIRTY_REGISTER:
119 proxyRenderer->backend->writeVideoRegister(proxyRenderer->backend, item->address, item->value);
120 break;
121 case DIRTY_PALETTE:
122 if (item->address < 64) {
123 proxyRenderer->backend->writePalette(proxyRenderer->backend, item->address, item->value);
124 }
125 break;
126 case DIRTY_OAM:
127 if (item->address < GB_SIZE_OAM) {
128 logger->oam[item->address] = item->value;
129 proxyRenderer->backend->writeOAM(proxyRenderer->backend, item->address);
130 }
131 break;
132 case DIRTY_VRAM:
133 if (item->address + 0x1000 <= GB_SIZE_VRAM) {
134 logger->readData(logger, &logger->vram[item->address >> 1], 0x1000, true);
135 proxyRenderer->backend->writeVRAM(proxyRenderer->backend, item->address);
136 }
137 break;
138 case DIRTY_SCANLINE:
139 if (item->address < GB_VIDEO_VERTICAL_PIXELS) {
140 proxyRenderer->backend->finishScanline(proxyRenderer->backend, item->address);
141 }
142 break;
143 case DIRTY_RANGE:
144 if (item->value < item->value2 && item->value2 <= GB_VIDEO_HORIZONTAL_PIXELS && item->address < GB_VIDEO_VERTICAL_PIXELS) {
145 proxyRenderer->backend->drawRange(proxyRenderer->backend, item->value, item->value2, item->address, proxyRenderer->objThisLine, proxyRenderer->oamMax);
146 }
147 break;
148 case DIRTY_FRAME:
149 proxyRenderer->backend->finishFrame(proxyRenderer->backend);
150 break;
151 case DIRTY_BUFFER:
152 switch (item->address) {
153 case BUFFER_OAM:
154 proxyRenderer->oamMax = item->value2 / sizeof(struct GBObj);
155 if (proxyRenderer->oamMax > 40) {
156 proxyRenderer->oamMax = 0;
157 return false;
158 }
159 logger->readData(logger, &proxyRenderer->objThisLine, item->value2, true);
160 }
161 break;
162 case DIRTY_FLUSH:
163 return false;
164 default:
165 return false;
166 }
167 return true;
168}
169
170static uint16_t* _vramBlock(struct mVideoLogger* logger, uint32_t address) {
171 struct GBVideoProxyRenderer* proxyRenderer = logger->context;
172 return (uint16_t*) &proxyRenderer->d.vram[address];
173}
174
175uint8_t GBVideoProxyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
176 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
177
178 mVideoLoggerRendererWriteVideoRegister(proxyRenderer->logger, address, value);
179 if (!proxyRenderer->logger->block) {
180 proxyRenderer->backend->writeVideoRegister(proxyRenderer->backend, address, value);
181 }
182 return value;
183}
184
185void GBVideoProxyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
186 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
187 mVideoLoggerRendererWriteVRAM(proxyRenderer->logger, address);
188 if (!proxyRenderer->logger->block) {
189 proxyRenderer->backend->writeVRAM(proxyRenderer->backend, address);
190 }
191 if (renderer->cache) {
192 mTileCacheWriteVRAM(renderer->cache, address);
193 }
194}
195
196void GBVideoProxyRendererWritePalette(struct GBVideoRenderer* renderer, int address, uint16_t value) {
197 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
198 mVideoLoggerRendererWritePalette(proxyRenderer->logger, address, value);
199 if (!proxyRenderer->logger->block) {
200 proxyRenderer->backend->writePalette(proxyRenderer->backend, address, value);
201 }
202 if (renderer->cache) {
203 mTileCacheWritePalette(renderer->cache, address);
204 }
205}
206
207void GBVideoProxyRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam) {
208 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
209 if (!proxyRenderer->logger->block) {
210 proxyRenderer->backend->writeOAM(proxyRenderer->backend, oam);
211 }
212 mVideoLoggerRendererWriteOAM(proxyRenderer->logger, oam, ((uint8_t*) proxyRenderer->d.oam->raw)[oam]);
213}
214
215void GBVideoProxyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
216 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
217 if (!proxyRenderer->logger->block) {
218 proxyRenderer->backend->drawRange(proxyRenderer->backend, startX, endX, y, obj, oamMax);
219 }
220 mVideoLoggerWriteBuffer(proxyRenderer->logger, BUFFER_OAM, 0, oamMax * sizeof(*obj), obj);
221 mVideoLoggerRendererDrawRange(proxyRenderer->logger, startX, endX, y);
222}
223
224void GBVideoProxyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
225 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
226 if (!proxyRenderer->logger->block) {
227 proxyRenderer->backend->finishScanline(proxyRenderer->backend, y);
228 }
229 mVideoLoggerRendererDrawScanline(proxyRenderer->logger, y);
230 if (proxyRenderer->logger->block && proxyRenderer->logger->wake) {
231 proxyRenderer->logger->wake(proxyRenderer->logger, y);
232 }
233}
234
235void GBVideoProxyRendererFinishFrame(struct GBVideoRenderer* renderer) {
236 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
237 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
238 proxyRenderer->logger->lock(proxyRenderer->logger);
239 proxyRenderer->logger->wait(proxyRenderer->logger);
240 }
241 proxyRenderer->backend->finishFrame(proxyRenderer->backend);
242 mVideoLoggerRendererFinishFrame(proxyRenderer->logger);
243 mVideoLoggerRendererFlush(proxyRenderer->logger);
244 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
245 proxyRenderer->logger->unlock(proxyRenderer->logger);
246 }
247}
248
249static void GBVideoProxyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
250 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
251 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
252 proxyRenderer->logger->lock(proxyRenderer->logger);
253 // Insert an extra item into the queue to make sure it gets flushed
254 mVideoLoggerRendererFlush(proxyRenderer->logger);
255 proxyRenderer->logger->wait(proxyRenderer->logger);
256 }
257 proxyRenderer->backend->getPixels(proxyRenderer->backend, stride, pixels);
258 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
259 proxyRenderer->logger->unlock(proxyRenderer->logger);
260 }
261}
262
263static void GBVideoProxyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
264 struct GBVideoProxyRenderer* proxyRenderer = (struct GBVideoProxyRenderer*) renderer;
265 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
266 proxyRenderer->logger->lock(proxyRenderer->logger);
267 // Insert an extra item into the queue to make sure it gets flushed
268 mVideoLoggerRendererFlush(proxyRenderer->logger);
269 proxyRenderer->logger->wait(proxyRenderer->logger);
270 }
271 proxyRenderer->backend->putPixels(proxyRenderer->backend, stride, pixels);
272 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
273 proxyRenderer->logger->unlock(proxyRenderer->logger);
274 }
275}