gl-cocoa.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /******************************************************************************
  2. Copyright (C) 2013 by Ruwen Hahn <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "gl-subsystem.h"
  15. #include <OpenGL/OpenGL.h>
  16. #import <Cocoa/Cocoa.h>
  17. #import <AppKit/AppKit.h>
  18. //#include "util/darray.h"
  19. struct gl_windowinfo {
  20. NSView *view;
  21. };
  22. struct gl_platform {
  23. NSOpenGLContext *context;
  24. struct gs_swap_chain swap;
  25. };
  26. static NSOpenGLContext *gl_context_create(struct gs_init_data *info)
  27. {
  28. unsigned attrib_count = 0;
  29. #define ADD_ATTR(x) \
  30. { attributes[attrib_count++] = (NSOpenGLPixelFormatAttribute)x; }
  31. #define ADD_ATTR2(x, y) { ADD_ATTR(x); ADD_ATTR(y); }
  32. NSOpenGLPixelFormatAttribute attributes[40];
  33. switch(info->num_backbuffers) {
  34. case 0:
  35. break;
  36. case 1:
  37. ADD_ATTR(NSOpenGLPFADoubleBuffer);
  38. break;
  39. case 2:
  40. ADD_ATTR(NSOpenGLPFATripleBuffer);
  41. break;
  42. default:
  43. blog(LOG_ERROR, "Requested backbuffers (%d) not "
  44. "supported", info->num_backbuffers);
  45. }
  46. ADD_ATTR(NSOpenGLPFAClosestPolicy);
  47. ADD_ATTR2(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
  48. int color_bits = 0;//get_color_format_bits(info->format);
  49. if(color_bits == 0) color_bits = 24;
  50. else if(color_bits < 15) color_bits = 15;
  51. ADD_ATTR2(NSOpenGLPFAColorSize, color_bits);
  52. ADD_ATTR2(NSOpenGLPFAAlphaSize, 8);
  53. ADD_ATTR2(NSOpenGLPFADepthSize, 16);
  54. ADD_ATTR(0);
  55. #undef ADD_ATTR2
  56. #undef ADD_ATTR
  57. NSOpenGLPixelFormat *pf;
  58. pf = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
  59. if(!pf) {
  60. blog(LOG_ERROR, "Failed to create pixel format");
  61. return NULL;
  62. }
  63. NSOpenGLContext *context;
  64. context = [[NSOpenGLContext alloc] initWithFormat:pf shareContext:nil];
  65. [pf release];
  66. if(!context) {
  67. blog(LOG_ERROR, "Failed to create context");
  68. return NULL;
  69. }
  70. [context setView:info->window.view];
  71. return context;
  72. }
  73. static bool gl_init_default_swap(struct gl_platform *plat, device_t dev,
  74. struct gs_init_data *info)
  75. {
  76. if(!(plat->context = gl_context_create(info)))
  77. return false;
  78. plat->swap.device = dev;
  79. plat->swap.info = *info;
  80. plat->swap.wi = gl_windowinfo_create(info);
  81. return plat->swap.wi != NULL;
  82. }
  83. struct gl_platform *gl_platform_create(device_t device,
  84. struct gs_init_data *info)
  85. {
  86. struct gl_platform *plat = bzalloc(sizeof(struct gl_platform));
  87. if(!gl_init_default_swap(plat, device, info))
  88. goto fail;
  89. [plat->context makeCurrentContext];
  90. if (!ogl_LoadFunctions())
  91. goto fail;
  92. return plat;
  93. fail:
  94. blog(LOG_ERROR, "gl_platform_create failed");
  95. gl_platform_destroy(plat);
  96. return NULL;
  97. }
  98. struct gs_swap_chain *gl_platform_getswap(struct gl_platform *platform)
  99. {
  100. if(platform)
  101. return &platform->swap;
  102. return NULL;
  103. }
  104. void gl_platform_destroy(struct gl_platform *platform)
  105. {
  106. if(!platform)
  107. return;
  108. [platform->context release];
  109. platform->context = nil;
  110. gl_windowinfo_destroy(platform->swap.wi);
  111. bfree(platform);
  112. }
  113. struct gl_windowinfo *gl_windowinfo_create(struct gs_init_data *info)
  114. {
  115. if(!info)
  116. return NULL;
  117. if(!info->window.view)
  118. return NULL;
  119. struct gl_windowinfo *wi = bzalloc(sizeof(struct gl_windowinfo));
  120. wi->view = info->window.view;
  121. return wi;
  122. }
  123. void gl_windowinfo_destroy(struct gl_windowinfo *wi)
  124. {
  125. if(!wi)
  126. return;
  127. wi->view = nil;
  128. bfree(wi);
  129. }
  130. void gl_update(device_t device)
  131. {
  132. [device->plat->context update];
  133. }
  134. void device_entercontext(device_t device)
  135. {
  136. [device->plat->context makeCurrentContext];
  137. }
  138. void device_leavecontext(device_t device)
  139. {
  140. UNUSED_PARAMETER(device);
  141. [NSOpenGLContext clearCurrentContext];
  142. }
  143. void device_load_swapchain(device_t device, swapchain_t swap)
  144. {
  145. if(!swap)
  146. swap = &device->plat->swap;
  147. if(device->cur_swap == swap)
  148. return;
  149. device->cur_swap = swap;
  150. [device->plat->context setView:swap->wi->view];
  151. }
  152. void device_present(device_t device)
  153. {
  154. [device->plat->context flushBuffer];
  155. }
  156. void gl_getclientsize(struct gs_swap_chain *swap, uint32_t *width,
  157. uint32_t *height)
  158. {
  159. if(width) *width = swap->info.cx;
  160. if(height) *height = swap->info.cy;
  161. }
  162. texture_t texture_create_from_iosurface(device_t device, void *iosurf)
  163. {
  164. IOSurfaceRef ref = (IOSurfaceRef)iosurf;
  165. struct gs_texture_2d *tex = bzalloc(sizeof(struct gs_texture_2d));
  166. OSType pf = IOSurfaceGetPixelFormat(ref);
  167. if (pf != 'BGRA')
  168. blog(LOG_ERROR, "Unexpected pixel format: %d (%c%c%c%c)", pf,
  169. pf >> 24, pf >> 16, pf >> 8, pf);
  170. const enum gs_color_format color_format = GS_BGRA;
  171. tex->base.device = device;
  172. tex->base.type = GS_TEXTURE_2D;
  173. tex->base.format = GS_BGRA;
  174. tex->base.levels = 1;
  175. tex->base.gl_format = convert_gs_format(color_format);
  176. tex->base.gl_internal_format = convert_gs_internal_format(color_format);
  177. tex->base.gl_type = GL_UNSIGNED_INT_8_8_8_8_REV;
  178. tex->base.gl_target = GL_TEXTURE_RECTANGLE;
  179. tex->base.is_dynamic = false;
  180. tex->base.is_render_target = false;
  181. tex->base.gen_mipmaps = false;
  182. tex->width = IOSurfaceGetWidth(ref);
  183. tex->height = IOSurfaceGetHeight(ref);
  184. if (!gl_gen_textures(1, &tex->base.texture))
  185. goto fail;
  186. if (!gl_bind_texture(tex->base.gl_target, tex->base.texture))
  187. goto fail;
  188. CGLError err = CGLTexImageIOSurface2D(
  189. [[NSOpenGLContext currentContext] CGLContextObj],
  190. tex->base.gl_target,
  191. tex->base.gl_internal_format,
  192. tex->width, tex->height,
  193. tex->base.gl_format,
  194. tex->base.gl_type,
  195. ref, 0);
  196. if(err != kCGLNoError) {
  197. blog(LOG_ERROR, "CGLTexImageIOSurface2D: %u, %s"
  198. " (texture_create_from_iosurface)",
  199. err, CGLErrorString(err));
  200. gl_success("CGLTexImageIOSurface2D");
  201. goto fail;
  202. }
  203. if (!gl_tex_param_i(tex->base.gl_target,
  204. GL_TEXTURE_MAX_LEVEL, 0))
  205. goto fail;
  206. if (!gl_bind_texture(tex->base.gl_target, 0))
  207. goto fail;
  208. return (texture_t)tex;
  209. fail:
  210. texture_destroy((texture_t)tex);
  211. blog(LOG_ERROR, "texture_create_from_iosurface (GL) failed");
  212. return NULL;
  213. }
  214. bool texture_rebind_iosurface(texture_t texture, void *iosurf)
  215. {
  216. if (!texture)
  217. return false;
  218. if (!iosurf)
  219. return false;
  220. struct gs_texture_2d *tex = (struct gs_texture_2d*)texture;
  221. IOSurfaceRef ref = (IOSurfaceRef)iosurf;
  222. OSType pf = IOSurfaceGetPixelFormat(ref);
  223. if (pf != 'BGRA')
  224. blog(LOG_ERROR, "Unexpected pixel format: %d (%c%c%c%c)", pf,
  225. pf >> 24, pf >> 16, pf >> 8, pf);
  226. if (tex->width != IOSurfaceGetWidth(ref) ||
  227. tex->height != IOSurfaceGetHeight(ref))
  228. return false;
  229. if (!gl_bind_texture(tex->base.gl_target, tex->base.texture))
  230. return false;
  231. CGLError err = CGLTexImageIOSurface2D(
  232. [[NSOpenGLContext currentContext] CGLContextObj],
  233. tex->base.gl_target,
  234. tex->base.gl_internal_format,
  235. tex->width, tex->height,
  236. tex->base.gl_format,
  237. tex->base.gl_type,
  238. ref, 0);
  239. if(err != kCGLNoError) {
  240. blog(LOG_ERROR, "CGLTexImageIOSurface2D: %u, %s"
  241. " (texture_rebind_iosurface)",
  242. err, CGLErrorString(err));
  243. gl_success("CGLTexImageIOSurface2D");
  244. return false;
  245. }
  246. if (!gl_bind_texture(tex->base.gl_target, 0))
  247. return false;
  248. return true;
  249. }