* This should be called before grayscale buffers are populated. * A `restoreBwBuffer` call should always follow the grayscale render if this method was called. * Uses chunked allocation to avoid needing 48KB of contiguous memory. * Returns true if buffer was stored successfully, false if allocation failed. */
| 1811 | * Returns true if buffer was stored successfully, false if allocation failed. |
| 1812 | */ |
| 1813 | bool GfxRenderer::storeBwBuffer() { |
| 1814 | // Allocate and copy each chunk |
| 1815 | for (size_t i = 0; i < bwBufferChunks.size(); i++) { |
| 1816 | // Check if any chunks are already allocated |
| 1817 | if (bwBufferChunks[i]) { |
| 1818 | LOG_ERR("GFX", "!! BW buffer chunk %zu already stored - this is likely a bug, freeing chunk", i); |
| 1819 | free(bwBufferChunks[i]); |
| 1820 | bwBufferChunks[i] = nullptr; |
| 1821 | } |
| 1822 | |
| 1823 | const size_t offset = i * BW_BUFFER_CHUNK_SIZE; |
| 1824 | const size_t chunkSize = std::min(BW_BUFFER_CHUNK_SIZE, static_cast<size_t>(frameBufferSize - offset)); |
| 1825 | bwBufferChunks[i] = static_cast<uint8_t*>(malloc(chunkSize)); |
| 1826 | |
| 1827 | if (!bwBufferChunks[i]) { |
| 1828 | LOG_ERR("GFX", "!! Failed to allocate BW buffer chunk %zu (%zu bytes)", i, chunkSize); |
| 1829 | // Free previously allocated chunks |
| 1830 | freeBwBufferChunks(); |
| 1831 | return false; |
| 1832 | } |
| 1833 | |
| 1834 | memcpy(bwBufferChunks[i], frameBuffer + offset, chunkSize); |
| 1835 | } |
| 1836 | |
| 1837 | LOG_DBG("GFX", "Stored BW buffer in %zu chunks (%zu bytes each)", bwBufferChunks.size(), BW_BUFFER_CHUNK_SIZE); |
| 1838 | return true; |
| 1839 | } |
| 1840 | |
| 1841 | /** |
| 1842 | * This can only be called if `storeBwBuffer` was called prior to the grayscale render. |
no test coverage detected