Path: blob/21.2-virgl/src/gallium/winsys/sw/kms-dri/kms_dri_sw_winsys.c
4573 views
/**************************************************************************1*2* Copyright 2009, VMware, Inc.3* All Rights Reserved.4* Copyright 2010 George Sapountzis <[email protected]>5* 2013 Red Hat, Inc.6*7* Permission is hereby granted, free of charge, to any person obtaining a8* copy of this software and associated documentation files (the9* "Software"), to deal in the Software without restriction, including10* without limitation the rights to use, copy, modify, merge, publish,11* distribute, sub license, and/or sell copies of the Software, and to12* permit persons to whom the Software is furnished to do so, subject to13* the following conditions:14*15* The above copyright notice and this permission notice (including the16* next paragraph) shall be included in all copies or substantial portions17* of the Software.18*19* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS20* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF21* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.22* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR23* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,24* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE25* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.26*27**************************************************************************/2829#include <stdio.h>30#include <stdlib.h>31#include <stddef.h>32#include <stdint.h>33#include <string.h>34#include <limits.h>3536#include <sys/types.h>37#include <sys/mman.h>38#include <unistd.h>39#include <dlfcn.h>40#include <fcntl.h>41#include <xf86drm.h>4243#include "pipe/p_compiler.h"44#include "pipe/p_format.h"45#include "pipe/p_state.h"46#include "util/u_inlines.h"47#include "util/format/u_format.h"48#include "util/u_math.h"49#include "util/u_memory.h"50#include "util/list.h"5152#include "frontend/sw_winsys.h"53#include "frontend/drm_driver.h"54#include "kms_dri_sw_winsys.h"5556#ifdef DEBUG57#define DEBUG_PRINT(msg, ...) fprintf(stderr, msg, __VA_ARGS__)58#else59#define DEBUG_PRINT(msg, ...)60#endif6162struct kms_sw_displaytarget;6364struct kms_sw_plane65{66unsigned width;67unsigned height;68unsigned stride;69unsigned offset;70struct kms_sw_displaytarget *dt;71struct list_head link;72};7374struct kms_sw_displaytarget75{76enum pipe_format format;77unsigned size;7879uint32_t handle;80void *mapped;81void *ro_mapped;8283int ref_count;84int map_count;85struct list_head link;86struct list_head planes;87};8889struct kms_sw_winsys90{91struct sw_winsys base;9293int fd;94struct list_head bo_list;95};9697static inline struct kms_sw_plane *98kms_sw_plane( struct sw_displaytarget *dt )99{100return (struct kms_sw_plane *)dt;101}102103static inline struct sw_displaytarget *104sw_displaytarget( struct kms_sw_plane *pl)105{106return (struct sw_displaytarget *)pl;107}108109static inline struct kms_sw_winsys *110kms_sw_winsys( struct sw_winsys *ws )111{112return (struct kms_sw_winsys *)ws;113}114115116static bool117kms_sw_is_displaytarget_format_supported( struct sw_winsys *ws,118unsigned tex_usage,119enum pipe_format format )120{121/* TODO: check visuals or other sensible thing here */122return true;123}124125static struct kms_sw_plane *get_plane(struct kms_sw_displaytarget *kms_sw_dt,126enum pipe_format format,127unsigned width, unsigned height,128unsigned stride, unsigned offset)129{130struct kms_sw_plane *plane = NULL;131132if (offset + util_format_get_2d_size(format, stride, height) >133kms_sw_dt->size) {134DEBUG_PRINT("KMS-DEBUG: plane too big. format: %d stride: %d height: %d "135"offset: %d size:%d\n", format, stride, height, offset,136kms_sw_dt->size);137return NULL;138}139140LIST_FOR_EACH_ENTRY(plane, &kms_sw_dt->planes, link) {141if (plane->offset == offset)142return plane;143}144145plane = CALLOC_STRUCT(kms_sw_plane);146if (!plane)147return NULL;148149plane->width = width;150plane->height = height;151plane->stride = stride;152plane->offset = offset;153plane->dt = kms_sw_dt;154list_add(&plane->link, &kms_sw_dt->planes);155return plane;156}157158static struct sw_displaytarget *159kms_sw_displaytarget_create(struct sw_winsys *ws,160unsigned tex_usage,161enum pipe_format format,162unsigned width, unsigned height,163unsigned alignment,164const void *front_private,165unsigned *stride)166{167struct kms_sw_winsys *kms_sw = kms_sw_winsys(ws);168struct kms_sw_displaytarget *kms_sw_dt;169struct drm_mode_create_dumb create_req;170struct drm_mode_destroy_dumb destroy_req;171int ret;172173kms_sw_dt = CALLOC_STRUCT(kms_sw_displaytarget);174if (!kms_sw_dt)175goto no_dt;176177list_inithead(&kms_sw_dt->planes);178kms_sw_dt->ref_count = 1;179kms_sw_dt->mapped = MAP_FAILED;180kms_sw_dt->ro_mapped = MAP_FAILED;181182kms_sw_dt->format = format;183184memset(&create_req, 0, sizeof(create_req));185create_req.bpp = util_format_get_blocksizebits(format);186create_req.width = width;187create_req.height = height;188ret = drmIoctl(kms_sw->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_req);189if (ret)190goto free_bo;191192kms_sw_dt->size = create_req.size;193kms_sw_dt->handle = create_req.handle;194struct kms_sw_plane *plane = get_plane(kms_sw_dt, format, width, height,195create_req.pitch, 0);196if (!plane)197goto free_bo;198199list_add(&kms_sw_dt->link, &kms_sw->bo_list);200201DEBUG_PRINT("KMS-DEBUG: created buffer %u (size %u)\n", kms_sw_dt->handle, kms_sw_dt->size);202203*stride = create_req.pitch;204return sw_displaytarget(plane);205206free_bo:207memset(&destroy_req, 0, sizeof destroy_req);208destroy_req.handle = create_req.handle;209drmIoctl(kms_sw->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_req);210FREE(kms_sw_dt);211no_dt:212return NULL;213}214215static void216kms_sw_displaytarget_destroy(struct sw_winsys *ws,217struct sw_displaytarget *dt)218{219struct kms_sw_winsys *kms_sw = kms_sw_winsys(ws);220struct kms_sw_plane *plane = kms_sw_plane(dt);221struct kms_sw_displaytarget *kms_sw_dt = plane->dt;222struct drm_mode_destroy_dumb destroy_req;223224kms_sw_dt->ref_count --;225if (kms_sw_dt->ref_count > 0)226return;227228if (kms_sw_dt->map_count > 0) {229DEBUG_PRINT("KMS-DEBUG: leaked map buffer %u\n", kms_sw_dt->handle);230}231232memset(&destroy_req, 0, sizeof destroy_req);233destroy_req.handle = kms_sw_dt->handle;234drmIoctl(kms_sw->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_req);235236list_del(&kms_sw_dt->link);237238DEBUG_PRINT("KMS-DEBUG: destroyed buffer %u\n", kms_sw_dt->handle);239240struct kms_sw_plane *tmp;241LIST_FOR_EACH_ENTRY_SAFE(plane, tmp, &kms_sw_dt->planes, link) {242FREE(plane);243}244245FREE(kms_sw_dt);246}247248static void *249kms_sw_displaytarget_map(struct sw_winsys *ws,250struct sw_displaytarget *dt,251unsigned flags)252{253struct kms_sw_winsys *kms_sw = kms_sw_winsys(ws);254struct kms_sw_plane *plane = kms_sw_plane(dt);255struct kms_sw_displaytarget *kms_sw_dt = plane->dt;256struct drm_mode_map_dumb map_req;257int prot, ret;258259memset(&map_req, 0, sizeof map_req);260map_req.handle = kms_sw_dt->handle;261ret = drmIoctl(kms_sw->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_req);262if (ret)263return NULL;264265prot = (flags == PIPE_MAP_READ) ? PROT_READ : (PROT_READ | PROT_WRITE);266void **ptr = (flags == PIPE_MAP_READ) ? &kms_sw_dt->ro_mapped : &kms_sw_dt->mapped;267if (*ptr == MAP_FAILED) {268void *tmp = mmap(0, kms_sw_dt->size, prot, MAP_SHARED,269kms_sw->fd, map_req.offset);270if (tmp == MAP_FAILED)271return NULL;272*ptr = tmp;273}274275DEBUG_PRINT("KMS-DEBUG: mapped buffer %u (size %u) at %p\n",276kms_sw_dt->handle, kms_sw_dt->size, *ptr);277278kms_sw_dt->map_count++;279280return *ptr + plane->offset;281}282283static struct kms_sw_displaytarget *284kms_sw_displaytarget_find_and_ref(struct kms_sw_winsys *kms_sw,285unsigned int kms_handle)286{287struct kms_sw_displaytarget *kms_sw_dt;288289LIST_FOR_EACH_ENTRY(kms_sw_dt, &kms_sw->bo_list, link) {290if (kms_sw_dt->handle == kms_handle) {291kms_sw_dt->ref_count++;292293DEBUG_PRINT("KMS-DEBUG: imported buffer %u (size %u)\n",294kms_sw_dt->handle, kms_sw_dt->size);295296return kms_sw_dt;297}298}299300return NULL;301}302303static struct kms_sw_plane *304kms_sw_displaytarget_add_from_prime(struct kms_sw_winsys *kms_sw, int fd,305enum pipe_format format,306unsigned width, unsigned height,307unsigned stride, unsigned offset)308{309uint32_t handle = -1;310struct kms_sw_displaytarget * kms_sw_dt;311int ret;312313ret = drmPrimeFDToHandle(kms_sw->fd, fd, &handle);314315if (ret)316return NULL;317318kms_sw_dt = kms_sw_displaytarget_find_and_ref(kms_sw, handle);319struct kms_sw_plane *plane = NULL;320if (kms_sw_dt) {321plane = get_plane(kms_sw_dt, format, width, height, stride, offset);322if (!plane)323kms_sw_dt->ref_count --;324return plane;325}326327kms_sw_dt = CALLOC_STRUCT(kms_sw_displaytarget);328if (!kms_sw_dt)329return NULL;330331list_inithead(&kms_sw_dt->planes);332off_t lseek_ret = lseek(fd, 0, SEEK_END);333if (lseek_ret == -1) {334FREE(kms_sw_dt);335return NULL;336}337kms_sw_dt->mapped = MAP_FAILED;338kms_sw_dt->ro_mapped = MAP_FAILED;339kms_sw_dt->size = lseek_ret;340kms_sw_dt->ref_count = 1;341kms_sw_dt->handle = handle;342343lseek(fd, 0, SEEK_SET);344plane = get_plane(kms_sw_dt, format, width, height, stride, offset);345if (!plane) {346FREE(kms_sw_dt);347return NULL;348}349350list_add(&kms_sw_dt->link, &kms_sw->bo_list);351352return plane;353}354355static void356kms_sw_displaytarget_unmap(struct sw_winsys *ws,357struct sw_displaytarget *dt)358{359struct kms_sw_plane *plane = kms_sw_plane(dt);360struct kms_sw_displaytarget *kms_sw_dt = plane->dt;361362if (!kms_sw_dt->map_count) {363DEBUG_PRINT("KMS-DEBUG: ignore duplicated unmap %u", kms_sw_dt->handle);364return;365}366kms_sw_dt->map_count--;367if (kms_sw_dt->map_count) {368DEBUG_PRINT("KMS-DEBUG: ignore unmap for busy buffer %u", kms_sw_dt->handle);369return;370}371372DEBUG_PRINT("KMS-DEBUG: unmapped buffer %u (was %p)\n", kms_sw_dt->handle, kms_sw_dt->mapped);373DEBUG_PRINT("KMS-DEBUG: unmapped buffer %u (was %p)\n", kms_sw_dt->handle, kms_sw_dt->ro_mapped);374375if (kms_sw_dt->mapped != MAP_FAILED) {376munmap(kms_sw_dt->mapped, kms_sw_dt->size);377kms_sw_dt->mapped = MAP_FAILED;378}379if (kms_sw_dt->ro_mapped != MAP_FAILED) {380munmap(kms_sw_dt->ro_mapped, kms_sw_dt->size);381kms_sw_dt->ro_mapped = MAP_FAILED;382}383}384385static struct sw_displaytarget *386kms_sw_displaytarget_from_handle(struct sw_winsys *ws,387const struct pipe_resource *templ,388struct winsys_handle *whandle,389unsigned *stride)390{391struct kms_sw_winsys *kms_sw = kms_sw_winsys(ws);392struct kms_sw_displaytarget *kms_sw_dt;393struct kms_sw_plane *kms_sw_pl;394395396assert(whandle->type == WINSYS_HANDLE_TYPE_KMS ||397whandle->type == WINSYS_HANDLE_TYPE_FD);398399switch(whandle->type) {400case WINSYS_HANDLE_TYPE_FD:401kms_sw_pl = kms_sw_displaytarget_add_from_prime(kms_sw, whandle->handle,402templ->format,403templ->width0,404templ->height0,405whandle->stride,406whandle->offset);407if (kms_sw_pl)408*stride = kms_sw_pl->stride;409return sw_displaytarget(kms_sw_pl);410case WINSYS_HANDLE_TYPE_KMS:411kms_sw_dt = kms_sw_displaytarget_find_and_ref(kms_sw, whandle->handle);412if (kms_sw_dt) {413struct kms_sw_plane *plane;414LIST_FOR_EACH_ENTRY(plane, &kms_sw_dt->planes, link) {415if (whandle->offset == plane->offset) {416*stride = plane->stride;417return sw_displaytarget(plane);418}419}420kms_sw_dt->ref_count --;421}422FALLTHROUGH;423default:424break;425}426427assert(0);428return NULL;429}430431static bool432kms_sw_displaytarget_get_handle(struct sw_winsys *winsys,433struct sw_displaytarget *dt,434struct winsys_handle *whandle)435{436struct kms_sw_winsys *kms_sw = kms_sw_winsys(winsys);437struct kms_sw_plane *plane = kms_sw_plane(dt);438struct kms_sw_displaytarget *kms_sw_dt = plane->dt;439440switch(whandle->type) {441case WINSYS_HANDLE_TYPE_KMS:442whandle->handle = kms_sw_dt->handle;443whandle->stride = plane->stride;444whandle->offset = plane->offset;445return true;446case WINSYS_HANDLE_TYPE_FD:447if (!drmPrimeHandleToFD(kms_sw->fd, kms_sw_dt->handle,448DRM_CLOEXEC, (int*)&whandle->handle)) {449whandle->stride = plane->stride;450whandle->offset = plane->offset;451return true;452}453FALLTHROUGH;454default:455whandle->handle = 0;456whandle->stride = 0;457whandle->offset = 0;458return false;459}460}461462static void463kms_sw_displaytarget_display(struct sw_winsys *ws,464struct sw_displaytarget *dt,465void *context_private,466struct pipe_box *box)467{468/* This function should not be called, instead the dri2 loader should469handle swap buffers internally.470*/471assert(0);472}473474475static void476kms_destroy_sw_winsys(struct sw_winsys *winsys)477{478FREE(winsys);479}480481struct sw_winsys *482kms_dri_create_winsys(int fd)483{484struct kms_sw_winsys *ws;485486ws = CALLOC_STRUCT(kms_sw_winsys);487if (!ws)488return NULL;489490ws->fd = fd;491list_inithead(&ws->bo_list);492493ws->base.destroy = kms_destroy_sw_winsys;494495ws->base.is_displaytarget_format_supported = kms_sw_is_displaytarget_format_supported;496497/* screen texture functions */498ws->base.displaytarget_create = kms_sw_displaytarget_create;499ws->base.displaytarget_destroy = kms_sw_displaytarget_destroy;500ws->base.displaytarget_from_handle = kms_sw_displaytarget_from_handle;501ws->base.displaytarget_get_handle = kms_sw_displaytarget_get_handle;502503/* texture functions */504ws->base.displaytarget_map = kms_sw_displaytarget_map;505ws->base.displaytarget_unmap = kms_sw_displaytarget_unmap;506507ws->base.displaytarget_display = kms_sw_displaytarget_display;508509return &ws->base;510}511512/* vim: set sw=3 ts=8 sts=3 expandtab: */513514515