diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /gfx/layers/BSPTree.cpp | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'gfx/layers/BSPTree.cpp')
-rw-r--r-- | gfx/layers/BSPTree.cpp | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/gfx/layers/BSPTree.cpp b/gfx/layers/BSPTree.cpp new file mode 100644 index 0000000000..d181b05e80 --- /dev/null +++ b/gfx/layers/BSPTree.cpp @@ -0,0 +1,115 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "BSPTree.h" +#include "mozilla/gfx/Polygon.h" + +namespace mozilla { +namespace layers { + +void BSPTree::BuildDrawOrder(BSPTreeNode* aNode, + nsTArray<LayerPolygon>& aLayers) const { + const gfx::Point4D& normal = aNode->First().GetNormal(); + + BSPTreeNode* front = aNode->front; + BSPTreeNode* back = aNode->back; + + // Since the goal is to return the draw order from back to front, we reverse + // the traversal order if the current polygon is facing towards the camera. + const bool reverseOrder = normal.z > 0.0f; + + if (reverseOrder) { + std::swap(front, back); + } + + if (front) { + BuildDrawOrder(front, aLayers); + } + + for (LayerPolygon& layer : aNode->layers) { + MOZ_ASSERT(layer.geometry); + + if (layer.geometry->GetPoints().Length() >= 3) { + aLayers.AppendElement(std::move(layer)); + } + } + + if (back) { + BuildDrawOrder(back, aLayers); + } +} + +void BSPTree::BuildTree(BSPTreeNode* aRoot, std::list<LayerPolygon>& aLayers) { + MOZ_ASSERT(!aLayers.empty()); + + aRoot->layers.push_back(std::move(aLayers.front())); + aLayers.pop_front(); + + if (aLayers.empty()) { + return; + } + + const gfx::Polygon& plane = aRoot->First(); + MOZ_ASSERT(!plane.IsEmpty()); + + const gfx::Point4D& planeNormal = plane.GetNormal(); + const gfx::Point4D& planePoint = plane.GetPoints()[0]; + + std::list<LayerPolygon> backLayers, frontLayers; + for (LayerPolygon& layerPolygon : aLayers) { + const nsTArray<gfx::Point4D>& geometry = layerPolygon.geometry->GetPoints(); + + // Calculate the plane-point distances for the polygon classification. + size_t pos = 0, neg = 0; + nsTArray<float> distances = CalculatePointPlaneDistances( + geometry, planeNormal, planePoint, pos, neg); + + // Back polygon + if (pos == 0 && neg > 0) { + backLayers.push_back(std::move(layerPolygon)); + } + // Front polygon + else if (pos > 0 && neg == 0) { + frontLayers.push_back(std::move(layerPolygon)); + } + // Coplanar polygon + else if (pos == 0 && neg == 0) { + aRoot->layers.push_back(std::move(layerPolygon)); + } + // Polygon intersects with the splitting plane. + else if (pos > 0 && neg > 0) { + nsTArray<gfx::Point4D> backPoints, frontPoints; + // Clip the polygon against the plane. We reuse the previously calculated + // distances to find the plane-edge intersections. + ClipPointsWithPlane(geometry, planeNormal, distances, backPoints, + frontPoints); + + const gfx::Point4D& normal = layerPolygon.geometry->GetNormal(); + Layer* layer = layerPolygon.layer; + + if (backPoints.Length() >= 3) { + backLayers.emplace_back(layer, std::move(backPoints), normal); + } + + if (frontPoints.Length() >= 3) { + frontLayers.emplace_back(layer, std::move(frontPoints), normal); + } + } + } + + if (!backLayers.empty()) { + aRoot->back = new (mPool) BSPTreeNode(mListPointers); + BuildTree(aRoot->back, backLayers); + } + + if (!frontLayers.empty()) { + aRoot->front = new (mPool) BSPTreeNode(mListPointers); + BuildTree(aRoot->front, frontLayers); + } +} + +} // namespace layers +} // namespace mozilla |