Path: blob/21.2-virgl/src/gallium/drivers/vc4/vc4_context.c
4570 views
/*1* Copyright © 2014 Broadcom2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include <xf86drm.h>24#include <err.h>2526#include "pipe/p_defines.h"27#include "util/ralloc.h"28#include "util/u_inlines.h"29#include "util/u_memory.h"30#include "util/u_blitter.h"31#include "util/u_upload_mgr.h"32#include "indices/u_primconvert.h"33#include "pipe/p_screen.h"3435#include "vc4_screen.h"36#include "vc4_context.h"37#include "vc4_resource.h"3839void40vc4_flush(struct pipe_context *pctx)41{42struct vc4_context *vc4 = vc4_context(pctx);4344hash_table_foreach(vc4->jobs, entry) {45struct vc4_job *job = entry->data;46vc4_job_submit(vc4, job);47}48}4950static void51vc4_pipe_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,52unsigned flags)53{54struct vc4_context *vc4 = vc4_context(pctx);5556vc4_flush(pctx);5758if (fence) {59struct pipe_screen *screen = pctx->screen;60int fd = -1;6162if (flags & PIPE_FLUSH_FENCE_FD) {63/* The vc4_fence takes ownership of the returned fd. */64drmSyncobjExportSyncFile(vc4->fd, vc4->job_syncobj,65&fd);66}6768struct vc4_fence *f = vc4_fence_create(vc4->screen,69vc4->last_emit_seqno,70fd);71screen->fence_reference(screen, fence, NULL);72*fence = (struct pipe_fence_handle *)f;73}74}7576/* We can't flush the texture cache within rendering a tile, so we have to77* flush all rendering to the kernel so that the next job reading from the78* tile gets a flushed cache.79*/80static void81vc4_texture_barrier(struct pipe_context *pctx, unsigned flags)82{83vc4_flush(pctx);84}8586static void87vc4_set_debug_callback(struct pipe_context *pctx,88const struct pipe_debug_callback *cb)89{90struct vc4_context *vc4 = vc4_context(pctx);9192if (cb)93vc4->debug = *cb;94else95memset(&vc4->debug, 0, sizeof(vc4->debug));96}9798static void99vc4_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)100{101struct vc4_context *vc4 = vc4_context(pctx);102struct vc4_resource *rsc = vc4_resource(prsc);103104rsc->initialized_buffers = 0;105106struct hash_entry *entry = _mesa_hash_table_search(vc4->write_jobs,107prsc);108if (!entry)109return;110111struct vc4_job *job = entry->data;112if (job->key.zsbuf && job->key.zsbuf->texture == prsc)113job->resolve &= ~(PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL);114}115116static void117vc4_context_destroy(struct pipe_context *pctx)118{119struct vc4_context *vc4 = vc4_context(pctx);120121vc4_flush(pctx);122123if (vc4->blitter)124util_blitter_destroy(vc4->blitter);125126if (vc4->primconvert)127util_primconvert_destroy(vc4->primconvert);128129if (vc4->uploader)130u_upload_destroy(vc4->uploader);131132slab_destroy_child(&vc4->transfer_pool);133134pipe_surface_reference(&vc4->framebuffer.cbufs[0], NULL);135pipe_surface_reference(&vc4->framebuffer.zsbuf, NULL);136137if (vc4->yuv_linear_blit_vs)138pctx->delete_vs_state(pctx, vc4->yuv_linear_blit_vs);139if (vc4->yuv_linear_blit_fs_8bit)140pctx->delete_fs_state(pctx, vc4->yuv_linear_blit_fs_8bit);141if (vc4->yuv_linear_blit_fs_16bit)142pctx->delete_fs_state(pctx, vc4->yuv_linear_blit_fs_16bit);143144vc4_program_fini(pctx);145146if (vc4->screen->has_syncobj) {147drmSyncobjDestroy(vc4->fd, vc4->job_syncobj);148drmSyncobjDestroy(vc4->fd, vc4->in_syncobj);149}150if (vc4->in_fence_fd >= 0)151close(vc4->in_fence_fd);152153ralloc_free(vc4);154}155156struct pipe_context *157vc4_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)158{159struct vc4_screen *screen = vc4_screen(pscreen);160struct vc4_context *vc4;161int err;162163/* Prevent dumping of the shaders built during context setup. */164uint32_t saved_shaderdb_flag = vc4_debug & VC4_DEBUG_SHADERDB;165vc4_debug &= ~VC4_DEBUG_SHADERDB;166167vc4 = rzalloc(NULL, struct vc4_context);168if (!vc4)169return NULL;170struct pipe_context *pctx = &vc4->base;171172vc4->screen = screen;173174pctx->screen = pscreen;175pctx->priv = priv;176pctx->destroy = vc4_context_destroy;177pctx->flush = vc4_pipe_flush;178pctx->set_debug_callback = vc4_set_debug_callback;179pctx->invalidate_resource = vc4_invalidate_resource;180pctx->texture_barrier = vc4_texture_barrier;181182vc4_draw_init(pctx);183vc4_state_init(pctx);184vc4_program_init(pctx);185vc4_query_init(pctx);186vc4_resource_context_init(pctx);187188vc4->fd = screen->fd;189190err = vc4_job_init(vc4);191if (err)192goto fail;193194err = vc4_fence_context_init(vc4);195if (err)196goto fail;197198slab_create_child(&vc4->transfer_pool, &screen->transfer_pool);199200vc4->uploader = u_upload_create_default(&vc4->base);201vc4->base.stream_uploader = vc4->uploader;202vc4->base.const_uploader = vc4->uploader;203204vc4->blitter = util_blitter_create(pctx);205if (!vc4->blitter)206goto fail;207208vc4->primconvert = util_primconvert_create(pctx,209(1 << PIPE_PRIM_QUADS) - 1);210if (!vc4->primconvert)211goto fail;212213vc4_debug |= saved_shaderdb_flag;214215vc4->sample_mask = (1 << VC4_MAX_SAMPLES) - 1;216217return &vc4->base;218219fail:220pctx->destroy(pctx);221return NULL;222}223224225