Flutter Engine
The Flutter Engine
IntersectionTree.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef skgpu_graphite_geom_IntersectionTree_DEFINED
9#define skgpu_graphite_geom_IntersectionTree_DEFINED
10
14
15namespace skgpu::graphite {
16
17/**
18 * Maintains a collection of non-overlapping rectangles.
19 *
20 * add() either adds the given rect to the collection, or returns false if it intersected with a
21 * rect already in the collection.
22 */
24public:
25 enum class SplitType : bool {
26 kX,
27 kY
28 };
29
31
32 bool add(Rect rect) {
33 if (rect.isEmptyNegativeOrNaN()) {
34 // Empty and undefined rects can simply pass without modifying the tree.
35 return true;
36 }
37 if (!fRoot->intersects(rect)) {
38 fRoot = fRoot->addNonIntersecting(rect, &fArena);
39 return true;
40 }
41 return false;
42 }
43
44private:
45 class Node {
46 public:
47 virtual ~Node() = default;
48
49 virtual bool intersects(Rect) = 0;
50 virtual Node* addNonIntersecting(Rect, SkArenaAlloc*) = 0;
51 };
52
53 template<SplitType kSplitType> class TreeNode;
54 class LeafNode;
55
56 // The TreeNode size is made of a vtable (i.e. sizeof(void*)), float, and two Node* pointers.
57 // We also align between the Node* and the float which may add some extra padding.
58 constexpr static int kTreeNodeSize = SkAlignTo(sizeof(void*) + sizeof(float), alignof(void*)) +
59 2 * sizeof(Node*);
60 constexpr static int kLeafNodeSize = 16 + (2 + 64) * sizeof(Rect);
61 constexpr static int kPadSize = 256; // For footers and alignment.
62 SkArenaAlloc fArena{kLeafNodeSize + kTreeNodeSize + kPadSize*2};
63 Node* fRoot;
64};
65
66} // namespace skgpu::graphite
67
68#endif // skgpu_graphite_geom_IntersectionTree_DEFINED
static constexpr size_t SkAlignTo(size_t x, size_t alignment)
Definition: SkAlign.h:33
const int kPadSize
Definition: dart.idl:29
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
TRect< Scalar > Rect
Definition: rect.h:769