Path: blob/21.2-virgl/src/gallium/winsys/svga/drm/vmw_surface.c
4573 views
/**********************************************************1* Copyright 2009-2015 VMware, Inc. All rights reserved.2*3* Permission is hereby granted, free of charge, to any person4* obtaining a copy of this software and associated documentation5* files (the "Software"), to deal in the Software without6* restriction, including without limitation the rights to use, copy,7* modify, merge, publish, distribute, sublicense, and/or sell copies8* of the Software, and to permit persons to whom the Software is9* furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*23**********************************************************/242526#include "svga_cmd.h"27#include "util/u_debug.h"28#include "util/u_memory.h"29#include "pipe/p_defines.h"30#include "vmw_surface.h"31#include "vmw_screen.h"32#include "vmw_buffer.h"33#include "vmw_context.h"34#include "pipebuffer/pb_bufmgr.h"3536void37vmw_svga_winsys_surface_init(struct svga_winsys_screen *sws,38struct svga_winsys_surface *srf,39unsigned surf_size, SVGA3dSurfaceAllFlags flags)40{41struct vmw_svga_winsys_surface *vsrf = vmw_svga_winsys_surface(srf);42void *data = NULL;43struct pb_buffer *pb_buf;44uint32_t pb_flags;45struct vmw_winsys_screen *vws = vsrf->screen;46pb_flags = PIPE_MAP_WRITE | PIPE_MAP_DISCARD_WHOLE_RESOURCE;4748struct pb_manager *provider;49struct pb_desc desc;5051mtx_lock(&vsrf->mutex);52data = vmw_svga_winsys_buffer_map(&vws->base, vsrf->buf, pb_flags);53if (data)54goto out_mapped;5556provider = vws->pools.mob_fenced;57memset(&desc, 0, sizeof(desc));58desc.alignment = 4096;59pb_buf = provider->create_buffer(provider, vsrf->size, &desc);60if (pb_buf != NULL) {61struct svga_winsys_buffer *vbuf =62vmw_svga_winsys_buffer_wrap(pb_buf);6364data = vmw_svga_winsys_buffer_map(&vws->base, vbuf, pb_flags);65if (data) {66vsrf->rebind = TRUE;67if (vsrf->buf)68vmw_svga_winsys_buffer_destroy(&vws->base, vsrf->buf);69vsrf->buf = vbuf;70goto out_mapped;71} else {72vmw_svga_winsys_buffer_destroy(&vws->base, vbuf);73goto out_unlock;74}75}76else {77/* Cannot create a buffer, just unlock */78goto out_unlock;79}8081out_mapped:82mtx_unlock(&vsrf->mutex);8384if (data) {85if (flags & SVGA3D_SURFACE_BIND_STREAM_OUTPUT) {86memset(data, 0, surf_size + sizeof(SVGA3dDXSOState));87}88else {89memset(data, 0, surf_size);90}91}9293mtx_lock(&vsrf->mutex);94vmw_svga_winsys_buffer_unmap(&vsrf->screen->base, vsrf->buf);95out_unlock:96mtx_unlock(&vsrf->mutex);97}9899100101void *102vmw_svga_winsys_surface_map(struct svga_winsys_context *swc,103struct svga_winsys_surface *srf,104unsigned flags, boolean *retry,105boolean *rebind)106{107struct vmw_svga_winsys_surface *vsrf = vmw_svga_winsys_surface(srf);108void *data = NULL;109struct pb_buffer *pb_buf;110uint32_t pb_flags;111struct vmw_winsys_screen *vws = vsrf->screen;112113*retry = FALSE;114*rebind = FALSE;115assert((flags & (PIPE_MAP_READ | PIPE_MAP_WRITE)) != 0);116mtx_lock(&vsrf->mutex);117118if (vsrf->mapcount) {119/* Other mappers will get confused if we discard. */120flags &= ~PIPE_MAP_DISCARD_WHOLE_RESOURCE;121}122123vsrf->rebind = FALSE;124125/*126* If we intend to read, there's no point discarding the127* data if busy.128*/129if (flags & PIPE_MAP_READ || vsrf->shared)130flags &= ~PIPE_MAP_DISCARD_WHOLE_RESOURCE;131132/*133* Discard is a hint to a synchronized map.134*/135if (flags & PIPE_MAP_DISCARD_WHOLE_RESOURCE)136flags &= ~PIPE_MAP_UNSYNCHRONIZED;137138/*139* The surface is allowed to be referenced on the command stream iff140* we're mapping unsynchronized or discard. This is an early check.141* We need to recheck after a failing discard map.142*/143if (!(flags & (PIPE_MAP_DISCARD_WHOLE_RESOURCE |144PIPE_MAP_UNSYNCHRONIZED)) &&145p_atomic_read(&vsrf->validated)) {146*retry = TRUE;147goto out_unlock;148}149150pb_flags = flags & (PIPE_MAP_READ_WRITE | PIPE_MAP_UNSYNCHRONIZED |151PIPE_MAP_PERSISTENT);152153if (flags & PIPE_MAP_DISCARD_WHOLE_RESOURCE) {154struct pb_manager *provider;155struct pb_desc desc;156157/*158* First, if possible, try to map existing storage with DONTBLOCK.159*/160if (!p_atomic_read(&vsrf->validated)) {161data = vmw_svga_winsys_buffer_map(&vws->base, vsrf->buf,162PIPE_MAP_DONTBLOCK | pb_flags);163if (data)164goto out_mapped;165}166167/*168* Attempt to get a new buffer.169*/170provider = vws->pools.mob_fenced;171memset(&desc, 0, sizeof(desc));172desc.alignment = 4096;173pb_buf = provider->create_buffer(provider, vsrf->size, &desc);174if (pb_buf != NULL) {175struct svga_winsys_buffer *vbuf =176vmw_svga_winsys_buffer_wrap(pb_buf);177178data = vmw_svga_winsys_buffer_map(&vws->base, vbuf, pb_flags);179if (data) {180vsrf->rebind = TRUE;181/*182* We've discarded data on this surface and thus183* it's data is no longer consider referenced.184*/185vmw_swc_surface_clear_reference(swc, vsrf);186if (vsrf->buf)187vmw_svga_winsys_buffer_destroy(&vws->base, vsrf->buf);188vsrf->buf = vbuf;189190/* Rebind persistent maps immediately */191if (flags & PIPE_MAP_PERSISTENT) {192*rebind = TRUE;193vsrf->rebind = FALSE;194}195goto out_mapped;196} else197vmw_svga_winsys_buffer_destroy(&vws->base, vbuf);198}199/*200* We couldn't get and map a new buffer for some reason.201* Fall through to an ordinary map.202* But tell pipe driver to flush now if already on validate list,203* Otherwise we'll overwrite previous contents.204*/205if (!(flags & PIPE_MAP_UNSYNCHRONIZED) &&206p_atomic_read(&vsrf->validated)) {207*retry = TRUE;208goto out_unlock;209}210}211212pb_flags |= (flags & PIPE_MAP_DONTBLOCK);213data = vmw_svga_winsys_buffer_map(&vws->base, vsrf->buf, pb_flags);214if (data == NULL)215goto out_unlock;216217out_mapped:218++vsrf->mapcount;219vsrf->data = data;220vsrf->map_mode = flags & (PIPE_MAP_READ | PIPE_MAP_WRITE);221out_unlock:222mtx_unlock(&vsrf->mutex);223return data;224}225226227void228vmw_svga_winsys_surface_unmap(struct svga_winsys_context *swc,229struct svga_winsys_surface *srf,230boolean *rebind)231{232struct vmw_svga_winsys_surface *vsrf = vmw_svga_winsys_surface(srf);233mtx_lock(&vsrf->mutex);234if (--vsrf->mapcount == 0) {235*rebind = vsrf->rebind;236vsrf->rebind = FALSE;237} else {238*rebind = FALSE;239}240vmw_svga_winsys_buffer_unmap(&vsrf->screen->base, vsrf->buf);241mtx_unlock(&vsrf->mutex);242}243244void245vmw_svga_winsys_surface_reference(struct vmw_svga_winsys_surface **pdst,246struct vmw_svga_winsys_surface *src)247{248struct pipe_reference *src_ref;249struct pipe_reference *dst_ref;250struct vmw_svga_winsys_surface *dst;251252if(pdst == NULL || *pdst == src)253return;254255dst = *pdst;256257src_ref = src ? &src->refcnt : NULL;258dst_ref = dst ? &dst->refcnt : NULL;259260if (pipe_reference(dst_ref, src_ref)) {261if (dst->buf)262vmw_svga_winsys_buffer_destroy(&dst->screen->base, dst->buf);263vmw_ioctl_surface_destroy(dst->screen, dst->sid);264#ifdef DEBUG265/* to detect dangling pointers */266assert(p_atomic_read(&dst->validated) == 0);267dst->sid = SVGA3D_INVALID_ID;268#endif269mtx_destroy(&dst->mutex);270FREE(dst);271}272273*pdst = src;274}275276277