-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrrblitter.cpp
146 lines (131 loc) · 3.34 KB
/
rrblitter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* Copyright (C)2004 Landmark Graphics
* Copyright (C)2005-2008 Sun Microsystems, Inc.
*
* This library is free software and may be redistributed and/or modified under
* the terms of the wxWindows Library License, Version 3 or (at your option)
* any later version. The full license is in the LICENSE.txt file included
* with this distribution.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* wxWindows Library License for more details.
*/
#include "rrblitter.h"
#include "rrtimer.h"
#include "fakerconfig.h"
rrblitter::rrblitter(void) : _t(NULL), _deadyet(false)
{
for(int i=0; i<NB; i++) _bmp[i]=NULL;
errifnot(_t=new Thread(this));
_t->start();
_prof_blit.setname("Blit ");
_prof_total.setname("Total ");
if(fconfig.verbose) fbx_printwarnings(rrout.getfile());
}
void rrblitter::run(void)
{
rrtimer t, sleept; double err=0.; bool first=true;
// rrfb *lastb=NULL;
try {
while(!_deadyet)
{
rrfb *b=NULL;
_q.get((void **)&b); if(_deadyet) return;
if(!b) _throw("Queue has been shut down");
_ready.signal();
_prof_blit.startframe();
b->redraw();
_prof_blit.endframe(b->_h.width*b->_h.height, 0, 1);
_prof_total.endframe(b->_h.width*b->_h.height, 0, 1);
_prof_total.startframe();
if(fconfig.fps>0.)
{
double elapsed=t.elapsed();
if(first) first=false;
else
{
if(elapsed<1./fconfig.fps)
{
sleept.start();
long usec=(long)((1./fconfig.fps-elapsed-err)*1000000.);
if(usec>0) usleep(usec);
double sleeptime=sleept.elapsed();
err=sleeptime-(1./fconfig.fps-elapsed-err); if(err<0.) err=0.;
}
}
t.start();
}
b->complete();
// lastb=b;
}
} catch(rrerror &e)
{
if(_t) _t->seterror(e);
_ready.signal(); throw;
}
}
rrfb *rrblitter::getbitmap(HWND hwnd, int w, int h, bool spoil)
{
rrfb *b=NULL;
if(!spoil) _ready.wait();
if(_t) _t->checkerror();
{
rrcs::safelock l(_bmpmutex);
int bmpi=-1;
for(int i=0; i<NB; i++)
if(!_bmp[i] || (_bmp[i] && _bmp[i]->iscomplete())) bmpi=i;
if(bmpi<0) _throw("No free buffers in pool");
if(!_bmp[bmpi]) errifnot(_bmp[bmpi]=new rrfb(hwnd));
b=_bmp[bmpi]; b->waituntilcomplete();
}
rrframeheader hdr;
memset(&hdr, 0, sizeof(hdr));
hdr.height=hdr.frameh=h;
hdr.width=hdr.framew=w;
hdr.x=hdr.y=0;
b->init(hdr);
return b;
}
bool rrblitter::frameready(void)
{
if(_t) _t->checkerror();
return(_q.items()<=0);
}
static void __rrblitter_spoilfct(void *b)
{
if(b) ((rrfb *)b)->complete();
}
void rrblitter::sendframe(rrfb *b)
{
static rrtimer t, sleept; double err=0.; static bool first=true;
if(_t) _t->checkerror();
if(fconfig.serial)
{
_ready.signal();
_prof_blit.startframe();
b->redraw();
_prof_blit.endframe(b->_h.width*b->_h.height, 0, 1);
_prof_total.endframe(b->_h.width*b->_h.height, 0, 1);
_prof_total.startframe();
if(fconfig.fps>0.)
{
double elapsed=t.elapsed();
if(first) first=false;
else
{
if(elapsed<1./fconfig.fps)
{
sleept.start();
long usec=(long)((1./fconfig.fps-elapsed-err)*1000000.);
if(usec>0) usleep(usec);
double sleeptime=sleept.elapsed();
err=sleeptime-(1./fconfig.fps-elapsed-err); if(err<0.) err=0.;
}
}
t.start();
}
b->complete();
}
else _q.spoil((void *)b, __rrblitter_spoilfct);
}