2 * Mesa 3-D graphics library
5 * Copyright (C) 2010 LunarG Inc.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
26 * Chia-I Wu <olv@lunarg.com>
29 #include "util/u_memory.h"
30 #include "util/u_string.h"
31 #include "util/u_inlines.h"
32 #include "util/u_dl.h"
33 #include "egldriver.h"
38 #include "egl_g3d_st.h"
40 struct egl_g3d_st_manager
{
41 struct st_manager base
;
45 static INLINE
struct egl_g3d_st_manager
*
46 egl_g3d_st_manager(struct st_manager
*smapi
)
48 return (struct egl_g3d_st_manager
*) smapi
;
51 static struct egl_g3d_st_module
{
53 struct util_dl_library
*lib
;
55 } egl_g3d_st_modules
[ST_API_COUNT
];
58 egl_g3d_search_path_callback(const char *dir
, size_t len
, void *callback_data
)
60 struct egl_g3d_st_module
*stmod
=
61 (struct egl_g3d_st_module
*) callback_data
;
65 ret
= util_snprintf(path
, sizeof(path
),
66 "%.*s/%s", len
, dir
, stmod
->filename
);
67 if (ret
> 0 && ret
< sizeof(path
))
68 stmod
->lib
= util_dl_open(path
);
74 egl_g3d_load_st_module(struct egl_g3d_st_module
*stmod
,
75 const char *filename
, const char *procname
)
77 struct st_api
*(*create_api
)(void);
79 stmod
->filename
= filename
;
81 _eglSearchPathForEach(egl_g3d_search_path_callback
, (void *) stmod
);
83 stmod
->lib
= util_dl_open(NULL
);
86 create_api
= (struct st_api
*(*)(void))
87 util_dl_get_proc_address(stmod
->lib
, procname
);
89 stmod
->stapi
= create_api();
92 util_dl_close(stmod
->lib
);
101 stmod
->filename
= NULL
;
107 egl_g3d_init_st_apis(struct st_api
*stapis
[ST_API_COUNT
])
109 const char *skip_checks
[ST_API_COUNT
], *symbols
[ST_API_COUNT
];
110 const char *filenames
[ST_API_COUNT
][4];
111 struct util_dl_library
*self
;
112 int num_needed
= 0, api
;
114 self
= util_dl_open(NULL
);
116 /* collect the necessary data for loading modules */
117 for (api
= 0; api
< ST_API_COUNT
; api
++) {
122 skip_checks
[api
] = "glColor4d";
123 symbols
[api
] = ST_CREATE_OPENGL_SYMBOL
;
124 filenames
[api
][count
++] = "api_GL.so";
126 case ST_API_OPENGL_ES1
:
127 skip_checks
[api
] = "glColor4x";
128 symbols
[api
] = ST_CREATE_OPENGL_ES1_SYMBOL
;
129 filenames
[api
][count
++] = "api_GLESv1_CM.so";
130 filenames
[api
][count
++] = "api_GL.so";
132 case ST_API_OPENGL_ES2
:
133 skip_checks
[api
] = "glShaderBinary";
134 symbols
[api
] = ST_CREATE_OPENGL_ES2_SYMBOL
;
135 filenames
[api
][count
++] = "api_GLESv2.so";
136 filenames
[api
][count
++] = "api_GL.so";
139 skip_checks
[api
] = "vgClear";
140 symbols
[api
] = ST_CREATE_OPENVG_SYMBOL
;
141 filenames
[api
][count
++]= "api_OpenVG.so";
144 assert(!"Unknown API Type\n");
145 skip_checks
[api
] = NULL
;
149 filenames
[api
][count
++]= NULL
;
150 assert(count
< Elements(filenames
[api
]));
152 /* heuristicically decide if the module is needed */
153 if (!self
|| !skip_checks
[api
] ||
154 util_dl_get_proc_address(self
, skip_checks
[api
])) {
155 /* unset so the module is not skipped */
156 skip_checks
[api
] = NULL
;
160 /* mark all moudles needed if we wrongly decided that none is needed */
162 memset(skip_checks
, 0, sizeof(skip_checks
));
167 for (api
= 0; api
< ST_API_COUNT
; api
++) {
168 struct egl_g3d_st_module
*stmod
= &egl_g3d_st_modules
[api
];
171 /* skip the module */
172 if (skip_checks
[api
])
175 /* try all filenames, including NULL */
176 for (p
= filenames
[api
]; *p
; p
++) {
177 if (egl_g3d_load_st_module(stmod
, *p
, symbols
[api
]))
181 egl_g3d_load_st_module(stmod
, NULL
, symbols
[api
]);
183 stapis
[api
] = stmod
->stapi
;
188 egl_g3d_destroy_st_apis(void)
192 for (api
= 0; api
< ST_API_COUNT
; api
++) {
193 struct egl_g3d_st_module
*stmod
= &egl_g3d_st_modules
[api
];
196 stmod
->stapi
->destroy(stmod
->stapi
);
200 util_dl_close(stmod
->lib
);
203 stmod
->filename
= NULL
;
208 egl_g3d_st_manager_get_egl_image(struct st_manager
*smapi
,
209 struct st_egl_image
*stimg
)
211 struct egl_g3d_st_manager
*gsmapi
= egl_g3d_st_manager(smapi
);
212 EGLImageKHR handle
= (EGLImageKHR
) stimg
->egl_image
;
214 struct egl_g3d_image
*gimg
;
216 /* this is called from state trackers */
217 _eglLockMutex(&gsmapi
->display
->Mutex
);
219 img
= _eglLookupImage(handle
, gsmapi
->display
);
221 _eglUnlockMutex(&gsmapi
->display
->Mutex
);
225 gimg
= egl_g3d_image(img
);
227 stimg
->texture
= NULL
;
228 pipe_resource_reference(&stimg
->texture
, gimg
->texture
);
229 stimg
->face
= gimg
->face
;
230 stimg
->level
= gimg
->level
;
231 stimg
->zslice
= gimg
->zslice
;
233 _eglUnlockMutex(&gsmapi
->display
->Mutex
);
239 egl_g3d_create_st_manager(_EGLDisplay
*dpy
)
241 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
242 struct egl_g3d_st_manager
*gsmapi
;
244 gsmapi
= CALLOC_STRUCT(egl_g3d_st_manager
);
246 gsmapi
->display
= dpy
;
248 gsmapi
->base
.screen
= gdpy
->native
->screen
;
249 gsmapi
->base
.get_egl_image
= egl_g3d_st_manager_get_egl_image
;
252 return &gsmapi
->base
;;
256 egl_g3d_destroy_st_manager(struct st_manager
*smapi
)
258 struct egl_g3d_st_manager
*gsmapi
= egl_g3d_st_manager(smapi
);
263 egl_g3d_st_framebuffer_flush_front_pbuffer(struct st_framebuffer_iface
*stfbi
,
264 enum st_attachment_type statt
)
270 egl_g3d_st_framebuffer_validate_pbuffer(struct st_framebuffer_iface
*stfbi
,
271 const enum st_attachment_type
*statts
,
273 struct pipe_resource
**out
)
275 _EGLSurface
*surf
= (_EGLSurface
*) stfbi
->st_manager_private
;
276 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
277 struct pipe_resource templ
;
280 for (i
= 0; i
< count
; i
++) {
283 if (gsurf
->stvis
.render_buffer
!= statts
[i
])
286 if (!gsurf
->render_texture
) {
287 struct egl_g3d_display
*gdpy
=
288 egl_g3d_display(gsurf
->base
.Resource
.Display
);
289 struct pipe_screen
*screen
= gdpy
->native
->screen
;
291 memset(&templ
, 0, sizeof(templ
));
292 templ
.target
= PIPE_TEXTURE_2D
;
293 templ
.last_level
= 0;
294 templ
.width0
= gsurf
->base
.Width
;
295 templ
.height0
= gsurf
->base
.Height
;
297 templ
.format
= gsurf
->stvis
.color_format
;
298 templ
.bind
= PIPE_BIND_RENDER_TARGET
;
300 gsurf
->render_texture
= screen
->resource_create(screen
, &templ
);
303 pipe_resource_reference(&out
[i
], gsurf
->render_texture
);
310 egl_g3d_st_framebuffer_flush_front(struct st_framebuffer_iface
*stfbi
,
311 enum st_attachment_type statt
)
313 _EGLSurface
*surf
= (_EGLSurface
*) stfbi
->st_manager_private
;
314 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
316 return gsurf
->native
->flush_frontbuffer(gsurf
->native
);
320 egl_g3d_st_framebuffer_validate(struct st_framebuffer_iface
*stfbi
,
321 const enum st_attachment_type
*statts
,
323 struct pipe_resource
**out
)
325 _EGLSurface
*surf
= (_EGLSurface
*) stfbi
->st_manager_private
;
326 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
327 struct pipe_resource
*textures
[NUM_NATIVE_ATTACHMENTS
];
328 uint attachment_mask
= 0;
331 for (i
= 0; i
< count
; i
++) {
335 case ST_ATTACHMENT_FRONT_LEFT
:
336 natt
= NATIVE_ATTACHMENT_FRONT_LEFT
;
338 case ST_ATTACHMENT_BACK_LEFT
:
339 natt
= NATIVE_ATTACHMENT_BACK_LEFT
;
341 case ST_ATTACHMENT_FRONT_RIGHT
:
342 natt
= NATIVE_ATTACHMENT_FRONT_RIGHT
;
344 case ST_ATTACHMENT_BACK_RIGHT
:
345 natt
= NATIVE_ATTACHMENT_BACK_RIGHT
;
353 attachment_mask
|= 1 << natt
;
356 if (!gsurf
->native
->validate(gsurf
->native
, attachment_mask
,
357 &gsurf
->sequence_number
, textures
, &gsurf
->base
.Width
,
358 &gsurf
->base
.Height
))
361 for (i
= 0; i
< count
; i
++) {
362 struct pipe_resource
*tex
;
366 case ST_ATTACHMENT_FRONT_LEFT
:
367 natt
= NATIVE_ATTACHMENT_FRONT_LEFT
;
369 case ST_ATTACHMENT_BACK_LEFT
:
370 natt
= NATIVE_ATTACHMENT_BACK_LEFT
;
372 case ST_ATTACHMENT_FRONT_RIGHT
:
373 natt
= NATIVE_ATTACHMENT_FRONT_RIGHT
;
375 case ST_ATTACHMENT_BACK_RIGHT
:
376 natt
= NATIVE_ATTACHMENT_BACK_RIGHT
;
384 tex
= textures
[natt
];
386 if (statts
[i
] == stfbi
->visual
->render_buffer
)
387 pipe_resource_reference(&gsurf
->render_texture
, tex
);
389 if (attachment_mask
& (1 << natt
)) {
390 /* transfer the ownership to the caller */
392 attachment_mask
&= ~(1 << natt
);
395 /* the attachment is listed more than once */
396 pipe_resource_reference(&out
[i
], tex
);
404 struct st_framebuffer_iface
*
405 egl_g3d_create_st_framebuffer(_EGLSurface
*surf
)
407 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
408 struct st_framebuffer_iface
*stfbi
;
410 stfbi
= CALLOC_STRUCT(st_framebuffer_iface
);
414 stfbi
->visual
= &gsurf
->stvis
;
415 if (gsurf
->base
.Type
!= EGL_PBUFFER_BIT
) {
416 stfbi
->flush_front
= egl_g3d_st_framebuffer_flush_front
;
417 stfbi
->validate
= egl_g3d_st_framebuffer_validate
;
420 stfbi
->flush_front
= egl_g3d_st_framebuffer_flush_front_pbuffer
;
421 stfbi
->validate
= egl_g3d_st_framebuffer_validate_pbuffer
;
423 stfbi
->st_manager_private
= (void *) &gsurf
->base
;
429 egl_g3d_destroy_st_framebuffer(struct st_framebuffer_iface
*stfbi
)