Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test_main_loop.cpp « qt_tstfrm - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2360cd1221913224dc4e87c455b616254abe3bcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "qt_tstfrm/test_main_loop.hpp"

#include <QtCore/QTimer>
#include <QtWidgets/QApplication>
#include <QtWidgets/QWidget>
#include <QtGui/QOffscreenSurface>
#include <QtGui/QOpenGLContext>

#include "base/scope_guard.hpp"

#include <cstring>
#include <memory>
#include <vector>

namespace
{
class MyWidget : public QWidget
{
public:
  explicit MyWidget(RenderFunction && fn)
    : m_fn(std::move(fn))
  {}

protected:
  void paintEvent(QPaintEvent * e) override
  {
    m_fn(this);
  }

private:
  RenderFunction m_fn;
};
}  // namespace

void RunTestLoop(char const * testName, RenderFunction && fn, bool autoExit)
{
  std::vector<char> buf(strlen(testName) + 1);
  strcpy(buf.data(), testName);
  char * raw = buf.data();

  int argc = 1;
  QApplication app(argc, &raw);
  if (autoExit)
    QTimer::singleShot(3000, &app, SLOT(quit()));

  auto widget = std::make_unique<MyWidget>(std::move(fn));
  widget->setWindowTitle(testName);
  widget->show();

  app.exec();
}

void RunTestInOpenGLOffscreenEnvironment(char const * testName, bool apiOpenGLES3,
                                         TestFunction const & fn)
{
  std::vector<char> buf(strlen(testName) + 1);
  strcpy(buf.data(), testName);
  char * raw = buf.data();

  int argc = 1;
  QApplication app(argc, &raw);

  QSurfaceFormat fmt;
  fmt.setAlphaBufferSize(8);
  fmt.setBlueBufferSize(8);
  fmt.setGreenBufferSize(8);
  fmt.setRedBufferSize(8);
  fmt.setStencilBufferSize(0);
  fmt.setSamples(0);
  fmt.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
  fmt.setSwapInterval(1);
  fmt.setDepthBufferSize(16);
  if (apiOpenGLES3)
  {
    fmt.setProfile(QSurfaceFormat::CoreProfile);
    fmt.setVersion(3, 2);
  }
  else
  {
    fmt.setProfile(QSurfaceFormat::CompatibilityProfile);
    fmt.setVersion(2, 1);
  }

  auto surface = std::make_unique<QOffscreenSurface>();
  surface->setFormat(fmt);
  surface->create();

  auto context = std::make_unique<QOpenGLContext>();
  context->setFormat(fmt);
  context->create();
  context->makeCurrent(surface.get());

  if (fn)
    fn(apiOpenGLES3);

  context->doneCurrent();
  surface->destroy();

  QTimer::singleShot(0, &app, SLOT(quit()));
  app.exec();
}