-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProducerDisplayThread.cpp
More file actions
120 lines (104 loc) · 2.62 KB
/
ProducerDisplayThread.cpp
File metadata and controls
120 lines (104 loc) · 2.62 KB
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
#include "ProducerDisplayThread.h"
#define MAX_FRAMES_PER_SECOND 30
ProducerDisplayThread::ProducerDisplayThread(int bufsize, MicroscopeManager* mm, QObject* mainWindow, int* targetFrameInfo):
ProducerThread(bufsize, mm),
mainWindow_(mainWindow),
targetFrameInfo_(targetFrameInfo),
frameCount_(targetFrameInfo[0]),
currentFrame_(targetFrameInfo[1]),
bufferCount_(0),
endDisplay_(false)
{
width = mm->GetCameraIntParameter(STREAM_MODULE, "Width");
height = mm->GetCameraIntParameter(STREAM_MODULE, "Height");
framerate = mm->GetCameraFloatParameter(REMOTE_MODULE, "AcquisitionFrameRate");
framesToDrop_ = ceilf(framerate / (frameCount_ * MAX_FRAMES_PER_SECOND)) - 1;
disBuf_ = new unsigned char[width * height];
pix_ = new PixmapReadyObject();
}
ProducerDisplayThread::~ProducerDisplayThread()
{}
void ProducerDisplayThread::StartThreads()
{
StartThread();
frameThd_ = std::thread(&ProducerDisplayThread::CheckFrameInfo, this);
disThd_ = std::thread(&ProducerDisplayThread::Display, this);
}
void ProducerDisplayThread::WaitForThread()
{
{
std::unique_lock<std::mutex> ul(pMutex);
endDisplay_ = true;
}
pCV.notify_all();
frameThd_.join();
disThd_.join();
ProducerThread::WaitForThread();
}
void ProducerDisplayThread::CheckFrameInfo()
{
while (active)
{
if (frameCount_ != targetFrameInfo_[0])
{
frameCount_ = targetFrameInfo_[0];
framesToDrop_ = ceilf(framerate / (frameCount_ * MAX_FRAMES_PER_SECOND)) - 1;
}
if (currentFrame_ != targetFrameInfo_[1])
{
currentFrame_ = targetFrameInfo_[1];
}
}
}
void ProducerDisplayThread::Display()
{
QObject::connect(pix_, SIGNAL(pixmapReady(const QPixmap&, bool)), mainWindow_, SLOT(updateDisplayFrame(const QPixmap&, bool)));
int targetFrame;
int framesDropped = 0;
while (active)
{
if (currentFrame_ < bufReadys.size())
{
targetFrame = currentFrame_;
}
else
{
targetFrame = 0;
}
if (framesDropped >= framesToDrop_)
{
{
std::unique_lock<std::mutex> ul(pMutex);
if (!bufReadys[targetFrame])
{
pCV.wait(ul, [this, targetFrame]() {return bufReadys[targetFrame] || endDisplay_; });
}
}
if (!active)
{
return;
}
try
{
std::memcpy(disBuf_, imgBuffers[targetFrame], bufSize);
QImage img = QImage(disBuf_, width, height, width, QImage::Format_Grayscale8);
const QPixmap pixmap = QPixmap::fromImage(img);
pixmapProcessed = false;
pix_->sendSignal(pixmap, true);
//while(!pixmapProcessed){} //Stall until successful display
}
catch (...)
{
active = false;
}
}
else
{
++framesDropped;
}
}
}
void ProducerDisplayThread::processPixmap()
{
pixmapProcessed = true;
}