the grainy texture is caused by the proteins in the egg yolk clumping together. the custard is still safe to eat, but the texture can be unpalatable. sometimes it is possible to rescue a curdled custard enough so that it can be used as a base for ice cream, it depends how badly the custard has curdled.