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