Improve QSurface / QWindow API a bit and use that to avoid errors
authorGunnar Sletta <gunnar.sletta@nokia.com>
Thu, 19 Jan 2012 11:51:43 +0000 (12:51 +0100)
committerQt by Nokia <qt-info@nokia.com>
Thu, 2 Feb 2012 16:20:24 +0000 (17:20 +0100)
Change-Id: Iadba1c3a7b8e6bc7f145455132cefed2a905c11d
Reviewed-by: Samuel Rødal <samuel.rodal@nokia.com>
examples/opengl/hellogl_es/glwindow.cpp
src/gui/kernel/qopenglcontext.cpp
src/gui/kernel/qplatformsurface_qpa.cpp
src/gui/kernel/qplatformsurface_qpa.h
src/gui/kernel/qsurface.cpp
src/gui/kernel/qsurface.h
src/gui/kernel/qwindow.h
tests/auto/gui/qopengl/tst_qopengl.cpp

index 6be4e63..d94d0d6 100644 (file)
@@ -64,6 +64,8 @@ inline void Normalize(qreal &x, qreal &y, qreal &z)
 
 GLWindow::GLWindow()
 {
+    setSurfaceType(OpenGLSurface);
+
     qtLogo = true;
     createdVertices = 0;
     createdNormals = 0;
index f6481d3..1b6d1d3 100644 (file)
@@ -288,6 +288,12 @@ bool QOpenGLContext::makeCurrent(QSurface *surface)
     if (!surface->surfaceHandle())
         return false;
 
+    if (surface->surfaceType() != QSurface::OpenGLSurface) {
+        qWarning() << "QOpenGLContext::makeBuffers() called with non-opengl surface";
+        return false;
+    }
+
+
     if (d->platformGLContext->makeCurrent(surface->surfaceHandle())) {
         QOpenGLContextPrivate::setCurrentContext(this);
         d->surface = surface;
@@ -354,6 +360,11 @@ void QOpenGLContext::swapBuffers(QSurface *surface)
         return;
     }
 
+    if (surface->surfaceType() != QSurface::OpenGLSurface) {
+         qWarning() << "QOpenGLContext::swapBuffers() called with non-opengl surface";
+         return;
+     }
+
     QPlatformSurface *surfaceHandle = surface->surfaceHandle();
     if (!surfaceHandle)
         return;
index 56b8f61..aeb73da 100644 (file)
 
 QT_BEGIN_NAMESPACE
 
-QSurface::SurfaceType QPlatformSurface::surfaceType() const
+QSurface::SurfaceClass QPlatformSurface::surfaceClass() const
 {
     return m_type;
 }
 
-QPlatformSurface::QPlatformSurface(QSurface::SurfaceType type) : m_type(type)
+QPlatformSurface::QPlatformSurface(QSurface::SurfaceClass type) : m_type(type)
 {
 }
 
index b57bbea..af23ad3 100644 (file)
@@ -56,12 +56,12 @@ class Q_GUI_EXPORT QPlatformSurface
 public:
     virtual QSurfaceFormat format() const = 0;
 
-    QSurface::SurfaceType surfaceType() const;
+    QSurface::SurfaceClass surfaceClass() const;
 
 private:
-    QPlatformSurface(QSurface::SurfaceType type);
+    QPlatformSurface(QSurface::SurfaceClass type);
 
-    QSurface::SurfaceType m_type;
+    QSurface::SurfaceClass m_type;
 
     friend class QPlatformWindow;
 };
index 24079c5..eb26768 100644 (file)
 
 QT_BEGIN_NAMESPACE
 
-QSurface::QSurface(SurfaceType type)
-    : m_type(type)
+
+/*!
+    \class QSurface
+    \brief The QSurface class is an abstraction of renderable surfaces in Qt.
+
+    The size of the surface is accessible with the size() function. The rendering
+    specific attributes of the surface are accessible through the format() function.
+ */
+
+
+/*!
+    \enum QSurface::SurfaceClass
+
+    The SurfaceClass enum describes the actual subclass of the surface.
+
+    \value Window The surface is an instance of QWindow.
+ */
+
+
+/*!
+    \enum QSurface::SurfaceType
+
+    The SurfaceType enum describes what type of surface the.
+
+    \value RasterSurface The surface is is composed of pixels and can be rendered to using
+    a software rasterizer like Qt's raster paint engine.
+    \value OpenGLSurface The surface is an OpenGL compatible surface and can be used
+    in conjunction with QOpenGLContext.
+ */
+
+
+/*!
+    QSize QSurface::size() const
+
+    Returns the size of the surface in pixels.
+ */
+
+
+
+QSurface::QSurface(SurfaceClass type)
+    : m_type(type), m_reserved(0)
 {
 }
 
-QSurface::SurfaceType QSurface::surfaceType() const
+
+
+QSurface::SurfaceClass QSurface::surfaceClass() const
 {
     return m_type;
 }
index fd8eaaf..a8900fd 100644 (file)
@@ -45,6 +45,8 @@
 #include <QtCore/qnamespace.h>
 #include <QtGui/qsurfaceformat.h>
 
+#include <QtCore/qsize.h>
+
 QT_BEGIN_HEADER
 
 QT_BEGIN_NAMESPACE
@@ -52,26 +54,37 @@ QT_BEGIN_NAMESPACE
 
 class QPlatformSurface;
 
+class QSurfacePrivate;
+
 class Q_GUI_EXPORT QSurface
 {
 public:
-    enum SurfaceType {
+    enum SurfaceClass {
         Window
     };
 
+    enum SurfaceType {
+        RasterSurface,
+        OpenGLSurface
+    };
+
     virtual ~QSurface();
 
-    SurfaceType surfaceType() const;
+    SurfaceClass surfaceClass() const;
 
     virtual QSurfaceFormat format() const = 0;
     virtual QPlatformSurface *surfaceHandle() const = 0;
 
-private:
-    QSurface(SurfaceType type);
+    virtual SurfaceType surfaceType() const = 0;
+
+    virtual QSize size() const = 0;
+
+protected:
+    QSurface(SurfaceClass type);
 
-    SurfaceType m_type;
+    SurfaceClass m_type;
 
-    friend class QWindow;
+    QSurfacePrivate *m_reserved;
 };
 
 QT_END_NAMESPACE
index 4e6a859..98b468b 100644 (file)
@@ -93,7 +93,6 @@ class Q_GUI_EXPORT QWindow : public QObject, public QSurface
     Q_PROPERTY(Qt::ScreenOrientation contentOrientation READ contentOrientation WRITE reportContentOrientationChange NOTIFY contentOrientationChanged)
 
 public:
-    enum SurfaceType { RasterSurface, OpenGLSurface };
 
     QWindow(QScreen *screen = 0);
     QWindow(QWindow *parent);
index 0304376..e8374b0 100644 (file)
@@ -130,6 +130,7 @@ struct SharedResource : public QOpenGLSharedResource
 void tst_QOpenGL::sharedResourceCleanup()
 {
     QWindow window;
+    window.setSurfaceType(QWindow::OpenGLSurface);
     window.setGeometry(0, 0, 10, 10);
     window.create();
 
@@ -152,6 +153,7 @@ void tst_QOpenGL::sharedResourceCleanup()
     QOpenGLContext *ctx2 = new QOpenGLContext;
     ctx2->setShareContext(ctx);
     ctx2->create();
+
     delete ctx;
 
     resource->free();
@@ -191,6 +193,7 @@ void tst_QOpenGL::sharedResourceCleanup()
 void tst_QOpenGL::multiGroupSharedResourceCleanup()
 {
     QWindow window;
+    window.setSurfaceType(QWindow::OpenGLSurface);
     window.setGeometry(0, 0, 10, 10);
     window.create();
 
@@ -212,6 +215,7 @@ void tst_QOpenGL::multiGroupSharedResourceCleanup()
 void tst_QOpenGL::multiGroupSharedResourceCleanupCustom()
 {
     QWindow window;
+    window.setSurfaceType(QWindow::OpenGLSurface);
     window.setGeometry(0, 0, 10, 10);
     window.create();
 
@@ -348,6 +352,7 @@ void qt_opengl_check_test_pattern(const QImage& img)
 void tst_QOpenGL::fboSimpleRendering()
 {
     QWindow window;
+    window.setSurfaceType(QWindow::OpenGLSurface);
     window.setGeometry(0, 0, 10, 10);
     window.create();
     QOpenGLContext ctx;
@@ -391,6 +396,7 @@ void tst_QOpenGL::fboRendering()
 #endif
 
     QWindow window;
+    window.setSurfaceType(QWindow::OpenGLSurface);
     window.setGeometry(0, 0, 10, 10);
     window.create();
     QOpenGLContext ctx;
@@ -430,6 +436,7 @@ void tst_QOpenGL::fboRendering()
 void tst_QOpenGL::fboHandleNulledAfterContextDestroyed()
 {
     QWindow window;
+    window.setSurfaceType(QWindow::OpenGLSurface);
     window.setGeometry(0, 0, 10, 10);
     window.create();
 
@@ -459,6 +466,7 @@ void tst_QOpenGL::openGLPaintDevice()
 #endif
 
     QWindow window;
+    window.setSurfaceType(QWindow::OpenGLSurface);
     window.setGeometry(0, 0, 128, 128);
     window.create();
 
@@ -506,6 +514,7 @@ void tst_QOpenGL::openGLPaintDevice()
 void tst_QOpenGL::aboutToBeDestroyed()
 {
     QWindow window;
+    window.setSurfaceType(QWindow::OpenGLSurface);
     window.setGeometry(0, 0, 128, 128);
     window.create();