1428d7b3dSmrg/*
2428d7b3dSmrg * Copyright © 2012 Intel Corporation
3428d7b3dSmrg *
4428d7b3dSmrg * Permission is hereby granted, free of charge, to any person obtaining a
5428d7b3dSmrg * copy of this software and associated documentation files (the "Software"),
6428d7b3dSmrg * to deal in the Software without restriction, including without limitation
7428d7b3dSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8428d7b3dSmrg * and/or sell copies of the Software, and to permit persons to whom the
9428d7b3dSmrg * Software is furnished to do so, subject to the following conditions:
10428d7b3dSmrg *
11428d7b3dSmrg * The above copyright notice and this permission notice (including the next
12428d7b3dSmrg * paragraph) shall be included in all copies or substantial portions of the
13428d7b3dSmrg * Software.
14428d7b3dSmrg *
15428d7b3dSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16428d7b3dSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17428d7b3dSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18428d7b3dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19428d7b3dSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20428d7b3dSmrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21428d7b3dSmrg * SOFTWARE.
22428d7b3dSmrg *
23428d7b3dSmrg * Authors:
24428d7b3dSmrg *    Chris Wilson <chris@chris-wilson.co.uk>
25428d7b3dSmrg *
26428d7b3dSmrg */
27428d7b3dSmrg
28428d7b3dSmrg#ifndef FBCLIP_H
29428d7b3dSmrg#define FBCLIP_H
30428d7b3dSmrg
31428d7b3dSmrgextern const BoxRec *
32428d7b3dSmrgfbClipBoxes(const RegionRec *region, const BoxRec *box, const BoxRec **end);
33428d7b3dSmrg
34428d7b3dSmrginline static bool
35428d7b3dSmrgbox_intersect(BoxPtr a, const BoxRec *b)
36428d7b3dSmrg{
37428d7b3dSmrg	if (a->x1 < b->x1)
38428d7b3dSmrg		a->x1 = b->x1;
39428d7b3dSmrg	if (a->x2 > b->x2)
40428d7b3dSmrg		a->x2 = b->x2;
41428d7b3dSmrg	if (a->x1 >= a->x2)
42428d7b3dSmrg		return false;
43428d7b3dSmrg
44428d7b3dSmrg	if (a->y1 < b->y1)
45428d7b3dSmrg		a->y1 = b->y1;
46428d7b3dSmrg	if (a->y2 > b->y2)
47428d7b3dSmrg		a->y2 = b->y2;
48428d7b3dSmrg	if (a->y1 >= a->y2)
49428d7b3dSmrg		return false;
50428d7b3dSmrg
51428d7b3dSmrg	return true;
52428d7b3dSmrg}
53428d7b3dSmrg
54428d7b3dSmrg#define run_box(b, c) \
55428d7b3dSmrg	DBG(("%s: box=(%d, %d), (%d, %d), clip=(%d, %d), (%d, %d)\n", \
56428d7b3dSmrg	     __FUNCTION__, (b)->x1, (b)->y1, (b)->x2, (b)->y2, (c)->x1, (c)->y1, (c)->x2, (c)->y2)); \
57428d7b3dSmrg	if ((b)->y2 <= (c)->y1) break; \
58428d7b3dSmrg	if ((b)->x1 >= (c)->x2) continue; \
59428d7b3dSmrg	if ((b)->x2 <= (c)->x1) { if ((b)->y2 <= (c)->y2) break; continue; }
60428d7b3dSmrg
61428d7b3dSmrgstatic inline void
62428d7b3dSmrgfbDrawableRun(DrawablePtr d, GCPtr gc, const BoxRec *box,
63428d7b3dSmrg	      void (*func)(DrawablePtr, GCPtr, const BoxRec *b, void *data),
64428d7b3dSmrg	      void *data)
65428d7b3dSmrg{
66428d7b3dSmrg	const BoxRec *c, *end;
67428d7b3dSmrg	for (c = fbClipBoxes(gc->pCompositeClip, box, &end); c != end; c++) {
68428d7b3dSmrg		BoxRec b;
69428d7b3dSmrg
70428d7b3dSmrg		run_box(box, c);
71428d7b3dSmrg
72428d7b3dSmrg		b = *box;
73428d7b3dSmrg		if (box_intersect(&b, c))
74428d7b3dSmrg			func(d, gc, &b, data);
75428d7b3dSmrg	}
76428d7b3dSmrg}
77428d7b3dSmrg
78428d7b3dSmrgstatic inline void
79428d7b3dSmrgfbDrawableRunUnclipped(DrawablePtr d, GCPtr gc, const BoxRec *box,
80428d7b3dSmrg		       void (*func)(DrawablePtr, GCPtr, const BoxRec *b, void *data),
81428d7b3dSmrg		       void *data)
82428d7b3dSmrg{
83428d7b3dSmrg	const BoxRec *c, *end;
84428d7b3dSmrg	for (c = fbClipBoxes(gc->pCompositeClip, box, &end); c != end; c++) {
85428d7b3dSmrg		run_box(box, c);
86428d7b3dSmrg		func(d, gc, c, data);
87428d7b3dSmrg	}
88428d7b3dSmrg}
89428d7b3dSmrg
90428d7b3dSmrg#endif /* FBCLIP_H */
91