Update spec to build Qt 5.0
[profile/ivi/qtbase.git] / src / plugins / platforms / openwfd / qopenwfdport.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qopenwfdport.h"
43
44 #include "qopenwfdportmode.h"
45 #include "qopenwfdscreen.h"
46
47 #include <QtCore/QDebug>
48
49 QOpenWFDPort::QOpenWFDPort(QOpenWFDDevice *device, WFDint portEnumeration)
50     : mDevice(device)
51     , mPortId(portEnumeration)
52     , mAttached(false)
53     , mPipelineId(WFD_INVALID_PIPELINE_ID)
54     , mPipeline(WFD_INVALID_HANDLE)
55 {
56     mPort = wfdCreatePort(device->handle(),portEnumeration,0);
57     WFDint isPortAttached = wfdGetPortAttribi(device->handle(),mPort,WFD_PORT_ATTACHED);
58     if (isPortAttached) {
59         attach();
60     }
61 }
62
63 QOpenWFDPort::~QOpenWFDPort()
64 {
65     detach();
66
67     wfdDestroyPort(mDevice->handle(),mPort);
68 }
69
70 void QOpenWFDPort::attach()
71 {
72     if (mAttached) {
73         return;
74     }
75
76     //just forcing port to be on
77     wfdSetPortAttribi(mDevice->handle(), mPort, WFD_PORT_POWER_MODE, WFD_POWER_MODE_ON);
78
79     int numberOfPortModes = wfdGetPortModes(mDevice->handle(),mPort,0,0);
80     WFDPortMode portModes[numberOfPortModes];
81     int actualNumberOfPortModes = wfdGetPortModes(mDevice->handle(),mPort,portModes,numberOfPortModes);
82     Q_ASSERT(actualNumberOfPortModes == numberOfPortModes);
83
84     if (!actualNumberOfPortModes) {
85         qDebug() << "didn't find any available port modes";
86         return;
87     }
88
89     for (int i = 0; i < actualNumberOfPortModes; i++) {
90         if (portModes[i] != WFD_INVALID_HANDLE) {
91             mPortModes.append(QOpenWFDPortMode(this,portModes[i]));
92             qDebug() << "PortModeAdded:" << mPortModes.at(mPortModes.size()-1);
93         }
94     }
95
96
97     mPixelSize = setNativeResolutionMode();
98     if (mPixelSize.isEmpty()) {
99         qDebug() << "Could not set native resolution mode in QOpenWFPort";
100     }
101
102     WFDfloat physicalWFDSize[2];
103     wfdGetPortAttribfv(mDevice->handle(),mPort,WFD_PORT_PHYSICAL_SIZE,2,physicalWFDSize);
104     mPhysicalSize = QSizeF(physicalWFDSize[0],physicalWFDSize[1]);
105
106     WFDint numAvailablePipelines = wfdGetPortAttribi(mDevice->handle(),mPort,WFD_PORT_PIPELINE_ID_COUNT);
107     if (!numAvailablePipelines) {
108         qFatal("Not possible to make screen that is not possible to create WFPort with no pipline");
109     }
110
111     WFDint pipeIds[numAvailablePipelines];
112     wfdGetPortAttribiv(mDevice->handle(),mPort,WFD_PORT_BINDABLE_PIPELINE_IDS,numAvailablePipelines,pipeIds);
113
114     for (int i = 0; i < numAvailablePipelines; i++) {
115         if (pipeIds[i] != WFD_INVALID_PIPELINE_ID && !mDevice->isPipelineUsed(pipeIds[i])) {
116             mPipelineId = pipeIds[i];
117             mDevice-> addToUsedPipelineSet(mPipelineId,this);
118
119             mPipeline = wfdCreatePipeline(mDevice->handle(),mPipelineId,WFD_NONE);
120             if (mPipeline == WFD_INVALID_HANDLE) {
121                 qFatal("Failed to create pipeline for port %p", this);
122             }
123             break;
124         }
125     }
126
127     if (mPipeline == WFD_INVALID_HANDLE) {
128         qWarning("Failed to create pipeline and cant bind it to port");
129     }
130
131     WFDint geomerty[] = { 0, 0, mPixelSize.width(), mPixelSize.height() };
132
133     wfdSetPipelineAttribiv(mDevice->handle(),mPipeline, WFD_PIPELINE_SOURCE_RECTANGLE, 4, geomerty);
134     wfdSetPipelineAttribiv(mDevice->handle(),mPipeline, WFD_PIPELINE_DESTINATION_RECTANGLE, 4, geomerty);
135
136     wfdBindPipelineToPort(mDevice->handle(),mPort,mPipeline);
137
138     mScreen = new QOpenWFDScreen(this);
139     mDevice->integration()->addScreen(mScreen);
140     mAttached = true;
141 }
142
143 void QOpenWFDPort::detach()
144 {
145     if (!mAttached)
146         return;
147
148     mAttached = false;
149     mOn = false;
150
151     delete mScreen;
152
153     wfdDestroyPipeline(mDevice->handle(),mPipeline);
154     mPipelineId = WFD_INVALID_PIPELINE_ID;
155     mPipeline = WFD_INVALID_HANDLE;
156 }
157
158 bool QOpenWFDPort::attached() const
159 {
160     return mAttached;
161 }
162
163 QSize QOpenWFDPort::setNativeResolutionMode()
164 {
165     WFDint nativePixelSize[2];
166     wfdGetPortAttribiv(device()->handle(),mPort,WFD_PORT_NATIVE_RESOLUTION,2,nativePixelSize);
167     QSize nativeSize(nativePixelSize[0],nativePixelSize[1]);
168
169     for (int i = 0; i < mPortModes.size(); i++) {
170         const QOpenWFDPortMode &mode = mPortModes.at(i);
171         if (nativeSize == mode.size()) {
172             wfdSetPortMode(device()->handle(),mPort,mode.handle());
173             return nativeSize;
174         }
175     }
176     return QSize();
177 }
178
179 QSize QOpenWFDPort::pixelSize() const
180 {
181     return mPixelSize;
182 }
183
184 QSizeF QOpenWFDPort::physicalSize() const
185 {
186     return mPhysicalSize;
187 }
188
189 QOpenWFDDevice * QOpenWFDPort::device() const
190 {
191     return mDevice;
192 }
193
194 WFDPort QOpenWFDPort::handle() const
195 {
196     return mPort;
197 }
198
199 WFDint QOpenWFDPort::portId() const
200 {
201     return mPortId;
202 }
203
204 WFDPipeline QOpenWFDPort::pipeline() const
205 {
206     return mPipeline;
207 }
208
209 QOpenWFDScreen * QOpenWFDPort::screen() const
210 {
211     return mScreen;
212 }