aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/drm_rect.c
diff options
context:
space:
mode:
authorVille Syrjälä <ville.syrjala@linux.intel.com>2013-04-24 11:52:34 -0400
committerDaniel Vetter <daniel.vetter@ffwll.ch>2013-04-30 16:19:08 -0400
commit3512f976d252bd5d07d04e9e157f0cd210c959a0 (patch)
tree5541eb3fee53099e1ea416c959cbe1890ba93aa2 /drivers/gpu/drm/drm_rect.c
parent5a80c45c5297a025c2615624042ea8b6840a5376 (diff)
drm: Add struct drm_rect and assorted utility functions
struct drm_rect represents a simple rectangle. The utility functions are there to help driver writers. v2: Moved the region stuff into its own file, made the smaller funcs static inline, used 64bit maths in the scaled clipping function to avoid overflows (instead it will saturate to INT_MIN or INT_MAX). v3: Renamed drm_region to drm_rect, drm_region_clip to drm_rect_intersect, and drm_region_subsample to drm_rect_downscale. v4: Renamed some function parameters, improve kernel-doc comments a bit, and actually generate documentation for drm_rect.[ch]. v5: s/RETUTRNS/RETURNS/ Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Chris Wilson <chris@chris-wilson.co.uk> Signed-off-by: Ville Syrjälä <ville.syrjala@linux.intel.com> Acked-by: Dave Airlie <airlied@gmail.com> Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Diffstat (limited to 'drivers/gpu/drm/drm_rect.c')
-rw-r--r--drivers/gpu/drm/drm_rect.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/drivers/gpu/drm/drm_rect.c b/drivers/gpu/drm/drm_rect.c
new file mode 100644
index 000000000000..22091ecdbff4
--- /dev/null
+++ b/drivers/gpu/drm/drm_rect.c
@@ -0,0 +1,96 @@
1/*
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);