summaryrefslogtreecommitdiff
path: root/hw/kdrive/src/koffscreen.c
blob: 65c15a284b4174e353cd5ace265037c38f179999 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * $Id$
 *
 * Copyright © 2003 Anders Carlsson
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Anders Carlsson not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Anders Carlsson makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * ANDERS CARLSSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL ANDERS CARLSSON BE LIABLE FOR 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.
 */
/* $Header$ */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "kdrive.h"

#define DEBUG_OFFSCREEN 0
#if DEBUG_OFFSCREEN
#define DBG_OFFSCREEN(a) ErrorF a
#else
#define DBG_OFFSCREEN(a)
#endif

typedef struct _RealOffscreenArea {
    KdOffscreenArea area;

    KdOffscreenSaveProc save;

    Bool locked;
    
    struct _RealOffscreenArea *prev;
    struct _RealOffscreenArea *next;
} RealOffscreenArea;

#if DEBUG_OFFSCREEN
static void
KdOffscreenValidate (ScreenPtr pScreen)
{
    KdScreenPriv (pScreen);
    RealOffscreenArea *prev = 0, *area;

    assert (pScreenPriv->screen->off_screen_areas->area.offset == 0);
    for (area = pScreenPriv->screen->off_screen_areas; area; area = area->next)
    {
	if (prev)
	    assert (prev->area.offset + prev->area.size == area->area.offset);
	    
	prev = area;
    }
    assert (prev->area.offset + prev->area.size == pScreenPriv->screen->off_screen_size);
}
#else
#define KdOffscreenValidate(s)
#endif

static void
KdOffscreenKickOut (KdOffscreenArea *area)
{
    RealOffscreenArea *real_area = (RealOffscreenArea *) area;
    KdCheckSync (area->screen);
    if (real_area->save)
	(*real_area->save) (area);
    KdOffscreenFree (area);
}

KdOffscreenArea *
KdOffscreenAlloc (ScreenPtr pScreen, int size, int align,
		  Bool locked,
		  KdOffscreenSaveProc save,
		  pointer privData)
{
    RealOffscreenArea *area, **prev;
    KdScreenPriv (pScreen);
    int tmp, real_size;

    KdOffscreenValidate (pScreen);
    if (!align)
	align = 1;

    if (!size)
    {
	DBG_OFFSCREEN (("Alloc 0x%x -> EMPTY\n", size));
	return NULL;
    }

    /* throw out requests that cannot fit */
    if (size > pScreenPriv->screen->off_screen_size)
    {
	DBG_OFFSCREEN (("Alloc 0x%x -> TOBIG\n", size));
	return NULL;
    }
    
retry:

    /* Go through the areas */
    for (area = pScreenPriv->screen->off_screen_areas; area; area = area->next)
    {
	/* skip allocated areas */
	if (area->area.screen)
	    continue;

	/* adjust size to match alignment requirement */
	real_size = size;
	tmp = area->area.offset % align;
	if (tmp)
	    real_size += (align - tmp);
	
	/* does it fit? */
	if (real_size <= area->area.size)
	{
	    RealOffscreenArea *new_area;

	    /* save extra space in new area */
	    if (real_size < area->area.size)
	    {
		new_area = xalloc (sizeof (RealOffscreenArea));
		if (!new_area)
		    return NULL;
		new_area->area.offset = area->area.offset + real_size;
		new_area->area.size = area->area.size - real_size;
		new_area->area.screen = 0;
		new_area->locked = FALSE;
		new_area->save = 0;
		if ((new_area->next = area->next))
		    new_area->next->prev = new_area;
		new_area->prev = area;
		area->next = new_area;
		area->area.size = real_size;
	    }
	    area->area.screen = pScreen;
	    area->area.privData = privData;
	    area->locked = locked;
	    area->save = save;

	    KdOffscreenValidate (pScreen);
	    
	    DBG_OFFSCREEN (("Alloc 0x%x -> 0x%x\n", size, area->area.offset));
	    return &area->area;
	}
    }
    
    /* 
     * Kick out existing users.  This is pretty simplistic; it just
     * keeps deleting areas until the first area is free and has enough room
     */
    
    prev = (RealOffscreenArea **) &pScreenPriv->screen->off_screen_areas;
    while ((area = *prev))
    {
	if (area->area.screen && !area->locked)
	{
	    KdOffscreenKickOut (&area->area);
	    continue;
	}
	/* adjust size to match alignment requirement */
	real_size = size;
	tmp = area->area.offset % align;
	if (tmp)
	    real_size += (align - tmp);
	
	/* does it fit? */
	if (real_size <= area->area.size)
	    goto retry;

	/* kick out the next area */
	area = area->next;
	if (!area)
	    break;
	/* skip over locked areas */
	if (area->locked)
	{
	    prev = &area->next;
	    continue;
	}
	assert (area->area.screen);
	KdOffscreenKickOut (&area->area);
    }
    
    DBG_OFFSCREEN (("Alloc 0x%x -> NOSPACE\n", size));
    /* Could not allocate memory */
    KdOffscreenValidate (pScreen);
    return NULL;
}

void
KdOffscreenSwapOut (ScreenPtr pScreen)
{
    KdScreenPriv (pScreen);

    KdOffscreenValidate (pScreen);
    /* loop until a single free area spans the space */
    for (;;)
    {
	RealOffscreenArea *area = pScreenPriv->screen->off_screen_areas;
	
	if (!area)
	    break;
	if (area->area.screen)
	{
	    KdOffscreenKickOut (&area->area);
	    continue;
	}
	area = area->next;
	if (!area)
	    break;
	assert (area->area.screen);
	KdOffscreenKickOut (&area->area);
	KdOffscreenValidate (pScreen);
    }    
    KdOffscreenValidate (pScreen);
    KdOffscreenFini (pScreen);
}

void
KdOffscreenSwapIn (ScreenPtr pScreen)
{
    KdOffscreenInit (pScreen);
}

/* merge the next free area into this one */
static void
KdOffscreenMerge (KdOffscreenArea *area)
{
    RealOffscreenArea	*real_area = (RealOffscreenArea *) area;
    RealOffscreenArea	*next = real_area->next;

    /* account for space */
    real_area->area.size += next->area.size;
    /* frob pointers */
    if ((real_area->next = next->next))
	real_area->next->prev = real_area;
    xfree (next);
}

void
KdOffscreenFree (KdOffscreenArea *area)
{
    RealOffscreenArea	*real_area = (RealOffscreenArea *) area;
    RealOffscreenArea	*next = real_area->next;
    RealOffscreenArea	*prev = real_area->prev;
    
    DBG_OFFSCREEN (("Free 0x%x -> 0x%x\n", area->size, area->offset));
    KdOffscreenValidate (pScreen);

    area->screen = NULL;

    /* link with next area if free */
    if (next && !next->area.screen)
	KdOffscreenMerge (&real_area->area);
    
    /* link with prev area if free */
    if (prev && !prev->area.screen)
	KdOffscreenMerge (&prev->area);

    KdOffscreenValidate (pScreen);
}

Bool
KdOffscreenInit (ScreenPtr pScreen)
{
    KdScreenPriv (pScreen);
    RealOffscreenArea *area;

    /* Allocate a big free area */
    area = xalloc (sizeof (RealOffscreenArea));

    if (!area)
	return FALSE;

    area->area.screen = NULL;
    area->area.offset = pScreenPriv->screen->off_screen_base;
    area->area.size = pScreenPriv->screen->off_screen_size;
    area->save = 0;
    area->locked = FALSE;
    area->next = NULL;
    area->prev = NULL;
    
    /* Add it to the free areas */
    pScreenPriv->screen->off_screen_areas = area;
    
    KdOffscreenValidate (pScreen);

    return TRUE;
}

void
KdOffscreenFini (ScreenPtr pScreen)
{
    KdScreenPriv (pScreen);
    RealOffscreenArea *area;
    
    /* just free all of the area records */
    while ((area = pScreenPriv->screen->off_screen_areas))
    {
	pScreenPriv->screen->off_screen_areas = area->next;
	xfree (area);
    }
}