OSDN Git Service

[EMU][THREAD] Fix thread dependency: Move OSD from DrawThread to EmuThreadClass....
[csp-qt/common_source_project-fm7.git] / source / src / qt / gui / draw_thread.cpp
1 /*
2         Skelton for retropc emulator
3         Author : Takeda.Toshiya
4         Port to Qt : K.Ohta <whatisthis.sowhat _at_ gmail.com>
5         Date   : 2006.08.18 -
6         License : GPLv2
7         History : 2015.11.10 Split from qt_main.cpp
8         [ win32 main ] -> [ Qt main ] -> [Drawing]
9 */
10
11 #include <Qt>
12 #include <QApplication>
13 #include <QImage>
14 #include <QGuiApplication>
15
16 #include <SDL.h>
17 #include "emu.h"
18 #include "osd.h"
19 #include "vm/vm.h"
20
21 #include "qt_main.h"
22 #include "csp_logger.h"
23 #include "mainwidget_base.h"
24 #include "draw_thread.h"
25 #include "qt_glutil_gl2_0.h"
26
27 DrawThreadClass::DrawThreadClass(EMU *p, OSD *o, QObject *parent) : QThread(parent) {
28         MainWindow = (Ui_MainWindowBase *)parent;
29         glv = MainWindow->getGraphicsView();
30         p_emu = emu;
31         p_osd = o;
32         
33         screen = QGuiApplication::primaryScreen();
34         
35         draw_screen_buffer = NULL;
36         
37         do_change_refresh_rate(screen->refreshRate());
38         connect(screen, SIGNAL(refreshRateChanged(qreal)), this, SLOT(do_change_refresh_rate(qreal)));
39         connect(this, SIGNAL(sig_update_screen(bitmap_t *)), glv, SLOT(update_screen(bitmap_t *)), Qt::QueuedConnection);
40         connect(this, SIGNAL(sig_push_frames_to_avio(int, int, int)), glv->extfunc, SLOT(paintGL_OffScreen(int, int, int)));
41         rec_frame_width = 640;
42         rec_frame_height = 480;
43         rec_frame_count = -1;
44
45         bDrawReq = false;
46 }
47
48 DrawThreadClass::~DrawThreadClass()
49 {
50 }
51
52 void DrawThreadClass::SetEmu(EMU *p)
53 {
54         //p_emu = p;
55         p_osd = p->get_osd();
56 }
57
58
59 void DrawThreadClass::doDraw(bool flag)
60 {
61         p_osd->do_decode_movie(1);
62         if(flag) {
63                 draw_frames = p_osd->draw_screen();
64         } else {
65                 draw_frames = p_osd->no_draw_screen();
66         }
67         emit sig_draw_frames(draw_frames);
68 }
69
70 void DrawThreadClass::doExit(void)
71 {
72         //csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL,
73         //                                        "DrawThread : Exit.");
74         bRunThread = false;
75 }
76
77 void DrawThreadClass::doWork(const QString &param)
78 {
79
80         bRunThread = true;
81         do {
82                 if(bDrawReq) {
83                         if(draw_screen_buffer != NULL) {
84                                 bDrawReq = false;
85                                 emit sig_update_screen(draw_screen_buffer);
86                         }
87                 }
88                 if(rec_frame_count > 0) {
89                         emit sig_push_frames_to_avio(rec_frame_count,
90                                                                                  rec_frame_width, rec_frame_height);
91                         rec_frame_count = -1;
92                 }
93                 if(wait_count < 1.0f) {
94                         msleep(1);
95                         wait_count = wait_count + wait_refresh - 1.0f;
96                 } else {
97                         wait_factor = (int)wait_count;
98                         msleep(wait_factor);
99                         wait_count -= (qreal)wait_factor;
100                 }
101         } while(bRunThread);
102         csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL,
103                                                   "DrawThread : Exit.");
104         this->exit(0);
105 }
106
107 void DrawThreadClass::do_change_refresh_rate(qreal rate)
108 {
109         refresh_rate = rate;    
110         wait_refresh = 1000.0f / (refresh_rate * 4.0);
111         wait_count = wait_refresh * 1.0;
112 }
113
114 void DrawThreadClass::do_update_screen(bitmap_t *p)
115 {
116         draw_screen_buffer = p;
117         bDrawReq = true;
118 }
119         
120 void DrawThreadClass::do_req_encueue_video(int count, int width, int height)
121 {
122         rec_frame_width = width;
123         rec_frame_height = height;
124         rec_frame_count = count;
125 }
126