1450N/A/*
1450N/A * Copyright (C) 2011-2013 Intel Corporation
1450N/A *
1450N/A * Permission is hereby granted, free of charge, to any person obtaining a
1450N/A * copy of this software and associated documentation files (the "Software"),
1450N/A * to deal in the Software without restriction, including without limitation
1450N/A * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1450N/A * and/or sell copies of the Software, and to permit persons to whom the
1450N/A * Software is furnished to do so, subject to the following conditions:
1450N/A *
1450N/A * The above copyright notice and this permission notice (including the next
1450N/A * paragraph) shall be included in all copies or substantial portions of the
1450N/A * Software.
1450N/A *
1450N/A * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1450N/A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1450N/A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1450N/A * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1450N/A * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1450N/A * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1450N/A * SOFTWARE.
1450N/A */
1450N/A
1450N/A#include "drmP.h"
1450N/A#include "drm_rect.h"
1450N/A
1450N/A/**
1450N/A * drm_rect_intersect - intersect two rectangles
1450N/A * @r1: first rectangle
1450N/A * @r2: second rectangle
1450N/A *
1450N/A * Calculate the intersection of rectangles @r1 and @r2.
1450N/A * @r1 will be overwritten with the intersection.
1450N/A *
1450N/A * RETURNS:
1450N/A * %true if rectangle @r1 is still visible after the operation,
1450N/A * %false otherwise.
1450N/A */
1450N/Aint drm_rect_intersect(struct drm_rect *r1, const struct drm_rect *r2)
1450N/A{
1450N/A r1->x1 = max(r1->x1, r2->x1);
1450N/A r1->y1 = max(r1->y1, r2->y1);
1450N/A r1->x2 = min(r1->x2, r2->x2);
1450N/A r1->y2 = min(r1->y2, r2->y2);
1450N/A
1450N/A return drm_rect_visible(r1);
1450N/A}
1450N/A
1450N/A/**
1450N/A * drm_rect_clip_scaled - perform a scaled clip operation
1450N/A * @src: source window rectangle
1450N/A * @dst: destination window rectangle
1450N/A * @clip: clip rectangle
1450N/A * @hscale: horizontal scaling factor
1450N/A * @vscale: vertical scaling factor
1450N/A *
1450N/A * Clip rectangle @dst by rectangle @clip. Clip rectangle @src by the
1450N/A * same amounts multiplied by @hscale and @vscale.
1450N/A *
1450N/A * RETURNS:
1450N/A * %true if rectangle @dst is still visible after being clipped,
1450N/A * %false otherwise
1450N/A */
1450N/Aint drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst,
1450N/A const struct drm_rect *clip,
1450N/A int hscale, int vscale)
1450N/A{
1450N/A int diff;
1450N/A
1450N/A diff = clip->x1 - dst->x1;
1450N/A if (diff > 0) {
1450N/A int64_t tmp = src->x1 + (int64_t) diff * hscale;
1450N/A src->x1 = clamp_int64_t(tmp);
1450N/A }
1450N/A diff = clip->y1 - dst->y1;
1450N/A if (diff > 0) {
1450N/A int64_t tmp = src->y1 + (int64_t) diff * vscale;
1450N/A src->y1 = clamp_int64_t(tmp);
1450N/A }
1450N/A diff = dst->x2 - clip->x2;
1450N/A if (diff > 0) {
1450N/A int64_t tmp = src->x2 - (int64_t) diff * hscale;
1450N/A src->x2 = clamp_int64_t(tmp);
1450N/A }
1450N/A diff = dst->y2 - clip->y2;
1450N/A if (diff > 0) {
1450N/A int64_t tmp = src->y2 - (int64_t) diff * vscale;
1450N/A src->y2 = clamp_int64_t(tmp);
1450N/A }
1450N/A
1450N/A return drm_rect_intersect(dst, clip);
1450N/A}
1450N/A
1450N/Astatic int drm_calc_scale(int src, int dst)
1450N/A{
1450N/A int scale = 0;
1450N/A
1450N/A if (src < 0 || dst < 0)
1450N/A return -EINVAL;
1450N/A
1450N/A if (dst == 0)
1450N/A return 0;
1450N/A
1450N/A scale = src / dst;
1450N/A
1450N/A return scale;
1450N/A}
1450N/A
1450N/A/**
1450N/A * drm_rect_calc_hscale - calculate the horizontal scaling factor
1450N/A * @src: source window rectangle
1450N/A * @dst: destination window rectangle
1450N/A * @min_hscale: minimum allowed horizontal scaling factor
1450N/A * @max_hscale: maximum allowed horizontal scaling factor
1450N/A *
1450N/A * Calculate the horizontal scaling factor as
1450N/A * (@src width) / (@dst width).
1450N/A *
1450N/A * RETURNS:
1450N/A * The horizontal scaling factor, or errno of out of limits.
1450N/A */
1450N/Aint drm_rect_calc_hscale(const struct drm_rect *src,
1450N/A const struct drm_rect *dst,
1450N/A int min_hscale, int max_hscale)
1450N/A{
1450N/A int src_w = drm_rect_width(src);
1450N/A int dst_w = drm_rect_width(dst);
1450N/A int hscale = drm_calc_scale(src_w, dst_w);
1450N/A
1450N/A if (hscale < 0 || dst_w == 0)
1450N/A return hscale;
1450N/A
1450N/A if (hscale < min_hscale || hscale > max_hscale)
1450N/A return -ERANGE;
1450N/A
1450N/A return hscale;
1450N/A}
1450N/A
1450N/A/**
1450N/A * drm_rect_calc_vscale - calculate the vertical scaling factor
1450N/A * @src: source window rectangle
1450N/A * @dst: destination window rectangle
1450N/A * @min_vscale: minimum allowed vertical scaling factor
1450N/A * @max_vscale: maximum allowed vertical scaling factor
1450N/A *
1450N/A * Calculate the vertical scaling factor as
1450N/A * (@src height) / (@dst height).
1450N/A *
1450N/A * RETURNS:
1450N/A * The vertical scaling factor, or errno of out of limits.
1450N/A */
1450N/Aint drm_rect_calc_vscale(const struct drm_rect *src,
1450N/A const struct drm_rect *dst,
1450N/A int min_vscale, int max_vscale)
1450N/A{
1450N/A int src_h = drm_rect_height(src);
1450N/A int dst_h = drm_rect_height(dst);
1450N/A int vscale = drm_calc_scale(src_h, dst_h);
1450N/A
1450N/A if (vscale < 0 || dst_h == 0)
1450N/A return vscale;
1450N/A
1450N/A if (vscale < min_vscale || vscale > max_vscale)
1450N/A return -ERANGE;
1450N/A
1450N/A return vscale;
1450N/A}
1450N/A
1450N/A/**
1450N/A * drm_calc_hscale_relaxed - calculate the horizontal scaling factor
1450N/A * @src: source window rectangle
1450N/A * @dst: destination window rectangle
1450N/A * @min_hscale: minimum allowed horizontal scaling factor
1450N/A * @max_hscale: maximum allowed horizontal scaling factor
1450N/A *
1450N/A * Calculate the horizontal scaling factor as
1450N/A * (@src width) / (@dst width).
1450N/A *
1450N/A * If the calculated scaling factor is below @min_vscale,
1450N/A * decrease the height of rectangle @dst to compensate.
1450N/A *
1450N/A * If the calculated scaling factor is above @max_vscale,
1450N/A * decrease the height of rectangle @src to compensate.
1450N/A *
1450N/A * RETURNS:
1450N/A * The horizontal scaling factor.
1450N/A */
1450N/Aint drm_rect_calc_hscale_relaxed(struct drm_rect *src,
1450N/A struct drm_rect *dst,
1450N/A int min_hscale, int max_hscale)
1450N/A{
1450N/A int src_w = drm_rect_width(src);
1450N/A int dst_w = drm_rect_width(dst);
1450N/A int hscale = drm_calc_scale(src_w, dst_w);
1450N/A
1450N/A if (hscale < 0 || dst_w == 0)
1450N/A return hscale;
1450N/A
1450N/A if (hscale < min_hscale) {
1450N/A int max_dst_w = src_w / min_hscale;
1450N/A
1450N/A drm_rect_adjust_size(dst, max_dst_w - dst_w, 0);
1450N/A
1450N/A return min_hscale;
1450N/A }
1450N/A
1450N/A if (hscale > max_hscale) {
1450N/A int max_src_w = dst_w * max_hscale;
1450N/A
1450N/A drm_rect_adjust_size(src, max_src_w - src_w, 0);
1450N/A
1450N/A return max_hscale;
1450N/A }
1450N/A
1450N/A return hscale;
1450N/A}
1450N/A
1450N/A/**
1450N/A * drm_rect_calc_vscale_relaxed - calculate the vertical scaling factor
1450N/A * @src: source window rectangle
1450N/A * @dst: destination window rectangle
1450N/A * @min_vscale: minimum allowed vertical scaling factor
1450N/A * @max_vscale: maximum allowed vertical scaling factor
1450N/A *
1450N/A * Calculate the vertical scaling factor as
1450N/A * (@src height) / (@dst height).
1450N/A *
1450N/A * If the calculated scaling factor is below @min_vscale,
1450N/A * decrease the height of rectangle @dst to compensate.
1450N/A *
1450N/A * If the calculated scaling factor is above @max_vscale,
1450N/A * decrease the height of rectangle @src to compensate.
1450N/A *
1450N/A * RETURNS:
1450N/A * The vertical scaling factor.
1450N/A */
1450N/Aint drm_rect_calc_vscale_relaxed(struct drm_rect *src,
1450N/A struct drm_rect *dst,
1450N/A int min_vscale, int max_vscale)
1450N/A{
1450N/A int src_h = drm_rect_height(src);
1450N/A int dst_h = drm_rect_height(dst);
1450N/A int vscale = drm_calc_scale(src_h, dst_h);
1450N/A
1450N/A if (vscale < 0 || dst_h == 0)
1450N/A return vscale;
1450N/A
1450N/A if (vscale < min_vscale) {
1450N/A int max_dst_h = src_h / min_vscale;
1450N/A
1450N/A drm_rect_adjust_size(dst, 0, max_dst_h - dst_h);
1450N/A
1450N/A return min_vscale;
1450N/A }
1450N/A
1450N/A if (vscale > max_vscale) {
1450N/A int max_src_h = dst_h * max_vscale;
1450N/A
1450N/A drm_rect_adjust_size(src, 0, max_src_h - src_h);
1450N/A
1450N/A return max_vscale;
1450N/A }
1450N/A
1450N/A return vscale;
1450N/A}
1450N/A
1450N/A/**
1450N/A * drm_rect_debug_print - print the rectangle information
1450N/A * @r: rectangle to print
1450N/A * @fixed_point: rectangle is in 16.16 fixed point format
1450N/A */
1450N/Avoid drm_rect_debug_print(const struct drm_rect *r, int fixed_point)
1450N/A{
1450N/A int w = drm_rect_width(r);
1450N/A int h = drm_rect_height(r);
1450N/A
1450N/A if (fixed_point)
1450N/A DRM_DEBUG_KMS("%d.%06ux%d.%06u%+d.%06u%+d.%06u\n",
1450N/A w >> 16, ((w & 0xffff) * 15625) >> 10,
1450N/A h >> 16, ((h & 0xffff) * 15625) >> 10,
1450N/A r->x1 >> 16, ((r->x1 & 0xffff) * 15625) >> 10,
1450N/A r->y1 >> 16, ((r->y1 & 0xffff) * 15625) >> 10);
1450N/A else
1450N/A DRM_DEBUG_KMS("%dx%d%+d%+d\n", w, h, r->x1, r->y1);
1450N/A}