Skip to content

Commit 9484401

Browse files
author
mfiess
authored
framerate updates (pxscene#671) (pxscene#692)
1 parent babb1cf commit 9484401

File tree

2 files changed

+45
-28
lines changed

2 files changed

+45
-28
lines changed

src/wayland_egl/pxWindowNative.cpp

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "../pxTimer.h"
99
#include "../pxWindowUtil.h"
1010
#include "../pxKeycodes.h"
11+
#include "../rtLog.h"
1112

1213
#include <stdlib.h>
1314
#include <string.h>
@@ -381,6 +382,12 @@ void displayRef::cleanupWaylandDisplay()
381382

382383
bool exitFlag = false;
383384

385+
pxWindowNative::pxWindowNative(): mTimerFPS(0), mLastWidth(-1), mLastHeight(-1),
386+
mResizeFlag(false), mLastAnimationTime(0.0), mVisible(false),
387+
mWaylandSurface(NULL), mWaylandBuffer(), waylandBufferIndex(0)
388+
{
389+
}
390+
384391
pxWindowNative::~pxWindowNative()
385392
{
386393
cleanupWaylandData();
@@ -549,27 +556,52 @@ void pxWindowNative::runEventLoop()
549556
waylandDisplay* display = dRef.getDisplay();
550557
std::vector<pxWindowNative*> windowVector = pxWindowNative::getNativeWindows();
551558

559+
int framerate = WAYLAND_PX_CORE_FPS;
560+
561+
char const *s = getenv("PXCORE_FRAMERATE");
562+
if (s)
563+
{
564+
int fps = atoi(s);
565+
if (fps > 0)
566+
{
567+
framerate = fps;
568+
}
569+
}
570+
571+
rtLogInfo("pxcore framerate: %d", framerate);
572+
573+
uint64_t* offsets = new uint64_t[ framerate ];
574+
for( int i = 0; i < framerate; ++i )
575+
offsets[ i ] = (i*1000000+framerate-1)/framerate;
576+
577+
int frameNo = 1;
578+
double wakeUpBase = pxMicroseconds();
579+
int count = 0, lastCount = -1;
552580
while(!exitFlag)
553581
{
554-
double startMilliseconds = pxMilliseconds();
582+
count++;
555583
std::vector<pxWindowNative*>::iterator i;
556584
for (i = windowVector.begin(); i < windowVector.end(); i++)
557585
{
558586
pxWindowNative* w = (*i);
559587
w->animateAndRender();
560588
}
561589
wl_display_dispatch_pending(display->display);
562-
int processTime = (int)pxMilliseconds() - (int)startMilliseconds;
563-
if (processTime < 0)
564-
{
565-
processTime = 0;
566-
}
567-
if (processTime < 32)
568-
{
569-
usleep((32-processTime)*1000);
590+
double delay = pxMicroseconds();
591+
double nextWakeUp = wakeUpBase + offsets[ frameNo ];
592+
while( delay > nextWakeUp ) {
593+
frameNo++;
594+
if( frameNo >= framerate ) {
595+
count = 0;
596+
wakeUpBase += 1000000;
597+
frameNo = 0;
598+
}
599+
nextWakeUp = wakeUpBase + offsets[ frameNo ];
570600
}
571-
//pxSleepMS(1000); // Breath
601+
delay = nextWakeUp - delay;
602+
usleep( delay );
572603
}
604+
delete [] offsets;
573605
}
574606

575607

@@ -791,28 +823,16 @@ waylandBuffer* pxWindowNative::nextBuffer()
791823

792824
void pxWindowNative::animateAndRender()
793825
{
794-
static double lastAnimationTime = pxMilliseconds();
795-
double currentAnimationTime = pxMilliseconds();
796826
drawFrame();
797827

798-
double animationDelta = currentAnimationTime-lastAnimationTime;
799828
if (mResizeFlag)
800829
{
801830
mResizeFlag = false;
802831
onSize(mLastWidth, mLastHeight);
803832
invalidateRectInternal(NULL);
804833
}
805834

806-
if (mTimerFPS)
807-
{
808-
animationDelta = currentAnimationTime - getLastAnimationTime();
809-
810-
if (animationDelta > (1000/mTimerFPS))
811-
{
812-
onAnimationTimerInternal();
813-
setLastAnimationTime(currentAnimationTime);
814-
}
815-
}
835+
onAnimationTimerInternal();
816836
}
817837

818838
void pxWindowNative::setLastAnimationTime(double time)
@@ -881,7 +901,7 @@ void pxWindowNative::initializeEgl()
881901

882902
waylandDisplay* display = mDisplayRef.getDisplay();
883903

884-
display->egl.dpy = eglGetDisplay(display->display);
904+
display->egl.dpy = eglGetDisplay((EGLNativeDisplayType)display->display);
885905
assert(display->egl.dpy);
886906

887907
ret = eglInitialize(display->egl.dpy, &major, &minor);

src/wayland_egl/pxWindowNative.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,7 @@ class displayRef
8787
class pxWindowNative
8888
{
8989
public:
90-
pxWindowNative(): mTimerFPS(0), mLastWidth(-1), mLastHeight(-1),
91-
mResizeFlag(false), mLastAnimationTime(0.0), mVisible(false),
92-
mWaylandSurface(NULL), mWaylandBuffer(), waylandBufferIndex(0)
93-
{ }
90+
pxWindowNative();
9491
virtual ~pxWindowNative();
9592

9693
// Contract between pxEventLoopNative and this class

0 commit comments

Comments
 (0)