mutter/src/tests/clutter/conform/actor-pick.c

227 lines
6.8 KiB
C
Raw Normal View History

#define CLUTTER_DISABLE_DEPRECATION_WARNINGS
#include <clutter/clutter.h>
#include "tests/clutter-test-utils.h"
#define STAGE_WIDTH 640
#define STAGE_HEIGHT 480
#define ACTORS_X 12
#define ACTORS_Y 16
typedef struct _State State;
struct _State
{
ClutterActor *stage;
int y, x;
ClutterActor *actors[ACTORS_X * ACTORS_Y];
guint actor_width, actor_height;
guint failed_pass;
guint failed_idx;
gboolean pass;
};
static const char *test_passes[] = {
"No covering actor",
"Invisible covering actor",
"Clipped covering actor",
"Blur effect",
};
static gboolean
on_timeout (gpointer data)
{
State *state = data;
int test_num = 0;
int y, x;
ClutterActor *over_actor = NULL;
/* This will cause an unclipped pick redraw that will get buffered.
We'll check below that this buffer is discarded because we also need
to pick non-reactive actors */
clutter_stage_get_actor_at_pos (CLUTTER_STAGE (state->stage),
CLUTTER_PICK_REACTIVE, 10, 10);
clutter_stage_get_actor_at_pos (CLUTTER_STAGE (state->stage),
CLUTTER_PICK_REACTIVE, 10, 10);
for (test_num = 0; test_num < G_N_ELEMENTS (test_passes); test_num++)
{
if (test_num == 0)
{
if (!g_test_quiet ())
g_print ("No covering actor:\n");
}
if (test_num == 1)
{
static const ClutterColor red = { 0xff, 0x00, 0x00, 0xff };
/* Create an actor that covers the whole stage but that
isn't visible so it shouldn't affect the picking */
over_actor = clutter_actor_new ();
clutter_actor_set_background_color (over_actor, &red);
clutter_actor_set_size (over_actor, STAGE_WIDTH, STAGE_HEIGHT);
clutter_actor_add_child (state->stage, over_actor);
clutter_actor_hide (over_actor);
if (!g_test_quiet ())
g_print ("Invisible covering actor:\n");
}
else if (test_num == 2)
{
ClutterActorBox over_actor_box =
CLUTTER_ACTOR_BOX_INIT (0, 0, STAGE_WIDTH, STAGE_HEIGHT);
/* Make the actor visible but set a clip so that only some
of the actors are accessible */
clutter_actor_show (over_actor);
clutter_actor_set_clip (over_actor,
state->actor_width * 2,
state->actor_height * 2,
state->actor_width * (ACTORS_X - 4),
state->actor_height * (ACTORS_Y - 4));
/* Only allocated actors can be picked, so force an allocation
* of the overlay actor here.
*/
clutter_actor_allocate (over_actor, &over_actor_box);
if (!g_test_quiet ())
g_print ("Clipped covering actor:\n");
}
else if (test_num == 3)
{
if (!clutter_feature_available (CLUTTER_FEATURE_SHADERS_GLSL))
continue;
clutter_actor_hide (over_actor);
clutter_actor_add_effect_with_name (CLUTTER_ACTOR (state->stage),
"blur",
clutter_blur_effect_new ());
if (!g_test_quiet ())
g_print ("With blur effect:\n");
}
for (y = 0; y < ACTORS_Y; y++)
{
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: https://gitlab.gnome.org/GNOME/mutter/issues/154, https://gitlab.gnome.org/GNOME/mutter/issues/691 Helps significantly with: https://gitlab.gnome.org/GNOME/mutter/issues/283, https://gitlab.gnome.org/GNOME/mutter/issues/590, https://gitlab.gnome.org/GNOME/mutter/issues/700 v2: Fix code style issues Simplify quadrilateral checks Remove the 0.5f hack Differentiate axis-aligned rectangles https://gitlab.gnome.org/GNOME/mutter/merge_requests/189
2018-08-02 07:03:30 -04:00
x = 0;
for (; x < ACTORS_X; x++)
{
gboolean pass = FALSE;
gfloat pick_x;
ClutterActor *actor;
pick_x = x * state->actor_width + state->actor_width / 2;
actor =
clutter_stage_get_actor_at_pos (CLUTTER_STAGE (state->stage),
CLUTTER_PICK_ALL,
pick_x,
y * state->actor_height
+ state->actor_height / 2);
if (!g_test_quiet ())
g_print ("% 3i,% 3i / %p -> ",
x, y, state->actors[y * ACTORS_X + x]);
if (actor == NULL)
{
if (!g_test_quiet ())
g_print ("NULL: FAIL\n");
}
else if (actor == over_actor)
{
if (test_num == 2
&& x >= 2 && x < ACTORS_X - 2
&& y >= 2 && y < ACTORS_Y - 2)
pass = TRUE;
if (!g_test_quiet ())
g_print ("over_actor: %s\n", pass ? "pass" : "FAIL");
}
else
{
if (actor == state->actors[y * ACTORS_X + x]
&& (test_num != 2
|| x < 2 || x >= ACTORS_X - 2
|| y < 2 || y >= ACTORS_Y - 2))
pass = TRUE;
if (!g_test_quiet ())
g_print ("%p: %s\n", actor, pass ? "pass" : "FAIL");
}
if (!pass)
{
state->failed_pass = test_num;
state->failed_idx = y * ACTORS_X + x;
state->pass = FALSE;
}
}
}
}
clutter_main_quit ();
return G_SOURCE_REMOVE;
}
static void
actor_pick (void)
{
int y, x;
State state;
state.pass = TRUE;
state.stage = clutter_test_get_stage ();
state.actor_width = STAGE_WIDTH / ACTORS_X;
state.actor_height = STAGE_HEIGHT / ACTORS_Y;
for (y = 0; y < ACTORS_Y; y++)
for (x = 0; x < ACTORS_X; x++)
{
ClutterColor color = { x * 255 / (ACTORS_X - 1),
y * 255 / (ACTORS_Y - 1),
128, 255 };
ClutterActor *rect = clutter_actor_new ();
clutter_actor_set_background_color (rect, &color);
clutter_actor_set_position (rect,
x * state.actor_width,
y * state.actor_height);
clutter_actor_set_size (rect,
state.actor_width,
state.actor_height);
clutter_actor_add_child (state.stage, rect);
state.actors[y * ACTORS_X + x] = rect;
}
clutter_actor_show (state.stage);
clutter_threads_add_idle (on_timeout, &state);
clutter_main ();
if (!g_test_quiet ())
{
if (!state.pass)
g_test_message ("Failed pass: %s[%d], actor index: %d [%p]\n",
test_passes[state.failed_pass],
state.failed_pass,
state.failed_idx,
state.actors[state.failed_idx]);
}
g_assert (state.pass);
}
CLUTTER_TEST_SUITE (
CLUTTER_TEST_UNIT ("/actor/pick", actor_pick)
)