Path: blob/21.2-virgl/src/gallium/frontends/glx/xlib/xm_st.c
4561 views
/*1* Mesa 3-D graphics library2*3* Copyright (C) 2010 LunarG Inc.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the "Software"),7* to deal in the Software without restriction, including without limitation8* the rights to use, copy, modify, merge, publish, distribute, sublicense,9* and/or sell copies of the Software, and to permit persons to whom the10* Software is furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice shall be included13* in all copies or substantial portions of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER21* DEALINGS IN THE SOFTWARE.22*23* Authors:24* Chia-I Wu <[email protected]>25*/2627#include "xm_api.h"28#include "xm_st.h"2930#include "util/u_inlines.h"31#include "util/u_atomic.h"32#include "util/u_memory.h"3334struct xmesa_st_framebuffer {35XMesaDisplay display;36XMesaBuffer buffer;37struct pipe_screen *screen;3839struct st_visual stvis;40enum pipe_texture_target target;4142unsigned texture_width, texture_height, texture_mask;43struct pipe_resource *textures[ST_ATTACHMENT_COUNT];4445struct pipe_resource *display_resource;46};474849static inline struct xmesa_st_framebuffer *50xmesa_st_framebuffer(struct st_framebuffer_iface *stfbi)51{52return (struct xmesa_st_framebuffer *) stfbi->st_manager_private;53}545556/**57* Display (present) an attachment to the xlib_drawable of the framebuffer.58*/59static bool60xmesa_st_framebuffer_display(struct st_framebuffer_iface *stfbi,61struct st_context_iface *stctx,62enum st_attachment_type statt,63struct pipe_box *box)64{65struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);66struct pipe_resource *ptex = xstfb->textures[statt];67struct pipe_resource *pres;68struct pipe_context *pctx = stctx ? stctx->pipe : NULL;6970if (!ptex)71return true;7273pres = xstfb->display_resource;74/* (re)allocate the surface for the texture to be displayed */75if (!pres || pres != ptex) {76pipe_resource_reference(&xstfb->display_resource, ptex);77pres = xstfb->display_resource;78}7980xstfb->screen->flush_frontbuffer(xstfb->screen, pctx, pres, 0, 0, &xstfb->buffer->ws, box);81return true;82}838485/**86* Copy the contents between the attachments.87*/88static void89xmesa_st_framebuffer_copy_textures(struct st_framebuffer_iface *stfbi,90enum st_attachment_type src_statt,91enum st_attachment_type dst_statt,92unsigned x, unsigned y,93unsigned width, unsigned height)94{95struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);96struct pipe_resource *src_ptex = xstfb->textures[src_statt];97struct pipe_resource *dst_ptex = xstfb->textures[dst_statt];98struct pipe_box src_box;99struct pipe_context *pipe;100101if (!src_ptex || !dst_ptex)102return;103104pipe = xmesa_get_context(stfbi);105106u_box_2d(x, y, width, height, &src_box);107108if (src_ptex && dst_ptex)109pipe->resource_copy_region(pipe, dst_ptex, 0, x, y, 0,110src_ptex, 0, &src_box);111}112113114/**115* Remove outdated textures and create the requested ones.116* This is a helper used during framebuffer validation.117*/118bool119xmesa_st_framebuffer_validate_textures(struct st_framebuffer_iface *stfbi,120unsigned width, unsigned height,121unsigned mask)122{123struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);124struct pipe_resource templ;125enum st_attachment_type i;126127/* remove outdated textures */128if (xstfb->texture_width != width || xstfb->texture_height != height) {129for (i = 0; i < ST_ATTACHMENT_COUNT; i++)130pipe_resource_reference(&xstfb->textures[i], NULL);131}132133memset(&templ, 0, sizeof(templ));134templ.target = xstfb->target;135templ.width0 = width;136templ.height0 = height;137templ.depth0 = 1;138templ.array_size = 1;139templ.last_level = 0;140templ.nr_samples = xstfb->stvis.samples;141templ.nr_storage_samples = xstfb->stvis.samples;142143for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {144enum pipe_format format;145unsigned bind;146147/* the texture already exists or not requested */148if (xstfb->textures[i] || !(mask & (1 << i))) {149/* remember the texture */150if (xstfb->textures[i])151mask |= (1 << i);152continue;153}154155switch (i) {156case ST_ATTACHMENT_FRONT_LEFT:157case ST_ATTACHMENT_BACK_LEFT:158case ST_ATTACHMENT_FRONT_RIGHT:159case ST_ATTACHMENT_BACK_RIGHT:160format = xstfb->stvis.color_format;161bind = PIPE_BIND_DISPLAY_TARGET |162PIPE_BIND_RENDER_TARGET;163break;164case ST_ATTACHMENT_DEPTH_STENCIL:165format = xstfb->stvis.depth_stencil_format;166bind = PIPE_BIND_DEPTH_STENCIL;167break;168default:169format = PIPE_FORMAT_NONE;170break;171}172173if (format != PIPE_FORMAT_NONE) {174templ.format = format;175templ.bind = bind;176177xstfb->textures[i] =178xstfb->screen->resource_create(xstfb->screen, &templ);179if (!xstfb->textures[i])180return FALSE;181}182}183184xstfb->texture_width = width;185xstfb->texture_height = height;186xstfb->texture_mask = mask;187188return true;189}190191192/**193* Check that a framebuffer's attachments match the window's size.194*195* Called via st_framebuffer_iface::validate()196*197* \param statts array of framebuffer attachments198* \param count number of framebuffer attachments in statts[]199* \param out returns resources for each of the attachments200*/201static bool202xmesa_st_framebuffer_validate(struct st_context_iface *stctx,203struct st_framebuffer_iface *stfbi,204const enum st_attachment_type *statts,205unsigned count,206struct pipe_resource **out)207{208struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);209unsigned statt_mask, new_mask, i;210bool resized;211bool ret;212213/* build mask of ST_ATTACHMENT bits */214statt_mask = 0x0;215for (i = 0; i < count; i++)216statt_mask |= 1 << statts[i];217218/* record newly allocated textures */219new_mask = statt_mask & ~xstfb->texture_mask;220221/* If xmesa_strict_invalidate is not set, we will not yet have222* called XGetGeometry(). Do so here:223*/224if (!xmesa_strict_invalidate)225xmesa_check_buffer_size(xstfb->buffer);226227resized = (xstfb->buffer->width != xstfb->texture_width ||228xstfb->buffer->height != xstfb->texture_height);229230/* revalidate textures */231if (resized || new_mask) {232ret = xmesa_st_framebuffer_validate_textures(stfbi,233xstfb->buffer->width, xstfb->buffer->height, statt_mask);234if (!ret)235return ret;236237if (!resized) {238enum st_attachment_type back, front;239240back = ST_ATTACHMENT_BACK_LEFT;241front = ST_ATTACHMENT_FRONT_LEFT;242/* copy the contents if front is newly allocated and back is not */243if ((statt_mask & (1 << back)) &&244(new_mask & (1 << front)) &&245!(new_mask & (1 << back))) {246xmesa_st_framebuffer_copy_textures(stfbi, back, front,2470, 0, xstfb->texture_width, xstfb->texture_height);248}249}250}251252for (i = 0; i < count; i++)253pipe_resource_reference(&out[i], xstfb->textures[statts[i]]);254255return true;256}257258259/**260* Called via st_framebuffer_iface::flush_front()261*/262static bool263xmesa_st_framebuffer_flush_front(struct st_context_iface *stctx,264struct st_framebuffer_iface *stfbi,265enum st_attachment_type statt)266{267struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);268bool ret;269270if (statt != ST_ATTACHMENT_FRONT_LEFT)271return false;272273ret = xmesa_st_framebuffer_display(stfbi, stctx, statt, NULL);274275if (ret && xmesa_strict_invalidate)276xmesa_check_buffer_size(xstfb->buffer);277278return ret;279}280281static uint32_t xmesa_stfbi_ID = 0;282283struct st_framebuffer_iface *284xmesa_create_st_framebuffer(XMesaDisplay xmdpy, XMesaBuffer b)285{286struct st_framebuffer_iface *stfbi;287struct xmesa_st_framebuffer *xstfb;288289assert(xmdpy->display == b->xm_visual->display);290291stfbi = CALLOC_STRUCT(st_framebuffer_iface);292xstfb = CALLOC_STRUCT(xmesa_st_framebuffer);293if (!stfbi || !xstfb) {294free(stfbi);295free(xstfb);296return NULL;297}298299xstfb->display = xmdpy;300xstfb->buffer = b;301xstfb->screen = xmdpy->screen;302xstfb->stvis = b->xm_visual->stvis;303if (xstfb->screen->get_param(xstfb->screen, PIPE_CAP_NPOT_TEXTURES))304xstfb->target = PIPE_TEXTURE_2D;305else306xstfb->target = PIPE_TEXTURE_RECT;307308stfbi->visual = &xstfb->stvis;309stfbi->flush_front = xmesa_st_framebuffer_flush_front;310stfbi->validate = xmesa_st_framebuffer_validate;311stfbi->ID = p_atomic_inc_return(&xmesa_stfbi_ID);312stfbi->state_manager = xmdpy->smapi;313p_atomic_set(&stfbi->stamp, 1);314stfbi->st_manager_private = (void *) xstfb;315316return stfbi;317}318319320void321xmesa_destroy_st_framebuffer(struct st_framebuffer_iface *stfbi)322{323struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);324int i;325326pipe_resource_reference(&xstfb->display_resource, NULL);327328for (i = 0; i < ST_ATTACHMENT_COUNT; i++)329pipe_resource_reference(&xstfb->textures[i], NULL);330331free(xstfb);332free(stfbi);333}334335336/**337* Return the pipe_surface which corresponds to the given338* framebuffer attachment.339*/340struct pipe_resource *341xmesa_get_framebuffer_resource(struct st_framebuffer_iface *stfbi,342enum st_attachment_type att)343{344struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);345return xstfb->textures[att];346}347348349void350xmesa_swap_st_framebuffer(struct st_framebuffer_iface *stfbi)351{352struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);353bool ret;354355ret = xmesa_st_framebuffer_display(stfbi, NULL, ST_ATTACHMENT_BACK_LEFT, NULL);356if (ret) {357struct pipe_resource **front, **back, *tmp;358359front = &xstfb->textures[ST_ATTACHMENT_FRONT_LEFT];360back = &xstfb->textures[ST_ATTACHMENT_BACK_LEFT];361/* swap textures only if the front texture has been allocated */362if (*front) {363tmp = *front;364*front = *back;365*back = tmp;366367/* the current context should validate the buffer after swapping */368if (!xmesa_strict_invalidate)369xmesa_notify_invalid_buffer(xstfb->buffer);370}371372if (xmesa_strict_invalidate)373xmesa_check_buffer_size(xstfb->buffer);374}375}376377378void379xmesa_copy_st_framebuffer(struct st_framebuffer_iface *stfbi,380enum st_attachment_type src,381enum st_attachment_type dst,382int x, int y, int w, int h)383{384xmesa_st_framebuffer_copy_textures(stfbi, src, dst, x, y, w, h);385if (dst == ST_ATTACHMENT_FRONT_LEFT) {386struct pipe_box box = {};387388box.x = x;389box.y = y;390box.width = w;391box.height = h;392xmesa_st_framebuffer_display(stfbi, NULL, src, &box);393}394}395396397struct pipe_resource*398xmesa_get_attachment(struct st_framebuffer_iface *stfbi,399enum st_attachment_type st_attachment)400{401struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);402struct pipe_resource *res;403404res = xstfb->textures[st_attachment];405return res;406}407408409struct pipe_context*410xmesa_get_context(struct st_framebuffer_iface *stfbi)411{412struct pipe_context *pipe;413struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);414415pipe = xstfb->display->pipe;416if (!pipe) {417pipe = xstfb->screen->context_create(xstfb->screen, NULL, 0);418if (!pipe)419return NULL;420xstfb->display->pipe = pipe;421}422return pipe;423}424425426