summaryrefslogtreecommitdiffstats
path: root/layout/painting/DisplayListClipState.h
blob: 0f1df2ca5882147a586d693e37a87eb4ab059676 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* -*- 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 DISPLAYLISTCLIPSTATE_H_
#define DISPLAYLISTCLIPSTATE_H_

#include "DisplayItemClip.h"
#include "DisplayItemClipChain.h"

#include "mozilla/DebugOnly.h"

class nsIFrame;
class nsIScrollableFrame;

namespace mozilla {

class nsDisplayListBuilder;

/**
 * All clip coordinates are in appunits relative to the reference frame
 * for the display item we're building.
 */
class DisplayListClipState {
 public:
  DisplayListClipState()
      : mClipChainContentDescendants(nullptr),
        mClipChainContainingBlockDescendants(nullptr),
        mCurrentCombinedClipChain(nullptr),
        mCurrentCombinedClipChainIsValid(false),
        mClippedToDisplayPort(false) {}

  void SetClippedToDisplayPort() { mClippedToDisplayPort = true; }
  bool IsClippedToDisplayPort() const { return mClippedToDisplayPort; }

  /**
   * Returns intersection of mClipChainContainingBlockDescendants and
   * mClipChainContentDescendants, allocated on aBuilder's arena.
   */
  const DisplayItemClipChain* GetCurrentCombinedClipChain(
      nsDisplayListBuilder* aBuilder);

  const DisplayItemClipChain* GetClipChainForContainingBlockDescendants()
      const {
    return mClipChainContainingBlockDescendants;
  }
  const DisplayItemClipChain* GetClipChainForContentDescendants() const {
    return mClipChainContentDescendants;
  }

  const ActiveScrolledRoot* GetContentClipASR() const {
    return mClipChainContentDescendants ? mClipChainContentDescendants->mASR
                                        : nullptr;
  }

  class AutoSaveRestore;

  class AutoClipContainingBlockDescendantsToContentBox;

  class AutoClipMultiple;

  enum { ASSUME_DRAWING_RESTRICTED_TO_CONTENT_RECT = 0x01 };

 private:
  void Clear() {
    mClipChainContentDescendants = nullptr;
    mClipChainContainingBlockDescendants = nullptr;
    mCurrentCombinedClipChain = nullptr;
    mCurrentCombinedClipChainIsValid = false;
    mClippedToDisplayPort = false;
  }

  void SetClipChainForContainingBlockDescendants(
      const DisplayItemClipChain* aClipChain) {
    mClipChainContainingBlockDescendants = aClipChain;
    InvalidateCurrentCombinedClipChain(aClipChain ? aClipChain->mASR : nullptr);
  }

  /**
   * Intersects the given clip rect (with optional aRadii) with the current
   * mClipContainingBlockDescendants and sets mClipContainingBlockDescendants to
   * the result, stored in aClipOnStack.
   */
  void ClipContainingBlockDescendants(nsDisplayListBuilder* aBuilder,
                                      const nsRect& aRect,
                                      const nscoord* aRadii,
                                      DisplayItemClipChain& aClipChainOnStack);

  void ClipContentDescendants(nsDisplayListBuilder* aBuilder,
                              const nsRect& aRect, const nscoord* aRadii,
                              DisplayItemClipChain& aClipChainOnStack);
  void ClipContentDescendants(nsDisplayListBuilder* aBuilder,
                              const nsRect& aRect, const nsRect& aRoundedRect,
                              const nscoord* aRadii,
                              DisplayItemClipChain& aClipChainOnStack);

  void InvalidateCurrentCombinedClipChain(
      const ActiveScrolledRoot* aInvalidateUpTo);

  /**
   * Clips containing-block descendants to the frame's content-box,
   * taking border-radius into account.
   * If aFlags contains ASSUME_DRAWING_RESTRICTED_TO_CONTENT_RECT then
   * we assume display items will not draw outside the content rect, so
   * clipping is only required if there is a border-radius. This is an
   * optimization to reduce the amount of clipping required.
   */
  void ClipContainingBlockDescendantsToContentBox(
      nsDisplayListBuilder* aBuilder, nsIFrame* aFrame,
      DisplayItemClipChain& aClipChainOnStack, uint32_t aFlags);

  /**
   * All content descendants (i.e. following placeholder frames to their
   * out-of-flows if necessary) should be clipped by
   * mClipChainContentDescendants. Null if no clipping applies.
   */
  const DisplayItemClipChain* mClipChainContentDescendants;
  /**
   * All containing-block descendants (i.e. frame descendants), including
   * display items for the current frame, should be clipped by
   * mClipChainContainingBlockDescendants.
   * Null if no clipping applies.
   */
  const DisplayItemClipChain* mClipChainContainingBlockDescendants;
  /**
   * The intersection of mClipChainContentDescendants and
   * mClipChainContainingBlockDescendants.
   * Allocated in the nsDisplayListBuilder arena. Null if none has been
   * allocated or both mClipChainContentDescendants and
   * mClipChainContainingBlockDescendants are null.
   */
  const DisplayItemClipChain* mCurrentCombinedClipChain;
  bool mCurrentCombinedClipChainIsValid;
  /**
   * A flag that is used by sticky positioned items to know if the clip applied
   * to them is just the displayport clip or if there is additional clipping.
   */
  bool mClippedToDisplayPort;
};

/**
 * A class to automatically save and restore the current clip state. Also
 * offers methods for modifying the clip state. Only one modification is allowed
 * to be in scope at a time using one of these objects; multiple modifications
 * require nested objects. The interface is written this way to prevent
 * dangling pointers to DisplayItemClips.
 */
class DisplayListClipState::AutoSaveRestore {
 public:
  explicit AutoSaveRestore(nsDisplayListBuilder* aBuilder);
  void Restore() {
    mState = mSavedState;
#ifdef DEBUG
    mRestored = true;
#endif
  }
  ~AutoSaveRestore() { Restore(); }

  void Clear() {
    NS_ASSERTION(!mRestored, "Already restored!");
    mState.Clear();
#ifdef DEBUG
    mClipUsed = false;
#endif
  }

  void SetClipChainForContainingBlockDescendants(
      const DisplayItemClipChain* aClipChain) {
    mState.SetClipChainForContainingBlockDescendants(aClipChain);
  }

  /**
   * Intersects the given clip rect (with optional aRadii) with the current
   * mClipContainingBlockDescendants and sets mClipContainingBlockDescendants to
   * the result, stored in aClipOnStack.
   */
  void ClipContainingBlockDescendants(const nsRect& aRect,
                                      const nscoord* aRadii = nullptr) {
    NS_ASSERTION(!mRestored, "Already restored!");
    NS_ASSERTION(!mClipUsed, "mClip already used");
#ifdef DEBUG
    mClipUsed = true;
#endif
    mState.ClipContainingBlockDescendants(mBuilder, aRect, aRadii, mClipChain);
  }

  void ClipContentDescendants(const nsRect& aRect,
                              const nscoord* aRadii = nullptr) {
    NS_ASSERTION(!mRestored, "Already restored!");
    NS_ASSERTION(!mClipUsed, "mClip already used");
#ifdef DEBUG
    mClipUsed = true;
#endif
    mState.ClipContentDescendants(mBuilder, aRect, aRadii, mClipChain);
  }

  void ClipContentDescendants(const nsRect& aRect, const nsRect& aRoundedRect,
                              const nscoord* aRadii = nullptr) {
    NS_ASSERTION(!mRestored, "Already restored!");
    NS_ASSERTION(!mClipUsed, "mClip already used");
#ifdef DEBUG
    mClipUsed = true;
#endif
    mState.ClipContentDescendants(mBuilder, aRect, aRoundedRect, aRadii,
                                  mClipChain);
  }

  /**
   * Clips containing-block descendants to the frame's content-box,
   * taking border-radius into account.
   * If aFlags contains ASSUME_DRAWING_RESTRICTED_TO_CONTENT_RECT then
   * we assume display items will not draw outside the content rect, so
   * clipping is only required if there is a border-radius. This is an
   * optimization to reduce the amount of clipping required.
   */
  void ClipContainingBlockDescendantsToContentBox(
      nsDisplayListBuilder* aBuilder, nsIFrame* aFrame, uint32_t aFlags = 0) {
    NS_ASSERTION(!mRestored, "Already restored!");
    NS_ASSERTION(!mClipUsed, "mClip already used");
#ifdef DEBUG
    mClipUsed = true;
#endif
    mState.ClipContainingBlockDescendantsToContentBox(aBuilder, aFrame,
                                                      mClipChain, aFlags);
  }

  void SetClippedToDisplayPort() { mState.SetClippedToDisplayPort(); }
  bool IsClippedToDisplayPort() const {
    return mState.IsClippedToDisplayPort();
  }

 protected:
  nsDisplayListBuilder* mBuilder;
  DisplayListClipState& mState;
  DisplayListClipState mSavedState;
  DisplayItemClipChain mClipChain;
#ifdef DEBUG
  bool mClipUsed;
  bool mRestored;
#endif
};

class DisplayListClipState::AutoClipContainingBlockDescendantsToContentBox
    : public AutoSaveRestore {
 public:
  AutoClipContainingBlockDescendantsToContentBox(nsDisplayListBuilder* aBuilder,
                                                 nsIFrame* aFrame,
                                                 uint32_t aFlags = 0)
      : AutoSaveRestore(aBuilder) {
#ifdef DEBUG
    mClipUsed = true;
#endif
    mState.ClipContainingBlockDescendantsToContentBox(aBuilder, aFrame,
                                                      mClipChain, aFlags);
  }
};

/**
 * Do not use this outside of nsIFrame::BuildDisplayListForChild, use
 * multiple AutoSaveRestores instead. We provide this class just to ensure
 * BuildDisplayListForChild is as efficient as possible.
 */
class DisplayListClipState::AutoClipMultiple : public AutoSaveRestore {
 public:
  explicit AutoClipMultiple(nsDisplayListBuilder* aBuilder)
      : AutoSaveRestore(aBuilder)
#ifdef DEBUG
        ,
        mExtraClipUsed(false)
#endif
  {
  }

  /**
   * Intersects the given clip rect (with optional aRadii) with the current
   * mClipContainingBlockDescendants and sets mClipContainingBlockDescendants to
   * the result, stored in aClipOnStack.
   */
  void ClipContainingBlockDescendantsExtra(const nsRect& aRect,
                                           const nscoord* aRadii) {
    NS_ASSERTION(!mRestored, "Already restored!");
    NS_ASSERTION(!mExtraClipUsed, "mExtraClip already used");
#ifdef DEBUG
    mExtraClipUsed = true;
#endif
    mState.ClipContainingBlockDescendants(mBuilder, aRect, aRadii,
                                          mExtraClipChain);
  }

 protected:
  DisplayItemClipChain mExtraClipChain;
#ifdef DEBUG
  bool mExtraClipUsed;
#endif
};

}  // namespace mozilla

#endif /* DISPLAYLISTCLIPSTATE_H_ */