summaryrefslogtreecommitdiffstats
path: root/plugins/multimedia/symbian/videooutput/s60eglendpoint.cpp
blob: 90e25b28cbb46d4a5b72e7c8c31e4990be933341 (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
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** 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, Nokia gives you certain additional
** rights. These rights are described in the Nokia 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.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "s60eglendpoint.h"
#include "s60eglextensions.h"
#include "s60mmtrace.h"
#include <e32base.h>
#include <egl/egl.h>
#include <graphics/surface.h>
#include <QtCore/QDebug>
#include <QtCore/QTime>

//-----------------------------------------------------------------------------
// S60EglEndpointEventHandler
//-----------------------------------------------------------------------------

class S60EglEndpointEventHandler : public CActive
{
public:
    static S60EglEndpointEventHandler *NewL(S60EglEndpoint *parent);
    ~S60EglEndpointEventHandler();
    void request();

private:
    S60EglEndpointEventHandler(S60EglEndpoint *parent);
    void RunL();
    void DoCancel();

private:
    S60EglEndpoint *const m_parent;
};

S60EglEndpointEventHandler *S60EglEndpointEventHandler::NewL(S60EglEndpoint *parent)
{
    S60EglEndpointEventHandler *self = new (ELeave) S60EglEndpointEventHandler(parent);
    CActiveScheduler::Add(self);
    return self;
}

S60EglEndpointEventHandler::S60EglEndpointEventHandler(S60EglEndpoint *parent)
    : CActive(EPriorityStandard)
    , m_parent(parent)
{

}

S60EglEndpointEventHandler::~S60EglEndpointEventHandler()
{
    Cancel();
}

void S60EglEndpointEventHandler::request()
{
    TRACE("S60EglEndpointEventHandler::request" << qtThisPtr());
    Q_ASSERT(!IsActive());
    iStatus = KRequestPending;
    EGLTRequestStatusNOK eglStatus = reinterpret_cast<EGLTRequestStatusNOK>(&iStatus);
    const EGLBoolean ret = m_parent->m_extensions->endpointRequestNotification(m_parent->m_display,
                                                                               m_parent->m_endpoint,
                                                                               eglStatus);
    if (ret) {
        VERBOSE_TRACE("S60EglEndpointEventHandler::request" << qtThisPtr() << "endpointRequestNotification OK");
        SetActive();
    } else {
        qWarning() << "S60EglEndpointEventHandler::request" << qtThisPtr() << "endpointRequestNotification failed";
    }
}

void S60EglEndpointEventHandler::RunL()
{
    VERBOSE_TRACE("S60EglEndpointEventHandler::RunL" << qtThisPtr());
    m_parent->endpointEvent();
}

void S60EglEndpointEventHandler::DoCancel()
{
    TRACE("S60EglEndpointEventHandler::DoCancel" << qtThisPtr());
    m_parent->m_extensions->endpointCancelNotification(m_parent->m_display,
                                                       m_parent->m_endpoint);

    if (iStatus == KRequestPending) {
        // If EGL display gets destroyed before cancel then endpointCancelNotification
        // will not cancel our request
        TRACE("S60EglEndpointEventHandler::DoCancel - completing request locally" << qtThisPtr());
        TRequestStatus *requestStatus = &iStatus;
        User::RequestComplete(requestStatus, KErrCancel);
    }
}

//-----------------------------------------------------------------------------
// S60EglEndpoint
//-----------------------------------------------------------------------------

S60EglEndpoint::S60EglEndpoint(const TSurfaceId &surface,
                               S60EglExtensions *extensions,
                               QObject *parent)
    : QObject(parent)
    , m_extensions(extensions)
    , m_context(eglGetCurrentContext())
    , m_api(0)
    , m_display(eglGetCurrentDisplay())
    , m_endpoint(EGL_NO_ENDPOINT_NOK)
    , m_state(Null)
{
    TRACE("S60EglEndpoint::S60EglEndpoint" << qtThisPtr()
          << "surface" << (void*)surface.iInternal[3]
          << "context" << m_context << "display" << m_display);
    Q_ASSERT(m_extensions);
    bool ok = false;
    if (m_context) {
        m_api = eglQueryAPI();
        if (m_api) {
            m_display = eglGetCurrentDisplay();
            if (EGL_NO_DISPLAY != m_display) {
                ok = true;
            } else {
                qWarning() << "S60EglEndpoint::S60EglEndpoint" << qtThisPtr() << "failed to get valid EGL display";
            }
        } else {
            qWarning() << "S60EglEndpoint::S60EglEndpoint" << qtThisPtr() << "failed to get valid rendering API";
        }
    } else {
        qWarning() << "S60EglEndpoint::S60EglEndpoint" << qtThisPtr() << "failed to get valid EGL context";
    }
    if (ok) {
        static const EGLenum type = EGL_ENDPOINT_TYPE_CONSUMER_NOK;
        static const EGLenum sourceType = EGL_TSURFACEID_NOK;
        TSurfaceId localSurface = surface;
        EGLEndpointSourceNOK source = reinterpret_cast<EGLEndpointSourceNOK>(&localSurface);
        static const EGLint *attributes = 0;
        m_endpoint = m_extensions->createEndpoint(m_display, type, sourceType, source, attributes);
        const EGLint error = eglGetError();
        if (EGL_SUCCESS == error) {
            TRACE("S60EglEndpoint::S60EglEndpoint" << qtThisPtr()
                  << "endpoint" << m_endpoint);
            if (EGL_NO_ENDPOINT_NOK == m_endpoint) {
                qWarning() << "S60EglEndpoint::S60EglEndpoint" << qtThisPtr() << "failed to create EGL endpoint";
            } else {
                TRAPD(err, m_eventHandler.reset(S60EglEndpointEventHandler::NewL(this)));
                if (err) {
                    qWarning() << "S60EglEndpoint::S60EglEndpoint" << qtThisPtr() << "failed to create EGL endpoint event handler: error" << err;
                } else {
                    m_state = Created;
                    EGLBoolean ready = m_extensions->getEndpointAttrib(m_display, m_endpoint, EGL_ENDPOINT_READY_NOK);
                    TRACE("S60EglEndpoint::S60EglEndpoint" << qtThisPtr() << "ready" << ready);
                    if (ready) {
                        // Producer has already submitted content to the source surface
                        m_state = Active;
                        m_eventHandler->request();
                    } else {
                        // Request notification when producer has submitted content
                        m_eventHandler->request();
                    }
                }
            }
        } else {
            qWarning() << "S60EglEndpoint::S60EglEndpoint" << qtThisPtr() << "failed to create EGL endpoint: error" << error;
        }
    }
}

S60EglEndpoint::~S60EglEndpoint()
{
    TRACE("S60EglEndpoint::~S60EglEndpoint" << qtThisPtr());
    if (EGL_NO_ENDPOINT_NOK != m_endpoint)
        m_extensions->destroyEndpoint(m_display, m_endpoint);
}

void S60EglEndpoint::endpointEvent()
{
    const EGLBoolean ready = m_extensions->getEndpointAttrib(m_display, m_endpoint, EGL_ENDPOINT_READY_NOK);
    VERBOSE_TRACE("S60EglEndpoint::endpointEvent" << qtThisPtr() << "state" << m_state << "ready" << ready);
    switch (m_state) {
    case Created:
        m_state = Active;
    case Active:
        emit imageAvailable();
        break;
    case Null:
    default:
        Q_ASSERT(false);
        break;
    }
}

bool S60EglEndpoint::isValid() const
{
    return (m_state != Null);
}

void S60EglEndpoint::setDelay(qint64 delay)
{
    VERBOSE_TRACE("S60EglEndpoint::setDelay" << qtThisPtr() << "delay" << delay);
    m_extensions->setEndpointAttrib(m_display, m_endpoint, EGL_DELAY_NOK, delay);
}

EGLImageKHR S60EglEndpoint::acquireImage()
{
    Q_ASSERT(EGL_NO_IMAGE_KHR == m_image);
    m_image = m_extensions->acquireImage(m_display, m_endpoint);
    VERBOSE_TRACE("S60EglEndpoint::acquireImage" << qtThisPtr() << "image" << m_image);
    if (EGL_NO_IMAGE_KHR == m_image) {
        qWarning() << "S60EglEndpoint::endpointEvent" << qtThisPtr() << "null image";
        if (!m_eventHandler->IsActive())
            m_eventHandler->request();
    }
    return m_image;
}

void S60EglEndpoint::releaseImage()
{
    if (EGL_NO_IMAGE_KHR != m_image) {
        VERBOSE_TRACE("S60EglEndpoint::releaseImage" << qtThisPtr() << "image" << m_image);
        m_extensions->releaseImage(m_display, m_endpoint, m_image, m_api);
        m_image = EGL_NO_IMAGE_KHR;
        if (!m_eventHandler->IsActive())
            m_eventHandler->request();
    }
}