GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/test/testmain.cpp Lines: 2 213 0.9 %
Date: 2018-12-09 Branches: 0 294 0.0 %

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