Clean up Raspberry Pi spec
[profile/ivi/qtbase.git] / mkspecs / devices / linux-rasp-pi-g++ / qeglfshooks_pi.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the qmake spec of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qeglfshooks.h"
43
44 #include <bcm_host.h>
45
46 #if 0 //fb size query
47 #include <stdio.h>
48 #include <sys/ioctl.h>
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <fcntl.h>
52 #include <linux/fb.h>
53 #endif
54
55 QT_BEGIN_NAMESPACE
56
57 static DISPMANX_DISPLAY_HANDLE_T dispman_display = 0;
58 static DISPMANX_UPDATE_HANDLE_T dispman_update = 0;
59
60 class QEglFSPiHooks : public QEglFSHooks
61 {
62 public:
63     virtual void platformInit();
64     virtual void platformDestroy();
65     virtual EGLNativeDisplayType platformDisplay() const;
66     virtual QSize screenSize() const;
67     virtual EGLNativeWindowType createNativeWindow(const QSize &size);
68     virtual void destroyNativeWindow(EGLNativeWindowType window);
69     virtual bool hasCapability(QPlatformIntegration::Capability cap) const;
70 };
71
72 void QEglFSPiHooks::platformInit()
73 {
74     bcm_host_init();
75 }
76
77 EGLNativeDisplayType QEglFSPiHooks::platformDisplay() const
78 {
79     dispman_display = vc_dispmanx_display_open(0/* LCD */);
80     return EGL_DEFAULT_DISPLAY;
81 }
82
83 void QEglFSPiHooks::platformDestroy()
84 {
85     vc_dispmanx_display_close(dispman_display);
86 }
87
88 QSize QEglFSPiHooks::screenSize() const
89 {
90     //both mechanisms work
91 #if 1
92     uint32_t width, height;
93     graphics_get_display_size(0 /* LCD */, &width, &height);
94     return QSize(width, height);
95 #else
96     int fd = open("/dev/fb0", O_RDONLY);
97     if (fd == -1) {
98         fprintf(stderr, "Failed to open fb to detect screen resolution!\n");
99         return QSize();
100     }
101
102     struct fb_var_screeninfo vinfo;
103     if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo) = -1) {
104         fprintf(stderr, "Could not query screen info variable\n");
105         close(fd);
106         return QSize();
107     }
108
109     close(fd);
110
111     return QSize(vinfo.xres, vinfo.yres);
112 #endif
113 }
114
115 EGLNativeWindowType QEglFSPiHooks::createNativeWindow(const QSize &size)
116 {
117     VC_RECT_T dst_rect;
118     dst_rect.x = 0;
119     dst_rect.y = 0;
120     dst_rect.width = size.width();
121     dst_rect.height = size.height();
122
123     VC_RECT_T src_rect;
124     src_rect.x = 0;
125     src_rect.y = 0;
126     src_rect.width = size.width() << 16;
127     src_rect.height = size.height() << 16;
128
129     dispman_update = vc_dispmanx_update_start(0);
130
131     VC_DISPMANX_ALPHA_T alpha;
132     alpha.flags = DISPMANX_FLAGS_ALPHA_FIXED_ALL_PIXELS;
133     alpha.opacity = 0xFF;
134     alpha.mask = 0;
135
136     DISPMANX_ELEMENT_HANDLE_T dispman_element = vc_dispmanx_element_add(
137             dispman_update, dispman_display, 0, &dst_rect, 0, &src_rect,
138             DISPMANX_PROTECTION_NONE, &alpha, (DISPMANX_CLAMP_T *)NULL, (DISPMANX_TRANSFORM_T)0);
139
140     vc_dispmanx_update_submit_sync(dispman_update);
141
142     EGL_DISPMANX_WINDOW_T *eglWindow = new EGL_DISPMANX_WINDOW_T;
143     eglWindow->element = dispman_element;
144     eglWindow->width = size.width();
145     eglWindow->height = size.height();
146
147     return eglWindow;
148 }
149
150 void QEglFSPiHooks::destroyNativeWindow(EGLNativeWindowType window)
151 {
152     EGL_DISPMANX_WINDOW_T *eglWindow = static_cast<EGL_DISPMANX_WINDOW_T *>(window);
153     vc_dispmanx_element_remove(dispman_update, eglWindow->element);
154     delete eglWindow;
155 }
156
157 bool QEglFSPiHooks::hasCapability(QPlatformIntegration::Capability cap) const
158 {
159     switch (cap) {
160         case QPlatformIntegration::ThreadedPixmaps:
161         case QPlatformIntegration::OpenGL:
162         case QPlatformIntegration::ThreadedOpenGL:
163         case QPlatformIntegration::BufferQueueingOpenGL:
164             return true;
165         default:
166             return false;
167     }
168 }
169
170 QEglFSPiHooks eglFSPiHooks;
171 QEglFSHooks *platformHooks = &eglFSPiHooks;
172
173 QT_END_NAMESPACE