---
title: "GPU"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/drivers/gpu/
last_updated: 2026-11-18
sdk_version: 4.44
---
# GPU

Source: https://developer.repebble.com/pebbleos/docs/samples/drivers/gpu/

This sample draws into the frame of the display with the GPU, `GPU`: an opaque fill, a fill blended over it, a sprite with its own alpha, the same at half opacity and then rotated, scaled and both, around its centre. It reads pixels back to check each result, times each call, and sends the frame to the display at the end.

What a GPU cannot do it refuses with `-ENOTSUP` before drawing anything, which the sample reports instead of failing.

Run:

`pbl samples samples/drivers/gpu`

Boards:

only sf32lb57\_dpi\_hdk

## Sources

src/main.c

```
#include \<errno.h\>#include \<stdint.h\>#include \<stdio.h\>#include \<pbl/drivers/backlight.h\>#include \<pbl/drivers/display.h\>#include \<pbl/drivers/gpu.h\>#include \<pbl/kernel/malloc.h\>#include \<pbl/kernel/time.h\>#include \<pbl/util/size.h\>#define SPRITE 96U#define RGB565(r, g, b) ((uint16\_t)((((r) \>\> 3) \<\< 11) | (((g) \>\> 2) \<\< 5) | ((b) \>\> 3)))/\* Where a point of the opaque left half ends up, and one outside of the transformed sprite. \*/struct transform {const char \*name;int16\_t angle;uint16\_t scale;int16\_t x, y;int16\_t in\_x, in\_y;int16\_t out\_x, out\_y;};static const struct transform s\_transforms[] = {/\* A quarter turn clockwise: the left half goes up. \*/{"rotated", 900, PBL\_GPU\_SCALE\_NONE, 270, 80, 0, -30, 0, 60},/\* Twice the size: the left edge is twice as far. \*/{"scaled", 0, 2U \* PBL\_GPU\_SCALE\_NONE, 60, 250, -80, 0, 0, 110},/\* Both: the left half goes up, twice as far. \*/{"rotated and scaled", 900, 2U \* PBL\_GPU\_SCALE\_NONE, 250, 250, 0, -80, 0, 115},};static uint32\_t s\_start;static void prv\_start(void) {s\_start = pbl\_cycle\_get\_32();}static unsigned int prv\_elapsed\_us(void) {return (unsigned int)((uint64\_t)(pbl\_cycle\_get\_32() - s\_start) \* 1000000U / pbl\_cycles\_per\_sec());}static uint16\_t prv\_pixel(const struct pbl\_gpu\_buffer \*fb, uint16\_t x, uint16\_t y) {return \*(const uint16\_t \*)(const void \*)((const uint8\_t \*)fb-\>data + y \* fb-\>pitch + x \* 2U);}static const char \*prv\_ok(bool ok) {return ok ? "ok" : "wrong";}int main(void) {const struct pbl\_display\_props \*props = pbl\_display\_get\_props(DISPLAY);const struct pbl\_gpu\_caps \*caps = pbl\_gpu\_get\_caps(GPU);struct pbl\_gpu\_rect bar = {.x = 0, .y = 0, .width = props-\>width, .height = 60};struct pbl\_gpu\_buffer sprite = {.pitch = SPRITE \* sizeof(uint32\_t),.width = SPRITE,.height = SPRITE,.format = PBL\_GPU\_FORMAT\_ARGB8888,};struct pbl\_gpu\_blit blit = {.src = &sprite, .x = 20, .y = 80, .alpha = PBL\_GPU\_ALPHA\_OPAQUE};struct pbl\_display\_buffer frame;struct pbl\_gpu\_buffer fb;unsigned int us;uint32\_t \*px;int ret;if (props-\>format != PBL\_DISPLAY\_FORMAT\_RGB565 ||(caps-\>dst\_formats & PBL\_BIT(PBL\_GPU\_FORMAT\_RGB565)) == 0U) {fprintf(stderr, "gpu: no RGB565 frame to draw into\n");return -ENOTSUP;}ret = pbl\_display\_get\_framebuffer(DISPLAY, &frame);if (ret != 0) {fprintf(stderr, "gpu: no frame (%d)\n", ret);return ret;}/\* The GPU draws into buffers the caller owns: here the frame the display sends. \*/fb = (struct pbl\_gpu\_buffer){.data = frame.data,.pitch = frame.pitch,.width = props-\>width,.height = props-\>height,.format = PBL\_GPU\_FORMAT\_RGB565,};printf("gpu: %dx%d frame\n", fb.width, fb.height);/\* An opaque color replaces what the rectangle holds: no rectangle is the whole buffer. \*/prv\_start();ret = pbl\_gpu\_fill(GPU, &fb, nullptr, 0xFF202040);us = prv\_elapsed\_us();if (ret != 0) {fprintf(stderr, "gpu: fill failed (%d)\n", ret);return ret;}printf("gpu: opaque fill: %s, %u us\n",prv\_ok(prv\_pixel(&fb, 5, 5) == RGB565(0x20, 0x20, 0x40) &&prv\_pixel(&fb, fb.width - 1U, fb.height - 1U) == RGB565(0x20, 0x20, 0x40)),us);/\* Any other alpha blends over it: half white over the background. \*/prv\_start();ret = pbl\_gpu\_fill(GPU, &fb, &bar, 0x80FFFFFF);us = prv\_elapsed\_us();if (ret != 0) {fprintf(stderr, "gpu: blended fill failed (%d)\n", ret);return ret;}printf("gpu: blended fill: %s, %u us\n",prv\_ok(prv\_pixel(&fb, 5, 5) != RGB565(0x20, 0x20, 0x40) &&prv\_pixel(&fb, 5, 5) != RGB565(0xFF, 0xFF, 0xFF) &&prv\_pixel(&fb, 5, 70) == RGB565(0x20, 0x20, 0x40)),us);/\* A source with its own alpha: opaque red on the left, fading out to the right. \*/sprite.data = pbl\_malloc(sprite.pitch \* SPRITE);if (sprite.data == nullptr) {fprintf(stderr, "gpu: no memory for the sprite\n");return -ENOMEM;}px = sprite.data;for (uint32\_t y = 0U; y \< SPRITE; y++) {for (uint32\_t x = 0U; x \< SPRITE; x++) {uint32\_t alpha = x \< SPRITE / 2U ? 0xFFU : 0xFFU - (x - SPRITE / 2U) \* 0xFFU / (SPRITE / 2U);px[y \* SPRITE + x] = (alpha \<\< 24) | 0xFF0000U;}}prv\_start();ret = pbl\_gpu\_blit(GPU, &fb, nullptr, &blit);us = prv\_elapsed\_us();if (ret != 0) {fprintf(stderr, "gpu: blit failed (%d)\n", ret);goto out;}printf("gpu: blit: %s, %u us\n",prv\_ok(prv\_pixel(&fb, 21, 81) == RGB565(0xFF, 0, 0) &&prv\_pixel(&fb, 20 + SPRITE + 1, 81) == RGB565(0x20, 0x20, 0x40)),us);/\* The alpha of the blit is on top of the source's. \*/blit.x = 150;blit.alpha = 128;prv\_start();ret = pbl\_gpu\_blit(GPU, &fb, nullptr, &blit);us = prv\_elapsed\_us();if (ret != 0) {fprintf(stderr, "gpu: blended blit failed (%d)\n", ret);goto out;}printf("gpu: blended blit: %s, %u us\n",prv\_ok(prv\_pixel(&fb, 151, 81) != RGB565(0xFF, 0, 0) &&prv\_pixel(&fb, 151, 81) != RGB565(0x20, 0x20, 0x40)),us);/\* \* A transform turns and scales the source around a pivot, which stays where it was. What a GPU \* cannot do it refuses before drawing, for the caller to draw it otherwise. \*/blit.alpha = PBL\_GPU\_ALPHA\_OPAQUE;for (size\_t i = 0U; i \< ARRAY\_LENGTH(s\_transforms); i++) {const struct transform \*t = &s\_transforms[i];struct pbl\_gpu\_transform transform = {.angle = t-\>angle,.scale\_x = t-\>scale,.scale\_y = t-\>scale,.pivot\_x = SPRITE / 2U,.pivot\_y = SPRITE / 2U,};/\* The pivot, a point of the opaque half and one the source never reaches. \*/const uint16\_t px = t-\>x + SPRITE / 2U;const uint16\_t py = t-\>y + SPRITE / 2U;blit.x = t-\>x;blit.y = t-\>y;blit.transform = &transform;prv\_start();ret = pbl\_gpu\_blit(GPU, &fb, nullptr, &blit);us = prv\_elapsed\_us();if (ret == -ENOTSUP) {printf("gpu: %s blit: not supported\n", t-\>name);} else if (ret != 0) {fprintf(stderr, "gpu: %s blit failed (%d)\n", t-\>name, ret);goto out;} else {printf("gpu: %s blit: %s, %u us\n", t-\>name,prv\_ok(prv\_pixel(&fb, px - 2U, py) == RGB565(0xFF, 0, 0) &&prv\_pixel(&fb, px + t-\>in\_x, py + t-\>in\_y) == RGB565(0xFF, 0, 0) &&prv\_pixel(&fb, px + t-\>out\_x, py + t-\>out\_y) == RGB565(0x20, 0x20, 0x40)),us);}}(void)pbl\_backlight\_set\_brightness(BACKLIGHT, PBL\_BACKLIGHT\_BRIGHTNESS\_MAX);ret = pbl\_display\_update\_sync(DISPLAY, &frame, 0U, props-\>height - 1U);if (ret != 0) {fprintf(stderr, "gpu: display update failed (%d)\n", ret);goto out;}printf("Sample done\n");out:pbl\_free(sprite.data);return ret;}
```

## Output

```
gpu: \d+x\d+ frame
gpu: opaque fill: ok, \d+ us
gpu: blended fill: ok, \d+ us
gpu: blit: ok, \d+ us
gpu: blended blit: ok, \d+ us
gpu: rotated blit: (ok, \d+ us|not supported)
gpu: scaled blit: (ok, \d+ us|not supported)
gpu: rotated and scaled blit: (ok, \d+ us|not supported)
Sample done
```

Generated from PebbleOS [7753e801d](https://github.com/coredevices/pebbleos/commit/7753e801d93d20f2d37534ce2fa69d9952015237). This page is maintained in the pebbleos repository: [docs/samples/drivers/gpu.md](https://github.com/coredevices/pebbleos/blob/7753e801d93d20f2d37534ce2fa69d9952015237/docs/samples/drivers/gpu.md).
