Commit: 633dbd01d7d18df280a6ae58724ac66c95a8c0b1
Author: Campbell Barton
Date:   Sat Jul 26 14:57:14 2014 +1000
Branches: soc-2014-viewport_context
https://developer.blender.org/rB633dbd01d7d18df280a6ae58724ac66c95a8c0b1

Initial GHOST_ContextSDL

Works with multiple windows & shared context, but may need some further changes 
to be considered finished.

===================================================================

M       intern/ghost/CMakeLists.txt
A       intern/ghost/intern/GHOST_ContextSDL.cpp
A       intern/ghost/intern/GHOST_ContextSDL.h
M       intern/ghost/intern/GHOST_WindowSDL.cpp
M       intern/ghost/intern/GHOST_WindowSDL.h

===================================================================

diff --git a/intern/ghost/CMakeLists.txt b/intern/ghost/CMakeLists.txt
index b6d1044..6f30cec 100644
--- a/intern/ghost/CMakeLists.txt
+++ b/intern/ghost/CMakeLists.txt
@@ -125,10 +125,12 @@ if(WITH_HEADLESS OR WITH_GHOST_SDL)
                        intern/GHOST_DisplayManagerSDL.cpp
                        intern/GHOST_SystemSDL.cpp
                        intern/GHOST_WindowSDL.cpp
+                       intern/GHOST_ContextSDL.cpp
 
                        intern/GHOST_DisplayManagerSDL.h
                        intern/GHOST_SystemSDL.h
                        intern/GHOST_WindowSDL.h
+                       intern/GHOST_ContextSDL.h
                )
                add_definitions(-DWITH_GHOST_SDL)
        endif()
diff --git a/intern/ghost/intern/GHOST_ContextSDL.cpp 
b/intern/ghost/intern/GHOST_ContextSDL.cpp
new file mode 100644
index 0000000..02befe5
--- /dev/null
+++ b/intern/ghost/intern/GHOST_ContextSDL.cpp
@@ -0,0 +1,207 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2014 Blender Foundation.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): Jason Wilkins
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file ghost/intern/GHOST_ContextSDL.cpp
+ *  \ingroup GHOST
+ *
+ * Definition of GHOST_ContextSDL class.
+ */
+
+#include "GHOST_ContextSDL.h"
+
+#include <vector>
+
+#include <cassert>
+#include <cstdio>
+#include <cstring>
+
+
+GLEWContext *glewContext = NULL;
+
+
+SDL_GLContext GHOST_ContextSDL::s_sharedContext = NULL;
+int           GHOST_ContextSDL::s_sharedCount   = 0;
+
+
+GHOST_ContextSDL::GHOST_ContextSDL(
+        bool stereoVisual,
+        GHOST_TUns16 numOfAASamples,
+        SDL_Window *window,
+        int contextProfileMask,
+        int contextMajorVersion,
+        int contextMinorVersion,
+        int contextFlags,
+        int contextResetNotificationStrategy)
+    : GHOST_Context(stereoVisual, numOfAASamples),
+      m_window(window),
+      m_contextProfileMask(contextProfileMask),
+      m_contextMajorVersion(contextMajorVersion),
+      m_contextMinorVersion(contextMinorVersion),
+      m_contextFlags(contextFlags),
+      m_contextResetNotificationStrategy(contextResetNotificationStrategy),
+      m_context(NULL)
+{
+       assert(m_window  != NULL);
+}
+
+
+GHOST_ContextSDL::~GHOST_ContextSDL()
+{
+       if (m_context != NULL) {
+               if (m_window != 0 && m_context == SDL_GL_GetCurrentContext())
+                       SDL_GL_MakeCurrent(m_window, m_context);
+
+               if (m_context != s_sharedContext || s_sharedCount == 1) {
+                       assert(s_sharedCount > 0);
+
+                       s_sharedCount--;
+
+                       if (s_sharedCount == 0)
+                               s_sharedContext = NULL;
+
+                       SDL_GL_DeleteContext(m_context);
+               }
+       }
+}
+
+
+GHOST_TSuccess GHOST_ContextSDL::swapBuffers()
+{
+       SDL_GL_SwapWindow(m_window);
+
+       return GHOST_kSuccess;
+}
+
+
+GHOST_TSuccess GHOST_ContextSDL::activateDrawingContext()
+{
+       if (m_context) {
+               activateGLEW();
+
+               return SDL_GL_MakeCurrent(m_window, m_context) ? GHOST_kSuccess 
: GHOST_kFailure;
+       }
+       else {
+               return GHOST_kFailure;
+       }
+}
+
+
+GHOST_TSuccess GHOST_ContextSDL::initializeDrawingContext()
+{
+#ifdef GHOST_OPENGL_ALPHA
+       const bool needAlpha   = true;
+#else
+       const bool needAlpha   = false;
+#endif
+
+#ifdef GHOST_OPENGL_STENCIL
+       const bool needStencil = true;
+#else
+       const bool needStencil = false;
+#endif
+
+       SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, m_contextProfileMask);
+       SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 
m_contextMajorVersion);
+       SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 
m_contextMinorVersion);
+       SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, m_contextFlags);
+
+       SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
+       SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
+       SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
+       SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
+       SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
+       SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
+
+       if (needAlpha) {
+               SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
+       }
+
+       if (needStencil) {
+               SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1);
+       }
+
+       if (m_stereoVisual) {
+               SDL_GL_SetAttribute(SDL_GL_STEREO, 1);
+       }
+
+       if (m_numOfAASamples) {
+               SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
+               SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 
m_numOfAASamples);
+       }
+
+       m_context = SDL_GL_CreateContext(m_window);
+
+       GHOST_TSuccess success;
+
+       if (m_context != NULL) {
+               if (!s_sharedContext)
+                       s_sharedContext = m_context;
+
+               s_sharedCount++;
+
+               success = (SDL_GL_MakeCurrent(m_window, m_context) < 0) ?
+                          GHOST_kFailure : GHOST_kSuccess;
+
+               initContextGLEW();
+
+               initClearGL();
+               SDL_GL_SwapWindow(m_window);
+
+               success = GHOST_kSuccess;
+       }
+       else {
+               success = GHOST_kFailure;
+       }
+
+       return success;
+}
+
+
+GHOST_TSuccess GHOST_ContextSDL::releaseNativeHandles()
+{
+       m_window = NULL;
+
+       return GHOST_kSuccess;
+}
+
+
+GHOST_TSuccess GHOST_ContextSDL::setSwapInterval(int interval)
+{
+       if (SDL_GL_SetSwapInterval(interval) != -1) {
+               return GHOST_kSuccess;
+       }
+       else {
+               return GHOST_kFailure;
+       }
+}
+
+
+GHOST_TSuccess GHOST_ContextSDL::getSwapInterval(int &intervalOut)
+{
+       intervalOut = SDL_GL_GetSwapInterval();
+       return GHOST_kSuccess;
+}
diff --git a/intern/ghost/intern/GHOST_ContextSDL.h 
b/intern/ghost/intern/GHOST_ContextSDL.h
new file mode 100644
index 0000000..abd3dad
--- /dev/null
+++ b/intern/ghost/intern/GHOST_ContextSDL.h
@@ -0,0 +1,130 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2014 Blender Foundation.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): Jason Wilkins
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file ghost/intern/GHOST_ContextSDL.h
+ *  \ingroup GHOST
+ */
+
+#ifndef __GHOST_CONTEXTSDL_H__
+#define __GHOST_CONTEXTSDL_H__
+
+#include "GHOST_Context.h"
+
+extern "C" {
+       #include "SDL.h"
+}
+
+//#define glewGetContext() glewGetContext
+#include "glew-mx.h"
+extern "C" GLEWContext *glewContext;
+
+#ifndef GHOST_OPENGL_SDL_CONTEXT_FLAGS
+#define GHOST_OPENGL_SDL_CONTEXT_FLAGS 0
+#endif
+
+#ifndef GHOST_OPENGL_SDL_RESET_NOTIFICATION_STRATEGY
+#define GHOST_OPENGL_SDL_RESET_NOTIFICATION_STRATEGY 0
+#endif
+
+
+class GHOST_ContextSDL : public GHOST_Context
+{
+public:
+       /**
+        * Constructor.
+        */
+       GHOST_ContextSDL(
+               bool stereoVisual,
+               GHOST_TUns16 numOfAASamples,
+               SDL_Window *window,
+               int contextProfileMask,
+               int contextMajorVersion,
+               int contextMinorVersion,
+               int contextFlags,
+               int contextResetNotificationStrategy);
+
+       /**
+        * Destructor.
+        */
+       virtual ~GHOST_ContextSDL();
+
+       /**
+        * Swaps front and back buffers of a window.
+        * \return  A boolean success indicator.
+        */
+       virtual GHOST_TSuccess swapBuffers();
+
+       /**
+        * Activates the drawing context of this window.
+        * \return  A boolean success indicator.
+        */
+       virtual GHOST_TSuccess activateDrawingContext();
+
+       /**
+        * Call immediately after new to initialize.  If this fails then 
immediately delete the object.
+        * \return Indication as to whether initialization has succeeded.
+        */
+       virtual GHOST_TSuccess initializeDrawingContext();
+
+       /**
+        * Removes references to native handles from this context and then 
returns
+        * \return GHOST_kSuccess if it is OK for the parent to release the 
handles and
+        * GHOST_kFailure if releasing the handles will interfere with sharing
+        */
+       virtual GHOST_TSuccess releaseNativeHandles();
+
+       /**
+        * Sets the swap interval for swapBuffers.
+        * \param interval The swap interval to use.
+        * \return A boolean success indicator.
+        */
+       virtual GHOST_TSuccess setSwapInterval(int interval);
+
+       /**
+        * Gets the current swap interval for swapBuffers.
+        * \param intervalOut Variable to store the swap interval if it can be 
read.
+        * \return Whether the swap interval can be read.
+        */
+       virtual GHOST_TSuccess getSwapInterval(int &intervalOut);
+
+private:
+       SDL_Window *m_window;
+
+       const int m_contextProfileMask;
+       const int m_contextMajorVersion;
+       const int m_contextMinorVersion;
+       const int m_contextFlags;
+       const int m_contextResetNotificationStrategy;
+
+       SDL_GLContext m_context; /* m_sdl_glcontext */
+
+       /** The first created OpenGL context (for sharing display lists) */
+       static SDL_GLContext s_sharedContext;
+       static int           s_sharedCount;
+};
+
+#endif // __GHOST_CONTEXTSDL_H__
diff --git a/intern/ghost/intern/GHOST_WindowSDL.cpp 
b/intern/ghost/intern/GHOST_WindowSDL.cpp
index 2a875e5..3128966 100644
--- a/intern/ghost/intern/GHOST_WindowSDL.cpp
+++ b/intern/ghost/intern/GHOST_WindowSDL.cpp
@@ -28,9 +28,9 @@
 #include "SDL_mouse.h"
 #include "glew-mx.h"
 
-#include <assert.h>
+#include "GHOST_ContextSDL.h"
 
-static SDL_GLContext s_firstContext = NULL;
+#include <assert.h>
 
 GHOST_WindowSDL::GHOST_WindowSDL(GHOST_SystemSDL *system,
                                  const STR_St

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to