18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (C) 2011-2013 Intel Corporation
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice (including the next
128c2ecf20Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
138c2ecf20Sopenharmony_ci * Software.
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
168c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
178c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
188c2ecf20Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
198c2ecf20Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
208c2ecf20Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
218c2ecf20Sopenharmony_ci * SOFTWARE.
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#ifndef DRM_RECT_H
258c2ecf20Sopenharmony_ci#define DRM_RECT_H
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#include <linux/types.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/**
308c2ecf20Sopenharmony_ci * DOC: rect utils
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci * Utility functions to help manage rectangular areas for
338c2ecf20Sopenharmony_ci * clipping, scaling, etc. calculations.
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/**
378c2ecf20Sopenharmony_ci * struct drm_rect - two dimensional rectangle
388c2ecf20Sopenharmony_ci * @x1: horizontal starting coordinate (inclusive)
398c2ecf20Sopenharmony_ci * @x2: horizontal ending coordinate (exclusive)
408c2ecf20Sopenharmony_ci * @y1: vertical starting coordinate (inclusive)
418c2ecf20Sopenharmony_ci * @y2: vertical ending coordinate (exclusive)
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_cistruct drm_rect {
448c2ecf20Sopenharmony_ci	int x1, y1, x2, y2;
458c2ecf20Sopenharmony_ci};
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/**
488c2ecf20Sopenharmony_ci * DRM_RECT_FMT - printf string for &struct drm_rect
498c2ecf20Sopenharmony_ci */
508c2ecf20Sopenharmony_ci#define DRM_RECT_FMT    "%dx%d%+d%+d"
518c2ecf20Sopenharmony_ci/**
528c2ecf20Sopenharmony_ci * DRM_RECT_ARG - printf arguments for &struct drm_rect
538c2ecf20Sopenharmony_ci * @r: rectangle struct
548c2ecf20Sopenharmony_ci */
558c2ecf20Sopenharmony_ci#define DRM_RECT_ARG(r) drm_rect_width(r), drm_rect_height(r), (r)->x1, (r)->y1
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/**
588c2ecf20Sopenharmony_ci * DRM_RECT_FP_FMT - printf string for &struct drm_rect in 16.16 fixed point
598c2ecf20Sopenharmony_ci */
608c2ecf20Sopenharmony_ci#define DRM_RECT_FP_FMT "%d.%06ux%d.%06u%+d.%06u%+d.%06u"
618c2ecf20Sopenharmony_ci/**
628c2ecf20Sopenharmony_ci * DRM_RECT_FP_ARG - printf arguments for &struct drm_rect in 16.16 fixed point
638c2ecf20Sopenharmony_ci * @r: rectangle struct
648c2ecf20Sopenharmony_ci *
658c2ecf20Sopenharmony_ci * This is useful for e.g. printing plane source rectangles, which are in 16.16
668c2ecf20Sopenharmony_ci * fixed point.
678c2ecf20Sopenharmony_ci */
688c2ecf20Sopenharmony_ci#define DRM_RECT_FP_ARG(r) \
698c2ecf20Sopenharmony_ci		drm_rect_width(r) >> 16, ((drm_rect_width(r) & 0xffff) * 15625) >> 10, \
708c2ecf20Sopenharmony_ci		drm_rect_height(r) >> 16, ((drm_rect_height(r) & 0xffff) * 15625) >> 10, \
718c2ecf20Sopenharmony_ci		(r)->x1 >> 16, (((r)->x1 & 0xffff) * 15625) >> 10, \
728c2ecf20Sopenharmony_ci		(r)->y1 >> 16, (((r)->y1 & 0xffff) * 15625) >> 10
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/**
758c2ecf20Sopenharmony_ci * drm_rect_init - initialize the rectangle from x/y/w/h
768c2ecf20Sopenharmony_ci * @r: rectangle
778c2ecf20Sopenharmony_ci * @x: x coordinate
788c2ecf20Sopenharmony_ci * @y: y coordinate
798c2ecf20Sopenharmony_ci * @width: width
808c2ecf20Sopenharmony_ci * @height: height
818c2ecf20Sopenharmony_ci */
828c2ecf20Sopenharmony_cistatic inline void drm_rect_init(struct drm_rect *r, int x, int y,
838c2ecf20Sopenharmony_ci				 int width, int height)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	r->x1 = x;
868c2ecf20Sopenharmony_ci	r->y1 = y;
878c2ecf20Sopenharmony_ci	r->x2 = x + width;
888c2ecf20Sopenharmony_ci	r->y2 = y + height;
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci/**
928c2ecf20Sopenharmony_ci * drm_rect_adjust_size - adjust the size of the rectangle
938c2ecf20Sopenharmony_ci * @r: rectangle to be adjusted
948c2ecf20Sopenharmony_ci * @dw: horizontal adjustment
958c2ecf20Sopenharmony_ci * @dh: vertical adjustment
968c2ecf20Sopenharmony_ci *
978c2ecf20Sopenharmony_ci * Change the size of rectangle @r by @dw in the horizontal direction,
988c2ecf20Sopenharmony_ci * and by @dh in the vertical direction, while keeping the center
998c2ecf20Sopenharmony_ci * of @r stationary.
1008c2ecf20Sopenharmony_ci *
1018c2ecf20Sopenharmony_ci * Positive @dw and @dh increase the size, negative values decrease it.
1028c2ecf20Sopenharmony_ci */
1038c2ecf20Sopenharmony_cistatic inline void drm_rect_adjust_size(struct drm_rect *r, int dw, int dh)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	r->x1 -= dw >> 1;
1068c2ecf20Sopenharmony_ci	r->y1 -= dh >> 1;
1078c2ecf20Sopenharmony_ci	r->x2 += (dw + 1) >> 1;
1088c2ecf20Sopenharmony_ci	r->y2 += (dh + 1) >> 1;
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci/**
1128c2ecf20Sopenharmony_ci * drm_rect_translate - translate the rectangle
1138c2ecf20Sopenharmony_ci * @r: rectangle to be tranlated
1148c2ecf20Sopenharmony_ci * @dx: horizontal translation
1158c2ecf20Sopenharmony_ci * @dy: vertical translation
1168c2ecf20Sopenharmony_ci *
1178c2ecf20Sopenharmony_ci * Move rectangle @r by @dx in the horizontal direction,
1188c2ecf20Sopenharmony_ci * and by @dy in the vertical direction.
1198c2ecf20Sopenharmony_ci */
1208c2ecf20Sopenharmony_cistatic inline void drm_rect_translate(struct drm_rect *r, int dx, int dy)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	r->x1 += dx;
1238c2ecf20Sopenharmony_ci	r->y1 += dy;
1248c2ecf20Sopenharmony_ci	r->x2 += dx;
1258c2ecf20Sopenharmony_ci	r->y2 += dy;
1268c2ecf20Sopenharmony_ci}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci/**
1298c2ecf20Sopenharmony_ci * drm_rect_translate_to - translate the rectangle to an absolute position
1308c2ecf20Sopenharmony_ci * @r: rectangle to be tranlated
1318c2ecf20Sopenharmony_ci * @x: horizontal position
1328c2ecf20Sopenharmony_ci * @y: vertical position
1338c2ecf20Sopenharmony_ci *
1348c2ecf20Sopenharmony_ci * Move rectangle @r to @x in the horizontal direction,
1358c2ecf20Sopenharmony_ci * and to @y in the vertical direction.
1368c2ecf20Sopenharmony_ci */
1378c2ecf20Sopenharmony_cistatic inline void drm_rect_translate_to(struct drm_rect *r, int x, int y)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	drm_rect_translate(r, x - r->x1, y - r->y1);
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci/**
1438c2ecf20Sopenharmony_ci * drm_rect_downscale - downscale a rectangle
1448c2ecf20Sopenharmony_ci * @r: rectangle to be downscaled
1458c2ecf20Sopenharmony_ci * @horz: horizontal downscale factor
1468c2ecf20Sopenharmony_ci * @vert: vertical downscale factor
1478c2ecf20Sopenharmony_ci *
1488c2ecf20Sopenharmony_ci * Divide the coordinates of rectangle @r by @horz and @vert.
1498c2ecf20Sopenharmony_ci */
1508c2ecf20Sopenharmony_cistatic inline void drm_rect_downscale(struct drm_rect *r, int horz, int vert)
1518c2ecf20Sopenharmony_ci{
1528c2ecf20Sopenharmony_ci	r->x1 /= horz;
1538c2ecf20Sopenharmony_ci	r->y1 /= vert;
1548c2ecf20Sopenharmony_ci	r->x2 /= horz;
1558c2ecf20Sopenharmony_ci	r->y2 /= vert;
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci/**
1598c2ecf20Sopenharmony_ci * drm_rect_width - determine the rectangle width
1608c2ecf20Sopenharmony_ci * @r: rectangle whose width is returned
1618c2ecf20Sopenharmony_ci *
1628c2ecf20Sopenharmony_ci * RETURNS:
1638c2ecf20Sopenharmony_ci * The width of the rectangle.
1648c2ecf20Sopenharmony_ci */
1658c2ecf20Sopenharmony_cistatic inline int drm_rect_width(const struct drm_rect *r)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	return r->x2 - r->x1;
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci/**
1718c2ecf20Sopenharmony_ci * drm_rect_height - determine the rectangle height
1728c2ecf20Sopenharmony_ci * @r: rectangle whose height is returned
1738c2ecf20Sopenharmony_ci *
1748c2ecf20Sopenharmony_ci * RETURNS:
1758c2ecf20Sopenharmony_ci * The height of the rectangle.
1768c2ecf20Sopenharmony_ci */
1778c2ecf20Sopenharmony_cistatic inline int drm_rect_height(const struct drm_rect *r)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	return r->y2 - r->y1;
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci/**
1838c2ecf20Sopenharmony_ci * drm_rect_visible - determine if the rectangle is visible
1848c2ecf20Sopenharmony_ci * @r: rectangle whose visibility is returned
1858c2ecf20Sopenharmony_ci *
1868c2ecf20Sopenharmony_ci * RETURNS:
1878c2ecf20Sopenharmony_ci * %true if the rectangle is visible, %false otherwise.
1888c2ecf20Sopenharmony_ci */
1898c2ecf20Sopenharmony_cistatic inline bool drm_rect_visible(const struct drm_rect *r)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	return drm_rect_width(r) > 0 && drm_rect_height(r) > 0;
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci/**
1958c2ecf20Sopenharmony_ci * drm_rect_equals - determine if two rectangles are equal
1968c2ecf20Sopenharmony_ci * @r1: first rectangle
1978c2ecf20Sopenharmony_ci * @r2: second rectangle
1988c2ecf20Sopenharmony_ci *
1998c2ecf20Sopenharmony_ci * RETURNS:
2008c2ecf20Sopenharmony_ci * %true if the rectangles are equal, %false otherwise.
2018c2ecf20Sopenharmony_ci */
2028c2ecf20Sopenharmony_cistatic inline bool drm_rect_equals(const struct drm_rect *r1,
2038c2ecf20Sopenharmony_ci				   const struct drm_rect *r2)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	return r1->x1 == r2->x1 && r1->x2 == r2->x2 &&
2068c2ecf20Sopenharmony_ci		r1->y1 == r2->y1 && r1->y2 == r2->y2;
2078c2ecf20Sopenharmony_ci}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_cibool drm_rect_intersect(struct drm_rect *r, const struct drm_rect *clip);
2108c2ecf20Sopenharmony_cibool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst,
2118c2ecf20Sopenharmony_ci			  const struct drm_rect *clip);
2128c2ecf20Sopenharmony_ciint drm_rect_calc_hscale(const struct drm_rect *src,
2138c2ecf20Sopenharmony_ci			 const struct drm_rect *dst,
2148c2ecf20Sopenharmony_ci			 int min_hscale, int max_hscale);
2158c2ecf20Sopenharmony_ciint drm_rect_calc_vscale(const struct drm_rect *src,
2168c2ecf20Sopenharmony_ci			 const struct drm_rect *dst,
2178c2ecf20Sopenharmony_ci			 int min_vscale, int max_vscale);
2188c2ecf20Sopenharmony_civoid drm_rect_debug_print(const char *prefix,
2198c2ecf20Sopenharmony_ci			  const struct drm_rect *r, bool fixed_point);
2208c2ecf20Sopenharmony_civoid drm_rect_rotate(struct drm_rect *r,
2218c2ecf20Sopenharmony_ci		     int width, int height,
2228c2ecf20Sopenharmony_ci		     unsigned int rotation);
2238c2ecf20Sopenharmony_civoid drm_rect_rotate_inv(struct drm_rect *r,
2248c2ecf20Sopenharmony_ci			 int width, int height,
2258c2ecf20Sopenharmony_ci			 unsigned int rotation);
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci#endif
228