summaryrefslogtreecommitdiff
path: root/layout/base/DisplayListClipState.cpp
blob: 5ec065b956bf91b5180220292e12d69240cc30aa (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
/* -*- Mode: C++; tab-width: 20; 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/. */

#include "DisplayListClipState.h"

#include "DisplayItemScrollClip.h"
#include "nsDisplayList.h"

namespace mozilla {

const DisplayItemClip*
DisplayListClipState::GetCurrentCombinedClip(nsDisplayListBuilder* aBuilder)
{
  if (mCurrentCombinedClip) {
    return mCurrentCombinedClip;
  }
  if (!mClipContentDescendants && !mClipContainingBlockDescendants) {
    return nullptr;
  }
  if (mClipContentDescendants) {
    if (mClipContainingBlockDescendants) {
      DisplayItemClip intersection = *mClipContentDescendants;
      intersection.IntersectWith(*mClipContainingBlockDescendants);
      mCurrentCombinedClip = aBuilder->AllocateDisplayItemClip(intersection);
    } else {
      mCurrentCombinedClip =
        aBuilder->AllocateDisplayItemClip(*mClipContentDescendants);
    }
  } else {
    mCurrentCombinedClip =
      aBuilder->AllocateDisplayItemClip(*mClipContainingBlockDescendants);
  }
  return mCurrentCombinedClip;
}

void
DisplayListClipState::SetScrollClipForContainingBlockDescendants(
    nsDisplayListBuilder* aBuilder,
    const DisplayItemScrollClip* aScrollClip)
{
  if (aBuilder->IsPaintingToWindow() &&
      mClipContentDescendants &&
      aScrollClip != mScrollClipContainingBlockDescendants) {
    // Disable paint skipping for all scroll frames on the way to aScrollClip.
    for (const DisplayItemScrollClip* sc = mClipContentDescendantsScrollClip;
         sc && !DisplayItemScrollClip::IsAncestor(sc, aScrollClip);
         sc = sc->mParent) {
      if (sc->mScrollableFrame) {
        sc->mScrollableFrame->SetScrollsClipOnUnscrolledOutOfFlow();
      }
    }
    mClipContentDescendantsScrollClip = nullptr;
  }
  mScrollClipContainingBlockDescendants = aScrollClip;
  mStackingContextAncestorSC = DisplayItemScrollClip::PickAncestor(mStackingContextAncestorSC, aScrollClip);
}

void
DisplayListClipState::ClipContainingBlockDescendants(const nsRect& aRect,
                                                     const nscoord* aRadii,
                                                     DisplayItemClip& aClipOnStack)
{
  if (aRadii) {
    aClipOnStack.SetTo(aRect, aRadii);
  } else {
    aClipOnStack.SetTo(aRect);
  }
  if (mClipContainingBlockDescendants) {
    aClipOnStack.IntersectWith(*mClipContainingBlockDescendants);
  }
  mClipContainingBlockDescendants = &aClipOnStack;
  mClipContentDescendantsScrollClip = GetCurrentInnermostScrollClip();
  mCurrentCombinedClip = nullptr;
}

void
DisplayListClipState::ClipContentDescendants(const nsRect& aRect,
                                             const nscoord* aRadii,
                                             DisplayItemClip& aClipOnStack)
{
  if (aRadii) {
    aClipOnStack.SetTo(aRect, aRadii);
  } else {
    aClipOnStack.SetTo(aRect);
  }
  if (mClipContentDescendants) {
    aClipOnStack.IntersectWith(*mClipContentDescendants);
  }
  mClipContentDescendants = &aClipOnStack;
  mCurrentCombinedClip = nullptr;
}

void
DisplayListClipState::ClipContentDescendants(const nsRect& aRect,
                                             const nsRect& aRoundedRect,
                                             const nscoord* aRadii,
                                             DisplayItemClip& aClipOnStack)
{
  if (aRadii) {
    aClipOnStack.SetTo(aRect, aRoundedRect, aRadii);
  } else {
    nsRect intersect = aRect.Intersect(aRoundedRect);
    aClipOnStack.SetTo(intersect);
  }
  if (mClipContentDescendants) {
    aClipOnStack.IntersectWith(*mClipContentDescendants);
  }
  mClipContentDescendants = &aClipOnStack;
  mCurrentCombinedClip = nullptr;
}

void
DisplayListClipState::ClipContainingBlockDescendantsToContentBox(nsDisplayListBuilder* aBuilder,
                                                                 nsIFrame* aFrame,
                                                                 DisplayItemClip& aClipOnStack,
                                                                 uint32_t aFlags)
{
  nscoord radii[8];
  bool hasBorderRadius = aFrame->GetContentBoxBorderRadii(radii);
  if (!hasBorderRadius && (aFlags & ASSUME_DRAWING_RESTRICTED_TO_CONTENT_RECT)) {
    return;
  }

  nsRect clipRect = aFrame->GetContentRectRelativeToSelf() +
    aBuilder->ToReferenceFrame(aFrame);
  // If we have a border-radius, we have to clip our content to that
  // radius.
  ClipContainingBlockDescendants(clipRect, hasBorderRadius ? radii : nullptr,
                                 aClipOnStack);
}

const DisplayItemScrollClip*
DisplayListClipState::GetCurrentInnermostScrollClip()
{
  return DisplayItemScrollClip::PickDescendant(
    mScrollClipContentDescendants, mScrollClipContainingBlockDescendants);
}

void
DisplayListClipState::TurnClipIntoScrollClipForContentDescendants(
    nsDisplayListBuilder* aBuilder, nsIScrollableFrame* aScrollableFrame)
{
  const DisplayItemScrollClip* parent = GetCurrentInnermostScrollClip();
  mScrollClipContentDescendants =
    aBuilder->AllocateDisplayItemScrollClip(parent,
                                      aScrollableFrame,
                                      GetCurrentCombinedClip(aBuilder), true);
  Clear();
}

void
DisplayListClipState::TurnClipIntoScrollClipForContainingBlockDescendants(
    nsDisplayListBuilder* aBuilder, nsIScrollableFrame* aScrollableFrame)
{
  const DisplayItemScrollClip* parent = GetCurrentInnermostScrollClip();
  mScrollClipContainingBlockDescendants =
    aBuilder->AllocateDisplayItemScrollClip(parent,
                                      aScrollableFrame,
                                      GetCurrentCombinedClip(aBuilder), true);
  Clear();
}

const DisplayItemClip*
WithoutRoundedCorners(nsDisplayListBuilder* aBuilder, const DisplayItemClip* aClip)
{
  if (!aClip) {
    return nullptr;
  }
  DisplayItemClip rectClip(*aClip);
  rectClip.RemoveRoundedCorners();
  return aBuilder->AllocateDisplayItemClip(rectClip);
}

DisplayItemScrollClip*
DisplayListClipState::CreateInactiveScrollClip(
    nsDisplayListBuilder* aBuilder, nsIScrollableFrame* aScrollableFrame)
{
  // We ignore the rounded corners on the current clip because we don't want
  // them to be double-applied (as scroll clip and as regular clip).
  // Double-applying rectangle clips doesn't make a visual difference so it's
  // fine.
  const DisplayItemClip* rectClip =
    WithoutRoundedCorners(aBuilder, GetCurrentCombinedClip(aBuilder));

  const DisplayItemScrollClip* parent = GetCurrentInnermostScrollClip();
  DisplayItemScrollClip* scrollClip =
    aBuilder->AllocateDisplayItemScrollClip(parent,
                                            aScrollableFrame,
                                            rectClip, false);
  return scrollClip;
}

DisplayItemScrollClip*
DisplayListClipState::InsertInactiveScrollClipForContentDescendants(
    nsDisplayListBuilder* aBuilder, nsIScrollableFrame* aScrollableFrame)
{
  DisplayItemScrollClip* scrollClip =
    CreateInactiveScrollClip(aBuilder, aScrollableFrame);
  mScrollClipContentDescendants = scrollClip;
  return scrollClip;
}

DisplayItemScrollClip*
DisplayListClipState::InsertInactiveScrollClipForContainingBlockDescendants(
    nsDisplayListBuilder* aBuilder, nsIScrollableFrame* aScrollableFrame)
{
  DisplayItemScrollClip* scrollClip =
    CreateInactiveScrollClip(aBuilder, aScrollableFrame);
  mScrollClipContainingBlockDescendants = scrollClip;
  return scrollClip;
}

DisplayListClipState::AutoSaveRestore::AutoSaveRestore(nsDisplayListBuilder* aBuilder)
  : mState(aBuilder->ClipState())
  , mSavedState(aBuilder->ClipState())
#ifdef DEBUG
  , mClipUsed(false)
  , mRestored(false)
#endif
  , mClearedForStackingContextContents(false)
{
  mState.mStackingContextAncestorSC = mState.GetCurrentInnermostScrollClip();
}

} // namespace mozilla