I think it is a mix of both. He thought up of ways to get stronger after his fight with Aokiji but couldn't think of a proper way to execute it as efficiently as he wanted to. After facing the CP9 however, he came up with the final piece of the puzzle which resulted in Gear Second. Gear Third is a little more mysterious. We see him casually walking away from door he broke, however that could be just because he didn't care about the side effects, as Robin was his only goal. He probably came up with Gear Third by thinking of the giants from the Little Garden Arc. I would think to much about this one however, as he BITES THROUGH BONE in order to activate it. I have no honest clue what gave him the idea to do that.