Commit 14c706e5 authored by Daniel van Vugt's avatar Daniel van Vugt Committed by Jonas Ådahl

clutter: Introduce geometric picking

Currently, Clutter does picking by drawing with Cogl and reading
the pixel that's beneath the given point. Since Cogl has a journal
that records drawing operations, and has optimizations to read a
single pixel from a list of rectangle, it would be expected that
we would hit this fast path and not flush the journal while picking.

However, that's not the case: dithering, clipping with scissors, etc,
can all flush the journal, issuing commands to the GPU and making
picking slow. On NVidia-based systems, this glReadPixels() call is
extremely costly.

Introduce geometric picking, and avoid using the Cogl journal entirely.
Do this by introducing a stack of actors in ClutterStage. This stack
is cached, but for now, don't use the cache as much as possible.

The picking routines are still tied to painting.

When projecting the actor vertexes, do it manually and take the modelview
matrix of the framebuffer into account as well.

CPU usage on an Intel i7-7700, tested with two different GPUs/drivers:

  |         |     Intel | Nvidia |
  | ------: | --------: | -----: |
  | Moving the mouse:            |
  | Before  |       10% |    10% |
  | After   |        6% |     6% |
  | Moving a window:             |
  | Before  |       23% |    81% |
  | After   |       19% |    40% |

Closes: #154,
        #691

Helps significantly with: #283,
                          #590,
                          #700

v2: Fix code style issues
    Simplify quadrilateral checks
    Remove the 0.5f hack
    Differentiate axis-aligned rectangles

!189
parent a70823dd
Pipeline #110722 passed with stages
in 5 minutes and 48 seconds
......@@ -297,8 +297,6 @@ const gchar * _clutter_actor_get_debug_name
void _clutter_actor_push_clone_paint (void);
void _clutter_actor_pop_clone_paint (void);
guint32 _clutter_actor_get_pick_id (ClutterActor *self);
void _clutter_actor_shader_pre_paint (ClutterActor *actor,
gboolean repeat);
void _clutter_actor_shader_post_paint (ClutterActor *actor);
......
......@@ -736,8 +736,6 @@ struct _ClutterActorPrivate
gchar *name; /* a non-unique name, used for debugging */
gint32 pick_id; /* per-stage unique id, used for picking */
/* a back-pointer to the Pango context that we can use
* to create pre-configured PangoLayout
*/
......@@ -1290,6 +1288,105 @@ clutter_actor_verify_map_state (ClutterActor *self)
#endif /* CLUTTER_ENABLE_DEBUG */
static gboolean
_clutter_actor_transform_local_box_to_stage (ClutterActor *self,
ClutterStage *stage,
const ClutterActorBox *box,
ClutterPoint vertices[4])
{
CoglFramebuffer *fb = cogl_get_draw_framebuffer ();
CoglMatrix stage_transform, inv_stage_transform;
CoglMatrix modelview, transform_to_stage;
int v;
clutter_actor_get_transform (CLUTTER_ACTOR (stage), &stage_transform);
if (!cogl_matrix_get_inverse (&stage_transform, &inv_stage_transform))
return FALSE;
cogl_framebuffer_get_modelview_matrix (fb, &modelview);
cogl_matrix_multiply (&transform_to_stage, &inv_stage_transform, &modelview);
vertices[0].x = box->x1;
vertices[0].y = box->y1;
vertices[1].x = box->x2;
vertices[1].y = box->y1;
vertices[2].x = box->x2;
vertices[2].y = box->y2;
vertices[3].x = box->x1;
vertices[3].y = box->y2;
for (v = 0; v < 4; v++)
{
float z = 0.f;
float w = 1.f;
cogl_matrix_transform_point (&transform_to_stage,
&vertices[v].x,
&vertices[v].y,
&z,
&w);
}
return TRUE;
}
/**
* clutter_actor_pick_box:
* @self: The #ClutterActor being "pick" painted.
* @box: A rectangle in the actor's own local coordinates.
*
* Logs (does a virtual paint of) a rectangle for picking. Note that @box is
* in the actor's own local coordinates, so is usually {0,0,width,height}
* to include the whole actor. That is unless the actor has a shaped input
* region in which case you may wish to log the (multiple) smaller rectangles
* that make up the input region.
*/
void
clutter_actor_pick_box (ClutterActor *self,
const ClutterActorBox *box)
{
ClutterStage *stage;
ClutterPoint vertices[4];
g_return_if_fail (CLUTTER_IS_ACTOR (self));
g_return_if_fail (box != NULL);
if (box->x1 >= box->x2 || box->y1 >= box->y2)
return;
stage = CLUTTER_STAGE (_clutter_actor_get_stage_internal (self));
if (_clutter_actor_transform_local_box_to_stage (self, stage, box, vertices))
clutter_stage_log_pick (stage, vertices, self);
}
static gboolean
_clutter_actor_push_pick_clip (ClutterActor *self,
const ClutterActorBox *clip)
{
ClutterStage *stage;
ClutterPoint vertices[4];
stage = CLUTTER_STAGE (_clutter_actor_get_stage_internal (self));
if (!_clutter_actor_transform_local_box_to_stage (self, stage, clip, vertices))
return FALSE;
clutter_stage_push_pick_clip (stage, vertices);
return TRUE;
}
static void
_clutter_actor_pop_pick_clip (ClutterActor *self)
{
ClutterActor *stage;
stage = _clutter_actor_get_stage_internal (self);
clutter_stage_pop_pick_clip (CLUTTER_STAGE (stage));
}
static void
clutter_actor_set_mapped (ClutterActor *self,
gboolean mapped)
......@@ -1518,8 +1615,7 @@ clutter_actor_update_map_state (ClutterActor *self,
static void
clutter_actor_real_map (ClutterActor *self)
{
ClutterActorPrivate *priv = self->priv;
ClutterActor *stage, *iter;
ClutterActor *iter;
g_assert (!CLUTTER_ACTOR_IS_MAPPED (self));
......@@ -1530,13 +1626,6 @@ clutter_actor_real_map (ClutterActor *self)
self->priv->needs_paint_volume_update = TRUE;
stage = _clutter_actor_get_stage_internal (self);
priv->pick_id = _clutter_stage_acquire_pick_id (CLUTTER_STAGE (stage), self);
CLUTTER_NOTE (ACTOR, "Pick id '%d' for actor '%s'",
priv->pick_id,
_clutter_actor_get_debug_name (self));
clutter_actor_ensure_resource_scale (self);
/* notify on parent mapped before potentially mapping
......@@ -1641,11 +1730,6 @@ clutter_actor_real_unmap (ClutterActor *self)
stage = CLUTTER_STAGE (_clutter_actor_get_stage_internal (self));
if (stage != NULL)
_clutter_stage_release_pick_id (stage, priv->pick_id);
priv->pick_id = -1;
if (stage != NULL &&
clutter_stage_get_key_focus (stage) == self)
{
......@@ -2264,46 +2348,16 @@ static void
clutter_actor_real_pick (ClutterActor *self,
const ClutterColor *color)
{
CoglFramebuffer *framebuffer = cogl_get_draw_framebuffer ();
/* the default implementation is just to paint a rectangle
* with the same size of the actor using the passed color
*/
if (clutter_actor_should_pick_paint (self))
{
static CoglPipeline *default_pick_pipeline = NULL;
ClutterActorBox box = { 0, };
CoglPipeline *pick_pipeline;
float width, height;
if (G_UNLIKELY (default_pick_pipeline == NULL))
{
CoglContext *ctx =
clutter_backend_get_cogl_context (clutter_get_default_backend ());
default_pick_pipeline = cogl_pipeline_new (ctx);
}
g_assert (default_pick_pipeline != NULL);
pick_pipeline = cogl_pipeline_copy (default_pick_pipeline);
ClutterActorBox box = {
.x1 = 0,
.y1 = 0,
.x2 = clutter_actor_get_width (self),
.y2 = clutter_actor_get_height (self),
};
clutter_actor_get_allocation_box (self, &box);
width = box.x2 - box.x1;
height = box.y2 - box.y1;
cogl_pipeline_set_color4ub (pick_pipeline,
color->red,
color->green,
color->blue,
color->alpha);
cogl_framebuffer_draw_rectangle (framebuffer,
pick_pipeline,
0, 0,
width, height);
cogl_object_unref (pick_pipeline);
clutter_actor_pick_box (self, &box);
}
/* XXX - this thoroughly sucks, but we need to maintain compatibility
......@@ -3594,15 +3648,6 @@ _clutter_actor_update_last_paint_volume (ClutterActor *self)
priv->last_paint_volume_valid = TRUE;
}
guint32
_clutter_actor_get_pick_id (ClutterActor *self)
{
if (self->priv->pick_id < 0)
return 0;
return self->priv->pick_id;
}
/* This is the same as clutter_actor_add_effect except that it doesn't
queue a redraw and it doesn't notify on the effect property */
static void
......@@ -3834,6 +3879,7 @@ clutter_actor_paint (ClutterActor *self)
{
ClutterActorPrivate *priv;
ClutterPickMode pick_mode;
ClutterActorBox clip;
gboolean clip_set = FALSE;
ClutterStage *stage;
......@@ -3927,24 +3973,38 @@ clutter_actor_paint (ClutterActor *self)
if (priv->has_clip)
{
CoglFramebuffer *fb = _clutter_stage_get_active_framebuffer (stage);
cogl_framebuffer_push_rectangle_clip (fb,
priv->clip.origin.x,
priv->clip.origin.y,
priv->clip.origin.x + priv->clip.size.width,
priv->clip.origin.y + priv->clip.size.height);
clip.x1 = priv->clip.origin.x;
clip.y1 = priv->clip.origin.y;
clip.x2 = priv->clip.origin.x + priv->clip.size.width;
clip.y2 = priv->clip.origin.y + priv->clip.size.height;
clip_set = TRUE;
}
else if (priv->clip_to_allocation)
{
CoglFramebuffer *fb = _clutter_stage_get_active_framebuffer (stage);
gfloat width, height;
clip.x1 = 0.f;
clip.y1 = 0.f;
clip.x2 = priv->allocation.x2 - priv->allocation.x1;
clip.y2 = priv->allocation.y2 - priv->allocation.y1;
clip_set = TRUE;
}
width = priv->allocation.x2 - priv->allocation.x1;
height = priv->allocation.y2 - priv->allocation.y1;
if (clip_set)
{
if (pick_mode == CLUTTER_PICK_NONE)
{
CoglFramebuffer *fb = _clutter_stage_get_active_framebuffer (stage);
cogl_framebuffer_push_rectangle_clip (fb, 0, 0, width, height);
clip_set = TRUE;
cogl_framebuffer_push_rectangle_clip (fb,
clip.x1,
clip.y1,
clip.x2,
clip.y2);
}
else
{
if (!_clutter_actor_push_pick_clip (self, &clip))
clip_set = FALSE;
}
}
if (pick_mode == CLUTTER_PICK_NONE)
......@@ -4025,9 +4085,16 @@ clutter_actor_paint (ClutterActor *self)
done:
if (clip_set)
{
CoglFramebuffer *fb = _clutter_stage_get_active_framebuffer (stage);
if (pick_mode == CLUTTER_PICK_NONE)
{
CoglFramebuffer *fb = _clutter_stage_get_active_framebuffer (stage);
cogl_framebuffer_pop_clip (fb);
cogl_framebuffer_pop_clip (fb);
}
else
{
_clutter_actor_pop_pick_clip (self);
}
}
cogl_pop_matrix ();
......@@ -4098,11 +4165,12 @@ clutter_actor_continue_paint (ClutterActor *self)
{
ClutterColor col = { 0, };
_clutter_id_to_color (_clutter_actor_get_pick_id (self), &col);
/* Actor will then paint silhouette of itself in supplied
* color. See clutter_stage_get_actor_at_pos() for where
* picking is enabled.
/* The actor will log a silhouette of itself to the stage pick log.
* Note that the picking color is no longer used as the "log" instead
* keeps a weak pointer to the actor itself. But we keep the color
* parameter for now so as to maintain ABI compatibility. The color
* parameter can be removed when someone feels like breaking the ABI
* along with gnome-shell.
*
* XXX:2.0 - Call the pick() virtual directly
*/
......@@ -8651,8 +8719,6 @@ clutter_actor_init (ClutterActor *self)
self->priv = priv = clutter_actor_get_instance_private (self);
priv->pick_id = -1;
priv->opacity = 0xff;
priv->show_on_set_parent = TRUE;
priv->resource_scale = -1.0f;
......
......@@ -902,6 +902,10 @@ void clutter_actor_bind_model_with_properties
const char *first_model_property,
...);
CLUTTER_EXPORT
void clutter_actor_pick_box (ClutterActor *self,
const ClutterActorBox *box);
G_END_DECLS
#endif /* __CLUTTER_ACTOR_H__ */
......@@ -30,7 +30,6 @@ typedef enum
typedef enum
{
CLUTTER_DEBUG_NOP_PICKING = 1 << 0,
CLUTTER_DEBUG_DUMP_PICK_BUFFERS = 1 << 1
} ClutterPickDebugFlag;
typedef enum
......
......@@ -129,7 +129,6 @@ static const GDebugKey clutter_debug_keys[] = {
static const GDebugKey clutter_pick_debug_keys[] = {
{ "nop-picking", CLUTTER_DEBUG_NOP_PICKING },
{ "dump-pick-buffers", CLUTTER_DEBUG_DUMP_PICK_BUFFERS },
};
static const GDebugKey clutter_paint_debug_keys[] = {
......@@ -401,125 +400,6 @@ clutter_disable_accessibility (void)
clutter_enable_accessibility = FALSE;
}
void
_clutter_id_to_color (guint id_,
ClutterColor *col)
{
ClutterMainContext *ctx;
gint red, green, blue;
ctx = _clutter_context_get_default ();
if (ctx->fb_g_mask == 0)
{
/* Figure out framebuffer masks used for pick */
cogl_get_bitmasks (&ctx->fb_r_mask,
&ctx->fb_g_mask,
&ctx->fb_b_mask, NULL);
ctx->fb_r_mask_used = ctx->fb_r_mask;
ctx->fb_g_mask_used = ctx->fb_g_mask;
ctx->fb_b_mask_used = ctx->fb_b_mask;
/* XXX - describe what "fuzzy picking" is */
if (clutter_use_fuzzy_picking)
{
ctx->fb_r_mask_used--;
ctx->fb_g_mask_used--;
ctx->fb_b_mask_used--;
}
}
/* compute the numbers we'll store in the components */
red = (id_ >> (ctx->fb_g_mask_used+ctx->fb_b_mask_used))
& (0xff >> (8-ctx->fb_r_mask_used));
green = (id_ >> ctx->fb_b_mask_used)
& (0xff >> (8-ctx->fb_g_mask_used));
blue = (id_)
& (0xff >> (8-ctx->fb_b_mask_used));
/* shift left bits a bit and add one, this circumvents
* at least some potential rounding errors in GL/GLES
* driver / hw implementation.
*/
if (ctx->fb_r_mask_used != ctx->fb_r_mask)
red = red * 2;
if (ctx->fb_g_mask_used != ctx->fb_g_mask)
green = green * 2;
if (ctx->fb_b_mask_used != ctx->fb_b_mask)
blue = blue * 2;
/* shift up to be full 8bit values */
red = (red << (8 - ctx->fb_r_mask)) | (0x7f >> (ctx->fb_r_mask_used));
green = (green << (8 - ctx->fb_g_mask)) | (0x7f >> (ctx->fb_g_mask_used));
blue = (blue << (8 - ctx->fb_b_mask)) | (0x7f >> (ctx->fb_b_mask_used));
col->red = red;
col->green = green;
col->blue = blue;
col->alpha = 0xff;
/* XXX: We rotate the nibbles of the colors here so that there is a
* visible variation between colors of sequential actor identifiers;
* otherwise pick buffers dumped to an image will pretty much just look
* black.
*/
if (G_UNLIKELY (clutter_pick_debug_flags & CLUTTER_DEBUG_DUMP_PICK_BUFFERS))
{
col->red = (col->red << 4) | (col->red >> 4);
col->green = (col->green << 4) | (col->green >> 4);
col->blue = (col->blue << 4) | (col->blue >> 4);
}
}
guint
_clutter_pixel_to_id (guchar pixel[4])
{
ClutterMainContext *ctx;
gint red, green, blue;
guint retval;
ctx = _clutter_context_get_default ();
/* reduce the pixel components to the number of bits actually used of the
* 8bits.
*/
if (G_UNLIKELY (clutter_pick_debug_flags & CLUTTER_DEBUG_DUMP_PICK_BUFFERS))
{
guchar tmp;
/* XXX: In _clutter_id_to_color we rotated the nibbles of the colors so
* that there is a visible variation between colors of sequential actor
* identifiers (otherwise pick buffers dumped to an image will pretty
* much just look black.) Here we reverse that rotation.
*/
tmp = ((pixel[0] << 4) | (pixel[0] >> 4));
red = tmp >> (8 - ctx->fb_r_mask);
tmp = ((pixel[1] << 4) | (pixel[1] >> 4));
green = tmp >> (8 - ctx->fb_g_mask);
tmp = ((pixel[2] << 4) | (pixel[2] >> 4));
blue = tmp >> (8 - ctx->fb_b_mask);
}
else
{
red = pixel[0] >> (8 - ctx->fb_r_mask);
green = pixel[1] >> (8 - ctx->fb_g_mask);
blue = pixel[2] >> (8 - ctx->fb_b_mask);
}
/* divide potentially by two if 'fuzzy' */
red = red >> (ctx->fb_r_mask - ctx->fb_r_mask_used);
green = green >> (ctx->fb_g_mask - ctx->fb_g_mask_used);
blue = blue >> (ctx->fb_b_mask - ctx->fb_b_mask_used);
/* combine the correct per component values into the final id */
retval = blue
+ (green << ctx->fb_b_mask_used)
+ (red << (ctx->fb_b_mask_used + ctx->fb_g_mask_used));
return retval;
}
static CoglPangoFontMap *
clutter_context_get_pango_fontmap (void)
{
......
......@@ -199,11 +199,6 @@ gboolean _clutter_feature_init (GError **error);
gboolean _clutter_diagnostic_enabled (void);
void _clutter_diagnostic_message (const char *fmt, ...) G_GNUC_PRINTF (1, 2);
/* Picking code */
guint _clutter_pixel_to_id (guchar pixel[4]);
void _clutter_id_to_color (guint id,
ClutterColor *col);
CLUTTER_EXPORT
void _clutter_set_sync_to_vblank (gboolean sync_to_vblank);
......
......@@ -79,6 +79,15 @@ gint64 _clutter_stage_get_update_time (ClutterStage *stage);
void _clutter_stage_clear_update_time (ClutterStage *stage);
gboolean _clutter_stage_has_full_redraw_queued (ClutterStage *stage);
void clutter_stage_log_pick (ClutterStage *stage,
const ClutterPoint *vertices,
ClutterActor *actor);
void clutter_stage_push_pick_clip (ClutterStage *stage,
const ClutterPoint *vertices);
void clutter_stage_pop_pick_clip (ClutterStage *stage);
ClutterActor *_clutter_stage_do_pick (ClutterStage *stage,
gint x,
gint y,
......@@ -97,13 +106,6 @@ void _clutter_stage_queue_redraw_entry_invalidate (Clut
CoglFramebuffer *_clutter_stage_get_active_framebuffer (ClutterStage *stage);
gint32 _clutter_stage_acquire_pick_id (ClutterStage *stage,
ClutterActor *actor);
void _clutter_stage_release_pick_id (ClutterStage *stage,
gint32 pick_id);
ClutterActor * _clutter_stage_get_actor_by_pick_id (ClutterStage *stage,
gint32 pick_id);
void _clutter_stage_add_pointer_drag_actor (ClutterStage *stage,
ClutterInputDevice *device,
ClutterActor *actor);
......
......@@ -275,24 +275,6 @@ _clutter_stage_window_redraw (ClutterStageWindow *window)
iface->redraw (window);
}
void
_clutter_stage_window_get_dirty_pixel (ClutterStageWindow *window,
ClutterStageView *view,
int *x, int *y)
{
ClutterStageWindowInterface *iface;
*x = 0;
*y = 0;
g_return_if_fail (CLUTTER_IS_STAGE_WINDOW (window));
iface = CLUTTER_STAGE_WINDOW_GET_IFACE (window);
if (iface->get_dirty_pixel)
iface->get_dirty_pixel (window, view, x, y);
}
gboolean
_clutter_stage_window_can_clip_redraws (ClutterStageWindow *window)
{
......
......@@ -64,10 +64,6 @@ struct _ClutterStageWindowInterface
void (* redraw) (ClutterStageWindow *stage_window);
void (* get_dirty_pixel) (ClutterStageWindow *stage_window,
ClutterStageView *view,
int *x, int *y);
gboolean (* can_clip_redraws) (ClutterStageWindow *stage_window);
GList *(* get_views) (ClutterStageWindow *stage_window);
......@@ -112,10 +108,6 @@ void _clutter_stage_window_set_accept_focus (ClutterStageWin
void _clutter_stage_window_redraw (ClutterStageWindow *window);
void _clutter_stage_window_get_dirty_pixel (ClutterStageWindow *window,
ClutterStageView *view,
int *x, int *y);
gboolean _clutter_stage_window_can_clip_redraws (ClutterStageWindow *window);
GList * _clutter_stage_window_get_views (ClutterStageWindow *window);
......
......@@ -105,6 +105,19 @@ struct _ClutterStageQueueRedrawEntry
ClutterPaintVolume clip;
};
typedef struct _PickRecord
{
ClutterPoint vertex[4];
ClutterActor *actor;
int clip_stack_top;
} PickRecord;
typedef struct _PickClipRecord
{
int prev;
ClutterPoint vertex[4];
} PickClipRecord;
struct _ClutterStagePrivate
{
/* the stage implementation */
......@@ -138,7 +151,11 @@ struct _ClutterStagePrivate
GTimer *fps_timer;
gint32 timer_n_frames;
ClutterIDPool *pick_id_pool;
GArray *pick_stack;
GArray *pick_clip_stack;
int pick_clip_stack_top;
gboolean pick_stack_frozen;
ClutterPickMode cached_pick_mode;
#ifdef CLUTTER_ENABLE_DEBUG
gulong redraw_count;
......@@ -323,6 +340,211 @@ clutter_stage_get_preferred_height (ClutterActor *self,
*natural_height_p = geom.height;
}
static void
add_pick_stack_weak_refs (ClutterStage *stage)
{
ClutterStagePrivate *priv = stage->priv;
int i;
if (priv->pick_stack_frozen)
return;
for (i = 0; i < priv->pick_stack->len; i++)
{
PickRecord *rec = &g_array_index (priv->pick_stack, PickRecord, i);
if (rec->actor)
g_object_add_weak_pointer (G_OBJECT (rec->actor),
(gpointer) &rec->actor);
}
priv->pick_stack_frozen = TRUE;
}
static void
remove_pick_stack_weak_refs (ClutterStage *stage)
{
ClutterStagePrivate *priv = stage->priv;
int i;
if (!priv->pick_stack_frozen)
return;
for (i = 0; i < priv->pick_stack->len; i++)
{
PickRecord *rec = &g_array_index (priv->pick_stack, PickRecord, i);
if (rec->actor)
g_object_remove_weak_pointer (G_OBJECT (rec->actor),
(gpointer) &rec->actor);