One possible approach could be the use of Hardware Occlusion Query.
You can use the facts that, by specification, the Stencil Test is executed before the depth test, and only the fragments that pass the depth test are counted by the Occlusion Query.
A simple example (not tested) would be like:
GLuint samples_query = 0;
GLuint samples_passed = 0;
glGenQueries(1, &samples_query);
// Initialize your buffers and textures ...
glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
// Set up the values on the stencil buffer ...
// Now we count the fragments that pass the stencil test
glDepthFunc(GL_ALWAYS); // Set up the depth test to always pass
glBeginQuery(GL_SAMPLES_PASSED, samples_query);
// Render your meshes here
glEndQuery(GL_SAMPLES_PASSED);
glGetQueryObjectuiv(samples_query, GL_QUERY_RESULT, &samples_passed);
// samples_passed holds the number of fragments that passed the stencil test (if any)
// Release your resources ...
glDeleteQueries(1, &samples_query);
Note that the call to obtain the number of samples will call forcibly the flush of the pipeline and wait for the query to finish.
If you need a more asynchronous approach you can query wether the occlusion query is done or not by using:
GLuint query_done = 0;
glGetQueryObjectuiv(samples_query, GL_QUERY_RESULT_AVAILABLE, &query_done);
if (query_done != 0)
// Your query result is ready
else
// Maybe check the next frame?