I have a server application that responds to http requests for images. The images are typically 256x256 map tiles that the server draws dynamically, but also some other dynamic images of various sizes. The server uses a thread for each request.
I have just become aware that multiple threads can't access the same "drawing context". I say "drawing context" because it's not clear to me where in the hierarchy things break. Specifically:
* I am using SDL to get a GL context. I call SDL_Init() once when the program starts.
* I call SDL_CreateWindow() once when the program starts. I am currently making a 4096x4096 window, because that's the maximum size my program could need.
* I call SDL_GL_CreateContext() once when the program starts.
* I call SDL_GL_MakeCurrent() once with that window and that context, when the program starts.
* I call glViewport( 0, 0, 4096, 4096 ) once when the program starts.
* I call GrDirectContext::MakeGL( NULL ) once when the program starts.
* I then call SkSurface::MakeRenderTarget() with that direct context, again just once.
* I call _mySurfaceGL->getCanvas() once to get a canvas for that surface.
Then I to do various clearing/drawing on that canvas, and then I call canvas->flush() and canvas->readPixels() to copy out some pixels (usually just a small region on the top-left of that large canvas) into an SkBitmap, and from there I can get PNG data.
That all works great when done from the main thread, but when it's a worker thread bad things happen, like canvas->flush() never returning, and getting no data when trying to get PNG encoded data.
So my question is: When I handle an http request with a worker thread, how much of that stack do I have to recreate for the thread? Is it way up at SDL_CreateWindow() or SDL_GL_CreateContext(), or perhaps at GrDirectContext::MakeGL(), or SkSurface::MakeRenderTarget()?
And if I need to do relatively expensive create functions for each thread, should I have a pool of premade windows/contexts/surfaces to draw from?
What is the standard pattern for multi-threaded apps?