summaryrefslogtreecommitdiffstats
path: root/gfx/src/TiledRegion.h
blob: 05177324180c96a40bdf3a7c79466eab11483547 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* -*- 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/. */

#ifndef MOZILLA_GFX_TILEDREGION_H_
#define MOZILLA_GFX_TILEDREGION_H_

#include "mozilla/ArrayView.h"
#include "mozilla/gfx/Rect.h"
#include "mozilla/Move.h"
#include "nsRegion.h"
#include "pixman.h"

namespace mozilla {
namespace gfx {

// See TiledRegion.cpp for documentation on TiledRegionImpl.
class TiledRegionImpl {
public:
  void Clear() { mRects.Clear(); }
  bool AddRect(const pixman_box32_t& aRect);
  bool Intersects(const pixman_box32_t& aRect) const;
  bool Contains(const pixman_box32_t& aRect) const;
  operator ArrayView<pixman_box32_t>() const { return ArrayView<pixman_box32_t>(mRects); }

private:
  nsTArray<pixman_box32_t> mRects;
};

/**
 * A auto-simplifying region type that supports one rectangle per tile.
 * The virtual tile grid is anchored at (0, 0) and has quadratic tiles whose
 * size is hard-coded as kTileSize in TiledRegion.cpp.
 * A TiledRegion starts out empty. You can add rectangles or (regular) regions
 * into it by calling Add(). Add() is a mutating union operation (similar to
 * OrWith on nsRegion) that's *not* exact, because it will enlarge the region as
 * necessary to satisfy the "one rectangle per tile" requirement.
 * Tiled regions convert implicitly to the underlying regular region type.
 * The only way to remove parts from a TiledRegion is by calling SetEmpty().
 */
template<typename RegionT>
class TiledRegion {
public:
  typedef typename RegionT::RectType RectT;

  TiledRegion()
    : mCoversBounds(false)
  {}

  TiledRegion(const TiledRegion& aOther)
    : mBounds(aOther.mBounds)
    , mImpl(aOther.mImpl)
    , mCoversBounds(false)
  {}

  TiledRegion(TiledRegion&& aOther)
    : mBounds(aOther.mBounds)
    , mImpl(Move(aOther.mImpl))
    , mCoversBounds(false)
  {}

  RegionT GetRegion() const
  {
    if (mBounds.IsEmpty()) {
      return RegionT();
    }
    if (mCoversBounds) {
      // Rect limit hit or allocation failed, treat as 1 rect.
      return RegionT(mBounds);
    }
    return RegionT(mImpl);
  }

  TiledRegion& operator=(const TiledRegion& aOther)
  {
    if (&aOther != this) {
      mBounds = aOther.mBounds;
      mImpl = aOther.mImpl;
      mCoversBounds = aOther.mCoversBounds;
    }
    return *this;
  }

  void Add(const RectT& aRect)
  {
    if (aRect.IsEmpty()) {
      return;
    }

    Maybe<RectT> newBounds = mBounds.SafeUnion(aRect);
    if (!newBounds) {
      return;
    }
    mBounds = newBounds.value();
    MOZ_ASSERT(!mBounds.Overflows());

    if (mCoversBounds) {
      return;
    }

    if (!mImpl.AddRect(RectToBox(aRect))) {
      FallBackToBounds();
    }
  }

  void Add(const RegionT& aRegion)
  {
    Maybe<RectT> newBounds = mBounds.SafeUnion(aRegion.GetBounds());
    if (!newBounds) {
      return;
    }
    mBounds = newBounds.value();
    MOZ_ASSERT(!mBounds.Overflows());

    if (mCoversBounds) {
      return;
    }

    for (auto iter = aRegion.RectIter(); !iter.Done(); iter.Next()) {
      RectT r = iter.Get();
      if (r.IsEmpty() || r.Overflows()) {
        // This can happen if e.g. a negative-width rect was wrapped into a
        // region. Treat it the same as we would if such a rect was passed to
        // the Add(const RectT&) function.
        continue;
      }
      if (!mImpl.AddRect(RectToBox(r))) {
        FallBackToBounds();
        return;
      }
    }
  }

  bool IsEmpty() const { return mBounds.IsEmpty(); }

  void SetEmpty()
  {
    mBounds.SetEmpty();
    mImpl.Clear();
    mCoversBounds = false;
  }

  RectT GetBounds() const { return mBounds; }
  bool CoversBounds() const { return mCoversBounds; }

  bool Intersects(const RectT& aRect) const
  {
    if (aRect.IsEmpty()) {
      return true;
    }
    if (aRect.Overflows() || !mBounds.Intersects(aRect)) {
      return false;
    }
    if (mCoversBounds) {
      return true;
    }

    return mImpl.Intersects(RectToBox(aRect));
  }

  bool Contains(const RectT& aRect) const
  {
    if (aRect.IsEmpty()) {
      return true;
    }
    if (aRect.Overflows() || !mBounds.Contains(aRect)) {
      return false;
    }
    if (mCoversBounds) {
      return true;
    }
    return mImpl.Contains(RectToBox(aRect));
  }

private:

  void FallBackToBounds()
  {
    mCoversBounds = true;
    mImpl.Clear();
  }

  static pixman_box32_t RectToBox(const RectT& aRect)
  {
    MOZ_ASSERT(!aRect.IsEmpty());
    MOZ_ASSERT(!aRect.Overflows());
    return { aRect.x, aRect.y, aRect.XMost(), aRect.YMost() };
  }

  RectT mBounds;
  TiledRegionImpl mImpl;

  // mCoversBounds is true if we bailed out due to a large number of tiles.
  // mCoversBounds being true means that this TiledRegion is just a simple
  // rectangle (our mBounds).
  // Once set to true, the TiledRegion will stay in this state until SetEmpty
  // is called.
  bool mCoversBounds;
};

typedef TiledRegion<IntRegion> TiledIntRegion;

} // namespace gfx
} // namespace mozilla

#endif /* MOZILLA_GFX_TILEDREGION_H_ */