From patchwork Mon Apr 30 13:46:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maarten Lankhorst X-Patchwork-Id: 10371813 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id DB8396032A for ; Mon, 30 Apr 2018 13:46:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CBF1228606 for ; Mon, 30 Apr 2018 13:46:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C09D728B10; Mon, 30 Apr 2018 13:46:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 7044828606 for ; Mon, 30 Apr 2018 13:46:31 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 7C3DC6E2EF; Mon, 30 Apr 2018 13:46:24 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mblankhorst.nl (mblankhorst.nl [141.105.120.124]) by gabe.freedesktop.org (Postfix) with ESMTPS id 574C86E303; Mon, 30 Apr 2018 13:46:23 +0000 (UTC) From: Maarten Lankhorst To: intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org Subject: [PATCH 2/5] drm/rect: Handle rounding errors in drm_rect_clip_scaled, v2. Date: Mon, 30 Apr 2018 15:46:08 +0200 Message-Id: <20180430134611.50988-3-maarten.lankhorst@linux.intel.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180430134611.50988-1-maarten.lankhorst@linux.intel.com> References: <20180430134611.50988-1-maarten.lankhorst@linux.intel.com> X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Vidya Srinivas MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP No matter how you perform the clip adjustments, a small error may push the scaling factor to the other side of 0x10000. Solve this with a macro that will fixup the scale to 0x10000 if we accidentally wrap to the other side. Changes since v1: - Adjust dst immediately, else drm_rect_width/height on dst gives bogus results. Signed-off-by: Maarten Lankhorst --- drivers/gpu/drm/drm_rect.c | 71 ++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 19 deletions(-) diff --git a/drivers/gpu/drm/drm_rect.c b/drivers/gpu/drm/drm_rect.c index b179c7c73cc5..9d6194780a74 100644 --- a/drivers/gpu/drm/drm_rect.c +++ b/drivers/gpu/drm/drm_rect.c @@ -50,6 +50,24 @@ bool drm_rect_intersect(struct drm_rect *r1, const struct drm_rect *r2) } EXPORT_SYMBOL(drm_rect_intersect); +static int drm_calc_scale(int src, int dst) +{ + int scale = 0; + + if (WARN_ON(src < 0 || dst < 0)) + return -EINVAL; + + if (dst == 0) + return 0; + + if (src > (dst << 16)) + return DIV_ROUND_UP(src, dst); + else + scale = src / dst; + + return scale; +} + /** * drm_rect_clip_scaled - perform a scaled clip operation * @src: source window rectangle @@ -71,49 +89,64 @@ bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst, { int diff; + /* + * When scale is near 1.0 rounding errors may cause the scaling + * factor to the other side. Some hardware may support + * upsampling, but not downsampling, and that would break when + * rounding. + */ +#define FIXUP(oldscale, fn, m, second) do { \ + if (oldscale != 1 << 16) { \ + int newscale = drm_calc_scale(fn(src), fn(dst)); \ + \ + if (newscale < 0) \ + return false; \ + \ + if ((oldscale < 0x10000) != (newscale < 0x10000)) { \ + if (!second) \ + src->m##1 = src->m##2 - (fn(dst) << 16); \ + else \ + src->m##2 = src->m##1 + (fn(dst) << 16); \ + } \ + } \ + } while (0) + diff = clip->x1 - dst->x1; if (diff > 0) { int64_t tmp = src->x1 + (int64_t) diff * hscale; src->x1 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX); + dst->x1 = clip->x1; + FIXUP(hscale, drm_rect_width, x, 0); } + diff = clip->y1 - dst->y1; if (diff > 0) { int64_t tmp = src->y1 + (int64_t) diff * vscale; src->y1 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX); + dst->y1 = clip->y1; + FIXUP(vscale, drm_rect_height, y, 0); } + diff = dst->x2 - clip->x2; if (diff > 0) { int64_t tmp = src->x2 - (int64_t) diff * hscale; src->x2 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX); + dst->x2 = clip->x2; + FIXUP(hscale, drm_rect_width, x, 1); } diff = dst->y2 - clip->y2; if (diff > 0) { int64_t tmp = src->y2 - (int64_t) diff * vscale; src->y2 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX); + dst->y2 = clip->y2; + FIXUP(vscale, drm_rect_height, y, 1); } +#undef FIXUP - return drm_rect_intersect(dst, clip); + return drm_rect_visible(dst); } EXPORT_SYMBOL(drm_rect_clip_scaled); -static int drm_calc_scale(int src, int dst) -{ - int scale = 0; - - if (WARN_ON(src < 0 || dst < 0)) - return -EINVAL; - - if (dst == 0) - return 0; - - if (src > (dst << 16)) - return DIV_ROUND_UP(src, dst); - else - scale = src / dst; - - return scale; -} - /** * drm_rect_calc_hscale - calculate the horizontal scaling factor * @src: source window rectangle