I've been working on integrating a game engine with Rendered Viewport feature via the RenderEngine interface. I'm using OpenGL to draw into the Blender window. The problem is that Blender doesn't seem to do anything to try and reset the state after the view_draw function has called, yet requires a very specific state to render the UI and other things. Not resetting the state leads to UI glitches that can be hard to track down, and discrepancies in shading when switching back to other viewport modes.
This patch fixes this by resetting the state appropriately. It doesn't cover all possible state changes, but probably the most common ones, and at least the ones relevant to the game engine I was integrating.
It also resets the projection and modelview matrices. This is necessary to restore the axes indicator and text at the bottom left of the viewport.