SkInterpolator.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright 2006 The Android Open Source Project
  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. #ifndef SkInterpolator_DEFINED
  8. #define SkInterpolator_DEFINED
  9. #include "include/core/SkScalar.h"
  10. #include "include/private/SkNoncopyable.h"
  11. #include "include/private/SkTo.h"
  12. class SK_API SkInterpolatorBase : SkNoncopyable {
  13. public:
  14. enum Result {
  15. kNormal_Result,
  16. kFreezeStart_Result,
  17. kFreezeEnd_Result
  18. };
  19. protected:
  20. SkInterpolatorBase();
  21. ~SkInterpolatorBase();
  22. public:
  23. void reset(int elemCount, int frameCount);
  24. /** Return the start and end time for this interpolator.
  25. If there are no key frames, return false.
  26. @param startTime If not null, returns the time (in milliseconds) of the
  27. first keyframe. If there are no keyframes, this param
  28. is ignored (left unchanged).
  29. @param endTime If not null, returns the time (in milliseconds) of the
  30. last keyframe. If there are no keyframes, this parameter
  31. is ignored (left unchanged).
  32. @return True if there are key frames, or false if there are none.
  33. */
  34. bool getDuration(SkMSec* startTime, SkMSec* endTime) const;
  35. /** Set the whether the repeat is mirrored.
  36. @param mirror If true, the odd repeats interpolate from the last key
  37. frame and the first.
  38. */
  39. void setMirror(bool mirror) {
  40. fFlags = SkToU8((fFlags & ~kMirror) | (int)mirror);
  41. }
  42. /** Set the repeat count. The repeat count may be fractional.
  43. @param repeatCount Multiplies the total time by this scalar.
  44. */
  45. void setRepeatCount(SkScalar repeatCount) { fRepeat = repeatCount; }
  46. /** Set the whether the repeat is mirrored.
  47. @param reset If true, the odd repeats interpolate from the last key
  48. frame and the first.
  49. */
  50. void setReset(bool reset) {
  51. fFlags = SkToU8((fFlags & ~kReset) | (int)reset);
  52. }
  53. Result timeToT(SkMSec time, SkScalar* T, int* index, bool* exact) const;
  54. protected:
  55. enum Flags {
  56. kMirror = 1,
  57. kReset = 2,
  58. kHasBlend = 4
  59. };
  60. static SkScalar ComputeRelativeT(SkMSec time, SkMSec prevTime, SkMSec nextTime,
  61. const SkScalar blend[4] = nullptr);
  62. int16_t fFrameCount;
  63. uint8_t fElemCount;
  64. uint8_t fFlags;
  65. SkScalar fRepeat;
  66. struct SkTimeCode {
  67. SkMSec fTime;
  68. SkScalar fBlend[4];
  69. };
  70. SkTimeCode* fTimes; // pointer into fStorage
  71. void* fStorage;
  72. #ifdef SK_DEBUG
  73. SkTimeCode(* fTimesArray)[10];
  74. #endif
  75. };
  76. class SK_API SkInterpolator : public SkInterpolatorBase {
  77. public:
  78. SkInterpolator();
  79. SkInterpolator(int elemCount, int frameCount);
  80. void reset(int elemCount, int frameCount);
  81. /** Add or replace a key frame, copying the values[] data into the
  82. interpolator.
  83. @param index The index of this frame (frames must be ordered by time)
  84. @param time The millisecond time for this frame
  85. @param values The array of values [elemCount] for this frame. The data
  86. is copied into the interpolator.
  87. @param blend A positive scalar specifying how to blend between this
  88. and the next key frame. [0...1) is a cubic lag/log/lag
  89. blend (slow to change at the beginning and end)
  90. 1 is a linear blend (default)
  91. */
  92. bool setKeyFrame(int index, SkMSec time, const SkScalar values[],
  93. const SkScalar blend[4] = nullptr);
  94. /** Return the computed values given the specified time. Return whether
  95. those values are the result of pinning to either the first
  96. (kFreezeStart) or last (kFreezeEnd), or from interpolated the two
  97. nearest key values (kNormal).
  98. @param time The time to sample (in milliseconds)
  99. @param (may be null) where to write the computed values.
  100. */
  101. Result timeToValues(SkMSec time, SkScalar values[] = nullptr) const;
  102. private:
  103. SkScalar* fValues; // pointer into fStorage
  104. #ifdef SK_DEBUG
  105. SkScalar(* fScalarsArray)[10];
  106. #endif
  107. typedef SkInterpolatorBase INHERITED;
  108. };
  109. /** Interpolate a cubic curve, typically to provide an ease-in ease-out transition.
  110. All the parameters are in the range of [0...1].
  111. The input value is treated as the x-coordinate of the cubic.
  112. The output value is the y-coordinate on the cubic at the x-coordinate.
  113. @param value The x-coordinate pinned between [0..1].
  114. @param bx,by,cx,cy The cubic control points where the cubic is specified
  115. as (0,0) (bx,by) (cx,cy) (1,1)
  116. @return the corresponding y-coordinate value, from [0..1].
  117. */
  118. SkScalar SkUnitCubicInterp(SkScalar value, SkScalar bx, SkScalar by,
  119. SkScalar cx, SkScalar cy);
  120. #endif