open-src/lib/libdga/sun-src/mbuf_update.c
author Alan Coopersmith <Alan.Coopersmith@Sun.COM>
Thu, 18 Feb 2010 16:03:08 -0800
changeset 907 3c35d611cdaa
parent 830 186f3c438b46
child 919 425233ae7236
permissions -rw-r--r--
6927784 Remove SCCS ident tags from X gate

/* Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, and/or sell copies of the Software, and to permit persons
 * to whom the Software is furnished to do so, provided that the above
 * copyright notice(s) and this permission notice appear in all copies of
 * the Software and that both the above copyright notice(s) and this
 * permission notice appear in supporting documentation.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
 * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
 * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Except as contained in this notice, the name of a copyright holder
 * shall not be used in advertising or otherwise to promote the sale, use
 * or other dealings in this Software without prior written authorization
 * of the copyright holder.
 */


/*
** mbuf_update.c
**
** Routines of the update phase for multibuffers.
*/

#include <assert.h>
#ifdef SERVER_DGA
#include <X11/Xlib.h>
#endif /* SERVER_DGA */
#include "dga_incls.h"
#include "pix_grab.h"
#include "mbufsetstr.h"

static void	dgai_mbuf_getLastSeqs (_Dga_window dgawin, DgaLastSeqsPtr pLastSeqs, short bufIndex);
static int	dgai_mbuf_syncAgain (_Dga_window dgawin, DgaLastSeqsPtr pLastSeqs);
static void	dgai_mbuf_mb_update (_Dga_window dgawin);
static void	dgai_mbuf_syncLockSubj (_Dga_window dgawin);
static void	dgai_mbuf_figureDerivChgs (_Dga_window dgawin);

/*
** Note: On every alias or mbufset change, we will report the following
** types of changes. See comment at start of win_update.c for reason why we
** don't try to optimize change reporting.  
**
**	site
**	clip
**	curs
**      bstore
**	cache (nonviewable mbufs only)
**
** TODO: ISSUE: since an alias change occurs on every multibuffer frame,
** if we don't optimize for multibuffers this could add unwanted per-frame 
** overhead.  Is it significant?  If so, we are forced to optimize alias 
** change reporting.  Composition changes are so infrequent that it still 
** won't be worth optimizing the reporting of them. (Note: this issue 
** doesn't apply to windows.)
*/


/*
** ENTRY ROUTINE FOR MULTIBUFFER UPDATE PHASE 
*/

int
dgai_mbuf_update (_Dga_window dgawin, short bufIndex)
{
    DgaLastSeqs		lastSeqs;
    DgaMbufSetPtr	pMbufSet;

    /*
     * Before we get going check to make sure that composition changes
     * have been noticed.  If we don't and the number of multibuffers
     * has increased bad things will happen.
     */

    if (dgawin->c_mbufseq != *dgawin->s_mbufseq_p)
	dgai_mbsmemb_syncMbufset(dgawin);

    /*
    ** 1. The first thing we do in the update phase is to synchronize
    **    the client structure.  When all pertinent client change
    **    counters match the server, we are synchronized.  The 
    **    reason we must do this in a loop is that some of the
    **    synchronizations require us to temporarily unlock and 
    **    relock.  While the drawable is unlocked, it can undergo
    **    more changes, so we may need to start the synchronization
    **    process over.
    */

    pMbufSet = dgawin->pMbs;
    /* Determine sequence counts for state reported to client */
    dgai_mbuf_getLastSeqs(dgawin, &lastSeqs, bufIndex);

    /* start accumulating changes */
    dgawin->changeMask = 0;

    /* repeat synchronization functions as needed through possible unlock/relocks */
    do {

	/* first, see if the window shared info is still valid */
	if (dgai_mbsmemb_syncZombie(dgawin)) {
	    break;
        }

	/* the first thing we must change is the multibuffer state.
	   This may later the effective lock subject, whose type the
	   other synchronization checks depend on */
	if (lastSeqs.mbufseq != *dgawin->s_mbufseq_p) {
	    dgai_mbuf_mb_update(dgawin);
	    lastSeqs.mbufseq = *dgawin->s_mbufseq_p;
	    pMbufSet->mbufseq[bufIndex] = lastSeqs.mbufseq;
	}

	/* synchronize with current changes to attributes of
	   the effective lock subject -- depends on the type
	   of the current lock subject */
	if        (DGA_LOCKSUBJ_WINDOW(dgawin, dgawin->eLockSubj)) {
	    dgai_win_syncChanges(dgawin, &lastSeqs);
	} else if (DGA_LOCKSUBJ_VMBUFFER(dgawin, dgawin->eLockSubj)) {
	    dgai_vmbuf_syncChanges(dgawin, &lastSeqs, bufIndex);
	} else if (DGA_LOCKSUBJ_NMBUFFER(dgawin, dgawin->eLockSubj)) {
	    dgai_nmbuf_syncChanges(dgawin);
	} else {
	    /* should never happen */
	    assert(0);
	}

	/* Note: whether we need to sync again depends on the type too */
    } while (dgai_mbuf_syncAgain(dgawin, &lastSeqs));

    /* Check if the devinfo has changed */
    dgai_mbsmemb_devinfo_update(dgawin);

    /* 
    ** 2. The foregoing synchronization step has determined whether 
    **    any attribute changes have occurred.  We must now determine
    **    if there are any derivative changes to report.
    */
    dgai_mbuf_figureDerivChgs(dgawin);


    /* 
    ** 3. Next, we must report changes through notification functions,
    **    if possible.
    */

    /* report any changes that we can through notification */
    dgai_mbsmemb_notify(dgawin);


    /* 
    ** 4. Lastly, indicate that we are fully synchronized and get out.
    */

    /* the dgawin client structure is now fully synchronized with the
       shared info */
    dgawin->c_wm_chngcnt[bufIndex + 1] = *dgawin->s_chngcnt_p;

    /* if there are still any changes that were not notified through notification
       functions, DGA_DRAW_MODIF will return nonzero (the client is
       supposed to call this routine immediately following the lock).
       This will cause a well-behaved client to synchronize with 
       the remaining unreported changes */
    return (dgawin->changeMask);
}


static void
dgai_mbuf_getLastSeqs (_Dga_window dgawin, DgaLastSeqsPtr pLastSeqs, short bufIndex)
{
    DgaMbufSetPtr	pMbufSet = dgawin->pMbs;

    if (DGA_LOCKSUBJ_WINDOW(dgawin, dgawin->eLockSubj)) {
        pLastSeqs->mbufseq = dgawin->c_mbufseq;
        pLastSeqs->clipseq = dgawin->c_clipseq;
        pLastSeqs->curseq = dgawin->c_curseq;
        pLastSeqs->rtnseq = dgawin->c_rtnseq;
    } else {
        pLastSeqs->mbufseq = pMbufSet->mbufseq[bufIndex];
        pLastSeqs->clipseq = pMbufSet->clipseq[bufIndex];
        pLastSeqs->curseq = pMbufSet->curseq[bufIndex];
        pLastSeqs->rtnseq = dgawin->c_rtnseq;
    }
}


/*
** Returns nonzero if we are still not synchronized 
*/

static int
dgai_mbuf_syncAgain (_Dga_window dgawin, DgaLastSeqsPtr pLastSeqs)
{
    /* if a multibuffer change happened, we're still not done */
    if (pLastSeqs->mbufseq != *dgawin->s_mbufseq_p) {
	return (1);
    }

    if (DGA_LOCKSUBJ_WINDOW(dgawin, dgawin->eLockSubj)) {
	if ((pLastSeqs->clipseq == *dgawin->s_clipseq_p)  &&
	    (pLastSeqs->curseq  == *dgawin->s_curseq_p)   &&
	    (pLastSeqs->rtnseq  == *dgawin->s_rtnseq_p)) {
	    return (0);
	}
    } else if (DGA_LOCKSUBJ_VMBUFFER(dgawin, dgawin->eLockSubj)) {
	if ((pLastSeqs->clipseq == *dgawin->s_clipseq_p)  &&
	    (pLastSeqs->curseq  == *dgawin->s_curseq_p)) {
	    /* Note: viewable mbuf never has bstore to sync up to */
	    return (0);
	}
    } else if (DGA_LOCKSUBJ_NMBUFFER(dgawin, dgawin->eLockSubj)) {
	/* Note: we don't need to check the cacheseq here, because for
	   a nonviewable drawable the cache is the only thing to sync 
	   so there will never be any reason for it to be out-of-sync 
	   when we get here */
        return (0);
    }

    /* we're now all synchronized */
    return (1);
}


/* 
** Called at window lock time when we have detected an mbufset change.
** At the return of this routine, dgawin->eLockSubj indicates the member
** drawable for which subsequent changes are to be detected.
*/

static void
dgai_mbuf_mb_update (_Dga_window dgawin)
{
    /* react to enable and composition changes */
    dgai_mbsmemb_syncMbufset(dgawin);

    /* if window is multibuffered we may need to synchronize */
    if (dgawin->pMbs) {
	WXINFO	*infop;
	
	infop = (WXINFO *) dgawin->w_info;

	/* Now that we know the current mbufset composition, we know the
	   number of multibuffers. if the real lock subject is beyond
	   this, treat it as a zombie */
	if (dgawin->rLockSubj != -1 &&
	    dgawin->rLockSubj >= infop->wx_dbuf.number_buffers) {
	    dgawin->changeMask |= DGA_CHANGE_ZOMBIE;
	    return;
	}	    

	/* synchronize with any display buffer change */
	dgai_mbuf_syncLockSubj(dgawin);

	/* synchronize render buffer state (if necessary) */
	dgai_mbsmemb_syncRendBuf(dgawin);

    } else {
	/* The window is no longer multibuffered. This multibuffer is a zombie */
	dgawin->changeMask |= DGA_CHANGE_ZOMBIE;
	return;
    }

    /* an mbufset change causes us to report changes to all attrs  */
    { u_int reportChanges;
      if (DGA_LOCKSUBJ_WINDOW(dgawin, dgawin->eLockSubj)) {
	  reportChanges = DGA_WIN_CHANGEABLE_ATTRS;
      } else if (DGA_LOCKSUBJ_VMBUFFER(dgawin, dgawin->eLockSubj)) {
	  reportChanges = DGA_VMBUF_CHANGEABLE_ATTRS;
      } else {
	  reportChanges = DGA_NMBUF_CHANGEABLE_ATTRS;
      }
      dgawin->changeMask |= (DGA_CHANGE_MBUFSET | reportChanges);
    }
}

/*
** Determine effective lock subject for a multibuffered window.
*/

static void
dgai_mbuf_syncLockSubj (_Dga_window dgawin)
{
    WXINFO	*infop;
	
    infop = (WXINFO *) dgawin->w_info;

    /* see if there is buffer aliasing.  Buffer aliasing
       can only happen in copy flip mode */
    /* TODO Daryl  Is this true?  In document it talks about
     * buffer aliasing with DGA_MBFLIP_VIDEO mode. */
    if ((infop->w_mbsInfo.flipMode == DGA_MBFLIP_COPY) &&
	(infop->wx_dbuf.display_buffer == dgawin->rLockSubj)) {
	
	/* when the buffer is aliased, the effective lock subject is
	   always the main window */
	dgawin->eLockSubj = -1;

	if (dgawin->eLockSubj != dgawin->eLockSubjPrev) {

	    /* an alias change causes us to report changes on all attributes */
	    dgawin->changeMask |= (DGA_CHANGE_ALIAS | DGA_WIN_CHANGEABLE_ATTRS);

	}
    }
}


/*
** In addition to the mbufset-common types of derivative changes, 
** we may also need to cause a clip change to be reported.
*/

static void
dgai_mbuf_figureDerivChgs (_Dga_window dgawin)
{
    int 	curViewable;
    int		prevViewable;

    /* common changes */
    dgai_mbsmemb_figureDerivChgs(dgawin);

    /* 
    ** If we've switched from a viewable lock subject to 
    ** a nonviewable one (or vice versa) force a clip change even
    ** if the clip of the current effective lock subject has not changed.
    ** This is because the clips of nonviewable drawables are
    ** always rectangular and those of viewable ones may not be.
    */
    curViewable = DGA_LOCKSUBJ_VALID(dgawin, dgawin->eLockSubj) &&
                  (dgawin->eLockSubj == -1 || 
		   DGA_MBUF_ISVIEWABLE(dgawin->pMbs, dgawin->eLockSubj));
    prevViewable = DGA_LOCKSUBJ_VALID(dgawin, dgawin->eLockSubjPrev) &&
                  (dgawin->eLockSubjPrev == -1 || 
		   DGA_MBUF_ISVIEWABLE(dgawin->pMbs, dgawin->eLockSubjPrev));
    
    if ((curViewable && !prevViewable) || 
	(!curViewable && prevViewable)) {
	dgawin->changeMask |= DGA_CHANGE_CLIP;
    }
}


/*
** Current lock subject is a viewable multibuffer. Synchronize with changes.
*/

void
dgai_vmbuf_syncChanges (_Dga_window dgawin, DgaLastSeqsPtr pLastSeqs, short bufIndex)
{
    /* Note: viewable multibuffers share the clip and cursor state 
       of the main window */

    /* clip state of main window has changed? */
    if (pLastSeqs->clipseq != *dgawin->s_clipseq_p) {
	dgawin->changeMask |= DGA_CHANGE_CLIP;
	dgai_win_clip_update(dgawin);
	pLastSeqs->clipseq = *dgawin->s_clipseq_p;
	dgawin->pMbs->clipseq[bufIndex] = pLastSeqs->clipseq;
    }

    /* cursor state of main window has changed? */
    if (pLastSeqs->curseq != *dgawin->s_curseq_p) {
	dgawin->changeMask |= DGA_CHANGE_CURSOR;
	dgai_win_curs_update(dgawin);
	pLastSeqs->curseq = *dgawin->s_curseq_p;
	dgawin->pMbs->curseq[bufIndex] = pLastSeqs->curseq;
    }

    /* Note: viewable multibuffers do not yet have backing store */
}


/*
** Current lock subject is a non-viewable multibuffer. Synchronize with changes.
*/

void
dgai_nmbuf_syncChanges (_Dga_window dgawin)
{
    SHARED_PIXMAP_INFO	*infop;

    /* don't try to synchronize if multibuffer is a zombie */
    if ((dgawin->changeMask & DGA_CHANGE_ZOMBIE)) {
	return;
    }

    /* first, see if the shared info is still valid */
    infop = dgawin->pMbs->pNbShinfo[dgawin->eLockSubj];
    if (infop->obsolete) {
        dgawin->changeMask |= DGA_CHANGE_ZOMBIE;
	return;
    }

    /* Note: a non-viewable multibuffer never has a cursor
       or backing store.  And it's clip is always constant.  The
       only thing that can change is the cache or devinfo state */

    /* Has the cache changed? */
    dgai_nmbuf_cache_update(dgawin);

    /* Has the dev_info changed? */
    dgai_nmbuf_devinfo_update(dgawin);
}


void 
dgai_nmbuf_cache_update (_Dga_window dgawin) 
{
    DgaMbufSetPtr 		pMbs;
    u_int			*s_cacheseq_p;

    pMbs = dgawin->pMbs;
    s_cacheseq_p = &(pMbs->pNbShinfo[dgawin->eLockSubj]->s_cacheseq);

    if (pMbs->cacheSeqs[dgawin->eLockSubj] != *s_cacheseq_p) {
	dgawin->changeMask |= DGA_CHANGE_CACHE;

	/* synchronize change counts */
	pMbs->cacheSeqs[dgawin->eLockSubj] = *s_cacheseq_p;
    }
}

void 
dgai_nmbuf_devinfo_update (_Dga_window dgawin) 
{
    DgaMbufSetPtr 		pMbs;
    u_int			*s_devinfoseq_p;

    pMbs = dgawin->pMbs;
    s_devinfoseq_p = &(pMbs->pNbShinfo[dgawin->eLockSubj]->s_devinfoseq);

    if (pMbs->devInfoSeqs[dgawin->eLockSubj] != *s_devinfoseq_p) {
	dgawin->changeMask |= DGA_CHANGE_DEVINFO;

	/* synchronize change counts */
	pMbs->devInfoSeqs[dgawin->eLockSubj] = *s_devinfoseq_p;
    }
}