primary_plane.c (5917B)
1 /* swc: primary_plane.c 2 * 3 * Copyright (c) 2013, 2014, 2016 Michael Forney 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a copy 6 * of this software and associated documentation files (the "Software"), to deal 7 * in the Software without restriction, including without limitation the rights 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 * copies of the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in 13 * all copies or substantial portions of the 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 THE 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 * SOFTWARE. 22 */ 23 24 #include "primary_plane.h" 25 #include "compositor.h" 26 #include "event.h" 27 #ifdef ENABLE_DRM 28 #include "drm.h" 29 #endif 30 #ifdef ENABLE_FB 31 #include "fb.h" 32 #endif 33 #include "internal.h" 34 #include "launch.h" 35 #include "util.h" 36 37 #ifdef ENABLE_DRM 38 #include <errno.h> 39 #include <wld/drm.h> 40 #include <xf86drm.h> 41 #include <xf86drmMode.h> 42 #endif 43 #include <wld/wld.h> 44 45 static bool 46 update(struct view *view) 47 { 48 return true; 49 } 50 51 static void 52 send_frame(void *data) 53 { 54 struct primary_plane *plane = data; 55 56 view_frame(&plane->view, get_time()); 57 } 58 59 #ifdef ENABLE_DRM 60 static int 61 attach_drm(struct primary_plane *plane, struct wld_buffer *buffer) 62 { 63 uint32_t fb; 64 int ret; 65 66 fb = drm_get_framebuffer(buffer); 67 if (plane->need_modeset) { 68 ret = drmModeSetCrtc(swc.drm->fd, plane->crtc, fb, 0, 0, 69 plane->connectors.data, plane->connectors.size / 4, 70 &plane->mode.info); 71 72 if (ret == 0) { 73 wl_event_loop_add_idle(swc.event_loop, &send_frame, plane); 74 plane->need_modeset = false; 75 } else { 76 ERROR("Could not set CRTC to next framebuffer: %s\n", 77 strerror(-ret)); 78 return ret; 79 } 80 } else { 81 ret = drmModePageFlip(swc.drm->fd, plane->crtc, fb, 82 DRM_MODE_PAGE_FLIP_EVENT, &plane->drm_handler); 83 84 if (ret < 0) { 85 ERROR("Page flip failed: %s\n", strerror(errno)); 86 return ret; 87 } 88 } 89 90 return 0; 91 } 92 #endif 93 94 #ifdef ENABLE_FB 95 static int 96 attach_fb(struct primary_plane *plane, struct wld_buffer *buffer) 97 { 98 if (!fb_present(buffer, plane->view.geometry.x, plane->view.geometry.y, 99 compositor_frame_damage())) { 100 return -1; 101 } 102 wl_event_loop_add_idle(swc.event_loop, &send_frame, plane); 103 return 0; 104 } 105 #endif 106 107 static int 108 attach(struct view *view, struct wld_buffer *buffer) 109 { 110 struct primary_plane *plane = wl_container_of(view, plane, view); 111 112 #ifdef ENABLE_DRM 113 if (plane->drm) { 114 return attach_drm(plane, buffer); 115 } 116 #endif 117 #ifdef ENABLE_FB 118 return attach_fb(plane, buffer); 119 #else 120 return -1; 121 #endif 122 } 123 124 static bool 125 move(struct view *view, int32_t x, int32_t y) 126 { 127 view_set_position(view, x, y); 128 return true; 129 } 130 131 static const struct view_impl view_impl = { 132 .update = update, 133 .attach = attach, 134 .move = move, 135 }; 136 137 #ifdef ENABLE_DRM 138 static void 139 handle_page_flip(struct drm_handler *handler, uint32_t time) 140 { 141 struct primary_plane *plane = wl_container_of(handler, plane, drm_handler); 142 view_frame(&plane->view, time); 143 } 144 #endif 145 146 static void 147 handle_swc_event(struct wl_listener *listener, void *data) 148 { 149 struct event *event = data; 150 struct primary_plane *plane = 151 wl_container_of(listener, plane, swc_listener); 152 153 switch (event->type) { 154 case SWC_EVENT_ACTIVATED: 155 #ifdef ENABLE_DRM 156 if (plane->drm) { 157 plane->need_modeset = true; 158 break; 159 } 160 #endif 161 compositor_damage_all(); 162 break; 163 } 164 } 165 166 static void 167 initialize_common(struct primary_plane *plane, struct mode *mode) 168 { 169 view_initialize(&plane->view, &view_impl); 170 plane->view.geometry.width = mode->width; 171 plane->view.geometry.height = mode->height; 172 plane->mode = *mode; 173 plane->swc_listener.notify = &handle_swc_event; 174 wl_signal_add(&swc.event_signal, &plane->swc_listener); 175 } 176 177 #ifdef ENABLE_DRM 178 bool 179 primary_plane_initialize_drm(struct primary_plane *plane, uint32_t crtc, 180 struct mode *mode, uint32_t *connectors, 181 uint32_t num_connectors) 182 { 183 uint32_t *plane_connectors; 184 185 if (!(plane->original_crtc_state = drmModeGetCrtc(swc.drm->fd, crtc))) { 186 ERROR("Failed to get CRTC state for CRTC %u: %s\n", crtc, 187 strerror(errno)); 188 goto error0; 189 } 190 191 wl_array_init(&plane->connectors); 192 plane_connectors = wl_array_add(&plane->connectors, 193 num_connectors * sizeof(connectors[0])); 194 195 if (!plane_connectors) { 196 ERROR("Failed to allocate connector array\n"); 197 goto error1; 198 } 199 200 memcpy(plane_connectors, connectors, 201 num_connectors * sizeof(connectors[0])); 202 plane->drm = true; 203 plane->crtc = crtc; 204 plane->need_modeset = true; 205 plane->drm_handler.page_flip = &handle_page_flip; 206 initialize_common(plane, mode); 207 208 return true; 209 210 error1: 211 drmModeFreeCrtc(plane->original_crtc_state); 212 error0: 213 return false; 214 } 215 #endif 216 217 #ifdef ENABLE_FB 218 bool 219 primary_plane_initialize_fb(struct primary_plane *plane, struct mode *mode) 220 { 221 #ifdef ENABLE_DRM 222 plane->drm = false; 223 #endif 224 initialize_common(plane, mode); 225 return true; 226 } 227 #endif 228 229 void 230 primary_plane_finalize(struct primary_plane *plane) 231 { 232 wl_list_remove(&plane->swc_listener.link); 233 #ifdef ENABLE_DRM 234 if (plane->drm) { 235 wl_array_release(&plane->connectors); 236 drmModeCrtcPtr crtc = plane->original_crtc_state; 237 drmModeSetCrtc(swc.drm->fd, crtc->crtc_id, crtc->buffer_id, crtc->x, 238 crtc->y, NULL, 0, &crtc->mode); 239 drmModeFreeCrtc(crtc); 240 } 241 #else 242 (void)plane; 243 #endif 244 }