1 /* 2 * Copyright © 2012 John Kåre Alsaker 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining 5 * a copy of this software and associated documentation files (the 6 * "Software"), to deal in the Software without restriction, including 7 * without limitation the rights to use, copy, modify, merge, publish, 8 * distribute, sublicense, and/or sell copies of the Software, and to 9 * permit persons to whom the Software is furnished to do so, subject to 10 * the following conditions: 11 * 12 * The above copyright notice and this permission notice (including the 13 * next paragraph) shall be included in all copies or substantial 14 * portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 * SOFTWARE. 24 */ 25 26 #include "config.h" 27 28 #include <stdint.h> 29 30 #include <libweston/libweston.h> 31 #include "backend.h" 32 #include "libweston-internal.h" 33 #include <hilog/log.h> // OHOS hilog 34 35 // OHOS hilog 36 #ifndef weston_log 37 #define weston_log(fmt, ...) (HILOG_INFO(LOG_CORE, fmt, ##__VA_ARGS__)) 38 #define weston_log_continue(fmt, ...) (HILOG_DEBUG(LOG_CORE, fmt, ##__VA_ARGS__)) 39 #endif 40 41 #ifdef ENABLE_EGL 42 43 #include <EGL/egl.h> 44 #include <EGL/eglext.h> 45 46 #else 47 48 typedef int EGLint; 49 typedef int EGLenum; 50 typedef void *EGLDisplay; 51 typedef void *EGLSurface; 52 typedef void *EGLConfig; 53 typedef intptr_t EGLNativeDisplayType; 54 typedef intptr_t EGLNativeWindowType; 55 #define EGL_DEFAULT_DISPLAY ((EGLNativeDisplayType)0) 56 #define EGL_PBUFFER_BIT 0x0001 57 #define EGL_WINDOW_BIT 0x0004 58 59 #endif /* ENABLE_EGL */ 60 61 enum gl_renderer_border_side { 62 GL_RENDERER_BORDER_TOP = 0, 63 GL_RENDERER_BORDER_LEFT = 1, 64 GL_RENDERER_BORDER_RIGHT = 2, 65 GL_RENDERER_BORDER_BOTTOM = 3, 66 }; 67 68 /** 69 * Options passed to the \c display_create method of the GL renderer interface. 70 * 71 * \see struct gl_renderer_interface 72 */ 73 struct gl_renderer_display_options { 74 /** The EGL platform identifier */ 75 EGLenum egl_platform; 76 /** The native display corresponding to the given EGL platform */ 77 void *egl_native_display; 78 /** EGL_SURFACE_TYPE bits for the base EGLConfig */ 79 EGLint egl_surface_type; 80 /** Array of DRM pixel formats acceptable for the base EGLConfig */ 81 const uint32_t *drm_formats; 82 /** The \c drm_formats array length */ 83 unsigned drm_formats_count; 84 }; 85 86 struct gl_renderer_output_options { 87 /** Native window handle for \c eglCreateWindowSurface */ 88 EGLNativeWindowType window_for_legacy; 89 /** Native window handle for \c eglCreatePlatformWindowSurface */ 90 void *window_for_platform; 91 /** Array of DRM pixel formats acceptable for the window */ 92 const uint32_t *drm_formats; 93 /** The \c drm_formats array length */ 94 unsigned drm_formats_count; 95 }; 96 97 struct gl_renderer_pbuffer_options { 98 /** Width of the rendering surface in pixels */ 99 int width; 100 /** Height of the rendering surface in pixels */ 101 int height; 102 /** Array of DRM pixel formats acceptable for the pbuffer */ 103 const uint32_t *drm_formats; 104 /** The \c drm_formats array length */ 105 unsigned drm_formats_count; 106 }; 107 108 struct gl_renderer_interface { 109 /** 110 * Initialize GL-renderer with the given EGL platform and native display 111 * 112 * \param ec The weston_compositor where to initialize. 113 * \param options The options struct describing display configuration 114 * \return 0 on success, -1 on failure. 115 * 116 * This function creates an EGLDisplay and initializes it. It also 117 * creates the GL ES context and sets it up. It attempts GL ES 3.0 118 * and falls back to GL ES 2.0 if 3.0 is not supported. 119 * 120 * If \c platform is zero or EGL_EXT_platform_base is not supported, 121 * choosing the platform is left for the EGL implementation. Otherwise 122 * the given platform is used explicitly if the EGL implementation 123 * advertises it. Without the advertisement this function fails. 124 * 125 * If neither EGL_KHR_no_config_context or EGL_MESA_configless_context 126 * are supported, the arguments egl_surface_type, drm_formats, and 127 * drm_formats_count are used to find a so called base EGLConfig. The 128 * GL context is created with the base EGLConfig, and outputs will be 129 * required to use the same config as well. If one or both of the 130 * extensions are supported, these arguments are unused, and each 131 * output can use a different EGLConfig (pixel format). 132 * 133 * The first format in drm_formats that matches any EGLConfig 134 * determines which EGLConfig is chosen. On EGL GBM platform, the 135 * pixel format must match exactly. On other platforms, it is enough 136 * that each R, G, B, A channel has the same number of bits as in the 137 * DRM format. 138 */ 139 int (*display_create)(struct weston_compositor *ec, 140 const struct gl_renderer_display_options *options); 141 142 /** 143 * Attach GL-renderer to the output with a native window 144 * 145 * \param output The output to create a rendering surface for. 146 * \param options The options struct describing output configuration 147 * \return 0 on success, -1 on failure. 148 * 149 * This function creates the renderer data structures needed to repaint 150 * the output. The repaint results will be directed to the given native 151 * window. 152 * 153 * If EGL_EXT_platform_base is supported then \c window_for_platform is 154 * used, otherwise \c window_for_legacy is used. This is because the 155 * handle on X11 platform is different between the two. 156 * 157 * The first format in drm_formats that matches any EGLConfig 158 * determines which EGLConfig is chosen. See \c display_create about 159 * how the matching works and the possible limitations. 160 * 161 * This function should be used only if \c display_create was called 162 * with \c EGL_WINDOW_BIT in \c egl_surface_type. 163 */ 164 int (*output_window_create)(struct weston_output *output, 165 const struct gl_renderer_output_options *options); 166 167 /** 168 * Attach GL-renderer to the output with internal pixel storage 169 * 170 * \param output The output to create a rendering surface for. 171 * \param options The options struct describing the pbuffer 172 * \return 0 on success, -1 on failure. 173 * 174 * This function creates the renderer data structures needed to repaint 175 * the output. The repaint results will be kept internal and can only 176 * be accessed through e.g. screen capture. 177 * 178 * The first format in drm_formats that matches any EGLConfig 179 * determines which EGLConfig is chosen. See \c display_create about 180 * how the matching works and the possible limitations. 181 * 182 * This function should be used only if \c display_create was called 183 * with \c EGL_PBUFFER_BIT in \c egl_surface_type. 184 */ 185 int (*output_pbuffer_create)(struct weston_output *output, 186 const struct gl_renderer_pbuffer_options *options); 187 188 void (*output_destroy)(struct weston_output *output); 189 190 /* Sets the output border. 191 * 192 * The side specifies the side for which we are setting the border. 193 * The width and height are the width and height of the border. 194 * The tex_width patemeter specifies the width of the actual 195 * texture; this may be larger than width if the data is not 196 * tightly packed. 197 * 198 * The top and bottom textures will extend over the sides to the 199 * full width of the bordered window. The right and left edges, 200 * however, will extend only to the top and bottom of the 201 * compositor surface. This is demonstrated by the picture below: 202 * 203 * +-----------------------+ 204 * | TOP | 205 * +-+-------------------+-+ 206 * | | | | 207 * |L| |R| 208 * |E| |I| 209 * |F| |G| 210 * |T| |H| 211 * | | |T| 212 * | | | | 213 * +-+-------------------+-+ 214 * | BOTTOM | 215 * +-----------------------+ 216 */ 217 void (*output_set_border)(struct weston_output *output, 218 enum gl_renderer_border_side side, 219 int32_t width, int32_t height, 220 int32_t tex_width, unsigned char *data); 221 222 /* Create fence sync FD to wait for GPU rendering. 223 * 224 * Return FD on success, -1 on failure or unsupported 225 * EGL_ANDROID_native_fence_sync extension. 226 */ 227 int (*create_fence_fd)(struct weston_output *output); 228 }; 229