^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Copyright (C) 2011-2013 Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Permission is hereby granted, free of charge, to any person obtaining a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * copy of this software and associated documentation files (the "Software"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * to deal in the Software without restriction, including without limitation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * the rights to use, copy, modify, merge, publish, distribute, sublicense,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * and/or sell copies of the Software, and to permit persons to whom the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Software is furnished to do so, subject to the following conditions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * The above copyright notice and this permission notice (including the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * paragraph) shall be included in all copies or substantial portions of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Software.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * SOFTWARE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <drm/drm_mode.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <drm/drm_print.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <drm/drm_rect.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * drm_rect_intersect - intersect two rectangles
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * @r1: first rectangle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * @r2: second rectangle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * Calculate the intersection of rectangles @r1 and @r2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * @r1 will be overwritten with the intersection.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * %true if rectangle @r1 is still visible after the operation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * %false otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) bool drm_rect_intersect(struct drm_rect *r1, const struct drm_rect *r2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) r1->x1 = max(r1->x1, r2->x1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) r1->y1 = max(r1->y1, r2->y1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) r1->x2 = min(r1->x2, r2->x2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) r1->y2 = min(r1->y2, r2->y2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return drm_rect_visible(r1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) EXPORT_SYMBOL(drm_rect_intersect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static u32 clip_scaled(int src, int dst, int *clip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) u64 tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (dst == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /* Only clip what we have. Keeps the result bounded. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) *clip = min(*clip, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) tmp = mul_u32_u32(src, dst - *clip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * Round toward 1.0 when clipping so that we don't accidentally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * change upscaling to downscaling or vice versa.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (src < (dst << 16))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return DIV_ROUND_UP_ULL(tmp, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return DIV_ROUND_DOWN_ULL(tmp, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * drm_rect_clip_scaled - perform a scaled clip operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * @src: source window rectangle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * @dst: destination window rectangle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * @clip: clip rectangle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * Clip rectangle @dst by rectangle @clip. Clip rectangle @src by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * the corresponding amounts, retaining the vertical and horizontal scaling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * factors from @src to @dst.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * %true if rectangle @dst is still visible after being clipped,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * %false otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) const struct drm_rect *clip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) int diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) diff = clip->x1 - dst->x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (diff > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) u32 new_src_w = clip_scaled(drm_rect_width(src),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) drm_rect_width(dst), &diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) src->x1 = src->x2 - new_src_w;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) dst->x1 += diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) diff = clip->y1 - dst->y1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (diff > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) u32 new_src_h = clip_scaled(drm_rect_height(src),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) drm_rect_height(dst), &diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) src->y1 = src->y2 - new_src_h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) dst->y1 += diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) diff = dst->x2 - clip->x2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (diff > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) u32 new_src_w = clip_scaled(drm_rect_width(src),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) drm_rect_width(dst), &diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) src->x2 = src->x1 + new_src_w;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) dst->x2 -= diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) diff = dst->y2 - clip->y2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (diff > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) u32 new_src_h = clip_scaled(drm_rect_height(src),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) drm_rect_height(dst), &diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) src->y2 = src->y1 + new_src_h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) dst->y2 -= diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return drm_rect_visible(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) EXPORT_SYMBOL(drm_rect_clip_scaled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static int drm_calc_scale(int src, int dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) int scale = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (WARN_ON(src < 0 || dst < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (dst == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (src > (dst << 16))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return DIV_ROUND_UP(src, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) scale = src / dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return scale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * drm_rect_calc_hscale - calculate the horizontal scaling factor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * @src: source window rectangle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * @dst: destination window rectangle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * @min_hscale: minimum allowed horizontal scaling factor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * @max_hscale: maximum allowed horizontal scaling factor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * Calculate the horizontal scaling factor as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * (@src width) / (@dst width).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * If the scale is below 1 << 16, round down. If the scale is above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * 1 << 16, round up. This will calculate the scale with the most
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * pessimistic limit calculation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * The horizontal scaling factor, or errno of out of limits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) int drm_rect_calc_hscale(const struct drm_rect *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) const struct drm_rect *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) int min_hscale, int max_hscale)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) int src_w = drm_rect_width(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) int dst_w = drm_rect_width(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) int hscale = drm_calc_scale(src_w, dst_w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (hscale < 0 || dst_w == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return hscale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (hscale < min_hscale || hscale > max_hscale)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return hscale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) EXPORT_SYMBOL(drm_rect_calc_hscale);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * drm_rect_calc_vscale - calculate the vertical scaling factor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * @src: source window rectangle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * @dst: destination window rectangle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * @min_vscale: minimum allowed vertical scaling factor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * @max_vscale: maximum allowed vertical scaling factor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * Calculate the vertical scaling factor as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * (@src height) / (@dst height).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * If the scale is below 1 << 16, round down. If the scale is above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * 1 << 16, round up. This will calculate the scale with the most
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * pessimistic limit calculation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * The vertical scaling factor, or errno of out of limits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) int drm_rect_calc_vscale(const struct drm_rect *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) const struct drm_rect *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) int min_vscale, int max_vscale)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) int src_h = drm_rect_height(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) int dst_h = drm_rect_height(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) int vscale = drm_calc_scale(src_h, dst_h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (vscale < 0 || dst_h == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return vscale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (vscale < min_vscale || vscale > max_vscale)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return vscale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) EXPORT_SYMBOL(drm_rect_calc_vscale);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * drm_rect_debug_print - print the rectangle information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * @prefix: prefix string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * @r: rectangle to print
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * @fixed_point: rectangle is in 16.16 fixed point format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) void drm_rect_debug_print(const char *prefix, const struct drm_rect *r, bool fixed_point)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (fixed_point)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) DRM_DEBUG_KMS("%s" DRM_RECT_FP_FMT "\n", prefix, DRM_RECT_FP_ARG(r));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) DRM_DEBUG_KMS("%s" DRM_RECT_FMT "\n", prefix, DRM_RECT_ARG(r));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) EXPORT_SYMBOL(drm_rect_debug_print);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * drm_rect_rotate - Rotate the rectangle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * @r: rectangle to be rotated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * @width: Width of the coordinate space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * @height: Height of the coordinate space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * @rotation: Transformation to be applied
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * Apply @rotation to the coordinates of rectangle @r.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * @width and @height combined with @rotation define
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * the location of the new origin.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * @width correcsponds to the horizontal and @height
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * to the vertical axis of the untransformed coordinate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) void drm_rect_rotate(struct drm_rect *r,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) int width, int height,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) unsigned int rotation)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) struct drm_rect tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (rotation & (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) tmp = *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (rotation & DRM_MODE_REFLECT_X) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) r->x1 = width - tmp.x2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) r->x2 = width - tmp.x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (rotation & DRM_MODE_REFLECT_Y) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) r->y1 = height - tmp.y2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) r->y2 = height - tmp.y1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) switch (rotation & DRM_MODE_ROTATE_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) case DRM_MODE_ROTATE_0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) case DRM_MODE_ROTATE_90:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) tmp = *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) r->x1 = tmp.y1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) r->x2 = tmp.y2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) r->y1 = width - tmp.x2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) r->y2 = width - tmp.x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) case DRM_MODE_ROTATE_180:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) tmp = *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) r->x1 = width - tmp.x2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) r->x2 = width - tmp.x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) r->y1 = height - tmp.y2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) r->y2 = height - tmp.y1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) case DRM_MODE_ROTATE_270:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) tmp = *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) r->x1 = height - tmp.y2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) r->x2 = height - tmp.y1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) r->y1 = tmp.x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) r->y2 = tmp.x2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) EXPORT_SYMBOL(drm_rect_rotate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * drm_rect_rotate_inv - Inverse rotate the rectangle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * @r: rectangle to be rotated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * @width: Width of the coordinate space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * @height: Height of the coordinate space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * @rotation: Transformation whose inverse is to be applied
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * Apply the inverse of @rotation to the coordinates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * of rectangle @r.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * @width and @height combined with @rotation define
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * the location of the new origin.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * @width correcsponds to the horizontal and @height
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * to the vertical axis of the original untransformed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * coordinate space, so that you never have to flip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * them when doing a rotatation and its inverse.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * That is, if you do ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * drm_rect_rotate(&r, width, height, rotation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * drm_rect_rotate_inv(&r, width, height, rotation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * you will always get back the original rectangle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) void drm_rect_rotate_inv(struct drm_rect *r,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) int width, int height,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) unsigned int rotation)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) struct drm_rect tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) switch (rotation & DRM_MODE_ROTATE_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) case DRM_MODE_ROTATE_0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) case DRM_MODE_ROTATE_90:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) tmp = *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) r->x1 = width - tmp.y2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) r->x2 = width - tmp.y1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) r->y1 = tmp.x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) r->y2 = tmp.x2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) case DRM_MODE_ROTATE_180:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) tmp = *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) r->x1 = width - tmp.x2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) r->x2 = width - tmp.x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) r->y1 = height - tmp.y2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) r->y2 = height - tmp.y1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) case DRM_MODE_ROTATE_270:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) tmp = *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) r->x1 = tmp.y1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) r->x2 = tmp.y2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) r->y1 = height - tmp.x2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) r->y2 = height - tmp.x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (rotation & (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) tmp = *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (rotation & DRM_MODE_REFLECT_X) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) r->x1 = width - tmp.x2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) r->x2 = width - tmp.x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) if (rotation & DRM_MODE_REFLECT_Y) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) r->y1 = height - tmp.y2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) r->y2 = height - tmp.y1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) EXPORT_SYMBOL(drm_rect_rotate_inv);