root/drivers/gpu/drm/drm_rect.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. drm_rect_intersect
  2. clip_scaled
  3. drm_rect_clip_scaled
  4. drm_calc_scale
  5. drm_rect_calc_hscale
  6. drm_rect_calc_vscale
  7. drm_rect_debug_print
  8. drm_rect_rotate
  9. drm_rect_rotate_inv

   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 
  28 #include <drm/drm_mode.h>
  29 #include <drm/drm_print.h>
  30 #include <drm/drm_rect.h>
  31 
  32 /**
  33  * drm_rect_intersect - intersect two rectangles
  34  * @r1: first rectangle
  35  * @r2: second rectangle
  36  *
  37  * Calculate the intersection of rectangles @r1 and @r2.
  38  * @r1 will be overwritten with the intersection.
  39  *
  40  * RETURNS:
  41  * %true if rectangle @r1 is still visible after the operation,
  42  * %false otherwise.
  43  */
  44 bool drm_rect_intersect(struct drm_rect *r1, const struct drm_rect *r2)
  45 {
  46         r1->x1 = max(r1->x1, r2->x1);
  47         r1->y1 = max(r1->y1, r2->y1);
  48         r1->x2 = min(r1->x2, r2->x2);
  49         r1->y2 = min(r1->y2, r2->y2);
  50 
  51         return drm_rect_visible(r1);
  52 }
  53 EXPORT_SYMBOL(drm_rect_intersect);
  54 
  55 static u32 clip_scaled(u32 src, u32 dst, u32 clip)
  56 {
  57         u64 tmp;
  58 
  59         if (dst == 0)
  60                 return 0;
  61 
  62         tmp = mul_u32_u32(src, dst - clip);
  63 
  64         /*
  65          * Round toward 1.0 when clipping so that we don't accidentally
  66          * change upscaling to downscaling or vice versa.
  67          */
  68         if (src < (dst << 16))
  69                 return DIV_ROUND_UP_ULL(tmp, dst);
  70         else
  71                 return DIV_ROUND_DOWN_ULL(tmp, dst);
  72 }
  73 
  74 /**
  75  * drm_rect_clip_scaled - perform a scaled clip operation
  76  * @src: source window rectangle
  77  * @dst: destination window rectangle
  78  * @clip: clip rectangle
  79  *
  80  * Clip rectangle @dst by rectangle @clip. Clip rectangle @src by the
  81  * same amounts multiplied by @hscale and @vscale.
  82  *
  83  * RETURNS:
  84  * %true if rectangle @dst is still visible after being clipped,
  85  * %false otherwise
  86  */
  87 bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst,
  88                           const struct drm_rect *clip)
  89 {
  90         int diff;
  91 
  92         diff = clip->x1 - dst->x1;
  93         if (diff > 0) {
  94                 u32 new_src_w = clip_scaled(drm_rect_width(src),
  95                                             drm_rect_width(dst), diff);
  96 
  97                 src->x1 = clamp_t(int64_t, src->x2 - new_src_w, INT_MIN, INT_MAX);
  98                 dst->x1 = clip->x1;
  99         }
 100         diff = clip->y1 - dst->y1;
 101         if (diff > 0) {
 102                 u32 new_src_h = clip_scaled(drm_rect_height(src),
 103                                             drm_rect_height(dst), diff);
 104 
 105                 src->y1 = clamp_t(int64_t, src->y2 - new_src_h, INT_MIN, INT_MAX);
 106                 dst->y1 = clip->y1;
 107         }
 108         diff = dst->x2 - clip->x2;
 109         if (diff > 0) {
 110                 u32 new_src_w = clip_scaled(drm_rect_width(src),
 111                                             drm_rect_width(dst), diff);
 112 
 113                 src->x2 = clamp_t(int64_t, src->x1 + new_src_w, INT_MIN, INT_MAX);
 114                 dst->x2 = clip->x2;
 115         }
 116         diff = dst->y2 - clip->y2;
 117         if (diff > 0) {
 118                 u32 new_src_h = clip_scaled(drm_rect_height(src),
 119                                             drm_rect_height(dst), diff);
 120 
 121                 src->y2 = clamp_t(int64_t, src->y1 + new_src_h, INT_MIN, INT_MAX);
 122                 dst->y2 = clip->y2;
 123         }
 124 
 125         return drm_rect_visible(dst);
 126 }
 127 EXPORT_SYMBOL(drm_rect_clip_scaled);
 128 
 129 static int drm_calc_scale(int src, int dst)
 130 {
 131         int scale = 0;
 132 
 133         if (WARN_ON(src < 0 || dst < 0))
 134                 return -EINVAL;
 135 
 136         if (dst == 0)
 137                 return 0;
 138 
 139         if (src > (dst << 16))
 140                 return DIV_ROUND_UP(src, dst);
 141         else
 142                 scale = src / dst;
 143 
 144         return scale;
 145 }
 146 
 147 /**
 148  * drm_rect_calc_hscale - calculate the horizontal scaling factor
 149  * @src: source window rectangle
 150  * @dst: destination window rectangle
 151  * @min_hscale: minimum allowed horizontal scaling factor
 152  * @max_hscale: maximum allowed horizontal scaling factor
 153  *
 154  * Calculate the horizontal scaling factor as
 155  * (@src width) / (@dst width).
 156  *
 157  * If the scale is below 1 << 16, round down. If the scale is above
 158  * 1 << 16, round up. This will calculate the scale with the most
 159  * pessimistic limit calculation.
 160  *
 161  * RETURNS:
 162  * The horizontal scaling factor, or errno of out of limits.
 163  */
 164 int drm_rect_calc_hscale(const struct drm_rect *src,
 165                          const struct drm_rect *dst,
 166                          int min_hscale, int max_hscale)
 167 {
 168         int src_w = drm_rect_width(src);
 169         int dst_w = drm_rect_width(dst);
 170         int hscale = drm_calc_scale(src_w, dst_w);
 171 
 172         if (hscale < 0 || dst_w == 0)
 173                 return hscale;
 174 
 175         if (hscale < min_hscale || hscale > max_hscale)
 176                 return -ERANGE;
 177 
 178         return hscale;
 179 }
 180 EXPORT_SYMBOL(drm_rect_calc_hscale);
 181 
 182 /**
 183  * drm_rect_calc_vscale - calculate the vertical scaling factor
 184  * @src: source window rectangle
 185  * @dst: destination window rectangle
 186  * @min_vscale: minimum allowed vertical scaling factor
 187  * @max_vscale: maximum allowed vertical scaling factor
 188  *
 189  * Calculate the vertical scaling factor as
 190  * (@src height) / (@dst height).
 191  *
 192  * If the scale is below 1 << 16, round down. If the scale is above
 193  * 1 << 16, round up. This will calculate the scale with the most
 194  * pessimistic limit calculation.
 195  *
 196  * RETURNS:
 197  * The vertical scaling factor, or errno of out of limits.
 198  */
 199 int drm_rect_calc_vscale(const struct drm_rect *src,
 200                          const struct drm_rect *dst,
 201                          int min_vscale, int max_vscale)
 202 {
 203         int src_h = drm_rect_height(src);
 204         int dst_h = drm_rect_height(dst);
 205         int vscale = drm_calc_scale(src_h, dst_h);
 206 
 207         if (vscale < 0 || dst_h == 0)
 208                 return vscale;
 209 
 210         if (vscale < min_vscale || vscale > max_vscale)
 211                 return -ERANGE;
 212 
 213         return vscale;
 214 }
 215 EXPORT_SYMBOL(drm_rect_calc_vscale);
 216 
 217 /**
 218  * drm_rect_debug_print - print the rectangle information
 219  * @prefix: prefix string
 220  * @r: rectangle to print
 221  * @fixed_point: rectangle is in 16.16 fixed point format
 222  */
 223 void drm_rect_debug_print(const char *prefix, const struct drm_rect *r, bool fixed_point)
 224 {
 225         if (fixed_point)
 226                 DRM_DEBUG_KMS("%s" DRM_RECT_FP_FMT "\n", prefix, DRM_RECT_FP_ARG(r));
 227         else
 228                 DRM_DEBUG_KMS("%s" DRM_RECT_FMT "\n", prefix, DRM_RECT_ARG(r));
 229 }
 230 EXPORT_SYMBOL(drm_rect_debug_print);
 231 
 232 /**
 233  * drm_rect_rotate - Rotate the rectangle
 234  * @r: rectangle to be rotated
 235  * @width: Width of the coordinate space
 236  * @height: Height of the coordinate space
 237  * @rotation: Transformation to be applied
 238  *
 239  * Apply @rotation to the coordinates of rectangle @r.
 240  *
 241  * @width and @height combined with @rotation define
 242  * the location of the new origin.
 243  *
 244  * @width correcsponds to the horizontal and @height
 245  * to the vertical axis of the untransformed coordinate
 246  * space.
 247  */
 248 void drm_rect_rotate(struct drm_rect *r,
 249                      int width, int height,
 250                      unsigned int rotation)
 251 {
 252         struct drm_rect tmp;
 253 
 254         if (rotation & (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y)) {
 255                 tmp = *r;
 256 
 257                 if (rotation & DRM_MODE_REFLECT_X) {
 258                         r->x1 = width - tmp.x2;
 259                         r->x2 = width - tmp.x1;
 260                 }
 261 
 262                 if (rotation & DRM_MODE_REFLECT_Y) {
 263                         r->y1 = height - tmp.y2;
 264                         r->y2 = height - tmp.y1;
 265                 }
 266         }
 267 
 268         switch (rotation & DRM_MODE_ROTATE_MASK) {
 269         case DRM_MODE_ROTATE_0:
 270                 break;
 271         case DRM_MODE_ROTATE_90:
 272                 tmp = *r;
 273                 r->x1 = tmp.y1;
 274                 r->x2 = tmp.y2;
 275                 r->y1 = width - tmp.x2;
 276                 r->y2 = width - tmp.x1;
 277                 break;
 278         case DRM_MODE_ROTATE_180:
 279                 tmp = *r;
 280                 r->x1 = width - tmp.x2;
 281                 r->x2 = width - tmp.x1;
 282                 r->y1 = height - tmp.y2;
 283                 r->y2 = height - tmp.y1;
 284                 break;
 285         case DRM_MODE_ROTATE_270:
 286                 tmp = *r;
 287                 r->x1 = height - tmp.y2;
 288                 r->x2 = height - tmp.y1;
 289                 r->y1 = tmp.x1;
 290                 r->y2 = tmp.x2;
 291                 break;
 292         default:
 293                 break;
 294         }
 295 }
 296 EXPORT_SYMBOL(drm_rect_rotate);
 297 
 298 /**
 299  * drm_rect_rotate_inv - Inverse rotate the rectangle
 300  * @r: rectangle to be rotated
 301  * @width: Width of the coordinate space
 302  * @height: Height of the coordinate space
 303  * @rotation: Transformation whose inverse is to be applied
 304  *
 305  * Apply the inverse of @rotation to the coordinates
 306  * of rectangle @r.
 307  *
 308  * @width and @height combined with @rotation define
 309  * the location of the new origin.
 310  *
 311  * @width correcsponds to the horizontal and @height
 312  * to the vertical axis of the original untransformed
 313  * coordinate space, so that you never have to flip
 314  * them when doing a rotatation and its inverse.
 315  * That is, if you do ::
 316  *
 317  *     drm_rect_rotate(&r, width, height, rotation);
 318  *     drm_rect_rotate_inv(&r, width, height, rotation);
 319  *
 320  * you will always get back the original rectangle.
 321  */
 322 void drm_rect_rotate_inv(struct drm_rect *r,
 323                          int width, int height,
 324                          unsigned int rotation)
 325 {
 326         struct drm_rect tmp;
 327 
 328         switch (rotation & DRM_MODE_ROTATE_MASK) {
 329         case DRM_MODE_ROTATE_0:
 330                 break;
 331         case DRM_MODE_ROTATE_90:
 332                 tmp = *r;
 333                 r->x1 = width - tmp.y2;
 334                 r->x2 = width - tmp.y1;
 335                 r->y1 = tmp.x1;
 336                 r->y2 = tmp.x2;
 337                 break;
 338         case DRM_MODE_ROTATE_180:
 339                 tmp = *r;
 340                 r->x1 = width - tmp.x2;
 341                 r->x2 = width - tmp.x1;
 342                 r->y1 = height - tmp.y2;
 343                 r->y2 = height - tmp.y1;
 344                 break;
 345         case DRM_MODE_ROTATE_270:
 346                 tmp = *r;
 347                 r->x1 = tmp.y1;
 348                 r->x2 = tmp.y2;
 349                 r->y1 = height - tmp.x2;
 350                 r->y2 = height - tmp.x1;
 351                 break;
 352         default:
 353                 break;
 354         }
 355 
 356         if (rotation & (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y)) {
 357                 tmp = *r;
 358 
 359                 if (rotation & DRM_MODE_REFLECT_X) {
 360                         r->x1 = width - tmp.x2;
 361                         r->x2 = width - tmp.x1;
 362                 }
 363 
 364                 if (rotation & DRM_MODE_REFLECT_Y) {
 365                         r->y1 = height - tmp.y2;
 366                         r->y2 = height - tmp.y1;
 367                 }
 368         }
 369 }
 370 EXPORT_SYMBOL(drm_rect_rotate_inv);

/* [<][>][^][v][top][bottom][index][help] */