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;
// Initialize your buffers and textures ...
// 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
// Render your meshes here
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 ...
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
// Maybe check the next frame?