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_context_iface
*stctx
,
211 struct st_egl_image
*out
)
213 struct egl_g3d_st_manager
*gsmapi
= egl_g3d_st_manager(smapi
);
214 EGLImageKHR handle
= (EGLImageKHR
) egl_image
;
216 struct egl_g3d_image
*gimg
;
218 /* this is called from state trackers */
219 _eglLockMutex(&gsmapi
->display
->Mutex
);
221 img
= _eglLookupImage(handle
, gsmapi
->display
);
223 _eglUnlockMutex(&gsmapi
->display
->Mutex
);
227 gimg
= egl_g3d_image(img
);
230 pipe_resource_reference(&out
->texture
, gimg
->texture
);
231 out
->face
= gimg
->face
;
232 out
->level
= gimg
->level
;
233 out
->zslice
= gimg
->zslice
;
235 _eglUnlockMutex(&gsmapi
->display
->Mutex
);
241 egl_g3d_st_manager_get_param(struct st_manager
*smapi
,
242 enum st_manager_param param
)
248 egl_g3d_create_st_manager(_EGLDisplay
*dpy
)
250 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
251 struct egl_g3d_st_manager
*gsmapi
;
253 gsmapi
= CALLOC_STRUCT(egl_g3d_st_manager
);
255 gsmapi
->display
= dpy
;
257 gsmapi
->base
.screen
= gdpy
->native
->screen
;
258 gsmapi
->base
.get_egl_image
= egl_g3d_st_manager_get_egl_image
;
259 gsmapi
->base
.get_param
= egl_g3d_st_manager_get_param
;
262 return &gsmapi
->base
;;
266 egl_g3d_destroy_st_manager(struct st_manager
*smapi
)
268 struct egl_g3d_st_manager
*gsmapi
= egl_g3d_st_manager(smapi
);
273 egl_g3d_st_framebuffer_flush_front_pbuffer(struct st_framebuffer_iface
*stfbi
,
274 enum st_attachment_type statt
)
280 egl_g3d_st_framebuffer_validate_pbuffer(struct st_framebuffer_iface
*stfbi
,
281 const enum st_attachment_type
*statts
,
283 struct pipe_resource
**out
)
285 _EGLSurface
*surf
= (_EGLSurface
*) stfbi
->st_manager_private
;
286 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
287 struct pipe_resource templ
;
290 for (i
= 0; i
< count
; i
++) {
293 if (gsurf
->stvis
.render_buffer
!= statts
[i
])
296 if (!gsurf
->render_texture
) {
297 struct egl_g3d_display
*gdpy
=
298 egl_g3d_display(gsurf
->base
.Resource
.Display
);
299 struct pipe_screen
*screen
= gdpy
->native
->screen
;
301 memset(&templ
, 0, sizeof(templ
));
302 templ
.target
= PIPE_TEXTURE_2D
;
303 templ
.last_level
= 0;
304 templ
.width0
= gsurf
->base
.Width
;
305 templ
.height0
= gsurf
->base
.Height
;
307 templ
.format
= gsurf
->stvis
.color_format
;
308 templ
.bind
= PIPE_BIND_RENDER_TARGET
;
310 gsurf
->render_texture
= screen
->resource_create(screen
, &templ
);
313 pipe_resource_reference(&out
[i
], gsurf
->render_texture
);
320 egl_g3d_st_framebuffer_flush_front(struct st_framebuffer_iface
*stfbi
,
321 enum st_attachment_type statt
)
323 _EGLSurface
*surf
= (_EGLSurface
*) stfbi
->st_manager_private
;
324 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
326 return gsurf
->native
->flush_frontbuffer(gsurf
->native
);
330 egl_g3d_st_framebuffer_validate(struct st_framebuffer_iface
*stfbi
,
331 const enum st_attachment_type
*statts
,
333 struct pipe_resource
**out
)
335 _EGLSurface
*surf
= (_EGLSurface
*) stfbi
->st_manager_private
;
336 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
337 struct pipe_resource
*textures
[NUM_NATIVE_ATTACHMENTS
];
338 uint attachment_mask
= 0;
341 for (i
= 0; i
< count
; i
++) {
345 case ST_ATTACHMENT_FRONT_LEFT
:
346 natt
= NATIVE_ATTACHMENT_FRONT_LEFT
;
348 case ST_ATTACHMENT_BACK_LEFT
:
349 natt
= NATIVE_ATTACHMENT_BACK_LEFT
;
351 case ST_ATTACHMENT_FRONT_RIGHT
:
352 natt
= NATIVE_ATTACHMENT_FRONT_RIGHT
;
354 case ST_ATTACHMENT_BACK_RIGHT
:
355 natt
= NATIVE_ATTACHMENT_BACK_RIGHT
;
363 attachment_mask
|= 1 << natt
;
366 if (!gsurf
->native
->validate(gsurf
->native
, attachment_mask
,
367 &gsurf
->sequence_number
, textures
, &gsurf
->base
.Width
,
368 &gsurf
->base
.Height
))
371 for (i
= 0; i
< count
; i
++) {
372 struct pipe_resource
*tex
;
376 case ST_ATTACHMENT_FRONT_LEFT
:
377 natt
= NATIVE_ATTACHMENT_FRONT_LEFT
;
379 case ST_ATTACHMENT_BACK_LEFT
:
380 natt
= NATIVE_ATTACHMENT_BACK_LEFT
;
382 case ST_ATTACHMENT_FRONT_RIGHT
:
383 natt
= NATIVE_ATTACHMENT_FRONT_RIGHT
;
385 case ST_ATTACHMENT_BACK_RIGHT
:
386 natt
= NATIVE_ATTACHMENT_BACK_RIGHT
;
394 tex
= textures
[natt
];
396 if (statts
[i
] == stfbi
->visual
->render_buffer
)
397 pipe_resource_reference(&gsurf
->render_texture
, tex
);
399 if (attachment_mask
& (1 << natt
)) {
400 /* transfer the ownership to the caller */
402 attachment_mask
&= ~(1 << natt
);
405 /* the attachment is listed more than once */
406 pipe_resource_reference(&out
[i
], tex
);
414 struct st_framebuffer_iface
*
415 egl_g3d_create_st_framebuffer(_EGLSurface
*surf
)
417 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
418 struct st_framebuffer_iface
*stfbi
;
420 stfbi
= CALLOC_STRUCT(st_framebuffer_iface
);
424 stfbi
->visual
= &gsurf
->stvis
;
425 if (gsurf
->base
.Type
!= EGL_PBUFFER_BIT
) {
426 stfbi
->flush_front
= egl_g3d_st_framebuffer_flush_front
;
427 stfbi
->validate
= egl_g3d_st_framebuffer_validate
;
430 stfbi
->flush_front
= egl_g3d_st_framebuffer_flush_front_pbuffer
;
431 stfbi
->validate
= egl_g3d_st_framebuffer_validate_pbuffer
;
433 stfbi
->st_manager_private
= (void *) &gsurf
->base
;
439 egl_g3d_destroy_st_framebuffer(struct st_framebuffer_iface
*stfbi
)