File: InvalidatableInterpolation.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (284 lines) | stat: -rw-r--r-- 11,312 bytes parent folder | download
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
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "core/animation/InvalidatableInterpolation.h"

#include "core/animation/InterpolationEnvironment.h"
#include "core/animation/StringKeyframe.h"
#include "core/css/resolver/StyleResolverState.h"
#include <memory>

namespace blink {

void InvalidatableInterpolation::interpolate(int, double fraction) {
  if (fraction == m_currentFraction)
    return;

  if (m_currentFraction == 0 || m_currentFraction == 1 || fraction == 0 ||
      fraction == 1) {
    clearConversionCache();
  }

  m_currentFraction = fraction;
  if (m_isConversionCached && m_cachedPairConversion)
    m_cachedPairConversion->interpolateValue(fraction, m_cachedValue);
  // We defer the interpolation to ensureValidConversion() if
  // m_cachedPairConversion is null.
}

std::unique_ptr<PairwisePrimitiveInterpolation>
InvalidatableInterpolation::maybeConvertPairwise(
    const InterpolationEnvironment& environment,
    const UnderlyingValueOwner& underlyingValueOwner) const {
  DCHECK(m_currentFraction != 0 && m_currentFraction != 1);
  for (const auto& interpolationType : *m_interpolationTypes) {
    if ((m_startKeyframe->isNeutral() || m_endKeyframe->isNeutral()) &&
        (!underlyingValueOwner ||
         underlyingValueOwner.type() != *interpolationType))
      continue;
    ConversionCheckers conversionCheckers;
    PairwiseInterpolationValue result = interpolationType->maybeConvertPairwise(
        *m_startKeyframe, *m_endKeyframe, environment,
        underlyingValueOwner.value(), conversionCheckers);
    addConversionCheckers(*interpolationType, conversionCheckers);
    if (result) {
      return PairwisePrimitiveInterpolation::create(
          *interpolationType, std::move(result.startInterpolableValue),
          std::move(result.endInterpolableValue),
          std::move(result.nonInterpolableValue));
    }
  }
  return nullptr;
}

std::unique_ptr<TypedInterpolationValue>
InvalidatableInterpolation::convertSingleKeyframe(
    const PropertySpecificKeyframe& keyframe,
    const InterpolationEnvironment& environment,
    const UnderlyingValueOwner& underlyingValueOwner) const {
  if (keyframe.isNeutral() && !underlyingValueOwner)
    return nullptr;
  for (const auto& interpolationType : *m_interpolationTypes) {
    if (keyframe.isNeutral() &&
        underlyingValueOwner.type() != *interpolationType)
      continue;
    ConversionCheckers conversionCheckers;
    InterpolationValue result = interpolationType->maybeConvertSingle(
        keyframe, environment, underlyingValueOwner.value(),
        conversionCheckers);
    addConversionCheckers(*interpolationType, conversionCheckers);
    if (result) {
      return TypedInterpolationValue::create(
          *interpolationType, std::move(result.interpolableValue),
          std::move(result.nonInterpolableValue));
    }
  }
  DCHECK(keyframe.isNeutral());
  return nullptr;
}

void InvalidatableInterpolation::addConversionCheckers(
    const InterpolationType& type,
    ConversionCheckers& conversionCheckers) const {
  for (size_t i = 0; i < conversionCheckers.size(); i++) {
    conversionCheckers[i]->setType(type);
    m_conversionCheckers.push_back(std::move(conversionCheckers[i]));
  }
}

std::unique_ptr<TypedInterpolationValue>
InvalidatableInterpolation::maybeConvertUnderlyingValue(
    const InterpolationEnvironment& environment) const {
  for (const auto& interpolationType : *m_interpolationTypes) {
    InterpolationValue result =
        interpolationType->maybeConvertUnderlyingValue(environment);
    if (result) {
      return TypedInterpolationValue::create(
          *interpolationType, std::move(result.interpolableValue),
          std::move(result.nonInterpolableValue));
    }
  }
  return nullptr;
}

bool InvalidatableInterpolation::dependsOnUnderlyingValue() const {
  return (m_startKeyframe->underlyingFraction() != 0 &&
          m_currentFraction != 1) ||
         (m_endKeyframe->underlyingFraction() != 0 && m_currentFraction != 0);
}

bool InvalidatableInterpolation::isNeutralKeyframeActive() const {
  return (m_startKeyframe->isNeutral() && m_currentFraction != 1) ||
         (m_endKeyframe->isNeutral() && m_currentFraction != 0);
}

void InvalidatableInterpolation::clearConversionCache() const {
  m_isConversionCached = false;
  m_cachedPairConversion.reset();
  m_conversionCheckers.clear();
  m_cachedValue.reset();
}

bool InvalidatableInterpolation::isConversionCacheValid(
    const InterpolationEnvironment& environment,
    const UnderlyingValueOwner& underlyingValueOwner) const {
  if (!m_isConversionCached)
    return false;
  if (isNeutralKeyframeActive()) {
    if (m_cachedPairConversion && m_cachedPairConversion->isFlip())
      return false;
    // Pairwise interpolation can never happen between different
    // InterpolationTypes, neutral values always represent the underlying value.
    if (!underlyingValueOwner || !m_cachedValue ||
        m_cachedValue->type() != underlyingValueOwner.type())
      return false;
  }
  for (const auto& checker : m_conversionCheckers) {
    if (!checker->isValid(environment, underlyingValueOwner.value()))
      return false;
  }
  return true;
}

const TypedInterpolationValue*
InvalidatableInterpolation::ensureValidConversion(
    const InterpolationEnvironment& environment,
    const UnderlyingValueOwner& underlyingValueOwner) const {
  DCHECK(!std::isnan(m_currentFraction));
  DCHECK(m_interpolationTypes &&
         m_interpolationTypesVersion ==
             environment.interpolationTypesMap().version());
  if (isConversionCacheValid(environment, underlyingValueOwner))
    return m_cachedValue.get();
  clearConversionCache();
  if (m_currentFraction == 0) {
    m_cachedValue = convertSingleKeyframe(*m_startKeyframe, environment,
                                          underlyingValueOwner);
  } else if (m_currentFraction == 1) {
    m_cachedValue = convertSingleKeyframe(*m_endKeyframe, environment,
                                          underlyingValueOwner);
  } else {
    std::unique_ptr<PairwisePrimitiveInterpolation> pairwiseConversion =
        maybeConvertPairwise(environment, underlyingValueOwner);
    if (pairwiseConversion) {
      m_cachedValue = pairwiseConversion->initialValue();
      m_cachedPairConversion = std::move(pairwiseConversion);
    } else {
      m_cachedPairConversion = FlipPrimitiveInterpolation::create(
          convertSingleKeyframe(*m_startKeyframe, environment,
                                underlyingValueOwner),
          convertSingleKeyframe(*m_endKeyframe, environment,
                                underlyingValueOwner));
    }
    m_cachedPairConversion->interpolateValue(m_currentFraction, m_cachedValue);
  }
  m_isConversionCached = true;
  return m_cachedValue.get();
}

void InvalidatableInterpolation::ensureValidInterpolationTypes(
    const InterpolationEnvironment& environment) const {
  const InterpolationTypesMap& map = environment.interpolationTypesMap();
  size_t latestVersion = map.version();
  if (m_interpolationTypes && m_interpolationTypesVersion == latestVersion) {
    return;
  }
  const InterpolationTypes* latestInterpolationTypes = &map.get(m_property);
  DCHECK(latestInterpolationTypes);
  if (m_interpolationTypes != latestInterpolationTypes) {
    clearConversionCache();
  }
  m_interpolationTypes = latestInterpolationTypes;
  m_interpolationTypesVersion = latestVersion;
}

void InvalidatableInterpolation::setFlagIfInheritUsed(
    InterpolationEnvironment& environment) const {
  if (!m_property.isCSSProperty() && !m_property.isPresentationAttribute())
    return;
  if (!environment.state().parentStyle())
    return;
  const CSSValue* startValue =
      toCSSPropertySpecificKeyframe(*m_startKeyframe).value();
  const CSSValue* endValue =
      toCSSPropertySpecificKeyframe(*m_endKeyframe).value();
  if ((startValue && startValue->isInheritedValue()) ||
      (endValue && endValue->isInheritedValue())) {
    environment.state().parentStyle()->setHasExplicitlyInheritedProperties();
  }
}

double InvalidatableInterpolation::underlyingFraction() const {
  if (m_currentFraction == 0)
    return m_startKeyframe->underlyingFraction();
  if (m_currentFraction == 1)
    return m_endKeyframe->underlyingFraction();
  return m_cachedPairConversion->interpolateUnderlyingFraction(
      m_startKeyframe->underlyingFraction(),
      m_endKeyframe->underlyingFraction(), m_currentFraction);
}

void InvalidatableInterpolation::applyStack(
    const ActiveInterpolations& interpolations,
    InterpolationEnvironment& environment) {
  DCHECK(!interpolations.isEmpty());
  size_t startingIndex = 0;

  // Compute the underlying value to composite onto.
  UnderlyingValueOwner underlyingValueOwner;
  const InvalidatableInterpolation& firstInterpolation =
      toInvalidatableInterpolation(*interpolations.at(startingIndex));
  firstInterpolation.ensureValidInterpolationTypes(environment);
  if (firstInterpolation.dependsOnUnderlyingValue()) {
    underlyingValueOwner.set(
        firstInterpolation.maybeConvertUnderlyingValue(environment));
  } else {
    const TypedInterpolationValue* firstValue =
        firstInterpolation.ensureValidConversion(environment,
                                                 underlyingValueOwner);
    // Fast path for replace interpolations that are the only one to apply.
    if (interpolations.size() == 1) {
      if (firstValue) {
        firstInterpolation.setFlagIfInheritUsed(environment);
        firstValue->type().apply(firstValue->interpolableValue(),
                                 firstValue->getNonInterpolableValue(),
                                 environment);
      }
      return;
    }
    underlyingValueOwner.set(firstValue);
    startingIndex++;
  }

  // Composite interpolations onto the underlying value.
  bool shouldApply = false;
  for (size_t i = startingIndex; i < interpolations.size(); i++) {
    const InvalidatableInterpolation& currentInterpolation =
        toInvalidatableInterpolation(*interpolations.at(i));
    DCHECK(currentInterpolation.dependsOnUnderlyingValue());
    currentInterpolation.ensureValidInterpolationTypes(environment);
    const TypedInterpolationValue* currentValue =
        currentInterpolation.ensureValidConversion(environment,
                                                   underlyingValueOwner);
    if (!currentValue)
      continue;
    shouldApply = true;
    currentInterpolation.setFlagIfInheritUsed(environment);
    double underlyingFraction = currentInterpolation.underlyingFraction();
    if (underlyingFraction == 0 || !underlyingValueOwner ||
        underlyingValueOwner.type() != currentValue->type())
      underlyingValueOwner.set(currentValue);
    else
      currentValue->type().composite(underlyingValueOwner, underlyingFraction,
                                     currentValue->value(),
                                     currentInterpolation.m_currentFraction);
  }

  if (shouldApply && underlyingValueOwner)
    underlyingValueOwner.type().apply(
        *underlyingValueOwner.value().interpolableValue,
        underlyingValueOwner.value().nonInterpolableValue.get(), environment);
}

}  // namespace blink