1 /*
2  * Copyright © 2008 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Eric Anholt <eric@anholt.net>
25  *    Zhigang Gong <zhigang.gong@linux.intel.com>
26  *
27  */
28 
29 #ifndef GLAMOR_H
30 #define GLAMOR_H
31 
32 #include <scrnintstr.h>
33 #include <pixmapstr.h>
34 #include <gcstruct.h>
35 #include <picturestr.h>
36 #include <fb.h>
37 #include <fbpict.h>
38 #ifdef GLAMOR_FOR_XORG
39 #include <xf86xv.h>
40 #endif
41 
42 struct glamor_context;
43 struct gbm_bo;
44 struct gbm_device;
45 
46 /*
47  * glamor_pixmap_type : glamor pixmap's type.
48  * @MEMORY: pixmap is in memory.
49  * @TEXTURE_DRM: pixmap is in a texture created from a DRM buffer.
50  * @SEPARATE_TEXTURE: The texture is created from a DRM buffer, but
51  * 		      the format is incompatible, so this type of pixmap
52  * 		      will never fallback to DDX layer.
53  * @DRM_ONLY: pixmap is in a external DRM buffer.
54  * @TEXTURE_ONLY: pixmap is in an internal texture.
55  */
56 typedef enum glamor_pixmap_type {
57     GLAMOR_MEMORY = 0, /* Newly calloc()ed pixmaps are memory. */
58     GLAMOR_TEXTURE_DRM,
59     GLAMOR_DRM_ONLY,
60     GLAMOR_TEXTURE_ONLY,
61 } glamor_pixmap_type_t;
62 
63 typedef Bool (*GetDrawableModifiersFuncPtr) (DrawablePtr draw,
64                                              uint32_t format,
65                                              uint32_t *num_modifiers,
66                                              uint64_t **modifiers);
67 
68 #define GLAMOR_EGL_EXTERNAL_BUFFER 3
69 #define GLAMOR_USE_EGL_SCREEN		(1 << 0)
70 #define GLAMOR_NO_DRI3			(1 << 1)
71 #define GLAMOR_VALID_FLAGS      (GLAMOR_USE_EGL_SCREEN                \
72                                  | GLAMOR_NO_DRI3)
73 
74 /* until we need geometry shaders GL3.1 should suffice. */
75 #define GLAMOR_GL_CORE_VER_MAJOR 3
76 #define GLAMOR_GL_CORE_VER_MINOR 1
77 
78 /* @glamor_init: Initialize glamor internal data structure.
79  *
80  * @screen: Current screen pointer.
81  * @flags:  Please refer the flags description above.
82  *
83  * 	@GLAMOR_USE_EGL_SCREEN:
84  * 	If you are using EGL layer, then please set this bit
85  * 	on, otherwise, clear it.
86  *
87  *      @GLAMOR_NO_DRI3
88  *      Disable the built-in DRI3 support
89  *
90  * This function initializes necessary internal data structure
91  * for glamor. And before calling into this function, the OpenGL
92  * environment should be ready. Should be called before any real
93  * glamor rendering or texture allocation functions. And should
94  * be called after the DDX's screen initialization or at the last
95  * step of the DDX's screen initialization.
96  */
97 extern _X_EXPORT Bool glamor_init(ScreenPtr screen, unsigned int flags);
98 extern _X_EXPORT void glamor_fini(ScreenPtr screen);
99 
100 /* This function is used to free the glamor private screen's
101  * resources. If the DDX driver is not set GLAMOR_USE_SCREEN,
102  * then, DDX need to call this function at proper stage, if
103  * it is the xorg DDX driver,then it should be called at free
104  * screen stage not the close screen stage. The reason is after
105  * call to this function, the xorg DDX may need to destroy the
106  * screen pixmap which must be a glamor pixmap and requires
107  * the internal data structure still exist at that time.
108  * Otherwise, the glamor internal structure will not be freed.*/
109 extern _X_EXPORT Bool glamor_close_screen(ScreenPtr screen);
110 
111 extern _X_EXPORT uint32_t glamor_get_pixmap_texture(PixmapPtr pixmap);
112 
113 extern _X_EXPORT Bool glamor_set_pixmap_texture(PixmapPtr pixmap,
114                                                 unsigned int tex);
115 
116 extern _X_EXPORT void glamor_set_pixmap_type(PixmapPtr pixmap,
117                                              glamor_pixmap_type_t type);
118 
119 extern _X_EXPORT void glamor_clear_pixmap(PixmapPtr pixmap);
120 
121 extern _X_EXPORT void glamor_block_handler(ScreenPtr screen);
122 
123 extern _X_EXPORT PixmapPtr glamor_create_pixmap(ScreenPtr screen, int w, int h,
124                                                 int depth, unsigned int usage);
125 extern _X_EXPORT Bool glamor_destroy_pixmap(PixmapPtr pixmap);
126 
127 #define GLAMOR_CREATE_PIXMAP_CPU        0x100
128 #define GLAMOR_CREATE_PIXMAP_FIXUP      0x101
129 #define GLAMOR_CREATE_FBO_NO_FBO        0x103
130 #define GLAMOR_CREATE_NO_LARGE          0x105
131 #define GLAMOR_CREATE_PIXMAP_NO_TEXTURE 0x106
132 #define GLAMOR_CREATE_FORMAT_CBCR       0x107
133 
134 /* @glamor_egl_exchange_buffers: Exchange the underlying buffers(KHR image,fbo).
135  *
136  * @front: front pixmap.
137  * @back: back pixmap.
138  *
139  * Used by the DRI2 page flip. This function will exchange the KHR images and
140  * fbos of the two pixmaps.
141  * */
142 extern _X_EXPORT void glamor_egl_exchange_buffers(PixmapPtr front,
143                                                   PixmapPtr back);
144 
145 extern _X_EXPORT void glamor_pixmap_exchange_fbos(PixmapPtr front,
146                                                   PixmapPtr back);
147 
148 /* The DDX is not supposed to call these four functions */
149 extern _X_EXPORT void glamor_enable_dri3(ScreenPtr screen);
150 extern _X_EXPORT int glamor_egl_fds_from_pixmap(ScreenPtr, PixmapPtr, int *,
151                                                 uint32_t *, uint32_t *,
152                                                 uint64_t *);
153 extern _X_EXPORT int glamor_egl_fd_name_from_pixmap(ScreenPtr, PixmapPtr,
154                                                     CARD16 *, CARD32 *);
155 
156 extern _X_EXPORT struct gbm_device *glamor_egl_get_gbm_device(ScreenPtr screen);
157 extern _X_EXPORT int glamor_egl_fd_from_pixmap(ScreenPtr, PixmapPtr, CARD16 *, CARD32 *);
158 
159 /* @glamor_supports_pixmap_import_export: Returns whether
160  * glamor_fds_from_pixmap(), glamor_name_from_pixmap(), and
161  * glamor_pixmap_from_fds() are supported.
162  *
163  * @screen: Current screen pointer.
164  *
165  * To have DRI3 support enabled, glamor and glamor_egl need to be
166  * initialized. glamor also has to be compiled with gbm support.
167  *
168  * The EGL layer needs to have the following extensions working:
169  *
170  * .EGL_KHR_surfaceless_context
171  * */
172 extern _X_EXPORT Bool glamor_supports_pixmap_import_export(ScreenPtr screen);
173 
174 /* @glamor_fds_from_pixmap: Get a dma-buf fd from a pixmap.
175  *
176  * @screen: Current screen pointer.
177  * @pixmap: The pixmap from which we want the fd.
178  * @fds, @strides, @offsets: Pointers to fill info of each plane.
179  * @modifier: Pointer to fill the modifier of the buffer.
180  *
181  * the pixmap and the buffer associated by the fds will share the same
182  * content. The caller is responsible to close the returned file descriptors.
183  * Returns the number of planes, -1 on error.
184  * */
185 extern _X_EXPORT int glamor_fds_from_pixmap(ScreenPtr screen,
186                                             PixmapPtr pixmap,
187                                             int *fds,
188                                             uint32_t *strides, uint32_t *offsets,
189                                             uint64_t *modifier);
190 
191 /* @glamor_fd_from_pixmap: Get a dma-buf fd from a pixmap.
192  *
193  * @screen: Current screen pointer.
194  * @pixmap: The pixmap from which we want the fd.
195  * @stride, @size: Pointers to fill the stride and size of the
196  * 		   buffer associated to the fd.
197  *
198  * the pixmap and the buffer associated by the fd will share the same
199  * content.
200  * Returns the fd on success, -1 on error.
201  * */
202 extern _X_EXPORT int glamor_fd_from_pixmap(ScreenPtr screen,
203                                            PixmapPtr pixmap,
204                                            CARD16 *stride, CARD32 *size);
205 
206 /* @glamor_shareable_fd_from_pixmap: Get a dma-buf fd suitable for sharing
207  *				     with other GPUs from a pixmap.
208  *
209  * @screen: Current screen pointer.
210  * @pixmap: The pixmap from which we want the fd.
211  * @stride, @size: Pointers to fill the stride and size of the
212  * 		   buffer associated to the fd.
213  *
214  * The returned fd will point to a buffer which is suitable for sharing
215  * across GPUs (not using GPU specific tiling).
216  * The pixmap and the buffer associated by the fd will share the same
217  * content.
218  * The pixmap's stride may be modified by this function.
219  * Returns the fd on success, -1 on error.
220  * */
221 extern _X_EXPORT int glamor_shareable_fd_from_pixmap(ScreenPtr screen,
222                                                      PixmapPtr pixmap,
223                                                      CARD16 *stride,
224                                                      CARD32 *size);
225 
226 /**
227  * @glamor_name_from_pixmap: Gets a gem name from a pixmap.
228  *
229  * @pixmap: The pixmap from which we want the gem name.
230  *
231  * the pixmap and the buffer associated by the gem name will share the
232  * same content. This function can be used by the DDX to support DRI2,
233  * and needs the same set of buffer export GL extensions as DRI3
234  * support.
235  *
236  * Returns the name on success, -1 on error.
237  * */
238 extern _X_EXPORT int glamor_name_from_pixmap(PixmapPtr pixmap,
239                                              CARD16 *stride, CARD32 *size);
240 
241 /* @glamor_gbm_bo_from_pixmap: Get a GBM bo from a pixmap.
242  *
243  * @screen: Current screen pointer.
244  * @pixmap: The pixmap from which we want the fd.
245  * @stride, @size: Pointers to fill the stride and size of the
246  * 		   buffer associated to the fd.
247  *
248  * the pixmap and the buffer represented by the gbm_bo will share the same
249  * content.
250  *
251  * Returns the gbm_bo on success, NULL on error.
252  * */
253 extern _X_EXPORT struct gbm_bo *glamor_gbm_bo_from_pixmap(ScreenPtr screen,
254                                                           PixmapPtr pixmap);
255 
256 /* @glamor_pixmap_from_fds: Creates a pixmap to wrap a dma-buf fds.
257  *
258  * @screen: Current screen pointer.
259  * @num_fds: Number of fds to import
260  * @fds: The dma-buf fds to import.
261  * @width: The width of the buffers.
262  * @height: The height of the buffers.
263  * @stride: The stride of the buffers.
264  * @depth: The depth of the buffers.
265  * @bpp: The bpp of the buffers.
266  * @modifier: The modifier of the buffers.
267  *
268  * Returns a valid pixmap if the import succeeded, else NULL.
269  * */
270 extern _X_EXPORT PixmapPtr glamor_pixmap_from_fds(ScreenPtr screen,
271                                                   CARD8 num_fds,
272                                                   const int *fds,
273                                                   CARD16 width,
274                                                   CARD16 height,
275                                                   const CARD32 *strides,
276                                                   const CARD32 *offsets,
277                                                   CARD8 depth,
278                                                   CARD8 bpp,
279                                                   uint64_t modifier);
280 
281 /* @glamor_pixmap_from_fd: Creates a pixmap to wrap a dma-buf fd.
282  *
283  * @screen: Current screen pointer.
284  * @fd: The dma-buf fd to import.
285  * @width: The width of the buffer.
286  * @height: The height of the buffer.
287  * @stride: The stride of the buffer.
288  * @depth: The depth of the buffer.
289  * @bpp: The bpp of the buffer.
290  *
291  * Returns a valid pixmap if the import succeeded, else NULL.
292  * */
293 extern _X_EXPORT PixmapPtr glamor_pixmap_from_fd(ScreenPtr screen,
294                                                  int fd,
295                                                  CARD16 width,
296                                                  CARD16 height,
297                                                  CARD16 stride,
298                                                  CARD8 depth,
299                                                  CARD8 bpp);
300 
301 /* @glamor_back_pixmap_from_fd: Backs an existing pixmap with a dma-buf fd.
302  *
303  * @pixmap: Pixmap to change backing for
304  * @fd: The dma-buf fd to import.
305  * @width: The width of the buffer.
306  * @height: The height of the buffer.
307  * @stride: The stride of the buffer.
308  * @depth: The depth of the buffer.
309  * @bpp: The number of bpp of the buffer.
310  *
311  * Returns TRUE if successful, FALSE on failure.
312  * */
313 extern _X_EXPORT Bool glamor_back_pixmap_from_fd(PixmapPtr pixmap,
314                                                  int fd,
315                                                  CARD16 width,
316                                                  CARD16 height,
317                                                  CARD16 stride,
318                                                  CARD8 depth,
319                                                  CARD8 bpp);
320 
321 extern _X_EXPORT Bool glamor_get_formats(ScreenPtr screen,
322                                          CARD32 *num_formats,
323                                          CARD32 **formats);
324 
325 extern _X_EXPORT Bool glamor_get_modifiers(ScreenPtr screen,
326                                            uint32_t format,
327                                            uint32_t *num_modifiers,
328                                            uint64_t **modifiers);
329 
330 extern _X_EXPORT Bool glamor_get_drawable_modifiers(DrawablePtr draw,
331                                                     uint32_t format,
332                                                     uint32_t *num_modifiers,
333                                                     uint64_t **modifiers);
334 
335 extern _X_EXPORT void glamor_set_drawable_modifiers_func(ScreenPtr screen,
336                                                          GetDrawableModifiersFuncPtr func);
337 
338 #ifdef GLAMOR_FOR_XORG
339 
340 #define GLAMOR_EGL_MODULE_NAME  "glamoregl"
341 
342 /* @glamor_egl_init: Initialize EGL environment.
343  *
344  * @scrn: Current screen info pointer.
345  * @fd:   Current drm fd.
346  *
347  * This function creates and initializes EGL contexts.
348  * Should be called from DDX's preInit function.
349  * Return TRUE if success, otherwise return FALSE.
350  * */
351 extern _X_EXPORT Bool glamor_egl_init(ScrnInfoPtr scrn, int fd);
352 
353 extern _X_EXPORT Bool glamor_egl_init_textured_pixmap(ScreenPtr screen);
354 
355 /* @glamor_egl_create_textured_screen: Create textured screen pixmap.
356  *
357  * @screen: screen pointer to be processed.
358  * @handle: screen pixmap's BO handle.
359  * @stride: screen pixmap's stride in bytes.
360  *
361  * This function is similar with the create_textured_pixmap. As the
362  * screen pixmap is a special, we handle it separately in this function.
363  */
364 extern _X_EXPORT Bool glamor_egl_create_textured_screen(ScreenPtr screen,
365                                                         int handle, int stride);
366 
367 /* Obsolete entrypoint, temporarily left here for API compatibility
368  * for xf86-video-ati.
369  */
370 #define glamor_egl_create_textured_screen_ext(a, b, c, d) \
371     glamor_egl_create_textured_screen(a, b, c)
372 
373 /*
374  * @glamor_egl_create_textured_pixmap: Try to create a textured pixmap from
375  * 				       a BO handle.
376  *
377  * @pixmap: The pixmap need to be processed.
378  * @handle: The BO's handle attached to this pixmap at DDX layer.
379  * @stride: Stride in bytes for this pixmap.
380  *
381  * This function try to create a texture from the handle and attach
382  * the texture to the pixmap , thus glamor can render to this pixmap
383  * as well. Return true if successful, otherwise return FALSE.
384  */
385 extern _X_EXPORT Bool glamor_egl_create_textured_pixmap(PixmapPtr pixmap,
386                                                         int handle, int stride);
387 
388 /*
389  * @glamor_egl_create_textured_pixmap_from_bo: Try to create a textured pixmap
390  * 					       from a gbm_bo.
391  *
392  * @pixmap: The pixmap need to be processed.
393  * @bo: a pointer on a gbm_bo structure attached to this pixmap at DDX layer.
394  *
395  * This function is similar to glamor_egl_create_textured_pixmap.
396  */
397 extern _X_EXPORT Bool
398  glamor_egl_create_textured_pixmap_from_gbm_bo(PixmapPtr pixmap,
399                                                struct gbm_bo *bo,
400                                                Bool used_modifiers);
401 
402 extern _X_EXPORT const char *glamor_egl_get_driver_name(ScreenPtr screen);
403 
404 #endif
405 
406 extern _X_EXPORT void glamor_egl_screen_init(ScreenPtr screen,
407                                              struct glamor_context *glamor_ctx);
408 
409 extern _X_EXPORT int glamor_create_gc(GCPtr gc);
410 
411 extern _X_EXPORT void glamor_validate_gc(GCPtr gc, unsigned long changes,
412                                          DrawablePtr drawable);
413 
414 extern _X_EXPORT void glamor_destroy_gc(GCPtr gc);
415 
416 #define HAS_GLAMOR_DESTROY_GC 1
417 
418 extern Bool _X_EXPORT glamor_change_window_attributes(WindowPtr pWin, unsigned long mask);
419 extern void _X_EXPORT glamor_copy_window(WindowPtr window, DDXPointRec old_origin, RegionPtr src_region);
420 
421 extern _X_EXPORT void glamor_finish(ScreenPtr screen);
422 #define HAS_GLAMOR_TEXT 1
423 
424 #ifdef GLAMOR_FOR_XORG
425 extern _X_EXPORT XF86VideoAdaptorPtr glamor_xv_init(ScreenPtr pScreen,
426                                                     int num_texture_ports);
427 #endif
428 
429 #endif                          /* GLAMOR_H */
430