Path: blob/21.2-virgl/src/gallium/drivers/i915/i915_resource_buffer.c
4570 views
/**************************************************************************1*2* Copyright 2006 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/26/*27* Authors:28* Keith Whitwell <[email protected]>29* Michel Dänzer <[email protected]>30*/3132#include "pipe/p_context.h"33#include "pipe/p_defines.h"34#include "util/u_inlines.h"35#include "util/u_math.h"36#include "util/u_memory.h"3738#include "i915_context.h"39#include "i915_resource.h"40#include "i915_screen.h"4142void43i915_resource_destroy(struct pipe_screen *screen,44struct pipe_resource *resource)45{46if (resource->target == PIPE_BUFFER) {47struct i915_buffer *buffer = i915_buffer(resource);48if (buffer->free_on_destroy)49align_free(buffer->data);50FREE(buffer);51} else {52struct i915_texture *tex = i915_texture(resource);53struct i915_winsys *iws = i915_screen(screen)->iws;54uint32_t i;5556if (tex->buffer)57iws->buffer_destroy(iws, tex->buffer);5859for (i = 0; i < ARRAY_SIZE(tex->image_offset); i++)60FREE(tex->image_offset[i]);6162FREE(tex);63}64}6566void *67i915_buffer_transfer_map(struct pipe_context *pipe,68struct pipe_resource *resource, unsigned level,69unsigned usage, const struct pipe_box *box,70struct pipe_transfer **ptransfer)71{72struct i915_context *i915 = i915_context(pipe);73struct i915_buffer *buffer = i915_buffer(resource);74struct pipe_transfer *transfer = slab_alloc_st(&i915->transfer_pool);7576if (!transfer)77return NULL;7879transfer->resource = resource;80transfer->level = level;81transfer->usage = usage;82transfer->box = *box;83*ptransfer = transfer;8485return buffer->data + transfer->box.x;86}8788void89i915_buffer_transfer_unmap(struct pipe_context *pipe,90struct pipe_transfer *transfer)91{92struct i915_context *i915 = i915_context(pipe);93slab_free_st(&i915->transfer_pool, transfer);94}9596void97i915_buffer_subdata(struct pipe_context *rm_ctx, struct pipe_resource *resource,98unsigned usage, unsigned offset, unsigned size,99const void *data)100{101struct i915_buffer *buffer = i915_buffer(resource);102103memcpy(buffer->data + offset, data, size);104}105106struct pipe_resource *107i915_buffer_create(struct pipe_screen *screen,108const struct pipe_resource *template)109{110struct i915_buffer *buf = CALLOC_STRUCT(i915_buffer);111112if (!buf)113return NULL;114115buf->b = *template;116pipe_reference_init(&buf->b.reference, 1);117buf->b.screen = screen;118buf->data = align_malloc(template->width0, 64);119buf->free_on_destroy = true;120121if (!buf->data)122goto err;123124return &buf->b;125126err:127FREE(buf);128return NULL;129}130131struct pipe_resource *132i915_user_buffer_create(struct pipe_screen *screen, void *ptr, unsigned bytes,133unsigned bind)134{135struct i915_buffer *buf = CALLOC_STRUCT(i915_buffer);136137if (!buf)138return NULL;139140pipe_reference_init(&buf->b.reference, 1);141buf->b.screen = screen;142buf->b.format = PIPE_FORMAT_R8_UNORM; /* ?? */143buf->b.usage = PIPE_USAGE_IMMUTABLE;144buf->b.bind = bind;145buf->b.flags = 0;146buf->b.width0 = bytes;147buf->b.height0 = 1;148buf->b.depth0 = 1;149buf->b.array_size = 1;150151buf->data = ptr;152buf->free_on_destroy = false;153154return &buf->b;155}156157158