aux/util: Add u_frame_clone

This commit is contained in:
Moses Turner 2021-08-24 08:10:13 -05:00 committed by Moses Turner
parent 815aa47ac2
commit a1b683385f
2 changed files with 42 additions and 0 deletions

View file

@ -42,3 +42,39 @@ u_frame_create_one_off(enum xrt_format f, uint32_t width, uint32_t height, struc
xrt_frame_reference(out_frame, xf);
}
static void
free_clone(struct xrt_frame *xf)
{
assert(xf->reference.count == 0);
free(xf->data);
free(xf);
}
void
u_frame_clone(struct xrt_frame *to_copy, struct xrt_frame **out_frame)
{
struct xrt_frame *xf = U_TYPED_CALLOC(struct xrt_frame);
// Paranoia: Explicitly only copy the fields we want
xf->width = to_copy->width;
xf->height = to_copy->height;
xf->stride = to_copy->stride;
xf->size = to_copy->size;
xf->format = to_copy->format;
xf->stereo_format = to_copy->stereo_format;
xf->timestamp = to_copy->timestamp;
xf->source_timestamp = to_copy->source_timestamp;
xf->source_sequence = to_copy->source_sequence;
xf->source_id = to_copy->source_id;
xf->destroy = free_clone;
xf->data = malloc(xf->size);
memcpy(xf->data, to_copy->data, xf->size);
xrt_frame_reference(out_frame, xf);
}

View file

@ -23,6 +23,12 @@ extern "C" {
void
u_frame_create_one_off(enum xrt_format f, uint32_t width, uint32_t height, struct xrt_frame **out_frame);
/*!
* Clones a frame. The cloned frame is not freed when the original frame is freed; instead the cloned frame is freed
* when its reference reaches zero.
*/
void
u_frame_clone(struct xrt_frame *to_copy, struct xrt_frame **out_frame);
#ifdef __cplusplus
}