blob: efe2d81762d821bab2a2c85f241ae1c746c12a62 [file] [log] [blame]
Brian Osman7c979f52019-02-12 13:27:51 -05001/*
2* Copyright 2019 Google LLC
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
8#ifndef SkCurve_DEFINED
9#define SkCurve_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/SkTArray.h"
13#include "modules/particles/include/SkParticleData.h"
Brian Osman7c979f52019-02-12 13:27:51 -050014
15class SkFieldVisitor;
Brian Osman7c979f52019-02-12 13:27:51 -050016
Brian Osmane12e4992019-02-19 15:39:18 -050017/**
18 * SkCurve implements a keyframed 1D function, useful for animating values over time. This pattern
19 * is common in digital content creation tools. An SkCurve might represent rotation, scale, opacity,
20 * or any other scalar quantity.
21 *
22 * An SkCurve has a logical domain of [0, 1], and is made of one or more SkCurveSegments.
23 * Each segment describes the behavior of the curve in some sub-domain. For an SkCurve with N
24 * segments, there are (N - 1) intermediate x-values that subdivide the domain. The first and last
25 * x-values are implicitly 0 and 1:
26 *
27 * 0 ... x[0] ... x[1] ... ... 1
28 * Segment_0 Segment_1 ... Segment_N-1
29 *
30 * Each segment describes a function over [0, 1] - x-values are re-normalized to the segment's
31 * domain when being evaluated. The segments are cubic polynomials, defined by four values (fMin).
32 * These are the values at x=0 and x=1, as well as control points at x=1/3 and x=2/3.
33 *
34 * For segments with fConstant == true, only the first value is used (fMin[0]).
35 *
36 * Each segment has two additional features for creating interesting (and varied) animation:
37 * - A segment can be ranged. Ranged segments have two sets of coefficients, and a random value
Brian Osmand0c1bd42019-03-06 16:56:58 -050038 * taken from the particle's SkRandom is used to lerp betwen them. Typically, the SkRandom is
39 * in the same state at each call, so this value is stable. That causes a ranged SkCurve to
40 * produce a single smooth cubic function somewhere within the range defined by fMin and fMax.
Brian Osmane12e4992019-02-19 15:39:18 -050041 * - A segment can be bidirectional. In that case, after a value is computed, it will be negated
42 * 50% of the time.
43 */
44
Brian Osman34d13312019-02-27 11:19:19 -050045enum SkCurveSegmentType {
46 kConstant_SegmentType,
47 kLinear_SegmentType,
48 kCubic_SegmentType,
49};
50
Brian Osman8b6283f2019-02-14 16:55:21 -050051struct SkCurveSegment {
Brian Osmane1cb9ac2019-06-24 10:44:02 -040052 float eval(float x, float t, bool negate) const;
Brian Osman8b6283f2019-02-14 16:55:21 -050053 void visitFields(SkFieldVisitor* v);
54
Brian Osmane1cb9ac2019-06-24 10:44:02 -040055 void setConstant(float c) {
Brian Osman34d13312019-02-27 11:19:19 -050056 fType = kConstant_SegmentType;
57 fRanged = false;
Brian Osman8b6283f2019-02-14 16:55:21 -050058 fMin[0] = c;
59 }
60
Brian Osmane1cb9ac2019-06-24 10:44:02 -040061 float fMin[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
62 float fMax[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
Brian Osman8b6283f2019-02-14 16:55:21 -050063
Brian Osman34d13312019-02-27 11:19:19 -050064 int fType = kConstant_SegmentType;
Brian Osman8b6283f2019-02-14 16:55:21 -050065 bool fRanged = false;
66 bool fBidirectional = false;
67};
Brian Osman7c979f52019-02-12 13:27:51 -050068
69struct SkCurve {
Brian Osmane1cb9ac2019-06-24 10:44:02 -040070 SkCurve(float c = 0.0f) {
Brian Osman8b6283f2019-02-14 16:55:21 -050071 fSegments.push_back().setConstant(c);
Brian Osman7c979f52019-02-12 13:27:51 -050072 }
73
Brian Osmane1cb9ac2019-06-24 10:44:02 -040074 float eval(const SkParticleUpdateParams& params, SkParticleState& ps) const;
Brian Osman7c979f52019-02-12 13:27:51 -050075 void visitFields(SkFieldVisitor* v);
Brian Osmane12e4992019-02-19 15:39:18 -050076
Brian Osmanbdcdf1a2019-03-04 10:55:22 -050077 // Parameters that determine our x-value during evaluation
78 SkParticleValue fInput;
79
Brian Osmane12e4992019-02-19 15:39:18 -050080 // It should always be true that (fXValues.count() + 1) == fSegments.count()
Brian Osmane1cb9ac2019-06-24 10:44:02 -040081 SkTArray<float, true> fXValues;
Brian Osman8b6283f2019-02-14 16:55:21 -050082 SkTArray<SkCurveSegment, true> fSegments;
Brian Osman7c979f52019-02-12 13:27:51 -050083};
84
Brian Osman125daa42019-02-20 12:25:20 -050085/**
86 * SkColorCurve is similar to SkCurve, but keyframes 4D values - specifically colors. Because
87 * negative colors rarely make sense, SkColorCurves do not support bidirectional segments, but
88 * support all other features (including cubic interpolation).
89 */
90
91struct SkColorCurveSegment {
92 SkColorCurveSegment() {
93 for (int i = 0; i < 4; ++i) {
94 fMin[i] = { 1.0f, 1.0f, 1.0f, 1.0f };
95 fMax[i] = { 1.0f, 1.0f, 1.0f, 1.0f };
96 }
97 }
98
Brian Osmane1cb9ac2019-06-24 10:44:02 -040099 SkColor4f eval(float x, float t) const;
Brian Osman125daa42019-02-20 12:25:20 -0500100 void visitFields(SkFieldVisitor* v);
101
102 void setConstant(SkColor4f c) {
Brian Osman34d13312019-02-27 11:19:19 -0500103 fType = kConstant_SegmentType;
Brian Osman125daa42019-02-20 12:25:20 -0500104 fRanged = false;
105 fMin[0] = c;
106 }
107
108 SkColor4f fMin[4];
109 SkColor4f fMax[4];
110
Brian Osman34d13312019-02-27 11:19:19 -0500111 int fType = kConstant_SegmentType;
Brian Osman125daa42019-02-20 12:25:20 -0500112 bool fRanged = false;
113};
114
115struct SkColorCurve {
116 SkColorCurve(SkColor4f c = { 1.0f, 1.0f, 1.0f, 1.0f }) {
117 fSegments.push_back().setConstant(c);
118 }
119
Brian Osmanbdcdf1a2019-03-04 10:55:22 -0500120 SkColor4f eval(const SkParticleUpdateParams& params, SkParticleState& ps) const;
Brian Osman125daa42019-02-20 12:25:20 -0500121 void visitFields(SkFieldVisitor* v);
122
Brian Osmanbdcdf1a2019-03-04 10:55:22 -0500123 SkParticleValue fInput;
Brian Osmane1cb9ac2019-06-24 10:44:02 -0400124 SkTArray<float, true> fXValues;
Brian Osman125daa42019-02-20 12:25:20 -0500125 SkTArray<SkColorCurveSegment, true> fSegments;
126};
127
Brian Osman7c979f52019-02-12 13:27:51 -0500128#endif // SkCurve_DEFINED