README-dynapi.txt 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. ================================================================================
  2. Dynamic API
  3. ================================================================================
  4. Originally posted by Ryan at https://plus.google.com/103391075724026391227/posts/TB8UfnDYu4U
  5. Background:
  6. - The Steam Runtime has (at least in theory) a really kick-ass build of SDL2,
  7. but developers are shipping their own SDL2 with individual Steam games.
  8. These games might stop getting updates, but a newer SDL2 might be needed later.
  9. Certainly we'll always be fixing bugs in SDL, even if a new video target isn't
  10. ever needed, and these fixes won't make it to a game shipping its own SDL.
  11. - Even if we replace the SDL2 in those games with a compatible one, that is to
  12. say, edit a developer's Steam depot (yuck!), there are developers that are
  13. statically linking SDL2 that we can't do this for. We can't even force the
  14. dynamic loader to ignore their SDL2 in this case, of course.
  15. - If you don't ship an SDL2 with the game in some form, people that disabled the
  16. Steam Runtime, or just tried to run the game from the command line instead of
  17. Steam might find themselves unable to run the game, due to a missing dependency.
  18. - If you want to ship on non-Steam platforms like GOG or Humble Bundle, or target
  19. generic Linux boxes that may or may not have SDL2 installed, you have to ship
  20. the library or risk a total failure to launch. So now, you might have to have
  21. a non-Steam build plus a Steam build (that is, one with and one without SDL2
  22. included), which is inconvenient if you could have had one universal build
  23. that works everywhere.
  24. - We like the zlib license, but the biggest complaint from the open source
  25. community about the license change is the static linking. The LGPL forced this
  26. as a legal, not technical issue, but zlib doesn't care. Even those that aren't
  27. concerned about the GNU freedoms found themselves solving the same problems:
  28. swapping in a newer SDL to an older game often times can save the day.
  29. Static linking stops this dead.
  30. So here's what we did:
  31. SDL now has, internally, a table of function pointers. So, this is what SDL_Init
  32. now looks like:
  33. UInt32 SDL_Init(Uint32 flags)
  34. {
  35. return jump_table.SDL_Init(flags);
  36. }
  37. Except that is all done with a bunch of macro magic so we don't have to maintain
  38. every one of these.
  39. What is jump_table.SDL_init()? Eventually, that's a function pointer of the real
  40. SDL_Init() that you've been calling all this time. But at startup, it looks more
  41. like this:
  42. Uint32 SDL_Init_DEFAULT(Uint32 flags)
  43. {
  44. SDL_InitDynamicAPI();
  45. return jump_table.SDL_Init(flags);
  46. }
  47. SDL_InitDynamicAPI() fills in jump_table with all the actual SDL function
  48. pointers, which means that this _DEFAULT function never gets called again.
  49. First call to any SDL function sets the whole thing up.
  50. So you might be asking, what was the value in that? Isn't this what the operating
  51. system's dynamic loader was supposed to do for us? Yes, but now we've got this
  52. level of indirection, we can do things like this:
  53. export SDL_DYNAMIC_API=/my/actual/libSDL-2.0.so.0
  54. ./MyGameThatIsStaticallyLinkedToSDL2
  55. And now, this game that is staticallly linked to SDL, can still be overridden
  56. with a newer, or better, SDL. The statically linked one will only be used as
  57. far as calling into the jump table in this case. But in cases where no override
  58. is desired, the statically linked version will provide its own jump table,
  59. and everyone is happy.
  60. So now:
  61. - Developers can statically link SDL, and users can still replace it.
  62. (We'd still rather you ship a shared library, though!)
  63. - Developers can ship an SDL with their game, Valve can override it for, say,
  64. new features on SteamOS, or distros can override it for their own needs,
  65. but it'll also just work in the default case.
  66. - Developers can ship the same package to everyone (Humble Bundle, GOG, etc),
  67. and it'll do the right thing.
  68. - End users (and Valve) can update a game's SDL in almost any case,
  69. to keep abandoned games running on newer platforms.
  70. - Everyone develops with SDL exactly as they have been doing all along.
  71. Same headers, same ABI. Just get the latest version to enable this magic.
  72. A little more about SDL_InitDynamicAPI():
  73. Internally, InitAPI does some locking to make sure everything waits until a
  74. single thread initializes everything (although even SDL_CreateThread() goes
  75. through here before spinning a thread, too), and then decides if it should use
  76. an external SDL library. If not, it sets up the jump table using the current
  77. SDL's function pointers (which might be statically linked into a program, or in
  78. a shared library of its own). If so, it loads that library and looks for and
  79. calls a single function:
  80. SInt32 SDL_DYNAPI_entry(Uint32 version, void *table, Uint32 tablesize);
  81. That function takes a version number (more on that in a moment), the address of
  82. the jump table, and the size, in bytes, of the table.
  83. Now, we've got policy here: this table's layout never changes; new stuff gets
  84. added to the end. Therefore SDL_DYNAPI_entry() knows that it can provide all
  85. the needed functions if tablesize <= sizeof its own jump table. If tablesize is
  86. bigger (say, SDL 2.0.4 is trying to load SDL 2.0.3), then we know to abort, but
  87. if it's smaller, we know we can provide the entire API that the caller needs.
  88. The version variable is a failsafe switch.
  89. Right now it's always 1. This number changes when there are major API changes
  90. (so we know if the tablesize might be smaller, or entries in it have changed).
  91. Right now SDL_DYNAPI_entry gives up if the version doesn't match, but it's not
  92. inconceivable to have a small dispatch library that only supplies this one
  93. function and loads different, otherwise-incompatible SDL libraries and has the
  94. right one initialize the jump table based on the version. For something that
  95. must generically catch lots of different versions of SDL over time, like the
  96. Steam Client, this isn't a bad option.
  97. Finally, I'm sure some people are reading this and thinking
  98. "I don't want that overhead in my project!"
  99. To which I would point out that the extra function call through the jump table
  100. probably wouldn't even show up in a profile, but lucky you: this can all be
  101. disabled. You can build SDL without this if you absolutely must, but we would
  102. encourage you not to do that. However, on heavily locked down platforms like
  103. iOS, or maybe when debugging, it makes sense to disable it. The way this is
  104. designed in SDL, you just have to change one #define, and the entire system
  105. vaporizes out, and SDL functions exactly like it always did. Most of it is
  106. macro magic, so the system is contained to one C file and a few headers.
  107. However, this is on by default and you have to edit a header file to turn it
  108. off. Our hopes is that if we make it easy to disable, but not too easy,
  109. everyone will ultimately be able to get what they want, but we've gently
  110. nudged everyone towards what we think is the best solution.