-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageSource.cpp
More file actions
151 lines (119 loc) · 3.03 KB
/
ImageSource.cpp
File metadata and controls
151 lines (119 loc) · 3.03 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "ImageSource.h"
#include <QQuickRenderControl>
#include <QOffscreenSurface>
#include <QQuickWindow>
#include <QOpenGLFramebufferObject>
#include <QOpenGLContext>
#include <QQuickItem>
ImageSource::ImageSource(QObject* parent)
: QObject(parent)
, m_renderControl(new QQuickRenderControl(this))
, m_offscreenSurface(nullptr)
, m_quickWindow(new QQuickWindow(m_renderControl))
, m_fbo(nullptr)
, m_initialized(false)
{
connect(m_quickWindow, &QQuickWindow::sceneGraphInitialized , [this]
{
createFbo(m_quickWindow->size());
});
connect(m_quickWindow, &QQuickWindow::sceneGraphInvalidated, this, &ImageSource::destroyFbo);
}
ImageSource::~ImageSource()
{
clear();
}
void ImageSource::init(QOpenGLContext* glContext, QQuickItem* rootItem, int width, int height)
{
m_glContext = glContext;
m_offscreenSurface = new QOffscreenSurface;
m_offscreenSurface->setFormat(m_glContext->format());
m_offscreenSurface->create();
m_glContext->makeCurrent(m_offscreenSurface);
m_renderControl->initialize(m_glContext);
m_rootItem = rootItem;
m_rootItem->setParentItem(m_quickWindow->contentItem());
m_initialized = true;
resize(width, height);
render();
}
void ImageSource::clear()
{
if (m_glContext)
{
m_glContext->makeCurrent(m_offscreenSurface);
delete m_renderControl;
delete m_quickWindow;
delete m_fbo;
m_glContext->doneCurrent();
delete m_offscreenSurface;
}
}
void ImageSource::render()
{
if (m_glContext->makeCurrent(m_offscreenSurface))
{
m_renderControl->polishItems();
m_renderControl->sync();
m_renderControl->render();
#if 0
m_quickWindow->resetOpenGLState();
QOpenGLFramebufferObject::bindDefault();
m_glContext->functions()->glFlush();
#endif
emit newImageAvailable();
}
}
void ImageSource::resize(int width, int height)
{
if (m_rootItem && m_glContext && m_glContext->makeCurrent(m_offscreenSurface))
{
QSize size(width, height);
createFbo(size);
m_glContext->doneCurrent();
m_rootItem->setSize(size);
m_quickWindow->setGeometry(0, 0, width, height);
}
else
qWarning("failed to resize");
}
QQuickWindow* ImageSource::window() const
{
return m_quickWindow;
}
QQuickItem* ImageSource::rootObject() const
{
return m_rootItem;
}
QOpenGLFramebufferObject* ImageSource::fbo() const
{
return m_fbo;
}
int ImageSource::width() const
{
return m_fbo ? m_fbo->width() : -1;
}
int ImageSource::height() const
{
return m_fbo ? m_fbo->height() : -1;
}
void ImageSource::createFbo(const QSize& size)
{
destroyFbo();
if (m_initialized)
{
m_fbo = new QOpenGLFramebufferObject(size);
m_quickWindow->setRenderTarget(m_fbo);
emit fboCreated(m_fbo);
}
}
void ImageSource::destroyFbo()
{
if (m_fbo)
{
delete m_fbo;
m_fbo = nullptr;
m_quickWindow->setRenderTarget(nullptr);
emit fboDestroyed();
}
}