native_shm.c revision 23aa978a9d76a48f4b93e9a8911ec50c0e5d94ab
1/* 2 * Mesa 3-D graphics library 3 * Version: 7.11 4 * 5 * Copyright (C) 2011 Benjamin Franzke <benjaminfranzke@googlemail.com> 6 * 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: 13 * 14 * The above copyright notice and this permission notice shall be included 15 * in all copies or substantial portions of the Software. 16 * 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. 24 */ 25 26#include "util/u_memory.h" 27#include "util/u_inlines.h" 28 29#include "pipe/p_compiler.h" 30#include "pipe/p_screen.h" 31#include "pipe/p_context.h" 32#include "pipe/p_state.h" 33 34#include "sw/wayland/wayland_sw_winsys.h" 35 36#include "egllog.h" 37 38#include "native_wayland.h" 39 40#include <wayland-client.h> 41#include "wayland-egl-priv.h" 42 43#include <sys/types.h> 44#include <sys/stat.h> 45#include <fcntl.h> 46 47struct wayland_shm_display { 48 struct wayland_display base; 49 50 const struct native_event_handler *event_handler; 51 struct wl_shm *wl_shm; 52}; 53 54static INLINE struct wayland_shm_display * 55wayland_shm_display(const struct native_display *ndpy) 56{ 57 return (struct wayland_shm_display *) ndpy; 58} 59 60 61static void 62wayland_shm_display_destroy(struct native_display *ndpy) 63{ 64 struct wayland_shm_display *shmdpy = wayland_shm_display(ndpy); 65 66 if (shmdpy->base.configs) 67 FREE(shmdpy->base.configs); 68 if (shmdpy->base.own_dpy) 69 wl_display_destroy(shmdpy->base.dpy); 70 71 ndpy_uninit(ndpy); 72 73 FREE(shmdpy); 74} 75 76 77static struct wl_buffer * 78wayland_create_shm_buffer(struct wayland_display *display, 79 struct wayland_surface *surface, 80 enum native_attachment attachment) 81{ 82 struct wayland_shm_display *shmdpy = (struct wayland_shm_display *) display; 83 struct pipe_screen *screen = shmdpy->base.base.screen; 84 struct pipe_resource *resource; 85 struct winsys_handle wsh; 86 uint width, height; 87 uint32_t format; 88 89 resource = resource_surface_get_single_resource(surface->rsurf, attachment); 90 resource_surface_get_size(surface->rsurf, &width, &height); 91 92 screen->resource_get_handle(screen, resource, &wsh); 93 94 pipe_resource_reference(&resource, NULL); 95 96 switch (surface->color_format) { 97 case PIPE_FORMAT_B8G8R8A8_UNORM: 98 format = (surface->premultiplied_alpha) ? 99 WL_SHM_FORMAT_PREMULTIPLIED_ARGB32 : WL_SHM_FORMAT_ARGB32; 100 break; 101 case PIPE_FORMAT_B8G8R8X8_UNORM: 102 format = WL_SHM_FORMAT_XRGB32; 103 break; 104 default: 105 return NULL; 106 break; 107 } 108 109 return wl_shm_create_buffer(shmdpy->wl_shm, wsh.fd, 110 width, height, 111 wsh.stride, format); 112} 113 114static boolean 115wayland_shm_display_add_configs(struct wayland_shm_display *shmdpy) 116{ 117 struct wayland_config *configs; 118 enum pipe_format formats[2]; 119 int i, num_formats = 0; 120 121 /* assume all formats are supported */ 122 formats[num_formats++] = PIPE_FORMAT_B8G8R8A8_UNORM; 123 formats[num_formats++] = PIPE_FORMAT_B8G8R8X8_UNORM; 124 125 configs = CALLOC(num_formats, sizeof(*configs)); 126 if (!configs) 127 return FALSE; 128 129 for (i = 0; i < num_formats; i++) { 130 struct native_config *nconf = &configs[i].base; 131 132 nconf->buffer_mask = 133 (1 << NATIVE_ATTACHMENT_FRONT_LEFT) | 134 (1 << NATIVE_ATTACHMENT_BACK_LEFT); 135 136 nconf->color_format = formats[i]; 137 138 nconf->window_bit = TRUE; 139 nconf->pixmap_bit = TRUE; 140 } 141 142 shmdpy->base.configs = configs; 143 shmdpy->base.num_configs = num_formats; 144 145 return TRUE; 146} 147 148static boolean 149wayland_shm_display_init_screen(struct native_display *ndpy) 150{ 151 struct wayland_shm_display *shmdpy = wayland_shm_display(ndpy); 152 struct sw_winsys *winsys = NULL; 153 uint32_t id; 154 155 id = wl_display_get_global(shmdpy->base.dpy, "wl_shm", 1); 156 if (id == 0) 157 wl_display_iterate(shmdpy->base.dpy, WL_DISPLAY_READABLE); 158 id = wl_display_get_global(shmdpy->base.dpy, "wl_shm", 1); 159 if (id == 0) 160 return FALSE; 161 162 shmdpy->wl_shm = wl_display_bind(shmdpy->base.dpy, id, &wl_shm_interface); 163 if (!shmdpy->wl_shm) 164 return FALSE; 165 166 if (!wayland_shm_display_add_configs(shmdpy)) 167 return FALSE; 168 169 /* assume all formats are supported */ 170 shmdpy->base.param_premultiplied_alpha = TRUE; 171 172 winsys = wayland_create_sw_winsys(shmdpy->base.dpy); 173 if (!winsys) 174 return FALSE; 175 176 shmdpy->base.base.screen = 177 shmdpy->event_handler->new_sw_screen(&shmdpy->base.base, winsys); 178 179 if (!shmdpy->base.base.screen) { 180 _eglLog(_EGL_WARNING, "failed to create shm screen"); 181 return FALSE; 182 } 183 184 return TRUE; 185} 186 187struct wayland_display * 188wayland_create_shm_display(struct wl_display *dpy, 189 const struct native_event_handler *event_handler) 190{ 191 struct wayland_shm_display *shmdpy; 192 193 shmdpy = CALLOC_STRUCT(wayland_shm_display); 194 if (!shmdpy) 195 return NULL; 196 197 shmdpy->event_handler = event_handler; 198 199 shmdpy->base.dpy = dpy; 200 if (!shmdpy->base.dpy) { 201 wayland_shm_display_destroy(&shmdpy->base.base); 202 return NULL; 203 } 204 205 shmdpy->base.base.init_screen = wayland_shm_display_init_screen; 206 shmdpy->base.base.destroy = wayland_shm_display_destroy; 207 shmdpy->base.create_buffer = wayland_create_shm_buffer; 208 209 return &shmdpy->base; 210} 211 212/* vim: set sw=3 ts=8 sts=3 expandtab: */ 213