Version

Quick search

Framebuffer

The Fbo is like an offscreen window. You can activate the fbo for rendering into a texture and use your fbo as a texture for other drawing.

The Fbo acts as a kivy.graphics.instructions.Canvas.

Here is an example of using an fbo for some colored rectangles:

from kivy.graphics import Fbo, Color, Rectangle

class FboTest(Widget):
    def __init__(self, **kwargs):
        super(FboTest, self).__init__(**kwargs)

        # first step is to create the fbo and use the fbo texture on other
        # rectangle

        with self.canvas:
            # create the fbo
            self.fbo = Fbo(size=(256, 256))

            # show our fbo on the widget in different size
            Color(1, 1, 1)
            Rectangle(size=(32, 32), texture=self.fbo.texture)
            Rectangle(pos=(32, 0), size=(64, 64), texture=self.fbo.texture)
            Rectangle(pos=(96, 0), size=(128, 128), texture=self.fbo.texture)

        # in the second step, you can draw whatever you want on the fbo
        with self.fbo:
            Color(1, 0, 0, .8)
            Rectangle(size=(256, 64))
            Color(0, 1, 0, .8)
            Rectangle(size=(64, 256))

If you change anything in the self.fbo object, it will be automatically updated. The canvas where the fbo is put will be automatically updated as well.

Reloading the FBO content

New in version 1.2.0.

If the OpenGL context is lost, then the FBO is lost too. You need to reupload data on it yourself. Use the Fbo.add_reload_observer() to add a reloading function that will be automatically called when needed:

def __init__(self, **kwargs):
    super(...).__init__(**kwargs)
    self.fbo = Fbo(size=(512, 512))
    self.fbo.add_reload_observer(self.populate_fbo)

    # and load the data now.
    self.populate_fbo(self.fbo)


def populate_fbo(self, fbo):
    with fbo:
        # .. put your Color / Rectangle / ... here

This way, you could use the same method for initialization and for reloading. But it’s up to you.

class kivy.graphics.fbo.Fbo

Bases: kivy.graphics.instructions.RenderContext

Fbo class for wrapping the OpenGL Framebuffer extension. The Fbo support “with” statement.

Parameters:
clear_color: tuple, defaults to (0, 0, 0, 0)

Define the default color for clearing the framebuffer

size: tuple, defaults to (1024, 1024)

Default size of the framebuffer

push_viewport: bool, defaults to True

If True, the OpenGL viewport will be set to the framebuffer size, and will be automatically restored when the framebuffer released.

with_depthbuffer: bool, defaults to False

If True, the framebuffer will be allocated with a Z buffer.

with_stencilbuffer: bool, defaults to False

New in version 1.9.0.

If True, the framebuffer will be allocated with a stencil buffer.

texture: Texture, defaults to None

If None, a default texture will be created.

Note

Using both of with_stencilbuffer and with_depthbuffer is not supported in kivy 1.9.0

add_reload_observer()

Add a callback to be called after the whole graphics context has been reloaded. This is where you can reupload your custom data in GPU.

New in version 1.2.0.

Parameters:
callback: func(context) -> return None

The first parameter will be the context itself

bind()

Bind the FBO to the current opengl context. Bind mean that you enable the Framebuffer, and all the drawing operations will act inside the Framebuffer, until release() is called.

The bind/release operations are automatically called when you add graphics objects into it. If you want to manipulate a Framebuffer yourself, you can use it like this:

self.fbo = FBO()
self.fbo.bind()
# do any drawing command
self.fbo.release()

# then, your fbo texture is available at
print(self.fbo.texture)
clear_buffer()

Clear the framebuffer with the clear_color.

You need to bind the framebuffer yourself before calling this method:

fbo.bind()
fbo.clear_buffer()
fbo.release()
clear_color

Clear color in (red, green, blue, alpha) format.

get_pixel_color()

Get the color of the pixel with specified window coordinates wx, wy. It returns result in RGBA format.

New in version 1.8.0.

pixels

Get the pixels texture, in RGBA format only, unsigned byte. The origin of the image is at bottom left.

New in version 1.7.0.

release()

Release the Framebuffer (unbind).

remove_reload_observer()

Remove a callback from the observer list, previously added by add_reload_observer().

New in version 1.2.0.

size

Size of the framebuffer, in (width, height) format.

If you change the size, the framebuffer content will be lost.

texture

Return the framebuffer texture