Check if the surface is valid before setting it as focus

A surface may get its resource destroyed and during the time it takes to
be destroyed it must not be set as mouse of keyboard focus.