2016-08-11 00:30:29 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* 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/. */
|
|
|
|
|
|
|
|
#ifndef MOZILLA_LAYERS_BSPTREE_H
|
|
|
|
#define MOZILLA_LAYERS_BSPTREE_H
|
|
|
|
|
|
|
|
#include "mozilla/gfx/Polygon.h"
|
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
|
|
|
#include <deque>
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2016-09-17 02:03:32 +03:00
|
|
|
class Layer;
|
2016-08-11 00:30:29 +03:00
|
|
|
|
2016-09-17 02:03:32 +03:00
|
|
|
// Represents a layer that might have a non-rectangular geometry.
|
|
|
|
struct LayerPolygon {
|
|
|
|
LayerPolygon(gfx::Polygon3D&& aGeometry, Layer *aLayer)
|
|
|
|
: layer(aLayer), geometry(Some(aGeometry)) {}
|
|
|
|
|
|
|
|
explicit LayerPolygon(Layer *aLayer)
|
|
|
|
: layer(aLayer) {}
|
|
|
|
|
|
|
|
Layer *layer;
|
|
|
|
Maybe<gfx::Polygon3D> geometry;
|
|
|
|
};
|
|
|
|
|
|
|
|
LayerPolygon PopFront(std::deque<LayerPolygon>& aLayers);
|
|
|
|
|
|
|
|
// Represents a node in a BSP tree. The node contains at least one layer with
|
|
|
|
// associated geometry that is used as a splitting plane, and at most two child
|
|
|
|
// nodes that represent the splitting planes that further subdivide the space.
|
2016-08-11 00:30:29 +03:00
|
|
|
struct BSPTreeNode {
|
2016-09-17 02:03:32 +03:00
|
|
|
explicit BSPTreeNode(LayerPolygon&& layer)
|
2016-08-11 00:30:29 +03:00
|
|
|
{
|
2016-09-17 02:03:32 +03:00
|
|
|
layers.push_back(std::move(layer));
|
2016-08-11 00:30:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const gfx::Polygon3D& First() const
|
|
|
|
{
|
2016-09-17 02:03:32 +03:00
|
|
|
MOZ_ASSERT(layers[0].geometry);
|
|
|
|
return *layers[0].geometry;
|
2016-08-11 00:30:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
UniquePtr<BSPTreeNode> front;
|
|
|
|
UniquePtr<BSPTreeNode> back;
|
2016-09-17 02:03:32 +03:00
|
|
|
std::deque<LayerPolygon> layers;
|
2016-08-11 00:30:29 +03:00
|
|
|
};
|
|
|
|
|
2016-09-17 02:03:32 +03:00
|
|
|
// BSPTree class takes a list of layers as an input and uses binary space
|
2016-08-11 00:30:29 +03:00
|
|
|
// partitioning algorithm to create a tree structure that can be used for
|
|
|
|
// depth sorting.
|
|
|
|
//
|
|
|
|
// Sources for more information:
|
|
|
|
// https://en.wikipedia.org/wiki/Binary_space_partitioning
|
|
|
|
// ftp://ftp.sgi.com/other/bspfaq/faq/bspfaq.html
|
|
|
|
class BSPTree {
|
|
|
|
public:
|
2016-09-17 02:03:32 +03:00
|
|
|
// This constructor takes the ownership of layers in the given list.
|
|
|
|
explicit BSPTree(std::deque<LayerPolygon>& aLayers)
|
2016-08-11 00:30:29 +03:00
|
|
|
{
|
2016-09-17 02:03:32 +03:00
|
|
|
MOZ_ASSERT(!aLayers.empty());
|
|
|
|
mRoot.reset(new BSPTreeNode(PopFront(aLayers)));
|
|
|
|
|
|
|
|
BuildTree(mRoot, aLayers);
|
2016-08-11 00:30:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the root node of the BSP tree.
|
|
|
|
const UniquePtr<BSPTreeNode>& GetRoot() const
|
|
|
|
{
|
|
|
|
return mRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Builds and returns the back-to-front draw order for the created BSP tree.
|
2016-09-17 02:03:32 +03:00
|
|
|
nsTArray<LayerPolygon> GetDrawOrder() const
|
2016-08-11 00:30:29 +03:00
|
|
|
{
|
2016-09-17 02:03:32 +03:00
|
|
|
nsTArray<LayerPolygon> layers;
|
|
|
|
BuildDrawOrder(mRoot, layers);
|
|
|
|
return layers;
|
2016-08-11 00:30:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
UniquePtr<BSPTreeNode> mRoot;
|
|
|
|
|
2016-09-17 02:03:32 +03:00
|
|
|
// BuildDrawOrder and BuildTree are called recursively. The depth of the
|
|
|
|
// recursion depends on the amount of polygons and their intersections.
|
2016-08-11 00:30:29 +03:00
|
|
|
void BuildDrawOrder(const UniquePtr<BSPTreeNode>& aNode,
|
2016-09-17 02:03:32 +03:00
|
|
|
nsTArray<LayerPolygon>& aLayers) const;
|
2016-08-11 00:30:29 +03:00
|
|
|
void BuildTree(UniquePtr<BSPTreeNode>& aRoot,
|
2016-09-17 02:03:32 +03:00
|
|
|
std::deque<LayerPolygon>& aLayers);
|
2016-08-11 00:30:29 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace layers
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2016-08-16 21:01:06 +03:00
|
|
|
#endif /* MOZILLA_LAYERS_BSPTREE_H */
|