Replace map_fbid_inflightflips pair with a struct

This makes it explicit what "first" and "second" are. This also allows
to add more fields.
This commit is contained in:
Simon Ser 2020-05-19 14:05:52 +02:00
parent 02b233429f
commit 0ee8d451e4
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
4 changed files with 25 additions and 17 deletions

View file

@ -207,11 +207,11 @@ static void page_flip_handler(int fd, unsigned int frame,
{ {
uint32_t previous_fbid = g_DRM.fbids_on_screen[ i ]; uint32_t previous_fbid = g_DRM.fbids_on_screen[ i ];
assert( previous_fbid != 0 ); assert( previous_fbid != 0 );
assert( g_DRM.map_fbid_inflightflips[ previous_fbid ].second > 0 ); assert( g_DRM.map_fbid_inflightflips[ previous_fbid ].n_refs > 0 );
g_DRM.map_fbid_inflightflips[ previous_fbid ].second--; g_DRM.map_fbid_inflightflips[ previous_fbid ].n_refs--;
if ( g_DRM.map_fbid_inflightflips[ previous_fbid ].second == 0 ) if ( g_DRM.map_fbid_inflightflips[ previous_fbid ].n_refs == 0 )
{ {
// we flipped away from this previous fbid, now safe to delete // we flipped away from this previous fbid, now safe to delete
std::lock_guard<std::mutex> lock( g_DRM.free_queue_lock ); std::lock_guard<std::mutex> lock( g_DRM.free_queue_lock );
@ -575,8 +575,8 @@ int drm_atomic_commit(struct drm_t *drm, struct Composite_t *pComposite, struct
// potentially beat us to the refcount checks. // potentially beat us to the refcount checks.
for ( uint32_t i = 0; i < drm->fbids_in_req.size(); i++ ) for ( uint32_t i = 0; i < drm->fbids_in_req.size(); i++ )
{ {
assert( g_DRM.map_fbid_inflightflips[ drm->fbids_in_req[ i ] ].first == true ); assert( g_DRM.map_fbid_inflightflips[ drm->fbids_in_req[ i ] ].held == true );
g_DRM.map_fbid_inflightflips[ drm->fbids_in_req[ i ] ].second++; g_DRM.map_fbid_inflightflips[ drm->fbids_in_req[ i ] ].n_refs++;
} }
g_DRM.flipcount++; g_DRM.flipcount++;
@ -597,7 +597,7 @@ int drm_atomic_commit(struct drm_t *drm, struct Composite_t *pComposite, struct
// Undo refcount if the commit didn't actually work // Undo refcount if the commit didn't actually work
for ( uint32_t i = 0; i < drm->fbids_in_req.size(); i++ ) for ( uint32_t i = 0; i < drm->fbids_in_req.size(); i++ )
{ {
g_DRM.map_fbid_inflightflips[ drm->fbids_in_req[ i ] ].second--; g_DRM.map_fbid_inflightflips[ drm->fbids_in_req[ i ] ].n_refs--;
} }
g_DRM.flipcount--; g_DRM.flipcount--;
@ -647,21 +647,22 @@ uint32_t drm_fbid_from_dmabuf( struct drm_t *drm, struct wlr_dmabuf_attributes *
{ {
printf("make fbid %u\n", fb_id); printf("make fbid %u\n", fb_id);
} }
assert( drm->map_fbid_inflightflips[ fb_id ].first == false ); assert( drm->map_fbid_inflightflips[ fb_id ].held == false );
drm->map_fbid_inflightflips[ fb_id ].first = true; drm->map_fbid_inflightflips[ fb_id ].held = true;
drm->map_fbid_inflightflips[ fb_id ].second = 0; drm->map_fbid_inflightflips[ fb_id ].n_refs = 0;
return fb_id; return fb_id;
} }
void drm_free_fbid( struct drm_t *drm, uint32_t fbid ) void drm_drop_fbid( struct drm_t *drm, uint32_t fbid )
{ {
assert( drm->map_fbid_inflightflips[ fbid ].first == true ); assert( drm->map_fbid_inflightflips[ fbid ].held == true );
drm->map_fbid_inflightflips[ fbid ].first = false; drm->map_fbid_inflightflips[ fbid ].held = false;
if ( drm->map_fbid_inflightflips[ fbid ].second == 0 ) if ( drm->map_fbid_inflightflips[ fbid ].n_refs == 0 )
{ {
/* FB isn't being used in any page-flip, free it immediately */
if ( s_drm_log != 0 ) if ( s_drm_log != 0 )
{ {
printf("free fbid %u\n", fbid); printf("free fbid %u\n", fbid);

View file

@ -37,6 +37,13 @@ struct connector {
drmModePropertyRes **props_info; drmModePropertyRes **props_info;
}; };
struct fb {
/* A FB is held if it's being used by steamcompmgr */
bool held;
/* Number of page-flips using the FB */
std::atomic< uint32_t > n_refs;
};
struct drm_t { struct drm_t {
int fd; int fd;
@ -64,7 +71,7 @@ struct drm_t {
std::vector < uint32_t > fbids_in_req; std::vector < uint32_t > fbids_in_req;
std::vector < uint32_t > fbids_on_screen; std::vector < uint32_t > fbids_on_screen;
std::unordered_map< uint32_t, std::pair< bool, std::atomic< uint32_t > > > map_fbid_inflightflips; std::unordered_map< uint32_t, struct fb > map_fbid_inflightflips;
std::mutex free_queue_lock; std::mutex free_queue_lock;
std::vector< uint32_t > fbid_free_queue; std::vector< uint32_t > fbid_free_queue;
@ -91,7 +98,7 @@ extern bool g_bDebugLayers;
int init_drm(struct drm_t *drm, const char *device, const char *mode_str, unsigned int vrefresh); int init_drm(struct drm_t *drm, const char *device, const char *mode_str, unsigned int vrefresh);
int drm_atomic_commit(struct drm_t *drm, struct Composite_t *pComposite, struct VulkanPipeline_t *pPipeline ); int drm_atomic_commit(struct drm_t *drm, struct Composite_t *pComposite, struct VulkanPipeline_t *pPipeline );
uint32_t drm_fbid_from_dmabuf( struct drm_t *drm, struct wlr_dmabuf_attributes *dma_buf ); uint32_t drm_fbid_from_dmabuf( struct drm_t *drm, struct wlr_dmabuf_attributes *dma_buf );
void drm_free_fbid( struct drm_t *drm, uint32_t fbid ); void drm_drop_fbid( struct drm_t *drm, uint32_t fbid );
bool drm_can_avoid_composite( struct drm_t *drm, struct Composite_t *pComposite, struct VulkanPipeline_t *pPipeline ); bool drm_can_avoid_composite( struct drm_t *drm, struct Composite_t *pComposite, struct VulkanPipeline_t *pPipeline );
#ifndef C_SIDE #ifndef C_SIDE

View file

@ -428,7 +428,7 @@ CVulkanTexture::~CVulkanTexture( void )
if ( m_FBID != 0 ) if ( m_FBID != 0 )
{ {
drm_free_fbid( &g_DRM, m_FBID ); drm_drop_fbid( &g_DRM, m_FBID );
m_FBID = 0; m_FBID = 0;
} }

View file

@ -498,7 +498,7 @@ release_commit ( commit_t &commit )
{ {
if ( commit.fb_id != 0 ) if ( commit.fb_id != 0 )
{ {
drm_free_fbid( &g_DRM, commit.fb_id ); drm_drop_fbid( &g_DRM, commit.fb_id );
commit.fb_id = 0; commit.fb_id = 0;
} }