summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/openwfd/qopenwfddevice.cpp
blob: 4b136b64646b1b11d093cebda921775f8712fa8f (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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qopenwfddevice.h"

#include "qopenwfdport.h"
#include "qopenwfdscreen.h"

#include <QtCore/QDebug>

#include <WF/wfdext.h>
#include <gbm.h>

QOpenWFDDevice::QOpenWFDDevice(QOpenWFDIntegration *integration, WFDint device_enumeration)
    : mIntegration(integration)
    , mDeviceEnum(device_enumeration)
    , mCommitedDevice(false)
    , mWaitingForBindSourceEvent(false)
{
    mDevice = wfdCreateDevice(WFD_DEFAULT_DEVICE_ID,WFD_NONE);
    if (mDevice == WFD_INVALID_HANDLE)
        qDebug() << "failed to create device";

    mEvent = wfdCreateEvent(mDevice,0);
    if (mEvent == WFD_INVALID_HANDLE)
        qDebug() << "failed to create event handle";

    //initialize pipelines for device.
    wfdEnumeratePipelines(mDevice,WFD_NONE,0,WFD_NONE);

    initializeGbmAndEgl();

    WFDint numberOfPorts = wfdEnumeratePorts(mDevice,0,0,0);
    WFDint port_enumerations[numberOfPorts];
    WFDint actualNumberOfPorts = wfdEnumeratePorts(mDevice,port_enumerations,numberOfPorts,WFD_NONE);
    Q_ASSERT(actualNumberOfPorts == numberOfPorts);

    for (int i = 0; i < actualNumberOfPorts; i++)
    {
        QOpenWFDPort *port = new QOpenWFDPort(this,port_enumerations[i]);
        if (port->attached()) {
            mPorts.append(port);
        } else {
            delete port;
        }
    }

    int fd = wfdDeviceEventGetFD(mDevice,mEvent);
    mEventSocketNotifier = new QSocketNotifier(fd,QSocketNotifier::Read,this);
    connect(mEventSocketNotifier,SIGNAL(activated(int)),SLOT(readEvents()));

    mCommitedDevice = true;
    commit(WFD_COMMIT_ENTIRE_DEVICE, handle());
}

QOpenWFDDevice::~QOpenWFDDevice()
{
    delete mEventSocketNotifier;
    wfdDestroyEvent(mDevice,mEvent);

    for (int i = 0; i < mPorts.size(); i++) {
        //probably don't need to remove them from the list
        QList <WFDint> keys = mUsedPipelines.keys(mPorts.at(i));
        for (int keyIndex = 0; keyIndex < keys.size(); keyIndex++) {
            mUsedPipelines.remove(keys.at(keyIndex));
        }
        //but we have to delete them :)
        delete mPorts[i];
    }

    eglDestroyContext(mEglDisplay,mEglContext);
    eglTerminate(mEglDisplay);

    gbm_device_destroy(mGbmDevice);

    wfdDestroyDevice(mDevice);
}

WFDDevice QOpenWFDDevice::handle() const
{
    return mDevice;
}

QOpenWFDIntegration * QOpenWFDDevice::integration() const
{
    return mIntegration;
}

bool QOpenWFDDevice::isPipelineUsed(WFDint pipelineId)
{
    return mUsedPipelines.contains(pipelineId);
}

void QOpenWFDDevice::addToUsedPipelineSet(WFDint pipelineId,QOpenWFDPort *port)
{
    mUsedPipelines.insert(pipelineId,port);
}

void QOpenWFDDevice::removeFromUsedPipelineSet(WFDint pipelineId)
{
    mUsedPipelines.remove(pipelineId);
}

gbm_device * QOpenWFDDevice::gbmDevice() const

{
    return mGbmDevice;
}

EGLDisplay QOpenWFDDevice::eglDisplay() const
{
    return mEglDisplay;
}

EGLContext QOpenWFDDevice::eglContext() const
{
    return mEglContext;
}

void QOpenWFDDevice::commit(WFDCommitType type, WFDHandle handle)
{
    if (mCommitedDevice) {
        wfdDeviceCommit(mDevice,type,handle);
    }
}

void QOpenWFDDevice::waitForPipelineBindSourceCompleteEvent()
{
    mWaitingForBindSourceEvent = true;

    while (mWaitingForBindSourceEvent) {
        readEvents(WFD_FOREVER);
    }
}

void QOpenWFDDevice::readEvents(WFDtime wait)
{
    WFDEventType type = wfdDeviceEventWait(mDevice,mEvent,wait);

    if (type == WFD_EVENT_NONE || type == WFD_EVENT_DESTROYED) {
        return;
    }
    switch (type) {
    case WFD_EVENT_INVALID:
    case WFD_EVENT_NONE:
        return;
    case WFD_EVENT_DESTROYED:
        qDebug() << "Event or Device destoryed!";
        return;
    case WFD_EVENT_PORT_ATTACH_DETACH:
        handlePortAttachDetach();
        break;
    case WFD_EVENT_PORT_PROTECTION_FAILURE:
        qDebug() << "Port protection event handling not implemented";
        break;
    case WFD_EVENT_PIPELINE_BIND_SOURCE_COMPLETE:
        handlePipelineBindSourceComplete();
        break;
    case WFD_EVENT_PIPELINE_BIND_MASK_COMPLETE:
        qDebug() << "Pipeline bind mask event handling not implemented";
        break;
    default:
        qDebug() << "Not recognised event type";
        break;
    }


}

void QOpenWFDDevice::initializeGbmAndEgl()
{

    qDebug() << "initializing GBM and EGL";
    int fd = wfdGetDeviceAttribi(mDevice,WFD_DEVICE_ID);
    if (fd < 0) {
        qDebug() << "failed to get WFD_DEVICE_ID";
    }

    mGbmDevice = gbm_create_device(fd);

    setenv("EGL_PLATFORM", "drm",1);

    mEglDisplay = eglGetDisplay(mGbmDevice);

    EGLint minor, major;

    if (!eglInitialize(mEglDisplay,&major,&minor)) {
        qDebug() << "failed to initialize egl";
    }

    QByteArray eglExtensions = eglQueryString(mEglDisplay, EGL_EXTENSIONS);
    if (!eglExtensions.contains("EGL_KHR_surfaceless_opengl")) {
        qDebug() << "This egl implementation does not have the required EGL extension EGL_KHR_surfaceless_opengl";
    }

    eglBindAPI(EGL_OPENGL_ES_API);

    EGLint contextAttribs[] = {
        EGL_CONTEXT_CLIENT_VERSION, 2,
        EGL_NONE
    };

    mEglContext = eglCreateContext(mEglDisplay,NULL,EGL_NO_CONTEXT,contextAttribs);
    if (mEglContext == EGL_NO_CONTEXT) {
        qDebug() << "Failed to create EGL context";
    }

    eglCreateImage = (PFNEGLCREATEIMAGEKHRPROC) eglGetProcAddress("eglCreateImageKHR");
    if (!eglCreateImage) {
        qWarning("failed to load extension eglCreateImageKHR");
    }

    eglDestroyImage = (PFNEGLDESTROYIMAGEKHRPROC) eglGetProcAddress("eglDestroyImageKHR");
    if (!eglDestroyImage) {
        qWarning("failed to load extension eglDestoryImageKHR");
    }

    glEglImageTargetRenderBufferStorage = (PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC) eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
    if (!glEglImageTargetRenderBufferStorage) {
        qWarning("failed to load extension glEGLImageTargetRenderbufferStorageOES");
    }
}

void QOpenWFDDevice::handlePortAttachDetach()
{
    WFDint id = wfdGetEventAttribi(mDevice,mEvent,WFD_EVENT_PORT_ATTACH_PORT_ID);
    if (id == WFD_INVALID_PORT_ID)
        return;

    WFDint attachState = wfdGetEventAttribi(mDevice,mEvent,WFD_EVENT_PORT_ATTACH_STATE);
    if (attachState == WFD_TRUE) {
        int indexToAdd = -1;
        for (int i = 0; i < mPorts.size(); i++) {
            if (mPorts.at(i)->portId() == id) {
                indexToAdd = i;
                qDebug() << "found index to attach";
                break;
            }
        }
        if (indexToAdd >= 0) {
            mPorts[indexToAdd]->attach();
        } else {
            mPorts.append(new QOpenWFDPort(this,id));
        }

    } else {
        int indexToDelete = -1;
        for (int i = 0; i < mPorts.size(); i++) {
            if (mPorts.at(i)->portId() == id) {
                indexToDelete = i;
                break;
            }
        }
        if (indexToDelete >= 0) {
            QOpenWFDPort *portToDelete = mPorts.at(indexToDelete);
            mPorts.removeAt(indexToDelete);
            delete portToDelete;
        }
    }
}

void QOpenWFDDevice::handlePipelineBindSourceComplete()
{
    mWaitingForBindSourceEvent = false;

    WFDint overflow = wfdGetEventAttribi(mDevice,mEvent, WFD_EVENT_PIPELINE_BIND_QUEUE_OVERFLOW);
    if (overflow == WFD_TRUE) {
        qDebug() << "PIPELINE_BIND_QUEUE_OVERFLOW event occurred";
    }

    WFDint pipelineId = wfdGetEventAttribi(mDevice,mEvent,WFD_EVENT_PIPELINE_BIND_PIPELINE_ID);
    for (int i = 0; i < mPorts.size(); i++) {
        if (pipelineId != WFD_INVALID_PIPELINE_ID && mUsedPipelines.contains(pipelineId)) {
            QOpenWFDPort *port = mUsedPipelines.value(pipelineId);
            port->screen()->pipelineBindSourceComplete();
            break;
        }
    }
}