So far in this title we have looked at programming in both Assembly language and C/C++. Next we will look at how we can take our C programming skills and explore writing graphics on the Raspberry Pi 2.
This will take advantage of the VideoCore IV GPU. In this chapter you will learn about the following topics:
Let's start with a recap on this GPU unit and some of the technologies it comes equipped with.
As we covered in Chapter 1, Introduction to the Raspberry Pi's Architecture and Setup, of this title, the Raspberry Pi 2 comes equipped with a Broadcom VideoCore IV GPU.
A GPU is a piece of electronic hardware specifically geared towards calculating complex mathematics and altering memory at high speed for the creation and manipulation of images in a frame buffer.
You will be exploring how to access the frame buffer via a C program shortly.
The graphical processing unit the Raspberry Pi 2 comes with provides a variety of features, including OpenMAX, Open EGL, OpenGL 1.1 and 2.2, and Open VG1.1. We will explore some of these briefly later in this chapter as well.
However, due to the complexity of these technologies, it is out of the scope of this book to delve into each in detail. For those who are interested, a comprehensive guide can be found at https://jan.newmarch.name/RPi/, including example programs.
In order to showcase some of the abilities of the GPU you will find a number of example programs bundled with the Raspberry Pi 2 that implement these technologies.
These can be accessed via the /opt/vc/src/hello_pi
folder in Raspbian.
Let's take a look at these in more detail. You will need to be logged into your Raspberry Pi and have access to the Raspbian desktop.
Your first task is to open a terminal window on the desktop.
Once you have this loaded, navigate via the shell to the hello_pi
directory:
cd /opt/vc/src/hello_pi
Each program we will run is located in its own folder in this directory.
We are going to start by looking at some programs that use the OpenMAX API.
OpenMax, an acronym for Open Media Acceleration, is a cross-platform set of C programming interfaces. These are geared towards video, image, and audio processing. You can read more about OpenMax at https://www.khronos.org/openmax/.
Let's start by running the first program. This is called hello_encode
:
./hello_encode/hello_encode.bin output
We have passed in the name of an output file for the program to write to. If the file does not exist it will be created.
You will now see some text displayed on the screen; the following is a sample of this:
Port 200: in 1/1 15360 16 disabled,not pop.,not cont. 160x64 160x64 @1966080 20 Port 200: in 1/1 15360 16 disabled,not pop.,not cont. 640x360 640x360 @1966080 20 OMX_SetParameter for video_encode:201... Current Bitrate=1000000 encode to idle... enabling port buffers for 200... enabling port buffers for 201... encode to executing... looping for buffers... 0 0 0 1 27 64 0 1e ac 2b 40 50 17 fc b0 f 12 26 a0 Writing frame 1/300 0 0 0 1 28 ee 2 5c b0
Once hello_encode
has run you should now see the following file in the directory:
output
This is a file in the H.264
format. H.264
, also known as MPEG-4 Part 10 AVC, is a commonly used video encoding format. You will find it implemented in a variety of areas, including YouTube, Apple iTunes, and Blu-ray discs.
The following Wikipedia article provides more information on this format:
https://en.wikipedia.org/wiki/H.264/MPEG-4_AVC
The output file we generated will be used in a moment as input to one of the other example programs that comes bundled with the Raspberry Pi 2. Let's take a look at how this works.
Navigate to the hello_video
directory. Let's now try using the output of hello_encode
as a parameter for the hello_video
program. As before, this is a binary file we can run from the command line:
./hello_video/hello_video.bin ../hello_encode/output
When you run this, you should see a variety of colors scrolling to the top left-hand corner of the screen. You'll no longer be able to see the command line or the Raspberry Pi's desktop.
To exit hello_video
press Ctrl + C. This will close the program and allow you access to the command line again.
By running this we have demonstrated how we can take the output of a video encoding program and then run it on the Raspberry Pi 2.
There is also a demo file in this directory you can try out with the following command if you wish:
./hello_video/hello_video.bin test.h264
Once again, press Ctrl+ C to exit at any time.
The next example we will look at uses OpenGL ES. This is a subset of the OpenGL graphics rendering suite and is often used for rendering 2D and 3D graphics for computer games.
You can read about the technology at https://www.khronos.org/opengles/.
The program is located in the hello_triangle
directory. Looking in this location you will see a number of other files, such as Gaudi_128_128.raw
. These .raw
files are images that are implemented by the hello_triangle
program.
You can run the OpenGL ES example as follows:
./hello_triangle/hello_triangle.bin
When the program launches a cube is displayed. This cube rotates and each face contains one of the three raw image files that were included in the directory. To exit, as with our other examples, you can use the Ctrl + C command.
Let's look at one more example of this technology in the hello_triangle2
directory.
We can run this program as follows:
./hello_triangle_2/hello_triangle2.bin
When this program runs it produces a fractal-style image implementing Mandelbrot sets.
You can exit by pressing the Ctrl + C command.
If you look through the hello_pi
directory you will see plenty more examples you can try out. Also, each directory contains the source code. Feel free to edit it and then use the Makefile
to rebuild the executable.
This concludes the examples that demonstrate some of the features of the GPU. Let's now take a look at writing applications that directly access the frame buffer and draw color and lines to the screen.