app/src/main/jni/SDL/src/video/kmsdrm/SDL_kmsdrmmouse.c
static SDL_Cursor *
KMSDRM_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
{
    SDL_VideoDevice *dev = SDL_GetVideoDevice();
    SDL_VideoData *vdata = ((SDL_VideoData *)dev->driverdata);
    SDL_PixelFormat *pixlfmt = surface->format;
    KMSDRM_CursorData *curdata;
    SDL_Cursor *cursor;
    int i, ret;
    uint32_t bo_format, bo_stride;
    char *buffer = NULL;
    size_t bufsize;

    switch(pixlfmt->format) {
    case SDL_PIXELFORMAT_RGB332:
        bo_format = GBM_FORMAT_RGB332;
        break;
    case SDL_PIXELFORMAT_ARGB4444:
        bo_format = GBM_FORMAT_ARGB4444;
        break;
    case SDL_PIXELFORMAT_RGBA4444:
        bo_format = GBM_FORMAT_RGBA4444;
        break;
    case SDL_PIXELFORMAT_ABGR4444:
        bo_format = GBM_FORMAT_ABGR4444;
        break;
    case SDL_PIXELFORMAT_BGRA4444:
        bo_format = GBM_FORMAT_BGRA4444;
        break;
    case SDL_PIXELFORMAT_ARGB1555:
        bo_format = GBM_FORMAT_ARGB1555;
        break;
    case SDL_PIXELFORMAT_RGBA5551:
        bo_format = GBM_FORMAT_RGBA5551;
        break;
    case SDL_PIXELFORMAT_ABGR1555:
        bo_format = GBM_FORMAT_ABGR1555;
        break;
    case SDL_PIXELFORMAT_BGRA5551:
        bo_format = GBM_FORMAT_BGRA5551;
        break;
    case SDL_PIXELFORMAT_RGB565:
        bo_format = GBM_FORMAT_RGB565;
        break;
    case SDL_PIXELFORMAT_BGR565:
        bo_format = GBM_FORMAT_BGR565;
        break;
    case SDL_PIXELFORMAT_RGB888:
    case SDL_PIXELFORMAT_RGB24:
        bo_format = GBM_FORMAT_RGB888;
        break;
    case SDL_PIXELFORMAT_BGR888:
    case SDL_PIXELFORMAT_BGR24:
        bo_format = GBM_FORMAT_BGR888;
        break;
    case SDL_PIXELFORMAT_RGBX8888:
        bo_format = GBM_FORMAT_RGBX8888;
        break;
    case SDL_PIXELFORMAT_BGRX8888:
        bo_format = GBM_FORMAT_BGRX8888;
        break;
    case SDL_PIXELFORMAT_ARGB8888:
        bo_format = GBM_FORMAT_ARGB8888;
        break;
    case SDL_PIXELFORMAT_RGBA8888:
        bo_format = GBM_FORMAT_RGBA8888;
        break;
    case SDL_PIXELFORMAT_ABGR8888:
        bo_format = GBM_FORMAT_ABGR8888;
        break;
    case SDL_PIXELFORMAT_BGRA8888:
        bo_format = GBM_FORMAT_BGRA8888;
        break;
    case SDL_PIXELFORMAT_ARGB2101010:
        bo_format = GBM_FORMAT_ARGB2101010;
        break;
    default:
        SDL_SetError("Unsupported pixel format for cursor");
        return NULL;
    }

    if (!KMSDRM_gbm_device_is_format_supported(vdata->gbm, bo_format, GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE)) {
        SDL_SetError("Unsupported pixel format for cursor");
        return NULL;
    }

    cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor));
    if (cursor == NULL) {
        SDL_OutOfMemory();
        return NULL;
    }
    curdata = (KMSDRM_CursorData *) SDL_calloc(1, sizeof(*curdata));
    if (curdata == NULL) {
        SDL_OutOfMemory();
        SDL_free(cursor);
        return NULL;
    }

    curdata->hot_x = hot_x;
    curdata->hot_y = hot_y;
    curdata->w = surface->w;
    curdata->h = surface->h;

    curdata->bo = KMSDRM_gbm_bo_create(vdata->gbm, surface->w, surface->h, bo_format,
                                       GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
    if (curdata->bo == NULL) {
        SDL_SetError("Could not create GBM cursor BO");
        goto cleanup;
    }

    bo_stride = KMSDRM_gbm_bo_get_stride(curdata->bo);
    bufsize = bo_stride * surface->h;

    if (surface->pitch != bo_stride) {
        /* pitch doesn't match stride, must be copied to temp buffer  */
        buffer = SDL_malloc(bufsize);
        if (buffer == NULL) {
            SDL_OutOfMemory();
            goto cleanup;
        }

        if (SDL_MUSTLOCK(surface)) {
            if (SDL_LockSurface(surface) < 0) {
                /* Could not lock surface */
                goto cleanup;
            }
        }

        /* Copy to temporary buffer */
        for (i = 0; i < surface->h; i++) {
            SDL_memcpy(buffer + (i * bo_stride),
                       ((char *)surface->pixels) + (i * surface->pitch),
                       surface->w * pixlfmt->BytesPerPixel);
        }

        if (SDL_MUSTLOCK(surface)) {
            SDL_UnlockSurface(surface);
        }

        if (KMSDRM_gbm_bo_write(curdata->bo, buffer, bufsize)) {
            SDL_SetError("Could not write to GBM cursor BO");
            goto cleanup;
        }

        /* Free temporary buffer */
        SDL_free(buffer);
        buffer = NULL;
    } else {
        /* surface matches BO format */
        if (SDL_MUSTLOCK(surface)) {
            if (SDL_LockSurface(surface) < 0) {
                /* Could not lock surface */
                goto cleanup;
            }
        }

        ret = KMSDRM_gbm_bo_write(curdata->bo, surface->pixels, bufsize);

        if (SDL_MUSTLOCK(surface)) {
            SDL_UnlockSurface(surface);
        }

        if (ret) {
            SDL_SetError("Could not write to GBM cursor BO");
            goto cleanup;
        }
    }

    cursor->driverdata = curdata;

    return cursor;

cleanup:
    if (buffer != NULL) {
        SDL_free(buffer);
    }
    if (cursor != NULL) {
        SDL_free(cursor);
    }
    if (curdata != NULL) {
        if (curdata->bo != NULL) {
            KMSDRM_gbm_bo_destroy(curdata->bo);
        }
        SDL_free(curdata);
    }
    return NULL;
}
/* Create a cursor from a surface */
Create a cursor from a surface
Create a cursor from a surface
static SDL_Cursor *
KMSDRM_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
{
    SDL_VideoDevice *dev = SDL_GetVideoDevice();
    SDL_VideoData *vdata = ((SDL_VideoData *)dev->driverdata);
    SDL_PixelFormat *pixlfmt = surface->format;
    KMSDRM_CursorData *curdata;
    SDL_Cursor *cursor;
    int i, ret;
    uint32_t bo_format, bo_stride;
    char *buffer = NULL;
    size_t bufsize;
    switch(pixlfmt->format) {
    case SDL_PIXELFORMAT_RGB332:
        bo_format = GBM_FORMAT_RGB332;
        break;
    case SDL_PIXELFORMAT_ARGB4444:
        bo_format = GBM_FORMAT_ARGB4444;
        break;
    case SDL_PIXELFORMAT_RGBA4444:
        bo_format = GBM_FORMAT_RGBA4444;
        break;
    case SDL_PIXELFORMAT_ABGR4444:
        bo_format = GBM_FORMAT_ABGR4444;
        break;
    case SDL_PIXELFORMAT_BGRA4444:
        bo_format = GBM_FORMAT_BGRA4444;
        break;
    case SDL_PIXELFORMAT_ARGB1555:
        bo_format = GBM_FORMAT_ARGB1555;
        break;
    case SDL_PIXELFORMAT_RGBA5551:
        bo_format = GBM_FORMAT_RGBA5551;
        break;
    case SDL_PIXELFORMAT_ABGR1555:
        bo_format = GBM_FORMAT_ABGR1555;
        break;
    case SDL_PIXELFORMAT_BGRA5551:
        bo_format = GBM_FORMAT_BGRA5551;
        break;
    case SDL_PIXELFORMAT_RGB565:
        bo_format = GBM_FORMAT_RGB565;
        break;
    case SDL_PIXELFORMAT_BGR565:
        bo_format = GBM_FORMAT_BGR565;
        break;
    case SDL_PIXELFORMAT_RGB888:
    case SDL_PIXELFORMAT_RGB24:
        bo_format = GBM_FORMAT_RGB888;
        break;
    case SDL_PIXELFORMAT_BGR888:
    case SDL_PIXELFORMAT_BGR24:
        bo_format = GBM_FORMAT_BGR888;
        break;
    case SDL_PIXELFORMAT_RGBX8888:
        bo_format = GBM_FORMAT_RGBX8888;
        break;
    case SDL_PIXELFORMAT_BGRX8888:
        bo_format = GBM_FORMAT_BGRX8888;
        break;
    case SDL_PIXELFORMAT_ARGB8888:
        bo_format = GBM_FORMAT_ARGB8888;
        break;
    case SDL_PIXELFORMAT_RGBA8888:
        bo_format = GBM_FORMAT_RGBA8888;
        break;
    case SDL_PIXELFORMAT_ABGR8888:
        bo_format = GBM_FORMAT_ABGR8888;
        break;
    case SDL_PIXELFORMAT_BGRA8888:
        bo_format = GBM_FORMAT_BGRA8888;
        break;
    case SDL_PIXELFORMAT_ARGB2101010:
        bo_format = GBM_FORMAT_ARGB2101010;
        break;
    default:
        SDL_SetError("Unsupported pixel format for cursor");
        return NULL;
    }
    if (!KMSDRM_gbm_device_is_format_supported(vdata->gbm, bo_format, GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE)) {
        SDL_SetError("Unsupported pixel format for cursor");
        return NULL;
    }
    cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor));
    if (cursor == NULL) {
        SDL_OutOfMemory();
        return NULL;
    }
    curdata = (KMSDRM_CursorData *) SDL_calloc(1, sizeof(*curdata));
    if (curdata == NULL) {
        SDL_OutOfMemory();
        SDL_free(cursor);
        return NULL;
    }
    curdata->hot_x = hot_x;
    curdata->hot_y = hot_y;
    curdata->w = surface->w;
    curdata->h = surface->h;
    curdata->bo = KMSDRM_gbm_bo_create(vdata->gbm, surface->w, surface->h, bo_format,
                                       GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
    if (curdata->bo == NULL) {
        SDL_SetError("Could not create GBM cursor BO");
        goto cleanup;
    }
    bo_stride = KMSDRM_gbm_bo_get_stride(curdata->bo);
    bufsize = bo_stride * surface->h;
    if (surface->pitch != bo_stride) {
        buffer = SDL_malloc(bufsize);
        if (buffer == NULL) {
            SDL_OutOfMemory();
            goto cleanup;
        }
        if (SDL_MUSTLOCK(surface)) {
            if (SDL_LockSurface(surface) < 0) {
                goto cleanup;
            }
        }
        for (i = 0; i < surface->h; i++) {
            SDL_memcpy(buffer + (i * bo_stride),
                       ((char *)surface->pixels) + (i * surface->pitch),
                       surface->w * pixlfmt->BytesPerPixel);
        }
        if (SDL_MUSTLOCK(surface)) {
            SDL_UnlockSurface(surface);
        }
        if (KMSDRM_gbm_bo_write(curdata->bo, buffer, bufsize)) {
            SDL_SetError("Could not write to GBM cursor BO");
            goto cleanup;
        }
        SDL_free(buffer);
        buffer = NULL;
    } else {
        if (SDL_MUSTLOCK(surface)) {
            if (SDL_LockSurface(surface) < 0) {
                goto cleanup;
            }
        }
        ret = KMSDRM_gbm_bo_write(curdata->bo, surface->pixels, bufsize);
        if (SDL_MUSTLOCK(surface)) {
            SDL_UnlockSurface(surface);
        }
        if (ret) {
            SDL_SetError("Could not write to GBM cursor BO");
            goto cleanup;
        }
    }
    cursor->driverdata = curdata;
    return cursor;
cleanup:
    if (buffer != NULL) {
        SDL_free(buffer);
    }
    if (cursor != NULL) {
        SDL_free(cursor);
    }
    if (curdata != NULL) {
        if (curdata->bo != NULL) {
            KMSDRM_gbm_bo_destroy(curdata->bo);
        }
        SDL_free(curdata);
    }
    return NULL;
}
static SDL_Cursor * KMSDRM_CreateCursor ( SDL_Surface * surface , int hot_x , int hot_y ) { SDL_VideoDevice * dev = SDL_GetVideoDevice ( ) ; SDL_VideoData * vdata = ( ( SDL_VideoData * ) dev -> driverdata ) ; SDL_PixelFormat * pixlfmt = surface -> format ; KMSDRM_CursorData * curdata ; SDL_Cursor * cursor ; int i , ret ; uint32_t bo_format , bo_stride ; char * buffer = NULL ; size_t bufsize ; switch ( pixlfmt -> format ) { case SDL_PIXELFORMAT_RGB332 : bo_format = GBM_FORMAT_RGB332 ; break ; case SDL_PIXELFORMAT_ARGB4444 : bo_format = GBM_FORMAT_ARGB4444 ; break ; case SDL_PIXELFORMAT_RGBA4444 : bo_format = GBM_FORMAT_RGBA4444 ; break ; case SDL_PIXELFORMAT_ABGR4444 : bo_format = GBM_FORMAT_ABGR4444 ; break ; case SDL_PIXELFORMAT_BGRA4444 : bo_format = GBM_FORMAT_BGRA4444 ; break ; case SDL_PIXELFORMAT_ARGB1555 : bo_format = GBM_FORMAT_ARGB1555 ; break ; case SDL_PIXELFORMAT_RGBA5551 : bo_format = GBM_FORMAT_RGBA5551 ; break ; case SDL_PIXELFORMAT_ABGR1555 : bo_format = GBM_FORMAT_ABGR1555 ; break ; case SDL_PIXELFORMAT_BGRA5551 : bo_format = GBM_FORMAT_BGRA5551 ; break ; case SDL_PIXELFORMAT_RGB565 : bo_format = GBM_FORMAT_RGB565 ; break ; case SDL_PIXELFORMAT_BGR565 : bo_format = GBM_FORMAT_BGR565 ; break ; case SDL_PIXELFORMAT_RGB888 : case SDL_PIXELFORMAT_RGB24 : bo_format = GBM_FORMAT_RGB888 ; break ; case SDL_PIXELFORMAT_BGR888 : case SDL_PIXELFORMAT_BGR24 : bo_format = GBM_FORMAT_BGR888 ; break ; case SDL_PIXELFORMAT_RGBX8888 : bo_format = GBM_FORMAT_RGBX8888 ; break ; case SDL_PIXELFORMAT_BGRX8888 : bo_format = GBM_FORMAT_BGRX8888 ; break ; case SDL_PIXELFORMAT_ARGB8888 : bo_format = GBM_FORMAT_ARGB8888 ; break ; case SDL_PIXELFORMAT_RGBA8888 : bo_format = GBM_FORMAT_RGBA8888 ; break ; case SDL_PIXELFORMAT_ABGR8888 : bo_format = GBM_FORMAT_ABGR8888 ; break ; case SDL_PIXELFORMAT_BGRA8888 : bo_format = GBM_FORMAT_BGRA8888 ; break ; case SDL_PIXELFORMAT_ARGB2101010 : bo_format = GBM_FORMAT_ARGB2101010 ; break ; default : SDL_SetError ( " " ) ; return NULL ; } if ( ! KMSDRM_gbm_device_is_format_supported ( vdata -> gbm , bo_format , GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE ) ) { SDL_SetError ( " " ) ; return NULL ; } cursor = ( SDL_Cursor * ) SDL_calloc ( 1 , sizeof ( * cursor ) ) ; if ( cursor == NULL ) { SDL_OutOfMemory ( ) ; return NULL ; } curdata = ( KMSDRM_CursorData * ) SDL_calloc ( 1 , sizeof ( * curdata ) ) ; if ( curdata == NULL ) { SDL_OutOfMemory ( ) ; SDL_free ( cursor ) ; return NULL ; } curdata -> hot_x = hot_x ; curdata -> hot_y = hot_y ; curdata -> w = surface -> w ; curdata -> h = surface -> h ; curdata -> bo = KMSDRM_gbm_bo_create ( vdata -> gbm , surface -> w , surface -> h , bo_format , GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE ) ; if ( curdata -> bo == NULL ) { SDL_SetError ( " " ) ; goto cleanup ; } bo_stride = KMSDRM_gbm_bo_get_stride ( curdata -> bo ) ; bufsize = bo_stride * surface -> h ; if ( surface -> pitch != bo_stride ) { buffer = SDL_malloc ( bufsize ) ; if ( buffer == NULL ) { SDL_OutOfMemory ( ) ; goto cleanup ; } if ( SDL_MUSTLOCK ( surface ) ) { if ( SDL_LockSurface ( surface ) < 0 ) { goto cleanup ; } } for ( i = 0 ; i < surface -> h ; i ++ ) { SDL_memcpy ( buffer + ( i * bo_stride ) , ( ( char * ) surface -> pixels ) + ( i * surface -> pitch ) , surface -> w * pixlfmt -> BytesPerPixel ) ; } if ( SDL_MUSTLOCK ( surface ) ) { SDL_UnlockSurface ( surface ) ; } if ( KMSDRM_gbm_bo_write ( curdata -> bo , buffer , bufsize ) ) { SDL_SetError ( " " ) ; goto cleanup ; } SDL_free ( buffer ) ; buffer = NULL ; } else { if ( SDL_MUSTLOCK ( surface ) ) { if ( SDL_LockSurface ( surface ) < 0 ) { goto cleanup ; } } ret = KMSDRM_gbm_bo_write ( curdata -> bo , surface -> pixels , bufsize ) ; if ( SDL_MUSTLOCK ( surface ) ) { SDL_UnlockSurface ( surface ) ; } if ( ret ) { SDL_SetError ( " " ) ; goto cleanup ; } } cursor -> driverdata = curdata ; return cursor ; cleanup : if ( buffer != NULL ) { SDL_free ( buffer ) ; } if ( cursor != NULL ) { SDL_free ( cursor ) ; } if ( curdata != NULL ) { if ( curdata -> bo != NULL ) { KMSDRM_gbm_bo_destroy ( curdata -> bo ) ; } SDL_free ( curdata ) ; } return NULL ; }
Create a cursor from a surface
Create a cursor from a surface
/* pitch doesn't match stride, must be copied to temp buffer  */ /* Could not lock surface */ /* Copy to temporary buffer */ /* Free temporary buffer */ /* surface matches BO format */ /* Could not lock surface */