Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Ville Syrjälä and committed by
Daniel Vetter
3512f976 5a80c45c

+232 -1
+2
Documentation/DocBook/drm.tmpl
··· 1653 1653 <sect2> 1654 1654 <title>KMS API Functions</title> 1655 1655 !Edrivers/gpu/drm/drm_crtc.c 1656 + !Edrivers/gpu/drm/drm_rect.c 1657 + !Finclude/drm/drm_rect.h 1656 1658 </sect2> 1657 1659 </sect1> 1658 1660
+2 -1
drivers/gpu/drm/Makefile
··· 12 12 drm_platform.o drm_sysfs.o drm_hashtab.o drm_mm.o \ 13 13 drm_crtc.o drm_modes.o drm_edid.o \ 14 14 drm_info.o drm_debugfs.o drm_encoder_slave.o \ 15 - drm_trace_points.o drm_global.o drm_prime.o 15 + drm_trace_points.o drm_global.o drm_prime.o \ 16 + drm_rect.o 16 17 17 18 drm-$(CONFIG_COMPAT) += drm_ioc32.o 18 19 drm-$(CONFIG_DRM_GEM_CMA_HELPER) += drm_gem_cma_helper.o
+96
drivers/gpu/drm/drm_rect.c
··· 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 + */ 41 + bool 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 + } 50 + EXPORT_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 + */ 67 + bool 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 + } 96 + EXPORT_SYMBOL(drm_rect_clip_scaled);
+132
include/drm/drm_rect.h
··· 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 + #ifndef DRM_RECT_H 25 + #define DRM_RECT_H 26 + 27 + /** 28 + * drm_rect - two dimensional rectangle 29 + * @x1: horizontal starting coordinate (inclusive) 30 + * @x2: horizontal ending coordinate (exclusive) 31 + * @y1: vertical starting coordinate (inclusive) 32 + * @y2: vertical ending coordinate (exclusive) 33 + */ 34 + struct drm_rect { 35 + int x1, y1, x2, y2; 36 + }; 37 + 38 + /** 39 + * drm_rect_adjust_size - adjust the size of the rectangle 40 + * @r: rectangle to be adjusted 41 + * @dw: horizontal adjustment 42 + * @dh: vertical adjustment 43 + * 44 + * Change the size of rectangle @r by @dw in the horizontal direction, 45 + * and by @dh in the vertical direction, while keeping the center 46 + * of @r stationary. 47 + * 48 + * Positive @dw and @dh increase the size, negative values decrease it. 49 + */ 50 + static inline void drm_rect_adjust_size(struct drm_rect *r, int dw, int dh) 51 + { 52 + r->x1 -= dw >> 1; 53 + r->y1 -= dh >> 1; 54 + r->x2 += (dw + 1) >> 1; 55 + r->y2 += (dh + 1) >> 1; 56 + } 57 + 58 + /** 59 + * drm_rect_translate - translate the rectangle 60 + * @r: rectangle to be tranlated 61 + * @dx: horizontal translation 62 + * @dy: vertical translation 63 + * 64 + * Move rectangle @r by @dx in the horizontal direction, 65 + * and by @dy in the vertical direction. 66 + */ 67 + static inline void drm_rect_translate(struct drm_rect *r, int dx, int dy) 68 + { 69 + r->x1 += dx; 70 + r->y1 += dy; 71 + r->x2 += dx; 72 + r->y2 += dy; 73 + } 74 + 75 + /** 76 + * drm_rect_downscale - downscale a rectangle 77 + * @r: rectangle to be downscaled 78 + * @horz: horizontal downscale factor 79 + * @vert: vertical downscale factor 80 + * 81 + * Divide the coordinates of rectangle @r by @horz and @vert. 82 + */ 83 + static inline void drm_rect_downscale(struct drm_rect *r, int horz, int vert) 84 + { 85 + r->x1 /= horz; 86 + r->y1 /= vert; 87 + r->x2 /= horz; 88 + r->y2 /= vert; 89 + } 90 + 91 + /** 92 + * drm_rect_width - determine the rectangle width 93 + * @r: rectangle whose width is returned 94 + * 95 + * RETURNS: 96 + * The width of the rectangle. 97 + */ 98 + static inline int drm_rect_width(const struct drm_rect *r) 99 + { 100 + return r->x2 - r->x1; 101 + } 102 + 103 + /** 104 + * drm_rect_height - determine the rectangle height 105 + * @r: rectangle whose height is returned 106 + * 107 + * RETURNS: 108 + * The height of the rectangle. 109 + */ 110 + static inline int drm_rect_height(const struct drm_rect *r) 111 + { 112 + return r->y2 - r->y1; 113 + } 114 + 115 + /** 116 + * drm_rect_visible - determine if the the rectangle is visible 117 + * @r: rectangle whose visibility is returned 118 + * 119 + * RETURNS: 120 + * %true if the rectangle is visible, %false otherwise. 121 + */ 122 + static inline bool drm_rect_visible(const struct drm_rect *r) 123 + { 124 + return drm_rect_width(r) > 0 && drm_rect_height(r) > 0; 125 + } 126 + 127 + bool drm_rect_intersect(struct drm_rect *r, const struct drm_rect *clip); 128 + bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst, 129 + const struct drm_rect *clip, 130 + int hscale, int vscale); 131 + 132 + #endif