Building SDL2-2.0.6 on the Raspberry Pi to enable OpenGLES 2 in Hardware

With the release of Raspbian Stretch, quite a lot has changed. One of those things is the default graphics drivers for the VC4. This matters to us because with the release of Stretch they have finally removed the old drivers which have been been installed by default as well. This is a bit of a gotcha because SDL2 has the locations and name of the drivers hard coded into it and without changing that we will be unable to get a good build of SDL2 with accelerated scaling in the framebuffer.

SDL2 is now installed by default but it is compiled for desktop users with support for X and OpenGL, neither of which we can take advantage of in a framebuffer and it will just confuse MAME if you try to compile using it. Just as a test I compiled a minimal MAME 0.188 using the default SDL2 and running on the Desktop I got ~3fps running Gridlee in a window.

So first things first you will need to get rid of the default SDL2

sudo apt-get remove -y --force-yes libsdl2-dev
sudo apt-get autoremove -y

Next we need to set up the build environment; so add a few needed packages

sudo apt-get install libfontconfig-dev qt5-default automake mercurial libtool libfreeimage-dev libopenal-dev libpango1.0-dev libsndfile-dev libudev-dev libtiff5-dev libwebp-dev libasound2-dev libaudio-dev libxrandr-dev libxcursor-dev libxi-dev libxinerama-dev libxss-dev libesd0-dev freeglut3-dev libmodplug-dev libsmpeg-dev

I’m going to assume you use some form of  ‘code’ or ‘develop’ type folder structure for your programming efforts (I use ‘code’, just substitute your development directory structure for mine)

cd
mkdir code
cd code

Now we have need to get the latest build of from the libsdl mercurial by typing:-

hg clone http://hg.libsdl.org/SDL
cd SDL

You will end up with a copy of the SDL2 source code and leave yourself in the source folder, now we need to change the hard coded OGLES2 drivers for the new broadcom ones.

nano src/video/SDL_egl.c

Look for lines 41-46, they should look like this…

#if SDL_VIDEO_DRIVER_RPI
/* Raspbian places the OpenGL ES/EGL binaries in a non standard path */
#define DEFAULT_EGL "/opt/vc/lib/libEGL.so"
#define DEFAULT_OGL_ES2 "/opt/vc/lib/libGLESv2.so"
#define DEFAULT_OGL_ES_PVR "/opt/vc/lib/libGLES_CM.so"
#define DEFAULT_OGL_ES "/opt/vc/lib/libGLESv1_CM.so"

and they need to be changed to look like this…

#if SDL_VIDEO_DRIVER_RPI
/* Raspbian places the OpenGL ES/EGL binaries in a non standard path */
#define DEFAULT_EGL "/opt/vc/lib/libbrcmEGL.so"
#define DEFAULT_OGL_ES2 "/opt/vc/lib/libbrcmGLESv2.so"
#define DEFAULT_OGL_ES_PVR "/opt/vc/lib/libbrcmGLESv2.so"
#define DEFAULT_OGL_ES "/opt/vc/lib/libbrcmGLESv2.so"

That’s all we need to change, now we need to actually build it.

./autogen.sh
./configure --disable-pulseaudio --disable-esd --disable-video-mir --disable-video-wayland --disable-video-opengl --host=arm-raspberry-linux-gnueabihf
make
sudo make install

That will take a little while but when it’s done if you type sdl2-config --version it should say 2.0.6.

Great success!

Next up we will build all the utility libraries, starting by downloading them all.

cd
cd code
wget http://www.libsdl.org/projects/SDL_image/release/SDL2_image-2.0.1.tar.gz
wget http://www.libsdl.org/projects/smpeg/release/smpeg2-2.0.0.tar.gz
wget http://www.libsdl.org/projects/SDL_mixer/release/SDL2_mixer-2.0.1.tar.gz
wget http://www.libsdl.org/projects/SDL_net/release/SDL2_net-2.0.1.tar.gz
wget http://www.libsdl.org/projects/SDL_ttf/release/SDL2_ttf-2.0.14.tar.gz

Uncompress them all

tar zxvf SDL2_image-2.0.1.tar.gz 
tar zxvf smpeg2-2.0.0.tar.gz
tar zxvf SDL2_mixer-2.0.1.tar.gz 
tar zxvf SDL2_net-2.0.1.tar.gz 
tar zxvf SDL2_ttf-2.0.14.tar.gz

Build the Image file loading library

cd SDL2_image-2.0.1 
./autogen.sh 
./configure 
make 
sudo make install
cd ..

Build the MPEG decoder (you need this for the mixer)

cd smpeg2-2.0.0 
./autogen.sh 
./configure 
make 
sudo make install
cd ..

Build the Audio mixer library

cd SDL2_mixer-2.0.1 
./autogen.sh 
./configure 
make 
sudo make install
cd ..

Build the Networking library

cd SDL2_net-2.0.1 
./autogen.sh 
./configure 
make 
sudo make install
cd ..

Build the Truetype font library

cd SDL2_ttf-2.0.14
./autogen.sh
./configure
make
sudo make install
cd ..

All done. Any program that uses SDL2 to draw to the framebuffer should now be doing it in hardware on the raspberry pis videocore4 using opengles2!