~jhodapp/qtubuntu/qtubuntu_add-live-orientation

« back to all changes in this revision

Viewing changes to src/plugins/platforms/hybris/qhybrisscreen.cpp

  • Committer: Loïc Molinari
  • Date: 2012-09-26 13:46:46 UTC
  • Revision ID: loic.molinari@canonical.com-20120926134646-4j5m7r0h5sikzoes
Initial import.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright © 2012 Canonical Ltd
 
2
// FIXME(loicm) Add copyright notice here.
 
3
 
 
4
#include "qhybrisscreen.h"
 
5
#include "qhybriswindow.h"
 
6
#include <QtPlatformSupport/private/qeglconvenience_p.h>
 
7
#include <QtPlatformSupport/private/qeglplatformcontext_p.h>
 
8
 
 
9
QT_BEGIN_NAMESPACE
 
10
 
 
11
class QHybrisContext : public QEGLPlatformContext {
 
12
 public:
 
13
  QHybrisContext(const QSurfaceFormat& format, QPlatformOpenGLContext* share, EGLDisplay display,
 
14
                 EGLenum eglApi = EGL_OPENGL_ES_API)
 
15
      : QEGLPlatformContext(format, share, display, eglApi) {
 
16
  }
 
17
 
 
18
  EGLSurface eglSurfaceForPlatformSurface(QPlatformSurface* surface) {
 
19
    QHybrisWindow* window = static_cast<QHybrisWindow*>(surface);
 
20
    QHybrisScreen* screen = static_cast<QHybrisScreen*>(window->screen());
 
21
    return screen->surface();
 
22
  }
 
23
};
 
24
 
 
25
QHybrisScreen::QHybrisScreen(EGLNativeDisplayType display)
 
26
    : m_depth(32)
 
27
    , m_format(QImage::Format_Invalid)
 
28
    , m_platformContext(0)
 
29
    , m_surface(0) {
 
30
#ifdef QHYBRIS_DEBUG
 
31
  qWarning("QEglScreen %p\n", this);
 
32
#endif
 
33
  EGLint major, minor;
 
34
 
 
35
  if (!eglBindAPI(EGL_OPENGL_ES_API)) {
 
36
    qWarning("Could not bind GL_ES API\n");
 
37
    qFatal("EGL error");
 
38
  }
 
39
 
 
40
  m_dpy = eglGetDisplay(display);
 
41
  if (m_dpy == EGL_NO_DISPLAY) {
 
42
    qWarning("Could not open egl display\n");
 
43
    qFatal("EGL error");
 
44
  }
 
45
  qWarning("Opened display %p\n", m_dpy);
 
46
 
 
47
  if (!eglInitialize(m_dpy, &major, &minor)) {
 
48
    qWarning("Could not initialize egl display\n");
 
49
    qFatal("EGL error");
 
50
  }
 
51
 
 
52
  qWarning("Initialized display %d %d\n", major, minor);
 
53
 
 
54
  int swapInterval = 1;
 
55
  QByteArray swapIntervalString = qgetenv("QT_QPA_EGLFS_SWAPINTERVAL");
 
56
  if (!swapIntervalString.isEmpty()) {
 
57
    bool ok;
 
58
    swapInterval = swapIntervalString.toInt(&ok);
 
59
    if (!ok)
 
60
      swapInterval = 1;
 
61
  }
 
62
  eglSwapInterval(m_dpy, swapInterval);
 
63
}
 
64
 
 
65
QHybrisScreen::~QHybrisScreen() {
 
66
  if (m_surface)
 
67
    eglDestroySurface(m_dpy, m_surface);
 
68
  eglTerminate(m_dpy);
 
69
}
 
70
 
 
71
void QHybrisScreen::createAndSetPlatformContext() const {
 
72
  const_cast<QHybrisScreen*>(this)->createAndSetPlatformContext();
 
73
}
 
74
 
 
75
void QHybrisScreen::createAndSetPlatformContext() {
 
76
  QSurfaceFormat platformFormat;
 
77
 
 
78
  QByteArray depthString = qgetenv("QT_QPA_EGLFS_DEPTH");
 
79
  if (depthString.toInt() == 16) {
 
80
    platformFormat.setDepthBufferSize(16);
 
81
    platformFormat.setRedBufferSize(5);
 
82
    platformFormat.setGreenBufferSize(6);
 
83
    platformFormat.setBlueBufferSize(5);
 
84
    m_depth = 16;
 
85
    m_format = QImage::Format_RGB16;
 
86
  } else {
 
87
    platformFormat.setDepthBufferSize(24);
 
88
    platformFormat.setStencilBufferSize(8);
 
89
    platformFormat.setRedBufferSize(8);
 
90
    platformFormat.setGreenBufferSize(8);
 
91
    platformFormat.setBlueBufferSize(8);
 
92
    m_depth = 32;
 
93
    m_format = QImage::Format_RGB32;
 
94
  }
 
95
 
 
96
  if (!qEnvironmentVariableIsEmpty("QT_QPA_EGLFS_MULTISAMPLE"))
 
97
    platformFormat.setSamples(4);
 
98
 
 
99
  EGLConfig config = q_configFromGLFormat(m_dpy, platformFormat);
 
100
 
 
101
  EGLNativeWindowType eglWindow = 0;
 
102
 
 
103
#ifdef QHYBRIS_DEBUG
 
104
  q_printEglConfig(m_dpy, config);
 
105
#endif
 
106
 
 
107
  m_surface = eglCreateWindowSurface(m_dpy, config, eglWindow, NULL);
 
108
  if (m_surface == EGL_NO_SURFACE) {
 
109
    qWarning("Could not create the egl surface: error = 0x%x\n", eglGetError());
 
110
    eglTerminate(m_dpy);
 
111
    qFatal("EGL error");
 
112
  }
 
113
 
 
114
  QEGLPlatformContext* platformContext = new QHybrisContext(platformFormat, 0, m_dpy);
 
115
  m_platformContext = platformContext;
 
116
 
 
117
  EGLint w, h;
 
118
  eglQuerySurface(m_dpy, m_surface, EGL_WIDTH, &w);
 
119
  eglQuerySurface(m_dpy, m_surface, EGL_HEIGHT, &h);
 
120
 
 
121
  m_geometry = QRect(0,0,w,h);
 
122
}
 
123
 
 
124
QRect QHybrisScreen::geometry() const {
 
125
  if (m_geometry.isNull()) {
 
126
    createAndSetPlatformContext();
 
127
  }
 
128
  return m_geometry;
 
129
}
 
130
 
 
131
int QHybrisScreen::depth() const {
 
132
  return m_depth;
 
133
}
 
134
 
 
135
QImage::Format QHybrisScreen::format() const {
 
136
  if (m_format == QImage::Format_Invalid)
 
137
    createAndSetPlatformContext();
 
138
  return m_format;
 
139
}
 
140
 
 
141
QPlatformOpenGLContext *QHybrisScreen::platformContext() const {
 
142
  if (!m_platformContext) {
 
143
    QHybrisScreen* screen = const_cast<QHybrisScreen*>(this);
 
144
    screen->createAndSetPlatformContext();
 
145
  }
 
146
  return m_platformContext;
 
147
}
 
148
 
 
149
QT_END_NAMESPACE