Unfortunately, even though I've good knowledge of programming in others areas, Android Java development is not one of my skills.
But in any cases, among every applications I've tested which are able to change state depending of the foreground application, ones which use that API (which can be detected by their presence in Settings -> "Security" -> "Apps with usage access"), are very little CPU ressource intensive as far as I've been able to notice.
That's what I thought, that Tasker could optionally use this method as an effective way to implement the "application context" state
Meanwhile, I think I've found a, pretty much obfuscated but working, way to do what I need.
Ut works by using
"Sleepless in AP" and autonotification apps.
The first one is able to selectively disable screen timeout (side effect but I can live with that) for selected applications. When such an app is detected, it creates a notifications with text "<appname> currently prevent the device sleep". So I'm able to intercept the notification text using autonotification, extract app name and set a global variable (%FGRND_NOSLEEP_APP for instance) accordingly. and have n associated profile activated.
It seem to work but I would have preferred to find a way to make this works entirely using Tasker.