103b705cfSriastradh/*
203b705cfSriastradh * Copyright © 2012 Intel Corporation
303b705cfSriastradh *
403b705cfSriastradh * Permission is hereby granted, free of charge, to any person obtaining a
503b705cfSriastradh * copy of this software and associated documentation files (the "Software"),
603b705cfSriastradh * to deal in the Software without restriction, including without limitation
703b705cfSriastradh * the rights to use, copy, modify, merge, publish, distribute, sublicense,
803b705cfSriastradh * and/or sell copies of the Software, and to permit persons to whom the
903b705cfSriastradh * Software is furnished to do so, subject to the following conditions:
1003b705cfSriastradh *
1103b705cfSriastradh * The above copyright notice and this permission notice (including the next
1203b705cfSriastradh * paragraph) shall be included in all copies or substantial portions of the
1303b705cfSriastradh * Software.
1403b705cfSriastradh *
1503b705cfSriastradh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1603b705cfSriastradh * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1703b705cfSriastradh * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1803b705cfSriastradh * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1903b705cfSriastradh * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2003b705cfSriastradh * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2103b705cfSriastradh * SOFTWARE.
2203b705cfSriastradh *
2303b705cfSriastradh * Authors:
2403b705cfSriastradh *    Chris Wilson <chris@chris-wilson.co.uk>
2503b705cfSriastradh *
2603b705cfSriastradh */
2703b705cfSriastradh
2803b705cfSriastradh#include "fb.h"
2903b705cfSriastradh#include "fbclip.h"
3003b705cfSriastradh
3103b705cfSriastradhstatic const BoxRec *
3203b705cfSriastradhfind_clip_row_for_y(const BoxRec *begin, const BoxRec *end, int16_t y)
3303b705cfSriastradh{
3403b705cfSriastradh	const BoxRec *mid;
3503b705cfSriastradh
3603b705cfSriastradh	if (end == begin)
3703b705cfSriastradh		return end;
3803b705cfSriastradh
3903b705cfSriastradh	if (end - begin == 1) {
4003b705cfSriastradh		if (begin->y2 > y)
4103b705cfSriastradh			return begin;
4203b705cfSriastradh		else
4303b705cfSriastradh			return end;
4403b705cfSriastradh	}
4503b705cfSriastradh
4603b705cfSriastradh	mid = begin + (end - begin) / 2;
4703b705cfSriastradh	if (mid->y2 > y)
4803b705cfSriastradh		return find_clip_row_for_y(begin, mid, y);
4903b705cfSriastradh	else
5003b705cfSriastradh		return find_clip_row_for_y(mid, end, y);
5103b705cfSriastradh}
5203b705cfSriastradh
5303b705cfSriastradhconst BoxRec *
5403b705cfSriastradhfbClipBoxes(const RegionRec *region, const BoxRec *box, const BoxRec **end)
5503b705cfSriastradh{
5603b705cfSriastradh	const BoxRec *c0, *c1;
5703b705cfSriastradh
5803b705cfSriastradh	DBG(("%s: box=(%d, %d),(%d, %d); region=(%d, %d),(%d, %d) x %ld\n",
5903b705cfSriastradh	     __FUNCTION__,
6003b705cfSriastradh	     box->x1, box->y1, box->x2, box->y2,
6103b705cfSriastradh	     region->extents.x1, region->extents.y1,
6203b705cfSriastradh	     region->extents.x2, region->extents.y2,
6303b705cfSriastradh	     region->data ? region->data->numRects : 1));
6403b705cfSriastradh
6503b705cfSriastradh	if (box->x1 >= region->extents.x2 || box->x2 <= region->extents.x1 ||
6603b705cfSriastradh	    box->y1 >= region->extents.y2 || box->y2 <= region->extents.y1) {
6703b705cfSriastradh		DBG(("%s: no intersection\n", __FUNCTION__));
6803b705cfSriastradh		return *end = box;
6903b705cfSriastradh	}
7003b705cfSriastradh
7103b705cfSriastradh	if (region->data == NULL) {
7203b705cfSriastradh		*end = &region->extents + 1;
7303b705cfSriastradh		return &region->extents;
7403b705cfSriastradh	}
7503b705cfSriastradh
7603b705cfSriastradh	c0 = (const BoxRec *)(region->data + 1);
7703b705cfSriastradh	c1 = c0 + region->data->numRects;
7803b705cfSriastradh
7903b705cfSriastradh	if (c0->y2 <= box->y1) {
8003b705cfSriastradh		DBG(("%s: first clip (%d, %d), (%d, %d) before box (%d, %d), (%d, %d)\n",
8103b705cfSriastradh		     __FUNCTION__,
8203b705cfSriastradh		     c0->x1, c0->y1, c0->x2, c0->y2,
8303b705cfSriastradh		     box->x1, box->y1, box->x2, box->y2));
8403b705cfSriastradh		c0 = find_clip_row_for_y(c0, c1, box->y1);
8503b705cfSriastradh	}
8603b705cfSriastradh
8703b705cfSriastradh	DBG(("%s: c0=(%d, %d),(%d, %d) x %ld\n",
8842542f5fSchristos	     __FUNCTION__, c0->x1, c0->y1, c0->x2, c0->y2, (long)(c1 - c0)));
8903b705cfSriastradh
9003b705cfSriastradh	*end = c1;
9103b705cfSriastradh	return c0;
9203b705cfSriastradh}
93