5 #include <android/hardware/gralloc.h>
10 #include <sys/types.h>
17 #include <wayland-client.h>
18 #include "server_wlegl.h"
19 #include "server_wlegl_buffer.h"
22 #include "windowbuffer.h"
24 static gralloc_module_t
*my_gralloc
= 0;
25 static alloc_device_t
*my_alloc
= 0;
27 extern "C" void eglplatformcommon_init(gralloc_module_t
*gralloc
, alloc_device_t
*allocdevice
)
30 my_alloc
= allocdevice
;
33 extern "C" int hybris_register_buffer_handle(buffer_handle_t handle
)
38 return my_gralloc
->registerBuffer(my_gralloc
, handle
);
41 extern "C" int hybris_unregister_buffer_handle(buffer_handle_t handle
)
46 return my_gralloc
->unregisterBuffer(my_gralloc
, handle
);
49 extern "C" void hybris_dump_buffer_to_file(ANativeWindowBuffer
*buf
)
53 int ret
= my_gralloc
->lock(my_gralloc
, buf
->handle
, buf
->usage
, 0, 0, buf
->width
, buf
->height
, &vaddr
);
54 TRACE("buf:%p gralloc lock returns %i", buf
, ret
);
55 TRACE("buf:%p lock to vaddr %p", buf
, vaddr
);
59 if (buf
->format
== HAL_PIXEL_FORMAT_RGBA_8888
)
61 else if (buf
->format
== HAL_PIXEL_FORMAT_RGB_565
)
64 snprintf(b
, 1020, "vaddr.%p.%p.%i.%is%ix%ix%i", buf
, vaddr
, cnt
, buf
->width
, buf
->stride
, buf
->height
, bytes_pp
);
66 int fd
= ::open(b
, O_WRONLY
|O_CREAT
, S_IRWXU
);
68 ::write(fd
, vaddr
, buf
->stride
* buf
->height
* bytes_pp
);
70 my_gralloc
->unlock(my_gralloc
, buf
->handle
);
75 extern "C" EGLBoolean
eglplatformcommon_eglBindWaylandDisplayWL(EGLDisplay dpy
, struct wl_display
*display
)
77 assert(my_gralloc
!= NULL
);
78 server_wlegl_create(display
, my_gralloc
);
82 extern "C" EGLBoolean
eglplatformcommon_eglUnbindWaylandDisplayWL(EGLDisplay dpy
, struct wl_display
*display
)
87 extern "C" EGLBoolean
eglplatformcommon_eglQueryWaylandBufferWL(EGLDisplay dpy
,
88 struct wl_buffer
*buffer
, EGLint attribute
, EGLint
*value
)
90 server_wlegl_buffer
*buf
= server_wlegl_buffer_from(buffer
);
91 ANativeWindowBuffer
* anwb
= (ANativeWindowBuffer
*) buf
->buf
;
93 if (attribute
== EGL_TEXTURE_FORMAT
) {
94 switch(anwb
->format
) {
95 case HAL_PIXEL_FORMAT_RGB_565
:
96 *value
= EGL_TEXTURE_RGB
;
98 case HAL_PIXEL_FORMAT_RGBA_8888
:
99 *value
= EGL_TEXTURE_RGBA
;
102 *value
= EGL_TEXTURE_EXTERNAL_WL
;
106 if (attribute
== EGL_WIDTH
) {
107 *value
= anwb
->width
;
110 if (attribute
== EGL_HEIGHT
) {
111 *value
= anwb
->height
;
119 extern "C" EGLBoolean
eglplatformcommon_eglHybrisCreateNativeBuffer(EGLint width
, EGLint height
, EGLint usage
, EGLint format
, EGLint
*stride
, EGLClientBuffer
*buffer
)
122 buffer_handle_t _handle
;
125 assert(my_gralloc
!= NULL
);
126 assert(my_alloc
!= NULL
);
128 ret
= my_alloc
->alloc(my_alloc
, width
, height
, format
, usage
, &_handle
, &_stride
);
132 RemoteWindowBuffer
*buf
= new RemoteWindowBuffer(width
, height
, _stride
, format
, usage
, _handle
, my_gralloc
);
133 buf
->common
.incRef(&buf
->common
);
134 *buffer
= (EGLClientBuffer
) static_cast<ANativeWindowBuffer
*>(buf
);
142 extern "C" EGLBoolean
eglplatformcommon_eglHybrisLockNativeBuffer(EGLClientBuffer buffer
, EGLint usage
, EGLint l
, EGLint t
, EGLint w
, EGLint h
, void **vaddr
)
145 RemoteWindowBuffer
*buf
= static_cast<RemoteWindowBuffer
*>((ANativeWindowBuffer
*) buffer
);
147 assert(my_gralloc
!= NULL
);
149 ret
= my_gralloc
->lock(my_gralloc
, buf
->handle
, usage
, l
, t
, w
, h
, vaddr
);
156 extern "C" EGLBoolean
eglplatformcommon_eglHybrisUnlockNativeBuffer(EGLClientBuffer buffer
)
159 RemoteWindowBuffer
*buf
= static_cast<RemoteWindowBuffer
*>((ANativeWindowBuffer
*) buffer
);
161 assert(my_gralloc
!= NULL
);
163 ret
= my_gralloc
->unlock(my_gralloc
, buf
->handle
);
171 extern "C" EGLBoolean
eglplatformcommon_eglHybrisReleaseNativeBuffer(EGLClientBuffer buffer
)
173 RemoteWindowBuffer
*buf
= static_cast<RemoteWindowBuffer
*>((ANativeWindowBuffer
*) buffer
);
175 buf
->common
.decRef(&buf
->common
);
182 eglplatformcommon_passthroughImageKHR(EGLContext
*ctx
, EGLenum
*target
, EGLClientBuffer
*buffer
, const EGLint
**attrib_list
)
185 static int debugenvchecked
= 0;
186 if (*target
== EGL_WAYLAND_BUFFER_WL
)
188 server_wlegl_buffer
*buf
= server_wlegl_buffer_from((struct wl_buffer
*)*buffer
);
189 HYBRIS_TRACE_BEGIN("eglplatformcommon", "Wayland_eglImageKHR", "-resource@%i", ((struct wl_buffer
*)*buffer
)->resource
.object
.id
);
190 HYBRIS_TRACE_END("eglplatformcommon", "Wayland_eglImageKHR", "-resource@%i", ((struct wl_buffer
*)*buffer
)->resource
.object
.id
);
191 if (debugenvchecked
== 0)
193 if (getenv("HYBRIS_WAYLAND_KHR_DUMP_BUFFERS") != NULL
)
197 } else if (debugenvchecked
== 2)
199 hybris_dump_buffer_to_file((ANativeWindowBuffer
*) buf
->buf
);
201 *buffer
= (EGLClientBuffer
) (ANativeWindowBuffer
*) buf
->buf
;
202 *target
= EGL_NATIVE_BUFFER_ANDROID
;
203 *ctx
= EGL_NO_CONTEXT
;
209 extern "C" __eglMustCastToProperFunctionPointerType
eglplatformcommon_eglGetProcAddress(const char *procname
)
212 if (strcmp(procname
, "eglBindWaylandDisplayWL") == 0)
214 return (__eglMustCastToProperFunctionPointerType
)eglplatformcommon_eglBindWaylandDisplayWL
;
217 if (strcmp(procname
, "eglUnbindWaylandDisplayWL") == 0)
219 return (__eglMustCastToProperFunctionPointerType
)eglplatformcommon_eglUnbindWaylandDisplayWL
;
221 if (strcmp(procname
, "eglQueryWaylandBufferWL") == 0)
223 return (__eglMustCastToProperFunctionPointerType
)eglplatformcommon_eglQueryWaylandBufferWL
;
227 if (strcmp(procname
, "eglHybrisCreateNativeBuffer") == 0)
229 return (__eglMustCastToProperFunctionPointerType
)eglplatformcommon_eglHybrisCreateNativeBuffer
;
232 if (strcmp(procname
, "eglHybrisLockNativeBuffer") == 0)
234 return (__eglMustCastToProperFunctionPointerType
)eglplatformcommon_eglHybrisLockNativeBuffer
;
237 if (strcmp(procname
, "eglHybrisUnlockNativeBuffer") == 0)
239 return (__eglMustCastToProperFunctionPointerType
)eglplatformcommon_eglHybrisUnlockNativeBuffer
;
242 if (strcmp(procname
, "eglHybrisReleaseNativeBuffer") == 0)
244 return (__eglMustCastToProperFunctionPointerType
)eglplatformcommon_eglHybrisReleaseNativeBuffer
;
249 extern "C" const char *eglplatformcommon_eglQueryString(EGLDisplay dpy
, EGLint name
, const char *(*real_eglQueryString
)(EGLDisplay dpy
, EGLint name
))
252 if (name
== EGL_EXTENSIONS
)
254 const char *ret
= (*real_eglQueryString
)(dpy
, name
);
255 static char eglextensionsbuf
[512];
257 snprintf(eglextensionsbuf
, 510, "%sEGL_HYBRIS_native_buffer %s", ret
,
259 "EGL_WL_bind_wayland_display "
264 ret
= eglextensionsbuf
;
268 return (*real_eglQueryString
)(dpy
, name
);