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