Skip to content

Commit

Permalink
Respond to PR comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
aliddell committed Jul 26, 2024
1 parent 369915d commit 50bf046
Showing 1 changed file with 42 additions and 53 deletions.
95 changes: 42 additions & 53 deletions acquire-driver-common/src/simcams/simulated.camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ struct SimulatedCamera

struct
{
void* data;
void* frame_data; // for storing the image
void* render_data; // for rendering the image
struct ImageShape shape;
struct lock lock;
int64_t frame_id;
Expand Down Expand Up @@ -218,50 +219,38 @@ simulated_camera_streamer_thread(struct SimulatedCamera* self)
{
clock_init(&self->streamer.throttle);

size_t bytes_of_data = aligned_bytes_of_image(&self->im.shape);
uint8_t* data = (uint8_t*)malloc(bytes_of_data);
int64_t frame_id = self->im.frame_id;

const float exposure_time_ms = self->properties.exposure_time_us * 1e-3f;

while (self->streamer.is_running) {
if (self->properties.input_triggers.frame_start.enable) {
ECHO(lock_acquire(&self->im.lock));
while (!self->software_trigger.triggered) {
ECHO(condition_variable_wait(
&self->software_trigger.trigger_ready, &self->im.lock));
}
self->software_trigger.triggered = 0;
ECHO(lock_release(&self->im.lock));
}

clock_tic(&self->streamer.throttle);

struct ImageShape full = { 0 };
uint32_t origin[2] = { 0, 0 };

ECHO(lock_acquire(&self->im.lock));
while (self->properties.input_triggers.frame_start.enable &&
!self->software_trigger.triggered) {
ECHO(condition_variable_wait(&self->software_trigger.trigger_ready,
&self->im.lock));
}
self->software_trigger.triggered = 0;

// compute the full resolution shape and offset
{
ECHO(lock_acquire(&self->im.lock));
ECHO(compute_full_resolution_shape_and_offset(self, &full, origin));
ECHO(lock_release(&self->im.lock));
ECHO(compute_full_resolution_shape_and_offset(self, &full, origin));

size_t nbytes = aligned_bytes_of_image(&full);
if (nbytes > bytes_of_data) {
free(data);
data = (uint8_t*)malloc(nbytes);
bytes_of_data = nbytes;
}
}
const float exposure_time_ms =
self->properties.exposure_time_us * 1e-3f;
ECHO(lock_release(&self->im.lock));

clock_tic(&self->streamer.throttle);

// generate the image
switch (self->kind) {
case BasicDevice_Camera_Random:
im_fill_rand(&full, data);
im_fill_rand(&full, self->im.render_data);
break;
case BasicDevice_Camera_Sin:
ECHO(im_fill_pattern(
&full, (float)origin[0], (float)origin[1], data));
ECHO(im_fill_pattern(&full,
(float)origin[0],
(float)origin[1],
self->im.render_data));
break;
case BasicDevice_Camera_Empty:
break; // do nothing
Expand All @@ -277,7 +266,7 @@ simulated_camera_streamer_thread(struct SimulatedCamera* self)
int h = full.dims.height;
int b = self->properties.binning >> 1;
while (b) {
ECHO(bin2(data, w, h));
ECHO(bin2(self->im.render_data, w, h));
b >>= 1;
w >>= 1;
h >>= 1;
Expand All @@ -286,32 +275,28 @@ simulated_camera_streamer_thread(struct SimulatedCamera* self)

++frame_id;

// sleep for the remainder of the exposure time
float toc = (float)clock_toc_ms(&self->streamer.throttle);

// TODO (aliddell):
// preferably this would be a wait on a condition variable with a
// timeout, but we don't have that in the platform yet.
if (self->streamer.is_running && toc < exposure_time_ms) {
if (self->streamer.is_running) {
clock_sleep_ms(&self->streamer.throttle, exposure_time_ms - toc);
}

if (self->im.frame_wanted) {
ECHO(lock_acquire(&self->im.lock));

size_t nbytes = aligned_bytes_of_image(&self->im.shape);
if (self->kind != BasicDevice_Camera_Empty) {
memcpy(self->im.data, data, bytes_of_data);
memcpy(self->im.frame_data, self->im.render_data, nbytes);
}

self->hardware_timestamp = clock_tic(0);
self->im.frame_id = frame_id;
self->im.frame_wanted = 0;

ECHO(condition_variable_notify_all(&self->im.frame_ready));
ECHO(lock_release(&self->im.lock));
}

ECHO(condition_variable_notify_all(&self->im.frame_ready));
}

free(data);
}

//
Expand Down Expand Up @@ -419,8 +404,11 @@ simcam_set(struct Camera* camera, struct CameraProperties* settings)
};

size_t nbytes = aligned_bytes_of_image(shape);
self->im.data = malloc(nbytes);
EXPECT(self->im.data, "Allocation of %llu bytes failed.", nbytes);
self->im.frame_data = malloc(nbytes);
EXPECT(self->im.frame_data, "Allocation of %llu bytes failed.", nbytes);

self->im.render_data = malloc(nbytes);
EXPECT(self->im.render_data, "Allocation of %llu bytes failed.", nbytes);

return Device_Ok;
Error:
Expand Down Expand Up @@ -468,9 +456,8 @@ simcam_execute_trigger(struct Camera* camera)
struct SimulatedCamera* self =
containerof(camera, struct SimulatedCamera, camera);

self->im.frame_wanted = 1;

lock_acquire(&self->im.lock);
self->im.frame_wanted = 1;
self->software_trigger.triggered = 1;
condition_variable_notify_all(&self->software_trigger.trigger_ready);
lock_release(&self->im.lock);
Expand Down Expand Up @@ -505,12 +492,11 @@ simcam_get_frame(struct Camera* camera,
CHECK(*nbytes >= bytes_of_image(&self->im.shape));
CHECK(self->streamer.is_running);

self->im.frame_wanted = 1;

TRACE("last: %5d current %5d",
self->im.last_emitted_frame_id,
self->im.frame_id);
ECHO(lock_acquire(&self->im.lock));
self->im.frame_wanted = 1;

while (self->streamer.is_running &&
self->im.last_emitted_frame_id >= self->im.frame_id) {
Expand All @@ -521,7 +507,7 @@ simcam_get_frame(struct Camera* camera,
goto Shutdown;
}

memcpy(im, self->im.data, bytes_of_image(&self->im.shape)); // NOLINT
memcpy(im, self->im.frame_data, bytes_of_image(&self->im.shape)); // NOLINT
info_out->shape = self->im.shape;
info_out->hardware_frame_id = self->im.frame_id;
info_out->hardware_timestamp = self->hardware_timestamp;
Expand All @@ -539,8 +525,10 @@ simcam_close_camera(struct Camera* camera_)
containerof(camera_, struct SimulatedCamera, camera);
EXPECT(camera_, "Invalid NULL parameter");
simcam_stop(&camera->camera);
if (camera->im.data)
free(camera->im.data);
if (camera->im.frame_data)
free(camera->im.frame_data);
if (camera->im.render_data)
free(camera->im.render_data);
free(camera);
return Device_Ok;
Error:
Expand Down Expand Up @@ -569,7 +557,8 @@ simcam_make_camera(enum BasicDeviceKind kind)
.properties = properties,
.kind=kind,
.im={
.data=0,
.frame_data=0,
.render_data=0,
.shape = {
.dims = {
.channels = 1,
Expand Down

0 comments on commit 50bf046

Please sign in to comment.