@ -21,6 +21,7 @@
* OTHER DEALINGS IN THE SOFTWARE .
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
@ -39,6 +40,7 @@
# include "util/u_memory.h"
# include "util/u_hash_table.h"
static int ctx_id = 1 ;
static int fence_id = 1 ;
@ -97,13 +99,14 @@ hash_func(void *key)
static int
compare_iovecs ( void * key1 , void * key2 )
{
if ( key1 < key2 )
if ( key1 < key2 ) {
return - 1 ;
if ( key1 > key2 )
} else if ( key1 > key2 ) {
return 1 ;
else
} else {
return 0 ;
}
}
static void free_iovec ( void * value )
{
@ -119,13 +122,17 @@ static int vtest_block_write(int fd, void *buf, int size)
int left ;
int ret ;
left = size ;
do {
ret = write ( fd , ptr , left ) ;
if ( ret < 0 )
if ( ret < 0 ) {
return - errno ;
}
left - = ret ;
ptr + = ret ;
} while ( left ) ;
return size ;
}
@ -135,12 +142,16 @@ static int vtest_block_write_zero(int fd, int size)
int left ;
int ret ;
left = size ;
do {
ret = write ( fd , zero , MIN2 ( left , 256 ) ) ;
if ( ret < 0 )
if ( ret < 0 ) {
return - errno ;
}
left - = ret ;
} while ( left ) ;
return size ;
}
@ -154,11 +165,14 @@ int vtest_block_read(int fd, void *buf, int size)
left = size ;
do {
ret = read ( fd , ptr , left ) ;
if ( ret < = 0 )
if ( ret < = 0 ) {
return ret = = - 1 ? - errno : 0 ;
}
left - = ret ;
ptr + = ret ;
} while ( left ) ;
if ( getenv ( " VTEST_SAVE " ) ) {
if ( savefd = = - 1 ) {
savefd = open ( getenv ( " VTEST_SAVE " ) ,
@ -173,6 +187,7 @@ int vtest_block_read(int fd, void *buf, int size)
exit ( 1 ) ;
}
}
return size ;
}
@ -189,8 +204,9 @@ int vtest_create_renderer(int in_fd, int out_fd, uint32_t length)
/* By default we support version 0 unless VCMD_PROTOCOL_VERSION is sent */
renderer . protocol_version = 0 ;
if ( getenv ( " VTEST_USE_GLX " ) )
if ( getenv ( " VTEST_USE_GLX " ) ) {
ctx = VIRGL_RENDERER_USE_GLX ;
}
if ( getenv ( " VTEST_USE_EGL_SURFACELESS " ) ) {
if ( ctx & VIRGL_RENDERER_USE_GLX ) {
@ -216,8 +232,9 @@ int vtest_create_renderer(int in_fd, int out_fd, uint32_t length)
}
vtestname = calloc ( 1 , length + 1 ) ;
if ( ! vtestname )
if ( ! vtestname ) {
return - 1 ;
}
ret = vtest_block_read ( renderer . in_fd , vtestname , length ) ;
if ( ret ! = ( int ) length ) {
@ -240,8 +257,10 @@ int vtest_ping_protocol_version(UNUSED uint32_t length_dw)
hdr_buf [ VTEST_CMD_LEN ] = VCMD_PING_PROTOCOL_VERSION_SIZE ;
hdr_buf [ VTEST_CMD_ID ] = VCMD_PING_PROTOCOL_VERSION ;
ret = vtest_block_write ( renderer . out_fd , hdr_buf , sizeof ( hdr_buf ) ) ;
if ( ret < 0 )
if ( ret < 0 ) {
return ret ;
}
return 0 ;
}
@ -264,12 +283,14 @@ int vtest_protocol_version(UNUSED uint32_t length_dw)
version_buf [ VCMD_PROTOCOL_VERSION_VERSION ] = renderer . protocol_version ;
ret = vtest_block_write ( renderer . out_fd , hdr_buf , sizeof ( hdr_buf ) ) ;
if ( ret < 0 )
if ( ret < 0 ) {
return ret ;
}
ret = vtest_block_write ( renderer . out_fd , version_buf , sizeof ( version_buf ) ) ;
if ( ret < 0 )
if ( ret < 0 ) {
return ret ;
}
return 0 ;
}
@ -291,22 +312,28 @@ int vtest_send_caps2(UNUSED uint32_t length_dw)
virgl_renderer_get_cap_set ( 2 , & max_ver , & max_size ) ;
if ( max_size = = 0 )
if ( max_size = = 0 ) {
return - 1 ;
}
caps_buf = malloc ( max_size ) ;
if ( ! caps_buf )
if ( ! caps_buf ) {
return - 1 ;
}
virgl_renderer_fill_caps ( 2 , 1 , caps_buf ) ;
hdr_buf [ 0 ] = max_size + 1 ;
hdr_buf [ 1 ] = 2 ;
ret = vtest_block_write ( renderer . out_fd , hdr_buf , 8 ) ;
if ( ret < 0 )
if ( ret < 0 ) {
goto end ;
}
vtest_block_write ( renderer . out_fd , caps_buf , max_size ) ;
if ( ret < 0 )
if ( ret < 0 ) {
goto end ;
}
end :
free ( caps_buf ) ;
@ -323,19 +350,23 @@ int vtest_send_caps(UNUSED uint32_t length_dw)
virgl_renderer_get_cap_set ( 1 , & max_ver , & max_size ) ;
caps_buf = malloc ( max_size ) ;
if ( ! caps_buf )
if ( ! caps_buf ) {
return - 1 ;
}
virgl_renderer_fill_caps ( 1 , 1 , caps_buf ) ;
hdr_buf [ 0 ] = max_size + 1 ;
hdr_buf [ 1 ] = 1 ;
ret = vtest_block_write ( renderer . out_fd , hdr_buf , 8 ) ;
if ( ret < 0 )
if ( ret < 0 ) {
goto end ;
}
vtest_block_write ( renderer . out_fd , caps_buf , max_size ) ;
if ( ret < 0 )
if ( ret < 0 ) {
goto end ;
}
end :
free ( caps_buf ) ;
@ -349,8 +380,9 @@ int vtest_create_resource(UNUSED uint32_t length_dw)
int ret ;
ret = vtest_block_read ( renderer . in_fd , & res_create_buf , sizeof ( res_create_buf ) ) ;
if ( ret ! = sizeof ( res_create_buf ) )
if ( ret ! = sizeof ( res_create_buf ) ) {
return - 1 ;
}
args . handle = res_create_buf [ VCMD_RES_CREATE_RES_HANDLE ] ;
args . target = res_create_buf [ VCMD_RES_CREATE_TARGET ] ;
@ -379,8 +411,9 @@ int vtest_create_resource2(UNUSED uint32_t length_dw)
int ret ;
ret = vtest_block_read ( renderer . in_fd , & res_create_buf , sizeof ( res_create_buf ) ) ;
if ( ret ! = sizeof ( res_create_buf ) )
if ( ret ! = sizeof ( res_create_buf ) ) {
return - 1 ;
}
args . handle = res_create_buf [ VCMD_RES_CREATE2_RES_HANDLE ] ;
args . target = res_create_buf [ VCMD_RES_CREATE2_TARGET ] ;
@ -396,23 +429,27 @@ int vtest_create_resource2(UNUSED uint32_t length_dw)
args . flags = 0 ;
// Check that the handle doesn't already exist.
if ( util_hash_table_get ( renderer . iovec_hash , intptr_to_pointer ( args . handle ) ) )
if ( util_hash_table_get ( renderer . iovec_hash , intptr_to_pointer ( args . handle ) ) ) {
return - EEXIST ;
}
ret = virgl_renderer_resource_create ( & args , NULL , 0 ) ;
if ( ret )
if ( ret ) {
return ret ;
}
virgl_renderer_ctx_attach_resource ( ctx_id , args . handle ) ;
iovec = CALLOC_STRUCT ( iovec ) ;
if ( ! iovec )
if ( ! iovec ) {
return - ENOMEM ;
}
iovec - > iov_len = res_create_buf [ VCMD_RES_CREATE2_DATA_SIZE ] ;
iovec - > iov_base = calloc ( 1 , iovec - > iov_len ) ;
if ( ! iovec - > iov_base )
if ( ! iovec - > iov_base ) {
return - ENOMEM ;
}
virgl_renderer_resource_attach_iov ( args . handle , iovec , 1 ) ;
util_hash_table_set ( renderer . iovec_hash , intptr_to_pointer ( args . handle ) , iovec ) ;
@ -427,8 +464,9 @@ int vtest_resource_unref(UNUSED uint32_t length_dw)
uint32_t handle ;
ret = vtest_block_read ( renderer . in_fd , & res_unref_buf , sizeof ( res_unref_buf ) ) ;
if ( ret ! = sizeof ( res_unref_buf ) )
if ( ret ! = sizeof ( res_unref_buf ) ) {
return - 1 ;
}
handle = res_unref_buf [ VCMD_RES_UNREF_RES_HANDLE ] ;
virgl_renderer_ctx_attach_resource ( ctx_id , handle ) ;
@ -445,12 +483,14 @@ int vtest_submit_cmd(uint32_t length_dw)
uint32_t * cbuf ;
int ret ;
if ( length_dw > UINT_MAX / 4 )
if ( length_dw > UINT_MAX / 4 ) {
return - 1 ;
}
cbuf = malloc ( length_dw * 4 ) ;
if ( ! cbuf )
if ( ! cbuf ) {
return - 1 ;
}
ret = vtest_block_read ( renderer . in_fd , cbuf , length_dw * 4 ) ;
if ( ret ! = ( int ) length_dw * 4 ) {
@ -492,14 +532,16 @@ int vtest_transfer_get(UNUSED uint32_t length_dw)
struct iovec iovec ;
ret = vtest_block_read ( renderer . in_fd , thdr_buf , VCMD_TRANSFER_HDR_SIZE * 4 ) ;
if ( ret ! = VCMD_TRANSFER_HDR_SIZE * 4 )
if ( ret ! = VCMD_TRANSFER_HDR_SIZE * 4 ) {
return ret ;
}
DECODE_TRANSFER ;
ptr = malloc ( data_size ) ;
if ( ! ptr )
if ( ! ptr ) {
return - ENOMEM ;
}
iovec . iov_len = data_size ;
iovec . iov_base = ptr ;
@ -511,8 +553,10 @@ int vtest_transfer_get(UNUSED uint32_t length_dw)
& box ,
0 ,
& iovec , 1 ) ;
if ( ret )
if ( ret ) {
fprintf ( stderr , " transfer read failed %d \n " , ret ) ;
}
ret = vtest_block_write ( renderer . out_fd , ptr , data_size ) ;
free ( ptr ) ;
@ -531,18 +575,21 @@ int vtest_transfer_put(UNUSED uint32_t length_dw)
struct iovec iovec ;
ret = vtest_block_read ( renderer . in_fd , thdr_buf , VCMD_TRANSFER_HDR_SIZE * 4 ) ;
if ( ret ! = VCMD_TRANSFER_HDR_SIZE * 4 )
if ( ret ! = VCMD_TRANSFER_HDR_SIZE * 4 ) {
return ret ;
}
DECODE_TRANSFER ;
ptr = malloc ( data_size ) ;
if ( ! ptr )
if ( ! ptr ) {
return - ENOMEM ;
}
ret = vtest_block_read ( renderer . in_fd , ptr , data_size ) ;
if ( ret < 0 )
if ( ret < 0 ) {
return ret ;
}
iovec . iov_len = data_size ;
iovec . iov_base = ptr ;
@ -554,8 +601,10 @@ int vtest_transfer_put(UNUSED uint32_t length_dw)
& box ,
0 ,
& iovec , 1 ) ;
if ( ret )
if ( ret ) {
fprintf ( stderr , " transfer write failed %d \n " , ret ) ;
}
free ( ptr ) ;
return 0 ;
}
@ -650,8 +699,9 @@ int vtest_transfer_put2(UNUSED uint32_t length_dw)
struct iovec * iovec ;
ret = vtest_block_read ( renderer . in_fd , thdr_buf , sizeof ( thdr_buf ) ) ;
if ( ret ! = sizeof ( thdr_buf ) )
if ( ret ! = sizeof ( thdr_buf ) ) {
return ret ;
}
DECODE_TRANSFER2 ;
@ -691,22 +741,27 @@ int vtest_resource_busy_wait(UNUSED uint32_t length_dw)
vtest_renderer_create_fence ( ) ;
ret = vtest_block_read ( renderer . in_fd , & bw_buf , sizeof ( bw_buf ) ) ;
if ( ret ! = sizeof ( bw_buf ) )
if ( ret ! = sizeof ( bw_buf ) ) {
return - 1 ;
}
/* handle = bw_buf[VCMD_BUSY_WAIT_HANDLE]; unused as of now */
flags = bw_buf [ VCMD_BUSY_WAIT_FLAGS ] ;
if ( flags = = VCMD_BUSY_WAIT_FLAG_WAIT ) {
do {
if ( last_fence = = ( fence_id - 1 ) )
if ( last_fence = = ( fence_id - 1 ) ) {
break ;
}
fd = virgl_renderer_get_poll_fd ( ) ;
if ( fd ! = - 1 )
if ( fd ! = - 1 ) {
vtest_wait_for_fd_read ( fd ) ;
}
virgl_renderer_poll ( ) ;
} while ( 1 ) ;
busy = false ;
} else {
busy = last_fence ! = ( fence_id - 1 ) ;
@ -717,12 +772,14 @@ int vtest_resource_busy_wait(UNUSED uint32_t length_dw)
reply_buf [ 0 ] = busy ? 1 : 0 ;
ret = vtest_block_write ( renderer . out_fd , hdr_buf , sizeof ( hdr_buf ) ) ;
if ( ret < 0 )
if ( ret < 0 ) {
return ret ;
}
ret = vtest_block_write ( renderer . out_fd , reply_buf , sizeof ( reply_buf ) ) ;
if ( ret < 0 )
if ( ret < 0 ) {
return ret ;
}
return 0 ;
}