ManaPlus
testmain.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "test/testmain.h"
23 
24 #ifdef USE_OPENGL
25 
26 #include "logger.h"
27 #include "settings.h"
28 
29 #include "fs/paths.h"
30 
31 #include "utils/cast.h"
32 #include "utils/delete2.h"
33 #include "utils/process.h"
34 
35 #include <fstream>
36 
37 #include "debug.h"
38 
39 std::string fileName;
40 extern char *selfName;
41 
43  log(new Logger),
44  mConfig()
45 {
47  log->setLogFile(pathJoin(settings.localDataDir, "manaplustest.log"));
48 }
49 
51 {
52  delete2(log)
53 }
54 
56 {
57  mConfig.init(settings.configDir + "/test.xml",
60  mConfig.clear();
61 // setConfigDefaults(mConfig);
62 
63  mConfig.setValue("hwaccel", false);
64  mConfig.setValue("screen", false);
65  mConfig.setValue("sound", false);
66  mConfig.setValue("guialpha", 0.8F);
67 // mConfig.setValue("remember", true);
68  mConfig.setValue("sfxVolume", 50);
69  mConfig.setValue("musicVolume", 60);
70  mConfig.setValue("fpslimit", 0);
71  mConfig.setValue("customcursor", true);
72  mConfig.setValue("useScreenshotDirectorySuffix", true);
73  mConfig.setValue("ChatLogLength", 128);
74  mConfig.setValue("screenwidth", 800);
75  mConfig.setValue("screenheight", 600);
76 }
77 
78 int TestMain::exec(const bool testAudio)
79 {
80  initConfig();
81  int softwareTest = invokeSoftwareRenderTest("1");
82  int soundTest = -1;
83  int rescaleTest[6];
84  int softFps = 0;
85  int normalOpenGLFps = 0;
86  int safeOpenGLFps = 0;
87  int modernOpenGLFps = 0;
88  int textureSize[6];
89  std::string textureSizeStr;
90 
92  int detectMode = 0;
93  for (int f = 0; f < 6; f ++)
94  {
95  rescaleTest[f] = -1;
96  textureSize[f] = 1024;
97  }
98  std::string info;
99 
100  const int videoDetectTest = invokeTest("99");
101  if (videoDetectTest == 0)
102  detectMode = readValue2(99);
103 
104  int normalOpenGLTest = invokeNormalOpenGLRenderTest("2");
105  int safeOpenGLTest = invokeSafeOpenGLRenderTest("3");
106  int modernOpenGLTest = invokeModernOpenGLRenderTest("19");
107  if (testAudio)
108  soundTest = invokeTest4();
109  else
110  soundTest = 0;
111 
112  info.append(strprintf("%d.%d,%d,%d,%d.", soundTest, softwareTest,
113  normalOpenGLTest, safeOpenGLTest, modernOpenGLTest));
114 
115  if (softwareTest == 0)
116  {
117  int softFpsTest = invokeSoftwareRenderTest("8");
118  info.append(strprintf("%d", softFpsTest));
119  if (softFpsTest == 0)
120  {
121  softFps = readValue2(8);
122  info.append(strprintf(",%d", softFps));
123  if (softFps == 0)
124  {
125  softwareTest = -1;
126  softFpsTest = -1;
127  }
128  else
129  {
130  rescaleTest[0] = invokeSoftwareRenderTest("5");
131  info.append(strprintf(",%d", rescaleTest[0]));
132  }
133  }
134  else
135  {
136  softwareTest = -1;
137  }
138  }
139  info.append(".");
140  if (modernOpenGLTest == 0)
141  {
142  int modernOpenGLFpsTest = invokeModernOpenGLRenderTest("17");
143  info.append(strprintf("%d", modernOpenGLFpsTest));
144  if (modernOpenGLFpsTest == 0)
145  {
146  modernOpenGLFps = readValue2(17);
147  info.append(strprintf(",%d", modernOpenGLFps));
148  if (modernOpenGLFps == 0)
149  {
150  modernOpenGLTest = -1;
151  modernOpenGLFpsTest = -1;
152  }
153  else
154  {
155  rescaleTest[3] = invokeModernOpenGLRenderTest("18");
156  info.append(strprintf(",%d", rescaleTest[3]));
157  }
158  }
159  else
160  {
161  modernOpenGLTest = -1;
162  }
163  }
164  info.append(".");
165  if (normalOpenGLTest == 0)
166  {
167  int normalOpenGLFpsTest = invokeNormalOpenGLRenderTest("9");
168  info.append(strprintf("%d", normalOpenGLFpsTest));
169  if (normalOpenGLFpsTest == 0)
170  {
171  normalOpenGLFps = readValue2(9);
172  info.append(strprintf(",%d", normalOpenGLFps));
173  if (normalOpenGLFps == 0)
174  {
175  normalOpenGLTest = -1;
176  normalOpenGLFpsTest = -1;
177  }
178  else
179  {
180  rescaleTest[1] = invokeNormalOpenGLRenderTest("6");
181  info.append(strprintf(",%d", rescaleTest[1]));
182  }
183  }
184  else
185  {
186  normalOpenGLTest = -1;
187  }
188  }
189  info.append(".");
190  if (safeOpenGLTest == 0)
191  {
192  int safeOpenGLFpsTest = invokeSafeOpenGLRenderTest("10");
193  info.append(strprintf("%d", safeOpenGLFpsTest));
194  if (safeOpenGLFpsTest == 0)
195  {
196  safeOpenGLFps = readValue2(10);
197  info.append(strprintf(",%d", safeOpenGLFps));
198  if (safeOpenGLFps == 0)
199  {
200  safeOpenGLTest = -1;
201  safeOpenGLFpsTest = -1;
202  }
203  else
204  {
205  rescaleTest[2] = invokeSafeOpenGLRenderTest("7");
206  info.append(strprintf(",%d", rescaleTest[2]));
207  }
208  }
209  else
210  {
211  safeOpenGLTest = -1;
212  }
213  }
214  info.append(".");
215 
216  int maxFps = softFps;
217  if (maxFps < normalOpenGLFps)
218  {
220  maxFps = normalOpenGLFps;
221  }
222  if (maxFps < modernOpenGLFps)
223  {
225  maxFps = modernOpenGLFps;
226  }
227  if (maxFps < safeOpenGLFps)
228  {
230  maxFps = safeOpenGLFps;
231  }
232 
233  int batchSize = 256;
234 
235  if (invokeNormalOpenBatchTest("11") == 0)
236  batchSize = readValue2(11);
237  if (batchSize < 256)
238  batchSize = 256;
239 
240  if (invokeNormalOpenBatchTest("14") == 0)
241  {
242  textureSize[CAST_SIZE(RENDER_NORMAL_OPENGL)]
243  = readValue2(14);
244  }
245  if (invokeModernOpenBatchTest("15") == 0)
246  {
247  textureSize[CAST_SIZE(RENDER_MODERN_OPENGL)]
248  = readValue2(15);
249  }
250  if (invokeSafeOpenBatchTest("16") == 0)
251  {
252  textureSize[CAST_SIZE(RENDER_SAFE_OPENGL)]
253  = readValue2(16);
254  }
255  if (invokeMobileOpenBatchTest("20") == 0)
256  {
257  textureSize[CAST_SIZE(RENDER_GLES_OPENGL)]
258  = readValue2(20);
259  }
260  for (int f = 0; f < 6; f ++)
261  info.append(strprintf(",%d", textureSize[f]));
262  info.append(",-");
263 
264  textureSizeStr = toString(textureSize[0]);
265  for (int f = 1; f < 6; f ++)
266  textureSizeStr.append(strprintf(",%d", textureSize[f]));
267 
268  // if OpenGL implimentation is not good, disable it.
269  if ((detectMode & 15) == 0)
271 
273  soundTest, info, batchSize, textureSizeStr, detectMode);
274  return 0;
275 }
276 
278  const int rescale,
279  const int sound,
280  const std::string &info,
281  const int batchSize A_UNUSED,
282  const std::string &textureSize,
283  const int detectMode)
284 {
285  mConfig.init(settings.configDir + "/config.xml",
288 
289  log->log("set mode to %d", CAST_S32(openGLMode));
290 
291  // searched values
292  mConfig.setValue("opengl", CAST_S32(openGLMode));
293  mConfig.setValue("showBackground", rescale == 0);
294  mConfig.setValue("sound", sound == 0);
295 
296  // better performance
297  mConfig.setValue("hwaccel", true);
298  mConfig.setValue("fpslimit", 60);
299  mConfig.setValue("altfpslimit", 2);
300  mConfig.setValue("safemode", false);
301  mConfig.setValue("enableMapReduce", true);
302  mConfig.setValue("textureSize", textureSize);
303 
304  // max batch size
305 // mConfig.setValue("batchsize", batchSize);
306 
307  // additional modes
308  mConfig.setValue("useTextureSampler",
309  static_cast<bool>(detectMode & 1024));
310  mConfig.setValue("compresstextures",
311  static_cast<bool>(detectMode & 2048));
312 
313  // stats
314  mConfig.setValue("testInfo", info);
315 
316  mConfig.write();
317 }
318 
319 int TestMain::readValue2(const int ver)
320 {
321  const int def = readValue(ver, 0);
322  log->log("value for %d = %d", ver, def);
323  return def;
324 }
325 
326 int TestMain::readValue(const int ver, int def)
327 {
328  std::string tmp;
329  int var;
330  std::ifstream file;
331  file.open(pathJoin(settings.localDataDir, "test.log").c_str(),
332  std::ios::in);
333  if (!getline(file, tmp))
334  {
335  file.close();
336  return def;
337  }
338  var = atoi(tmp.c_str());
339  if (ver != var || !getline(file, tmp))
340  {
341  file.close();
342  return def;
343  }
344  def = atoi(tmp.c_str());
345  file.close();
346  return def;
347 }
348 
349 int TestMain::invokeTest(const std::string &test)
350 {
352 
353  mConfig.write();
354  const int ret = execFileWait(fileName, fileName, "-t", test, 0);
355  return ret;
356 }
357 
359 {
360  mConfig.setValue("sound", true);
361  const int ret = invokeTest("4");
362 
363  log->log("4: %d", ret);
364  return ret;
365 }
366 
367 int TestMain::invokeSoftwareRenderTest(const std::string &test)
368 {
370  mConfig.write();
371  const int ret = execFileWait(fileName, fileName, "-t", test, 30);
372  log->log("%s: %d", test.c_str(), ret);
373  return ret;
374 }
375 
376 int TestMain::invokeNormalOpenGLRenderTest(const std::string &test)
377 {
379  mConfig.write();
380  const int ret = execFileWait(fileName, fileName, "-t", test, 30);
381  log->log("%s: %d", test.c_str(), ret);
382  return ret;
383 }
384 
385 int TestMain::invokeModernOpenGLRenderTest(const std::string &test)
386 {
388  mConfig.write();
389  const int ret = execFileWait(fileName, fileName, "-t", test, 30);
390  log->log("%s: %d", test.c_str(), ret);
391  return ret;
392 }
393 
394 int TestMain::invokeNormalOpenBatchTest(const std::string &test)
395 {
397  mConfig.write();
398  const int ret = execFileWait(fileName, fileName, "-t", test, 30);
399 // log->log("%s: %d", test.c_str(), ret);
400  return ret;
401 }
402 
403 int TestMain::invokeModernOpenBatchTest(const std::string &test)
404 {
406  mConfig.write();
407  const int ret = execFileWait(fileName, fileName, "-t", test, 30);
408 // log->log("%s: %d", test.c_str(), ret);
409  return ret;
410 }
411 
412 int TestMain::invokeMobileOpenBatchTest(const std::string &test)
413 {
415  mConfig.write();
416  const int ret = execFileWait(fileName, fileName, "-t", test, 30);
417 // log->log("%s: %d", test.c_str(), ret);
418  return ret;
419 }
420 
421 int TestMain::invokeSafeOpenBatchTest(const std::string &test)
422 {
424  mConfig.write();
425  const int ret = execFileWait(fileName, fileName, "-t", test, 30);
426 // log->log("%s: %d", test.c_str(), ret);
427  return ret;
428 }
429 
430 int TestMain::invokeSafeOpenGLRenderTest(const std::string &test)
431 {
433  mConfig.write();
434  const int ret = execFileWait(fileName, fileName, "-t", test, 30);
435  log->log("%s: %d", test.c_str(), ret);
436  return ret;
437 }
438 #endif // USE_OPENGL
#define CAST_S32
Definition: cast.h:30
#define CAST_SIZE
Definition: cast.h:34
void setValue(const std::string &key, const std::string &value)
void init(const std::string &filename, const UseVirtFs useResManager, const SkipError skipError)
Definition: logger.h:69
void log(const char *const log_text,...)
Definition: logger.cpp:269
void setLogFile(const std::string &logFilename)
Definition: logger.cpp:120
std::string localDataDir
Definition: settings.h:112
std::string configDir
Definition: settings.h:111
Configuration mConfig
Definition: testmain.h:90
int invokeMobileOpenBatchTest(const std::string &test)
Definition: testmain.cpp:412
void initConfig()
Definition: testmain.cpp:55
static int readValue(const int ver, int def)
Definition: testmain.cpp:326
int invokeNormalOpenGLRenderTest(const std::string &test)
Definition: testmain.cpp:376
int readValue2(const int ver)
Definition: testmain.cpp:319
int exec(const bool testAudio=true)
Definition: testmain.cpp:78
TestMain()
Definition: testmain.cpp:42
Logger * log
Definition: testmain.h:88
int invokeModernOpenGLRenderTest(const std::string &test)
Definition: testmain.cpp:385
int invokeTest(const std::string &test)
Definition: testmain.cpp:349
int invokeSafeOpenGLRenderTest(const std::string &test)
Definition: testmain.cpp:430
int invokeSoftwareRenderTest(const std::string &test)
Definition: testmain.cpp:367
void writeConfig(const RenderType openGLMode, const int rescale, const int sound, const std::string &info, const int batchSize, const std::string &textureSize, const int detectMode)
Definition: testmain.cpp:277
~TestMain()
Definition: testmain.cpp:50
int invokeModernOpenBatchTest(const std::string &test)
Definition: testmain.cpp:403
int invokeTest4()
Definition: testmain.cpp:358
int invokeNormalOpenBatchTest(const std::string &test)
Definition: testmain.cpp:394
int invokeSafeOpenBatchTest(const std::string &test)
Definition: testmain.cpp:421
#define new
Definition: debug_new.h:147
#define delete2(var)
Definition: delete2.h:25
RenderType openGLMode
#define A_UNUSED
Definition: localconsts.h:160
bool info(InputEvent &event)
Definition: commands.cpp:57
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
std::string getSelfName()
Definition: paths.cpp:200
int execFileWait(const std::string &pathName, const std::string &name, const std::string &arg1, const std::string &arg2, int waitTime)
Definition: process.cpp:231
RenderType
Definition: rendertype.h:26
@ RENDER_SAFE_OPENGL
Definition: rendertype.h:29
@ RENDER_GLES_OPENGL
Definition: rendertype.h:30
@ RENDER_MODERN_OPENGL
Definition: rendertype.h:32
@ RENDER_NORMAL_OPENGL
Definition: rendertype.h:28
@ RENDER_SOFTWARE
Definition: rendertype.h:27
Settings settings
Definition: settings.cpp:32
const bool SkipError_false
Definition: skiperror.h:30
std::string strprintf(const char *const format,...)
std::string pathJoin(std::string str1, const std::string &str2)
char * selfName
Definition: maingui.cpp:135
std::string fileName
Definition: testmain.cpp:39
const bool UseVirtFs_false
Definition: usevirtfs.h:30