2017-10-28 02:10:06 +03:00
|
|
|
/* -*- 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
|
2016-08-11 00:30:29 +03:00
|
|
|
* 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
|
|
|
|
|
2017-04-10 17:35:56 +03:00
|
|
|
#include "mozilla/ArenaAllocator.h"
|
2016-08-11 00:30:29 +03:00
|
|
|
#include "mozilla/gfx/Polygon.h"
|
2016-10-04 02:35:52 +03:00
|
|
|
#include "mozilla/Move.h"
|
2016-08-11 00:30:29 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
2017-04-10 17:35:56 +03:00
|
|
|
#include <list>
|
2016-08-11 00:30:29 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2016-09-17 02:03:32 +03:00
|
|
|
class Layer;
|
2016-08-11 00:30:29 +03:00
|
|
|
|
2017-04-04 04:59:31 +03:00
|
|
|
/**
|
|
|
|
* Represents a layer that might have a non-rectangular geometry.
|
|
|
|
*/
|
2016-09-17 02:03:32 +03:00
|
|
|
struct LayerPolygon {
|
2017-04-04 04:59:31 +03:00
|
|
|
explicit LayerPolygon(Layer* aLayer) : layer(aLayer) {}
|
2016-09-17 02:03:32 +03:00
|
|
|
|
2017-04-04 04:59:31 +03:00
|
|
|
LayerPolygon(Layer* aLayer, gfx::Polygon&& aGeometry)
|
2018-05-30 22:15:35 +03:00
|
|
|
: layer(aLayer), geometry(Some(std::move(aGeometry))) {}
|
2016-10-04 02:35:52 +03:00
|
|
|
|
2016-12-04 19:49:32 +03:00
|
|
|
LayerPolygon(Layer* aLayer, nsTArray<gfx::Point4D>&& aPoints,
|
|
|
|
const gfx::Point4D& aNormal)
|
2017-04-10 17:35:56 +03:00
|
|
|
: layer(aLayer) {
|
2018-05-30 22:15:35 +03:00
|
|
|
geometry.emplace(std::move(aPoints), aNormal);
|
2017-04-10 17:35:56 +03:00
|
|
|
}
|
2016-10-04 02:35:52 +03:00
|
|
|
|
2017-04-04 04:59:31 +03:00
|
|
|
Layer* layer;
|
2016-12-04 19:49:32 +03:00
|
|
|
Maybe<gfx::Polygon> geometry;
|
2016-09-17 02:03:32 +03:00
|
|
|
};
|
|
|
|
|
2017-04-10 17:35:56 +03:00
|
|
|
/**
|
|
|
|
* Allocate BSPTreeNodes from a memory arena to improve performance with
|
|
|
|
* complex scenes.
|
|
|
|
* The arena size of 4096 bytes was selected as an arbitrary power of two.
|
|
|
|
* Depending on the platform, this size accommodates roughly 100 BSPTreeNodes.
|
|
|
|
*/
|
|
|
|
typedef mozilla::ArenaAllocator<4096, 8> BSPTreeArena;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Aliases the container type used to store layers within BSPTreeNodes.
|
|
|
|
*/
|
|
|
|
typedef std::list<LayerPolygon> LayerList;
|
2016-09-17 02:03:32 +03:00
|
|
|
|
2017-04-04 04:59:31 +03:00
|
|
|
/**
|
|
|
|
* 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 {
|
2017-04-10 17:35:56 +03:00
|
|
|
explicit BSPTreeNode(nsTArray<LayerList*>& aListPointers)
|
|
|
|
: front(nullptr), back(nullptr) {
|
|
|
|
// Store the layer list pointer to free memory when BSPTree is destroyed.
|
|
|
|
aListPointers.AppendElement(&layers);
|
2016-08-11 00:30:29 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 16:15:21 +03:00
|
|
|
const gfx::Polygon& First() const {
|
2017-04-10 17:35:56 +03:00
|
|
|
MOZ_ASSERT(!layers.empty());
|
|
|
|
MOZ_ASSERT(layers.front().geometry);
|
|
|
|
return *layers.front().geometry;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* operator new(size_t aSize, BSPTreeArena& mPool) {
|
|
|
|
return mPool.Allocate(aSize);
|
2016-08-11 00:30:29 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 17:35:56 +03:00
|
|
|
BSPTreeNode* front;
|
|
|
|
BSPTreeNode* back;
|
|
|
|
LayerList layers;
|
2016-08-11 00:30:29 +03:00
|
|
|
};
|
|
|
|
|
2017-04-04 04:59:31 +03:00
|
|
|
/**
|
|
|
|
* BSPTree class takes a list of layers as an input and uses binary space
|
|
|
|
* 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
|
|
|
|
*/
|
2019-04-11 15:36:51 +03:00
|
|
|
class BSPTree final {
|
2016-08-11 00:30:29 +03:00
|
|
|
public:
|
2017-04-10 17:35:56 +03:00
|
|
|
/**
|
|
|
|
* The constructor modifies layers in the given list.
|
|
|
|
*/
|
|
|
|
explicit BSPTree(std::list<LayerPolygon>& aLayers) {
|
2016-09-17 02:03:32 +03:00
|
|
|
MOZ_ASSERT(!aLayers.empty());
|
|
|
|
|
2017-04-10 17:35:56 +03:00
|
|
|
mRoot = new (mPool) BSPTreeNode(mListPointers);
|
2016-09-17 02:03:32 +03:00
|
|
|
BuildTree(mRoot, aLayers);
|
2016-08-11 00:30:29 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 17:35:56 +03:00
|
|
|
~BSPTree() {
|
|
|
|
for (LayerList* listPtr : mListPointers) {
|
|
|
|
listPtr->~LayerList();
|
|
|
|
}
|
2017-04-10 16:15:21 +03:00
|
|
|
}
|
|
|
|
|
2017-04-04 04:59:31 +03:00
|
|
|
/**
|
|
|
|
* 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 {
|
|
|
|
nsTArray<LayerPolygon> layers;
|
|
|
|
BuildDrawOrder(mRoot, layers);
|
|
|
|
return layers;
|
2016-08-11 00:30:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-04-10 17:35:56 +03:00
|
|
|
BSPTreeArena mPool;
|
|
|
|
BSPTreeNode* mRoot;
|
|
|
|
nsTArray<LayerList*> mListPointers;
|
2016-08-11 00:30:29 +03:00
|
|
|
|
2017-04-04 04:59:31 +03:00
|
|
|
/**
|
|
|
|
* BuildDrawOrder and BuildTree are called recursively. The depth of the
|
|
|
|
* recursion depends on the amount of polygons and their intersections.
|
|
|
|
*/
|
2017-04-10 17:35:56 +03:00
|
|
|
void BuildDrawOrder(BSPTreeNode* aNode,
|
2016-09-17 02:03:32 +03:00
|
|
|
nsTArray<LayerPolygon>& aLayers) const;
|
2017-04-10 17:35:56 +03:00
|
|
|
|
|
|
|
void BuildTree(BSPTreeNode* aRoot, std::list<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 */
|