From d6ad731ebe65858bca9e50478a4caca459aecb85 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Wed, 17 Nov 2021 17:52:25 -0800 Subject: [PATCH] Qt: Pay down technical debt with map caches --- include/mgba/core/map-cache.h | 2 ++ src/core/map-cache.c | 6 +++--- src/platform/qt/AssetView.cpp | 8 ++++++-- src/platform/qt/AssetView.h | 2 +- src/platform/qt/FrameView.cpp | 2 +- src/platform/qt/FrameView.h | 2 +- src/platform/qt/MapView.cpp | 10 ++++++++-- src/platform/qt/MapView.h | 4 ++-- 8 files changed, 24 insertions(+), 12 deletions(-) diff --git a/include/mgba/core/map-cache.h b/include/mgba/core/map-cache.h index 6dc9e72f6..2bd5934e8 100644 --- a/include/mgba/core/map-cache.h +++ b/include/mgba/core/map-cache.h @@ -31,6 +31,8 @@ DECL_BIT(mMapCacheEntryFlags, HMirror, 5); DECL_BIT(mMapCacheEntryFlags, VMirror, 6); DECL_BITS(mMapCacheEntryFlags, Mirror, 5, 2); +#define mMapCacheTileCount(C) (1 << mMapCacheSystemInfoGetTilesWide((C)->sysConfig)) * (1 << mMapCacheSystemInfoGetTilesHigh((C)->sysConfig)) + struct mMapCacheEntry { uint32_t vramVersion; uint16_t tileId; diff --git a/src/core/map-cache.c b/src/core/map-cache.c index 470f72b89..17df206dd 100644 --- a/src/core/map-cache.c +++ b/src/core/map-cache.c @@ -32,7 +32,7 @@ static void _redoCacheSize(struct mMapCache* cache) { return; } - size_t tiles = (1 << mMapCacheSystemInfoGetTilesWide(cache->sysConfig)) * (1 << mMapCacheSystemInfoGetTilesHigh(cache->sysConfig)); + size_t tiles = mMapCacheTileCount(cache); cache->cache = anonymousMemoryMap(8 * 8 * sizeof(color_t) * tiles); cache->status = anonymousMemoryMap(tiles * sizeof(*cache->status)); } @@ -54,12 +54,12 @@ void mMapCacheConfigureSystem(struct mMapCache* cache, mMapCacheSystemInfo confi cache->sysConfig = config; _redoCacheSize(cache); - size_t mapSize = (1 << mMapCacheSystemInfoGetTilesWide(cache->sysConfig)) * (1 << mMapCacheSystemInfoGetTilesHigh(cache->sysConfig)); + size_t mapSize = mMapCacheTileCount(cache); cache->mapSize = mapSize << mMapCacheSystemInfoGetMapAlign(cache->sysConfig); } void mMapCacheConfigureMap(struct mMapCache* cache, uint32_t mapStart) { - size_t tiles = (1 << mMapCacheSystemInfoGetTilesWide(cache->sysConfig)) * (1 << mMapCacheSystemInfoGetTilesHigh(cache->sysConfig)); + size_t tiles = mMapCacheTileCount(cache); memset(cache->status, 0, tiles * sizeof(*cache->status)); cache->mapStart = mapStart; } diff --git a/src/platform/qt/AssetView.cpp b/src/platform/qt/AssetView.cpp index 66574b0df..034b0b420 100644 --- a/src/platform/qt/AssetView.cpp +++ b/src/platform/qt/AssetView.cpp @@ -112,15 +112,19 @@ void AssetView::compositeTile(const void* tBuffer, void* buffer, size_t stride, } } -QImage AssetView::compositeMap(int map, mMapCacheEntry* mapStatus) { +QImage AssetView::compositeMap(int map, QVector* mapStatus) { mMapCache* mapCache = mMapCacheSetGetPointer(&m_cacheSet->maps, map); int tilesW = 1 << mMapCacheSystemInfoGetTilesWide(mapCache->sysConfig); int tilesH = 1 << mMapCacheSystemInfoGetTilesHigh(mapCache->sysConfig); + if (mapStatus->size() != tilesW * tilesH) { + mapStatus->resize(tilesW * tilesH); + mapStatus->fill({}); + } QImage rawMap = QImage(QSize(tilesW * 8, tilesH * 8), QImage::Format_ARGB32); uchar* bgBits = rawMap.bits(); for (int j = 0; j < tilesH; ++j) { for (int i = 0; i < tilesW; ++i) { - mMapCacheCleanTile(mapCache, mapStatus, i, j); + mMapCacheCleanTile(mapCache, mapStatus->data(), i, j); } for (int i = 0; i < 8; ++i) { memcpy(static_cast(&bgBits[tilesW * 32 * (i + j * 8)]), mMapCacheGetRow(mapCache, i + j * 8), tilesW * 32); diff --git a/src/platform/qt/AssetView.h b/src/platform/qt/AssetView.h index c3d3a92b2..2614a598a 100644 --- a/src/platform/qt/AssetView.h +++ b/src/platform/qt/AssetView.h @@ -65,7 +65,7 @@ protected: }; static void compositeTile(const void* tile, void* image, size_t stride, size_t x, size_t y, int depth = 8); - QImage compositeMap(int map, mMapCacheEntry*); + QImage compositeMap(int map, QVector*); QImage compositeObj(const ObjInfo&); bool lookupObj(int id, struct ObjInfo*); diff --git a/src/platform/qt/FrameView.cpp b/src/platform/qt/FrameView.cpp index 530e8a2e5..d09e477ba 100644 --- a/src/platform/qt/FrameView.cpp +++ b/src/platform/qt/FrameView.cpp @@ -245,7 +245,7 @@ void FrameView::updateTilesGBA(bool) { m_queue.append({ { LayerId::BACKGROUND, bg }, !m_disabled.contains({ LayerId::BACKGROUND, bg }), - QPixmap::fromImage(compositeMap(bg, m_mapStatus[bg])), + QPixmap::fromImage(compositeMap(bg, &m_mapStatus[bg])), {}, offset, true, false }); if (m_queue.back().image.hasAlpha()) { diff --git a/src/platform/qt/FrameView.h b/src/platform/qt/FrameView.h index c3054b2a7..02844d7ed 100644 --- a/src/platform/qt/FrameView.h +++ b/src/platform/qt/FrameView.h @@ -108,7 +108,7 @@ private: QSet m_disabled; QPixmap m_composited; QPixmap m_rendered; - mMapCacheEntry m_mapStatus[4][128 * 128] = {}; // TODO: Correct size + QVector m_mapStatus[4]; ColorPicker m_backdropPicker; QColor m_overrideBackdrop; diff --git a/src/platform/qt/MapView.cpp b/src/platform/qt/MapView.cpp index be709a8f9..61347d501 100644 --- a/src/platform/qt/MapView.cpp +++ b/src/platform/qt/MapView.cpp @@ -116,12 +116,18 @@ void MapView::selectMap(int map) { return; } m_map = map; + m_mapStatus.fill({}); updateTiles(true); } void MapView::selectTile(int x, int y) { CoreController::Interrupter interrupter(m_controller); mMapCache* mapCache = mMapCacheSetGetPointer(&m_cacheSet->maps, m_map); + int tiles = mMapCacheTileCount(mapCache); + if (m_mapStatus.size() != tiles) { + m_mapStatus.resize(tiles); + m_mapStatus.fill({}); + } size_t tileCache = mTileCacheSetIndex(&m_cacheSet->tiles, mapCache->tileCache); m_ui.tile->setBoundary(m_boundary, tileCache, tileCache); uint32_t location = mMapCacheTileId(mapCache, x, y); @@ -221,7 +227,7 @@ void MapView::updateTilesGBA(bool) { m_rawMap = QImage(QSize(width, height), QImage::Format_ARGB32); uchar* bgBits = m_rawMap.bits(); for (int j = 0; j < height; ++j) { - mBitmapCacheCleanRow(bitmapCache, m_bitmapStatus, j); + mBitmapCacheCleanRow(bitmapCache, m_bitmapStatus.data(), j); memcpy(static_cast(&bgBits[width * j * 4]), mBitmapCacheGetRow(bitmapCache, j), width * 4); } m_rawMap = m_rawMap.convertToFormat(QImage::Format_RGB32).rgbSwapped(); @@ -239,7 +245,7 @@ void MapView::updateTilesGBA(bool) { m_ui.bgInfo->setCustomProperty("priority", priority); m_ui.bgInfo->setCustomProperty("offset", offset); m_ui.bgInfo->setCustomProperty("transform", transform); - m_rawMap = compositeMap(m_map, m_mapStatus); + m_rawMap = compositeMap(m_map, &m_mapStatus); } } QPixmap map = QPixmap::fromImage(m_rawMap.convertToFormat(QImage::Format_RGB32)); diff --git a/src/platform/qt/MapView.h b/src/platform/qt/MapView.h index 89c09259b..1a3ca1b91 100644 --- a/src/platform/qt/MapView.h +++ b/src/platform/qt/MapView.h @@ -43,8 +43,8 @@ private: Ui::MapView m_ui; std::shared_ptr m_controller; - mMapCacheEntry m_mapStatus[128 * 128] = {}; // TODO: Correct size - mBitmapCacheEntry m_bitmapStatus[512 * 2] = {}; // TODO: Correct size + QVector m_mapStatus; + QVector m_bitmapStatus{512 * 2}; // TODO: Correct size int m_map = 0; QImage m_rawMap; int m_boundary;