Untitled

 avatar
unknown
plain_text
a month ago
2.9 kB
3
Indexable
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <gbm.h>
#include <string.h>
#include <errno.h>

int main() {
    printf("Starting GBM test...\n");
    
    int drm_fd = open("/dev/dri/card0", O_RDWR);
    if (drm_fd < 0) {
        printf("Failed to open DRM device: %s\n", strerror(errno));
        return 1;
    }
    printf("DRM device opened (fd: %d)\n", drm_fd);

    struct gbm_device *gbm = gbm_create_device(drm_fd);
    if (!gbm) {
        printf("Failed to create GBM device: %s\n", strerror(errno));
        close(drm_fd);
        return 1;
    }
    printf("GBM device created successfully\n");

    // Use the known working combination
    struct gbm_bo *bo =  gbm_bo_create(gbm, 64, 64, GBM_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING | GBM_BO_USE_LINEAR);
/*gbm_bo_create(gbm,
        64, 64,
        GBM_FORMAT_XRGB8888,
        GBM_BO_USE_SCANOUT);*/

//    gbm_bo_create(rdp_alloc->gbm, 64, 64, GBM_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING | GBM_BO_USE_LINEAR);

    if (!bo) {
        printf("Failed to create GBM buffer: %s\n", strerror(errno));
        gbm_device_destroy(gbm);
        close(drm_fd);
        return 1;
    }
    printf("Buffer created successfully\n");

    // Try to map the buffer
    uint32_t stride;
    void *data;
    void *map_data = gbm_bo_map(bo, 
        0, 0,      // offset x,y
        64, 64,    // width, height
        GBM_BO_TRANSFER_WRITE | GBM_BO_TRANSFER_READ,
        &stride,
        &data);

    if (!map_data || !data) {
        printf("Failed to map GBM buffer: %s\n", strerror(errno));
        gbm_bo_destroy(bo);
        gbm_device_destroy(gbm);
        close(drm_fd);
        return 1;
    }
    printf("Buffer mapped successfully:\n");
    printf("  data: %p\n", data);
    printf("  map_data: %p\n", map_data);
    printf("  stride: %d bytes\n", stride);

    // Try writing a test pattern
    uint32_t *pixels = (uint32_t *)data;
    uint32_t pixels_per_row = stride / 4;
    
    printf("Writing test pattern...\n");
    for (int y = 0; y < 64; y++) {
        for (int x = 0; x < 64; x++) {
            // Create a gradient pattern
            uint8_t r = (x * 255) / 63;
            uint8_t g = (y * 255) / 63;
            uint8_t b = 128;
            pixels[y * pixels_per_row + x] = (r << 16) | (g << 8) | b;
        }
    }
    printf("Pattern written\n");

    // Try to read back some pixels
    printf("Reading back test pixels:\n");
    for (int y = 0; y < 3; y++) {
        for (int x = 0; x < 3; x++) {
            uint32_t pixel = pixels[y * pixels_per_row + x];
            printf("  Pixel[%d,%d] = 0x%08x\n", x, y, pixel);
        }
    }

    printf("Unmapping buffer...\n");
    gbm_bo_unmap(bo, map_data);
    printf("Unmap successful\n");

    printf("Cleaning up...\n");
    gbm_bo_destroy(bo);
    gbm_device_destroy(gbm);
    close(drm_fd);

    printf("Test completed successfully!\n");
    return 0;
}
Leave a Comment