2 Skelton for retropc emulator
3 Author : Takeda.Toshiya
4 Port to Qt : K.Ohta <whatisthis.sowhat _at_ gmail.com>
7 History : 2015.11.10 Split from qt_main.cpp
8 [ win32 main ] -> [ Qt main ] -> [Drawing]
12 #include <QApplication>
14 #include <QGuiApplication>
17 #include <QWaitCondition>
25 #include "csp_logger.h"
26 #include "mainwidget_base.h"
27 #include "draw_thread.h"
28 #include "gl2/qt_glutil_gl2_0.h"
32 DrawThreadClass::DrawThreadClass(OSD *o, CSP_Logger *logger,QObject *parent) : QThread(parent) {
33 MainWindow = (Ui_MainWindowBase *)parent;
34 glv = MainWindow->getGraphicsView();
38 if(p_osd != NULL) using_flags = p_osd->get_config_flags();
39 screen = QGuiApplication::primaryScreen();
41 draw_screen_buffer = NULL;
43 do_change_refresh_rate(screen->refreshRate());
44 connect(screen, SIGNAL(refreshRateChanged(qreal)), this, SLOT(do_change_refresh_rate(qreal)));
45 connect(this, SIGNAL(sig_update_screen(bitmap_t *)), glv, SLOT(update_screen(bitmap_t *)), Qt::QueuedConnection);
47 connect(this, SIGNAL(sig_update_osd()), glv, SLOT(update_osd()), Qt::QueuedConnection);
48 connect(this, SIGNAL(sig_push_frames_to_avio(int, int, int)), glv->extfunc, SLOT(paintGL_OffScreen(int, int, int)));
49 //connect(this, SIGNAL(sig_call_draw_screen()), p_osd, SLOT(draw_screen()));
50 //connect(this, SIGNAL(sig_call_no_draw_screen()), p_osd, SLOT(no_draw_screen()));
51 use_separate_thread_draw = config.use_separate_thread_draw;
53 rec_frame_width = 640;
54 rec_frame_height = 480;
56 emu_frame_rate = 1000.0 / 30.0;
57 wait_count = emu_frame_rate;
58 wait_refresh = emu_frame_rate;
60 renderSemaphore = new QSemaphore(0);
63 DrawThreadClass::~DrawThreadClass()
65 if(renderSemaphore != NULL) {
66 while(renderSemaphore->available() <= 0) renderSemaphore->release(1);
67 delete renderSemaphore;
72 void DrawThreadClass::SetEmu(EMU *p)
78 void DrawThreadClass::do_set_frames_per_second(double fps)
80 double _n = 1000.0 / (fps * 2.0);
82 wait_count += (_n * 1.0);
85 void DrawThreadClass::doDrawMain(bool flag)
87 p_osd->do_decode_movie(1);
89 draw_frames = p_osd->draw_screen();
91 draw_frames = p_osd->no_draw_screen();
93 emit sig_draw_frames(draw_frames);
95 void DrawThreadClass::doDraw(bool flag)
97 bRecentRenderStatus = flag;
98 if(!use_separate_thread_draw) {
101 if(renderSemaphore != NULL) renderSemaphore->release(1);
105 void DrawThreadClass::doExit(void)
107 //csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL,
108 // "DrawThread : Exit.");
110 if(renderSemaphore != NULL) {
111 while(renderSemaphore->available() <= 0) renderSemaphore->release(1);
115 void DrawThreadClass::do_draw_one_turn(bool _req_draw)
117 if((_req_draw) && (draw_screen_buffer != NULL)) {
118 emit sig_update_screen(draw_screen_buffer);
120 if(ncount == 0) emit sig_update_osd();
123 if(ncount >= 8) ncount = 0;
124 if(rec_frame_count > 0) {
125 emit sig_push_frames_to_avio(rec_frame_count,
126 rec_frame_width, rec_frame_height);
127 rec_frame_count = -1;
131 void DrawThreadClass::doWork(const QString ¶m)
135 double _rate = 1000.0 / 30.0;
137 if(renderSemaphore == NULL) goto __exit;
139 _rate = (wait_refresh < emu_frame_rate) ? emu_frame_rate : wait_refresh;
143 wait_factor = (int)_rate - 1;
145 if(renderSemaphore->tryAcquire(1, wait_factor)) { // Success
146 if(!bRunThread) break;
147 volatile bool _b = bRecentRenderStatus;
148 bRecentRenderStatus = false;
151 if(!bRunThread) break;
152 volatile bool _d = bDrawReq;
153 if(draw_screen_buffer == NULL) _d = false;
154 if((_d) && (draw_screen_buffer != NULL)) bDrawReq = false;
155 do_draw_one_turn(_d);
158 csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL,
159 "DrawThread : Exit.");
163 void DrawThreadClass::do_change_refresh_rate(qreal rate)
166 wait_refresh = 1000.0 / (refresh_rate * 2.0);
167 wait_count += (wait_refresh * 1.0);
170 void DrawThreadClass::do_update_screen(bitmap_t *p)
172 draw_screen_buffer = p;
176 void DrawThreadClass::do_req_encueue_video(int count, int width, int height)
178 rec_frame_width = width;
179 rec_frame_height = height;
180 rec_frame_count = count;