GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/test/testmain.cpp Lines: 2 208 1.0 %
Date: 2017-11-29 Branches: 0 382 0.0 %

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