Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid frequent file open and close for dumps, ignore WPC transitional frames #11

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/DMDUtil/DMD.h
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@

#define DMDUTIL_FRAME_BUFFER_SIZE 16
#define DMDUTIL_MAX_NAME_SIZE 32
#define DMDUTIL_MAX_TRANSITIONAL_FRAME_DURATION 25

#include <atomic>
#include <condition_variable>
62 changes: 47 additions & 15 deletions src/DMD.cpp
Original file line number Diff line number Diff line change
@@ -974,9 +974,12 @@ void DMD::DumpDMDTxtThread()
char name[DMDUTIL_MAX_NAME_SIZE] = "";
char filename[128];
int bufferPosition = 0;
uint8_t renderBuffer[256 * 64] = {0};
int renderBufferPosition = 1;
uint8_t renderBuffer[3][256 * 64] = {0};
uint32_t passed[3] = {0};
bool update = false;
std::chrono::steady_clock::time_point start;
FILE* f = nullptr;

while (true)
{
@@ -985,6 +988,7 @@ void DMD::DumpDMDTxtThread()
sl.unlock();
if (m_stopFlag)
{
if (f) fclose(f);
return;
}

@@ -999,36 +1003,62 @@ void DMD::DumpDMDTxtThread()
if (strcmp(m_updateBuffer[m_updateBufferPosition]->name, name) != 0)
{
// New game ROM.
start = std::chrono::steady_clock::now();
if (f) fclose(f);
strcpy(name, m_updateBuffer[m_updateBufferPosition]->name);
snprintf(filename, DMDUTIL_MAX_NAME_SIZE + 5, "%s.txt", name);
f = fopen(filename, "a");
update = true;
start = std::chrono::steady_clock::now();
memset(renderBuffer, 0, 2 * 256 * 64);
passed[0] = passed[1] = 0;
}

if (name[0] != '\0')
{
int length = m_updateBuffer[bufferPosition]->width * m_updateBuffer[bufferPosition]->height;
if (update || (memcmp(renderBuffer, m_updateBuffer[bufferPosition]->data, length) != 0))
if (update || (memcmp(renderBuffer[1], m_updateBuffer[bufferPosition]->data, length) != 0))
{
memcpy(renderBuffer, m_updateBuffer[bufferPosition]->data, length);
FILE* f = fopen(filename, "a");
passed[2] = (uint32_t)(std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start)
.count());
memcpy(renderBuffer[2], m_updateBuffer[bufferPosition]->data, length);

if (m_updateBuffer[bufferPosition]->depth == 2 &&
(passed[2] - passed[1]) < DMDUTIL_MAX_TRANSITIONAL_FRAME_DURATION)
{
int i = 0;
while (i < length &&
((renderBuffer[0][i] == 2) ||
((renderBuffer[0][i] == 3) || (renderBuffer[2][i] > 1)) == (renderBuffer[1][i] > 0)))
{
i++;
}
if (i == length)
{
// renderBuffer[1] is a transitional frame, delete it.
memcpy(renderBuffer[1], renderBuffer[2], length);
passed[1] += passed[2];
continue;
}
}

if (f)
{
fprintf(f, "0x%08x\n",
(uint32_t)(std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start)
.count()));
fprintf(f, "0x%08x\n", passed[0]);
for (int y = 0; y < m_updateBuffer[bufferPosition]->height; y++)
{
for (int x = 0; x < m_updateBuffer[bufferPosition]->width; x++)
{
fprintf(f, "%x", renderBuffer[y * m_updateBuffer[bufferPosition]->width + x]);
fprintf(f, "%x", renderBuffer[0][y * m_updateBuffer[bufferPosition]->width + x]);
}
fprintf(f, "\n");
}
fprintf(f, "\n");
fclose(f);
}
memcpy(renderBuffer[0], renderBuffer[1], length);
passed[0] = passed[1];
memcpy(renderBuffer[1], renderBuffer[2], length);
passed[1] = passed[2];
}
}
}
@@ -1042,6 +1072,7 @@ void DMD::DumpDMDRawThread()
char filename[128];
int bufferPosition = 0;
std::chrono::steady_clock::time_point start;
FILE* f = nullptr;

while (true)
{
@@ -1050,6 +1081,7 @@ void DMD::DumpDMDRawThread()
sl.unlock();
if (m_stopFlag)
{
if (f) fclose(f);
return;
}

@@ -1062,15 +1094,17 @@ void DMD::DumpDMDRawThread()
if (strcmp(m_updateBuffer[m_updateBufferPosition]->name, name) != 0)
{
// New game ROM.
start = std::chrono::steady_clock::now();
if (f) fclose(f);
strcpy(name, m_updateBuffer[m_updateBufferPosition]->name);
snprintf(filename, DMDUTIL_MAX_NAME_SIZE + 5, "%s.raw", name);
start = std::chrono::steady_clock::now();
f = fopen(filename, "ab");
}

if (name[0] != '\0')
{
int length = m_updateBuffer[bufferPosition]->width * m_updateBuffer[bufferPosition]->height;
FILE* f = fopen(filename, "ab");

if (f)
{
auto current =
@@ -1081,8 +1115,6 @@ void DMD::DumpDMDRawThread()
fwrite(&size, 1, 4, f);

fwrite(m_updateBuffer[bufferPosition], 1, size, f);

fclose(f);
}
}
}
3 changes: 1 addition & 2 deletions src/test.cpp
Original file line number Diff line number Diff line change
@@ -171,8 +171,7 @@ int main(int argc, const char* argv[])

for (int i = 1; i <= 100; i++)
{
snprintf(filename, 28, "test/rgb565_%dx%d/%04d.raw",
width, height, i);
snprintf(filename, 28, "test/rgb565_%dx%d/%04d.raw", width, height, i);
printf("Render raw: %s\n", filename);
fileptr = fopen(filename, "rb");
fread(buffer, size, 1, fileptr);