Home | History | Annotate | Line # | Download | only in drm
drm_rect.c revision 1.1.1.1
      1  1.1  riastrad /*
      2  1.1  riastrad  * Copyright (C) 2011-2013 Intel Corporation
      3  1.1  riastrad  *
      4  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      5  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      6  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      7  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      8  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
      9  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     10  1.1  riastrad  *
     11  1.1  riastrad  * The above copyright notice and this permission notice (including the next
     12  1.1  riastrad  * paragraph) shall be included in all copies or substantial portions of the
     13  1.1  riastrad  * Software.
     14  1.1  riastrad  *
     15  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     18  1.1  riastrad  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19  1.1  riastrad  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     20  1.1  riastrad  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     21  1.1  riastrad  * SOFTWARE.
     22  1.1  riastrad  */
     23  1.1  riastrad 
     24  1.1  riastrad #include <linux/errno.h>
     25  1.1  riastrad #include <linux/export.h>
     26  1.1  riastrad #include <linux/kernel.h>
     27  1.1  riastrad #include <drm/drmP.h>
     28  1.1  riastrad #include <drm/drm_rect.h>
     29  1.1  riastrad 
     30  1.1  riastrad /**
     31  1.1  riastrad  * drm_rect_intersect - intersect two rectangles
     32  1.1  riastrad  * @r1: first rectangle
     33  1.1  riastrad  * @r2: second rectangle
     34  1.1  riastrad  *
     35  1.1  riastrad  * Calculate the intersection of rectangles @r1 and @r2.
     36  1.1  riastrad  * @r1 will be overwritten with the intersection.
     37  1.1  riastrad  *
     38  1.1  riastrad  * RETURNS:
     39  1.1  riastrad  * %true if rectangle @r1 is still visible after the operation,
     40  1.1  riastrad  * %false otherwise.
     41  1.1  riastrad  */
     42  1.1  riastrad bool drm_rect_intersect(struct drm_rect *r1, const struct drm_rect *r2)
     43  1.1  riastrad {
     44  1.1  riastrad 	r1->x1 = max(r1->x1, r2->x1);
     45  1.1  riastrad 	r1->y1 = max(r1->y1, r2->y1);
     46  1.1  riastrad 	r1->x2 = min(r1->x2, r2->x2);
     47  1.1  riastrad 	r1->y2 = min(r1->y2, r2->y2);
     48  1.1  riastrad 
     49  1.1  riastrad 	return drm_rect_visible(r1);
     50  1.1  riastrad }
     51  1.1  riastrad EXPORT_SYMBOL(drm_rect_intersect);
     52  1.1  riastrad 
     53  1.1  riastrad /**
     54  1.1  riastrad  * drm_rect_clip_scaled - perform a scaled clip operation
     55  1.1  riastrad  * @src: source window rectangle
     56  1.1  riastrad  * @dst: destination window rectangle
     57  1.1  riastrad  * @clip: clip rectangle
     58  1.1  riastrad  * @hscale: horizontal scaling factor
     59  1.1  riastrad  * @vscale: vertical scaling factor
     60  1.1  riastrad  *
     61  1.1  riastrad  * Clip rectangle @dst by rectangle @clip. Clip rectangle @src by the
     62  1.1  riastrad  * same amounts multiplied by @hscale and @vscale.
     63  1.1  riastrad  *
     64  1.1  riastrad  * RETURNS:
     65  1.1  riastrad  * %true if rectangle @dst is still visible after being clipped,
     66  1.1  riastrad  * %false otherwise
     67  1.1  riastrad  */
     68  1.1  riastrad bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst,
     69  1.1  riastrad 			  const struct drm_rect *clip,
     70  1.1  riastrad 			  int hscale, int vscale)
     71  1.1  riastrad {
     72  1.1  riastrad 	int diff;
     73  1.1  riastrad 
     74  1.1  riastrad 	diff = clip->x1 - dst->x1;
     75  1.1  riastrad 	if (diff > 0) {
     76  1.1  riastrad 		int64_t tmp = src->x1 + (int64_t) diff * hscale;
     77  1.1  riastrad 		src->x1 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
     78  1.1  riastrad 	}
     79  1.1  riastrad 	diff = clip->y1 - dst->y1;
     80  1.1  riastrad 	if (diff > 0) {
     81  1.1  riastrad 		int64_t tmp = src->y1 + (int64_t) diff * vscale;
     82  1.1  riastrad 		src->y1 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
     83  1.1  riastrad 	}
     84  1.1  riastrad 	diff = dst->x2 - clip->x2;
     85  1.1  riastrad 	if (diff > 0) {
     86  1.1  riastrad 		int64_t tmp = src->x2 - (int64_t) diff * hscale;
     87  1.1  riastrad 		src->x2 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
     88  1.1  riastrad 	}
     89  1.1  riastrad 	diff = dst->y2 - clip->y2;
     90  1.1  riastrad 	if (diff > 0) {
     91  1.1  riastrad 		int64_t tmp = src->y2 - (int64_t) diff * vscale;
     92  1.1  riastrad 		src->y2 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
     93  1.1  riastrad 	}
     94  1.1  riastrad 
     95  1.1  riastrad 	return drm_rect_intersect(dst, clip);
     96  1.1  riastrad }
     97  1.1  riastrad EXPORT_SYMBOL(drm_rect_clip_scaled);
     98  1.1  riastrad 
     99  1.1  riastrad static int drm_calc_scale(int src, int dst)
    100  1.1  riastrad {
    101  1.1  riastrad 	int scale = 0;
    102  1.1  riastrad 
    103  1.1  riastrad 	if (src < 0 || dst < 0)
    104  1.1  riastrad 		return -EINVAL;
    105  1.1  riastrad 
    106  1.1  riastrad 	if (dst == 0)
    107  1.1  riastrad 		return 0;
    108  1.1  riastrad 
    109  1.1  riastrad 	scale = src / dst;
    110  1.1  riastrad 
    111  1.1  riastrad 	return scale;
    112  1.1  riastrad }
    113  1.1  riastrad 
    114  1.1  riastrad /**
    115  1.1  riastrad  * drm_rect_calc_hscale - calculate the horizontal scaling factor
    116  1.1  riastrad  * @src: source window rectangle
    117  1.1  riastrad  * @dst: destination window rectangle
    118  1.1  riastrad  * @min_hscale: minimum allowed horizontal scaling factor
    119  1.1  riastrad  * @max_hscale: maximum allowed horizontal scaling factor
    120  1.1  riastrad  *
    121  1.1  riastrad  * Calculate the horizontal scaling factor as
    122  1.1  riastrad  * (@src width) / (@dst width).
    123  1.1  riastrad  *
    124  1.1  riastrad  * RETURNS:
    125  1.1  riastrad  * The horizontal scaling factor, or errno of out of limits.
    126  1.1  riastrad  */
    127  1.1  riastrad int drm_rect_calc_hscale(const struct drm_rect *src,
    128  1.1  riastrad 			 const struct drm_rect *dst,
    129  1.1  riastrad 			 int min_hscale, int max_hscale)
    130  1.1  riastrad {
    131  1.1  riastrad 	int src_w = drm_rect_width(src);
    132  1.1  riastrad 	int dst_w = drm_rect_width(dst);
    133  1.1  riastrad 	int hscale = drm_calc_scale(src_w, dst_w);
    134  1.1  riastrad 
    135  1.1  riastrad 	if (hscale < 0 || dst_w == 0)
    136  1.1  riastrad 		return hscale;
    137  1.1  riastrad 
    138  1.1  riastrad 	if (hscale < min_hscale || hscale > max_hscale)
    139  1.1  riastrad 		return -ERANGE;
    140  1.1  riastrad 
    141  1.1  riastrad 	return hscale;
    142  1.1  riastrad }
    143  1.1  riastrad EXPORT_SYMBOL(drm_rect_calc_hscale);
    144  1.1  riastrad 
    145  1.1  riastrad /**
    146  1.1  riastrad  * drm_rect_calc_vscale - calculate the vertical scaling factor
    147  1.1  riastrad  * @src: source window rectangle
    148  1.1  riastrad  * @dst: destination window rectangle
    149  1.1  riastrad  * @min_vscale: minimum allowed vertical scaling factor
    150  1.1  riastrad  * @max_vscale: maximum allowed vertical scaling factor
    151  1.1  riastrad  *
    152  1.1  riastrad  * Calculate the vertical scaling factor as
    153  1.1  riastrad  * (@src height) / (@dst height).
    154  1.1  riastrad  *
    155  1.1  riastrad  * RETURNS:
    156  1.1  riastrad  * The vertical scaling factor, or errno of out of limits.
    157  1.1  riastrad  */
    158  1.1  riastrad int drm_rect_calc_vscale(const struct drm_rect *src,
    159  1.1  riastrad 			 const struct drm_rect *dst,
    160  1.1  riastrad 			 int min_vscale, int max_vscale)
    161  1.1  riastrad {
    162  1.1  riastrad 	int src_h = drm_rect_height(src);
    163  1.1  riastrad 	int dst_h = drm_rect_height(dst);
    164  1.1  riastrad 	int vscale = drm_calc_scale(src_h, dst_h);
    165  1.1  riastrad 
    166  1.1  riastrad 	if (vscale < 0 || dst_h == 0)
    167  1.1  riastrad 		return vscale;
    168  1.1  riastrad 
    169  1.1  riastrad 	if (vscale < min_vscale || vscale > max_vscale)
    170  1.1  riastrad 		return -ERANGE;
    171  1.1  riastrad 
    172  1.1  riastrad 	return vscale;
    173  1.1  riastrad }
    174  1.1  riastrad EXPORT_SYMBOL(drm_rect_calc_vscale);
    175  1.1  riastrad 
    176  1.1  riastrad /**
    177  1.1  riastrad  * drm_calc_hscale_relaxed - calculate the horizontal scaling factor
    178  1.1  riastrad  * @src: source window rectangle
    179  1.1  riastrad  * @dst: destination window rectangle
    180  1.1  riastrad  * @min_hscale: minimum allowed horizontal scaling factor
    181  1.1  riastrad  * @max_hscale: maximum allowed horizontal scaling factor
    182  1.1  riastrad  *
    183  1.1  riastrad  * Calculate the horizontal scaling factor as
    184  1.1  riastrad  * (@src width) / (@dst width).
    185  1.1  riastrad  *
    186  1.1  riastrad  * If the calculated scaling factor is below @min_vscale,
    187  1.1  riastrad  * decrease the height of rectangle @dst to compensate.
    188  1.1  riastrad  *
    189  1.1  riastrad  * If the calculated scaling factor is above @max_vscale,
    190  1.1  riastrad  * decrease the height of rectangle @src to compensate.
    191  1.1  riastrad  *
    192  1.1  riastrad  * RETURNS:
    193  1.1  riastrad  * The horizontal scaling factor.
    194  1.1  riastrad  */
    195  1.1  riastrad int drm_rect_calc_hscale_relaxed(struct drm_rect *src,
    196  1.1  riastrad 				 struct drm_rect *dst,
    197  1.1  riastrad 				 int min_hscale, int max_hscale)
    198  1.1  riastrad {
    199  1.1  riastrad 	int src_w = drm_rect_width(src);
    200  1.1  riastrad 	int dst_w = drm_rect_width(dst);
    201  1.1  riastrad 	int hscale = drm_calc_scale(src_w, dst_w);
    202  1.1  riastrad 
    203  1.1  riastrad 	if (hscale < 0 || dst_w == 0)
    204  1.1  riastrad 		return hscale;
    205  1.1  riastrad 
    206  1.1  riastrad 	if (hscale < min_hscale) {
    207  1.1  riastrad 		int max_dst_w = src_w / min_hscale;
    208  1.1  riastrad 
    209  1.1  riastrad 		drm_rect_adjust_size(dst, max_dst_w - dst_w, 0);
    210  1.1  riastrad 
    211  1.1  riastrad 		return min_hscale;
    212  1.1  riastrad 	}
    213  1.1  riastrad 
    214  1.1  riastrad 	if (hscale > max_hscale) {
    215  1.1  riastrad 		int max_src_w = dst_w * max_hscale;
    216  1.1  riastrad 
    217  1.1  riastrad 		drm_rect_adjust_size(src, max_src_w - src_w, 0);
    218  1.1  riastrad 
    219  1.1  riastrad 		return max_hscale;
    220  1.1  riastrad 	}
    221  1.1  riastrad 
    222  1.1  riastrad 	return hscale;
    223  1.1  riastrad }
    224  1.1  riastrad EXPORT_SYMBOL(drm_rect_calc_hscale_relaxed);
    225  1.1  riastrad 
    226  1.1  riastrad /**
    227  1.1  riastrad  * drm_rect_calc_vscale_relaxed - calculate the vertical scaling factor
    228  1.1  riastrad  * @src: source window rectangle
    229  1.1  riastrad  * @dst: destination window rectangle
    230  1.1  riastrad  * @min_vscale: minimum allowed vertical scaling factor
    231  1.1  riastrad  * @max_vscale: maximum allowed vertical scaling factor
    232  1.1  riastrad  *
    233  1.1  riastrad  * Calculate the vertical scaling factor as
    234  1.1  riastrad  * (@src height) / (@dst height).
    235  1.1  riastrad  *
    236  1.1  riastrad  * If the calculated scaling factor is below @min_vscale,
    237  1.1  riastrad  * decrease the height of rectangle @dst to compensate.
    238  1.1  riastrad  *
    239  1.1  riastrad  * If the calculated scaling factor is above @max_vscale,
    240  1.1  riastrad  * decrease the height of rectangle @src to compensate.
    241  1.1  riastrad  *
    242  1.1  riastrad  * RETURNS:
    243  1.1  riastrad  * The vertical scaling factor.
    244  1.1  riastrad  */
    245  1.1  riastrad int drm_rect_calc_vscale_relaxed(struct drm_rect *src,
    246  1.1  riastrad 				 struct drm_rect *dst,
    247  1.1  riastrad 				 int min_vscale, int max_vscale)
    248  1.1  riastrad {
    249  1.1  riastrad 	int src_h = drm_rect_height(src);
    250  1.1  riastrad 	int dst_h = drm_rect_height(dst);
    251  1.1  riastrad 	int vscale = drm_calc_scale(src_h, dst_h);
    252  1.1  riastrad 
    253  1.1  riastrad 	if (vscale < 0 || dst_h == 0)
    254  1.1  riastrad 		return vscale;
    255  1.1  riastrad 
    256  1.1  riastrad 	if (vscale < min_vscale) {
    257  1.1  riastrad 		int max_dst_h = src_h / min_vscale;
    258  1.1  riastrad 
    259  1.1  riastrad 		drm_rect_adjust_size(dst, 0, max_dst_h - dst_h);
    260  1.1  riastrad 
    261  1.1  riastrad 		return min_vscale;
    262  1.1  riastrad 	}
    263  1.1  riastrad 
    264  1.1  riastrad 	if (vscale > max_vscale) {
    265  1.1  riastrad 		int max_src_h = dst_h * max_vscale;
    266  1.1  riastrad 
    267  1.1  riastrad 		drm_rect_adjust_size(src, 0, max_src_h - src_h);
    268  1.1  riastrad 
    269  1.1  riastrad 		return max_vscale;
    270  1.1  riastrad 	}
    271  1.1  riastrad 
    272  1.1  riastrad 	return vscale;
    273  1.1  riastrad }
    274  1.1  riastrad EXPORT_SYMBOL(drm_rect_calc_vscale_relaxed);
    275  1.1  riastrad 
    276  1.1  riastrad /**
    277  1.1  riastrad  * drm_rect_debug_print - print the rectangle information
    278  1.1  riastrad  * @r: rectangle to print
    279  1.1  riastrad  * @fixed_point: rectangle is in 16.16 fixed point format
    280  1.1  riastrad  */
    281  1.1  riastrad void drm_rect_debug_print(const struct drm_rect *r, bool fixed_point)
    282  1.1  riastrad {
    283  1.1  riastrad 	int w = drm_rect_width(r);
    284  1.1  riastrad 	int h = drm_rect_height(r);
    285  1.1  riastrad 
    286  1.1  riastrad 	if (fixed_point)
    287  1.1  riastrad 		DRM_DEBUG_KMS("%d.%06ux%d.%06u%+d.%06u%+d.%06u\n",
    288  1.1  riastrad 			      w >> 16, ((w & 0xffff) * 15625) >> 10,
    289  1.1  riastrad 			      h >> 16, ((h & 0xffff) * 15625) >> 10,
    290  1.1  riastrad 			      r->x1 >> 16, ((r->x1 & 0xffff) * 15625) >> 10,
    291  1.1  riastrad 			      r->y1 >> 16, ((r->y1 & 0xffff) * 15625) >> 10);
    292  1.1  riastrad 	else
    293  1.1  riastrad 		DRM_DEBUG_KMS("%dx%d%+d%+d\n", w, h, r->x1, r->y1);
    294  1.1  riastrad }
    295  1.1  riastrad EXPORT_SYMBOL(drm_rect_debug_print);
    296