st/node-drawing: Pass CoglContext around

We already have access to it in most of places, just pass it around
instead of going through globals.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/3528>
This commit is contained in:
Bilal Elmoussaoui 2024-12-30 14:32:34 +01:00 committed by Marge Bot
parent 084a1b3be7
commit 343761e70e
7 changed files with 89 additions and 33 deletions

View File

@ -293,6 +293,7 @@ typedef struct {
StIconInfo *icon_info;
StIconColors *colors;
GFile *file;
CoglContext *cogl_context;
} AsyncTextureLoadData;
static void
@ -488,6 +489,7 @@ load_pixbuf_async_finish (StTextureCache *cache, GAsyncResult *result, GError **
static ClutterContent *
pixbuf_to_st_content_image (GdkPixbuf *pixbuf,
CoglContext *context,
int width,
int height,
int paint_scale,
@ -700,6 +702,7 @@ finish_texture_load (AsyncTextureLoadData *data,
&orig_key, &value))
{
image = pixbuf_to_st_content_image (pixbuf,
data->cogl_context,
data->width, data->height,
data->paint_scale,
data->resource_scale);
@ -717,6 +720,7 @@ finish_texture_load (AsyncTextureLoadData *data,
else
{
image = pixbuf_to_st_content_image (pixbuf,
data->cogl_context,
data->width, data->height,
data->paint_scale,
data->resource_scale);
@ -1065,6 +1069,8 @@ st_texture_cache_load_gicon (StTextureCache *cache,
{
/* Else, make a new request */
StIconInfo *info;
ClutterContext *context = clutter_actor_get_context (actor);
ClutterBackend *clutter_backend = clutter_context_get_backend (context);
info = st_icon_theme_lookup_by_gicon_for_scale (theme, icon,
size, scale,
@ -1086,6 +1092,7 @@ st_texture_cache_load_gicon (StTextureCache *cache,
request->width = request->height = size;
request->paint_scale = paint_scale;
request->resource_scale = resource_scale;
request->cogl_context = clutter_backend_get_cogl_context (clutter_backend);
load_texture_async (cache, request);
}
@ -1100,12 +1107,19 @@ load_from_pixbuf (GdkPixbuf *pixbuf,
{
g_autoptr(ClutterContent) image = NULL;
ClutterActor *actor;
image = pixbuf_to_st_content_image (pixbuf, -1, -1, paint_scale, resource_scale);
ClutterContext *context;
ClutterBackend *backend;
CoglContext *cogl_context;
actor = g_object_new (CLUTTER_TYPE_ACTOR,
"request-mode", CLUTTER_REQUEST_CONTENT_SIZE,
NULL);
context = clutter_actor_get_context (actor);
backend = clutter_context_get_backend (context);
cogl_context = clutter_backend_get_cogl_context (backend);
image = pixbuf_to_st_content_image (pixbuf, cogl_context, -1, -1,
paint_scale, resource_scale);
clutter_actor_set_content (actor, image);
return actor;
@ -1454,6 +1468,8 @@ st_texture_cache_load_file_async (StTextureCache *cache,
}
else
{
ClutterContext *context = clutter_actor_get_context (actor);
ClutterBackend *clutter_backend = clutter_context_get_backend (context);
/* Else, make a new request */
request->cache = cache;
@ -1465,6 +1481,7 @@ st_texture_cache_load_file_async (StTextureCache *cache,
request->height = available_height;
request->paint_scale = paint_scale;
request->resource_scale = resource_scale;
request->cogl_context = clutter_backend_get_cogl_context (clutter_backend);
load_texture_async (cache, request);
}
@ -1477,6 +1494,7 @@ st_texture_cache_load_file_async (StTextureCache *cache,
static CoglTexture *
st_texture_cache_load_file_sync_to_cogl_texture (StTextureCache *cache,
StTextureCachePolicy policy,
CoglContext *context,
GFile *file,
int available_width,
int available_height,
@ -1501,7 +1519,7 @@ st_texture_cache_load_file_sync_to_cogl_texture (StTextureCache *cache,
if (!pixbuf)
goto out;
image = pixbuf_to_st_content_image (pixbuf,
image = pixbuf_to_st_content_image (pixbuf, context,
available_height, available_width,
paint_scale, resource_scale);
g_object_unref (pixbuf);
@ -1578,6 +1596,7 @@ out:
/**
* st_texture_cache_load_file_to_cogl_texture: (skip)
* @cache: A #StTextureCache
* @context: A #CoglContext
* @file: A #GFile in supported image format
* @paint_scale: Scale factor of the display
* @resource_scale: Resource scale factor
@ -1590,6 +1609,7 @@ out:
*/
CoglTexture *
st_texture_cache_load_file_to_cogl_texture (StTextureCache *cache,
CoglContext *context,
GFile *file,
gint paint_scale,
gfloat resource_scale)
@ -1598,6 +1618,7 @@ st_texture_cache_load_file_to_cogl_texture (StTextureCache *cache,
GError *error = NULL;
texture = st_texture_cache_load_file_sync_to_cogl_texture (cache, ST_TEXTURE_CACHE_POLICY_FOREVER,
context,
file, -1, -1, paint_scale, resource_scale,
&error);

View File

@ -73,6 +73,7 @@ ClutterActor *st_texture_cache_load_file_async (StTextureCache *cache,
gfloat resource_scale);
CoglTexture *st_texture_cache_load_file_to_cogl_texture (StTextureCache *cache,
CoglContext *context,
GFile *file,
gint paint_scale,
gfloat resource_scale);

View File

@ -42,6 +42,11 @@
****/
typedef struct {
/*
* The context here is not really part of the corner
* definition per se, but is used for the drawing part.
*/
CoglContext *cogl_context;
CoglColor color;
CoglColor border_color_1;
CoglColor border_color_2;
@ -75,8 +80,6 @@ elliptical_arc (cairo_t *cr,
static CoglTexture *
create_corner_pipeline (StCornerSpec *corner)
{
ClutterBackend *backend = clutter_get_default_backend ();
CoglContext *ctx = clutter_backend_get_cogl_context (backend);
GError *error = NULL;
CoglTexture *texture;
cairo_t *cr;
@ -180,7 +183,7 @@ create_corner_pipeline (StCornerSpec *corner)
cairo_surface_destroy (surface);
texture = cogl_texture_2d_new_from_data (ctx, size, size,
texture = cogl_texture_2d_new_from_data (corner->cogl_context, size, size,
COGL_PIXEL_FORMAT_CAIRO_ARGB32_COMPAT,
rowstride,
data,
@ -362,6 +365,7 @@ st_theme_node_get_corner_border_widths (StThemeNode *node,
static CoglPipeline *
st_theme_node_lookup_corner (StThemeNode *node,
CoglContext *cogl_context,
float width,
float height,
float resource_scale,
@ -384,6 +388,7 @@ st_theme_node_lookup_corner (StThemeNode *node,
corner.radius = radius[corner_id];
corner.color = node->background_color;
corner.resource_scale = resource_scale;
corner.cogl_context = cogl_context;
st_theme_node_get_corner_border_widths (node, corner_id,
&corner.border_width_1,
&corner.border_width_2);
@ -1064,12 +1069,11 @@ paint_inset_box_shadow_to_cairo_context (StThemeNode *node,
*/
static CoglTexture *
st_theme_node_prerender_background (StThemeNode *node,
CoglContext *ctx,
float actor_width,
float actor_height,
float resource_scale)
{
ClutterBackend *backend = clutter_get_default_backend ();
CoglContext *ctx = clutter_backend_get_cogl_context (backend);
GError *error = NULL;
StBorderImage *border_image;
CoglTexture *texture;
@ -1421,6 +1425,7 @@ st_theme_node_prerender_background (StThemeNode *node,
static void
st_theme_node_maybe_prerender_background (StThemeNodePaintState *state,
StThemeNode *node,
CoglContext *cogl_context,
float width,
float height,
float resource_scale)
@ -1477,13 +1482,21 @@ st_theme_node_maybe_prerender_background (StThemeNodePaintState *state,
g_clear_object (&state->corner_pipeline[corner_id]);
state->corner_pipeline[ST_CORNER_TOPLEFT] =
st_theme_node_lookup_corner (node, width, height, resource_scale, ST_CORNER_TOPLEFT);
st_theme_node_lookup_corner (node, cogl_context,
width, height, resource_scale,
ST_CORNER_TOPLEFT);
state->corner_pipeline[ST_CORNER_TOPRIGHT] =
st_theme_node_lookup_corner (node, width, height, resource_scale, ST_CORNER_TOPRIGHT);
st_theme_node_lookup_corner (node, cogl_context,
width, height, resource_scale,
ST_CORNER_TOPRIGHT);
state->corner_pipeline[ST_CORNER_BOTTOMRIGHT] =
st_theme_node_lookup_corner (node, width, height, resource_scale, ST_CORNER_BOTTOMRIGHT);
st_theme_node_lookup_corner (node, cogl_context,
width, height, resource_scale,
ST_CORNER_BOTTOMRIGHT);
state->corner_pipeline[ST_CORNER_BOTTOMLEFT] =
st_theme_node_lookup_corner (node, width, height, resource_scale, ST_CORNER_BOTTOMLEFT);
st_theme_node_lookup_corner (node, cogl_context,
width, height, resource_scale,
ST_CORNER_BOTTOMLEFT);
/* Use cairo to prerender the node if there is a gradient, or
* background image with borders and/or rounded corners,
@ -1499,7 +1512,9 @@ st_theme_node_maybe_prerender_background (StThemeNodePaintState *state,
|| (st_theme_node_get_background_image (node) && (has_border || has_border_radius))
|| has_large_corners)
{
state->prerendered_texture = st_theme_node_prerender_background (node, width, height, resource_scale);
state->prerendered_texture = st_theme_node_prerender_background (node, cogl_context,
width, height,
resource_scale);
if (state->prerendered_texture)
state->prerendered_pipeline = _st_create_texture_pipeline (state->prerendered_texture);
@ -1523,6 +1538,7 @@ st_theme_node_invalidate_border_image (StThemeNode *node)
static gboolean
st_theme_node_load_border_image (StThemeNode *node,
CoglContext *cogl_context,
gfloat resource_scale)
{
if (node->border_slices_texture == NULL)
@ -1537,6 +1553,7 @@ st_theme_node_load_border_image (StThemeNode *node,
file = st_border_image_get_file (border_image);
node->border_slices_texture = st_texture_cache_load_file_to_cogl_texture (st_texture_cache_get_default (),
cogl_context,
file,
node->cached_scale_factor,
resource_scale);
@ -1560,6 +1577,7 @@ st_theme_node_invalidate_background_image (StThemeNode *node)
static gboolean
st_theme_node_load_background_image (StThemeNode *node,
CoglContext *cogl_context,
ClutterPaintContext *paint_context,
float resource_scale)
{
@ -1574,6 +1592,7 @@ st_theme_node_load_background_image (StThemeNode *node,
background_image_shadow_spec = st_theme_node_get_background_image_shadow (node);
node->background_texture = st_texture_cache_load_file_to_cogl_texture (st_texture_cache_get_default (),
cogl_context,
background_image,
node->cached_scale_factor,
resource_scale);
@ -1627,11 +1646,13 @@ st_theme_node_invalidate_resources_for_file (StThemeNode *node,
static void st_theme_node_compute_maximum_borders (StThemeNodePaintState *state);
static void st_theme_node_prerender_shadow (StThemeNodePaintState *state,
CoglContext *cogl_context,
ClutterPaintContext *paint_context);
static void
st_theme_node_render_resources (StThemeNodePaintState *state,
StThemeNode *node,
CoglContext *cogl_context,
ClutterPaintContext *paint_context,
float width,
float height,
@ -1659,13 +1680,14 @@ st_theme_node_render_resources (StThemeNodePaintState *state,
box_shadow_spec = st_theme_node_get_box_shadow (node);
has_inset_box_shadow = box_shadow_spec && box_shadow_spec->inset;
st_theme_node_maybe_prerender_background (state, node, width, height, resource_scale);
st_theme_node_maybe_prerender_background (state, node, cogl_context,
width, height, resource_scale);
if (box_shadow_spec && !has_inset_box_shadow)
{
st_theme_node_compute_maximum_borders (state);
if (st_theme_node_load_border_image (node, resource_scale))
if (st_theme_node_load_border_image (node, cogl_context, resource_scale))
state->box_shadow_pipeline = _st_create_shadow_pipeline (box_shadow_spec,
paint_context,
node->border_slices_texture,
@ -1676,7 +1698,7 @@ st_theme_node_render_resources (StThemeNodePaintState *state,
state->prerendered_texture,
state->resource_scale);
else
st_theme_node_prerender_shadow (state, paint_context);
st_theme_node_prerender_shadow (state, cogl_context, paint_context);
}
/* If we don't have cached textures yet, check whether we can cache
@ -1696,6 +1718,7 @@ st_theme_node_render_resources (StThemeNodePaintState *state,
static void
st_theme_node_update_resources (StThemeNodePaintState *state,
StThemeNode *node,
CoglContext *cogl_context,
ClutterPaintContext *paint_context,
float width,
float height,
@ -1728,7 +1751,8 @@ st_theme_node_update_resources (StThemeNodePaintState *state,
box_shadow_spec = st_theme_node_get_box_shadow (node);
st_theme_node_maybe_prerender_background (state, node, width, height, resource_scale);
st_theme_node_maybe_prerender_background (state, node, cogl_context,
width, height, resource_scale);
if (had_box_shadow)
state->box_shadow_pipeline = _st_create_shadow_pipeline (box_shadow_spec,
@ -2480,18 +2504,16 @@ st_theme_node_paint_sliced_shadow (StThemeNodePaintState *state,
static void
st_theme_node_prerender_shadow (StThemeNodePaintState *state,
CoglContext *ctx,
ClutterPaintContext *paint_context)
{
StThemeNode *node = state->node;
CoglContext *ctx;
int fb_width, fb_height;
CoglTexture *buffer;
CoglOffscreen *offscreen = NULL;
CoglFramebuffer *framebuffer;
GError *error = NULL;
ctx = clutter_backend_get_cogl_context (clutter_get_default_backend ());
/* Render offscreen */
fb_width = ceilf (state->box_shadow_width * state->resource_scale);
fb_height = ceilf (state->box_shadow_height * state->resource_scale);
@ -2794,6 +2816,7 @@ st_theme_node_needs_new_box_shadow_for_size (StThemeNodePaintState *state,
void
st_theme_node_paint (StThemeNode *node,
StThemeNodePaintState *state,
CoglContext *cogl_context,
ClutterPaintContext *paint_context,
ClutterPaintNode *root,
const ClutterActorBox *box,
@ -2831,7 +2854,7 @@ st_theme_node_paint (StThemeNode *node,
fabsf (resource_scale - state->resource_scale) < FLT_EPSILON)
st_theme_node_paint_state_copy (state, &node->cached_state);
else
st_theme_node_render_resources (state, node, paint_context,
st_theme_node_render_resources (state, node, cogl_context, paint_context,
width, height, resource_scale);
node->rendered_once = TRUE;
@ -2839,7 +2862,7 @@ st_theme_node_paint (StThemeNode *node,
else if (state->alloc_width != width || state->alloc_height != height ||
fabsf (state->resource_scale - resource_scale) > FLT_EPSILON)
{
st_theme_node_update_resources (state, node, paint_context,
st_theme_node_update_resources (state, node, cogl_context, paint_context,
width, height, resource_scale);
}
@ -2886,7 +2909,7 @@ st_theme_node_paint (StThemeNode *node,
}
if (state->prerendered_pipeline != NULL ||
st_theme_node_load_border_image (node, resource_scale))
st_theme_node_load_border_image (node, cogl_context, resource_scale))
{
if (state->prerendered_pipeline != NULL)
{
@ -2914,7 +2937,7 @@ st_theme_node_paint (StThemeNode *node,
st_theme_node_paint_outline (node, root, box, paint_opacity);
if (state->prerendered_pipeline == NULL &&
st_theme_node_load_background_image (node, paint_context, resource_scale))
st_theme_node_load_background_image (node, cogl_context, paint_context, resource_scale))
{
ClutterActorBox background_box;
ClutterActorBox texture_coords;

View File

@ -235,6 +235,7 @@ st_theme_node_transition_get_paint_box (StThemeNodeTransition *transition,
static gboolean
setup_framebuffers (StThemeNodeTransition *transition,
CoglContext *ctx,
ClutterPaintContext *paint_context,
ClutterPaintNode *node,
const ClutterActorBox *allocation,
@ -243,14 +244,12 @@ setup_framebuffers (StThemeNodeTransition *transition,
g_autoptr (ClutterPaintNode) old_layer_node = NULL;
g_autoptr (ClutterPaintNode) new_layer_node = NULL;
CoglPipeline *noop_pipeline;
CoglContext *ctx;
guint width, height;
GError *catch_error = NULL;
/* template pipeline to avoid unnecessary shader compilation */
static CoglPipeline *pipeline_template = NULL;
ctx = clutter_backend_get_cogl_context (clutter_get_default_backend ());
width = ceilf ((transition->offscreen_box.x2 - transition->offscreen_box.x1) * resource_scale);
height = ceilf ((transition->offscreen_box.y2 - transition->offscreen_box.y1) * resource_scale);
@ -324,7 +323,7 @@ setup_framebuffers (StThemeNodeTransition *transition,
clutter_paint_node_add_child (node, old_layer_node);
st_theme_node_paint (transition->old_theme_node, &transition->old_paint_state,
paint_context,
ctx, paint_context,
old_layer_node, allocation, 255, resource_scale);
new_layer_node = clutter_layer_node_new_to_framebuffer (transition->new_offscreen,
@ -336,7 +335,7 @@ setup_framebuffers (StThemeNodeTransition *transition,
transition->offscreen_box.x2,
transition->offscreen_box.y2, 0.0, 1.0);
st_theme_node_paint (transition->new_theme_node, &transition->new_paint_state,
paint_context,
ctx, paint_context,
new_layer_node, allocation, 255, resource_scale);
g_clear_object (&noop_pipeline);
@ -346,6 +345,7 @@ setup_framebuffers (StThemeNodeTransition *transition,
void
st_theme_node_transition_paint (StThemeNodeTransition *transition,
CoglContext *cogl_context,
ClutterPaintContext *paint_context,
ClutterPaintNode *node,
ClutterActorBox *allocation,
@ -373,6 +373,7 @@ st_theme_node_transition_paint (StThemeNodeTransition *transition,
calculate_offscreen_box (transition, allocation);
transition->needs_setup = clutter_actor_box_get_area (&transition->offscreen_box) == 0 ||
!setup_framebuffers (transition,
cogl_context,
paint_context,
node,
allocation,

View File

@ -41,6 +41,7 @@ void st_theme_node_transition_update (StThemeNodeTransition *transition,
StThemeNode *new_node);
void st_theme_node_transition_paint (StThemeNodeTransition *transition,
CoglContext *cogl_context,
ClutterPaintContext *paint_context,
ClutterPaintNode *node,
ClutterActorBox *allocation,

View File

@ -344,6 +344,7 @@ gboolean st_theme_node_paint_equal (StThemeNode *node,
*/
void st_theme_node_paint (StThemeNode *node,
StThemeNodePaintState *state,
CoglContext *cogl_context,
ClutterPaintContext *paint_context,
ClutterPaintNode *root,
const ClutterActorBox *box,

View File

@ -404,7 +404,13 @@ st_widget_paint_background (StWidget *widget,
ClutterActorBox allocation;
float resource_scale;
guint8 opacity;
ClutterContext *clutter_context;
ClutterBackend *clutter_backend;
CoglContext *cogl_context;
clutter_context = clutter_actor_get_context (CLUTTER_ACTOR (widget));
clutter_backend = clutter_context_get_backend (clutter_context);
cogl_context = clutter_backend_get_cogl_context (clutter_backend);
resource_scale = clutter_actor_get_resource_scale (CLUTTER_ACTOR (widget));
theme_node = st_widget_get_theme_node (widget);
@ -415,6 +421,7 @@ st_widget_paint_background (StWidget *widget,
if (priv->transition_animation)
st_theme_node_transition_paint (priv->transition_animation,
cogl_context,
paint_context,
node,
&allocation,
@ -423,6 +430,7 @@ st_widget_paint_background (StWidget *widget,
else
st_theme_node_paint (theme_node,
current_paint_state (widget),
cogl_context,
paint_context,
node,
&allocation,