This is usually due to a GPU issue on the Mac. I have seen things online which say that this is due to graphic switching, which only occurs if you have two graphic cards which switch due to software (e.g say if you are using Photoshop which uses a much more powerful card and then you quit it and it returns to a normal card).
A MacBook Pro 2012 only has internal graphics and not a AMD Radeon R9 M370X, which only the MacBook Pro 15-inch has, meaning it is not software, but it is due to a hardware issue, in this case, the GPU.
If it is in warranty, take it in to Apple and see what happens there.
I hope you get something sorted out soon.