Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

egl_g3d_image.c 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Mesa 3-D graphics library
  3. * Version: 7.8
  4. *
  5. * Copyright (C) 2010 LunarG Inc.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. *
  25. * Authors:
  26. * Chia-I Wu <olv@lunarg.com>
  27. */
  28. #include "pipe/p_screen.h"
  29. #include "util/u_memory.h"
  30. #include "util/u_rect.h"
  31. #include "util/u_inlines.h"
  32. #include "eglcurrent.h"
  33. #include "egllog.h"
  34. #include "native.h"
  35. #include "egl_g3d.h"
  36. #include "egl_g3d_api.h"
  37. #include "egl_g3d_image.h"
  38. /* move this to native display? */
  39. #include "state_tracker/drm_driver.h"
  40. /**
  41. * Reference and return the front left buffer of the native pixmap.
  42. */
  43. static struct pipe_resource *
  44. egl_g3d_reference_native_pixmap(_EGLDisplay *dpy, EGLNativePixmapType pix)
  45. {
  46. struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
  47. struct egl_g3d_config *gconf;
  48. struct native_surface *nsurf;
  49. struct pipe_resource *textures[NUM_NATIVE_ATTACHMENTS];
  50. enum native_attachment natt;
  51. gconf = egl_g3d_config(egl_g3d_find_pixmap_config(dpy, pix));
  52. if (!gconf)
  53. return NULL;
  54. nsurf = gdpy->native->create_pixmap_surface(gdpy->native,
  55. pix, gconf->native);
  56. if (!nsurf)
  57. return NULL;
  58. natt = NATIVE_ATTACHMENT_FRONT_LEFT;
  59. if (!nsurf->validate(nsurf, 1 << natt, NULL, textures, NULL, NULL))
  60. textures[natt] = NULL;
  61. nsurf->destroy(nsurf);
  62. return textures[natt];
  63. }
  64. #ifdef EGL_MESA_drm_image
  65. static struct pipe_resource *
  66. egl_g3d_create_drm_buffer(_EGLDisplay *dpy, _EGLImage *img,
  67. const EGLint *attribs)
  68. {
  69. struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
  70. struct pipe_screen *screen = gdpy->native->screen;
  71. struct pipe_resource templ;
  72. _EGLImageAttribs attrs;
  73. EGLint format, valid_use;
  74. if (_eglParseImageAttribList(&attrs, dpy, attribs) != EGL_SUCCESS)
  75. return NULL;
  76. if (attrs.Width <= 0 || attrs.Height <= 0) {
  77. _eglLog(_EGL_DEBUG, "bad width or height (%dx%d)",
  78. attrs.Width, attrs.Height);
  79. return NULL;
  80. }
  81. switch (attrs.DRMBufferFormatMESA) {
  82. case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
  83. format = PIPE_FORMAT_B8G8R8A8_UNORM;
  84. break;
  85. default:
  86. _eglLog(_EGL_DEBUG, "bad image format value 0x%04x",
  87. attrs.DRMBufferFormatMESA);
  88. return NULL;
  89. break;
  90. }
  91. valid_use = EGL_DRM_BUFFER_USE_SCANOUT_MESA |
  92. EGL_DRM_BUFFER_USE_SHARE_MESA;
  93. if (attrs.DRMBufferUseMESA & ~valid_use) {
  94. _eglLog(_EGL_DEBUG, "bad image use bit 0x%04x",
  95. attrs.DRMBufferUseMESA);
  96. return NULL;
  97. }
  98. memset(&templ, 0, sizeof(templ));
  99. templ.target = PIPE_TEXTURE_2D;
  100. templ.format = format;
  101. templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
  102. templ.width0 = attrs.Width;
  103. templ.height0 = attrs.Height;
  104. templ.depth0 = 1;
  105. /*
  106. * XXX fix apps (e.g. wayland) and pipe drivers (e.g. i915) and remove the
  107. * size check
  108. */
  109. if ((attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA) &&
  110. attrs.Width >= 640 && attrs.Height >= 480)
  111. templ.bind |= PIPE_BIND_SCANOUT;
  112. if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA)
  113. templ.bind |= PIPE_BIND_SHARED;
  114. return screen->resource_create(screen, &templ);
  115. }
  116. static struct pipe_resource *
  117. egl_g3d_reference_drm_buffer(_EGLDisplay *dpy, EGLint name,
  118. _EGLImage *img, const EGLint *attribs)
  119. {
  120. struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
  121. struct pipe_screen *screen = gdpy->native->screen;
  122. struct pipe_resource templ;
  123. struct winsys_handle wsh;
  124. _EGLImageAttribs attrs;
  125. EGLint format;
  126. /* winsys_handle is in theory platform-specific */
  127. if (dpy->Platform != _EGL_PLATFORM_DRM)
  128. return NULL;
  129. if (_eglParseImageAttribList(&attrs, dpy, attribs) != EGL_SUCCESS)
  130. return NULL;
  131. if (attrs.Width <= 0 || attrs.Height <= 0 ||
  132. attrs.DRMBufferStrideMESA <= 0) {
  133. _eglLog(_EGL_DEBUG, "bad width, height, or stride (%dx%dx%d)",
  134. attrs.Width, attrs.Height, attrs.DRMBufferStrideMESA);
  135. return NULL;
  136. }
  137. switch (attrs.DRMBufferFormatMESA) {
  138. case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
  139. format = PIPE_FORMAT_B8G8R8A8_UNORM;
  140. break;
  141. default:
  142. _eglLog(_EGL_DEBUG, "bad image format value 0x%04x",
  143. attrs.DRMBufferFormatMESA);
  144. return NULL;
  145. break;
  146. }
  147. memset(&templ, 0, sizeof(templ));
  148. templ.target = PIPE_TEXTURE_2D;
  149. templ.format = format;
  150. templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
  151. templ.width0 = attrs.Width;
  152. templ.height0 = attrs.Height;
  153. templ.depth0 = 1;
  154. memset(&wsh, 0, sizeof(wsh));
  155. wsh.handle = (unsigned) name;
  156. wsh.stride = attrs.DRMBufferStrideMESA;
  157. return screen->resource_from_handle(screen, &templ, &wsh);
  158. }
  159. #endif /* EGL_MESA_drm_image */
  160. _EGLImage *
  161. egl_g3d_create_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx,
  162. EGLenum target, EGLClientBuffer buffer,
  163. const EGLint *attribs)
  164. {
  165. struct pipe_resource *ptex;
  166. struct egl_g3d_image *gimg;
  167. unsigned face = 0, level = 0, zslice = 0;
  168. gimg = CALLOC_STRUCT(egl_g3d_image);
  169. if (!gimg) {
  170. _eglError(EGL_BAD_ALLOC, "eglCreateEGLImageKHR");
  171. return NULL;
  172. }
  173. if (!_eglInitImage(&gimg->base, dpy)) {
  174. FREE(gimg);
  175. return NULL;
  176. }
  177. switch (target) {
  178. case EGL_NATIVE_PIXMAP_KHR:
  179. ptex = egl_g3d_reference_native_pixmap(dpy,
  180. (EGLNativePixmapType) buffer);
  181. break;
  182. #ifdef EGL_MESA_drm_image
  183. case EGL_DRM_BUFFER_MESA:
  184. ptex = egl_g3d_reference_drm_buffer(dpy,
  185. (EGLint) buffer, &gimg->base, attribs);
  186. break;
  187. #endif
  188. default:
  189. ptex = NULL;
  190. break;
  191. }
  192. if (!ptex) {
  193. FREE(gimg);
  194. return NULL;
  195. }
  196. if (level > ptex->last_level) {
  197. _eglError(EGL_BAD_MATCH, "eglCreateEGLImageKHR");
  198. pipe_resource_reference(&gimg->texture, NULL);
  199. FREE(gimg);
  200. return NULL;
  201. }
  202. if (zslice > ptex->depth0) {
  203. _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
  204. pipe_resource_reference(&gimg->texture, NULL);
  205. FREE(gimg);
  206. return NULL;
  207. }
  208. /* transfer the ownership to the image */
  209. gimg->texture = ptex;
  210. gimg->face = face;
  211. gimg->level = level;
  212. gimg->zslice = zslice;
  213. return &gimg->base;
  214. }
  215. EGLBoolean
  216. egl_g3d_destroy_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img)
  217. {
  218. struct egl_g3d_image *gimg = egl_g3d_image(img);
  219. pipe_resource_reference(&gimg->texture, NULL);
  220. FREE(gimg);
  221. return EGL_TRUE;
  222. }
  223. _EGLImage *
  224. egl_g3d_create_drm_image(_EGLDriver *drv, _EGLDisplay *dpy,
  225. const EGLint *attribs)
  226. {
  227. struct egl_g3d_image *gimg;
  228. struct pipe_resource *ptex;
  229. gimg = CALLOC_STRUCT(egl_g3d_image);
  230. if (!gimg) {
  231. _eglError(EGL_BAD_ALLOC, "eglCreateDRMImageKHR");
  232. return NULL;
  233. }
  234. if (!_eglInitImage(&gimg->base, dpy)) {
  235. FREE(gimg);
  236. return NULL;
  237. }
  238. #ifdef EGL_MESA_drm_image
  239. ptex = egl_g3d_create_drm_buffer(dpy, &gimg->base, attribs);
  240. #else
  241. ptex = NULL;
  242. #endif
  243. if (!ptex) {
  244. FREE(gimg);
  245. return NULL;
  246. }
  247. /* transfer the ownership to the image */
  248. gimg->texture = ptex;
  249. gimg->face = 0;
  250. gimg->level = 0;
  251. gimg->zslice = 0;
  252. return &gimg->base;
  253. }
  254. EGLBoolean
  255. egl_g3d_export_drm_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img,
  256. EGLint *name, EGLint *handle, EGLint *stride)
  257. {
  258. struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
  259. struct egl_g3d_image *gimg = egl_g3d_image(img);
  260. struct pipe_screen *screen = gdpy->native->screen;
  261. struct winsys_handle wsh;
  262. /* winsys_handle is in theory platform-specific */
  263. if (dpy->Platform != _EGL_PLATFORM_DRM)
  264. return EGL_FALSE;
  265. /* get shared handle */
  266. if (name) {
  267. memset(&handle, 0, sizeof(handle));
  268. wsh.type = DRM_API_HANDLE_TYPE_SHARED;
  269. if (!screen->resource_get_handle(screen, gimg->texture, &wsh)) {
  270. return EGL_FALSE;
  271. }
  272. *name = wsh.handle;
  273. }
  274. /* get KMS handle */
  275. if (handle || stride) {
  276. memset(&wsh, 0, sizeof(wsh));
  277. wsh.type = DRM_API_HANDLE_TYPE_KMS;
  278. if (!screen->resource_get_handle(screen, gimg->texture, &wsh))
  279. return EGL_FALSE;
  280. if (handle)
  281. *handle = wsh.handle;
  282. if (stride)
  283. *stride = wsh.stride;
  284. }
  285. return EGL_TRUE;
  286. }