blob: 22091ecdbff4282ab5756684630a4f1373dec97b [file] [log] [blame]
Ville Syrjälä3512f972013-04-24 18:52:34 +03001/*
2 * Copyright (C) 2011-2013 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24#include <linux/errno.h>
25#include <linux/export.h>
26#include <linux/kernel.h>
27#include <drm/drm_rect.h>
28
29/**
30 * drm_rect_intersect - intersect two rectangles
31 * @r1: first rectangle
32 * @r2: second rectangle
33 *
34 * Calculate the intersection of rectangles @r1 and @r2.
35 * @r1 will be overwritten with the intersection.
36 *
37 * RETURNS:
38 * %true if rectangle @r1 is still visible after the operation,
39 * %false otherwise.
40 */
41bool drm_rect_intersect(struct drm_rect *r1, const struct drm_rect *r2)
42{
43 r1->x1 = max(r1->x1, r2->x1);
44 r1->y1 = max(r1->y1, r2->y1);
45 r1->x2 = min(r1->x2, r2->x2);
46 r1->y2 = min(r1->y2, r2->y2);
47
48 return drm_rect_visible(r1);
49}
50EXPORT_SYMBOL(drm_rect_intersect);
51
52/**
53 * drm_rect_clip_scaled - perform a scaled clip operation
54 * @src: source window rectangle
55 * @dst: destination window rectangle
56 * @clip: clip rectangle
57 * @hscale: horizontal scaling factor
58 * @vscale: vertical scaling factor
59 *
60 * Clip rectangle @dst by rectangle @clip. Clip rectangle @src by the
61 * same amounts multiplied by @hscale and @vscale.
62 *
63 * RETURNS:
64 * %true if rectangle @dst is still visible after being clipped,
65 * %false otherwise
66 */
67bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst,
68 const struct drm_rect *clip,
69 int hscale, int vscale)
70{
71 int diff;
72
73 diff = clip->x1 - dst->x1;
74 if (diff > 0) {
75 int64_t tmp = src->x1 + (int64_t) diff * hscale;
76 src->x1 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
77 }
78 diff = clip->y1 - dst->y1;
79 if (diff > 0) {
80 int64_t tmp = src->y1 + (int64_t) diff * vscale;
81 src->y1 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
82 }
83 diff = dst->x2 - clip->x2;
84 if (diff > 0) {
85 int64_t tmp = src->x2 - (int64_t) diff * hscale;
86 src->x2 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
87 }
88 diff = dst->y2 - clip->y2;
89 if (diff > 0) {
90 int64_t tmp = src->y2 - (int64_t) diff * vscale;
91 src->y2 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
92 }
93
94 return drm_rect_intersect(dst, clip);
95}
96EXPORT_SYMBOL(drm_rect_clip_scaled);