Continue refactoring of global state

This commit is contained in:
Martijn Braam
2023-07-19 00:59:30 +02:00
parent 0170544013
commit c4969cec6c
5 changed files with 56 additions and 53 deletions

View File

@@ -162,7 +162,8 @@ capture(MPPipeline *pipeline, const void *data)
// Change camera mode for capturing // Change camera mode for capturing
mp_process_pipeline_sync(); mp_process_pipeline_sync();
mp_camera_stop_capture(mpcamera); mp_camera_stop_capture(mpcamera);
libmegapixels_select_mode(io_camera, mode_capture); struct v4l2_format format = {0};
libmegapixels_select_mode(io_camera, mode_capture, &format);
just_switched_mode = true; just_switched_mode = true;
mp_camera_start_capture(mpcamera); mp_camera_start_capture(mpcamera);
@@ -323,7 +324,8 @@ on_frame(MPBuffer buffer, void *_data)
// Go back to preview mode // Go back to preview mode
mp_process_pipeline_sync(); mp_process_pipeline_sync();
mp_camera_stop_capture(mpcamera); mp_camera_stop_capture(mpcamera);
libmegapixels_select_mode(io_camera, mode_preview); struct v4l2_format format = {0};
libmegapixels_select_mode(io_camera, mode_preview, &format);
just_switched_mode = true; just_switched_mode = true;
mp_camera_start_capture(mpcamera); mp_camera_start_capture(mpcamera);
@@ -392,7 +394,8 @@ update_state(MPPipeline *pipeline, const struct mp_io_pipeline_state *state)
if (io_camera->video_fd == 0) { if (io_camera->video_fd == 0) {
libmegapixels_open(io_camera); libmegapixels_open(io_camera);
} }
libmegapixels_select_mode(io_camera, mode_preview); struct v4l2_format format = {0};
libmegapixels_select_mode(io_camera, mode_preview, &format);
} }
mp_camera_start_capture(mpcamera); mp_camera_start_capture(mpcamera);

View File

@@ -46,16 +46,11 @@ RENDERDOC_API_1_1_2 *rdoc_api = NULL;
#define APP_ID "org.postmarketos.Megapixels" #define APP_ID "org.postmarketos.Megapixels"
enum user_control { USER_CONTROL_ISO, USER_CONTROL_SHUTTER };
mp_state_main state; mp_state_main state;
static bool camera_is_initialized = false; static bool camera_is_initialized = false;
struct mp_main_state current_state = { 0 };
static MPProcessPipelineBuffer *current_preview_buffer = NULL; static MPProcessPipelineBuffer *current_preview_buffer = NULL;
static int preview_buffer_width = -1;
static int preview_buffer_height = -1;
static char last_path[260] = ""; static char last_path[260] = "";
@@ -115,39 +110,35 @@ update_io_pipeline()
} }
static bool static bool
update_state(const struct mp_main_state *state) update_state(const mp_state_main *new_state)
{ {
if (!camera_is_initialized) { if (!camera_is_initialized) {
camera_is_initialized = true; camera_is_initialized = true;
} }
if (current_state.camera == state->camera) { if (state.camera == new_state->camera) {
current_state.mode = state->mode; state.gain_is_manual = new_state->gain_is_manual;
state.gain = new_state->gain;
state.gain_max = new_state->gain_max;
if (!state.gain_is_manual) { state.exposure_is_manual = new_state->exposure_is_manual;
state.gain = state->gain; state.exposure = new_state->exposure;
}
state.gain_max = state->gain_max;
if (!state.exposure_is_manual) { state.has_auto_focus_continuous =
state.exposure = state->exposure; new_state->has_auto_focus_continuous;
} state.has_auto_focus_start = new_state->has_auto_focus_start;
has_auto_focus_continuous = state->has_auto_focus_continuous;
has_auto_focus_start = state->has_auto_focus_start;
} }
preview_buffer_width = state->image_width; state.preview_buffer_width = new_state->preview_buffer_width;
preview_buffer_height = state->image_height; state.preview_buffer_height = new_state->preview_buffer_height;
return false; return false;
} }
void void
mp_main_update_state(const struct mp_main_state *state) mp_main_update_state(const mp_state_main *new_state)
{ {
struct mp_main_state *state_copy = malloc(sizeof(struct mp_main_state)); mp_state_main *state_copy = malloc(sizeof(mp_state_main));
*state_copy = *state; *state_copy = *new_state;
g_main_context_invoke_full(g_main_context_default(), g_main_context_invoke_full(g_main_context_default(),
G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_DEFAULT_IDLE,
@@ -308,11 +299,11 @@ position_preview(float *offset_x, float *offset_y, float *size_x, float *size_y)
{ {
int buffer_width, buffer_height; int buffer_width, buffer_height;
if (state.device_rotation == 0 || state.device_rotation == 180) { if (state.device_rotation == 0 || state.device_rotation == 180) {
buffer_width = preview_buffer_width; buffer_width = state.preview_buffer_width;
buffer_height = preview_buffer_height; buffer_height = state.preview_buffer_height;
} else { } else {
buffer_width = preview_buffer_height; buffer_width = state.preview_buffer_height;
buffer_height = preview_buffer_width; buffer_height = state.preview_buffer_width;
} }
int scale_factor = gtk_widget_get_scale_factor(preview); int scale_factor = gtk_widget_get_scale_factor(preview);
@@ -322,18 +313,19 @@ position_preview(float *offset_x, float *offset_y, float *size_x, float *size_y)
gtk_widget_get_allocated_height(preview_bottom_box) * scale_factor; gtk_widget_get_allocated_height(preview_bottom_box) * scale_factor;
int inner_height = state.preview_height - top_height - bottom_height; int inner_height = state.preview_height - top_height - bottom_height;
double scale = MIN(state.preview_width / (float)buffer_width, float scale = (float)MIN(state.preview_width / (float)buffer_width,
state.preview_height / (float)buffer_height); state.preview_height / (float)buffer_height);
*size_x = scale * buffer_width; *size_x = scale * (float)buffer_width;
*size_y = scale * buffer_height; *size_y = scale * (float)buffer_height;
*offset_x = (state.preview_width - *size_x) / 2.0; *offset_x = ((float)state.preview_width - *size_x) / 2.0f;
if (*size_y > inner_height) { if (*size_y > (float)inner_height) {
*offset_y = (state.preview_height - *size_y) / 2.0; *offset_y = ((float)state.preview_height - *size_y) / 2.0f;
} else { } else {
*offset_y = top_height + (inner_height - *size_y) / 2.0; *offset_y =
(float)top_height + ((float)inner_height - *size_y) / 2.0f;
} }
} }
@@ -344,7 +336,7 @@ preview_draw(GtkGLArea *area, GdkGLContext *ctx, gpointer data)
return FALSE; return FALSE;
} }
if (!camera_is_initialized) { if (current_preview_buffer == NULL) {
return FALSE; return FALSE;
} }
@@ -417,11 +409,12 @@ preview_draw(GtkGLArea *area, GdkGLContext *ctx, gpointer data)
for (int i = 0; i < 4; ++i) { for (int i = 0; i < 4; ++i) {
vertices[i * 2] = vertices[i * 2] =
2 * vertices[i * 2] / preview_buffer_width - 2 * vertices[i * 2] /
state.preview_buffer_width -
1.0; 1.0;
vertices[i * 2 + 1] = vertices[i * 2 + 1] =
1.0 - 2 * vertices[i * 2 + 1] / 1.0 - 2 * vertices[i * 2 + 1] /
preview_buffer_height; state.preview_buffer_height;
} }
if (gtk_gl_area_get_use_es(area)) { if (gtk_gl_area_get_use_es(area)) {
@@ -651,9 +644,9 @@ preview_pressed(GtkGestureClick *gesture, int n_press, double x, double y)
position_preview(&offset_x, &offset_y, &size_x, &size_y); position_preview(&offset_x, &offset_y, &size_x, &size_y);
int zbar_x = (x - offset_x) * scale_factor / size_x * int zbar_x = (x - offset_x) * scale_factor / size_x *
preview_buffer_width; state.preview_buffer_width;
int zbar_y = (y - offset_y) * scale_factor / size_y * int zbar_y = (y - offset_y) * scale_factor / size_y *
preview_buffer_height; state.preview_buffer_height;
for (uint8_t i = 0; i < zbar_result->size; ++i) { for (uint8_t i = 0; i < zbar_result->size; ++i) {
MPZBarCode *code = &zbar_result->codes[i]; MPZBarCode *code = &zbar_result->codes[i];
@@ -780,7 +773,7 @@ static void
set_gain(double value) set_gain(double value)
{ {
if (state.gain != (int)value) { if (state.gain != (int)value) {
state.gain = value; state.gain = (int)value;
update_io_pipeline(); update_io_pipeline();
} }
} }
@@ -848,8 +841,8 @@ flash_button_clicked(GtkWidget *button, gpointer user_data)
state.flash_enabled = !state.flash_enabled; state.flash_enabled = !state.flash_enabled;
update_io_pipeline(); update_io_pipeline();
const char *icon_name = const char *icon_name = state.flash_enabled ? "flash-enabled-symbolic" :
state.flash_enabled ? "flash-enabled-symbolic" : "flash-disabled-symbolic"; "flash-disabled-symbolic";
gtk_button_set_icon_name(GTK_BUTTON(button), icon_name); gtk_button_set_icon_name(GTK_BUTTON(button), icon_name);
} }

View File

@@ -2,6 +2,7 @@
#include "gtk/gtk.h" #include "gtk/gtk.h"
#include "process_pipeline.h" #include "process_pipeline.h"
#include "state.h"
#include "zbar_pipeline.h" #include "zbar_pipeline.h"
struct mp_main_state { struct mp_main_state {
@@ -22,7 +23,7 @@ struct mp_main_state {
int image_height; int image_height;
}; };
void mp_main_update_state(const struct mp_main_state *state); void mp_main_update_state(const mp_state_main *new_state);
void mp_main_set_preview(MPProcessPipelineBuffer *buffer); void mp_main_set_preview(MPProcessPipelineBuffer *buffer);
void mp_main_capture_completed(GdkTexture *thumb, const char *fname); void mp_main_capture_completed(GdkTexture *thumb, const char *fname);

View File

@@ -1072,9 +1072,8 @@ update_state(MPPipeline *pipeline, const struct mp_process_pipeline_state *state
on_output_changed(format_changed); on_output_changed(format_changed);
} }
struct mp_main_state main_state = { mp_state_main new_state = {
.camera = pr_camera, .camera = pr_camera,
.mode = mode,
.gain_is_manual = state->gain_is_manual, .gain_is_manual = state->gain_is_manual,
.gain = gain, .gain = gain,
.gain_max = gain_max, .gain_max = gain_max,
@@ -1082,10 +1081,10 @@ update_state(MPPipeline *pipeline, const struct mp_process_pipeline_state *state
.exposure = exposure, .exposure = exposure,
.has_auto_focus_continuous = state->has_auto_focus_continuous, .has_auto_focus_continuous = state->has_auto_focus_continuous,
.has_auto_focus_start = state->has_auto_focus_start, .has_auto_focus_start = state->has_auto_focus_start,
.image_width = output_buffer_width, .preview_buffer_width = output_buffer_width,
.image_height = output_buffer_height, .preview_buffer_height = output_buffer_height,
}; };
mp_main_update_state(&main_state); mp_main_update_state(&new_state);
} }
void void

View File

@@ -1,3 +1,4 @@
#pragma once
#include <libmegapixels.h> #include <libmegapixels.h>
#include <stdbool.h> #include <stdbool.h>
@@ -5,8 +6,14 @@ typedef struct state_main {
libmegapixels_devconfig *configuration; libmegapixels_devconfig *configuration;
libmegapixels_camera *camera; libmegapixels_camera *camera;
// Size of the preview widget
int preview_width; int preview_width;
int preview_height; int preview_height;
// Size of the frame to draw in the preview widget
int preview_buffer_width;
int preview_buffer_height;
int device_rotation; int device_rotation;
int burst_length; int burst_length;