Sink: keep track of widget allocation size

Instead of obtaining allocation size on each frame draw, keep track of its current size and update value on its change.
This commit is contained in:
Rafał Dzięgiel
2021-03-05 18:41:26 +01:00
parent 637212f7e8
commit fe3fd32932
3 changed files with 33 additions and 18 deletions

View File

@@ -91,6 +91,23 @@ gtk_gst_base_widget_measure (GtkWidget * widget, GtkOrientation orientation,
*minimum_baseline = -1;
*natural_baseline = -1;
}
static void
gtk_gst_base_widget_size_allocate (GtkWidget * widget,
gint width, gint height, gint baseline)
{
GtkGstBaseWidget *base_widget = GTK_GST_BASE_WIDGET (widget);
gint scale_factor = gtk_widget_get_scale_factor (widget);
GTK_GST_BASE_WIDGET_LOCK (base_widget);
base_widget->scaled_width = width * scale_factor;
base_widget->scaled_height = height * scale_factor;
GTK_GST_BASE_WIDGET_UNLOCK (base_widget);
gtk_gl_area_queue_render (GTK_GL_AREA (widget));
}
#endif
static void
@@ -170,7 +187,6 @@ _calculate_par (GtkGstBaseWidget * widget, GstVideoInfo * info)
display_par_d = 1;
}
ok = gst_video_calculate_display_ratio (&widget->display_ratio_num,
&widget->display_ratio_den, width, height, par_n, par_d, display_par_n,
display_par_d);
@@ -234,7 +250,7 @@ _queue_draw (GtkGstBaseWidget * widget)
gtk_widget_queue_resize (GTK_WIDGET (widget));
} else {
gtk_widget_queue_draw (GTK_WIDGET (widget));
gtk_gl_area_queue_render (GTK_GL_AREA (widget));
}
GTK_GST_BASE_WIDGET_UNLOCK (widget);
@@ -308,8 +324,7 @@ gtk_gst_base_widget_key_event (GtkEventControllerKey * key_controller,
}
static void
_fit_stream_to_allocated_size (GtkGstBaseWidget * base_widget,
GtkAllocation * allocation, GstVideoRectangle * result)
_fit_stream_to_allocated_size (GtkGstBaseWidget * base_widget, GstVideoRectangle * result)
{
if (base_widget->force_aspect_ratio) {
GstVideoRectangle src, dst;
@@ -321,15 +336,15 @@ _fit_stream_to_allocated_size (GtkGstBaseWidget * base_widget,
dst.x = 0;
dst.y = 0;
dst.w = allocation->width;
dst.h = allocation->height;
dst.w = base_widget->scaled_width;
dst.h = base_widget->scaled_height;
gst_video_sink_center_rect (src, dst, result, TRUE);
} else {
result->x = 0;
result->y = 0;
result->w = allocation->width;
result->h = allocation->height;
result->w = base_widget->scaled_width;
result->h = base_widget->scaled_height;
}
}
@@ -338,11 +353,9 @@ _display_size_to_stream_size (GtkGstBaseWidget * base_widget, gdouble x,
gdouble y, gdouble * stream_x, gdouble * stream_y)
{
gdouble stream_width, stream_height;
GtkAllocation allocation;
GstVideoRectangle result;
gtk_widget_get_allocation (GTK_WIDGET (base_widget), &allocation);
_fit_stream_to_allocated_size (base_widget, &allocation, &result);
_fit_stream_to_allocated_size (base_widget, &result);
stream_width = (gdouble) GST_VIDEO_INFO_WIDTH (&base_widget->v_info);
stream_height = (gdouble) GST_VIDEO_INFO_HEIGHT (&base_widget->v_info);
@@ -475,6 +488,7 @@ gtk_gst_base_widget_class_init (GtkGstBaseWidgetClass * klass)
#if defined(BUILD_FOR_GTK4)
widget_klass->measure = gtk_gst_base_widget_measure;
widget_klass->size_allocate = gtk_gst_base_widget_size_allocate;
#else
widget_klass->get_preferred_width = gtk_gst_base_widget_get_preferred_width;
widget_klass->get_preferred_height = gtk_gst_base_widget_get_preferred_height;

View File

@@ -55,6 +55,10 @@ struct _GtkGstBaseWidget
gint display_width;
gint display_height;
/* Widget dimensions */
gint scaled_width;
gint scaled_height;
gboolean negotiated;
GstBuffer *pending_buffer;
GstBuffer *buffer;

View File

@@ -253,15 +253,10 @@ gtk_gst_gl_widget_render (GtkGLArea * widget, GdkGLContext * context)
if (base_widget->force_aspect_ratio) {
GstVideoRectangle src, dst, result;
gint widget_width, widget_height, widget_scale;
gl->ClearColor (0.0, 0.0, 0.0, 1.0);
gl->Clear (GL_COLOR_BUFFER_BIT);
widget_scale = gtk_widget_get_scale_factor (GTK_WIDGET (widget));
widget_width = gtk_widget_get_allocated_width (GTK_WIDGET (widget));
widget_height = gtk_widget_get_allocated_height (GTK_WIDGET (widget));
src.x = 0;
src.y = 0;
src.w = base_widget->display_width;
@@ -269,8 +264,8 @@ gtk_gst_gl_widget_render (GtkGLArea * widget, GdkGLContext * context)
dst.x = 0;
dst.y = 0;
dst.w = widget_width * widget_scale;
dst.h = widget_height * widget_scale;
dst.w = base_widget->scaled_width;
dst.h = base_widget->scaled_height;
gst_video_sink_center_rect (src, dst, &result, TRUE);
@@ -429,6 +424,8 @@ gtk_gst_gl_widget_init (GtkGstGLWidget * gst_widget)
gtk_gl_area_set_has_alpha (GTK_GL_AREA (gst_widget),
!base_widget->ignore_alpha);
#endif
gtk_gl_area_set_auto_render (GTK_GL_AREA (gst_widget), FALSE);
}
static void