7bc36352ef14bc3e5e71756ec513f0bfbdfcff5d
[dcpomatic.git] / src / wx / gpu_config_panel.h
1 #pragma once
2
3 static std::vector<std::string>
4 get_gpu_names(std::string binary, std::string filename)
5 {
6         // Execute the GPU listing program and redirect its output to a file
7         std::system((binary + " > " + filename).c_str());
8
9         std::vector<std::string> gpu_names;
10         std::ifstream file(filename);
11         if (file.is_open()) {
12                 std::string line;
13                 while (std::getline(file, line))
14                         gpu_names.push_back(line);
15                 file.close();
16         }
17
18         return gpu_names;
19 }
20
21 class GpuList : public wxPanel
22 {
23 public:
24         GpuList(wxPanel* parent)
25                 : wxPanel(parent, wxID_ANY)
26                 , selection(0)
27         {
28                 comboBox = new wxComboBox(this, wxID_ANY, "", wxDefaultPosition, wxSize(400, -1));
29                 comboBox->Bind(wxEVT_COMBOBOX, &GpuList::OnComboBox, this);
30                 update();
31
32                 wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
33
34                 sizer->Add(comboBox, 0, wxALIGN_CENTER_VERTICAL); // Vertically center the comboBox
35
36                 this->SetSizerAndFit(sizer);
37         }
38
39         void update(void)
40         {
41                 auto cfg = Config::instance();
42                 auto lister_binary = cfg->gpu_binary_location() + "/" + "gpu_lister";
43                 auto lister_file = cfg->gpu_binary_location() + "/" + "gpus.txt";
44                 if (boost::filesystem::exists(lister_binary)) {
45                         auto gpu_names = get_gpu_names(lister_binary, lister_file);
46
47                         comboBox->Clear();
48                         for (const auto& name : gpu_names)
49                                 comboBox->Append(name);
50                 }
51         }
52
53         int getSelection(void)
54         {
55                 return selection;
56         }
57
58         void setSelection(int sel)
59         {
60                 if ((int)comboBox->GetCount() > sel)
61                         comboBox->SetSelection(sel);
62         }
63
64 private:
65         void OnComboBox([[maybe_unused]] wxCommandEvent& event)
66         {
67                 selection = comboBox->GetSelection();
68                 if (selection != wxNOT_FOUND)
69                         Config::instance()->set_selected_gpu(selection);
70         }
71
72         wxComboBox* comboBox;
73         int selection;
74 };
75
76 class GPUPage : public Page
77 {
78 public:
79         GPUPage(wxSize panel_size, int border)
80                 : Page(panel_size, border)
81                 , _enable_gpu(nullptr)
82                 , _binary_location(nullptr)
83                 , _gpu_list_control(nullptr)
84         {
85         }
86
87         wxString GetName() const override
88         {
89                 return _("GPU");
90         }
91
92 #ifdef DCPOMATIC_OSX
93         wxBitmap GetLargeIcon() const override
94         {
95                 return wxBitmap(icon_path("tms"), wxBITMAP_TYPE_PNG);
96         }
97 #endif
98
99 private:
100         void setup() override
101         {
102                 auto config = Config::instance();
103
104                 _enable_gpu = new CheckBox(_panel, _("Enable GPU Acceleration"));
105                 _panel->GetSizer()->Add(_enable_gpu, 0, wxALL | wxEXPAND, _border);
106
107                 wxFlexGridSizer* table = new wxFlexGridSizer(2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
108                 table->AddGrowableCol(1, 1);
109                 _panel->GetSizer()->Add(table, 1, wxALL | wxEXPAND, _border);
110
111                 add_label_to_sizer(table, _panel, _("Acceleration Binary Folder"), true, 0, wxLEFT | wxLEFT | wxALIGN_CENTRE_VERTICAL);
112                 _binary_location = new wxDirPickerCtrl(_panel, wxDD_DIR_MUST_EXIST);
113                 table->Add(_binary_location, 1, wxEXPAND);
114
115                 add_label_to_sizer(table, _panel, _("GPU Selection"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
116                 _gpu_list_control = new GpuList(_panel);
117                 table->Add(_gpu_list_control, 1, wxEXPAND);
118
119                 add_label_to_sizer(table, _panel, _("License Server"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
120                 _server = new wxTextCtrl(_panel, wxID_ANY);
121                 table->Add(_server, 1, wxEXPAND | wxALL);
122
123                 add_label_to_sizer(table, _panel, _("Port"), false, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
124                 _port = new wxSpinCtrl(_panel, wxID_ANY);
125                 _port->SetRange(0, 65535);
126                 table->Add(_port);
127
128                 add_label_to_sizer(table, _panel, _("License"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
129                 _license = new PasswordEntry(_panel);
130                 table->Add(_license->get_panel(), 1, wxEXPAND | wxALL);
131
132                 _enable_gpu->bind(&GPUPage::enable_gpu_changed, this);
133                 _binary_location->Bind(wxEVT_DIRPICKER_CHANGED, boost::bind(&GPUPage::binary_location_changed, this));
134                 _server->Bind(wxEVT_TEXT, boost::bind(&GPUPage::server_changed, this));
135                 _port->Bind(wxEVT_SPINCTRL, boost::bind(&GPUPage::port_changed, this));
136                 _license->Changed.connect(boost::bind(&GPUPage::license_changed, this));
137
138                 _binary_location->Enable(config->enable_gpu());
139                 _gpu_list_control->Enable(config->enable_gpu());
140                 _server->Enable(config->enable_gpu());
141                 _port->Enable(config->enable_gpu());
142                 _license->get_panel()->Enable(config->enable_gpu());
143         }
144
145         void config_changed() override
146         {
147                 auto config = Config::instance();
148
149                 checked_set(_enable_gpu, config->enable_gpu());
150                 _binary_location->SetPath(config->gpu_binary_location());
151                 _gpu_list_control->update();
152                 _gpu_list_control->setSelection(config->selected_gpu());
153                 checked_set(_server, config->gpu_license_server());
154                 checked_set(_port, config->gpu_license_port());
155                 checked_set(_license, config->gpu_license());
156         }
157
158         void enable_gpu_changed()
159         {
160                 auto config = Config::instance();
161
162                 config->set_enable_gpu(_enable_gpu->GetValue());
163                 _binary_location->Enable(config->enable_gpu());
164                 _gpu_list_control->Enable(config->enable_gpu());
165                 _server->Enable(config->enable_gpu());
166                 _port->Enable(config->enable_gpu());
167                 _license->get_panel()->Enable(config->enable_gpu());
168         }
169
170         void binary_location_changed()
171         {
172                 Config::instance()->set_gpu_binary_location(wx_to_std(_binary_location->GetPath()));
173                 _gpu_list_control->update();
174         }
175
176         void server_changed()
177         {
178                 Config::instance()->set_gpu_license_server(wx_to_std(_server->GetValue()));
179         }
180
181         void port_changed()
182         {
183                 Config::instance()->set_gpu_license_port(_port->GetValue());
184         }
185
186         void license_changed()
187         {
188                 Config::instance()->set_gpu_license(_license->get());
189         }
190
191         CheckBox* _enable_gpu;
192         wxDirPickerCtrl* _binary_location;
193         GpuList* _gpu_list_control;
194         wxTextCtrl* _server;
195         wxSpinCtrl* _port;
196         PasswordEntry* _license;
197 };