summaryrefslogtreecommitdiff
path: root/twin_screen.c
blob: 1821cc6abd2948ce278c30ab9c299c0a1f2246ca (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
/*
 * $Id$
 *
 * Copyright © 2004 Keith Packard
 *
 * 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 Keith Packard not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Keith Packard makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL KEITH PACKARD 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.
 */

#include "twinint.h"

twin_screen_t *
twin_screen_create (twin_coord_t	width,
		    twin_coord_t	height, 
		    twin_put_begin_t	put_begin,
		    twin_put_span_t	put_span,
		    void		*closure)
{
    twin_screen_t   *screen = malloc (sizeof (twin_screen_t));
    if (!screen)
	return 0;
    screen->bottom = 0;
    screen->width = width;
    screen->height = height;
    screen->damage.left = screen->damage.right = 0;
    screen->damage.top = screen->damage.bottom = 0;
    screen->damaged = NULL;
    screen->damaged_closure = NULL;
    screen->disable = 0;
#if HAVE_PTHREAD_H
    pthread_mutex_init (&screen->screen_mutex, NULL);
#endif
    screen->put_begin = put_begin;
    screen->put_span = put_span;
    screen->closure = closure;
    return screen;
}

void
twin_screen_lock (twin_screen_t *screen)
{
#if HAVE_PTHREAD_H
    pthread_mutex_lock (&screen->screen_mutex);
#endif
}

void
twin_screen_unlock (twin_screen_t *screen)
{
#if HAVE_PTHREAD_H
    pthread_mutex_unlock (&screen->screen_mutex);
#endif
}

void
twin_screen_destroy (twin_screen_t *screen)
{
    while (screen->bottom)
	twin_pixmap_hide (screen->bottom);
    free (screen);
}

void
twin_screen_register_damaged (twin_screen_t *screen, 
			      void (*damaged) (void *),
			      void *closure)
{
    screen->damaged = damaged;
    screen->damaged_closure = closure;
}

void
twin_screen_enable_update (twin_screen_t *screen)
{
    if (--screen->disable == 0)
    {
	if (screen->damage.left < screen->damage.right &&
	    screen->damage.top < screen->damage.bottom)
	{
	    if (screen->damaged)
		(*screen->damaged) (screen->damaged_closure);
	}
    }
}

void
twin_screen_disable_update (twin_screen_t *screen)
{
    screen->disable++;
}

void
twin_screen_damage (twin_screen_t *screen,
		    twin_coord_t left, twin_coord_t top,
		    twin_coord_t right, twin_coord_t bottom)
{
    if (screen->damage.left == screen->damage.right)
    {
	screen->damage.left = left;
	screen->damage.right = right;
	screen->damage.top = top;
	screen->damage.bottom = bottom;
    }
    else
    {
	if (left < screen->damage.left)
	    screen->damage.left = left;
	if (top < screen->damage.top)
	    screen->damage.top = top;
	if (screen->damage.right < right)
	    screen->damage.right = right;
	if (screen->damage.bottom < bottom)
	    screen->damage.bottom = bottom;
    }
    if (screen->damaged && !screen->disable)
	(*screen->damaged) (screen->damaged_closure);
}

void
twin_screen_resize (twin_screen_t *screen, 
		    twin_coord_t width, twin_coord_t height)
{
    screen->width = width;
    screen->height = height;
    twin_screen_damage (screen, 0, 0, screen->width, screen->height);
}

twin_bool_t
twin_screen_damaged (twin_screen_t *screen)
{
    return (screen->damage.left < screen->damage.right &&
	    screen->damage.top < screen->damage.bottom);
}

void
twin_screen_update (twin_screen_t *screen)
{
    if (!screen->disable &&
	screen->damage.left < screen->damage.right &&
	screen->damage.top < screen->damage.bottom)
    {
	twin_coord_t	x = screen->damage.left;
	twin_coord_t	y = screen->damage.top;
	twin_coord_t	width = screen->damage.right - screen->damage.left;
	twin_coord_t	height = screen->damage.bottom - screen->damage.top;
	twin_argb32_t	*span;
        twin_pixmap_t	*p;

	/* XXX what is the maximum number of lines? */
	span = malloc (width * sizeof (twin_argb32_t));
	if (!span)
	    return;
	
	if (screen->put_begin)
	    (*screen->put_begin) (x, y, width, height, screen->closure);
	while (height--)
	{
	    memset (span, 0xff, width * sizeof (twin_argb32_t));
	    for (p = screen->bottom; p; p = p->higher)
	    {
		twin_pointer_t  dst;
		twin_source_u	src;

		int left, right;
		/* bounds check in y */
		if (y < p->y)
		    continue;
		if (p->y + p->height <= y)
		    continue;
		/* bounds check in x*/
		left = x;
		if (left < p->x)
		    left = p->x;
		right = x + width;
		if (right > p->x + p->width)
		    right = p->x + p->width;
		if (left >= right)
		    continue;
		dst.argb32 = span + (left - x);
		src.p = twin_pixmap_pointer (p, left - p->x, y - p->y);
		if (p->format == TWIN_RGB16)
		    _twin_rgb16_source_argb32 (dst, src, right - left);
		else
		    _twin_argb32_over_argb32 (dst, src, right - left);
	    }
	    (*screen->put_span) (x, y, width, span, screen->closure);
	    y++;
	}
	free (span);
	screen->damage.left = screen->damage.right = 0;
	screen->damage.top = screen->damage.bottom = 0;
    }
}