From b77cf10a4fbe328fd8b7e0bd0294813dc6394773 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Thu, 29 Oct 2020 15:31:14 +0100 Subject: Move QPolygonClipper to XCB native painting It is the only code using it. Change-Id: I30060a63b6621ea94ae487ec93cd857117e12a46 Reviewed-by: Eirik Aavitsland --- src/plugins/platforms/xcb/CMakeLists.txt | 1 + .../xcb/nativepainting/nativepainting.pri | 1 + .../xcb/nativepainting/qpaintengine_x11.cpp | 2 +- .../xcb/nativepainting/qpolygonclipper_p.h | 316 +++++++++++++++++++++ 4 files changed, 319 insertions(+), 1 deletion(-) create mode 100644 src/plugins/platforms/xcb/nativepainting/qpolygonclipper_p.h (limited to 'src/plugins/platforms/xcb') diff --git a/src/plugins/platforms/xcb/CMakeLists.txt b/src/plugins/platforms/xcb/CMakeLists.txt index 6d6b76a641..e4af25458e 100644 --- a/src/plugins/platforms/xcb/CMakeLists.txt +++ b/src/plugins/platforms/xcb/CMakeLists.txt @@ -120,6 +120,7 @@ qt_internal_extend_target(XcbQpa CONDITION QT_FEATURE_xcb_native_painting nativepainting/qcolormap_x11.cpp nativepainting/qcolormap_x11_p.h nativepainting/qpaintengine_x11.cpp nativepainting/qpaintengine_x11_p.h nativepainting/qpixmap_x11.cpp nativepainting/qpixmap_x11_p.h + nativepainting/polygonclipper_p.h nativepainting/qt_x11_p.h nativepainting/qtessellator.cpp nativepainting/qtessellator_p.h nativepainting/qxcbnativepainting.cpp nativepainting/qxcbnativepainting.h diff --git a/src/plugins/platforms/xcb/nativepainting/nativepainting.pri b/src/plugins/platforms/xcb/nativepainting/nativepainting.pri index 78ed00843f..e1a47fb7d7 100644 --- a/src/plugins/platforms/xcb/nativepainting/nativepainting.pri +++ b/src/plugins/platforms/xcb/nativepainting/nativepainting.pri @@ -7,6 +7,7 @@ qtConfig(xcb-native-painting) { $$PWD/qtessellator_p.h \ $$PWD/qpixmap_x11_p.h \ $$PWD/qpaintengine_x11_p.h \ + $$PWD/qpolygonclipper_p.h \ $$PWD/qt_x11_p.h \ $$PWD/qcolormap_x11_p.h \ $$PWD/qbackingstore_x11_p.h \ diff --git a/src/plugins/platforms/xcb/nativepainting/qpaintengine_x11.cpp b/src/plugins/platforms/xcb/nativepainting/qpaintengine_x11.cpp index 82900d4054..e69030ca97 100644 --- a/src/plugins/platforms/xcb/nativepainting/qpaintengine_x11.cpp +++ b/src/plugins/platforms/xcb/nativepainting/qpaintengine_x11.cpp @@ -41,7 +41,6 @@ #include #include -#include #include #include #include @@ -51,6 +50,7 @@ #endif #include "qpaintengine_x11_p.h" +#include "qpolygonclipper_p.h" #include "qtessellator_p.h" #include "qpixmap_x11_p.h" #include "qcolormap_x11_p.h" diff --git a/src/plugins/platforms/xcb/nativepainting/qpolygonclipper_p.h b/src/plugins/platforms/xcb/nativepainting/qpolygonclipper_p.h new file mode 100644 index 0000000000..33499df6ed --- /dev/null +++ b/src/plugins/platforms/xcb/nativepainting/qpolygonclipper_p.h @@ -0,0 +1,316 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtGui module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QPOLYGONCLIPPER_P_H +#define QPOLYGONCLIPPER_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of other Qt classes. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include + +QT_BEGIN_NAMESPACE + +/* based on sutherland-hodgman line-by-line clipping, as described in + Computer Graphics and Principles */ +template class QPolygonClipper +{ +public: + QPolygonClipper() : + buffer1(0), buffer2(0) + { + x1 = y1 = x2 = y2 = 0; + } + + ~QPolygonClipper() + { + } + + void setBoundingRect(const QRect bounds) + { + x1 = bounds.x(); + x2 = bounds.x() + bounds.width(); + y1 = bounds.y(); + y2 = bounds.y() + bounds.height(); + } + + QRect boundingRect() + { + return QRect(QPoint(x1, y1), QPoint(x2, y2)); + } + + inline OutType intersectLeft(const OutType &p1, const OutType &p2) + { + OutType t; + qreal dy = (p1.y - p2.y) / qreal(p1.x - p2.x); + t.x = x1; + t.y = static_cast(p2.y + (x1 - p2.x) * dy); + return t; + } + + + inline OutType intersectRight(const OutType &p1, const OutType &p2) + { + OutType t; + qreal dy = (p1.y - p2.y) / qreal(p1.x - p2.x); + t.x = x2; + t.y = static_cast(p2.y + (x2 - p2.x) * dy); + return t; + } + + + inline OutType intersectTop(const OutType &p1, const OutType &p2) + { + OutType t; + qreal dx = (p1.x - p2.x) / qreal(p1.y - p2.y); + t.x = static_cast(p2.x + (y1 - p2.y) * dx); + t.y = y1; + return t; + } + + + inline OutType intersectBottom(const OutType &p1, const OutType &p2) + { + OutType t; + qreal dx = (p1.x - p2.x) / qreal(p1.y - p2.y); + t.x = static_cast(p2.x + (y2 - p2.y) * dx); + t.y = y2; + return t; + } + + + void clipPolygon(const InType *inPoints, int inCount, OutType **outPoints, int *outCount, + bool closePolygon = true) + { + Q_ASSERT(outPoints); + Q_ASSERT(outCount); + + if (inCount < 2) { + *outCount = 0; + return; + } + + buffer1.reset(); + buffer2.reset(); + + QDataBuffer *source = &buffer1; + QDataBuffer *clipped = &buffer2; + + // Gather some info since we are iterating through the points anyway.. + bool doLeft = false, doRight = false, doTop = false, doBottom = false; + OutType ot; + for (int i=0; iadd(ot); + + if (ot.x < x1) + doLeft = true; + else if (ot.x > x2) + doRight = true; + if (ot.y < y1) + doTop = true; + else if (ot.y > y2) + doBottom = true; + } + + if (doLeft && clipped->size() > 1) { + QDataBuffer *tmp = source; + source = clipped; + clipped = tmp; + clipped->reset(); + int lastPos, start; + if (closePolygon) { + lastPos = source->size() - 1; + start = 0; + } else { + lastPos = 0; + start = 1; + if (source->at(0).x >= x1) + clipped->add(source->at(0)); + } + for (int i=start; iat(i); + const OutType &ppt = source->at(lastPos); + + if (cpt.x >= x1) { + if (ppt.x >= x1) { + clipped->add(cpt); + } else { + clipped->add(intersectLeft(cpt, ppt)); + clipped->add(cpt); + } + } else if (ppt.x >= x1) { + clipped->add(intersectLeft(cpt, ppt)); + } + lastPos = i; + } + } + + if (doRight && clipped->size() > 1) { + QDataBuffer *tmp = source; + source = clipped; + clipped = tmp; + clipped->reset(); + int lastPos, start; + if (closePolygon) { + lastPos = source->size() - 1; + start = 0; + } else { + lastPos = 0; + start = 1; + if (source->at(0).x <= x2) + clipped->add(source->at(0)); + } + for (int i=start; isize(); ++i) { + const OutType &cpt = source->at(i); + const OutType &ppt = source->at(lastPos); + + if (cpt.x <= x2) { + if (ppt.x <= x2) { + clipped->add(cpt); + } else { + clipped->add(intersectRight(cpt, ppt)); + clipped->add(cpt); + } + } else if (ppt.x <= x2) { + clipped->add(intersectRight(cpt, ppt)); + } + + lastPos = i; + } + + } + + if (doTop && clipped->size() > 1) { + QDataBuffer *tmp = source; + source = clipped; + clipped = tmp; + clipped->reset(); + int lastPos, start; + if (closePolygon) { + lastPos = source->size() - 1; + start = 0; + } else { + lastPos = 0; + start = 1; + if (source->at(0).y >= y1) + clipped->add(source->at(0)); + } + for (int i=start; isize(); ++i) { + const OutType &cpt = source->at(i); + const OutType &ppt = source->at(lastPos); + + if (cpt.y >= y1) { + if (ppt.y >= y1) { + clipped->add(cpt); + } else { + clipped->add(intersectTop(cpt, ppt)); + clipped->add(cpt); + } + } else if (ppt.y >= y1) { + clipped->add(intersectTop(cpt, ppt)); + } + + lastPos = i; + } + } + + if (doBottom && clipped->size() > 1) { + QDataBuffer *tmp = source; + source = clipped; + clipped = tmp; + clipped->reset(); + int lastPos, start; + if (closePolygon) { + lastPos = source->size() - 1; + start = 0; + } else { + lastPos = 0; + start = 1; + if (source->at(0).y <= y2) + clipped->add(source->at(0)); + } + for (int i=start; isize(); ++i) { + const OutType &cpt = source->at(i); + const OutType &ppt = source->at(lastPos); + + if (cpt.y <= y2) { + if (ppt.y <= y2) { + clipped->add(cpt); + } else { + clipped->add(intersectBottom(cpt, ppt)); + clipped->add(cpt); + } + } else if (ppt.y <= y2) { + clipped->add(intersectBottom(cpt, ppt)); + } + lastPos = i; + } + } + + if (closePolygon && clipped->size() > 0) { + // close clipped polygon + if (clipped->at(0).x != clipped->at(clipped->size()-1).x || + clipped->at(0).y != clipped->at(clipped->size()-1).y) { + OutType ot = clipped->at(0); + clipped->add(ot); + } + } + *outCount = clipped->size(); + *outPoints = clipped->data(); + } + +private: + int x1, x2, y1, y2; + QDataBuffer buffer1; + QDataBuffer buffer2; +}; + +QT_END_NAMESPACE + +#endif // QPOLYGONCLIPPER_P_H -- cgit v1.2.3